3
0

test_Utility.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from unittest import TestCase
  2. from ID2TLib.Utility import *
  3. from Test.Lib import test_resource_dir
  4. class TestUtility(TestCase):
  5. def test_update_timestamp_no_delay(self):
  6. self.assertTrue(100+10/5 >= update_timestamp(100, 5) >= 100+1/5)
  7. def test_update_timestamp_with_delay(self):
  8. self.assertTrue(100+1/5+10*100 >= update_timestamp(100, 5, 10) >= 100+1/5+10)
  9. def test_update_timestamp_comparison(self):
  10. self.assertTrue(update_timestamp(100, 5) <= 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(get_interval_pps(cipps, 3), 1)
  14. self.assertEqual(get_interval_pps(cipps, 7), 2)
  15. self.assertEqual(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(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(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(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(get_nth_random_element(letters), letters)
  33. def test_index_increment_not_max(self):
  34. self.assertEqual(index_increment(5, 10), 6)
  35. def test_index_increment_max(self):
  36. self.assertEqual(index_increment(10, 10), 0)
  37. # Correct?
  38. def test_index_increment_max2(self):
  39. self.assertEqual(index_increment(9, 10), 0)
  40. def test_get_rnd_os(self):
  41. self.assertIn(get_rnd_os(), platforms)
  42. def test_check_platform_valid(self):
  43. check_platform("linux")
  44. def test_check_platform_invalid(self):
  45. with self.assertRaises(SystemExit):
  46. 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(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(get_ip_range(start, end), result)
  57. def test_generate_source_port_from_platform_invalid(self):
  58. with self.assertRaises(SystemExit):
  59. generate_source_port_from_platform("abc")
  60. def test_generate_source_port_from_platform_oldwin_firstport(self):
  61. self.assertTrue(1024 <= generate_source_port_from_platform("winxp") <= 5000)
  62. def test_generate_source_port_from_platform_oldwin_nextport(self):
  63. self.assertEqual(generate_source_port_from_platform("winxp", 2000), 2001)
  64. def test_generate_source_port_from_platform_oldwin_maxport(self):
  65. self.assertTrue(1024 <= generate_source_port_from_platform("winxp", 5000) <= 5000)
  66. def test_generate_source_port_from_platform_linux(self):
  67. self.assertTrue(32768 <= generate_source_port_from_platform("linux") <= 61000)
  68. def test_generate_source_port_from_platform_newwinmac_firstport(self):
  69. self.assertTrue(49152 <= generate_source_port_from_platform("win7") <= 65535)
  70. def test_generate_source_port_from_platform_newwinmac_nextport(self):
  71. self.assertEqual(generate_source_port_from_platform("win7", 50000), 50001)
  72. def test_generate_source_port_from_platform_newwinmac_maxport(self):
  73. self.assertTrue(49152 <= generate_source_port_from_platform("win7", 65535) <= 65535)
  74. # Test get_filetime_format????
  75. def test_get_rnd_boot_time_invalid(self):
  76. with self.assertRaises(SystemExit):
  77. get_rnd_boot_time(10, "abc")
  78. def test_get_rnd_boot_time_linux(self):
  79. self.assertTrue(get_rnd_boot_time(100, "linux") < 100)
  80. def test_get_rnd_boot_time_macos(self):
  81. self.assertTrue(get_rnd_boot_time(100, "macos") < 100)
  82. def test_get_rnd_boot_time_win(self):
  83. self.assertTrue(get_rnd_boot_time(100, "win7") < 100)
  84. def test_get_rnd_x86_nop_len(self):
  85. result = get_rnd_x86_nop(1000)
  86. self.assertEqual(len(result), 1000)
  87. def test_get_rnd_x86_nop_with_sideeffects(self):
  88. result = get_rnd_x86_nop(1000, False)
  89. correct = True
  90. for byte in result:
  91. if byte.to_bytes(1, "little") not in x86_nops and byte.to_bytes(1, "little") not in x86_pseudo_nops:
  92. correct = False
  93. self.assertTrue(correct)
  94. def test_get_rnd_x86_nop_without_sideeffects(self):
  95. result = get_rnd_x86_nop(1000, True)
  96. correct = True
  97. for byte in result:
  98. if byte.to_bytes(1, "little") in x86_pseudo_nops:
  99. correct = False
  100. self.assertTrue(correct)
  101. def test_get_rnd_x86_nop_filter(self):
  102. result = get_rnd_x86_nop(1000, False, x86_nops.copy())
  103. correct = True
  104. for byte in result:
  105. if byte.to_bytes(1, "little") in x86_nops:
  106. correct = False
  107. self.assertTrue(correct)
  108. def test_get_rnd_bytes_number(self):
  109. result = get_rnd_bytes(1000)
  110. self.assertEqual(len(result), 1000)
  111. def test_get_rnd_bytes_filter(self):
  112. result = get_rnd_bytes(1000, x86_pseudo_nops.copy())
  113. correct = True
  114. for byte in result:
  115. if byte.to_bytes(1, "little") in x86_pseudo_nops:
  116. correct = False
  117. self.assertTrue(correct)
  118. def test_get_bytes_from_file_invalid_path(self):
  119. with self.assertRaises(SystemExit):
  120. get_bytes_from_file(test_resource_dir+"/NonExistingFile.txt")
  121. def test_get_bytes_from_file_invalid_header(self):
  122. with self.assertRaises(SystemExit):
  123. get_bytes_from_file(test_resource_dir+"/InvalidHeader.txt")
  124. def test_get_bytes_from_file_invalid_hexfile(self):
  125. with self.assertRaises(SystemExit):
  126. get_bytes_from_file(test_resource_dir+"/InvalidHexFile.txt")
  127. def test_get_bytes_from_file_str(self):
  128. result = get_bytes_from_file(test_resource_dir+"/StringTestFile.txt")
  129. self.assertEqual(result, b'This is a string-test')
  130. def test_get_bytes_from_file_hex(self):
  131. result = get_bytes_from_file(test_resource_dir+"/HexTestFile.txt")
  132. self.assertEqual(result, b'\xab\xcd\xef\xff\x10\xff\xaa\xab')