test_Utility.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. # TODO: ???
  32. #def test_get_nth_random_element_single_list(self):
  33. #letters = ["A", "B", "C"]
  34. #self.assertIn(Utility.get_nth_random_element(letters), letters)
  35. def test_index_increment_not_max(self):
  36. self.assertEqual(Utility.index_increment(5, 10), 6)
  37. def test_index_increment_max(self):
  38. self.assertEqual(Utility.index_increment(10, 10), 0)
  39. # Correct?
  40. def test_index_increment_max2(self):
  41. self.assertEqual(Utility.index_increment(9, 10), 0)
  42. def test_get_rnd_os(self):
  43. self.assertIn(Utility.get_rnd_os(), Utility.platforms)
  44. def test_check_platform_valid(self):
  45. try:
  46. Utility.check_platform("linux")
  47. except SystemExit:
  48. self.fail()
  49. def test_check_platform_invalid(self):
  50. with self.assertRaises(SystemExit):
  51. Utility.check_platform("abc")
  52. def test_get_ip_range_forwards(self):
  53. start = "192.168.178.254"
  54. end = "192.168.179.1"
  55. result = ["192.168.178.254", "192.168.178.255", "192.168.179.0", "192.168.179.1"]
  56. self.assertEqual(Utility.get_ip_range(start, end), result)
  57. def test_get_ip_range_backwards(self):
  58. end = "192.168.178.254"
  59. start = "192.168.179.1"
  60. result = ["192.168.179.1", "192.168.179.0", "192.168.178.255", "192.168.178.254"]
  61. self.assertEqual(Utility.get_ip_range(start, end), result)
  62. def test_get_ip_range_equal(self):
  63. end = "192.168.178.254"
  64. start = "192.168.178.254"
  65. result = ["192.168.178.254"]
  66. self.assertEqual(Utility.get_ip_range(start, end), result)
  67. def test_generate_source_port_from_platform_invalid(self):
  68. with self.assertRaises(SystemExit):
  69. Utility.generate_source_port_from_platform("abc")
  70. def test_generate_source_port_from_platform_oldwin_firstport(self):
  71. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp") <= 5000)
  72. def test_generate_source_port_from_platform_oldwin_nextport(self):
  73. self.assertEqual(Utility.generate_source_port_from_platform("winxp", 2000), 2001)
  74. def test_generate_source_port_from_platform_oldwin_maxport(self):
  75. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp", 5000) <= 5000)
  76. def test_generate_source_port_from_platform_linux(self):
  77. self.assertTrue(32768 <= Utility.generate_source_port_from_platform("linux") <= 61000)
  78. def test_generate_source_port_from_platform_newwinmac_firstport(self):
  79. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7") <= 65535)
  80. def test_generate_source_port_from_platform_newwinmac_nextport(self):
  81. self.assertEqual(Utility.generate_source_port_from_platform("win7", 50000), 50001)
  82. def test_generate_source_port_from_platform_newwinmac_maxport(self):
  83. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7", 65535) <= 65535)
  84. # TODO: get_filetime_format Test
  85. def test_get_rnd_boot_time_invalid(self):
  86. with self.assertRaises(SystemExit):
  87. Utility.get_rnd_boot_time(10, "abc")
  88. def test_get_rnd_boot_time_linux(self):
  89. self.assertTrue(Utility.get_rnd_boot_time(100, "linux") < 100)
  90. def test_get_rnd_boot_time_macos(self):
  91. self.assertTrue(Utility.get_rnd_boot_time(100, "macos") < 100)
  92. def test_get_rnd_boot_time_win(self):
  93. self.assertTrue(Utility.get_rnd_boot_time(100, "win7") < 100)
  94. def test_get_rnd_x86_nop_len(self):
  95. result = Utility.get_rnd_x86_nop(1000)
  96. self.assertEqual(len(result), 1000)
  97. def test_get_rnd_x86_nop_with_sideeffects(self):
  98. result = Utility.get_rnd_x86_nop(1000, False)
  99. correct = True
  100. for byte in result:
  101. if byte.to_bytes(1, "little") not in Utility.x86_nops and byte.to_bytes(1, "little") not in Utility.x86_pseudo_nops:
  102. correct = False
  103. self.assertTrue(correct)
  104. def test_get_rnd_x86_nop_without_sideeffects(self):
  105. result = Utility.get_rnd_x86_nop(1000, True)
  106. correct = True
  107. for byte in result:
  108. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  109. correct = False
  110. self.assertTrue(correct)
  111. def test_get_rnd_x86_nop_filter(self):
  112. result = Utility.get_rnd_x86_nop(1000, False, Utility.x86_nops.copy())
  113. correct = True
  114. for byte in result:
  115. if byte.to_bytes(1, "little") in Utility.x86_nops:
  116. correct = False
  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. 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