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