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 <gmock/gmock-matchers.h>
20 #include <grpc/grpc.h>
21 #include <grpcpp/channel.h>
22 #include <grpcpp/client_context.h>
23 #include <grpcpp/create_channel.h>
24 #include <grpcpp/ext/proto_server_reflection_plugin.h>
25 #include <grpcpp/security/credentials.h>
26 #include <grpcpp/security/server_credentials.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 #include <grpcpp/server_context.h>
30 #include <gtest/gtest.h>
31
32 #include <memory>
33 #include <vector>
34
35 #include "src/proto/grpc/reflection/v1/reflection.grpc.pb.h"
36 #include "src/proto/grpc/reflection/v1/reflection.pb.h"
37 #include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h"
38 #include "src/proto/grpc/reflection/v1alpha/reflection.pb.h"
39 #include "src/proto/grpc/testing/echo.grpc.pb.h"
40 #include "test/core/test_util/port.h"
41 #include "test/core/test_util/test_config.h"
42 #include "test/cpp/util/proto_reflection_descriptor_database.h"
43
44 namespace grpc {
45 namespace testing {
46
47 class ProtoServerReflectionTest : public ::testing::Test {
48 public:
ProtoServerReflectionTest()49 ProtoServerReflectionTest() {}
50
SetUp()51 void SetUp() override {
52 port_ = grpc_pick_unused_port_or_die();
53 ref_desc_pool_ = protobuf::DescriptorPool::generated_pool();
54
55 ServerBuilder builder;
56 std::string server_address = "localhost:" + to_string(port_);
57 builder.AddListeningPort(server_address, InsecureServerCredentials());
58 server_ = builder.BuildAndStart();
59 }
60
ResetStub()61 void ResetStub() {
62 string target = "dns:localhost:" + to_string(port_);
63 channel_ = grpc::CreateChannel(target, InsecureChannelCredentials());
64 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
65 desc_db_ = std::make_unique<ProtoReflectionDescriptorDatabase>(channel_);
66 desc_pool_ = std::make_unique<protobuf::DescriptorPool>(desc_db_.get());
67 }
68
to_string(const int number)69 string to_string(const int number) {
70 std::stringstream strs;
71 strs << number;
72 return strs.str();
73 }
74
CompareService(const std::string & service)75 void CompareService(const std::string& service) {
76 const protobuf::ServiceDescriptor* service_desc =
77 desc_pool_->FindServiceByName(service);
78 const protobuf::ServiceDescriptor* ref_service_desc =
79 ref_desc_pool_->FindServiceByName(service);
80 EXPECT_TRUE(service_desc != nullptr);
81 EXPECT_TRUE(ref_service_desc != nullptr);
82 EXPECT_EQ(service_desc->DebugString(), ref_service_desc->DebugString());
83
84 const protobuf::FileDescriptor* file_desc = service_desc->file();
85 if (known_files_.find(std::string(file_desc->package()) + "/" +
86 std::string(file_desc->name())) !=
87 known_files_.end()) {
88 EXPECT_EQ(file_desc->DebugString(),
89 ref_service_desc->file()->DebugString());
90 known_files_.insert(std::string(file_desc->package()) + "/" +
91 std::string(file_desc->name()));
92 }
93
94 for (int i = 0; i < service_desc->method_count(); ++i) {
95 CompareMethod(std::string(service_desc->method(i)->full_name()));
96 }
97 }
98
CompareMethod(const std::string & method)99 void CompareMethod(const std::string& method) {
100 const protobuf::MethodDescriptor* method_desc =
101 desc_pool_->FindMethodByName(method);
102 const protobuf::MethodDescriptor* ref_method_desc =
103 ref_desc_pool_->FindMethodByName(method);
104 EXPECT_TRUE(method_desc != nullptr);
105 EXPECT_TRUE(ref_method_desc != nullptr);
106 EXPECT_EQ(method_desc->DebugString(), ref_method_desc->DebugString());
107
108 CompareType(std::string(method_desc->input_type()->full_name()));
109 CompareType(std::string(method_desc->output_type()->full_name()));
110 }
111
CompareType(const std::string & type)112 void CompareType(const std::string& type) {
113 if (known_types_.find(type) != known_types_.end()) {
114 return;
115 }
116
117 const protobuf::Descriptor* desc = desc_pool_->FindMessageTypeByName(type);
118 const protobuf::Descriptor* ref_desc =
119 ref_desc_pool_->FindMessageTypeByName(type);
120 EXPECT_TRUE(desc != nullptr);
121 EXPECT_TRUE(ref_desc != nullptr);
122 EXPECT_EQ(desc->DebugString(), ref_desc->DebugString());
123 }
124
125 template <typename Response>
ServicesFromResponse(const Response & response)126 std::vector<std::string> ServicesFromResponse(const Response& response) {
127 std::vector<std::string> services;
128 for (const auto& service : response.list_services_response().service()) {
129 services.emplace_back(service.name());
130 }
131 return services;
132 }
133
134 protected:
135 std::unique_ptr<Server> server_;
136 std::shared_ptr<Channel> channel_;
137 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
138 std::unique_ptr<ProtoReflectionDescriptorDatabase> desc_db_;
139 std::unique_ptr<protobuf::DescriptorPool> desc_pool_;
140 std::unordered_set<string> known_files_;
141 std::unordered_set<string> known_types_;
142 const protobuf::DescriptorPool* ref_desc_pool_;
143 int port_;
144 reflection::ProtoServerReflectionPlugin plugin_;
145 };
146
TEST_F(ProtoServerReflectionTest,CheckResponseWithLocalDescriptorPool)147 TEST_F(ProtoServerReflectionTest, CheckResponseWithLocalDescriptorPool) {
148 ResetStub();
149
150 std::vector<std::string> services;
151 desc_db_->GetServices(&services);
152 // The service list has at least one service (reflection service).
153 EXPECT_TRUE(!services.empty());
154
155 for (auto it = services.begin(); it != services.end(); ++it) {
156 CompareService(*it);
157 }
158 }
159
TEST_F(ProtoServerReflectionTest,V1AlphaApiInstalled)160 TEST_F(ProtoServerReflectionTest, V1AlphaApiInstalled) {
161 ResetStub();
162 using Service = reflection::v1alpha::ServerReflection;
163 using Request = reflection::v1alpha::ServerReflectionRequest;
164 using Response = reflection::v1alpha::ServerReflectionResponse;
165 Service::Stub stub(channel_);
166 ClientContext context;
167 auto reader_writer = stub.ServerReflectionInfo(&context);
168 Request request;
169 request.set_list_services("*");
170 reader_writer->Write(request);
171 Response response;
172 ASSERT_EQ(reader_writer->Read(&response), true);
173 EXPECT_THAT(ServicesFromResponse(response),
174 ::testing::UnorderedElementsAre(
175 reflection::v1alpha::ServerReflection::service_full_name(),
176 reflection::v1::ServerReflection::service_full_name()));
177 request = Request::default_instance();
178 request.set_file_containing_symbol(Service::service_full_name());
179 reader_writer->WriteLast(request, WriteOptions());
180 response = Response::default_instance();
181 ASSERT_EQ(reader_writer->Read(&response), true);
182 EXPECT_EQ(response.file_descriptor_response().file_descriptor_proto_size(), 1)
183 << response.DebugString();
184 }
185
TEST_F(ProtoServerReflectionTest,V1ApiInstalled)186 TEST_F(ProtoServerReflectionTest, V1ApiInstalled) {
187 ResetStub();
188 using Service = reflection::v1::ServerReflection;
189 using Request = reflection::v1::ServerReflectionRequest;
190 using Response = reflection::v1::ServerReflectionResponse;
191 Service::Stub stub(channel_);
192 ClientContext context;
193 auto reader_writer = stub.ServerReflectionInfo(&context);
194 Request request;
195 request.set_list_services("*");
196 reader_writer->Write(request);
197 Response response;
198 ASSERT_TRUE(reader_writer->Read(&response));
199 EXPECT_THAT(ServicesFromResponse(response),
200 ::testing::UnorderedElementsAre(
201 reflection::v1alpha::ServerReflection::service_full_name(),
202 reflection::v1::ServerReflection::service_full_name()));
203 request = Request::default_instance();
204 request.set_file_containing_symbol(Service::service_full_name());
205 reader_writer->WriteLast(request, WriteOptions());
206 response = Response::default_instance();
207 ASSERT_TRUE(reader_writer->Read(&response));
208 EXPECT_EQ(response.file_descriptor_response().file_descriptor_proto_size(), 1)
209 << response.DebugString();
210 }
211
212 } // namespace testing
213 } // namespace grpc
214
main(int argc,char ** argv)215 int main(int argc, char** argv) {
216 grpc::testing::TestEnvironment env(&argc, argv);
217 ::testing::InitGoogleTest(&argc, argv);
218 return RUN_ALL_TESTS();
219 }
220