3
0

TestLibrary.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import hashlib
  2. import os
  3. import random
  4. import ID2TLib.Utility as Util
  5. test_resource_dir = Util.TEST_DIR
  6. test_pcap = Util.TEST_DIR + "reference_1998.pcap"
  7. test_pcap_ips = ["10.0.2.15", "52.85.173.182"]
  8. test_pcap_empty = []
  9. """
  10. helper functions for ID2TAttackTest
  11. """
  12. def get_sha256(file):
  13. """
  14. Generates a sha256 checksum from file
  15. :param file: absolute path to file
  16. :return: sha256 checksum
  17. """
  18. sha = hashlib.sha256()
  19. with open(file, 'rb') as f:
  20. while True:
  21. data = f.read(0x100000)
  22. if not data:
  23. break
  24. sha.update(data)
  25. f.close()
  26. return sha.hexdigest()
  27. def clean_up(controller):
  28. """
  29. Removes the output files from a given controller
  30. :param controller: controller which created output files
  31. """
  32. os.remove(controller.pcap_dest_path)
  33. os.remove(controller.label_manager.label_file_path)
  34. def rename_test_result_files(controller, caller_function: str, attack_sub_dir=False, test_sub_dir=False):
  35. """
  36. :param controller: controller which created output files
  37. :param caller_function: the name of the function which called the generic test
  38. :param attack_sub_dir: create sub-directory for each attack-class if True
  39. :param test_sub_dir: create sub-directory for each test-function/case if True
  40. """
  41. tmp_path_tuple = controller.pcap_dest_path.rpartition("_")
  42. result_pcap_path = tmp_path_tuple[0] + tmp_path_tuple[1] + caller_function + "_" + tmp_path_tuple[2]
  43. tmp_label_path_tuple = controller.label_manager.label_file_path.rpartition("_")
  44. tmp_path_tuple = tmp_label_path_tuple[0].rpartition("_")
  45. result_labels_path = tmp_path_tuple[0] + tmp_path_tuple[1] + caller_function + "_" + tmp_path_tuple[2]
  46. result_labels_path = result_labels_path + tmp_label_path_tuple[1] + tmp_label_path_tuple[2]
  47. if attack_sub_dir:
  48. caller_attack = caller_function.replace("test_", "").partition("_")[0]
  49. tmp_dir_tuple = result_pcap_path.rpartition("/")
  50. result_dir = tmp_dir_tuple[0] + tmp_dir_tuple[1] + caller_attack + "/"
  51. result_pcap_path = result_dir + tmp_dir_tuple[2]
  52. os.makedirs(result_dir, exist_ok=True)
  53. tmp_dir_tuple = result_labels_path.rpartition("/")
  54. result_labels_path = result_dir + tmp_dir_tuple[2]
  55. if test_sub_dir:
  56. tmp_dir_tuple = result_pcap_path.rpartition("/")
  57. result_dir = tmp_dir_tuple[0] + tmp_dir_tuple[1] + (caller_function.replace("test_", "")) + "/"
  58. result_pcap_path = result_dir + tmp_dir_tuple[2]
  59. os.makedirs(result_dir, exist_ok=True)
  60. tmp_dir_tuple = result_labels_path.rpartition("/")
  61. result_labels_path = result_dir + tmp_dir_tuple[2]
  62. os.rename(controller.pcap_dest_path, result_pcap_path)
  63. controller.pcap_dest_path = result_pcap_path
  64. os.rename(controller.label_manager.label_file_path, result_labels_path)
  65. controller.label_manager.label_file_path = result_labels_path
  66. """
  67. function patches for unittests
  68. """
  69. def get_bytes(count, ignore):
  70. """
  71. unittest patch for get_rnd_bytes (ID2TLib.Utility.py)
  72. :param count: count of requested bytes
  73. :param ignore: <not used>
  74. :return: a count of As
  75. """
  76. return b'A' * count
  77. def get_x86_nop(count, side_effect_free, char_filter):
  78. """
  79. unittest patch for get_rnd_x86_nop (ID2TLib.Utility.py)
  80. :param count: count of requested nops
  81. :param side_effect_free: <not used>
  82. :param char_filter: <not used>
  83. :return: a count of \x90
  84. """
  85. return b'\x90' * count
  86. def get_attacker_config(ip_source_list, ipAddress: str):
  87. """
  88. unittest patch for get_attacker_config (ID2TLib.Utility.py)
  89. :param ip_source_list: List of source IPs
  90. :param ipAddress: The IP address of the attacker
  91. :return: A tuple consisting of (port, ttlValue)
  92. """
  93. next_port = random.randint(0, 2 ** 16 - 1)
  94. ttl = random.randint(1, 255)
  95. return next_port, ttl