test_regression_mmcomm.py 2.5 KB

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