Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/client_config/resolvers/dns_resolver.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/log.h> |
| 39 | |
| 40 | #include "src/core/client_config/resolver.h" |
| 41 | #include "test/core/util/test_config.h" |
| 42 | |
| 43 | static void subchannel_factory_ref(grpc_subchannel_factory *scv) {} |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 44 | static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 45 | grpc_subchannel_factory *scv) {} |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 46 | static grpc_subchannel *subchannel_factory_create_subchannel( |
| 47 | grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *factory, |
| 48 | grpc_subchannel_args *args) { |
Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 49 | GPR_UNREACHABLE_CODE(return NULL); |
| 50 | } |
| 51 | |
| 52 | static const grpc_subchannel_factory_vtable sc_vtable = { |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 53 | subchannel_factory_ref, subchannel_factory_unref, |
| 54 | subchannel_factory_create_subchannel}; |
Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 55 | |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 56 | static grpc_subchannel_factory sc_factory = {&sc_vtable}; |
Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 57 | |
| 58 | static void test_succeeds(grpc_resolver_factory *factory, const char *string) { |
| 59 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 60 | grpc_uri *uri = grpc_uri_parse(string, 0); |
| 61 | grpc_resolver_args args; |
| 62 | grpc_resolver *resolver; |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 63 | gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string, |
| 64 | factory->vtable->scheme); |
Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 65 | GPR_ASSERT(uri); |
| 66 | memset(&args, 0, sizeof(args)); |
| 67 | args.uri = uri; |
| 68 | args.subchannel_factory = &sc_factory; |
| 69 | resolver = grpc_resolver_factory_create_resolver(factory, &args); |
| 70 | GPR_ASSERT(resolver != NULL); |
| 71 | GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_succeeds"); |
| 72 | grpc_uri_destroy(uri); |
| 73 | grpc_exec_ctx_finish(&exec_ctx); |
| 74 | } |
| 75 | |
| 76 | static void test_fails(grpc_resolver_factory *factory, const char *string) { |
| 77 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 78 | grpc_uri *uri = grpc_uri_parse(string, 0); |
| 79 | grpc_resolver_args args; |
| 80 | grpc_resolver *resolver; |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 81 | gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string, |
| 82 | factory->vtable->scheme); |
Craig Tiller | 147c4f4 | 2015-12-11 12:32:39 -0800 | [diff] [blame] | 83 | GPR_ASSERT(uri); |
| 84 | memset(&args, 0, sizeof(args)); |
| 85 | args.uri = uri; |
| 86 | resolver = grpc_resolver_factory_create_resolver(factory, &args); |
| 87 | GPR_ASSERT(resolver == NULL); |
| 88 | grpc_uri_destroy(uri); |
| 89 | grpc_exec_ctx_finish(&exec_ctx); |
| 90 | } |
| 91 | |
| 92 | int main(int argc, char **argv) { |
| 93 | grpc_resolver_factory *dns; |
| 94 | grpc_test_init(argc, argv); |
| 95 | |
| 96 | dns = grpc_dns_resolver_factory_create(); |
| 97 | |
| 98 | test_succeeds(dns, "dns:10.2.1.1"); |
| 99 | test_succeeds(dns, "dns:10.2.1.1:1234"); |
| 100 | test_succeeds(dns, "ipv4:www.google.com"); |
| 101 | test_fails(dns, "ipv4://8.8.8.8/8.8.8.8:8888"); |
| 102 | |
| 103 | grpc_resolver_factory_unref(dns); |
| 104 | |
| 105 | return 0; |
| 106 | } |