Debian Codegraph @

test/cpp/util/grpc_tool_test.cc

raw
1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "test/cpp/util/grpc_tool.h"
20
21 #include <chrono>
22 #include <sstream>
23
24 #include <gtest/gtest.h>
25
26 #include "absl/flags/declare.h"
27 #include "absl/flags/flag.h"
28
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpcpp/channel.h>
32 #include <grpcpp/client_context.h>
33 #include <grpcpp/create_channel.h>
34 #include <grpcpp/ext/proto_server_reflection_plugin.h>
35 #include <grpcpp/server.h>
36 #include <grpcpp/server_builder.h>
37 #include <grpcpp/server_context.h>
38
39 #include "src/core/lib/gprpp/env.h"
40 #include "src/core/lib/iomgr/load_file.h"
41 #include "src/proto/grpc/testing/echo.grpc.pb.h"
42 #include "src/proto/grpc/testing/echo.pb.h"
43 #include "test/core/util/port.h"
44 #include "test/core/util/test_config.h"
45 #include "test/cpp/util/cli_credentials.h"
46 #include "test/cpp/util/string_ref_helper.h"
47 #include "test/cpp/util/test_config.h"
48
49 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
50 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
51 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
52
53 using grpc::testing::EchoRequest;
54 using grpc::testing::EchoResponse;
55
56 #define USAGE_REGEX "( grpc_cli .+\n){2,10}"
57
58 #define ECHO_TEST_SERVICE_SUMMARY \
59 "Echo\n" \
60 "Echo1\n" \
61 "Echo2\n" \
62 "CheckDeadlineUpperBound\n" \
63 "CheckDeadlineSet\n" \
64 "CheckClientInitialMetadata\n" \
65 "RequestStream\n" \
66 "ResponseStream\n" \
67 "BidiStream\n" \
68 "Unimplemented\n" \
69 "UnimplementedBidi\n"
70
71 #define ECHO_TEST_SERVICE_DESCRIPTION \
72 "filename: src/proto/grpc/testing/echo.proto\n" \
73 "package: grpc.testing;\n" \
74 "service EchoTestService {\n" \
75 " rpc Echo(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
76 "{}\n" \
77 " rpc Echo1(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
78 "{}\n" \
79 " rpc Echo2(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
80 "{}\n" \
81 " rpc CheckDeadlineUpperBound(grpc.testing.SimpleRequest) returns " \
82 "(grpc.testing.StringValue) {}\n" \
83 " rpc CheckDeadlineSet(grpc.testing.SimpleRequest) returns " \
84 "(grpc.testing.StringValue) {}\n" \
85 " rpc CheckClientInitialMetadata(grpc.testing.SimpleRequest) returns " \
86 "(grpc.testing.SimpleResponse) {}\n" \
87 " rpc RequestStream(stream grpc.testing.EchoRequest) returns " \
88 "(grpc.testing.EchoResponse) {}\n" \
89 " rpc ResponseStream(grpc.testing.EchoRequest) returns (stream " \
90 "grpc.testing.EchoResponse) {}\n" \
91 " rpc BidiStream(stream grpc.testing.EchoRequest) returns (stream " \
92 "grpc.testing.EchoResponse) {}\n" \
93 " rpc Unimplemented(grpc.testing.EchoRequest) returns " \
94 "(grpc.testing.EchoResponse) {}\n" \
95 " rpc UnimplementedBidi(stream grpc.testing.EchoRequest) returns (stream " \
96 "grpc.testing.EchoResponse) {}\n" \
97 "}\n" \
98 "\n"
99
100 #define ECHO_METHOD_DESCRIPTION \
101 " rpc Echo(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
102 "{}\n"
103
104 #define ECHO_RESPONSE_MESSAGE_TEXT_FORMAT \
105 "message: \"echo\"\n" \
106 "param {\n" \
107 " host: \"localhost\"\n" \
108 " peer: \"peer\"\n" \
109 "}\n\n"
110
111 #define ECHO_RESPONSE_MESSAGE_JSON_FORMAT \
112 "{\n" \
113 " \"message\": \"echo\",\n" \
114 " \"param\": {\n" \
115 " \"host\": \"localhost\",\n" \
116 " \"peer\": \"peer\"\n" \
117 " }\n" \
118 "}\n\n"
119
120 ABSL_DECLARE_FLAG(std::string, channel_creds_type);
121 ABSL_DECLARE_FLAG(std::string, ssl_target);
122 ABSL_DECLARE_FLAG(bool, binary_input);
123 ABSL_DECLARE_FLAG(bool, binary_output);
124 ABSL_DECLARE_FLAG(bool, json_input);
125 ABSL_DECLARE_FLAG(bool, json_output);
126 ABSL_DECLARE_FLAG(bool, l);
127 ABSL_DECLARE_FLAG(bool, batch);
128 ABSL_DECLARE_FLAG(std::string, metadata);
129 ABSL_DECLARE_FLAG(std::string, protofiles);
130 ABSL_DECLARE_FLAG(std::string, proto_path);
131 ABSL_DECLARE_FLAG(std::string, default_service_config);
132 ABSL_DECLARE_FLAG(double, timeout);
133 ABSL_DECLARE_FLAG(int, max_recv_msg_size);
134
135 namespace grpc {
136 namespace testing {
137 namespace {
138
139 const int kServerDefaultResponseStreamsToSend = 3;
140
141 class TestCliCredentials final : public grpc::testing::CliCredentials {
142 public:
143 explicit TestCliCredentials(bool secure = false) : secure_(secure) {}
144 std::shared_ptr<grpc::ChannelCredentials> GetChannelCredentials()
145 const override {
146 if (!secure_) {
147 return InsecureChannelCredentials();
148 }
149 grpc_slice ca_slice;
150 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
151 grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
152 const char* test_root_cert =
153 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
154 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
155 std::shared_ptr<grpc::ChannelCredentials> credential_ptr =
156 grpc::SslCredentials(grpc::SslCredentialsOptions(ssl_opts));
157 grpc_slice_unref(ca_slice);
158 return credential_ptr;
159 }
160 std::string GetCredentialUsage() const override { return ""; }
161
162 private:
163 const bool secure_;
164 };
165
166 bool PrintStream(std::stringstream* ss, const std::string& output) {
167 (*ss) << output;
168 return true;
169 }
170
171 template <typename T>
172 size_t ArraySize(T& a) {
173 return ((sizeof(a) / sizeof(*(a))) /
174 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))));
175 }
176
177 class TestServiceImpl : public grpc::testing::EchoTestService::Service {
178 public:
179 Status Echo(ServerContext* context, const EchoRequest* request,
180 EchoResponse* response) override {
181 if (!context->client_metadata().empty()) {
182 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
183 iter = context->client_metadata().begin();
184 iter != context->client_metadata().end(); ++iter) {
185 context->AddInitialMetadata(ToString(iter->first),
186 ToString(iter->second));
187 }
188 }
189 context->AddTrailingMetadata("trailing_key", "trailing_value");
190 response->set_message(request->message());
191 return Status::OK;
192 }
193
194 Status CheckDeadlineSet(ServerContext* context,
195 const SimpleRequest* /*request*/,
196 StringValue* response) override {
197 response->set_message(context->deadline() !=
198 std::chrono::system_clock::time_point::max()
199 ? "true"
200 : "false");
201 return Status::OK;
202 }
203
204 // Check if deadline - current time <= timeout
205 // If deadline set, timeout + current time should be an upper bound for it
206 Status CheckDeadlineUpperBound(ServerContext* context,
207 const SimpleRequest* /*request*/,
208 StringValue* response) override {
209 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(
210 context->deadline() - std::chrono::system_clock::now());
211
212 // Returning string instead of bool to avoid using embedded messages in
213 // proto3
214 response->set_message(
215 seconds.count() <= absl::GetFlag(FLAGS_timeout) ? "true" : "false");
216 return Status::OK;
217 }
218
219 Status RequestStream(ServerContext* context,
220 ServerReader<EchoRequest>* reader,
221 EchoResponse* response) override {
222 EchoRequest request;
223 response->set_message("");
224 if (!context->client_metadata().empty()) {
225 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
226 iter = context->client_metadata().begin();
227 iter != context->client_metadata().end(); ++iter) {
228 context->AddInitialMetadata(ToString(iter->first),
229 ToString(iter->second));
230 }
231 }
232 context->AddTrailingMetadata("trailing_key", "trailing_value");
233 while (reader->Read(&request)) {
234 response->mutable_message()->append(request.message());
235 }
236
237 return Status::OK;
238 }
239
240 Status ResponseStream(ServerContext* context, const EchoRequest* request,
241 ServerWriter<EchoResponse>* writer) override {
242 if (!context->client_metadata().empty()) {
243 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
244 iter = context->client_metadata().begin();
245 iter != context->client_metadata().end(); ++iter) {
246 context->AddInitialMetadata(ToString(iter->first),
247 ToString(iter->second));
248 }
249 }
250 context->AddTrailingMetadata("trailing_key", "trailing_value");
251
252 EchoResponse response;
253 for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {
254 response.set_message(request->message() + std::to_string(i));
255 writer->Write(response);
256 }
257
258 return Status::OK;
259 }
260
261 Status BidiStream(
262 ServerContext* context,
263 ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
264 EchoRequest request;
265 EchoResponse response;
266 if (!context->client_metadata().empty()) {
267 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
268 iter = context->client_metadata().begin();
269 iter != context->client_metadata().end(); ++iter) {
270 context->AddInitialMetadata(ToString(iter->first),
271 ToString(iter->second));
272 }
273 }
274 context->AddTrailingMetadata("trailing_key", "trailing_value");
275
276 while (stream->Read(&request)) {
277 response.set_message(request.message());
278 stream->Write(response);
279 }
280
281 return Status::OK;
282 }
283 };
284
285 } // namespace
286
287 class GrpcToolTest : public ::testing::Test {
288 protected:
289 GrpcToolTest() {}
290
291 // SetUpServer cannot be used with EXPECT_EXIT. grpc_pick_unused_port_or_die()
292 // uses atexit() to free chosen ports, and it will spawn a new thread in
293 // resolve_address_posix.c:192 at exit time.
294 std::string SetUpServer(bool secure = false) {
295 std::ostringstream server_address;
296 int port = grpc_pick_unused_port_or_die();
297 server_address << "localhost:" << port;
298 // Setup server
299 ServerBuilder builder;
300 std::shared_ptr<grpc::ServerCredentials> creds;
301 grpc_slice cert_slice, key_slice;
302 GPR_ASSERT(GRPC_LOG_IF_ERROR(
303 "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
304 GPR_ASSERT(GRPC_LOG_IF_ERROR(
305 "load_file", grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
306 const char* server_cert =
307 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
308 const char* server_key =
309 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
310 SslServerCredentialsOptions::PemKeyCertPair pkcp = {server_key,
311 server_cert};
312 if (secure) {
313 SslServerCredentialsOptions ssl_opts;
314 ssl_opts.pem_root_certs = "";
315 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
316 creds = SslServerCredentials(ssl_opts);
317 } else {
318 creds = InsecureServerCredentials();
319 }
320 builder.AddListeningPort(server_address.str(), creds);
321 builder.RegisterService(&service_);
322 server_ = builder.BuildAndStart();
323 grpc_slice_unref(cert_slice);
324 grpc_slice_unref(key_slice);
325 return server_address.str();
326 }
327
328 void ShutdownServer() { server_->Shutdown(); }
329
330 std::unique_ptr<Server> server_;
331 TestServiceImpl service_;
332 reflection::ProtoServerReflectionPlugin plugin_;
333 };
334
335 TEST_F(GrpcToolTest, NoCommand) {
336 // Test input "grpc_cli"
337 std::stringstream output_stream;
338 const char* argv[] = {"grpc_cli"};
339 // Exit with 1, print usage instruction in stderr
340 EXPECT_EXIT(
341 GrpcToolMainLib(
342 ArraySize(argv), argv, TestCliCredentials(),
343 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
344 ::testing::ExitedWithCode(1), "No command specified\n" USAGE_REGEX);
345 // No output
346 EXPECT_TRUE(0 == output_stream.tellp());
347 }
348
349 TEST_F(GrpcToolTest, InvalidCommand) {
350 // Test input "grpc_cli"
351 std::stringstream output_stream;
352 const char* argv[] = {"grpc_cli", "abc"};
353 // Exit with 1, print usage instruction in stderr
354 EXPECT_EXIT(
355 GrpcToolMainLib(
356 ArraySize(argv), argv, TestCliCredentials(),
357 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
358 ::testing::ExitedWithCode(1), "Invalid command 'abc'\n" USAGE_REGEX);
359 // No output
360 EXPECT_TRUE(0 == output_stream.tellp());
361 }
362
363 TEST_F(GrpcToolTest, HelpCommand) {
364 // Test input "grpc_cli help"
365 std::stringstream output_stream;
366 const char* argv[] = {"grpc_cli", "help"};
367 // Exit with 1, print usage instruction in stderr
368 EXPECT_EXIT(GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
369 std::bind(PrintStream, &output_stream,
370 std::placeholders::_1)),
371 ::testing::ExitedWithCode(1), USAGE_REGEX);
372 // No output
373 EXPECT_TRUE(0 == output_stream.tellp());
374 }
375
376 TEST_F(GrpcToolTest, ListCommand) {
377 // Test input "grpc_cli list localhost:<port>"
378 std::stringstream output_stream;
379
380 const std::string server_address = SetUpServer();
381 const char* argv[] = {"grpc_cli", "ls", server_address.c_str()};
382
383 absl::SetFlag(&FLAGS_l, false);
384 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
385 std::bind(PrintStream, &output_stream,
386 std::placeholders::_1)));
387 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
388 "grpc.testing.EchoTestService\n"
389 "grpc.reflection.v1alpha.ServerReflection\n"));
390
391 ShutdownServer();
392 }
393
394 TEST_F(GrpcToolTest, ListOneService) {
395 // Test input "grpc_cli list localhost:<port> grpc.testing.EchoTestService"
396 std::stringstream output_stream;
397
398 const std::string server_address = SetUpServer();
399 const char* argv[] = {"grpc_cli", "ls", server_address.c_str(),
400 "grpc.testing.EchoTestService"};
401 // without -l flag
402 absl::SetFlag(&FLAGS_l, false);
403 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
404 std::bind(PrintStream, &output_stream,
405 std::placeholders::_1)));
406 // Expected output: ECHO_TEST_SERVICE_SUMMARY
407 EXPECT_TRUE(0 ==
408 strcmp(output_stream.str().c_str(), ECHO_TEST_SERVICE_SUMMARY));
409
410 // with -l flag
411 output_stream.str(std::string());
412 output_stream.clear();
413 absl::SetFlag(&FLAGS_l, true);
414 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
415 std::bind(PrintStream, &output_stream,
416 std::placeholders::_1)));
417 // Expected output: ECHO_TEST_SERVICE_DESCRIPTION
418 EXPECT_TRUE(
419 0 == strcmp(output_stream.str().c_str(), ECHO_TEST_SERVICE_DESCRIPTION));
420
421 ShutdownServer();
422 }
423
424 TEST_F(GrpcToolTest, TypeCommand) {
425 // Test input "grpc_cli type localhost:<port> grpc.testing.EchoRequest"
426 std::stringstream output_stream;
427
428 const std::string server_address = SetUpServer();
429 const char* argv[] = {"grpc_cli", "type", server_address.c_str(),
430 "grpc.testing.EchoRequest"};
431
432 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
433 std::bind(PrintStream, &output_stream,
434 std::placeholders::_1)));
435 const grpc::protobuf::Descriptor* desc =
436 grpc::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(
437 "grpc.testing.EchoRequest");
438 // Expected output: the DebugString of grpc.testing.EchoRequest
439 EXPECT_TRUE(0 ==
440 strcmp(output_stream.str().c_str(), desc->DebugString().c_str()));
441
442 ShutdownServer();
443 }
444
445 TEST_F(GrpcToolTest, ListOneMethod) {
446 // Test input "grpc_cli list localhost:<port> grpc.testing.EchoTestService"
447 std::stringstream output_stream;
448
449 const std::string server_address = SetUpServer();
450 const char* argv[] = {"grpc_cli", "ls", server_address.c_str(),
451 "grpc.testing.EchoTestService.Echo"};
452 // without -l flag
453 absl::SetFlag(&FLAGS_l, false);
454 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
455 std::bind(PrintStream, &output_stream,
456 std::placeholders::_1)));
457 // Expected output: "Echo"
458 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), "Echo\n"));
459
460 // with -l flag
461 output_stream.str(std::string());
462 output_stream.clear();
463 absl::SetFlag(&FLAGS_l, true);
464 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
465 std::bind(PrintStream, &output_stream,
466 std::placeholders::_1)));
467 // Expected output: ECHO_METHOD_DESCRIPTION
468 EXPECT_TRUE(0 ==
469 strcmp(output_stream.str().c_str(), ECHO_METHOD_DESCRIPTION));
470
471 ShutdownServer();
472 }
473
474 TEST_F(GrpcToolTest, TypeNotFound) {
475 // Test input "grpc_cli type localhost:<port> grpc.testing.PhonyRequest"
476 std::stringstream output_stream;
477
478 const std::string server_address = SetUpServer();
479 const char* argv[] = {"grpc_cli", "type", server_address.c_str(),
480 "grpc.testing.PhonyRequest"};
481
482 EXPECT_TRUE(1 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
483 std::bind(PrintStream, &output_stream,
484 std::placeholders::_1)));
485 ShutdownServer();
486 }
487
488 TEST_F(GrpcToolTest, CallCommand) {
489 // Test input "grpc_cli call localhost:<port> Echo "message: 'Hello'"
490 std::stringstream output_stream;
491
492 const std::string server_address = SetUpServer();
493 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
494 "message: 'Hello'"};
495
496 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
497 std::bind(PrintStream, &output_stream,
498 std::placeholders::_1)));
499 // Expected output: "message: \"Hello\""
500 EXPECT_TRUE(nullptr !=
501 strstr(output_stream.str().c_str(), "message: \"Hello\""));
502
503 // with json_output
504 output_stream.str(std::string());
505 output_stream.clear();
506
507 // TODO(Capstan): Consider using absl::FlagSaver
508 absl::SetFlag(&FLAGS_json_output, true);
509 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
510 std::bind(PrintStream, &output_stream,
511 std::placeholders::_1)));
512 absl::SetFlag(&FLAGS_json_output, false);
513
514 // Expected output:
515 // {
516 // "message": "Hello"
517 // }
518 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
519 "{\n \"message\": \"Hello\"\n}"));
520
521 ShutdownServer();
522 }
523
524 TEST_F(GrpcToolTest, CallCommandJsonInput) {
525 // Test input "grpc_cli call localhost:<port> Echo "{ \"message\": \"Hello\"}"
526 std::stringstream output_stream;
527
528 const std::string server_address = SetUpServer();
529 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
530 "{ \"message\": \"Hello\"}"};
531
532 absl::SetFlag(&FLAGS_json_input, true);
533 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
534 std::bind(PrintStream, &output_stream,
535 std::placeholders::_1)));
536 // Expected output: "message: \"Hello\""
537 EXPECT_TRUE(nullptr !=
538 strstr(output_stream.str().c_str(), "message: \"Hello\""));
539
540 // with json_output
541 output_stream.str(std::string());
542 output_stream.clear();
543
544 absl::SetFlag(&FLAGS_json_output, true);
545 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
546 std::bind(PrintStream, &output_stream,
547 std::placeholders::_1)));
548 absl::SetFlag(&FLAGS_json_output, false);
549 absl::SetFlag(&FLAGS_json_input, false);
550
551 // Expected output:
552 // {
553 // "message": "Hello"
554 // }
555 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
556 "{\n \"message\": \"Hello\"\n}"));
557
558 ShutdownServer();
559 }
560
561 TEST_F(GrpcToolTest, CallCommandBatch) {
562 // Test input "grpc_cli call Echo"
563 std::stringstream output_stream;
564
565 const std::string server_address = SetUpServer();
566 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
567 "message: 'Hello0'"};
568
569 // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
570 std::streambuf* orig = std::cin.rdbuf();
571 std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");
572 std::cin.rdbuf(ss.rdbuf());
573
574 absl::SetFlag(&FLAGS_batch, true);
575 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
576 std::bind(PrintStream, &output_stream,
577 std::placeholders::_1)));
578 absl::SetFlag(&FLAGS_batch, false);
579
580 // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
581 // "Hello2"\n"
582 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
583 "message: \"Hello0\"\nmessage: "
584 "\"Hello1\"\nmessage: \"Hello2\"\n"));
585 // with json_output
586 output_stream.str(std::string());
587 output_stream.clear();
588 ss.clear();
589 ss.seekg(0);
590 std::cin.rdbuf(ss.rdbuf());
591
592 absl::SetFlag(&FLAGS_batch, true);
593 absl::SetFlag(&FLAGS_json_output, true);
594 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
595 std::bind(PrintStream, &output_stream,
596 std::placeholders::_1)));
597 absl::SetFlag(&FLAGS_json_output, false);
598 absl::SetFlag(&FLAGS_batch, false);
599
600 // Expected output:
601 // {
602 // "message": "Hello0"
603 // }
604 // {
605 // "message": "Hello1"
606 // }
607 // {
608 // "message": "Hello2"
609 // }
610 // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
611 // "Hello2"\n"
612 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
613 "{\n \"message\": \"Hello0\"\n}\n"
614 "{\n \"message\": \"Hello1\"\n}\n"
615 "{\n \"message\": \"Hello2\"\n}\n"));
616
617 std::cin.rdbuf(orig);
618 ShutdownServer();
619 }
620
621 TEST_F(GrpcToolTest, CallCommandBatchJsonInput) {
622 // Test input "grpc_cli call Echo"
623 std::stringstream output_stream;
624
625 const std::string server_address = SetUpServer();
626 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
627 "{\"message\": \"Hello0\"}"};
628
629 // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
630 std::streambuf* orig = std::cin.rdbuf();
631 std::istringstream ss(
632 "{\"message\": \"Hello1\"}\n\n{\"message\": \"Hello2\" }\n\n");
633 std::cin.rdbuf(ss.rdbuf());
634
635 absl::SetFlag(&FLAGS_json_input, true);
636 absl::SetFlag(&FLAGS_batch, true);
637 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
638 std::bind(PrintStream, &output_stream,
639 std::placeholders::_1)));
640 absl::SetFlag(&FLAGS_batch, false);
641
642 // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
643 // "Hello2"\n"
644 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
645 "message: \"Hello0\"\nmessage: "
646 "\"Hello1\"\nmessage: \"Hello2\"\n"));
647 // with json_output
648 output_stream.str(std::string());
649 output_stream.clear();
650 ss.clear();
651 ss.seekg(0);
652 std::cin.rdbuf(ss.rdbuf());
653
654 absl::SetFlag(&FLAGS_batch, true);
655 absl::SetFlag(&FLAGS_json_output, true);
656 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
657 std::bind(PrintStream, &output_stream,
658 std::placeholders::_1)));
659 absl::SetFlag(&FLAGS_json_output, false);
660 absl::SetFlag(&FLAGS_batch, false);
661 absl::SetFlag(&FLAGS_json_input, false);
662
663 // Expected output:
664 // {
665 // "message": "Hello0"
666 // }
667 // {
668 // "message": "Hello1"
669 // }
670 // {
671 // "message": "Hello2"
672 // }
673 // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
674 // "Hello2"\n"
675 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
676 "{\n \"message\": \"Hello0\"\n}\n"
677 "{\n \"message\": \"Hello1\"\n}\n"
678 "{\n \"message\": \"Hello2\"\n}\n"));
679
680 std::cin.rdbuf(orig);
681 ShutdownServer();
682 }
683
684 TEST_F(GrpcToolTest, CallCommandBatchWithBadRequest) {
685 // Test input "grpc_cli call Echo"
686 std::stringstream output_stream;
687
688 const std::string server_address = SetUpServer();
689 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
690 "message: 'Hello0'"};
691
692 // Mock std::cin input "message: 1\n\n message: 'Hello2'\n\n"
693 std::streambuf* orig = std::cin.rdbuf();
694 std::istringstream ss("message: 1\n\n message: 'Hello2'\n\n");
695 std::cin.rdbuf(ss.rdbuf());
696
697 absl::SetFlag(&FLAGS_batch, true);
698 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
699 std::bind(PrintStream, &output_stream,
700 std::placeholders::_1)));
701 absl::SetFlag(&FLAGS_batch, false);
702
703 // Expected output: "message: "Hello0"\nmessage: "Hello2"\n"
704 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
705 "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
706
707 // with json_output
708 output_stream.str(std::string());
709 output_stream.clear();
710 ss.clear();
711 ss.seekg(0);
712 std::cin.rdbuf(ss.rdbuf());
713
714 absl::SetFlag(&FLAGS_batch, true);
715 absl::SetFlag(&FLAGS_json_output, true);
716 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
717 std::bind(PrintStream, &output_stream,
718 std::placeholders::_1)));
719 absl::SetFlag(&FLAGS_json_output, false);
720 absl::SetFlag(&FLAGS_batch, false);
721
722 // Expected output:
723 // {
724 // "message": "Hello0"
725 // }
726 // {
727 // "message": "Hello2"
728 // }
729 // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
730 // "Hello2"\n"
731 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
732 "{\n \"message\": \"Hello0\"\n}\n"
733 "{\n \"message\": \"Hello2\"\n}\n"));
734
735 std::cin.rdbuf(orig);
736 ShutdownServer();
737 }
738
739 TEST_F(GrpcToolTest, CallCommandBatchJsonInputWithBadRequest) {
740 // Test input "grpc_cli call Echo"
741 std::stringstream output_stream;
742
743 const std::string server_address = SetUpServer();
744 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
745 "{ \"message\": \"Hello0\"}"};
746
747 // Mock std::cin input "message: 1\n\n message: 'Hello2'\n\n"
748 std::streambuf* orig = std::cin.rdbuf();
749 std::istringstream ss(
750 "{ \"message\": 1 }\n\n { \"message\": \"Hello2\" }\n\n");
751 std::cin.rdbuf(ss.rdbuf());
752
753 absl::SetFlag(&FLAGS_batch, true);
754 absl::SetFlag(&FLAGS_json_input, true);
755 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
756 std::bind(PrintStream, &output_stream,
757 std::placeholders::_1)));
758 absl::SetFlag(&FLAGS_json_input, false);
759 absl::SetFlag(&FLAGS_batch, false);
760
761 // Expected output: "message: "Hello0"\nmessage: "Hello2"\n"
762 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
763 "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
764
765 // with json_output
766 output_stream.str(std::string());
767 output_stream.clear();
768 ss.clear();
769 ss.seekg(0);
770 std::cin.rdbuf(ss.rdbuf());
771
772 absl::SetFlag(&FLAGS_batch, true);
773 absl::SetFlag(&FLAGS_json_input, true);
774 absl::SetFlag(&FLAGS_json_output, true);
775 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
776 std::bind(PrintStream, &output_stream,
777 std::placeholders::_1)));
778 absl::SetFlag(&FLAGS_json_output, false);
779 absl::SetFlag(&FLAGS_json_input, false);
780 absl::SetFlag(&FLAGS_batch, false);
781
782 // Expected output:
783 // {
784 // "message": "Hello0"
785 // }
786 // {
787 // "message": "Hello2"
788 // }
789 // Expected output: "message: "Hello0"\nmessage: "Hello1"\nmessage:
790 // "Hello2"\n"
791 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
792 "{\n \"message\": \"Hello0\"\n}\n"
793 "{\n \"message\": \"Hello2\"\n}\n"));
794
795 std::cin.rdbuf(orig);
796 ShutdownServer();
797 }
798
799 TEST_F(GrpcToolTest, CallCommandRequestStream) {
800 // Test input: grpc_cli call localhost:<port> RequestStream "message:
801 // 'Hello0'"
802 std::stringstream output_stream;
803
804 const std::string server_address = SetUpServer();
805 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
806 "RequestStream", "message: 'Hello0'"};
807
808 // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
809 std::streambuf* orig = std::cin.rdbuf();
810 std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");
811 std::cin.rdbuf(ss.rdbuf());
812
813 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
814 std::bind(PrintStream, &output_stream,
815 std::placeholders::_1)));
816
817 // Expected output: "message: \"Hello0Hello1Hello2\""
818 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
819 "message: \"Hello0Hello1Hello2\""));
820 std::cin.rdbuf(orig);
821 ShutdownServer();
822 }
823
824 TEST_F(GrpcToolTest, CallCommandRequestStreamJsonInput) {
825 // Test input: grpc_cli call localhost:<port> RequestStream "{ \"message\":
826 // \"Hello0\"}"
827 std::stringstream output_stream;
828
829 const std::string server_address = SetUpServer();
830 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
831 "RequestStream", "{ \"message\": \"Hello0\" }"};
832
833 // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
834 std::streambuf* orig = std::cin.rdbuf();
835 std::istringstream ss(
836 "{ \"message\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
837 std::cin.rdbuf(ss.rdbuf());
838
839 absl::SetFlag(&FLAGS_json_input, true);
840 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
841 std::bind(PrintStream, &output_stream,
842 std::placeholders::_1)));
843 absl::SetFlag(&FLAGS_json_input, false);
844
845 // Expected output: "message: \"Hello0Hello1Hello2\""
846 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
847 "message: \"Hello0Hello1Hello2\""));
848 std::cin.rdbuf(orig);
849 ShutdownServer();
850 }
851
852 TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequest) {
853 // Test input: grpc_cli call localhost:<port> RequestStream "message:
854 // 'Hello0'"
855 std::stringstream output_stream;
856
857 const std::string server_address = SetUpServer();
858 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
859 "RequestStream", "message: 'Hello0'"};
860
861 // Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"
862 std::streambuf* orig = std::cin.rdbuf();
863 std::istringstream ss("bad_field: 'Hello1'\n\n message: 'Hello2'\n\n");
864 std::cin.rdbuf(ss.rdbuf());
865
866 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
867 std::bind(PrintStream, &output_stream,
868 std::placeholders::_1)));
869
870 // Expected output: "message: \"Hello0Hello2\""
871 EXPECT_TRUE(nullptr !=
872 strstr(output_stream.str().c_str(), "message: \"Hello0Hello2\""));
873 std::cin.rdbuf(orig);
874 ShutdownServer();
875 }
876
877 TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequestJsonInput) {
878 // Test input: grpc_cli call localhost:<port> RequestStream "message:
879 // 'Hello0'"
880 std::stringstream output_stream;
881
882 const std::string server_address = SetUpServer();
883 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
884 "RequestStream", "{ \"message\": \"Hello0\" }"};
885
886 // Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"
887 std::streambuf* orig = std::cin.rdbuf();
888 std::istringstream ss(
889 "{ \"bad_field\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
890 std::cin.rdbuf(ss.rdbuf());
891
892 absl::SetFlag(&FLAGS_json_input, true);
893 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
894 std::bind(PrintStream, &output_stream,
895 std::placeholders::_1)));
896 absl::SetFlag(&FLAGS_json_input, false);
897
898 // Expected output: "message: \"Hello0Hello2\""
899 EXPECT_TRUE(nullptr !=
900 strstr(output_stream.str().c_str(), "message: \"Hello0Hello2\""));
901 std::cin.rdbuf(orig);
902 ShutdownServer();
903 }
904
905 TEST_F(GrpcToolTest, CallCommandWithTimeoutDeadlineSet) {
906 // Test input "grpc_cli call CheckDeadlineSet --timeout=5000.25"
907 std::stringstream output_stream;
908
909 const std::string server_address = SetUpServer();
910 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
911 "CheckDeadlineSet"};
912
913 // Set timeout to 5000.25 seconds
914 absl::SetFlag(&FLAGS_timeout, 5000.25);
915
916 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
917 std::bind(PrintStream, &output_stream,
918 std::placeholders::_1)));
919
920 // Expected output: "message: "true"", deadline set
921 EXPECT_TRUE(nullptr !=
922 strstr(output_stream.str().c_str(), "message: \"true\""));
923 ShutdownServer();
924 }
925
926 TEST_F(GrpcToolTest, CallCommandWithTimeoutDeadlineUpperBound) {
927 // Test input "grpc_cli call CheckDeadlineUpperBound --timeout=900"
928 std::stringstream output_stream;
929
930 const std::string server_address = SetUpServer();
931 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
932 "CheckDeadlineUpperBound"};
933
934 // Set timeout to 900 seconds
935 absl::SetFlag(&FLAGS_timeout, 900);
936
937 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
938 std::bind(PrintStream, &output_stream,
939 std::placeholders::_1)));
940
941 std::string output = output_stream.str();
942
943 // Expected output: "message: "true""
944 // deadline not greater than timeout + current time
945 EXPECT_TRUE(nullptr != strstr(output.c_str(), "message: \"true\"")) << output;
946 ShutdownServer();
947 }
948
949 TEST_F(GrpcToolTest, CallCommandWithNegativeTimeoutValue) {
950 // Test input "grpc_cli call CheckDeadlineSet --timeout=-5"
951 std::stringstream output_stream;
952
953 const std::string server_address = SetUpServer();
954 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
955 "CheckDeadlineSet"};
956
957 // Set timeout to -5 (deadline not set)
958 absl::SetFlag(&FLAGS_timeout, -5);
959
960 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
961 std::bind(PrintStream, &output_stream,
962 std::placeholders::_1)));
963
964 // Expected output: "message: "false"", deadline not set
965 EXPECT_TRUE(nullptr !=
966 strstr(output_stream.str().c_str(), "message: \"false\""));
967
968 ShutdownServer();
969 }
970
971 TEST_F(GrpcToolTest, CallCommandWithDefaultTimeoutValue) {
972 // Test input "grpc_cli call CheckDeadlineSet --timeout=-1"
973 std::stringstream output_stream;
974
975 const std::string server_address = SetUpServer();
976 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
977 "CheckDeadlineSet"};
978
979 // Set timeout to -1 (default value, deadline not set)
980 absl::SetFlag(&FLAGS_timeout, -1);
981
982 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
983 std::bind(PrintStream, &output_stream,
984 std::placeholders::_1)));
985
986 // Expected output: "message: "false"", deadline not set
987 EXPECT_TRUE(nullptr !=
988 strstr(output_stream.str().c_str(), "message: \"false\""));
989
990 ShutdownServer();
991 }
992
993 TEST_F(GrpcToolTest, CallCommandResponseStream) {
994 // Test input: grpc_cli call localhost:<port> ResponseStream "message:
995 // 'Hello'"
996 std::stringstream output_stream;
997
998 const std::string server_address = SetUpServer();
999 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1000 "ResponseStream", "message: 'Hello'"};
1001
1002 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1003 std::bind(PrintStream, &output_stream,
1004 std::placeholders::_1)));
1005
1006 // Expected output: "message: \"Hello{n}\""
1007 for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {
1008 std::string expected_response_text =
1009 "message: \"Hello" + std::to_string(i) + "\"\n";
1010 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
1011 expected_response_text.c_str()));
1012 }
1013
1014 // with json_output
1015 output_stream.str(std::string());
1016 output_stream.clear();
1017
1018 absl::SetFlag(&FLAGS_json_output, true);
1019 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1020 std::bind(PrintStream, &output_stream,
1021 std::placeholders::_1)));
1022 absl::SetFlag(&FLAGS_json_output, false);
1023
1024 // Expected output: "{\n \"message\": \"Hello{n}\"\n}\n"
1025 for (int i = 0; i < kServerDefaultResponseStreamsToSend; i++) {
1026 std::string expected_response_text =
1027 "{\n \"message\": \"Hello" + std::to_string(i) + "\"\n}\n";
1028 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
1029 expected_response_text.c_str()));
1030 }
1031
1032 ShutdownServer();
1033 }
1034
1035 TEST_F(GrpcToolTest, CallCommandBidiStream) {
1036 // Test input: grpc_cli call localhost:<port> BidiStream "message: 'Hello0'"
1037 std::stringstream output_stream;
1038
1039 const std::string server_address = SetUpServer();
1040 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1041 "BidiStream", "message: 'Hello0'"};
1042
1043 // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
1044 std::streambuf* orig = std::cin.rdbuf();
1045 std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");
1046 std::cin.rdbuf(ss.rdbuf());
1047
1048 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1049 std::bind(PrintStream, &output_stream,
1050 std::placeholders::_1)));
1051
1052 // Expected output: "message: \"Hello0\"\nmessage: \"Hello1\"\nmessage:
1053 // \"Hello2\"\n\n"
1054 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
1055 "message: \"Hello0\"\nmessage: "
1056 "\"Hello1\"\nmessage: \"Hello2\"\n"));
1057 std::cin.rdbuf(orig);
1058 ShutdownServer();
1059 }
1060
1061 TEST_F(GrpcToolTest, CallCommandBidiStreamWithBadRequest) {
1062 // Test input: grpc_cli call localhost:<port> BidiStream "message: 'Hello0'"
1063 std::stringstream output_stream;
1064
1065 const std::string server_address = SetUpServer();
1066 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1067 "BidiStream", "message: 'Hello0'"};
1068
1069 // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
1070 std::streambuf* orig = std::cin.rdbuf();
1071 std::istringstream ss("message: 1.0\n\n message: 'Hello2'\n\n");
1072 std::cin.rdbuf(ss.rdbuf());
1073
1074 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1075 std::bind(PrintStream, &output_stream,
1076 std::placeholders::_1)));
1077
1078 // Expected output: "message: \"Hello0\"\nmessage: \"Hello1\"\nmessage:
1079 // \"Hello2\"\n\n"
1080 EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
1081 "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
1082 std::cin.rdbuf(orig);
1083
1084 ShutdownServer();
1085 }
1086
1087 TEST_F(GrpcToolTest, ParseCommand) {
1088 // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse
1089 // ECHO_RESPONSE_MESSAGE"
1090 std::stringstream output_stream;
1091 std::stringstream binary_output_stream;
1092
1093 const std::string server_address = SetUpServer();
1094 const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
1095 "grpc.testing.EchoResponse",
1096 ECHO_RESPONSE_MESSAGE_TEXT_FORMAT};
1097
1098 absl::SetFlag(&FLAGS_binary_input, false);
1099 absl::SetFlag(&FLAGS_binary_output, false);
1100 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1101 std::bind(PrintStream, &output_stream,
1102 std::placeholders::_1)));
1103 // Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT
1104 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1105 ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
1106
1107 // with json_output
1108 output_stream.str(std::string());
1109 output_stream.clear();
1110
1111 absl::SetFlag(&FLAGS_json_output, true);
1112 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1113 std::bind(PrintStream, &output_stream,
1114 std::placeholders::_1)));
1115 absl::SetFlag(&FLAGS_json_output, false);
1116
1117 // Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT
1118 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1119 ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
1120
1121 // Parse text message to binary message and then parse it back to text message
1122 output_stream.str(std::string());
1123 output_stream.clear();
1124 absl::SetFlag(&FLAGS_binary_output, true);
1125 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1126 std::bind(PrintStream, &output_stream,
1127 std::placeholders::_1)));
1128 std::string binary_data = output_stream.str();
1129 output_stream.str(std::string());
1130 output_stream.clear();
1131 argv[4] = binary_data.c_str();
1132 absl::SetFlag(&FLAGS_binary_input, true);
1133 absl::SetFlag(&FLAGS_binary_output, false);
1134 EXPECT_TRUE(0 == GrpcToolMainLib(5, argv, TestCliCredentials(),
1135 std::bind(PrintStream, &output_stream,
1136 std::placeholders::_1)));
1137
1138 // Expected output: ECHO_RESPONSE_MESSAGE
1139 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1140 ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
1141
1142 absl::SetFlag(&FLAGS_binary_input, false);
1143 absl::SetFlag(&FLAGS_binary_output, false);
1144 ShutdownServer();
1145 }
1146
1147 TEST_F(GrpcToolTest, ParseCommandJsonFormat) {
1148 // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse
1149 // ECHO_RESPONSE_MESSAGE_JSON_FORMAT"
1150 std::stringstream output_stream;
1151 std::stringstream binary_output_stream;
1152
1153 const std::string server_address = SetUpServer();
1154 const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
1155 "grpc.testing.EchoResponse",
1156 ECHO_RESPONSE_MESSAGE_JSON_FORMAT};
1157
1158 absl::SetFlag(&FLAGS_json_input, true);
1159 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1160 std::bind(PrintStream, &output_stream,
1161 std::placeholders::_1)));
1162
1163 // Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT
1164 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1165 ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
1166
1167 // with json_output
1168 output_stream.str(std::string());
1169 output_stream.clear();
1170
1171 absl::SetFlag(&FLAGS_json_output, true);
1172 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1173 std::bind(PrintStream, &output_stream,
1174 std::placeholders::_1)));
1175 absl::SetFlag(&FLAGS_json_output, false);
1176 absl::SetFlag(&FLAGS_json_input, false);
1177
1178 // Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT
1179 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1180 ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
1181
1182 ShutdownServer();
1183 }
1184
1185 TEST_F(GrpcToolTest, TooFewArguments) {
1186 // Test input "grpc_cli call Echo"
1187 std::stringstream output_stream;
1188 const char* argv[] = {"grpc_cli", "call", "Echo"};
1189
1190 // Exit with 1
1191 EXPECT_EXIT(
1192 GrpcToolMainLib(
1193 ArraySize(argv), argv, TestCliCredentials(),
1194 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1195 ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*");
1196 // No output
1197 EXPECT_TRUE(0 == output_stream.tellp());
1198 }
1199
1200 TEST_F(GrpcToolTest, TooManyArguments) {
1201 // Test input "grpc_cli call localhost:<port> Echo Echo "message: 'Hello'"
1202 std::stringstream output_stream;
1203 const char* argv[] = {"grpc_cli", "call", "localhost:10000",
1204 "Echo", "Echo", "message: 'Hello'"};
1205
1206 // Exit with 1
1207 EXPECT_EXIT(
1208 GrpcToolMainLib(
1209 ArraySize(argv), argv, TestCliCredentials(),
1210 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1211 ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*");
1212 // No output
1213 EXPECT_TRUE(0 == output_stream.tellp());
1214 }
1215
1216 TEST_F(GrpcToolTest, CallCommandWithMetadata) {
1217 // Test input "grpc_cli call localhost:<port> Echo "message: 'Hello'"
1218 const std::string server_address = SetUpServer();
1219 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
1220 "message: 'Hello'"};
1221
1222 {
1223 std::stringstream output_stream;
1224 absl::SetFlag(&FLAGS_metadata, "key0:val0:key1:valq:key2:val2");
1225 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv,
1226 TestCliCredentials(),
1227 std::bind(PrintStream, &output_stream,
1228 std::placeholders::_1)));
1229 // Expected output: "message: \"Hello\""
1230 EXPECT_TRUE(nullptr !=
1231 strstr(output_stream.str().c_str(), "message: \"Hello\""));
1232 }
1233
1234 {
1235 std::stringstream output_stream;
1236 absl::SetFlag(&FLAGS_metadata, "key:val\\:val");
1237 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv,
1238 TestCliCredentials(),
1239 std::bind(PrintStream, &output_stream,
1240 std::placeholders::_1)));
1241 // Expected output: "message: \"Hello\""
1242 EXPECT_TRUE(nullptr !=
1243 strstr(output_stream.str().c_str(), "message: \"Hello\""));
1244 }
1245
1246 {
1247 std::stringstream output_stream;
1248 absl::SetFlag(&FLAGS_metadata, "key:val\\\\val");
1249 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv,
1250 TestCliCredentials(),
1251 std::bind(PrintStream, &output_stream,
1252 std::placeholders::_1)));
1253 // Expected output: "message: \"Hello\""
1254 EXPECT_TRUE(nullptr !=
1255 strstr(output_stream.str().c_str(), "message: \"Hello\""));
1256 }
1257
1258 absl::SetFlag(&FLAGS_metadata, "");
1259 ShutdownServer();
1260 }
1261
1262 TEST_F(GrpcToolTest, CallCommandWithBadMetadata) {
1263 // Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'"
1264 const char* argv[] = {"grpc_cli", "call", "localhost:10000",
1265 "grpc.testing.EchoTestService.Echo",
1266 "message: 'Hello'"};
1267 absl::SetFlag(&FLAGS_protofiles, "src/proto/grpc/testing/echo.proto");
1268 auto test_srcdir = grpc_core::GetEnv("TEST_SRCDIR");
1269 if (test_srcdir.has_value()) {
1270 absl::SetFlag(&FLAGS_proto_path,
1271 *test_srcdir + std::string("/com_github_grpc_grpc"));
1272 }
1273
1274 {
1275 std::stringstream output_stream;
1276 absl::SetFlag(&FLAGS_metadata, "key0:val0:key1");
1277 // Exit with 1
1278 EXPECT_EXIT(
1279 GrpcToolMainLib(
1280 ArraySize(argv), argv, TestCliCredentials(),
1281 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1282 ::testing::ExitedWithCode(1), ".*Failed to parse metadata flag.*");
1283 }
1284
1285 {
1286 std::stringstream output_stream;
1287 absl::SetFlag(&FLAGS_metadata, "key:val\\val");
1288 // Exit with 1
1289 EXPECT_EXIT(
1290 GrpcToolMainLib(
1291 ArraySize(argv), argv, TestCliCredentials(),
1292 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1293 ::testing::ExitedWithCode(1), ".*Failed to parse metadata flag.*");
1294 }
1295
1296 absl::SetFlag(&FLAGS_metadata, "");
1297 absl::SetFlag(&FLAGS_protofiles, "");
1298 }
1299
1300 TEST_F(GrpcToolTest, CallMaxRecvMessageSizeSmall) {
1301 std::stringstream output_stream;
1302 const std::string server_address = SetUpServer();
1303 // Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'
1304 // --max_recv_msg_size=4"
1305 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1306 "grpc.testing.EchoTestService.Echo",
1307 "message: 'Hello'"};
1308
1309 // Set max_recv_msg_size to 4 which is not enough.
1310 absl::SetFlag(&FLAGS_max_recv_msg_size, 4);
1311
1312 // This should fail.
1313 EXPECT_FALSE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1314 std::bind(PrintStream, &output_stream,
1315 std::placeholders::_1)));
1316 // No output expected.
1317 EXPECT_TRUE(0 == output_stream.tellp());
1318 ShutdownServer();
1319 }
1320
1321 TEST_F(GrpcToolTest, CallMaxRecvMessageSizeEnough) {
1322 std::stringstream output_stream;
1323 const std::string server_address = SetUpServer();
1324 // Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'
1325 // --max_recv_msg_size=1048576"
1326 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1327 "grpc.testing.EchoTestService.Echo",
1328 "message: 'Hello'"};
1329
1330 // Set max_recv_msg_size to a large enough number.
1331 absl::SetFlag(&FLAGS_max_recv_msg_size, 1024 * 1024);
1332
1333 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1334 std::bind(PrintStream, &output_stream,
1335 std::placeholders::_1)));
1336 // Expected output: "message: \"Hello\""
1337 EXPECT_TRUE(nullptr !=
1338 strstr(output_stream.str().c_str(), "message: \"Hello\""));
1339 ShutdownServer();
1340 }
1341
1342 TEST_F(GrpcToolTest, CallMaxRecvMessageSizeDefault) {
1343 std::stringstream output_stream;
1344 const std::string server_address = SetUpServer();
1345 // Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'
1346 // --max_recv_msg_size=0"
1347 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1348 "grpc.testing.EchoTestService.Echo",
1349 "message: 'Hello'"};
1350
1351 // Set max_recv_msg_size to gRPC default, which should suffice.
1352 absl::SetFlag(&FLAGS_max_recv_msg_size, 0);
1353
1354 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1355 std::bind(PrintStream, &output_stream,
1356 std::placeholders::_1)));
1357 // Expected output: "message: \"Hello\""
1358 EXPECT_TRUE(nullptr !=
1359 strstr(output_stream.str().c_str(), "message: \"Hello\""));
1360 ShutdownServer();
1361 }
1362
1363 TEST_F(GrpcToolTest, CallMaxRecvMessageSizeUnlimited) {
1364 std::stringstream output_stream;
1365 const std::string server_address = SetUpServer();
1366 // Test input "grpc_cli call localhost:10000 Echo "message: 'Hello'
1367 // --max_recv_msg_size=-1"
1368 const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
1369 "grpc.testing.EchoTestService.Echo",
1370 "message: 'Hello'"};
1371
1372 // Set max_recv_msg_size to unlimited (-1), which should work.
1373 absl::SetFlag(&FLAGS_max_recv_msg_size, -1);
1374
1375 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1376 std::bind(PrintStream, &output_stream,
1377 std::placeholders::_1)));
1378 // Expected output: "message: \"Hello\""
1379 EXPECT_TRUE(nullptr !=
1380 strstr(output_stream.str().c_str(), "message: \"Hello\""));
1381 ShutdownServer();
1382 }
1383
1384 TEST_F(GrpcToolTest, ListCommand_OverrideSslHostName) {
1385 const std::string server_address = SetUpServer(true);
1386
1387 // Test input "grpc_cli ls localhost:<port> --channel_creds_type=ssl
1388 // --ssl_target=z.test.google.fr"
1389 std::stringstream output_stream;
1390 const char* argv[] = {"grpc_cli", "ls", server_address.c_str()};
1391 absl::SetFlag(&FLAGS_l, false);
1392 absl::SetFlag(&FLAGS_channel_creds_type, "ssl");
1393 absl::SetFlag(&FLAGS_ssl_target, "z.test.google.fr");
1394 EXPECT_TRUE(
1395 0 == GrpcToolMainLib(
1396 ArraySize(argv), argv, TestCliCredentials(true),
1397 std::bind(PrintStream, &output_stream, std::placeholders::_1)));
1398 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1399 "grpc.testing.EchoTestService\n"
1400 "grpc.reflection.v1alpha.ServerReflection\n"));
1401
1402 absl::SetFlag(&FLAGS_channel_creds_type, "");
1403 absl::SetFlag(&FLAGS_ssl_target, "");
1404 ShutdownServer();
1405 }
1406
1407 TEST_F(GrpcToolTest, ConfiguringDefaultServiceConfig) {
1408 // Test input "grpc_cli list localhost:<port>
1409 // --default_service_config={\"loadBalancingConfig\":[{\"pick_first\":{}}]}"
1410 std::stringstream output_stream;
1411 const std::string server_address = SetUpServer();
1412 const char* argv[] = {"grpc_cli", "ls", server_address.c_str()};
1413 // Just check that the tool is still operational when --default_service_config
1414 // is configured. This particular service config is in reality redundant with
1415 // the channel's default configuration.
1416 absl::SetFlag(&FLAGS_l, false);
1417 absl::SetFlag(&FLAGS_default_service_config,
1418 "{\"loadBalancingConfig\":[{\"pick_first\":{}}]}");
1419 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
1420 std::bind(PrintStream, &output_stream,
1421 std::placeholders::_1)));
1422 absl::SetFlag(&FLAGS_default_service_config, "");
1423 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1424 "grpc.testing.EchoTestService\n"
1425 "grpc.reflection.v1alpha.ServerReflection\n"));
1426 ShutdownServer();
1427 }
1428
1429 } // namespace testing
1430 } // namespace grpc
1431
1432 int main(int argc, char** argv) {
1433 grpc::testing::TestEnvironment env(&argc, argv);
1434 ::testing::InitGoogleTest(&argc, argv);
1435 GTEST_FLAG_SET(death_test_style, "threadsafe");
1436 return RUN_ALL_TESTS();
1437 }