diff --git a/libnozzle/tests/Makefile.am b/libnozzle/tests/Makefile.am index 2c9b754e..d677bd9b 100644 --- a/libnozzle/tests/Makefile.am +++ b/libnozzle/tests/Makefile.am @@ -1,55 +1,65 @@ # # Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk EXTRA_DIST = \ tap_updown_bad \ tap_updown_good \ api-test-coverage noinst_HEADERS = \ test-common.h if BUILD_LIBNOZZLE check_PROGRAMS = \ api_nozzle_open_test \ api_nozzle_close_test \ + api_nozzle_set_up_test \ + api_nozzle_set_down_test \ nozzle_test noinst_PROGRAMS = $(check_PROGRAMS) noinst_SCRIPTS = \ api-test-coverage TESTS = $(check_PROGRAMS) check-local: check-api-test-coverage check-api-test-coverage: chmod u+x $(top_srcdir)/libnozzle/tests/api-test-coverage $(top_srcdir)/libnozzle/tests/api-test-coverage $(top_srcdir) $(top_builddir) AM_CPPFLAGS = -I$(top_srcdir)/libnozzle -DABSBUILDDIR=\"$(abs_builddir)\" AM_CFLAGS += $(PTHREAD_CFLAGS) $(libnl_CFLAGS) LIBS += $(top_builddir)/libnozzle/libnozzle.la $(PTHREAD_LIBS) $(libnl_LIBS) api_nozzle_open_test_SOURCES = api_nozzle_open.c \ test-common.c api_nozzle_close_test_SOURCES = api_nozzle_close.c \ - test-common.c + test-common.c + +api_nozzle_set_up_test_SOURCES = api_nozzle_set_up.c \ + test-common.c \ + ../internals.c + +api_nozzle_set_down_test_SOURCES = api_nozzle_set_down.c \ + test-common.c \ + ../internals.c nozzle_test_SOURCES = nozzle_test.c \ test-common.c \ ../internals.c endif diff --git a/libnozzle/tests/api_nozzle_set_down.c b/libnozzle/tests/api_nozzle_set_down.c new file mode 100644 index 00000000..da48d584 --- /dev/null +++ b/libnozzle/tests/api_nozzle_set_down.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-2.0+ + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "test-common.h" + +static int test(void) +{ + char verifycmd[1024]; + char device_name[IFNAMSIZ]; + size_t size = IFNAMSIZ; + int err=0; + nozzle_t nozzle; + char *error_string = NULL; + + printf("Testing interface down\n"); + + memset(device_name, 0, size); + nozzle = nozzle_open(device_name, size, NULL); + if (!nozzle) { + printf("Unable to init %s\n", device_name); + return -1; + } + + printf("Put the interface up\n"); + + err = nozzle_set_up(nozzle); + if (err < 0) { + printf("Unable to set interface up\n"); + err = -1; + goto out_clean; + } + + memset(verifycmd, 0, sizeof(verifycmd)); + snprintf(verifycmd, sizeof(verifycmd)-1, +#ifdef KNET_LINUX + "ip addr show dev %s | grep -q UP", nozzle->name); +#endif +#ifdef KNET_BSD + "ifconfig %s | grep -q UP", nozzle->name); +#endif + err = execute_bin_sh_command(verifycmd, &error_string); + if (error_string) { + printf("Error string: %s\n", error_string); + free(error_string); + error_string = NULL; + } + if (err < 0) { + printf("Unable to verify inteface UP\n"); + err = -1; + goto out_clean; + } + + printf("Put the interface down\n"); + + err = nozzle_set_down(nozzle); + if (err < 0) { + printf("Unable to put the interface down\n"); + err = -1; + goto out_clean; + } + + memset(verifycmd, 0, sizeof(verifycmd)); + snprintf(verifycmd, sizeof(verifycmd)-1, +#ifdef KNET_LINUX + "ip addr show dev %s | grep -q UP", nozzle->name); +#endif +#ifdef KNET_BSD + "ifconfig %s | grep -q UP", nozzle->name); +#endif + err = execute_bin_sh_command(verifycmd, &error_string); + if (error_string) { + printf("Error string: %s\n", error_string); + free(error_string); + error_string = NULL; + } + if (!err) { + printf("Unable to verify inteface DOWN\n"); + err = -1; + goto out_clean; + } + + printf("Pass NULL to nozzle set_down\n"); + errno = 0; + if ((nozzle_set_down(NULL) >= 0) || (errno != EINVAL)) { + printf("Something is wrong in nozzle_set_down sanity checks\n"); + err = -1; + goto out_clean; + } + +out_clean: + nozzle_close(nozzle); + + return err; +} + +int main(void) +{ + need_root(); + + if (test() < 0) + return FAIL; + + return PASS; +} diff --git a/libnozzle/tests/api_nozzle_set_up.c b/libnozzle/tests/api_nozzle_set_up.c new file mode 100644 index 00000000..7c8f81f1 --- /dev/null +++ b/libnozzle/tests/api_nozzle_set_up.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-2.0+ + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test-common.h" + +static int test(void) +{ + char verifycmd[1024]; + char device_name[IFNAMSIZ]; + size_t size = IFNAMSIZ; + int err=0; + nozzle_t nozzle; + char *error_string = NULL; + + printf("Testing interface up/down\n"); + + memset(device_name, 0, size); + nozzle = nozzle_open(device_name, size, NULL); + if (!nozzle) { + printf("Unable to init %s\n", device_name); + return -1; + } + + printf("Put the interface up\n"); + + err = nozzle_set_up(nozzle); + if (err < 0) { + printf("Unable to set interface up\n"); + err = -1; + goto out_clean; + } + + memset(verifycmd, 0, sizeof(verifycmd)); + snprintf(verifycmd, sizeof(verifycmd)-1, +#ifdef KNET_LINUX + "ip addr show dev %s | grep -q UP", nozzle->name); +#endif +#ifdef KNET_BSD + "ifconfig %s | grep -q UP", nozzle->name); +#endif + err = execute_bin_sh_command(verifycmd, &error_string); + if (error_string) { + printf("Error string: %s\n", error_string); + free(error_string); + error_string = NULL; + } + if (err < 0) { + printf("Unable to verify inteface UP\n"); + err = -1; + goto out_clean; + } + + printf("Test ERROR conditions\n"); + + printf("Try to UP the same interface twice\n"); + err = nozzle_set_up(nozzle); + if (err < 0) { + printf("Interface was already UP, spurious error received from nozzle_set_up"); + err = -1; + goto out_clean; + } + + printf("Pass NULL to nozzle set_up\n"); + err = 0; + errno = 0; + if ((nozzle_set_up(NULL) >= 0) || (errno != EINVAL)) { + printf("Something is wrong in nozzle_set_up sanity checks\n"); + err = -1; + goto out_clean; + } + +out_clean: + nozzle_set_down(nozzle); + nozzle_close(nozzle); + + return err; +} + +int main(void) +{ + need_root(); + + if (test() < 0) + return FAIL; + + return PASS; +}