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 and byte.to_bytes(1, "little") not in Utility.x86_pseudo_nops:
  98. correct = False
  99. break
  100. self.assertTrue(correct)
  101. def test_get_rnd_x86_nop_without_sideeffects(self):
  102. result = Utility.get_rnd_x86_nop(1000, True)
  103. correct = True
  104. for byte in result:
  105. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  106. correct = False
  107. break
  108. self.assertTrue(correct)
  109. def test_get_rnd_x86_nop_filter(self):
  110. result = Utility.get_rnd_x86_nop(1000, False, Utility.x86_nops.copy())
  111. correct = True
  112. for byte in result:
  113. if byte.to_bytes(1, "little") in Utility.x86_nops:
  114. correct = False
  115. break
  116. self.assertTrue(correct)
  117. def test_get_rnd_x86_nop_single_filter(self):
  118. result = Utility.get_rnd_x86_nop(1000, False, b'\x20')
  119. correct = True
  120. for byte in result:
  121. if byte.to_bytes(1, "little") == b'\x20':
  122. correct = False
  123. break
  124. self.assertTrue(correct)
  125. def test_get_rnd_bytes_number(self):
  126. result = Utility.get_rnd_bytes(1000)
  127. self.assertEqual(len(result), 1000)
  128. def test_get_rnd_bytes_filter(self):
  129. result = Utility.get_rnd_bytes(1000, Utility.x86_pseudo_nops.copy())
  130. correct = True
  131. for byte in result:
  132. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  133. correct = False
  134. self.assertTrue(correct)
  135. def test_get_bytes_from_file_invalid_path(self):
  136. with self.assertRaises(SystemExit):
  137. Utility.get_bytes_from_file(Lib.test_resource_dir + "/NonExistingFile.txt")
  138. def test_get_bytes_from_file_invalid_header(self):
  139. with self.assertRaises(SystemExit):
  140. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidHeader.txt")
  141. def test_get_bytes_from_file_invalid_hexfile(self):
  142. with self.assertRaises(SystemExit):
  143. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidHexFile.txt")
  144. def test_get_bytes_from_file_invalid_strfile(self):
  145. with self.assertRaises(SystemExit):
  146. Utility.get_bytes_from_file(Lib.test_resource_dir + "/InvalidStringFile.txt")
  147. def test_get_bytes_from_file_str(self):
  148. result = Utility.get_bytes_from_file(Lib.test_resource_dir + "/StringTestFile.txt")
  149. self.assertEqual(result, b'This is a string-test')
  150. def test_get_bytes_from_file_hex(self):
  151. result = Utility.get_bytes_from_file(Lib.test_resource_dir + "/HexTestFile.txt")
  152. self.assertEqual(result, b'\xab\xcd\xef\xff\x10\xff\xaa\xab')
  153. def test_handle_most_used_outputs_empty(self):
  154. self.assertIsNone(Utility.handle_most_used_outputs([]))
  155. def test_handle_most_used_outputs_one(self):
  156. test_input = "SomeTest"
  157. self.assertEqual(Utility.handle_most_used_outputs(test_input), test_input)
  158. def test_handle_most_used_outputs_one_list(self):
  159. test_input = ["SomeTest"]
  160. self.assertEqual(Utility.handle_most_used_outputs(test_input), test_input[0])
  161. def test_handle_most_used_outputs_list_sorted(self):
  162. test_input = [0, 1, 2, 3, 4]
  163. self.assertEqual(Utility.handle_most_used_outputs(test_input), 0)
  164. def test_handle_most_used_outputs_list_unsorted(self):
  165. test_input = [2, 4, 0, 1, 3]
  166. self.assertEqual(Utility.handle_most_used_outputs(test_input), 0)
  167. def test_check_payload_len_exceeded(self):
  168. with self.assertRaises(SystemExit):
  169. Utility.check_payload_len(10, 5)
  170. def test_check_payload_len_valid(self):
  171. try:
  172. Utility.check_payload_len(5, 10)
  173. except SystemExit:
  174. self.fail()
  175. # TODO: get_attacker_config Tests