def testSecureNoCert(self): handler = grpc.method_handlers_generic_handler('test', { 'UnaryUnary': grpc.unary_unary_rpc_method_handler(handle_unary_unary) }) server = test_common.test_server() server.add_generic_rpc_handlers((handler, )) server_cred = grpc.ssl_server_credentials(_SERVER_CERTS) port = server.add_secure_port('[::]:0', server_cred) server.start() channel_creds = grpc.ssl_channel_credentials( root_certificates=_TEST_ROOT_CERTIFICATES) channel = grpc.secure_channel('localhost:{}'.format(port), channel_creds, options=_PROPERTY_OPTIONS) response = channel.unary_unary(_UNARY_UNARY)(_REQUEST) channel.close() server.stop(None) auth_data = pickle.loads(response) self.assertIsNone(auth_data[_ID]) self.assertIsNone(auth_data[_ID_KEY]) self.assertDictEqual( { 'security_level': (b'TSI_PRIVACY_AND_INTEGRITY', ), 'transport_security_type': (b'ssl', ), 'ssl_session_reused': (b'false', ), }, auth_data[_AUTH_CTX])
def setUp(self): self._server = test_common.test_server() port = self._server.add_insecure_port('[::]:0') self._server.add_generic_rpc_handlers((_GenericHandler(), )) self._server.start() self._channel = grpc.insecure_channel('localhost:%d' % port)
def testSecureClientCert(self): handler = grpc.method_handlers_generic_handler('test', { 'UnaryUnary': grpc.unary_unary_rpc_method_handler(handle_unary_unary) }) server = test_common.test_server() server.add_generic_rpc_handlers((handler, )) server_cred = grpc.ssl_server_credentials( _SERVER_CERTS, root_certificates=_TEST_ROOT_CERTIFICATES, require_client_auth=True) port = server.add_secure_port('[::]:0', server_cred) server.start() channel_creds = grpc.ssl_channel_credentials( root_certificates=_TEST_ROOT_CERTIFICATES, private_key=_PRIVATE_KEY, certificate_chain=_CERTIFICATE_CHAIN) channel = grpc.secure_channel('localhost:{}'.format(port), channel_creds, options=_PROPERTY_OPTIONS) response = channel.unary_unary(_UNARY_UNARY)(_REQUEST) channel.close() server.stop(None) auth_data = pickle.loads(response) auth_ctx = auth_data[_AUTH_CTX] six.assertCountEqual(self, _CLIENT_IDS, auth_data[_ID]) self.assertEqual('x509_subject_alternative_name', auth_data[_ID_KEY]) self.assertSequenceEqual([b'ssl'], auth_ctx['transport_security_type']) self.assertSequenceEqual([b'*.test.google.com'], auth_ctx['x509_common_name'])
def setUp(self): self._server = test_common.test_server() self._server.add_generic_rpc_handlers( (_GenericHandler(weakref.proxy(self)), )) port = self._server.add_insecure_port('[::]:0') self._server.start() self._channel = grpc.insecure_channel('localhost:%d' % port, options=_CHANNEL_ARGS)
def setUp(self): self._control = test_control.PauseFailControl() self._thread_pool = thread_pool.RecordingThreadPool(max_workers=None) self._handler = _Handler(self._control, self._thread_pool) self._server = test_common.test_server() port = self._server.add_insecure_port("[::]:0") self._server.add_generic_rpc_handlers((_GenericHandler(self._handler),)) self._server.start() self._channel = grpc.insecure_channel("localhost:%d" % port)
def test_call_wait_for_ready_enabled(self): # To test the wait mechanism, Python thread is required to make # client set up first without handling them case by case. # Also, Python thread don't pass the unhandled exceptions to # main thread. So, it need another method to store the # exceptions and raise them again in main thread. unhandled_exceptions = queue.Queue() with bound_socket(listen=False) as (host, port): addr = '{}:{}'.format(host, port) wg = test_common.WaitGroup(len(_ALL_CALL_CASES)) def wait_for_transient_failure(channel_connectivity): if channel_connectivity == grpc.ChannelConnectivity.TRANSIENT_FAILURE: wg.done() def test_call(perform_call): with grpc.insecure_channel(addr) as channel: try: channel.subscribe(wait_for_transient_failure) perform_call(channel, wait_for_ready=True) except BaseException as e: # pylint: disable=broad-except # If the call failed, the thread would be destroyed. The # channel object can be collected before calling the # callback, which will result in a deadlock. wg.done() unhandled_exceptions.put(e, True) test_threads = [] for perform_call in _ALL_CALL_CASES: test_thread = threading.Thread(target=test_call, args=(perform_call, )) test_thread.exception = None test_thread.start() test_threads.append(test_thread) # Start the server after the connections are waiting wg.wait() server = test_common.test_server(reuse_port=True) server.add_generic_rpc_handlers( (_GenericHandler(weakref.proxy(self)), )) server.add_insecure_port(addr) server.start() for test_thread in test_threads: test_thread.join() # Stop the server to make test end properly server.stop(0) if not unhandled_exceptions.empty(): raise unhandled_exceptions.get(True)
def testInsecure(self): handler = grpc.method_handlers_generic_handler('test', { 'UnaryUnary': grpc.unary_unary_rpc_method_handler(handle_unary_unary) }) server = test_common.test_server() server.add_generic_rpc_handlers((handler, )) port = server.add_insecure_port('[::]:0') server.start() with grpc.insecure_channel('localhost:%d' % port) as channel: response = channel.unary_unary(_UNARY_UNARY)(_REQUEST) server.stop(None) auth_data = pickle.loads(response) self.assertIsNone(auth_data[_ID]) self.assertIsNone(auth_data[_ID_KEY]) self.assertDictEqual({}, auth_data[_AUTH_CTX])
def setUp(self): self._servicer = _Servicer() self._server = test_common.test_server() self._server.add_generic_rpc_handlers( (_generic_handler(self._servicer), )) port = self._server.add_insecure_port('[::]:0') self._server.start() self._channel = grpc.insecure_channel('localhost:{}'.format(port)) self._unary_unary = self._channel.unary_unary( '/'.join(( '', _SERVICE, _UNARY_UNARY, )), request_serializer=_REQUEST_SERIALIZER, response_deserializer=_RESPONSE_DESERIALIZER, ) self._unary_stream = self._channel.unary_stream( '/'.join(( '', _SERVICE, _UNARY_STREAM, )), ) self._stream_unary = self._channel.stream_unary( '/'.join(( '', _SERVICE, _STREAM_UNARY, )), ) self._stream_stream = self._channel.stream_stream( '/'.join(( '', _SERVICE, _STREAM_STREAM, )), request_serializer=_REQUEST_SERIALIZER, response_deserializer=_RESPONSE_DESERIALIZER, )
def setUp(self): self._server = test_common.test_server( max_workers=test_constants.THREAD_CONCURRENCY) self._server.add_generic_rpc_handlers((_GENERIC_HANDLER, )) self._port = self._server.add_insecure_port('[::]:0') self._server.start()