File size: 7,357 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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"