Commit fb4a7daccb57a33ca83b9cbb981c7f15a3f3a132

Authored by Samir Aguiar
Committed by Christian Herdtweck
1 parent aa24ab97

fix unit tests

oletools/olevba.py
@@ -293,6 +293,7 @@ from oletools.thirdparty.pyparsing.pyparsing import \ @@ -293,6 +293,7 @@ from oletools.thirdparty.pyparsing.pyparsing import \
293 infixNotation, ParserElement 293 infixNotation, ParserElement
294 from oletools import ppt_parser 294 from oletools import ppt_parser
295 from oletools import oleform 295 from oletools import oleform
  296 +from oletools import rtfobj
296 297
297 298
298 # monkeypatch email to fix issue #32: 299 # monkeypatch email to fix issue #32:
oletools/rtfobj.py
@@ -701,7 +701,7 @@ def is_rtf(arg, treat_str_as_data=False): @@ -701,7 +701,7 @@ def is_rtf(arg, treat_str_as_data=False):
701 if isinstance(arg, str): # could be bytes, but we assume file name 701 if isinstance(arg, str): # could be bytes, but we assume file name
702 if treat_str_as_data: 702 if treat_str_as_data:
703 try: 703 try:
704 - return arg[:magic_len].encode('ascii', error='strict').lower()\ 704 + return arg[:magic_len].encode('ascii', errors='strict').lower()\
705 == RTF_MAGIC 705 == RTF_MAGIC
706 except UnicodeError: 706 except UnicodeError:
707 return False 707 return False
tests/json/test_output.py
@@ -28,7 +28,7 @@ class TestValidJson(unittest.TestCase): @@ -28,7 +28,7 @@ class TestValidJson(unittest.TestCase):
28 for filename in filenames: 28 for filename in filenames:
29 yield join(dirpath, filename) 29 yield join(dirpath, filename)
30 30
31 - def run_and_parse(self, program, args, print_output=False): 31 + def run_and_parse(self, program, args, print_output=False, check_return_code=True):
32 """ run single program with single file and parse output """ 32 """ run single program with single file and parse output """
33 return_code = None 33 return_code = None
34 with OutputCapture() as capturer: # capture stdout 34 with OutputCapture() as capturer: # capture stdout
@@ -38,7 +38,7 @@ class TestValidJson(unittest.TestCase): @@ -38,7 +38,7 @@ class TestValidJson(unittest.TestCase):
38 return_code = 1 # would result in non-zero exit 38 return_code = 1 # would result in non-zero exit
39 except SystemExit as se: 39 except SystemExit as se:
40 return_code = se.code or 0 # se.code can be None 40 return_code = se.code or 0 # se.code can be None
41 - if return_code is not 0: 41 + if check_return_code and return_code is not 0:
42 if print_output: 42 if print_output:
43 print('Command failed ({0}) -- not parsing output' 43 print('Command failed ({0}) -- not parsing output'
44 .format(return_code)) 44 .format(return_code))
@@ -82,7 +82,8 @@ class TestValidJson(unittest.TestCase): @@ -82,7 +82,8 @@ class TestValidJson(unittest.TestCase):
82 def test_olevba_recurse(self): 82 def test_olevba_recurse(self):
83 """ Test olevba.py with -r """ 83 """ Test olevba.py with -r """
84 json_data = self.run_and_parse(olevba.main, 84 json_data = self.run_and_parse(olevba.main,
85 - ['-j', '-r', join(DATA_BASE_DIR, '*')]) 85 + ['-j', '-r', join(DATA_BASE_DIR, '*')],
  86 + check_return_code=False)
86 self.assertNotEqual(len(json_data), 0, 87 self.assertNotEqual(len(json_data), 0,
87 msg='olevba[3] returned non-zero or no output') 88 msg='olevba[3] returned non-zero or no output')
88 self.assertNotEqual(json_data[-1]['n_processed'], 0, 89 self.assertNotEqual(json_data[-1]['n_processed'], 0,
tests/rtfobj/test_is_rtf.py
@@ -23,9 +23,9 @@ class TestIsRtf(unittest.TestCase): @@ -23,9 +23,9 @@ class TestIsRtf(unittest.TestCase):
23 23
24 def test_bytes(self): 24 def test_bytes(self):
25 """ test that is_rtf works with bytearray """ 25 """ test that is_rtf works with bytearray """
26 - self.assertTrue(is_rtf(RTF_MAGIC + b'asasdffdfasdfasdfasdfasdf'), True)  
27 - self.assertTrue(is_rtf(RTF_MAGIC.upper() + b'asdffasdfasdasdff'), True)  
28 - self.assertFalse(is_rtf(b'asdfasdfasdfasdfasdfasdasdfffsdfsdfa'), True) 26 + self.assertTrue(is_rtf(RTF_MAGIC + b'asasdffdfasdfasdfasdfasdf', True), True)
  27 + self.assertTrue(is_rtf(RTF_MAGIC.upper() + b'asdffasdfasdasdff', True), True)
  28 + self.assertFalse(is_rtf(b'asdfasdfasdfasdfasdfasdasdfffsdfsdfa', True), True)
29 29
30 def test_tuple(self): 30 def test_tuple(self):
31 """ test that is_rtf works with byte tuples """ 31 """ test that is_rtf works with byte tuples """