Cleanup check-psw, change api
[rarfile.git] / test / test_reading.py
blobd989855e214639c1382bf5cc820a9c3e60032f06
1 """Read all test files.
2 """
4 import io
5 from glob import glob
7 import pytest
9 import rarfile
11 ARCHIVE_COMMENTS = {
12 "rar15-comment-lock.rar": "RARcomment -----",
13 "rar15-comment.rar": "RARcomment -----",
14 "rar202-comment-nopsw.rar": "RARcomment",
15 "rar202-comment-psw.rar": "RARcomment",
16 "rar3-comment-hpsw.rar": "RARcomment\n",
17 "rar3-comment-plain.rar": "RARcomment\n",
18 "rar3-comment-psw.rar": "RARcomment\n",
19 "rar5-blake.rar": "RAR5 archive - blake\n",
20 "rar5-crc.rar": "RAR5 archive - crc\n",
21 "rar5-crc.sfx": "RAR5 archive - crc\n",
22 "rar5-hpsw.rar": "RAR5 archive - hdr-password\n",
23 "rar5-psw-blake.rar": "RAR5 archive - nohdr-password-blake\n",
24 "rar5-psw.rar": "RAR5 archive - nohdr-password\n",
27 ARCHIVE_FILES = [
28 f.replace("\\", "/")
29 for f in sorted(glob("test/files/*.rar"))
30 if "hpsw" not in f
34 def run_reading_normal(fn, comment):
35 try:
36 rf = rarfile.RarFile(fn)
37 except rarfile.NeedFirstVolume:
38 return
39 if rf.needs_password():
40 rf.setpassword("password")
41 assert rf.strerror() is None
42 assert rf.comment == comment
43 for ifn in rf.namelist():
44 if ifn.endswith("/"):
45 continue
47 info = rf.getinfo(ifn)
48 if info.is_dir():
49 continue
50 if info.is_symlink():
51 continue
53 # full read
54 rf.read(ifn)
56 # read from stream
57 item = rf.getinfo(ifn)
58 f = rf.open(ifn)
59 total = 0
60 while True:
61 buf = f.read(1024)
62 if not buf:
63 break
64 total += len(buf)
65 f.close()
66 assert total == item.file_size, ifn
68 # read from stream with readinto
69 bbuf = bytearray(1024)
70 with rf.open(ifn) as f:
71 res = f.readinto(memoryview(bbuf))
72 if res == 0:
73 break
76 def run_reading_inmem(fn, comment):
77 try:
78 rf = rarfile.RarFile(fn)
79 except rarfile.NeedFirstVolume:
80 return
81 if len(rf.volumelist()) > 1:
82 return
84 with io.open(fn, "rb") as f:
85 buf = f.read()
86 run_reading_normal(io.BytesIO(buf), comment)
89 def run_reading(fn):
90 basename = fn.split("/")[-1]
91 comment = ARCHIVE_COMMENTS.get(basename)
92 run_reading_normal(fn, comment)
93 run_reading_inmem(fn, comment)
96 @pytest.mark.parametrize("fn", ARCHIVE_FILES)
97 def test_reading(fn):
98 run_reading(fn)
101 @pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
102 def test_reading_rar3_hpsw():
103 run_reading("test/files/rar3-comment-hpsw.rar")
106 @pytest.mark.skipif(rarfile._have_crypto, reason="Has crypto")
107 def test_reading_rar3_hpsw_nocrypto():
108 with pytest.raises(rarfile.NoCrypto):
109 run_reading("test/files/rar3-comment-hpsw.rar")
112 @pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
113 def test_reading_rar5_hpsw():
114 run_reading("test/files/rar5-hpsw.rar")
117 @pytest.mark.skipif(rarfile._have_crypto, reason="Has crypto")
118 def test_reading_rar5_hpsw_nocrypto():
119 with pytest.raises(rarfile.NoCrypto):
120 run_reading("test/files/rar5-hpsw.rar")
123 def test_reading_rar3_sfx():
124 assert rarfile.is_rarfile("test/files/rar3-seektest.sfx") is False
125 assert rarfile.is_rarfile_sfx("test/files/rar3-seektest.sfx") is True
126 run_reading("test/files/rar3-seektest.sfx")
127 run_reading("test/files/rar3-seektest.sfx")
130 def test_reading_rar5_crc_sfx():
131 assert rarfile.is_rarfile("test/files/rar5-crc.sfx") is False
132 assert rarfile.is_rarfile_sfx("test/files/rar5-crc.sfx") is True
133 run_reading("test/files/rar5-crc.sfx")