• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 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 <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/log/log.h"
24 #include "absl/status/status.h"
25 #include "absl/status/statusor.h"
26 #include "gtest/gtest.h"
27 #include "src/core/config/core_configuration.h"
28 #include "src/core/lib/event_engine/default_event_engine.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/lib/iomgr/port.h"
31 #include "src/core/resolver/resolver.h"
32 #include "src/core/resolver/resolver_factory.h"
33 #include "src/core/resolver/resolver_registry.h"
34 #include "src/core/util/orphanable.h"
35 #include "src/core/util/uri.h"
36 #include "src/core/util/work_serializer.h"
37 #include "test/core/test_util/test_config.h"
38 
39 static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer;
40 
41 class ResultHandler : public grpc_core::Resolver::ResultHandler {
42  public:
ReportResult(grpc_core::Resolver::Result)43   void ReportResult(grpc_core::Resolver::Result /*result*/) override {}
44 };
45 
test_succeeds(grpc_core::ResolverFactory * factory,const char * string)46 static void test_succeeds(grpc_core::ResolverFactory* factory,
47                           const char* string) {
48   VLOG(2) << "test: '" << string << "' should be valid for '"
49           << factory->scheme() << "'";
50   grpc_core::ExecCtx exec_ctx;
51   absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string);
52   if (!uri.ok()) {
53     LOG(ERROR) << uri.status().ToString();
54     ASSERT_TRUE(uri.ok());
55   }
56   grpc_core::ResolverArgs args;
57   args.uri = std::move(*uri);
58   args.work_serializer = *g_work_serializer;
59   args.result_handler = std::make_unique<ResultHandler>();
60   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
61       factory->CreateResolver(std::move(args));
62   ASSERT_NE(resolver, nullptr);
63   resolver->StartLocked();
64   // Flush ExecCtx to avoid stack-use-after-scope on on_res_arg which is
65   // accessed in the closure on_resolution_cb
66   grpc_core::ExecCtx::Get()->Flush();
67 }
68 
test_fails(grpc_core::ResolverFactory * factory,const char * string)69 static void test_fails(grpc_core::ResolverFactory* factory,
70                        const char* string) {
71   VLOG(2) << "test: '" << string << "' should be invalid for '"
72           << factory->scheme() << "'";
73   grpc_core::ExecCtx exec_ctx;
74   absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string);
75   if (!uri.ok()) {
76     LOG(ERROR) << uri.status().ToString();
77     ASSERT_TRUE(uri.ok());
78   }
79   grpc_core::ResolverArgs args;
80   args.uri = std::move(*uri);
81   args.work_serializer = *g_work_serializer;
82   args.result_handler = std::make_unique<ResultHandler>();
83   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
84       factory->CreateResolver(std::move(args));
85   ASSERT_EQ(resolver, nullptr);
86 }
87 
TEST(SockaddrResolverTest,MainTest)88 TEST(SockaddrResolverTest, MainTest) {
89   auto work_serializer = std::make_shared<grpc_core::WorkSerializer>(
90       grpc_event_engine::experimental::GetDefaultEventEngine());
91   g_work_serializer = &work_serializer;
92 
93   grpc_core::ResolverFactory* ipv4 = grpc_core::CoreConfiguration::Get()
94                                          .resolver_registry()
95                                          .LookupResolverFactory("ipv4");
96   grpc_core::ResolverFactory* ipv6 = grpc_core::CoreConfiguration::Get()
97                                          .resolver_registry()
98                                          .LookupResolverFactory("ipv6");
99 
100   test_fails(ipv4, "ipv4:10.2.1.1");
101   test_succeeds(ipv4, "ipv4:10.2.1.1:1234");
102   test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321");
103   test_fails(ipv4, "ipv4:10.2.1.1:123456");
104   test_fails(ipv4, "ipv4:www.google.com");
105   test_fails(ipv4, "ipv4:[");
106   test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888");
107 
108   test_fails(ipv6, "ipv6:[");
109   test_fails(ipv6, "ipv6:[::]");
110   test_succeeds(ipv6, "ipv6:[::]:1234");
111   test_fails(ipv6, "ipv6:[::]:123456");
112   test_fails(ipv6, "ipv6:www.google.com");
113 
114 #ifdef GRPC_HAVE_UNIX_SOCKET
115   grpc_core::ResolverFactory* uds = grpc_core::CoreConfiguration::Get()
116                                         .resolver_registry()
117                                         .LookupResolverFactory("unix");
118   grpc_core::ResolverFactory* uds_abstract =
119       grpc_core::CoreConfiguration::Get()
120           .resolver_registry()
121           .LookupResolverFactory("unix-abstract");
122 
123   test_succeeds(uds, "unix:///tmp/sockaddr_resolver_test");
124   test_succeeds(uds_abstract, "unix-abstract:sockaddr_resolver_test");
125 #endif  // GRPC_HAVE_UNIX_SOCKET
126 }
127 
main(int argc,char ** argv)128 int main(int argc, char** argv) {
129   grpc::testing::TestEnvironment env(&argc, argv);
130   ::testing::InitGoogleTest(&argc, argv);
131   grpc::testing::TestGrpcScope grpc_scope;
132   return RUN_ALL_TESTS();
133 }
134