diff --git a/libnozzle/tests/Makefile.am b/libnozzle/tests/Makefile.am index 64af8388b..ba050002b 100644 --- a/libnozzle/tests/Makefile.am +++ b/libnozzle/tests/Makefile.am @@ -32,7 +32,8 @@ api_checks = \ 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_run_updown_test \ + api_nozzle_add_ip_test int_checks = \ int_execute_bin_sh_command_test \ @@ -106,6 +107,10 @@ 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 + int_execute_bin_sh_command_test_SOURCES = int_execute_bin_sh_command.c \ test-common.c \ ../internals.c diff --git a/libnozzle/tests/api_nozzle_add_ip.c b/libnozzle/tests/api_nozzle_add_ip.c new file mode 100644 index 000000000..af8c5f03e --- /dev/null +++ b/libnozzle/tests/api_nozzle_add_ip.c @@ -0,0 +1,293 @@ +/* + * 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; + char verifycmd[2048]; + int err = 0; + nozzle_t nozzle; + char *error_string = NULL; + + printf("Testing interface add 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("Testing error conditions\n"); + + printf("Testing invalid nozzle handle\n"); + err = nozzle_add_ip(NULL, testipv4_1, "24"); + if ((!err) || (errno != EINVAL)) { + printf("nozzle_add_ip accepted invalid nozzle handle\n"); + err = -1; + goto out_clean; + } + + printf("Testing empty ip address\n"); + err = nozzle_add_ip(nozzle, NULL, "24"); + if ((!err) || (errno != EINVAL)) { + printf("nozzle_add_ip accepted invalid ip address\n"); + err = -1; + goto out_clean; + } + + + printf("Testing empty netmask\n"); + err = nozzle_add_ip(nozzle, testipv4_1, NULL); + if ((!err) || (errno != EINVAL)) { + printf("nozzle_add_ip accepted invalid netmask\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 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("Checking ip: %s/24\n", testipv4_2); + + 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_2); +#endif +#ifdef KNET_BSD + "ifconfig %s | grep -q %s", nozzle->name, testipv4_2); +#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/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("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; + } + + printf("Testing adding an IPv6 address with mtu < 1280 and restore\n"); + printf("Lowering interface MTU\n"); + + err = nozzle_set_mtu(nozzle, 1200); + if (err) { + printf("Unable to set MTU to 1200\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("Resetting MTU\n"); + + err = nozzle_reset_mtu(nozzle); + if (err) { + printf("Unable to set reset MTU\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; + } + +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; +}