tests: use AES-GCM key generation

This commit is contained in:
thePR0M3TH3AN
2025-07-12 20:32:04 -04:00
parent f4a169ca80
commit f7c2017f1c
9 changed files with 31 additions and 20 deletions

View File

@@ -3,7 +3,8 @@ import sys
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from cryptography.fernet import Fernet import os
import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -14,7 +15,7 @@ from utils.checksum import verify_and_update_checksum
def test_encryption_checksum_workflow(): def test_encryption_checksum_workflow():
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir) tmp_path = Path(tmpdir)
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
manager = EncryptionManager(key, tmp_path) manager = EncryptionManager(key, tmp_path)
data = {"value": 1} data = {"value": 1}

View File

@@ -3,7 +3,8 @@ import sys
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from cryptography.fernet import Fernet import os
import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -12,7 +13,7 @@ from password_manager.encryption import EncryptionManager
def test_json_save_and_load_round_trip(): def test_json_save_and_load_round_trip():
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
manager = EncryptionManager(key, Path(tmpdir)) manager = EncryptionManager(key, Path(tmpdir))
data = {"hello": "world", "nums": [1, 2, 3]} data = {"hello": "world", "nums": [1, 2, 3]}
@@ -27,7 +28,7 @@ def test_json_save_and_load_round_trip():
def test_encrypt_and_decrypt_file_binary_round_trip(): def test_encrypt_and_decrypt_file_binary_round_trip():
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
manager = EncryptionManager(key, Path(tmpdir)) manager = EncryptionManager(key, Path(tmpdir))
payload = b"binary secret" payload = b"binary secret"

View File

@@ -3,7 +3,8 @@ import sys
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from cryptography.fernet import Fernet import os
import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -24,7 +25,7 @@ def test_generate_fingerprint_deterministic():
def test_encryption_round_trip(): def test_encryption_round_trip():
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
manager = EncryptionManager(key, Path(tmpdir)) manager = EncryptionManager(key, Path(tmpdir))
data = b"secret data" data = b"secret data"
rel_path = Path("testfile.enc") rel_path = Path("testfile.enc")

View File

@@ -4,7 +4,8 @@ from tempfile import TemporaryDirectory
from unittest.mock import patch from unittest.mock import patch
import json import json
import asyncio import asyncio
from cryptography.fernet import Fernet import os
import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -15,7 +16,7 @@ import nostr.client as nostr_client
def test_nostr_client_uses_custom_relays(): def test_nostr_client_uses_custom_relays():
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
enc_mgr = EncryptionManager(key, Path(tmpdir)) enc_mgr = EncryptionManager(key, Path(tmpdir))
custom_relays = ["wss://relay1", "wss://relay2"] custom_relays = ["wss://relay1", "wss://relay2"]
@@ -73,7 +74,7 @@ class FakeWebSocket:
def _setup_client(tmpdir, fake_cls): def _setup_client(tmpdir, fake_cls):
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
enc_mgr = EncryptionManager(key, Path(tmpdir)) enc_mgr = EncryptionManager(key, Path(tmpdir))
with patch("nostr.client.Client", fake_cls), patch( with patch("nostr.client.Client", fake_cls), patch(

View File

@@ -3,7 +3,8 @@ from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
import asyncio import asyncio
import gzip import gzip
from cryptography.fernet import Fernet import os
import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -61,7 +62,7 @@ class MockClient:
def setup_client(tmp_path, server): def setup_client(tmp_path, server):
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
enc_mgr = EncryptionManager(key, tmp_path) enc_mgr = EncryptionManager(key, tmp_path)
with patch("nostr.client.Client", lambda signer: MockClient(server)), patch( with patch("nostr.client.Client", lambda signer: MockClient(server)), patch(

View File

@@ -10,7 +10,8 @@ import uuid
import pytest import pytest
from cryptography.fernet import Fernet import base64
import os
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -32,7 +33,7 @@ def test_nostr_index_size_limits(pytestconfig: pytest.Config):
) )
results = [] results = []
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
enc_mgr = EncryptionManager(key, Path(tmpdir)) enc_mgr = EncryptionManager(key, Path(tmpdir))
with patch.object(enc_mgr, "decrypt_parent_seed", return_value=seed): with patch.object(enc_mgr, "decrypt_parent_seed", return_value=seed):
client = NostrClient( client = NostrClient(

View File

@@ -9,7 +9,7 @@ import gzip
import uuid import uuid
import pytest import pytest
from cryptography.fernet import Fernet import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -25,7 +25,9 @@ def test_nostr_publish_and_retrieve():
"abandon abandon abandon abandon about" "abandon abandon abandon abandon about"
) )
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
enc_mgr = EncryptionManager(Fernet.generate_key(), Path(tmpdir)) enc_mgr = EncryptionManager(
base64.urlsafe_b64encode(os.urandom(32)), Path(tmpdir)
)
with patch.object(enc_mgr, "decrypt_parent_seed", return_value=seed): with patch.object(enc_mgr, "decrypt_parent_seed", return_value=seed):
client = NostrClient( client = NostrClient(
enc_mgr, enc_mgr,

View File

@@ -3,8 +3,8 @@ import json
import gzip import gzip
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from cryptography.fernet import Fernet
import base64 import base64
import os
import asyncio import asyncio
from unittest.mock import patch from unittest.mock import patch
@@ -82,7 +82,9 @@ def test_fetch_latest_snapshot():
client = DummyClient(events) client = DummyClient(events)
with TemporaryDirectory() as tmpdir: with TemporaryDirectory() as tmpdir:
enc_mgr = EncryptionManager(Fernet.generate_key(), Path(tmpdir)) enc_mgr = EncryptionManager(
base64.urlsafe_b64encode(os.urandom(32)), Path(tmpdir)
)
with patch("nostr.client.Client", lambda signer: client), patch( with patch("nostr.client.Client", lambda signer: client), patch(
"nostr.client.KeyManager" "nostr.client.KeyManager"
) as MockKM, patch.object(NostrClient, "initialize_client_pool"), patch.object( ) as MockKM, patch.object(NostrClient, "initialize_client_pool"), patch.object(

View File

@@ -4,7 +4,8 @@ from tempfile import TemporaryDirectory
from unittest.mock import patch from unittest.mock import patch
import asyncio import asyncio
import pytest import pytest
from cryptography.fernet import Fernet import os
import base64
sys.path.append(str(Path(__file__).resolve().parents[1])) sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -13,7 +14,7 @@ from nostr.client import NostrClient, Manifest
def setup_client(tmp_path): def setup_client(tmp_path):
key = Fernet.generate_key() key = base64.urlsafe_b64encode(os.urandom(32))
enc_mgr = EncryptionManager(key, tmp_path) enc_mgr = EncryptionManager(key, tmp_path)
with patch("nostr.client.ClientBuilder"), patch( with patch("nostr.client.ClientBuilder"), patch(