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
|
"""test corrupt file parsing.
"""
import glob
import io
import rarfile
def try_read(tmpfn):
if not rarfile.is_rarfile(tmpfn):
return
rarfile.RarFile(tmpfn, errors="stop")
try:
rf = rarfile.RarFile(tmpfn, errors="strict")
if rf.needs_password():
rf.setpassword("password")
except rarfile.Error:
return
for fn in rf.namelist():
try:
rf.read(fn)
except rarfile.Error:
pass
def process_rar(rarfn, quick=False):
with open(rarfn, "rb") as f:
data = f.read()
for n in range(len(data)):
bad = io.BytesIO(data[:n])
if not rarfile.is_rarfile(bad):
return
try_read(bad)
crap = b"\x00\xff\x01\x80\x7f"
if quick:
crap = b"\xff"
for n in range(1, len(data)):
for i in range(len(crap)):
c = crap[i:i + 1]
bad = data[:n - 1] + c + data[n:]
try_read(io.BytesIO(bad))
def test_corrupt_quick_rar3():
process_rar("test/files/rar3-comment-plain.rar", True)
def test_corrupt_quick_rar5():
process_rar("test/files/rar5-times.rar", True)
def test_corrupt_all():
test_rar_list = glob.glob("test/files/*.rar")
test_rar_list = []
for rar in test_rar_list:
process_rar(rar)
if __name__ == "__main__":
test_corrupt_quick_rar5()
|