1
0
Fork 0
mirror of synced 2024-11-18 15:15:35 -05:00
yadm/test/test_unit_encryption.py

136 lines
4 KiB
Python
Raw Permalink Normal View History

2020-10-05 23:34:17 -04:00
"""Unit tests: encryption functions"""
import pytest
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("condition", ["default", "override"])
2020-10-05 23:34:17 -04:00
def test_get_cipher(runner, paths, condition):
"""Test _get_cipher()"""
2023-07-10 15:43:17 -04:00
if condition == "override":
paths.config.write("[yadm]\n\tcipher = override-cipher")
2020-10-05 23:34:17 -04:00
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}"
set_yadm_dirs
2020-10-05 23:34:17 -04:00
configure_paths
_get_cipher test-archive
echo "output_archive:$output_archive"
echo "yadm_cipher:$yadm_cipher"
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
2020-10-05 23:34:17 -04:00
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
assert "output_archive:test-archive" in run.out
if condition == "override":
assert "yadm_cipher:override-cipher" in run.out
2020-10-05 23:34:17 -04:00
else:
2023-07-10 15:43:17 -04:00
assert "yadm_cipher:gpg" in run.out
2020-10-05 23:34:17 -04:00
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("cipher", ["gpg", "openssl", "bad"])
@pytest.mark.parametrize("mode", ["_encrypt_to", "_decrypt_from"])
2020-10-05 23:34:17 -04:00
def test_encrypt_decrypt(runner, paths, cipher, mode):
"""Test _encrypt_to() & _decrypt_from"""
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}"
set_yadm_dirs
2020-10-05 23:34:17 -04:00
configure_paths
function mock_openssl() {{ echo openssl $*; }}
function mock_gpg() {{ echo gpg $*; }}
function _get_cipher() {{
output_archive="$1"
yadm_cipher="{cipher}"
}}
OPENSSL_PROGRAM=mock_openssl
GPG_PROGRAM=mock_gpg
{mode} {paths.archive}
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
2020-10-05 23:34:17 -04:00
2023-07-10 15:43:17 -04:00
if cipher != "bad":
2020-10-05 23:34:17 -04:00
assert run.success
assert run.out.startswith(cipher)
assert str(paths.archive) in run.out
2023-07-10 15:43:17 -04:00
assert run.err == ""
2020-10-05 23:34:17 -04:00
else:
assert run.failure
2023-07-10 15:43:17 -04:00
assert "Unknown cipher" in run.err
2020-10-05 23:34:17 -04:00
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("condition", ["default", "override"])
2020-10-05 23:34:17 -04:00
def test_get_openssl_ciphername(runner, paths, condition):
"""Test _get_openssl_ciphername()"""
2023-07-10 15:43:17 -04:00
if condition == "override":
paths.config.write("[yadm]\n\topenssl-ciphername = override-cipher")
2020-10-05 23:34:17 -04:00
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}"
set_yadm_dirs
2020-10-05 23:34:17 -04:00
configure_paths
result=$(_get_openssl_ciphername)
echo "result:$result"
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
2020-10-05 23:34:17 -04:00
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
if condition == "override":
assert run.out.strip() == "result:override-cipher"
2020-10-05 23:34:17 -04:00
else:
2023-07-10 15:43:17 -04:00
assert run.out.strip() == "result:aes-256-cbc"
2020-10-05 23:34:17 -04:00
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("condition", ["old", "not-old"])
2020-10-05 23:34:17 -04:00
def test_set_openssl_options(runner, paths, condition):
"""Test _set_openssl_options()"""
2023-07-10 15:43:17 -04:00
if condition == "old":
paths.config.write("[yadm]\n\topenssl-old = true")
2020-10-05 23:34:17 -04:00
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}"
set_yadm_dirs
2020-10-05 23:34:17 -04:00
configure_paths
function _get_openssl_ciphername() {{ echo "testcipher"; }}
_set_openssl_options
echo "result:${{OPENSSL_OPTS[@]}}"
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
2020-10-05 23:34:17 -04:00
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
if condition == "old":
assert "-testcipher -salt -md md5" in run.out
2020-10-05 23:34:17 -04:00
else:
2023-07-10 15:43:17 -04:00
assert "-testcipher -salt -pbkdf2 -iter 100000 -md sha512" in run.out
2020-10-05 23:34:17 -04:00
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("recipient", ["ASK", "present", ""])
2020-10-05 23:34:17 -04:00
def test_set_gpg_options(runner, paths, recipient):
"""Test _set_gpg_options()"""
2023-07-10 15:43:17 -04:00
paths.config.write(f"[yadm]\n\tgpg-recipient = {recipient}")
2020-10-05 23:34:17 -04:00
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}"
set_yadm_dirs
2020-10-05 23:34:17 -04:00
configure_paths
_set_gpg_options
echo "result:${{GPG_OPTS[@]}}"
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
2020-10-05 23:34:17 -04:00
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
if recipient == "ASK":
assert run.out.strip() == "result:--no-default-recipient -e"
elif recipient != "":
assert run.out.strip() == f"result:-e -r {recipient}"
2020-10-05 23:34:17 -04:00
else:
2023-07-10 15:43:17 -04:00
assert run.out.strip() == "result:-c"