1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
"""
Tests for olefile
"""
from __future__ import print_function
import unittest
import os, sys
import io
from shutil import copy2
# Insert the olefile package relative to this test dir in the sys.path
# to make sure we test the right one, in case another olefile version
# is also installed in the main python path.
_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
# print('_thismodule_dir = %r' % _thismodule_dir)
_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
# print('_parent_dir = %r' % _thirdparty_dir)
if _parent_dir not in sys.path:
sys.path.insert(0, _parent_dir)
import olefile
class Test_isOleFile(unittest.TestCase):
'Tests for the isOleFile function'
def setUp(self):
self.non_ole_file = u"tests/images/flower.jpg"
self.ole_file = u"tests/images/test-ole-file.doc"
def test_isOleFile_str_path_false(self):
'Test isOleFile with a unicode filename of a non-OLE file'
is_ole = olefile.isOleFile(filename=self.non_ole_file)
self.assertFalse(is_ole)
def test_isOleFile_str_path_true(self):
'Test isOleFile with a unicode filename of an OLE file'
is_ole = olefile.isOleFile(filename=self.ole_file)
self.assertTrue(is_ole)
def test_isOleFile_bytes_path_false(self):
'Test isOleFile with a bytes filename of a non-OLE file'
is_ole = olefile.isOleFile(filename=self.non_ole_file.encode('latin_1'))
self.assertFalse(is_ole)
def test_isOleFile_bytes_path_true(self):
'Test isOleFile with a bytes filename of an OLE file'
is_ole = olefile.isOleFile(filename=self.ole_file.encode('latin_1'))
self.assertTrue(is_ole)
def test_isOleFile_bytes_string_false(self):
'Test isOleFile with contents of a non-OLE file in a bytes string as filename parameter'
with open(self.non_ole_file, 'rb') as f:
data = f.read()
assert (len(data) >= olefile.MINIMAL_OLEFILE_SIZE)
is_ole = olefile.isOleFile(filename=data)
self.assertFalse(is_ole)
def test_isOleFile_small_bytes_string_false(self):
'Test isOleFile with contents of a small non-OLE file (<1536b) in a bytes string as filename parameter'
data = b" " * 100
# In this case, we expect a FileNotFoundError exception, because isOleFile treats filename as a path
# due to issue #142
if sys.version_info[0] < 3:
# On Python 2.x, this case raises an IOError exception:
expected_exception = IOError
else:
# On Python 3.x, this case raises a FileNotFoundError exception:
expected_exception = FileNotFoundError
with self.assertRaises(expected_exception):
is_ole = olefile.isOleFile(filename=data)
def test_isOleFile_bytes_string_true(self):
'Test isOleFile with contents of an OLE file in a bytes string as filename parameter'
with open(self.ole_file, 'rb') as f:
data = f.read()
assert(len(data) >= olefile.MINIMAL_OLEFILE_SIZE)
is_ole = olefile.isOleFile(filename=data)
self.assertTrue(is_ole)
def test_isOleFile_bytes_data_false(self):
'Test isOleFile with contents of a non-OLE file in a bytes string as data parameter'
with open(self.non_ole_file, 'rb') as f:
data = f.read()
is_ole = olefile.isOleFile(data=data)
self.assertFalse(is_ole)
def test_isOleFile_bytes_data_true(self):
'Test isOleFile with contents of an OLE file in a bytes string as data parameter'
with open(self.ole_file, 'rb') as f:
data = f.read()
is_ole = olefile.isOleFile(data=data)
self.assertTrue(is_ole)
def test_isOleFile_small_bytes_data_false(self):
'Test isOleFile with contents of a small non-OLE file (<1536b) in a bytes string as data parameter'
data = b" " * 100
is_ole = olefile.isOleFile(data=data)
self.assertFalse(is_ole)
def test_isOleFile_small_bytes_ole_magic_data_false(self):
'Test isOleFile with contents of a small non-OLE file (<1536b) starting with OLE magic in a bytes string as data parameter'
with open(self.ole_file, 'rb') as f:
data = f.read(100)
is_ole = olefile.isOleFile(data=data)
self.assertFalse(is_ole)
def test_isOleFile_file_false(self):
'Test isOleFile with a non-OLE file object as filename parameter'
with open(self.non_ole_file, 'rb') as f:
is_ole = olefile.isOleFile(filename=f)
self.assertFalse(is_ole)
def test_isOleFile_file_true(self):
'Test isOleFile with an OLE file object as filename parameter'
with open(self.ole_file, 'rb') as f:
is_ole = olefile.isOleFile(filename=f)
self.assertTrue(is_ole)
class TestOleFileIO(unittest.TestCase):
'Tests for the OleFileIO class'
def setUp(self):
self.non_ole_file = u"tests/images/flower.jpg"
self.ole_file = u"tests/images/test-ole-file.doc"
def test_context_manager(self):
with olefile.OleFileIO(self.ole_file) as ole:
exists = ole.exists('worddocument')
self.assertTrue(exists)
def test_exists_worddocument(self):
ole = olefile.OleFileIO(self.ole_file)
exists = ole.exists('worddocument')
self.assertTrue(exists)
ole.close()
def test_exists_no_vba_macros(self):
ole = olefile.OleFileIO(self.ole_file)
exists = ole.exists('macros/vba')
self.assertFalse(exists)
ole.close()
def test_get_type(self):
ole = olefile.OleFileIO(self.ole_file)
doc_type = ole.get_type('worddocument')
self.assertEqual(doc_type, olefile.STGTY_STREAM)
ole.close()
def test_get_size(self):
ole = olefile.OleFileIO(self.ole_file)
size = ole.get_size('worddocument')
self.assertGreater(size, 0)
ole.close()
def test_get_rootentry_name(self):
ole = olefile.OleFileIO(self.ole_file)
root = ole.get_rootentry_name()
self.assertEqual(root, "Root Entry")
ole.close()
def test_meta(self):
ole = olefile.OleFileIO(self.ole_file)
meta = ole.get_metadata()
self.assertEqual(meta.author, b"Laurence Ipsum")
self.assertEqual(meta.num_pages, 1)
ole.close()
def test_minifat_writing(self):
ole_file_copy = "tests/images/test-ole-file-copy.doc"
minifat_stream_name = "\x01compobj"
if os.path.isfile(ole_file_copy):
os.remove(ole_file_copy)
copy2(self.ole_file, ole_file_copy)
ole = olefile.OleFileIO(ole_file_copy, write_mode = True)
stream = ole.openstream(minifat_stream_name)
self.assertTrue(stream.size < ole.minisectorcutoff)
str_read = stream.read()
self.assertTrue(len(str_read) == stream.size)
self.assertTrue(str_read != b'\x00' * stream.size)
stream.close()
ole.write_stream(minifat_stream_name, b'\x00' * stream.size)
ole.close()
ole = olefile.OleFileIO(ole_file_copy)
stream = ole.openstream(minifat_stream_name)
self.assertTrue(stream.size < ole.minisectorcutoff)
str_read_replaced = stream.read()
self.assertTrue(len(str_read_replaced) == stream.size)
self.assertTrue(str_read_replaced != str_read)
self.assertTrue(str_read_replaced == b'\x00' * len(str_read))
stream.close()
ole.close()
os.remove(ole_file_copy)
def test_write_sect_too_large(self):
'Test write_sect with data larger than a sector'
# read OLE file in memory to avoid writing to disk
with open(self.ole_file, 'rb') as f:
file_in_memory = io.BytesIO(f.read())
with olefile.OleFileIO(file_in_memory, write_mode=True) as ole:
size = ole.sector_size + 1
# Attempt to overwrite last sector (ole.nb_sect - 1) with data too large
# it should raise ValueError:
with self.assertRaises(ValueError):
ole.write_sect(ole.nb_sect - 1, b"x" * size)
class FileHandleCloseTest(unittest.TestCase):
"""Test file handles are closed correctly."""
def setUp(self):
self.non_ole_file = "tests/images/flower.jpg"
self.ole_file = "tests/images/test-ole-file.doc"
def leaking_test_function(self):
"""Function that leaks an open OleFileIO."""
ole = olefile.OleFileIO(self.ole_file)
@unittest.skip('Cannot predict when __del__ is run, so cannot test that '
'it issues a warning')
# requires python version 3.2 or higher
def test_warning(self):
"""Test that warning is issued when ole file leaks open fp."""
with self.assertWarns(olefile.OleFileIONotClosed):
self.leaking_test_function()
@unittest.skip('Cannot test attribute fp of OleFileIO instance that '
'failed to construct')
def test_init_fail(self):
"""Test that file handle is closed if open() from __init__ fails."""
ole = None
try:
ole = olefile.OleFileIO(self.non_ole_file)
self.fail('Should have raised an exception')
except Exception as exc:
self.assertEqual(str(exc), 'not an OLE2 structured storage file')
self.assertTrue(ole.fp.closed) # ole is still None
def test_context_manager(self):
"""Test that file handle is closed by context manager."""
file_handle = None
with olefile.OleFileIO(self.ole_file) as ole:
file_handle = ole.fp
self.assertFalse(file_handle.closed)
self.assertTrue(file_handle.closed)
def test_manual(self):
"""Test that simple manual close always closes self-created handle."""
ole = olefile.OleFileIO(self.ole_file)
self.assertFalse(ole.fp.closed)
_ = ole.listdir()
self.assertFalse(ole.fp.closed)
ole.close()
self.assertTrue(ole.fp.closed)
# TODO: check if this test is still relevant
# def test_fp_stays_open(self):
# """Test that fp is not automatically closed if provided by caller."""
# with open(self.ole_file, 'rb') as file_handle:
# self.assertFalse(file_handle.closed)
# with olefile.OleFileIO(file_handle) as ole:
# self.assertFalse(file_handle.closed)
# self.assertEqual(file_handle, ole.fp)
# # check that handle is still open, although ole is now closed
# self.assertFalse(file_handle.closed)
#
# # do something with it
# file_handle.seek(0)
# self.assertTrue(olefile.isOleFile(file_handle))
#
# # now should be closed
# self.assertTrue(file_handle.closed)
if __name__ == '__main__':
unittest.main()
|