xref: /aosp_15_r20/external/grpc-grpc/src/python/grpcio_tests/tests/unit/_invalid_metadata_test.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2016 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"""Test of RPCs made against gRPC Python's application-layer API."""
15
16import logging
17import unittest
18
19import grpc
20
21from tests.unit.framework.common import test_constants
22
23_SERIALIZE_REQUEST = lambda bytestring: bytestring * 2
24_DESERIALIZE_REQUEST = lambda bytestring: bytestring[len(bytestring) // 2 :]
25_SERIALIZE_RESPONSE = lambda bytestring: bytestring * 3
26_DESERIALIZE_RESPONSE = lambda bytestring: bytestring[: len(bytestring) // 3]
27
28_UNARY_UNARY = "/test/UnaryUnary"
29_UNARY_STREAM = "/test/UnaryStream"
30_STREAM_UNARY = "/test/StreamUnary"
31_STREAM_STREAM = "/test/StreamStream"
32
33
34def _unary_unary_multi_callable(channel):
35    return channel.unary_unary(_UNARY_UNARY, _registered_method=True)
36
37
38def _unary_stream_multi_callable(channel):
39    return channel.unary_stream(
40        _UNARY_STREAM,
41        request_serializer=_SERIALIZE_REQUEST,
42        response_deserializer=_DESERIALIZE_RESPONSE,
43        _registered_method=True,
44    )
45
46
47def _stream_unary_multi_callable(channel):
48    return channel.stream_unary(
49        _STREAM_UNARY,
50        request_serializer=_SERIALIZE_REQUEST,
51        response_deserializer=_DESERIALIZE_RESPONSE,
52        _registered_method=True,
53    )
54
55
56def _stream_stream_multi_callable(channel):
57    return channel.stream_stream(
58        _STREAM_STREAM,
59        _registered_method=True,
60    )
61
62
63class InvalidMetadataTest(unittest.TestCase):
64    def setUp(self):
65        self._channel = grpc.insecure_channel("localhost:8080")
66        self._unary_unary = _unary_unary_multi_callable(self._channel)
67        self._unary_stream = _unary_stream_multi_callable(self._channel)
68        self._stream_unary = _stream_unary_multi_callable(self._channel)
69        self._stream_stream = _stream_stream_multi_callable(self._channel)
70
71    def tearDown(self):
72        self._channel.close()
73
74    def testUnaryRequestBlockingUnaryResponse(self):
75        request = b"\x07\x08"
76        metadata = (("InVaLiD", "UnaryRequestBlockingUnaryResponse"),)
77        expected_error_details = "metadata was invalid: %s" % metadata
78        with self.assertRaises(ValueError) as exception_context:
79            self._unary_unary(request, metadata=metadata)
80        self.assertIn(expected_error_details, str(exception_context.exception))
81
82    def testUnaryRequestBlockingUnaryResponseWithCall(self):
83        request = b"\x07\x08"
84        metadata = (("InVaLiD", "UnaryRequestBlockingUnaryResponseWithCall"),)
85        expected_error_details = "metadata was invalid: %s" % metadata
86        with self.assertRaises(ValueError) as exception_context:
87            self._unary_unary.with_call(request, metadata=metadata)
88        self.assertIn(expected_error_details, str(exception_context.exception))
89
90    def testUnaryRequestFutureUnaryResponse(self):
91        request = b"\x07\x08"
92        metadata = (("InVaLiD", "UnaryRequestFutureUnaryResponse"),)
93        expected_error_details = "metadata was invalid: %s" % metadata
94        with self.assertRaises(ValueError) as exception_context:
95            self._unary_unary.future(request, metadata=metadata)
96
97    def testUnaryRequestStreamResponse(self):
98        request = b"\x37\x58"
99        metadata = (("InVaLiD", "UnaryRequestStreamResponse"),)
100        expected_error_details = "metadata was invalid: %s" % metadata
101        with self.assertRaises(ValueError) as exception_context:
102            self._unary_stream(request, metadata=metadata)
103        self.assertIn(expected_error_details, str(exception_context.exception))
104
105    def testStreamRequestBlockingUnaryResponse(self):
106        request_iterator = (
107            b"\x07\x08" for _ in range(test_constants.STREAM_LENGTH)
108        )
109        metadata = (("InVaLiD", "StreamRequestBlockingUnaryResponse"),)
110        expected_error_details = "metadata was invalid: %s" % metadata
111        with self.assertRaises(ValueError) as exception_context:
112            self._stream_unary(request_iterator, metadata=metadata)
113        self.assertIn(expected_error_details, str(exception_context.exception))
114
115    def testStreamRequestBlockingUnaryResponseWithCall(self):
116        request_iterator = (
117            b"\x07\x08" for _ in range(test_constants.STREAM_LENGTH)
118        )
119        metadata = (("InVaLiD", "StreamRequestBlockingUnaryResponseWithCall"),)
120        expected_error_details = "metadata was invalid: %s" % metadata
121        multi_callable = _stream_unary_multi_callable(self._channel)
122        with self.assertRaises(ValueError) as exception_context:
123            multi_callable.with_call(request_iterator, metadata=metadata)
124        self.assertIn(expected_error_details, str(exception_context.exception))
125
126    def testStreamRequestFutureUnaryResponse(self):
127        request_iterator = (
128            b"\x07\x08" for _ in range(test_constants.STREAM_LENGTH)
129        )
130        metadata = (("InVaLiD", "StreamRequestFutureUnaryResponse"),)
131        expected_error_details = "metadata was invalid: %s" % metadata
132        with self.assertRaises(ValueError) as exception_context:
133            self._stream_unary.future(request_iterator, metadata=metadata)
134        self.assertIn(expected_error_details, str(exception_context.exception))
135
136    def testStreamRequestStreamResponse(self):
137        request_iterator = (
138            b"\x07\x08" for _ in range(test_constants.STREAM_LENGTH)
139        )
140        metadata = (("InVaLiD", "StreamRequestStreamResponse"),)
141        expected_error_details = "metadata was invalid: %s" % metadata
142        with self.assertRaises(ValueError) as exception_context:
143            self._stream_stream(request_iterator, metadata=metadata)
144        self.assertIn(expected_error_details, str(exception_context.exception))
145
146    def testInvalidMetadata(self):
147        self.assertRaises(TypeError, self._unary_unary, b"", metadata=42)
148
149
150if __name__ == "__main__":
151    logging.basicConfig()
152    unittest.main(verbosity=2)
153