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