test_net.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. from salt.utils.validate import net
  6. # Import Salt Testing Libs
  7. from tests.support.unit import TestCase
  8. class ValidateNetTestCase(TestCase):
  9. '''
  10. TestCase for salt.utils.validate.net module
  11. '''
  12. def test_ipv4_addr(self):
  13. '''
  14. Test IPv4 address validation
  15. '''
  16. true_addrs = [
  17. '127.0.0.1',
  18. '127.0.0.1',
  19. '127.0.0.19',
  20. '1.1.1.1/28',
  21. '127.0.0.11/32',
  22. ]
  23. false_addrs = [
  24. '127.0.0.911',
  25. '127.0.0911',
  26. '127.0.011',
  27. '127.0.011/32',
  28. '::1',
  29. '::1/128',
  30. '::1/28',
  31. ]
  32. for addr in true_addrs:
  33. self.assertTrue(net.ipv4_addr(addr))
  34. for addr in false_addrs:
  35. self.assertFalse(net.ipv4_addr(addr))
  36. def test_ipv6_addr(self):
  37. '''
  38. Test IPv6 address validation
  39. '''
  40. true_addrs = [
  41. '::',
  42. '::1',
  43. '::1/32',
  44. '::1/32',
  45. '::1/128',
  46. '2a03:4000:c:10aa:1017:f00d:aaaa:a',
  47. ]
  48. false_addrs = [
  49. '1.1.1.1',
  50. '::1/0',
  51. '::1/32d',
  52. '::1/129',
  53. '2a03:4000:c:10aa:1017:f00d:aaaa:a:4506',
  54. '2a03::1::2',
  55. ]
  56. for addr in true_addrs:
  57. self.assertTrue(net.ipv6_addr(addr))
  58. for addr in false_addrs:
  59. self.assertFalse(net.ipv6_addr(addr))