1 /*
2 *
3 * Copyright 2015 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 <memory>
20 #include <unordered_map>
21
22 #include <gflags/gflags.h>
23 #include <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpcpp/channel.h>
27 #include <grpcpp/client_context.h>
28
29 #include "src/core/lib/gpr/string.h"
30 #include "test/core/util/test_config.h"
31 #include "test/cpp/interop/client_helper.h"
32 #include "test/cpp/interop/interop_client.h"
33 #include "test/cpp/util/test_config.h"
34
35 DEFINE_bool(use_alts, false,
36 "Whether to use alts. Enable alts will disable tls.");
37 DEFINE_bool(use_tls, false, "Whether to use tls.");
38 DEFINE_string(custom_credentials_type, "", "User provided credentials type.");
39 DEFINE_bool(use_test_ca, false, "False to use SSL roots for google");
40 DEFINE_int32(server_port, 0, "Server port.");
41 DEFINE_string(server_host, "localhost", "Server host to connect to");
42 DEFINE_string(server_host_override, "",
43 "Override the server host which is sent in HTTP header");
44 DEFINE_string(
45 test_case, "large_unary",
46 "Configure different test cases. Valid options are:\n\n"
47 "all : all test cases;\n"
48 "cancel_after_begin : cancel stream after starting it;\n"
49 "cancel_after_first_response: cancel on first response;\n"
50 "channel_soak: sends 'soak_iterations' rpcs, rebuilds channel each time;\n"
51 "client_compressed_streaming : compressed request streaming with "
52 "client_compressed_unary : single compressed request;\n"
53 "client_streaming : request streaming with single response;\n"
54 "compute_engine_creds: large_unary with compute engine auth;\n"
55 "custom_metadata: server will echo custom metadata;\n"
56 "empty_stream : bi-di stream with no request/response;\n"
57 "empty_unary : empty (zero bytes) request and response;\n"
58 "google_default_credentials: large unary using GDC;\n"
59 "half_duplex : half-duplex streaming;\n"
60 "jwt_token_creds: large_unary with JWT token auth;\n"
61 "large_unary : single request and (large) response;\n"
62 "long_lived_channel: sends large_unary rpcs over a long-lived channel;\n"
63 "oauth2_auth_token: raw oauth2 access token auth;\n"
64 "per_rpc_creds: raw oauth2 access token on a single rpc;\n"
65 "ping_pong : full-duplex streaming;\n"
66 "response streaming;\n"
67 "rpc_soak: 'sends soak_iterations' large_unary rpcs;\n"
68 "server_compressed_streaming : single request with compressed "
69 "server_compressed_unary : single compressed response;\n"
70 "server_streaming : single request with response streaming;\n"
71 "slow_consumer : single request with response streaming with "
72 "slow client consumer;\n"
73 "status_code_and_message: verify status code & message;\n"
74 "timeout_on_sleeping_server: deadline exceeds on stream;\n"
75 "unimplemented_method: client calls an unimplemented method;\n"
76 "unimplemented_service: client calls an unimplemented service;\n");
77 DEFINE_string(default_service_account, "",
78 "Email of GCE default service account");
79 DEFINE_string(service_account_key_file, "",
80 "Path to service account json key file.");
81 DEFINE_string(oauth_scope, "", "Scope for OAuth tokens.");
82 DEFINE_bool(do_not_abort_on_transient_failures, false,
83 "If set to 'true', abort() is not called in case of transient "
84 "failures (i.e failures that are temporary and will likely go away "
85 "on retrying; like a temporary connection failure) and an error "
86 "message is printed instead. Note that this flag just controls "
87 "whether abort() is called or not. It does not control whether the "
88 "test is retried in case of transient failures (and currently the "
89 "interop tests are not retried even if this flag is set to true)");
90 DEFINE_int32(soak_iterations, 1000,
91 "The number of iterations to use for the two soak tests; rpc_soak "
92 "and channel_soak.");
93 DEFINE_int32(soak_max_failures, 0,
94 "The number of iterations in soak tests that are allowed to fail "
95 "(either due to non-OK status code or exceeding the "
96 "per-iteration max acceptable latency).");
97 DEFINE_int32(soak_per_iteration_max_acceptable_latency_ms, 0,
98 "The number of milliseconds a single iteration in the two soak "
99 "tests (rpc_soak and channel_soak) should take.");
100 DEFINE_int32(soak_overall_timeout_seconds, 0,
101 "The overall number of seconds after which a soak test should "
102 "stop and fail, if the desired number of iterations have not yet "
103 "completed.");
104 DEFINE_int32(iteration_interval, 10,
105 "The interval in seconds between rpcs. This is used by "
106 "long_connection test");
107 DEFINE_string(additional_metadata, "",
108 "Additional metadata to send in each request, as a "
109 "semicolon-separated list of key:value pairs.");
110
111 using grpc::testing::CreateChannelForTestCase;
112 using grpc::testing::GetServiceAccountJsonKey;
113 using grpc::testing::UpdateActions;
114
115 namespace {
116
117 // Parse the contents of FLAGS_additional_metadata into a map. Allow
118 // alphanumeric characters and dashes in keys, and any character but semicolons
119 // in values. Convert keys to lowercase. On failure, log an error and return
120 // false.
ParseAdditionalMetadataFlag(const std::string & flag,std::multimap<std::string,std::string> * additional_metadata)121 bool ParseAdditionalMetadataFlag(
122 const std::string& flag,
123 std::multimap<std::string, std::string>* additional_metadata) {
124 size_t start_pos = 0;
125 while (start_pos < flag.length()) {
126 size_t colon_pos = flag.find(':', start_pos);
127 if (colon_pos == std::string::npos) {
128 gpr_log(GPR_ERROR,
129 "Couldn't parse metadata flag: extra characters at end of flag");
130 return false;
131 }
132 size_t semicolon_pos = flag.find(';', colon_pos);
133
134 std::string key = flag.substr(start_pos, colon_pos - start_pos);
135 std::string value =
136 flag.substr(colon_pos + 1, semicolon_pos - colon_pos - 1);
137
138 constexpr char alphanum_and_hyphen[] =
139 "-0123456789"
140 "abcdefghijklmnopqrstuvwxyz"
141 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
142 if (key.find_first_not_of(alphanum_and_hyphen) != std::string::npos) {
143 gpr_log(GPR_ERROR,
144 "Couldn't parse metadata flag: key contains characters other "
145 "than alphanumeric and hyphens: %s",
146 key.c_str());
147 return false;
148 }
149
150 // Convert to lowercase.
151 for (char& c : key) {
152 if (c >= 'A' && c <= 'Z') {
153 c += ('a' - 'A');
154 }
155 }
156
157 gpr_log(GPR_INFO, "Adding additional metadata with key %s and value %s",
158 key.c_str(), value.c_str());
159 additional_metadata->insert({key, value});
160
161 if (semicolon_pos == std::string::npos) {
162 break;
163 } else {
164 start_pos = semicolon_pos + 1;
165 }
166 }
167
168 return true;
169 }
170
171 } // namespace
172
main(int argc,char ** argv)173 int main(int argc, char** argv) {
174 grpc::testing::TestEnvironment env(argc, argv);
175 grpc::testing::InitTest(&argc, &argv, true);
176 gpr_log(GPR_INFO, "Testing these cases: %s", FLAGS_test_case.c_str());
177 int ret = 0;
178
179 grpc::testing::ChannelCreationFunc channel_creation_func;
180 std::string test_case = FLAGS_test_case;
181 if (FLAGS_additional_metadata == "") {
182 channel_creation_func = [test_case]() {
183 return CreateChannelForTestCase(test_case);
184 };
185 } else {
186 std::multimap<std::string, std::string> additional_metadata;
187 if (!ParseAdditionalMetadataFlag(FLAGS_additional_metadata,
188 &additional_metadata)) {
189 return 1;
190 }
191
192 channel_creation_func = [test_case, additional_metadata]() {
193 std::vector<std::unique_ptr<
194 grpc::experimental::ClientInterceptorFactoryInterface>>
195 factories;
196 factories.emplace_back(
197 new grpc::testing::AdditionalMetadataInterceptorFactory(
198 additional_metadata));
199 return CreateChannelForTestCase(test_case, std::move(factories));
200 };
201 }
202
203 grpc::testing::InteropClient client(channel_creation_func, true,
204 FLAGS_do_not_abort_on_transient_failures);
205
206 std::unordered_map<std::string, std::function<bool()>> actions;
207 actions["empty_unary"] =
208 std::bind(&grpc::testing::InteropClient::DoEmpty, &client);
209 actions["large_unary"] =
210 std::bind(&grpc::testing::InteropClient::DoLargeUnary, &client);
211 actions["server_compressed_unary"] = std::bind(
212 &grpc::testing::InteropClient::DoServerCompressedUnary, &client);
213 actions["client_compressed_unary"] = std::bind(
214 &grpc::testing::InteropClient::DoClientCompressedUnary, &client);
215 actions["client_streaming"] =
216 std::bind(&grpc::testing::InteropClient::DoRequestStreaming, &client);
217 actions["server_streaming"] =
218 std::bind(&grpc::testing::InteropClient::DoResponseStreaming, &client);
219 actions["server_compressed_streaming"] = std::bind(
220 &grpc::testing::InteropClient::DoServerCompressedStreaming, &client);
221 actions["client_compressed_streaming"] = std::bind(
222 &grpc::testing::InteropClient::DoClientCompressedStreaming, &client);
223 actions["slow_consumer"] = std::bind(
224 &grpc::testing::InteropClient::DoResponseStreamingWithSlowConsumer,
225 &client);
226 actions["half_duplex"] =
227 std::bind(&grpc::testing::InteropClient::DoHalfDuplex, &client);
228 actions["ping_pong"] =
229 std::bind(&grpc::testing::InteropClient::DoPingPong, &client);
230 actions["cancel_after_begin"] =
231 std::bind(&grpc::testing::InteropClient::DoCancelAfterBegin, &client);
232 actions["cancel_after_first_response"] = std::bind(
233 &grpc::testing::InteropClient::DoCancelAfterFirstResponse, &client);
234 actions["timeout_on_sleeping_server"] = std::bind(
235 &grpc::testing::InteropClient::DoTimeoutOnSleepingServer, &client);
236 actions["empty_stream"] =
237 std::bind(&grpc::testing::InteropClient::DoEmptyStream, &client);
238 actions["pick_first_unary"] =
239 std::bind(&grpc::testing::InteropClient::DoPickFirstUnary, &client);
240 if (FLAGS_use_tls) {
241 actions["compute_engine_creds"] =
242 std::bind(&grpc::testing::InteropClient::DoComputeEngineCreds, &client,
243 FLAGS_default_service_account, FLAGS_oauth_scope);
244 actions["jwt_token_creds"] =
245 std::bind(&grpc::testing::InteropClient::DoJwtTokenCreds, &client,
246 GetServiceAccountJsonKey());
247 actions["oauth2_auth_token"] =
248 std::bind(&grpc::testing::InteropClient::DoOauth2AuthToken, &client,
249 FLAGS_default_service_account, FLAGS_oauth_scope);
250 actions["per_rpc_creds"] =
251 std::bind(&grpc::testing::InteropClient::DoPerRpcCreds, &client,
252 GetServiceAccountJsonKey());
253 }
254 if (FLAGS_custom_credentials_type == "google_default_credentials") {
255 actions["google_default_credentials"] =
256 std::bind(&grpc::testing::InteropClient::DoGoogleDefaultCredentials,
257 &client, FLAGS_default_service_account);
258 }
259 actions["status_code_and_message"] =
260 std::bind(&grpc::testing::InteropClient::DoStatusWithMessage, &client);
261 actions["custom_metadata"] =
262 std::bind(&grpc::testing::InteropClient::DoCustomMetadata, &client);
263 actions["unimplemented_method"] =
264 std::bind(&grpc::testing::InteropClient::DoUnimplementedMethod, &client);
265 actions["unimplemented_service"] =
266 std::bind(&grpc::testing::InteropClient::DoUnimplementedService, &client);
267 actions["cacheable_unary"] =
268 std::bind(&grpc::testing::InteropClient::DoCacheableUnary, &client);
269 actions["channel_soak"] =
270 std::bind(&grpc::testing::InteropClient::DoChannelSoakTest, &client,
271 FLAGS_soak_iterations, FLAGS_soak_max_failures,
272 FLAGS_soak_per_iteration_max_acceptable_latency_ms,
273 FLAGS_soak_overall_timeout_seconds);
274 actions["rpc_soak"] =
275 std::bind(&grpc::testing::InteropClient::DoRpcSoakTest, &client,
276 FLAGS_soak_iterations, FLAGS_soak_max_failures,
277 FLAGS_soak_per_iteration_max_acceptable_latency_ms,
278 FLAGS_soak_overall_timeout_seconds);
279 actions["long_lived_channel"] =
280 std::bind(&grpc::testing::InteropClient::DoLongLivedChannelTest, &client,
281 FLAGS_soak_iterations, FLAGS_iteration_interval);
282
283 UpdateActions(&actions);
284
285 if (FLAGS_test_case == "all") {
286 for (const auto& action : actions) {
287 action.second();
288 }
289 } else if (actions.find(FLAGS_test_case) != actions.end()) {
290 actions.find(FLAGS_test_case)->second();
291 } else {
292 std::string test_cases;
293 for (const auto& action : actions) {
294 if (!test_cases.empty()) test_cases += "\n";
295 test_cases += action.first;
296 }
297 gpr_log(GPR_ERROR, "Unsupported test case %s. Valid options are\n%s",
298 FLAGS_test_case.c_str(), test_cases.c_str());
299 ret = 1;
300 }
301
302 return ret;
303 }
304