| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 | /* * * Copyright 2016 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 "test/cpp/util/grpc_tool.h"#include <sstream>#include <gflags/gflags.h>#include <grpc/grpc.h>#include <grpc/support/alloc.h>#include <grpcpp/channel.h>#include <grpcpp/client_context.h>#include <grpcpp/create_channel.h>#include <grpcpp/ext/proto_server_reflection_plugin.h>#include <grpcpp/server.h>#include <grpcpp/server_builder.h>#include <grpcpp/server_context.h>#include <gtest/gtest.h>#include "src/core/lib/gpr/env.h"#include "src/proto/grpc/testing/echo.grpc.pb.h"#include "src/proto/grpc/testing/echo.pb.h"#include "test/core/end2end/data/ssl_test_data.h"#include "test/core/util/port.h"#include "test/core/util/test_config.h"#include "test/cpp/util/cli_credentials.h"#include "test/cpp/util/string_ref_helper.h"using grpc::testing::EchoRequest;using grpc::testing::EchoResponse;#define USAGE_REGEX "(  grpc_cli .+\n){2,10}"#define ECHO_TEST_SERVICE_SUMMARY \  "Echo\n"                        \  "RequestStream\n"               \  "ResponseStream\n"              \  "BidiStream\n"                  \  "Unimplemented\n"#define ECHO_TEST_SERVICE_DESCRIPTION                                         \  "filename: src/proto/grpc/testing/echo.proto\n"                             \  "package: grpc.testing;\n"                                                  \  "service EchoTestService {\n"                                               \  "  rpc Echo(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \  "{}\n"                                                                      \  "  rpc RequestStream(stream grpc.testing.EchoRequest) returns "             \  "(grpc.testing.EchoResponse) {}\n"                                          \  "  rpc ResponseStream(grpc.testing.EchoRequest) returns (stream "           \  "grpc.testing.EchoResponse) {}\n"                                           \  "  rpc BidiStream(stream grpc.testing.EchoRequest) returns (stream "        \  "grpc.testing.EchoResponse) {}\n"                                           \  "  rpc Unimplemented(grpc.testing.EchoRequest) returns "                    \  "(grpc.testing.EchoResponse) {}\n"                                          \  "}\n"                                                                       \  "\n"#define ECHO_METHOD_DESCRIPTION                                               \  "  rpc Echo(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \  "{}\n"#define ECHO_RESPONSE_MESSAGE_TEXT_FORMAT \  "message: \"echo\"\n"                   \  "param {\n"                             \  "  host: \"localhost\"\n"               \  "  peer: \"peer\"\n"                    \  "}\n\n"#define ECHO_RESPONSE_MESSAGE_JSON_FORMAT \  "{\n"                                   \  " \"message\": \"echo\",\n"             \  " \"param\": {\n"                       \  "  \"host\": \"localhost\",\n"          \  "  \"peer\": \"peer\"\n"                \  " }\n"                                  \  "}\n\n"DECLARE_string(channel_creds_type);DECLARE_string(ssl_target);namespace grpc {namespace testing {DECLARE_bool(binary_input);DECLARE_bool(binary_output);DECLARE_bool(json_input);DECLARE_bool(json_output);DECLARE_bool(l);DECLARE_bool(batch);DECLARE_string(metadata);DECLARE_string(protofiles);DECLARE_string(proto_path);namespace {const int kServerDefaultResponseStreamsToSend = 3;class TestCliCredentials final : public grpc::testing::CliCredentials { public:  TestCliCredentials(bool secure = false) : secure_(secure) {}  std::shared_ptr<grpc::ChannelCredentials> GetChannelCredentials()      const override {    if (!secure_) {      return InsecureChannelCredentials();    }    SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};    return SslCredentials(grpc::SslCredentialsOptions(ssl_opts));  }  const grpc::string GetCredentialUsage() const override { return ""; } private:  const bool secure_;};bool PrintStream(std::stringstream* ss, const grpc::string& output) {  (*ss) << output;  return true;}template <typename T>size_t ArraySize(T& a) {  return ((sizeof(a) / sizeof(*(a))) /          static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))));}class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { public:  Status Echo(ServerContext* context, const EchoRequest* request,              EchoResponse* response) override {    if (!context->client_metadata().empty()) {      for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator               iter = context->client_metadata().begin();           iter != context->client_metadata().end(); ++iter) {        context->AddInitialMetadata(ToString(iter->first),                                    ToString(iter->second));      }    }    context->AddTrailingMetadata("trailing_key", "trailing_value");    response->set_message(request->message());    return Status::OK;  }  Status RequestStream(ServerContext* context,                       ServerReader<EchoRequest>* reader,                       EchoResponse* response) override {    EchoRequest request;    response->set_message("");    if (!context->client_metadata().empty()) {      for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator               iter = context->client_metadata().begin();           iter != context->client_metadata().end(); ++iter) {        context->AddInitialMetadata(ToString(iter->first),                                    ToString(iter->second));      }    }    context->AddTrailingMetadata("trailing_key", "trailing_value");    while (reader->Read(&request)) {      response->mutable_message()->append(request.message());    }    return Status::OK;  }  Status ResponseStream(ServerContext* context, const EchoRequest* request,                        ServerWriter<EchoResponse>* writer) override {    if (!context->client_metadata().empty()) {      for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator               iter = context->client_metadata().begin();           iter != context->client_metadata().end(); ++iter) {        context->AddInitialMetadata(ToString(iter->first),                                    ToString(iter->second));      }    }    context->AddTrailingMetadata("trailing_key", "trailing_value");    EchoResponse response;    for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {      response.set_message(request->message() + grpc::to_string(i));      writer->Write(response);    }    return Status::OK;  }  Status BidiStream(      ServerContext* context,      ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {    EchoRequest request;    EchoResponse response;    if (!context->client_metadata().empty()) {      for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator               iter = context->client_metadata().begin();           iter != context->client_metadata().end(); ++iter) {        context->AddInitialMetadata(ToString(iter->first),                                    ToString(iter->second));      }    }    context->AddTrailingMetadata("trailing_key", "trailing_value");    while (stream->Read(&request)) {      response.set_message(request.message());      stream->Write(response);    }    return Status::OK;  }};}  // namespaceclass GrpcToolTest : public ::testing::Test { protected:  GrpcToolTest() {}  // SetUpServer cannot be used with EXPECT_EXIT. grpc_pick_unused_port_or_die()  // uses atexit() to free chosen ports, and it will spawn a new thread in  // resolve_address_posix.c:192 at exit time.  const grpc::string SetUpServer(bool secure = false) {    std::ostringstream server_address;    int port = grpc_pick_unused_port_or_die();    server_address << "localhost:" << port;    // Setup server    ServerBuilder builder;    std::shared_ptr<grpc::ServerCredentials> creds;    if (secure) {      SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,                                                          test_server1_cert};      SslServerCredentialsOptions ssl_opts;      ssl_opts.pem_root_certs = "";      ssl_opts.pem_key_cert_pairs.push_back(pkcp);      creds = SslServerCredentials(ssl_opts);    } else {      creds = InsecureServerCredentials();    }    builder.AddListeningPort(server_address.str(), creds);    builder.RegisterService(&service_);    server_ = builder.BuildAndStart();    return server_address.str();  }  void ShutdownServer() { server_->Shutdown(); }  void ExitWhenError(int argc, const char** argv, const CliCredentials& cred,                     GrpcToolOutputCallback callback) {    int result = GrpcToolMainLib(argc, argv, cred, callback);    if (result) {      exit(result);    }  }  std::unique_ptr<Server> server_;  TestServiceImpl service_;  reflection::ProtoServerReflectionPlugin plugin_;};TEST_F(GrpcToolTest, NoCommand) {  // Test input "grpc_cli"  std::stringstream output_stream;  const char* argv[] = {"grpc_cli"};  // Exit with 1, print usage instruction in stderr  EXPECT_EXIT(      GrpcToolMainLib(          ArraySize(argv), argv, TestCliCredentials(),          std::bind(PrintStream, &output_stream, std::placeholders::_1)),      ::testing::ExitedWithCode(1), "No command specified\n" USAGE_REGEX);  // No output  EXPECT_TRUE(0 == output_stream.tellp());}TEST_F(GrpcToolTest, InvalidCommand) {  // Test input "grpc_cli"  std::stringstream output_stream;  const char* argv[] = {"grpc_cli", "abc"};  // Exit with 1, print usage instruction in stderr  EXPECT_EXIT(      GrpcToolMainLib(          ArraySize(argv), argv, TestCliCredentials(),          std::bind(PrintStream, &output_stream, std::placeholders::_1)),      ::testing::ExitedWithCode(1), "Invalid command 'abc'\n" USAGE_REGEX);  // No output  EXPECT_TRUE(0 == output_stream.tellp());}TEST_F(GrpcToolTest, HelpCommand) {  // Test input "grpc_cli help"  std::stringstream output_stream;  const char* argv[] = {"grpc_cli", "help"};  // Exit with 1, print usage instruction in stderr  EXPECT_EXIT(GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                              std::bind(PrintStream, &output_stream,                                        std::placeholders::_1)),              ::testing::ExitedWithCode(1), USAGE_REGEX);  // No output  EXPECT_TRUE(0 == output_stream.tellp());}TEST_F(GrpcToolTest, ListCommand) {  // Test input "grpc_cli list localhost:<port>"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "ls", server_address.c_str()};  FLAGS_l = false;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          "grpc.testing.EchoTestService\n"                          "grpc.reflection.v1alpha.ServerReflection\n"));  ShutdownServer();}TEST_F(GrpcToolTest, ListOneService) {  // Test input "grpc_cli list localhost:<port> grpc.testing.EchoTestService"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "ls", server_address.c_str(),                        "grpc.testing.EchoTestService"};  // without -l flag  FLAGS_l = false;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: ECHO_TEST_SERVICE_SUMMARY  EXPECT_TRUE(0 ==              strcmp(output_stream.str().c_str(), ECHO_TEST_SERVICE_SUMMARY));  // with -l flag  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_l = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: ECHO_TEST_SERVICE_DESCRIPTION  EXPECT_TRUE(      0 == strcmp(output_stream.str().c_str(), ECHO_TEST_SERVICE_DESCRIPTION));  ShutdownServer();}TEST_F(GrpcToolTest, TypeCommand) {  // Test input "grpc_cli type localhost:<port> grpc.testing.EchoRequest"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "type", server_address.c_str(),                        "grpc.testing.EchoRequest"};  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  const grpc::protobuf::Descriptor* desc =      grpc::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(          "grpc.testing.EchoRequest");  // Expected output: the DebugString of grpc.testing.EchoRequest  EXPECT_TRUE(0 ==              strcmp(output_stream.str().c_str(), desc->DebugString().c_str()));  ShutdownServer();}TEST_F(GrpcToolTest, ListOneMethod) {  // Test input "grpc_cli list localhost:<port> grpc.testing.EchoTestService"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "ls", server_address.c_str(),                        "grpc.testing.EchoTestService.Echo"};  // without -l flag  FLAGS_l = false;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "Echo"  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), "Echo\n"));  // with -l flag  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_l = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: ECHO_METHOD_DESCRIPTION  EXPECT_TRUE(0 ==              strcmp(output_stream.str().c_str(), ECHO_METHOD_DESCRIPTION));  ShutdownServer();}TEST_F(GrpcToolTest, TypeNotFound) {  // Test input "grpc_cli type localhost:<port> grpc.testing.DummyRequest"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "type", server_address.c_str(),                        "grpc.testing.DummyRequest"};  EXPECT_DEATH(ExitWhenError(ArraySize(argv), argv, TestCliCredentials(),                             std::bind(PrintStream, &output_stream,                                       std::placeholders::_1)),               ".*Type grpc.testing.DummyRequest not found.*");  ShutdownServer();}TEST_F(GrpcToolTest, CallCommand) {  // Test input "grpc_cli call localhost:<port> Echo "message: 'Hello'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "message: 'Hello'"};  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello\""  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"Hello\""));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  // Expected output:  // {  //  "message": "Hello"  // }  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "{\n \"message\": \"Hello\"\n}"));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandJsonInput) {  // Test input "grpc_cli call localhost:<port> Echo "{ \"message\": \"Hello\"}"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "{ \"message\": \"Hello\"}"};  FLAGS_json_input = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello\""  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"Hello\""));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  FLAGS_json_input = false;  // Expected output:  // {  //  "message": "Hello"  // }  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "{\n \"message\": \"Hello\"\n}"));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandBatch) {  // Test input "grpc_cli call Echo"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "message: 'Hello0'"};  // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_batch = false;  // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:  // "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0\"\nmessage: "                                "\"Hello1\"\nmessage: \"Hello2\"\n"));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  ss.clear();  ss.seekg(0);  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  FLAGS_batch = false;  // Expected output:  // {  //  "message": "Hello0"  // }  // {  //  "message": "Hello1"  // }  // {  //  "message": "Hello2"  // }  // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:  // "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "{\n \"message\": \"Hello0\"\n}\n"                                "{\n \"message\": \"Hello1\"\n}\n"                                "{\n \"message\": \"Hello2\"\n}\n"));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandBatchJsonInput) {  // Test input "grpc_cli call Echo"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "{\"message\": \"Hello0\"}"};  // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss(      "{\"message\": \"Hello1\"}\n\n{\"message\": \"Hello2\" }\n\n");  std::cin.rdbuf(ss.rdbuf());  FLAGS_json_input = true;  FLAGS_batch = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_batch = false;  // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:  // "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0\"\nmessage: "                                "\"Hello1\"\nmessage: \"Hello2\"\n"));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  ss.clear();  ss.seekg(0);  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  FLAGS_batch = false;  FLAGS_json_input = false;  // Expected output:  // {  //  "message": "Hello0"  // }  // {  //  "message": "Hello1"  // }  // {  //  "message": "Hello2"  // }  // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:  // "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "{\n \"message\": \"Hello0\"\n}\n"                                "{\n \"message\": \"Hello1\"\n}\n"                                "{\n \"message\": \"Hello2\"\n}\n"));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandBatchWithBadRequest) {  // Test input "grpc_cli call Echo"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "message: 'Hello0'"};  // Mock std::cin input "message: 1\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss("message: 1\n\n message: 'Hello2'\n\n");  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_batch = false;  // Expected output: "message: "Hello0"\nmessage: "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0\"\nmessage: \"Hello2\"\n"));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  ss.clear();  ss.seekg(0);  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  FLAGS_batch = false;  // Expected output:  // {  //  "message": "Hello0"  // }  // {  //  "message": "Hello2"  // }  // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:  // "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "{\n \"message\": \"Hello0\"\n}\n"                                "{\n \"message\": \"Hello2\"\n}\n"));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandBatchJsonInputWithBadRequest) {  // Test input "grpc_cli call Echo"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "{ \"message\": \"Hello0\"}"};  // Mock std::cin input "message: 1\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss(      "{ \"message\": 1 }\n\n { \"message\": \"Hello2\" }\n\n");  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  FLAGS_json_input = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_input = false;  FLAGS_batch = false;  // Expected output: "message: "Hello0"\nmessage: "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0\"\nmessage: \"Hello2\"\n"));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  ss.clear();  ss.seekg(0);  std::cin.rdbuf(ss.rdbuf());  FLAGS_batch = true;  FLAGS_json_input = true;  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  FLAGS_json_input = false;  FLAGS_batch = false;  // Expected output:  // {  //  "message": "Hello0"  // }  // {  //  "message": "Hello2"  // }  // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:  // "Hello2"\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "{\n \"message\": \"Hello0\"\n}\n"                                "{\n \"message\": \"Hello2\"\n}\n"));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandRequestStream) {  // Test input: grpc_cli call localhost:<port> RequestStream "message:  // 'Hello0'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "RequestStream", "message: 'Hello0'"};  // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");  std::cin.rdbuf(ss.rdbuf());  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello0Hello1Hello2\""  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0Hello1Hello2\""));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandRequestStreamJsonInput) {  // Test input: grpc_cli call localhost:<port> RequestStream "{ \"message\":  // \"Hello0\"}"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "RequestStream", "{ \"message\": \"Hello0\" }"};  // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss(      "{ \"message\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");  std::cin.rdbuf(ss.rdbuf());  FLAGS_json_input = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_input = false;  // Expected output: "message: \"Hello0Hello1Hello2\""  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0Hello1Hello2\""));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequest) {  // Test input: grpc_cli call localhost:<port> RequestStream "message:  // 'Hello0'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "RequestStream", "message: 'Hello0'"};  // Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss("bad_field: 'Hello1'\n\n message: 'Hello2'\n\n");  std::cin.rdbuf(ss.rdbuf());  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello0Hello2\""  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"Hello0Hello2\""));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequestJsonInput) {  // Test input: grpc_cli call localhost:<port> RequestStream "message:  // 'Hello0'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "RequestStream", "{ \"message\": \"Hello0\" }"};  // Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss(      "{ \"bad_field\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");  std::cin.rdbuf(ss.rdbuf());  FLAGS_json_input = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_input = false;  // Expected output: "message: \"Hello0Hello2\""  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"Hello0Hello2\""));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandResponseStream) {  // Test input: grpc_cli call localhost:<port> ResponseStream "message:  // 'Hello'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "ResponseStream", "message: 'Hello'"};  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello{n}\""  for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {    grpc::string expected_response_text =        "message: \"Hello" + grpc::to_string(i) + "\"\n";    EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                  expected_response_text.c_str()));  }  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  // Expected output: "{\n \"message\": \"Hello{n}\"\n}\n"  for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {    grpc::string expected_response_text =        "{\n \"message\": \"Hello" + grpc::to_string(i) + "\"\n}\n";    EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                  expected_response_text.c_str()));  }  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandBidiStream) {  // Test input: grpc_cli call localhost:<port> BidiStream "message: 'Hello0'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "BidiStream", "message: 'Hello0'"};  // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");  std::cin.rdbuf(ss.rdbuf());  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello0\"\nmessage: \"Hello1\"\nmessage:  // \"Hello2\"\n\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0\"\nmessage: "                                "\"Hello1\"\nmessage: \"Hello2\"\n"));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandBidiStreamWithBadRequest) {  // Test input: grpc_cli call localhost:<port> BidiStream "message: 'Hello0'"  std::stringstream output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "BidiStream", "message: 'Hello0'"};  // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"  std::streambuf* orig = std::cin.rdbuf();  std::istringstream ss("message: 1.0\n\n message: 'Hello2'\n\n");  std::cin.rdbuf(ss.rdbuf());  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: \"Hello0\"\nmessage: \"Hello1\"\nmessage:  // \"Hello2\"\n\n"  EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                "message: \"Hello0\"\nmessage: \"Hello2\"\n"));  std::cin.rdbuf(orig);  ShutdownServer();}TEST_F(GrpcToolTest, ParseCommand) {  // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse  // ECHO_RESPONSE_MESSAGE"  std::stringstream output_stream;  std::stringstream binary_output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),                        "grpc.testing.EchoResponse",                        ECHO_RESPONSE_MESSAGE_TEXT_FORMAT};  FLAGS_binary_input = false;  FLAGS_binary_output = false;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  // Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          ECHO_RESPONSE_MESSAGE_JSON_FORMAT));  // Parse text message to binary message and then parse it back to text message  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_binary_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  grpc::string binary_data = output_stream.str();  output_stream.str(grpc::string());  output_stream.clear();  argv[4] = binary_data.c_str();  FLAGS_binary_input = true;  FLAGS_binary_output = false;  EXPECT_TRUE(0 == GrpcToolMainLib(5, argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: ECHO_RESPONSE_MESSAGE  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));  FLAGS_binary_input = false;  FLAGS_binary_output = false;  ShutdownServer();}TEST_F(GrpcToolTest, ParseCommandJsonFormat) {  // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse  // ECHO_RESPONSE_MESSAGE_JSON_FORMAT"  std::stringstream output_stream;  std::stringstream binary_output_stream;  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),                        "grpc.testing.EchoResponse",                        ECHO_RESPONSE_MESSAGE_JSON_FORMAT};  FLAGS_json_input = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));  // with json_output  output_stream.str(grpc::string());  output_stream.clear();  FLAGS_json_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_json_output = false;  FLAGS_json_input = false;  // Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          ECHO_RESPONSE_MESSAGE_JSON_FORMAT));  ShutdownServer();}TEST_F(GrpcToolTest, TooFewArguments) {  // Test input "grpc_cli call Echo"  std::stringstream output_stream;  const char* argv[] = {"grpc_cli", "call", "Echo"};  // Exit with 1  EXPECT_EXIT(      GrpcToolMainLib(          ArraySize(argv), argv, TestCliCredentials(),          std::bind(PrintStream, &output_stream, std::placeholders::_1)),      ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*");  // No output  EXPECT_TRUE(0 == output_stream.tellp());}TEST_F(GrpcToolTest, TooManyArguments) {  // Test input "grpc_cli call localhost:<port> Echo Echo "message: 'Hello'"  std::stringstream output_stream;  const char* argv[] = {"grpc_cli", "call", "localhost:10000",                        "Echo",     "Echo", "message: 'Hello'"};  // Exit with 1  EXPECT_EXIT(      GrpcToolMainLib(          ArraySize(argv), argv, TestCliCredentials(),          std::bind(PrintStream, &output_stream, std::placeholders::_1)),      ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*");  // No output  EXPECT_TRUE(0 == output_stream.tellp());}TEST_F(GrpcToolTest, CallCommandWithMetadata) {  // Test input "grpc_cli call localhost:<port> Echo "message: 'Hello'"  const grpc::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",                        "message: 'Hello'"};  {    std::stringstream output_stream;    FLAGS_metadata = "key0:val0:key1:valq:key2:val2";    EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv,                                     TestCliCredentials(),                                     std::bind(PrintStream, &output_stream,                                               std::placeholders::_1)));    // Expected output: "message: \"Hello\""    EXPECT_TRUE(nullptr !=                strstr(output_stream.str().c_str(), "message: \"Hello\""));  }  {    std::stringstream output_stream;    FLAGS_metadata = "key:val\\:val";    EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv,                                     TestCliCredentials(),                                     std::bind(PrintStream, &output_stream,                                               std::placeholders::_1)));    // Expected output: "message: \"Hello\""    EXPECT_TRUE(nullptr !=                strstr(output_stream.str().c_str(), "message: \"Hello\""));  }  {    std::stringstream output_stream;    FLAGS_metadata = "key:val\\\\val";    EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv,                                     TestCliCredentials(),                                     std::bind(PrintStream, &output_stream,                                               std::placeholders::_1)));    // Expected output: "message: \"Hello\""    EXPECT_TRUE(nullptr !=                strstr(output_stream.str().c_str(), "message: \"Hello\""));  }  FLAGS_metadata = "";  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandWithBadMetadata) {  // Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'"  const char* argv[] = {"grpc_cli", "call", "localhost:10000", "Echo",                        "message: 'Hello'"};  FLAGS_protofiles = "src/proto/grpc/testing/echo.proto";  char* test_srcdir = gpr_getenv("TEST_SRCDIR");  if (test_srcdir != nullptr) {    FLAGS_proto_path = test_srcdir + std::string("/com_github_grpc_grpc");  }  {    std::stringstream output_stream;    FLAGS_metadata = "key0:val0:key1";    // Exit with 1    EXPECT_EXIT(        GrpcToolMainLib(            ArraySize(argv), argv, TestCliCredentials(),            std::bind(PrintStream, &output_stream, std::placeholders::_1)),        ::testing::ExitedWithCode(1), ".*Failed to parse metadata flag.*");  }  {    std::stringstream output_stream;    FLAGS_metadata = "key:val\\val";    // Exit with 1    EXPECT_EXIT(        GrpcToolMainLib(            ArraySize(argv), argv, TestCliCredentials(),            std::bind(PrintStream, &output_stream, std::placeholders::_1)),        ::testing::ExitedWithCode(1), ".*Failed to parse metadata flag.*");  }  FLAGS_metadata = "";  FLAGS_protofiles = "";  gpr_free(test_srcdir);}TEST_F(GrpcToolTest, ListCommand_OverrideSslHostName) {  const grpc::string server_address = SetUpServer(true);  // Test input "grpc_cli ls localhost:<port> --channel_creds_type=ssl  // --ssl_target=z.test.google.fr"  std::stringstream output_stream;  const char* argv[] = {"grpc_cli", "ls", server_address.c_str()};  FLAGS_l = false;  FLAGS_channel_creds_type = "ssl";  FLAGS_ssl_target = "z.test.google.fr";  EXPECT_TRUE(      0 == GrpcToolMainLib(               ArraySize(argv), argv, TestCliCredentials(true),               std::bind(PrintStream, &output_stream, std::placeholders::_1)));  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          "grpc.testing.EchoTestService\n"                          "grpc.reflection.v1alpha.ServerReflection\n"));  FLAGS_channel_creds_type = "";  FLAGS_ssl_target = "";  ShutdownServer();}}  // namespace testing}  // namespace grpcint main(int argc, char** argv) {  grpc_test_init(argc, argv);  ::testing::InitGoogleTest(&argc, argv);  ::testing::FLAGS_gtest_death_test_style = "threadsafe";  return RUN_ALL_TESTS();}
 |