Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/resolve_address.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | #include <grpc/support/log.h> |
| 36 | #include <grpc/support/sync.h> |
| 37 | #include <grpc/support/time.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/executor.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include "test/core/util/test_config.h" |
| 40 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 41 | static gpr_timespec test_deadline(void) { |
| 42 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(100); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 45 | static void must_succeed(grpc_exec_ctx *exec_ctx, void *evp, |
| 46 | grpc_resolved_addresses *p) { |
| 47 | GPR_ASSERT(p); |
| 48 | GPR_ASSERT(p->naddrs >= 1); |
| 49 | grpc_resolved_addresses_destroy(p); |
| 50 | gpr_event_set(evp, (void *)1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | static void must_fail(grpc_exec_ctx *exec_ctx, void *evp, |
| 54 | grpc_resolved_addresses *p) { |
| 55 | GPR_ASSERT(!p); |
| 56 | gpr_event_set(evp, (void *)1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | static void test_localhost(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | gpr_event ev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 61 | gpr_event_init(&ev); |
| 62 | grpc_resolve_address("localhost:1", NULL, must_succeed, &ev); |
| 63 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | static void test_default_port(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | gpr_event ev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | gpr_event_init(&ev); |
| 69 | grpc_resolve_address("localhost", "1", must_succeed, &ev); |
| 70 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 73 | static void test_missing_default_port(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | gpr_event ev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | gpr_event_init(&ev); |
| 76 | grpc_resolve_address("localhost", NULL, must_fail, &ev); |
| 77 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | static void test_ipv6_with_port(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | gpr_event ev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | gpr_event_init(&ev); |
| 83 | grpc_resolve_address("[2001:db8::1]:1", NULL, must_succeed, &ev); |
| 84 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | static void test_ipv6_without_port(void) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 88 | const char *const kCases[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | "2001:db8::1", "2001:db8::1.2.3.4", "[2001:db8::1]", |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 90 | }; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 91 | unsigned i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) { |
| 93 | gpr_event ev; |
| 94 | gpr_event_init(&ev); |
| 95 | grpc_resolve_address(kCases[i], "80", must_succeed, &ev); |
| 96 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
| 97 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | static void test_invalid_ip_addresses(void) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 101 | const char *const kCases[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | "293.283.1238.3:1", "[2001:db8::11111]:1", |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 103 | }; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 104 | unsigned i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) { |
| 106 | gpr_event ev; |
| 107 | gpr_event_init(&ev); |
| 108 | grpc_resolve_address(kCases[i], NULL, must_fail, &ev); |
| 109 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
| 110 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void test_unparseable_hostports(void) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 114 | const char *const kCases[] = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | "[", "[::1", "[::1]bad", "[1.2.3.4]", "[localhost]", "[localhost]:1", |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 116 | }; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 117 | unsigned i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | for (i = 0; i < sizeof(kCases) / sizeof(*kCases); i++) { |
| 119 | gpr_event ev; |
| 120 | gpr_event_init(&ev); |
| 121 | grpc_resolve_address(kCases[i], "1", must_fail, &ev); |
| 122 | GPR_ASSERT(gpr_event_wait(&ev, test_deadline())); |
| 123 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | int main(int argc, char **argv) { |
| 127 | grpc_test_init(argc, argv); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 128 | grpc_executor_init(); |
David Garcia Quintas | 661ad7f | 2015-10-13 15:51:31 -0700 | [diff] [blame] | 129 | grpc_iomgr_init(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | test_localhost(); |
| 131 | test_default_port(); |
| 132 | test_missing_default_port(); |
| 133 | test_ipv6_with_port(); |
| 134 | test_ipv6_without_port(); |
| 135 | test_invalid_ip_addresses(); |
| 136 | test_unparseable_hostports(); |
David Garcia Quintas | 661ad7f | 2015-10-13 15:51:31 -0700 | [diff] [blame] | 137 | grpc_iomgr_shutdown(); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 138 | grpc_executor_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 139 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 140 | } |