• 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/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/gprpp/memory.h"
28 #include "src/core/lib/iomgr/work_serializer.h"
29 #include "test/core/util/test_config.h"
30 
31 static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer;
32 
33 class TestResultHandler : public grpc_core::Resolver::ResultHandler {
ReturnResult(grpc_core::Resolver::Result)34   void ReturnResult(grpc_core::Resolver::Result /*result*/) override {}
ReturnError(grpc_error *)35   void ReturnError(grpc_error* /*error*/) override {}
36 };
37 
test_succeeds(grpc_core::ResolverFactory * factory,const char * string)38 static void test_succeeds(grpc_core::ResolverFactory* factory,
39                           const char* string) {
40   gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
41           factory->scheme());
42   grpc_core::ExecCtx exec_ctx;
43   grpc_uri* uri = grpc_uri_parse(string, 0);
44   GPR_ASSERT(uri);
45   grpc_core::ResolverArgs args;
46   args.uri = uri;
47   args.work_serializer = *g_work_serializer;
48   args.result_handler = absl::make_unique<TestResultHandler>();
49   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
50       factory->CreateResolver(std::move(args));
51   GPR_ASSERT(resolver != nullptr);
52   grpc_uri_destroy(uri);
53 }
54 
test_fails(grpc_core::ResolverFactory * factory,const char * string)55 static void test_fails(grpc_core::ResolverFactory* factory,
56                        const char* string) {
57   gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
58           factory->scheme());
59   grpc_core::ExecCtx exec_ctx;
60   grpc_uri* uri = grpc_uri_parse(string, 0);
61   GPR_ASSERT(uri);
62   grpc_core::ResolverArgs args;
63   args.uri = uri;
64   args.work_serializer = *g_work_serializer;
65   args.result_handler = absl::make_unique<TestResultHandler>();
66   grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
67       factory->CreateResolver(std::move(args));
68   GPR_ASSERT(resolver == nullptr);
69   grpc_uri_destroy(uri);
70 }
71 
main(int argc,char ** argv)72 int main(int argc, char** argv) {
73   grpc::testing::TestEnvironment env(argc, argv);
74   grpc_init();
75 
76   auto work_serializer = std::make_shared<grpc_core::WorkSerializer>();
77   g_work_serializer = &work_serializer;
78 
79   grpc_core::ResolverFactory* dns =
80       grpc_core::ResolverRegistry::LookupResolverFactory("dns");
81 
82   test_succeeds(dns, "dns:10.2.1.1");
83   test_succeeds(dns, "dns:10.2.1.1:1234");
84   test_succeeds(dns, "dns:www.google.com");
85   test_succeeds(dns, "dns:///www.google.com");
86   grpc_core::UniquePtr<char> resolver =
87       GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
88   if (gpr_stricmp(resolver.get(), "native") == 0) {
89     test_fails(dns, "dns://8.8.8.8/8.8.8.8:8888");
90   } else {
91     test_succeeds(dns, "dns://8.8.8.8/8.8.8.8:8888");
92   }
93   grpc_shutdown();
94 
95   return 0;
96 }
97