Commit 225312a8abe8e8ba48cb87b395219333be2b7370

Authored by Christian Herdtweck
1 parent 6067550a

tests: Reduce text output from oleobj tests

Instead of calling main() in current interpreter, fork a new one using
call_and_capture
tests/oleobj/test_basic.py
... ... @@ -8,7 +8,7 @@ from hashlib import md5
8 8 from glob import glob
9 9  
10 10 # Directory with test data, independent of current working directory
11   -from tests.test_utils import DATA_BASE_DIR
  11 +from tests.test_utils import DATA_BASE_DIR, call_and_capture
12 12 from oletools import oleobj
13 13  
14 14  
... ... @@ -81,10 +81,6 @@ class TestOleObj(unittest.TestCase):
81 81 """ fixture start: create temp dir """
82 82 self.temp_dir = mkdtemp(prefix='oletools-oleobj-')
83 83 self.did_fail = False
84   - if DEBUG:
85   - import logging
86   - logging.basicConfig(level=logging.DEBUG if DEBUG else logging.INFO)
87   - oleobj.log.setLevel(logging.NOTSET)
88 84  
89 85 def tearDown(self):
90 86 """ fixture end: remove temp dir """
... ... @@ -101,7 +97,8 @@ class TestOleObj(unittest.TestCase):
101 97 """
102 98 test that oleobj can be called with -i and -v
103 99  
104   - this is the way that amavisd calls oleobj, thinking it is ripOLE
  100 + This is how ripOLE used to be often called (e.g. by amavisd-new);
  101 + ensure oleobj is a compatible replacement.
105 102 """
106 103 self.do_test_md5(['-d', self.temp_dir, '-v', '-i'])
107 104  
... ... @@ -112,30 +109,43 @@ class TestOleObj(unittest.TestCase):
112 109 'embedded-simple-2007.xml',
113 110 'embedded-simple-2007-as2003.xml'):
114 111 full_name = join(DATA_BASE_DIR, 'oleobj', sample_name)
115   - ret_val = oleobj.main(args + [full_name, ])
  112 + output, ret_val = call_and_capture('oleobj', args + [full_name, ],
  113 + accept_nonzero_exit=True)
116 114 if glob(self.temp_dir + 'ole-object-*'):
117   - self.fail('found embedded data in {0}'.format(sample_name))
118   - self.assertEqual(ret_val, oleobj.RETURN_NO_DUMP)
  115 + self.fail('found embedded data in {0}. Output:\n{1}'
  116 + .format(sample_name, output))
  117 + self.assertEqual(ret_val, oleobj.RETURN_NO_DUMP,
  118 + msg='Wrong return value {} for {}. Output:\n{}'
  119 + .format(ret_val, sample_name, output))
119 120  
120   - def do_test_md5(self, args, test_fun=oleobj.main):
  121 + def do_test_md5(self, args, test_fun=None):
121 122 """ helper for test_md5 and test_md5_args """
122   - # name of sample, extension of embedded file, md5 hash of embedded file
123 123 data_dir = join(DATA_BASE_DIR, 'oleobj')
  124 +
  125 + # name of sample, extension of embedded file, md5 hash of embedded file
124 126 for sample_name, embedded_name, expect_hash in SAMPLES:
125   - ret_val = test_fun(args + [join(data_dir, sample_name), ])
126   - self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP)
  127 + args_with_path = args + [join(data_dir, sample_name), ]
  128 + if test_fun is None:
  129 + output, ret_val = call_and_capture('oleobj', args_with_path,
  130 + accept_nonzero_exit=True)
  131 + else:
  132 + ret_val = test_fun(args_with_path)
  133 + output = '[output: see above]'
  134 + self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP,
  135 + msg='Wrong return value {} for {}. Output:\n{}'
  136 + .format(ret_val, sample_name, output))
127 137 expect_name = join(self.temp_dir,
128 138 sample_name + '_' + embedded_name)
129 139 if not isfile(expect_name):
130 140 self.did_fail = True
131   - self.fail('{0} not created from {1}'.format(expect_name,
132   - sample_name))
  141 + self.fail('{0} not created from {1}. Output:\n{2}'
  142 + .format(expect_name, sample_name, output))
133 143 continue
134 144 md5_hash = calc_md5(expect_name)
135 145 if md5_hash != expect_hash:
136 146 self.did_fail = True
137   - self.fail('Wrong md5 {0} of {1} from {2}'
138   - .format(md5_hash, expect_name, sample_name))
  147 + self.fail('Wrong md5 {0} of {1} from {2}. Output:\n{3}'
  148 + .format(md5_hash, expect_name, sample_name, output))
139 149 continue
140 150  
141 151 def test_non_streamed(self):
... ...
tests/oleobj/test_external_links.py
... ... @@ -6,7 +6,7 @@ import os
6 6 from os import path
7 7  
8 8 # Directory with test data, independent of current working directory
9   -from tests.test_utils import DATA_BASE_DIR
  9 +from tests.test_utils import DATA_BASE_DIR, call_and_capture
10 10 from oletools import oleobj
11 11  
12 12 BASE_DIR = path.join(DATA_BASE_DIR, 'oleobj', 'external_link')
... ... @@ -22,8 +22,11 @@ class TestExternalLinks(unittest.TestCase):
22 22 for filename in filenames:
23 23 file_path = path.join(dirpath, filename)
24 24  
25   - ret_val = oleobj.main([file_path])
26   - self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP)
  25 + output, ret_val = call_and_capture('oleobj', [file_path, ],
  26 + accept_nonzero_exit=True)
  27 + self.assertEqual(ret_val, oleobj.RETURN_DID_DUMP,
  28 + msg='Wrong return value {} for {}. Output:\n{}'
  29 + .format(ret_val, filename, output))
27 30  
28 31  
29 32 # just in case somebody calls this file as a script
... ...