• 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 <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_registry.h"
25 #include "src/core/lib/iomgr/combiner.h"
26 #include "test/core/util/test_config.h"
27 
28 static grpc_combiner* g_combiner;
29 
test_succeeds(grpc_core::ResolverFactory * factory,const char * string)30 static void test_succeeds(grpc_core::ResolverFactory* factory,
31                           const char* string) {
32   gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
33           factory->scheme());
34   grpc_core::ExecCtx exec_ctx;
35   grpc_uri* uri = grpc_uri_parse(string, 0);
36   GPR_ASSERT(uri);
37   grpc_core::ResolverArgs args;
38   args.uri = uri;
39   args.combiner = g_combiner;
40   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
41       factory->CreateResolver(args);
42   GPR_ASSERT(resolver != nullptr);
43   grpc_uri_destroy(uri);
44 }
45 
test_fails(grpc_core::ResolverFactory * factory,const char * string)46 static void test_fails(grpc_core::ResolverFactory* factory,
47                        const char* string) {
48   gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
49           factory->scheme());
50   grpc_core::ExecCtx exec_ctx;
51   grpc_uri* uri = grpc_uri_parse(string, 0);
52   GPR_ASSERT(uri);
53   grpc_core::ResolverArgs args;
54   args.uri = uri;
55   args.combiner = g_combiner;
56   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
57       factory->CreateResolver(args);
58   GPR_ASSERT(resolver == nullptr);
59   grpc_uri_destroy(uri);
60 }
61 
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63   grpc_test_init(argc, argv);
64   grpc_init();
65 
66   g_combiner = grpc_combiner_create();
67 
68   grpc_core::ResolverFactory* dns =
69       grpc_core::ResolverRegistry::LookupResolverFactory("dns");
70 
71   test_succeeds(dns, "dns:10.2.1.1");
72   test_succeeds(dns, "dns:10.2.1.1:1234");
73   test_succeeds(dns, "dns:www.google.com");
74   test_succeeds(dns, "dns:///www.google.com");
75   if (grpc_resolve_address == grpc_resolve_address_ares) {
76     test_succeeds(dns, "dns://8.8.8.8/8.8.8.8:8888");
77   } else {
78     test_fails(dns, "dns://8.8.8.8/8.8.8.8:8888");
79   }
80 
81   {
82     grpc_core::ExecCtx exec_ctx;
83     GRPC_COMBINER_UNREF(g_combiner, "test");
84   }
85   grpc_shutdown();
86 
87   return 0;
88 }
89