socket_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011-2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 #include <time.h>
00027 
00028 #include <isc/socket.h>
00029 
00030 #include "../task_p.h"
00031 #include "../unix/socket_p.h"
00032 #include "isctest.h"
00033 
00034 static isc_boolean_t recv_dscp;
00035 static unsigned int recv_dscp_value;
00036 
00037 /*
00038  * Helper functions
00039  */
00040 
00041 typedef struct {
00042         isc_boolean_t done;
00043         isc_result_t result;
00044         isc_socket_t *socket;
00045 } completion_t;
00046 
00047 static void
00048 completion_init(completion_t *completion) {
00049         completion->done = ISC_FALSE;
00050         completion->socket = NULL;
00051 }
00052 
00053 static void
00054 accept_done(isc_task_t *task, isc_event_t *event) {
00055         isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
00056         completion_t *completion = event->ev_arg;
00057 
00058         UNUSED(task);
00059 
00060         completion->result = nevent->result;
00061         completion->done = ISC_TRUE;
00062         if (completion->result == ISC_R_SUCCESS)
00063                 completion->socket = nevent->newsocket;
00064 
00065         isc_event_free(&event);
00066 }
00067 
00068 static void
00069 event_done(isc_task_t *task, isc_event_t *event) {
00070         isc_socketevent_t *dev;
00071         completion_t *completion = event->ev_arg;
00072 
00073         UNUSED(task);
00074 
00075         dev = (isc_socketevent_t *) event;
00076         completion->result = dev->result;
00077         completion->done = ISC_TRUE;
00078         if ((dev->attributes & ISC_SOCKEVENTATTR_DSCP) != 0) {
00079                 recv_dscp = ISC_TRUE;
00080                 recv_dscp_value = dev->dscp;;
00081         } else
00082                 recv_dscp = ISC_FALSE;
00083         isc_event_free(&event);
00084 }
00085 
00086 static isc_result_t
00087 waitfor(completion_t *completion) {
00088         int i = 0;
00089         while (!completion->done && i++ < 5000) {
00090 #ifndef ISC_PLATFORM_USETHREADS
00091                 while (isc__taskmgr_ready(taskmgr))
00092                         isc__taskmgr_dispatch(taskmgr);
00093 #endif
00094                 isc_test_nap(1000);
00095         }
00096         if (completion->done)
00097                 return (ISC_R_SUCCESS);
00098         return (ISC_R_FAILURE);
00099 }
00100 
00101 #if 0
00102 static isc_result_t
00103 waitfor(completion_t *completion) {
00104         int i = 0;
00105         while (!completion->done && i++ < 5000) {
00106                 waitbody();
00107         }
00108         if (completion->done)
00109                 return (ISC_R_SUCCESS);
00110         return (ISC_R_FAILURE);
00111 }
00112 #endif
00113 
00114 static void
00115 waitbody(void) {
00116 #ifndef ISC_PLATFORM_USETHREADS
00117         struct timeval tv;
00118         isc_socketwait_t *swait = NULL;
00119 
00120         while (isc__taskmgr_ready(taskmgr))
00121                 isc__taskmgr_dispatch(taskmgr);
00122         if (socketmgr != NULL) {
00123                 tv.tv_sec = 0;
00124                 tv.tv_usec = 1000 ;
00125                 if (isc__socketmgr_waitevents(socketmgr, &tv, &swait) > 0)
00126                         isc__socketmgr_dispatch(socketmgr, swait);
00127         } else
00128 #endif
00129                 isc_test_nap(1000);
00130 }
00131 
00132 static isc_result_t
00133 waitfor2(completion_t *c1, completion_t *c2) {
00134         int i = 0;
00135 
00136         while (!(c1->done && c2->done) && i++ < 5000) {
00137                 waitbody();
00138         }
00139         if (c1->done && c2->done)
00140                 return (ISC_R_SUCCESS);
00141         return (ISC_R_FAILURE);
00142 }
00143 
00144 /*
00145  * Individual unit tests
00146  */
00147 
00148 /* Test UDP sendto/recv (IPv4) */
00149 ATF_TC(udp_sendto);
00150 ATF_TC_HEAD(udp_sendto, tc) {
00151         atf_tc_set_md_var(tc, "descr", "UDP sendto/recv");
00152 }
00153 ATF_TC_BODY(udp_sendto, tc) {
00154         isc_result_t result;
00155         isc_sockaddr_t addr1, addr2;
00156         struct in_addr in;
00157         isc_socket_t *s1 = NULL, *s2 = NULL;
00158         isc_task_t *task = NULL;
00159         char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
00160         completion_t completion;
00161         isc_region_t r;
00162 
00163         UNUSED(tc);
00164 
00165         result = isc_test_begin(NULL, ISC_TRUE);
00166         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00167 
00168         /*
00169          * Create two sockets: 127.0.0.1/5444 and 127.0.0.1/5445, talking to
00170          * each other.
00171          */
00172         in.s_addr = inet_addr("127.0.0.1");
00173         isc_sockaddr_fromin(&addr1, &in, 5444);
00174         isc_sockaddr_fromin(&addr2, &in, 5445);
00175 
00176         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s1);
00177         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00178         result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
00179         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00180 
00181         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s2);
00182         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00183         result = isc_socket_bind(s2, &addr2, ISC_SOCKET_REUSEADDRESS);
00184         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00185 
00186         result = isc_task_create(taskmgr, 0, &task);
00187         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00188 
00189         strcpy(sendbuf, "Hello");
00190         r.base = (void *) sendbuf;
00191         r.length = strlen(sendbuf) + 1;
00192 
00193         completion_init(&completion);
00194         result = isc_socket_sendto(s1, &r, task, event_done, &completion,
00195                                    &addr2, NULL);
00196         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00197         waitfor(&completion);
00198         ATF_CHECK(completion.done);
00199         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00200 
00201         r.base = (void *) recvbuf;
00202         r.length = BUFSIZ;
00203         completion_init(&completion);
00204         result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
00205         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00206         waitfor(&completion);
00207         ATF_CHECK(completion.done);
00208         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00209         ATF_CHECK_STREQ(recvbuf, "Hello");
00210 
00211         isc_task_detach(&task);
00212 
00213         isc_socket_detach(&s1);
00214         isc_socket_detach(&s2);
00215 
00216         isc_test_end();
00217 }
00218 
00219 /* Test UDP sendto/recv with duplicated socket */
00220 ATF_TC(udp_dup);
00221 ATF_TC_HEAD(udp_dup, tc) {
00222         atf_tc_set_md_var(tc, "descr", "duplicated socket sendto/recv");
00223 }
00224 ATF_TC_BODY(udp_dup, tc) {
00225         isc_result_t result;
00226         isc_sockaddr_t addr1, addr2;
00227         struct in_addr in;
00228         isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL;
00229         isc_task_t *task = NULL;
00230         char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
00231         completion_t completion;
00232         isc_region_t r;
00233 
00234         UNUSED(tc);
00235 
00236         result = isc_test_begin(NULL, ISC_TRUE);
00237         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00238 
00239         /*
00240          * Create two sockets: 127.0.0.1/5444 and 127.0.0.1/5445, talking to
00241          * each other.
00242          */
00243         in.s_addr = inet_addr("127.0.0.1");
00244         isc_sockaddr_fromin(&addr1, &in, 5444);
00245         isc_sockaddr_fromin(&addr2, &in, 5445);
00246 
00247         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s1);
00248         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00249         result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
00250         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00251 
00252         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s2);
00253         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00254         result = isc_socket_bind(s2, &addr2, ISC_SOCKET_REUSEADDRESS);
00255         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00256 
00257         result = isc_socket_dup(s2, &s3);
00258         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00259 
00260         result = isc_task_create(taskmgr, 0, &task);
00261         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00262 
00263         strcpy(sendbuf, "Hello");
00264         r.base = (void *) sendbuf;
00265         r.length = strlen(sendbuf) + 1;
00266 
00267         completion_init(&completion);
00268         result = isc_socket_sendto(s1, &r, task, event_done, &completion,
00269                                    &addr2, NULL);
00270         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00271         waitfor(&completion);
00272         ATF_CHECK(completion.done);
00273         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00274 
00275         strcpy(sendbuf, "World");
00276         r.base = (void *) sendbuf;
00277         r.length = strlen(sendbuf) + 1;
00278 
00279         completion_init(&completion);
00280         result = isc_socket_sendto(s1, &r, task, event_done, &completion,
00281                                    &addr2, NULL);
00282         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00283         waitfor(&completion);
00284         ATF_CHECK(completion.done);
00285         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00286 
00287         r.base = (void *) recvbuf;
00288         r.length = BUFSIZ;
00289         completion_init(&completion);
00290         result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
00291         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00292         waitfor(&completion);
00293         ATF_CHECK(completion.done);
00294         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00295         ATF_CHECK_STREQ(recvbuf, "Hello");
00296 
00297         r.base = (void *) recvbuf;
00298         r.length = BUFSIZ;
00299         completion_init(&completion);
00300         result = isc_socket_recv(s3, &r, 1, task, event_done, &completion);
00301         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00302         waitfor(&completion);
00303         ATF_CHECK(completion.done);
00304         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00305         ATF_CHECK_STREQ(recvbuf, "World");
00306 
00307         isc_task_detach(&task);
00308 
00309         isc_socket_detach(&s1);
00310         isc_socket_detach(&s2);
00311         isc_socket_detach(&s3);
00312 
00313         isc_test_end();
00314 }
00315 
00316 /* Test TCP sendto/recv (IPv4) */
00317 ATF_TC(udp_dscp_v4);
00318 ATF_TC_HEAD(udp_dscp_v4, tc) {
00319         atf_tc_set_md_var(tc, "descr", "UDP DSCP IPV4");
00320 }
00321 ATF_TC_BODY(udp_dscp_v4, tc) {
00322         isc_result_t result;
00323         isc_sockaddr_t addr1, addr2;
00324         struct in_addr in;
00325         isc_socket_t *s1 = NULL, *s2 = NULL;
00326         isc_task_t *task = NULL;
00327         char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
00328         completion_t completion;
00329         isc_region_t r;
00330         isc_socketevent_t *socketevent;
00331 
00332         UNUSED(tc);
00333 
00334         result = isc_test_begin(NULL, ISC_TRUE);
00335         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00336 
00337         /*
00338          * Create two sockets: 127.0.0.1/5444 and 127.0.0.1/5445, talking to
00339          * each other.
00340          */
00341         in.s_addr = inet_addr("127.0.0.1");
00342         isc_sockaddr_fromin(&addr1, &in, 5444);
00343         isc_sockaddr_fromin(&addr2, &in, 5445);
00344 
00345         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s1);
00346         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00347                            isc_result_totext(result));
00348         result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
00349         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00350                            isc_result_totext(result));
00351 
00352         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp, &s2);
00353         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00354                            isc_result_totext(result));
00355         result = isc_socket_bind(s2, &addr2, ISC_SOCKET_REUSEADDRESS);
00356         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00357                            isc_result_totext(result));
00358 
00359         result = isc_task_create(taskmgr, 0, &task);
00360         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00361                            isc_result_totext(result));
00362 
00363         strcpy(sendbuf, "Hello");
00364         r.base = (void *) sendbuf;
00365         r.length = strlen(sendbuf) + 1;
00366 
00367         completion_init(&completion);
00368 
00369         socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE,
00370                                              event_done, &completion);
00371         ATF_REQUIRE(socketevent != NULL);
00372 
00373         if ((isc_net_probedscp() & ISC_NET_DSCPPKTV4) != 0) {
00374                 socketevent->dscp = 056; /* EF */
00375                 socketevent->attributes |= ISC_SOCKEVENTATTR_DSCP;
00376         } else if ((isc_net_probedscp() & ISC_NET_DSCPSETV4) != 0) {
00377                 isc_socket_dscp(s1, 056);  /* EF */
00378                 socketevent->dscp = 0;
00379                 socketevent->attributes &= ~ISC_SOCKEVENTATTR_DSCP;
00380         }
00381 
00382         recv_dscp = ISC_FALSE;
00383         recv_dscp_value = 0;
00384 
00385         result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0);
00386         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00387                            isc_result_totext(result));
00388         waitfor(&completion);
00389         ATF_CHECK(completion.done);
00390         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00391 
00392         r.base = (void *) recvbuf;
00393         r.length = BUFSIZ;
00394         completion_init(&completion);
00395         result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
00396         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00397         waitfor(&completion);
00398         ATF_CHECK(completion.done);
00399         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00400         ATF_CHECK_STREQ(recvbuf, "Hello");
00401 
00402         if ((isc_net_probedscp() & ISC_NET_DSCPRECVV4) != 0) {
00403                 ATF_CHECK(recv_dscp);
00404                 ATF_CHECK_EQ(recv_dscp_value, 056);
00405         } else
00406                 ATF_CHECK(!recv_dscp);
00407         isc_task_detach(&task);
00408 
00409         isc_socket_detach(&s1);
00410         isc_socket_detach(&s2);
00411 
00412         isc_test_end();
00413 }
00414 
00415 /* Test TCP sendto/recv (IPv4) */
00416 ATF_TC(udp_dscp_v6);
00417 ATF_TC_HEAD(udp_dscp_v6, tc) {
00418         atf_tc_set_md_var(tc, "descr", "udp dscp ipv6");
00419 }
00420 ATF_TC_BODY(udp_dscp_v6, tc) {
00421         isc_result_t result;
00422         isc_sockaddr_t addr1, addr2;
00423         struct in6_addr in6;
00424         isc_socket_t *s1 = NULL, *s2 = NULL;
00425         isc_task_t *task = NULL;
00426         char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
00427         completion_t completion;
00428         isc_region_t r;
00429         isc_socketevent_t *socketevent;
00430         int n;
00431 
00432         UNUSED(tc);
00433 
00434         result = isc_test_begin(NULL, ISC_TRUE);
00435         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00436 
00437         /*
00438          * Create two sockets: ::1/5444 and ::1/5445, talking to
00439          * each other.
00440          */
00441         n = inet_pton(AF_INET6, "::1", &in6.s6_addr);
00442         ATF_REQUIRE(n == 1);
00443         isc_sockaddr_fromin6(&addr1, &in6, 5444);
00444         isc_sockaddr_fromin6(&addr2, &in6, 5445);
00445 
00446         result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_udp,
00447                                    &s1);
00448         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00449                          isc_result_totext(result));
00450         result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
00451         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00452                          isc_result_totext(result));
00453 
00454         result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_udp,
00455                                    &s2);
00456         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00457                          isc_result_totext(result));
00458         result = isc_socket_bind(s2, &addr2, ISC_SOCKET_REUSEADDRESS);
00459         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00460                          isc_result_totext(result));
00461 
00462         result = isc_task_create(taskmgr, 0, &task);
00463         ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "%s",
00464                          isc_result_totext(result));
00465 
00466         strcpy(sendbuf, "Hello");
00467         r.base = (void *) sendbuf;
00468         r.length = strlen(sendbuf) + 1;
00469 
00470         completion_init(&completion);
00471 
00472         socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE,
00473                                              event_done, &completion);
00474         ATF_REQUIRE(socketevent != NULL);
00475 
00476         if ((isc_net_probedscp() & ISC_NET_DSCPPKTV6) != 0) {
00477                 socketevent->dscp = 056; /* EF */
00478                 socketevent->attributes = ISC_SOCKEVENTATTR_DSCP;
00479         } else if ((isc_net_probedscp() & ISC_NET_DSCPSETV6) != 0)
00480                 isc_socket_dscp(s1, 056);  /* EF */
00481 
00482         recv_dscp = ISC_FALSE;
00483         recv_dscp_value = 0;
00484 
00485         result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0);
00486         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00487         waitfor(&completion);
00488         ATF_CHECK(completion.done);
00489         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00490 
00491         r.base = (void *) recvbuf;
00492         r.length = BUFSIZ;
00493         completion_init(&completion);
00494         result = isc_socket_recv(s2, &r, 1, task, event_done, &completion);
00495         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00496         waitfor(&completion);
00497         ATF_CHECK(completion.done);
00498         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00499         ATF_CHECK_STREQ(recvbuf, "Hello");
00500         if ((isc_net_probedscp() & ISC_NET_DSCPRECVV6) != 0) {
00501                 ATF_CHECK(recv_dscp);
00502                 ATF_CHECK_EQ(recv_dscp_value, 056);
00503         } else
00504                 ATF_CHECK(!recv_dscp);
00505 
00506         isc_task_detach(&task);
00507 
00508         isc_socket_detach(&s1);
00509         isc_socket_detach(&s2);
00510 
00511         isc_test_end();
00512 }
00513 
00514 /* Test TCP sendto/recv (IPv4) */
00515 ATF_TC(tcp_dscp_v4);
00516 ATF_TC_HEAD(tcp_dscp_v4, tc) {
00517         atf_tc_set_md_var(tc, "descr", "tcp dscp ipv4");
00518 }
00519 ATF_TC_BODY(tcp_dscp_v4, tc) {
00520         isc_result_t result;
00521         isc_sockaddr_t addr1;
00522         struct in_addr in;
00523         isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL;
00524         isc_task_t *task = NULL;
00525         char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
00526         completion_t completion, completion2;
00527         isc_region_t r;
00528 
00529         UNUSED(tc);
00530 
00531         result = isc_test_begin(NULL, ISC_TRUE);
00532         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00533 
00534         /*
00535          * Create two sockets: 127.0.0.1/5444, talking to each other.
00536          */
00537         in.s_addr = inet_addr("127.0.0.1");
00538         isc_sockaddr_fromin(&addr1, &in, 5444);
00539 
00540         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_tcp, &s1);
00541         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00542 
00543         result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
00544         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00545 
00546         result = isc_socket_listen(s1, 3);
00547         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00548 
00549         result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_tcp, &s2);
00550         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00551 
00552         result = isc_task_create(taskmgr, 0, &task);
00553         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00554 
00555         completion_init(&completion2);
00556         result = isc_socket_accept(s1, task, accept_done, &completion2);
00557         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00558 
00559         completion_init(&completion);
00560         result = isc_socket_connect(s2, &addr1, task, event_done, &completion);
00561         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00562         waitfor2(&completion, &completion2);
00563         ATF_CHECK(completion.done);
00564         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00565         ATF_CHECK(completion2.done);
00566         ATF_CHECK_EQ(completion2.result, ISC_R_SUCCESS);
00567         s3 = completion2.socket;
00568 
00569         isc_socket_dscp(s2, 056);  /* EF */
00570 
00571         strcpy(sendbuf, "Hello");
00572         r.base = (void *) sendbuf;
00573         r.length = strlen(sendbuf) + 1;
00574 
00575         recv_dscp = ISC_FALSE;
00576         recv_dscp_value = 0;
00577 
00578         completion_init(&completion);
00579         result = isc_socket_sendto(s2, &r, task, event_done, &completion,
00580                                    NULL, NULL);
00581         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00582         waitfor(&completion);
00583         ATF_CHECK(completion.done);
00584         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00585 
00586         r.base = (void *) recvbuf;
00587         r.length = BUFSIZ;
00588         completion_init(&completion);
00589         result = isc_socket_recv(s3, &r, 1, task, event_done, &completion);
00590         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00591         waitfor(&completion);
00592         ATF_CHECK(completion.done);
00593         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00594         ATF_CHECK_STREQ(recvbuf, "Hello");
00595 
00596         if ((isc_net_probedscp() & ISC_NET_DSCPRECVV4) != 0) {
00597                 if (recv_dscp)
00598                         ATF_CHECK_EQ(recv_dscp_value, 056);
00599         } else
00600                 ATF_CHECK(!recv_dscp);
00601 
00602         isc_task_detach(&task);
00603 
00604         isc_socket_detach(&s1);
00605         isc_socket_detach(&s2);
00606         isc_socket_detach(&s3);
00607 
00608         isc_test_end();
00609 }
00610 
00611 /* Test TCP sendto/recv (IPv6) */
00612 ATF_TC(tcp_dscp_v6);
00613 ATF_TC_HEAD(tcp_dscp_v6, tc) {
00614         atf_tc_set_md_var(tc, "descr", "tcp dscp ipv6");
00615 }
00616 ATF_TC_BODY(tcp_dscp_v6, tc) {
00617         isc_result_t result;
00618         isc_sockaddr_t addr1;
00619         struct in6_addr in6;
00620         isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL;
00621         isc_task_t *task = NULL;
00622         char sendbuf[BUFSIZ], recvbuf[BUFSIZ];
00623         completion_t completion, completion2;
00624         isc_region_t r;
00625         int n;
00626 
00627         UNUSED(tc);
00628 
00629         result = isc_test_begin(NULL, ISC_TRUE);
00630         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00631 
00632         /*
00633          * Create two sockets: ::1/5444, talking to each other.
00634          */
00635         n = inet_pton(AF_INET6, "::1", &in6.s6_addr);
00636         ATF_REQUIRE(n == 1);
00637         isc_sockaddr_fromin6(&addr1, &in6, 5444);
00638 
00639         result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_tcp,
00640                                    &s1);
00641         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00642 
00643         result = isc_socket_bind(s1, &addr1, ISC_SOCKET_REUSEADDRESS);
00644         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00645 
00646         result = isc_socket_listen(s1, 3);
00647         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00648 
00649         result = isc_socket_create(socketmgr, PF_INET6, isc_sockettype_tcp,
00650                                    &s2);
00651         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00652 
00653         result = isc_task_create(taskmgr, 0, &task);
00654         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00655 
00656         completion_init(&completion2);
00657         result = isc_socket_accept(s1, task, accept_done, &completion2);
00658         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00659 
00660         completion_init(&completion);
00661         result = isc_socket_connect(s2, &addr1, task, event_done, &completion);
00662         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00663         waitfor2(&completion, &completion2);
00664         ATF_CHECK(completion.done);
00665         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00666         ATF_CHECK(completion2.done);
00667         ATF_CHECK_EQ(completion2.result, ISC_R_SUCCESS);
00668         s3 = completion2.socket;
00669 
00670         isc_socket_dscp(s2, 056);  /* EF */
00671 
00672         strcpy(sendbuf, "Hello");
00673         r.base = (void *) sendbuf;
00674         r.length = strlen(sendbuf) + 1;
00675 
00676         recv_dscp = ISC_FALSE;
00677         recv_dscp_value = 0;
00678 
00679         completion_init(&completion);
00680         result = isc_socket_sendto(s2, &r, task, event_done, &completion,
00681                                    NULL, NULL);
00682         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00683         waitfor(&completion);
00684         ATF_CHECK(completion.done);
00685         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00686 
00687         r.base = (void *) recvbuf;
00688         r.length = BUFSIZ;
00689         completion_init(&completion);
00690         result = isc_socket_recv(s3, &r, 1, task, event_done, &completion);
00691         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00692         waitfor(&completion);
00693         ATF_CHECK(completion.done);
00694         ATF_CHECK_EQ(completion.result, ISC_R_SUCCESS);
00695         ATF_CHECK_STREQ(recvbuf, "Hello");
00696 
00697         if ((isc_net_probedscp() & ISC_NET_DSCPRECVV6) != 0) {
00698                 /*
00699                  * IPV6_RECVTCLASS is undefined for TCP however
00700                  * if we do get it should be the the value we set.
00701                  */
00702                 if (recv_dscp)
00703                         ATF_CHECK_EQ(recv_dscp_value, 056);
00704         } else
00705                 ATF_CHECK(!recv_dscp);
00706 
00707         isc_task_detach(&task);
00708 
00709         isc_socket_detach(&s1);
00710         isc_socket_detach(&s2);
00711         isc_socket_detach(&s3);
00712 
00713         isc_test_end();
00714 }
00715 
00716 ATF_TC(net_probedscp);
00717 ATF_TC_HEAD(net_probedscp, tc) {
00718         atf_tc_set_md_var(tc, "descr", "probe dscp capabilities");
00719 }
00720 ATF_TC_BODY(net_probedscp, tc) {
00721         unsigned int n;
00722 
00723         UNUSED(tc);
00724 
00725         n = isc_net_probedscp();
00726         ATF_CHECK((n & ~ISC_NET_DSCPALL) == 0);
00727 
00728         /* ISC_NET_DSCPSETV4 MUST be set if any is set. */
00729         if (n & (ISC_NET_DSCPSETV4|ISC_NET_DSCPPKTV4|ISC_NET_DSCPRECVV4))
00730                 ATF_CHECK_MSG((n & ISC_NET_DSCPSETV4) != 0,
00731                               "IPv4:%s%s%s\n",
00732                               (n & ISC_NET_DSCPSETV4) ? " set" : " none",
00733                               (n & ISC_NET_DSCPPKTV4) ? " packet" : "",
00734                               (n & ISC_NET_DSCPRECVV4) ? " receive" : "");
00735 
00736         /* ISC_NET_DSCPSETV6 MUST be set if any is set. */
00737         if (n & (ISC_NET_DSCPSETV6|ISC_NET_DSCPPKTV4|ISC_NET_DSCPRECVV4))
00738                 ATF_CHECK_MSG((n & ISC_NET_DSCPSETV6) != 0,
00739                               "IPv6:%s%s%s\n",
00740                               (n & ISC_NET_DSCPSETV6) ? " set" : " none",
00741                               (n & ISC_NET_DSCPPKTV6) ? " packet" : "",
00742                               (n & ISC_NET_DSCPRECVV6) ? " receive" : "");
00743 
00744 #if 0
00745         fprintf(stdout, "IPv4:%s%s%s\n",
00746                 (n & ISC_NET_DSCPSETV4) ? " set" : "none",
00747                 (n & ISC_NET_DSCPPKTV4) ? " packet" : "",
00748                 (n & ISC_NET_DSCPRECVV4) ? " receive" : "");
00749 
00750         fprintf(stdout, "IPv6:%s%s%s\n",
00751                 (n & ISC_NET_DSCPSETV6) ? " set" : "none",
00752                 (n & ISC_NET_DSCPPKTV6) ? " packet" : "",
00753                 (n & ISC_NET_DSCPRECVV6) ? " receive" : "");
00754 #endif
00755 }
00756 
00757 /*
00758  * Main
00759  */
00760 ATF_TP_ADD_TCS(tp) {
00761         ATF_TP_ADD_TC(tp, udp_sendto);
00762         ATF_TP_ADD_TC(tp, udp_dup);
00763         ATF_TP_ADD_TC(tp, tcp_dscp_v4);
00764         ATF_TP_ADD_TC(tp, tcp_dscp_v6);
00765         ATF_TP_ADD_TC(tp, udp_dscp_v4);
00766         ATF_TP_ADD_TC(tp, udp_dscp_v6);
00767         ATF_TP_ADD_TC(tp, net_probedscp);
00768 
00769         return (atf_no_error());
00770 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha