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/alloc.h>
22 #include <grpc/support/log.h>
23 #include <grpc/support/string_util.h>
24
25 #include "src/core/ext/filters/client_channel/resolver_registry.h"
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/iomgr/work_serializer.h"
28
29 #include "test/core/util/test_config.h"
30
31 static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer;
32
33 class ResultHandler : public grpc_core::Resolver::ResultHandler {
34 public:
ReturnResult(grpc_core::Resolver::Result)35 void ReturnResult(grpc_core::Resolver::Result /*result*/) override {}
36
ReturnError(grpc_error * error)37 void ReturnError(grpc_error* error) override { GRPC_ERROR_UNREF(error); }
38 };
39
test_succeeds(grpc_core::ResolverFactory * factory,const char * string)40 static void test_succeeds(grpc_core::ResolverFactory* factory,
41 const char* string) {
42 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
43 factory->scheme());
44 grpc_core::ExecCtx exec_ctx;
45 grpc_uri* uri = grpc_uri_parse(string, 0);
46 GPR_ASSERT(uri);
47 grpc_core::ResolverArgs args;
48 args.uri = uri;
49 args.work_serializer = *g_work_serializer;
50 args.result_handler = absl::make_unique<ResultHandler>();
51 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
52 factory->CreateResolver(std::move(args));
53 GPR_ASSERT(resolver != nullptr);
54 grpc_uri_destroy(uri);
55 resolver->StartLocked();
56 /* Flush ExecCtx to avoid stack-use-after-scope on on_res_arg which is
57 * accessed in the closure on_resolution_cb */
58 grpc_core::ExecCtx::Get()->Flush();
59 }
60
test_fails(grpc_core::ResolverFactory * factory,const char * string)61 static void test_fails(grpc_core::ResolverFactory* factory,
62 const char* string) {
63 gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
64 factory->scheme());
65 grpc_core::ExecCtx exec_ctx;
66 grpc_uri* uri = grpc_uri_parse(string, 0);
67 GPR_ASSERT(uri);
68 grpc_core::ResolverArgs args;
69 args.uri = uri;
70 args.work_serializer = *g_work_serializer;
71 args.result_handler = absl::make_unique<ResultHandler>();
72 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver =
73 factory->CreateResolver(std::move(args));
74 GPR_ASSERT(resolver == nullptr);
75 grpc_uri_destroy(uri);
76 }
77
main(int argc,char ** argv)78 int main(int argc, char** argv) {
79 grpc::testing::TestEnvironment env(argc, argv);
80 grpc_init();
81
82 auto work_serializer = std::make_shared<grpc_core::WorkSerializer>();
83 g_work_serializer = &work_serializer;
84
85 grpc_core::ResolverFactory* ipv4 =
86 grpc_core::ResolverRegistry::LookupResolverFactory("ipv4");
87 grpc_core::ResolverFactory* ipv6 =
88 grpc_core::ResolverRegistry::LookupResolverFactory("ipv6");
89
90 test_fails(ipv4, "ipv4:10.2.1.1");
91 test_succeeds(ipv4, "ipv4:10.2.1.1:1234");
92 test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321");
93 test_fails(ipv4, "ipv4:10.2.1.1:123456");
94 test_fails(ipv4, "ipv4:www.google.com");
95 test_fails(ipv4, "ipv4:[");
96 test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888");
97
98 test_fails(ipv6, "ipv6:[");
99 test_fails(ipv6, "ipv6:[::]");
100 test_succeeds(ipv6, "ipv6:[::]:1234");
101 test_fails(ipv6, "ipv6:[::]:123456");
102 test_fails(ipv6, "ipv6:www.google.com");
103
104 grpc_shutdown();
105
106 return 0;
107 }
108