proto_server_reflection_test.cc
Go to the documentation of this file.
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>
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"
39 
40 namespace grpc {
41 namespace testing {
42 
44  public:
46 
47  void SetUp() override {
50 
52  std::string server_address = "localhost:" + to_string(port_);
54  server_ = builder.BuildAndStart();
55  }
56 
57  void ResetStub() {
58  string target = "dns:localhost:" + to_string(port_);
59  std::shared_ptr<Channel> channel =
61  stub_ = grpc::testing::EchoTestService::NewStub(channel);
62  desc_db_ = absl::make_unique<ProtoReflectionDescriptorDatabase>(channel);
63  desc_pool_ = absl::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 
73  const protobuf::ServiceDescriptor* service_desc =
74  desc_pool_->FindServiceByName(service);
75  const protobuf::ServiceDescriptor* ref_service_desc =
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 
95  const protobuf::MethodDescriptor* method_desc =
96  desc_pool_->FindMethodByName(method);
97  const protobuf::MethodDescriptor* ref_method_desc =
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 =
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_;
128  int port_;
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 }
google::protobuf::Descriptor::full_name
const std::string & full_name() const
testing
Definition: aws_request_signer_test.cc:25
google::protobuf::MethodDescriptor::DebugString
std::string DebugString() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2891
regen-readme.it
it
Definition: regen-readme.py:15
port.h
grpc::testing::ProtoServerReflectionTest::stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: proto_server_reflection_test.cc:122
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
proto_server_reflection_plugin.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::testing::ProtoServerReflectionTest::ProtoServerReflectionTest
ProtoServerReflectionTest()
Definition: proto_server_reflection_test.cc:45
google::protobuf::Descriptor::DebugString
std::string DebugString() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2497
google::protobuf::MethodDescriptor::output_type
const Descriptor * output_type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:7249
grpc::testing::ProtoServerReflectionTest::known_types_
std::unordered_set< string > known_types_
Definition: proto_server_reflection_test.cc:126
server_address
std::string server_address("0.0.0.0:10000")
proto_reflection_descriptor_database.h
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
test_service_impl.h
grpc::testing::ProtoServerReflectionTest::port_
int port_
Definition: proto_server_reflection_test.cc:128
grpc::testing::ProtoServerReflectionTest::ref_desc_pool_
const protobuf::DescriptorPool * ref_desc_pool_
Definition: proto_server_reflection_test.cc:127
google::protobuf::FileDescriptor::package
const std::string & package() const
google::protobuf::ServiceDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1152
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
google::protobuf::FileDescriptor::DebugString
std::string DebugString() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2396
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
grpc::testing::ProtoServerReflectionTest::CompareMethod
void CompareMethod(const std::string &method)
Definition: proto_server_reflection_test.cc:94
grpc.h
google::protobuf::DescriptorPool
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1539
channel.h
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
grpc::testing::ProtoServerReflectionTest::CompareService
void CompareService(const std::string &service)
Definition: proto_server_reflection_test.cc:72
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
main
int main(int argc, char **argv)
Definition: proto_server_reflection_test.cc:148
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
google::protobuf::ServiceDescriptor::file
const FileDescriptor * file() const
grpc::testing::ProtoServerReflectionTest::known_files_
std::unordered_set< string > known_files_
Definition: proto_server_reflection_test.cc:125
google::protobuf::MethodDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1234
server_credentials.h
grpc::testing::ProtoServerReflectionTest::CompareType
void CompareType(const std::string &type)
Definition: proto_server_reflection_test.cc:107
test_config.h
greeter_client.services
services
Definition: no_codegen/greeter_client.py:34
grpc::reflection::ProtoServerReflectionPlugin
Definition: grpcpp/ext/proto_server_reflection_plugin.h:31
google::protobuf::FileDescriptor::name
const std::string & name() const
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::testing::ProtoServerReflectionTest::plugin_
reflection::ProtoServerReflectionPlugin plugin_
Definition: proto_server_reflection_test.cc:129
credentials.h
google::protobuf::MethodDescriptor::input_type
const Descriptor * input_type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:7245
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
grpc::testing::ProtoServerReflectionTest
Definition: proto_server_reflection_test.cc:43
server_context.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
google::protobuf::DescriptorPool::generated_pool
static const DescriptorPool * generated_pool()
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1326
google::protobuf::ServiceDescriptor::method_count
int method_count() const
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
desc
#define desc
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.h:338
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
grpc::testing::ProtoServerReflectionTest::desc_pool_
std::unique_ptr< protobuf::DescriptorPool > desc_pool_
Definition: proto_server_reflection_test.cc:124
grpc::testing::ProtoServerReflectionTest::ResetStub
void ResetStub()
Definition: proto_server_reflection_test.cc:57
google::protobuf::ServiceDescriptor::method
const MethodDescriptor * method(int index) const
grpc::testing::EXPECT_TRUE
EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(minimum_valid_json, &options) .ok())
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
server.h
grpc::testing::ProtoServerReflectionTest::desc_db_
std::unique_ptr< ProtoReflectionDescriptorDatabase > desc_db_
Definition: proto_server_reflection_test.cc:123
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
method
NSString * method
Definition: ProtoMethod.h:28
google::protobuf::DescriptorPool::FindMethodByName
const MethodDescriptor * FindMethodByName(const std::string &name) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1462
grpc::testing::ProtoServerReflectionTest::to_string
string to_string(const int number)
Definition: proto_server_reflection_test.cc:66
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
google::protobuf::ServiceDescriptor::DebugString
std::string DebugString() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2859
server_builder.h
google::protobuf::MethodDescriptor::full_name
const std::string & full_name() const
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
grpc::testing::ProtoServerReflectionTest::server_
std::unique_ptr< Server > server_
Definition: proto_server_reflection_test.cc:121
google::protobuf::DescriptorPool::FindServiceByName
const ServiceDescriptor * FindServiceByName(const std::string &name) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1456
grpc::testing::ProtoServerReflectionTest::SetUp
void SetUp() override
Definition: proto_server_reflection_test.cc:47
google::protobuf::DescriptorPool::FindMessageTypeByName
const Descriptor * FindMessageTypeByName(const std::string &name) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1410


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:56