async def test_init_client_peer_throttling(peer_listener, peer_manager, test_endpoint): """Test setup of ClientPeer.""" client = ClientPeer("127.0.0.1", "8893") connector = Connector("127.0.0.1", "8822") assert not client.is_connected assert not peer_manager.peer_available("localhost") valid = datetime.utcnow() + timedelta(days=1) aes_key = os.urandom(32) aes_iv = os.urandom(16) hostname = "localhost" fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv) await client.start(connector, fernet_token, aes_key, aes_iv, throttling=500) await asyncio.sleep(0.1) assert peer_manager.peer_available("localhost") assert client.is_connected assert client._multiplexer._throttling == 0.002 await client.stop() await asyncio.sleep(0.1) assert not client.is_connected assert not peer_manager.peer_available("localhost")
async def test_close_connector_local(test_endpoint, multiplexer_client, multiplexer_server): """Test and init a connector.""" assert not test_endpoint connector = Connector("127.0.0.1", "8822") multiplexer_client._new_connections = connector.handler channel = await multiplexer_server.create_channel(IP_ADDR) await asyncio.sleep(0.1) assert test_endpoint test_connection = test_endpoint[0] await channel.write(b"Hallo") data = await test_connection.reader.read(1024) assert data == b"Hallo" test_connection.writer.write(b"Hiro") await test_connection.writer.drain() data = await channel.read() assert data == b"Hiro" test_connection.writer.close() test_connection.close.set() await asyncio.sleep(0.1) with pytest.raises(MultiplexerTransportClose): await channel.read()
async def test_init_client_peer_wait(peer_listener, peer_manager, test_endpoint): """Test setup of ClientPeer.""" client = ClientPeer("127.0.0.1", "8893") connector = Connector("127.0.0.1", "8822") assert not client.is_connected assert not peer_manager.peer_available("localhost") valid = datetime.utcnow() + timedelta(days=1) aes_key = os.urandom(32) aes_iv = os.urandom(16) hostname = "localhost" fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv) await client.start(connector, fernet_token, aes_key, aes_iv) await asyncio.sleep(0.1) assert peer_manager.peer_available("localhost") assert client.is_connected assert not client.wait().done() await client.stop() await asyncio.sleep(0.1) assert not client.is_connected assert not peer_manager.peer_available("localhost") with pytest.raises(RuntimeError): assert client.wait().done()
async def test_close_connector_remote(test_endpoint, multiplexer_client, multiplexer_server): """Test and init a connector with remote close.""" assert not test_endpoint connector = Connector("127.0.0.1", "8822") multiplexer_client._new_connections = connector.handler channel = await multiplexer_server.create_channel(IP_ADDR) await asyncio.sleep(0.1) assert test_endpoint test_connection = test_endpoint[0] await channel.write(b"Hallo") data = await test_connection.reader.read(1024) assert data == b"Hallo" test_connection.writer.write(b"Hiro") await test_connection.writer.drain() data = await channel.read() assert data == b"Hiro" await multiplexer_server.delete_channel(channel) data = await test_connection.reader.read(1024) assert not data test_connection.close.set()
async def test_close_client_peer(peer_listener, peer_manager, test_endpoint): """Test setup of ClientPeer, test flow - close it.""" client = ClientPeer("127.0.0.1", "8893") connector = Connector("127.0.0.1", "8822") assert not peer_manager.peer_available("localhost") valid = datetime.utcnow() + timedelta(days=1) aes_key = os.urandom(32) aes_iv = os.urandom(16) hostname = "localhost" fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv) await client.start(connector, fernet_token, aes_key, aes_iv) await asyncio.sleep(0.1) assert peer_manager.peer_available("localhost") peer = peer_manager.get_peer("localhost") channel = await peer.multiplexer.create_channel(IP_ADDR) await asyncio.sleep(0.1) assert test_endpoint test_connection = test_endpoint[0] await channel.write(b"Hallo") data = await test_connection.reader.read(1024) assert data == b"Hallo" assert channel.ip_address == IP_ADDR test_connection.writer.write(b"Hiro") await test_connection.writer.drain() data = await channel.read() assert data == b"Hiro" await client.stop() await asyncio.sleep(0.1) assert not client.is_connected assert not peer_manager.peer_available("localhost") data = await test_connection.reader.read(1024) assert not data test_connection.close.set()
async def test_init_connector_whitelist_bad(test_endpoint, multiplexer_client, multiplexer_server): """Test and init a connector with whitelist bad requests.""" assert not test_endpoint connector = Connector("127.0.0.1", "8822", True) multiplexer_client._new_connections = connector.handler connector.whitelist.add(IP_ADDR) assert IP_ADDR in connector.whitelist assert BAD_ADDR not in connector.whitelist channel = await multiplexer_server.create_channel(BAD_ADDR) await asyncio.sleep(0.1) assert not test_endpoint with pytest.raises(MultiplexerTransportClose): await channel.read()
async def test_fernet_token_date(peer_listener, peer_manager, test_endpoint): """Test fernet token created by server as invalid.""" client = ClientPeer("127.0.0.1", "8893") connector = Connector("127.0.0.1", "8822") assert not peer_manager.peer_available("localhost") valid = timedelta(days=-1) aes_key = os.urandom(32) aes_iv = os.urandom(16) hostname = "localhost" fernet_token = server.generate_client_token(FERNET_TOKENS, valid, hostname, aes_key, aes_iv) with pytest.raises(SniTunConnectionError): await client.start(connector, fernet_token, aes_key, aes_iv) await asyncio.sleep(0.1) assert not peer_manager.peer_available("localhost")
async def test_init_connector_whitelist(test_endpoint, multiplexer_client, multiplexer_server): """Test and init a connector with whitelist.""" assert not test_endpoint connector = Connector("127.0.0.1", "8822", True) multiplexer_client._new_connections = connector.handler connector.whitelist.add(IP_ADDR) assert IP_ADDR in connector.whitelist channel = await multiplexer_server.create_channel(IP_ADDR) await asyncio.sleep(0.1) assert test_endpoint test_connection = test_endpoint[0] await channel.write(b"Hallo") data = await test_connection.reader.read(1024) assert data == b"Hallo" test_connection.close.set()