Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/bouyer-socketcan]: src/tests/net Basic tests for our SocketCAN implement...



details:   https://anonhg.NetBSD.org/src/rev/33f3a9b64328
branches:  bouyer-socketcan
changeset: 820805:33f3a9b64328
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Sun Jan 15 20:29:01 2017 +0000

description:
Basic tests for our SocketCAN implementation (using rump)

diffstat:

 tests/net/Makefile     |    7 +-
 tests/net/can/Makefile |   17 +
 tests/net/can/t_can.c  |  688 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 709 insertions(+), 3 deletions(-)

diffs (truncated from 734 to 300 lines):

diff -r 73862f0fb509 -r 33f3a9b64328 tests/net/Makefile
--- a/tests/net/Makefile        Sun Jan 15 20:27:33 2017 +0000
+++ b/tests/net/Makefile        Sun Jan 15 20:29:01 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.30 2016/11/26 03:19:48 ozaki-r Exp $
+# $NetBSD: Makefile,v 1.30.2.1 2017/01/15 20:29:01 bouyer Exp $
 
 .include <bsd.own.mk>
 
@@ -6,8 +6,9 @@
 
 TESTS_SUBDIRS=         fdpass in_cksum net sys
 .if (${MKRUMP} != "no") && !defined(BSD_MK_COMPAT_FILE)
-TESTS_SUBDIRS+=                arp bpf bpfilter carp icmp if if_bridge if_gif if_loop
-TESTS_SUBDIRS+=                if_pppoe if_tap if_tun mcast mpls ndp npf route if_vlan
+TESTS_SUBDIRS+=                arp bpf bpfilter can carp icmp if
+TESTS_SUBDIRS+=                if_bridge if_gif if_loop if_pppoe if_tap if_tun
+TESTS_SUBDIRS+=                mcast mpls ndp npf route if_vlan
 .if (${MKSLJIT} != "no")
 TESTS_SUBDIRS+=                bpfjit
 .endif
diff -r 73862f0fb509 -r 33f3a9b64328 tests/net/can/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/can/Makefile    Sun Jan 15 20:29:01 2017 +0000
@@ -0,0 +1,17 @@
+# $NetBSD: Makefile,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $
+#
+
+.include <bsd.own.mk>
+
+TESTSDIR=      ${TESTSBASE}/net/can
+
+TESTS_C=       t_can
+
+# XXX we don't use INET here, but we need rumpnet_netinet anyway:
+# common code in if.c is compiled with -DINET and will dereference ip_pktq,
+# which is NULL if rumpnet_netinet is not inclued.
+# 
+LDADD+=                -lrumpnet_netcan -lrumpnet_netinet -lrumpnet_net -lrumpnet
+LDADD+=                -lrump -lrumpuser -lrump -lpthread -lrumpdev -lrumpvfs
+
+.include <bsd.test.mk>
diff -r 73862f0fb509 -r 33f3a9b64328 tests/net/can/t_can.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/can/t_can.c     Sun Jan 15 20:29:01 2017 +0000
@@ -0,0 +1,688 @@
+/*     $NetBSD: t_can.c,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $     */
+
+/*-
+ * Copyright (c) 2017 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Manuel Bouyer
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
+ * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: t_can.c,v 1.1.2.1 2017/01/15 20:29:01 bouyer Exp $");
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+#include <sys/sockio.h>
+
+#include <atf-c.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include <net/if.h>
+#include <netcan/can.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include "h_macros.h"
+
+static void
+cancfg_rump_createif(const char *ifname)
+{
+       int s, rv;
+       struct ifreq ifr;
+
+       s = -1;
+       if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
+               atf_tc_fail_errno("if config socket");
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+       if ((rv = rump_sys_ioctl(s, SIOCIFCREATE, &ifr)) < 0) {
+               atf_tc_fail_errno("if config create");
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+       if ((rv = rump_sys_ioctl(s, SIOCGIFFLAGS, &ifr)) < 0) {
+               atf_tc_fail_errno("if config get flags");
+       }
+
+       ifr.ifr_flags |= IFF_UP;
+       if ((rv = rump_sys_ioctl(s, SIOCSIFFLAGS, &ifr)) < 0) {
+               atf_tc_fail_errno("if config set flags");
+       }
+}
+
+ATF_TC(canlocreate);
+ATF_TC_HEAD(canlocreate, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "check CAN loopback create/destroy");
+       atf_tc_set_md_var(tc, "timeout", "5");
+}
+
+ATF_TC_BODY(canlocreate, tc)
+{
+       const char ifname[] = "canlo0";
+       int s, rv;
+       struct ifreq ifr;
+
+       rump_init();
+       cancfg_rump_createif(ifname);
+
+       s = -1;
+       if ((s = rump_sys_socket(AF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
+               atf_tc_fail_errno("if config socket(2)");
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+
+       if ((rv = rump_sys_ioctl(s, SIOCIFDESTROY, &ifr)) < 0) {
+               atf_tc_fail_errno("if config destroy");
+       }
+}
+
+ATF_TC(canwritelo);
+ATF_TC_HEAD(canwritelo, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "check that CAN sockets gets its own message via write");
+       atf_tc_set_md_var(tc, "timeout", "5");
+}
+
+ATF_TC_BODY(canwritelo, tc)
+{
+       const char ifname[] = "canlo0";
+       int s, rv;
+       struct sockaddr_can sa;
+       struct ifreq ifr;
+       struct can_frame cf_send, cf_receive;
+
+       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 ((rv = 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 ((rv = rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa))) < 0) {
+               atf_tc_fail_errno("bind");
+       }
+
+       /*
+        * 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");
+       }
+
+       memset(&cf_receive, 0, sizeof(cf_receive));
+       if (( rv = rump_sys_read(s, &cf_receive, sizeof(cf_receive))) < 0) {
+               atf_tc_fail_errno("read");
+       }
+
+       ATF_CHECK_MSG(rv > 0, "short read on socket");
+
+       memset(&cf_send, 0, sizeof(cf_send));
+       cf_send.can_id = 1;
+       cf_send.can_dlc = 1;
+       cf_send.data[0] = 0xde;
+       /* other data[] are expected to be 0 */
+
+       ATF_CHECK_MSG(memcmp(&cf_send, &cf_receive, sizeof(cf_send)) == 0,
+           "received packet is not what we sent");
+}
+
+ATF_TC(canwriteunbound);
+ATF_TC_HEAD(canwriteunbound, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "check that write to unbound CAN sockets fails");
+       atf_tc_set_md_var(tc, "timeout", "5");
+}
+
+ATF_TC_BODY(canwriteunbound, tc)
+{
+       const char ifname[] = "canlo0";
+       int s, rv;
+       struct sockaddr_can sa;
+       struct can_frame cf_send;
+
+       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");
+       }
+
+       /*
+        * send a single byte message.
+        * not 0.
+        */
+
+       memset(&cf_send, 0, sizeof(cf_send));
+       cf_send.can_id = 1;
+       cf_send.can_dlc = 1;
+       cf_send.data[0] = 0xde;
+
+       rv = rump_sys_write(s, &cf_send, sizeof(cf_send) - 7);
+       ATF_CHECK_MSG(rv < 0 && errno == EDESTADDRREQ, 
+           "write to unbound socket didn't fail");
+}
+
+ATF_TC(cansendtolo);
+ATF_TC_HEAD(cansendtolo, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "check that CAN sockets gets its own message via sendto");
+       atf_tc_set_md_var(tc, "timeout", "5");
+}
+
+ATF_TC_BODY(cansendtolo, tc)
+{
+       const char ifname[] = "canlo0";
+       int s, rv;
+       struct sockaddr_can sa;
+       struct ifreq ifr;
+       struct can_frame cf_send, cf_receive;
+
+       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");
+       }



Home | Main Index | Thread Index | Old Index