diff options
Diffstat (limited to 'project2/proj2_s4498062/dns_tests.py')
-rwxr-xr-x | project2/proj2_s4498062/dns_tests.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/project2/proj2_s4498062/dns_tests.py b/project2/proj2_s4498062/dns_tests.py index 6a5a16e..fe4ffc2 100755 --- a/project2/proj2_s4498062/dns_tests.py +++ b/project2/proj2_s4498062/dns_tests.py @@ -3,6 +3,7 @@ # pylint: disable=too-many-public-methods, invalid-name +import copy from random import randint import socket import sys @@ -143,6 +144,53 @@ class TestServer(unittest.TestCase): elif record.type_ == Type.CNAME: self.assertIn(record.rdata.data, aliases) + def test_concurrency(self): + """Test the server's concurrent abilities + + Note: we can never be sure that the server handles these requests in + parallel. What we do is (1) sending a request for a domain outside the + zone, and (2) sending a request for inside the zone. We would expect + the latter to be quicker. + """ + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.settimeout(self.TIMEOUT) + + header = Header(0, 0, 1, 0, 0, 0) + header.qr = 0 + header.opcode = 0 + header.rd = 1 + + header.ident = 1 + req1 = Message(header, [Question('camilstaps.nl', Type.A, Class.IN)]) + + header = copy.copy(header) + header.ident = 2 + req2 = Message(header, [Question('cloogle.org', Type.A, Class.IN)]) + + sock.sendto(req1.to_bytes(), ('127.0.0.1', portnr)) + sock.sendto(req2.to_bytes(), ('127.0.0.1', portnr)) + + try: + # The second request + data = sock.recv(512) + resp = Message.from_bytes(data) + self.assertEqual(resp.header.ident, 2) + self.assert_match( + resp.authorities[0], + 'cloogle.org', Type.A, Class.IN, '84.22.111.158') + self.assertEqual(resp.answers + resp.additionals, []) + + # The first request + data = sock.recv(512) + resp = Message.from_bytes(data) + self.assertEqual(resp.header.ident, 1) + self.assert_match( + resp.answers[0], + 'camilstaps.nl', Type.A, Class.IN, '84.22.111.158') + self.assertEqual(resp.authorities + resp.additionals, []) + except socket.timeout: + self.fail('timeout') + if __name__ == "__main__": # Parse command line arguments import argparse |