message_test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 functions in data.py
  17. #
  18. import unittest
  19. import isc.cc
  20. class MessageTest(unittest.TestCase):
  21. def setUp(self):
  22. self.msg1 = { "just": [ "an", "arbitrary", "structure" ] }
  23. self.msg1_str = "{\"just\": [\"an\", \"arbitrary\", \"structure\"]}";
  24. self.msg1_wire = self.msg1_str.encode()
  25. self.msg2 = { "aaa": [ 1, True, False, None ] }
  26. self.msg2_str = "{\"aaa\": [1, true, false, null]}";
  27. self.msg2_wire = self.msg2_str.encode()
  28. self.msg3 = { "aaa": [ 1, 1.1, True, False, "string\n" ] }
  29. self.msg3_str = "{\"aaa\": [1, 1.1, true, false, \"string\n\" ]}";
  30. self.msg3_wire = self.msg3_str.encode()
  31. # Due to the inherent impreciseness of floating point values,
  32. # we test this one separately (with AlmostEqual)
  33. self.msg_float = 1.1
  34. self.msg_float_str = "1.1";
  35. self.msg_float_wire = self.msg_float_str.encode()
  36. def test_encode_json(self):
  37. self.assertEqual(self.msg1_wire, isc.cc.message.to_wire(self.msg1))
  38. self.assertEqual(self.msg2_wire, isc.cc.message.to_wire(self.msg2))
  39. self.assertAlmostEqual(float(self.msg_float_wire),
  40. float(isc.cc.message.to_wire(self.msg_float)))
  41. self.assertRaises(TypeError, isc.cc.message.to_wire, NotImplemented)
  42. def test_decode_json(self):
  43. self.assertEqual(self.msg1, isc.cc.message.from_wire(self.msg1_wire))
  44. self.assertEqual(self.msg2, isc.cc.message.from_wire(self.msg2_wire))
  45. self.assertEqual(self.msg3, isc.cc.message.from_wire(self.msg3_wire))
  46. self.assertRaises(AttributeError, isc.cc.message.from_wire, 1)
  47. self.assertRaises(ValueError, isc.cc.message.from_wire, b'\x001')
  48. self.assertRaises(ValueError, isc.cc.message.from_wire, b'')
  49. self.assertRaises(ValueError, isc.cc.message.from_wire, b'{"a": ')
  50. self.assertRaises(ValueError, isc.cc.message.from_wire, b'[ 1 ')
  51. self.assertRaises(ValueError, isc.cc.message.from_wire, b']')
  52. if __name__ == '__main__':
  53. unittest.main()