Conghao97's picture tcftrees's picture
ProgramGen release
d086025
Raw History Blame Contribute Delete
7.36 kB
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,1 @@
+"""localzone test package initialization"""
diff --git a/tests/test_context.py b/tests/test_context.py
new file mode 100644
--- /dev/null
+++ b/tests/test_context.py
@@ -0,0 +1,29 @@
+import pytest
+import localzone
+try:
+ from dns.zonefile import UnknownOrigin
+except ImportError:
+ from dns.zone import UnknownOrigin
+
+ZONEFILE = "tests/zonefiles/db.example.com"
+ORIGIN = "example.com."
+TTL = 3600
+
+
+def test_load():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ assert z.filename == ZONEFILE
+ assert z.ttl == TTL
+ assert len(z.records) == 16
+
+
+def test_load_missing_origin():
+ with pytest.raises(UnknownOrigin):
+ localzone.load("tests/zonefiles/db.no-origin.com")
+
+
+def test_manage():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ assert z.filename == ZONEFILE
+ assert z.ttl == TTL
+ assert len(z.records) == 16
diff --git a/tests/test_models.py b/tests/test_models.py
new file mode 100644
--- /dev/null
+++ b/tests/test_models.py
@@ -0,0 +1,126 @@
+from dns.rdatatype import UnknownRdatatype
+from dns.exception import SyntaxError as DNSSyntaxError
+import pytest
+import localzone
+
+ZONEFILE = "tests/zonefiles/db.example.com"
+ORIGIN = "example.com."
+HASHID = "dd03d449"
+
+#
+# readers
+#
+
+
+def test_get_all_records():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ records = z.get_records("ANY")
+ assert len(records) == 16
+
+
+def test_get_record():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ record = z.get_record(HASHID)
+ assert record.name == "@"
+ assert record.rdtype == "A"
+ assert record.content == "192.0.2.1"
+ assert record.ttl == 3600
+
+
+def test_get_record_not_found():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ with pytest.raises(KeyError):
+ z.get_record("deadbeef")
+
+
+def test_find_records_by_type():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ records = z.find_record("MX")
+ assert len(records) == 3
+
+
+def test_find_record_by_name():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ records = z.find_record("CNAME", "www")
+ assert len(records) == 1
+ assert records[0].content == "@"
+
+
+def test_find_record_by_content():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ records = z.find_record("A", content="192.0.2.2")
+ assert len(records) == 1
+ assert records[0].name == "ns"
+
+
+def test_zone_records_property():
+ z = localzone.load(ZONEFILE, ORIGIN)
+ records = z.records
+ assert len(records) == 16
+
+
+#
+# writers
+#
+
+
+def test_zone_add_record():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ record = z.add_record("test", "txt", "testing")
+ assert record.hashid == "28c9e108"
+
+
+def test_zone_add_record_unknown_type():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ with pytest.raises(UnknownRdatatype):
+ z.add_record("test", "err", "testing")
+
+
+def test_zone_add_record_no_content():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ with pytest.raises((AttributeError, DNSSyntaxError, ValueError)):
+ z.add_record("test", "txt", None)
+
+
+def test_zone_remove_record():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ z.remove_record(HASHID)
+ with pytest.raises(KeyError):
+ z.get_record(HASHID)
+
+
+def test_zone_remove_record_not_found():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ with pytest.raises(KeyError):
+ z.remove_record("deadbeef")
+
+
+def test_zone_update_record():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ record = z.update_record(HASHID, "192.0.2.100")
+ assert record.hashid == "117e047a"
+ assert record.name == "@"
+ assert record.rdtype == "A"
+ assert record.content == "192.0.2.100"
+ assert record.ttl == 3600
+
+
+def test_zone_update_record_not_found():
+ with localzone.manage(ZONEFILE, ORIGIN) as z:
+ with pytest.raises(KeyError):
+ z.update_record("deadbeef", "eat mor chikin")
+
+
+def test_zone_save():
+ savefile = "tests/db.example.com.saved"
+ z = localzone.load(ZONEFILE, ORIGIN)
+ serial = z.soa.rdata.serial
+ record = z.update_record(HASHID, "192.0.2.100")
+ z.save(savefile)
+ z = localzone.load(savefile, ORIGIN)
+ record = z.find_record("A", content="192.0.2.100")[0]
+ assert z.soa.rdata.serial > serial
+ assert record.name == "@"
+ assert record.rdtype == "A"
+ assert record.content == "192.0.2.100"
+ assert record.ttl == 3600
diff --git a/tests/test_util.py b/tests/test_util.py
new file mode 100644
--- /dev/null
+++ b/tests/test_util.py
@@ -0,0 +1,28 @@
+import pytest
+from localzone.util import checksum
+
+# Test vectors
+WORD = "abcde"
+CS16 = "c8f0"
+CS32 = "f04fc729"
+CS64 = "c8c6c527646362c6"
+
+
+def test_checksum_16():
+ cs = checksum(WORD, 16)
+ assert cs == CS16
+
+
+def test_checksum_32():
+ cs = checksum(WORD)
+ assert cs == CS32
+
+
+def test_checksum_64():
+ cs = checksum(WORD, 64)
+ assert cs == CS64
+
+
+def test_checksum_invalid_size():
+ with pytest.raises(ValueError):
+ cs = checksum(WORD, 128)
diff --git a/tests/zonefiles/db.example.com b/tests/zonefiles/db.example.com
new file mode 100644
--- /dev/null
+++ b/tests/zonefiles/db.example.com
@@ -0,0 +1,18 @@
+$ORIGIN example.com. ; designates the start of this zone file in the namespace
+$TTL 1h ; default expiration time of all resource records without their own TTL value
+example.com. IN SOA ns.example.com. username.example.com. ( 2007120710 1d 2h 4w 1h )
+example.com. IN NS ns ; ns.example.com is a nameserver for example.com
+example.com. IN NS ns.somewhere.example. ; ns.somewhere.example is a backup nameserver for example.com
+example.com. IN MX 10 mail.example.com. ; mail.example.com is the mailserver for example.com
+@ IN MX 20 mail2.example.com. ; equivalent to above line, "@" represents zone origin
+@ IN MX 50 mail3 ; equivalent to above line, but using a relative host name
+example.com. IN A 192.0.2.1 ; IPv4 address for example.com
+ IN AAAA 2001:db8:10::1 ; IPv6 address for example.com
+ns IN A 192.0.2.2 ; IPv4 address for ns.example.com
+ IN AAAA 2001:db8:10::2 ; IPv6 address for ns.example.com
+www IN CNAME example.com. ; www.example.com is an alias for example.com
+wwwtest IN CNAME www ; wwwtest.example.com is another alias for www.example.com
+mail IN A 192.0.2.3 ; IPv4 address for mail.example.com
+mail2 IN A 192.0.2.4 ; IPv4 address for mail2.example.com
+mail3 IN A 192.0.2.5 ; IPv4 address for mail3.example.com
+@ IN TXT "v=spf1 mx ~all" ; SPFv1 record for example.com
diff --git a/tests/zonefiles/db.no-origin.com b/tests/zonefiles/db.no-origin.com
new file mode 100644
--- /dev/null
+++ b/tests/zonefiles/db.no-origin.com
@@ -0,0 +1,2 @@
+islegit.com. IN SOA ns.islegit.com. ags.islegit.com. ( 2019010100 1d 2h 4w 1h )
+@ IN TXT "coming2019"