Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/bouyer-socketcan]: src/tests/net/can Adapt to CAN_RAW_RECV_OWN_MSGS bein...
details: https://anonhg.NetBSD.org/src/rev/a860ec260e11
branches: bouyer-socketcan
changeset: 820809:a860ec260e11
user: bouyer <bouyer%NetBSD.org@localhost>
date: Mon Jan 16 18:04:27 2017 +0000
description:
Adapt to CAN_RAW_RECV_OWN_MSGS being off by default, and test
CAN_RAW_RECV_OWN_MSGS and CAN_RAW_LOOPBACK options.
diffstat:
tests/net/can/t_can.c | 351 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 344 insertions(+), 7 deletions(-)
diffs (truncated from 498 to 300 lines):
diff -r cf36359cdadb -r a860ec260e11 tests/net/can/t_can.c
--- a/tests/net/can/t_can.c Mon Jan 16 18:03:38 2017 +0000
+++ b/tests/net/can/t_can.c Mon Jan 16 18:04:27 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_can.c,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $ */
+/* $NetBSD: t_can.c,v 1.1.2.2 2017/01/16 18:04:27 bouyer Exp $ */
/*-
* Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: t_can.c,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $");
+__RCSID("$NetBSD: t_can.c,v 1.1.2.2 2017/01/16 18:04:27 bouyer Exp $");
#endif /* not lint */
#include <sys/types.h>
@@ -40,6 +40,7 @@
#include <sys/sysctl.h>
#include <sys/wait.h>
#include <sys/sockio.h>
+#include <sys/param.h>
#include <atf-c.h>
#include <assert.h>
@@ -119,6 +120,121 @@
}
}
+ATF_TC(cannoown);
+ATF_TC_HEAD(cannoown, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "check that CAN sockets don't gets its own message");
+ atf_tc_set_md_var(tc, "timeout", "5");
+}
+
+ATF_TC_BODY(cannoown, tc)
+{
+ const char ifname[] = "canlo0";
+ int s, rv, v, vlen;
+ struct sockaddr_can sa;
+ int salen;
+ struct ifreq ifr;
+ struct can_frame cf_send, cf_receive;
+ fd_set rfds;
+ struct timeval tmout;
+
+ rump_init();
+ cancfg_rump_createif(ifname);
+
+ s = -1;
+ if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
+ atf_tc_fail_errno("CAN socket");
+ }
+
+ strcpy(ifr.ifr_name, ifname );
+ if (rump_sys_ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+ atf_tc_fail_errno("SIOCGIFINDEX");
+ }
+ ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
+ ifname, ifr.ifr_ifindex);
+
+ sa.can_family = AF_CAN;
+ sa.can_ifindex = ifr.ifr_ifindex;
+
+ if (rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
+ atf_tc_fail_errno("bind");
+ }
+
+ /* check sockopt CAN_RAW_LOOPBACK */
+ vlen = sizeof(v);
+ if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
+ &v, &vlen) < 0) {
+ atf_tc_fail_errno("getsockopt(CAN_RAW_LOOPBACK)");
+ }
+ ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_LOOPBACK) returns wrong len %d", vlen);
+ ATF_CHECK_MSG(v == 1, "CAN_RAW_LOOPBACK is not on by default");
+
+ /* check sockopt CAN_RAW_RECV_OWN_MSGS, and set it */
+ vlen = sizeof(v);
+ if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, &vlen) < 0) {
+ atf_tc_fail_errno("getsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
+ ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_RECV_OWN_MSGS) returns wrong len %d", vlen);
+ ATF_CHECK_MSG(v == 0, "CAN_RAW_RECV_OWN_MSGS is not off by default");
+
+ /*
+ * send a single byte message, but make sure remaining payload is
+ * not 0.
+ */
+
+ memset(&cf_send, 0, sizeof(cf_send));
+ cf_send.can_id = 1;
+ cf_send.can_dlc = 1;
+ cf_send.data[0] = 0xde;
+ cf_send.data[1] = 0xad;
+ cf_send.data[2] = 0xbe;
+ cf_send.data[3] = 0xef;
+
+ if (rump_sys_write(s, &cf_send, sizeof(cf_send) - 7) < 0) {
+ atf_tc_fail_errno("write");
+ }
+
+ /* now try to read */
+
+ memset(&cf_receive, 0, sizeof(cf_receive));
+ FD_ZERO(&rfds);
+ FD_SET(s, &rfds);
+ /* we should receive no message; wait for 2 seconds */
+ tmout.tv_sec = 2;
+ tmout.tv_usec = 0;
+ rv = rump_sys_select(s + 1, &rfds, NULL, NULL, &tmout);
+ switch(rv) {
+ case -1:
+ atf_tc_fail_errno("select");
+ break;
+ case 0:
+ /* timeout: expected case */
+ return;
+ default: break;
+ }
+ salen = sizeof(sa);
+ ATF_CHECK_MSG(FD_ISSET(s, &rfds), "select returns but s not in set");
+ if (( rv = rump_sys_recvfrom(s, &cf_receive, sizeof(cf_receive),
+ 0, (struct sockaddr *)&sa, &salen)) < 0) {
+ atf_tc_fail_errno("recvfrom");
+ }
+
+ ATF_CHECK_MSG(rv > 0, "short read on socket");
+
+ ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
+ "recvfrom packet is not what we sent");
+ ATF_CHECK_MSG(sa.can_family == AF_CAN,
+ "recvfrom provided wrong %d family", sa.can_family);
+ ATF_CHECK_MSG(salen == sizeof(sa),
+ "recvfrom provided wrong size %d (!= %d)", salen, sizeof(sa));
+ ATF_CHECK_MSG(sa.can_ifindex == ifr.ifr_ifindex,
+ "recvfrom provided wrong ifindex %d (!= %d)",
+ sa.can_ifindex, ifr.ifr_ifindex);
+ atf_tc_fail("we got our own message");
+}
+
ATF_TC(canwritelo);
ATF_TC_HEAD(canwritelo, tc)
{
@@ -130,7 +246,7 @@
ATF_TC_BODY(canwritelo, tc)
{
const char ifname[] = "canlo0";
- int s, rv;
+ int s, rv, v, vlen;
struct sockaddr_can sa;
struct ifreq ifr;
struct can_frame cf_send, cf_receive;
@@ -144,7 +260,7 @@
}
strcpy(ifr.ifr_name, ifname );
- if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
+ if (rump_sys_ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
atf_tc_fail_errno("SIOCGIFINDEX");
}
ATF_CHECK_MSG(ifr.ifr_ifindex > 0, "%s index is %d (not > 0)",
@@ -153,10 +269,40 @@
sa.can_family = AF_CAN;
sa.can_ifindex = ifr.ifr_ifindex;
- if ((rv = rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
+ if (rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
atf_tc_fail_errno("bind");
}
+ /* check sockopt CAN_RAW_LOOPBACK */
+ vlen = sizeof(v);
+ if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
+ &v, &vlen) < 0) {
+ atf_tc_fail_errno("getsockopt(CAN_RAW_LOOPBACK)");
+ }
+ ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_LOOPBACK) returns wrong len %d", vlen);
+ ATF_CHECK_MSG(v == 1, "CAN_RAW_LOOPBACK is not on by default");
+
+ /* check sockopt CAN_RAW_RECV_OWN_MSGS, and set it */
+ vlen = sizeof(v);
+ if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, &vlen) < 0) {
+ atf_tc_fail_errno("getsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
+ ATF_CHECK_MSG(vlen == sizeof(v), "getsockopt(CAN_RAW_RECV_OWN_MSGS) returns wrong len %d", vlen);
+ ATF_CHECK_MSG(v == 0, "CAN_RAW_RECV_OWN_MSGS is not off by default");
+ v = 1;
+ if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, sizeof(v)) < 0) {
+ atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
+ /* check sockopt CAN_RAW_RECV_OWN_MSGS again */
+ vlen = sizeof(v);
+ if (rump_sys_getsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, &vlen) < 0) {
+ atf_tc_fail_errno("getsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
+ ATF_CHECK_MSG(v == 1, "CAN_RAW_RECV_OWN_MSGS is not on");
+
/*
* send a single byte message, but make sure remaining payload is
* not 0.
@@ -240,7 +386,7 @@
ATF_TC_BODY(cansendtolo, tc)
{
const char ifname[] = "canlo0";
- int s, rv;
+ int s, v, rv;
struct sockaddr_can sa;
struct ifreq ifr;
struct can_frame cf_send, cf_receive;
@@ -253,6 +399,12 @@
atf_tc_fail_errno("CAN socket");
}
+ v = 1;
+ if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, sizeof(v)) < 0) {
+ atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
+
strcpy(ifr.ifr_name, ifname );
if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
atf_tc_fail_errno("SIOCGIFINDEX");
@@ -309,7 +461,7 @@
ATF_TC_BODY(cansendtowrite, tc)
{
const char ifname[] = "canlo0";
- int s, rv;
+ int s, rv, v;
struct sockaddr_can sa;
struct ifreq ifr;
struct can_frame cf_send, cf_receive;
@@ -321,6 +473,11 @@
if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
atf_tc_fail_errno("CAN socket");
}
+ v = 1;
+ if (rump_sys_setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, sizeof(v)) < 0) {
+ atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
strcpy(ifr.ifr_name, ifname );
if ((rv = rump_sys_ioctl(s, SIOCGIFINDEX, &ifr)) < 0) {
@@ -381,6 +538,7 @@
const char ifname[] = "canlo0";
int s1, rv1;
int s2, rv2;
+ int v;
struct sockaddr_can sa;
struct ifreq ifr;
struct can_frame cf_send, cf_receive1, cf_receive2;
@@ -408,6 +566,11 @@
if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
atf_tc_fail_errno("CAN socket");
}
+ v = 1;
+ if (rump_sys_setsockopt(s2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, sizeof(v)) < 0) {
+ atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
strcpy(ifr.ifr_name, ifname );
if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
@@ -467,6 +630,7 @@
const char ifname[] = "canlo0";
int s1, rv1;
int s2, rv2;
+ int v;
struct sockaddr_can sa;
struct ifreq ifr;
struct can_frame cf_send, cf_receive1, cf_receive2;
@@ -495,6 +659,11 @@
if ((s2 = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
atf_tc_fail_errno("CAN socket");
}
+ v = 1;
+ if (rump_sys_setsockopt(s2, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
+ &v, sizeof(v)) < 0) {
+ atf_tc_fail_errno("setsockopt(CAN_RAW_RECV_OWN_MSGS)");
+ }
strcpy(ifr.ifr_name, ifname );
if ((rv2 = rump_sys_ioctl(s2, SIOCGIFINDEX, &ifr)) < 0) {
@@ -560,6 +729,7 @@
const char ifname2[] = "canlo1";
int s1, rv1;
int s2, rv2;
+ int v;
struct sockaddr_can sa;
struct ifreq ifr;
struct can_frame cf_send, cf_receive1, cf_receive2;
@@ -584,6 +754,11 @@
Home |
Main Index |
Thread Index |
Old Index