def test_server_can_connect(): from pseud import Server user_id = b'echo' endpoint = b'tcp://127.0.0.1:5000' server = Server(user_id, security_plugin='noop_auth_backend') server.connect(endpoint)
async def test_trusted_curve(loop, unused_tcp_port, trusted_curve_auth_backend): from pseud import Client, Server from pseud.utils import register_rpc server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, loop=loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE register_rpc(name='string.lower')(str.lower) async with server, client: result = await client.string.lower('FOO') assert result == 'foo'
def test_server_can_bind(): from pseud import Server user_id = b'echo' endpoint = 'inproc://{}'.format(__name__).encode() server = Server(user_id, security_plugin='noop_auth_backend') server.bind(endpoint)
async def test_trusted_curve(loop, unused_tcp_port): from pseud import Client, Server from pseud.utils import register_rpc server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, loop=loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE register_rpc(name='string.lower')(str.lower) async with server, client: result = await client.string.lower('FOO') assert result == 'foo'
async def test_trusted_curve_with_wrong_peer_public_key( loop, unused_tcp_port_factory): from pseud import Client, Server from pseud.utils import register_rpc server_id = b'server' port = unused_tcp_port_factory() endpoint = f'tcp://127.0.0.1:{port}' server_public, server_secret = zmq.curve_keypair() server = Server(server_id, security_plugin='trusted_curve', public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) alice_public, alice_secret = \ server.auth_backend.known_identities[b'alice'] client = Client(server_id, user_id=b'alice', security_plugin='trusted_curve', public_key=alice_public, secret_key=alice_secret, peer_public_key=z85.encode(b'R' * 32), timeout=.5, loop=loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE register_rpc(name='string.lower')(str.lower) async with server, client: with pytest.raises(asyncio.TimeoutError): await client.string.lower('BAR')
async def test_untrusted_curve_with_allowed_password_and_client_disconnect( loop, unused_tcp_port): from pseud import Client, Server client_id = b'john' server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, user_id=client_id, password=password, timeout=1, loop=loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password server.register_rpc(name='string.lower')(str.lower) async with server, client: result = await client.string.lower('FOO') assert result == 'foo' # Simulate disconnection and reconnection with new identity client.disconnect(endpoint) client.connect(endpoint) await asyncio.sleep(.1) result = await client.string.lower('ABC') assert result == 'abc'
async def test_untrusted_curve_with_allowed_password( loop, unused_tcp_port, untrusted_curve_auth_backend): from pseud import Client, Server from pseud.utils import register_rpc client_id = b'john' server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, user_id=client_id, password=password, loop=loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password register_rpc(name='string.lower')(str.lower) async with server, client: result = await client.string.lower('FOO') result2 = await client.string.lower('FOO_JJ') result3 = await server.send_to(client_id).string.lower('ABC') assert result == 'foo' assert result2 == 'foo_jj' assert result3 == 'abc'
def start_server(): server = Server(b'a') server.bind(endpoint) server.register_rpc(str.lower) server.io_loop.add_timeout(server.io_loop.time() + .2, server.stop) server.start()
def make_one_server(user_id, endpoint, security_plugin='noop_auth_backend', heartbeat_plugin=None, loop=None): from pseud import Server server = Server(user_id, heartbeat_plugin=heartbeat_plugin, security_plugin=security_plugin, loop=loop) return server
async def test_untrusted_curve_with_allowed_password(loop, unused_tcp_port): from pseud import Client, Server from pseud.utils import register_rpc client_id = b'john' server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, user_id=client_id, password=password, loop=loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password register_rpc(name='string.lower')(str.lower) async with server, client: result = await client.string.lower('FOO') result2 = await client.string.lower('FOO_JJ') result3 = await server.send_to(client_id).string.lower('ABC') assert result == 'foo' assert result2 == 'foo_jj' assert result3 == 'abc'
def test_untrusted_curve_with_allowed_password_and_client_disconnect(self): from pseud import Client, Server client_id = b'john' server_id = b'server' endpoint = b'tcp://127.0.0.1:8999' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, user_id=client_id, password=password, timeout=1, io_loop=self.io_loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password yield server.start() yield client.start() server.register_rpc(name='string.lower')(str.lower) result = yield client.string.lower('FOO') assert result == 'foo' # Simulate disconnection and reconnection with new identity client.disconnect(endpoint) client.connect(endpoint) result = yield client.string.lower('ABC') assert result == 'abc' server.stop() client.stop()
def make_one_server(user_id, loop, proxy_to=None, security_plugin='noop_auth_backend'): from pseud import Server server = Server(user_id, proxy_to=proxy_to, security_plugin=security_plugin, loop=loop) return server
async def test_untrusted_curve_with_wrong_password(loop, unused_tcp_port): from pseud import Client, Server from pseud.interfaces import UnauthorizedError from pseud.utils import register_rpc client_id = b'john' server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, user_id=client_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, password=password, loop=loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password + b'Looser' register_rpc(name='string.lower')(str.lower) async with server, client: with pytest.raises(UnauthorizedError): await client.string.lower(b'IMSCREAMING')
def test_untrusted_curve_with_allowed_password_and_client_disconnect(self): from pseud import Client, Server from pseud._tornado import async_sleep client_id = b'john' server_id = b'server' endpoint = b'tcp://127.0.0.1:8999' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, user_id=client_id, password=password, timeout=1, io_loop=self.io_loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password yield server.start() yield client.start() server.register_rpc(name='string.lower')(str.lower) result = yield client.string.lower('FOO') assert result == 'foo' # Simulate disconnection and reconnection with new identity client.disconnect(endpoint) client.connect(endpoint) yield async_sleep(self.io_loop, .15) result = yield client.string.lower('ABC') assert result == 'abc' server.stop() client.stop()
async def test_server_can_send_to_trustable_peer_identity(loop, unused_tcp_port): """ Uses internal metadata of zmq.Frame.get() to fetch identity of sender """ from pseud import Client, Server server_id = b'server' endpoint = f'tcp://127.0.0.1:{unused_tcp_port}' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, loop=loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE @server.register_rpc(with_identity=True) def echo(peer_identity, message): return peer_identity, message async with server, client: result = await client.echo(b'one') if zmq.zmq_version_info() >= (4, 1, 0): assert result == (b'bob', b'one') else: assert result == (b'', b'one')
def start_server(can_stop): server = Server(b'a') server.bind(endpoint) server.register_rpc(str.lower) def stop_server(server, can_stop): can_stop.wait() server.stop() stop_thread = threading.Thread( target=functools.partial(stop_server, server, can_stop)) stop_thread.start() server.start()
async def test_client_can_reconnect(loop, unused_tcp_port_factory): from pseud import Client, Server port = unused_tcp_port_factory() server_id = b'server' endpoint = f'tcp://127.0.0.1:{port}' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, loop=loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, loop=loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE server.register_rpc(name='string.upper')(str.upper) async with server, client: result = await client.string.upper('hello') assert result == 'HELLO' client.disconnect(endpoint) client.connect(endpoint) await asyncio.sleep(.01) result = await client.string.upper('hello2') assert result == 'HELLO2'
def test_untrusted_curve_with_allowed_password(self): from pseud import Client, Server from pseud.utils import register_rpc from pseud._tornado import async_sleep client_id = b'john' server_id = b'server' endpoint = b'tcp://127.0.0.1:8999' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, user_id=client_id, password=password, io_loop=self.io_loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password yield server.start() yield client.start() register_rpc(name='string.lower')(str.lower) future = client.string.lower('FOO') future2 = client.string.lower('FOO_JJ') yield async_sleep(self.io_loop, .01) future3 = server.send_to(client_id).string.lower('ABC') result = yield future result2 = yield future2 result3 = yield future3 assert result == 'foo' assert result2 == 'foo_jj' assert result3 == 'abc' server.stop() client.stop()
def test_client_can_reconnect(self): from pseud import Client, Server server_id = b'server' endpoint = b'tcp://127.0.0.1:8989' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, io_loop=self.io_loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE yield server.start() yield client.start() server.register_rpc(name='string.upper')(str.upper) result = yield client.string.upper('hello') assert result == 'HELLO' client.disconnect(endpoint) client.connect(endpoint) result = yield client.string.upper('hello2') assert result == 'HELLO2' client.stop() server.stop()
def test_client_can_reconnect(self): from pseud import Client, Server from pseud._tornado import async_sleep server_id = b'server' endpoint = b'tcp://127.0.0.1:8989' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, io_loop=self.io_loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE yield server.start() yield client.start() server.register_rpc(name='string.upper')(str.upper) result = yield client.string.upper('hello') assert result == 'HELLO' client.disconnect(endpoint) client.connect(endpoint) yield async_sleep(self.io_loop, .1) result = yield client.string.upper('hello2') assert result == 'HELLO2' client.stop() server.stop()
def test_untrusted_curve_with_wrong_password(self): from pseud import Client, Server from pseud.interfaces import UnauthorizedError from pseud.utils import register_rpc client_id = b'john' server_id = b'server' endpoint = b'tcp://127.0.0.1:8998' server_public, server_secret = zmq.curve_keypair() client_public, client_secret = zmq.curve_keypair() security_plugin = 'untrusted_curve' password = b's3cret!' client = Client(server_id, user_id=client_id, security_plugin=security_plugin, public_key=client_public, secret_key=client_secret, peer_public_key=server_public, password=password, io_loop=self.io_loop) server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE # configure manually authentication backend server.auth_backend.user_map[client_id] = password + b'Looser' yield server.start() yield client.start() register_rpc(name='string.lower')(str.lower) future = client.string.lower(b'IMSCREAMING') with pytest.raises(UnauthorizedError): yield future server.stop() client.stop()
def test_server_can_send_to_trustable_peer_identity(self): """ Uses internal metadata of zmq.Frame.get() to fetch identity of sender """ from pseud import Client, Server server_id = b'server' endpoint = b'tcp://127.0.0.1:8989' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, io_loop=self.io_loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE yield server.start() yield client.start() @server.register_rpc(with_identity=True) def echo(peer_identity, message): return peer_identity, message result = yield client.echo(b'one') if zmq.zmq_version_info() >= (4, 1, 0): assert result == [b'bob', b'one'] else: assert result == [b'', b'one'] server.stop() client.stop()
def test_trusted_curve_with_wrong_peer_public_key(self): from pseud import Client, Server from pseud.utils import register_rpc server_id = b'server' endpoint = 'inproc://{}'.format(__name__).encode() endpoint = b'tcp://127.0.0.1:8998' server_public, server_secret = zmq.curve_keypair() server = Server(server_id, security_plugin='trusted_curve', public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) alice_public, alice_secret = \ server.auth_backend.known_identities[b'alice'] client = Client(server_id, user_id=b'alice', security_plugin='trusted_curve', public_key=alice_public, secret_key=alice_secret, peer_public_key=z85.encode(b'R' * 32), timeout=.5, io_loop=self.io_loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE yield server.start() yield client.start() register_rpc(name='string.lower')(str.lower) with pytest.raises(TimeoutError): yield client.string.lower('BAR') server.stop() client.stop()
def test_trusted_curve(self): from pseud import Client, Server from pseud.utils import register_rpc server_id = b'server' endpoint = b'tcp://127.0.0.1:8998' server_public, server_secret = zmq.curve_keypair() security_plugin = 'trusted_curve' server = Server(server_id, security_plugin=security_plugin, public_key=server_public, secret_key=server_secret, io_loop=self.io_loop) server.bind(endpoint) bob_public, bob_secret = server.auth_backend.known_identities[b'bob'] client = Client(server_id, user_id=b'bob', security_plugin=security_plugin, public_key=bob_public, secret_key=bob_secret, peer_public_key=server_public, io_loop=self.io_loop) client.connect(endpoint) assert server.socket.mechanism == zmq.CURVE assert client.socket.mechanism == zmq.CURVE yield server.start() yield client.start() register_rpc(name='string.lower')(str.lower) result = yield client.string.lower('FOO') assert result == 'foo' server.stop() client.stop()
def make_one_server(user_id, endpoint, loop): from pseud import Server server = Server(user_id, loop=loop) server.bind(endpoint) return server
def make_one_server(self, user_id, endpoint): from pseud import Server server = Server(user_id, io_loop=self.io_loop) server.bind(endpoint) return server
async def test_server_creation(): from pseud import Server user_id = b'echo' server = Server(user_id) assert server.user_id == user_id assert server.security_plugin == 'noop_auth_backend'