ID2TAttackTest.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import unittest
  2. import inspect
  3. import ID2TLib.Controller as Ctrl
  4. import ID2TLib.TestLibrary as Lib
  5. import scapy.utils as pcr
  6. class ID2TAttackTest(unittest.TestCase):
  7. """
  8. Generic Test Class for ID2T 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):
  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. """
  26. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False)
  27. controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  28. controller.process_attacks(attack_args, [[seed]])
  29. caller_function = inspect.stack()[1].function
  30. try:
  31. self.assertEqual(sha256_checksum, Lib.get_sha256(controller.pcap_dest_path))
  32. except self.failureException:
  33. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  34. raise
  35. if cleanup:
  36. Lib.clean_up(controller)
  37. else:
  38. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  39. def order_test(self, attack_args, seed=None, cleanup=True, pcap=Lib.test_pcap,
  40. flag_write_file=False, flag_recalculate_stats=False, flag_print_statistics=False,
  41. attack_sub_dir=True, test_sub_dir=True):
  42. """
  43. Checks if the result of an attack includes all packets in correct order.
  44. :param attack_args: A list of attacks with their attack parameters (as defined in Controller.process_attacks).
  45. :param seed: A random seed to keep random values static (care for count and order of random generation).
  46. :param cleanup: Clean up attack output after testing.
  47. :param pcap: The input pcap for the attack.
  48. :param flag_write_file: Writes the statistics to a file.
  49. :param flag_recalculate_stats: Forces the recalculation of statistics.
  50. :param flag_print_statistics: Prints the statistics on the terminal.
  51. :param attack_sub_dir: create sub-directory for each attack-class if True
  52. :param test_sub_dir: create sub-directory for each test-function/case if True
  53. """
  54. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False)
  55. controller.load_pcap_statistics(flag_write_file, flag_recalculate_stats, flag_print_statistics)
  56. controller.process_attacks(attack_args, [[seed]])
  57. caller_function = inspect.stack()[1].function
  58. try:
  59. path = controller.pcap_dest_path
  60. file = pcr.RawPcapReader(path)
  61. packet_a = file.read_packet()
  62. packet_b = file.read_packet()
  63. i = 0
  64. while packet_b is not None:
  65. time_a = packet_a[2][0:2]
  66. time_b = packet_b[2][0:2]
  67. if time_a[0] > time_b[0]:
  68. file.close()
  69. self.fail("Packet order incorrect at: " + str(i+1) + "-" + str(i+2) +
  70. ". Current time: " + str(time_a) + " Next time: " + str(time_b))
  71. elif time_a[0] == time_b[0]:
  72. if time_a[1] > time_b[1]:
  73. file.close()
  74. self.fail("Packet order incorrect at: " + str(i + 1) + "-" + str(i + 2) +
  75. ". Current time: " + str(time_a) + " Next time: " + str(time_b))
  76. packet_a = packet_b
  77. packet_b = file.read_packet()
  78. i += 1
  79. file.close()
  80. except self.failureException:
  81. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
  82. raise
  83. if cleanup:
  84. Lib.clean_up(controller)
  85. else:
  86. Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)