| 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.flags |
| 21 |
import dns.rcode |
| 22 |
import dns.opcode |
| 23 |
|
| 24 |
|
| 25 |
class FlagsTestCase(unittest.TestCase): |
| 26 |
def test_rcode1(self): |
| 27 |
self.assertEqual(dns.rcode.from_text("FORMERR"), dns.rcode.FORMERR) |
| 28 |
|
| 29 |
def test_rcode2(self): |
| 30 |
self.assertEqual(dns.rcode.to_text(dns.rcode.FORMERR), "FORMERR") |
| 31 |
|
| 32 |
def test_rcode3(self): |
| 33 |
self.assertEqual(dns.rcode.to_flags(dns.rcode.FORMERR), (1, 0)) |
| 34 |
|
| 35 |
def test_rcode4(self): |
| 36 |
self.assertEqual(dns.rcode.to_flags(dns.rcode.BADVERS), (0, 0x01000000)) |
| 37 |
|
| 38 |
def test_rcode6(self): |
| 39 |
self.assertEqual(dns.rcode.from_flags(0, 0x01000000), dns.rcode.BADVERS) |
| 40 |
|
| 41 |
def test_rcode7(self): |
| 42 |
self.assertEqual(dns.rcode.from_flags(5, 0), dns.rcode.REFUSED) |
| 43 |
|
| 44 |
def test_rcode8(self): |
| 45 |
def bad(): |
| 46 |
dns.rcode.to_flags(4096) |
| 47 |
|
| 48 |
self.assertRaises(ValueError, bad) |
| 49 |
|
| 50 |
def test_flags1(self): |
| 51 |
self.assertEqual( |
| 52 |
dns.flags.from_text("RA RD AA QR"), |
| 53 |
dns.flags.QR | dns.flags.AA | dns.flags.RD | dns.flags.RA, |
| 54 |
) |
| 55 |
|
| 56 |
def test_flags2(self): |
| 57 |
flags = dns.flags.QR | dns.flags.AA | dns.flags.RD | dns.flags.RA |
| 58 |
self.assertEqual(dns.flags.to_text(flags), "QR AA RD RA") |
| 59 |
|
| 60 |
def test_rcode_badvers(self): |
| 61 |
rcode = dns.rcode.BADVERS |
| 62 |
self.assertEqual(rcode.value, 16) |
| 63 |
self.assertEqual(rcode.name, "BADVERS") |
| 64 |
self.assertEqual(dns.rcode.to_text(rcode), "BADVERS") |
| 65 |
|
| 66 |
def test_rcode_badsig(self): |
| 67 |
rcode = dns.rcode.BADSIG |
| 68 |
self.assertEqual(rcode.value, 16) |
| 69 |
# Yes, we mean BADVERS on the next line. BADSIG and BADVERS have |
| 70 |
# the same code. |
| 71 |
self.assertEqual(rcode.name, "BADVERS") |
| 72 |
self.assertEqual(dns.rcode.to_text(rcode), "BADVERS") |
| 73 |
# In TSIG text mode, it should be BADSIG |
| 74 |
self.assertEqual(dns.rcode.to_text(rcode, True), "BADSIG") |
| 75 |
|
| 76 |
def test_unknown_rcode(self): |
| 77 |
with self.assertRaises(dns.rcode.UnknownRcode): |
| 78 |
dns.rcode.Rcode.make("BOGUS") |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
unittest.main() |