t_can.c revision 1.1.2.1 1 1.1.2.1 bouyer /* $NetBSD: t_can.c,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $ */
2 1.1.2.1 bouyer
3 1.1.2.1 bouyer /*-
4 1.1.2.1 bouyer * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 1.1.2.1 bouyer * All rights reserved.
6 1.1.2.1 bouyer *
7 1.1.2.1 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 bouyer * by Manuel Bouyer
9 1.1.2.1 bouyer *
10 1.1.2.1 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 bouyer * modification, are permitted provided that the following conditions
12 1.1.2.1 bouyer * are met:
13 1.1.2.1 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.2.1 bouyer *
19 1.1.2.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
20 1.1.2.1 bouyer * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 1.1.2.1 bouyer * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 1.1.2.1 bouyer * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1.2.1 bouyer * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
24 1.1.2.1 bouyer * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1.2.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 1.1.2.1 bouyer * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1.2.1 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 1.1.2.1 bouyer * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 1.1.2.1 bouyer * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30 1.1.2.1 bouyer * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1.2.1 bouyer */
32 1.1.2.1 bouyer
33 1.1.2.1 bouyer #include <sys/cdefs.h>
34 1.1.2.1 bouyer #ifndef lint
35 1.1.2.1 bouyer __RCSID("$NetBSD: t_can.c,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $");
36 1.1.2.1 bouyer #endif /* not lint */
37 1.1.2.1 bouyer
38 1.1.2.1 bouyer #include <sys/types.h>
39 1.1.2.1 bouyer #include <sys/resource.h>
40 1.1.2.1 bouyer #include <sys/sysctl.h>
41 1.1.2.1 bouyer #include <sys/wait.h>
42 1.1.2.1 bouyer #include <sys/sockio.h>
43 1.1.2.1 bouyer
44 1.1.2.1 bouyer #include <atf-c.h>
45 1.1.2.1 bouyer #include <assert.h>
46 1.1.2.1 bouyer #include <fcntl.h>
47 1.1.2.1 bouyer #include <stdio.h>
48 1.1.2.1 bouyer #include <stdlib.h>
49 1.1.2.1 bouyer #include <string.h>
50 1.1.2.1 bouyer #include <unistd.h>
51 1.1.2.1 bouyer #include <signal.h>
52 1.1.2.1 bouyer
53 1.1.2.1 bouyer #include <net/if.h>
54 1.1.2.1 bouyer #include <netcan/can.h>
55 1.1.2.1 bouyer
56 1.1.2.1 bouyer #include <rump/rump.h>
57 1.1.2.1 bouyer #include <rump/rump_syscalls.h>
58 1.1.2.1 bouyer
59 1.1.2.1 bouyer #include "h_macros.h"
60 1.1.2.1 bouyer
61 1.1.2.1 bouyer static void
62 1.1.2.1 bouyer cancfg_rump_createif(const char *ifname)
63 1.1.2.1 bouyer {
64 1.1.2.1 bouyer int s, rv;
65 1.1.2.1 bouyer struct ifreq ifr;
66 1.1.2.1 bouyer
67 1.1.2.1 bouyer s = -1;
68 1.1.2.1 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
69 1.1.2.1 bouyer atf_tc_fail_errno("if config socket");
70 1.1.2.1 bouyer }
71 1.1.2.1 bouyer
72 1.1.2.1 bouyer memset(&ifr, 0, sizeof(ifr));
73 1.1.2.1 bouyer strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
74 1.1.2.1 bouyer
75 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCIFCREATE, &ifr)) < 0) {
76 1.1.2.1 bouyer atf_tc_fail_errno("if config create");
77 1.1.2.1 bouyer }
78 1.1.2.1 bouyer
79 1.1.2.1 bouyer memset(&ifr, 0, sizeof(ifr));
80 1.1.2.1 bouyer strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
81 1.1.2.1 bouyer
82 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCGIFFLAGS, &ifr)) < 0) {
83 1.1.2.1 bouyer atf_tc_fail_errno("if config get flags");
84 1.1.2.1 bouyer }
85 1.1.2.1 bouyer
86 1.1.2.1 bouyer ifr.ifr_flags |= IFF_UP;
87 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCSIFFLAGS, &ifr)) < 0) {
88 1.1.2.1 bouyer atf_tc_fail_errno("if config set flags");
89 1.1.2.1 bouyer }
90 1.1.2.1 bouyer }
91 1.1.2.1 bouyer
92 1.1.2.1 bouyer ATF_TC(canlocreate);
93 1.1.2.1 bouyer ATF_TC_HEAD(canlocreate, tc)
94 1.1.2.1 bouyer {
95 1.1.2.1 bouyer
96 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check CAN loopback create/destroy");
97 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
98 1.1.2.1 bouyer }
99 1.1.2.1 bouyer
100 1.1.2.1 bouyer ATF_TC_BODY(canlocreate, tc)
101 1.1.2.1 bouyer {
102 1.1.2.1 bouyer const char ifname[] = "canlo0";
103 1.1.2.1 bouyer int s, rv;
104 1.1.2.1 bouyer struct ifreq ifr;
105 1.1.2.1 bouyer
106 1.1.2.1 bouyer rump_init();
107 1.1.2.1 bouyer cancfg_rump_createif(ifname);
108 1.1.2.1 bouyer
109 1.1.2.1 bouyer s = -1;
110 1.1.2.1 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
111 1.1.2.1 bouyer atf_tc_fail_errno("if config socket(2)");
112 1.1.2.1 bouyer }
113 1.1.2.1 bouyer
114 1.1.2.1 bouyer memset(&ifr, 0, sizeof(ifr));
115 1.1.2.1 bouyer strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
116 1.1.2.1 bouyer
117 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCIFDESTROY, &ifr)) < 0) {
118 1.1.2.1 bouyer atf_tc_fail_errno("if config destroy");
119 1.1.2.1 bouyer }
120 1.1.2.1 bouyer }
121 1.1.2.1 bouyer
122 1.1.2.1 bouyer ATF_TC(canwritelo);
123 1.1.2.1 bouyer ATF_TC_HEAD(canwritelo, tc)
124 1.1.2.1 bouyer {
125 1.1.2.1 bouyer
126 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check that CAN sockets gets its own message via write");
127 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
128 1.1.2.1 bouyer }
129 1.1.2.1 bouyer
130 1.1.2.1 bouyer ATF_TC_BODY(canwritelo, tc)
131 1.1.2.1 bouyer {
132 1.1.2.1 bouyer const char ifname[] = "canlo0";
133 1.1.2.1 bouyer int s, rv;
134 1.1.2.1 bouyer struct sockaddr_can sa;
135 1.1.2.1 bouyer struct ifreq ifr;
136 1.1.2.1 bouyer struct can_frame cf_send, cf_receive;
137 1.1.2.1 bouyer
138 1.1.2.1 bouyer rump_init();
139 1.1.2.1 bouyer cancfg_rump_createif(ifname);
140 1.1.2.1 bouyer
141 1.1.2.1 bouyer s = -1;
142 1.1.2.1 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
143 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
144 1.1.2.1 bouyer }
145 1.1.2.1 bouyer
146 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname );
147 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
148 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
149 1.1.2.1 bouyer }
150 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
151 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
152 1.1.2.1 bouyer
153 1.1.2.1 bouyer sa.can_family = AF_CAN;
154 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
155 1.1.2.1 bouyer
156 1.1.2.1 bouyer if ((rv = rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
157 1.1.2.1 bouyer atf_tc_fail_errno("bind");
158 1.1.2.1 bouyer }
159 1.1.2.1 bouyer
160 1.1.2.1 bouyer /*
161 1.1.2.1 bouyer * send a single byte message, but make sure remaining payload is
162 1.1.2.1 bouyer * not 0.
163 1.1.2.1 bouyer */
164 1.1.2.1 bouyer
165 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
166 1.1.2.1 bouyer cf_send.can_id = 1;
167 1.1.2.1 bouyer cf_send.can_dlc = 1;
168 1.1.2.1 bouyer cf_send.data[0] = 0xde;
169 1.1.2.1 bouyer cf_send.data[1] = 0xad;
170 1.1.2.1 bouyer cf_send.data[2] = 0xbe;
171 1.1.2.1 bouyer cf_send.data[3] = 0xef;
172 1.1.2.1 bouyer
173 1.1.2.1 bouyer if (rump_sys_write(s, &cf_send, sizeof(cf_send) - 7) < 0) {
174 1.1.2.1 bouyer atf_tc_fail_errno("write");
175 1.1.2.1 bouyer }
176 1.1.2.1 bouyer
177 1.1.2.1 bouyer memset(&cf_receive, 0, sizeof(cf_receive));
178 1.1.2.1 bouyer if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
179 1.1.2.1 bouyer atf_tc_fail_errno("read");
180 1.1.2.1 bouyer }
181 1.1.2.1 bouyer
182 1.1.2.1 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket");
183 1.1.2.1 bouyer
184 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
185 1.1.2.1 bouyer cf_send.can_id = 1;
186 1.1.2.1 bouyer cf_send.can_dlc = 1;
187 1.1.2.1 bouyer cf_send.data[0] = 0xde;
188 1.1.2.1 bouyer /* other data[] are expected to be 0 */
189 1.1.2.1 bouyer
190 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
191 1.1.2.1 bouyer "received packet is not what we sent");
192 1.1.2.1 bouyer }
193 1.1.2.1 bouyer
194 1.1.2.1 bouyer ATF_TC(canwriteunbound);
195 1.1.2.1 bouyer ATF_TC_HEAD(canwriteunbound, tc)
196 1.1.2.1 bouyer {
197 1.1.2.1 bouyer
198 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check that write to unbound CAN sockets fails");
199 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
200 1.1.2.1 bouyer }
201 1.1.2.1 bouyer
202 1.1.2.1 bouyer ATF_TC_BODY(canwriteunbound, tc)
203 1.1.2.1 bouyer {
204 1.1.2.1 bouyer const char ifname[] = "canlo0";
205 1.1.2.1 bouyer int s, rv;
206 1.1.2.1 bouyer struct sockaddr_can sa;
207 1.1.2.1 bouyer struct can_frame cf_send;
208 1.1.2.1 bouyer
209 1.1.2.1 bouyer rump_init();
210 1.1.2.1 bouyer cancfg_rump_createif(ifname);
211 1.1.2.1 bouyer
212 1.1.2.1 bouyer s = -1;
213 1.1.2.1 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
214 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
215 1.1.2.1 bouyer }
216 1.1.2.1 bouyer
217 1.1.2.1 bouyer /*
218 1.1.2.1 bouyer * send a single byte message.
219 1.1.2.1 bouyer * not 0.
220 1.1.2.1 bouyer */
221 1.1.2.1 bouyer
222 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
223 1.1.2.1 bouyer cf_send.can_id = 1;
224 1.1.2.1 bouyer cf_send.can_dlc = 1;
225 1.1.2.1 bouyer cf_send.data[0] = 0xde;
226 1.1.2.1 bouyer
227 1.1.2.1 bouyer rv = rump_sys_write(s, &cf_send, sizeof(cf_send) - 7);
228 1.1.2.1 bouyer ATF_CHECK_MSG(rv < 0 && errno == EDESTADDRREQ,
229 1.1.2.1 bouyer "write to unbound socket didn't fail");
230 1.1.2.1 bouyer }
231 1.1.2.1 bouyer
232 1.1.2.1 bouyer ATF_TC(cansendtolo);
233 1.1.2.1 bouyer ATF_TC_HEAD(cansendtolo, tc)
234 1.1.2.1 bouyer {
235 1.1.2.1 bouyer
236 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check that CAN sockets gets its own message via sendto");
237 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
238 1.1.2.1 bouyer }
239 1.1.2.1 bouyer
240 1.1.2.1 bouyer ATF_TC_BODY(cansendtolo, tc)
241 1.1.2.1 bouyer {
242 1.1.2.1 bouyer const char ifname[] = "canlo0";
243 1.1.2.1 bouyer int s, rv;
244 1.1.2.1 bouyer struct sockaddr_can sa;
245 1.1.2.1 bouyer struct ifreq ifr;
246 1.1.2.1 bouyer struct can_frame cf_send, cf_receive;
247 1.1.2.1 bouyer
248 1.1.2.1 bouyer rump_init();
249 1.1.2.1 bouyer cancfg_rump_createif(ifname);
250 1.1.2.1 bouyer
251 1.1.2.1 bouyer s = -1;
252 1.1.2.1 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
253 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
254 1.1.2.1 bouyer }
255 1.1.2.1 bouyer
256 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname );
257 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
258 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
259 1.1.2.1 bouyer }
260 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
261 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
262 1.1.2.1 bouyer
263 1.1.2.1 bouyer sa.can_family = AF_CAN;
264 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
265 1.1.2.1 bouyer
266 1.1.2.1 bouyer /*
267 1.1.2.1 bouyer * send a single byte message, but make sure remaining payload is
268 1.1.2.1 bouyer * not 0.
269 1.1.2.1 bouyer */
270 1.1.2.1 bouyer
271 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
272 1.1.2.1 bouyer cf_send.can_id = 1;
273 1.1.2.1 bouyer cf_send.can_dlc = 1;
274 1.1.2.1 bouyer cf_send.data[0] = 0xde;
275 1.1.2.1 bouyer cf_send.data[1] = 0xad;
276 1.1.2.1 bouyer cf_send.data[2] = 0xbe;
277 1.1.2.1 bouyer cf_send.data[3] = 0xef;
278 1.1.2.1 bouyer
279 1.1.2.1 bouyer if (rump_sys_sendto(s, &cf_send, sizeof(cf_send) - 7,
280 1.1.2.1 bouyer 0, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
281 1.1.2.1 bouyer atf_tc_fail_errno("sendto");
282 1.1.2.1 bouyer }
283 1.1.2.1 bouyer
284 1.1.2.1 bouyer memset(&cf_receive, 0, sizeof(cf_receive));
285 1.1.2.1 bouyer if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
286 1.1.2.1 bouyer atf_tc_fail_errno("read");
287 1.1.2.1 bouyer }
288 1.1.2.1 bouyer
289 1.1.2.1 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket");
290 1.1.2.1 bouyer
291 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
292 1.1.2.1 bouyer cf_send.can_id = 1;
293 1.1.2.1 bouyer cf_send.can_dlc = 1;
294 1.1.2.1 bouyer cf_send.data[0] = 0xde;
295 1.1.2.1 bouyer /* other data[] are expected to be 0 */
296 1.1.2.1 bouyer
297 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
298 1.1.2.1 bouyer "received packet is not what we sent");
299 1.1.2.1 bouyer }
300 1.1.2.1 bouyer
301 1.1.2.1 bouyer ATF_TC(cansendtowrite);
302 1.1.2.1 bouyer ATF_TC_HEAD(cansendtowrite, tc)
303 1.1.2.1 bouyer {
304 1.1.2.1 bouyer
305 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check that write after sendto on unbound socket fails");
306 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
307 1.1.2.1 bouyer }
308 1.1.2.1 bouyer
309 1.1.2.1 bouyer ATF_TC_BODY(cansendtowrite, tc)
310 1.1.2.1 bouyer {
311 1.1.2.1 bouyer const char ifname[] = "canlo0";
312 1.1.2.1 bouyer int s, rv;
313 1.1.2.1 bouyer struct sockaddr_can sa;
314 1.1.2.1 bouyer struct ifreq ifr;
315 1.1.2.1 bouyer struct can_frame cf_send, cf_receive;
316 1.1.2.1 bouyer
317 1.1.2.1 bouyer rump_init();
318 1.1.2.1 bouyer cancfg_rump_createif(ifname);
319 1.1.2.1 bouyer
320 1.1.2.1 bouyer s = -1;
321 1.1.2.1 bouyer if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
322 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
323 1.1.2.1 bouyer }
324 1.1.2.1 bouyer
325 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname );
326 1.1.2.1 bouyer if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
327 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
328 1.1.2.1 bouyer }
329 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
330 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
331 1.1.2.1 bouyer
332 1.1.2.1 bouyer sa.can_family = AF_CAN;
333 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
334 1.1.2.1 bouyer
335 1.1.2.1 bouyer /*
336 1.1.2.1 bouyer * send a single byte message.
337 1.1.2.1 bouyer * not 0.
338 1.1.2.1 bouyer */
339 1.1.2.1 bouyer
340 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
341 1.1.2.1 bouyer cf_send.can_id = 1;
342 1.1.2.1 bouyer cf_send.can_dlc = 1;
343 1.1.2.1 bouyer cf_send.data[0] = 0xde;
344 1.1.2.1 bouyer
345 1.1.2.1 bouyer if (rump_sys_sendto(s, &cf_send, sizeof(cf_send) - 7,
346 1.1.2.1 bouyer 0, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
347 1.1.2.1 bouyer atf_tc_fail_errno("sendto");
348 1.1.2.1 bouyer }
349 1.1.2.1 bouyer
350 1.1.2.1 bouyer memset(&cf_receive, 0, sizeof(cf_receive));
351 1.1.2.1 bouyer if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
352 1.1.2.1 bouyer atf_tc_fail_errno("read");
353 1.1.2.1 bouyer }
354 1.1.2.1 bouyer
355 1.1.2.1 bouyer ATF_CHECK_MSG(rv > 0, "short read on socket");
356 1.1.2.1 bouyer
357 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
358 1.1.2.1 bouyer cf_send.can_id = 1;
359 1.1.2.1 bouyer cf_send.can_dlc = 1;
360 1.1.2.1 bouyer cf_send.data[0] = 0xde;
361 1.1.2.1 bouyer /* other data[] are expected to be 0 */
362 1.1.2.1 bouyer
363 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
364 1.1.2.1 bouyer "received packet is not what we sent");
365 1.1.2.1 bouyer
366 1.1.2.1 bouyer rv = rump_sys_write(s, &cf_send, sizeof(cf_send) - 7);
367 1.1.2.1 bouyer ATF_CHECK_MSG(rv < 0 && errno == EDESTADDRREQ,
368 1.1.2.1 bouyer "write to unbound socket didn't fail");
369 1.1.2.1 bouyer }
370 1.1.2.1 bouyer
371 1.1.2.1 bouyer ATF_TC(canreadlocal);
372 1.1.2.1 bouyer ATF_TC_HEAD(canreadlocal, tc)
373 1.1.2.1 bouyer {
374 1.1.2.1 bouyer
375 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check all CAN sockets get messages");
376 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
377 1.1.2.1 bouyer }
378 1.1.2.1 bouyer
379 1.1.2.1 bouyer ATF_TC_BODY(canreadlocal, tc)
380 1.1.2.1 bouyer {
381 1.1.2.1 bouyer const char ifname[] = "canlo0";
382 1.1.2.1 bouyer int s1, rv1;
383 1.1.2.1 bouyer int s2, rv2;
384 1.1.2.1 bouyer struct sockaddr_can sa;
385 1.1.2.1 bouyer struct ifreq ifr;
386 1.1.2.1 bouyer struct can_frame cf_send, cf_receive1, cf_receive2;
387 1.1.2.1 bouyer
388 1.1.2.1 bouyer rump_init();
389 1.1.2.1 bouyer cancfg_rump_createif(ifname);
390 1.1.2.1 bouyer
391 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
392 1.1.2.1 bouyer cf_send.can_id = 1;
393 1.1.2.1 bouyer cf_send.can_dlc = 8;
394 1.1.2.1 bouyer cf_send.data[0] = 0xde;
395 1.1.2.1 bouyer cf_send.data[1] = 0xad;
396 1.1.2.1 bouyer cf_send.data[2] = 0xbe;
397 1.1.2.1 bouyer cf_send.data[3] = 0xef;
398 1.1.2.1 bouyer
399 1.1.2.1 bouyer
400 1.1.2.1 bouyer s1 = -1;
401 1.1.2.1 bouyer if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
402 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
403 1.1.2.1 bouyer }
404 1.1.2.1 bouyer
405 1.1.2.1 bouyer /* create a second socket */
406 1.1.2.1 bouyer
407 1.1.2.1 bouyer s2 = -1;
408 1.1.2.1 bouyer if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
409 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
410 1.1.2.1 bouyer }
411 1.1.2.1 bouyer
412 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname );
413 1.1.2.1 bouyer if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
414 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
415 1.1.2.1 bouyer }
416 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
417 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
418 1.1.2.1 bouyer
419 1.1.2.1 bouyer sa.can_family = AF_CAN;
420 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
421 1.1.2.1 bouyer
422 1.1.2.1 bouyer if ((rv2 = rump_sys_bind(s2, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
423 1.1.2.1 bouyer atf_tc_fail_errno("bind");
424 1.1.2.1 bouyer }
425 1.1.2.1 bouyer
426 1.1.2.1 bouyer /*
427 1.1.2.1 bouyer * send a single byte message, but make sure remaining payload is
428 1.1.2.1 bouyer * not 0.
429 1.1.2.1 bouyer */
430 1.1.2.1 bouyer
431 1.1.2.1 bouyer if (rump_sys_write(s2, &cf_send, sizeof(cf_send)) < 0) {
432 1.1.2.1 bouyer atf_tc_fail_errno("write");
433 1.1.2.1 bouyer }
434 1.1.2.1 bouyer
435 1.1.2.1 bouyer memset(&cf_receive2, 0, sizeof(cf_receive2));
436 1.1.2.1 bouyer if (( rv2 = rump_sys_read(s2, &cf_receive2, sizeof(cf_receive2))) < 0) {
437 1.1.2.1 bouyer atf_tc_fail_errno("read");
438 1.1.2.1 bouyer }
439 1.1.2.1 bouyer
440 1.1.2.1 bouyer ATF_CHECK_MSG(rv2 > 0, "short read on socket");
441 1.1.2.1 bouyer
442 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2, sizeof(cf_send)) == 0,
443 1.1.2.1 bouyer "received (2) packet is not what we sent");
444 1.1.2.1 bouyer
445 1.1.2.1 bouyer /* now check first socket */
446 1.1.2.1 bouyer memset(&cf_receive1, 0, sizeof(cf_receive1));
447 1.1.2.1 bouyer if (( rv1 = rump_sys_read(s1, &cf_receive1, sizeof(cf_receive1))) < 0) {
448 1.1.2.1 bouyer atf_tc_fail_errno("read");
449 1.1.2.1 bouyer }
450 1.1.2.1 bouyer
451 1.1.2.1 bouyer ATF_CHECK_MSG(rv1 > 0, "short read on socket");
452 1.1.2.1 bouyer
453 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1, sizeof(cf_send)) == 0,
454 1.1.2.1 bouyer "received (1) packet is not what we sent");
455 1.1.2.1 bouyer }
456 1.1.2.1 bouyer
457 1.1.2.1 bouyer ATF_TC(canrecvfrom);
458 1.1.2.1 bouyer ATF_TC_HEAD(canrecvfrom, tc)
459 1.1.2.1 bouyer {
460 1.1.2.1 bouyer
461 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check that recvfrom gets the CAN interface");
462 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
463 1.1.2.1 bouyer }
464 1.1.2.1 bouyer
465 1.1.2.1 bouyer ATF_TC_BODY(canrecvfrom, tc)
466 1.1.2.1 bouyer {
467 1.1.2.1 bouyer const char ifname[] = "canlo0";
468 1.1.2.1 bouyer int s1, rv1;
469 1.1.2.1 bouyer int s2, rv2;
470 1.1.2.1 bouyer struct sockaddr_can sa;
471 1.1.2.1 bouyer struct ifreq ifr;
472 1.1.2.1 bouyer struct can_frame cf_send, cf_receive1, cf_receive2;
473 1.1.2.1 bouyer socklen_t salen;
474 1.1.2.1 bouyer
475 1.1.2.1 bouyer rump_init();
476 1.1.2.1 bouyer cancfg_rump_createif(ifname);
477 1.1.2.1 bouyer
478 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
479 1.1.2.1 bouyer cf_send.can_id = 1;
480 1.1.2.1 bouyer cf_send.can_dlc = 8;
481 1.1.2.1 bouyer cf_send.data[0] = 0xde;
482 1.1.2.1 bouyer cf_send.data[1] = 0xad;
483 1.1.2.1 bouyer cf_send.data[2] = 0xbe;
484 1.1.2.1 bouyer cf_send.data[3] = 0xef;
485 1.1.2.1 bouyer
486 1.1.2.1 bouyer
487 1.1.2.1 bouyer s1 = -1;
488 1.1.2.1 bouyer if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
489 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
490 1.1.2.1 bouyer }
491 1.1.2.1 bouyer
492 1.1.2.1 bouyer /* create a second socket */
493 1.1.2.1 bouyer
494 1.1.2.1 bouyer s2 = -1;
495 1.1.2.1 bouyer if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
496 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
497 1.1.2.1 bouyer }
498 1.1.2.1 bouyer
499 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname );
500 1.1.2.1 bouyer if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
501 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
502 1.1.2.1 bouyer }
503 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
504 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
505 1.1.2.1 bouyer
506 1.1.2.1 bouyer sa.can_family = AF_CAN;
507 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
508 1.1.2.1 bouyer
509 1.1.2.1 bouyer if ((rv2 = rump_sys_bind(s2, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
510 1.1.2.1 bouyer atf_tc_fail_errno("bind");
511 1.1.2.1 bouyer }
512 1.1.2.1 bouyer
513 1.1.2.1 bouyer if (rump_sys_write(s2, &cf_send, sizeof(cf_send)) < 0) {
514 1.1.2.1 bouyer atf_tc_fail_errno("write");
515 1.1.2.1 bouyer }
516 1.1.2.1 bouyer
517 1.1.2.1 bouyer memset(&cf_receive2, 0, sizeof(cf_receive2));
518 1.1.2.1 bouyer if (( rv2 = rump_sys_read(s2, &cf_receive2, sizeof(cf_receive2))) < 0) {
519 1.1.2.1 bouyer atf_tc_fail_errno("read");
520 1.1.2.1 bouyer }
521 1.1.2.1 bouyer
522 1.1.2.1 bouyer ATF_CHECK_MSG(rv2 > 0, "short read on socket");
523 1.1.2.1 bouyer
524 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2, sizeof(cf_send)) == 0,
525 1.1.2.1 bouyer "received (2) packet is not what we sent");
526 1.1.2.1 bouyer
527 1.1.2.1 bouyer /* now check first socket */
528 1.1.2.1 bouyer memset(&cf_receive1, 0, sizeof(cf_receive1));
529 1.1.2.1 bouyer memset(&sa, 0, sizeof(sa));
530 1.1.2.1 bouyer salen = sizeof(sa);
531 1.1.2.1 bouyer if (( rv1 = rump_sys_recvfrom(s1, &cf_receive1, sizeof(cf_receive1),
532 1.1.2.1 bouyer 0, (struct sockaddr *)&sa, &salen)) < 0) {
533 1.1.2.1 bouyer atf_tc_fail_errno("recvfrom");
534 1.1.2.1 bouyer }
535 1.1.2.1 bouyer
536 1.1.2.1 bouyer ATF_CHECK_MSG(rv1 > 0, "short read on socket");
537 1.1.2.1 bouyer
538 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1, sizeof(cf_send)) == 0,
539 1.1.2.1 bouyer "recvfrom (1) packet is not what we sent");
540 1.1.2.1 bouyer ATF_CHECK_MSG(sa.can_family == AF_CAN,
541 1.1.2.1 bouyer "recvfrom provided wrong %d family", sa.can_family);
542 1.1.2.1 bouyer ATF_CHECK_MSG(salen == sizeof(sa),
543 1.1.2.1 bouyer "recvfrom provided wrong size %d (!= %d)", salen, sizeof(sa));
544 1.1.2.1 bouyer ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
545 1.1.2.1 bouyer "recvfrom provided wrong ifindex %d (!= %d)",
546 1.1.2.1 bouyer sa.can_ifindex, ifr.ifr_ifindex);
547 1.1.2.1 bouyer }
548 1.1.2.1 bouyer
549 1.1.2.1 bouyer ATF_TC(canbindfilter);
550 1.1.2.1 bouyer ATF_TC_HEAD(canbindfilter, tc)
551 1.1.2.1 bouyer {
552 1.1.2.1 bouyer
553 1.1.2.1 bouyer atf_tc_set_md_var(tc, "descr", "check that socket bound to an interface doens't get other interface's messages");
554 1.1.2.1 bouyer atf_tc_set_md_var(tc, "timeout", "5");
555 1.1.2.1 bouyer }
556 1.1.2.1 bouyer
557 1.1.2.1 bouyer ATF_TC_BODY(canbindfilter, tc)
558 1.1.2.1 bouyer {
559 1.1.2.1 bouyer const char ifname[] = "canlo0";
560 1.1.2.1 bouyer const char ifname2[] = "canlo1";
561 1.1.2.1 bouyer int s1, rv1;
562 1.1.2.1 bouyer int s2, rv2;
563 1.1.2.1 bouyer struct sockaddr_can sa;
564 1.1.2.1 bouyer struct ifreq ifr;
565 1.1.2.1 bouyer struct can_frame cf_send, cf_receive1, cf_receive2;
566 1.1.2.1 bouyer socklen_t salen;
567 1.1.2.1 bouyer fd_set rfds;
568 1.1.2.1 bouyer struct timeval tmout;
569 1.1.2.1 bouyer
570 1.1.2.1 bouyer rump_init();
571 1.1.2.1 bouyer cancfg_rump_createif(ifname);
572 1.1.2.1 bouyer cancfg_rump_createif(ifname2);
573 1.1.2.1 bouyer
574 1.1.2.1 bouyer memset(&cf_send, 0, sizeof(cf_send));
575 1.1.2.1 bouyer cf_send.can_id = 1;
576 1.1.2.1 bouyer cf_send.can_dlc = 8;
577 1.1.2.1 bouyer cf_send.data[0] = 0xde;
578 1.1.2.1 bouyer cf_send.data[1] = 0xad;
579 1.1.2.1 bouyer cf_send.data[2] = 0xbe;
580 1.1.2.1 bouyer cf_send.data[3] = 0xef;
581 1.1.2.1 bouyer
582 1.1.2.1 bouyer
583 1.1.2.1 bouyer s1 = -1;
584 1.1.2.1 bouyer if ((s1 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
585 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
586 1.1.2.1 bouyer }
587 1.1.2.1 bouyer
588 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname );
589 1.1.2.1 bouyer if ((rv1 = rump_sys_ioctl(s1, SIOCGIFINDEX, &ifr)) < 0) {
590 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX (1)");
591 1.1.2.1 bouyer }
592 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
593 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
594 1.1.2.1 bouyer
595 1.1.2.1 bouyer sa.can_family = AF_CAN;
596 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
597 1.1.2.1 bouyer
598 1.1.2.1 bouyer if ((rv1 = rump_sys_bind(s1, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
599 1.1.2.1 bouyer atf_tc_fail_errno("bind (1)");
600 1.1.2.1 bouyer }
601 1.1.2.1 bouyer
602 1.1.2.1 bouyer /* create a second socket */
603 1.1.2.1 bouyer
604 1.1.2.1 bouyer s2 = -1;
605 1.1.2.1 bouyer if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
606 1.1.2.1 bouyer atf_tc_fail_errno("CAN socket");
607 1.1.2.1 bouyer }
608 1.1.2.1 bouyer
609 1.1.2.1 bouyer strcpy(ifr.ifr_name, ifname2);
610 1.1.2.1 bouyer if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
611 1.1.2.1 bouyer atf_tc_fail_errno("SIOCGIFINDEX");
612 1.1.2.1 bouyer }
613 1.1.2.1 bouyer ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
614 1.1.2.1 bouyer ifname, ifr.ifr_ifindex);
615 1.1.2.1 bouyer
616 1.1.2.1 bouyer sa.can_family = AF_CAN;
617 1.1.2.1 bouyer sa.can_ifindex = ifr.ifr_ifindex;
618 1.1.2.1 bouyer
619 1.1.2.1 bouyer if ((rv2 = rump_sys_bind(s2, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
620 1.1.2.1 bouyer atf_tc_fail_errno("bind");
621 1.1.2.1 bouyer }
622 1.1.2.1 bouyer
623 1.1.2.1 bouyer if (rump_sys_write(s2, &cf_send, sizeof(cf_send)) < 0) {
624 1.1.2.1 bouyer atf_tc_fail_errno("write");
625 1.1.2.1 bouyer }
626 1.1.2.1 bouyer
627 1.1.2.1 bouyer memset(&cf_receive2, 0, sizeof(cf_receive2));
628 1.1.2.1 bouyer if (( rv2 = rump_sys_read(s2, &cf_receive2, sizeof(cf_receive2))) < 0) {
629 1.1.2.1 bouyer atf_tc_fail_errno("read");
630 1.1.2.1 bouyer }
631 1.1.2.1 bouyer
632 1.1.2.1 bouyer ATF_CHECK_MSG(rv2 > 0, "short read on socket");
633 1.1.2.1 bouyer
634 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive2, sizeof(cf_send)) == 0,
635 1.1.2.1 bouyer "received (2) packet is not what we sent");
636 1.1.2.1 bouyer
637 1.1.2.1 bouyer /* now check first socket */
638 1.1.2.1 bouyer memset(&cf_receive1, 0, sizeof(cf_receive1));
639 1.1.2.1 bouyer FD_ZERO(&rfds);
640 1.1.2.1 bouyer FD_SET(s1, &rfds);
641 1.1.2.1 bouyer /* we should receive no message; wait for 2 seconds */
642 1.1.2.1 bouyer tmout.tv_sec = 2;
643 1.1.2.1 bouyer tmout.tv_usec = 0;
644 1.1.2.1 bouyer rv1 = rump_sys_select(s1 + 1, &rfds, NULL, NULL, &tmout);
645 1.1.2.1 bouyer switch(rv1) {
646 1.1.2.1 bouyer case -1:
647 1.1.2.1 bouyer atf_tc_fail_errno("select");
648 1.1.2.1 bouyer break;
649 1.1.2.1 bouyer case 0:
650 1.1.2.1 bouyer /* timeout: expected case */
651 1.1.2.1 bouyer return;
652 1.1.2.1 bouyer default: break;
653 1.1.2.1 bouyer }
654 1.1.2.1 bouyer salen = sizeof(sa);
655 1.1.2.1 bouyer ATF_CHECK_MSG(FD_ISSET(s1, &rfds), "select returns but s1 not in set");
656 1.1.2.1 bouyer if (( rv1 = rump_sys_recvfrom(s1, &cf_receive1, sizeof(cf_receive1),
657 1.1.2.1 bouyer 0, (struct sockaddr *)&sa, &salen)) < 0) {
658 1.1.2.1 bouyer atf_tc_fail_errno("recvfrom");
659 1.1.2.1 bouyer }
660 1.1.2.1 bouyer
661 1.1.2.1 bouyer ATF_CHECK_MSG(rv1 > 0, "short read on socket");
662 1.1.2.1 bouyer
663 1.1.2.1 bouyer ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive1, sizeof(cf_send)) == 0,
664 1.1.2.1 bouyer "recvfrom (1) packet is not what we sent");
665 1.1.2.1 bouyer ATF_CHECK_MSG(sa.can_family == AF_CAN,
666 1.1.2.1 bouyer "recvfrom provided wrong %d family", sa.can_family);
667 1.1.2.1 bouyer ATF_CHECK_MSG(salen == sizeof(sa),
668 1.1.2.1 bouyer "recvfrom provided wrong size %d (!= %d)", salen, sizeof(sa));
669 1.1.2.1 bouyer ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
670 1.1.2.1 bouyer "recvfrom provided wrong ifindex %d (!= %d)",
671 1.1.2.1 bouyer sa.can_ifindex, ifr.ifr_ifindex);
672 1.1.2.1 bouyer atf_tc_fail("we got message from other interface");
673 1.1.2.1 bouyer }
674 1.1.2.1 bouyer
675 1.1.2.1 bouyer ATF_TP_ADD_TCS(tp)
676 1.1.2.1 bouyer {
677 1.1.2.1 bouyer
678 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, canlocreate);
679 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, canwritelo);
680 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, canwriteunbound);
681 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, cansendtolo);
682 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, cansendtowrite);
683 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, canreadlocal);
684 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, canrecvfrom);
685 1.1.2.1 bouyer ATF_TP_ADD_TC(tp, canbindfilter);
686 1.1.2.1 bouyer return atf_no_error();
687 1.1.2.1 bouyer }
688 1.1.2.1 bouyer
689