Skip to content

Commit

Permalink
test: pyfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
markokr committed Jul 7, 2020
1 parent 3080218 commit 4fb1bc3
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 34 deletions.
32 changes: 28 additions & 4 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,81 @@
# test start
#


def test_bad_arc_mode_w():
with pytest.raises(NotImplementedError):
rarfile.RarFile('test/files/rar3-comment-plain.rar', 'w')


def test_bad_arc_mode_rb():
with pytest.raises(NotImplementedError):
rarfile.RarFile('test/files/rar3-comment-plain.rar', 'rb')


def test_bad_errs():
with pytest.raises(ValueError):
rarfile.RarFile('test/files/rar3-comment-plain.rar', 'r', errors='foo')


def test_bad_open_mode_w():
rf = rarfile.RarFile('test/files/rar3-comment-plain.rar')
with pytest.raises(NotImplementedError):
rf.open('qwe', 'w')


def test_bad_open_psw():
rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
with pytest.raises(rarfile.PasswordRequired):
rf.open('file1.txt')


def test_bad_filelike():
with pytest.raises(ValueError):
rarfile.is_rarfile(bytearray(10))


def test_open_psw_late_rar3():
rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
d1 = rf.open('file1.txt', 'r', 'password').read()
d2 = rf.open('file1.txt', 'r', b'password').read()
assert d1 == d2


def test_open_psw_late_rar5():
rf = rarfile.RarFile('test/files/rar5-psw.rar')
rf.open('stest1.txt', 'r', 'password').read()
rf.open('stest1.txt', 'r', b'password').read()


def test_open_pathlib_path():
rf = rarfile.RarFile('test/files/rar5-psw.rar')
rf.open(Path('stest1.txt'), 'r', 'password').read()


def test_read_psw_late_rar3():
rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
rf.read('file1.txt', 'password')
rf.read('file1.txt', b'password')


def test_read_psw_late_rar5():
rf = rarfile.RarFile('test/files/rar5-psw.rar')
rf.read('stest1.txt', 'password')
rf.read('stest1.txt', b'password')


def test_open_psw_late():
rf = rarfile.RarFile('test/files/rar5-psw.rar')
with pytest.raises(rarfile.BadRarFile):
rf.read('stest1.txt', 'password222')


def test_create_from_pathlib_path():
# Make sure we can open both relative and absolute Paths
rarfile.RarFile(Path('test/files/rar5-psw.rar'))
rarfile.RarFile(Path('test/files/rar5-psw.rar').resolve())


def test_detection():
assert rarfile.is_rarfile('test/files/ctime4.rar.exp') is False
assert rarfile.is_rarfile('test/files/ctime4.rar') is True
Expand All @@ -85,29 +99,32 @@ def test_signature_error():
with pytest.raises(rarfile.BadRarFile):
rarfile.RarFile('test/files/ctime4.rar.exp')


def test_signature_error_mem():
data = io.BytesIO(b'x'*40)
data = io.BytesIO(b'x' * 40)
with pytest.raises(rarfile.BadRarFile):
rarfile.RarFile(data)


def test_with():
with rarfile.RarFile('test/files/rar5-crc.rar') as rf:
data = rf.read('stest1.txt')
with rf.open('stest1.txt') as f:
dst = io.BytesIO()
while 1:
while True:
buf = f.read(7)
if not buf:
break
dst.write(buf)
assert dst.getvalue() == data


def test_readline():
def load_readline(rf, fn):
with rf.open(fn) as f:
tr = io.TextIOWrapper(io.BufferedReader(f))
res = []
while 1:
while True:
ln = tr.readline()
if not ln:
break
Expand All @@ -120,22 +137,26 @@ def load_readline(rf, fn):
assert len(v1) == 512
assert v1 == v2


def test_printdir(capsys):
rf = rarfile.RarFile('test/files/seektest.rar')
rf.printdir()
res = capsys.readouterr()
assert res.out == 'stest1.txt\nstest2.txt\n'


def test_testrar():
rf = rarfile.RarFile('test/files/seektest.rar')
rf.testrar()


def test_testrar_mem():
with open('test/files/seektest.rar', 'rb') as f:
arc = f.read()
rf = rarfile.RarFile(io.BytesIO(arc))
rf.testrar()


def test_extract(tmp_path):
ex1 = tmp_path / "extract1"
ex2 = tmp_path / "extract2"
Expand Down Expand Up @@ -170,6 +191,7 @@ def test_extract(tmp_path):
assert os.path.isfile(str(ex4 / 'stest1.txt')) is True
assert os.path.isfile(str(ex4 / 'stest2.txt')) is True


def test_extract_mem(tmp_path):
ex1 = tmp_path / "extract11"
ex2 = tmp_path / "extract22"
Expand All @@ -195,10 +217,12 @@ def test_extract_mem(tmp_path):
assert os.path.isfile(str(ex3 / 'stest1.txt')) is False
assert os.path.isfile(str(ex3 / 'stest2.txt')) is True


def test_infocb():
infos = []

def info_cb(info):
infos.append( (info.type, info.needs_password(), info.isdir(), info._must_disable_hack()) )
infos.append((info.type, info.needs_password(), info.isdir(), info._must_disable_hack()))

rf = rarfile.RarFile('test/files/seektest.rar', info_callback=info_cb)
assert infos == [
Expand Down
3 changes: 2 additions & 1 deletion test/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from binascii import unhexlify

import pytest

import rarfile

try:
Expand All @@ -19,6 +18,7 @@ def aes_encrypt(key, iv, data):
except ImportError:
pass


@pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
def test_aes128_cbc():
data = b'0123456789abcdef' * 2
Expand All @@ -31,6 +31,7 @@ def test_aes128_cbc():
ctx = rarfile.AES_CBC_Decrypt(key, iv)
assert ctx.decrypt(encdata) == data


@pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
def test_aes256_cbc():
data = b'0123456789abcdef' * 2
Expand Down
48 changes: 35 additions & 13 deletions test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"""

from datetime import datetime

import pytest
import rarfile


def render_date(dt):
if isinstance(dt, datetime):
return dt.isoformat('T')
Expand All @@ -13,13 +15,15 @@ def render_date(dt):
else:
return dt


def mkitem(**kwargs):
res = {}
for k in kwargs:
if kwargs[k] is not None:
res[k] = kwargs[k]
return res


def dumparc(rf):
res = []
for item in rf.infolist():
Expand All @@ -40,6 +44,7 @@ def dumparc(rf):
res.append(info)
return res


def diffs(a, b):
if len(a) != len(b):
return 'Different lengths'
Expand All @@ -57,13 +62,15 @@ def diffs(a, b):
problems.append('ErrValue(%d,%s):got=%r/exp=%r' % (i, k, xa[k], xb[k]))
return '; '.join(problems)


def cmp_struct(a, b):
assert a == b, diffs(a, b)

#
# test start
#


@pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
def test_rar3_header_encryption():
r = rarfile.RarFile('test/files/rar3-comment-hpsw.rar', 'r')
Expand All @@ -74,9 +81,10 @@ def test_rar3_header_encryption():
r.setpassword('password')
assert r.needs_password() is True
assert r.namelist() == ['file1.txt', 'file2.txt']
assert r.comment is not None
assert r.comment is not None
assert r.comment == 'RARcomment\n'


@pytest.mark.skipif(not rarfile._have_crypto, reason="No crypto")
def test_rar5_header_encryption():
r = rarfile.RarFile('test/files/rar5-hpsw.rar')
Expand All @@ -91,6 +99,7 @@ def test_rar5_header_encryption():
assert r.comment == 'RAR5 archive - hdr-password\n'
r.close()


def get_vol_info(extver=20, tz='', hr='11'):
return [
mkitem(CRC=1352324940,
Expand All @@ -114,6 +123,7 @@ def get_vol_info(extver=20, tz='', hr='11'):
host_os=3,
fn='vols/smallfile.txt')]


def test_rar3_vols():
r = rarfile.RarFile('test/files/rar3-vols.part1.rar')
assert r.needs_password() is False
Expand All @@ -127,6 +137,7 @@ def test_rar3_vols():
with pytest.raises(rarfile.NeedFirstVolume):
rarfile.RarFile('test/files/rar3-vols.part2.rar')


def test_rar3_oldvols():
r = rarfile.RarFile('test/files/rar3-old.rar')
assert r.needs_password() is False
Expand All @@ -140,6 +151,7 @@ def test_rar3_oldvols():
with pytest.raises(rarfile.NeedFirstVolume):
rarfile.RarFile('test/files/rar3-old.r00')


def test_rar5_vols():
r = rarfile.RarFile('test/files/rar5-vols.part1.rar')
assert r.needs_password() is False
Expand All @@ -153,6 +165,7 @@ def test_rar5_vols():
with pytest.raises(rarfile.NeedFirstVolume):
rarfile.RarFile('test/files/rar5-vols.part2.rar')


def expect_ctime(mtime, ctime):
return [mkitem(
mtime=mtime,
Expand All @@ -167,53 +180,62 @@ def expect_ctime(mtime, ctime):
mode=32,
host_os=2)]


def test_rar3_ctime0():
r = rarfile.RarFile('test/files/ctime0.rar')
cmp_struct(dumparc(r), expect_ctime('2011-05-10T21:28:47.899345', None))


def test_rar3_ctime1():
r = rarfile.RarFile('test/files/ctime1.rar')
cmp_struct(dumparc(r), expect_ctime('2011-05-10T21:28:47.899345', '2011-05-10T21:28:47'))


def test_rar3_ctime2():
r = rarfile.RarFile('test/files/ctime2.rar')
cmp_struct(dumparc(r), expect_ctime('2011-05-10T21:28:47.899345', '2011-05-10T21:28:47.897843'))


def test_rar3_ctime3():
r = rarfile.RarFile('test/files/ctime3.rar')
cmp_struct(dumparc(r), expect_ctime('2011-05-10T21:28:47.899345', '2011-05-10T21:28:47.899328'))


def test_rar3_ctime4():
r = rarfile.RarFile('test/files/ctime4.rar')
cmp_struct(dumparc(r), expect_ctime('2011-05-10T21:28:47.899345', '2011-05-10T21:28:47.899345'))


def test_rar5_times():
r = rarfile.RarFile('test/files/rar5-times.rar')
cmp_struct(dumparc(r), [mkitem(
fn='stest1.txt',
file_size=2048,
compress_size=55,
compress_type=rarfile.RAR_M3,
extract_version=50,
host_os=rarfile.RAR_OS_UNIX,
mode=33188,
date_time='2011-06-12 09:53:33',
mtime='2011-06-12T09:53:33+00:00',
atime='2016-05-22T09:12:36+00:00',
CRC=3317163682
)])
fn='stest1.txt',
file_size=2048,
compress_size=55,
compress_type=rarfile.RAR_M3,
extract_version=50,
host_os=rarfile.RAR_OS_UNIX,
mode=33188,
date_time='2011-06-12 09:53:33',
mtime='2011-06-12T09:53:33+00:00',
atime='2016-05-22T09:12:36+00:00',
CRC=3317163682
)])


def test_oldvols():
assert rarfile._next_oldvol('qq00.part0.rar') == 'qq00.part0.r00'
assert rarfile._next_oldvol('qq00.part0.r00') == 'qq00.part0.r01'
assert rarfile._next_oldvol('qq00.part0.r29') == 'qq00.part0.r30'
assert rarfile._next_oldvol('qq00.part0.r99') == 'qq00.part0.s00'


def test_newvols():
assert rarfile._next_newvol('qq00.part0.rar') == 'qq00.part1.rar'
assert rarfile._next_newvol('qq00.part09.rar') == 'qq00.part10.rar'
assert rarfile._next_newvol('qq00.part99.rar') == 'qq00.paru00.rar'


def test_newvols_err():
with pytest.raises(rarfile.BadRarName):
rarfile._next_newvol('xx.rar')
Expand Down
Loading

0 comments on commit 4fb1bc3

Please sign in to comment.