2017-11-22 04:19:31 +00:00
|
|
|
import datetime
|
2018-02-28 03:57:55 +00:00
|
|
|
import os
|
2018-04-13 00:24:55 +00:00
|
|
|
import tempfile
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2018-04-12 03:17:39 +00:00
|
|
|
import maya
|
|
|
|
import pytest
|
2018-04-05 17:20:34 +00:00
|
|
|
from constant_sorrow import constants
|
2018-04-12 03:17:39 +00:00
|
|
|
from sqlalchemy.engine import create_engine
|
|
|
|
|
2018-05-21 19:37:22 +00:00
|
|
|
from nucypher.blockchain.eth.chains import Blockchain
|
2018-05-08 19:35:34 +00:00
|
|
|
from nucypher.characters import Alice, Bob
|
2018-05-31 17:18:37 +00:00
|
|
|
|
2018-06-04 17:57:03 +00:00
|
|
|
from nucypher.config.configs import NucypherConfiguration
|
2018-04-13 23:22:26 +00:00
|
|
|
|
2018-05-08 19:35:34 +00:00
|
|
|
from nucypher.data_sources import DataSource
|
|
|
|
from nucypher.keystore import keystore
|
|
|
|
from nucypher.keystore.db import Base
|
|
|
|
from nucypher.keystore.keypairs import SigningKeypair
|
2018-06-04 17:57:03 +00:00
|
|
|
from tests.utilities import MockNetworkMiddleware, make_ursulas, EVENT_LOOP
|
|
|
|
from constant_sorrow import constants
|
2018-02-13 20:28:39 +00:00
|
|
|
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2018-05-21 19:37:22 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def nucypher_test_config(blockchain_config):
|
|
|
|
|
2018-06-04 17:57:03 +00:00
|
|
|
config = NucypherConfiguration(keyring="this is a faked keyring object",
|
2018-05-21 19:37:22 +00:00
|
|
|
blockchain_config=blockchain_config)
|
|
|
|
yield config
|
2018-06-04 17:57:03 +00:00
|
|
|
NucypherConfiguration.reset()
|
2018-05-21 19:37:22 +00:00
|
|
|
Blockchain.sever()
|
|
|
|
del config
|
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-12-15 04:36:52 +00:00
|
|
|
def idle_policy(alice, bob):
|
2017-11-22 04:19:31 +00:00
|
|
|
"""
|
2018-04-11 15:15:08 +00:00
|
|
|
Creates a Policy, in a manner typical of how Alice might do it, with a unique uri (soon to be "label" - see #183)
|
2017-11-22 04:19:31 +00:00
|
|
|
"""
|
|
|
|
alice.__resource_id += b"/unique-again" # A unique name each time, like a path.
|
2018-06-04 17:57:03 +00:00
|
|
|
n = constants.NUMBER_OF_URSULAS_IN_NETWORK
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2018-04-11 15:15:08 +00:00
|
|
|
policy = alice.create_policy(
|
2017-11-22 04:19:31 +00:00
|
|
|
bob,
|
|
|
|
alice.__resource_id,
|
|
|
|
m=3,
|
|
|
|
n=n,
|
|
|
|
)
|
2018-04-11 15:15:08 +00:00
|
|
|
return policy
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-08 04:41:04 +00:00
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-12-15 04:36:52 +00:00
|
|
|
def enacted_policy(idle_policy, ursulas):
|
2017-11-22 04:19:31 +00:00
|
|
|
# Alice has a policy in mind and knows of enough qualifies Ursulas; she crafts an offer for them.
|
2018-06-01 01:28:38 +00:00
|
|
|
deposit = constants.NON_PAYMENT(b"0000000")
|
2018-04-07 02:26:13 +00:00
|
|
|
contract_end_datetime = maya.now() + datetime.timedelta(days=5)
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2018-06-01 20:34:42 +00:00
|
|
|
networky_stuff = MockNetworkMiddleware(ursulas)
|
2017-12-15 05:21:09 +00:00
|
|
|
found_ursulas = idle_policy.find_ursulas(networky_stuff, deposit, expiration=contract_end_datetime)
|
|
|
|
idle_policy.match_kfrags_to_found_ursulas(found_ursulas)
|
2017-12-15 04:36:52 +00:00
|
|
|
idle_policy.enact(networky_stuff) # REST call happens here, as does population of TreasureMap.
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-15 04:36:52 +00:00
|
|
|
return idle_policy
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-08 04:41:04 +00:00
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-05-21 19:37:22 +00:00
|
|
|
def alice(ursulas, mock_policy_agent, nucypher_test_config):
|
2018-06-01 20:45:41 +00:00
|
|
|
etherbase, alice, bob, *everyone_else = nucypher_test_config.blockchain.chain.interface.w3.eth.accounts
|
|
|
|
|
|
|
|
_alice = Alice(network_middleware=MockNetworkMiddleware(ursulas),
|
|
|
|
policy_agent=mock_policy_agent, ether_address=alice,
|
|
|
|
config=nucypher_test_config)
|
|
|
|
|
|
|
|
_alice.dht_server.listen(8471)
|
|
|
|
_alice.__resource_id = b"some_resource_id"
|
|
|
|
EVENT_LOOP.run_until_complete(_alice.dht_server.bootstrap([("127.0.0.1", u.dht_port) for u in ursulas]))
|
|
|
|
_alice.network_bootstrap([("127.0.0.1", u.rest_port) for u in ursulas])
|
|
|
|
return _alice
|
2017-11-22 04:19:31 +00:00
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-04-17 08:46:13 +00:00
|
|
|
def bob(ursulas):
|
2018-06-01 20:34:42 +00:00
|
|
|
_bob = Bob(network_middleware=MockNetworkMiddleware(ursulas))
|
|
|
|
return _bob
|
2017-11-22 04:19:31 +00:00
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-05-21 19:37:22 +00:00
|
|
|
def ursulas(nucypher_test_config):
|
2018-06-01 20:45:41 +00:00
|
|
|
|
|
|
|
etherbase, alice, bob, *everyone_else = nucypher_test_config.blockchain.chain.interface.w3.eth.accounts
|
|
|
|
ursula_addresses = everyone_else[:NUMBER_OF_URSULAS_IN_NETWORK]
|
|
|
|
|
|
|
|
_ursulas = make_ursulas(ether_addresses=ursula_addresses,
|
|
|
|
ursula_starting_port=URSULA_PORT,
|
|
|
|
config=nucypher_test_config)
|
|
|
|
yield _ursulas
|
2018-02-28 03:52:22 +00:00
|
|
|
# Remove the DBs that have been sprayed hither and yon.
|
2018-06-01 20:45:41 +00:00
|
|
|
for index, ursula in enumerate(_ursulas):
|
|
|
|
port = URSULA_PORT + index
|
2018-02-28 03:52:22 +00:00
|
|
|
os.remove("test-{}".format(port))
|
2017-12-08 04:41:26 +00:00
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-04-17 08:46:13 +00:00
|
|
|
def treasure_map_is_set_on_dht(enacted_policy, ursulas):
|
2018-06-01 20:34:42 +00:00
|
|
|
networky_stuff = MockNetworkMiddleware(ursulas)
|
2018-04-17 08:46:13 +00:00
|
|
|
enacted_policy.publish_treasure_map(networky_stuff, use_dht=True)
|
2018-02-13 20:28:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def test_keystore():
|
|
|
|
engine = create_engine('sqlite:///:memory:')
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
test_keystore = keystore.KeyStore(engine)
|
|
|
|
yield test_keystore
|
2018-02-13 20:22:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2018-04-13 01:59:04 +00:00
|
|
|
def capsule_side_channel(enacted_policy):
|
2018-04-12 08:37:02 +00:00
|
|
|
signing_keypair = SigningKeypair()
|
2018-04-19 06:24:19 +00:00
|
|
|
data_source = DataSource(policy_pubkey_enc=enacted_policy.public_key,
|
2018-05-28 19:10:05 +00:00
|
|
|
signing_keypair=signing_keypair)
|
2018-04-12 08:37:02 +00:00
|
|
|
message_kit, _signature = data_source.encapsulate_single_message(b"Welcome to the flippering.")
|
|
|
|
return message_kit, data_source
|
2018-02-19 23:20:40 +00:00
|
|
|
|
|
|
|
|
2018-04-13 00:24:55 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def tempfile_path():
|
|
|
|
"""
|
|
|
|
User is responsible for closing the file given at the path.
|
|
|
|
"""
|
|
|
|
_, path = tempfile.mkstemp()
|
|
|
|
yield path
|
|
|
|
os.remove(path)
|