| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
import unittest |
| 19 |
|
| 20 |
import dns.name |
| 21 |
import dns.namedict |
| 22 |
|
| 23 |
|
| 24 |
class NameTestCase(unittest.TestCase): |
| 25 |
def setUp(self): |
| 26 |
self.ndict = dns.namedict.NameDict() |
| 27 |
n1 = dns.name.from_text("foo.bar.") |
| 28 |
n2 = dns.name.from_text("bar.") |
| 29 |
self.ndict[n1] = 1 |
| 30 |
self.ndict[n2] = 2 |
| 31 |
self.rndict = dns.namedict.NameDict() |
| 32 |
n1 = dns.name.from_text("foo.bar", None) |
| 33 |
n2 = dns.name.from_text("bar", None) |
| 34 |
self.rndict[n1] = 1 |
| 35 |
self.rndict[n2] = 2 |
| 36 |
|
| 37 |
def testDepth(self): |
| 38 |
self.assertEqual(self.ndict.max_depth, 3) |
| 39 |
|
| 40 |
def testLookup1(self): |
| 41 |
k = dns.name.from_text("foo.bar.") |
| 42 |
self.assertEqual(self.ndict[k], 1) |
| 43 |
|
| 44 |
def testLookup2(self): |
| 45 |
k = dns.name.from_text("foo.bar.") |
| 46 |
self.assertEqual(self.ndict.get_deepest_match(k)[1], 1) |
| 47 |
|
| 48 |
def testLookup3(self): |
| 49 |
k = dns.name.from_text("a.b.c.foo.bar.") |
| 50 |
self.assertEqual(self.ndict.get_deepest_match(k)[1], 1) |
| 51 |
|
| 52 |
def testLookup4(self): |
| 53 |
k = dns.name.from_text("a.b.c.bar.") |
| 54 |
self.assertEqual(self.ndict.get_deepest_match(k)[1], 2) |
| 55 |
|
| 56 |
def testLookup5(self): |
| 57 |
def bad(): |
| 58 |
n = dns.name.from_text("a.b.c.") |
| 59 |
self.ndict.get_deepest_match(n) |
| 60 |
|
| 61 |
self.assertRaises(KeyError, bad) |
| 62 |
|
| 63 |
def testLookup6(self): |
| 64 |
def bad(): |
| 65 |
self.ndict.get_deepest_match(dns.name.empty) |
| 66 |
|
| 67 |
self.assertRaises(KeyError, bad) |
| 68 |
|
| 69 |
def testLookup7(self): |
| 70 |
self.ndict[dns.name.empty] = 100 |
| 71 |
n = dns.name.from_text("a.b.c.") |
| 72 |
v = self.ndict.get_deepest_match(n)[1] |
| 73 |
self.assertEqual(v, 100) |
| 74 |
|
| 75 |
def testLookup8(self): |
| 76 |
def bad(): |
| 77 |
self.ndict["foo"] = 100 |
| 78 |
|
| 79 |
self.assertRaises(ValueError, bad) |
| 80 |
|
| 81 |
def testRelDepth(self): |
| 82 |
self.assertEqual(self.rndict.max_depth, 2) |
| 83 |
|
| 84 |
def testRelLookup1(self): |
| 85 |
k = dns.name.from_text("foo.bar", None) |
| 86 |
self.assertEqual(self.rndict[k], 1) |
| 87 |
|
| 88 |
def testRelLookup2(self): |
| 89 |
k = dns.name.from_text("foo.bar", None) |
| 90 |
self.assertEqual(self.rndict.get_deepest_match(k)[1], 1) |
| 91 |
|
| 92 |
def testRelLookup3(self): |
| 93 |
k = dns.name.from_text("a.b.c.foo.bar", None) |
| 94 |
self.assertEqual(self.rndict.get_deepest_match(k)[1], 1) |
| 95 |
|
| 96 |
def testRelLookup4(self): |
| 97 |
k = dns.name.from_text("a.b.c.bar", None) |
| 98 |
self.assertEqual(self.rndict.get_deepest_match(k)[1], 2) |
| 99 |
|
| 100 |
def testRelLookup7(self): |
| 101 |
self.rndict[dns.name.empty] = 100 |
| 102 |
n = dns.name.from_text("a.b.c", None) |
| 103 |
v = self.rndict.get_deepest_match(n)[1] |
| 104 |
self.assertEqual(v, 100) |
| 105 |
|
| 106 |
def test_max_depth_increases(self): |
| 107 |
n = dns.name.from_text("a.foo.bar.") |
| 108 |
self.assertEqual(self.ndict.max_depth, 3) |
| 109 |
self.ndict[n] = 1 |
| 110 |
self.assertEqual(self.ndict.max_depth, 4) |
| 111 |
|
| 112 |
def test_delete_no_max_depth_change(self): |
| 113 |
self.assertEqual(self.ndict.max_depth, 3) |
| 114 |
n = dns.name.from_text("bar.") |
| 115 |
del self.ndict[n] |
| 116 |
self.assertEqual(self.ndict.max_depth, 3) |
| 117 |
self.assertEqual(self.ndict.get(n), None) |
| 118 |
|
| 119 |
def test_delete_max_depth_changes(self): |
| 120 |
self.assertEqual(self.ndict.max_depth, 3) |
| 121 |
n = dns.name.from_text("foo.bar.") |
| 122 |
del self.ndict[n] |
| 123 |
self.assertEqual(self.ndict.max_depth, 2) |
| 124 |
self.assertEqual(self.ndict.get(n), None) |
| 125 |
|
| 126 |
def test_delete_multiple_max_depth_changes(self): |
| 127 |
self.assertEqual(self.ndict.max_depth, 3) |
| 128 |
nr = dns.name.from_text("roo.") |
| 129 |
self.ndict[nr] = 1 |
| 130 |
nf = dns.name.from_text("foo.bar.") |
| 131 |
nb = dns.name.from_text("bar.bar.") |
| 132 |
self.ndict[nb] = 1 |
| 133 |
self.assertEqual(self.ndict.max_depth, 3) |
| 134 |
self.assertEqual(self.ndict.max_depth_items, 2) |
| 135 |
del self.ndict[nb] |
| 136 |
self.assertEqual(self.ndict.max_depth, 3) |
| 137 |
self.assertEqual(self.ndict.max_depth_items, 1) |
| 138 |
del self.ndict[nf] |
| 139 |
self.assertEqual(self.ndict.max_depth, 2) |
| 140 |
self.assertEqual(self.ndict.max_depth_items, 2) |
| 141 |
self.assertEqual(self.ndict.get(nf), None) |
| 142 |
self.assertEqual(self.ndict.get(nb), None) |
| 143 |
|
| 144 |
def test_iter(self): |
| 145 |
nf = dns.name.from_text("foo.bar.") |
| 146 |
nb = dns.name.from_text("bar.") |
| 147 |
keys = set([x for x in self.ndict]) |
| 148 |
self.assertEqual(len(keys), 2) |
| 149 |
self.assertTrue(nf in keys) |
| 150 |
self.assertTrue(nb in keys) |
| 151 |
|
| 152 |
def test_len(self): |
| 153 |
self.assertEqual(len(self.ndict), 2) |
| 154 |
|
| 155 |
def test_haskey(self): |
| 156 |
nf = dns.name.from_text("foo.bar.") |
| 157 |
nb = dns.name.from_text("bar.") |
| 158 |
nx = dns.name.from_text("x.") |
| 159 |
self.assertTrue(self.ndict.has_key(nf)) |
| 160 |
self.assertTrue(self.ndict.has_key(nb)) |
| 161 |
self.assertFalse(self.ndict.has_key(nx)) |
| 162 |
|
| 163 |
|
| 164 |
if __name__ == "__main__": |
| 165 |
unittest.main() |