Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 13 | * derived from this software without specific prior written permission. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | #ifdef WIN32 |
michael@0 | 27 | #include <winsock2.h> |
michael@0 | 28 | #include <windows.h> |
michael@0 | 29 | #include <ws2tcpip.h> |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include "event2/event-config.h" |
michael@0 | 33 | |
michael@0 | 34 | #include <sys/types.h> |
michael@0 | 35 | |
michael@0 | 36 | #ifndef WIN32 |
michael@0 | 37 | #include <sys/socket.h> |
michael@0 | 38 | #include <netinet/in.h> |
michael@0 | 39 | #include <arpa/inet.h> |
michael@0 | 40 | #include <unistd.h> |
michael@0 | 41 | #endif |
michael@0 | 42 | #ifdef _EVENT_HAVE_NETINET_IN6_H |
michael@0 | 43 | #include <netinet/in6.h> |
michael@0 | 44 | #endif |
michael@0 | 45 | #ifdef _EVENT_HAVE_SYS_WAIT_H |
michael@0 | 46 | #include <sys/wait.h> |
michael@0 | 47 | #endif |
michael@0 | 48 | #include <signal.h> |
michael@0 | 49 | #include <stdio.h> |
michael@0 | 50 | #include <stdlib.h> |
michael@0 | 51 | #include <string.h> |
michael@0 | 52 | |
michael@0 | 53 | #include "event2/event.h" |
michael@0 | 54 | #include "event2/util.h" |
michael@0 | 55 | #include "../ipv6-internal.h" |
michael@0 | 56 | #include "../util-internal.h" |
michael@0 | 57 | #include "../log-internal.h" |
michael@0 | 58 | #include "../strlcpy-internal.h" |
michael@0 | 59 | |
michael@0 | 60 | #include "regress.h" |
michael@0 | 61 | |
michael@0 | 62 | enum entry_status { NORMAL, CANONICAL, BAD }; |
michael@0 | 63 | |
michael@0 | 64 | /* This is a big table of results we expect from generating and parsing */ |
michael@0 | 65 | static struct ipv4_entry { |
michael@0 | 66 | const char *addr; |
michael@0 | 67 | ev_uint32_t res; |
michael@0 | 68 | enum entry_status status; |
michael@0 | 69 | } ipv4_entries[] = { |
michael@0 | 70 | { "1.2.3.4", 0x01020304u, CANONICAL }, |
michael@0 | 71 | { "255.255.255.255", 0xffffffffu, CANONICAL }, |
michael@0 | 72 | { "256.0.0.0", 0, BAD }, |
michael@0 | 73 | { "ABC", 0, BAD }, |
michael@0 | 74 | { "1.2.3.4.5", 0, BAD }, |
michael@0 | 75 | { "176.192.208.244", 0xb0c0d0f4, CANONICAL }, |
michael@0 | 76 | { NULL, 0, BAD }, |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | static struct ipv6_entry { |
michael@0 | 80 | const char *addr; |
michael@0 | 81 | ev_uint32_t res[4]; |
michael@0 | 82 | enum entry_status status; |
michael@0 | 83 | } ipv6_entries[] = { |
michael@0 | 84 | { "::", { 0, 0, 0, 0, }, CANONICAL }, |
michael@0 | 85 | { "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL }, |
michael@0 | 86 | { "::1", { 0, 0, 0, 1, }, CANONICAL }, |
michael@0 | 87 | { "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL }, |
michael@0 | 88 | { "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL }, |
michael@0 | 89 | { "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL }, |
michael@0 | 90 | { "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL }, |
michael@0 | 91 | { "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL }, |
michael@0 | 92 | { "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL }, |
michael@0 | 93 | { "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL }, |
michael@0 | 94 | { "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL }, |
michael@0 | 95 | { "foobar.", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 96 | { "foobar", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 97 | { "fo:obar", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 98 | { "ffff", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 99 | { "fffff::", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 100 | { "fffff::", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 101 | { "::1.0.1.1000", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 102 | { "1:2:33333:4::", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 103 | { "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 104 | { "1::2::3", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 105 | { ":::1", { 0, 0, 0, 0 }, BAD }, |
michael@0 | 106 | { NULL, { 0, 0, 0, 0, }, BAD }, |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | static void |
michael@0 | 110 | regress_ipv4_parse(void *ptr) |
michael@0 | 111 | { |
michael@0 | 112 | int i; |
michael@0 | 113 | for (i = 0; ipv4_entries[i].addr; ++i) { |
michael@0 | 114 | char written[128]; |
michael@0 | 115 | struct ipv4_entry *ent = &ipv4_entries[i]; |
michael@0 | 116 | struct in_addr in; |
michael@0 | 117 | int r; |
michael@0 | 118 | r = evutil_inet_pton(AF_INET, ent->addr, &in); |
michael@0 | 119 | if (r == 0) { |
michael@0 | 120 | if (ent->status != BAD) { |
michael@0 | 121 | TT_FAIL(("%s did not parse, but it's a good address!", |
michael@0 | 122 | ent->addr)); |
michael@0 | 123 | } |
michael@0 | 124 | continue; |
michael@0 | 125 | } |
michael@0 | 126 | if (ent->status == BAD) { |
michael@0 | 127 | TT_FAIL(("%s parsed, but we expected an error", ent->addr)); |
michael@0 | 128 | continue; |
michael@0 | 129 | } |
michael@0 | 130 | if (ntohl(in.s_addr) != ent->res) { |
michael@0 | 131 | TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr, |
michael@0 | 132 | (unsigned long)ntohl(in.s_addr), |
michael@0 | 133 | (unsigned long)ent->res)); |
michael@0 | 134 | continue; |
michael@0 | 135 | } |
michael@0 | 136 | if (ent->status == CANONICAL) { |
michael@0 | 137 | const char *w = evutil_inet_ntop(AF_INET, &in, written, |
michael@0 | 138 | sizeof(written)); |
michael@0 | 139 | if (!w) { |
michael@0 | 140 | TT_FAIL(("Tried to write out %s; got NULL.", ent->addr)); |
michael@0 | 141 | continue; |
michael@0 | 142 | } |
michael@0 | 143 | if (strcmp(written, ent->addr)) { |
michael@0 | 144 | TT_FAIL(("Tried to write out %s; got %s", |
michael@0 | 145 | ent->addr, written)); |
michael@0 | 146 | continue; |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | static void |
michael@0 | 155 | regress_ipv6_parse(void *ptr) |
michael@0 | 156 | { |
michael@0 | 157 | #ifdef AF_INET6 |
michael@0 | 158 | int i, j; |
michael@0 | 159 | |
michael@0 | 160 | for (i = 0; ipv6_entries[i].addr; ++i) { |
michael@0 | 161 | char written[128]; |
michael@0 | 162 | struct ipv6_entry *ent = &ipv6_entries[i]; |
michael@0 | 163 | struct in6_addr in6; |
michael@0 | 164 | int r; |
michael@0 | 165 | r = evutil_inet_pton(AF_INET6, ent->addr, &in6); |
michael@0 | 166 | if (r == 0) { |
michael@0 | 167 | if (ent->status != BAD) |
michael@0 | 168 | TT_FAIL(("%s did not parse, but it's a good address!", |
michael@0 | 169 | ent->addr)); |
michael@0 | 170 | continue; |
michael@0 | 171 | } |
michael@0 | 172 | if (ent->status == BAD) { |
michael@0 | 173 | TT_FAIL(("%s parsed, but we expected an error", ent->addr)); |
michael@0 | 174 | continue; |
michael@0 | 175 | } |
michael@0 | 176 | for (j = 0; j < 4; ++j) { |
michael@0 | 177 | /* Can't use s6_addr32 here; some don't have it. */ |
michael@0 | 178 | ev_uint32_t u = |
michael@0 | 179 | (in6.s6_addr[j*4 ] << 24) | |
michael@0 | 180 | (in6.s6_addr[j*4+1] << 16) | |
michael@0 | 181 | (in6.s6_addr[j*4+2] << 8) | |
michael@0 | 182 | (in6.s6_addr[j*4+3]); |
michael@0 | 183 | if (u != ent->res[j]) { |
michael@0 | 184 | TT_FAIL(("%s did not parse as expected.", ent->addr)); |
michael@0 | 185 | continue; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | if (ent->status == CANONICAL) { |
michael@0 | 189 | const char *w = evutil_inet_ntop(AF_INET6, &in6, written, |
michael@0 | 190 | sizeof(written)); |
michael@0 | 191 | if (!w) { |
michael@0 | 192 | TT_FAIL(("Tried to write out %s; got NULL.", ent->addr)); |
michael@0 | 193 | continue; |
michael@0 | 194 | } |
michael@0 | 195 | if (strcmp(written, ent->addr)) { |
michael@0 | 196 | TT_FAIL(("Tried to write out %s; got %s", ent->addr, written)); |
michael@0 | 197 | continue; |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | } |
michael@0 | 202 | #else |
michael@0 | 203 | TT_BLATHER(("Skipping IPv6 address parsing.")); |
michael@0 | 204 | #endif |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | static struct sa_port_ent { |
michael@0 | 208 | const char *parse; |
michael@0 | 209 | int safamily; |
michael@0 | 210 | const char *addr; |
michael@0 | 211 | int port; |
michael@0 | 212 | } sa_port_ents[] = { |
michael@0 | 213 | { "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 }, |
michael@0 | 214 | { "[ffff::1]", AF_INET6, "ffff::1", 0 }, |
michael@0 | 215 | { "[ffff::1", 0, NULL, 0 }, |
michael@0 | 216 | { "[ffff::1]:65599", 0, NULL, 0 }, |
michael@0 | 217 | { "[ffff::1]:0", 0, NULL, 0 }, |
michael@0 | 218 | { "[ffff::1]:-1", 0, NULL, 0 }, |
michael@0 | 219 | { "::1", AF_INET6, "::1", 0 }, |
michael@0 | 220 | { "1:2::1", AF_INET6, "1:2::1", 0 }, |
michael@0 | 221 | { "192.168.0.1:50", AF_INET, "192.168.0.1", 50 }, |
michael@0 | 222 | { "1.2.3.4", AF_INET, "1.2.3.4", 0 }, |
michael@0 | 223 | { NULL, 0, NULL, 0 }, |
michael@0 | 224 | }; |
michael@0 | 225 | |
michael@0 | 226 | static void |
michael@0 | 227 | regress_sockaddr_port_parse(void *ptr) |
michael@0 | 228 | { |
michael@0 | 229 | struct sockaddr_storage ss; |
michael@0 | 230 | int i, r; |
michael@0 | 231 | |
michael@0 | 232 | for (i = 0; sa_port_ents[i].parse; ++i) { |
michael@0 | 233 | struct sa_port_ent *ent = &sa_port_ents[i]; |
michael@0 | 234 | int len = sizeof(ss); |
michael@0 | 235 | memset(&ss, 0, sizeof(ss)); |
michael@0 | 236 | r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len); |
michael@0 | 237 | if (r < 0) { |
michael@0 | 238 | if (ent->safamily) |
michael@0 | 239 | TT_FAIL(("Couldn't parse %s!", ent->parse)); |
michael@0 | 240 | continue; |
michael@0 | 241 | } else if (! ent->safamily) { |
michael@0 | 242 | TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse)); |
michael@0 | 243 | continue; |
michael@0 | 244 | } |
michael@0 | 245 | if (ent->safamily == AF_INET) { |
michael@0 | 246 | struct sockaddr_in sin; |
michael@0 | 247 | memset(&sin, 0, sizeof(sin)); |
michael@0 | 248 | #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
michael@0 | 249 | sin.sin_len = sizeof(sin); |
michael@0 | 250 | #endif |
michael@0 | 251 | sin.sin_family = AF_INET; |
michael@0 | 252 | sin.sin_port = htons(ent->port); |
michael@0 | 253 | r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr); |
michael@0 | 254 | if (1 != r) { |
michael@0 | 255 | TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr)); |
michael@0 | 256 | } else if (memcmp(&sin, &ss, sizeof(sin))) { |
michael@0 | 257 | TT_FAIL(("Parse for %s was not as expected.", ent->parse)); |
michael@0 | 258 | } else if (len != sizeof(sin)) { |
michael@0 | 259 | TT_FAIL(("Length for %s not as expected.",ent->parse)); |
michael@0 | 260 | } |
michael@0 | 261 | } else { |
michael@0 | 262 | struct sockaddr_in6 sin6; |
michael@0 | 263 | memset(&sin6, 0, sizeof(sin6)); |
michael@0 | 264 | #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN |
michael@0 | 265 | sin6.sin6_len = sizeof(sin6); |
michael@0 | 266 | #endif |
michael@0 | 267 | sin6.sin6_family = AF_INET6; |
michael@0 | 268 | sin6.sin6_port = htons(ent->port); |
michael@0 | 269 | r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr); |
michael@0 | 270 | if (1 != r) { |
michael@0 | 271 | TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr)); |
michael@0 | 272 | } else if (memcmp(&sin6, &ss, sizeof(sin6))) { |
michael@0 | 273 | TT_FAIL(("Parse for %s was not as expected.", ent->parse)); |
michael@0 | 274 | } else if (len != sizeof(sin6)) { |
michael@0 | 275 | TT_FAIL(("Length for %s not as expected.",ent->parse)); |
michael@0 | 276 | } |
michael@0 | 277 | } |
michael@0 | 278 | } |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | |
michael@0 | 282 | static void |
michael@0 | 283 | regress_sockaddr_port_format(void *ptr) |
michael@0 | 284 | { |
michael@0 | 285 | struct sockaddr_storage ss; |
michael@0 | 286 | int len; |
michael@0 | 287 | const char *cp; |
michael@0 | 288 | char cbuf[128]; |
michael@0 | 289 | int r; |
michael@0 | 290 | |
michael@0 | 291 | len = sizeof(ss); |
michael@0 | 292 | r = evutil_parse_sockaddr_port("192.168.1.1:80", |
michael@0 | 293 | (struct sockaddr*)&ss, &len); |
michael@0 | 294 | tt_int_op(r,==,0); |
michael@0 | 295 | cp = evutil_format_sockaddr_port( |
michael@0 | 296 | (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); |
michael@0 | 297 | tt_ptr_op(cp,==,cbuf); |
michael@0 | 298 | tt_str_op(cp,==,"192.168.1.1:80"); |
michael@0 | 299 | |
michael@0 | 300 | len = sizeof(ss); |
michael@0 | 301 | r = evutil_parse_sockaddr_port("[ff00::8010]:999", |
michael@0 | 302 | (struct sockaddr*)&ss, &len); |
michael@0 | 303 | tt_int_op(r,==,0); |
michael@0 | 304 | cp = evutil_format_sockaddr_port( |
michael@0 | 305 | (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); |
michael@0 | 306 | tt_ptr_op(cp,==,cbuf); |
michael@0 | 307 | tt_str_op(cp,==,"[ff00::8010]:999"); |
michael@0 | 308 | |
michael@0 | 309 | ss.ss_family=99; |
michael@0 | 310 | cp = evutil_format_sockaddr_port( |
michael@0 | 311 | (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); |
michael@0 | 312 | tt_ptr_op(cp,==,cbuf); |
michael@0 | 313 | tt_str_op(cp,==,"<addr with socktype 99>"); |
michael@0 | 314 | end: |
michael@0 | 315 | ; |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | static struct sa_pred_ent { |
michael@0 | 319 | const char *parse; |
michael@0 | 320 | |
michael@0 | 321 | int is_loopback; |
michael@0 | 322 | } sa_pred_entries[] = { |
michael@0 | 323 | { "127.0.0.1", 1 }, |
michael@0 | 324 | { "127.0.3.2", 1 }, |
michael@0 | 325 | { "128.1.2.3", 0 }, |
michael@0 | 326 | { "18.0.0.1", 0 }, |
michael@0 | 327 | { "129.168.1.1", 0 }, |
michael@0 | 328 | |
michael@0 | 329 | { "::1", 1 }, |
michael@0 | 330 | { "::0", 0 }, |
michael@0 | 331 | { "f::1", 0 }, |
michael@0 | 332 | { "::501", 0 }, |
michael@0 | 333 | { NULL, 0 }, |
michael@0 | 334 | |
michael@0 | 335 | }; |
michael@0 | 336 | |
michael@0 | 337 | static void |
michael@0 | 338 | test_evutil_sockaddr_predicates(void *ptr) |
michael@0 | 339 | { |
michael@0 | 340 | struct sockaddr_storage ss; |
michael@0 | 341 | int r, i; |
michael@0 | 342 | |
michael@0 | 343 | for (i=0; sa_pred_entries[i].parse; ++i) { |
michael@0 | 344 | struct sa_pred_ent *ent = &sa_pred_entries[i]; |
michael@0 | 345 | int len = sizeof(ss); |
michael@0 | 346 | |
michael@0 | 347 | r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len); |
michael@0 | 348 | |
michael@0 | 349 | if (r<0) { |
michael@0 | 350 | TT_FAIL(("Couldn't parse %s!", ent->parse)); |
michael@0 | 351 | continue; |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | /* sockaddr_is_loopback */ |
michael@0 | 355 | if (ent->is_loopback != evutil_sockaddr_is_loopback((struct sockaddr*)&ss)) { |
michael@0 | 356 | TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected", |
michael@0 | 357 | ent->parse)); |
michael@0 | 358 | } |
michael@0 | 359 | } |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | static void |
michael@0 | 363 | test_evutil_strtoll(void *ptr) |
michael@0 | 364 | { |
michael@0 | 365 | const char *s; |
michael@0 | 366 | char *endptr; |
michael@0 | 367 | |
michael@0 | 368 | tt_want(evutil_strtoll("5000000000", NULL, 10) == |
michael@0 | 369 | ((ev_int64_t)5000000)*1000); |
michael@0 | 370 | tt_want(evutil_strtoll("-5000000000", NULL, 10) == |
michael@0 | 371 | ((ev_int64_t)5000000)*-1000); |
michael@0 | 372 | s = " 99999stuff"; |
michael@0 | 373 | tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999); |
michael@0 | 374 | tt_want(endptr == s+6); |
michael@0 | 375 | tt_want(evutil_strtoll("foo", NULL, 10) == 0); |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | static void |
michael@0 | 379 | test_evutil_snprintf(void *ptr) |
michael@0 | 380 | { |
michael@0 | 381 | char buf[16]; |
michael@0 | 382 | int r; |
michael@0 | 383 | ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200; |
michael@0 | 384 | ev_int64_t i64 = -1 * (ev_int64_t) u64; |
michael@0 | 385 | size_t size = 8000; |
michael@0 | 386 | ev_ssize_t ssize = -9000; |
michael@0 | 387 | |
michael@0 | 388 | r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100); |
michael@0 | 389 | tt_str_op(buf, ==, "50 100"); |
michael@0 | 390 | tt_int_op(r, ==, 6); |
michael@0 | 391 | |
michael@0 | 392 | r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890); |
michael@0 | 393 | tt_str_op(buf, ==, "longish 1234567"); |
michael@0 | 394 | tt_int_op(r, ==, 18); |
michael@0 | 395 | |
michael@0 | 396 | r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64)); |
michael@0 | 397 | tt_str_op(buf, ==, "200000000000"); |
michael@0 | 398 | tt_int_op(r, ==, 12); |
michael@0 | 399 | |
michael@0 | 400 | r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64)); |
michael@0 | 401 | tt_str_op(buf, ==, "-200000000000"); |
michael@0 | 402 | tt_int_op(r, ==, 13); |
michael@0 | 403 | |
michael@0 | 404 | r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT, |
michael@0 | 405 | EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize)); |
michael@0 | 406 | tt_str_op(buf, ==, "8000 -9000"); |
michael@0 | 407 | tt_int_op(r, ==, 10); |
michael@0 | 408 | |
michael@0 | 409 | end: |
michael@0 | 410 | ; |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | static void |
michael@0 | 414 | test_evutil_casecmp(void *ptr) |
michael@0 | 415 | { |
michael@0 | 416 | tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0); |
michael@0 | 417 | tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0); |
michael@0 | 418 | tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0); |
michael@0 | 419 | tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0); |
michael@0 | 420 | tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0); |
michael@0 | 421 | |
michael@0 | 422 | tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0); |
michael@0 | 423 | tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0); |
michael@0 | 424 | tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0); |
michael@0 | 425 | tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0); |
michael@0 | 426 | tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0); |
michael@0 | 427 | tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0); |
michael@0 | 428 | tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0); |
michael@0 | 429 | tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0); |
michael@0 | 430 | end: |
michael@0 | 431 | ; |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | static int logsev = 0; |
michael@0 | 435 | static char *logmsg = NULL; |
michael@0 | 436 | |
michael@0 | 437 | static void |
michael@0 | 438 | logfn(int severity, const char *msg) |
michael@0 | 439 | { |
michael@0 | 440 | logsev = severity; |
michael@0 | 441 | tt_want(msg); |
michael@0 | 442 | if (msg) { |
michael@0 | 443 | if (logmsg) |
michael@0 | 444 | free(logmsg); |
michael@0 | 445 | logmsg = strdup(msg); |
michael@0 | 446 | } |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | static int fatal_want_severity = 0; |
michael@0 | 450 | static const char *fatal_want_message = NULL; |
michael@0 | 451 | static void |
michael@0 | 452 | fatalfn(int exitcode) |
michael@0 | 453 | { |
michael@0 | 454 | if (logsev != fatal_want_severity || |
michael@0 | 455 | !logmsg || |
michael@0 | 456 | strcmp(logmsg, fatal_want_message)) |
michael@0 | 457 | exit(0); |
michael@0 | 458 | else |
michael@0 | 459 | exit(exitcode); |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | #ifndef WIN32 |
michael@0 | 463 | #define CAN_CHECK_ERR |
michael@0 | 464 | static void |
michael@0 | 465 | check_error_logging(void (*fn)(void), int wantexitcode, |
michael@0 | 466 | int wantseverity, const char *wantmsg) |
michael@0 | 467 | { |
michael@0 | 468 | pid_t pid; |
michael@0 | 469 | int status = 0, exitcode; |
michael@0 | 470 | fatal_want_severity = wantseverity; |
michael@0 | 471 | fatal_want_message = wantmsg; |
michael@0 | 472 | if ((pid = regress_fork()) == 0) { |
michael@0 | 473 | /* child process */ |
michael@0 | 474 | fn(); |
michael@0 | 475 | exit(0); /* should be unreachable. */ |
michael@0 | 476 | } else { |
michael@0 | 477 | wait(&status); |
michael@0 | 478 | exitcode = WEXITSTATUS(status); |
michael@0 | 479 | tt_int_op(wantexitcode, ==, exitcode); |
michael@0 | 480 | } |
michael@0 | 481 | end: |
michael@0 | 482 | ; |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | static void |
michael@0 | 486 | errx_fn(void) |
michael@0 | 487 | { |
michael@0 | 488 | event_errx(2, "Fatal error; too many kumquats (%d)", 5); |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | static void |
michael@0 | 492 | err_fn(void) |
michael@0 | 493 | { |
michael@0 | 494 | errno = ENOENT; |
michael@0 | 495 | event_err(5,"Couldn't open %s", "/very/bad/file"); |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | static void |
michael@0 | 499 | sock_err_fn(void) |
michael@0 | 500 | { |
michael@0 | 501 | evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0); |
michael@0 | 502 | #ifdef WIN32 |
michael@0 | 503 | EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); |
michael@0 | 504 | #else |
michael@0 | 505 | errno = EAGAIN; |
michael@0 | 506 | #endif |
michael@0 | 507 | event_sock_err(20, fd, "Unhappy socket"); |
michael@0 | 508 | } |
michael@0 | 509 | #endif |
michael@0 | 510 | |
michael@0 | 511 | static void |
michael@0 | 512 | test_evutil_log(void *ptr) |
michael@0 | 513 | { |
michael@0 | 514 | evutil_socket_t fd = -1; |
michael@0 | 515 | char buf[128]; |
michael@0 | 516 | |
michael@0 | 517 | event_set_log_callback(logfn); |
michael@0 | 518 | event_set_fatal_callback(fatalfn); |
michael@0 | 519 | #define RESET() do { \ |
michael@0 | 520 | logsev = 0; \ |
michael@0 | 521 | if (logmsg) free(logmsg); \ |
michael@0 | 522 | logmsg = NULL; \ |
michael@0 | 523 | } while (0) |
michael@0 | 524 | #define LOGEQ(sev,msg) do { \ |
michael@0 | 525 | tt_int_op(logsev,==,sev); \ |
michael@0 | 526 | tt_assert(logmsg != NULL); \ |
michael@0 | 527 | tt_str_op(logmsg,==,msg); \ |
michael@0 | 528 | } while (0) |
michael@0 | 529 | |
michael@0 | 530 | #ifdef CAN_CHECK_ERR |
michael@0 | 531 | /* We need to disable these tests for now. Previously, the logging |
michael@0 | 532 | * module didn't enforce the requirement that a fatal callback |
michael@0 | 533 | * actually exit. Now, it exits no matter what, so if we wan to |
michael@0 | 534 | * reinstate these tests, we'll need to fork for each one. */ |
michael@0 | 535 | check_error_logging(errx_fn, 2, _EVENT_LOG_ERR, |
michael@0 | 536 | "Fatal error; too many kumquats (5)"); |
michael@0 | 537 | RESET(); |
michael@0 | 538 | #endif |
michael@0 | 539 | |
michael@0 | 540 | event_warnx("Far too many %s (%d)", "wombats", 99); |
michael@0 | 541 | LOGEQ(_EVENT_LOG_WARN, "Far too many wombats (99)"); |
michael@0 | 542 | RESET(); |
michael@0 | 543 | |
michael@0 | 544 | event_msgx("Connecting lime to coconut"); |
michael@0 | 545 | LOGEQ(_EVENT_LOG_MSG, "Connecting lime to coconut"); |
michael@0 | 546 | RESET(); |
michael@0 | 547 | |
michael@0 | 548 | event_debug(("A millisecond passed! We should log that!")); |
michael@0 | 549 | #ifdef USE_DEBUG |
michael@0 | 550 | LOGEQ(_EVENT_LOG_DEBUG, "A millisecond passed! We should log that!"); |
michael@0 | 551 | #else |
michael@0 | 552 | tt_int_op(logsev,==,0); |
michael@0 | 553 | tt_ptr_op(logmsg,==,NULL); |
michael@0 | 554 | #endif |
michael@0 | 555 | RESET(); |
michael@0 | 556 | |
michael@0 | 557 | /* Try with an errno. */ |
michael@0 | 558 | errno = ENOENT; |
michael@0 | 559 | event_warn("Couldn't open %s", "/bad/file"); |
michael@0 | 560 | evutil_snprintf(buf, sizeof(buf), |
michael@0 | 561 | "Couldn't open /bad/file: %s",strerror(ENOENT)); |
michael@0 | 562 | LOGEQ(_EVENT_LOG_WARN,buf); |
michael@0 | 563 | RESET(); |
michael@0 | 564 | |
michael@0 | 565 | #ifdef CAN_CHECK_ERR |
michael@0 | 566 | evutil_snprintf(buf, sizeof(buf), |
michael@0 | 567 | "Couldn't open /very/bad/file: %s",strerror(ENOENT)); |
michael@0 | 568 | check_error_logging(err_fn, 5, _EVENT_LOG_ERR, buf); |
michael@0 | 569 | RESET(); |
michael@0 | 570 | #endif |
michael@0 | 571 | |
michael@0 | 572 | /* Try with a socket errno. */ |
michael@0 | 573 | fd = socket(AF_INET, SOCK_STREAM, 0); |
michael@0 | 574 | #ifdef WIN32 |
michael@0 | 575 | evutil_snprintf(buf, sizeof(buf), |
michael@0 | 576 | "Unhappy socket: %s", |
michael@0 | 577 | evutil_socket_error_to_string(WSAEWOULDBLOCK)); |
michael@0 | 578 | EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); |
michael@0 | 579 | #else |
michael@0 | 580 | evutil_snprintf(buf, sizeof(buf), |
michael@0 | 581 | "Unhappy socket: %s", strerror(EAGAIN)); |
michael@0 | 582 | errno = EAGAIN; |
michael@0 | 583 | #endif |
michael@0 | 584 | event_sock_warn(fd, "Unhappy socket"); |
michael@0 | 585 | LOGEQ(_EVENT_LOG_WARN, buf); |
michael@0 | 586 | RESET(); |
michael@0 | 587 | |
michael@0 | 588 | #ifdef CAN_CHECK_ERR |
michael@0 | 589 | check_error_logging(sock_err_fn, 20, _EVENT_LOG_ERR, buf); |
michael@0 | 590 | RESET(); |
michael@0 | 591 | #endif |
michael@0 | 592 | |
michael@0 | 593 | #undef RESET |
michael@0 | 594 | #undef LOGEQ |
michael@0 | 595 | end: |
michael@0 | 596 | if (logmsg) |
michael@0 | 597 | free(logmsg); |
michael@0 | 598 | if (fd >= 0) |
michael@0 | 599 | evutil_closesocket(fd); |
michael@0 | 600 | } |
michael@0 | 601 | |
michael@0 | 602 | static void |
michael@0 | 603 | test_evutil_strlcpy(void *arg) |
michael@0 | 604 | { |
michael@0 | 605 | char buf[8]; |
michael@0 | 606 | |
michael@0 | 607 | /* Successful case. */ |
michael@0 | 608 | tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf))); |
michael@0 | 609 | tt_str_op(buf, ==, "Hello"); |
michael@0 | 610 | |
michael@0 | 611 | /* Overflow by a lot. */ |
michael@0 | 612 | tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf))); |
michael@0 | 613 | tt_str_op(buf, ==, "pentasy"); |
michael@0 | 614 | |
michael@0 | 615 | /* Overflow by exactly one. */ |
michael@0 | 616 | tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf))); |
michael@0 | 617 | tt_str_op(buf, ==, "overlon"); |
michael@0 | 618 | end: |
michael@0 | 619 | ; |
michael@0 | 620 | } |
michael@0 | 621 | |
michael@0 | 622 | struct example_struct { |
michael@0 | 623 | const char *a; |
michael@0 | 624 | const char *b; |
michael@0 | 625 | long c; |
michael@0 | 626 | }; |
michael@0 | 627 | |
michael@0 | 628 | static void |
michael@0 | 629 | test_evutil_upcast(void *arg) |
michael@0 | 630 | { |
michael@0 | 631 | struct example_struct es1; |
michael@0 | 632 | const char **cp; |
michael@0 | 633 | es1.a = "World"; |
michael@0 | 634 | es1.b = "Hello"; |
michael@0 | 635 | es1.c = -99; |
michael@0 | 636 | |
michael@0 | 637 | tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*)); |
michael@0 | 638 | |
michael@0 | 639 | cp = &es1.b; |
michael@0 | 640 | tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1); |
michael@0 | 641 | |
michael@0 | 642 | end: |
michael@0 | 643 | ; |
michael@0 | 644 | } |
michael@0 | 645 | |
michael@0 | 646 | static void |
michael@0 | 647 | test_evutil_integers(void *arg) |
michael@0 | 648 | { |
michael@0 | 649 | ev_int64_t i64; |
michael@0 | 650 | ev_uint64_t u64; |
michael@0 | 651 | ev_int32_t i32; |
michael@0 | 652 | ev_uint32_t u32; |
michael@0 | 653 | ev_int16_t i16; |
michael@0 | 654 | ev_uint16_t u16; |
michael@0 | 655 | ev_int8_t i8; |
michael@0 | 656 | ev_uint8_t u8; |
michael@0 | 657 | |
michael@0 | 658 | void *ptr; |
michael@0 | 659 | ev_intptr_t iptr; |
michael@0 | 660 | ev_uintptr_t uptr; |
michael@0 | 661 | |
michael@0 | 662 | ev_ssize_t ssize; |
michael@0 | 663 | |
michael@0 | 664 | tt_int_op(sizeof(u64), ==, 8); |
michael@0 | 665 | tt_int_op(sizeof(i64), ==, 8); |
michael@0 | 666 | tt_int_op(sizeof(u32), ==, 4); |
michael@0 | 667 | tt_int_op(sizeof(i32), ==, 4); |
michael@0 | 668 | tt_int_op(sizeof(u16), ==, 2); |
michael@0 | 669 | tt_int_op(sizeof(i16), ==, 2); |
michael@0 | 670 | tt_int_op(sizeof(u8), ==, 1); |
michael@0 | 671 | tt_int_op(sizeof(i8), ==, 1); |
michael@0 | 672 | |
michael@0 | 673 | tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t)); |
michael@0 | 674 | tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *)); |
michael@0 | 675 | tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t)); |
michael@0 | 676 | |
michael@0 | 677 | u64 = 1000000000; |
michael@0 | 678 | u64 *= 1000000000; |
michael@0 | 679 | tt_assert(u64 / 1000000000 == 1000000000); |
michael@0 | 680 | i64 = -1000000000; |
michael@0 | 681 | i64 *= 1000000000; |
michael@0 | 682 | tt_assert(i64 / 1000000000 == -1000000000); |
michael@0 | 683 | |
michael@0 | 684 | u64 = EV_UINT64_MAX; |
michael@0 | 685 | i64 = EV_INT64_MAX; |
michael@0 | 686 | tt_assert(u64 > 0); |
michael@0 | 687 | tt_assert(i64 > 0); |
michael@0 | 688 | u64++; |
michael@0 | 689 | i64++; |
michael@0 | 690 | tt_assert(u64 == 0); |
michael@0 | 691 | tt_assert(i64 == EV_INT64_MIN); |
michael@0 | 692 | tt_assert(i64 < 0); |
michael@0 | 693 | |
michael@0 | 694 | u32 = EV_UINT32_MAX; |
michael@0 | 695 | i32 = EV_INT32_MAX; |
michael@0 | 696 | tt_assert(u32 > 0); |
michael@0 | 697 | tt_assert(i32 > 0); |
michael@0 | 698 | u32++; |
michael@0 | 699 | i32++; |
michael@0 | 700 | tt_assert(u32 == 0); |
michael@0 | 701 | tt_assert(i32 == EV_INT32_MIN); |
michael@0 | 702 | tt_assert(i32 < 0); |
michael@0 | 703 | |
michael@0 | 704 | u16 = EV_UINT16_MAX; |
michael@0 | 705 | i16 = EV_INT16_MAX; |
michael@0 | 706 | tt_assert(u16 > 0); |
michael@0 | 707 | tt_assert(i16 > 0); |
michael@0 | 708 | u16++; |
michael@0 | 709 | i16++; |
michael@0 | 710 | tt_assert(u16 == 0); |
michael@0 | 711 | tt_assert(i16 == EV_INT16_MIN); |
michael@0 | 712 | tt_assert(i16 < 0); |
michael@0 | 713 | |
michael@0 | 714 | u8 = EV_UINT8_MAX; |
michael@0 | 715 | i8 = EV_INT8_MAX; |
michael@0 | 716 | tt_assert(u8 > 0); |
michael@0 | 717 | tt_assert(i8 > 0); |
michael@0 | 718 | u8++; |
michael@0 | 719 | i8++; |
michael@0 | 720 | tt_assert(u8 == 0); |
michael@0 | 721 | tt_assert(i8 == EV_INT8_MIN); |
michael@0 | 722 | tt_assert(i8 < 0); |
michael@0 | 723 | |
michael@0 | 724 | ssize = EV_SSIZE_MAX; |
michael@0 | 725 | tt_assert(ssize > 0); |
michael@0 | 726 | ssize++; |
michael@0 | 727 | tt_assert(ssize < 0); |
michael@0 | 728 | tt_assert(ssize == EV_SSIZE_MIN); |
michael@0 | 729 | |
michael@0 | 730 | ptr = &ssize; |
michael@0 | 731 | iptr = (ev_intptr_t)ptr; |
michael@0 | 732 | uptr = (ev_uintptr_t)ptr; |
michael@0 | 733 | ptr = (void *)iptr; |
michael@0 | 734 | tt_assert(ptr == &ssize); |
michael@0 | 735 | ptr = (void *)uptr; |
michael@0 | 736 | tt_assert(ptr == &ssize); |
michael@0 | 737 | |
michael@0 | 738 | iptr = -1; |
michael@0 | 739 | tt_assert(iptr < 0); |
michael@0 | 740 | end: |
michael@0 | 741 | ; |
michael@0 | 742 | } |
michael@0 | 743 | |
michael@0 | 744 | struct evutil_addrinfo * |
michael@0 | 745 | ai_find_by_family(struct evutil_addrinfo *ai, int family) |
michael@0 | 746 | { |
michael@0 | 747 | while (ai) { |
michael@0 | 748 | if (ai->ai_family == family) |
michael@0 | 749 | return ai; |
michael@0 | 750 | ai = ai->ai_next; |
michael@0 | 751 | } |
michael@0 | 752 | return NULL; |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | struct evutil_addrinfo * |
michael@0 | 756 | ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol) |
michael@0 | 757 | { |
michael@0 | 758 | while (ai) { |
michael@0 | 759 | if (ai->ai_protocol == protocol) |
michael@0 | 760 | return ai; |
michael@0 | 761 | ai = ai->ai_next; |
michael@0 | 762 | } |
michael@0 | 763 | return NULL; |
michael@0 | 764 | } |
michael@0 | 765 | |
michael@0 | 766 | |
michael@0 | 767 | int |
michael@0 | 768 | _test_ai_eq(const struct evutil_addrinfo *ai, const char *sockaddr_port, |
michael@0 | 769 | int socktype, int protocol, int line) |
michael@0 | 770 | { |
michael@0 | 771 | struct sockaddr_storage ss; |
michael@0 | 772 | int slen = sizeof(ss); |
michael@0 | 773 | int gotport; |
michael@0 | 774 | char buf[128]; |
michael@0 | 775 | memset(&ss, 0, sizeof(ss)); |
michael@0 | 776 | if (socktype > 0) |
michael@0 | 777 | tt_int_op(ai->ai_socktype, ==, socktype); |
michael@0 | 778 | if (protocol > 0) |
michael@0 | 779 | tt_int_op(ai->ai_protocol, ==, protocol); |
michael@0 | 780 | |
michael@0 | 781 | if (evutil_parse_sockaddr_port( |
michael@0 | 782 | sockaddr_port, (struct sockaddr*)&ss, &slen)<0) { |
michael@0 | 783 | TT_FAIL(("Couldn't parse expected address %s on line %d", |
michael@0 | 784 | sockaddr_port, line)); |
michael@0 | 785 | return -1; |
michael@0 | 786 | } |
michael@0 | 787 | if (ai->ai_family != ss.ss_family) { |
michael@0 | 788 | TT_FAIL(("Address family %d did not match %d on line %d", |
michael@0 | 789 | ai->ai_family, ss.ss_family, line)); |
michael@0 | 790 | return -1; |
michael@0 | 791 | } |
michael@0 | 792 | if (ai->ai_addr->sa_family == AF_INET) { |
michael@0 | 793 | struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr; |
michael@0 | 794 | evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)); |
michael@0 | 795 | gotport = ntohs(sin->sin_port); |
michael@0 | 796 | if (ai->ai_addrlen != sizeof(struct sockaddr_in)) { |
michael@0 | 797 | TT_FAIL(("Addr size mismatch on line %d", line)); |
michael@0 | 798 | return -1; |
michael@0 | 799 | } |
michael@0 | 800 | } else { |
michael@0 | 801 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr; |
michael@0 | 802 | evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf)); |
michael@0 | 803 | gotport = ntohs(sin6->sin6_port); |
michael@0 | 804 | if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) { |
michael@0 | 805 | TT_FAIL(("Addr size mismatch on line %d", line)); |
michael@0 | 806 | return -1; |
michael@0 | 807 | } |
michael@0 | 808 | } |
michael@0 | 809 | if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) { |
michael@0 | 810 | TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port, |
michael@0 | 811 | buf, gotport, line)); |
michael@0 | 812 | return -1; |
michael@0 | 813 | } else { |
michael@0 | 814 | TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port, |
michael@0 | 815 | buf, gotport, line)); |
michael@0 | 816 | } |
michael@0 | 817 | return 0; |
michael@0 | 818 | end: |
michael@0 | 819 | TT_FAIL(("Test failed on line %d", line)); |
michael@0 | 820 | return -1; |
michael@0 | 821 | } |
michael@0 | 822 | |
michael@0 | 823 | static void |
michael@0 | 824 | test_evutil_rand(void *arg) |
michael@0 | 825 | { |
michael@0 | 826 | char buf1[32]; |
michael@0 | 827 | char buf2[32]; |
michael@0 | 828 | int counts[256]; |
michael@0 | 829 | int i, j, k, n=0; |
michael@0 | 830 | |
michael@0 | 831 | memset(buf2, 0, sizeof(buf2)); |
michael@0 | 832 | memset(counts, 0, sizeof(counts)); |
michael@0 | 833 | |
michael@0 | 834 | for (k=0;k<32;++k) { |
michael@0 | 835 | /* Try a few different start and end points; try to catch |
michael@0 | 836 | * the various misaligned cases of arc4random_buf */ |
michael@0 | 837 | int startpoint = _evutil_weakrand() % 4; |
michael@0 | 838 | int endpoint = 32 - (_evutil_weakrand() % 4); |
michael@0 | 839 | |
michael@0 | 840 | memset(buf2, 0, sizeof(buf2)); |
michael@0 | 841 | |
michael@0 | 842 | /* Do 6 runs over buf1, or-ing the result into buf2 each |
michael@0 | 843 | * time, to make sure we're setting each byte that we mean |
michael@0 | 844 | * to set. */ |
michael@0 | 845 | for (i=0;i<8;++i) { |
michael@0 | 846 | memset(buf1, 0, sizeof(buf1)); |
michael@0 | 847 | evutil_secure_rng_get_bytes(buf1 + startpoint, |
michael@0 | 848 | endpoint-startpoint); |
michael@0 | 849 | n += endpoint - startpoint; |
michael@0 | 850 | for (j=0; j<32; ++j) { |
michael@0 | 851 | if (j >= startpoint && j < endpoint) { |
michael@0 | 852 | buf2[j] |= buf1[j]; |
michael@0 | 853 | ++counts[(unsigned char)buf1[j]]; |
michael@0 | 854 | } else { |
michael@0 | 855 | tt_assert(buf1[j] == 0); |
michael@0 | 856 | tt_int_op(buf1[j], ==, 0); |
michael@0 | 857 | |
michael@0 | 858 | } |
michael@0 | 859 | } |
michael@0 | 860 | } |
michael@0 | 861 | |
michael@0 | 862 | /* This will give a false positive with P=(256**8)==(2**64) |
michael@0 | 863 | * for each character. */ |
michael@0 | 864 | for (j=startpoint;j<endpoint;++j) { |
michael@0 | 865 | tt_int_op(buf2[j], !=, 0); |
michael@0 | 866 | } |
michael@0 | 867 | } |
michael@0 | 868 | |
michael@0 | 869 | /* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */ |
michael@0 | 870 | end: |
michael@0 | 871 | ; |
michael@0 | 872 | } |
michael@0 | 873 | |
michael@0 | 874 | static void |
michael@0 | 875 | test_evutil_getaddrinfo(void *arg) |
michael@0 | 876 | { |
michael@0 | 877 | struct evutil_addrinfo *ai = NULL, *a; |
michael@0 | 878 | struct evutil_addrinfo hints; |
michael@0 | 879 | |
michael@0 | 880 | struct sockaddr_in6 *sin6; |
michael@0 | 881 | struct sockaddr_in *sin; |
michael@0 | 882 | char buf[128]; |
michael@0 | 883 | const char *cp; |
michael@0 | 884 | int r; |
michael@0 | 885 | |
michael@0 | 886 | /* Try using it as a pton. */ |
michael@0 | 887 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 888 | hints.ai_family = PF_UNSPEC; |
michael@0 | 889 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 890 | r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai); |
michael@0 | 891 | tt_int_op(r, ==, 0); |
michael@0 | 892 | tt_assert(ai); |
michael@0 | 893 | tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */ |
michael@0 | 894 | test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 895 | evutil_freeaddrinfo(ai); |
michael@0 | 896 | ai = NULL; |
michael@0 | 897 | |
michael@0 | 898 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 899 | hints.ai_family = PF_UNSPEC; |
michael@0 | 900 | hints.ai_protocol = IPPROTO_UDP; |
michael@0 | 901 | r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai); |
michael@0 | 902 | tt_int_op(r, ==, 0); |
michael@0 | 903 | tt_assert(ai); |
michael@0 | 904 | tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */ |
michael@0 | 905 | test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP); |
michael@0 | 906 | evutil_freeaddrinfo(ai); |
michael@0 | 907 | ai = NULL; |
michael@0 | 908 | |
michael@0 | 909 | /* Try out the behavior of nodename=NULL */ |
michael@0 | 910 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 911 | hints.ai_family = PF_INET; |
michael@0 | 912 | hints.ai_protocol = IPPROTO_TCP; |
michael@0 | 913 | hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */ |
michael@0 | 914 | r = evutil_getaddrinfo(NULL, "9999", &hints, &ai); |
michael@0 | 915 | tt_int_op(r,==,0); |
michael@0 | 916 | tt_assert(ai); |
michael@0 | 917 | tt_ptr_op(ai->ai_next, ==, NULL); |
michael@0 | 918 | test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 919 | evutil_freeaddrinfo(ai); |
michael@0 | 920 | ai = NULL; |
michael@0 | 921 | hints.ai_flags = 0; /* as if for connect */ |
michael@0 | 922 | r = evutil_getaddrinfo(NULL, "9998", &hints, &ai); |
michael@0 | 923 | tt_assert(ai); |
michael@0 | 924 | tt_int_op(r,==,0); |
michael@0 | 925 | test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 926 | tt_ptr_op(ai->ai_next, ==, NULL); |
michael@0 | 927 | evutil_freeaddrinfo(ai); |
michael@0 | 928 | ai = NULL; |
michael@0 | 929 | |
michael@0 | 930 | hints.ai_flags = 0; /* as if for connect */ |
michael@0 | 931 | hints.ai_family = PF_INET6; |
michael@0 | 932 | r = evutil_getaddrinfo(NULL, "9997", &hints, &ai); |
michael@0 | 933 | tt_assert(ai); |
michael@0 | 934 | tt_int_op(r,==,0); |
michael@0 | 935 | tt_ptr_op(ai->ai_next, ==, NULL); |
michael@0 | 936 | test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 937 | evutil_freeaddrinfo(ai); |
michael@0 | 938 | ai = NULL; |
michael@0 | 939 | |
michael@0 | 940 | hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */ |
michael@0 | 941 | hints.ai_family = PF_INET6; |
michael@0 | 942 | r = evutil_getaddrinfo(NULL, "9996", &hints, &ai); |
michael@0 | 943 | tt_assert(ai); |
michael@0 | 944 | tt_int_op(r,==,0); |
michael@0 | 945 | tt_ptr_op(ai->ai_next, ==, NULL); |
michael@0 | 946 | test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 947 | evutil_freeaddrinfo(ai); |
michael@0 | 948 | ai = NULL; |
michael@0 | 949 | |
michael@0 | 950 | /* Now try an unspec one. We should get a v6 and a v4. */ |
michael@0 | 951 | hints.ai_family = PF_UNSPEC; |
michael@0 | 952 | r = evutil_getaddrinfo(NULL, "9996", &hints, &ai); |
michael@0 | 953 | tt_assert(ai); |
michael@0 | 954 | tt_int_op(r,==,0); |
michael@0 | 955 | a = ai_find_by_family(ai, PF_INET6); |
michael@0 | 956 | tt_assert(a); |
michael@0 | 957 | test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 958 | a = ai_find_by_family(ai, PF_INET); |
michael@0 | 959 | tt_assert(a); |
michael@0 | 960 | test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 961 | evutil_freeaddrinfo(ai); |
michael@0 | 962 | ai = NULL; |
michael@0 | 963 | |
michael@0 | 964 | /* Try out AI_NUMERICHOST: successful case. Also try |
michael@0 | 965 | * multiprotocol. */ |
michael@0 | 966 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 967 | hints.ai_family = PF_UNSPEC; |
michael@0 | 968 | hints.ai_flags = EVUTIL_AI_NUMERICHOST; |
michael@0 | 969 | r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai); |
michael@0 | 970 | tt_int_op(r, ==, 0); |
michael@0 | 971 | a = ai_find_by_protocol(ai, IPPROTO_TCP); |
michael@0 | 972 | tt_assert(a); |
michael@0 | 973 | test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 974 | a = ai_find_by_protocol(ai, IPPROTO_UDP); |
michael@0 | 975 | tt_assert(a); |
michael@0 | 976 | test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP); |
michael@0 | 977 | evutil_freeaddrinfo(ai); |
michael@0 | 978 | ai = NULL; |
michael@0 | 979 | |
michael@0 | 980 | /* Try the failing case of AI_NUMERICHOST */ |
michael@0 | 981 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 982 | hints.ai_family = PF_UNSPEC; |
michael@0 | 983 | hints.ai_flags = EVUTIL_AI_NUMERICHOST; |
michael@0 | 984 | r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai); |
michael@0 | 985 | tt_int_op(r, ==, EVUTIL_EAI_NONAME); |
michael@0 | 986 | tt_ptr_op(ai, ==, NULL); |
michael@0 | 987 | |
michael@0 | 988 | /* Try symbolic service names wit AI_NUMERICSERV */ |
michael@0 | 989 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 990 | hints.ai_family = PF_UNSPEC; |
michael@0 | 991 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 992 | hints.ai_flags = EVUTIL_AI_NUMERICSERV; |
michael@0 | 993 | r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai); |
michael@0 | 994 | tt_int_op(r,==,EVUTIL_EAI_NONAME); |
michael@0 | 995 | |
michael@0 | 996 | /* Try symbolic service names */ |
michael@0 | 997 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 998 | hints.ai_family = PF_UNSPEC; |
michael@0 | 999 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1000 | r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai); |
michael@0 | 1001 | if (r!=0) { |
michael@0 | 1002 | TT_DECLARE("SKIP", ("Symbolic service names seem broken.")); |
michael@0 | 1003 | } else { |
michael@0 | 1004 | tt_assert(ai); |
michael@0 | 1005 | test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP); |
michael@0 | 1006 | evutil_freeaddrinfo(ai); |
michael@0 | 1007 | ai = NULL; |
michael@0 | 1008 | } |
michael@0 | 1009 | |
michael@0 | 1010 | /* Now do some actual lookups. */ |
michael@0 | 1011 | memset(&hints, 0, sizeof(hints)); |
michael@0 | 1012 | hints.ai_family = PF_INET; |
michael@0 | 1013 | hints.ai_protocol = IPPROTO_TCP; |
michael@0 | 1014 | hints.ai_socktype = SOCK_STREAM; |
michael@0 | 1015 | r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai); |
michael@0 | 1016 | if (r != 0) { |
michael@0 | 1017 | TT_DECLARE("SKIP", ("Couldn't resolve www.google.com")); |
michael@0 | 1018 | } else { |
michael@0 | 1019 | tt_assert(ai); |
michael@0 | 1020 | tt_int_op(ai->ai_family, ==, PF_INET); |
michael@0 | 1021 | tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP); |
michael@0 | 1022 | tt_int_op(ai->ai_socktype, ==, SOCK_STREAM); |
michael@0 | 1023 | tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in)); |
michael@0 | 1024 | sin = (struct sockaddr_in*)ai->ai_addr; |
michael@0 | 1025 | tt_int_op(sin->sin_family, ==, AF_INET); |
michael@0 | 1026 | tt_int_op(sin->sin_port, ==, htons(80)); |
michael@0 | 1027 | tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff); |
michael@0 | 1028 | |
michael@0 | 1029 | cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)); |
michael@0 | 1030 | TT_BLATHER(("www.google.com resolved to %s", |
michael@0 | 1031 | cp?cp:"<unwriteable>")); |
michael@0 | 1032 | evutil_freeaddrinfo(ai); |
michael@0 | 1033 | ai = NULL; |
michael@0 | 1034 | } |
michael@0 | 1035 | |
michael@0 | 1036 | hints.ai_family = PF_INET6; |
michael@0 | 1037 | r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai); |
michael@0 | 1038 | if (r != 0) { |
michael@0 | 1039 | TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com")); |
michael@0 | 1040 | } else { |
michael@0 | 1041 | tt_assert(ai); |
michael@0 | 1042 | tt_int_op(ai->ai_family, ==, PF_INET6); |
michael@0 | 1043 | tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6)); |
michael@0 | 1044 | sin6 = (struct sockaddr_in6*)ai->ai_addr; |
michael@0 | 1045 | tt_int_op(sin6->sin6_port, ==, htons(80)); |
michael@0 | 1046 | |
michael@0 | 1047 | cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, |
michael@0 | 1048 | sizeof(buf)); |
michael@0 | 1049 | TT_BLATHER(("ipv6.google.com resolved to %s", |
michael@0 | 1050 | cp?cp:"<unwriteable>")); |
michael@0 | 1051 | } |
michael@0 | 1052 | |
michael@0 | 1053 | end: |
michael@0 | 1054 | if (ai) |
michael@0 | 1055 | evutil_freeaddrinfo(ai); |
michael@0 | 1056 | } |
michael@0 | 1057 | |
michael@0 | 1058 | #ifdef WIN32 |
michael@0 | 1059 | static void |
michael@0 | 1060 | test_evutil_loadsyslib(void *arg) |
michael@0 | 1061 | { |
michael@0 | 1062 | HANDLE h=NULL; |
michael@0 | 1063 | |
michael@0 | 1064 | h = evutil_load_windows_system_library(TEXT("kernel32.dll")); |
michael@0 | 1065 | tt_assert(h); |
michael@0 | 1066 | |
michael@0 | 1067 | end: |
michael@0 | 1068 | if (h) |
michael@0 | 1069 | CloseHandle(h); |
michael@0 | 1070 | |
michael@0 | 1071 | } |
michael@0 | 1072 | #endif |
michael@0 | 1073 | |
michael@0 | 1074 | struct testcase_t util_testcases[] = { |
michael@0 | 1075 | { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, |
michael@0 | 1076 | { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, |
michael@0 | 1077 | { "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL }, |
michael@0 | 1078 | { "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL }, |
michael@0 | 1079 | { "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL }, |
michael@0 | 1080 | { "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL }, |
michael@0 | 1081 | { "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL }, |
michael@0 | 1082 | { "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL }, |
michael@0 | 1083 | { "strlcpy", test_evutil_strlcpy, 0, NULL, NULL }, |
michael@0 | 1084 | { "log", test_evutil_log, TT_FORK, NULL, NULL }, |
michael@0 | 1085 | { "upcast", test_evutil_upcast, 0, NULL, NULL }, |
michael@0 | 1086 | { "integers", test_evutil_integers, 0, NULL, NULL }, |
michael@0 | 1087 | { "rand", test_evutil_rand, TT_FORK, NULL, NULL }, |
michael@0 | 1088 | { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL }, |
michael@0 | 1089 | #ifdef WIN32 |
michael@0 | 1090 | { "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL }, |
michael@0 | 1091 | #endif |
michael@0 | 1092 | END_OF_TESTCASES, |
michael@0 | 1093 | }; |
michael@0 | 1094 |