4 from datetime
import datetime
12 if isinstance(dt
, datetime
):
13 return dt
.isoformat("T")
14 elif isinstance(dt
, tuple):
15 return "%04d-%02d-%02d %02d:%02d:%02d" % dt
22 for k
, v
in kwargs
.items():
30 for item
in rf
.infolist():
31 info
= mkitem(fn
=item
.filename
,
32 file_size
=item
.file_size
,
33 compress_size
=item
.compress_size
,
35 date_time
=render_date(item
.date_time
),
36 arctime
=render_date(item
.arctime
),
37 mtime
=render_date(item
.mtime
),
38 atime
=render_date(item
.atime
),
39 ctime
=render_date(item
.ctime
),
41 extract_version
=item
.extract_version
,
42 compress_type
=item
.compress_type
,
51 return "Different lengths"
53 for i
, xa
in enumerate(a
):
57 problems
.append("NewKey(%d,%s)=%r" % (i
, k
, xa
[k
]))
60 problems
.append("MissingKey(%d,%s)=%r" % (i
, k
, xb
[k
]))
62 if k
in xb
and xa
[k
] != xb
[k
]:
63 problems
.append("ErrValue(%d,%s):got=%r/exp=%r" % (i
, k
, xa
[k
], xb
[k
]))
64 return "; ".join(problems
)
68 assert a
== b
, diffs(a
, b
)
75 @pytest.mark
.skipif(not rarfile
._have
_crypto
, reason
="No crypto")
76 def test_rar3_header_encryption():
77 r
= rarfile
.RarFile("test/files/rar3-comment-hpsw.rar", "r")
78 assert r
.needs_password() is True
79 assert r
.comment
is None
80 assert r
.namelist() == []
82 r
.setpassword("password")
83 assert r
.needs_password() is True
84 assert r
.namelist() == ["file1.txt", "file2.txt"]
85 assert r
.comment
is not None
86 assert r
.comment
== "RARcomment\n"
89 @pytest.mark
.skipif(not rarfile
._have
_crypto
, reason
="No crypto")
90 def test_rar5_header_encryption():
91 r
= rarfile
.RarFile("test/files/rar5-hpsw.rar")
92 assert r
.needs_password() is True
93 assert r
.comment
is None
94 assert r
.namelist() == []
96 r
.setpassword("password")
97 assert r
.needs_password() is True
98 assert r
.namelist() == ["stest1.txt", "stest2.txt"]
99 assert r
.comment
is not None
100 assert r
.comment
== "RAR5 archive - hdr-password\n"
104 def get_vol_info(extver
=20, tz
="", hr
="11"):
106 mkitem(CRC
=1352324940,
107 date_time
="2016-05-24 %s:42:37%s" % (hr
, ""),
108 mtime
="2016-05-24T%s:42:37%s" % (hr
, tz
),
110 compress_size
=205000,
111 extract_version
=extver
,
115 fn
="vols/bigfile.txt"),
116 mkitem(CRC
=3498712966,
117 date_time
="2016-05-24 %s:42:43%s" % (hr
, ""),
118 mtime
="2016-05-24T%s:42:43%s" % (hr
, tz
),
119 extract_version
=extver
,
125 fn
="vols/smallfile.txt")]
128 def test_rar3_vols():
129 r
= rarfile
.RarFile("test/files/rar3-vols.part1.rar")
130 assert r
.needs_password() is False
131 assert r
.comment
is None
132 assert r
.strerror() is None
133 cmp_struct(dumparc(r
), get_vol_info())
134 assert r
.volumelist() == [
135 "test/files/rar3-vols.part1.rar",
136 "test/files/rar3-vols.part2.rar",
137 "test/files/rar3-vols.part3.rar"]
138 with pytest
.raises(rarfile
.NeedFirstVolume
):
139 rarfile
.RarFile("test/files/rar3-vols.part2.rar")
142 def test_rar3_oldvols():
143 r
= rarfile
.RarFile("test/files/rar3-old.rar")
144 assert r
.needs_password() is False
145 assert r
.comment
is None
146 assert r
.strerror() is None
147 cmp_struct(dumparc(r
), get_vol_info())
148 assert r
.volumelist() == [
149 "test/files/rar3-old.rar",
150 "test/files/rar3-old.r00",
151 "test/files/rar3-old.r01"]
152 with pytest
.raises(rarfile
.NeedFirstVolume
):
153 rarfile
.RarFile("test/files/rar3-old.r00")
156 def test_rar5_vols():
157 r
= rarfile
.RarFile("test/files/rar5-vols.part1.rar")
158 assert r
.needs_password() is False
159 assert r
.comment
is None
160 assert r
.strerror() is None
161 cmp_struct(dumparc(r
), get_vol_info(50, "+00:00", "08"))
162 assert r
.volumelist() == [
163 "test/files/rar5-vols.part1.rar",
164 "test/files/rar5-vols.part2.rar",
165 "test/files/rar5-vols.part3.rar"]
166 with pytest
.raises(rarfile
.NeedFirstVolume
):
167 rarfile
.RarFile("test/files/rar5-vols.part2.rar")
170 def expect_ctime(mtime
, ctime
):
173 date_time
=mtime
.split(".")[0].replace("T", " "),
185 def test_rar3_ctime0():
186 r
= rarfile
.RarFile("test/files/ctime0.rar")
187 cmp_struct(dumparc(r
), expect_ctime("2011-05-10T21:28:47.899345100", None))
190 def test_rar3_ctime1():
191 r
= rarfile
.RarFile("test/files/ctime1.rar")
192 cmp_struct(dumparc(r
), expect_ctime("2011-05-10T21:28:47.899345100", "2011-05-10T21:28:47"))
195 def test_rar3_ctime2():
196 r
= rarfile
.RarFile("test/files/ctime2.rar")
197 cmp_struct(dumparc(r
), expect_ctime("2011-05-10T21:28:47.899345100", "2011-05-10T21:28:47.897843200"))
200 def test_rar3_ctime3():
201 r
= rarfile
.RarFile("test/files/ctime3.rar")
202 cmp_struct(dumparc(r
), expect_ctime("2011-05-10T21:28:47.899345100", "2011-05-10T21:28:47.899328"))
205 def test_rar3_ctime4():
206 r
= rarfile
.RarFile("test/files/ctime4.rar")
207 cmp_struct(dumparc(r
), expect_ctime("2011-05-10T21:28:47.899345100", "2011-05-10T21:28:47.899345100"))
210 def test_rar5_ctime5():
211 r
= rarfile
.RarFile("test/files/ctime5.rar")
212 inf
= r
.getinfo("timed.txt")
213 assert inf
.mtime
.isoformat() == "2020-07-30T20:26:59.677675904+00:00"
214 assert inf
.ctime
.isoformat() == "2020-07-30T20:28:19.398867888+00:00"
215 assert inf
.atime
.isoformat() == "2020-07-30T20:27:10.121196721+00:00"
218 def test_rar5_times():
219 r
= rarfile
.RarFile("test/files/rar5-times.rar")
220 cmp_struct(dumparc(r
), [mkitem(
224 compress_type
=rarfile
.RAR_M3
,
226 host_os
=rarfile
.RAR_OS_UNIX
,
228 date_time
="2011-06-12 09:53:33",
229 mtime
="2011-06-12T09:53:33+00:00",
230 atime
="2016-05-22T09:12:36+00:00",
236 assert rarfile
._next
_oldvol
("archive") == "archive.r00"
237 assert rarfile
._next
_oldvol
("archive.rar/foo") == "archive.rar/foo.r00"
238 assert rarfile
._next
_oldvol
("archive.arr") == "archive.a00"
239 assert rarfile
._next
_oldvol
("archive.brar") == "archive.b00"
240 assert rarfile
._next
_oldvol
("qq00.part0.rar") == "qq00.part0.r00"
241 assert rarfile
._next
_oldvol
("qq00.part0.r00") == "qq00.part0.r01"
242 assert rarfile
._next
_oldvol
("qq00.part0.r29") == "qq00.part0.r30"
243 assert rarfile
._next
_oldvol
("qq00.part0.r99") == "qq00.part0.s00"
247 assert rarfile
._next
_newvol
("qq00.part0.rar") == "qq00.part1.rar"
248 assert rarfile
._next
_newvol
("qq00.part09.rar") == "qq00.part10.rar"
249 assert rarfile
._next
_newvol
("qq00.part99.rar") == "qq00.part100.rar"
250 assert rarfile
._next
_newvol
("part20") == "part21.rar"
251 assert rarfile
._next
_newvol
("qq00.part3.exe") == "qq00.part4.rar"
252 assert rarfile
._next
_newvol
("qq00.part5.sfx") == "qq00.part6.rar"
253 assert rarfile
._next
_newvol
("qq00.part6.bin") == "qq00.part7.bin"
254 assert rarfile
._next
_newvol
("99") == "100.rar"
255 assert rarfile
._next
_newvol
("dir/99.rar") == "dir/100.rar"
256 with pytest
.raises(rarfile
.BadRarName
):
257 rarfile
._next
_newvol
("qq00.part7.rar/foo")
258 with pytest
.raises(rarfile
.BadRarName
):
259 rarfile
._next
_newvol
("foo")
262 def test_newvols_err():
263 with pytest
.raises(rarfile
.BadRarName
):
264 rarfile
._next
_newvol
("xx.rar")
267 @pytest.mark
.parametrize("fn", ["test/files/rar3-versions.rar", "test/files/rar5-versions.rar"])
268 def test_versions(fn
):
269 with rarfile
.RarFile(fn
) as rf
:
270 assert rf
.namelist() == ["versioned.txt"]