From 225312a8abe8e8ba48cb87b395219333be2b7370 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Tue, 7 May 2019 12:32:53 +0200 Subject: [PATCH] tests: Reduce text output from oleobj tests --- tests/oleobj/test_basic.py | 44 +++++++++++++++++++++++++++----------------- tests/oleobj/test_external_links.py | 9 ++++++--- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/tests/oleobj/test_basic.py b/tests/oleobj/test_basic.py index 8154e54..b5447ad 100644 --- a/tests/oleobj/test_basic.py +++ b/tests/oleobj/test_basic.py @@ -8,7 +8,7 @@ from hashlib import md5 from glob import glob # Directory with test data, independent of current working directory -from tests.test_utils import DATA_BASE_DIR +from tests.test_utils import DATA_BASE_DIR, call_and_capture from oletools import oleobj @@ -81,10 +81,6 @@ class TestOleObj(unittest.TestCase): """ fixture start: create temp dir """ self.temp_dir = mkdtemp(prefix='oletools-oleobj-') self.did_fail = False - if DEBUG: - import logging - logging.basicConfig(level=logging.DEBUG if DEBUG else logging.INFO) - oleobj.log.setLevel(logging.NOTSET) def tearDown(self): """ fixture end: remove temp dir """ @@ -101,7 +97,8 @@ class TestOleObj(unittest.TestCase): """ test that oleobj can be called with -i and -v - this is the way that amavisd calls oleobj, thinking it is ripOLE + This is how ripOLE used to be often called (e.g. by amavisd-new); + ensure oleobj is a compatible replacement. """ self.do_test_md5(['-d', self.temp_dir, '-v', '-i']) @@ -112,30 +109,43 @@ class TestOleObj(unittest.TestCase): 'embedded-simple-2007.xml', 'embedded-simple-2007-as2003.xml'): full_name = join(DATA_BASE_DIR, 'oleobj', sample_name) - ret_val = oleobj.main(args + [full_name, ]) + output, ret_val = call_and_capture('oleobj', args + [full_name, ], + accept_nonzero_exit=True) if glob(self.temp_dir + 'ole-object-*'): - self.fail('found embedded data in {0}'.format(sample_name)) - self.assertEqual(ret_val, oleobj.RETURN_NO_DUMP) + self.fail('found embedded data in {0}. Output:\n{1}' + .format(sample_name, output)) + self.assertEqual(ret_val, oleobj.RETURN_NO_DUMP, + msg='Wrong return value {} for {}. Output:\n{}' + .format(ret_val, sample_name, output)) - def do_test_md5(self, args, test_fun=oleobj.main): + def do_test_md5(self, args, test_fun=None): """ helper for test_md5 and test_md5_args """ - # name of sample, extension of embedded file, md5 hash of embedded file data_dir = join(DATA_BASE_DIR, 'oleobj') + + # name of sample, extension of embedded file, md5 hash of embedded file for sample_name, embedded_name, expect_hash in SAMPLES: - ret_val = test_fun(args + [join(data_dir, sample_name), ]) - self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP) + args_with_path = args + [join(data_dir, sample_name), ] + if test_fun is None: + output, ret_val = call_and_capture('oleobj', args_with_path, + accept_nonzero_exit=True) + else: + ret_val = test_fun(args_with_path) + output = '[output: see above]' + self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP, + msg='Wrong return value {} for {}. Output:\n{}' + .format(ret_val, sample_name, output)) expect_name = join(self.temp_dir, sample_name + '_' + embedded_name) if not isfile(expect_name): self.did_fail = True - self.fail('{0} not created from {1}'.format(expect_name, - sample_name)) + self.fail('{0} not created from {1}. Output:\n{2}' + .format(expect_name, sample_name, output)) continue md5_hash = calc_md5(expect_name) if md5_hash != expect_hash: self.did_fail = True - self.fail('Wrong md5 {0} of {1} from {2}' - .format(md5_hash, expect_name, sample_name)) + self.fail('Wrong md5 {0} of {1} from {2}. Output:\n{3}' + .format(md5_hash, expect_name, sample_name, output)) continue def test_non_streamed(self): diff --git a/tests/oleobj/test_external_links.py b/tests/oleobj/test_external_links.py index 9c7e632..2b7fc5b 100644 --- a/tests/oleobj/test_external_links.py +++ b/tests/oleobj/test_external_links.py @@ -6,7 +6,7 @@ import os from os import path # Directory with test data, independent of current working directory -from tests.test_utils import DATA_BASE_DIR +from tests.test_utils import DATA_BASE_DIR, call_and_capture from oletools import oleobj BASE_DIR = path.join(DATA_BASE_DIR, 'oleobj', 'external_link') @@ -22,8 +22,11 @@ class TestExternalLinks(unittest.TestCase): for filename in filenames: file_path = path.join(dirpath, filename) - ret_val = oleobj.main([file_path]) - self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP) + output, ret_val = call_and_capture('oleobj', [file_path, ], + accept_nonzero_exit=True) + self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP, + msg='Wrong return value {} for {}. Output:\n{}' + .format(ret_val, filename, output)) # just in case somebody calls this file as a script -- libgit2 0.21.4