File: test_olefileio.py

package info (click to toggle)
pillow 4.0.0-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 20,544 kB
  • ctags: 5,769
  • sloc: python: 25,718; ansic: 24,461; makefile: 403; sh: 117
file content (147 lines) | stat: -rw-r--r-- 3,690 bytes parent folder | download
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
from helper import unittest, PillowTestCase

import datetime

import PIL.OleFileIO as OleFileIO


class TestOleFileIo(PillowTestCase):

    def test_isOleFile(self):
        ole_file = "Tests/images/test-ole-file.doc"

        self.assertTrue(OleFileIO.isOleFile(ole_file))
        with open(ole_file, 'rb') as fp:
            self.assertTrue(OleFileIO.isOleFile(fp))
            self.assertTrue(OleFileIO.isOleFile(fp.read()))

        non_ole_file = "Tests/images/flower.jpg"

        self.assertFalse(OleFileIO.isOleFile(non_ole_file))
        with open(non_ole_file, 'rb') as fp:
            self.assertFalse(OleFileIO.isOleFile(fp))
            self.assertFalse(OleFileIO.isOleFile(fp.read()))

    def test_exists_worddocument(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)

        # Act
        exists = ole.exists('worddocument')

        # Assert
        self.assertTrue(exists)
        ole.close()

    def test_exists_no_vba_macros(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)

        # Act
        exists = ole.exists('macros/vba')

        # Assert
        self.assertFalse(exists)
        ole.close()

    def test_get_type(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)

        # Act
        entry_type = ole.get_type('worddocument')

        # Assert
        self.assertEqual(entry_type, OleFileIO.STGTY_STREAM)
        ole.close()

    def test_get_size(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)

        # Act
        size = ole.get_size('worddocument')

        # Assert
        self.assertGreater(size, 0)
        ole.close()

    def test_get_rootentry_name(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)

        # Act
        root = ole.get_rootentry_name()

        # Assert
        self.assertEqual(root, "Root Entry")
        ole.close()

    def test_meta(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)

        # Act
        meta = ole.get_metadata()

        # Assert
        self.assertEqual(meta.author, b"Laurence Ipsum")
        self.assertEqual(meta.num_pages, 1)
        ole.close()

    def test_gettimes(self):
        # Arrange
        ole_file = "Tests/images/test-ole-file.doc"
        ole = OleFileIO.OleFileIO(ole_file)
        root_entry = ole.direntries[0]

        # Act
        ctime = root_entry.getctime()
        mtime = root_entry.getmtime()

        # Assert
        self.assertIsNone(ctime)
        self.assertIsInstance(mtime, datetime.datetime)
        self.assertEqual(mtime.year, 2014)
        ole.close()

    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()