123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import unittest
- import ID2TLib.TestLibrary as TestLibrary
- import ID2TLib.Utility as Utility
- class TestUtility(unittest.TestCase):
- def test_update_timestamp_no_delay(self):
- self.assertTrue(100+10/5 >= Utility.update_timestamp(100, 5) >= 100+1/5)
- def test_update_timestamp_with_delay(self):
- self.assertTrue(100+1/5+10*100 >= Utility.update_timestamp(100, 5, 10) >= 100+1/5+10)
- def test_update_timestamp_comparison(self):
- self.assertTrue(Utility.update_timestamp(100, 5) <= Utility.update_timestamp(100, 5, 10))
- def test_get_interval_pps_below_max(self):
- cipps = [(5, 1), (10, 2), (15, 3)]
- self.assertEqual(Utility.get_interval_pps(cipps, 3), 1)
- self.assertEqual(Utility.get_interval_pps(cipps, 7), 2)
- self.assertEqual(Utility.get_interval_pps(cipps, 12), 3)
- def test_get_interval_pps_above_max(self):
- cipps = [(5, 1), (10, 2), (15, 3)]
- self.assertEqual(Utility.get_interval_pps(cipps, 30), 3)
- # Errors if empty list and result bad if only one list
- def test_get_nth_random_element_equal_no(self):
- letters = ["A", "B", "C"]
- numbers = [1, 2, 3]
- results = [("A", 1), ("B", 2), ("C", 3)]
- self.assertIn(Utility.get_nth_random_element(letters, numbers), results)
- def test_get_nth_random_element_unequal_no(self):
- letters = ["A", "B", "C"]
- numbers = [1, 2]
- results = [("A", 1), ("B", 2)]
- self.assertIn(Utility.get_nth_random_element(letters, numbers), results)
- #def test_get_nth_random_element_single_list(self):
- #letters = ["A", "B", "C"]
- #self.assertIn(Utility.get_nth_random_element(letters), letters)
- def test_index_increment_not_max(self):
- self.assertEqual(Utility.index_increment(5, 10), 6)
- def test_index_increment_max(self):
- self.assertEqual(Utility.index_increment(10, 10), 0)
- # Correct?
- def test_index_increment_max2(self):
- self.assertEqual(Utility.index_increment(9, 10), 0)
- def test_get_rnd_os(self):
- self.assertIn(Utility.get_rnd_os(), Utility.platforms)
- def test_check_platform_valid(self):
- Utility.check_platform("linux")
- def test_check_platform_invalid(self):
- with self.assertRaises(SystemExit):
- Utility.check_platform("abc")
- def test_get_ip_range_forwards(self):
- start = "192.168.178.254"
- end = "192.168.179.1"
- result = ["192.168.178.254", "192.168.178.255", "192.168.179.0", "192.168.179.1"]
- self.assertEqual(Utility.get_ip_range(start, end), result)
- def test_get_ip_range_backwards(self):
- end = "192.168.178.254"
- start = "192.168.179.1"
- result = ["192.168.179.1", "192.168.179.0", "192.168.178.255", "192.168.178.254"]
- self.assertEqual(Utility.get_ip_range(start, end), result)
- def test_get_ip_range_equal(self):
- end = "192.168.178.254"
- start = "192.168.178.254"
- result = ["192.168.178.254"]
- self.assertEqual(Utility.get_ip_range(start, end), result)
- def test_generate_source_port_from_platform_invalid(self):
- with self.assertRaises(SystemExit):
- Utility.generate_source_port_from_platform("abc")
- def test_generate_source_port_from_platform_oldwin_firstport(self):
- self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp") <= 5000)
- def test_generate_source_port_from_platform_oldwin_nextport(self):
- self.assertEqual(Utility.generate_source_port_from_platform("winxp", 2000), 2001)
- def test_generate_source_port_from_platform_oldwin_maxport(self):
- self.assertTrue(1024 <= Utility.generate_source_port_from_platform("winxp", 5000) <= 5000)
- def test_generate_source_port_from_platform_linux(self):
- self.assertTrue(32768 <= Utility.generate_source_port_from_platform("linux") <= 61000)
- def test_generate_source_port_from_platform_newwinmac_firstport(self):
- self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7") <= 65535)
- def test_generate_source_port_from_platform_newwinmac_nextport(self):
- self.assertEqual(Utility.generate_source_port_from_platform("win7", 50000), 50001)
- def test_generate_source_port_from_platform_newwinmac_maxport(self):
- self.assertTrue(49152 <= Utility.generate_source_port_from_platform("win7", 65535) <= 65535)
- # Test get_filetime_format????
- def test_get_rnd_boot_time_invalid(self):
- with self.assertRaises(SystemExit):
- Utility.get_rnd_boot_time(10, "abc")
- def test_get_rnd_boot_time_linux(self):
- self.assertTrue(Utility.get_rnd_boot_time(100, "linux") < 100)
- def test_get_rnd_boot_time_macos(self):
- self.assertTrue(Utility.get_rnd_boot_time(100, "macos") < 100)
- def test_get_rnd_boot_time_win(self):
- self.assertTrue(Utility.get_rnd_boot_time(100, "win7") < 100)
- def test_get_rnd_x86_nop_len(self):
- result = Utility.get_rnd_x86_nop(1000)
- self.assertEqual(len(result), 1000)
- def test_get_rnd_x86_nop_with_sideeffects(self):
- result = Utility.get_rnd_x86_nop(1000, False)
- correct = True
- for byte in result:
- if byte.to_bytes(1, "little") not in Utility.x86_nops and byte.to_bytes(1, "little") not in Utility.x86_pseudo_nops:
- correct = False
- self.assertTrue(correct)
- def test_get_rnd_x86_nop_without_sideeffects(self):
- result = Utility.get_rnd_x86_nop(1000, True)
- correct = True
- for byte in result:
- if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
- correct = False
- self.assertTrue(correct)
- def test_get_rnd_x86_nop_filter(self):
- result = Utility.get_rnd_x86_nop(1000, False, Utility.x86_nops.copy())
- correct = True
- for byte in result:
- if byte.to_bytes(1, "little") in Utility.x86_nops:
- correct = False
- self.assertTrue(correct)
- def test_get_rnd_x86_nop_single_filter(self):
- result = Utility.get_rnd_x86_nop(1000, False, b'\x20')
- correct = True
- for byte in result:
- if byte.to_bytes(1, "little") == b'\x20':
- correct = False
- self.assertTrue(correct)
- def test_get_rnd_bytes_number(self):
- result = Utility.get_rnd_bytes(1000)
- self.assertEqual(len(result), 1000)
- def test_get_rnd_bytes_filter(self):
- result = Utility.get_rnd_bytes(1000, Utility.x86_pseudo_nops.copy())
- correct = True
- for byte in result:
- if byte.to_bytes(1, "little") in Utility.x86_pseudo_nops:
- correct = False
- self.assertTrue(correct)
- def test_get_bytes_from_file_invalid_path(self):
- with self.assertRaises(SystemExit):
- Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/NonExistingFile.txt")
- def test_get_bytes_from_file_invalid_header(self):
- with self.assertRaises(SystemExit):
- Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/InvalidHeader.txt")
- def test_get_bytes_from_file_invalid_hexfile(self):
- with self.assertRaises(SystemExit):
- Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/InvalidHexFile.txt")
- def test_get_bytes_from_file_invalid_strfile(self):
- with self.assertRaises(SystemExit):
- Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/InvalidStringFile.txt")
- def test_get_bytes_from_file_str(self):
- result = Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/StringTestFile.txt")
- self.assertEqual(result, b'This is a string-test')
- def test_get_bytes_from_file_hex(self):
- result = Utility.get_bytes_from_file(TestLibrary.test_resource_dir+"/HexTestFile.txt")
- self.assertEqual(result, b'\xab\xcd\xef\xff\x10\xff\xaa\xab')
|