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