test_BaseAttack.py 7.4 KB

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