parse_test.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  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. """Tests for isc.net.parse."""
  16. import unittest
  17. import socket
  18. from isc.net.parse import port_parse, addr_parse
  19. class TestCheckPort(unittest.TestCase):
  20. """
  21. Testcases for the isc.net.port_parse function
  22. """
  23. def test_fail(self):
  24. """
  25. Test if it fails on invalid input in the correct way.
  26. """
  27. self.assertRaises(ValueError, port_parse, "not a number")
  28. self.assertRaises(ValueError, port_parse, -1)
  29. self.assertRaises(ValueError, port_parse, 65536)
  30. def test_success(self):
  31. """
  32. Test if it succeeds on valid inputs and returns the correct output
  33. """
  34. self.assertEqual(port_parse(0), 0)
  35. self.assertEqual(port_parse("65535"), 65535)
  36. self.assertEqual(port_parse(1234), 1234)
  37. class TestCheckIP(unittest.TestCase):
  38. """
  39. Testcases for the isc.net.ip_par function
  40. """
  41. def test_fail(self):
  42. """
  43. Test if it fails on invalid input the correct way.
  44. """
  45. self.assertRaises(ValueError, addr_parse, "not an address")
  46. self.assertRaises(ValueError, addr_parse, "123.456.789.012")
  47. self.assertRaises(ValueError, addr_parse, "123.0.0.")
  48. # Address range not allowed
  49. self.assertRaises(ValueError, addr_parse, "192.0.2.0/24")
  50. try:
  51. # XXX: MacOS X's inet_pton() doesn't reject this form, so we
  52. # check the behavior of the underlying library implementation
  53. # before the actual test
  54. socket.inet_pton(socket.AF_INET, "0000.0.0.0")
  55. except socket.error:
  56. self.assertRaises(ValueError, addr_parse, "0000.0.0.0")
  57. self.assertRaises(ValueError, addr_parse, "bada:ddr0::")
  58. self.assertRaises(ValueError, addr_parse, "2001:db8::/32")
  59. # This should be one part too long (eg. 9 segments)
  60. self.assertRaises(ValueError, addr_parse, "2001:db8:0:0:0:0:0:0:0")
  61. # Only one :: allowed
  62. self.assertRaises(ValueError, addr_parse, "2001::db8::c")
  63. def test_success(self):
  64. """
  65. Test if it succeeds on valid inputs and returns addresses that look
  66. the same.
  67. """
  68. self.assertEqual("192.0.2.0", str(addr_parse("192.0.2.0")))
  69. # The OS could return something else than canonical form, in which
  70. # case the test would fail. However, I do not see an easy way to fix
  71. # the test, so it is left this way unless someone finds an OS that
  72. # does return something else.
  73. self.assertEqual("2001:bd8::", str(addr_parse("2001:bd8::")))
  74. # It should strip the unnecesarry parts
  75. self.assertEqual("2001:bd8::", str(addr_parse("2001:bd8:0:0:0:0:0:0")))
  76. self.assertEqual("::", str(addr_parse("::")))
  77. self.assertEqual("2001:bd8::", str(addr_parse("2001:bd8::0.0.0.0")))
  78. if __name__ == "__main__":
  79. unittest.main()