3
0

test_regression_mmcomm.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import unittest
  2. import xml.etree.ElementTree
  3. import os.path
  4. import sys
  5. from Test.TestUtil import PcapComparator, ID2TExecution
  6. class RegressionTest(unittest.TestCase):
  7. REGRESSION_DIRECTORY = "../resources/test/Botnet/regression_files"
  8. REGRESSION_DIRECTORY_ID2T_RELATIVE = "resources/test/Botnet/regression_files"
  9. ID2T_RELATIVE_TO_LOCAL_PREFIX = "../"
  10. VERBOSE = False
  11. META_FILE = "fileinfo.xml"
  12. def test_regression(self):
  13. self.printed_newline = False
  14. config_location = self.REGRESSION_DIRECTORY + os.sep + self.META_FILE
  15. xml_root = xml.etree.ElementTree.parse(config_location).getroot()
  16. comparator = PcapComparator()
  17. for test in xml_root.getchildren():
  18. self.assertXMLTagHasAttribute(test, "seed", "<test>s needs a seed-attribute")
  19. self.assertXMLTagHasAttribute(test, "outfile", "<test>s needs a outfile-attribute")
  20. self.assertXMLTagHasAttribute(test, "infile", "<test>s needs a infile-attribute")
  21. self.assertXMLTagHasAttribute(test, "name", "<test>s needs a name-attribute")
  22. params = []
  23. for param in test.getchildren():
  24. self.assertEqual(param.tag, "param", "<test>-children must be <params>s")
  25. self.assertIsNotNone(param.get("key"), "<param> needs a key-attribute")
  26. self.assertIsNotNone(param.get("value"), "<param> needs a value-attribute")
  27. params.append("%s=%s" % (param.get("key"), param.get("value")))
  28. infile = os.path.join(self.REGRESSION_DIRECTORY_ID2T_RELATIVE, test.get("infile"))
  29. outfile = os.path.join(self.REGRESSION_DIRECTORY, test.get("outfile"))
  30. execution = ID2TExecution(infile, seed=test.get("seed"))
  31. self.print_warning("Running %s with command:" % test.get("name"))
  32. self.print_warning(execution.get_run_command(params))
  33. execution.run(params)
  34. new_file = self.ID2T_RELATIVE_TO_LOCAL_PREFIX + os.sep + execution.get_pcap_filename()
  35. old_file = outfile
  36. try:
  37. comparator.compare_files(new_file, old_file)
  38. except AssertionError as e:
  39. execution.cleanup()
  40. raise AssertionError("Test failed") from e
  41. self.print_warning("Test passed")
  42. def assertXMLTagHasAttribute(self, tag, attribute, msg=None):
  43. self.assertIsNotNone(tag.get(attribute), msg)
  44. def print_warning(self, *text):
  45. if self.VERBOSE:
  46. if not self.printed_newline:
  47. print("\n", file=sys.stderr)
  48. self.printed_newline = True
  49. print(*text, file=sys.stderr)