Exemplo n.º 1
0
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'
Exemplo n.º 2
0
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')
Exemplo n.º 3
0
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'
Exemplo n.º 4
0
def test_server_can_send():
    from pseud.utils import register_rpc

    client_id = 'client'
    server_id = 'server'
    endpoint = 'tcp://127.0.0.1:5000'

    # PLAIN
    server = make_one_server(server_id, security_plugin='plain')

    client = make_one_client(server_id,
                             user_id=client_id,
                             password=client_id,
                             security_plugin='plain')

    server.bind(endpoint)
    server.start()

    client.connect(endpoint)
    client.start()

    import string
    register_rpc(name='string.lower')(string.lower)

    result = client.string.lower('TATA').get()
    assert result == 'tata'

    future = server.send_to(client_id).string.lower('SCREAM')

    assert future.get() == 'scream'
    client.stop()
    server.stop()
Exemplo n.º 5
0
def test_client_can_reconnect():
    from pseud.utils import register_rpc

    server_id = 'server'
    endpoint = 'tcp://127.0.0.1:8989'

    server = make_one_server(server_id)

    client = make_one_client(server_id)

    server.bind(endpoint)
    server.start()

    client.connect(endpoint)

    import string
    register_rpc(name='string.upper')(string.upper)

    future = client.string.upper('hello')
    assert future.get() == 'HELLO'

    client.disconnect(endpoint)
    client.connect(endpoint)
    gevent.sleep(.1)
    future = client.string.upper('hello')
    assert future.get() == 'HELLO'

    client.stop()
    server.stop()
Exemplo n.º 6
0
    def test_server_can_proxy_another_server(self):
        """
        Client1 --> Server1.string.lower()
        Client2 --> Server2(Server1.string.lower())
        """
        from pseud.interfaces import ServiceNotFoundError
        from pseud.utils import get_rpc_callable, register_rpc

        server1 = self.make_one_server(b'server1')
        server2 = self.make_one_server(b'server2', proxy_to=server1)

        client1 = self.make_one_client(b'server1')
        client2 = self.make_one_client(b'server2')

        server1.bind(b'inproc://server1')
        server2.bind(b'inproc://server2')
        client1.connect(b'inproc://server1')
        client2.connect(b'inproc://server2')
        yield server1.start()
        yield server2.start()

        # Local registration
        server1.register_rpc(name='str.lower')(str.lower)

        # Global registration
        register_rpc(name='str.upper')(str.upper)

        # local registration only to proxy
        server2.register_rpc(name='bla.lower')(str.lower)

        with pytest.raises(ServiceNotFoundError):
            get_rpc_callable('str.lower', registry=server2.registry)

        with pytest.raises(ServiceNotFoundError):
            get_rpc_callable('bla.lower', registry=server1.registry)

        with pytest.raises(ServiceNotFoundError):
            get_rpc_callable('bla.lower')

        with pytest.raises(ServiceNotFoundError):
            assert get_rpc_callable('str.lower')

        assert get_rpc_callable('str.lower',
                                registry=server1.registry)('L') == 'l'

        result1 = yield client1.str.lower('SCREAM')
        result2 = yield client2.str.lower('SCREAM')
        result3 = yield client1.str.upper('whisper')
        result4 = yield client2.str.upper('whisper')
        result5 = yield client2.bla.lower('SCREAM')
        assert result1 == 'scream'
        assert result2 == 'scream'
        assert result3 == 'WHISPER'
        assert result4 == 'WHISPER'
        assert result5 == 'scream'

        client1.stop()
        client2.stop()
        server1.stop()
        server2.stop()
Exemplo n.º 7
0
async def test_server_can_send_to_several_client(loop, unused_tcp_port):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = f'tcp://127.0.0.1:{unused_tcp_port}'

    server = make_one_server(server_id, loop, security_plugin='plain')

    client1 = make_one_client(server_id, loop, user_id=b'alice',
                              password=b'alice',
                              security_plugin='plain')
    client2 = make_one_client(server_id, loop, user_id=b'bob',
                              password=b'bob',
                              security_plugin='plain')

    server.bind(endpoint)
    client1.connect(endpoint)
    client2.connect(endpoint)
    register_rpc(name='string.lower')(str.lower)
    async with server, client1, client2:
        await asyncio.sleep(.1)
        result1 = await server.send_to(b'alice').string.lower('SCREAM1')
        result2 = await server.send_to(b'bob').string.lower('SCREAM2')
        assert result1 == 'scream1'
        assert result2 == 'scream2'
Exemplo n.º 8
0
    def test_server_can_send(self):
        from pseud.utils import register_rpc

        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:5000'

        server = self.make_one_server(server_id, security_plugin='plain')

        client = self.make_one_client(server_id,
                                      user_id=b'alice',
                                      password=b'alice',
                                      security_plugin='plain')

        server.bind(endpoint)
        client.connect(endpoint)
        yield server.start()
        yield client.start()

        register_rpc(name='string.lower')(str.lower)
        yield client.string.lower('TATA')

        result = yield server.send_to(b'alice').string.lower('SCREAM')
        assert result == 'scream'
        client.stop()
        server.stop()
Exemplo n.º 9
0
def test_client_can_reconnect():
    from pseud.utils import register_rpc

    server_id = 'server'
    endpoint = 'tcp://127.0.0.1:8989'

    server = make_one_server(server_id)

    client = make_one_client(server_id)

    server.bind(endpoint)
    server.start()

    client.connect(endpoint)

    import string
    register_rpc(name='string.upper')(string.upper)

    future = client.string.upper('hello')
    assert future.get() == 'HELLO'

    client.disconnect(endpoint)
    client.connect(endpoint)
    gevent.sleep(.1)
    future = client.string.upper('hello')
    assert future.get() == 'HELLO'

    client.stop()
    server.stop()
Exemplo n.º 10
0
    def test_client_can_reconnect(self):
        from pseud.utils import register_rpc

        client_id = b'client'
        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:8989'

        server = self.make_one_server(server_id)

        client = self.make_one_client(server_id)

        server.bind(endpoint)
        server.start()

        client.connect(endpoint)

        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('hello')
        assert result == 'HELLO'

        client.stop()
        server.stop()
Exemplo n.º 11
0
def test_server_can_send():
    from pseud.utils import register_rpc

    client_id = 'client'
    server_id = 'server'
    endpoint = 'tcp://127.0.0.1:5000'

    # PLAIN
    server = make_one_server(server_id, security_plugin='plain')

    client = make_one_client(server_id, user_id=client_id,
                             password=client_id,
                             security_plugin='plain')

    server.bind(endpoint)
    server.start()

    client.connect(endpoint)
    client.start()

    import string
    register_rpc(name='string.lower')(string.lower)

    result = client.string.lower('TATA').get()
    assert result == 'tata'

    future = server.send_to(client_id).string.lower('SCREAM')

    assert future.get() == 'scream'
    client.stop()
    server.stop()
Exemplo n.º 12
0
    def test_server_can_proxy_another_server(self):
        """
        Client1 --> Server1.string.lower()
        Client2 --> Server2(Server1.string.lower())
        """
        from pseud.interfaces import ServiceNotFoundError
        from pseud.utils import get_rpc_callable, register_rpc

        server1 = self.make_one_server(b'server1')
        server2 = self.make_one_server(b'server2', proxy_to=server1)

        client1 = self.make_one_client(b'server1')
        client2 = self.make_one_client(b'server2')

        server1.bind(b'inproc://server1')
        server2.bind(b'inproc://server2')
        client1.connect(b'inproc://server1')
        client2.connect(b'inproc://server2')
        yield server1.start()
        yield server2.start()

        # Local registration
        server1.register_rpc(name='str.lower')(str.lower)

        # Global registration
        register_rpc(name='str.upper')(str.upper)

        # local registration only to proxy
        server2.register_rpc(name='bla.lower')(str.lower)

        with pytest.raises(ServiceNotFoundError):
            get_rpc_callable('str.lower', registry=server2.registry)

        with pytest.raises(ServiceNotFoundError):
            get_rpc_callable('bla.lower', registry=server1.registry)

        with pytest.raises(ServiceNotFoundError):
            get_rpc_callable('bla.lower')

        with pytest.raises(ServiceNotFoundError):
            assert get_rpc_callable('str.lower')

        assert get_rpc_callable('str.lower',
                                registry=server1.registry)('L') == 'l'

        result1 = yield client1.str.lower('SCREAM')
        result2 = yield client2.str.lower('SCREAM')
        result3 = yield client1.str.upper('whisper')
        result4 = yield client2.str.upper('whisper')
        result5 = yield client2.bla.lower('SCREAM')
        assert result1 == 'scream'
        assert result2 == 'scream'
        assert result3 == 'WHISPER'
        assert result4 == 'WHISPER'
        assert result5 == 'scream'

        client1.stop()
        client2.stop()
        server1.stop()
        server2.stop()
Exemplo n.º 13
0
def test_rpc_simple_registration():
    from pseud.interfaces import ServiceNotFoundError
    from pseud.utils import (get_rpc_callable,
                             register_rpc,
                             RPCCallable,
                             )

    @register_rpc
    def callme(*args, **kw):
        return args, kw

    assert isinstance(get_rpc_callable('callme'), RPCCallable)
    assert get_rpc_callable('callme')() == ((), {})
    assert get_rpc_callable('callme')('a', foo='goo') == (('a',),
                                                          {'foo': 'goo'})

    @register_rpc(name='totally.something.else')
    def callme2(*args, **kw):
        return args, kw

    with pytest.raises(ServiceNotFoundError):
        get_rpc_callable('callme2')

    assert isinstance(get_rpc_callable('totally.something.else'),
                      RPCCallable)

    def call_me_again():
        return True

    register_rpc(functools.partial(call_me_again), name='call_me_again')
    assert get_rpc_callable('call_me_again')() is True
Exemplo n.º 14
0
def test_trusted_curve():
    from pseud._gevent import Client, Server
    from pseud.utils import register_rpc

    server_id = 'server'
    endpoint = '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)
    server.bind(endpoint)
    bob_public, bob_secret = server.auth_backend.known_identities[b'bob']
    client = Client(server_id,
                    security_plugin=security_plugin,
                    public_key=bob_public,
                    secret_key=bob_secret,
                    peer_public_key=server_public)

    client.connect(endpoint)
    assert server.socket.mechanism == zmq.CURVE
    assert client.socket.mechanism == zmq.CURVE

    server.start()
    client.start()
    import string
    register_rpc(name='string.lower')(string.lower)
    future = client.string.lower('FOO')
    assert future.get() == 'foo'
    server.stop()
    client.stop()
Exemplo n.º 15
0
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')
Exemplo n.º 16
0
    def test_client_can_reconnect(self):
        from pseud.utils import register_rpc

        client_id = b'client'
        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:8989'

        server = self.make_one_server(server_id)

        client = self.make_one_client(server_id)

        server.bind(endpoint)
        server.start()

        client.connect(endpoint)

        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('hello')
        assert result == 'HELLO'

        client.stop()
        server.stop()
Exemplo n.º 17
0
async def test_server_can_send_to_several_client(loop, unused_tcp_port):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = f'tcp://127.0.0.1:{unused_tcp_port}'

    server = make_one_server(server_id, loop, security_plugin='plain')

    client1 = make_one_client(server_id,
                              loop,
                              user_id=b'alice',
                              password=b'alice',
                              security_plugin='plain')
    client2 = make_one_client(server_id,
                              loop,
                              user_id=b'bob',
                              password=b'bob',
                              security_plugin='plain')

    server.bind(endpoint)
    client1.connect(endpoint)
    client2.connect(endpoint)
    register_rpc(name='string.lower')(str.lower)
    async with server, client1, client2:
        await asyncio.sleep(.1)
        result1 = await server.send_to(b'alice').string.lower('SCREAM1')
        result2 = await server.send_to(b'bob').string.lower('SCREAM2')
        assert result1 == 'scream1'
        assert result2 == 'scream2'
Exemplo n.º 18
0
def test_trusted_curve():
    from pseud._gevent import Client, Server
    from pseud.utils import register_rpc

    server_id = 'server'
    endpoint = '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)
    server.bind(endpoint)
    bob_public, bob_secret = server.auth_backend.known_identities[b'bob']
    client = Client(server_id,
                    security_plugin=security_plugin,
                    public_key=bob_public,
                    secret_key=bob_secret,
                    peer_public_key=server_public)

    client.connect(endpoint)
    assert server.socket.mechanism == zmq.CURVE
    assert client.socket.mechanism == zmq.CURVE

    server.start()
    client.start()
    import string
    register_rpc(name='string.lower')(string.lower)
    future = client.string.lower('FOO')
    assert future.get() == 'foo'
    server.stop()
    client.stop()
Exemplo n.º 19
0
    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()
Exemplo n.º 20
0
    def test_untrusted_curve_with_allowed_password(self):
        from pseud import Client, Server
        from pseud.utils import register_rpc

        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)

        result = yield client.string.lower('FOO')
        result2 = yield client.string.lower('FOO_JJ')
        result3 = yield server.send_to(client_id).string.lower('ABC')
        assert result == 'foo'
        assert result2 == 'foo_jj'
        assert result3 == 'abc'
        server.stop()
        client.stop()
Exemplo n.º 21
0
async def test_client_can_send(loop):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = b'inproc://here'

    server = make_one_server(server_id, loop)

    client = make_one_client(server_id, loop)

    server.bind(endpoint)
    client.connect(endpoint)
    register_rpc(name='string.upper')(str.upper)
    async with server, client:
        result = await client.string.upper('hello')
        assert result == 'HELLO'
Exemplo n.º 22
0
async def test_client_can_send(loop):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = b'inproc://here'

    server = make_one_server(server_id, loop)

    client = make_one_client(server_id, loop)

    server.bind(endpoint)
    client.connect(endpoint)
    register_rpc(name='string.upper')(str.upper)
    async with server, client:
        result = await client.string.upper('hello')
        assert result == 'HELLO'
Exemplo n.º 23
0
    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()
Exemplo n.º 24
0
    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()
Exemplo n.º 25
0
def test_untrusted_curve_with_allowed_password_and_client_disconnect():
    from pseud._gevent import Client, Server
    from pseud.utils import register_rpc

    client_id = 'john'
    server_id = 'server'
    endpoint = '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 = '******'

    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)

    server = Server(server_id,
                    security_plugin=security_plugin,
                    public_key=server_public,
                    secret_key=server_secret)

    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.start()
    import string
    register_rpc(name='string.lower')(string.lower)
    future = client.string.lower('FOO')
    assert future.get() == 'foo'
    # Simulate disconnection and reconnection with new identity
    client.socket.disconnect(endpoint)
    client.socket.identity = 'wow-doge'
    client.socket.connect(endpoint)
    gevent.sleep(.1)  # Warmup
    assert client.string.lower('ABC').get() == 'abc'
    server.stop()
    client.stop()
Exemplo n.º 26
0
def test_untrusted_curve_with_allowed_password_and_client_disconnect():
    from pseud._gevent import Client, Server
    from pseud.utils import register_rpc

    client_id = 'john'
    server_id = 'server'
    endpoint = '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 = '******'

    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)

    server = Server(server_id,
                    security_plugin=security_plugin,
                    public_key=server_public,
                    secret_key=server_secret)

    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.start()
    import string
    register_rpc(name='string.lower')(string.lower)
    future = client.string.lower('FOO')
    assert future.get() == 'foo'
    # Simulate disconnection and reconnection with new identity
    client.socket.disconnect(endpoint)
    client.socket.identity = 'wow-doge'
    client.socket.connect(endpoint)
    gevent.sleep(.1)  # Warmup
    assert client.string.lower('ABC').get() == 'abc'
    server.stop()
    client.stop()
Exemplo n.º 27
0
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'
Exemplo n.º 28
0
def test_untrusted_curve_with_allowed_password():
    from pseud._gevent import Client, Server
    from pseud.utils import register_rpc

    client_id = 'john'
    server_id = 'server'
    endpoint = '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 = '******'

    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)

    server = Server(server_id,
                    security_plugin=security_plugin,
                    public_key=server_public,
                    secret_key=server_secret)

    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.start()
    import string
    register_rpc(name='string.lower')(string.lower)
    future = client.string.lower('FOO')
    future2 = client.string.lower('FOO_JJ')
    assert future.get() == 'foo'
    assert future2.get() == 'foo_jj'
    future3 = server.send_to(client_id).string.lower('ABC')
    assert future3.get() == 'abc'
    server.stop()
    client.stop()
Exemplo n.º 29
0
def test_untrusted_curve_with_allowed_password():
    from pseud._gevent import Client, Server
    from pseud.utils import register_rpc

    client_id = 'john'
    server_id = 'server'
    endpoint = '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 = '******'

    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)

    server = Server(server_id,
                    security_plugin=security_plugin,
                    public_key=server_public,
                    secret_key=server_secret)

    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.start()
    import string
    register_rpc(name='string.lower')(string.lower)
    future = client.string.lower('FOO')
    future2 = client.string.lower('FOO_JJ')
    assert future.get() == 'foo'
    assert future2.get() == 'foo_jj'
    future3 = server.send_to(client_id).string.lower('ABC')
    assert future3.get() == 'abc'
    server.stop()
    client.stop()
Exemplo n.º 30
0
async def test_server_can_send(loop, unused_tcp_port, plain_auth_backend):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = f'tcp://127.0.0.1:{unused_tcp_port}'

    server = make_one_server(server_id, loop, security_plugin='plain')

    client = make_one_client(server_id, loop, user_id=b'alice',
                             password=b'alice',
                             security_plugin='plain')

    server.bind(endpoint)
    client.connect(endpoint)
    register_rpc(name='string.lower')(str.lower)
    async with server, client:
        await asyncio.sleep(.1)
        result = await server.send_to(b'alice').string.lower('SCREAM')
        assert result == 'scream'
Exemplo n.º 31
0
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')
Exemplo n.º 32
0
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')
Exemplo n.º 33
0
def test_server_can_send_to_several_client():
    from pseud.utils import register_rpc
    server_id = 'server'
    endpoint = 'tcp://127.0.0.1:5000'

    server = make_one_server(server_id, security_plugin='plain')

    client1 = make_one_client(
        server_id,
        security_plugin='plain',
        user_id='client1',
        password='******',
    )
    client2 = make_one_client(
        server_id,
        security_plugin='plain',
        user_id='client2',
        password='******',
    )

    server.bind(endpoint)
    client1.connect(endpoint)
    client2.connect(endpoint)
    server.start()
    client1.start()
    client2.start()

    import string
    register_rpc(name='string.lower')(string.lower)
    client1.string.lower('TATA').get()
    client2.string.lower('TATA').get()

    future1 = server.send_to('client1').string.lower('SCREAM1')

    future2 = server.send_to('client2').string.lower('SCREAM2')

    assert future1.get() == 'scream1'
    assert future2.get() == 'scream2'
    client1.stop()
    client2.stop()
    server.stop()
Exemplo n.º 34
0
async def test_client_can_reconnect(loop, unused_tcp_port):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = f'tcp://127.0.0.1:{unused_tcp_port}'

    server = make_one_server(server_id, loop)
    client = make_one_client(server_id, loop)

    server.bind(endpoint)
    client.connect(endpoint)
    register_rpc(name='string.upper')(str.upper)
    async with server:
        result = await client.string.upper('hello')
        assert result == 'HELLO'

        client.disconnect(endpoint)
        client.connect(endpoint)

        await asyncio.sleep(.1)
        result = await client.string.upper('hello')
        assert result == 'HELLO'
Exemplo n.º 35
0
async def test_client_can_reconnect(loop, unused_tcp_port):
    from pseud.utils import register_rpc

    server_id = b'server'
    endpoint = f'tcp://127.0.0.1:{unused_tcp_port}'

    server = make_one_server(server_id, loop)
    client = make_one_client(server_id, loop)

    server.bind(endpoint)
    client.connect(endpoint)
    register_rpc(name='string.upper')(str.upper)
    async with server:
        result = await client.string.upper('hello')
        assert result == 'HELLO'

        client.disconnect(endpoint)
        client.connect(endpoint)

        await asyncio.sleep(.1)
        result = await client.string.upper('hello')
        assert result == 'HELLO'
Exemplo n.º 36
0
    def test_client_can_send(self):
        from pseud.utils import register_rpc

        server_id = b'server'
        endpoint = b'inproc://here'

        server = self.make_one_server(server_id)

        client = self.make_one_client(server_id)

        server.bind(endpoint)
        yield server.start()

        client.connect(endpoint)
        yield client.start()

        register_rpc(name='string.upper')(str.upper)

        result = yield client.string.upper('hello')
        assert result == 'HELLO'
        client.stop()
        server.stop()
Exemplo n.º 37
0
def test_client_can_send():
    from pseud.utils import register_rpc
    server_id = 'server'
    endpoint = 'inproc://here'

    server = make_one_server(server_id)

    client = make_one_client(server_id)

    server.bind(endpoint)
    server.start()

    client.connect(endpoint)
    client.start()

    import string
    register_rpc(name='string.upper')(string.upper)

    future = client.string.upper('hello')
    assert future.get() == 'HELLO'
    client.stop()
    server.stop()
Exemplo n.º 38
0
    def test_server_can_send_to_several_client(self):
        from pseud.utils import register_rpc
        from pseud._tornado import async_sleep

        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:5000'

        server = self.make_one_server(server_id, security_plugin='plain')

        client1 = self.make_one_client(server_id,
                                       user_id=b'alice',
                                       password=b'alice',
                                       security_plugin='plain')
        client2 = self.make_one_client(server_id,
                                       user_id=b'bob',
                                       password=b'bob',
                                       security_plugin='plain')

        server.bind(endpoint)
        yield server.start()
        client1.connect(endpoint)
        client2.connect(endpoint)
        yield client1.start()
        yield client2.start()

        register_rpc(name='string.lower')(str.lower)

        # call the server to register
        yield client1.string.lower('TATA')
        yield client2.string.lower('TATA')
        result1 = yield server.send_to(b'alice').string.lower('SCREAM1')

        result2 = yield server.send_to(b'bob').string.lower('SCREAM2')

        assert result1 == 'scream1'
        assert result2 == 'scream2'
        client1.stop()
        client2.stop()
        server.stop()
Exemplo n.º 39
0
    def test_client_can_send(self):
        from pseud.utils import register_rpc

        server_id = b'server'
        endpoint = b'inproc://here'

        server = self.make_one_server(server_id)

        client = self.make_one_client(server_id)

        server.bind(endpoint)
        yield server.start()

        client.connect(endpoint)
        yield client.start()

        register_rpc(name='string.upper')(str.upper)

        result = yield client.string.upper('hello')
        assert result == 'HELLO'
        client.stop()
        server.stop()
Exemplo n.º 40
0
def test_client_can_send():
    from pseud.utils import register_rpc
    server_id = 'server'
    endpoint = 'inproc://here'

    server = make_one_server(server_id)

    client = make_one_client(server_id)

    server.bind(endpoint)
    server.start()

    client.connect(endpoint)
    client.start()

    import string
    register_rpc(name='string.upper')(string.upper)

    future = client.string.upper('hello')
    assert future.get() == 'HELLO'
    client.stop()
    server.stop()
Exemplo n.º 41
0
    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()
Exemplo n.º 42
0
def test_server_can_send_to_several_client():
    from pseud.utils import register_rpc
    server_id = 'server'
    endpoint = 'tcp://127.0.0.1:5000'

    server = make_one_server(server_id, security_plugin='plain')

    client1 = make_one_client(server_id, security_plugin='plain',
                              user_id='client1',
                              password='******',
                              )
    client2 = make_one_client(server_id, security_plugin='plain',
                              user_id='client2',
                              password='******',
                              )

    server.bind(endpoint)
    client1.connect(endpoint)
    client2.connect(endpoint)
    server.start()
    client1.start()
    client2.start()

    import string
    register_rpc(name='string.lower')(string.lower)
    client1.string.lower('TATA').get()
    client2.string.lower('TATA').get()

    future1 = server.send_to('client1').string.lower('SCREAM1')

    future2 = server.send_to('client2').string.lower('SCREAM2')

    assert future1.get() == 'scream1'
    assert future2.get() == 'scream2'
    client1.stop()
    client2.stop()
    server.stop()
Exemplo n.º 43
0
    def test_server_can_send_to_several_client(self):
        from pseud.utils import register_rpc
        from pseud._tornado import async_sleep

        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:5000'

        server = self.make_one_server(server_id, security_plugin='plain')

        client1 = self.make_one_client(server_id, user_id=b'alice',
                                       password=b'alice',
                                       security_plugin='plain')
        client2 = self.make_one_client(server_id, user_id=b'bob',
                                       password=b'bob',
                                       security_plugin='plain')

        server.bind(endpoint)
        yield server.start()
        client1.connect(endpoint)
        client2.connect(endpoint)
        yield client1.start()
        yield client2.start()

        register_rpc(name='string.lower')(str.lower)

        # call the server to register
        yield client1.string.lower('TATA')
        yield client2.string.lower('TATA')
        result1 = yield server.send_to(b'alice').string.lower('SCREAM1')

        result2 = yield server.send_to(b'bob').string.lower('SCREAM2')

        assert result1 == 'scream1'
        assert result2 == 'scream2'
        client1.stop()
        client2.stop()
        server.stop()
Exemplo n.º 44
0
    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()
Exemplo n.º 45
0
    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()
Exemplo n.º 46
0
    def test_server_can_send(self):
        from pseud.utils import register_rpc

        server_id = b'server'
        endpoint = b'tcp://127.0.0.1:5000'

        server = self.make_one_server(server_id, security_plugin='plain')

        client = self.make_one_client(server_id, user_id=b'alice',
                                      password=b'alice',
                                      security_plugin='plain')

        server.bind(endpoint)
        client.connect(endpoint)
        yield server.start()
        yield client.start()

        register_rpc(name='string.lower')(str.lower)
        yield client.string.lower('TATA')

        result = yield server.send_to(b'alice').string.lower('SCREAM')
        assert result == 'scream'
        client.stop()
        server.stop()
Exemplo n.º 47
0
    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()