Fix win32 test
[rarfile.git] / test / test_reading.py
blob87fb17515642d063e4916bde0274725024bdc94b
1 """Read all test files.
2 """
4 import io
6 from glob import glob
8 import rarfile
10 from nose.tools import *
12 _done_reading = set()
14 def run_reading_normal(fn, comment):
15 try:
16 rf = rarfile.RarFile(fn)
17 except rarfile.NeedFirstVolume:
18 return
19 if rf.needs_password():
20 rf.setpassword('password')
21 eq_(rf.strerror(), None)
22 eq_(rf.comment, comment)
23 for ifn in rf.namelist():
25 # full read
26 rf.read(ifn)
28 # read from stream
29 item = rf.getinfo(ifn)
30 f = rf.open(ifn)
31 total = 0
32 while 1:
33 buf = f.read(1024)
34 if not buf:
35 break
36 total += len(buf)
37 f.close()
38 eq_(total, item.file_size)
40 # read from stream with readinto
41 bbuf = bytearray(1024)
42 with rf.open(ifn) as f:
43 res = f.readinto(memoryview(bbuf))
44 if res == 0:
45 break
47 def run_reading_inmem(fn, comment):
48 try:
49 rf = rarfile.RarFile(fn)
50 except rarfile.NeedFirstVolume:
51 return
52 if len(rf.volumelist()) > 1:
53 return
55 buf = io.open(fn, 'rb').read()
56 run_reading_normal(io.BytesIO(buf), comment)
58 def run_reading(fn, comment=None):
59 _done_reading.add(fn)
60 run_reading_normal(fn, comment)
61 run_reading_inmem(fn, comment)
63 def test_reading_rar3_ctime():
64 run_reading('test/files/ctime0.rar')
65 run_reading('test/files/ctime1.rar')
66 run_reading('test/files/ctime2.rar')
67 run_reading('test/files/ctime3.rar')
68 run_reading('test/files/ctime4.rar')
70 def test_reading_rar2():
71 run_reading('test/files/rar15-comment-lock.rar', u'RARcomment -----')
72 run_reading('test/files/rar15-comment.rar', u'RARcomment -----')
73 run_reading('test/files/rar202-comment-nopsw.rar', u'RARcomment')
75 def test_reading_rar3():
76 run_reading('test/files/rar3-comment-plain.rar', u'RARcomment\n')
77 run_reading('test/files/seektest.rar')
78 run_reading('test/files/unicode.rar')
79 run_reading('test/files/unicode2.rar')
81 def test_reading_rar2_psw():
82 run_reading('test/files/rar202-comment-psw.rar', u'RARcomment')
84 def test_reading_rar3_psw():
85 run_reading('test/files/rar3-comment-psw.rar', u'RARcomment\n')
87 if rarfile._have_crypto:
88 def test_reading_rar3_hpsw():
89 run_reading('test/files/rar3-comment-hpsw.rar', u'RARcomment\n')
90 else:
91 @raises(rarfile.NoCrypto)
92 def test_reading_rar3_hpsw_nocrypto():
93 run_reading('test/files/rar3-comment-hpsw.rar', u'RARcomment\n')
95 def test_reading_rar3_vols():
96 run_reading('test/files/rar3-old.rar')
97 run_reading('test/files/rar3-vols.part1.rar')
98 run_reading('test/files/rar3-vols.part2.rar')
99 run_reading('test/files/rar3-vols.part3.rar')
101 def test_reading_rar5_blake():
102 run_reading('test/files/rar5-blake.rar', u'RAR5 archive - blake\n')
104 def test_reading_rar5_crc():
105 run_reading('test/files/rar5-crc.rar', u'RAR5 archive - crc\n')
107 def test_reading_rar5_links():
108 run_reading('test/files/rar5-dups.rar')
109 run_reading('test/files/rar5-hlink.rar')
111 def test_reading_rar5_quick_open():
112 run_reading('test/files/rar5-quick-open.rar')
114 def test_reading_rar5_solid_qo():
115 run_reading('test/files/rar5-solid-qo.rar')
117 def test_reading_rar5_solid():
118 run_reading('test/files/rar5-solid.rar')
120 def test_reading_rar5_times():
121 run_reading('test/files/rar5-times.rar')
122 run_reading('test/files/rar5-times2.rar')
124 def test_reading_rar5_vols():
125 run_reading('test/files/rar5-vols.part1.rar')
126 run_reading('test/files/rar5-vols.part2.rar')
127 run_reading('test/files/rar5-vols.part3.rar')
129 if rarfile._have_crypto:
130 def test_reading_rar5_hpsw():
131 run_reading('test/files/rar5-hpsw.rar', u'RAR5 archive - hdr-password\n')
132 else:
133 @raises(rarfile.NoCrypto)
134 def test_reading_rar5_hpsw():
135 run_reading('test/files/rar5-hpsw.rar', u'RAR5 archive - hdr-password\n')
137 def test_reading_rar5_psw_blake():
138 run_reading('test/files/rar5-psw-blake.rar', u'RAR5 archive - nohdr-password-blake\n')
140 def test_reading_rar5_psw():
141 run_reading('test/files/rar5-psw.rar', u'RAR5 archive - nohdr-password\n')
143 def test_reading_missed():
144 problems = []
145 missed = []
146 for fn in glob('test/files/*.rar'):
147 fn = fn.replace('\\', '/')
148 if fn not in _done_reading:
149 missed.append(fn)
150 eq_(missed, problems)