test_Utility.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import unittest
  2. import ID2TLib.TestLibrary as Lib
  3. import ID2TLib.Utility as Utility
  4. # TODO: improve coverage
  5. class TestUtility(unittest.TestCase):
  6. def test_update_timestamp_no_delay(self):
  7. self.assertTrue(100 + 10 / 5 >= Utility.update_timestamp(100, 5) >= 100 + 1 / 5)
  8. def test_update_timestamp_with_delay(self):
  9. self.assertTrue(100 + 1 / 5 + 10 * 100 >= Utility.update_timestamp(100, 5, 10) >= 100 + 1 / 5 + 10)
  10. def test_update_timestamp_comparison(self):
  11. self.assertTrue(Utility.update_timestamp(100, 5) <= Utility.update_timestamp(100, 5, 10))
  12. def test_get_interval_pps_below_max(self):
  13. cipps = [(5, 1), (10, 2), (15, 3)]
  14. self.assertEqual(Utility.get_interval_pps(cipps, 3), 1)
  15. self.assertEqual(Utility.get_interval_pps(cipps, 7), 2)
  16. self.assertEqual(Utility.get_interval_pps(cipps, 12), 3)
  17. def test_get_interval_pps_above_max(self):
  18. cipps = [(5, 1), (10, 2), (15, 3)]
  19. self.assertEqual(Utility.get_interval_pps(cipps, 30), 3)
  20. def test_get_nth_random_element_equal_no(self):
  21. letters = ["A", "B", "C"]
  22. numbers = [1, 2, 3]
  23. results = [("A", 1), ("B", 2), ("C", 3)]
  24. self.assertIn(Utility.get_nth_random_element(letters, numbers), results)
  25. def test_get_nth_random_element_unequal_no(self):
  26. letters = ["A", "B", "C"]
  27. numbers = [1, 2]
  28. results = [("A", 1), ("B", 2)]
  29. self.assertIn(Utility.get_nth_random_element(letters, numbers), results)
  30. def test_get_nth_random_element_single_list(self):
  31. letters = ["A", "B", "C"]
  32. self.assertIn(Utility.get_nth_random_element(letters), letters)
  33. def test_get_nth_random_element_empty_list(self):
  34. letters = ["A", "B", "C"]
  35. self.assertEqual(Utility.get_nth_random_element(letters, []), None)
  36. def test_get_nth_random_element_nothing(self):
  37. self.assertEqual(Utility.get_nth_random_element(), None)
  38. def test_get_rnd_os(self):
  39. self.assertIn(Utility.get_rnd_os(), Utility.platforms)
  40. def test_check_platform_valid(self):
  41. Utility.check_platform("linux")
  42. def test_check_platform_invalid(self):
  43. with self.assertRaises(ValueError):
  44. Utility.check_platform("abc")
  45. def test_get_ip_range_forwards(self):
  46. start = "192.168.178.254"
  47. end = "192.168.179.1"
  48. result = ["192.168.178.254", "192.168.178.255", "192.168.179.0", "192.168.179.1"]
  49. self.assertEqual(Utility.get_ip_range(start, end), result)
  50. def test_get_ip_range_backwards(self):
  51. end = "192.168.178.254"
  52. start = "192.168.179.1"
  53. result = ["192.168.179.1", "192.168.179.0", "192.168.178.255", "192.168.178.254"]
  54. self.assertEqual(Utility.get_ip_range(start, end), result)
  55. def test_get_ip_range_equal(self):
  56. end = "192.168.178.254"
  57. start = "192.168.178.254"
  58. result = ["192.168.178.254"]
  59. self.assertEqual(Utility.get_ip_range(start, end), result)
  60. def test_generate_source_port_from_platform_invalid(self):
  61. with self.assertRaises(ValueError):
  62. Utility.generate_source_port_from_platform("abc")
  63. def test_generate_source_port_from_platform_oldwin_firstport(self):
  64. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp") <= 5000)
  65. def test_generate_source_port_from_platform_oldwin_nextport(self):
  66. self.assertEqual(Utility.generate_source_port_from_platform("winxp", 2000), 2001)
  67. def test_generate_source_port_from_platform_oldwin_maxport(self):
  68. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp", 5000) <= 5000)
  69. def test_generate_source_port_from_platform_linux(self):
  70. self.assertTrue(32768 <= Utility.generate_source_port_from_platform("linux") <= 61000)
  71. def test_generate_source_port_from_platform_newwinmac_firstport(self):
  72. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7") <= 65535)
  73. def test_generate_source_port_from_platform_newwinmac_nextport(self):
  74. self.assertEqual(Utility.generate_source_port_from_platform("win7", 50000), 50001)
  75. def test_generate_source_port_from_platform_newwinmac_maxport(self):
  76. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7", 65535) <= 65535)
  77. # TODO: get_filetime_format Test
  78. def test_get_rnd_boot_time_invalid(self):
  79. with self.assertRaises(ValueError):
  80. Utility.get_rnd_boot_time(10, "abc")
  81. def test_get_rnd_boot_time_linux(self):
  82. self.assertTrue(Utility.get_rnd_boot_time(100, "linux") < 100)
  83. def test_get_rnd_boot_time_macos(self):
  84. self.assertTrue(Utility.get_rnd_boot_time(100, "macos") < 100)
  85. def test_get_rnd_boot_time_win(self):
  86. self.assertTrue(Utility.get_rnd_boot_time(100, "win7") < 100)
  87. def test_get_rnd_x86_nop_len(self):
  88. result = Utility.get_rnd_x86_nop(1000)
  89. self.assertEqual(len(result), 1000)
  90. def test_get_rnd_x86_nop_with_sideeffects(self):
  91. result = Utility.get_rnd_x86_nop(1000, False)
  92. for i in range(0, len(result)):
  93. with self.subTest(i=i):
  94. self.assertTrue(result[i].to_bytes(1, "little") in Utility.x86_nops or
  95. result[i].to_bytes(1, "little") in Utility.x86_pseudo_nops)
  96. def test_get_rnd_x86_nop_without_sideeffects(self):
  97. result = Utility.get_rnd_x86_nop(1000, True)
  98. for i in range(0, len(result)):
  99. with self.subTest(i=i):
  100. self.assertIn(result[i].to_bytes(1, "little"), Utility.x86_nops)
  101. self.assertNotIn(result[i].to_bytes(1, "little"), Utility.x86_pseudo_nops)
  102. def test_get_rnd_x86_nop_filter(self):
  103. result = Utility.get_rnd_x86_nop(1000, False, Utility.x86_nops.copy())
  104. for i in range(0, len(result)):
  105. with self.subTest(i=i):
  106. self.assertNotIn(result[i].to_bytes(1, "little"), Utility.x86_nops)
  107. def test_get_rnd_x86_nop_single_filter(self):
  108. result = Utility.get_rnd_x86_nop(1000, False, b'\x20')
  109. for i in range(0, len(result)):
  110. with self.subTest(i=i):
  111. self.assertNotEqual(result[i].to_bytes(1, "little"), b'\x20')
  112. def test_get_rnd_bytes_number(self):
  113. result = Utility.get_rnd_bytes(1000)
  114. self.assertEqual(len(result), 1000)
  115. def test_get_rnd_bytes_filter(self):
  116. result = Utility.get_rnd_bytes(1000, Utility.x86_pseudo_nops.copy())
  117. for i in range(0, len(result)):
  118. with self.subTest(i=i):
  119. self.assertNotIn(result[i].to_bytes(1, "little"), Utility.x86_pseudo_nops)
  120. def test_get_bytes_from_file_invalid_path(self):
  121. with self.assertRaises(SystemExit):
  122. Utility.get_bytes_from_file(Lib.test_resource_dir + "/NonExistingFile.txt")
  123. def test_get_bytes_from_file_invalid_header(self):
  124. with self.assertRaises(SystemExit):
  125. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidHeader.txt")
  126. def test_get_bytes_from_file_invalid_hexfile(self):
  127. with self.assertRaises(SystemExit):
  128. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidHexFile.txt")
  129. def test_get_bytes_from_file_invalid_strfile(self):
  130. with self.assertRaises(SystemExit):
  131. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidStringFile.txt")
  132. def test_get_bytes_from_file_str(self):
  133. result = Utility.get_bytes_from_file(Lib.test_resource_dir + "/StringTestFile.txt")
  134. self.assertEqual(result, b'This is a string-test')
  135. def test_get_bytes_from_file_hex(self):
  136. result = Utility.get_bytes_from_file(Lib.test_resource_dir + "/HexTestFile.txt")
  137. self.assertEqual(result, b'\xab\xcd\xef\xff\x10\xff\xaa\xab')
  138. def test_handle_most_used_outputs_empty(self):
  139. self.assertIsNone(Utility.handle_most_used_outputs([]))
  140. def test_handle_most_used_outputs_one(self):
  141. test_input = "SomeTest"
  142. self.assertEqual(Utility.handle_most_used_outputs(test_input), test_input)
  143. def test_handle_most_used_outputs_one_list(self):
  144. test_input = ["SomeTest"]
  145. self.assertEqual(Utility.handle_most_used_outputs(test_input), test_input[0])
  146. def test_handle_most_used_outputs_list_sorted(self):
  147. test_input = [0, 1, 2, 3, 4]
  148. self.assertEqual(Utility.handle_most_used_outputs(test_input), 0)
  149. def test_handle_most_used_outputs_list_unsorted(self):
  150. test_input = [2, 4, 0, 1, 3]
  151. self.assertEqual(Utility.handle_most_used_outputs(test_input), 0)
  152. def test_check_payload_len_exceeded(self):
  153. with self.assertRaises(ValueError):
  154. Utility.check_payload_len(10, 5)
  155. def test_check_payload_len_valid(self):
  156. Utility.check_payload_len(5, 10)
  157. def test_remove_generic_ending_attack(self):
  158. self.assertEqual(Utility.remove_generic_ending("someattack"), "some")
  159. def test_remove_generic_ending_exploit(self):
  160. self.assertEqual(Utility.remove_generic_ending("someexploit"), "some")
  161. def test_remove_generic_ending_wrong_ending(self):
  162. self.assertEqual(Utility.remove_generic_ending("somestuff"), "somestuff")
  163. # TODO: get_attacker_config Tests