PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/tests/simian/mac/common/ipcalc_test.py

http://simian.googlecode.com/
Python | 132 lines | 75 code | 26 blank | 31 comment | 4 complexity | 6724131d78c12ed1a1e23893a259261a MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2012 Google Inc. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS-IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # #
  17. """ip module tests."""
  18. import socket
  19. import struct
  20. from google.apputils import app
  21. from google.apputils import basetest
  22. import mox
  23. import stubout
  24. from simian.mac.common import ipcalc
  25. class IpModuleTest(mox.MoxTestBase):
  26. def setUp(self):
  27. mox.MoxTestBase.setUp(self)
  28. self.stubs = stubout.StubOutForTesting()
  29. def tearDown(self):
  30. self.mox.UnsetStubs()
  31. self.stubs.UnsetAll()
  32. def _socket_ip2int(self, ip_str):
  33. """Convert an IP string to int with code other than ours.
  34. Args:
  35. ip_str: str
  36. Returns:
  37. int in network byte order
  38. """
  39. i = struct.unpack('I', socket.inet_aton(ip_str))[0]
  40. i = socket.htonl(i)
  41. # on python 2.5 socket.htonl() returns a signed int, on later versions
  42. # python returns a python long type to return the number without sign.
  43. if i < 0:
  44. i = 4294967296L - (long(i) * -1)
  45. return i
  46. def testIpToIntWhenIpv6(self):
  47. """Test IpToInt() when passed ipv6."""
  48. ip_str = '2620::1003:1004:129a:ddff:fe60:fb46'
  49. self.assertRaises(ValueError, ipcalc.IpToInt, ip_str)
  50. def testIpToInt(self):
  51. """Test IpToInt()."""
  52. ip_tests = [
  53. ['192.168.0.0', 3232235520],
  54. ['10.0.0.5', 167772165],
  55. ]
  56. for ip_str, ip_int_expected in ip_tests:
  57. self.assertEqual(ip_int_expected, self._socket_ip2int(ip_str))
  58. self.assertEqual(ip_int_expected, ipcalc.IpToInt(ip_str))
  59. def testIpMaskToInts(self):
  60. """Test IpMaskToInts()."""
  61. mask_str = '1.2.3.4/8'
  62. ip_mask_ints_expected = (
  63. self._socket_ip2int('1.2.3.4'),
  64. self._socket_ip2int('255.0.0.0'),
  65. )
  66. self.assertEqual(ip_mask_ints_expected, ipcalc.IpMaskToInts(mask_str))
  67. def testIpMaskToIntsWhenIpv6(self):
  68. """Test IpMaskToInts() when ipv6."""
  69. self.assertRaises(
  70. ValueError, ipcalc.IpMaskToInts, 'fe80::be30:5bff:fed6:764f/64')
  71. def testIpMaskMatch(self):
  72. """Test IpMaskMatch()."""
  73. ip_tests = [
  74. ['192.168.0.0', '192.168.0.0/25', True],
  75. ['192.168.0.127', '192.168.0.0/25', True],
  76. ['192.168.0.128', '192.168.0.0/25', False],
  77. ['192.168.0.0', '192.168.0.0/24', True],
  78. ['192.168.0.255', '192.168.0.0/24', True],
  79. ['192.168.0.100', '192.168.1.0/24', False],
  80. ['192.168.0.0', '192.168.0.0/23', True],
  81. ['192.168.1.0', '192.168.0.0/23', True],
  82. ['192.168.1.255', '192.168.0.0/23', True],
  83. ['192.168.2.1', '192.168.0.0/23', False],
  84. ['192.168.0.0', '192.168.0.0/22', True],
  85. ['192.168.1.0', '192.168.0.0/22', True],
  86. ['192.168.1.255', '192.168.0.0/22', True],
  87. ['192.168.2.255', '192.168.0.0/22', True],
  88. ['192.168.3.255', '192.168.0.0/22', True],
  89. ['192.168.4.0', '192.168.0.0/22', False],
  90. ['10.0.0.0', '10.0.0.0/8', True],
  91. ['10.0.0.1', '10.0.0.0/8', True],
  92. ['10.0.1.0', '10.0.0.0/8', True],
  93. ['10.1.0.0', '10.0.0.0/8', True],
  94. ['10.1.2.3', '10.0.0.0/8', True],
  95. ['10.255.255.255', '10.0.0.0/8', True],
  96. ['11.0.0.0', '10.0.0.0/8', False],
  97. ]
  98. for ip, ip_mask, expected in ip_tests:
  99. self.assertEqual(
  100. expected, ipcalc.IpMaskMatch(ip, ip_mask),
  101. '%s %s expected %s' % (ip, ip_mask, expected))
  102. def main(unused_argv):
  103. basetest.main()
  104. if __name__ == '__main__':
  105. app.run()