Debian Codegraph @

src/python/grpcio_tests/tests/http2/negative_http2_client.py

raw
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 """The Python client used to test negative http2 conditions."""
15
16 import argparse
17 import time
18
19 import grpc
20
21 from src.proto.grpc.testing import messages_pb2
22 from src.proto.grpc.testing import test_pb2_grpc
23
24
25 def _validate_payload_type_and_length(response, expected_type, expected_length):
26 if response.payload.type is not expected_type:
27 raise ValueError('expected payload type %s, got %s' %
28 (expected_type, type(response.payload.type)))
29 elif len(response.payload.body) != expected_length:
30 raise ValueError('expected payload body size %d, got %d' %
31 (expected_length, len(response.payload.body)))
32
33
34 def _expect_status_code(call, expected_code):
35 if call.code() != expected_code:
36 raise ValueError('expected code %s, got %s' %
37 (expected_code, call.code()))
38
39
40 def _expect_status_details(call, expected_details):
41 if call.details() != expected_details:
42 raise ValueError('expected message %s, got %s' %
43 (expected_details, call.details()))
44
45
46 def _validate_status_code_and_details(call, expected_code, expected_details):
47 _expect_status_code(call, expected_code)
48 _expect_status_details(call, expected_details)
49
50
51 # common requests
52 _REQUEST_SIZE = 314159
53 _RESPONSE_SIZE = 271828
54
55 _SIMPLE_REQUEST = messages_pb2.SimpleRequest(
56 response_type=messages_pb2.COMPRESSABLE,
57 response_size=_RESPONSE_SIZE,
58 payload=messages_pb2.Payload(body=b'\x00' * _REQUEST_SIZE))
59
60
61 def _goaway(stub):
62 first_response = stub.UnaryCall(_SIMPLE_REQUEST)
63 _validate_payload_type_and_length(first_response, messages_pb2.COMPRESSABLE,
64 _RESPONSE_SIZE)
65 time.sleep(1)
66 second_response = stub.UnaryCall(_SIMPLE_REQUEST)
67 _validate_payload_type_and_length(second_response,
68 messages_pb2.COMPRESSABLE, _RESPONSE_SIZE)
69
70
71 def _rst_after_header(stub):
72 resp_future = stub.UnaryCall.future(_SIMPLE_REQUEST)
73 _validate_status_code_and_details(resp_future, grpc.StatusCode.INTERNAL,
74 "Received RST_STREAM with error code 0")
75
76
77 def _rst_during_data(stub):
78 resp_future = stub.UnaryCall.future(_SIMPLE_REQUEST)
79 _validate_status_code_and_details(resp_future, grpc.StatusCode.INTERNAL,
80 "Received RST_STREAM with error code 0")
81
82
83 def _rst_after_data(stub):
84 resp_future = stub.UnaryCall.future(_SIMPLE_REQUEST)
85 _validate_status_code_and_details(resp_future, grpc.StatusCode.INTERNAL,
86 "Received RST_STREAM with error code 0")
87
88
89 def _ping(stub):
90 response = stub.UnaryCall(_SIMPLE_REQUEST)
91 _validate_payload_type_and_length(response, messages_pb2.COMPRESSABLE,
92 _RESPONSE_SIZE)
93
94
95 def _max_streams(stub):
96 # send one req to ensure server sets MAX_STREAMS
97 response = stub.UnaryCall(_SIMPLE_REQUEST)
98 _validate_payload_type_and_length(response, messages_pb2.COMPRESSABLE,
99 _RESPONSE_SIZE)
100
101 # give the streams a workout
102 futures = []
103 for _ in range(15):
104 futures.append(stub.UnaryCall.future(_SIMPLE_REQUEST))
105 for future in futures:
106 _validate_payload_type_and_length(future.result(),
107 messages_pb2.COMPRESSABLE,
108 _RESPONSE_SIZE)
109
110
111 def _run_test_case(test_case, stub):
112 if test_case == 'goaway':
113 _goaway(stub)
114 elif test_case == 'rst_after_header':
115 _rst_after_header(stub)
116 elif test_case == 'rst_during_data':
117 _rst_during_data(stub)
118 elif test_case == 'rst_after_data':
119 _rst_after_data(stub)
120 elif test_case == 'ping':
121 _ping(stub)
122 elif test_case == 'max_streams':
123 _max_streams(stub)
124 else:
125 raise ValueError("Invalid test case: %s" % test_case)
126
127
128 def _args():
129 parser = argparse.ArgumentParser()
130 parser.add_argument('--server_host',
131 help='the host to which to connect',
132 type=str,
133 default="127.0.0.1")
134 parser.add_argument('--server_port',
135 help='the port to which to connect',
136 type=int,
137 default="8080")
138 parser.add_argument('--test_case',
139 help='the test case to execute',
140 type=str,
141 default="goaway")
142 return parser.parse_args()
143
144
145 def _stub(server_host, server_port):
146 target = '{}:{}'.format(server_host, server_port)
147 channel = grpc.insecure_channel(target)
148 grpc.channel_ready_future(channel).result()
149 return test_pb2_grpc.TestServiceStub(channel)
150
151
152 def main():
153 args = _args()
154 stub = _stub(args.server_host, args.server_port)
155 _run_test_case(args.test_case, stub)
156
157
158 if __name__ == '__main__':
159 main()