ID2TAttackTest.py 8.9 KB

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