Files
sshecret/tests/test_crypto.py
2025-03-17 21:42:56 +01:00

54 lines
1.4 KiB
Python

"""Tests of the encryption methods."""
import os
from pathlib import Path
from typing import override
import unittest
from sshecret.crypto import (
load_public_key,
load_private_key,
encrypt_string,
decode_string,
)
def read_public_key() -> bytes:
"""Load public key."""
keyname = "testkey"
public_key_file = Path(os.path.dirname(__file__)) / f"{keyname}.pub"
with open(public_key_file, "rb") as f:
public_key = f.read()
return public_key
class TestBasicCrypto(unittest.TestCase):
"""Test basic crypto functionality."""
@override
def setUp(self) -> None:
"""Set up keys."""
keyname = "testkey"
self.private_key: str = os.path.join(os.path.dirname(__file__), keyname)
self.public_key: bytes = read_public_key()
def test_key_loading(self) -> None:
"""Test basic flow."""
public_key = load_public_key(self.public_key)
private_key = load_private_key(self.private_key)
self.assertEqual(True, True)
def test_encrypt_decrypt(self) -> None:
"""Test encryption and decryption."""
password = "MySecretPassword"
public_key = load_public_key(self.public_key)
encoded = encrypt_string(password, public_key)
private_key = load_private_key(self.private_key)
decoded = decode_string(encoded, private_key)
self.assertEqual(password, decoded)
if __name__ == "__main__":
unittest.main()