xref: /aosp_15_r20/external/grpc-grpc/test/core/client_channel/resolvers/dns_resolver_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 "absl/strings/string_view.h"
26 #include "gtest/gtest.h"
27 
28 #include <grpc/support/log.h>
29 
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/config/config_vars.h"
32 #include "src/core/lib/config/core_configuration.h"
33 #include "src/core/lib/event_engine/default_event_engine.h"
34 #include "src/core/lib/gprpp/orphanable.h"
35 #include "src/core/lib/gprpp/work_serializer.h"
36 #include "src/core/lib/iomgr/exec_ctx.h"
37 #include "src/core/lib/uri/uri_parser.h"
38 #include "src/core/resolver/resolver.h"
39 #include "src/core/resolver/resolver_factory.h"
40 #include "src/core/resolver/resolver_registry.h"
41 #include "test/core/util/test_config.h"
42 
43 using ::grpc_event_engine::experimental::GetDefaultEventEngine;
44 
45 static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer;
46 
47 class TestResultHandler : public grpc_core::Resolver::ResultHandler {
ReportResult(grpc_core::Resolver::Result)48   void ReportResult(grpc_core::Resolver::Result /*result*/) override {}
49 };
50 
test_succeeds(grpc_core::ResolverFactory * factory,const char * string)51 static void test_succeeds(grpc_core::ResolverFactory* factory,
52                           const char* string) {
53   gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
54           std::string(factory->scheme()).c_str());
55   grpc_core::ExecCtx exec_ctx;
56   absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string);
57   if (!uri.ok()) {
58     FAIL() << "Error: " << uri.status().ToString();
59   }
60   grpc_core::ResolverArgs args;
61   args.uri = std::move(*uri);
62   args.work_serializer = *g_work_serializer;
63   args.result_handler = std::make_unique<TestResultHandler>();
64   args.args = args.args.SetObject(GetDefaultEventEngine());
65   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
66       factory->CreateResolver(std::move(args));
67   ASSERT_NE(resolver, nullptr);
68 }
69 
test_fails(grpc_core::ResolverFactory * factory,const char * string)70 static void test_fails(grpc_core::ResolverFactory* factory,
71                        const char* string) {
72   gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
73           std::string(factory->scheme()).c_str());
74   grpc_core::ExecCtx exec_ctx;
75   absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string);
76   if (!uri.ok()) {
77     FAIL() << "Error: " << uri.status().ToString();
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<TestResultHandler>();
83   args.args = args.args.SetObject(GetDefaultEventEngine());
84   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
85       factory->CreateResolver(std::move(args));
86   ASSERT_EQ(resolver, nullptr);
87 }
88 
TEST(DnsResolverTest,MainTest)89 TEST(DnsResolverTest, MainTest) {
90   auto work_serializer = std::make_shared<grpc_core::WorkSerializer>(
91       grpc_event_engine::experimental::GetDefaultEventEngine());
92   g_work_serializer = &work_serializer;
93 
94   grpc_core::ResolverFactory* dns = grpc_core::CoreConfiguration::Get()
95                                         .resolver_registry()
96                                         .LookupResolverFactory("dns");
97 
98   test_succeeds(dns, "dns:10.2.1.1");
99   test_succeeds(dns, "dns:10.2.1.1:1234");
100   test_succeeds(dns, "dns:www.google.com");
101   test_succeeds(dns, "dns:///www.google.com");
102   if (grpc_core::ConfigVars::Get().DnsResolver() == "native") {
103     test_fails(dns, "dns://8.8.8.8/8.8.8.8:8888");
104   } else {
105     test_succeeds(dns, "dns://8.8.8.8/8.8.8.8:8888");
106   }
107 }
108 
main(int argc,char ** argv)109 int main(int argc, char** argv) {
110   grpc::testing::TestEnvironment env(&argc, argv);
111   ::testing::InitGoogleTest(&argc, argv);
112   grpc::testing::TestGrpcScope grpc_scope;
113   return RUN_ALL_TESTS();
114 }
115