2018-11-04 19:23:11 +00:00
|
|
|
"""
|
|
|
|
This file is part of nucypher.
|
|
|
|
|
|
|
|
nucypher is free software: you can redistribute it and/or modify
|
2019-03-05 02:50:11 +00:00
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
2018-11-04 19:23:11 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
nucypher is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-03-05 02:50:11 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2018-11-04 19:23:11 +00:00
|
|
|
|
2019-03-05 02:50:11 +00:00
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2018-11-04 19:23:11 +00:00
|
|
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2019-02-16 18:57:44 +00:00
|
|
|
|
2020-05-13 03:23:33 +00:00
|
|
|
import os
|
2021-07-12 15:50:39 +00:00
|
|
|
import random
|
|
|
|
import string
|
2018-09-13 19:26:25 +00:00
|
|
|
|
2021-07-12 15:50:39 +00:00
|
|
|
from nucypher.characters.lawful import Enrico
|
|
|
|
from nucypher.crypto.powers import DecryptingPower
|
|
|
|
from nucypher.policy.orders import WorkOrder
|
2021-06-25 12:48:35 +00:00
|
|
|
|
2019-02-16 18:57:44 +00:00
|
|
|
|
|
|
|
def generate_random_label() -> bytes:
|
|
|
|
"""
|
|
|
|
Generates a random bytestring for use as a test label.
|
|
|
|
:return: bytes
|
|
|
|
"""
|
2019-02-16 18:58:52 +00:00
|
|
|
adjs = ('my', 'sesame-street', 'black', 'cute')
|
2019-02-16 18:57:44 +00:00
|
|
|
nouns = ('lizard', 'super-secret', 'data', 'coffee')
|
|
|
|
combinations = list('-'.join((a, n)) for a in adjs for n in nouns)
|
|
|
|
selection = random.choice(combinations)
|
|
|
|
random_label = f'label://{selection}-{os.urandom(4).hex()}'
|
|
|
|
return bytes(random_label, encoding='utf-8')
|
2021-06-25 12:48:35 +00:00
|
|
|
|
|
|
|
|
2021-07-12 15:50:39 +00:00
|
|
|
def work_order_setup(enacted_policy,
|
2021-06-25 12:48:35 +00:00
|
|
|
ursulas,
|
|
|
|
bob,
|
|
|
|
alice):
|
2021-07-12 15:50:39 +00:00
|
|
|
|
2021-08-18 22:45:53 +00:00
|
|
|
treasure_map = bob._decrypt_treasure_map(enacted_policy.treasure_map,
|
|
|
|
enacted_policy.publisher_verifying_key)
|
2021-08-15 05:00:50 +00:00
|
|
|
|
2021-07-12 15:50:39 +00:00
|
|
|
# We pick up our story with Bob already having followed the treasure map above, ie:
|
|
|
|
bob.start_learning_loop()
|
|
|
|
|
|
|
|
bob.follow_treasure_map(treasure_map=treasure_map, block=True, timeout=1)
|
|
|
|
|
|
|
|
assert len(bob.known_nodes) == len(ursulas)
|
|
|
|
|
|
|
|
# Bob has no saved work orders yet, ever.
|
|
|
|
assert len(bob._completed_work_orders) == 0
|
|
|
|
|
|
|
|
# We'll test against just a single Ursula - here, we make a WorkOrder for just one.
|
|
|
|
# We can pass any number of capsules as args; here we pass just one.
|
|
|
|
enrico = Enrico(policy_encrypting_key=enacted_policy.public_key)
|
|
|
|
original_message = ''.join(random.choice(string.ascii_lowercase) for i in range(20)) # random message
|
|
|
|
message_kit, _ = enrico.encrypt_message(original_message.encode())
|
|
|
|
message_kit.set_correctness_keys(delegating=enacted_policy.public_key,
|
|
|
|
receiving=bob.public_keys(DecryptingPower),
|
|
|
|
verifying=alice.stamp.as_umbral_pubkey())
|
|
|
|
work_orders, _ = bob.work_orders_for_capsules(
|
|
|
|
message_kit.capsule,
|
|
|
|
label=enacted_policy.label,
|
|
|
|
treasure_map=treasure_map,
|
|
|
|
alice_verifying_key=alice.stamp.as_umbral_pubkey(),
|
|
|
|
num_ursulas=1)
|
|
|
|
|
|
|
|
# Again: one Ursula, one work_order.
|
|
|
|
assert len(work_orders) == 1
|
|
|
|
ursula_address, work_order = list(work_orders.items())[0]
|
|
|
|
return ursula_address, work_order
|