ID2TAttackTest.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import unittest
  2. import inspect
  3. import ID2TLib.Controller as Ctrl
  4. import ID2TLib.TestLibrary as Lib
  5. class ID2TAttackTest(unittest.TestCase):
  6. """
  7. Generic Test Class for ID2T attacks based on unittest.TestCase.
  8. """
  9. def checksum_test(self, attack_args, sha256_checksum, seed=5, cleanup=True, pcap=Lib.test_pcap,
  10. flag_write_file=False, flag_recalculate_stats=False, flag_print_statistics=False,
  11. attack_sub_dir=True, test_sub_dir=True):
  12. """
  13. Runs the attack against a given sha256 checksum.
  14. :param attack_args: A list of attacks with their attack parameters (as defined in Controller.process_attacks).
  15. :param sha256_checksum: The checksum to verify the result pcap.
  16. :param seed: A random seed to keep random values static (care for count and order of random generation).
  17. :param cleanup: Clean up attack output after testing.
  18. :param pcap: The input pcap for the attack.
  19. :param flag_write_file: Writes the statistics to a file.
  20. :param flag_recalculate_stats: Forces the recalculation of statistics.
  21. :param flag_print_statistics: Prints the statistics on the terminal.
  22. :param attack_sub_dir: create sub-directory for each attack-class if True
  23. :param test_sub_dir: create sub-directory for each test-function/case if True
  24. """
  25. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False)
  26. controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  27. controller.process_attacks(attack_args, [[seed]])
  28. caller_function = inspect.stack()[1].function
  29. try:
  30. self.assertEqual(sha256_checksum, Lib.get_sha256(controller.pcap_dest_path))
  31. except self.failureException:
  32. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  33. raise
  34. if cleanup:
  35. Lib.clean_up(controller)
  36. else:
  37. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)