summaryrefslogtreecommitdiff
path: root/tests/test_rrset.py
blob: 9540e3c4d6ad9beced078cd216777cc60eec3a8a (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
# -*- coding: utf-8
# 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.rrset

class RRsetTestCase(unittest.TestCase):

    def testEqual1(self):
        r1 = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1', '10.0.0.2')
        r2 = dns.rrset.from_text('FOO', 300, 'in', 'a', '10.0.0.2', '10.0.0.1')
        self.assertEqual(r1, r2)

    def testEqual2(self):
        r1 = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1', '10.0.0.2')
        r2 = dns.rrset.from_text('FOO', 600, 'in', 'a', '10.0.0.2', '10.0.0.1')
        self.assertEqual(r1, r2)

    def testNotEqual1(self):
        r1 = dns.rrset.from_text('fooa', 30, 'in', 'a', '10.0.0.1', '10.0.0.2')
        r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1')
        self.assertNotEqual(r1, r2)

    def testNotEqual2(self):
        r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1', '10.0.0.3')
        r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1')
        self.assertNotEqual(r1, r2)

    def testNotEqual3(self):
        r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1', '10.0.0.2',
                                 '10.0.0.3')
        r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1')
        self.assertNotEqual(r1, r2)

    def testNotEqual4(self):
        r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1')
        r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1')
        self.assertNotEqual(r1, r2)

    def testCodec2003(self):
        r1 = dns.rrset.from_text_list('Königsgäßchen', 30, 'in', 'ns',
                                      ['Königsgäßchen'])
        r2 = dns.rrset.from_text_list('xn--knigsgsschen-lcb0w', 30, 'in', 'ns',
                                      ['xn--knigsgsschen-lcb0w'])
        self.assertEqual(r1, r2)

    @unittest.skipUnless(dns.name.have_idna_2008,
                         'Python idna cannot be imported; no IDNA2008')
    def testCodec2008(self):
        r1 = dns.rrset.from_text_list('Königsgäßchen', 30, 'in', 'ns',
                                      ['Königsgäßchen'],
                                      idna_codec=dns.name.IDNA_2008)
        r2 = dns.rrset.from_text_list('xn--knigsgchen-b4a3dun', 30, 'in', 'ns',
                                      ['xn--knigsgchen-b4a3dun'],
                                      idna_codec=dns.name.IDNA_2008)
        self.assertEqual(r1, r2)

    def testCopy(self):
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        r2 = r1.copy()
        self.assertFalse(r1 is r2)
        self.assertTrue(r1 == r2)

    def testMatch1(self):
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        self.assertTrue(r1.match(r1.name, dns.rdataclass.IN,
                                 dns.rdatatype.A, dns.rdatatype.NONE))

    def testMatch2(self):
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        r1.deleting = dns.rdataclass.NONE
        self.assertTrue(r1.match(r1.name, dns.rdataclass.IN,
                                 dns.rdatatype.A, dns.rdatatype.NONE,
                                 dns.rdataclass.NONE))

    def testNoMatch1(self):
        n = dns.name.from_text('bar', None)
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        self.assertFalse(r1.match(n, dns.rdataclass.IN,
                                  dns.rdatatype.A, dns.rdatatype.NONE,
                                  dns.rdataclass.ANY))

    def testNoMatch2(self):
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        r1.deleting = dns.rdataclass.NONE
        self.assertFalse(r1.match(r1.name, dns.rdataclass.IN,
                                  dns.rdatatype.A, dns.rdatatype.NONE,
                                  dns.rdataclass.ANY))

    def testNoMatch3(self):
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        self.assertFalse(r1.match(r1.name, dns.rdataclass.IN,
                                  dns.rdatatype.MX, dns.rdatatype.NONE,
                                  dns.rdataclass.ANY))

    def testToRdataset(self):
        r1 = dns.rrset.from_text_list('foo', 30, 'in', 'a',
                                      ['10.0.0.1', '10.0.0.2'])
        r2 = dns.rdataset.from_text_list('in', 'a', 30,
                                         ['10.0.0.1', '10.0.0.2'])
        self.assertEqual(r1.to_rdataset(), r2)

    def testFromRdata(self):
        rdata1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A,
                                     '10.0.0.1')
        rdata2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A,
                                     '10.0.0.2')
        expected_rrs = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1',
                                           '10.0.0.2')
        rrs = dns.rrset.from_rdata('foo', 300, rdata1, rdata2)
        self.assertEqual(rrs, expected_rrs)

    def testEmptyList(self):
        def bad():
            rrs = dns.rrset.from_rdata_list('foo', 300, [])
        self.assertRaises(ValueError, bad)

    def testTTLMinimization(self):
        rrs = dns.rrset.RRset(dns.name.from_text('foo'),
                              dns.rdataclass.IN, dns.rdatatype.A)
        rdata1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A,
                                     '10.0.0.1')
        rdata2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A,
                                     '10.0.0.2')
        rrs.add(rdata1, 300)
        self.assertEqual(rrs.ttl, 300)
        rrs.add(rdata2, 30)
        self.assertEqual(rrs.ttl, 30)
        # adding the same thing with a smaller TTL also minimizes
        rrs.add(rdata2, 3)
        self.assertEqual(rrs.ttl, 3)

    def testNotEqualOtherType(self):
        rrs = dns.rrset.RRset(dns.name.from_text('foo'),
                              dns.rdataclass.IN, dns.rdatatype.A)
        self.assertFalse(rrs == 123)

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