• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
20 #include <grpcpp/channel.h>
21 #include <grpcpp/client_context.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/ext/proto_server_reflection_plugin.h>
24 #include <grpcpp/security/credentials.h>
25 #include <grpcpp/security/server_credentials.h>
26 #include <grpcpp/server.h>
27 #include <grpcpp/server_builder.h>
28 #include <grpcpp/server_context.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/end2end/test_service_impl.h"
34 #include "test/cpp/util/proto_reflection_descriptor_database.h"
35 
36 #include <gtest/gtest.h>
37 
38 namespace grpc {
39 namespace testing {
40 
41 class ProtoServerReflectionTest : public ::testing::Test {
42  public:
ProtoServerReflectionTest()43   ProtoServerReflectionTest() {}
44 
SetUp()45   void SetUp() override {
46     port_ = grpc_pick_unused_port_or_die();
47     ref_desc_pool_ = protobuf::DescriptorPool::generated_pool();
48 
49     ServerBuilder builder;
50     grpc::string server_address = "localhost:" + to_string(port_);
51     builder.AddListeningPort(server_address, InsecureServerCredentials());
52     server_ = builder.BuildAndStart();
53   }
54 
ResetStub()55   void ResetStub() {
56     string target = "dns:localhost:" + to_string(port_);
57     std::shared_ptr<Channel> channel =
58         CreateChannel(target, InsecureChannelCredentials());
59     stub_ = grpc::testing::EchoTestService::NewStub(channel);
60     desc_db_.reset(new ProtoReflectionDescriptorDatabase(channel));
61     desc_pool_.reset(new protobuf::DescriptorPool(desc_db_.get()));
62   }
63 
to_string(const int number)64   string to_string(const int number) {
65     std::stringstream strs;
66     strs << number;
67     return strs.str();
68   }
69 
CompareService(const grpc::string & service)70   void CompareService(const grpc::string& service) {
71     const protobuf::ServiceDescriptor* service_desc =
72         desc_pool_->FindServiceByName(service);
73     const protobuf::ServiceDescriptor* ref_service_desc =
74         ref_desc_pool_->FindServiceByName(service);
75     EXPECT_TRUE(service_desc != nullptr);
76     EXPECT_TRUE(ref_service_desc != nullptr);
77     EXPECT_EQ(service_desc->DebugString(), ref_service_desc->DebugString());
78 
79     const protobuf::FileDescriptor* file_desc = service_desc->file();
80     if (known_files_.find(file_desc->package() + "/" + file_desc->name()) !=
81         known_files_.end()) {
82       EXPECT_EQ(file_desc->DebugString(),
83                 ref_service_desc->file()->DebugString());
84       known_files_.insert(file_desc->package() + "/" + file_desc->name());
85     }
86 
87     for (int i = 0; i < service_desc->method_count(); ++i) {
88       CompareMethod(service_desc->method(i)->full_name());
89     }
90   }
91 
CompareMethod(const grpc::string & method)92   void CompareMethod(const grpc::string& method) {
93     const protobuf::MethodDescriptor* method_desc =
94         desc_pool_->FindMethodByName(method);
95     const protobuf::MethodDescriptor* ref_method_desc =
96         ref_desc_pool_->FindMethodByName(method);
97     EXPECT_TRUE(method_desc != nullptr);
98     EXPECT_TRUE(ref_method_desc != nullptr);
99     EXPECT_EQ(method_desc->DebugString(), ref_method_desc->DebugString());
100 
101     CompareType(method_desc->input_type()->full_name());
102     CompareType(method_desc->output_type()->full_name());
103   }
104 
CompareType(const grpc::string & type)105   void CompareType(const grpc::string& type) {
106     if (known_types_.find(type) != known_types_.end()) {
107       return;
108     }
109 
110     const protobuf::Descriptor* desc = desc_pool_->FindMessageTypeByName(type);
111     const protobuf::Descriptor* ref_desc =
112         ref_desc_pool_->FindMessageTypeByName(type);
113     EXPECT_TRUE(desc != nullptr);
114     EXPECT_TRUE(ref_desc != nullptr);
115     EXPECT_EQ(desc->DebugString(), ref_desc->DebugString());
116   }
117 
118  protected:
119   std::unique_ptr<Server> server_;
120   std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
121   std::unique_ptr<ProtoReflectionDescriptorDatabase> desc_db_;
122   std::unique_ptr<protobuf::DescriptorPool> desc_pool_;
123   std::unordered_set<string> known_files_;
124   std::unordered_set<string> known_types_;
125   const protobuf::DescriptorPool* ref_desc_pool_;
126   int port_;
127   reflection::ProtoServerReflectionPlugin plugin_;
128 };
129 
TEST_F(ProtoServerReflectionTest,CheckResponseWithLocalDescriptorPool)130 TEST_F(ProtoServerReflectionTest, CheckResponseWithLocalDescriptorPool) {
131   ResetStub();
132 
133   std::vector<grpc::string> services;
134   desc_db_->GetServices(&services);
135   // The service list has at least one service (reflection servcie).
136   EXPECT_TRUE(services.size() > 0);
137 
138   for (auto it = services.begin(); it != services.end(); ++it) {
139     CompareService(*it);
140   }
141 }
142 
143 }  // namespace testing
144 }  // namespace grpc
145 
main(int argc,char ** argv)146 int main(int argc, char** argv) {
147   grpc_test_init(argc, argv);
148   ::testing::InitGoogleTest(&argc, argv);
149   return RUN_ALL_TESTS();
150 }
151