1 //
2 //
3 // Copyright 2021 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 <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "absl/strings/str_cat.h"
23 
24 #include <grpcpp/ext/admin_services.h>
25 #include <grpcpp/ext/proto_server_reflection_plugin.h>
26 #include <grpcpp/grpcpp.h>
27 
28 #include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h"
29 #include "test/core/util/port.h"
30 #include "test/core/util/test_config.h"
31 
32 namespace grpc {
33 namespace testing {
34 
35 class AdminServicesTest : public ::testing::Test {
36  public:
SetUp()37   void SetUp() override {
38     std::string address =
39         absl::StrCat("localhost:", grpc_pick_unused_port_or_die());
40     // Create admin server
41     grpc::reflection::InitProtoReflectionServerBuilderPlugin();
42     ServerBuilder builder;
43     builder.AddListeningPort(address, InsecureServerCredentials());
44     ::grpc::AddAdminServices(&builder);
45     server_ = builder.BuildAndStart();
46     // Create channel
47     auto reflection_stub = reflection::v1alpha::ServerReflection::NewStub(
48         CreateChannel(address, InsecureChannelCredentials()));
49     stream_ = reflection_stub->ServerReflectionInfo(&reflection_ctx_);
50   }
51 
GetServiceList()52   std::vector<std::string> GetServiceList() {
53     std::vector<std::string> services;
54     reflection::v1alpha::ServerReflectionRequest request;
55     reflection::v1alpha::ServerReflectionResponse response;
56     request.set_list_services("");
57     stream_->Write(request);
58     stream_->Read(&response);
59     for (auto& service : response.list_services_response().service()) {
60       services.push_back(service.name());
61     }
62     return services;
63   }
64 
65  private:
66   std::unique_ptr<Server> server_;
67   ClientContext reflection_ctx_;
68   std::shared_ptr<
69       ClientReaderWriter<reflection::v1alpha::ServerReflectionRequest,
70                          reflection::v1alpha::ServerReflectionResponse>>
71       stream_;
72 };
73 
TEST_F(AdminServicesTest,ValidateRegisteredServices)74 TEST_F(AdminServicesTest, ValidateRegisteredServices) {
75   // Using Contains here, because the server builder might register other
76   // services in certain environments.
77   EXPECT_THAT(
78       GetServiceList(),
79       ::testing::AllOf(
80           ::testing::Contains("grpc.channelz.v1.Channelz"),
81           ::testing::Contains("grpc.reflection.v1alpha.ServerReflection")));
82 #if defined(GRPC_NO_XDS) || defined(DISABLED_XDS_PROTO_IN_CC)
83   EXPECT_THAT(GetServiceList(),
84               ::testing::Not(::testing::Contains(
85                   "envoy.service.status.v3.ClientStatusDiscoveryService")));
86 #else
87   EXPECT_THAT(GetServiceList(),
88               ::testing::Contains(
89                   "envoy.service.status.v3.ClientStatusDiscoveryService"));
90 #endif  // GRPC_NO_XDS or DISABLED_XDS_PROTO_IN_CC
91 }
92 
93 }  // namespace testing
94 }  // namespace grpc
95 
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97   grpc::testing::TestEnvironment env(argc, argv);
98   ::testing::InitGoogleTest(&argc, argv);
99   int ret = RUN_ALL_TESTS();
100   return ret;
101 }
102