| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331 | /* * * 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 <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 <sstream>#include "src/core/lib/gpr/env.h"#include "src/core/lib/iomgr/load_file.h"#include "src/proto/grpc/testing/echo.grpc.pb.h"#include "src/proto/grpc/testing/echo.pb.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"#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"#define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"using grpc::testing::EchoRequest;using grpc::testing::EchoResponse;#define USAGE_REGEX "(  grpc_cli .+\n){2,10}"#define ECHO_TEST_SERVICE_SUMMARY \  "Echo\n"                        \  "Echo1\n"                       \  "Echo2\n"                       \  "CheckDeadlineUpperBound\n"     \  "CheckDeadlineSet\n"            \  "CheckClientInitialMetadata\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 Echo1(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \  "{}\n"                                                                       \  "  rpc Echo2(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \  "{}\n"                                                                       \  "  rpc CheckDeadlineUpperBound(grpc.testing.SimpleRequest) returns "         \  "(grpc.testing.StringValue) {}\n"                                            \  "  rpc CheckDeadlineSet(grpc.testing.SimpleRequest) returns "                \  "(grpc.testing.StringValue) {}\n"                                            \  "  rpc CheckClientInitialMetadata(grpc.testing.SimpleRequest) returns "      \  "(grpc.testing.SimpleResponse) {}\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 {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();    }    grpc_slice ca_slice;    GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",                                 grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));    const char* test_root_cert =        reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);    SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};    std::shared_ptr<grpc::ChannelCredentials> credential_ptr =        grpc::SslCredentials(grpc::SslCredentialsOptions(ssl_opts));    grpc_slice_unref(ca_slice);    return credential_ptr;  }  const std::string GetCredentialUsage() const override { return ""; } private:  const bool secure_;};bool PrintStream(std::stringstream* ss, const std::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 CheckDeadlineSet(ServerContext* context, const SimpleRequest* request,                          StringValue* response) override {    response->set_message(context->deadline() !=                                  std::chrono::system_clock::time_point::max()                              ? "true"                              : "false");    return Status::OK;  }  // Check if deadline - current time <= timeout  // If deadline set, timeout + current time should be an upper bound for it  Status CheckDeadlineUpperBound(ServerContext* context,                                 const SimpleRequest* request,                                 StringValue* response) override {    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(        context->deadline() - std::chrono::system_clock::now());    // Returning string instead of bool to avoid using embedded messages in    // proto3    response->set_message(seconds.count() <= FLAGS_timeout ? "true"                                                              : "false");    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() + std::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 std::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;    grpc_slice cert_slice, key_slice;    GPR_ASSERT(GRPC_LOG_IF_ERROR(        "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));    GPR_ASSERT(GRPC_LOG_IF_ERROR(        "load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));    const char* server_cert =        reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);    const char* server_key =        reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);    SslServerCredentialsOptions::PemKeyCertPair pkcp = {server_key,                                                        server_cert};    if (secure) {      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();    grpc_slice_unref(cert_slice);    grpc_slice_unref(key_slice);    return server_address.str();  }  void ShutdownServer() { server_->Shutdown(); }  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 std::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 std::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(std::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 std::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 std::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(std::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 std::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "type", server_address.c_str(),                        "grpc.testing.DummyRequest"};  EXPECT_TRUE(1 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommand) {  // Test input "grpc_cli call localhost:<port> Echo "message: 'Hello'"  std::stringstream output_stream;  const std::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(std::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 std::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(std::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 std::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(std::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 std::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(std::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 std::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(std::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 std::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(std::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 std::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 std::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 std::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 std::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, CallCommandWithTimeoutDeadlineSet) {  // Test input "grpc_cli call CheckDeadlineSet --timeout=5000.25"  std::stringstream output_stream;  const std::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "CheckDeadlineSet"};  // Set timeout to 5000.25 seconds  FLAGS_timeout = 5000.25;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: "true"", deadline set  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"true\""));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandWithTimeoutDeadlineUpperBound) {  // Test input "grpc_cli call CheckDeadlineUpperBound --timeout=900"  std::stringstream output_stream;  const std::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "CheckDeadlineUpperBound"};  // Set timeout to 900 seconds  FLAGS_timeout = 900;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: "true""  // deadline not greater than timeout + current time  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"true\""));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandWithNegativeTimeoutValue) {  // Test input "grpc_cli call CheckDeadlineSet --timeout=-5"  std::stringstream output_stream;  const std::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "CheckDeadlineSet"};  // Set timeout to -5 (deadline not set)  FLAGS_timeout = -5;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: "false"", deadline not set  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"false\""));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandWithDefaultTimeoutValue) {  // Test input "grpc_cli call CheckDeadlineSet --timeout=-1"  std::stringstream output_stream;  const std::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "call", server_address.c_str(),                        "CheckDeadlineSet"};  // Set timeout to -1 (default value, deadline not set)  FLAGS_timeout = -1;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  // Expected output: "message: "false"", deadline not set  EXPECT_TRUE(nullptr !=              strstr(output_stream.str().c_str(), "message: \"false\""));  ShutdownServer();}TEST_F(GrpcToolTest, CallCommandResponseStream) {  // Test input: grpc_cli call localhost:<port> ResponseStream "message:  // 'Hello'"  std::stringstream output_stream;  const std::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++) {    std::string expected_response_text =        "message: \"Hello" + std::to_string(i) + "\"\n";    EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),                                  expected_response_text.c_str()));  }  // with json_output  output_stream.str(std::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++) {    std::string expected_response_text =        "{\n \"message\": \"Hello" + std::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 std::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 std::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 std::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(std::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(std::string());  output_stream.clear();  FLAGS_binary_output = true;  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  std::string binary_data = output_stream.str();  output_stream.str(std::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 std::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(std::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 std::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",                        "grpc.testing.EchoTestService.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 std::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();}TEST_F(GrpcToolTest, ConfiguringDefaultServiceConfig) {  // Test input "grpc_cli list localhost:<port>  // --default_service_config={\"loadBalancingConfig\":[{\"pick_first\":{}}]}"  std::stringstream output_stream;  const std::string server_address = SetUpServer();  const char* argv[] = {"grpc_cli", "ls", server_address.c_str()};  // Just check that the tool is still operational when --default_service_config  // is configured. This particular service config is in reality redundant with  // the channel's default configuration.  FLAGS_l = false;  FLAGS_default_service_config =      "{\"loadBalancingConfig\":[{\"pick_first\":{}}]}";  EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),                                   std::bind(PrintStream, &output_stream,                                             std::placeholders::_1)));  FLAGS_default_service_config = "";  EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),                          "grpc.testing.EchoTestService\n"                          "grpc.reflection.v1alpha.ServerReflection\n"));  ShutdownServer();}}  // namespace testing}  // namespace grpcint main(int argc, char** argv) {  grpc::testing::TestEnvironment env(argc, argv);  ::testing::InitGoogleTest(&argc, argv);  ::testing::FLAGS_gtest_death_test_style = "threadsafe";  return RUN_ALL_TESTS();}
 |