test_BaseAttack.py 8.0 KB

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