1# Copyright (C) 2015 Red Hat, Inc.
2# Author: Petr Spacek <pspacek@redhat.com>
3#
4# Permission to use, copy, modify, and distribute this software and its
5# documentation for any purpose with or without fee is hereby granted,
6# provided that the above copyright notice and this permission notice
7# appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED 'AS IS' AND RED HAT DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17import unittest
18
19import dns.rrset
20import dns.rdtypes.ANY.EUI48
21import dns.rdtypes.ANY.EUI64
22import dns.exception
23
24
25class RdtypeAnyEUI48TestCase(unittest.TestCase):
26    def testInstOk(self):
27        '''Valid binary input.'''
28        eui = b'\x01\x23\x45\x67\x89\xab'
29        inst = dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN,
30                                           dns.rdatatype.EUI48,
31                                           eui)
32        self.assertEqual(inst.eui, eui)
33
34    def testInstLength(self):
35        '''Incorrect input length.'''
36        eui = b'\x01\x23\x45\x67\x89\xab\xcd'
37        with self.assertRaises(dns.exception.FormError):
38            dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN,
39                                        dns.rdatatype.EUI48,
40                                        eui)
41
42    def testFromTextOk(self):
43        '''Valid text input.'''
44        r1 = dns.rrset.from_text('foo', 300, 'IN', 'EUI48',
45                                 '01-23-45-67-89-ab')
46        eui = b'\x01\x23\x45\x67\x89\xab'
47        self.assertEqual(r1[0].eui, eui)
48
49    def testFromTextLength(self):
50        '''Invalid input length.'''
51        with self.assertRaises(dns.exception.SyntaxError):
52            dns.rrset.from_text('foo', 300, 'IN', 'EUI48',
53                                '00-01-23-45-67-89-ab')
54
55    def testFromTextDelim(self):
56        '''Invalid delimiter.'''
57        with self.assertRaises(dns.exception.SyntaxError):
58            dns.rrset.from_text('foo', 300, 'IN', 'EUI48', '01_23-45-67-89-ab')
59
60    def testFromTextExtraDash(self):
61        '''Extra dash instead of hex digit.'''
62        with self.assertRaises(dns.exception.SyntaxError):
63            dns.rrset.from_text('foo', 300, 'IN', 'EUI48', '0--23-45-67-89-ab')
64
65    def testFromTextMultipleTokens(self):
66        '''Invalid input divided to multiple tokens.'''
67        with self.assertRaises(dns.exception.SyntaxError):
68            dns.rrset.from_text('foo', 300, 'IN', 'EUI48', '01 23-45-67-89-ab')
69
70    def testFromTextInvalidHex(self):
71        '''Invalid hexadecimal input.'''
72        with self.assertRaises(dns.exception.SyntaxError):
73            dns.rrset.from_text('foo', 300, 'IN', 'EUI48', 'g0-23-45-67-89-ab')
74
75    def testToTextOk(self):
76        '''Valid text output.'''
77        eui = b'\x01\x23\x45\x67\x89\xab'
78        exp_text = '01-23-45-67-89-ab'
79        inst = dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN,
80                                           dns.rdatatype.EUI48,
81                                           eui)
82        text = inst.to_text()
83        self.assertEqual(exp_text, text)
84
85    def testToWire(self):
86        '''Valid wire format.'''
87        eui = b'\x01\x23\x45\x67\x89\xab'
88        inst = dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN,
89                                           dns.rdatatype.EUI48,
90                                           eui)
91        self.assertEqual(inst.to_wire(), eui)
92
93    def testFromWireOk(self):
94        '''Valid wire format.'''
95        eui = b'\x01\x23\x45\x67\x89\xab'
96        pad_len = 100
97        wire = b'x' * pad_len + eui + b'y' * pad_len * 2
98        inst = dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI48,
99                                   wire, pad_len, len(eui))
100        self.assertEqual(inst.eui, eui)
101
102    def testFromWireLength(self):
103        '''Valid wire format.'''
104        eui = b'\x01\x23\x45\x67\x89'
105        pad_len = 100
106        wire = b'x' * pad_len + eui + b'y' * pad_len * 2
107        with self.assertRaises(dns.exception.FormError):
108            dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI48,
109                                wire, pad_len, len(eui))
110
111
112class RdtypeAnyEUI64TestCase(unittest.TestCase):
113    def testInstOk(self):
114        '''Valid binary input.'''
115        eui = b'\x01\x23\x45\x67\x89\xab\xcd\xef'
116        inst = dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN,
117                                           dns.rdatatype.EUI64,
118                                           eui)
119        self.assertEqual(inst.eui, eui)
120
121    def testInstLength(self):
122        '''Incorrect input length.'''
123        eui = b'\x01\x23\x45\x67\x89\xab'
124        with self.assertRaises(dns.exception.FormError):
125            dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN,
126                                        dns.rdatatype.EUI64,
127                                        eui)
128
129    def testFromTextOk(self):
130        '''Valid text input.'''
131        r1 = dns.rrset.from_text('foo', 300, 'IN', 'EUI64',
132                                 '01-23-45-67-89-ab-cd-ef')
133        eui = b'\x01\x23\x45\x67\x89\xab\xcd\xef'
134        self.assertEqual(r1[0].eui, eui)
135
136    def testFromTextLength(self):
137        '''Invalid input length.'''
138        with self.assertRaises(dns.exception.SyntaxError):
139            dns.rrset.from_text('foo', 300, 'IN', 'EUI64',
140                                '01-23-45-67-89-ab')
141
142    def testFromTextDelim(self):
143        '''Invalid delimiter.'''
144        with self.assertRaises(dns.exception.SyntaxError):
145            dns.rrset.from_text('foo', 300, 'IN', 'EUI64',
146                                '01_23-45-67-89-ab-cd-ef')
147
148    def testFromTextExtraDash(self):
149        '''Extra dash instead of hex digit.'''
150        with self.assertRaises(dns.exception.SyntaxError):
151            dns.rrset.from_text('foo', 300, 'IN', 'EUI64',
152                                '0--23-45-67-89-ab-cd-ef')
153
154    def testFromTextMultipleTokens(self):
155        '''Invalid input divided to multiple tokens.'''
156        with self.assertRaises(dns.exception.SyntaxError):
157            dns.rrset.from_text('foo', 300, 'IN', 'EUI64',
158                                '01 23-45-67-89-ab-cd-ef')
159
160    def testFromTextInvalidHex(self):
161        '''Invalid hexadecimal input.'''
162        with self.assertRaises(dns.exception.SyntaxError):
163            dns.rrset.from_text('foo', 300, 'IN', 'EUI64',
164                                'g0-23-45-67-89-ab-cd-ef')
165
166    def testToTextOk(self):
167        '''Valid text output.'''
168        eui = b'\x01\x23\x45\x67\x89\xab\xcd\xef'
169        exp_text = '01-23-45-67-89-ab-cd-ef'
170        inst = dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN,
171                                           dns.rdatatype.EUI64,
172                                           eui)
173        text = inst.to_text()
174        self.assertEqual(exp_text, text)
175
176    def testToWire(self):
177        '''Valid wire format.'''
178        eui = b'\x01\x23\x45\x67\x89\xab\xcd\xef'
179        inst = dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN,
180                                           dns.rdatatype.EUI64,
181                                           eui)
182        self.assertEqual(inst.to_wire(), eui)
183
184    def testFromWireOk(self):
185        '''Valid wire format.'''
186        eui = b'\x01\x23\x45\x67\x89\xab\xcd\xef'
187        pad_len = 100
188        wire = b'x' * pad_len + eui + b'y' * pad_len * 2
189        inst = dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI64,
190                                   wire, pad_len, len(eui))
191        self.assertEqual(inst.eui, eui)
192
193    def testFromWireLength(self):
194        '''Valid wire format.'''
195        eui = b'\x01\x23\x45\x67\x89'
196        pad_len = 100
197        wire = b'x' * pad_len + eui + b'y' * pad_len * 2
198        with self.assertRaises(dns.exception.FormError):
199            dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI64,
200                                wire, pad_len, len(eui))
201
202
203if __name__ == '__main__':
204    unittest.main()
205