24 #include <gtest/gtest.h>
26 #include "absl/flags/declare.h"
27 #include "absl/flags/flag.h"
41 #include "src/proto/grpc/testing/echo.grpc.pb.h"
42 #include "src/proto/grpc/testing/echo.pb.h"
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"
53 using grpc::testing::EchoRequest;
54 using grpc::testing::EchoResponse;
56 #define USAGE_REGEX "( grpc_cli .+\n){2,10}"
58 #define ECHO_TEST_SERVICE_SUMMARY \
62 "CheckDeadlineUpperBound\n" \
63 "CheckDeadlineSet\n" \
64 "CheckClientInitialMetadata\n" \
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) " \
77 " rpc Echo1(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
79 " rpc Echo2(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
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" \
100 #define ECHO_METHOD_DESCRIPTION \
101 " rpc Echo(grpc.testing.EchoRequest) returns (grpc.testing.EchoResponse) " \
104 #define ECHO_RESPONSE_MESSAGE_TEXT_FORMAT \
105 "message: \"echo\"\n" \
107 " host: \"localhost\"\n" \
108 " peer: \"peer\"\n" \
111 #define ECHO_RESPONSE_MESSAGE_JSON_FORMAT \
113 " \"message\": \"echo\",\n" \
115 " \"host\": \"localhost\",\n" \
116 " \"peer\": \"peer\"\n" \
142 explicit TestCliCredentials(
bool secure =
false) :
secure_(secure) {}
143 std::shared_ptr<grpc::ChannelCredentials> GetChannelCredentials()
154 std::shared_ptr<grpc::ChannelCredentials> credential_ptr =
157 return credential_ptr;
159 std::string GetCredentialUsage()
const override {
return ""; }
170 template <
typename T>
171 size_t ArraySize(
T& a) {
172 return ((
sizeof(a) /
sizeof(*(a))) /
173 static_cast<size_t>(!(
sizeof(a) %
sizeof(*(a)))));
176 class TestServiceImpl :
public grpc::testing::EchoTestService::Service {
180 if (!
context->client_metadata().empty()) {
181 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
188 context->AddTrailingMetadata(
"trailing_key",
"trailing_value");
208 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(
219 ServerReader<EchoRequest>*
reader,
223 if (!
context->client_metadata().empty()) {
224 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
231 context->AddTrailingMetadata(
"trailing_key",
"trailing_value");
240 ServerWriter<EchoResponse>*
writer)
override {
241 if (!
context->client_metadata().empty()) {
242 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
249 context->AddTrailingMetadata(
"trailing_key",
"trailing_value");
262 ServerReaderWriter<EchoResponse, EchoRequest>*
stream)
override {
265 if (!
context->client_metadata().empty()) {
266 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
273 context->AddTrailingMetadata(
"trailing_key",
"trailing_value");
299 std::shared_ptr<grpc::ServerCredentials> creds;
305 const char* server_cert =
307 const char* server_key =
336 std::stringstream output_stream;
337 const char* argv[] = {
"grpc_cli"};
341 ArraySize(argv), argv, TestCliCredentials(),
342 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
343 ::testing::ExitedWithCode(1),
"No command specified\n" USAGE_REGEX);
350 std::stringstream output_stream;
351 const char* argv[] = {
"grpc_cli",
"abc"};
355 ArraySize(argv), argv, TestCliCredentials(),
356 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
357 ::testing::ExitedWithCode(1),
"Invalid command 'abc'\n" USAGE_REGEX);
364 std::stringstream output_stream;
365 const char* argv[] = {
"grpc_cli",
"help"};
367 EXPECT_EXIT(
GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
368 std::bind(PrintStream, &output_stream,
369 std::placeholders::_1)),
377 std::stringstream output_stream;
384 std::bind(PrintStream, &output_stream,
385 std::placeholders::_1)));
386 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
387 "grpc.testing.EchoTestService\n"
388 "grpc.reflection.v1alpha.ServerReflection\n"));
395 std::stringstream output_stream;
399 "grpc.testing.EchoTestService"};
403 std::bind(PrintStream, &output_stream,
404 std::placeholders::_1)));
411 output_stream.clear();
414 std::bind(PrintStream, &output_stream,
415 std::placeholders::_1)));
425 std::stringstream output_stream;
429 "grpc.testing.EchoRequest"};
432 std::bind(PrintStream, &output_stream,
433 std::placeholders::_1)));
436 "grpc.testing.EchoRequest");
439 strcmp(output_stream.str().c_str(),
desc->DebugString().c_str()));
446 std::stringstream output_stream;
450 "grpc.testing.EchoTestService.Echo"};
454 std::bind(PrintStream, &output_stream,
455 std::placeholders::_1)));
457 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
"Echo\n"));
461 output_stream.clear();
464 std::bind(PrintStream, &output_stream,
465 std::placeholders::_1)));
475 std::stringstream output_stream;
479 "grpc.testing.PhonyRequest"};
482 std::bind(PrintStream, &output_stream,
483 std::placeholders::_1)));
489 std::stringstream output_stream;
492 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
496 std::bind(PrintStream, &output_stream,
497 std::placeholders::_1)));
500 strstr(output_stream.str().c_str(),
"message: \"Hello\""));
504 output_stream.clear();
509 std::bind(PrintStream, &output_stream,
510 std::placeholders::_1)));
517 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
518 "{\n \"message\": \"Hello\"\n}"));
525 std::stringstream output_stream;
528 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
529 "{ \"message\": \"Hello\"}"};
533 std::bind(PrintStream, &output_stream,
534 std::placeholders::_1)));
537 strstr(output_stream.str().c_str(),
"message: \"Hello\""));
541 output_stream.clear();
545 std::bind(PrintStream, &output_stream,
546 std::placeholders::_1)));
554 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
555 "{\n \"message\": \"Hello\"\n}"));
562 std::stringstream output_stream;
565 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
566 "message: 'Hello0'"};
569 std::streambuf* orig = std::cin.rdbuf();
570 std::istringstream ss(
"message: 'Hello1'\n\n message: 'Hello2'\n\n");
571 std::cin.rdbuf(ss.rdbuf());
575 std::bind(PrintStream, &output_stream,
576 std::placeholders::_1)));
581 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
582 "message: \"Hello0\"\nmessage: "
583 "\"Hello1\"\nmessage: \"Hello2\"\n"));
586 output_stream.clear();
589 std::cin.rdbuf(ss.rdbuf());
594 std::bind(PrintStream, &output_stream,
595 std::placeholders::_1)));
611 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
612 "{\n \"message\": \"Hello0\"\n}\n"
613 "{\n \"message\": \"Hello1\"\n}\n"
614 "{\n \"message\": \"Hello2\"\n}\n"));
616 std::cin.rdbuf(orig);
622 std::stringstream output_stream;
625 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
626 "{\"message\": \"Hello0\"}"};
629 std::streambuf* orig = std::cin.rdbuf();
630 std::istringstream ss(
631 "{\"message\": \"Hello1\"}\n\n{\"message\": \"Hello2\" }\n\n");
632 std::cin.rdbuf(ss.rdbuf());
637 std::bind(PrintStream, &output_stream,
638 std::placeholders::_1)));
643 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
644 "message: \"Hello0\"\nmessage: "
645 "\"Hello1\"\nmessage: \"Hello2\"\n"));
648 output_stream.clear();
651 std::cin.rdbuf(ss.rdbuf());
656 std::bind(PrintStream, &output_stream,
657 std::placeholders::_1)));
674 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
675 "{\n \"message\": \"Hello0\"\n}\n"
676 "{\n \"message\": \"Hello1\"\n}\n"
677 "{\n \"message\": \"Hello2\"\n}\n"));
679 std::cin.rdbuf(orig);
685 std::stringstream output_stream;
688 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
689 "message: 'Hello0'"};
692 std::streambuf* orig = std::cin.rdbuf();
693 std::istringstream ss(
"message: 1\n\n message: 'Hello2'\n\n");
694 std::cin.rdbuf(ss.rdbuf());
698 std::bind(PrintStream, &output_stream,
699 std::placeholders::_1)));
703 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
704 "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
708 output_stream.clear();
711 std::cin.rdbuf(ss.rdbuf());
716 std::bind(PrintStream, &output_stream,
717 std::placeholders::_1)));
730 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
731 "{\n \"message\": \"Hello0\"\n}\n"
732 "{\n \"message\": \"Hello2\"\n}\n"));
734 std::cin.rdbuf(orig);
740 std::stringstream output_stream;
743 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
744 "{ \"message\": \"Hello0\"}"};
747 std::streambuf* orig = std::cin.rdbuf();
748 std::istringstream ss(
749 "{ \"message\": 1 }\n\n { \"message\": \"Hello2\" }\n\n");
750 std::cin.rdbuf(ss.rdbuf());
755 std::bind(PrintStream, &output_stream,
756 std::placeholders::_1)));
761 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
762 "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
766 output_stream.clear();
769 std::cin.rdbuf(ss.rdbuf());
775 std::bind(PrintStream, &output_stream,
776 std::placeholders::_1)));
790 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
791 "{\n \"message\": \"Hello0\"\n}\n"
792 "{\n \"message\": \"Hello2\"\n}\n"));
794 std::cin.rdbuf(orig);
801 std::stringstream output_stream;
805 "RequestStream",
"message: 'Hello0'"};
808 std::streambuf* orig = std::cin.rdbuf();
809 std::istringstream ss(
"message: 'Hello1'\n\n message: 'Hello2'\n\n");
810 std::cin.rdbuf(ss.rdbuf());
813 std::bind(PrintStream, &output_stream,
814 std::placeholders::_1)));
817 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
818 "message: \"Hello0Hello1Hello2\""));
819 std::cin.rdbuf(orig);
826 std::stringstream output_stream;
830 "RequestStream",
"{ \"message\": \"Hello0\" }"};
833 std::streambuf* orig = std::cin.rdbuf();
834 std::istringstream ss(
835 "{ \"message\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
836 std::cin.rdbuf(ss.rdbuf());
840 std::bind(PrintStream, &output_stream,
841 std::placeholders::_1)));
845 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
846 "message: \"Hello0Hello1Hello2\""));
847 std::cin.rdbuf(orig);
854 std::stringstream output_stream;
858 "RequestStream",
"message: 'Hello0'"};
861 std::streambuf* orig = std::cin.rdbuf();
862 std::istringstream ss(
"bad_field: 'Hello1'\n\n message: 'Hello2'\n\n");
863 std::cin.rdbuf(ss.rdbuf());
866 std::bind(PrintStream, &output_stream,
867 std::placeholders::_1)));
871 strstr(output_stream.str().c_str(),
"message: \"Hello0Hello2\""));
872 std::cin.rdbuf(orig);
879 std::stringstream output_stream;
883 "RequestStream",
"{ \"message\": \"Hello0\" }"};
886 std::streambuf* orig = std::cin.rdbuf();
887 std::istringstream ss(
888 "{ \"bad_field\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
889 std::cin.rdbuf(ss.rdbuf());
893 std::bind(PrintStream, &output_stream,
894 std::placeholders::_1)));
899 strstr(output_stream.str().c_str(),
"message: \"Hello0Hello2\""));
900 std::cin.rdbuf(orig);
906 std::stringstream output_stream;
916 std::bind(PrintStream, &output_stream,
917 std::placeholders::_1)));
921 strstr(output_stream.str().c_str(),
"message: \"true\""));
927 std::stringstream output_stream;
931 "CheckDeadlineUpperBound"};
937 std::bind(PrintStream, &output_stream,
938 std::placeholders::_1)));
950 std::stringstream output_stream;
960 std::bind(PrintStream, &output_stream,
961 std::placeholders::_1)));
965 strstr(output_stream.str().c_str(),
"message: \"false\""));
972 std::stringstream output_stream;
982 std::bind(PrintStream, &output_stream,
983 std::placeholders::_1)));
987 strstr(output_stream.str().c_str(),
"message: \"false\""));
995 std::stringstream output_stream;
999 "ResponseStream",
"message: 'Hello'"};
1002 std::bind(PrintStream, &output_stream,
1003 std::placeholders::_1)));
1009 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
1010 expected_response_text.c_str()));
1015 output_stream.clear();
1019 std::bind(PrintStream, &output_stream,
1020 std::placeholders::_1)));
1027 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
1028 expected_response_text.c_str()));
1036 std::stringstream output_stream;
1039 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
1040 "BidiStream",
"message: 'Hello0'"};
1043 std::streambuf* orig = std::cin.rdbuf();
1044 std::istringstream ss(
"message: 'Hello1'\n\n message: 'Hello2'\n\n");
1045 std::cin.rdbuf(ss.rdbuf());
1048 std::bind(PrintStream, &output_stream,
1049 std::placeholders::_1)));
1053 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
1054 "message: \"Hello0\"\nmessage: "
1055 "\"Hello1\"\nmessage: \"Hello2\"\n"));
1056 std::cin.rdbuf(orig);
1062 std::stringstream output_stream;
1065 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
1066 "BidiStream",
"message: 'Hello0'"};
1069 std::streambuf* orig = std::cin.rdbuf();
1070 std::istringstream ss(
"message: 1.0\n\n message: 'Hello2'\n\n");
1071 std::cin.rdbuf(ss.rdbuf());
1074 std::bind(PrintStream, &output_stream,
1075 std::placeholders::_1)));
1079 EXPECT_TRUE(
nullptr != strstr(output_stream.str().c_str(),
1080 "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
1081 std::cin.rdbuf(orig);
1089 std::stringstream output_stream;
1090 std::stringstream binary_output_stream;
1093 const char* argv[] = {
"grpc_cli",
"parse",
server_address.c_str(),
1094 "grpc.testing.EchoResponse",
1100 std::bind(PrintStream, &output_stream,
1101 std::placeholders::_1)));
1103 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1108 output_stream.clear();
1112 std::bind(PrintStream, &output_stream,
1113 std::placeholders::_1)));
1117 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1122 output_stream.clear();
1125 std::bind(PrintStream, &output_stream,
1126 std::placeholders::_1)));
1129 output_stream.clear();
1130 argv[4] = binary_data.c_str();
1134 std::bind(PrintStream, &output_stream,
1135 std::placeholders::_1)));
1138 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1149 std::stringstream output_stream;
1150 std::stringstream binary_output_stream;
1153 const char* argv[] = {
"grpc_cli",
"parse",
server_address.c_str(),
1154 "grpc.testing.EchoResponse",
1159 std::bind(PrintStream, &output_stream,
1160 std::placeholders::_1)));
1163 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1168 output_stream.clear();
1172 std::bind(PrintStream, &output_stream,
1173 std::placeholders::_1)));
1178 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1186 std::stringstream output_stream;
1187 const char* argv[] = {
"grpc_cli",
"call",
"Echo"};
1192 ArraySize(argv), argv, TestCliCredentials(),
1193 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1194 ::testing::ExitedWithCode(1),
".*Wrong number of arguments for call.*");
1201 std::stringstream output_stream;
1202 const char* argv[] = {
"grpc_cli",
"call",
"localhost:10000",
1203 "Echo",
"Echo",
"message: 'Hello'"};
1208 ArraySize(argv), argv, TestCliCredentials(),
1209 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1210 ::testing::ExitedWithCode(1),
".*Wrong number of arguments for call.*");
1218 const char* argv[] = {
"grpc_cli",
"call",
server_address.c_str(),
"Echo",
1219 "message: 'Hello'"};
1222 std::stringstream output_stream;
1223 absl::SetFlag(&FLAGS_metadata,
"key0:val0:key1:valq:key2:val2");
1225 TestCliCredentials(),
1226 std::bind(PrintStream, &output_stream,
1227 std::placeholders::_1)));
1230 strstr(output_stream.str().c_str(),
"message: \"Hello\""));
1234 std::stringstream output_stream;
1237 TestCliCredentials(),
1238 std::bind(PrintStream, &output_stream,
1239 std::placeholders::_1)));
1242 strstr(output_stream.str().c_str(),
"message: \"Hello\""));
1246 std::stringstream output_stream;
1249 TestCliCredentials(),
1250 std::bind(PrintStream, &output_stream,
1251 std::placeholders::_1)));
1254 strstr(output_stream.str().c_str(),
"message: \"Hello\""));
1263 const char* argv[] = {
"grpc_cli",
"call",
"localhost:10000",
1264 "grpc.testing.EchoTestService.Echo",
1265 "message: 'Hello'"};
1266 absl::SetFlag(&FLAGS_protofiles,
"src/proto/grpc/testing/echo.proto");
1267 char* test_srcdir =
gpr_getenv(
"TEST_SRCDIR");
1268 if (test_srcdir !=
nullptr) {
1270 test_srcdir +
std::string(
"/com_github_grpc_grpc"));
1274 std::stringstream output_stream;
1279 ArraySize(argv), argv, TestCliCredentials(),
1280 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1281 ::testing::ExitedWithCode(1),
".*Failed to parse metadata flag.*");
1285 std::stringstream output_stream;
1290 ArraySize(argv), argv, TestCliCredentials(),
1291 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
1292 ::testing::ExitedWithCode(1),
".*Failed to parse metadata flag.*");
1306 std::stringstream output_stream;
1313 ArraySize(argv), argv, TestCliCredentials(
true),
1314 std::bind(PrintStream, &output_stream, std::placeholders::_1)));
1315 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1316 "grpc.testing.EchoTestService\n"
1317 "grpc.reflection.v1alpha.ServerReflection\n"));
1327 std::stringstream output_stream;
1335 "{\"loadBalancingConfig\":[{\"pick_first\":{}}]}");
1337 std::bind(PrintStream, &output_stream,
1338 std::placeholders::_1)));
1340 EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
1341 "grpc.testing.EchoTestService\n"
1342 "grpc.reflection.v1alpha.ServerReflection\n"));