2014-06-10 13:10:47 +04:00
|
|
|
from helper import unittest, PillowTestCase, tearDownModule
|
|
|
|
|
2014-04-14 00:16:01 +04:00
|
|
|
import datetime
|
2014-04-11 15:53:33 +04:00
|
|
|
|
|
|
|
import PIL.OleFileIO as OleFileIO
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestOleFileIo(PillowTestCase):
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_isOleFile_false(self):
|
|
|
|
# Arrange
|
|
|
|
non_ole_file = "Tests/images/flower.jpg"
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
is_ole = OleFileIO.isOleFile(non_ole_file)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertFalse(is_ole)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_isOleFile_true(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
is_ole = OleFileIO.isOleFile(ole_file)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertTrue(is_ole)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_exists_worddocument(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
exists = ole.exists('worddocument')
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertTrue(exists)
|
|
|
|
ole.close()
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_exists_no_vba_macros(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
exists = ole.exists('macros/vba')
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertFalse(exists)
|
|
|
|
ole.close()
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_get_type(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
2014-04-11 15:53:33 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
type = ole.get_type('worddocument')
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertEqual(type, OleFileIO.STGTY_STREAM)
|
|
|
|
ole.close()
|
2014-04-14 01:27:08 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_get_size(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
size = ole.get_size('worddocument')
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertGreater(size, 0)
|
|
|
|
ole.close()
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_get_rootentry_name(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
root = ole.get_rootentry_name()
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertEqual(root, "Root Entry")
|
|
|
|
ole.close()
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_meta(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
meta = ole.get_metadata()
|
2014-04-14 00:16:01 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertEqual(meta.author, b"Laurence Ipsum")
|
|
|
|
self.assertEqual(meta.num_pages, 1)
|
|
|
|
ole.close()
|
2014-04-14 01:05:31 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_gettimes(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
|
|
|
root_entry = ole.direntries[0]
|
2014-04-14 01:05:31 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
ctime = root_entry.getctime()
|
|
|
|
mtime = root_entry.getmtime()
|
2014-04-14 01:05:31 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assertIsInstance(ctime, type(None))
|
|
|
|
self.assertIsInstance(mtime, datetime.datetime)
|
|
|
|
self.assertEqual(ctime, None)
|
|
|
|
self.assertEqual(mtime.year, 2014)
|
|
|
|
ole.close()
|
2014-04-14 01:05:31 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_listdir(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
dirlist = ole.listdir()
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
self.assertIn(['WordDocument'], dirlist)
|
|
|
|
ole.close()
|
|
|
|
|
|
|
|
def test_debug(self):
|
|
|
|
# Arrange
|
|
|
|
ole_file = "Tests/images/test-ole-file.doc"
|
|
|
|
ole = OleFileIO.OleFileIO(ole_file)
|
|
|
|
meta = ole.get_metadata()
|
|
|
|
|
|
|
|
# Act
|
|
|
|
OleFileIO.set_debug_mode(True)
|
|
|
|
ole.dumpdirectory()
|
|
|
|
meta.dump()
|
|
|
|
|
|
|
|
OleFileIO.set_debug_mode(False)
|
|
|
|
ole.dumpdirectory()
|
|
|
|
meta.dump()
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
# No assert, just check they run ok
|
|
|
|
ole.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2014-04-14 01:05:31 +04:00
|
|
|
|
2014-04-11 15:53:33 +04:00
|
|
|
# End of file
|