forked from SpiderOak/ZipStream
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_zipstream.py
More file actions
199 lines (159 loc) · 6.19 KB
/
test_zipstream.py
File metadata and controls
199 lines (159 loc) · 6.19 KB
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
import os
import tempfile
import unittest
import zipstream
import zipfile
SAMPLE_FILE_RTF = 'tests/sample.rtf'
class ZipInfoTestCase(unittest.TestCase):
pass
class ZipStreamTestCase(unittest.TestCase):
def setUp(self):
self.fileobjs = [
tempfile.NamedTemporaryFile(delete=False, suffix='.txt'),
tempfile.NamedTemporaryFile(delete=False, suffix='.py'),
]
def tearDown(self):
for fileobj in self.fileobjs:
fileobj.close()
os.remove(fileobj.name)
def test_init_no_args(self):
zipstream.ZipFile()
def test_init_mode(self):
try:
zipstream.ZipFile(mode='w')
except Exception as err:
self.fail(err)
for mode in ['wb', 'r', 'rb', 'a', 'ab']:
self.assertRaises(Exception, zipstream.ZipFile, mode=mode)
for mode in ['wb', 'r', 'rb', 'a', 'ab']:
self.assertRaises(Exception, zipstream.ZipFile, mode=mode + '+')
def test_write_file(self):
z = zipstream.ZipFile(mode='w')
for fileobj in self.fileobjs:
z.write(fileobj.name)
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
for chunk in z:
f.write(chunk)
f.close()
z2 = zipfile.ZipFile(f.name, 'r')
self.assertFalse(z2.testzip())
os.remove(f.name)
def test_write_iterable(self):
z = zipstream.ZipFile(mode='w')
def string_generator():
for _ in range(10):
yield b'zipstream\x01\n'
data = [string_generator(), string_generator()]
for i, d in enumerate(data):
z.write_iter(iterable=d, arcname='data_{0}'.format(i))
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
for chunk in z:
f.write(chunk)
f.close()
z2 = zipfile.ZipFile(f.name, 'r')
self.assertFalse(z2.testzip())
os.remove(f.name)
def test_writestr(self):
z = zipstream.ZipFile(mode='w')
with open(SAMPLE_FILE_RTF, 'rb') as fp:
z.writestr('sample.rtf', fp.read())
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
for chunk in z:
f.write(chunk)
f.close()
z2 = zipfile.ZipFile(f.name, 'r')
self.assertFalse(z2.testzip())
os.remove(f.name)
def test_write_iterable_no_archive(self):
z = zipstream.ZipFile(mode='w')
self.assertRaises(TypeError, z.write_iter, iterable=range(10))
def test_write_iterable_zip64_with_not_allow_zip64_many_smalls(self):
# check many small streams that sum length require ZIP64 extensions when not allowed zip64
z = zipstream.ZipFile(mode='w', allowZip64=False)
def string_small_generator():
counter = 0
sample = b'zipstream0' * 10000000
len_sample = len(sample)
while counter + len_sample < zipstream.ZIP64_LIMIT:
counter += len_sample
yield sample
data = [string_small_generator(), string_small_generator()]
for i, d in enumerate(data):
z.write_iter(iterable=d, arcname='data_{0}'.format(i))
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
try:
self.assertRaises(zipfile.LargeZipFile, lambda: [f.write(c) for c in z])
f.close()
except Exception:
raise
finally:
os.remove(f.name)
def test_write_iterable_zip64_with_not_allow_zip64_1_big_file(self):
# check 1 big stream that length require ZIP64 extensions when not allowed zip64
z = zipstream.ZipFile(mode='w', allowZip64=False)
def string_big_generator():
counter = 0
sample = b'zipstream0' * 10000000
len_sample = len(sample)
while counter < zipstream.ZIP64_LIMIT:
counter += len_sample
yield sample
data = [string_big_generator()]
for i, d in enumerate(data):
z.write_iter(iterable=d, arcname='data_{0}'.format(i))
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
try:
self.assertRaises(zipfile.LargeZipFile, lambda: [f.write(c) for c in z])
f.close()
except Exception:
raise
finally:
os.remove(f.name)
def test_write_iterable_zip64_with_allow_zip64_many_smalls(self):
# check many small streams that sum length require ZIP64 extensions when allowed zip64
z = zipstream.ZipFile(mode='w', allowZip64=True)
def string_small_generator():
counter = 0
sample = b'zipstream0' * 10000000
len_sample = len(sample)
while counter + len_sample < zipstream.ZIP64_LIMIT:
counter += len_sample
yield sample
data = [string_small_generator(), string_small_generator()]
for i, d in enumerate(data):
z.write_iter(iterable=d, arcname='data_{0}'.format(i))
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
try:
for chunk in z:
f.write(chunk)
f.close()
except Exception:
raise
finally:
os.remove(f.name)
def test_write_iterable_zip64_with_allow_zip64_1_big_file(self):
# check 1 big stream that length require ZIP64 extensions when allowed zip64
z = zipstream.ZipFile(mode='w', allowZip64=True)
def string_big_generator():
counter = 0
sample = b'zipstream0' * 10000000
len_sample = len(sample)
while counter < zipstream.ZIP64_LIMIT:
counter += len_sample
yield sample
data = [string_big_generator()]
for i, d in enumerate(data):
z.write_iter(iterable=d, arcname='data_{0}'.format(i))
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
try:
for chunk in z:
f.write(chunk)
f.close()
except Exception:
raise
finally:
os.remove(f.name)
if __name__ == '__main__':
unittest.main()