TestLibrary.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import os
  2. import hashlib
  3. import random
  4. from definitions import ROOT_DIR
  5. test_resource_dir = ROOT_DIR + "/../resources/test"
  6. test_pcap = ROOT_DIR + "/../resources/test/reference_1998.pcap"
  7. test_pcap_ips = ["10.0.2.15", "52.85.173.182"]
  8. test_pcap_empty = []
  9. """
  10. helper functions for generic_test
  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. """
  35. function patches for unittests
  36. """
  37. def get_bytes(count, ignore):
  38. """
  39. unittest patch for get_rnd_bytes (ID2TLib.Utility.py)
  40. :param count: count of requested bytes
  41. :param ignore: <not used>
  42. :return: a count of As
  43. """
  44. return b'A' * count
  45. def get_x86_nop(count, side_effect_free, char_filter):
  46. """
  47. unittest patch for get_rnd_x86_nop (ID2TLib.Utility.py)
  48. :param count: count of requested nops
  49. :param side_effect_free: <not used>
  50. :param char_filter: <not used>
  51. :return: a count of \x90
  52. """
  53. return b'\x90' * count
  54. def rename_test_result_files(controller, caller_function: str, attack_sub_dir=False, test_sub_dir=False):
  55. """
  56. :param controller: controller which created output files
  57. :param caller_function: the name of the function which called the generic test
  58. :param attack_sub_dir: create sub-directory for each attack-class if True
  59. :param test_sub_dir: create sub-directory for each test-function/case if True
  60. """
  61. tmp_path_tuple = controller.pcap_dest_path.rpartition("_")
  62. result_pcap_path = tmp_path_tuple[0] + tmp_path_tuple[1] + caller_function + "_" + tmp_path_tuple[2]
  63. tmp_label_path_tuple = controller.label_manager.label_file_path.rpartition("_")
  64. tmp_path_tuple = tmp_label_path_tuple[0].rpartition("_")
  65. result_labels_path = tmp_path_tuple[0] + tmp_path_tuple[1] + caller_function + "_" + tmp_path_tuple[2]
  66. result_labels_path = result_labels_path + tmp_label_path_tuple[1] + tmp_label_path_tuple[2]
  67. if attack_sub_dir:
  68. caller_attack = caller_function.replace("test_", "").partition("_")[0]
  69. tmp_dir_tuple = result_pcap_path.rpartition("/")
  70. result_dir = tmp_dir_tuple[0] + tmp_dir_tuple[1] + caller_attack + "/"
  71. result_pcap_path = result_dir + tmp_dir_tuple[2]
  72. os.makedirs(result_dir, exist_ok=True)
  73. tmp_dir_tuple = result_labels_path.rpartition("/")
  74. result_labels_path = result_dir + tmp_dir_tuple[2]
  75. if test_sub_dir:
  76. tmp_dir_tuple = result_pcap_path.rpartition("/")
  77. result_dir = tmp_dir_tuple[0] + tmp_dir_tuple[1] + (caller_function.replace("test_", "")) + "/"
  78. result_pcap_path = result_dir + tmp_dir_tuple[2]
  79. os.makedirs(result_dir, exist_ok=True)
  80. tmp_dir_tuple = result_labels_path.rpartition("/")
  81. result_labels_path = result_dir + tmp_dir_tuple[2]
  82. os.rename(controller.pcap_dest_path, result_pcap_path)
  83. controller.pcap_dest_path = result_pcap_path
  84. os.rename(controller.label_manager.label_file_path, result_labels_path)
  85. controller.label_manager.label_file_path = result_labels_path
  86. def get_win_size(pkts_num):
  87. result = []
  88. for i in range(0, pkts_num):
  89. result.append(10)
  90. return result
  91. def get_attacker_config(ip_source_list, ipAddress: str):
  92. next_port = random.randint(0, 2 ** 16 - 1)
  93. ttl = random.randint(1, 255)
  94. return next_port, ttl