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/combiner.h" 28 29 #include "test/core/util/test_config.h" 30 31 static grpc_combiner* g_combiner; 32 33 typedef struct on_resolution_arg { 34 char* expected_server_name; 35 grpc_channel_args* resolver_result; 36 } on_resolution_arg; 37 38 void on_resolution_cb(void* arg, grpc_error* error) { 39 on_resolution_arg* res = static_cast<on_resolution_arg*>(arg); 40 grpc_channel_args_destroy(res->resolver_result); 41 } 42 43 static void test_succeeds(grpc_core::ResolverFactory* factory, 44 const char* string) { 45 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string, 46 factory->scheme()); 47 grpc_core::ExecCtx exec_ctx; 48 grpc_uri* uri = grpc_uri_parse(string, 0); 49 GPR_ASSERT(uri); 50 grpc_core::ResolverArgs args; 51 args.uri = uri; 52 args.combiner = g_combiner; 53 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver = 54 factory->CreateResolver(args); 55 GPR_ASSERT(resolver != nullptr); 56 57 on_resolution_arg on_res_arg; 58 memset(&on_res_arg, 0, sizeof(on_res_arg)); 59 on_res_arg.expected_server_name = uri->path; 60 grpc_closure* on_resolution = GRPC_CLOSURE_CREATE( 61 on_resolution_cb, &on_res_arg, grpc_schedule_on_exec_ctx); 62 63 resolver->NextLocked(&on_res_arg.resolver_result, on_resolution); 64 grpc_uri_destroy(uri); 65 /* Flush ExecCtx to avoid stack-use-after-scope on on_res_arg which is 66 * accessed in the closure on_resolution_cb */ 67 grpc_core::ExecCtx::Get()->Flush(); 68 } 69 70 static void test_fails(grpc_core::ResolverFactory* factory, 71 const char* string) { 72 gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string, 73 factory->scheme()); 74 grpc_core::ExecCtx exec_ctx; 75 grpc_uri* uri = grpc_uri_parse(string, 0); 76 GPR_ASSERT(uri); 77 grpc_core::ResolverArgs args; 78 args.uri = uri; 79 args.combiner = g_combiner; 80 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver = 81 factory->CreateResolver(args); 82 GPR_ASSERT(resolver == nullptr); 83 grpc_uri_destroy(uri); 84 } 85 86 int main(int argc, char** argv) { 87 grpc_test_init(argc, argv); 88 grpc_init(); 89 90 g_combiner = grpc_combiner_create(); 91 92 grpc_core::ResolverFactory* ipv4 = 93 grpc_core::ResolverRegistry::LookupResolverFactory("ipv4"); 94 grpc_core::ResolverFactory* ipv6 = 95 grpc_core::ResolverRegistry::LookupResolverFactory("ipv6"); 96 97 test_fails(ipv4, "ipv4:10.2.1.1"); 98 test_succeeds(ipv4, "ipv4:10.2.1.1:1234"); 99 test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321"); 100 test_fails(ipv4, "ipv4:10.2.1.1:123456"); 101 test_fails(ipv4, "ipv4:www.google.com"); 102 test_fails(ipv4, "ipv4:["); 103 test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888"); 104 105 test_fails(ipv6, "ipv6:["); 106 test_fails(ipv6, "ipv6:[::]"); 107 test_succeeds(ipv6, "ipv6:[::]:1234"); 108 test_fails(ipv6, "ipv6:[::]:123456"); 109 test_fails(ipv6, "ipv6:www.google.com"); 110 111 { 112 grpc_core::ExecCtx exec_ctx; 113 GRPC_COMBINER_UNREF(g_combiner, "test"); 114 } 115 grpc_shutdown(); 116 117 return 0; 118 } 119