Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / test / core / client_channel / resolvers / dns_resolver_test.cc
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 <string.h>
20
21 #include <grpc/support/log.h>
22
23 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
24 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
25 #include "src/core/ext/filters/client_channel/resolver_registry.h"
26 #include "src/core/lib/gpr/string.h"
27 #include "src/core/lib/iomgr/combiner.h"
28 #include "test/core/util/test_config.h"
29
30 static grpc_combiner* g_combiner;
31
32 static void test_succeeds(grpc_core::ResolverFactory* factory,
33                           const char* string) {
34   gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
35           factory->scheme());
36   grpc_core::ExecCtx exec_ctx;
37   grpc_uri* uri = grpc_uri_parse(string, 0);
38   GPR_ASSERT(uri);
39   grpc_core::ResolverArgs args;
40   args.uri = uri;
41   args.combiner = g_combiner;
42   args.result_handler =
43       grpc_core::MakeUnique<grpc_core::Resolver::ResultHandler>();
44   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
45       factory->CreateResolver(std::move(args));
46   GPR_ASSERT(resolver != nullptr);
47   grpc_uri_destroy(uri);
48 }
49
50 static void test_fails(grpc_core::ResolverFactory* factory,
51                        const char* string) {
52   gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
53           factory->scheme());
54   grpc_core::ExecCtx exec_ctx;
55   grpc_uri* uri = grpc_uri_parse(string, 0);
56   GPR_ASSERT(uri);
57   grpc_core::ResolverArgs args;
58   args.uri = uri;
59   args.combiner = g_combiner;
60   args.result_handler =
61       grpc_core::MakeUnique<grpc_core::Resolver::ResultHandler>();
62   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
63       factory->CreateResolver(std::move(args));
64   GPR_ASSERT(resolver == nullptr);
65   grpc_uri_destroy(uri);
66 }
67
68 int main(int argc, char** argv) {
69   grpc::testing::TestEnvironment env(argc, argv);
70   grpc_init();
71
72   g_combiner = grpc_combiner_create();
73
74   grpc_core::ResolverFactory* dns =
75       grpc_core::ResolverRegistry::LookupResolverFactory("dns");
76
77   test_succeeds(dns, "dns:10.2.1.1");
78   test_succeeds(dns, "dns:10.2.1.1:1234");
79   test_succeeds(dns, "dns:www.google.com");
80   test_succeeds(dns, "dns:///www.google.com");
81   grpc_core::UniquePtr<char> resolver =
82       GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
83   if (gpr_stricmp(resolver.get(), "native") == 0) {
84     test_fails(dns, "dns://8.8.8.8/8.8.8.8:8888");
85   } else {
86     test_succeeds(dns, "dns://8.8.8.8/8.8.8.8:8888");
87   }
88   {
89     grpc_core::ExecCtx exec_ctx;
90     GRPC_COMBINER_UNREF(g_combiner, "test");
91   }
92   grpc_shutdown();
93
94   return 0;
95 }