1 /*
2  *
3  * Copyright 2015 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/cli_call.h"
20 
21 #include <grpc/grpc.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/client_context.h>
24 #include <grpcpp/create_channel.h>
25 #include <grpcpp/server.h>
26 #include <grpcpp/server_builder.h>
27 #include <grpcpp/server_context.h>
28 #include <gtest/gtest.h>
29 
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33 #include "test/cpp/util/string_ref_helper.h"
34 
35 using grpc::testing::EchoRequest;
36 using grpc::testing::EchoResponse;
37 
38 namespace grpc {
39 namespace testing {
40 
41 class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
42  public:
Echo(ServerContext * context,const EchoRequest * request,EchoResponse * response)43   Status Echo(ServerContext* context, const EchoRequest* request,
44               EchoResponse* response) override {
45     if (!context->client_metadata().empty()) {
46       for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
47                iter = context->client_metadata().begin();
48            iter != context->client_metadata().end(); ++iter) {
49         context->AddInitialMetadata(ToString(iter->first),
50                                     ToString(iter->second));
51       }
52     }
53     context->AddTrailingMetadata("trailing_key", "trailing_value");
54     response->set_message(request->message());
55     return Status::OK;
56   }
57 };
58 
59 class CliCallTest : public ::testing::Test {
60  protected:
CliCallTest()61   CliCallTest() {}
62 
SetUp()63   void SetUp() override {
64     int port = grpc_pick_unused_port_or_die();
65     server_address_ << "localhost:" << port;
66     // Setup server
67     ServerBuilder builder;
68     builder.AddListeningPort(server_address_.str(),
69                              InsecureServerCredentials());
70     builder.RegisterService(&service_);
71     server_ = builder.BuildAndStart();
72   }
73 
TearDown()74   void TearDown() override { server_->Shutdown(); }
75 
ResetStub()76   void ResetStub() {
77     channel_ =
78         CreateChannel(server_address_.str(), InsecureChannelCredentials());
79     stub_ = grpc::testing::EchoTestService::NewStub(channel_);
80   }
81 
82   std::shared_ptr<Channel> channel_;
83   std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
84   std::unique_ptr<Server> server_;
85   std::ostringstream server_address_;
86   TestServiceImpl service_;
87 };
88 
89 // Send a rpc with a normal stub and then a CliCall. Verify they match.
TEST_F(CliCallTest,SimpleRpc)90 TEST_F(CliCallTest, SimpleRpc) {
91   ResetStub();
92   // Normal stub.
93   EchoRequest request;
94   EchoResponse response;
95   request.set_message("Hello");
96 
97   ClientContext context;
98   context.AddMetadata("key1", "val1");
99   Status s = stub_->Echo(&context, request, &response);
100   EXPECT_EQ(response.message(), request.message());
101   EXPECT_TRUE(s.ok());
102 
103   const grpc::string kMethod("/grpc.testing.EchoTestService/Echo");
104   grpc::string request_bin, response_bin, expected_response_bin;
105   EXPECT_TRUE(request.SerializeToString(&request_bin));
106   EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
107   std::multimap<grpc::string, grpc::string> client_metadata;
108   std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
109       server_trailing_metadata;
110   client_metadata.insert(std::pair<grpc::string, grpc::string>("key1", "val1"));
111   Status s2 = CliCall::Call(channel_, kMethod, request_bin, &response_bin,
112                             client_metadata, &server_initial_metadata,
113                             &server_trailing_metadata);
114   EXPECT_TRUE(s2.ok());
115 
116   EXPECT_EQ(expected_response_bin, response_bin);
117   EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata);
118   EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata);
119 }
120 
121 }  // namespace testing
122 }  // namespace grpc
123 
main(int argc,char ** argv)124 int main(int argc, char** argv) {
125   grpc_test_init(argc, argv);
126   ::testing::InitGoogleTest(&argc, argv);
127   return RUN_ALL_TESTS();
128 }
129