1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "testutil.h"
#include "testsock.h"
#include "apr_thread_proc.h"
#include "apr_network_io.h"
#include "apr_errno.h"
#include "apr_general.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_poll.h"
#define APR_WANT_BYTEFUNC
#include "apr_want.h"
#define UNIX_SOCKET_NAME "/tmp/apr-socket"
#define IPV4_SOCKET_NAME "127.0.0.1"
static char *socket_name = NULL;
static int socket_type = APR_INET;
static void launch_child(abts_case *tc, apr_proc_t *proc, const char *arg1, apr_pool_t *p)
{
apr_procattr_t *procattr;
const char *args[4];
apr_status_t rv;
rv = apr_procattr_create(&procattr, p);
APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);
rv = apr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE,
APR_NO_PIPE);
APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);
rv = apr_procattr_error_check_set(procattr, 1);
APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);
rv = apr_procattr_cmdtype_set(procattr, APR_PROGRAM_ENV);
APR_ASSERT_SUCCESS(tc, "Couldn't set copy environment", rv);
args[0] = "sockchild" EXTENSION;
args[1] = arg1;
args[2] = socket_name;
args[3] = NULL;
rv = apr_proc_create(proc, TESTBINPATH "sockchild" EXTENSION, args, NULL,
procattr, p);
APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
}
static int wait_child(abts_case *tc, apr_proc_t *proc)
{
int exitcode;
apr_exit_why_e why;
ABTS_ASSERT(tc, "Error waiting for child process",
apr_proc_wait(proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE);
ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
return exitcode;
}
static void test_addr_info(abts_case *tc, void *data)
{
apr_status_t rv;
apr_sockaddr_t *sa;
int rc;
rv = apr_sockaddr_info_get(&sa, NULL, APR_UNSPEC, 80, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
rc = apr_sockaddr_is_wildcard(sa);
ABTS_INT_NEQUAL(tc, 0, rc);
rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_UNSPEC, 80, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
ABTS_STR_EQUAL(tc, "127.0.0.1", sa->hostname);
rc = apr_sockaddr_is_wildcard(sa);
ABTS_INT_EQUAL(tc, 0, rc);
rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_UNSPEC, 0, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
ABTS_STR_EQUAL(tc, "127.0.0.1", sa->hostname);
ABTS_INT_EQUAL(tc, 0, sa->port);
ABTS_INT_EQUAL(tc, 0, ntohs(sa->sa.sin.sin_port));
}
static void test_addr_copy(abts_case *tc, void *data)
{
apr_status_t rv;
apr_sockaddr_t *sa1, *sa2;
int rc;
const char *hosts[] = {
"127.0.0.1",
#if APR_HAVE_IPV6
"::1",
#endif
NULL
}, **host = hosts;
/* Loop up to and including NULL */
do {
rv = apr_sockaddr_info_get(&sa1, *host, APR_UNSPEC, 80, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
rv = apr_sockaddr_info_copy(&sa2, sa1, p);
APR_ASSERT_SUCCESS(tc, "Problem copying sockaddr", rv);
ABTS_PTR_NOTNULL(tc, sa1);
do {
ABTS_PTR_NOTNULL(tc, sa2);
rc = apr_sockaddr_equal(sa2, sa1);
ABTS_INT_NEQUAL(tc, 0, rc);
ABTS_INT_EQUAL(tc, 80, sa1->port);
ABTS_INT_EQUAL(tc, sa2->port, sa1->port);
ABTS_INT_EQUAL(tc, 80, ntohs(sa1->sa.sin.sin_port));
ABTS_INT_EQUAL(tc, ntohs(sa2->sa.sin.sin_port), ntohs(sa1->sa.sin.sin_port));
if (*host) {
ABTS_PTR_NOTNULL(tc, sa1->hostname);
ABTS_PTR_NOTNULL(tc, sa2->hostname);
ABTS_STR_EQUAL(tc, *host, sa1->hostname);
ABTS_STR_EQUAL(tc, sa1->hostname, sa2->hostname);
ABTS_TRUE(tc, sa1->hostname != sa2->hostname);
}
else {
ABTS_PTR_EQUAL(tc, NULL, sa1->hostname);
ABTS_PTR_EQUAL(tc, NULL, sa2->hostname);
}
} while ((sa2 = sa2->next, sa1 = sa1->next));
ABTS_PTR_EQUAL(tc, NULL, sa2);
} while (*host++);
}
static void test_serv_by_name(abts_case *tc, void *data)
{
apr_status_t rv;
apr_sockaddr_t *sa;
rv = apr_sockaddr_info_get(&sa, NULL, APR_UNSPEC, 0, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
rv = apr_getservbyname(sa, "ftp");
APR_ASSERT_SUCCESS(tc, "Problem getting ftp service", rv);
ABTS_INT_EQUAL(tc, 21, sa->port);
rv = apr_getservbyname(sa, "complete_and_utter_rubbish");
APR_ASSERT_SUCCESS(tc, "Problem getting non-existent service", !rv);
rv = apr_getservbyname(sa, "telnet");
APR_ASSERT_SUCCESS(tc, "Problem getting telnet service", rv);
ABTS_INT_EQUAL(tc, 23, sa->port);
}
static apr_socket_t *setup_socket(abts_case *tc)
{
apr_status_t rv;
apr_sockaddr_t *sa;
apr_socket_t *sock;
rv = apr_sockaddr_info_get(&sa, socket_name, socket_type, 8021, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
rv = apr_socket_create(&sock, sa->family, SOCK_STREAM, APR_PROTO_TCP, p);
APR_ASSERT_SUCCESS(tc, "Problem creating socket", rv);
rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
rv = apr_socket_bind(sock, sa);
APR_ASSERT_SUCCESS(tc, "Problem binding to port", rv);
if (rv) return NULL;
rv = apr_socket_listen(sock, 5);
APR_ASSERT_SUCCESS(tc, "Problem listening on socket", rv);
return sock;
}
static void test_create_bind_listen(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *sock = setup_socket(tc);
if (!sock) return;
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
static void test_send(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *sock;
apr_socket_t *sock2;
apr_proc_t proc;
int protocol;
apr_size_t length;
sock = setup_socket(tc);
if (!sock) return;
launch_child(tc, &proc, "read", p);
rv = apr_socket_accept(&sock2, sock, p);
APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);
apr_socket_protocol_get(sock2, &protocol);
ABTS_INT_EQUAL(tc, APR_PROTO_TCP, protocol);
length = strlen(DATASTR);
apr_socket_send(sock2, DATASTR, &length);
/* Make sure that the client received the data we sent */
ABTS_SIZE_EQUAL(tc, strlen(DATASTR), wait_child(tc, &proc));
rv = apr_socket_close(sock2);
APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
static void test_recv(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *sock;
apr_socket_t *sock2;
apr_proc_t proc;
int protocol;
apr_size_t length = STRLEN;
char datastr[STRLEN];
sock = setup_socket(tc);
if (!sock) return;
launch_child(tc, &proc, "write", p);
rv = apr_socket_accept(&sock2, sock, p);
APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);
apr_socket_protocol_get(sock2, &protocol);
ABTS_INT_EQUAL(tc, APR_PROTO_TCP, protocol);
memset(datastr, 0, STRLEN);
apr_socket_recv(sock2, datastr, &length);
/* Make sure that the server received the data we sent */
ABTS_STR_EQUAL(tc, DATASTR, datastr);
ABTS_SIZE_EQUAL(tc, strlen(datastr), wait_child(tc, &proc));
rv = apr_socket_close(sock2);
APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
static void test_atreadeof(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *sock;
apr_socket_t *sock2;
apr_proc_t proc;
apr_size_t length = STRLEN;
char datastr[STRLEN];
int atreadeof = -1;
sock = setup_socket(tc);
if (!sock) return;
launch_child(tc, &proc, "write", p);
rv = apr_socket_accept(&sock2, sock, p);
APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);
/* Check that the remote socket is still open */
rv = apr_socket_atreadeof(sock2, &atreadeof);
APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #1", rv);
ABTS_INT_EQUAL(tc, 0, atreadeof);
memset(datastr, 0, STRLEN);
apr_socket_recv(sock2, datastr, &length);
/* Make sure that the server received the data we sent */
ABTS_STR_EQUAL(tc, DATASTR, datastr);
ABTS_SIZE_EQUAL(tc, strlen(datastr), wait_child(tc, &proc));
/* The child is dead, so should be the remote socket */
rv = apr_socket_atreadeof(sock2, &atreadeof);
APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #2", rv);
ABTS_INT_EQUAL(tc, 1, atreadeof);
rv = apr_socket_close(sock2);
APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);
launch_child(tc, &proc, "close", p);
rv = apr_socket_accept(&sock2, sock, p);
APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);
/* The child closed the socket as soon as it could... */
rv = apr_socket_atreadeof(sock2, &atreadeof);
APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #3", rv);
if (!atreadeof) { /* ... but perhaps not yet; wait a moment */
apr_sleep(apr_time_from_msec(5));
rv = apr_socket_atreadeof(sock2, &atreadeof);
APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #4", rv);
}
ABTS_INT_EQUAL(tc, 1, atreadeof);
wait_child(tc, &proc);
rv = apr_socket_close(sock2);
APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
static void test_timeout(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *sock;
apr_socket_t *sock2;
apr_proc_t proc;
int protocol;
int exit;
sock = setup_socket(tc);
if (!sock) return;
launch_child(tc, &proc, "read", p);
rv = apr_socket_accept(&sock2, sock, p);
APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);
apr_socket_protocol_get(sock2, &protocol);
ABTS_INT_EQUAL(tc, APR_PROTO_TCP, protocol);
exit = wait_child(tc, &proc);
ABTS_INT_EQUAL(tc, SOCKET_TIMEOUT, exit);
/* We didn't write any data, so make sure the child program returns
* an error.
*/
rv = apr_socket_close(sock2);
APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
static void test_print_addr(abts_case *tc, void *data)
{
apr_sockaddr_t *sa;
apr_status_t rv;
char *s;
rv = apr_sockaddr_info_get(&sa, "0.0.0.0", APR_INET, 80, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
s = apr_psprintf(p, "foo %pI bar", sa);
ABTS_STR_EQUAL(tc, "foo 0.0.0.0:80 bar", s);
#if APR_HAVE_IPV6
rv = apr_sockaddr_info_get(&sa, "::ffff:0.0.0.0", APR_INET6, 80, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
if (rv == APR_SUCCESS)
ABTS_TRUE(tc, sa != NULL);
if (rv == APR_SUCCESS && sa) {
/* sa should now be a v4-mapped IPv6 address. */
char buf[128];
int rc;
rc = apr_sockaddr_is_wildcard(sa);
ABTS_INT_NEQUAL(tc, 0, rc);
memset(buf, 'z', sizeof buf);
APR_ASSERT_SUCCESS(tc, "could not get IP address",
apr_sockaddr_ip_getbuf(buf, 22, sa));
ABTS_STR_EQUAL(tc, "0.0.0.0", buf);
}
#endif
}
static void test_get_addr(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *ld, *sd, *cd;
apr_sockaddr_t *sa, *ca;
apr_pool_t *subp;
char *a, *b;
APR_ASSERT_SUCCESS(tc, "create subpool", apr_pool_create(&subp, p));
ld = setup_socket(tc);
if (!ld) return;
APR_ASSERT_SUCCESS(tc,
"get local address of bound socket",
apr_socket_addr_get(&sa, APR_LOCAL, ld));
rv = apr_socket_create(&cd, sa->family, SOCK_STREAM,
APR_PROTO_TCP, subp);
APR_ASSERT_SUCCESS(tc, "create client socket", rv);
APR_ASSERT_SUCCESS(tc, "enable non-block mode",
apr_socket_timeout_set(cd, 0));
/* It is valid for a connect() on a non-blocking socket to succeed
* (if the connection can be established synchronously), but if it
* does, this test cannot proceed. */
rv = apr_socket_connect(cd, sa);
if (rv == APR_SUCCESS) {
apr_socket_close(ld);
apr_socket_close(cd);
ABTS_SKIP(tc, data, "Cannot test if connect() completes "
"synchronously");
return;
}
if (!APR_STATUS_IS_EINPROGRESS(rv)) {
apr_socket_close(ld);
apr_socket_close(cd);
APR_ASSERT_SUCCESS(tc, "connect to listener", rv);
return;
}
APR_ASSERT_SUCCESS(tc, "accept connection",
apr_socket_accept(&sd, ld, subp));
{
/* wait for writability */
apr_pollfd_t pfd;
int n;
pfd.p = p;
pfd.desc_type = APR_POLL_SOCKET;
pfd.reqevents = APR_POLLOUT|APR_POLLHUP;
pfd.desc.s = cd;
pfd.client_data = NULL;
APR_ASSERT_SUCCESS(tc, "poll for connect completion",
apr_poll(&pfd, 1, &n, 5 * APR_USEC_PER_SEC));
}
APR_ASSERT_SUCCESS(tc, "get local address of server socket",
apr_socket_addr_get(&sa, APR_LOCAL, sd));
APR_ASSERT_SUCCESS(tc, "get remote address of client socket",
apr_socket_addr_get(&ca, APR_REMOTE, cd));
/* Test that the pool of the returned sockaddr objects exactly
* match the socket. */
ABTS_PTR_EQUAL(tc, subp, sa->pool);
ABTS_PTR_EQUAL(tc, subp, ca->pool);
/* Check equivalence. */
a = apr_psprintf(p, "%pI fam=%d", sa, sa->family);
b = apr_psprintf(p, "%pI fam=%d", ca, ca->family);
ABTS_STR_EQUAL(tc, a, b);
/* Check pool of returned sockaddr, as above. */
APR_ASSERT_SUCCESS(tc, "get local address of client socket",
apr_socket_addr_get(&sa, APR_LOCAL, cd));
APR_ASSERT_SUCCESS(tc, "get remote address of server socket",
apr_socket_addr_get(&ca, APR_REMOTE, sd));
/* Check equivalence. */
a = apr_psprintf(p, "%pI fam=%d", sa, sa->family);
b = apr_psprintf(p, "%pI fam=%d", ca, ca->family);
ABTS_STR_EQUAL(tc, a, b);
ABTS_PTR_EQUAL(tc, subp, sa->pool);
ABTS_PTR_EQUAL(tc, subp, ca->pool);
apr_socket_close(cd);
apr_socket_close(sd);
apr_socket_close(ld);
apr_pool_destroy(subp);
}
/* Make sure that setting a connected socket non-blocking works
* when the listening socket was non-blocking.
* If APR thinks that non-blocking is inherited but it really
* isn't, this testcase will fail.
*/
static void test_nonblock_inheritance(abts_case *tc, void *data)
{
apr_status_t rv;
apr_socket_t *sock;
apr_socket_t *sock2;
apr_proc_t proc;
char buffer[10];
apr_size_t length;
int tries;
sock = setup_socket(tc);
if (!sock) return;
rv = apr_socket_opt_set(sock, APR_SO_NONBLOCK, 1);
APR_ASSERT_SUCCESS(tc, "Could not make listening socket nonblocking", rv);
launch_child(tc, &proc, "write_after_delay", p);
tries = 10;
while (tries--) {
rv = apr_socket_accept(&sock2, sock, p);
if (!APR_STATUS_IS_EAGAIN(rv)) {
break;
}
apr_sleep(apr_time_from_msec(50));
}
APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);
rv = apr_socket_opt_set(sock2, APR_SO_NONBLOCK, 1);
APR_ASSERT_SUCCESS(tc, "Could not make connected socket nonblocking", rv);
length = sizeof buffer;
rv = apr_socket_recv(sock2, buffer, &length);
ABTS_ASSERT(tc, "should have gotten EAGAIN", APR_STATUS_IS_EAGAIN(rv));
wait_child(tc, &proc);
rv = apr_socket_close(sock2);
APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
static void test_freebind(abts_case *tc, void *data)
{
#ifdef IP_FREEBIND
apr_status_t rv;
apr_socket_t *sock;
apr_sockaddr_t *sa;
apr_int32_t on;
/* RFC 5737 address */
rv = apr_sockaddr_info_get(&sa, "192.0.2.1", APR_INET, 8080, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
rv = apr_socket_create(&sock, sa->family, SOCK_STREAM, APR_PROTO_TCP, p);
APR_ASSERT_SUCCESS(tc, "Problem creating socket", rv);
rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
rv = apr_socket_opt_set(sock, APR_SO_FREEBIND, 1);
APR_ASSERT_SUCCESS(tc, "Could not enable FREEBIND option", rv);
rv = apr_socket_opt_get(sock, APR_SO_FREEBIND, &on);
APR_ASSERT_SUCCESS(tc, "Could not retrieve FREEBIND option", rv);
ABTS_INT_EQUAL(tc, 1, on);
rv = apr_socket_bind(sock, sa);
APR_ASSERT_SUCCESS(tc, "Problem binding to port with FREEBIND", rv);
rv = apr_socket_close(sock);
APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
#endif
}
#define TEST_ZONE_ADDR "fe80::1"
#ifdef __linux__
/* Reasonable bet that "lo" will exist. */
#define TEST_ZONE_NAME "lo"
/* ... fill in other platforms here */
#endif
#ifdef TEST_ZONE_NAME
#define TEST_ZONE_FULLADDR TEST_ZONE_ADDR "%" TEST_ZONE_NAME
#endif
static void test_zone(abts_case *tc, void *data)
{
#if APR_HAVE_IPV6
apr_sockaddr_t *sa;
apr_status_t rv;
const char *name = NULL;
apr_uint32_t id = 0;
rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_INET, 8080, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
/* Fail for an IPv4 address! */
ABTS_INT_EQUAL(tc, APR_EBADIP,
apr_sockaddr_zone_set(sa, "1"));
ABTS_INT_EQUAL(tc, APR_EBADIP,
apr_sockaddr_zone_get(sa, &name, &id, p));
rv = apr_sockaddr_info_get(&sa, "::1", APR_INET6, 8080, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
/* Fail for an address which isn't link-local */
ABTS_INT_EQUAL(tc, APR_EBADIP, apr_sockaddr_zone_set(sa, "1"));
rv = apr_sockaddr_info_get(&sa, TEST_ZONE_ADDR, APR_INET6, 8080, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
ABTS_INT_EQUAL(tc, APR_EBADIP, apr_sockaddr_zone_get(sa, &name, &id, p));
#ifdef TEST_ZONE_NAME
{
apr_sockaddr_t *sa2;
char buf[50];
APR_ASSERT_SUCCESS(tc, "Set zone to " TEST_ZONE_NAME,
apr_sockaddr_zone_set(sa, TEST_ZONE_NAME));
APR_ASSERT_SUCCESS(tc, "Get zone",
apr_sockaddr_zone_get(sa, NULL, NULL, p));
APR_ASSERT_SUCCESS(tc, "Get zone",
apr_sockaddr_zone_get(sa, &name, &id, p));
ABTS_STR_EQUAL(tc, TEST_ZONE_NAME, name);
ABTS_INT_NEQUAL(tc, 0, id); /* Only guarantee is that it should be non-zero */
/* Check string translation. */
APR_ASSERT_SUCCESS(tc, "get IP address",
apr_sockaddr_ip_getbuf(buf, 50, sa));
ABTS_STR_EQUAL(tc, TEST_ZONE_FULLADDR, buf);
memset(buf, 'A', sizeof buf);
ABTS_INT_EQUAL(tc, APR_ENOSPC, apr_sockaddr_ip_getbuf(buf, strlen(TEST_ZONE_ADDR), sa));
ABTS_INT_EQUAL(tc, APR_ENOSPC, apr_sockaddr_ip_getbuf(buf, strlen(TEST_ZONE_FULLADDR), sa));
APR_ASSERT_SUCCESS(tc, "get IP address",
apr_sockaddr_ip_getbuf(buf, strlen(TEST_ZONE_FULLADDR) + 1, sa));
/* Check for overflow. */
ABTS_INT_EQUAL(tc, 'A', buf[strlen(buf) + 1]);
rv = apr_sockaddr_info_copy(&sa2, sa, p);
APR_ASSERT_SUCCESS(tc, "Problem copying sockaddr", rv);
/* Copy copied zone matches */
APR_ASSERT_SUCCESS(tc, "Get zone",
apr_sockaddr_zone_get(sa2, &name, &id, p));
ABTS_STR_EQUAL(tc, TEST_ZONE_NAME, name);
ABTS_INT_NEQUAL(tc, 0, id); /* Only guarantee is that it should be non-zero */
/* Should match self and copy */
ABTS_INT_NEQUAL(tc, 0, apr_sockaddr_equal(sa, sa));
ABTS_INT_NEQUAL(tc, 0, apr_sockaddr_equal(sa2, sa2));
ABTS_INT_NEQUAL(tc, 0, apr_sockaddr_equal(sa2, sa));
/* Should not match against copy without zone set. */
rv = apr_sockaddr_info_get(&sa2, TEST_ZONE_ADDR, APR_INET6, 8080, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
ABTS_INT_EQUAL(tc, 0, apr_sockaddr_equal(sa2, sa));
}
#endif /* TEST_ZONE_NAME */
#endif /* APR_HAVE_IPV6 */
}
abts_suite *testsock(abts_suite *suite)
{
suite = ADD_SUITE(suite)
socket_name = IPV4_SOCKET_NAME;
abts_run_test(suite, test_addr_info, NULL);
abts_run_test(suite, test_addr_copy, NULL);
abts_run_test(suite, test_serv_by_name, NULL);
abts_run_test(suite, test_create_bind_listen, NULL);
abts_run_test(suite, test_send, NULL);
abts_run_test(suite, test_recv, NULL);
abts_run_test(suite, test_atreadeof, NULL);
abts_run_test(suite, test_timeout, NULL);
abts_run_test(suite, test_print_addr, NULL);
abts_run_test(suite, test_get_addr, NULL);
abts_run_test(suite, test_nonblock_inheritance, NULL);
abts_run_test(suite, test_freebind, NULL);
abts_run_test(suite, test_zone, NULL);
#if APR_HAVE_SOCKADDR_UN
socket_name = UNIX_SOCKET_NAME;
socket_type = APR_UNIX;
abts_run_test(suite, test_create_bind_listen, NULL);
abts_run_test(suite, test_send, NULL);
abts_run_test(suite, test_recv, NULL);
abts_run_test(suite, test_timeout, NULL);
#endif
return suite;
}
|