3
0

ID2TAttackTest.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import inspect
  2. import unittest
  3. import scapy.utils as pcr
  4. import Core.Controller as Ctrl
  5. import ID2TLib.TestLibrary as Lib
  6. class ID2TAttackTest(unittest.TestCase):
  7. """
  8. Generic Test Class for Core attacks based on unittest.TestCase.
  9. """
  10. def checksum_test(self, attack_args, sha256_checksum, seed=5, cleanup=True, pcap=Lib.test_pcap,
  11. flag_write_file=False, flag_recalculate_stats=False, flag_print_statistics=False,
  12. attack_sub_dir=True, test_sub_dir=True, time=False):
  13. """
  14. Runs the attack against a given sha256 checksum.
  15. :param attack_args: A list of attacks with their attack parameters (as defined in Controller.process_attacks).
  16. :param sha256_checksum: The checksum to verify the result pcap.
  17. :param seed: A random seed to keep random values static (care for count and order of random generation).
  18. :param cleanup: Clean up attack output after testing.
  19. :param pcap: The input pcap for the attack.
  20. :param flag_write_file: Writes the statistics to a file.
  21. :param flag_recalculate_stats: Forces the recalculation of statistics.
  22. :param flag_print_statistics: Prints the statistics on the terminal.
  23. :param attack_sub_dir: create sub-directory for each attack-class if True
  24. :param test_sub_dir: create sub-directory for each test-function/case if True
  25. :param time: Measure time for packet generation.
  26. """
  27. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False, non_verbose=True)
  28. controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  29. controller.process_attacks(attack_args, [[seed]], time)
  30. caller_function = inspect.stack()[1].function
  31. try:
  32. self.assertEqual(sha256_checksum, Lib.get_sha256(controller.pcap_dest_path))
  33. except self.failureException:
  34. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  35. raise
  36. if cleanup:
  37. Lib.clean_up(controller)
  38. else:
  39. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  40. def temporal_efficiency_test(self, attack_args, time_limit=15, factor=1, seed=None, cleanup=True,
  41. pcap=Lib.test_pcap,
  42. flag_write_file=False, flag_recalculate_stats=False, flag_print_statistics=False,
  43. attack_sub_dir=True, test_sub_dir=True):
  44. """
  45. Runs the attack with given aruments and monitors time efficiency.
  46. :param attack_args: A list of attacks with their attack parameters (as defined in Controller.process_attacks).
  47. :param time_limit: The given time limit in seconds.
  48. :param factor: A factor to scale the generation time (e.g. only 7 pkts generated -> *10000/7 for 15 seconds).
  49. :param seed: A random seed to keep random values static (care for count and order of random generation).
  50. :param cleanup: Clean up attack output after testing.
  51. :param pcap: The input pcap for the attack.
  52. :param flag_write_file: Writes the statistics to a file.
  53. :param flag_recalculate_stats: Forces the recalculation of statistics.
  54. :param flag_print_statistics: Prints the statistics on the terminal.
  55. :param attack_sub_dir: create sub-directory for each attack-class if True
  56. :param test_sub_dir: create sub-directory for each test-function/case if True
  57. """
  58. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False, non_verbose=True)
  59. controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  60. if seed is None:
  61. controller.process_attacks(attack_args, time=True)
  62. else:
  63. controller.process_attacks(attack_args, [[seed]], time=True)
  64. duration = controller.durations[0] * factor / controller.attack_controller.total_packets
  65. print(attack_args[0][0] + ' needs ' + str(duration) + ' seconds to generate ' + str(factor) + ' packets.')
  66. caller_function = inspect.stack()[1].function
  67. try:
  68. self.assertLessEqual(duration, time_limit)
  69. except self.failureException:
  70. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  71. raise
  72. if cleanup:
  73. Lib.clean_up(controller)
  74. else:
  75. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  76. def order_test(self, attack_args, seed=None, cleanup=True, pcap=Lib.test_pcap,
  77. flag_write_file=False, flag_recalculate_stats=False, flag_print_statistics=False,
  78. attack_sub_dir=True, test_sub_dir=True):
  79. """
  80. Checks if the result of an attack includes all packets in correct order.
  81. :param attack_args: A list of attacks with their attack parameters (as defined in Controller.process_attacks).
  82. :param seed: A random seed to keep random values static (care for count and order of random generation).
  83. :param cleanup: Clean up attack output after testing.
  84. :param pcap: The input pcap for the attack.
  85. :param flag_write_file: Writes the statistics to a file.
  86. :param flag_recalculate_stats: Forces the recalculation of statistics.
  87. :param flag_print_statistics: Prints the statistics on the terminal.
  88. :param attack_sub_dir: create sub-directory for each attack-class if True
  89. :param test_sub_dir: create sub-directory for each test-function/case if True
  90. """
  91. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False, non_verbose=True)
  92. controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  93. controller.process_attacks(attack_args, [[seed]])
  94. caller_function = inspect.stack()[1].function
  95. try:
  96. path = controller.pcap_dest_path
  97. file = pcr.RawPcapReader(path)
  98. packet_a = file.read_packet()
  99. packet_b = file.read_packet()
  100. i = 0
  101. while packet_b is not None:
  102. time_a = packet_a[2][0:2]
  103. time_b = packet_b[2][0:2]
  104. if time_a[0] > time_b[0]:
  105. file.close()
  106. self.fail("Packet order incorrect at: " + str(i + 1) + "-" + str(i + 2) +
  107. ". Current time: " + str(time_a) + " Next time: " + str(time_b))
  108. elif time_a[0] == time_b[0]:
  109. if time_a[1] > time_b[1]:
  110. file.close()
  111. self.fail("Packet order incorrect at: " + str(i + 1) + "-" + str(i + 2) +
  112. ". Current time: " + str(time_a) + " Next time: " + str(time_b))
  113. packet_a = packet_b
  114. packet_b = file.read_packet()
  115. i += 1
  116. file.close()
  117. except self.failureException:
  118. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  119. raise
  120. if cleanup:
  121. Lib.clean_up(controller)
  122. else:
  123. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)