| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347 | 
							- /*
 
-  *
 
-  * 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 <chrono>
 
- #include <sstream>
 
- #include "absl/flags/declare.h"
 
- #include "absl/flags/flag.h"
 
- #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"
 
- ABSL_DECLARE_FLAG(std::string, channel_creds_type);
 
- ABSL_DECLARE_FLAG(std::string, ssl_target);
 
- ABSL_DECLARE_FLAG(bool, binary_input);
 
- ABSL_DECLARE_FLAG(bool, binary_output);
 
- ABSL_DECLARE_FLAG(bool, json_input);
 
- ABSL_DECLARE_FLAG(bool, json_output);
 
- ABSL_DECLARE_FLAG(bool, l);
 
- ABSL_DECLARE_FLAG(bool, batch);
 
- ABSL_DECLARE_FLAG(std::string, metadata);
 
- ABSL_DECLARE_FLAG(std::string, protofiles);
 
- ABSL_DECLARE_FLAG(std::string, proto_path);
 
- ABSL_DECLARE_FLAG(std::string, default_service_config);
 
- ABSL_DECLARE_FLAG(double, timeout);
 
- namespace grpc {
 
- namespace testing {
 
- namespace {
 
- const int kServerDefaultResponseStreamsToSend = 3;
 
- class TestCliCredentials final : public grpc::testing::CliCredentials {
 
-  public:
 
-   explicit 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() <= absl::GetFlag(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;
 
-   }
 
- };
 
- }  // namespace
 
- class 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()};
 
-   absl::SetFlag(&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
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&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
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&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.PhonyRequest"
 
-   std::stringstream output_stream;
 
-   const std::string server_address = SetUpServer();
 
-   const char* argv[] = {"grpc_cli", "type", server_address.c_str(),
 
-                         "grpc.testing.PhonyRequest"};
 
-   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();
 
-   // TODO(Capstan): Consider using absl::FlagSaver
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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\"}"};
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_output, false);
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_output, false);
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_json_input, true);
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_output, false);
 
-   absl::SetFlag(&FLAGS_batch, false);
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_output, false);
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   absl::SetFlag(&FLAGS_json_input, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_input, false);
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_batch, true);
 
-   absl::SetFlag(&FLAGS_json_input, true);
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_output, false);
 
-   absl::SetFlag(&FLAGS_json_input, false);
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_json_input, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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());
 
-   absl::SetFlag(&FLAGS_json_input, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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
 
-   absl::SetFlag(&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
 
-   absl::SetFlag(&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)
 
-   absl::SetFlag(&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)
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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};
 
-   absl::SetFlag(&FLAGS_binary_input, false);
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&FLAGS_binary_input, true);
 
-   absl::SetFlag(&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));
 
-   absl::SetFlag(&FLAGS_binary_input, false);
 
-   absl::SetFlag(&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};
 
-   absl::SetFlag(&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();
 
-   absl::SetFlag(&FLAGS_json_output, true);
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&FLAGS_json_output, false);
 
-   absl::SetFlag(&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;
 
-     absl::SetFlag(&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;
 
-     absl::SetFlag(&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;
 
-     absl::SetFlag(&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\""));
 
-   }
 
-   absl::SetFlag(&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'"};
 
-   absl::SetFlag(&FLAGS_protofiles, "src/proto/grpc/testing/echo.proto");
 
-   char* test_srcdir = gpr_getenv("TEST_SRCDIR");
 
-   if (test_srcdir != nullptr) {
 
-     absl::SetFlag(&FLAGS_proto_path,
 
-                   test_srcdir + std::string("/com_github_grpc_grpc"));
 
-   }
 
-   {
 
-     std::stringstream output_stream;
 
-     absl::SetFlag(&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;
 
-     absl::SetFlag(&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.*");
 
-   }
 
-   absl::SetFlag(&FLAGS_metadata, "");
 
-   absl::SetFlag(&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()};
 
-   absl::SetFlag(&FLAGS_l, false);
 
-   absl::SetFlag(&FLAGS_channel_creds_type, "ssl");
 
-   absl::SetFlag(&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"));
 
-   absl::SetFlag(&FLAGS_channel_creds_type, "");
 
-   absl::SetFlag(&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.
 
-   absl::SetFlag(&FLAGS_l, false);
 
-   absl::SetFlag(&FLAGS_default_service_config,
 
-                 "{\"loadBalancingConfig\":[{\"pick_first\":{}}]}");
 
-   EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
 
-                                    std::bind(PrintStream, &output_stream,
 
-                                              std::placeholders::_1)));
 
-   absl::SetFlag(&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 grpc
 
- int 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();
 
- }
 
 
  |