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/cpp/interop/client_helper.h"
31 #include "test/cpp/interop/interop_client.h"
32 #include "test/cpp/util/test_config.h"
33 
34 DEFINE_bool(use_alts, false,
35             "Whether to use alts. Enable alts will disable tls.");
36 DEFINE_bool(use_tls, false, "Whether to use tls.");
37 DEFINE_string(custom_credentials_type, "", "User provided credentials type.");
38 DEFINE_bool(use_test_ca, false, "False to use SSL roots for google");
39 DEFINE_int32(server_port, 0, "Server port.");
40 DEFINE_string(server_host, "localhost", "Server host to connect to");
41 DEFINE_string(server_host_override, "foo.test.google.fr",
42               "Override the server host which is sent in HTTP header");
43 DEFINE_string(
44     test_case, "large_unary",
45     "Configure different test cases. Valid options are:\n\n"
46     "all : all test cases;\n"
47     "cancel_after_begin : cancel stream after starting it;\n"
48     "cancel_after_first_response: cancel on first response;\n"
49     "channel_soak: sends 'soak_iterations' rpcs, rebuilds channel each time;\n"
50     "client_compressed_streaming : compressed request streaming with "
51     "client_compressed_unary : single compressed request;\n"
52     "client_streaming : request streaming with single response;\n"
53     "compute_engine_creds: large_unary with compute engine auth;\n"
54     "custom_metadata: server will echo custom metadata;\n"
55     "empty_stream : bi-di stream with no request/response;\n"
56     "empty_unary : empty (zero bytes) request and response;\n"
57     "half_duplex : half-duplex streaming;\n"
58     "jwt_token_creds: large_unary with JWT token auth;\n"
59     "large_unary : single request and (large) response;\n"
60     "long_lived_channel: sends large_unary rpcs over a long-lived channel;\n"
61     "oauth2_auth_token: raw oauth2 access token auth;\n"
62     "per_rpc_creds: raw oauth2 access token on a single rpc;\n"
63     "ping_pong : full-duplex streaming;\n"
64     "response streaming;\n"
65     "rpc_soak: 'sends soak_iterations' large_unary rpcs;\n"
66     "server_compressed_streaming : single request with compressed "
67     "server_compressed_unary : single compressed response;\n"
68     "server_streaming : single request with response streaming;\n"
69     "slow_consumer : single request with response streaming with "
70     "slow client consumer;\n"
71     "status_code_and_message: verify status code & message;\n"
72     "timeout_on_sleeping_server: deadline exceeds on stream;\n"
73     "unimplemented_method: client calls an unimplemented method;\n"
74     "unimplemented_service: client calls an unimplemented service;\n");
75 DEFINE_string(default_service_account, "",
76               "Email of GCE default service account");
77 DEFINE_string(service_account_key_file, "",
78               "Path to service account json key file.");
79 DEFINE_string(oauth_scope, "", "Scope for OAuth tokens.");
80 DEFINE_bool(do_not_abort_on_transient_failures, false,
81             "If set to 'true', abort() is not called in case of transient "
82             "failures (i.e failures that are temporary and will likely go away "
83             "on retrying; like a temporary connection failure) and an error "
84             "message is printed instead. Note that this flag just controls "
85             "whether abort() is called or not. It does not control whether the "
86             "test is retried in case of transient failures (and currently the "
87             "interop tests are not retried even if this flag is set to true)");
88 DEFINE_int32(soak_iterations, 1000,
89              "number of iterations to use for the two soak tests; rpc_soak and "
90              "channel_soak");
91 DEFINE_int32(iteration_interval, 10,
92              "The interval in seconds between rpcs. This is used by "
93              "long_connection test");
94 
95 using grpc::testing::CreateChannelForTestCase;
96 using grpc::testing::GetServiceAccountJsonKey;
97 using grpc::testing::UpdateActions;
98 
main(int argc,char ** argv)99 int main(int argc, char** argv) {
100   grpc::testing::InitTest(&argc, &argv, true);
101   gpr_log(GPR_INFO, "Testing these cases: %s", FLAGS_test_case.c_str());
102   int ret = 0;
103   grpc::testing::ChannelCreationFunc channel_creation_func =
104       std::bind(&CreateChannelForTestCase, FLAGS_test_case);
105   grpc::testing::InteropClient client(channel_creation_func, true,
106                                       FLAGS_do_not_abort_on_transient_failures);
107 
108   std::unordered_map<grpc::string, std::function<bool()>> actions;
109   actions["empty_unary"] =
110       std::bind(&grpc::testing::InteropClient::DoEmpty, &client);
111   actions["large_unary"] =
112       std::bind(&grpc::testing::InteropClient::DoLargeUnary, &client);
113   actions["server_compressed_unary"] = std::bind(
114       &grpc::testing::InteropClient::DoServerCompressedUnary, &client);
115   actions["client_compressed_unary"] = std::bind(
116       &grpc::testing::InteropClient::DoClientCompressedUnary, &client);
117   actions["client_streaming"] =
118       std::bind(&grpc::testing::InteropClient::DoRequestStreaming, &client);
119   actions["server_streaming"] =
120       std::bind(&grpc::testing::InteropClient::DoResponseStreaming, &client);
121   actions["server_compressed_streaming"] = std::bind(
122       &grpc::testing::InteropClient::DoServerCompressedStreaming, &client);
123   actions["client_compressed_streaming"] = std::bind(
124       &grpc::testing::InteropClient::DoClientCompressedStreaming, &client);
125   actions["slow_consumer"] = std::bind(
126       &grpc::testing::InteropClient::DoResponseStreamingWithSlowConsumer,
127       &client);
128   actions["half_duplex"] =
129       std::bind(&grpc::testing::InteropClient::DoHalfDuplex, &client);
130   actions["ping_pong"] =
131       std::bind(&grpc::testing::InteropClient::DoPingPong, &client);
132   actions["cancel_after_begin"] =
133       std::bind(&grpc::testing::InteropClient::DoCancelAfterBegin, &client);
134   actions["cancel_after_first_response"] = std::bind(
135       &grpc::testing::InteropClient::DoCancelAfterFirstResponse, &client);
136   actions["timeout_on_sleeping_server"] = std::bind(
137       &grpc::testing::InteropClient::DoTimeoutOnSleepingServer, &client);
138   actions["empty_stream"] =
139       std::bind(&grpc::testing::InteropClient::DoEmptyStream, &client);
140   if (FLAGS_use_tls) {
141     actions["compute_engine_creds"] =
142         std::bind(&grpc::testing::InteropClient::DoComputeEngineCreds, &client,
143                   FLAGS_default_service_account, FLAGS_oauth_scope);
144     actions["jwt_token_creds"] =
145         std::bind(&grpc::testing::InteropClient::DoJwtTokenCreds, &client,
146                   GetServiceAccountJsonKey());
147     actions["oauth2_auth_token"] =
148         std::bind(&grpc::testing::InteropClient::DoOauth2AuthToken, &client,
149                   FLAGS_default_service_account, FLAGS_oauth_scope);
150     actions["per_rpc_creds"] =
151         std::bind(&grpc::testing::InteropClient::DoPerRpcCreds, &client,
152                   GetServiceAccountJsonKey());
153   }
154   actions["status_code_and_message"] =
155       std::bind(&grpc::testing::InteropClient::DoStatusWithMessage, &client);
156   actions["custom_metadata"] =
157       std::bind(&grpc::testing::InteropClient::DoCustomMetadata, &client);
158   actions["unimplemented_method"] =
159       std::bind(&grpc::testing::InteropClient::DoUnimplementedMethod, &client);
160   actions["unimplemented_service"] =
161       std::bind(&grpc::testing::InteropClient::DoUnimplementedService, &client);
162   actions["cacheable_unary"] =
163       std::bind(&grpc::testing::InteropClient::DoCacheableUnary, &client);
164   actions["channel_soak"] =
165       std::bind(&grpc::testing::InteropClient::DoChannelSoakTest, &client,
166                 FLAGS_soak_iterations);
167   actions["rpc_soak"] = std::bind(&grpc::testing::InteropClient::DoRpcSoakTest,
168                                   &client, FLAGS_soak_iterations);
169   actions["long_lived_channel"] =
170       std::bind(&grpc::testing::InteropClient::DoLongLivedChannelTest, &client,
171                 FLAGS_soak_iterations, FLAGS_iteration_interval);
172 
173   UpdateActions(&actions);
174 
175   if (FLAGS_test_case == "all") {
176     for (const auto& action : actions) {
177       action.second();
178     }
179   } else if (actions.find(FLAGS_test_case) != actions.end()) {
180     actions.find(FLAGS_test_case)->second();
181   } else {
182     grpc::string test_cases;
183     for (const auto& action : actions) {
184       if (!test_cases.empty()) test_cases += "\n";
185       test_cases += action.first;
186     }
187     gpr_log(GPR_ERROR, "Unsupported test case %s. Valid options are\n%s",
188             FLAGS_test_case.c_str(), test_cases.c_str());
189     ret = 1;
190   }
191 
192   return ret;
193 }
194