test_Utility.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. try:
  42. Utility.check_platform("linux")
  43. except SystemExit:
  44. self.fail()
  45. def test_check_platform_invalid(self):
  46. with self.assertRaises(SystemExit):
  47. Utility.check_platform("abc")
  48. def test_get_ip_range_forwards(self):
  49. start = "192.168.178.254"
  50. end = "192.168.179.1"
  51. result = ["192.168.178.254", "192.168.178.255", "192.168.179.0", "192.168.179.1"]
  52. self.assertEqual(Utility.get_ip_range(start, end), result)
  53. def test_get_ip_range_backwards(self):
  54. end = "192.168.178.254"
  55. start = "192.168.179.1"
  56. result = ["192.168.179.1", "192.168.179.0", "192.168.178.255", "192.168.178.254"]
  57. self.assertEqual(Utility.get_ip_range(start, end), result)
  58. def test_get_ip_range_equal(self):
  59. end = "192.168.178.254"
  60. start = "192.168.178.254"
  61. result = ["192.168.178.254"]
  62. self.assertEqual(Utility.get_ip_range(start, end), result)
  63. def test_generate_source_port_from_platform_invalid(self):
  64. with self.assertRaises(SystemExit):
  65. Utility.generate_source_port_from_platform("abc")
  66. def test_generate_source_port_from_platform_oldwin_firstport(self):
  67. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp") <= 5000)
  68. def test_generate_source_port_from_platform_oldwin_nextport(self):
  69. self.assertEqual(Utility.generate_source_port_from_platform("winxp", 2000), 2001)
  70. def test_generate_source_port_from_platform_oldwin_maxport(self):
  71. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp", 5000) <= 5000)
  72. def test_generate_source_port_from_platform_linux(self):
  73. self.assertTrue(32768 <= Utility.generate_source_port_from_platform("linux") <= 61000)
  74. def test_generate_source_port_from_platform_newwinmac_firstport(self):
  75. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7") <= 65535)
  76. def test_generate_source_port_from_platform_newwinmac_nextport(self):
  77. self.assertEqual(Utility.generate_source_port_from_platform("win7", 50000), 50001)
  78. def test_generate_source_port_from_platform_newwinmac_maxport(self):
  79. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7", 65535) <= 65535)
  80. # TODO: get_filetime_format Test
  81. def test_get_rnd_boot_time_invalid(self):
  82. with self.assertRaises(SystemExit):
  83. Utility.get_rnd_boot_time(10, "abc")
  84. def test_get_rnd_boot_time_linux(self):
  85. self.assertTrue(Utility.get_rnd_boot_time(100, "linux") < 100)
  86. def test_get_rnd_boot_time_macos(self):
  87. self.assertTrue(Utility.get_rnd_boot_time(100, "macos") < 100)
  88. def test_get_rnd_boot_time_win(self):
  89. self.assertTrue(Utility.get_rnd_boot_time(100, "win7") < 100)
  90. def test_get_rnd_x86_nop_len(self):
  91. result = Utility.get_rnd_x86_nop(1000)
  92. self.assertEqual(len(result), 1000)
  93. def test_get_rnd_x86_nop_with_sideeffects(self):
  94. result = Utility.get_rnd_x86_nop(1000, False)
  95. correct = True
  96. for byte in result:
  97. if byte.to_bytes(1, "little") not in Utility.x86_nops \
  98. and byte.to_bytes(1, "little") not in Utility.x86_pseudo_nops:
  99. correct = False
  100. break
  101. self.assertTrue(correct)
  102. def test_get_rnd_x86_nop_without_sideeffects(self):
  103. result = Utility.get_rnd_x86_nop(1000, True)
  104. correct = True
  105. for byte in result:
  106. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  107. correct = False
  108. break
  109. self.assertTrue(correct)
  110. def test_get_rnd_x86_nop_filter(self):
  111. result = Utility.get_rnd_x86_nop(1000, False, Utility.x86_nops.copy())
  112. correct = True
  113. for byte in result:
  114. if byte.to_bytes(1, "little") in Utility.x86_nops:
  115. correct = False
  116. break
  117. self.assertTrue(correct)
  118. def test_get_rnd_x86_nop_single_filter(self):
  119. result = Utility.get_rnd_x86_nop(1000, False, b'\x20')
  120. correct = True
  121. for byte in result:
  122. if byte.to_bytes(1, "little") == b'\x20':
  123. correct = False
  124. break
  125. self.assertTrue(correct)
  126. def test_get_rnd_bytes_number(self):
  127. result = Utility.get_rnd_bytes(1000)
  128. self.assertEqual(len(result), 1000)
  129. def test_get_rnd_bytes_filter(self):
  130. result = Utility.get_rnd_bytes(1000, Utility.x86_pseudo_nops.copy())
  131. correct = True
  132. for byte in result:
  133. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  134. correct = False
  135. self.assertTrue(correct)
  136. def test_get_bytes_from_file_invalid_path(self):
  137. with self.assertRaises(SystemExit):
  138. Utility.get_bytes_from_file(Lib.test_resource_dir + "/NonExistingFile.txt")
  139. def test_get_bytes_from_file_invalid_header(self):
  140. with self.assertRaises(SystemExit):
  141. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidHeader.txt")
  142. def test_get_bytes_from_file_invalid_hexfile(self):
  143. with self.assertRaises(SystemExit):
  144. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidHexFile.txt")
  145. def test_get_bytes_from_file_invalid_strfile(self):
  146. with self.assertRaises(SystemExit):
  147. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidStringFile.txt")
  148. def test_get_bytes_from_file_str(self):
  149. result = Utility.get_bytes_from_file(Lib.test_resource_dir + "/StringTestFile.txt")
  150. self.assertEqual(result, b'This is a string-test')
  151. def test_get_bytes_from_file_hex(self):
  152. result = Utility.get_bytes_from_file(Lib.test_resource_dir + "/HexTestFile.txt")
  153. self.assertEqual(result, b'\xab\xcd\xef\xff\x10\xff\xaa\xab')
  154. def test_handle_most_used_outputs_empty(self):
  155. self.assertIsNone(Utility.handle_most_used_outputs([]))
  156. def test_handle_most_used_outputs_one(self):
  157. test_input = "SomeTest"
  158. self.assertEqual(Utility.handle_most_used_outputs(test_input), test_input)
  159. def test_handle_most_used_outputs_one_list(self):
  160. test_input = ["SomeTest"]
  161. self.assertEqual(Utility.handle_most_used_outputs(test_input), test_input[0])
  162. def test_handle_most_used_outputs_list_sorted(self):
  163. test_input = [0, 1, 2, 3, 4]
  164. self.assertEqual(Utility.handle_most_used_outputs(test_input), 0)
  165. def test_handle_most_used_outputs_list_unsorted(self):
  166. test_input = [2, 4, 0, 1, 3]
  167. self.assertEqual(Utility.handle_most_used_outputs(test_input), 0)
  168. def test_check_payload_len_exceeded(self):
  169. with self.assertRaises(SystemExit):
  170. Utility.check_payload_len(10, 5)
  171. def test_check_payload_len_valid(self):
  172. try:
  173. Utility.check_payload_len(5, 10)
  174. except SystemExit:
  175. self.fail()
  176. # TODO: get_attacker_config Tests