test_Queries.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import unittest
  2. import ID2TLib.Controller as Ctrl
  3. # TODO: change to generic path
  4. pcap = "/home/jones/Desktop/ID2T-toolkit/resources/test/reference_1998.pcap"
  5. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False)
  6. controller.load_pcap_statistics(flag_write_file=False, flag_recalculate_stats=True, flag_print_statistics=False)
  7. #TODO: also change path
  8. file_information = [('Pcap file', '/home/jones/Desktop/ID2T-toolkit/resources/test/reference_1998.pcap'),
  9. ('Packets', 1998, 'packets'), ('Capture length', '25.4294414520264', 'seconds'),
  10. ('Capture start', '1970-01-01 01:01:45.647675'), ('Capture end', '1970-01-01 01:08:10.102034')]
  11. file_statistics = [('Avg. packet rate', 78.57034301757812, 'packets/sec'), ('Avg. packet size', 0.0, 'kbytes'),
  12. ('Avg. packets sent', 90.0, 'packets'), ('Avg. bandwidth in', 9.529012680053711, 'kbit/s'),
  13. ('Avg. bandwidth out', 9.529012680053711, 'kbit/s')]
  14. #FIXME: currently sorted ascending
  15. ip_addresses = ["10.0.2.15", "104.83.103.45", "13.107.21.200", "131.253.61.100","172.217.23.142",
  16. "172.217.23.174", "192.168.33.254", "204.79.197.200", "23.51.123.27", "35.161.3.50",
  17. "52.11.17.245", "52.34.37.177", "52.39.210.199", "52.41.250.141", "52.85.173.182",
  18. "54.149.74.139", "54.187.98.195", "54.192.44.108", "54.192.44.177", "72.247.178.113",
  19. "72.247.178.67", "93.184.220.29"]
  20. class TestQueries(unittest.TestCase):
  21. def test_get_file_information(self):
  22. self.assertEqual(controller.statistics.get_file_information(), file_information)
  23. def test_get_general_file_statistics(self):
  24. self.assertEqual(controller.statistics.get_general_file_statistics(), file_statistics)
  25. def test_get_capture_duration(self):
  26. self.assertEqual(controller.statistics.get_capture_duration(),'25.4294414520264')
  27. def test_get_pcap_timestamp_start(self):
  28. self.assertEqual(controller.statistics.get_pcap_timestamp_start(), '1970-01-01 01:01:45.647675')
  29. def test_get_pcap_timestamp_end(self):
  30. self.assertEqual(controller.statistics.get_pcap_timestamp_end(), '1970-01-01 01:08:10.102034')
  31. def test_get_pps_sent_1(self):
  32. self.assertEqual(controller.statistics.get_pps_sent(ip_address='72.247.178.67'), 0)
  33. def test_get_pps_sent_2(self):
  34. self.assertEqual(controller.statistics.get_pps_sent(ip_address='10.0.2.15'), 32)
  35. def test_get_pps_received_1(self):
  36. self.assertEqual(controller.statistics.get_pps_received(ip_address='72.247.178.67'), 0)
  37. def test_get_pps_received_2(self):
  38. self.assertEqual(controller.statistics.get_pps_received(ip_address='10.0.2.15'), 46)
  39. def test_get_packet_count(self):
  40. self.assertEqual(controller.statistics.get_packet_count(), 1998)
  41. def test_get_most_used_ip_address(self):
  42. self.assertEqual(controller.statistics.get_most_used_ip_address(), '10.0.2.15')
  43. def test_get_ttl_distribution_1(self):
  44. self.assertEqual(controller.statistics.get_ttl_distribution(ipAddress='72.247.178.67'), {64: 5})
  45. def test_get_ttl_distribution_2(self):
  46. self.assertEqual(controller.statistics.get_ttl_distribution(ipAddress='10.0.2.15'), {128: 817})
  47. def test_get_mss_distribution_1(self):
  48. self.assertEqual(controller.statistics.get_mss_distribution(ipAddress='72.247.178.67'), {1460: 1})
  49. def test_get_mss_distribution_2(self):
  50. self.assertEqual(controller.statistics.get_mss_distribution(ipAddress='10.0.2.15'), {1460: 36})
  51. def test_get_win_distribution_1(self):
  52. self.assertEqual(controller.statistics.get_win_distribution(ipAddress='72.247.178.67'), {65535: 5})
  53. # TODO: get win_distribution for this ip
  54. #def test_get_win_distribution_2(self):
  55. # self.assertEqual(controller.statistics.get_win_distribution(ipAddress='10.0.2.15'),'')
  56. def test_get_tos_distribution_1(self):
  57. self.assertEqual(controller.statistics.get_tos_distribution(ipAddress='72.247.178.67'), {0: 5})
  58. def test_get_tos_distribution_2(self):
  59. self.assertEqual(controller.statistics.get_tos_distribution(ipAddress='10.0.2.15'), {0: 817})
  60. def test_get_ip_address_count(self):
  61. self.assertEqual(controller.statistics.get_ip_address_count(), 22)
  62. def test_get_ip_addresses(self):
  63. self.assertEqual(controller.statistics.get_ip_addresses(), ip_addresses)
  64. #TODO: move random outside of query and use seed to test
  65. #def test_get_random_ip_address(self):
  66. # self.assertEqual(controller.statistics.get_random_ip_address(), '')
  67. def test_get_mac_address_1(self):
  68. self.assertEqual(controller.statistics.get_mac_address(ipAddress='72.247.178.67'), '52:54:00:12:35:02')
  69. def test_get_mac_address_2(self):
  70. self.assertEqual(controller.statistics.get_mac_address(ipAddress='10.0.2.15'), '08:00:27:a3:83:43')
  71. def test_get_most_used_mss(self):
  72. self.assertEqual(controller.statistics.get_most_used_mss(ipAddress='10.0.2.15'), 1460)
  73. def test_get_most_used_ttl(self):
  74. self.assertEqual(controller.statistics.get_most_used_ttl(ipAddress='10.0.2.15'), 128)
  75. def test_is_query_no_string(self):
  76. self.assertFalse(controller.statistics.is_query(42))
  77. def test_is_query_named_query(self):
  78. self.assertTrue(controller.statistics.is_query('least_used(ipaddress)'))
  79. def test_is_query_standard_query(self):
  80. self.assertTrue(controller.statistics.is_query('SELECT * from ip_statistics'))
  81. def test_calculate_standard_deviation(self):
  82. self.assertEqual(controller.statistics.calculate_standard_deviation([1,1,2,3,5,8,13,21]), 6.609652033201143)
  83. def test_calculate_entropy_unnormalized(self):
  84. self.assertEqual(controller.statistics.calculate_entropy([1,1,2,3,5,8,13,21]), 2.371389165297016)
  85. def test_calculate_entropy_normalized(self):
  86. self.assertEqual(controller.statistics.calculate_entropy([1, 1, 2, 3, 5, 8, 13, 21], normalized=True),
  87. (2.371389165297016, 0.7904630550990053))
  88. #TODO: get complement packet rates and a reasonable pps
  89. #def test_calculate_complement_packet_rates(self):
  90. # self.assertEqual(controller.statistics.calculate_complement_packet_rates(42), '')
  91. if __name__ == '__main__':
  92. unittest.main()