forked from smartfile/python-libarchive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
377 lines (307 loc) · 11.9 KB
/
tests.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (c) 2011, SmartFile <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the organization nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os, unittest, tempfile, random, string, sys
import zipfile
import io
from libarchive import Archive, is_archive_name, is_archive
from libarchive.zip import is_zipfile, ZipFile, ZipEntry
PY3 = sys.version_info[0] == 3
TMPDIR = tempfile.mkdtemp(suffix='.python-libarchive')
ZIPFILE = 'test.zip'
ZIPPATH = os.path.join(TMPDIR, ZIPFILE)
FILENAMES = [
'test1.txt',
'foo',
# TODO: test non-ASCII chars.
#'álért.txt',
]
def make_temp_files():
if not os.path.exists(ZIPPATH):
for name in FILENAMES:
with open(os.path.join(TMPDIR, name), 'w') as f:
f.write(''.join(random.sample(string.ascii_letters, 10)))
def make_temp_archive():
make_temp_files()
with zipfile.ZipFile(ZIPPATH, mode="w") as z:
for name in FILENAMES:
z.write(os.path.join(TMPDIR, name), arcname=name)
class TestIsArchiveName(unittest.TestCase):
def test_formats(self):
self.assertEqual(is_archive_name('foo'), None)
self.assertEqual(is_archive_name('foo.txt'), None)
self.assertEqual(is_archive_name('foo.txt.gz'), None)
self.assertEqual(is_archive_name('foo.tar.gz'), 'tar')
self.assertEqual(is_archive_name('foo.tar.bz2'), 'tar')
self.assertEqual(is_archive_name('foo.zip'), 'zip')
self.assertEqual(is_archive_name('foo.rar'), 'rar')
self.assertEqual(is_archive_name('foo.iso'), 'iso')
self.assertEqual(is_archive_name('foo.rpm'), 'cpio')
class TestIsArchiveZip(unittest.TestCase):
def setUp(self):
make_temp_archive()
def test_zip(self):
self.assertEqual(is_archive(ZIPPATH), True)
self.assertEqual(is_archive(ZIPPATH, formats=('zip',)), True)
self.assertEqual(is_archive(ZIPPATH, formats=('tar',)), False)
class TestIsArchiveTar(unittest.TestCase):
def test_tar(self):
pass
# TODO: incorporate tests from:
# http://hg.python.org/cpython/file/a6e1d926cd98/Lib/test/test_zipfile.py
class TestZipRead(unittest.TestCase):
def setUp(self):
make_temp_archive()
self.f = open(ZIPPATH, mode='r')
def tearDown(self):
self.f.close()
def test_iszipfile(self):
self.assertEqual(is_zipfile('/dev/null'), False)
self.assertEqual(is_zipfile(ZIPPATH), True)
def test_iterate(self):
z = ZipFile(self.f, 'r')
count = 0
for e in z:
count += 1
self.assertEqual(count, len(FILENAMES), 'Did not enumerate correct number of items in archive.')
def test_deferred_close_by_archive(self):
"""Test archive deferred close without a stream."""
z = ZipFile(self.f, 'r')
self.assertIsNotNone(z._a)
self.assertIsNone(z._stream)
z.close()
self.assertIsNone(z._a)
def test_deferred_close_by_stream(self):
"""Ensure archive closes self if stream is closed first."""
z = ZipFile(self.f, 'r')
stream = z.readstream(FILENAMES[0])
stream.close()
# Make sure archive stays open after stream is closed.
self.assertIsNotNone(z._a)
self.assertIsNone(z._stream)
z.close()
self.assertIsNone(z._a)
self.assertTrue(stream.closed)
def test_close_stream_first(self):
"""Ensure that archive stays open after being closed if a stream is
open. Further, ensure closing the stream closes the archive."""
z = ZipFile(self.f, 'r')
stream = z.readstream(FILENAMES[0])
z.close()
try:
stream.read()
except:
self.fail("Reading stream from closed archive failed!")
stream.close()
# Now the archive should close.
self.assertIsNone(z._a)
self.assertTrue(stream.closed)
self.assertIsNone(z._stream)
def test_filenames(self):
z = ZipFile(self.f, 'r')
names = []
for e in z:
names.append(e.filename)
self.assertEqual(names, FILENAMES, 'File names differ in archive.')
# ~ def test_non_ascii(self):
# ~ pass
def test_extract_str(self):
pass
class TestZipWrite(unittest.TestCase):
def setUp(self):
make_temp_files()
self.f = open(ZIPPATH, mode='w')
def tearDown(self):
self.f.close()
def test_writepath(self):
z = ZipFile(self.f, 'w')
for fname in FILENAMES:
with open(os.path.join(TMPDIR, fname), 'r') as f:
z.writepath(f)
z.close()
def test_writepath_directory(self):
"""Test writing a directory."""
z = ZipFile(self.f, 'w')
z.writepath(None, pathname='/testdir', folder=True)
z.writepath(None, pathname='/testdir/testinside', folder=True)
z.close()
self.f.close()
f = open(ZIPPATH, mode='r')
z = ZipFile(f, 'r')
entries = z.infolist()
assert len(entries) == 2
assert entries[0].isdir()
z.close()
f.close()
def test_writestream(self):
z = ZipFile(self.f, 'w')
for fname in FILENAMES:
full_path = os.path.join(TMPDIR, fname)
i = open(full_path)
o = z.writestream(fname)
while True:
data = i.read(1)
if not data:
break
if PY3:
o.write(data)
else:
o.write(unicode(data))
o.close()
i.close()
z.close()
def test_writestream_unbuffered(self):
z = ZipFile(self.f, 'w')
for fname in FILENAMES:
full_path = os.path.join(TMPDIR, fname)
i = open(full_path)
o = z.writestream(fname, os.path.getsize(full_path))
while True:
data = i.read(1)
if not data:
break
if PY3:
o.write(data)
else:
o.write(unicode(data))
o.close()
i.close()
z.close()
def test_deferred_close_by_archive(self):
"""Test archive deferred close without a stream."""
z = ZipFile(self.f, 'w')
o = z.writestream(FILENAMES[0])
z.close()
self.assertIsNotNone(z._a)
self.assertIsNotNone(z._stream)
if PY3:
o.write('testdata')
else:
o.write(unicode('testdata'))
o.close()
self.assertIsNone(z._a)
self.assertIsNone(z._stream)
z.close()
import base64
# ZIP_CONTENT is base64 encoded password protected zip file with password: 'pwd' and following contents:
# unzip -l /tmp/zzz.zip
#Archive: /tmp/zzz.zip
# Length Date Time Name
#--------- ---------- ----- ----
# 9 08-09-2022 19:29 test.txt
#--------- -------
# 9 1 file
ZIP_CONTENT='UEsDBAoACQAAAKubCVVjZ7b1FQAAAAkAAAAIABwAdGVzdC50eHRVVAkAA5K18mKStfJid' + \
'XgLAAEEAAAAAAQAAAAA5ryoP1rrRK5apjO41YMAPjpkWdU3UEsHCGNntvUVAAAACQAAAF' + \
'BLAQIeAwoACQAAAKubCVVjZ7b1FQAAAAkAAAAIABgAAAAAAAEAAACkgQAAAAB0ZXN0LnR' + \
'4dFVUBQADkrXyYnV4CwABBAAAAAAEAAAAAFBLBQYAAAAAAQABAE4AAABnAAAAAAA='
ITEM_CONTENT='test.txt\n'
ITEM_NAME='test.txt'
ZIP1_PWD='pwd'
ZIP2_PWD='12345'
def create_file_from_content():
if PY3:
with open(ZIPPATH, mode='wb') as f:
f.write(base64.b64decode(ZIP_CONTENT))
else:
with open(ZIPPATH, mode='w') as f:
f.write(base64.b64decode(ZIP_CONTENT))
def create_protected_zip():
z = ZipFile(ZIPPATH, mode='w', password=ZIP2_PWD)
z.writestr(ITEM_NAME, ITEM_CONTENT)
z.close()
class TestProtectedReading(unittest.TestCase):
def setUp(self):
create_file_from_content()
def tearDown(self):
os.remove(ZIPPATH)
def test_read_with_password(self):
z = ZipFile(ZIPPATH, 'r', password=ZIP1_PWD)
if PY3:
self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
else:
self.assertEqual(z.read(ITEM_NAME), ITEM_CONTENT)
z.close()
def test_read_without_password(self):
z = ZipFile(ZIPPATH, 'r')
self.assertRaises(RuntimeError, z.read, ITEM_NAME)
z.close()
def test_read_with_wrong_password(self):
z = ZipFile(ZIPPATH, 'r', password='wrong')
self.assertRaises(RuntimeError, z.read, ITEM_NAME)
z.close()
class TestProtectedWriting(unittest.TestCase):
def setUp(self):
create_protected_zip()
def tearDown(self):
os.remove(ZIPPATH)
def test_read_with_password(self):
z = ZipFile(ZIPPATH, 'r', password=ZIP2_PWD)
if PY3:
self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
else:
self.assertEqual(z.read(ITEM_NAME), ITEM_CONTENT)
z.close()
def test_read_without_password(self):
z = ZipFile(ZIPPATH, 'r')
self.assertRaises(RuntimeError, z.read, ITEM_NAME)
z.close()
def test_read_with_wrong_password(self):
z = ZipFile(ZIPPATH, 'r', password='wrong')
self.assertRaises(RuntimeError, z.read, ITEM_NAME)
z.close()
def test_read_with_password_list(self):
z = ZipFile(ZIPPATH, 'r', password=[ZIP1_PWD, ZIP2_PWD])
if PY3:
self.assertEqual(z.read(ITEM_NAME), bytes(ITEM_CONTENT, 'utf-8'))
else:
self.assertEqual(z.read(ITEM_NAME), ITEM_CONTENT)
z.close()
class TestHighLevelAPI(unittest.TestCase):
def setUp(self):
make_temp_archive()
def _test_listing_content(self, f):
"""Test helper capturing file paths while iterating the archive."""
found = []
with Archive(f) as a:
for entry in a:
found.append(entry.pathname)
self.assertEqual(set(found), set(FILENAMES))
def test_open_by_name(self):
"""Test an archive opened directly by name."""
self._test_listing_content(ZIPPATH)
def test_open_by_named_fobj(self):
"""Test an archive using a file-like object opened by name."""
with open(ZIPPATH, 'rb') as f:
self._test_listing_content(f)
def test_open_by_unnamed_fobj(self):
"""Test an archive using file-like object opened by fileno()."""
with open(ZIPPATH, 'rb') as zf:
with io.FileIO(zf.fileno(), mode='r', closefd=False) as f:
self._test_listing_content(f)
if __name__ == '__main__':
unittest.main()