test_BukuCrypt: check different payload sizes...

and roundtrip integrity.

Should catch bugs like #480.
This commit is contained in:
Zhiming Wang 2020-11-24 18:27:03 +08:00
parent 932b5e9eda
commit af147f3b3b
No known key found for this signature in database
GPG Key ID: 5B58F95EC95965D8

View File

@ -1,6 +1,7 @@
"""test module."""
from unittest import mock
import os
import random
import pytest
@ -16,20 +17,23 @@ def test_get_filehash(tmpdir):
assert res == exp_res
def touch(fname):
"""touch implementation for python."""
if os.path.exists(fname):
os.utime(fname, None)
else:
open(fname, 'a').close()
def test_encrypt_decrypt(tmpdir):
@pytest.mark.parametrize(
'filesize',
list(range(0, 17)) + [511, 512, 513, 1023, 1024, 1025, 524288, 524289, 1000000, 1048576, 2097152, 4194304]
)
def test_encrypt_decrypt(tmpdir, filesize):
"""test method."""
dbfile = os.path.join(tmpdir.strpath, 'test_encrypt_decrypt_dbfile')
touch(dbfile)
content = bytes(random.getrandbits(8) for _ in range(filesize))
with open(dbfile, 'wb') as fp:
fp.write(content)
assert os.stat(dbfile).st_size == filesize
with mock.patch('getpass.getpass', return_value='password'):
from buku import BukuCrypt
with pytest.raises(SystemExit):
BukuCrypt.encrypt_file(1, dbfile=dbfile)
BukuCrypt.decrypt_file(1, dbfile=dbfile)
assert os.path.exists(dbfile)
with open(dbfile, 'rb') as fp:
roundtrip_content = fp.read()
assert roundtrip_content == content