test_Utility.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. Utility.check_platform("linux")
  44. def test_check_platform_invalid(self):
  45. with self.assertRaises(SystemExit):
  46. Utility.check_platform("abc")
  47. def test_get_ip_range_forwards(self):
  48. start = "192.168.178.254"
  49. end = "192.168.179.1"
  50. result = ["192.168.178.254", "192.168.178.255", "192.168.179.0", "192.168.179.1"]
  51. self.assertEqual(Utility.get_ip_range(start, end), result)
  52. def test_get_ip_range_backwards(self):
  53. end = "192.168.178.254"
  54. start = "192.168.179.1"
  55. result = ["192.168.179.1", "192.168.179.0", "192.168.178.255", "192.168.178.254"]
  56. self.assertEqual(Utility.get_ip_range(start, end), result)
  57. def test_get_ip_range_equal(self):
  58. end = "192.168.178.254"
  59. start = "192.168.178.254"
  60. result = ["192.168.178.254"]
  61. self.assertEqual(Utility.get_ip_range(start, end), result)
  62. def test_generate_source_port_from_platform_invalid(self):
  63. with self.assertRaises(SystemExit):
  64. Utility.generate_source_port_from_platform("abc")
  65. def test_generate_source_port_from_platform_oldwin_firstport(self):
  66. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp") <= 5000)
  67. def test_generate_source_port_from_platform_oldwin_nextport(self):
  68. self.assertEqual(Utility.generate_source_port_from_platform("winxp", 2000), 2001)
  69. def test_generate_source_port_from_platform_oldwin_maxport(self):
  70. self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp", 5000) <= 5000)
  71. def test_generate_source_port_from_platform_linux(self):
  72. self.assertTrue(32768 <= Utility.generate_source_port_from_platform("linux") <= 61000)
  73. def test_generate_source_port_from_platform_newwinmac_firstport(self):
  74. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7") <= 65535)
  75. def test_generate_source_port_from_platform_newwinmac_nextport(self):
  76. self.assertEqual(Utility.generate_source_port_from_platform("win7", 50000), 50001)
  77. def test_generate_source_port_from_platform_newwinmac_maxport(self):
  78. self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7", 65535) <= 65535)
  79. # Test get_filetime_format????
  80. def test_get_rnd_boot_time_invalid(self):
  81. with self.assertRaises(SystemExit):
  82. Utility.get_rnd_boot_time(10, "abc")
  83. def test_get_rnd_boot_time_linux(self):
  84. self.assertTrue(Utility.get_rnd_boot_time(100, "linux") < 100)
  85. def test_get_rnd_boot_time_macos(self):
  86. self.assertTrue(Utility.get_rnd_boot_time(100, "macos") < 100)
  87. def test_get_rnd_boot_time_win(self):
  88. self.assertTrue(Utility.get_rnd_boot_time(100, "win7") < 100)
  89. def test_get_rnd_x86_nop_len(self):
  90. result = Utility.get_rnd_x86_nop(1000)
  91. self.assertEqual(len(result), 1000)
  92. def test_get_rnd_x86_nop_with_sideeffects(self):
  93. result = Utility.get_rnd_x86_nop(1000, False)
  94. correct = True
  95. for byte in result:
  96. if byte.to_bytes(1, "little") not in Utility.x86_nops and byte.to_bytes(1, "little") not in Utility.x86_pseudo_nops:
  97. correct = False
  98. self.assertTrue(correct)
  99. def test_get_rnd_x86_nop_without_sideeffects(self):
  100. result = Utility.get_rnd_x86_nop(1000, True)
  101. correct = True
  102. for byte in result:
  103. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  104. correct = False
  105. self.assertTrue(correct)
  106. def test_get_rnd_x86_nop_filter(self):
  107. result = Utility.get_rnd_x86_nop(1000, False, Utility.x86_nops.copy())
  108. correct = True
  109. for byte in result:
  110. if byte.to_bytes(1, "little") in Utility.x86_nops:
  111. correct = False
  112. self.assertTrue(correct)
  113. def test_get_rnd_x86_nop_single_filter(self):
  114. result = Utility.get_rnd_x86_nop(1000, False, b'\x20')
  115. correct = True
  116. for byte in result:
  117. if byte.to_bytes(1, "little") == b'\x20':
  118. correct = False
  119. self.assertTrue(correct)
  120. def test_get_rnd_bytes_number(self):
  121. result = Utility.get_rnd_bytes(1000)
  122. self.assertEqual(len(result), 1000)
  123. def test_get_rnd_bytes_filter(self):
  124. result = Utility.get_rnd_bytes(1000, Utility.x86_pseudo_nops.copy())
  125. correct = True
  126. for byte in result:
  127. if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
  128. correct = False
  129. self.assertTrue(correct)
  130. def test_get_bytes_from_file_invalid_path(self):
  131. with self.assertRaises(SystemExit):
  132. Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/NonExistingFile.txt")
  133. def test_get_bytes_from_file_invalid_header(self):
  134. with self.assertRaises(SystemExit):
  135. Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/InvalidHeader.txt")
  136. def test_get_bytes_from_file_invalid_hexfile(self):
  137. with self.assertRaises(SystemExit):
  138. Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/InvalidHexFile.txt")
  139. def test_get_bytes_from_file_invalid_strfile(self):
  140. with self.assertRaises(SystemExit):
  141. Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/InvalidStringFile.txt")
  142. def test_get_bytes_from_file_str(self):
  143. result = Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/StringTestFile.txt")
  144. self.assertEqual(result, b'This is a string-test')
  145. def test_get_bytes_from_file_hex(self):
  146. result = Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/HexTestFile.txt")
  147. self.assertEqual(result, b'\xab\xcd\xef\xff\x10\xff\xaa\xab')