test_Utility.py 9.4 KB

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