mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-10 00:09:04 +00:00
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import os
|
|
import base64
|
|
import pytest
|
|
|
|
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
|
|
|
from seedpass.core.encryption import EncryptionManager
|
|
|
|
|
|
def test_json_save_and_load_round_trip():
|
|
with TemporaryDirectory() as tmpdir:
|
|
key = base64.urlsafe_b64encode(os.urandom(32))
|
|
manager = EncryptionManager(key, Path(tmpdir))
|
|
|
|
data = {"hello": "world", "nums": [1, 2, 3]}
|
|
manager.save_json_data(data)
|
|
loaded = manager.load_json_data()
|
|
assert loaded == data
|
|
|
|
file_path = Path(tmpdir) / "seedpass_entries_db.json.enc"
|
|
raw = file_path.read_bytes()
|
|
assert raw != json.dumps(data, indent=4).encode("utf-8")
|
|
|
|
|
|
def test_encrypt_and_decrypt_file_binary_round_trip():
|
|
with TemporaryDirectory() as tmpdir:
|
|
key = base64.urlsafe_b64encode(os.urandom(32))
|
|
manager = EncryptionManager(key, Path(tmpdir))
|
|
|
|
payload = b"binary secret"
|
|
rel = Path("payload.bin.enc")
|
|
manager.encrypt_and_save_file(payload, rel)
|
|
decrypted = manager.decrypt_file(rel)
|
|
assert decrypted == payload
|
|
|
|
file_path = Path(tmpdir) / rel
|
|
raw = file_path.read_bytes()
|
|
assert raw != payload
|
|
|
|
|
|
def test_encrypt_file_rejects_traversal():
|
|
with TemporaryDirectory() as tmpdir:
|
|
key = base64.urlsafe_b64encode(os.urandom(32))
|
|
manager = EncryptionManager(key, Path(tmpdir))
|
|
|
|
with pytest.raises(ValueError):
|
|
manager.encrypt_and_save_file(b"data", Path("../evil.enc"))
|
|
|
|
with pytest.raises(ValueError):
|
|
manager.encrypt_and_save_file(b"data", Path("/abs.enc"))
|