test_regression.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. META_FILE = "fileinfo.xml"
  11. def test_regression(self):
  12. config_location = self.REGRESSION_DIRECTORY + os.sep + self.META_FILE
  13. xml_root = xml.etree.ElementTree.parse(config_location).getroot()
  14. comparator = PcapComparator()
  15. for test in xml_root.getchildren():
  16. self.assertXMLTagHasAttribute(test, "seed", "<test>s needs a seed-attribute")
  17. self.assertXMLTagHasAttribute(test, "outfile", "<test>s needs a outfile-attribute")
  18. self.assertXMLTagHasAttribute(test, "infile", "<test>s needs a infile-attribute")
  19. self.assertXMLTagHasAttribute(test, "name", "<test>s needs a name-attribute")
  20. params = []
  21. for param in test.getchildren():
  22. self.assertEqual(param.tag, "param", "<test>-children must be <params>s")
  23. self.assertIsNotNone(param.get("key"), "<param> needs a key-attribute")
  24. self.assertIsNotNone(param.get("value"), "<param> needs a value-attribute")
  25. params.append("%s=%s" % (param.get("key"), param.get("value")))
  26. infile = os.path.join(self.REGRESSION_DIRECTORY_ID2T_RELATIVE, test.get("infile"))
  27. outfile = os.path.join(self.REGRESSION_DIRECTORY, test.get("outfile"))
  28. execution = ID2TExecution(infile, seed=test.get("seed"))
  29. self.print_warning(execution.get_run_command(params))
  30. execution.run(params)
  31. new_file = self.ID2T_RELATIVE_TO_LOCAL_PREFIX + os.sep + execution.get_pcap_filename()
  32. old_file = outfile
  33. try:
  34. comparator.compare_files(new_file, old_file)
  35. except AssertionError as e:
  36. execution.cleanup()
  37. raise AssertionError("Test %s failed" % test.get("name")) from e
  38. self.print_warning("Regression-test %s passed" % test.get("name"))
  39. def assertXMLTagHasAttribute(self, tag, attribute, msg=None):
  40. self.assertIsNotNone(tag.get(attribute), msg)
  41. def print_warning(self, *text):
  42. print(*text, file=sys.stderr)