summaryrefslogtreecommitdiff
path: root/tests/test_namedict.py
blob: c5563e5553078316e54e6d5ead1f97631dbdd9d7 (plain)
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
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license

# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose with or without fee is hereby granted,
# provided that the above copyright notice and this permission notice
# appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import unittest

import dns.name
import dns.namedict


class NameTestCase(unittest.TestCase):
    def setUp(self):
        self.ndict = dns.namedict.NameDict()
        n1 = dns.name.from_text("foo.bar.")
        n2 = dns.name.from_text("bar.")
        self.ndict[n1] = 1
        self.ndict[n2] = 2
        self.rndict = dns.namedict.NameDict()
        n1 = dns.name.from_text("foo.bar", None)
        n2 = dns.name.from_text("bar", None)
        self.rndict[n1] = 1
        self.rndict[n2] = 2

    def testDepth(self):
        self.assertEqual(self.ndict.max_depth, 3)

    def testLookup1(self):
        k = dns.name.from_text("foo.bar.")
        self.assertEqual(self.ndict[k], 1)

    def testLookup2(self):
        k = dns.name.from_text("foo.bar.")
        self.assertEqual(self.ndict.get_deepest_match(k)[1], 1)

    def testLookup3(self):
        k = dns.name.from_text("a.b.c.foo.bar.")
        self.assertEqual(self.ndict.get_deepest_match(k)[1], 1)

    def testLookup4(self):
        k = dns.name.from_text("a.b.c.bar.")
        self.assertEqual(self.ndict.get_deepest_match(k)[1], 2)

    def testLookup5(self):
        def bad():
            n = dns.name.from_text("a.b.c.")
            self.ndict.get_deepest_match(n)

        self.assertRaises(KeyError, bad)

    def testLookup6(self):
        def bad():
            self.ndict.get_deepest_match(dns.name.empty)

        self.assertRaises(KeyError, bad)

    def testLookup7(self):
        self.ndict[dns.name.empty] = 100
        n = dns.name.from_text("a.b.c.")
        v = self.ndict.get_deepest_match(n)[1]
        self.assertEqual(v, 100)

    def testLookup8(self):
        def bad():
            self.ndict["foo"] = 100

        self.assertRaises(ValueError, bad)

    def testRelDepth(self):
        self.assertEqual(self.rndict.max_depth, 2)

    def testRelLookup1(self):
        k = dns.name.from_text("foo.bar", None)
        self.assertEqual(self.rndict[k], 1)

    def testRelLookup2(self):
        k = dns.name.from_text("foo.bar", None)
        self.assertEqual(self.rndict.get_deepest_match(k)[1], 1)

    def testRelLookup3(self):
        k = dns.name.from_text("a.b.c.foo.bar", None)
        self.assertEqual(self.rndict.get_deepest_match(k)[1], 1)

    def testRelLookup4(self):
        k = dns.name.from_text("a.b.c.bar", None)
        self.assertEqual(self.rndict.get_deepest_match(k)[1], 2)

    def testRelLookup7(self):
        self.rndict[dns.name.empty] = 100
        n = dns.name.from_text("a.b.c", None)
        v = self.rndict.get_deepest_match(n)[1]
        self.assertEqual(v, 100)

    def test_max_depth_increases(self):
        n = dns.name.from_text("a.foo.bar.")
        self.assertEqual(self.ndict.max_depth, 3)
        self.ndict[n] = 1
        self.assertEqual(self.ndict.max_depth, 4)

    def test_delete_no_max_depth_change(self):
        self.assertEqual(self.ndict.max_depth, 3)
        n = dns.name.from_text("bar.")
        del self.ndict[n]
        self.assertEqual(self.ndict.max_depth, 3)
        self.assertEqual(self.ndict.get(n), None)

    def test_delete_max_depth_changes(self):
        self.assertEqual(self.ndict.max_depth, 3)
        n = dns.name.from_text("foo.bar.")
        del self.ndict[n]
        self.assertEqual(self.ndict.max_depth, 2)
        self.assertEqual(self.ndict.get(n), None)

    def test_delete_multiple_max_depth_changes(self):
        self.assertEqual(self.ndict.max_depth, 3)
        nr = dns.name.from_text("roo.")
        self.ndict[nr] = 1
        nf = dns.name.from_text("foo.bar.")
        nb = dns.name.from_text("bar.bar.")
        self.ndict[nb] = 1
        self.assertEqual(self.ndict.max_depth, 3)
        self.assertEqual(self.ndict.max_depth_items, 2)
        del self.ndict[nb]
        self.assertEqual(self.ndict.max_depth, 3)
        self.assertEqual(self.ndict.max_depth_items, 1)
        del self.ndict[nf]
        self.assertEqual(self.ndict.max_depth, 2)
        self.assertEqual(self.ndict.max_depth_items, 2)
        self.assertEqual(self.ndict.get(nf), None)
        self.assertEqual(self.ndict.get(nb), None)

    def test_iter(self):
        nf = dns.name.from_text("foo.bar.")
        nb = dns.name.from_text("bar.")
        keys = set([x for x in self.ndict])
        self.assertEqual(len(keys), 2)
        self.assertTrue(nf in keys)
        self.assertTrue(nb in keys)

    def test_len(self):
        self.assertEqual(len(self.ndict), 2)

    def test_haskey(self):
        nf = dns.name.from_text("foo.bar.")
        nb = dns.name.from_text("bar.")
        nx = dns.name.from_text("x.")
        self.assertTrue(self.ndict.has_key(nf))
        self.assertTrue(self.ndict.has_key(nb))
        self.assertFalse(self.ndict.has_key(nx))


if __name__ == "__main__":
    unittest.main()