async def test_different_accounts(test_suite: ServerTestSuite): params1 = test_suite.get_agent_params('agent1') params2 = test_suite.get_agent_params('agent2') agent1 = Agent( server_address=params1['server_address'], credentials=params1['credentials'], p2p=params1['p2p'], timeout=5, ) agent2 = Agent( server_address=params2['server_address'], credentials=params2['credentials'], p2p=params2['p2p'], timeout=5, ) await agent1.open() await agent2.open() try: same_resources = [f'resource/{uuid.uuid4().hex}'] ok1, _ = await agent1.acquire(resources=same_resources, lock_timeout=10.0) ok2, _ = await agent1.acquire(resources=same_resources, lock_timeout=10.0) assert ok1 is True assert ok2 is True finally: await agent1.close() await agent2.close()
async def test_lock_multiple_time(test_suite: ServerTestSuite): params = test_suite.get_agent_params('agent1') session1 = Agent( server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=5, ) session2 = Agent( server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=5, ) await session1.open() await session2.open() try: resources1 = [f'resource-{uuid.uuid4().hex}'] timeout = 5.0 ok, _ = await session1.acquire(resources=resources1, lock_timeout=timeout) assert ok is True resources2 = [f'resource-{uuid.uuid4().hex}'] ok, _ = await session1.acquire(resources=resources2, lock_timeout=timeout) assert ok is True # session1 must unlock previously locked resources on new acquire call ok, _ = await session2.acquire(resources=resources1, lock_timeout=timeout) assert ok is True finally: await session1.close() await session2.close()
async def test_agents_communications(test_suite: ServerTestSuite): agent1_params = test_suite.get_agent_params('agent1') agent2_params = test_suite.get_agent_params('agent2') entity1 = list(agent1_params['entities'].items())[0][1] entity2 = list(agent2_params['entities'].items())[0][1] agent1 = Agent( server_address=agent1_params['server_address'], credentials=agent1_params['credentials'], p2p=agent1_params['p2p'], timeout=5, ) agent2 = Agent( server_address=agent2_params['server_address'], credentials=agent2_params['credentials'], p2p=agent2_params['p2p'], timeout=5, ) await agent1.open() await agent2.open() try: # Get endpoints agent2_endpoint = [ e for e in agent2.endpoints if e.routing_keys == [] ][0].address agent2_listener = await agent2.subscribe() # Exchange Pairwise await agent1.wallet.did.store_their_did(entity2['did'], entity2['verkey']) if not await agent1.wallet.pairwise.is_pairwise_exists(entity2['did']): print('#1') await agent1.wallet.pairwise.create_pairwise( their_did=entity2['did'], my_did=entity1['did']) await agent2.wallet.did.store_their_did(entity1['did'], entity1['verkey']) if not await agent2.wallet.pairwise.is_pairwise_exists(entity1['did']): print('#2') await agent2.wallet.pairwise.create_pairwise( their_did=entity1['did'], my_did=entity2['did']) # Prepare message trust_ping = Message({ '@id': 'trust-ping-message-' + uuid.uuid4().hex, '@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/trust_ping/1.0/ping', "comment": "Hi. Are you listening?", "response_requested": True }) await agent1.send_message(message=trust_ping, their_vk=entity2['verkey'], endpoint=agent2_endpoint, my_vk=entity1['verkey'], routing_keys=[]) event = await agent2_listener.get_one(timeout=5) msg = event['message'] assert msg[ '@type'] == 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/trust_ping/1.0/ping' assert msg['@id'] == trust_ping.id finally: await agent1.close() await agent2.close()
async def test__threadbased_protocol_on_hub(test_suite: ServerTestSuite): agent1_params = test_suite.get_agent_params('agent1') agent2_params = test_suite.get_agent_params('agent2') agent1 = Agent( server_address=agent1_params['server_address'], credentials=agent1_params['credentials'], p2p=agent1_params['p2p'], timeout=5, ) agent2 = Agent( server_address=agent2_params['server_address'], credentials=agent2_params['credentials'], p2p=agent2_params['p2p'], timeout=5, ) await agent1.open() await agent2.open() try: # Get endpoints agent1_endpoint = [ e for e in agent1.endpoints if e.routing_keys == [] ][0].address agent2_endpoint = [ e for e in agent2.endpoints if e.routing_keys == [] ][0].address # Init pairwise list #1 did1, verkey1 = await agent1.wallet.did.create_and_store_my_did() did2, verkey2 = await agent2.wallet.did.create_and_store_my_did() await agent1.wallet.did.store_their_did(did2, verkey2) await agent1.wallet.pairwise.create_pairwise(their_did=did2, my_did=did1) await agent2.wallet.did.store_their_did(did1, verkey1) await agent2.wallet.pairwise.create_pairwise(their_did=did1, my_did=did2) # Init pairwise list #2 pairwise1 = Pairwise(me=Pairwise.Me(did=did1, verkey=verkey1), their=Pairwise.Their(did=did2, label='Label-2', endpoint=agent2_endpoint, verkey=verkey2)) pairwise2 = Pairwise(me=Pairwise.Me(did=did2, verkey=verkey2), their=Pairwise.Their(did=did1, label='Label-1', endpoint=agent1_endpoint, verkey=verkey1)) finally: await agent1.close() await agent2.close() thread_id = uuid.uuid4().hex co1 = sirius_sdk.CoProtocolThreadedP2P(thread_id, pairwise1) co2 = sirius_sdk.CoProtocolThreadedP2P(thread_id, pairwise2) MSG_LOG.clear() await run_coroutines(routine1_on_hub(co1, **agent1_params), routine2_on_hub(co2, **agent2_params)) check_msg_log()
def handle(self, *args, **options): meta_url = options['meta_url'] print('======================') print('Meta URL: ' + meta_url) print('======================') resp = requests.get(meta_url) assert resp.status_code == 200 meta = resp.json() components = list(urlsplit(meta_url)) components[2] = '' url = urlunsplit(components) for outer_name in meta.keys(): for inner_name in meta.keys(): if inner_name == outer_name: continue print('### establish pairwise %s:%s' % (outer_name, inner_name)) agent1 = Agent( server_address=url, credentials=meta[outer_name]['credentials'].encode( 'ascii'), p2p=P2PConnection(my_keys=( meta[outer_name]['p2p']['smart_contract']['verkey'], meta[outer_name]['p2p']['smart_contract'] ['secret_key'], ), their_verkey=meta[outer_name]['p2p'] ['agent']['verkey'])) key1 = list(meta[outer_name]['entities'].keys())[0] entity1 = meta[outer_name]['entities'][key1] agent2 = Agent( server_address=url, credentials=meta[inner_name]['credentials'].encode( 'ascii'), p2p=P2PConnection(my_keys=( meta[inner_name]['p2p']['smart_contract']['verkey'], meta[inner_name]['p2p']['smart_contract'] ['secret_key'], ), their_verkey=meta[inner_name]['p2p'] ['agent']['verkey'])) key2 = list(meta[inner_name]['entities'].keys())[0] entity2 = meta[inner_name]['entities'][key2] run_async(self.establish_connection(agent1, entity1, agent2, entity2), timeout=190) print('### establish success !')
async def participants_trust_ping(ping_id: str): if settings.AGENT['entity']: # extract neighbours neighbours = {did: meta for did, meta in settings.PARTICIPANTS_META.items() if did != settings.AGENT['entity']} agent = Agent( server_address=settings.AGENT['server_address'], credentials=settings.AGENT['credentials'].encode('ascii'), p2p=P2PConnection( my_keys=( settings.AGENT['my_verkey'], settings.AGENT['my_secret_key'] ), their_verkey=settings.AGENT['agent_verkey'] ) ) print('agent.open()') await agent.open() print('start ping to all') try: for their_did, meta in neighbours.items(): to = await agent.pairwise_list.load_for_did(their_did) print('ping to: ' + their_did) await agent.send_to( message=Ping(comment=ping_id), to=to ) finally: await agent.close()
async def test__their_endpoint_protocol(test_suite: ServerTestSuite): agent1_params = test_suite.get_agent_params('agent1') agent2_params = test_suite.get_agent_params('agent2') entity1 = list(agent1_params['entities'].items())[0][1] entity2 = list(agent2_params['entities'].items())[0][1] agent1 = Agent( server_address=agent1_params['server_address'], credentials=agent1_params['credentials'], p2p=agent1_params['p2p'], timeout=5, ) agent2 = Agent( server_address=agent2_params['server_address'], credentials=agent2_params['credentials'], p2p=agent2_params['p2p'], timeout=5, ) await agent1.open() await agent2.open() try: # Get endpoints agent1_endpoint = [ e for e in agent1.endpoints if e.routing_keys == [] ][0].address agent2_endpoint = [ e for e in agent2.endpoints if e.routing_keys == [] ][0].address # Make protocol instances their1 = TheirEndpoint(agent2_endpoint, entity2['verkey']) agent1_protocol = await agent1.spawn(entity1['verkey'], their1) assert isinstance(agent1_protocol, TheirEndpointCoProtocolTransport) their2 = TheirEndpoint(agent1_endpoint, entity1['verkey']) agent2_protocol = await agent2.spawn(entity2['verkey'], their2) assert isinstance(agent2_protocol, TheirEndpointCoProtocolTransport) await agent1_protocol.start(['test_protocol']) await agent2_protocol.start(['test_protocol']) try: MSG_LOG.clear() await run_coroutines(routine1(agent1_protocol), routine2(agent2_protocol)) check_msg_log() finally: await agent1_protocol.stop() await agent2_protocol.stop() finally: await agent1.close() await agent2.close()
def alloc_agent_connection() -> Agent: agent = Agent( server_address=settings.AGENT['server_address'], credentials=settings.AGENT['credentials'].encode('ascii'), p2p=P2PConnection(my_keys=(settings.AGENT['my_verkey'], settings.AGENT['my_secret_key']), their_verkey=settings.AGENT['agent_verkey'])) return agent
def get_agent(name: str) -> Agent: params = get_suite_singleton().get_agent_params(name) agent = Agent(server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=30, name=name) return agent
async def test_same_account(test_suite: ServerTestSuite): params = test_suite.get_agent_params('agent1') session1 = Agent( server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=5, ) session2 = Agent( server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=5, ) await session1.open() await session2.open() try: # check locking OK resources = [f'resource-{uuid.uuid4().hex}' for i in range(100)] ok, busy = await session1.acquire(resources=resources, lock_timeout=5) try: assert ok is True ok, busy = await session2.acquire(resources=resources, lock_timeout=1) assert ok is False assert set(busy) == set(resources) finally: await session1.release() # check session ok may lock after explicitly release ok, busy = await session2.acquire(resources=resources, lock_timeout=1) assert ok is True # Check after timeout resources = [f'resource-{uuid.uuid4().hex}' for i in range(100)] timeout = 5.0 ok, _ = await session1.acquire(resources=resources, lock_timeout=timeout) assert ok is True ok, _ = await session2.acquire(resources=resources, lock_timeout=1.0) assert ok is False await asyncio.sleep(timeout + 1.0) ok, _ = await session2.acquire(resources=resources, lock_timeout=1.0) assert ok is True finally: await session1.close() await session2.close()
async def test__their_endpoint_protocol_on_hub(test_suite: ServerTestSuite): agent1_params = test_suite.get_agent_params('agent1') agent2_params = test_suite.get_agent_params('agent2') entity1 = list(agent1_params['entities'].items())[0][1] entity2 = list(agent2_params['entities'].items())[0][1] agent1 = Agent( server_address=agent1_params['server_address'], credentials=agent1_params['credentials'], p2p=agent1_params['p2p'], timeout=5, ) agent2 = Agent( server_address=agent2_params['server_address'], credentials=agent2_params['credentials'], p2p=agent2_params['p2p'], timeout=5, ) await agent1.open() await agent2.open() try: # Get endpoints agent1_endpoint = [ e for e in agent1.endpoints if e.routing_keys == [] ][0].address agent2_endpoint = [ e for e in agent2.endpoints if e.routing_keys == [] ][0].address finally: await agent1.close() await agent2.close() # FIRE!!! their1 = TheirEndpoint(agent2_endpoint, entity2['verkey']) their2 = TheirEndpoint(agent1_endpoint, entity1['verkey']) co1 = sirius_sdk.CoProtocolP2PAnon(entity1['verkey'], their1, ['test_protocol']) co2 = sirius_sdk.CoProtocolP2PAnon(entity2['verkey'], their2, ['test_protocol']) MSG_LOG.clear() await run_coroutines(routine1_on_hub(co1, **agent1_params), routine2_on_hub(co2, **agent2_params)) check_msg_log()
async def test__all_agents_ping(test_suite: ServerTestSuite): for name in ['agent1', 'agent2', 'agent3', 'agent4']: params = test_suite.get_agent_params(name) agent = Agent( server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=5, ) await agent.open() try: success = await agent.ping() assert success is True, 'agent [%s] is not ping-able' % name finally: await agent.close()
async def test_agents_wallet(test_suite: ServerTestSuite): params = test_suite.get_agent_params('agent1') agent = Agent( server_address=params['server_address'], credentials=params['credentials'], p2p=params['p2p'], timeout=5, ) await agent.open() try: # Check wallet calls is ok did, verkey = await agent.wallet.did.create_and_store_my_did() assert did assert verkey # check reopen is OK await agent.reopen() finally: await agent.close()
async def test__protocols_intersections(test_suite: ServerTestSuite): agent1_params = test_suite.get_agent_params('agent1') agent2_params = test_suite.get_agent_params('agent2') agent1 = Agent( server_address=agent1_params['server_address'], credentials=agent1_params['credentials'], p2p=agent1_params['p2p'], timeout=15, ) agent2 = Agent( server_address=agent2_params['server_address'], credentials=agent2_params['credentials'], p2p=agent2_params['p2p'], timeout=15, ) await agent1.open() await agent2.open() try: # Get endpoints agent1_endpoint = [ e for e in agent1.endpoints if e.routing_keys == [] ][0].address agent2_endpoint = [ e for e in agent2.endpoints if e.routing_keys == [] ][0].address # Init pairwise list #1 did1, verkey1 = await agent1.wallet.did.create_and_store_my_did() did2, verkey2 = await agent2.wallet.did.create_and_store_my_did() await agent1.wallet.did.store_their_did(did2, verkey2) await agent1.wallet.pairwise.create_pairwise(their_did=did2, my_did=did1) await agent2.wallet.did.store_their_did(did1, verkey1) await agent2.wallet.pairwise.create_pairwise(their_did=did1, my_did=did2) # Init pairwise list #2 pairwise1 = Pairwise(me=Pairwise.Me(did=did1, verkey=verkey1), their=Pairwise.Their(did=did2, label='Label-2', endpoint=agent2_endpoint, verkey=verkey2)) pairwise2 = Pairwise(me=Pairwise.Me(did=did2, verkey=verkey2), their=Pairwise.Their(did=did1, label='Label-1', endpoint=agent1_endpoint, verkey=verkey1)) thread_id = uuid.uuid4().hex agent1_protocol_threaded = await agent1.spawn(thread_id, pairwise1) agent2_protocol_threaded = await agent2.spawn(thread_id, pairwise2) assert isinstance(agent1_protocol_threaded, ThreadBasedCoProtocolTransport) assert isinstance(agent2_protocol_threaded, ThreadBasedCoProtocolTransport) agent1_protocol_pairwise = await agent1.spawn(pairwise1) agent2_protocol_pairwise = await agent2.spawn(pairwise2) assert isinstance(agent1_protocol_pairwise, PairwiseCoProtocolTransport) assert isinstance(agent2_protocol_pairwise, PairwiseCoProtocolTransport) await agent1_protocol_threaded.start(['test_protocol']) await agent2_protocol_threaded.start(['test_protocol']) await agent1_protocol_pairwise.start(['test_protocol']) await agent2_protocol_pairwise.start(['test_protocol']) try: MSG_LOG.clear() await run_coroutines( routine1(agent1_protocol_threaded), routine2(agent2_protocol_threaded), routine1(agent1_protocol_pairwise), routine2(agent2_protocol_pairwise), ) # collect messages threaded_sequence = [msg for msg in MSG_LOG if '~thread' in msg] non_threaded_sequence = [ msg for msg in MSG_LOG if '~thread' not in msg ] # threaded messages MSG_LOG.clear() MSG_LOG.extend(threaded_sequence) check_msg_log() for msg in threaded_sequence: assert msg['~thread']['thid'] == thread_id # non-threaded messages MSG_LOG.clear() MSG_LOG.extend(non_threaded_sequence) check_msg_log() finally: await agent1_protocol_threaded.stop() await agent2_protocol_threaded.stop() await agent1_protocol_pairwise.stop() await agent2_protocol_pairwise.stop() finally: await agent1.close() await agent2.close()
async def test__threadbased_protocol(test_suite: ServerTestSuite): agent1_params = test_suite.get_agent_params('agent1') agent2_params = test_suite.get_agent_params('agent2') agent1 = Agent( server_address=agent1_params['server_address'], credentials=agent1_params['credentials'], p2p=agent1_params['p2p'], timeout=5, ) agent2 = Agent( server_address=agent2_params['server_address'], credentials=agent2_params['credentials'], p2p=agent2_params['p2p'], timeout=5, ) await agent1.open() await agent2.open() try: # Get endpoints agent1_endpoint = [ e for e in agent1.endpoints if e.routing_keys == [] ][0].address agent2_endpoint = [ e for e in agent2.endpoints if e.routing_keys == [] ][0].address # Init pairwise list #1 did1, verkey1 = await agent1.wallet.did.create_and_store_my_did() did2, verkey2 = await agent2.wallet.did.create_and_store_my_did() await agent1.wallet.did.store_their_did(did2, verkey2) await agent1.wallet.pairwise.create_pairwise(their_did=did2, my_did=did1) await agent2.wallet.did.store_their_did(did1, verkey1) await agent2.wallet.pairwise.create_pairwise(their_did=did1, my_did=did2) # Init pairwise list #2 pairwise1 = Pairwise(me=Pairwise.Me(did=did1, verkey=verkey1), their=Pairwise.Their(did=did2, label='Label-2', endpoint=agent2_endpoint, verkey=verkey2)) pairwise2 = Pairwise(me=Pairwise.Me(did=did2, verkey=verkey2), their=Pairwise.Their(did=did1, label='Label-1', endpoint=agent1_endpoint, verkey=verkey1)) thread_id = uuid.uuid4().hex agent1_protocol = await agent1.spawn(thread_id, pairwise1) agent2_protocol = await agent2.spawn(thread_id, pairwise2) assert isinstance(agent1_protocol, ThreadBasedCoProtocolTransport) assert isinstance(agent2_protocol, ThreadBasedCoProtocolTransport) await agent1_protocol.start() await agent2_protocol.start() try: MSG_LOG.clear() await run_coroutines(routine1(agent1_protocol), routine2(agent2_protocol)) check_msg_log() finally: await agent1_protocol.stop() await agent2_protocol.stop() finally: await agent1.close() await agent2.close()