File: testcorrupt.py

package info (click to toggle)
python-rarfile 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 340 kB
  • ctags: 306
  • sloc: python: 1,764; makefile: 156; sh: 36
file content (85 lines) | stat: -rwxr-xr-x 1,871 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python

import rarfile
import sys, os, time
import tempfile

def progress():
    sys.stdout.write('.')
    sys.stdout.flush()

def try_read(tmpfn):
    #progress()
    try:
        rf = rarfile.RarFile(tmpfn)
        if rf.needs_password():
            rf.setpassword('password')
    except rarfile.Error:
        return
    for fn in rf.namelist():
        try:
            data = rf.read(fn)
            pass
        except rarfile.Error:
            pass

def test_rar(rarfn):
    data = open(rarfn, "rb").read()

    fd, tmpfn = tempfile.mkstemp('.rar')
    os.close(fd)

    print('testcorrupt 1')
    for n in range(len(data)):
        bad = data[:n]
        f = open(tmpfn, 'wb')
        f.write(bad)
        f.close()
        
        try_read(tmpfn)

    print('testcorrupt 2')
    crap = rarfile.RAR_ID
    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:]
            f = open(tmpfn, 'wb')
            f.write(bad)
            f.close()
            try_read(tmpfn)

    os.unlink(tmpfn)

test_rar_list = [
    "files/ctime0.rar",
    "files/ctime1.rar",
    "files/ctime2.rar",
    "files/ctime3.rar",
    "files/ctime4.rar",
    "files/seektest.rar",
    "files/rar15-comment-lock.rar",
    "files/rar15-comment.rar",
    "files/rar202-comment-nopsw.rar",
    "files/rar202-comment-psw.rar",
    "files/rar3-comment-hpsw.rar",
    "files/rar3-comment-plain.rar",
    "files/rar3-comment-psw.rar",
    "files/unicode.rar",
]

def main():
    if sys.argv[-1] == '--quick':
        test_rar("files/rar3-comment-plain.rar")
        return
    for rar in test_rar_list:
        print(rar)
        test_rar(rar)

if __name__ == '__main__':
    try:
        main()
    except OSError:
        print('OSError: pid = %d' % os.getpid())
        time.sleep(80000)