blob: edb66b72d83736bc2eb9a3e01c22a6b5eb726bec [file] [log] [blame]
Yuchen Zeng29ca79b2016-07-25 12:00:08 -07001/*
2 *
Yuchen Zeng02139a02016-08-15 11:34:21 -07003 * Copyright 2016, Google Inc.
Yuchen Zeng29ca79b2016-07-25 12:00:08 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "test/cpp/util/grpc_tool.h"
35
36#include <sstream>
37
38#include <grpc++/channel.h>
39#include <grpc++/client_context.h>
40#include <grpc++/create_channel.h>
41#include <grpc++/ext/proto_server_reflection_plugin.h>
42#include <grpc++/server.h>
43#include <grpc++/server_builder.h>
44#include <grpc++/server_context.h>
45#include <grpc/grpc.h>
46#include <gtest/gtest.h>
47
48#include "src/proto/grpc/testing/echo.grpc.pb.h"
49#include "src/proto/grpc/testing/echo.pb.h"
50#include "test/core/util/port.h"
51#include "test/core/util/test_config.h"
Yuchen Zeng02139a02016-08-15 11:34:21 -070052#include "test/cpp/util/cli_credentials.h"
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070053#include "test/cpp/util/string_ref_helper.h"
54
55using grpc::testing::EchoRequest;
56using grpc::testing::EchoResponse;
57
58namespace grpc {
59namespace testing {
Yuchen Zeng02139a02016-08-15 11:34:21 -070060namespace {
61
62class TestCliCredentials GRPC_FINAL : public grpc::testing::CliCredentials {
63 public:
64 std::shared_ptr<grpc::ChannelCredentials> GetCredentials() const
65 GRPC_OVERRIDE {
66 return InsecureChannelCredentials();
67 }
68 const grpc::string GetCredentialUsage() const GRPC_OVERRIDE { return ""; }
69};
70
71} // namespame
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070072
73class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
74 public:
75 Status Echo(ServerContext* context, const EchoRequest* request,
76 EchoResponse* response) GRPC_OVERRIDE {
77 if (!context->client_metadata().empty()) {
78 for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
79 iter = context->client_metadata().begin();
80 iter != context->client_metadata().end(); ++iter) {
81 context->AddInitialMetadata(ToString(iter->first),
82 ToString(iter->second));
83 }
84 }
85 context->AddTrailingMetadata("trailing_key", "trailing_value");
86 response->set_message(request->message());
87 return Status::OK;
88 }
89};
90
91class GrpcToolTest : public ::testing::Test {
92 protected:
93 GrpcToolTest() {}
94
Yuchen Zengf4046cd2016-07-26 12:22:42 -070095 // SetUpServer cannot be used with EXPECT_EXIT. grpc_pick_unused_port_or_die()
96 // uses atexit() to free chosen ports, and it will spawn a new thread in
97 // resolve_address_posix.c:192 at exit time.
98 const grpc::string SetUpServer() {
99 std::ostringstream server_address;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700100 int port = grpc_pick_unused_port_or_die();
Yuchen Zengf4046cd2016-07-26 12:22:42 -0700101 server_address << "localhost:" << port;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700102 // Setup server
103 ServerBuilder builder;
Yuchen Zeng68ca3512016-07-26 16:48:46 -0700104 builder.AddListeningPort(server_address.str(), InsecureServerCredentials());
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700105 builder.RegisterService(&service_);
106 server_ = builder.BuildAndStart();
Yuchen Zengf4046cd2016-07-26 12:22:42 -0700107 return server_address.str();
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700108 }
109
Yuchen Zengf4046cd2016-07-26 12:22:42 -0700110 void ShutdownServer() { server_->Shutdown(); }
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700111
Yuchen Zeng734fd712016-09-16 16:10:53 -0700112 void ExitWhenError(int argc, const char** argv, const CliCredentials& cred,
Yuchen Zeng94d786f2016-08-26 16:23:29 -0700113 GrpcToolOutputCallback callback) {
114 int result = GrpcToolMainLib(argc, argv, cred, callback);
115 if (result) {
116 exit(result);
117 }
118 }
119
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700120 std::unique_ptr<Server> server_;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700121 TestServiceImpl service_;
122 reflection::ProtoServerReflectionPlugin plugin_;
123};
124
125static bool PrintStream(std::stringstream* ss, const grpc::string& output) {
Yuchen Zeng94d786f2016-08-26 16:23:29 -0700126 (*ss) << output;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700127 return true;
128}
129
130template <typename T>
131static size_t ArraySize(T& a) {
132 return ((sizeof(a) / sizeof(*(a))) /
133 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))));
134}
135
136#define USAGE_REGEX "( grpc_cli .+\n){2,10}"
137
138TEST_F(GrpcToolTest, NoCommand) {
139 // Test input "grpc_cli"
140 std::stringstream output_stream;
141 const char* argv[] = {"grpc_cli"};
142 // Exit with 1, print usage instruction in stderr
143 EXPECT_EXIT(
144 GrpcToolMainLib(
Yuchen Zeng02139a02016-08-15 11:34:21 -0700145 ArraySize(argv), argv, TestCliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700146 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
147 ::testing::ExitedWithCode(1), "No command specified\n" USAGE_REGEX);
148 // No output
149 EXPECT_TRUE(0 == output_stream.tellp());
150}
151
152TEST_F(GrpcToolTest, InvalidCommand) {
153 // Test input "grpc_cli"
154 std::stringstream output_stream;
155 const char* argv[] = {"grpc_cli", "abc"};
156 // Exit with 1, print usage instruction in stderr
157 EXPECT_EXIT(
158 GrpcToolMainLib(
Yuchen Zeng02139a02016-08-15 11:34:21 -0700159 ArraySize(argv), argv, TestCliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700160 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
161 ::testing::ExitedWithCode(1), "Invalid command 'abc'\n" USAGE_REGEX);
162 // No output
163 EXPECT_TRUE(0 == output_stream.tellp());
164}
165
166TEST_F(GrpcToolTest, HelpCommand) {
167 // Test input "grpc_cli help"
168 std::stringstream output_stream;
169 const char* argv[] = {"grpc_cli", "help"};
170 // Exit with 1, print usage instruction in stderr
Yuchen Zeng02139a02016-08-15 11:34:21 -0700171 EXPECT_EXIT(GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700172 std::bind(PrintStream, &output_stream,
173 std::placeholders::_1)),
174 ::testing::ExitedWithCode(1), USAGE_REGEX);
175 // No output
176 EXPECT_TRUE(0 == output_stream.tellp());
177}
178
Yuchen Zeng94d786f2016-08-26 16:23:29 -0700179TEST_F(GrpcToolTest, TypeCommand) {
180 // Test input "grpc_cli type localhost:<port> grpc.testing.EchoRequest"
181 std::stringstream output_stream;
182
183 const grpc::string server_address = SetUpServer();
184 const char* argv[] = {"grpc_cli", "type", server_address.c_str(),
185 "grpc.testing.EchoRequest"};
186
187 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
188 std::bind(PrintStream, &output_stream,
189 std::placeholders::_1)));
190 const grpc::protobuf::Descriptor* desc =
191 grpc::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(
192 "grpc.testing.EchoRequest");
193 // Expected output: the DebugString of grpc.testing.EchoRequest
194 EXPECT_TRUE(0 ==
195 strcmp(output_stream.str().c_str(), desc->DebugString().c_str()));
196
197 ShutdownServer();
198}
199
200TEST_F(GrpcToolTest, TypeNotFound) {
201 // Test input "grpc_cli type localhost:<port> grpc.testing.DummyRequest"
202 std::stringstream output_stream;
203
204 const grpc::string server_address = SetUpServer();
205 const char* argv[] = {"grpc_cli", "type", server_address.c_str(),
206 "grpc.testing.DummyRequest"};
207
208 EXPECT_DEATH(ExitWhenError(ArraySize(argv), argv, TestCliCredentials(),
209 std::bind(PrintStream, &output_stream,
210 std::placeholders::_1)),
211 ".*Type grpc.testing.DummyRequest not found.*");
212
213 ShutdownServer();
214}
215
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700216TEST_F(GrpcToolTest, CallCommand) {
217 // Test input "grpc_cli call Echo"
218 std::stringstream output_stream;
Yuchen Zengf4046cd2016-07-26 12:22:42 -0700219
220 const grpc::string server_address = SetUpServer();
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700221 const char* argv[] = {"grpc_cli", "call", server_address.c_str(), "Echo",
222 "message: 'Hello'"};
223
Yuchen Zeng02139a02016-08-15 11:34:21 -0700224 EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700225 std::bind(PrintStream, &output_stream,
226 std::placeholders::_1)));
227 // Expected output: "message: \"Hello\""
228 EXPECT_TRUE(NULL !=
229 strstr(output_stream.str().c_str(), "message: \"Hello\""));
Yuchen Zengf4046cd2016-07-26 12:22:42 -0700230 ShutdownServer();
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700231}
232
233TEST_F(GrpcToolTest, TooFewArguments) {
234 // Test input "grpc_cli call localhost:<port> Echo "message: 'Hello'"
235 std::stringstream output_stream;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700236 const char* argv[] = {"grpc_cli", "call", "Echo"};
237
238 // Exit with 1
239 EXPECT_EXIT(
240 GrpcToolMainLib(
Yuchen Zeng02139a02016-08-15 11:34:21 -0700241 ArraySize(argv), argv, TestCliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700242 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
243 ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*");
244 // No output
245 EXPECT_TRUE(0 == output_stream.tellp());
246}
247
248TEST_F(GrpcToolTest, TooManyArguments) {
249 // Test input "grpc_cli call localhost:<port> Echo Echo "message: 'Hello'"
250 std::stringstream output_stream;
Yuchen Zengf4046cd2016-07-26 12:22:42 -0700251 const char* argv[] = {"grpc_cli", "call", "localhost:10000",
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700252 "Echo", "Echo", "message: 'Hello'"};
253
254 // Exit with 1
255 EXPECT_EXIT(
256 GrpcToolMainLib(
Yuchen Zeng02139a02016-08-15 11:34:21 -0700257 ArraySize(argv), argv, TestCliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700258 std::bind(PrintStream, &output_stream, std::placeholders::_1)),
259 ::testing::ExitedWithCode(1), ".*Wrong number of arguments for call.*");
260 // No output
261 EXPECT_TRUE(0 == output_stream.tellp());
262}
263
264} // namespace testing
265} // namespace grpc
266
267int main(int argc, char** argv) {
268 grpc_test_init(argc, argv);
269 ::testing::InitGoogleTest(&argc, argv);
270 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
271 return RUN_ALL_TESTS();
272}