Home | History | Annotate | Line # | Download | only in sys
t_sendrecv.c revision 1.3
      1 /*	$NetBSD: t_sendrecv.c,v 1.3 2018/08/21 11:04:49 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_sendrecv.c,v 1.3 2018/08/21 11:04:49 christos Exp $");
     33 
     34 #include <atf-c.h>
     35 #include <sys/types.h>
     36 #include <sys/socket.h>
     37 
     38 #include <string.h>
     39 #include <stdint.h>
     40 #include <errno.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <unistd.h>
     44 #include <sched.h>
     45 #include <signal.h>
     46 
     47 ATF_TC(sendrecv_basic);
     48 ATF_TC_HEAD(sendrecv_basic, tc)
     49 {
     50 	atf_tc_set_md_var(tc, "descr", "A basic test of sendrecv(2)");
     51 }
     52 
     53 #define COUNT 100
     54 
     55 union packet {
     56 	uint8_t buf[1316];
     57 	uintmax_t seq;
     58 };
     59 
     60 static volatile sig_atomic_t rdied;
     61 
     62 static void
     63 handle_sigchld(__unused int pid)
     64 {
     65 
     66 	rdied = 1;
     67 }
     68 
     69 static void
     70 sender(int fd)
     71 {
     72 	union packet p;
     73 	ssize_t n;
     74 	p.seq = 0;
     75 	for (size_t i = 0; i < COUNT; i++) {
     76 		for (; (n = send(fd, &p, sizeof(p), 0)) == sizeof(p);
     77 		    p.seq++)
     78 			continue;
     79 		printf(">>%zd %d %ju\n", n, errno, p.seq);
     80 		ATF_REQUIRE_MSG(errno == ENOBUFS, "send %s", strerror(errno));
     81 		sched_yield();
     82 	}
     83 	printf("sender done\n");
     84 }
     85 
     86 static void
     87 receiver(int fd)
     88 {
     89 	union packet p;
     90 	ssize_t n;
     91 	uintmax_t seq = 0;
     92 
     93 	do {
     94 		if (rdied)
     95 			return;
     96 		while ((n = recv(fd, &p, sizeof(p), 0), sizeof(p))
     97 		    == sizeof(p))
     98 		{
     99 			if (rdied)
    100 				return;
    101 			if (p.seq != seq)
    102 				printf("%ju != %ju\n", p.seq, seq);
    103 			seq = p.seq + 1;
    104 		}
    105 		printf("<<%zd %d %ju\n", n, errno, seq);
    106 		if (n == 0)
    107 			return;
    108 		ATF_REQUIRE_EQ(n, -1);
    109 		ATF_REQUIRE_MSG(errno == ENOBUFS, "recv %s", strerror(errno));
    110 	} while (p.seq < COUNT);
    111 }
    112 
    113 ATF_TC_BODY(sendrecv_basic, tc)
    114 {
    115 	int fd[2], error;
    116 	struct sigaction sa;
    117 
    118 	atf_tc_fail("does not terminate");
    119 
    120 	error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
    121 //	error = pipe(fd);
    122 	ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
    123 
    124 	memset(&sa, 0, sizeof(sa));
    125 	sa.sa_flags = SA_RESTART;
    126 	sa.sa_handler = &handle_sigchld;
    127 	sigemptyset(&sa.sa_mask);
    128 	error = sigaction(SIGCHLD, &sa, 0);
    129 	ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
    130 	    strerror(errno));
    131 
    132 	switch (fork()) {
    133 	case -1:
    134 		ATF_REQUIRE_MSG(errno == 0,
    135 		    "socketpair failed (%s)", strerror(errno));
    136 		/*NOTREACHED*/
    137 	case 0:
    138 		sched_yield();
    139 		sender(fd[0]);
    140 		close(fd[0]);
    141 		exit(EXIT_SUCCESS);
    142 		/*NOTREACHED*/
    143 	default:
    144 		receiver(fd[1]);
    145 		return;
    146 	}
    147 }
    148 
    149 ATF_TP_ADD_TCS(tp)
    150 {
    151 
    152 	ATF_TP_ADD_TC(tp, sendrecv_basic);
    153 
    154 	return atf_no_error();
    155 }
    156