Add basic tests for ifconf (SIOCGIFCONF)

This commit is contained in:
ozaki-r 2014-12-08 04:23:03 +00:00
parent 7e80a07c88
commit b5aa5c8930
5 changed files with 233 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <bsd.own.mk>
@ -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 <bsd.test.mk>

134
tests/net/if/ifconf.c Normal file
View File

@ -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 <sys/cdefs.h>
__RCSID("$NetBSD: ifconf.c,v 1.1 2014/12/08 04:23:03 ozaki-r Exp $");
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
static void
help(void)
{
fprintf(stderr, "usage:\n\t%s total\n\t%s list [<nifreqs>]\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;
}

86
tests/net/if/t_ifconf.sh Normal file
View File

@ -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
}