55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""Tests for encryption service."""
|
|
|
|
import tempfile
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from snip.crypto.service import CryptoService
|
|
|
|
|
|
@pytest.fixture
|
|
def crypto_service():
|
|
with tempfile.NamedTemporaryFile(suffix=".key", delete=False) as f:
|
|
key_file = f.name
|
|
service = CryptoService(key_file)
|
|
yield service
|
|
if os.path.exists(key_file):
|
|
os.unlink(key_file)
|
|
salt_file = f"{key_file}.salt"
|
|
if os.path.exists(salt_file):
|
|
os.unlink(salt_file)
|
|
|
|
|
|
def test_encrypt_decrypt(crypto_service):
|
|
"""Test encryption and decryption round-trip."""
|
|
plaintext = "Hello, World!"
|
|
password = "test_password_123"
|
|
|
|
encrypted = crypto_service.encrypt(plaintext, password)
|
|
assert encrypted != plaintext
|
|
|
|
decrypted = crypto_service.decrypt(encrypted, password)
|
|
assert decrypted == plaintext
|
|
|
|
|
|
def test_wrong_password_fails(crypto_service):
|
|
"""Test that wrong password fails to decrypt."""
|
|
plaintext = "Secret message"
|
|
password = "correct_password"
|
|
|
|
encrypted = crypto_service.encrypt(plaintext, password)
|
|
|
|
with pytest.raises(Exception):
|
|
crypto_service.decrypt(encrypted, "wrong_password")
|
|
|
|
|
|
def test_different_passwords_different_output(crypto_service):
|
|
"""Test that different passwords produce different ciphertext."""
|
|
plaintext = "Same text"
|
|
|
|
encrypted1 = crypto_service.encrypt(plaintext, "password1")
|
|
encrypted2 = crypto_service.encrypt(plaintext, "password2")
|
|
|
|
assert encrypted1 != encrypted2
|