proto_server_reflection_test.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include <gtest/gtest.h>
  19. #include "absl/memory/memory.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 "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. namespace grpc {
  36. namespace testing {
  37. class ProtoServerReflectionTest : public ::testing::Test {
  38. public:
  39. ProtoServerReflectionTest() {}
  40. void SetUp() override {
  41. port_ = grpc_pick_unused_port_or_die();
  42. ref_desc_pool_ = protobuf::DescriptorPool::generated_pool();
  43. ServerBuilder builder;
  44. std::string server_address = "localhost:" + to_string(port_);
  45. builder.AddListeningPort(server_address, InsecureServerCredentials());
  46. server_ = builder.BuildAndStart();
  47. }
  48. void ResetStub() {
  49. string target = "dns:localhost:" + to_string(port_);
  50. std::shared_ptr<Channel> channel =
  51. grpc::CreateChannel(target, InsecureChannelCredentials());
  52. stub_ = grpc::testing::EchoTestService::NewStub(channel);
  53. desc_db_ = absl::make_unique<ProtoReflectionDescriptorDatabase>(channel);
  54. desc_pool_ = absl::make_unique<protobuf::DescriptorPool>(desc_db_.get());
  55. }
  56. string to_string(const int number) {
  57. std::stringstream strs;
  58. strs << number;
  59. return strs.str();
  60. }
  61. void CompareService(const std::string& service) {
  62. const protobuf::ServiceDescriptor* service_desc =
  63. desc_pool_->FindServiceByName(service);
  64. const protobuf::ServiceDescriptor* ref_service_desc =
  65. ref_desc_pool_->FindServiceByName(service);
  66. EXPECT_TRUE(service_desc != nullptr);
  67. EXPECT_TRUE(ref_service_desc != nullptr);
  68. EXPECT_EQ(service_desc->DebugString(), ref_service_desc->DebugString());
  69. const protobuf::FileDescriptor* file_desc = service_desc->file();
  70. if (known_files_.find(file_desc->package() + "/" + file_desc->name()) !=
  71. known_files_.end()) {
  72. EXPECT_EQ(file_desc->DebugString(),
  73. ref_service_desc->file()->DebugString());
  74. known_files_.insert(file_desc->package() + "/" + file_desc->name());
  75. }
  76. for (int i = 0; i < service_desc->method_count(); ++i) {
  77. CompareMethod(service_desc->method(i)->full_name());
  78. }
  79. }
  80. void CompareMethod(const std::string& method) {
  81. const protobuf::MethodDescriptor* method_desc =
  82. desc_pool_->FindMethodByName(method);
  83. const protobuf::MethodDescriptor* ref_method_desc =
  84. ref_desc_pool_->FindMethodByName(method);
  85. EXPECT_TRUE(method_desc != nullptr);
  86. EXPECT_TRUE(ref_method_desc != nullptr);
  87. EXPECT_EQ(method_desc->DebugString(), ref_method_desc->DebugString());
  88. CompareType(method_desc->input_type()->full_name());
  89. CompareType(method_desc->output_type()->full_name());
  90. }
  91. void CompareType(const std::string& type) {
  92. if (known_types_.find(type) != known_types_.end()) {
  93. return;
  94. }
  95. const protobuf::Descriptor* desc = desc_pool_->FindMessageTypeByName(type);
  96. const protobuf::Descriptor* ref_desc =
  97. ref_desc_pool_->FindMessageTypeByName(type);
  98. EXPECT_TRUE(desc != nullptr);
  99. EXPECT_TRUE(ref_desc != nullptr);
  100. EXPECT_EQ(desc->DebugString(), ref_desc->DebugString());
  101. }
  102. protected:
  103. std::unique_ptr<Server> server_;
  104. std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
  105. std::unique_ptr<ProtoReflectionDescriptorDatabase> desc_db_;
  106. std::unique_ptr<protobuf::DescriptorPool> desc_pool_;
  107. std::unordered_set<string> known_files_;
  108. std::unordered_set<string> known_types_;
  109. const protobuf::DescriptorPool* ref_desc_pool_;
  110. int port_;
  111. reflection::ProtoServerReflectionPlugin plugin_;
  112. };
  113. TEST_F(ProtoServerReflectionTest, CheckResponseWithLocalDescriptorPool) {
  114. ResetStub();
  115. std::vector<std::string> services;
  116. desc_db_->GetServices(&services);
  117. // The service list has at least one service (reflection servcie).
  118. EXPECT_TRUE(!services.empty());
  119. for (auto it = services.begin(); it != services.end(); ++it) {
  120. CompareService(*it);
  121. }
  122. }
  123. } // namespace testing
  124. } // namespace grpc
  125. int main(int argc, char** argv) {
  126. grpc::testing::TestEnvironment env(argc, argv);
  127. ::testing::InitGoogleTest(&argc, argv);
  128. return RUN_ALL_TESTS();
  129. }