question_python_test.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #
  16. # Tests for the rrtype part of the libdns_python module
  17. #
  18. import unittest
  19. import os
  20. from libdns_python import *
  21. from testutil import *
  22. if "TESTDATA_PATH" in os.environ:
  23. testdata_path = os.environ["TESTDATA_PATH"]
  24. else:
  25. testdata_path = "../tests/testdata"
  26. def question_from_wire(file, position = 0):
  27. data = read_wire_data(file)
  28. return Question(data, position)
  29. class QuestionTest(unittest.TestCase):
  30. def setUp(self):
  31. self.example_name1 = Name("foo.example.com")
  32. self.example_name2 = Name("bar.example.com")
  33. self.test_question1 = Question(self.example_name1, RRClass("IN"), RRType("NS"))
  34. self.test_question2 = Question(self.example_name2, RRClass("CH"), RRType("A"))
  35. def test_init(self):
  36. self.assertRaises(TypeError, Question, "wrong")
  37. # tests below based on cpp unit tests
  38. # also tests get_name, get_class and get_type
  39. def test_from_wire(self):
  40. q = question_from_wire("question_fromWire")
  41. self.assertEqual(self.example_name1, q.get_name())
  42. self.assertEqual(RRClass("IN"), q.get_class())
  43. self.assertEqual(RRType("NS"), q.get_type())
  44. # owner name of the second Question is compressed. It's uncommon
  45. # (to have multiple questions), but isn't prohibited by the protocol.
  46. q = question_from_wire("question_fromWire", 21)
  47. self.assertEqual(self.example_name2, q.get_name())
  48. self.assertEqual(RRClass("CH"), q.get_class())
  49. self.assertEqual(RRType("A"), q.get_type())
  50. # Pathological cases: Corresponding exceptions will be thrown from
  51. # the underlying parser.
  52. self.assertRaises(DNSMessageFORMERR,
  53. question_from_wire,
  54. "question_fromWire", 31)
  55. self.assertRaises(IncompleteRRClass,
  56. question_from_wire,
  57. "question_fromWire", 36)
  58. def test_to_text(self):
  59. self.assertEqual("foo.example.com. IN NS\n", self.test_question1.to_text())
  60. self.assertEqual("foo.example.com. IN NS\n", str(self.test_question1))
  61. self.assertEqual("bar.example.com. CH A\n", self.test_question2.to_text())
  62. def test_to_wire_buffer(self):
  63. obuffer = bytes()
  64. obuffer = self.test_question1.to_wire(obuffer)
  65. obuffer = self.test_question2.to_wire(obuffer)
  66. wiredata = read_wire_data("question_toWire1")
  67. self.assertEqual(obuffer, wiredata)
  68. def test_to_wire_renderer(self):
  69. renderer = MessageRenderer()
  70. self.test_question1.to_wire(renderer)
  71. self.test_question2.to_wire(renderer)
  72. wiredata = read_wire_data("question_toWire2")
  73. self.assertEqual(renderer.get_data(), wiredata)
  74. self.assertRaises(TypeError, self.test_question1.to_wire, 1)
  75. if __name__ == '__main__':
  76. unittest.main()