test_Utility.py 9.1 KB

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