test_BaseAttack.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import unittest
  2. import Attack.BaseAttack as BA
  3. # TODO: improve coverage
  4. class TestBaseAttack(unittest.TestCase):
  5. def test_is_mac_address_valid(self):
  6. self.assertTrue(BA.BaseAttack._is_mac_address("00:80:41:ae:fd:7e"))
  7. def test_is_mac_address_invalid(self):
  8. self.assertFalse(BA.BaseAttack._is_mac_address("00:80:41:aec:fd:7e"))
  9. def test_is_mac_address_empty(self):
  10. self.assertFalse(BA.BaseAttack._is_mac_address(""))
  11. def test_is_mac_address_minus_valid(self):
  12. self.assertTrue(BA.BaseAttack._is_mac_address("00-80-41-ae-fd-7e"))
  13. def test_is_mac_address_minus_invalid(self):
  14. self.assertFalse(BA.BaseAttack._is_mac_address("00-80-41-aec-fd-7e"))
  15. def test_is_mac_address_list_valid(self):
  16. self.assertTrue(BA.BaseAttack._is_mac_address(["00:80:41:ae:fd:7e", "00-80-41-ae-fd-7e"]))
  17. def test_is_mac_address_list_invalid(self):
  18. self.assertFalse(BA.BaseAttack._is_mac_address(["00:80:41:aec:fd:7e", "00-80-41-aec-fd-7e"]))
  19. def test_is_ip_address_empty(self):
  20. self.assertFalse(BA.BaseAttack._is_ip_address("")[0])
  21. def test_is_ip_address_v4_valid(self):
  22. self.assertTrue(BA.BaseAttack._is_ip_address("192.168.178.1")[0])
  23. def test_is_ip_address_v4_invalid(self):
  24. self.assertFalse(BA.BaseAttack._is_ip_address("192.1689.178.1")[0])
  25. def test_is_ip_address_v6_valid(self):
  26. self.assertTrue(BA.BaseAttack._is_ip_address("2001:0db8:85a3:08d3:1319:8a2e:0370:7344")[0])
  27. def test_is_ip_address_v6_invalid(self):
  28. self.assertFalse(BA.BaseAttack._is_ip_address("2001:0db8:85a3:08d3X:1319:8a2e:0370:7344")[0])
  29. def test_is_ip_address_v6_shortened_valid(self):
  30. self.assertTrue(BA.BaseAttack._is_ip_address("2001:0db8:85a3:08d3:1319::0370:7344")[0])
  31. def test_is_ip_address_v6_shortened_invalid(self):
  32. self.assertFalse(BA.BaseAttack._is_ip_address("2001::85a3:08d3X::8a2e:0370:7344")[0])
  33. def test_is_ip_address_list_valid(self):
  34. self.assertTrue(BA.BaseAttack._is_ip_address(["192.168.178.1", "192.168.178.10"])[0])
  35. def test_is_ip_address_list_invalid(self):
  36. self.assertFalse(BA.BaseAttack._is_ip_address(["192.1689.178.1", "192.168.178.10"])[0])
  37. def test_is_ip_address_comma_list_valid(self):
  38. self.assertTrue(BA.BaseAttack._is_ip_address("192.168.178.1,192.168.178.10")[0])
  39. def test_is_ip_address_comma_list_invalid(self):
  40. self.assertFalse(BA.BaseAttack._is_ip_address("192.168.178.1,192.1689.178.10")[0])
  41. def test_is_port_none(self):
  42. self.assertFalse(BA.BaseAttack._is_port(None))
  43. def test_is_port_empty(self):
  44. self.assertFalse(BA.BaseAttack._is_port(""))
  45. def test_is_port_empty_list(self):
  46. self.assertFalse(BA.BaseAttack._is_port([]))
  47. def test_is_port_valid(self):
  48. self.assertTrue(BA.BaseAttack._is_port(5000))
  49. def test_is_port_invalid(self):
  50. self.assertFalse(BA.BaseAttack._is_port(70000))
  51. def test_is_port_string_valid(self):
  52. self.assertTrue(BA.BaseAttack._is_port("5000"))
  53. def test_is_port_string_invalid(self):
  54. self.assertFalse(BA.BaseAttack._is_port("70000"))
  55. def test_is_port_string_comma_valid(self):
  56. self.assertTrue(BA.BaseAttack._is_port("5000, 4000, 3000"))
  57. def test_is_port_string_comma_ivalid(self):
  58. self.assertFalse(BA.BaseAttack._is_port("5000, 70000, 3000"))
  59. def test_is_port_valid_list(self):
  60. self.assertTrue(BA.BaseAttack._is_port([5000, 4000, 3000]))
  61. def test_is_port_invalid_list(self):
  62. self.assertFalse(BA.BaseAttack._is_port([5000, 70000, 0]))
  63. def test_is_port_valid_string_list(self):
  64. self.assertTrue(BA.BaseAttack._is_port(["5000", "4000", "3000"]))
  65. def test_is_port_invalid_string_list(self):
  66. self.assertFalse(BA.BaseAttack._is_port(["5000", "70000", "0"]))
  67. def test_is_port_range_valid(self):
  68. self.assertTrue(BA.BaseAttack._is_port("3000-5000"))
  69. def test_is_port_range_invalid(self):
  70. self.assertFalse(BA.BaseAttack._is_port("0-70000"))
  71. def test_is_port_range_dots_valid(self):
  72. self.assertTrue(BA.BaseAttack._is_port("3000...5000"))
  73. def test_is_port_range_dots_invalid(self):
  74. self.assertFalse(BA.BaseAttack._is_port("0...70000"))
  75. def test_is_port_range_list_valid(self):
  76. self.assertTrue(BA.BaseAttack._is_port(["3000-5000", "6000-7000"]))
  77. def test_is_port_range_list_invalid(self):
  78. self.assertFalse(BA.BaseAttack._is_port(["0-70000", "6000-7000"]))
  79. def test_is_timestamp_valid(self):
  80. self.assertTrue(BA.BaseAttack._is_timestamp("2018-01-25 23:54:00"))
  81. def test_is_timestamp_invalid(self):
  82. self.assertFalse(BA.BaseAttack._is_timestamp("20-0100-125 23c:54x:00a"))
  83. def test_is_boolean_invalid(self):
  84. self.assertFalse(BA.BaseAttack._is_boolean("42")[0])
  85. def test_is_boolean_valid(self):
  86. self.assertTrue(BA.BaseAttack._is_boolean(True))
  87. self.assertTrue(BA.BaseAttack._is_boolean(False))
  88. def test_is_boolean_valid_strings(self):
  89. for value in {"y", "yes", "t", "true", "on", "1", "n", "no", "f", "false", "off", "0"}:
  90. with self.subTest(value=value):
  91. self.assertTrue(BA.BaseAttack._is_boolean(value))
  92. def test_is_float_valid(self):
  93. self.assertTrue(BA.BaseAttack._is_float(50.67)[0])
  94. def test_is_float_invalid(self):
  95. self.assertFalse(BA.BaseAttack._is_float("invalid")[0])
  96. def test_is_domain_valid(self):
  97. self.assertTrue(BA.BaseAttack._is_domain("foo://example.com:8042/over/there?name=ferret"))
  98. def test_is_domain_invalid(self):
  99. self.assertFalse(BA.BaseAttack._is_domain("this is not a valid domain, I guess, maybe, let's find out."))
  100. def test_is_valid_ipaddress_valid(self):
  101. self.assertTrue(BA.BaseAttack.is_valid_ip_address("192.168.178.42"))
  102. def test_is_valid_ipaddress_invalid(self):
  103. self.assertFalse(BA.BaseAttack.is_valid_ip_address("192.168.1789.42"))
  104. def test_ip_src_dst_equal_check_equal(self):
  105. with self.assertRaises(SystemExit):
  106. BA.BaseAttack.ip_src_dst_equal_check("192.168.178.42", "192.168.178.42")
  107. def test_ip_src_dst_equal_check_unequal(self):
  108. BA.BaseAttack.ip_src_dst_equal_check("192.168.178.42", "192.168.178.43")
  109. def test_clean_whitespaces(self):
  110. self.assertEqual("a\nb\rc\td\'e", BA.BaseAttack.clean_white_spaces("a\\nb\\rc\\td\\\'e"))
  111. def test_generate_random_ipv4_address(self):
  112. ip_list = BA.BaseAttack.generate_random_ipv4_address("Unknown", 10)
  113. for ip in ip_list:
  114. with self.subTest(ip=ip):
  115. self.assertTrue(BA.BaseAttack._is_ip_address(ip))
  116. def test_generate_random_ipv6_address(self):
  117. ip_list = BA.BaseAttack.generate_random_ipv6_address(10)
  118. for ip in ip_list:
  119. with self.subTest(ip=ip):
  120. self.assertTrue(BA.BaseAttack._is_ip_address(ip))
  121. def test_generate_random_mac_address(self):
  122. mac_list = BA.BaseAttack.generate_random_mac_address(10)
  123. for mac in mac_list:
  124. with self.subTest(mac=mac):
  125. self.assertTrue(BA.BaseAttack._is_mac_address(mac))