diff --git a/tests/ooxml/test_basic.py b/tests/ooxml/test_basic.py index b97c432..e4f5760 100644 --- a/tests/ooxml/test_basic.py +++ b/tests/ooxml/test_basic.py @@ -39,7 +39,7 @@ class TestOOXML(unittest.TestCase): # files that are neither OLE nor xml: except_files = 'empty', 'text' - except_extns = 'rtf', 'csv' + except_extns = 'rtf', 'csv', 'zip' # analyse all files in data dir for base_dir, _, files in os.walk(DATA_BASE_DIR): diff --git a/tests/rtfobj/test_issue_185.py b/tests/rtfobj/test_issue_185.py index a395a67..cbfc97f 100644 --- a/tests/rtfobj/test_issue_185.py +++ b/tests/rtfobj/test_issue_185.py @@ -5,7 +5,7 @@ from oletools import rtfobj class TestRtfObjIssue185(unittest.TestCase): def test_skip_space_after_bin_control_word(self): - data = testdata_reader.read('rtfobj/issue_185.rtf') + data = testdata_reader.read_encrypted('rtfobj/issue_185.rtf.zip') rtfp = rtfobj.RtfObjParser(data) rtfp.parse() objects = rtfp.objects diff --git a/tests/test-data/rtfobj/issue_185.rtf b/tests/test-data/rtfobj/issue_185.rtf deleted file mode 100644 index 3a1da01..0000000 --- a/tests/test-data/rtfobj/issue_185.rtf +++ /dev/null @@ -1 +0,0 @@ -{\rt{\object\objautlink\objupdate\rsltpict\objw37542\objh829\objscalex59286\objscaley86308{\*\objclass \'77}{\*\objdata 32\bin6 FF}}} \ No newline at end of file diff --git a/tests/test-data/rtfobj/issue_185.rtf.zip b/tests/test-data/rtfobj/issue_185.rtf.zip new file mode 100644 index 0000000..37f7a0a --- /dev/null +++ b/tests/test-data/rtfobj/issue_185.rtf.zip diff --git a/tests/test_utils/testdata_reader.py b/tests/test_utils/testdata_reader.py index 4445024..3a4baee 100644 --- a/tests/test_utils/testdata_reader.py +++ b/tests/test_utils/testdata_reader.py @@ -1,8 +1,37 @@ -import os +import os, sys, zipfile from os.path import dirname, abspath, normpath, join from . import DATA_BASE_DIR +ENCRYPTED_FILES_PASSWORD='infected-test' + +if sys.version_info[0] <= 2: + # Python 2.x + if sys.version_info[1] <= 6: + # Python 2.6 + # use is_zipfile backported from Python 2.7: + from thirdparty.zipfile27 import is_zipfile + else: + # Python 2.7 + from zipfile import is_zipfile +else: + # Python 3.x+ + from zipfile import is_zipfile + ENCRYPTED_FILES_PASSWORD = ENCRYPTED_FILES_PASSWORD.encode() def read(relative_path): - with open(join(DATA_BASE_DIR, relative_path), 'rb') as file_handle: + with open(get_path_from_root(relative_path), 'rb') as file_handle: return file_handle.read() + +def read_encrypted(relative_path, filename=None): + z = zipfile.ZipFile(get_path_from_root(relative_path)) + + if filename == None: + contents = z.read(z.namelist()[0], pwd=ENCRYPTED_FILES_PASSWORD) + else: + contents = z.read(filename, pwd=ENCRYPTED_FILES_PASSWORD) + + z.close() + return contents + +def get_path_from_root(relative_path): + return join(DATA_BASE_DIR, relative_path) \ No newline at end of file