diff --git a/libnozzle/tests/Makefile.am b/libnozzle/tests/Makefile.am index 7c8b256e..9a732d52 100644 --- a/libnozzle/tests/Makefile.am +++ b/libnozzle/tests/Makefile.am @@ -1,127 +1,125 @@ # # 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 = \ nozzle_run_updown_exit_true \ nozzle_run_updown_exit_false \ api-test-coverage noinst_HEADERS = \ test-common.h if BUILD_LIBNOZZLE api_checks = \ api_nozzle_open_test \ api_nozzle_close_test \ api_nozzle_set_up_test \ api_nozzle_set_down_test \ api_nozzle_get_mtu_test \ api_nozzle_set_mtu_test \ api_nozzle_get_mac_test \ api_nozzle_set_mac_test \ api_nozzle_get_handle_by_name_test \ api_nozzle_get_name_by_handle_test \ api_nozzle_get_fd_test \ api_nozzle_run_updown_test \ api_nozzle_add_ip_test \ - api_nozzle_del_ip_test + api_nozzle_del_ip_test \ + api_nozzle_get_ips_test int_checks = \ - int_execute_bin_sh_command_test \ - nozzle_test + int_execute_bin_sh_command_test fun_checks = benchmarks = check_PROGRAMS = \ $(api_checks) \ $(int_checks) \ $(fun_checks) noinst_PROGRAMS = \ $(benchmarks) \ $(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)\" -DABSSRCDIR=\"$(abs_srcdir)\" 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 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 api_nozzle_get_mtu_test_SOURCES = api_nozzle_get_mtu.c \ test-common.c api_nozzle_set_mtu_test_SOURCES = api_nozzle_set_mtu.c \ test-common.c \ ../internals.c api_nozzle_get_mac_test_SOURCES = api_nozzle_get_mac.c \ test-common.c api_nozzle_set_mac_test_SOURCES = api_nozzle_set_mac.c \ test-common.c api_nozzle_get_handle_by_name_test_SOURCES = api_nozzle_get_handle_by_name.c \ test-common.c api_nozzle_get_name_by_handle_test_SOURCES = api_nozzle_get_name_by_handle.c \ test-common.c api_nozzle_get_fd_test_SOURCES = api_nozzle_get_fd.c \ test-common.c api_nozzle_run_updown_test_SOURCES = api_nozzle_run_updown.c \ test-common.c \ ../internals.c api_nozzle_add_ip_test_SOURCES = api_nozzle_add_ip.c \ test-common.c \ ../internals.c api_nozzle_del_ip_test_SOURCES = api_nozzle_del_ip.c \ test-common.c \ ../internals.c +api_nozzle_get_ips_test_SOURCES = api_nozzle_get_ips.c \ + test-common.c + int_execute_bin_sh_command_test_SOURCES = int_execute_bin_sh_command.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_get_ips.c b/libnozzle/tests/api_nozzle_get_ips.c new file mode 100644 index 00000000..e29885e6 --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_ips.c @@ -0,0 +1,182 @@ +/* + * 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" + +char testipv4_1[IPBUFSIZE]; +char testipv4_2[IPBUFSIZE]; +char testipv6_1[IPBUFSIZE]; +char testipv6_2[IPBUFSIZE]; + +static int test(void) +{ + char device_name[IFNAMSIZ]; + size_t size = IFNAMSIZ; + int err = 0; + nozzle_t nozzle; + struct nozzle_ip *ip_list = NULL, *ip_list_tmp = NULL; + int ip_list_entries = 0, ipv4_list_entries = 0, ipv6_list_entries = 0; + + printf("Testing get ips\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("Testing error conditions\n"); + + printf("Testing invalid nozzle\n"); + err = nozzle_get_ips(NULL, &ip_list); + if ((!err) || (errno != EINVAL)) { + printf("nozzle_get_ips accepted invalid nozzle\n"); + err = -1; + goto out_clean; + } + + printf("Testing invalid ip list\n"); + err = nozzle_get_ips(nozzle, NULL); + if ((!err) || (errno != EINVAL)) { + printf("nozzle_get_ips accepted invalid ip list\n"); + err = -1; + goto out_clean; + } + + printf("Adding ip: %s/24\n", testipv4_1); + + err = nozzle_add_ip(nozzle, testipv4_1, "24"); + if (err < 0) { + printf("Unable to assign IP address\n"); + err = -1; + goto out_clean; + } + + printf("Adding ip: %s/24\n", testipv4_2); + + err = nozzle_add_ip(nozzle, testipv4_2, "24"); + if (err < 0) { + printf("Unable to assign IP address\n"); + err = -1; + goto out_clean; + } + + printf("Adding ip: %s/64\n", testipv6_1); + + err = nozzle_add_ip(nozzle, testipv6_1, "64"); + if (err < 0) { + printf("Unable to assign IP address\n"); + err = -1; + goto out_clean; + } + + printf("Adding ip: %s/64\n", testipv6_2); + + err = nozzle_add_ip(nozzle, testipv6_2, "64"); + if (err < 0) { + printf("Unable to assign IP address\n"); + err = -1; + goto out_clean; + } + + printf("Get ip list from libnozzle:\n"); + + if (nozzle_get_ips(nozzle, &ip_list) < 0) { + printf("Not enough mem?\n"); + err = -1; + goto out_clean; + } + + ip_list_tmp = ip_list; + ip_list_entries = 0; + + while(ip_list_tmp) { + ip_list_entries++; + if (ip_list_tmp->domain == AF_INET) { + ipv4_list_entries++; + } else { + ipv6_list_entries++; + } + printf("Found IP %s %s in libnozzle db\n", ip_list_tmp->ipaddr, ip_list_tmp->prefix); + ip_list_tmp = ip_list_tmp->next; + } + + if ((ip_list_entries != 4) || + (ipv4_list_entries != 2) || + (ipv6_list_entries != 2)) { + printf("Didn't get enough ip back from libnozzle?\n"); + err = -1; + goto out_clean; + } + + printf("Deleting ip: %s/24\n", testipv4_1); + + err = nozzle_del_ip(nozzle, testipv4_1, "24"); + if (err < 0) { + printf("Unable to delete IP address\n"); + err = -1; + goto out_clean; + } + + printf("Deleting ip: %s/24\n", testipv4_2); + + err = nozzle_del_ip(nozzle, testipv4_2, "24"); + if (err < 0) { + printf("Unable to delete IP address\n"); + err = -1; + goto out_clean; + } + + printf("Deleting ip: %s/64\n", testipv6_1); + + err = nozzle_del_ip(nozzle, testipv6_1, "64"); + if (err) { + printf("Unable to delete IP address\n"); + err = -1; + goto out_clean; + } + + printf("Deleting ip: %s/64\n", testipv6_2); + + err = nozzle_del_ip(nozzle, testipv6_2, "64"); + if (err) { + printf("Unable to delete IP address\n"); + err = -1; + goto out_clean; + } + +out_clean: + nozzle_close(nozzle); + + return err; +} + +int main(void) +{ + need_root(); + + make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); + + if (test() < 0) + return FAIL; + + return PASS; +} diff --git a/libnozzle/tests/nozzle_test.c b/libnozzle/tests/nozzle_test.c deleted file mode 100644 index 475035d0..00000000 --- a/libnozzle/tests/nozzle_test.c +++ /dev/null @@ -1,241 +0,0 @@ -/* - * 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" - -char testipv4_1[IPBUFSIZE]; -char testipv4_2[IPBUFSIZE]; -char testipv6_1[IPBUFSIZE]; -char testipv6_2[IPBUFSIZE]; - -static int check_knet_set_del_ip(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - char verifycmd[2048]; - int err = 0; - nozzle_t nozzle; - struct nozzle_ip *ip_list = NULL, *ip_list_tmp = NULL; - int ip_list_entries = 0; - char *error_string = NULL; - - printf("Testing interface add/remove ip\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("Adding ip: %s/24\n", testipv4_1); - - err = nozzle_add_ip(nozzle, testipv4_1, "24"); - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: %s/24\n", testipv4_2); - - err = nozzle_add_ip(nozzle, testipv4_2, "24"); - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding duplicate ip: %s/24\n", testipv4_1); - - err = nozzle_add_ip(nozzle, testipv4_1, "24"); - if (err < 0) { - printf("Unable to find IP address in libnozzle db\n"); - err=-1; - goto out_clean; - } - - printf("Checking ip: %s/24\n", testipv4_1); - - memset(verifycmd, 0, sizeof(verifycmd)); - snprintf(verifycmd, sizeof(verifycmd)-1, -#ifdef KNET_LINUX - "ip addr show dev %s | grep -q %s/24", nozzle->name, testipv4_1); -#endif -#ifdef KNET_BSD - "ifconfig %s | grep -q %s", nozzle->name, testipv4_1); -#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 IP address\n"); - err=-1; - goto out_clean; - } - - printf("Get ip list from libnozzle:\n"); - - if (nozzle_get_ips(nozzle, &ip_list) < 0) { - printf("Not enough mem?\n"); - err=-1; - goto out_clean; - } - - ip_list_tmp = ip_list; - ip_list_entries = 0; - - while(ip_list_tmp) { - ip_list_entries++; - printf("Found IP %s %s in libnozzle db\n", ip_list_tmp->ipaddr, ip_list_tmp->prefix); - ip_list_tmp = ip_list_tmp->next; - } - - if (ip_list_entries != 2) { - printf("Didn't get enough ip back from libnozzle?\n"); - err=-1; - goto out_clean; - } - - printf("Deleting ip: %s/24\n", testipv4_1); - - err = nozzle_del_ip(nozzle, testipv4_1, "24"); - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting ip: %s/24\n", testipv4_2); - - err = nozzle_del_ip(nozzle, testipv4_2, "24"); - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting again ip: %s/24\n", testipv4_1); - - err = nozzle_del_ip(nozzle, testipv4_1, "24"); - if (err < 0) { - printf("Unable to delete IP address\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 %s/24", nozzle->name, testipv4_1); -#endif -#ifdef KNET_BSD - "ifconfig %s | grep -q %s", nozzle->name, testipv4_1); -#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 IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: %s/64\n", testipv6_1); - - err = nozzle_add_ip(nozzle, testipv6_1, "64"); - if (err < 0) { - printf("Unable to assign IP address\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 %s/64", nozzle->name, testipv6_1); -#endif -#ifdef KNET_BSD - "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); -#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 IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting ip: %s/64\n", testipv6_1); - - err = nozzle_del_ip(nozzle, testipv6_1, "64"); - if (err) { - printf("Unable to delete IP address\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 %s/64", nozzle->name, testipv6_1); -#endif -#ifdef KNET_BSD - "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); -#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 IP address\n"); - err=-1; - goto out_clean; - } - -out_clean: - nozzle_close(nozzle); - - return err; -} - -int main(void) -{ - need_root(); - - make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); - - if (check_knet_set_del_ip() < 0) - return -1; - - return 0; -}