secure_call_test.py
Go to the documentation of this file.
1 # Copyright 2020 The gRPC Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Tests the behaviour of the Call classes under a secure channel."""
15 
16 import logging
17 import unittest
18 
19 import grpc
20 from grpc.experimental import aio
21 
22 from src.proto.grpc.testing import messages_pb2
23 from src.proto.grpc.testing import test_pb2_grpc
24 from tests.unit import resources
25 from tests_aio.unit._test_base import AioTestBase
26 from tests_aio.unit._test_server import start_test_server
27 
28 _SERVER_HOST_OVERRIDE = 'foo.test.google.fr'
29 _NUM_STREAM_RESPONSES = 5
30 _RESPONSE_PAYLOAD_SIZE = 42
31 
32 
34  """A Mixin to run the call tests over a secure channel."""
35 
36  async def setUp(self):
37  server_credentials = grpc.ssl_server_credentials([
38  (resources.private_key(), resources.certificate_chain())
39  ])
40  channel_credentials = grpc.ssl_channel_credentials(
41  resources.test_root_certificates())
42 
43  self._server_address, self._server = await start_test_server(
44  secure=True, server_credentials=server_credentials)
45  channel_options = ((
46  'grpc.ssl_target_name_override',
47  _SERVER_HOST_OVERRIDE,
48  ),)
49  self._channel = aio.secure_channel(self._server_address,
50  channel_credentials, channel_options)
51  self._stub = test_pb2_grpc.TestServiceStub(self._channel)
52 
53  async def tearDown(self):
54  await self._channel.close()
55  await self._server.stop(None)
56 
57 
59  """unary_unary Calls made over a secure channel."""
60 
62  call = self._stub.UnaryCall(messages_pb2.SimpleRequest())
63  response = await call
64  self.assertIsInstance(response, messages_pb2.SimpleResponse)
65  self.assertEqual(await call.code(), grpc.StatusCode.OK)
66 
67  async def test_call_with_credentials(self):
68  call_credentials = grpc.composite_call_credentials(
71  )
72  call = self._stub.UnaryCall(messages_pb2.SimpleRequest(),
73  credentials=call_credentials)
74  response = await call
75 
76  self.assertIsInstance(response, messages_pb2.SimpleResponse)
77 
78 
80  """unary_stream calls over a secure channel"""
81 
84  request.response_parameters.extend(
85  messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)
86  for _ in range(_NUM_STREAM_RESPONSES))
87  call_credentials = grpc.composite_call_credentials(
90  )
91  call = self._stub.StreamingOutputCall(request,
92  credentials=call_credentials)
93 
94  async for response in call:
95  self.assertIsInstance(response,
96  messages_pb2.StreamingOutputCallResponse)
97  self.assertEqual(len(response.payload.body), _RESPONSE_PAYLOAD_SIZE)
98 
99  self.assertEqual(await call.code(), grpc.StatusCode.OK)
100 
101 
102 # Prepares the request that stream in a ping-pong manner.
103 _STREAM_OUTPUT_REQUEST_ONE_RESPONSE = messages_pb2.StreamingOutputCallRequest()
104 _STREAM_OUTPUT_REQUEST_ONE_RESPONSE.response_parameters.append(
105  messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE))
106 
107 
109  _STREAM_ITERATIONS = 2
110 
112 
113  async def request_generator():
114  for _ in range(self._STREAM_ITERATIONS):
115  yield _STREAM_OUTPUT_REQUEST_ONE_RESPONSE
116 
117  call_credentials = grpc.composite_call_credentials(
120  )
121 
122  call = self._stub.FullDuplexCall(request_generator(),
123  credentials=call_credentials)
124  async for response in call:
125  self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body))
126 
127  self.assertEqual(await call.code(), grpc.StatusCode.OK)
128 
129 
130 if __name__ == '__main__':
131  logging.basicConfig(level=logging.DEBUG)
132  unittest.main(verbosity=2)
messages_pb2.SimpleRequest
SimpleRequest
Definition: messages_pb2.py:597
tests_aio.unit.secure_call_test.TestUnaryUnarySecureCall
Definition: secure_call_test.py:58
tests_aio.unit._test_server
Definition: tests_aio/unit/_test_server.py:1
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
tests_aio.unit._test_server.start_test_server
def start_test_server(port=0, secure=False, server_credentials=None, interceptors=None)
Definition: tests_aio/unit/_test_server.py:128
tests_aio.unit._test_base
Definition: _test_base.py:1
tests_aio.unit.secure_call_test.TestStreamStreamSecureCall.test_async_generator_secure_channel
def test_async_generator_secure_channel(self)
Definition: secure_call_test.py:111
grpc.composite_call_credentials
def composite_call_credentials(*call_credentials)
Definition: src/python/grpcio/grpc/__init__.py:1676
tests_aio.unit.secure_call_test.TestUnaryStreamSecureCall
Definition: secure_call_test.py:79
tests_aio.unit.secure_call_test.TestUnaryUnarySecureCall.test_call_ok_over_secure_channel
def test_call_ok_over_secure_channel(self)
Definition: secure_call_test.py:61
tests_aio.unit.secure_call_test.TestStreamStreamSecureCall
Definition: secure_call_test.py:108
tests_aio.unit.secure_call_test._SecureCallMixin._server
_server
Definition: secure_call_test.py:43
tests_aio.unit.secure_call_test._SecureCallMixin
Definition: secure_call_test.py:33
grpc::experimental
Definition: include/grpcpp/channel.h:46
grpc.ssl_server_credentials
def ssl_server_credentials(private_key_certificate_chain_pairs, root_certificates=None, require_client_auth=False)
Definition: src/python/grpcio/grpc/__init__.py:1709
tests_aio.unit.secure_call_test._SecureCallMixin._channel
_channel
Definition: secure_call_test.py:49
tests_aio.unit.secure_call_test._SecureCallMixin._stub
_stub
Definition: secure_call_test.py:51
close
#define close
Definition: test-fs.c:48
messages_pb2.ResponseParameters
ResponseParameters
Definition: messages_pb2.py:625
messages_pb2.StreamingOutputCallRequest
StreamingOutputCallRequest
Definition: messages_pb2.py:632
tests_aio.unit.secure_call_test.TestStreamStreamSecureCall._STREAM_ITERATIONS
int _STREAM_ITERATIONS
Definition: secure_call_test.py:109
tests.unit
Definition: src/python/grpcio_tests/tests/unit/__init__.py:1
grpc.ssl_channel_credentials
def ssl_channel_credentials(root_certificates=None, private_key=None, certificate_chain=None)
Definition: src/python/grpcio/grpc/__init__.py:1607
tests_aio.unit.secure_call_test.TestUnaryStreamSecureCall.test_unary_stream_async_generator_secure
def test_unary_stream_async_generator_secure(self)
Definition: secure_call_test.py:82
stop
static const char stop[]
Definition: benchmark-async-pummel.c:35
grpc.access_token_call_credentials
def access_token_call_credentials(access_token)
Definition: src/python/grpcio/grpc/__init__.py:1659
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
tests_aio.unit.secure_call_test._SecureCallMixin.setUp
def setUp(self)
Definition: secure_call_test.py:36
tests_aio.unit._test_base.AioTestBase
Definition: _test_base.py:49


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:15