Debian Codegraph @

tests/test_btreezone.py

raw
1 from typing import cast
2
3 import dns.btreezone
4 import dns.rdataset
5 import dns.zone
6
7 Node = dns.btreezone.Node
8
9 simple_zone = """
10 $ORIGIN example.
11 $TTL 300
12 @ soa foo bar 1 2 3 4 5
13 @ ns ns1
14 @ ns ns2
15 ns1 a 10.0.0.1
16 ns2 a 10.0.0.2
17 a txt "a"
18 c.b.a txt "cba"
19 b txt "b"
20 sub ns ns1.sub
21 sub ns ns2.sub
22 ns1.sub a 10.0.0.3
23 ns2.sub a 10.0.0.4
24 ns1.sub2 a 10.0.0.5
25 ns2.sub2 a 10.0.0.6
26 text txt "here to be after sub2"
27 z txt "z"
28 """
29
30
31 def make_example(text: str, relativize: bool = False) -> dns.btreezone.Zone:
32 z = dns.zone.from_text(
33 simple_zone, "example.", relativize=relativize, zone_factory=dns.btreezone.Zone
34 )
35 return cast(dns.btreezone.Zone, z)
36
37
38 def do_test_node_flags(relativize: bool):
39 z = make_example(simple_zone, relativize)
40 n = cast(Node, z.get_node("@"))
41 assert not n.is_delegation()
42 assert not n.is_glue()
43 assert n.is_origin()
44 assert n.is_origin_or_glue()
45 assert n.is_immutable()
46 n = cast(Node, z.get_node("sub"))
47 assert n.is_delegation()
48 assert not n.is_glue()
49 assert not n.is_origin()
50 assert not n.is_origin_or_glue()
51 n = cast(Node, z.get_node("ns1.sub"))
52 assert not n.is_delegation()
53 assert n.is_glue()
54 assert not n.is_origin()
55 assert n.is_origin_or_glue()
56
57
58 def test_node_flags_absolute():
59 do_test_node_flags(False)
60
61
62 def test_node_flags_relative():
63 do_test_node_flags(True)
64
65
66 def test_flags_in_constructor():
67 n = Node()
68 assert n.flags == 0
69 n = Node(dns.btreezone.NodeFlags.ORIGIN)
70 assert n.is_origin()
71
72
73 def do_test_obscure_and_expose(relativize: bool):
74 z = make_example(simple_zone, relativize=relativize)
75 n = cast(Node, z.get_node("ns1.sub2"))
76 assert not n.is_delegation()
77 assert not n.is_glue()
78 assert not n.is_origin()
79 assert not n.is_origin_or_glue()
80 sub2_name = z._validate_name("sub2")
81 with z.reader() as txn:
82 version = cast(dns.btreezone.ImmutableVersion, txn.version)
83 assert sub2_name not in version.delegations
84 rds = dns.rdataset.from_text("in", "ns", 300, "ns1.sub2", "ns2.sub2")
85 with z.writer() as txn:
86 txn.replace("sub2", rds)
87 with z.reader() as txn:
88 version = cast(dns.btreezone.ImmutableVersion, txn.version)
89 assert sub2_name in version.delegations
90 n = cast(Node, z.get_node("ns1.sub2"))
91 assert not n.is_delegation()
92 assert n.is_glue()
93 assert not n.is_origin()
94 assert n.is_origin_or_glue()
95 with z.writer() as txn:
96 txn.delete("sub2")
97 txn.delete("ns2.sub2") # for other coverage purposes!
98 with z.reader() as txn:
99 version = cast(dns.btreezone.ImmutableVersion, txn.version)
100 assert sub2_name not in version.delegations
101 n = cast(Node, z.get_node("ns1.sub2"))
102 assert not n.is_delegation()
103 assert not n.is_glue()
104 assert not n.is_origin()
105 assert not n.is_origin_or_glue()
106 # repeat but delete just the rdataset
107 rds = dns.rdataset.from_text("in", "ns", 300, "ns1.sub2", "ns2.sub2")
108 with z.writer() as txn:
109 txn.replace("sub2", rds)
110 with z.reader() as txn:
111 version = cast(dns.btreezone.ImmutableVersion, txn.version)
112 assert sub2_name in version.delegations
113 n = cast(Node, z.get_node("ns1.sub2"))
114 assert not n.is_delegation()
115 assert n.is_glue()
116 assert not n.is_origin()
117 assert n.is_origin_or_glue()
118 with z.writer() as txn:
119 txn.delete("sub2", "NS")
120 with z.reader() as txn:
121 version = cast(dns.btreezone.ImmutableVersion, txn.version)
122 assert sub2_name not in version.delegations
123 n = cast(Node, z.get_node("ns1.sub2"))
124 assert not n.is_delegation()
125 assert not n.is_glue()
126 assert not n.is_origin()
127 assert not n.is_origin_or_glue()
128
129
130 def test_obscure_and_expose_absolute():
131 do_test_obscure_and_expose(False)
132
133
134 def test_obscure_and_expose_relative():
135 do_test_obscure_and_expose(True)
136
137
138 def do_test_delegations(relativize: bool):
139 z = make_example(simple_zone, relativize=relativize)
140 with z.reader() as txn:
141 version = cast(dns.btreezone.ImmutableVersion, txn.version)
142 name = z._validate_name("a.b.c.sub.example.")
143 delegation, is_glue = version.delegations.get_delegation(name)
144 assert delegation == z._validate_name("sub.example.")
145 assert is_glue
146 assert version.delegations.is_glue(name)
147 name = z._validate_name("sub.example.")
148 delegation, is_glue = version.delegations.get_delegation(name)
149 assert delegation == z._validate_name("sub.example.")
150 assert not is_glue
151 assert not version.delegations.is_glue(name)
152 name = z._validate_name("text.example.")
153 delegation, is_glue = version.delegations.get_delegation(name)
154 assert delegation is None
155 assert not is_glue
156 assert not version.delegations.is_glue(name)
157
158
159 def test_delegations_absolute():
160 do_test_delegations(False)
161
162
163 def test_delegations_relative():
164 do_test_delegations(True)
165
166
167 def do_test_bounds(relativize: bool):
168 z = make_example(simple_zone, relativize=relativize)
169 with z.reader() as txn:
170 version = cast(dns.btreezone.ImmutableVersion, txn.version)
171 # tuple is (name, left, right, closest, is_equal, is_delegation)
172 tests = [
173 ("example.", "example.", "a.example.", "example.", True, False),
174 ("a.z.example.", "z.example.", None, "z.example.", False, False),
175 (
176 "a.b.a.example.",
177 "a.example.",
178 "c.b.a.example.",
179 "b.a.example.",
180 False,
181 False,
182 ),
183 (
184 "d.b.a.example.",
185 "c.b.a.example.",
186 "b.example.",
187 "b.a.example.",
188 False,
189 False,
190 ),
191 (
192 "d.c.b.a.example.",
193 "c.b.a.example.",
194 "b.example.",
195 "c.b.a.example.",
196 False,
197 False,
198 ),
199 (
200 "sub.example.",
201 "sub.example.",
202 "ns1.sub2.example.",
203 "sub.example.",
204 True,
205 True,
206 ),
207 (
208 "ns1.sub.example.",
209 "sub.example.",
210 "ns1.sub2.example.",
211 "sub.example.",
212 False,
213 True,
214 ),
215 ]
216 for name, left, right, closest, is_equal, is_delegation in tests:
217 name = z._validate_name(name)
218 left = z._validate_name(left)
219 if right is not None:
220 right = z._validate_name(right)
221 closest = z._validate_name(closest)
222 bounds = version.bounds(name)
223 print(bounds)
224 assert bounds.left == left
225 assert bounds.right == right
226 assert bounds.closest_encloser == closest
227 assert bounds.is_equal == is_equal
228 assert bounds.is_delegation == is_delegation
229
230
231 def test_bounds_absolute():
232 do_test_bounds(False)
233
234
235 def test_bounds_relative():
236 do_test_bounds(True)