Esempio n. 1
0
    def test_twisted_server_tls_non_tls_client_fail(self):
        server = ServerRunner('../examples/servertls.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            with self.assertRaises(NetworkError) as cm:
                client.rpc_call('echo', 'Hello Server')

            self.assertEqual(cm.exception.real_exception, "Non-JSON content received")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 2
0
    def test_twisted_server_http(self):
        server = ServerRunner('../examples/serverhttp.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()

        try:
            result = client.rpc_call('echo', 'Hello Server')

            self.assertEqual(result, 'Hello Server')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 3
0
    def test_twisted_server_http(self):
        server = ServerRunner('../examples/serverhttp.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()

        try:
            result = client.rpc_call('echo', 'Hello Server')

            self.assertEqual(result, 'Hello Server')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 4
0
    def test_twisted_server_tls_non_tls_client_fail(self):
        server = ServerRunner('../examples/servertls.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            with self.assertRaises(NetworkError) as cm:
                client.rpc_call('echo', 'Hello Server')

            self.assertEqual(cm.exception.real_exception,
                             "Non-JSON content received")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 5
0
    def test_unix_socket(self):
        server = ServerRunner('../examples/serverunixsocket.py',
                '/tmp/reflectrpc.sock')
        server.run()

        client = RpcClient('unix:///tmp/reflectrpc.sock', 0)

        try:
            result = client.rpc_call('echo', 'Hello Server')
            client.close_connection()

            self.assertEqual(result, 'Hello Server')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 6
0
    def test_unix_socket(self):
        server = ServerRunner('../examples/serverunixsocket.py',
                              '/tmp/reflectrpc.sock')
        server.run()

        client = RpcClient('unix:///tmp/reflectrpc.sock', 0)

        try:
            result = client.rpc_call('echo', 'Hello Server')
            client.close_connection()

            self.assertEqual(result, 'Hello Server')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 7
0
    def test_client_http_invalid_answer(self):
        server = FakeServer('localhost', 5500)
        server.add_reply('{"error": null, "result": "Hello Server", "id": 1}')
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()

        try:
            with self.assertRaises(HttpException) as cm:
                client.rpc_call('echo', 'Hello Server')

            self.assertEqual(cm.exception.message, "Received invalid HTTP response: Couldn't find a HTTP header")
            server.stop()
        finally:
            client.close_connection()
Esempio n. 8
0
    def test_twisted_server_http_basic_auth_no_password(self):
        server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()

        try:
            with self.assertRaises(HttpException) as cm:
                client.rpc_call('is_authenticated')

            self.assertEqual(cm.exception.message, "Expected status code '200' but got '401'")
            self.assertEqual(cm.exception.status, '401')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 9
0
    def test_twisted_server_http_basic_auth(self):
        server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()
        client.enable_http_basic_auth('testuser', '123456')

        try:
            authenticated = client.rpc_call('is_authenticated')
            username = client.rpc_call('get_username')

            self.assertEqual(authenticated, True)
            self.assertEqual(username, 'testuser')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 10
0
    def test_twisted_server_tls_client_auth(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt', False)
            client.enable_client_auth('../examples/certs/client.crt',
                    '../examples/certs/client.key')

            result = client.rpc_call('echo', 'Hello Server')

            self.assertEqual(result, "Hello Server")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 11
0
    def test_twisted_server_http_basic_auth_no_password(self):
        server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()

        try:
            with self.assertRaises(HttpException) as cm:
                client.rpc_call('is_authenticated')

            self.assertEqual(cm.exception.message,
                             "Expected status code '200' but got '401'")
            self.assertEqual(cm.exception.status, '401')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 12
0
    def test_client_simple(self):
        server = FakeServer('localhost', 5500)
        server.add_reply('{"error": null, "result": "Hello Server", "id": 1}')
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            result = client.rpc_call('echo', 'Hello Server')
            server.stop()
            request = server.requests.pop()

            expected = {'method': 'echo', 'params': ['Hello Server'], 'id': 1}
            self.assertEqual(json.loads(request), expected)
            self.assertEqual(result, 'Hello Server')
        finally:
            client.close_connection()
Esempio n. 13
0
    def test_twisted_server_http_basic_auth(self):
        server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()
        client.enable_http_basic_auth('testuser', '123456')

        try:
            authenticated = client.rpc_call('is_authenticated')
            username = client.rpc_call('get_username')

            self.assertEqual(authenticated, True)
            self.assertEqual(username, 'testuser')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 14
0
    def test_twisted_server_tls_hostname_check(self):
        server = ServerRunner('../examples/servertls.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            self.assertEqual(str(cm.exception.real_exception),
                    "TLSHostnameError: Host name 'localhost' doesn't match certificate host 'reflectrpc'")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 15
0
    def test_twisted_server_tls_client_auth(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt', False)
            client.enable_client_auth('../examples/certs/client.crt',
                                      '../examples/certs/client.key')

            result = client.rpc_call('echo', 'Hello Server')

            self.assertEqual(result, "Hello Server")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 16
0
    def test_client_simple(self):
        server = FakeServer('localhost', 5500)
        server.add_reply('{"error": null, "result": "Hello Server", "id": 1}')
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            result = client.rpc_call('echo', 'Hello Server')
            server.stop()
            request = server.requests.pop()

            expected = {'method': 'echo', 'params': ['Hello Server'], 'id': 1}
            self.assertEqual(json.loads(request), expected)
            self.assertEqual(result, 'Hello Server')
        finally:
            client.close_connection()
Esempio n. 17
0
    def test_client_http_invalid_answer(self):
        server = FakeServer('localhost', 5500)
        server.add_reply('{"error": null, "result": "Hello Server", "id": 1}')
        server.run()

        client = RpcClient('localhost', 5500)
        client.enable_http()

        try:
            with self.assertRaises(HttpException) as cm:
                client.rpc_call('echo', 'Hello Server')

            self.assertEqual(
                cm.exception.message,
                "Received invalid HTTP response: Couldn't find a HTTP header")
            server.stop()
        finally:
            client.close_connection()
Esempio n. 18
0
    def test_twisted_server_tls_client_auth_wrong_client_cert(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt', False)
            client.enable_client_auth('../examples/certs/wrong-client.crt',
                    '../examples/certs/wrong-client.key')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            self.assertTrue('alert decrypt error' in str(cm.exception.real_exception))
        finally:
            client.close_connection()
            server.stop()
Esempio n. 19
0
    def test_concurrency_error_handling(self):
        server = ServerRunner('../examples/concurrency.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            with self.assertRaises(RpcError) as cm:
                client.rpc_call('deferred_error')
            self.assertEqual(cm.exception.json['name'], "JsonRpcError")
            self.assertEqual(cm.exception.json['message'], "You wanted an error, here you have it!")

            with self.assertRaises(RpcError) as cm:
                result = client.rpc_call('deferred_internal_error')
            self.assertEqual(cm.exception.json['name'], "InternalError")
            self.assertEqual(cm.exception.json['message'], "Internal error")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 20
0
    def test_twisted_server_tls_client_auth_username(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt', False)
            client.enable_client_auth('../examples/certs/client.crt',
                                      '../examples/certs/client.key')

            authenticated = client.rpc_call('is_authenticated')
            username = client.rpc_call('get_username')

            self.assertEqual(authenticated, True)
            self.assertEqual(username, 'example-username')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 21
0
    def test_twisted_server_tls_client_auth_wrong_client_cert(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt', False)
            client.enable_client_auth('../examples/certs/wrong-client.crt',
                                      '../examples/certs/wrong-client.key')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            self.assertTrue(
                'alert decrypt error' in str(cm.exception.real_exception))
        finally:
            client.close_connection()
            server.stop()
Esempio n. 22
0
    def test_twisted_server_tls_hostname_check(self):
        server = ServerRunner('../examples/servertls.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            self.assertEqual(
                str(cm.exception.real_exception),
                "TLSHostnameError: Host name 'localhost' doesn't match certificate host 'reflectrpc'"
            )
        finally:
            client.close_connection()
            server.stop()
Esempio n. 23
0
    def test_twisted_server_tls_client_auth_no_client_cert(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            python2_check = 'alert handshake failure' in str(cm.exception.real_exception)
            python3_check = str(cm.exception.real_exception).startswith(
                    "[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE]")
            self.assertTrue(python2_check or python3_check)
        finally:
            client.close_connection()
            server.stop()
Esempio n. 24
0
    def test_twisted_server_tls_client_auth_username(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt', False)
            client.enable_client_auth('../examples/certs/client.crt',
                    '../examples/certs/client.key')

            authenticated = client.rpc_call('is_authenticated')
            username = client.rpc_call('get_username')

            self.assertEqual(authenticated, True)
            self.assertEqual(username, 'example-username')
        finally:
            client.close_connection()
            server.stop()
Esempio n. 25
0
    def test_twisted_server_tls_client_auth_no_client_cert(self):
        server = ServerRunner('../examples/servertls_clientauth.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/rootCA.crt')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            python2_check = 'alert handshake failure' in str(
                cm.exception.real_exception)
            python3_check = str(cm.exception.real_exception).startswith(
                "[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE]")
            self.assertTrue(python2_check or python3_check)
        finally:
            client.close_connection()
            server.stop()
Esempio n. 26
0
    def test_twisted_server_tls_server_check(self):
        server = ServerRunner('../examples/servertls.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/wrongCA.crt')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            python2_check = str(
                cm.exception.real_exception).startswith('[Errno 1]')
            python3_check = str(cm.exception.real_exception).startswith(
                '[SSL: CERTIFICATE_VERIFY_FAILED]')
            self.assertTrue(python2_check or python3_check)
        finally:
            client.close_connection()
            server.stop()
Esempio n. 27
0
    def test_twisted_server_tls_server_check(self):
        server = ServerRunner('../examples/servertls.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            client.enable_tls('../examples/certs/wrongCA.crt')

            with self.assertRaises(NetworkError) as cm:
                result = client.rpc_call('echo', 'Hello Server')

            python2_check = str(cm.exception.real_exception).startswith(
                    '[Errno 1]')
            python3_check = str(cm.exception.real_exception).startswith(
                    '[SSL: CERTIFICATE_VERIFY_FAILED]')
            self.assertTrue(python2_check or python3_check)
        finally:
            client.close_connection()
            server.stop()
Esempio n. 28
0
    def test_concurrency_error_handling(self):
        server = ServerRunner('../examples/concurrency.py', 5500)
        server.run()

        client = RpcClient('localhost', 5500)

        try:
            with self.assertRaises(RpcError) as cm:
                client.rpc_call('deferred_error')
            self.assertEqual(cm.exception.json['name'], "JsonRpcError")
            self.assertEqual(cm.exception.json['message'],
                             "You wanted an error, here you have it!")

            with self.assertRaises(RpcError) as cm:
                result = client.rpc_call('deferred_internal_error')
            self.assertEqual(cm.exception.json['name'], "InternalError")
            self.assertEqual(cm.exception.json['message'], "Internal error")
        finally:
            client.close_connection()
            server.stop()
Esempio n. 29
0
    def test_concurrency_http(self):
        server = ServerRunner('../examples/concurrency-http.py', 5500)
        server.run()

        client1 = RpcClient('localhost', 5500)
        client1.enable_http()
        client2 = RpcClient('localhost', 5500)
        client2.enable_http()

        results = []

        def t1_func():
            result = client1.rpc_call('slow_operation')
            results.append(result)

        def t2_func():
            time.sleep(0.5)
            result = client2.rpc_call('fast_operation')
            results.append(result)

        try:
            t1 = threading.Thread(target = t1_func, args = ())
            t1.start()

            t2 = threading.Thread(target = t2_func, args = ())
            t2.start()

            t1.join()
            if t1.is_alive():
                raise RuntimeError("Failed to join on thread 1")
            t2.join()
            if t2.is_alive():
                raise RuntimeError("Failed to join on thread 2")

            # slow_operation (value 42) must finish last
            self.assertEqual(results[0], 41)
            self.assertEqual(results[1], 42)
        finally:
            client1.close_connection()
            client2.close_connection()
            server.stop()
Esempio n. 30
0
    def test_concurrency_http(self):
        server = ServerRunner('../examples/concurrency-http.py', 5500)
        server.run()

        client1 = RpcClient('localhost', 5500)
        client1.enable_http()
        client2 = RpcClient('localhost', 5500)
        client2.enable_http()

        results = []

        def t1_func():
            result = client1.rpc_call('slow_operation')
            results.append(result)

        def t2_func():
            time.sleep(0.5)
            result = client2.rpc_call('fast_operation')
            results.append(result)

        try:
            t1 = threading.Thread(target=t1_func, args=())
            t1.start()

            t2 = threading.Thread(target=t2_func, args=())
            t2.start()

            t1.join()
            if t1.is_alive():
                raise RuntimeError("Failed to join on thread 1")
            t2.join()
            if t2.is_alive():
                raise RuntimeError("Failed to join on thread 2")

            # slow_operation (value 42) must finish last
            self.assertEqual(results[0], 41)
            self.assertEqual(results[1], 42)
        finally:
            client1.close_connection()
            client2.close_connection()
            server.stop()