Commit 3c3b70934252c10f1a971b72fbaf58da49460b3e

Authored by Christian Herdtweck
1 parent 26f922b5

tests: Deal with warnings in output

Do not assume a known length of output when checking it. Only assume that
meta information about oletool used is first. Ignore warnings and messages
that come after that to identify the actual result we want to check.
tests/olevba/test_basic.py
@@ -120,10 +120,14 @@ class TestOlevbaBasic(unittest.TestCase): @@ -120,10 +120,14 @@ class TestOlevbaBasic(unittest.TestCase):
120 args=[full_name, ] + ADD_ARGS, 120 args=[full_name, ] + ADD_ARGS,
121 accept_nonzero_exit=True) 121 accept_nonzero_exit=True)
122 output = json.loads(out_str) 122 output = json.loads(out_str)
123 - self.assertEqual(len(output), 2) 123 + self.assertGreaterEqual(len(output), 2)
124 self.assertEqual(output[0]['type'], 'MetaInformation') 124 self.assertEqual(output[0]['type'], 'MetaInformation')
125 self.assertEqual(output[0]['script_name'], 'olevba') 125 self.assertEqual(output[0]['script_name'], 'olevba')
126 - result = output[1] 126 + for entry in output[1:]:
  127 + if entry['type'] in ('msg', 'warning'):
  128 + continue # ignore messages
  129 + result = entry
  130 + break
127 self.assertTrue(result['json_conversion_successful']) 131 self.assertTrue(result['json_conversion_successful'])
128 if suffix in ('.xlsb', '.xltm', '.xlsm'): 132 if suffix in ('.xlsb', '.xltm', '.xlsm'):
129 # TODO: cannot extract xlm macros for these types yet 133 # TODO: cannot extract xlm macros for these types yet
tests/olevba/test_crypto.py
@@ -40,7 +40,7 @@ class OlevbaCryptoWriteProtectTest(unittest.TestCase): @@ -40,7 +40,7 @@ class OlevbaCryptoWriteProtectTest(unittest.TestCase):
40 exclude_stderr=True) 40 exclude_stderr=True)
41 data = json.loads(output, object_pairs_hook=OrderedDict) 41 data = json.loads(output, object_pairs_hook=OrderedDict)
42 # debug: json.dump(data, sys.stdout, indent=4) 42 # debug: json.dump(data, sys.stdout, indent=4)
43 - self.assertIn(len(data), (3, 4)) 43 + self.assertGreaterEqual(len(data), 3)
44 44
45 # first 2 parts: general info about script and file 45 # first 2 parts: general info about script and file
46 self.assertIn('script_name', data[0]) 46 self.assertIn('script_name', data[0])
@@ -53,22 +53,23 @@ class OlevbaCryptoWriteProtectTest(unittest.TestCase): @@ -53,22 +53,23 @@ class OlevbaCryptoWriteProtectTest(unittest.TestCase):
53 self.assertEqual(data[1]['type'], 'OLE') 53 self.assertEqual(data[1]['type'], 'OLE')
54 self.assertTrue(data[1]['json_conversion_successful']) 54 self.assertTrue(data[1]['json_conversion_successful'])
55 55
56 - # possible VBA stomping warning  
57 - if len(data) == 4:  
58 - self.assertEqual(data[2]['type'], 'msg')  
59 - self.assertIn('VBA stomping', data[2]['msg']) 56 + for entry in data[2:]:
  57 + if entry['type'] in ('msg', 'warning'):
  58 + continue
  59 + result = entry
  60 + break
60 61
61 # last part is the actual result 62 # last part is the actual result
62 - self.assertEqual(data[-1]['container'], example_file)  
63 - self.assertNotEqual(data[-1]['file'], example_file)  
64 - self.assertEqual(data[-1]['type'], "OpenXML")  
65 - analysis = data[-1]['analysis'] 63 + self.assertEqual(result['container'], example_file)
  64 + self.assertNotEqual(result['file'], example_file)
  65 + self.assertEqual(result['type'], "OpenXML")
  66 + analysis = result['analysis']
66 self.assertEqual(analysis[0]['type'], 'AutoExec') 67 self.assertEqual(analysis[0]['type'], 'AutoExec')
67 self.assertEqual(analysis[0]['keyword'], 'Auto_Open') 68 self.assertEqual(analysis[0]['keyword'], 'Auto_Open')
68 - macros = data[-1]['macros'] 69 + macros = result['macros']
69 self.assertEqual(macros[0]['vba_filename'], 'Modul1.bas') 70 self.assertEqual(macros[0]['vba_filename'], 'Modul1.bas')
70 self.assertIn('Sub Auto_Open()', macros[0]['code']) 71 self.assertIn('Sub Auto_Open()', macros[0]['code'])
71 - self.assertTrue(data[-1]['json_conversion_successful']) 72 + self.assertTrue(result['json_conversion_successful'])
72 73
73 74
74 if __name__ == '__main__': 75 if __name__ == '__main__':