summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns_tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'project2/proj2_s4498062/dns_tests.py')
-rwxr-xr-xproject2/proj2_s4498062/dns_tests.py68
1 files changed, 61 insertions, 7 deletions
diff --git a/project2/proj2_s4498062/dns_tests.py b/project2/proj2_s4498062/dns_tests.py
index 26bc00b..4a054b6 100755
--- a/project2/proj2_s4498062/dns_tests.py
+++ b/project2/proj2_s4498062/dns_tests.py
@@ -1,16 +1,70 @@
#!/usr/bin/env python2
+""" Tests for the DNS resolver and server """
-""" Tests for your DNS resolver and server """
+import sys
+import time
+import unittest
-portnr = 5353
-server = "localhost"
+from dns.cache import RecordCache
+from dns.classes import Class
+from dns.resolver import Resolver
+from dns.resource import ResourceRecord, CNAMERecordData
+from dns.types import Type
+
+portnr = 5300
+server = '127.0.0.1'
class TestResolver(unittest.TestCase):
- pass
+ """Test cases for the resolver with caching disabled"""
+
+ def setUp(self):
+ self.resolv = Resolver(False, 0)
+
+ def test_solve(self):
+ """Test solving some FQDN"""
+ host, aliases, addrs = self.resolv.gethostbyname('camilstaps.nl')
+ self.assertEqual(host, 'camilstaps.nl')
+ self.assertEqual(aliases, [])
+ self.assertEqual(addrs, ['84.22.111.158'])
+
+ def test_nonexistant(self):
+ """Test solving a nonexistant FQDN"""
+ host, aliases, addrs = self.resolv.gethostbyname('nothing.ru.nl')
+ self.assertEqual(host, 'nothing.ru.nl')
+ self.assertEqual(aliases, [])
+ self.assertEqual(addrs, [])
class TestResolverCache(unittest.TestCase):
- pass
+ """Test cases for the resolver with caching enabled"""
+
+ TTL = 3
+
+ def setup_resolver(self):
+ """Setup a resolver with an invalid cache"""
+ self.resolv = Resolver(True, self.TTL)
+ self.cache = RecordCache(self.TTL)
+ self.cache.add_record(ResourceRecord(
+ 'nothing.ru.nl', Type.CNAME, Class.IN, self.TTL,
+ CNAMERecordData('camilstaps.nl')))
+ self.resolv.cache = self.cache
+
+ def test_solve_invalid(self):
+ """Test solving an invalid cached FQDN"""
+ self.setup_resolver()
+ host, aliases, addrs = self.resolv.gethostbyname('nothing.ru.nl')
+ self.assertEqual(host, 'camilstaps.nl')
+ self.assertEqual(aliases, [])
+ self.assertEqual(addrs, ['84.22.111.158'])
+
+ def test_solve_invalid_after_expiration(self):
+ """Test solving an invalid cached FQDN after TTL expiration"""
+ self.setup_resolver()
+ time.sleep(self.TTL + 1)
+ host, aliases, addrs = self.resolv.gethostbyname('nothing.ru.nl')
+ self.assertEqual(host, 'nothing.ru.nl')
+ self.assertEqual(aliases, [])
+ self.assertEqual(addrs, [])
class TestServer(unittest.TestCase):
@@ -22,11 +76,11 @@ if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="HTTP Tests")
parser.add_argument("-s", "--server", type=str, default="localhost")
- parser.add_argument("-p", "--port", type=int, default=5001)
+ parser.add_argument("-p", "--port", type=int, default=5300)
args, extra = parser.parse_known_args()
portnr = args.port
server = args.server
-
+
# Pass the extra arguments to unittest
sys.argv[1:] = extra