Debian Codegraph @

test/cpp/end2end/proto_server_reflection_test.cc

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