xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/http2_client.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include "test/cpp/interop/http2_client.h"
20 
21 #include <thread>
22 
23 #include "absl/flags/flag.h"
24 #include "absl/strings/str_format.h"
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpcpp/channel.h>
29 #include <grpcpp/client_context.h>
30 
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/gpr/useful.h"
33 #include "src/core/lib/gprpp/crash.h"
34 #include "src/proto/grpc/testing/messages.pb.h"
35 #include "src/proto/grpc/testing/test.grpc.pb.h"
36 #include "test/cpp/util/create_test_channel.h"
37 #include "test/cpp/util/test_config.h"
38 
39 namespace grpc {
40 namespace testing {
41 
42 namespace {
43 const int kLargeRequestSize = 271828;
44 const int kLargeResponseSize = 314159;
45 }  // namespace
46 
ServiceStub(const std::shared_ptr<Channel> & channel)47 Http2Client::ServiceStub::ServiceStub(const std::shared_ptr<Channel>& channel)
48     : channel_(channel) {
49   stub_ = TestService::NewStub(channel);
50 }
51 
Get()52 TestService::Stub* Http2Client::ServiceStub::Get() { return stub_.get(); }
53 
Http2Client(const std::shared_ptr<Channel> & channel)54 Http2Client::Http2Client(const std::shared_ptr<Channel>& channel)
55     : serviceStub_(channel),
56       channel_(channel),
57       defaultRequest_(BuildDefaultRequest()) {}
58 
AssertStatusCode(const Status & s,StatusCode expected_code)59 bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) {
60   if (s.error_code() == expected_code) {
61     return true;
62   }
63 
64   grpc_core::Crash(absl::StrFormat(
65       "Error status code: %d (expected: %d), message: %s", s.error_code(),
66       expected_code, s.error_message().c_str()));
67 }
68 
SendUnaryCall(SimpleResponse * response)69 Status Http2Client::SendUnaryCall(SimpleResponse* response) {
70   ClientContext context;
71   return serviceStub_.Get()->UnaryCall(&context, defaultRequest_, response);
72 }
73 
BuildDefaultRequest()74 SimpleRequest Http2Client::BuildDefaultRequest() {
75   SimpleRequest request;
76   request.set_response_size(kLargeResponseSize);
77   std::string payload(kLargeRequestSize, '\0');
78   request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
79   return request;
80 }
81 
DoRstAfterHeader()82 bool Http2Client::DoRstAfterHeader() {
83   gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after header");
84 
85   SimpleResponse response;
86   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
87   GPR_ASSERT(!response.has_payload());  // no data should be received
88 
89   gpr_log(GPR_DEBUG, "Done testing reset stream after header");
90   return true;
91 }
92 
DoRstAfterData()93 bool Http2Client::DoRstAfterData() {
94   gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after data");
95 
96   SimpleResponse response;
97   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
98   // There is no guarantee that data would be received.
99 
100   gpr_log(GPR_DEBUG, "Done testing reset stream after data");
101   return true;
102 }
103 
DoRstDuringData()104 bool Http2Client::DoRstDuringData() {
105   gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream during data");
106 
107   SimpleResponse response;
108   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
109   GPR_ASSERT(!response.has_payload());  // no data should be received
110 
111   gpr_log(GPR_DEBUG, "Done testing reset stream during data");
112   return true;
113 }
114 
DoGoaway()115 bool Http2Client::DoGoaway() {
116   gpr_log(GPR_DEBUG, "Sending two RPCs and expecting goaway");
117   SimpleResponse response;
118   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
119   GPR_ASSERT(response.payload().body() ==
120              std::string(kLargeResponseSize, '\0'));
121 
122   // Sleep for one second to give time for client to receive goaway frame.
123   gpr_timespec sleep_time = gpr_time_add(
124       gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(1, GPR_TIMESPAN));
125   gpr_sleep_until(sleep_time);
126 
127   response.Clear();
128   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
129   GPR_ASSERT(response.payload().body() ==
130              std::string(kLargeResponseSize, '\0'));
131   gpr_log(GPR_DEBUG, "Done testing goaway");
132   return true;
133 }
134 
DoPing()135 bool Http2Client::DoPing() {
136   gpr_log(GPR_DEBUG, "Sending RPC and expecting ping");
137   SimpleResponse response;
138   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
139   GPR_ASSERT(response.payload().body() ==
140              std::string(kLargeResponseSize, '\0'));
141   gpr_log(GPR_DEBUG, "Done testing ping");
142   return true;
143 }
144 
MaxStreamsWorker(const std::shared_ptr<grpc::Channel> &)145 void Http2Client::MaxStreamsWorker(
146     const std::shared_ptr<grpc::Channel>& /*channel*/) {
147   SimpleResponse response;
148   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
149   GPR_ASSERT(response.payload().body() ==
150              std::string(kLargeResponseSize, '\0'));
151 }
152 
DoMaxStreams()153 bool Http2Client::DoMaxStreams() {
154   gpr_log(GPR_DEBUG, "Testing max streams");
155 
156   // Make an initial call on the channel to ensure the server's max streams
157   // setting is received
158   SimpleResponse response;
159   AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
160   GPR_ASSERT(response.payload().body() ==
161              std::string(kLargeResponseSize, '\0'));
162 
163   std::vector<std::thread> test_threads;
164   test_threads.reserve(10);
165   for (int i = 0; i < 10; i++) {
166     test_threads.emplace_back(
167         std::thread(&Http2Client::MaxStreamsWorker, this, channel_));
168   }
169 
170   for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
171     it->join();
172   }
173 
174   gpr_log(GPR_DEBUG, "Done testing max streams");
175   return true;
176 }
177 
178 }  // namespace testing
179 }  // namespace grpc
180 
181 ABSL_FLAG(int32_t, server_port, 0, "Server port.");
182 ABSL_FLAG(std::string, server_host, "localhost", "Server host to connect to");
183 ABSL_FLAG(std::string, test_case, "rst_after_header",
184           "Configure different test cases. Valid options are:\n\n"
185           "goaway\n"
186           "max_streams\n"
187           "ping\n"
188           "rst_after_data\n"
189           "rst_after_header\n"
190           "rst_during_data\n");
191 
main(int argc,char ** argv)192 int main(int argc, char** argv) {
193   grpc::testing::InitTest(&argc, &argv, true);
194   GPR_ASSERT(absl::GetFlag(FLAGS_server_port));
195   const int host_port_buf_size = 1024;
196   char host_port[host_port_buf_size];
197   snprintf(host_port, host_port_buf_size, "%s:%d",
198            absl::GetFlag(FLAGS_server_host).c_str(),
199            absl::GetFlag(FLAGS_server_port));
200   std::shared_ptr<grpc::Channel> channel =
201       grpc::CreateTestChannel(host_port, grpc::testing::INSECURE);
202   GPR_ASSERT(channel->WaitForConnected(gpr_time_add(
203       gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(300, GPR_TIMESPAN))));
204   grpc::testing::Http2Client client(channel);
205   gpr_log(GPR_INFO, "Testing case: %s", absl::GetFlag(FLAGS_test_case).c_str());
206   int ret = 0;
207   if (absl::GetFlag(FLAGS_test_case) == "rst_after_header") {
208     client.DoRstAfterHeader();
209   } else if (absl::GetFlag(FLAGS_test_case) == "rst_after_data") {
210     client.DoRstAfterData();
211   } else if (absl::GetFlag(FLAGS_test_case) == "rst_during_data") {
212     client.DoRstDuringData();
213   } else if (absl::GetFlag(FLAGS_test_case) == "goaway") {
214     client.DoGoaway();
215   } else if (absl::GetFlag(FLAGS_test_case) == "ping") {
216     client.DoPing();
217   } else if (absl::GetFlag(FLAGS_test_case) == "max_streams") {
218     client.DoMaxStreams();
219   } else {
220     const char* testcases[] = {
221         "goaway",         "max_streams",      "ping",
222         "rst_after_data", "rst_after_header", "rst_during_data"};
223     char* joined_testcases =
224         gpr_strjoin_sep(testcases, GPR_ARRAY_SIZE(testcases), "\n", nullptr);
225 
226     gpr_log(GPR_ERROR, "Unsupported test case %s. Valid options are\n%s",
227             absl::GetFlag(FLAGS_test_case).c_str(), joined_testcases);
228     gpr_free(joined_testcases);
229     ret = 1;
230   }
231 
232   return ret;
233 }
234