Debian Codegraph @

tests/test_multivolume.py

raw symbols SCIP
1 import binascii
2 import hashlib
3 import os
4 import shutil
5
6 import pytest
7
8 import multivolumefile as MV
9
10 BLOCKSIZE = 8192
11 testdata_path = os.path.join(os.path.dirname(__file__), "data")
12 os.umask(0o022)
13
14
15 def test_read_check_sha1():
16 target = os.path.join(testdata_path, "archive.7z")
17 mv = MV.open(target, mode="rb")
18 sha = hashlib.sha256()
19 data = mv.read(BLOCKSIZE)
20 while len(data) > 0:
21 sha.update(data)
22 data = mv.read(BLOCKSIZE)
23 mv.close()
24 assert sha.digest() == binascii.unhexlify(
25 "e0cfeb8010d105d0e2d56d2f94d2b786ead352cef5bca579cb7689d08b3614d1"
26 )
27
28
29 def test_read_seek():
30 target = os.path.join(testdata_path, "archive.7z")
31 mv = MV.open(target, mode="rb")
32 assert mv.isatty() is False
33 assert mv.seekable()
34 mv.seek(40000)
35 pos = mv.tell()
36 assert pos == 40000
37 mv.seek(-1000, os.SEEK_CUR)
38 pos = mv.tell()
39 assert pos == 39000
40 mv.seek(-2337, os.SEEK_END)
41 pos = mv.tell()
42 assert pos == 50000
43 mv.close()
44 assert mv.readable() is False
45 mv.flush()
46
47
48 def test_read_context():
49 target = os.path.join(testdata_path, "archive.7z")
50 with MV.open(target, mode="rb") as mv:
51 assert mv.readable()
52 mv.seek(24900)
53 data = mv.read(200)
54 assert len(data) == 100
55 data = mv.read(200)
56 assert len(data) == 200
57
58
59 def test_readinto():
60 target = os.path.join(testdata_path, "archive.7z")
61 b = bytearray(200)
62 with MV.open(target, mode="rb") as mv:
63 mv.seek(24900)
64 size = mv.readinto(b)
65 assert size == 100
66
67
68 def test_read_boundary():
69 target = os.path.join(testdata_path, "archive.7z")
70 with MV.open(target, mode="rb") as mv:
71 mv.seek(24999)
72 b = mv.read(200)
73 assert len(b) == 1
74
75
76 def test_readinto_boundary():
77 target = os.path.join(testdata_path, "archive.7z")
78 b = bytearray(200)
79 with MV.open(target, mode="rb") as mv:
80 mv.seek(25000)
81 size = mv.readinto(b)
82 assert size == 200
83 sha = hashlib.sha256(b)
84 assert (
85 sha.digest()
86 == b"6\xbc\x92\n\xa53tY\x97\xaa`!\xf3\xa7\xaa\xc3\\V\xac\xc1p\x97\xb0\xf5M\xc0)\x12\x0eD$\x9a"
87 )
88
89
90 def test_readline():
91 target = os.path.join(testdata_path, "archive.7z")
92 with pytest.raises(NotImplementedError):
93 with MV.open(target, mode="rb") as mv:
94 mv.readline()
95
96
97 def test_readlines():
98 target = os.path.join(testdata_path, "archive.7z")
99 with pytest.raises(NotImplementedError):
100 with MV.open(target, mode="rb") as mv:
101 mv.readlines()
102
103
104 def test_readall():
105 target = os.path.join(testdata_path, "archive.7z")
106 with MV.open(target, mode="rb") as mv:
107 b = mv.readall()
108 sha = hashlib.sha256(b)
109 assert sha.digest() == binascii.unhexlify(
110 "e0cfeb8010d105d0e2d56d2f94d2b786ead352cef5bca579cb7689d08b3614d1"
111 )
112
113
114 def test_write(tmp_path):
115 target = tmp_path.joinpath("target.7z")
116 with MV.open(target, mode="wb", volume=10240) as volume:
117 assert volume.writable()
118 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
119 data = r.read(BLOCKSIZE)
120 while len(data) > 0:
121 volume.write(data)
122 data = r.read(BLOCKSIZE)
123
124 with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r:
125 data = r.read(BLOCKSIZE)
126 while len(data) > 0:
127 volume.write(data)
128 data = r.read(BLOCKSIZE)
129 assert volume.seekable()
130 volume.seek(0)
131 volume.seek(51000)
132 volume.flush()
133 created = tmp_path.joinpath("target.7z.0001")
134 assert created.exists()
135 assert created.stat().st_size == 10240
136
137
138 def test_write_default_volume(tmp_path):
139 target = tmp_path.joinpath("target.7z").as_posix()
140 with MV.open(target, mode="wb") as volume:
141 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
142 data = r.read(BLOCKSIZE)
143 while len(data) > 0:
144 volume.write(data)
145 data = r.read(BLOCKSIZE)
146 with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r:
147 data = r.read(BLOCKSIZE)
148 while len(data) > 0:
149 volume.write(data)
150 data = r.read(BLOCKSIZE)
151 assert volume.seekable()
152 volume.seek(0)
153 volume.seek(51000)
154 volume.flush()
155 created = tmp_path.joinpath("target.7z.0001")
156 assert created.exists()
157 assert created.stat().st_size == 52337
158
159
160 def test_write_posixpath(tmp_path):
161 target = tmp_path.joinpath("target.7z").as_posix()
162 with MV.open(target, mode="wb", volume=10240) as volume:
163 assert volume.writable()
164 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
165 data = r.read(BLOCKSIZE)
166 while len(data) > 0:
167 volume.write(data)
168 data = r.read(BLOCKSIZE)
169
170 with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r:
171 data = r.read(BLOCKSIZE)
172 while len(data) > 0:
173 volume.write(data)
174 data = r.read(BLOCKSIZE)
175 assert volume.seekable()
176 volume.seek(0)
177 volume.seek(51000)
178 volume.flush()
179 created = tmp_path.joinpath("target.7z.0001")
180 assert created.exists()
181 assert created.stat().st_size == 10240
182
183
184 def test_write_boundary(tmp_path):
185 target = tmp_path.joinpath("target.7z")
186 with MV.open(target, mode="wb", volume=10240) as volume:
187 assert volume.writable()
188 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
189 data = r.read(10000)
190 volume.write(data)
191 data = r.read(250)
192 volume.write(data)
193 volume.flush()
194 created = tmp_path.joinpath("target.7z.0002")
195 assert created.exists()
196 assert created.stat().st_size == 10
197
198
199 def test_write_exist(tmp_path):
200 target = tmp_path.joinpath("target.7z")
201 target_volume = tmp_path.joinpath("target.7z.0001")
202 shutil.copyfile(os.path.join(testdata_path, "archive.7z.001"), target_volume)
203 #
204 with MV.open(target, mode="wb", volume=10240) as volume:
205 assert volume.writable()
206 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
207 data = r.read(10000)
208 volume.write(data)
209 volume.flush()
210 created = tmp_path.joinpath("target.7z.0001")
211 assert created.stat().st_size == 10000
212
213
214 def test_exclusive_write(tmp_path):
215 target = tmp_path.joinpath("target.7z")
216 with MV.open(target, mode="xb", volume=10240) as volume:
217 assert volume.writable()
218 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
219 data = r.read(BLOCKSIZE)
220 while len(data) > 0:
221 volume.write(data)
222 data = r.read(BLOCKSIZE)
223
224 with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r:
225 data = r.read(BLOCKSIZE)
226 while len(data) > 0:
227 volume.write(data)
228 data = r.read(BLOCKSIZE)
229 assert volume.seekable()
230 volume.seek(0)
231 volume.seek(51000)
232 volume.flush()
233 created = tmp_path.joinpath("target.7z.0001")
234 assert created.exists()
235 assert created.stat().st_size == 10240
236
237
238 def test_exclusive_write_exist(tmp_path):
239 target = tmp_path.joinpath("target.7z")
240 target_volume = tmp_path.joinpath("target.7z.0001")
241 shutil.copyfile(os.path.join(testdata_path, "archive.7z.001"), target_volume)
242 with pytest.raises(FileExistsError):
243 MV.open(target, "x")
244
245
246 def test_write_append1(tmp_path):
247 target_volume = tmp_path.joinpath("target.7z.0001")
248 with target_volume.open(mode="wb") as target:
249 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as src:
250 target.write(src.read(1000))
251 #
252 target = tmp_path.joinpath("target.7z")
253 with MV.open(target, mode="ab", volume=10240) as volume:
254 assert volume.seekable() is False
255 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as src:
256 src.seek(1000)
257 volume.write(src.read(24000))
258 with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r:
259 data = r.read(BLOCKSIZE)
260 while len(data) > 0:
261 volume.write(data)
262 data = r.read(BLOCKSIZE)
263 volume.flush()
264 #
265 existed = tmp_path.joinpath("target.7z.0001")
266 assert existed.exists()
267 assert existed.stat().st_size == 10240
268 created2 = tmp_path.joinpath("target.7z.0002")
269 assert created2.exists()
270 assert created2.stat().st_size == 10240
271 created6 = tmp_path.joinpath("target.7z.0006")
272 assert created6.exists()
273 assert created6.stat().st_size == 1137
274
275
276 def test_write_append2(tmp_path):
277 target = tmp_path.joinpath("target.7z")
278 target_volume = tmp_path.joinpath("target.7z.0001")
279 shutil.copyfile(os.path.join(testdata_path, "archive.7z.001"), target_volume)
280 #
281 with MV.open(target, mode="ab", volume=10240) as volume:
282 with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r:
283 data = r.read(BLOCKSIZE)
284 while len(data) > 0:
285 volume.write(data)
286 data = r.read(BLOCKSIZE)
287 volume.flush()
288 #
289 existed = tmp_path.joinpath("target.7z.0001")
290 assert existed.exists()
291 assert existed.stat().st_size == 25000
292 created2 = tmp_path.joinpath("target.7z.0002")
293 assert created2.exists()
294 assert created2.stat().st_size == 10240
295 created4 = tmp_path.joinpath("target.7z.0004")
296 assert created4.exists()
297 assert created4.stat().st_size == 6857 # 52337 - 25000 - 10240 * 2
298
299
300 def test_read_double_close():
301 target = os.path.join(testdata_path, "archive.7z")
302 mv = MV.open(target, mode="rb")
303 assert not mv.closed
304 mv.close()
305 assert mv.closed
306 mv.close()
307
308
309 def test_read_fileno():
310 target = os.path.join(testdata_path, "archive.7z")
311 mv = MV.open(target, mode="rb")
312 with pytest.raises(RuntimeError):
313 mv.fileno()
314 mv.close()
315
316
317 def test_write_short_digits(tmp_path):
318 target = tmp_path.joinpath("target.7z")
319 with MV.MultiVolume(
320 target, mode="wb", volume=10240, ext_digits=3, ext_start=0
321 ) as volume:
322 assert volume.writable()
323 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
324 data = r.read(10000)
325 volume.write(data)
326 data = r.read(250)
327 volume.write(data)
328 volume.flush()
329 created = tmp_path.joinpath("target.7z.001")
330 assert created.exists()
331 assert created.stat().st_size == 10
332
333
334 def test_write_hex_digits(tmp_path):
335 target = tmp_path.joinpath("target.7z")
336 with MV.MultiVolume(
337 target, mode="wb", volume=800, hex=True, ext_digits=3, ext_start=0
338 ) as volume:
339 assert volume.writable()
340 with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r:
341 for _ in range(5):
342 data = r.read(2000)
343 volume.write(data)
344 data = r.read(250)
345 volume.write(data)
346 volume.flush()
347 created = tmp_path.joinpath("target.7z.00c")
348 assert created.exists()
349 assert created.stat().st_size == 650
350
351
352 def test_unsupported_open_mode(tmp_path):
353 target = tmp_path.joinpath("target.7z")
354 with pytest.raises(NotImplementedError):
355 with MV.MultiVolume(target, mode="qb", volume=800):
356 pass
357
358
359 def test_stat():
360 target = os.path.join(testdata_path, "archive.7z")
361 first_target = os.path.join(testdata_path, "archive.7z.001")
362 with MV.open(target, mode="rb") as mv:
363 st = mv.stat()
364 assert st.st_size == 52337
365 assert st.st_mtime == os.stat(first_target).st_mtime
366 assert st.st_mtime_ns == os.stat(first_target).st_mtime_ns
367 assert st.st_dev == os.stat(first_target).st_dev
368 assert st.st_ino == 0