Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_skip_space_after_bin_control_word(self):
data = testdata_reader.read_encrypted('rtfobj/issue_185.rtf.zip')
rtfp = rtfobj.RtfObjParser(data)
rtfp.parse()
objects = rtfp.objects
self.assertTrue(len(objects) == 1)
def do_test_md5(self, args, test_fun=oleobj.main):
""" 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')
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)
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))
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))
continue
def preread_file(args):
"""helper for TestOleObj.test_non_streamed: preread + call process_file"""
ignore_arg, output_dir, filename = args
if ignore_arg != '-d':
raise ValueError('ignore_arg not as expected!')
with open(filename, 'rb') as file_handle:
data = file_handle.read()
err_stream, err_dumping, did_dump = \
oleobj.process_file(filename, data, output_dir=output_dir)
if did_dump and not err_stream and not err_dumping:
return oleobj.RETURN_DID_DUMP
else:
return oleobj.RETURN_NO_DUMP # just anything else
- Enable and log: ['enable', '']
* logging when being run as script - should log messages
- Enable and log as JSON: ['as-json', 'enable', '']
* logging as JSON when being run as script - should log messages as JSON
- Enable, log as JSON and throw: ['enable', 'as-json', 'throw', '']
* should produce JSON-compatible output, even after an unhandled exception
"""
# the level should always be the last argument passed
level = args[-1]
use_json = 'as-json' in args
throw = 'throw' in args
percent_autoformat = '%-autoformat' in args
if 'enable' in args:
log_helper.enable_logging(use_json, level, stream=sys.stdout)
_log()
if percent_autoformat:
logger.info('The %s is %d.', 'answer', 47)
if throw:
raise Exception('An exception occurred before ending the logging')
log_helper.end_logging()
""" Test log_helpers """
import sys
from tests.common.log_helper import log_helper_test_imported
from oletools.common.log_helper import log_helper
DEBUG_MESSAGE = 'main: debug log'
INFO_MESSAGE = 'main: info log'
WARNING_MESSAGE = 'main: warning log'
ERROR_MESSAGE = 'main: error log'
CRITICAL_MESSAGE = 'main: critical log'
RESULT_MESSAGE = 'main: result log'
RESULT_TYPE = 'main: result'
logger = log_helper.get_or_create_silent_logger('test_main')
def init_logging_and_log(args):
"""
Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome:
- Log without enabling: ['']
* logging when being imported - should never print
- Log as JSON without enabling: ['as-json', '']
* logging as JSON when being imported - should never print
- Enable and log: ['enable', '']
* logging when being run as script - should log messages
- Enable and log as JSON: ['as-json', 'enable', '']
* logging as JSON when being run as script - should log messages as JSON
- Enable, log as JSON and throw: ['enable', 'as-json', 'throw', '']
* should produce JSON-compatible output, even after an unhandled exception
"""
def run_print():
"""This is called from test_read* tests as script. Prints & logs unicode"""
from oletools.common.io_encoding import ensure_stdout_handles_unicode
from oletools.common.log_helper import log_helper
ensure_stdout_handles_unicode()
print(u'Check: \u2713') # print check mark
# check logging as well
logger = log_helper.get_or_create_silent_logger('test_encoding_handler')
log_helper.enable_logging(False, 'debug', stream=sys.stdout)
logger.info(u'Check: \u2713')
return 0
use_json = 'as-json' in args
throw = 'throw' in args
percent_autoformat = '%-autoformat' in args
if 'enable' in args:
log_helper.enable_logging(use_json, level, stream=sys.stdout)
_log()
if percent_autoformat:
logger.info('The %s is %d.', 'answer', 47)
if throw:
raise Exception('An exception occurred before ending the logging')
log_helper.end_logging()
def test_rough_doctype(self):
"""Checks all samples, expect either ole files or good ooxml output"""
# map from extension to expected doctype
ext2doc = dict(
docx=ooxml.DOCTYPE_WORD, docm=ooxml.DOCTYPE_WORD,
dotx=ooxml.DOCTYPE_WORD, dotm=ooxml.DOCTYPE_WORD,
xml=(ooxml.DOCTYPE_EXCEL_XML, ooxml.DOCTYPE_WORD_XML),
xlsx=ooxml.DOCTYPE_EXCEL, xlsm=ooxml.DOCTYPE_EXCEL,
xlsb=ooxml.DOCTYPE_EXCEL, xlam=ooxml.DOCTYPE_EXCEL,
xltx=ooxml.DOCTYPE_EXCEL, xltm=ooxml.DOCTYPE_EXCEL,
pptx=ooxml.DOCTYPE_POWERPOINT, pptm=ooxml.DOCTYPE_POWERPOINT,
ppsx=ooxml.DOCTYPE_POWERPOINT, ppsm=ooxml.DOCTYPE_POWERPOINT,
potx=ooxml.DOCTYPE_POWERPOINT, potm=ooxml.DOCTYPE_POWERPOINT,
ods=ooxml.DOCTYPE_NONE, odt=ooxml.DOCTYPE_NONE,
odp=ooxml.DOCTYPE_NONE,
)
# files that are neither OLE nor xml:
except_files = 'empty', 'text'
except_extns = 'rtf', 'csv', 'zip'
def test_clean_rtf_ddeonly(self):
""" find no dde links in rtf spec """
filename = 'RTF-Spec-1.7.rtf'
output = msodde.process_maybe_encrypted(
join(BASE_DIR, 'msodde', filename),
field_filter_mode=msodde.FIELD_FILTER_DDE)
self.assertEqual(len(self.get_dde_from_output(output)), 0,
msg='Found dde links in output of ' + filename)
def test_standard_password(self):
"""Check dde-link is found in xls[mb] sample files."""
for suffix in 'xls', 'xlsx', 'xlsm', 'xlsb':
example_file = pjoin(DATA_BASE_DIR, 'encrypted',
'dde-test-encrypt-standardpassword.' + suffix)
link_text = msodde.process_maybe_encrypted(example_file)
self.assertEqual(link_text, 'cmd /c calc.exe',
msg='Unexpected output {!r} for {}'
.format(link_text, suffix))