| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 | /* * * Copyright 2015 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include <memory>#include <unordered_map>#include <gflags/gflags.h>#include <grpc/grpc.h>#include <grpc/support/alloc.h>#include <grpc/support/log.h>#include <grpcpp/channel.h>#include <grpcpp/client_context.h>#include "src/core/lib/gpr/string.h"#include "test/core/util/test_config.h"#include "test/cpp/interop/client_helper.h"#include "test/cpp/interop/interop_client.h"#include "test/cpp/util/test_config.h"DEFINE_bool(use_alts, false,            "Whether to use alts. Enable alts will disable tls.");DEFINE_bool(use_tls, false, "Whether to use tls.");DEFINE_string(custom_credentials_type, "", "User provided credentials type.");DEFINE_bool(use_test_ca, false, "False to use SSL roots for google");DEFINE_int32(server_port, 0, "Server port.");DEFINE_string(server_host, "localhost", "Server host to connect to");DEFINE_string(server_host_override, "",              "Override the server host which is sent in HTTP header");DEFINE_string(    test_case, "large_unary",    "Configure different test cases. Valid options are:\n\n"    "all : all test cases;\n"    "cancel_after_begin : cancel stream after starting it;\n"    "cancel_after_first_response: cancel on first response;\n"    "channel_soak: sends 'soak_iterations' rpcs, rebuilds channel each time;\n"    "client_compressed_streaming : compressed request streaming with "    "client_compressed_unary : single compressed request;\n"    "client_streaming : request streaming with single response;\n"    "compute_engine_creds: large_unary with compute engine auth;\n"    "custom_metadata: server will echo custom metadata;\n"    "empty_stream : bi-di stream with no request/response;\n"    "empty_unary : empty (zero bytes) request and response;\n"    "google_default_credentials: large unary using GDC;\n"    "half_duplex : half-duplex streaming;\n"    "jwt_token_creds: large_unary with JWT token auth;\n"    "large_unary : single request and (large) response;\n"    "long_lived_channel: sends large_unary rpcs over a long-lived channel;\n"    "oauth2_auth_token: raw oauth2 access token auth;\n"    "per_rpc_creds: raw oauth2 access token on a single rpc;\n"    "ping_pong : full-duplex streaming;\n"    "response streaming;\n"    "rpc_soak: 'sends soak_iterations' large_unary rpcs;\n"    "server_compressed_streaming : single request with compressed "    "server_compressed_unary : single compressed response;\n"    "server_streaming : single request with response streaming;\n"    "slow_consumer : single request with response streaming with "    "slow client consumer;\n"    "status_code_and_message: verify status code & message;\n"    "timeout_on_sleeping_server: deadline exceeds on stream;\n"    "unimplemented_method: client calls an unimplemented method;\n"    "unimplemented_service: client calls an unimplemented service;\n");DEFINE_string(default_service_account, "",              "Email of GCE default service account");DEFINE_string(service_account_key_file, "",              "Path to service account json key file.");DEFINE_string(oauth_scope, "", "Scope for OAuth tokens.");DEFINE_bool(do_not_abort_on_transient_failures, false,            "If set to 'true', abort() is not called in case of transient "            "failures (i.e failures that are temporary and will likely go away "            "on retrying; like a temporary connection failure) and an error "            "message is printed instead. Note that this flag just controls "            "whether abort() is called or not. It does not control whether the "            "test is retried in case of transient failures (and currently the "            "interop tests are not retried even if this flag is set to true)");DEFINE_int32(soak_iterations, 1000,             "number of iterations to use for the two soak tests; rpc_soak and "             "channel_soak");DEFINE_int32(iteration_interval, 10,             "The interval in seconds between rpcs. This is used by "             "long_connection test");DEFINE_string(additional_metadata, "",              "Additional metadata to send in each request, as a "              "semicolon-separated list of key:value pairs.");using grpc::testing::CreateChannelForTestCase;using grpc::testing::GetServiceAccountJsonKey;using grpc::testing::UpdateActions;namespace {// Parse the contents of FLAGS_additional_metadata into a map. Allow// alphanumeric characters and dashes in keys, and any character but semicolons// in values. Convert keys to lowercase. On failure, log an error and return// false.bool ParseAdditionalMetadataFlag(    const grpc::string& flag,    std::multimap<grpc::string, grpc::string>* additional_metadata) {  size_t start_pos = 0;  while (start_pos < flag.length()) {    size_t colon_pos = flag.find(':', start_pos);    if (colon_pos == grpc::string::npos) {      gpr_log(GPR_ERROR,              "Couldn't parse metadata flag: extra characters at end of flag");      return false;    }    size_t semicolon_pos = flag.find(';', colon_pos);    grpc::string key = flag.substr(start_pos, colon_pos - start_pos);    grpc::string value =        flag.substr(colon_pos + 1, semicolon_pos - colon_pos - 1);    constexpr char alphanum_and_hyphen[] =        "-0123456789"        "abcdefghijklmnopqrstuvwxyz"        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";    if (key.find_first_not_of(alphanum_and_hyphen) != grpc::string::npos) {      gpr_log(GPR_ERROR,              "Couldn't parse metadata flag: key contains characters other "              "than alphanumeric and hyphens: %s",              key.c_str());      return false;    }    // Convert to lowercase.    for (char& c : key) {      if (c >= 'A' && c <= 'Z') {        c += ('a' - 'A');      }    }    gpr_log(GPR_INFO, "Adding additional metadata with key %s and value %s",            key.c_str(), value.c_str());    additional_metadata->insert({key, value});    if (semicolon_pos == grpc::string::npos) {      break;    } else {      start_pos = semicolon_pos + 1;    }  }  return true;}}  // namespaceint main(int argc, char** argv) {  grpc::testing::TestEnvironment env(argc, argv);  grpc::testing::InitTest(&argc, &argv, true);  gpr_log(GPR_INFO, "Testing these cases: %s", FLAGS_test_case.c_str());  int ret = 0;  grpc::testing::ChannelCreationFunc channel_creation_func;  grpc::string test_case = FLAGS_test_case;  if (FLAGS_additional_metadata == "") {    channel_creation_func = [test_case]() {      return CreateChannelForTestCase(test_case);    };  } else {    std::multimap<grpc::string, grpc::string> additional_metadata;    if (!ParseAdditionalMetadataFlag(FLAGS_additional_metadata,                                     &additional_metadata)) {      return 1;    }    channel_creation_func = [test_case, additional_metadata]() {      std::vector<std::unique_ptr<          grpc::experimental::ClientInterceptorFactoryInterface>>          factories;      factories.emplace_back(          new grpc::testing::AdditionalMetadataInterceptorFactory(              additional_metadata));      return CreateChannelForTestCase(test_case, std::move(factories));    };  }  grpc::testing::InteropClient client(channel_creation_func, true,                                      FLAGS_do_not_abort_on_transient_failures);  std::unordered_map<grpc::string, std::function<bool()>> actions;  actions["empty_unary"] =      std::bind(&grpc::testing::InteropClient::DoEmpty, &client);  actions["large_unary"] =      std::bind(&grpc::testing::InteropClient::DoLargeUnary, &client);  actions["server_compressed_unary"] = std::bind(      &grpc::testing::InteropClient::DoServerCompressedUnary, &client);  actions["client_compressed_unary"] = std::bind(      &grpc::testing::InteropClient::DoClientCompressedUnary, &client);  actions["client_streaming"] =      std::bind(&grpc::testing::InteropClient::DoRequestStreaming, &client);  actions["server_streaming"] =      std::bind(&grpc::testing::InteropClient::DoResponseStreaming, &client);  actions["server_compressed_streaming"] = std::bind(      &grpc::testing::InteropClient::DoServerCompressedStreaming, &client);  actions["client_compressed_streaming"] = std::bind(      &grpc::testing::InteropClient::DoClientCompressedStreaming, &client);  actions["slow_consumer"] = std::bind(      &grpc::testing::InteropClient::DoResponseStreamingWithSlowConsumer,      &client);  actions["half_duplex"] =      std::bind(&grpc::testing::InteropClient::DoHalfDuplex, &client);  actions["ping_pong"] =      std::bind(&grpc::testing::InteropClient::DoPingPong, &client);  actions["cancel_after_begin"] =      std::bind(&grpc::testing::InteropClient::DoCancelAfterBegin, &client);  actions["cancel_after_first_response"] = std::bind(      &grpc::testing::InteropClient::DoCancelAfterFirstResponse, &client);  actions["timeout_on_sleeping_server"] = std::bind(      &grpc::testing::InteropClient::DoTimeoutOnSleepingServer, &client);  actions["empty_stream"] =      std::bind(&grpc::testing::InteropClient::DoEmptyStream, &client);  actions["pick_first_unary"] =      std::bind(&grpc::testing::InteropClient::DoPickFirstUnary, &client);  if (FLAGS_use_tls) {    actions["compute_engine_creds"] =        std::bind(&grpc::testing::InteropClient::DoComputeEngineCreds, &client,                  FLAGS_default_service_account, FLAGS_oauth_scope);    actions["jwt_token_creds"] =        std::bind(&grpc::testing::InteropClient::DoJwtTokenCreds, &client,                  GetServiceAccountJsonKey());    actions["oauth2_auth_token"] =        std::bind(&grpc::testing::InteropClient::DoOauth2AuthToken, &client,                  FLAGS_default_service_account, FLAGS_oauth_scope);    actions["per_rpc_creds"] =        std::bind(&grpc::testing::InteropClient::DoPerRpcCreds, &client,                  GetServiceAccountJsonKey());  }  if (FLAGS_custom_credentials_type == "google_default_credentials") {    actions["google_default_credentials"] =        std::bind(&grpc::testing::InteropClient::DoGoogleDefaultCredentials,                  &client, FLAGS_default_service_account);  }  actions["status_code_and_message"] =      std::bind(&grpc::testing::InteropClient::DoStatusWithMessage, &client);  actions["custom_metadata"] =      std::bind(&grpc::testing::InteropClient::DoCustomMetadata, &client);  actions["unimplemented_method"] =      std::bind(&grpc::testing::InteropClient::DoUnimplementedMethod, &client);  actions["unimplemented_service"] =      std::bind(&grpc::testing::InteropClient::DoUnimplementedService, &client);  actions["cacheable_unary"] =      std::bind(&grpc::testing::InteropClient::DoCacheableUnary, &client);  actions["channel_soak"] =      std::bind(&grpc::testing::InteropClient::DoChannelSoakTest, &client,                FLAGS_soak_iterations);  actions["rpc_soak"] = std::bind(&grpc::testing::InteropClient::DoRpcSoakTest,                                  &client, FLAGS_soak_iterations);  actions["long_lived_channel"] =      std::bind(&grpc::testing::InteropClient::DoLongLivedChannelTest, &client,                FLAGS_soak_iterations, FLAGS_iteration_interval);  UpdateActions(&actions);  if (FLAGS_test_case == "all") {    for (const auto& action : actions) {      action.second();    }  } else if (actions.find(FLAGS_test_case) != actions.end()) {    actions.find(FLAGS_test_case)->second();  } else {    grpc::string test_cases;    for (const auto& action : actions) {      if (!test_cases.empty()) test_cases += "\n";      test_cases += action.first;    }    gpr_log(GPR_ERROR, "Unsupported test case %s. Valid options are\n%s",            FLAGS_test_case.c_str(), test_cases.c_str());    ret = 1;  }  return ret;}
 |