test_Utility.py 9.2 KB

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