Commit 90a88964 authored by Vincent Texier's avatar Vincent Texier

[enh] add unittests for the key modules

parent 45a105cf
......@@ -4,8 +4,8 @@ import unittest
class TestEncryptionKey(unittest.TestCase):
def test_from_bob_to_alice(self):
bob_secret_key = SecretKey("bobsalt", "bobpassword")
alice_secret_key = SecretKey("alicesalt", "alicepassword")
bob_secret_key = SecretKey("bob", "password")
alice_secret_key = SecretKey("alice", "password")
text = "Relatively ciphered text"
noonce = "00005D6FC6E22FB308D8815A565A01C66FFB7DC761D616DE0698F6322565F1D6"[:24]
bob_to_alice = bob_secret_key.encrypt(alice_secret_key.public_key.base58(), noonce, text)
......
from duniterpy.key import SigningKey, PublicKey
from duniterpy.key.scrypt_params import ScryptParams
import unittest
class TestPublicKey(unittest.TestCase):
def test_encrypt_seal(self):
sign_key = SigningKey.from_credentials("alice", "password", ScryptParams())
public_key = PublicKey(sign_key.pubkey)
message = "Hello world with utf-8 chars like éàè !"
encrypted_message = public_key.encrypt_seal(bytes(message, 'utf-8'))
decrypted_message = sign_key.decrypt_seal(encrypted_message)
self.assertEqual(message, decrypted_message.decode('utf-8'))
import os
from duniterpy.key import VerifyingKey, SigningKey, PublicKey
from duniterpy.key.scrypt_params import ScryptParams
import unittest
TEST_FILE_PATH = '/tmp/test_file.txt'
class TestSigningKey(unittest.TestCase):
def tearDown(self) -> None:
super(TestSigningKey, self)
# remove test file from disk
if os.path.exists(TEST_FILE_PATH):
os.unlink(TEST_FILE_PATH)
def test_decrypt_seal(self):
sign_key = SigningKey.from_credentials("alice", "password", ScryptParams())
public_key = PublicKey(sign_key.pubkey)
message = "Hello world with utf-8 chars like éàè !"
encrypted_message = public_key.encrypt_seal(bytes(message, 'utf-8'))
decrypted_message = sign_key.decrypt_seal(encrypted_message)
self.assertEqual(message, decrypted_message.decode('utf-8'))
def test_from_credentials(self):
sign_key = SigningKey.from_credentials("alice", "password", ScryptParams())
verify_key = VerifyingKey(sign_key.pubkey)
self.assertEqual(verify_key.vk, sign_key.vk)
def test_save_and_load_from_seedhex_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_seedhex_file(TEST_FILE_PATH)
sign_key_load = SigningKey.from_seedhex_file(TEST_FILE_PATH)
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_save_and_load_from_pubsec_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_pubsec_file(TEST_FILE_PATH)
sign_key_load = SigningKey.from_pubsec_file(TEST_FILE_PATH)
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_save_and_load_from_wif_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_wif_file(TEST_FILE_PATH)
sign_key_load = SigningKey.from_wif_file(TEST_FILE_PATH)
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_save_and_load_from_private_key_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_private_key(TEST_FILE_PATH)
sign_key_load = SigningKey.from_private_key(TEST_FILE_PATH)
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_save_and_load_from_ewif_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_ewif_file(TEST_FILE_PATH, "password")
sign_key_load = SigningKey.from_ewif_file(TEST_FILE_PATH, "password")
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_save_ewif_and_load_from_ewif_or_wif_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_ewif_file(TEST_FILE_PATH, "password")
sign_key_load = SigningKey.from_wif_or_ewif_file(TEST_FILE_PATH, "password")
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_save_wif_and_load_from_ewif_or_wif_file(self):
sign_key_save = SigningKey.from_credentials("alice", "password", ScryptParams())
sign_key_save.save_wif_file(TEST_FILE_PATH)
sign_key_load = SigningKey.from_wif_or_ewif_file(TEST_FILE_PATH)
self.assertEqual(sign_key_save.sk, sign_key_load.sk)
\ No newline at end of file
......@@ -7,10 +7,23 @@ import unittest
class TestVerifyingKey(unittest.TestCase):
def test_from_sign_to_verify(self):
sign_key = SigningKey.from_credentials("saltsalt", "passwordpassword", ScryptParams())
sign_key = SigningKey.from_credentials("alice", "password", ScryptParams())
verify_key = VerifyingKey(sign_key.pubkey)
self.assertEqual(verify_key.vk, sign_key.vk)
def test_get_verified_data(self):
sign_key = SigningKey.from_credentials("alice", "password", ScryptParams())
message = "Hello world with utf-8 chars like éàè !"
# Sign the message, the signed string is the message itself plus the
# signature
signed_message = sign_key.sign(bytes(message, 'utf-8')) # type: bytes
# Verify the message!
verifier = VerifyingKey(sign_key.pubkey)
verified_message = verifier.get_verified_data(signed_message)
self.assertEqual(message, verified_message.decode('utf-8'))
def test_peer_signature(self):
signed_raw = """Version: 2
Type: Peer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment