blob: 9212f01c3fc6377a587da34de6a1e480dd568be7 [file] [log] [blame]
nnoble0c475f02014-12-05 15:37:39 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
nnoble0c475f02014-12-05 15:37:39 -08004 * 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
ctiller18b49ab2014-12-09 14:39:16 -080034#include "src/core/iomgr/sockaddr_utils.h"
nnoble0c475f02014-12-05 15:37:39 -080035
36#include <errno.h>
nnoble0c475f02014-12-05 15:37:39 -080037#include <string.h>
38
39#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
41#include <grpc/support/port_platform.h>
42#include "test/core/util/test_config.h"
43
44static struct sockaddr_in make_addr4(const gpr_uint8 *data, size_t data_len) {
45 struct sockaddr_in addr4;
46 memset(&addr4, 0, sizeof(addr4));
47 addr4.sin_family = AF_INET;
48 GPR_ASSERT(data_len == sizeof(addr4.sin_addr.s_addr));
49 memcpy(&addr4.sin_addr.s_addr, data, data_len);
50 addr4.sin_port = htons(12345);
51 return addr4;
52}
53
54static struct sockaddr_in6 make_addr6(const gpr_uint8 *data, size_t data_len) {
55 struct sockaddr_in6 addr6;
56 memset(&addr6, 0, sizeof(addr6));
57 addr6.sin6_family = AF_INET6;
58 GPR_ASSERT(data_len == sizeof(addr6.sin6_addr.s6_addr));
59 memcpy(&addr6.sin6_addr.s6_addr, data, data_len);
60 addr6.sin6_port = htons(12345);
61 return addr6;
62}
63
64static const gpr_uint8 kMapped[] = {0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0xff, 0xff, 192, 0, 2, 1};
66static const gpr_uint8 kNotQuiteMapped[] = {0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0xff, 0xfe, 192, 0, 2, 99};
68static const gpr_uint8 kIPv4[] = {192, 0, 2, 1};
69static const gpr_uint8 kIPv6[] = {0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 1};
71
Craig Tiller32946d32015-01-15 11:37:30 -080072static void test_sockaddr_is_v4mapped(void) {
nnoble0c475f02014-12-05 15:37:39 -080073 struct sockaddr_in input4;
74 struct sockaddr_in6 input6;
75 struct sockaddr_in output4;
76 struct sockaddr_in expect4;
77
78 gpr_log(GPR_INFO, "%s", __FUNCTION__);
79
80 /* v4mapped input should succeed. */
81 input6 = make_addr6(kMapped, sizeof(kMapped));
82 GPR_ASSERT(grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, NULL));
83 GPR_ASSERT(
84 grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, &output4));
85 expect4 = make_addr4(kIPv4, sizeof(kIPv4));
86 GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0);
87
88 /* Non-v4mapped input should fail. */
89 input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped));
90 GPR_ASSERT(
91 !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, NULL));
92 GPR_ASSERT(
93 !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, &output4));
94 /* Output is unchanged. */
95 GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0);
96
97 /* Plain IPv4 input should also fail. */
98 input4 = make_addr4(kIPv4, sizeof(kIPv4));
99 GPR_ASSERT(
100 !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input4, NULL));
101}
102
Craig Tiller32946d32015-01-15 11:37:30 -0800103static void test_sockaddr_to_v4mapped(void) {
nnoble0c475f02014-12-05 15:37:39 -0800104 struct sockaddr_in input4;
105 struct sockaddr_in6 input6;
106 struct sockaddr_in6 output6;
107 struct sockaddr_in6 expect6;
108
109 gpr_log(GPR_INFO, "%s", __FUNCTION__);
110
111 /* IPv4 input should succeed. */
112 input4 = make_addr4(kIPv4, sizeof(kIPv4));
113 GPR_ASSERT(
114 grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input4, &output6));
115 expect6 = make_addr6(kMapped, sizeof(kMapped));
116 GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0);
117
118 /* IPv6 input should fail. */
119 input6 = make_addr6(kIPv6, sizeof(kIPv6));
120 GPR_ASSERT(
121 !grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input6, &output6));
122 /* Output is unchanged. */
123 GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0);
124
125 /* Already-v4mapped input should also fail. */
126 input6 = make_addr6(kMapped, sizeof(kMapped));
127 GPR_ASSERT(
128 !grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input6, &output6));
129}
130
Craig Tiller32946d32015-01-15 11:37:30 -0800131static void test_sockaddr_is_wildcard(void) {
nnoble0c475f02014-12-05 15:37:39 -0800132 struct sockaddr_in wild4;
133 struct sockaddr_in6 wild6;
134 struct sockaddr_in6 wild_mapped;
135 struct sockaddr dummy;
136 int port;
137
138 gpr_log(GPR_INFO, "%s", __FUNCTION__);
139
140 /* Generate wildcards. */
141 grpc_sockaddr_make_wildcards(555, &wild4, &wild6);
142 GPR_ASSERT(
143 grpc_sockaddr_to_v4mapped((const struct sockaddr *)&wild4, &wild_mapped));
144
145 /* Test 0.0.0.0:555 */
146 port = -1;
147 GPR_ASSERT(grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild4, &port));
148 GPR_ASSERT(port == 555);
149 memset(&wild4.sin_addr.s_addr, 0xbd, 1);
150 GPR_ASSERT(
151 !grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild4, &port));
152
153 /* Test [::]:555 */
154 port = -1;
155 GPR_ASSERT(grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild6, &port));
156 GPR_ASSERT(port == 555);
157 memset(&wild6.sin6_addr.s6_addr, 0xbd, 1);
158 GPR_ASSERT(
159 !grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild6, &port));
160
161 /* Test [::ffff:0.0.0.0]:555 */
162 port = -1;
163 GPR_ASSERT(
164 grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild_mapped, &port));
165 GPR_ASSERT(port == 555);
166 memset(&wild_mapped.sin6_addr.s6_addr, 0xbd, 1);
167 GPR_ASSERT(
168 !grpc_sockaddr_is_wildcard((const struct sockaddr *)&wild_mapped, &port));
169
170 /* Test AF_UNSPEC. */
171 port = -1;
172 memset(&dummy, 0, sizeof(dummy));
173 GPR_ASSERT(!grpc_sockaddr_is_wildcard(&dummy, &port));
174 GPR_ASSERT(port == -1);
175}
176
177static void expect_sockaddr_str(const char *expected, void *addr,
178 int normalize) {
179 int result;
180 char *str;
181 gpr_log(GPR_INFO, " expect_sockaddr_str(%s)", expected);
182 result = grpc_sockaddr_to_string(&str, (struct sockaddr *)addr, normalize);
183 GPR_ASSERT(str != NULL);
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100184 GPR_ASSERT(result >= 0);
185 GPR_ASSERT((size_t)result == strlen(str));
nnoble0c475f02014-12-05 15:37:39 -0800186 GPR_ASSERT(strcmp(expected, str) == 0);
187 gpr_free(str);
188}
189
Craig Tiller32946d32015-01-15 11:37:30 -0800190static void test_sockaddr_to_string(void) {
nnoble0c475f02014-12-05 15:37:39 -0800191 struct sockaddr_in input4;
192 struct sockaddr_in6 input6;
193 struct sockaddr dummy;
194
195 gpr_log(GPR_INFO, "%s", __FUNCTION__);
196
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100197 errno = 0x7EADBEEF;
nnoble0c475f02014-12-05 15:37:39 -0800198
199 input4 = make_addr4(kIPv4, sizeof(kIPv4));
200 expect_sockaddr_str("192.0.2.1:12345", &input4, 0);
201 expect_sockaddr_str("192.0.2.1:12345", &input4, 1);
202
203 input6 = make_addr6(kIPv6, sizeof(kIPv6));
204 expect_sockaddr_str("[2001:db8::1]:12345", &input6, 0);
205 expect_sockaddr_str("[2001:db8::1]:12345", &input6, 1);
206
207 input6 = make_addr6(kMapped, sizeof(kMapped));
208 expect_sockaddr_str("[::ffff:192.0.2.1]:12345", &input6, 0);
209 expect_sockaddr_str("192.0.2.1:12345", &input6, 1);
210
211 input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped));
212 expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 0);
213 expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 1);
214
215 memset(&dummy, 0, sizeof(dummy));
Craig Tillerd19d8502015-01-21 16:28:39 -0800216 dummy.sa_family = 123;
217 expect_sockaddr_str("(sockaddr family=123)", &dummy, 0);
218 expect_sockaddr_str("(sockaddr family=123)", &dummy, 1);
nnoble0c475f02014-12-05 15:37:39 -0800219
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100220 GPR_ASSERT(errno == 0x7EADBEEF);
nnoble0c475f02014-12-05 15:37:39 -0800221}
222
223int main(int argc, char **argv) {
224 grpc_test_init(argc, argv);
225
226 test_sockaddr_is_v4mapped();
227 test_sockaddr_to_v4mapped();
228 test_sockaddr_is_wildcard();
229 test_sockaddr_to_string();
230
231 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800232}