socket_test.c 21.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2011-2018  Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9 10 11 12 13 14 15 16 17
 */

/*! \file */

#include <config.h>

#include <atf-c.h>

#include <unistd.h>
#include <time.h>

18
#include <isc/platform.h>
19
#include <isc/socket.h>
20
#include <isc/task.h>
21
#include <isc/print.h>
22

Evan Hunt's avatar
Evan Hunt committed
23
#include "../unix/socket_p.h"
24 25
#include "isctest.h"

Evan Hunt's avatar
Evan Hunt committed
26 27 28
static isc_boolean_t recv_dscp;
static unsigned int recv_dscp_value;

29 30 31
/*
 * Helper functions
 */
Evan Hunt's avatar
Evan Hunt committed
32

33 34 35
typedef struct {
	isc_boolean_t done;
	isc_result_t result;
Evan Hunt's avatar
Evan Hunt committed
36
	isc_socket_t *socket;
37 38 39 40 41
} completion_t;

static void
completion_init(completion_t *completion) {
	completion->done = ISC_FALSE;
Evan Hunt's avatar
Evan Hunt committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	completion->socket = NULL;
}

static void
accept_done(isc_task_t *task, isc_event_t *event) {
	isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
	completion_t *completion = event->ev_arg;

	UNUSED(task);

	completion->result = nevent->result;
	completion->done = ISC_TRUE;
	if (completion->result == ISC_R_SUCCESS)
		completion->socket = nevent->newsocket;
Tinderbox User's avatar
Tinderbox User committed
56

Evan Hunt's avatar
Evan Hunt committed
57
	isc_event_free(&event);
58 59 60 61 62 63 64 65 66 67 68 69
}

static void
event_done(isc_task_t *task, isc_event_t *event) {
	isc_socketevent_t *dev;
	completion_t *completion = event->ev_arg;

	UNUSED(task);

	dev = (isc_socketevent_t *) event;
	completion->result = dev->result;
	completion->done = ISC_TRUE;
Evan Hunt's avatar
Evan Hunt committed
70 71 72 73 74
	if ((dev->attributes & ISC_SOCKEVENTATTR_DSCP) != 0) {
		recv_dscp = ISC_TRUE;
		recv_dscp_value = dev->dscp;;
	} else
		recv_dscp = ISC_FALSE;
75 76 77 78 79 80 81 82 83 84 85
	isc_event_free(&event);
}

static isc_result_t
waitfor(completion_t *completion) {
	int i = 0;
	while (!completion->done && i++ < 5000) {
#ifndef ISC_PLATFORM_USETHREADS
		while (isc__taskmgr_ready(taskmgr))
			isc__taskmgr_dispatch(taskmgr);
#endif
86
		isc_test_nap(1000);
87 88 89 90 91 92
	}
	if (completion->done)
		return (ISC_R_SUCCESS);
	return (ISC_R_FAILURE);
}

Evan Hunt's avatar
Evan Hunt committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106
#if 0
static isc_result_t
waitfor(completion_t *completion) {
	int i = 0;
	while (!completion->done && i++ < 5000) {
		waitbody();
	}
	if (completion->done)
		return (ISC_R_SUCCESS);
	return (ISC_R_FAILURE);
}
#endif

static void
107
waitbody(void) {
Evan Hunt's avatar
Evan Hunt committed
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
#ifndef ISC_PLATFORM_USETHREADS
	struct timeval tv;
	isc_socketwait_t *swait = NULL;

	while (isc__taskmgr_ready(taskmgr))
		isc__taskmgr_dispatch(taskmgr);
	if (socketmgr != NULL) {
		tv.tv_sec = 0;
		tv.tv_usec = 1000 ;
		if (isc__socketmgr_waitevents(socketmgr, &tv, &swait) > 0)
			isc__socketmgr_dispatch(socketmgr, swait);
	} else
#endif
		isc_test_nap(1000);
}

static isc_result_t
waitfor2(completion_t *c1, completion_t *c2) {
	int i = 0;

	while (!(c1->done && c2->done) && i++ < 5000) {
		waitbody();
	}
	if (c1->done && c2->done)
		return (ISC_R_SUCCESS);
	return (ISC_R_FAILURE);
}

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
/*
 * Individual unit tests
 */

/* Test UDP sendto/recv (IPv4) */
ATF_TC(udp_sendto);
ATF_TC_HEAD(udp_sendto, tc) {
	atf_tc_set_md_var(tc, "descr", "UDP sendto/recv");
}
ATF_TC_BODY(udp_sendto, tc) {
	isc_result_t result;
	isc_sockaddr_t addr1, addr2;
	struct in_addr in;
	isc_socket_t *s1 = NULL, *s2 = NULL;
	isc_task_t *task = NULL;
	char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
	completion_t completion;
	isc_region_t r;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	in.s_addr = inet_addr("127.0.0.1");
161 162
	isc_sockaddr_fromin(&addr1, &in, 0);
	isc_sockaddr_fromin(&addr2, &in, 0);
163 164 165

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
166
	result = isc_socket_bind(s1, &addr1, 0);
167
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
168 169 170 171
	result = isc_socket_getsockname(s1, &addr1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr1) != 0);
172 173 174

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
175
	result = isc_socket_bind(s2, &addr2, 0);
176
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
177 178 179 180
	result = isc_socket_getsockname(s2, &addr2);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr2) != 0);
Automatic Updater's avatar
Automatic Updater committed
181

182 183 184
	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

Evan Hunt's avatar
Evan Hunt committed
185
	snprintf(sendbuf, sizeof(sendbuf), "Hello");
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
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	completion_init(&completion);
	result = isc_socket_sendto(s1, &r, task, event_done, &completion,
				   &addr2, NULL);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "Hello");

	isc_task_detach(&task);

	isc_socket_detach(&s1);
	isc_socket_detach(&s2);

	isc_test_end();
}

/* Test UDP sendto/recv with duplicated socket */
ATF_TC(udp_dup);
ATF_TC_HEAD(udp_dup, tc) {
	atf_tc_set_md_var(tc, "descr", "duplicated socket sendto/recv");
}
ATF_TC_BODY(udp_dup, tc) {
	isc_result_t result;
	isc_sockaddr_t addr1, addr2;
	struct in_addr in;
	isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL;
	isc_task_t *task = NULL;
	char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
	completion_t completion;
	isc_region_t r;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	in.s_addr = inet_addr("127.0.0.1");
236 237
	isc_sockaddr_fromin(&addr1, &in, 0);
	isc_sockaddr_fromin(&addr2, &in, 0);
238 239 240

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
241
	result = isc_socket_bind(s1, &addr1, 0);
242
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
243 244 245 246
	result = isc_socket_getsockname(s1, &addr1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr1) != 0);
247 248 249

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
250
	result = isc_socket_bind(s2, &addr2, 0);
251
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
252 253 254 255
	result = isc_socket_getsockname(s2, &addr2);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr2) != 0);
256 257

	result = isc_socket_dup(s2, &s3);
258
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
Automatic Updater's avatar
Automatic Updater committed
259

260 261 262
	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

Evan Hunt's avatar
Evan Hunt committed
263
	snprintf(sendbuf, sizeof(sendbuf), "Hello");
264 265 266 267 268 269 270 271 272 273 274
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	completion_init(&completion);
	result = isc_socket_sendto(s1, &r, task, event_done, &completion,
				   &addr2, NULL);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

Evan Hunt's avatar
Evan Hunt committed
275
	snprintf(sendbuf, sizeof(sendbuf), "World");
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
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	completion_init(&completion);
	result = isc_socket_sendto(s1, &r, task, event_done, &completion,
				   &addr2, NULL);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "Hello");

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s3, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "World");

	isc_task_detach(&task);

	isc_socket_detach(&s1);
	isc_socket_detach(&s2);
	isc_socket_detach(&s3);

	isc_test_end();
}

Evan Hunt's avatar
Evan Hunt committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/* Test TCP sendto/recv (IPv4) */
ATF_TC(udp_dscp_v4);
ATF_TC_HEAD(udp_dscp_v4, tc) {
	atf_tc_set_md_var(tc, "descr", "UDP DSCP IPV4");
}
ATF_TC_BODY(udp_dscp_v4, tc) {
	isc_result_t result;
	isc_sockaddr_t addr1, addr2;
	struct in_addr in;
	isc_socket_t *s1 = NULL, *s2 = NULL;
	isc_task_t *task = NULL;
	char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
	completion_t completion;
	isc_region_t r;
	isc_socketevent_t *socketevent;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	in.s_addr = inet_addr("127.0.0.1");
338 339
	isc_sockaddr_fromin(&addr1, &in, 0);
	isc_sockaddr_fromin(&addr2, &in, 0);
Evan Hunt's avatar
Evan Hunt committed
340 341 342 343 344 345 346

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			   isc_result_totext(result));
	result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			   isc_result_totext(result));
347 348 349 350
	result = isc_socket_getsockname(s1, &addr1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr1) != 0);
Evan Hunt's avatar
Evan Hunt committed
351 352 353 354 355 356 357

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s2);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			   isc_result_totext(result));
	result = isc_socket_bind(s2, &addr2, ISC_SOCKET_REUSEADDRESS);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			   isc_result_totext(result));
358 359 360 361
	result = isc_socket_getsockname(s2, &addr2);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr2) != 0);
Evan Hunt's avatar
Evan Hunt committed
362 363 364 365 366

	result = isc_task_create(taskmgr, 0, &task);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			   isc_result_totext(result));

Evan Hunt's avatar
Evan Hunt committed
367
	snprintf(sendbuf, sizeof(sendbuf), "Hello");
Evan Hunt's avatar
Evan Hunt committed
368 369 370 371
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	completion_init(&completion);
Tinderbox User's avatar
Tinderbox User committed
372

Evan Hunt's avatar
Evan Hunt committed
373
	socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE,
Tinderbox User's avatar
Tinderbox User committed
374
					     event_done, &completion);
Evan Hunt's avatar
Evan Hunt committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	ATF_REQUIRE(socketevent != NULL);

	if ((isc_net_probedscp() & ISC_NET_DSCPPKTV4) != 0) {
		socketevent->dscp = 056; /* EF */
		socketevent->attributes |= ISC_SOCKEVENTATTR_DSCP;
	} else if ((isc_net_probedscp() & ISC_NET_DSCPSETV4) != 0) {
		isc_socket_dscp(s1, 056);  /* EF */
		socketevent->dscp = 0;
		socketevent->attributes &= ~ISC_SOCKEVENTATTR_DSCP;
	}

	recv_dscp = ISC_FALSE;
	recv_dscp_value = 0;

	result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0);
390
	ATF_REQUIRE_EQ_MSG(result, ISC_R_SUCCESS, "%s",
Evan Hunt's avatar
Evan Hunt committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
			   isc_result_totext(result));
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "Hello");

	if ((isc_net_probedscp() & ISC_NET_DSCPRECVV4) != 0) {
		ATF_CHECK(recv_dscp);
		ATF_CHECK_EQ(recv_dscp_value, 056);
Tinderbox User's avatar
Tinderbox User committed
409
	} else
Evan Hunt's avatar
Evan Hunt committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
		ATF_CHECK(!recv_dscp);
	isc_task_detach(&task);

	isc_socket_detach(&s1);
	isc_socket_detach(&s2);

	isc_test_end();
}

/* Test TCP sendto/recv (IPv4) */
ATF_TC(udp_dscp_v6);
ATF_TC_HEAD(udp_dscp_v6, tc) {
	atf_tc_set_md_var(tc, "descr", "udp dscp ipv6");
}
ATF_TC_BODY(udp_dscp_v6, tc) {
425
#if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
Evan Hunt's avatar
Evan Hunt committed
426 427 428 429 430 431 432 433 434
	isc_result_t result;
	isc_sockaddr_t addr1, addr2;
	struct in6_addr in6;
	isc_socket_t *s1 = NULL, *s2 = NULL;
	isc_task_t *task = NULL;
	char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
	completion_t completion;
	isc_region_t r;
	isc_socketevent_t *socketevent;
Mark Andrews's avatar
Mark Andrews committed
435
	int n;
Evan Hunt's avatar
Evan Hunt committed
436 437 438 439 440 441

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

Mark Andrews's avatar
Mark Andrews committed
442 443
	n = inet_pton(AF_INET6, "::1", &in6.s6_addr);
	ATF_REQUIRE(n == 1);
444 445
	isc_sockaddr_fromin6(&addr1, &in6, 0);
	isc_sockaddr_fromin6(&addr2, &in6, 0);
Evan Hunt's avatar
Evan Hunt committed
446 447 448 449 450

	result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_udp,
				   &s1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
451
	result = isc_socket_bind(s1, &addr1, 0);
Evan Hunt's avatar
Evan Hunt committed
452 453
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
454 455 456 457
	result = isc_socket_getsockname(s1, &addr1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr1) != 0);
Evan Hunt's avatar
Evan Hunt committed
458 459 460 461 462

	result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_udp,
				   &s2);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
463 464 465 466
	result = isc_socket_bind(s2, &addr2, 0);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	result = isc_socket_getsockname(s2, &addr2);
Evan Hunt's avatar
Evan Hunt committed
467 468
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
469
	ATF_REQUIRE(isc_sockaddr_getport(&addr2) != 0);
Evan Hunt's avatar
Evan Hunt committed
470 471 472 473 474

	result = isc_task_create(taskmgr, 0, &task);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));

Evan Hunt's avatar
Evan Hunt committed
475
	snprintf(sendbuf, sizeof(sendbuf), "Hello");
Evan Hunt's avatar
Evan Hunt committed
476 477 478 479
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	completion_init(&completion);
Tinderbox User's avatar
Tinderbox User committed
480

Evan Hunt's avatar
Evan Hunt committed
481
	socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE,
Tinderbox User's avatar
Tinderbox User committed
482
					     event_done, &completion);
Evan Hunt's avatar
Evan Hunt committed
483 484 485 486 487 488 489 490 491 492 493 494
	ATF_REQUIRE(socketevent != NULL);

	if ((isc_net_probedscp() & ISC_NET_DSCPPKTV6) != 0) {
		socketevent->dscp = 056; /* EF */
		socketevent->attributes = ISC_SOCKEVENTATTR_DSCP;
	} else if ((isc_net_probedscp() & ISC_NET_DSCPSETV6) != 0)
		isc_socket_dscp(s1, 056);  /* EF */

	recv_dscp = ISC_FALSE;
	recv_dscp_value = 0;

	result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0);
495
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
Evan Hunt's avatar
Evan Hunt committed
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
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "Hello");
	if ((isc_net_probedscp() & ISC_NET_DSCPRECVV6) != 0) {
		ATF_CHECK(recv_dscp);
		ATF_CHECK_EQ(recv_dscp_value, 056);
	} else
		ATF_CHECK(!recv_dscp);

	isc_task_detach(&task);

	isc_socket_detach(&s1);
	isc_socket_detach(&s2);

	isc_test_end();
521 522 523 524
#else
	UNUSED(tc);
	atf_tc_skip("IPv6 not available");
#endif
Evan Hunt's avatar
Evan Hunt committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
}

/* Test TCP sendto/recv (IPv4) */
ATF_TC(tcp_dscp_v4);
ATF_TC_HEAD(tcp_dscp_v4, tc) {
	atf_tc_set_md_var(tc, "descr", "tcp dscp ipv4");
}
ATF_TC_BODY(tcp_dscp_v4, tc) {
	isc_result_t result;
	isc_sockaddr_t addr1;
	struct in_addr in;
	isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL;
	isc_task_t *task = NULL;
	char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
	completion_t completion, completion2;
	isc_region_t r;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	in.s_addr = inet_addr("127.0.0.1");
548
	isc_sockaddr_fromin(&addr1, &in, 0);
Evan Hunt's avatar
Evan Hunt committed
549 550 551 552

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_tcp, &s1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

553
	result = isc_socket_bind(s1, &addr1, 0);
Evan Hunt's avatar
Evan Hunt committed
554
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
555 556 557 558
	result = isc_socket_getsockname(s1, &addr1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr1) != 0);
Evan Hunt's avatar
Evan Hunt committed
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

	result = isc_socket_listen(s1, 3);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_tcp, &s2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	completion_init(&completion2);
	result = isc_socket_accept(s1, task, accept_done, &completion2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	completion_init(&completion);
	result = isc_socket_connect(s2, &addr1, task, event_done, &completion);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	waitfor2(&completion, &completion2);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK(completion2.done);
	ATF_CHECK_EQ(completion2.result, ISC_R_SUCCESS);
	s3 = completion2.socket;

	isc_socket_dscp(s2, 056);  /* EF */

Evan Hunt's avatar
Evan Hunt committed
585
	snprintf(sendbuf, sizeof(sendbuf), "Hello");
Evan Hunt's avatar
Evan Hunt committed
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
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	recv_dscp = ISC_FALSE;
	recv_dscp_value = 0;

	completion_init(&completion);
	result = isc_socket_sendto(s2, &r, task, event_done, &completion,
				   NULL, NULL);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s3, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "Hello");

	if ((isc_net_probedscp() & ISC_NET_DSCPRECVV4) != 0) {
		if (recv_dscp)
			ATF_CHECK_EQ(recv_dscp_value, 056);
	} else
		ATF_CHECK(!recv_dscp);

	isc_task_detach(&task);

	isc_socket_detach(&s1);
	isc_socket_detach(&s2);
	isc_socket_detach(&s3);

	isc_test_end();
}

/* Test TCP sendto/recv (IPv6) */
ATF_TC(tcp_dscp_v6);
ATF_TC_HEAD(tcp_dscp_v6, tc) {
	atf_tc_set_md_var(tc, "descr", "tcp dscp ipv6");
}
ATF_TC_BODY(tcp_dscp_v6, tc) {
631
#ifdef ISC_PLATFORM_HAVEIPV6
Evan Hunt's avatar
Evan Hunt committed
632 633 634 635 636 637 638 639
	isc_result_t result;
	isc_sockaddr_t addr1;
	struct in6_addr in6;
	isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL;
	isc_task_t *task = NULL;
	char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
	completion_t completion, completion2;
	isc_region_t r;
Mark Andrews's avatar
Mark Andrews committed
640
	int n;
Evan Hunt's avatar
Evan Hunt committed
641 642 643 644 645 646

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

Mark Andrews's avatar
Mark Andrews committed
647 648
	n = inet_pton(AF_INET6, "::1", &in6.s6_addr);
	ATF_REQUIRE(n == 1);
649
	isc_sockaddr_fromin6(&addr1, &in6, 0);
Evan Hunt's avatar
Evan Hunt committed
650 651 652 653 654

	result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_tcp,
				   &s1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

655
	result = isc_socket_bind(s1, &addr1, 0);
Evan Hunt's avatar
Evan Hunt committed
656
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
657 658 659 660
	result = isc_socket_getsockname(s1, &addr1);
	ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
			 isc_result_totext(result));
	ATF_REQUIRE(isc_sockaddr_getport(&addr1) != 0);
Evan Hunt's avatar
Evan Hunt committed
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

	result = isc_socket_listen(s1, 3);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_tcp,
				   &s2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	completion_init(&completion2);
	result = isc_socket_accept(s1, task, accept_done, &completion2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	completion_init(&completion);
	result = isc_socket_connect(s2, &addr1, task, event_done, &completion);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	waitfor2(&completion, &completion2);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK(completion2.done);
	ATF_CHECK_EQ(completion2.result, ISC_R_SUCCESS);
	s3 = completion2.socket;

	isc_socket_dscp(s2, 056);  /* EF */

Evan Hunt's avatar
Evan Hunt committed
688
	snprintf(sendbuf, sizeof(sendbuf), "Hello");
Evan Hunt's avatar
Evan Hunt committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	r.base = (void *) sendbuf;
	r.length = strlen(sendbuf) + 1;

	recv_dscp = ISC_FALSE;
	recv_dscp_value = 0;

	completion_init(&completion);
	result = isc_socket_sendto(s2, &r, task, event_done, &completion,
				   NULL, NULL);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);

	r.base = (void *) recvbuf;
	r.length = BUFSIZ;
	completion_init(&completion);
	result = isc_socket_recv(s3, &r, 1, task, event_done, &completion);
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	waitfor(&completion);
	ATF_CHECK(completion.done);
	ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
	ATF_CHECK_STREQ(recvbuf, "Hello");

	if ((isc_net_probedscp() & ISC_NET_DSCPRECVV6) != 0) {
		/*
		 * IPV6_RECVTCLASS is undefined for TCP however
Evan Hunt's avatar
Evan Hunt committed
716
		 * if we do get it it should be the value we set.
Evan Hunt's avatar
Evan Hunt committed
717 718 719 720 721 722 723 724 725 726 727 728 729
		 */
		if (recv_dscp)
			ATF_CHECK_EQ(recv_dscp_value, 056);
	} else
		ATF_CHECK(!recv_dscp);

	isc_task_detach(&task);

	isc_socket_detach(&s1);
	isc_socket_detach(&s2);
	isc_socket_detach(&s3);

	isc_test_end();
730 731 732 733
#else
	UNUSED(tc);
	atf_tc_skip("IPv6 not available");
#endif
Evan Hunt's avatar
Evan Hunt committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
}

ATF_TC(net_probedscp);
ATF_TC_HEAD(net_probedscp, tc) {
	atf_tc_set_md_var(tc, "descr", "probe dscp capabilities");
}
ATF_TC_BODY(net_probedscp, tc) {
	unsigned int n;

	UNUSED(tc);

	n = isc_net_probedscp();
	ATF_CHECK((n & ~ISC_NET_DSCPALL) == 0);

	/* ISC_NET_DSCPSETV4 MUST be set if any is set. */
	if (n & (ISC_NET_DSCPSETV4|ISC_NET_DSCPPKTV4|ISC_NET_DSCPRECVV4))
		ATF_CHECK_MSG((n & ISC_NET_DSCPSETV4) != 0,
			      "IPv4:%s%s%s\n",
			      (n & ISC_NET_DSCPSETV4) ? " set" : " none",
Tinderbox User's avatar
Tinderbox User committed
753
			      (n & ISC_NET_DSCPPKTV4) ? " packet" : "",
Evan Hunt's avatar
Evan Hunt committed
754 755 756
			      (n & ISC_NET_DSCPRECVV4) ? " receive" : "");

	/* ISC_NET_DSCPSETV6 MUST be set if any is set. */
757
	if (n & (ISC_NET_DSCPSETV6|ISC_NET_DSCPPKTV6|ISC_NET_DSCPRECVV6))
Evan Hunt's avatar
Evan Hunt committed
758 759 760
		ATF_CHECK_MSG((n & ISC_NET_DSCPSETV6) != 0,
			      "IPv6:%s%s%s\n",
			      (n & ISC_NET_DSCPSETV6) ? " set" : " none",
Tinderbox User's avatar
Tinderbox User committed
761
			      (n & ISC_NET_DSCPPKTV6) ? " packet" : "",
Evan Hunt's avatar
Evan Hunt committed
762 763 764 765 766 767 768 769
			      (n & ISC_NET_DSCPRECVV6) ? " receive" : "");

#if 0
	fprintf(stdout, "IPv4:%s%s%s\n",
		(n & ISC_NET_DSCPSETV4) ? " set" : "none",
		(n & ISC_NET_DSCPPKTV4) ? " packet" : "",
		(n & ISC_NET_DSCPRECVV4) ? " receive" : "");

Mark Andrews's avatar
Mark Andrews committed
770
	fprintf(stdout, "IPv6:%s%s%s\n",
Evan Hunt's avatar
Evan Hunt committed
771 772 773 774 775 776
		(n & ISC_NET_DSCPSETV6) ? " set" : "none",
		(n & ISC_NET_DSCPPKTV6) ? " packet" : "",
		(n & ISC_NET_DSCPRECVV6) ? " receive" : "");
#endif
}

777 778 779 780 781 782
/*
 * Main
 */
ATF_TP_ADD_TCS(tp) {
	ATF_TP_ADD_TC(tp, udp_sendto);
	ATF_TP_ADD_TC(tp, udp_dup);
Evan Hunt's avatar
Evan Hunt committed
783 784 785 786 787
	ATF_TP_ADD_TC(tp, tcp_dscp_v4);
	ATF_TP_ADD_TC(tp, tcp_dscp_v6);
	ATF_TP_ADD_TC(tp, udp_dscp_v4);
	ATF_TP_ADD_TC(tp, udp_dscp_v6);
	ATF_TP_ADD_TC(tp, net_probedscp);
788 789 790

	return (atf_no_error());
}