test_internalQueries.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import unittest
  2. import random
  3. import ID2TLib.TestLibrary as Lib
  4. import ID2TLib.Utility as Util
  5. import Core.Controller as Ctrl
  6. controller = Ctrl.Controller(pcap_file_path=Lib.test_pcap, do_extra_tests=False, non_verbose=True)
  7. controller.load_pcap_statistics(flag_write_file=False, flag_recalculate_stats=True, flag_print_statistics=False)
  8. controller_synth = Ctrl.Controller(pcap_file_path=Util.TEST_DIR + "synth_ttl_mss.pcap", do_extra_tests=False,
  9. non_verbose=True)
  10. controller_synth.load_pcap_statistics(flag_write_file=False, flag_recalculate_stats=True, flag_print_statistics=False)
  11. ipAddresses = ['10.0.2.15', '104.83.103.45', '13.107.21.200', '131.253.61.100', '172.217.23.142', '172.217.23.174',
  12. '192.168.33.254', '204.79.197.200', '23.51.123.27', '35.161.3.50', '52.11.17.245', '52.34.37.177',
  13. '52.39.210.199', '52.41.250.141', '52.85.173.182', '54.149.74.139', '54.187.98.195', '54.192.44.108',
  14. '54.192.44.177', '72.247.178.113', '72.247.178.67', '93.184.220.29']
  15. class UnitTestInternalQueries(unittest.TestCase):
  16. # FILE METAINFORMATION TESTS
  17. def test_get_file_information(self):
  18. self.assertEqual(controller.statistics.get_file_information(),
  19. [('Pcap file path', Lib.test_pcap),
  20. ('Total packet count', 1998, 'packets'),
  21. ("Recognized packets", 1988, "packets"),
  22. ("Unrecognized packets", 10, "PDUs"), ("% Recognized packets", 99.49949949949949, "%"),
  23. ("% Unrecognized packets", 0.5005005005005005, "%"),
  24. ("Last unknown PDU", '1970-01-01 00:07:39.604899'),
  25. ('Capture duration', '384.454345703125', 'seconds'),
  26. ('Capture start', '\t1970-01-01 00:01:45.647675'),
  27. ('Capture end', '\t1970-01-01 00:08:10.102034')])
  28. def test_get_packet_count(self):
  29. self.assertEqual(controller.statistics.get_packet_count(), 1998)
  30. def test_get_capture_duration(self):
  31. self.assertEqual(controller.statistics.get_capture_duration(), '384.454345703125')
  32. def test_get_pcap_timestamp_start(self):
  33. self.assertEqual(controller.statistics.get_pcap_timestamp_start(), '1970-01-01 00:01:45.647675')
  34. def test_get_pcap_timestamp_end(self):
  35. self.assertEqual(controller.statistics.get_pcap_timestamp_end(), '1970-01-01 00:08:10.102034')
  36. # FIXME: This seems to be the only testcase where float values differ slightly between macOS and Linux
  37. def test_get_general_file_statistics(self):
  38. file_stats = controller.statistics.get_general_file_statistics()
  39. self.assertEqual(file_stats[0], ('Avg. packet rate', 5.196976184844971, 'packets/sec'))
  40. self.assertEqual(file_stats[1], ('Avg. packet size', 0.0, 'kbytes'))
  41. self.assertEqual(file_stats[2], ('Avg. packets sent', 90.0, 'packets'))
  42. self.assertEqual(file_stats[3][0], 'Avg. bandwidth in')
  43. self.assertAlmostEqual(file_stats[3][1], 0.6302894353866577, places=5)
  44. self.assertEqual(file_stats[3][2], 'kbit/s')
  45. self.assertEqual(file_stats[4][0], 'Avg. bandwidth out')
  46. self.assertAlmostEqual(file_stats[4][1], 0.6302894353866577, places=5)
  47. # INTERNAL QUERY TESTS
  48. def test_get_ip_address_count(self):
  49. self.assertEqual(controller.statistics.get_ip_address_count(), 22)
  50. def test_get_ip_addresses(self):
  51. self.assertEqual(controller.statistics.get_ip_addresses(), ipAddresses)
  52. def test_get_most_used_ip_address(self):
  53. self.assertEqual(controller.statistics.get_most_used_ip_address(), '10.0.2.15')
  54. def test_get_random_ip_address(self):
  55. random.seed(5)
  56. self.assertEqual(controller.statistics.get_random_ip_address(), '72.247.178.113')
  57. def test_get_random_ip_address_count_2(self):
  58. random.seed(5)
  59. self.assertEqual(controller.statistics.get_random_ip_address(2), ['72.247.178.113', '23.51.123.27'])
  60. def test_get_ip_address_from_mac(self):
  61. self.assertEqual(controller.statistics.get_ip_address_from_mac('08:00:27:a3:83:43'), '10.0.2.15')
  62. def test_get_mac_address_1(self):
  63. self.assertEqual(controller.statistics.get_mac_address(ip_address='72.247.178.67'), '52:54:00:12:35:02')
  64. def test_get_mac_address_2(self):
  65. self.assertEqual(controller.statistics.get_mac_address(ip_address='10.0.2.15'), '08:00:27:a3:83:43')
  66. def test_get_most_used_mss(self):
  67. self.assertEqual(controller.statistics.get_most_used_mss(ip_address='10.0.2.15'), 1460)
  68. def test_get_most_used_mss_multiple(self):
  69. self.assertEqual(controller_synth.statistics.get_most_used_mss(ip_address='72.247.178.67'), 1200)
  70. def test_get_most_used_mss_none(self):
  71. self.assertEqual(controller_synth.statistics.get_most_used_mss(ip_address='2.2.2.2'), None)
  72. def test_get_most_used_ttl(self):
  73. self.assertEqual(controller.statistics.get_most_used_ttl(ip_address='10.0.2.15'), 128)
  74. def test_get_most_used_ttl_multiple(self):
  75. self.assertEqual(controller_synth.statistics.get_most_used_ttl(ip_address='72.247.178.67'), 48)
  76. def test_get_pps_sent_1(self):
  77. self.assertEqual(controller.statistics.get_pps_sent(ip_address='72.247.178.67'), 0)
  78. def test_get_pps_sent_2(self):
  79. self.assertEqual(controller.statistics.get_pps_sent(ip_address='10.0.2.15'), 2)
  80. def test_get_pps_sent_wrong_input(self):
  81. # wrong input parameter
  82. with self.assertRaises(TypeError):
  83. self.assertEqual(controller.statistics.get_pps_sent('08:00:27:a3:83:43'), 32)
  84. def test_get_pps_received_1(self):
  85. self.assertEqual(controller.statistics.get_pps_received(ip_address='72.247.178.67'), 0)
  86. def test_get_pps_received_2(self):
  87. self.assertEqual(controller.statistics.get_pps_received(ip_address='10.0.2.15'), 3)
  88. def test_get_ttl_distribution_1(self):
  89. self.assertEqual(controller.statistics.get_ttl_distribution(ip_address='72.247.178.67'), {64: 5})
  90. def test_get_ttl_distribution_2(self):
  91. self.assertEqual(controller.statistics.get_ttl_distribution(ip_address='10.0.2.15'), {128: 817})
  92. def test_get_mss_distribution_1(self):
  93. self.assertEqual(controller.statistics.get_mss_distribution(ip_address='72.247.178.67'), {1460: 1})
  94. def test_get_mss_distribution_2(self):
  95. self.assertEqual(controller.statistics.get_mss_distribution(ip_address='10.0.2.15'), {1460: 36})
  96. def test_get_win_distribution_1(self):
  97. self.assertEqual(controller.statistics.get_win_distribution(ip_address='72.247.178.67'), {65535: 5})
  98. def test_get_tos_distribution_1(self):
  99. self.assertEqual(controller.statistics.get_tos_distribution(ip_address='72.247.178.67'), {0: 5})
  100. def test_get_tos_distribution_2(self):
  101. self.assertEqual(controller.statistics.get_tos_distribution(ip_address='10.0.2.15'), {0: 817})
  102. # INTERNAL HELPER-FUNCTION TESTS
  103. def test_calculate_standard_deviation(self):
  104. self.assertEqual(controller.statistics.calculate_standard_deviation([1, 1, 2, 3, 5, 8, 13, 21]),
  105. 6.609652033201143)
  106. def test_calculate_entropy(self):
  107. self.assertEqual(controller.statistics.calculate_entropy([1, 1, 2, 3, 5, 8, 13, 21]), 2.371389165297016)
  108. def test_calculate_entropy_normalized(self):
  109. self.assertEqual(controller.statistics.calculate_entropy([1, 1, 2, 3, 5, 8, 13, 21], normalized=True),
  110. (2.371389165297016, 0.7904630550990053))
  111. def test_calculate_complement_packet_rates_1(self):
  112. cpr = controller.statistics.calculate_complement_packet_rates(0)[0:9]
  113. self.assertEqual(cpr, [(186.418564, 0), (186.418824, 0), (186.419346, 0), (186.445361, 0),
  114. (186.46954399999998, 0), (186.476234, 0), (186.477304, 0), (186.48606999999998, 0),
  115. (186.486761, 0)])
  116. def test_calculate_complement_packet_rates_2(self):
  117. cpr = controller.statistics.calculate_complement_packet_rates(42)[0:9]
  118. self.assertEqual(cpr, [(186.418564, 41), (186.418824, 42), (186.419346, 42), (186.445361, 42),
  119. (186.46954399999998, 42), (186.476234, 42), (186.477304, 42),
  120. (186.48606999999998, 42),
  121. (186.486761, 42)])