| 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 |
|
| 33 |
class _SecureCallMixin: |
| 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 |
|
| 58 |
class TestUnaryUnarySecureCall(_SecureCallMixin, AioTestBase): |
| 59 |
"""unary_unary Calls made over a secure channel.""" |
| 60 |
|
| 61 |
async def test_call_ok_over_secure_channel(self): |
| 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( |
| 69 |
grpc.access_token_call_credentials("abc"), |
| 70 |
grpc.access_token_call_credentials("def"), |
| 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 |
|
| 79 |
class TestUnaryStreamSecureCall(_SecureCallMixin, AioTestBase): |
| 80 |
"""unary_stream calls over a secure channel""" |
| 81 |
|
| 82 |
async def test_unary_stream_async_generator_secure(self): |
| 83 |
request = messages_pb2.StreamingOutputCallRequest() |
| 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( |
| 88 |
grpc.access_token_call_credentials("abc"), |
| 89 |
grpc.access_token_call_credentials("def"), |
| 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 |
|
| 108 |
class TestStreamStreamSecureCall(_SecureCallMixin, AioTestBase): |
| 109 |
_STREAM_ITERATIONS = 2 |
| 110 |
|
| 111 |
async def test_async_generator_secure_channel(self): |
| 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( |
| 118 |
grpc.access_token_call_credentials("abc"), |
| 119 |
grpc.access_token_call_credentials("def"), |
| 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) |