From b5aa5c893074b7c070f4ce9069ec3b3ac8e7a7c1 Mon Sep 17 00:00:00 2001 From: ozaki-r Date: Mon, 8 Dec 2014 04:23:03 +0000 Subject: [PATCH] Add basic tests for ifconf (SIOCGIFCONF) --- distrib/sets/lists/debug/mi | 3 +- distrib/sets/lists/tests/mi | 4 +- tests/net/if/Makefile | 11 ++- tests/net/if/ifconf.c | 134 ++++++++++++++++++++++++++++++++++++ tests/net/if/t_ifconf.sh | 86 +++++++++++++++++++++++ 5 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 tests/net/if/ifconf.c create mode 100644 tests/net/if/t_ifconf.sh diff --git a/distrib/sets/lists/debug/mi b/distrib/sets/lists/debug/mi index 57b03ff1d839..adaaead3eb4a 100644 --- a/distrib/sets/lists/debug/mi +++ b/distrib/sets/lists/debug/mi @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.95 2014/12/04 04:35:13 htodd Exp $ +# $NetBSD: mi,v 1.96 2014/12/08 04:23:03 ozaki-r Exp $ ./etc/mtree/set.debug comp-sys-root ./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib @@ -2154,6 +2154,7 @@ ./usr/libdata/debug/usr/tests/net/fdpass/fdpass64.debug tests-net-debug debug,atf ./usr/libdata/debug/usr/tests/net/icmp/t_forward.debug tests-net-debug debug,atf,rump ./usr/libdata/debug/usr/tests/net/icmp/t_ping.debug tests-net-debug debug,atf,rump +./usr/libdata/debug/usr/tests/net/if/ifconf.debug tests-net-debug debug,atf,rump ./usr/libdata/debug/usr/tests/net/if/t_compat.debug tests-net-debug debug,atf,rump ./usr/libdata/debug/usr/tests/net/if_loop/t_pr.debug tests-net-debug debug,atf,rump ./usr/libdata/debug/usr/tests/net/mcast/t_mcast.debug tests-net-debug debug,atf diff --git a/distrib/sets/lists/tests/mi b/distrib/sets/lists/tests/mi index 38860d6b7c93..5e4b5614e7e8 100644 --- a/distrib/sets/lists/tests/mi +++ b/distrib/sets/lists/tests/mi @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.602 2014/12/02 19:56:17 christos Exp $ +# $NetBSD: mi,v 1.603 2014/12/08 04:23:03 ozaki-r Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -3125,7 +3125,9 @@ ./usr/tests/net/if tests-net-tests ./usr/tests/net/if/Atffile tests-net-tests atf,rump ./usr/tests/net/if/Kyuafile tests-net-tests atf,rump,kyua +./usr/tests/net/if/ifconf tests-net-tests atf,rump ./usr/tests/net/if/t_compat tests-net-tests atf,rump +./usr/tests/net/if/t_ifconf tests-net-tests atf,rump ./usr/tests/net/if_bridge tests-net-tests ./usr/tests/net/if_bridge/Atffile tests-net-tests atf,rump ./usr/tests/net/if_bridge/Kyuafile tests-net-tests atf,rump,kyua diff --git a/tests/net/if/Makefile b/tests/net/if/Makefile index 639ce363d721..2d324badcf42 100644 --- a/tests/net/if/Makefile +++ b/tests/net/if/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.2 2014/06/10 04:28:40 he Exp $ +# $NetBSD: Makefile,v 1.3 2014/12/08 04:23:03 ozaki-r Exp $ # .include @@ -6,8 +6,13 @@ TESTSDIR= ${TESTSBASE}/net/if TESTS_C= t_compat +TESTS_SH= t_ifconf -LDADD+= -lrumpnet_shmif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump -LDADD+= -lrumpuser -lrump -lpthread +PROGS= ifconf +MAN.ifconf= # empty +BINDIR.ifconf= ${TESTSDIR} + +LDADD.t_compat= -lrumpnet_shmif -lrumpnet_netinet -lrumpnet_net -lrumpnet -lrump +LDADD.t_compat= -lrumpuser -lrump -lpthread .include diff --git a/tests/net/if/ifconf.c b/tests/net/if/ifconf.c new file mode 100644 index 000000000000..424c5e82a2ef --- /dev/null +++ b/tests/net/if/ifconf.c @@ -0,0 +1,134 @@ +/* $NetBSD: ifconf.c,v 1.1 2014/12/08 04:23:03 ozaki-r Exp $ */ +/* + * Copyright (c) 2014 The NetBSD Foundation, Inc. + * All rights reserved. + * + * 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 AUTHOR ``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 AUTHOR 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 +__RCSID("$NetBSD: ifconf.c,v 1.1 2014/12/08 04:23:03 ozaki-r Exp $"); +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +static void +help(void) +{ + fprintf(stderr, "usage:\n\t%s total\n\t%s list []\n", + getprogname(), getprogname()); + exit(EXIT_FAILURE); +} + +static int +get_number_of_entries(void) +{ + int fd, r; + struct ifconf ifc; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd == -1) + err(EXIT_FAILURE, "socket"); + + ifc.ifc_len = 0; + ifc.ifc_buf = NULL; + + r = ioctl(fd, SIOCGIFCONF, &ifc); + if (r == -1) + err(EXIT_FAILURE, "ioctl"); + + close(fd); + + return ifc.ifc_len / sizeof(struct ifreq); +} + +static void +show_number_of_entries(void) +{ + printf("%d\n", get_number_of_entries()); +} + +static void +show_interfaces(int nifreqs) +{ + int i, fd, r; + struct ifconf ifc; + struct ifreq *ifreqs; + + if (nifreqs == 0) + nifreqs = get_number_of_entries(); + + if (nifreqs <= 0) + errx(EXIT_FAILURE, "nifreqs=%d", nifreqs); + + ifreqs = malloc(sizeof(struct ifreq) * nifreqs); + if (ifreqs == NULL) + err(EXIT_FAILURE, "malloc(sizeof(ifreq) * %d)", nifreqs); + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd == -1) + err(EXIT_FAILURE, "socket"); + + ifc.ifc_len = sizeof(struct ifreq) * nifreqs; + ifc.ifc_req = ifreqs; + + r = ioctl(fd, SIOCGIFCONF, &ifc); + if (r == -1) + err(EXIT_FAILURE, "ioctl"); + close(fd); + + for (i=0; i < (int)(ifc.ifc_len / sizeof(struct ifreq)); i++) { + printf("%s: af=%hhu socklen=%hhu\n", ifreqs[i].ifr_name, + ifreqs[i].ifr_addr.sa_family, ifreqs[i].ifr_addr.sa_len); + } + + free(ifreqs); +} + +int +main(int argc, char *argv[]) +{ + if (argc < 2) + help(); + + if (strcmp(argv[1], "total") == 0) { + show_number_of_entries(); + } else if (strcmp(argv[1], "list") == 0) { + if (argc == 2) + show_interfaces(0); + else if (argc == 3) + show_interfaces(atoi(argv[2])); + else + help(); + } else + help(); + + return EXIT_SUCCESS; +} diff --git a/tests/net/if/t_ifconf.sh b/tests/net/if/t_ifconf.sh new file mode 100644 index 000000000000..d7b860b1dae5 --- /dev/null +++ b/tests/net/if/t_ifconf.sh @@ -0,0 +1,86 @@ +# $NetBSD: t_ifconf.sh,v 1.1 2014/12/08 04:23:03 ozaki-r Exp $ +# +# Copyright (c) 2014 The NetBSD Foundation, Inc. +# All rights reserved. +# +# 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. +# + +RUMP_SERVER1=unix://./r1 + +RUMP_FLAGS=\ +"-lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif" + +atf_test_case basic cleanup +basic_head() +{ + + atf_set "descr" "basic ifconf (SIOCGIFCONF) test" + atf_set "require.progs" "rump_server" +} + +basic_body() +{ + local ifconf="$(atf_get_srcdir)/ifconf" + + atf_check -s exit:0 rump_server ${RUMP_FLAGS} ${RUMP_SERVER1} + + export RUMP_SERVER=${RUMP_SERVER1} + export LD_PRELOAD=/usr/lib/librumphijack.so + + # lo0 (127.0.0.1 and link local) + atf_check -s exit:0 -o match:'^2$' "$ifconf" total + atf_check -s exit:0 -o match:'lo0' "$ifconf" list + + # Add shmif0 (no address) + atf_check -s exit:0 rump.ifconfig shmif0 create + atf_check -s exit:0 -o match:'^3$' "$ifconf" total + atf_check -s exit:0 -o match:'shmif0' "$ifconf" list + + # Add shmif1 (no address) + atf_check -s exit:0 rump.ifconfig shmif1 create + atf_check -s exit:0 -o match:'^4$' "$ifconf" total + atf_check -s exit:0 -o match:'shmif1' "$ifconf" list + + # Add an address to shmif0 + atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus + atf_check -s exit:0 rump.ifconfig shmif0 192.168.0.1/24 + atf_check -s exit:0 -o match:'^5$' "$ifconf" total + + # Get only first two entries (lo0's) + atf_check -s exit:0 -o not-match:'shmif' "$ifconf" list 2 + + unset LD_PRELOAD + unset RUMP_SERVER +} + +basic_cleanup() +{ + + RUMP_SERVER=${RUMP_SERVER1} rump.halt +} + +atf_init_test_cases() +{ + + atf_add_test_case basic +}