Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 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 | |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/support/log.h> |
| 37 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 38 | #include "src/core/ext/client_config/resolver_registry.h" |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 39 | #include "test/core/util/test_config.h" |
| 40 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 41 | static void client_channel_factory_ref(grpc_client_channel_factory *scv) {} |
| 42 | static void client_channel_factory_unref(grpc_exec_ctx *exec_ctx, |
| 43 | grpc_client_channel_factory *scv) {} |
| 44 | static grpc_subchannel *client_channel_factory_create_subchannel( |
| 45 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *factory, |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 46 | grpc_subchannel_args *args) { |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 47 | GPR_UNREACHABLE_CODE(return NULL); |
| 48 | } |
| 49 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 50 | static grpc_channel *client_channel_factory_create_channel( |
| 51 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory, |
David Garcia Quintas | 0b868c7 | 2016-04-01 14:28:22 -0700 | [diff] [blame] | 52 | const char *target, grpc_client_channel_type type, |
| 53 | grpc_channel_args *args) { |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 54 | GPR_UNREACHABLE_CODE(return NULL); |
| 55 | } |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 56 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 57 | static const grpc_client_channel_factory_vtable sc_vtable = { |
| 58 | client_channel_factory_ref, client_channel_factory_unref, |
| 59 | client_channel_factory_create_subchannel, |
| 60 | client_channel_factory_create_channel}; |
| 61 | |
| 62 | static grpc_client_channel_factory cc_factory = {&sc_vtable}; |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 63 | |
| 64 | static void test_succeeds(grpc_resolver_factory *factory, const char *string) { |
| 65 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 66 | grpc_uri *uri = grpc_uri_parse(string, 0); |
| 67 | grpc_resolver_args args; |
| 68 | grpc_resolver *resolver; |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 69 | gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string, |
| 70 | factory->vtable->scheme); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 71 | GPR_ASSERT(uri); |
| 72 | memset(&args, 0, sizeof(args)); |
| 73 | args.uri = uri; |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 74 | args.client_channel_factory = &cc_factory; |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 75 | resolver = grpc_resolver_factory_create_resolver(factory, &args); |
| 76 | GPR_ASSERT(resolver != NULL); |
| 77 | GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_succeeds"); |
| 78 | grpc_uri_destroy(uri); |
| 79 | grpc_exec_ctx_finish(&exec_ctx); |
| 80 | } |
| 81 | |
| 82 | static void test_fails(grpc_resolver_factory *factory, const char *string) { |
| 83 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 84 | grpc_uri *uri = grpc_uri_parse(string, 0); |
| 85 | grpc_resolver_args args; |
| 86 | grpc_resolver *resolver; |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 87 | gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string, |
| 88 | factory->vtable->scheme); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 89 | GPR_ASSERT(uri); |
| 90 | memset(&args, 0, sizeof(args)); |
| 91 | args.uri = uri; |
| 92 | resolver = grpc_resolver_factory_create_resolver(factory, &args); |
| 93 | GPR_ASSERT(resolver == NULL); |
| 94 | grpc_uri_destroy(uri); |
| 95 | grpc_exec_ctx_finish(&exec_ctx); |
| 96 | } |
| 97 | |
| 98 | int main(int argc, char **argv) { |
| 99 | grpc_resolver_factory *ipv4, *ipv6; |
| 100 | grpc_test_init(argc, argv); |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 101 | grpc_init(); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 102 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 103 | ipv4 = grpc_resolver_factory_lookup("ipv4"); |
| 104 | ipv6 = grpc_resolver_factory_lookup("ipv6"); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 105 | |
| 106 | test_fails(ipv4, "ipv4:10.2.1.1"); |
| 107 | test_succeeds(ipv4, "ipv4:10.2.1.1:1234"); |
David Garcia Quintas | 2c73b22 | 2016-03-23 14:09:26 -0700 | [diff] [blame] | 108 | test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321"); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 109 | test_fails(ipv4, "ipv4:10.2.1.1:123456"); |
| 110 | test_fails(ipv4, "ipv4:www.google.com"); |
| 111 | test_fails(ipv4, "ipv4:["); |
| 112 | test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888"); |
| 113 | |
| 114 | test_fails(ipv6, "ipv6:["); |
| 115 | test_fails(ipv6, "ipv6:[::]"); |
| 116 | test_succeeds(ipv6, "ipv6:[::]:1234"); |
| 117 | test_fails(ipv6, "ipv6:[::]:123456"); |
Craig Tiller | 795764b | 2015-12-11 08:56:09 -0800 | [diff] [blame] | 118 | test_fails(ipv6, "ipv6:www.google.com"); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 119 | |
| 120 | grpc_resolver_factory_unref(ipv4); |
| 121 | grpc_resolver_factory_unref(ipv6); |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 122 | grpc_shutdown(); |
Craig Tiller | b8bd62e | 2015-12-10 15:51:15 -0800 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |