| 1 |
# Copyright (C) 2015 Red Hat, Inc. |
| 2 |
# Author: Petr Spacek <[email protected]> |
| 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 |
|
| 17 |
import unittest |
| 18 |
|
| 19 |
import dns.rrset |
| 20 |
import dns.rdtypes.ANY.EUI48 |
| 21 |
import dns.rdtypes.ANY.EUI64 |
| 22 |
import dns.exception |
| 23 |
|
| 24 |
|
| 25 |
class 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, dns.rdatatype.EUI48, eui) |
| 30 |
self.assertEqual(inst.eui, eui) |
| 31 |
|
| 32 |
def testInstLength(self): |
| 33 |
"""Incorrect input length.""" |
| 34 |
eui = b"\x01\x23\x45\x67\x89\xab\xcd" |
| 35 |
with self.assertRaises(dns.exception.FormError): |
| 36 |
dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN, dns.rdatatype.EUI48, eui) |
| 37 |
|
| 38 |
def testFromTextOk(self): |
| 39 |
"""Valid text input.""" |
| 40 |
r1 = dns.rrset.from_text("foo", 300, "IN", "EUI48", "01-23-45-67-89-ab") |
| 41 |
eui = b"\x01\x23\x45\x67\x89\xab" |
| 42 |
self.assertEqual(r1[0].eui, eui) |
| 43 |
|
| 44 |
def testFromTextLength(self): |
| 45 |
"""Invalid input length.""" |
| 46 |
with self.assertRaises(dns.exception.SyntaxError): |
| 47 |
dns.rrset.from_text("foo", 300, "IN", "EUI48", "00-01-23-45-67-89-ab") |
| 48 |
|
| 49 |
def testFromTextDelim(self): |
| 50 |
"""Invalid delimiter.""" |
| 51 |
with self.assertRaises(dns.exception.SyntaxError): |
| 52 |
dns.rrset.from_text("foo", 300, "IN", "EUI48", "01_23-45-67-89-ab") |
| 53 |
|
| 54 |
def testFromTextExtraDash(self): |
| 55 |
"""Extra dash instead of hex digit.""" |
| 56 |
with self.assertRaises(dns.exception.SyntaxError): |
| 57 |
dns.rrset.from_text("foo", 300, "IN", "EUI48", "0--23-45-67-89-ab") |
| 58 |
|
| 59 |
def testFromTextMultipleTokens(self): |
| 60 |
"""Invalid input divided to multiple tokens.""" |
| 61 |
with self.assertRaises(dns.exception.SyntaxError): |
| 62 |
dns.rrset.from_text("foo", 300, "IN", "EUI48", "01 23-45-67-89-ab") |
| 63 |
|
| 64 |
def testFromTextInvalidHex(self): |
| 65 |
"""Invalid hexadecimal input.""" |
| 66 |
with self.assertRaises(dns.exception.SyntaxError): |
| 67 |
dns.rrset.from_text("foo", 300, "IN", "EUI48", "g0-23-45-67-89-ab") |
| 68 |
|
| 69 |
def testToTextOk(self): |
| 70 |
"""Valid text output.""" |
| 71 |
eui = b"\x01\x23\x45\x67\x89\xab" |
| 72 |
exp_text = "01-23-45-67-89-ab" |
| 73 |
inst = dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN, dns.rdatatype.EUI48, eui) |
| 74 |
text = inst.to_text() |
| 75 |
self.assertEqual(exp_text, text) |
| 76 |
|
| 77 |
def testToWire(self): |
| 78 |
"""Valid wire format.""" |
| 79 |
eui = b"\x01\x23\x45\x67\x89\xab" |
| 80 |
inst = dns.rdtypes.ANY.EUI48.EUI48(dns.rdataclass.IN, dns.rdatatype.EUI48, eui) |
| 81 |
self.assertEqual(inst.to_wire(), eui) |
| 82 |
|
| 83 |
def testFromWireOk(self): |
| 84 |
"""Valid wire format.""" |
| 85 |
eui = b"\x01\x23\x45\x67\x89\xab" |
| 86 |
pad_len = 100 |
| 87 |
wire = b"x" * pad_len + eui + b"y" * pad_len * 2 |
| 88 |
inst = dns.rdata.from_wire( |
| 89 |
dns.rdataclass.IN, dns.rdatatype.EUI48, wire, pad_len, len(eui) |
| 90 |
) |
| 91 |
self.assertEqual(inst.eui, eui) |
| 92 |
|
| 93 |
def testFromWireLength(self): |
| 94 |
"""Valid wire format.""" |
| 95 |
eui = b"\x01\x23\x45\x67\x89" |
| 96 |
pad_len = 100 |
| 97 |
wire = b"x" * pad_len + eui + b"y" * pad_len * 2 |
| 98 |
with self.assertRaises(dns.exception.FormError): |
| 99 |
dns.rdata.from_wire( |
| 100 |
dns.rdataclass.IN, dns.rdatatype.EUI48, wire, pad_len, len(eui) |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
class RdtypeAnyEUI64TestCase(unittest.TestCase): |
| 105 |
def testInstOk(self): |
| 106 |
"""Valid binary input.""" |
| 107 |
eui = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 108 |
inst = dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN, dns.rdatatype.EUI64, eui) |
| 109 |
self.assertEqual(inst.eui, eui) |
| 110 |
|
| 111 |
def testInstLength(self): |
| 112 |
"""Incorrect input length.""" |
| 113 |
eui = b"\x01\x23\x45\x67\x89\xab" |
| 114 |
with self.assertRaises(dns.exception.FormError): |
| 115 |
dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN, dns.rdatatype.EUI64, eui) |
| 116 |
|
| 117 |
def testFromTextOk(self): |
| 118 |
"""Valid text input.""" |
| 119 |
r1 = dns.rrset.from_text("foo", 300, "IN", "EUI64", "01-23-45-67-89-ab-cd-ef") |
| 120 |
eui = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 121 |
self.assertEqual(r1[0].eui, eui) |
| 122 |
|
| 123 |
def testFromTextLength(self): |
| 124 |
"""Invalid input length.""" |
| 125 |
with self.assertRaises(dns.exception.SyntaxError): |
| 126 |
dns.rrset.from_text("foo", 300, "IN", "EUI64", "01-23-45-67-89-ab") |
| 127 |
|
| 128 |
def testFromTextDelim(self): |
| 129 |
"""Invalid delimiter.""" |
| 130 |
with self.assertRaises(dns.exception.SyntaxError): |
| 131 |
dns.rrset.from_text("foo", 300, "IN", "EUI64", "01_23-45-67-89-ab-cd-ef") |
| 132 |
|
| 133 |
def testFromTextExtraDash(self): |
| 134 |
"""Extra dash instead of hex digit.""" |
| 135 |
with self.assertRaises(dns.exception.SyntaxError): |
| 136 |
dns.rrset.from_text("foo", 300, "IN", "EUI64", "0--23-45-67-89-ab-cd-ef") |
| 137 |
|
| 138 |
def testFromTextMultipleTokens(self): |
| 139 |
"""Invalid input divided to multiple tokens.""" |
| 140 |
with self.assertRaises(dns.exception.SyntaxError): |
| 141 |
dns.rrset.from_text("foo", 300, "IN", "EUI64", "01 23-45-67-89-ab-cd-ef") |
| 142 |
|
| 143 |
def testFromTextInvalidHex(self): |
| 144 |
"""Invalid hexadecimal input.""" |
| 145 |
with self.assertRaises(dns.exception.SyntaxError): |
| 146 |
dns.rrset.from_text("foo", 300, "IN", "EUI64", "g0-23-45-67-89-ab-cd-ef") |
| 147 |
|
| 148 |
def testToTextOk(self): |
| 149 |
"""Valid text output.""" |
| 150 |
eui = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 151 |
exp_text = "01-23-45-67-89-ab-cd-ef" |
| 152 |
inst = dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN, dns.rdatatype.EUI64, eui) |
| 153 |
text = inst.to_text() |
| 154 |
self.assertEqual(exp_text, text) |
| 155 |
|
| 156 |
def testToWire(self): |
| 157 |
"""Valid wire format.""" |
| 158 |
eui = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 159 |
inst = dns.rdtypes.ANY.EUI64.EUI64(dns.rdataclass.IN, dns.rdatatype.EUI64, eui) |
| 160 |
self.assertEqual(inst.to_wire(), eui) |
| 161 |
|
| 162 |
def testFromWireOk(self): |
| 163 |
"""Valid wire format.""" |
| 164 |
eui = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 165 |
pad_len = 100 |
| 166 |
wire = b"x" * pad_len + eui + b"y" * pad_len * 2 |
| 167 |
inst = dns.rdata.from_wire( |
| 168 |
dns.rdataclass.IN, dns.rdatatype.EUI64, wire, pad_len, len(eui) |
| 169 |
) |
| 170 |
self.assertEqual(inst.eui, eui) |
| 171 |
|
| 172 |
def testFromWireLength(self): |
| 173 |
"""Valid wire format.""" |
| 174 |
eui = b"\x01\x23\x45\x67\x89" |
| 175 |
pad_len = 100 |
| 176 |
wire = b"x" * pad_len + eui + b"y" * pad_len * 2 |
| 177 |
with self.assertRaises(dns.exception.FormError): |
| 178 |
dns.rdata.from_wire( |
| 179 |
dns.rdataclass.IN, dns.rdatatype.EUI64, wire, pad_len, len(eui) |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == "__main__": |
| 184 |
unittest.main() |