From 608d468b2692e3381a49e5cb69fa5feb3164228d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=BA=C3=B1ez?= Date: Thu, 8 Nov 2018 12:07:12 +0100 Subject: [PATCH] Testing persistence of Alice's DelegatingPower --- .../test_alice_can_grant_and_revoke.py | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/characters/test_alice_can_grant_and_revoke.py b/tests/characters/test_alice_can_grant_and_revoke.py index f06c407d2..9e6805794 100644 --- a/tests/characters/test_alice_can_grant_and_revoke.py +++ b/tests/characters/test_alice_can_grant_and_revoke.py @@ -16,10 +16,16 @@ along with nucypher. If not, see . """ import datetime import maya +import os import pytest from umbral.fragments import KFrag +from nucypher.characters.lawful import Alice, Bob +from nucypher.config.characters import AliceConfiguration +from nucypher.config.storages import LocalFileBasedNodeStorage from nucypher.crypto.api import keccak_digest +from nucypher.utilities.sandbox.constants import TEST_URSULA_INSECURE_DEVELOPMENT_PASSWORD +from nucypher.utilities.sandbox.middleware import MockRestMiddleware from nucypher.utilities.sandbox.policy import MockPolicyCreation @@ -87,3 +93,86 @@ def test_federated_grant(federated_alice, federated_bob): retrieved_kfrag = KFrag.from_bytes(retrieved_policy.kfrag) assert kfrag == retrieved_kfrag + + +def test_alice_delegation_power_is_persistent(federated_ursulas, tmpdir): + + passphrase = TEST_URSULA_INSECURE_DEVELOPMENT_PASSWORD + + # Let's create an Alice from a Configuration. + # This requires creating a local storage for her first. + node_storage = LocalFileBasedNodeStorage( + federated_only=True, + character_class=Alice, + known_metadata_dir=os.path.join(tmpdir, "known_metadata"), + ) + + alice_config = AliceConfiguration( + config_root=os.path.join(tmpdir, "config_root"), + node_storage=node_storage, + auto_initialize=True, + auto_generate_keys=True, + passphrase=passphrase, + is_me=True, + network_middleware=MockRestMiddleware(), + known_nodes=federated_ursulas, + start_learning_now=False, + federated_only=True, + # abort_on_learning_error=True, + save_metadata=False, + load_metadata=False + ) + alice = alice_config(passphrase=passphrase) + + # We will save Alice's config to a file for later use + alice_config_file = alice_config.to_configuration_file() + + # Now, let's create a policy for some Bob + m, n = 3, 4 + policy_end_datetime = maya.now() + datetime.timedelta(days=5) + label = b"this_is_the_path_to_which_access_is_being_granted" + + bob = Bob(federated_only=True, + start_learning_now=False, + network_middleware=MockRestMiddleware(), + ) + + bob_policy = alice.grant(bob, label, m=m, n=n, expiration=policy_end_datetime) + + # ... and Alice and her configuration disappear. + del alice + del alice_config + + ################################### + # Some time passes. # + # ... # + # (jmyles plays the Song of Time) # + # ... # + # Alice appears again. # + ################################### + + new_alice_config = AliceConfiguration.from_configuration_file( + filepath=alice_config_file, + network_middleware=MockRestMiddleware(), + known_nodes=federated_ursulas, + start_learning_now=False, + ) + + new_alice = new_alice_config(passphrase=passphrase) + + # Bob's eldest brother, Roberto, appears too + roberto = Bob(federated_only=True, + start_learning_now=False, + network_middleware=MockRestMiddleware(), + ) + + # Alice creates a new policy for Roberto. Note how all the parameters + # except for the label (i.e., recipient, m, n, policy_end) are different + # from previous policy + m, n = 2, 5 + policy_end_datetime = maya.now() + datetime.timedelta(days=3) + roberto_policy = new_alice.grant(roberto, label, m=m, n=n, expiration=policy_end_datetime) + + # Both policies must share the same delegation public key + assert bob_policy.public_key == roberto_policy.public_key +