From d18944e2bac8603b8dfdd852964bb59477b33811 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 17 Mar 2020 20:22:03 +0100 Subject: [PATCH] Add more contrains to load_keys and updates unit tests --- apps/cli/teos_cli.py | 31 +++++++++++---- test/apps/cli/unit/test_teos_cli.py | 60 ++++++++++++++++------------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/apps/cli/teos_cli.py b/apps/cli/teos_cli.py index 34ff23d..d1d15f0 100644 --- a/apps/cli/teos_cli.py +++ b/apps/cli/teos_cli.py @@ -39,24 +39,39 @@ def load_keys(teos_pk_path, cli_sk_path, cli_pk_path): encoded key if all keys can be loaded. ``None`` otherwise. """ - teos_pk_der = Cryptographer.load_key_file(teos_pk_path) - teos_pk = PublicKey(teos_pk_der) - - if teos_pk is None: + if teos_pk_path is None: logger.error("TEOS's public key file not found. Please check your settings") return None + if cli_sk_path is None: + logger.error("Client's private key file not found. Please check your settings") + return None + + if cli_pk_path is None: + logger.error("Client's public key file not found. Please check your settings") + return None + + try: + teos_pk_der = Cryptographer.load_key_file(teos_pk_path) + teos_pk = PublicKey(teos_pk_der) + + except ValueError: + logger.error("TEOS public key is invalid or cannot be parsed") + return None + cli_sk_der = Cryptographer.load_key_file(cli_sk_path) cli_sk = Cryptographer.load_private_key_der(cli_sk_der) if cli_sk is None: - logger.error("Client's private key file not found. Please check your settings") + logger.error("Client private key is invalid or cannot be parsed") return None - cli_pk_der = Cryptographer.load_key_file(cli_pk_path) + try: + cli_pk_der = Cryptographer.load_key_file(cli_pk_path) + PublicKey(cli_pk_der) - if cli_pk_der is None: - logger.error("Client's public key file not found. Please check your settings") + except ValueError: + logger.error("Client public key is invalid or cannot be parsed") return None return teos_pk, cli_sk, cli_pk_der diff --git a/test/apps/cli/unit/test_teos_cli.py b/test/apps/cli/unit/test_teos_cli.py index 853afd9..00c9c9d 100644 --- a/test/apps/cli/unit/test_teos_cli.py +++ b/test/apps/cli/unit/test_teos_cli.py @@ -52,8 +52,7 @@ dummy_appointment = Appointment.from_dict(dummy_appointment_full) def load_dummy_keys(*args): - # return dummy_pk, dummy_sk, dummy_pk_der - return dummy_pk + return dummy_pk, dummy_sk, dummy_pk.format(compressed=True) def get_dummy_signature(*args): @@ -64,30 +63,39 @@ def get_bad_signature(*args): return Cryptographer.sign(dummy_appointment.serialize(), another_sk) -# def test_load_keys(): -# # Let's first create a private key and public key files -# private_key_file_path = "sk_test_file" -# public_key_file_path = "pk_test_file" -# with open(private_key_file_path, "wb") as f: -# f.write(dummy_sk.to_der()) -# with open(public_key_file_path, "wb") as f: -# f.write(dummy_pk_der) -# -# # Now we can test the function passing the using this files (we'll use the same pk for both) -# r = teos_cli.load_keys(public_key_file_path, private_key_file_path, public_key_file_path) -# assert isinstance(r, tuple) -# assert len(r) == 3 -# -# # If any param does not match we should get None as result -# assert teos_cli.load_keys(None, private_key_file_path, public_key_file_path) is None -# assert teos_cli.load_keys(public_key_file_path, None, public_key_file_path) is None -# assert teos_cli.load_keys(public_key_file_path, private_key_file_path, None) is None -# -# # The same should happen if we pass a public key where a private should be, for instance -# assert teos_cli.load_keys(private_key_file_path, public_key_file_path, private_key_file_path) is None -# -# os.remove(private_key_file_path) -# os.remove(public_key_file_path) +def test_load_keys(): + # Let's first create a private key and public key files + private_key_file_path = "sk_test_file" + public_key_file_path = "pk_test_file" + empty_file_path = "empty_file" + with open(private_key_file_path, "wb") as f: + f.write(dummy_sk.to_der()) + with open(public_key_file_path, "wb") as f: + f.write(dummy_pk.format(compressed=True)) + with open(empty_file_path, "wb") as f: + pass + + # Now we can test the function passing the using this files (we'll use the same pk for both) + r = teos_cli.load_keys(public_key_file_path, private_key_file_path, public_key_file_path) + assert isinstance(r, tuple) + assert len(r) == 3 + + # If any param does not match we should get None as result + assert teos_cli.load_keys(None, private_key_file_path, public_key_file_path) is None + assert teos_cli.load_keys(public_key_file_path, None, public_key_file_path) is None + assert teos_cli.load_keys(public_key_file_path, private_key_file_path, None) is None + + # The same should happen if we pass a public key where a private should be, for instance + assert teos_cli.load_keys(private_key_file_path, public_key_file_path, private_key_file_path) is None + + # Same if any of the files is empty + assert teos_cli.load_keys(empty_file_path, private_key_file_path, public_key_file_path) is None + assert teos_cli.load_keys(public_key_file_path, empty_file_path, public_key_file_path) is None + assert teos_cli.load_keys(public_key_file_path, private_key_file_path, empty_file_path) is None + + os.remove(private_key_file_path) + os.remove(public_key_file_path) + os.remove(empty_file_path) # TODO: 90-add-more-add-appointment-tests