doc: add readthedocs config
[rarfile.git] / test / test_korrupt.py
blob40034940b98e20c90ae6e688ed372c9ad153f7e6
1 """test corrupt file parsing.
2 """
4 import glob
5 import io
7 import rarfile
10 def try_read(tmpfn):
11 if not rarfile.is_rarfile(tmpfn):
12 return
13 rarfile.RarFile(tmpfn, errors="stop")
14 try:
15 rf = rarfile.RarFile(tmpfn, errors="strict")
16 if rf.needs_password():
17 rf.setpassword("password")
18 except rarfile.Error:
19 return
20 for fn in rf.namelist():
21 try:
22 rf.read(fn)
23 except rarfile.Error:
24 pass
27 def process_rar(rarfn, quick=False):
28 with open(rarfn, "rb") as f:
29 data = f.read()
30 for n in range(len(data)):
31 bad = io.BytesIO(data[:n])
32 if not rarfile.is_rarfile(bad):
33 return
34 try_read(bad)
36 crap = b"\x00\xff\x01\x80\x7f"
37 if quick:
38 crap = b"\xff"
39 for n in range(1, len(data)):
40 for i in range(len(crap)):
41 c = crap[i:i + 1]
42 bad = data[:n - 1] + c + data[n:]
43 try_read(io.BytesIO(bad))
46 def test_corrupt_quick_rar3():
47 process_rar("test/files/rar3-comment-plain.rar", True)
50 def test_corrupt_quick_rar5():
51 process_rar("test/files/rar5-times.rar", True)
54 def test_corrupt_all():
55 test_rar_list = glob.glob("test/files/*.rar")
56 test_rar_list = []
57 for rar in test_rar_list:
58 process_rar(rar)
61 if __name__ == "__main__":
62 test_corrupt_quick_rar5()