Debian Codegraph @

test/core/client_channel/resolvers/dns_resolver_test.cc

raw
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/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
31 #include "src/core/lib/config/core_configuration.h"
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gprpp/global_config_generic.h"
34 #include "src/core/lib/gprpp/memory.h"
35 #include "src/core/lib/gprpp/orphanable.h"
36 #include "src/core/lib/gprpp/work_serializer.h"
37 #include "src/core/lib/iomgr/exec_ctx.h"
38 #include "src/core/lib/resolver/resolver.h"
39 #include "src/core/lib/resolver/resolver_factory.h"
40 #include "src/core/lib/resolver/resolver_registry.h"
41 #include "src/core/lib/uri/uri_parser.h"
42 #include "test/core/util/test_config.h"
43
44 static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer;
45
46 class TestResultHandler : public grpc_core::Resolver::ResultHandler {
47 void ReportResult(grpc_core::Resolver::Result /*result*/) override {}
48 };
49
50 static void test_succeeds(grpc_core::ResolverFactory* factory,
51 const char* string) {
52 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
53 std::string(factory->scheme()).c_str());
54 grpc_core::ExecCtx exec_ctx;
55 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string);
56 if (!uri.ok()) {
57 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
58 ASSERT_TRUE(uri.ok());
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 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
65 factory->CreateResolver(std::move(args));
66 ASSERT_NE(resolver, nullptr);
67 }
68
69 static void test_fails(grpc_core::ResolverFactory* factory,
70 const char* string) {
71 gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
72 std::string(factory->scheme()).c_str());
73 grpc_core::ExecCtx exec_ctx;
74 absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string);
75 if (!uri.ok()) {
76 gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
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<TestResultHandler>();
83 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
84 factory->CreateResolver(std::move(args));
85 ASSERT_EQ(resolver, nullptr);
86 }
87
88 TEST(DnsResolverTest, MainTest) {
89 auto work_serializer = std::make_shared<grpc_core::WorkSerializer>();
90 g_work_serializer = &work_serializer;
91
92 grpc_core::ResolverFactory* dns = grpc_core::CoreConfiguration::Get()
93 .resolver_registry()
94 .LookupResolverFactory("dns");
95
96 test_succeeds(dns, "dns:10.2.1.1");
97 test_succeeds(dns, "dns:10.2.1.1:1234");
98 test_succeeds(dns, "dns:www.google.com");
99 test_succeeds(dns, "dns:///www.google.com");
100 grpc_core::UniquePtr<char> resolver =
101 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
102 if (gpr_stricmp(resolver.get(), "native") == 0) {
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
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 }