diff --git a/libnozzle/tests/api_nozzle_add_ip.c b/libnozzle/tests/api_nozzle_add_ip.c index 8491e25e..017d8f21 100644 --- a/libnozzle/tests/api_nozzle_add_ip.c +++ b/libnozzle/tests/api_nozzle_add_ip.c @@ -1,293 +1,294 @@ /* * Copyright (C) 2010-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-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(); + need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_close.c b/libnozzle/tests/api_nozzle_close.c index 27885b02..2d4e1ab8 100644 --- a/libnozzle/tests/api_nozzle_close.c +++ b/libnozzle/tests/api_nozzle_close.c @@ -1,130 +1,131 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include #include #endif #ifdef KNET_BSD #include #endif #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[2*IFNAMSIZ]; size_t size = IFNAMSIZ; nozzle_t nozzle; memset(device_name, 0, sizeof(device_name)); /* * this test is duplicated from api_nozzle_open.c */ printf("Testing random nozzle interface:\n"); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create random interface\n"); return -1; } printf("Testing ERROR conditions\n"); printf("Testing nozzle_close with NULL nozzle\n"); if ((nozzle_close(NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_close sanity checks\n"); return -1; } printf("Testing nozzle_close with random bytes nozzle pointer\n"); nozzle = (nozzle_t)0x1; if ((nozzle_close(nozzle) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_close sanity checks\n"); return -1; } return 0; } /* * requires running the test suite with valgrind */ static int check_nozzle_close_leak(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; printf("Testing close leak (needs valgrind)\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; } out_clean: nozzle_close(nozzle); return err; } int main(void) { need_root(); + need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; if (check_nozzle_close_leak() < 0) return FAIL; return 0; } diff --git a/libnozzle/tests/api_nozzle_del_ip.c b/libnozzle/tests/api_nozzle_del_ip.c index 9f67a3ee..ed1fadf9 100644 --- a/libnozzle/tests/api_nozzle_del_ip.c +++ b/libnozzle/tests/api_nozzle_del_ip.c @@ -1,266 +1,267 @@ /* * Copyright (C) 2010-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-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 del 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_del_ip(NULL, testipv4_1, "24"); if ((!err) || (errno != EINVAL)) { printf("nozzle_del_ip accepted invalid nozzle handle\n"); err = -1; goto out_clean; } printf("Testing empty ip address\n"); err = nozzle_del_ip(nozzle, NULL, "24"); if ((!err) || (errno != EINVAL)) { printf("nozzle_del_ip accepted invalid ip address\n"); err = -1; goto out_clean; } printf("Testing empty netmask\n"); err = nozzle_del_ip(nozzle, testipv4_1, NULL); if ((!err) || (errno != EINVAL)) { printf("nozzle_del_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("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("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("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("Deleting ip: %s/24 again\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("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; } printf("Testing deleting an IPv6 address with mtu < 1280 (in db, not on interface)\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("Deleting ip: %s/64 with low mtu\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(); + need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_fd.c b/libnozzle/tests/api_nozzle_get_fd.c index 6b12724b..1e8f2a11 100644 --- a/libnozzle/tests/api_nozzle_get_fd.c +++ b/libnozzle/tests/api_nozzle_get_fd.c @@ -1,77 +1,78 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; int fd; printf("Testing get fd\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; } fd = nozzle_get_fd(nozzle); if (fd < 0) { printf("Unable to get fd\n"); err = -1; goto out_clean; } if (fcntl(fd, F_GETFD) < 0) { printf("Unable to get valid fd\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Passing empty struct to get_fd\n"); if (nozzle_get_fd(NULL) > 0) { printf("Something is wrong in nozzle_get_fd sanity checks\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_handle_by_name.c b/libnozzle/tests/api_nozzle_get_handle_by_name.c index 5db12936..49b06dd1 100644 --- a/libnozzle/tests/api_nozzle_get_handle_by_name.c +++ b/libnozzle/tests/api_nozzle_get_handle_by_name.c @@ -1,85 +1,86 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" static int test(void) { char device_name[2*IFNAMSIZ]; size_t size = IFNAMSIZ; nozzle_t nozzle, nozzle_tmp; int err = 0; printf("Testing get handle by name\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; } nozzle_tmp = nozzle_get_handle_by_name(device_name); if ((!nozzle_tmp) && (errno != ENOENT)) { printf("Unable to get handle by name\n"); err = -1; goto out_clean; } if (nozzle != nozzle_tmp) { printf("get handle by name returned wrong handle!\n"); err = -1; goto out_clean; } printf("Testing error conditions\n"); printf("Testing with NULL device name\n"); nozzle_tmp = nozzle_get_handle_by_name(NULL); if ((nozzle_tmp) || (errno != EINVAL)) { printf("get handle by name returned wrong error\n"); err = -1; goto out_clean; } printf("Testing with device name longer than IFNAMSIZ\n"); nozzle_tmp = nozzle_get_handle_by_name("antanisupercazzolaunpotapioca"); if ((nozzle_tmp) || (errno != EINVAL)) { printf("get handle by name returned wrong error\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_ips.c b/libnozzle/tests/api_nozzle_get_ips.c index ff96698b..d8100dae 100644 --- a/libnozzle/tests/api_nozzle_get_ips.c +++ b/libnozzle/tests/api_nozzle_get_ips.c @@ -1,182 +1,183 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-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(); + need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_mac.c b/libnozzle/tests/api_nozzle_get_mac.c index 3c99bf5f..0e2a46f2 100644 --- a/libnozzle/tests/api_nozzle_get_mac.c +++ b/libnozzle/tests/api_nozzle_get_mac.c @@ -1,130 +1,131 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include #include #endif #ifdef KNET_BSD #include #endif #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; char *current_mac = NULL, *temp_mac = NULL, *err_mac = NULL; struct ether_addr *cur_mac, *tmp_mac; printf("Testing get MAC\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("Get current MAC\n"); if (nozzle_get_mac(nozzle, ¤t_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", current_mac); printf("Setting MAC: 00:01:01:01:01:01\n"); if (nozzle_set_mac(nozzle, "00:01:01:01:01:01") < 0) { printf("Unable to set current MAC address.\n"); err = -1; goto out_clean; } if (nozzle_get_mac(nozzle, &temp_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", temp_mac); cur_mac = ether_aton(current_mac); tmp_mac = ether_aton(temp_mac); printf("Comparing MAC addresses\n"); if (memcmp(cur_mac, tmp_mac, sizeof(struct ether_addr))) { printf("Mac addresses are not the same?!\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Pass NULL to get_mac (pass1)\n"); errno = 0; if ((nozzle_get_mac(NULL, &err_mac) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_get_mac sanity checks\n"); err = -1; goto out_clean; } printf("Pass NULL to get_mac (pass2)\n"); errno = 0; if ((nozzle_get_mac(nozzle, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_get_mac sanity checks\n"); err = -1; goto out_clean; } out_clean: if (err_mac) { printf("Something managed to set err_mac!\n"); err = -1; free(err_mac); } if (current_mac) free(current_mac); if (temp_mac) free(temp_mac); if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_mtu.c b/libnozzle/tests/api_nozzle_get_mtu.c index 9f009042..3f8ffd39 100644 --- a/libnozzle/tests/api_nozzle_get_mtu.c +++ b/libnozzle/tests/api_nozzle_get_mtu.c @@ -1,98 +1,99 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; int current_mtu = 0; int expected_mtu = 1500; printf("Testing get MTU\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("Comparing default MTU\n"); current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected default [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Setting MTU to 9000\n"); expected_mtu = 9000; if (nozzle_set_mtu(nozzle, expected_mtu) < 0) { printf("Unable to set MTU to %d\n", expected_mtu); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Passing empty struct to get_mtu\n"); if (nozzle_get_mtu(NULL) > 0) { printf("Something is wrong in nozzle_get_mtu sanity checks\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_name_by_handle.c b/libnozzle/tests/api_nozzle_get_name_by_handle.c index 942f997d..cbb1739a 100644 --- a/libnozzle/tests/api_nozzle_get_name_by_handle.c +++ b/libnozzle/tests/api_nozzle_get_name_by_handle.c @@ -1,78 +1,79 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" static int test(void) { char device_name[2*IFNAMSIZ]; const char *device_name_tmp; size_t size = IFNAMSIZ; nozzle_t nozzle; int err = 0; printf("Testing get name by handle\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; } device_name_tmp = nozzle_get_name_by_handle(nozzle); if (!device_name_tmp) { if (errno != ENOENT) { printf("Unable to get name by handle\n"); } else { printf("received incorrect errno!\n"); } err = -1; goto out_clean; } if (strcmp(device_name, device_name_tmp)) { printf("get name by handle returned different names for the same handle\n"); err = -1; goto out_clean; } printf("Testing error conditions\n"); device_name_tmp = nozzle_get_name_by_handle(NULL); if ((device_name_tmp) || (errno != ENOENT)) { printf("get name by handle returned wrong error\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_open.c b/libnozzle/tests/api_nozzle_open.c index dccaf8b4..ebf1fcb4 100644 --- a/libnozzle/tests/api_nozzle_open.c +++ b/libnozzle/tests/api_nozzle_open.c @@ -1,202 +1,203 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" static int test_multi_eth(void) { char device_name1[IFNAMSIZ]; char device_name2[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle1 = NULL; nozzle_t nozzle2 = NULL; printf("Testing multiple nozzle interface instances\n"); memset(device_name1, 0, size); memset(device_name2, 0, size); nozzle1 = nozzle_open(device_name1, size, NULL); if (!nozzle1) { printf("Unable to init %s\n", device_name1); err = -1; goto out_clean; } if (is_if_in_system(device_name1) > 0) { printf("Found interface %s on the system\n", device_name1); } else { printf("Unable to find interface %s on the system\n", device_name1); } nozzle2 = nozzle_open(device_name2, size, NULL); if (!nozzle2) { printf("Unable to init %s\n", device_name2); err = -1; goto out_clean; } if (is_if_in_system(device_name2) > 0) { printf("Found interface %s on the system\n", device_name2); } else { printf("Unable to find interface %s on the system\n", device_name2); } if (nozzle1) { nozzle_close(nozzle1); } if (nozzle2) { nozzle_close(nozzle2); } printf("Testing error conditions\n"); printf("Open same device twice\n"); memset(device_name1, 0, size); nozzle1 = nozzle_open(device_name1, size, NULL); if (!nozzle1) { printf("Unable to init %s\n", device_name1); err = -1; goto out_clean; } if (is_if_in_system(device_name1) > 0) { printf("Found interface %s on the system\n", device_name1); } else { printf("Unable to find interface %s on the system\n", device_name1); } nozzle2 = nozzle_open(device_name1, size, NULL); if (nozzle2) { printf("We were able to init 2 interfaces with the same name!\n"); err = -1; goto out_clean; } out_clean: if (nozzle1) { nozzle_close(nozzle1); } if (nozzle2) { nozzle_close(nozzle2); } return err; } static int test(void) { char device_name[2*IFNAMSIZ]; char fakepath[PATH_MAX]; size_t size = IFNAMSIZ; uint8_t randombyte = get_random_byte(); memset(device_name, 0, sizeof(device_name)); printf("Creating random nozzle interface:\n"); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create random interface\n"); return -1; } #ifdef KNET_LINUX printf("Creating kronostest%u nozzle interface:\n", randombyte); snprintf(device_name, IFNAMSIZ, "kronostest%u", randombyte); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create kronostest%u interface\n", randombyte); return -1; } #endif #ifdef KNET_BSD printf("Creating tap%u nozzle interface:\n", randombyte); snprintf(device_name, IFNAMSIZ, "tap%u", randombyte); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create tap%u interface\n", randombyte); return -1; } printf("Creating kronostest%u nozzle interface:\n", randombyte); snprintf(device_name, IFNAMSIZ, "kronostest%u", randombyte); if (test_iface(device_name, size, NULL) == 0) { printf("BSD should not accept kronostest%u interface\n", randombyte); return -1; } #endif printf("Testing ERROR conditions\n"); printf("Testing dev == NULL\n"); errno=0; if ((test_iface(NULL, size, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } printf("Testing size < IFNAMSIZ\n"); errno=0; if ((test_iface(device_name, 1, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } printf("Testing device_name size > IFNAMSIZ\n"); errno=0; strcpy(device_name, "abcdefghilmnopqrstuvwz"); if ((test_iface(device_name, IFNAMSIZ, NULL) >= 0) || (errno != E2BIG)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } printf("Testing updown path != abs\n"); errno=0; memset(device_name, 0, IFNAMSIZ); if ((test_iface(device_name, IFNAMSIZ, "foo") >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } memset(fakepath, 0, PATH_MAX); memset(fakepath, '/', PATH_MAX - 2); printf("Testing updown path > PATH_MAX\n"); errno=0; memset(device_name, 0, IFNAMSIZ); if ((test_iface(device_name, IFNAMSIZ, fakepath) >= 0) || (errno != E2BIG)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } return 0; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; if (test_multi_eth() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_run_updown.c b/libnozzle/tests/api_nozzle_run_updown.c index b7522765..b4b5a86a 100644 --- a/libnozzle/tests/api_nozzle_run_updown.c +++ b/libnozzle/tests/api_nozzle_run_updown.c @@ -1,406 +1,407 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle = NULL; char *error_string = NULL; char *tmpdir = NULL; char tmpdirsrc[PATH_MAX*2]; char tmpstr[PATH_MAX*2]; char srcfile[PATH_MAX]; char dstfile[PATH_MAX]; char current_dir[PATH_MAX]; /* * create a tmp dir for storing up/down scripts. * we cannot create symlinks src dir */ if (getcwd(current_dir, sizeof(current_dir)) == NULL) { printf("Unable to get current working directory: %s\n", strerror(errno)); return -1; } snprintf(tmpdirsrc, sizeof(tmpdirsrc)-1, "%s/nozzle_test_XXXXXX", current_dir); tmpdir = mkdtemp(tmpdirsrc); if (!tmpdir) { printf("Unable to create temporary directory %s for testing: %s\n", tmpdirsrc, strerror(errno)); return -1; } printf("Created temporary test dir: %s\n", tmpdir); printf("Populating test dir...\n"); snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/pre-up.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/pre-up.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/up.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/up.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/down.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/down.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/post-down.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/post-down.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } printf("Testing error conditions\n"); printf("Init nozzle device with no path\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); err = -1; goto out_clean; } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } nozzle_close(nozzle); printf("Init nozzle device with path\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, tmpdir); if (!nozzle) { printf("Unable to init %s\n", device_name); err = -1; goto out_clean; } printf("Testing invalid nozzle handle\n"); err = nozzle_run_updown(NULL, NOZZLE_POSTDOWN, &error_string); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } printf("Testing invalid action\n"); err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN + 1, &error_string); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } printf("Testing invalid error string\n"); err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN + 1, NULL); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } printf("Testing interface pre-up/up/down/post-down (no scripts installed)\n"); err = nozzle_run_updown(nozzle, NOZZLE_PREUP, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in pre-up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_UP, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_DOWN, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in post-down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } printf("Populating test dir with fail scripts\n"); snprintf(srcfile, sizeof(srcfile) - 1, "%s/nozzle_run_updown_exit_false", ABSSRCDIR); snprintf(dstfile, sizeof(dstfile) - 1, "%s/pre-up.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/up.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/down.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/post-down.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } printf("Testing interface pre-up/up/down/post-down (FAIL scripts installed)\n"); err = nozzle_run_updown(nozzle, NOZZLE_PREUP, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in pre-up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_UP, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_DOWN, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in post-down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } printf("Populating test dir with true scripts\n"); snprintf(srcfile, sizeof(srcfile) - 1, "%s/nozzle_run_updown_exit_true", ABSSRCDIR); snprintf(dstfile, sizeof(dstfile) - 1, "%s/pre-up.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/up.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/down.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/post-down.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } printf("Testing interface pre-up/up/down/post-down (TRUE scripts installed)\n"); err = nozzle_run_updown(nozzle, NOZZLE_PREUP, &error_string); if (err) { printf("nozzle_run_updown failed to execute true script in pre-up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_UP, &error_string); if (err) { printf("nozzle_run_updown failed to execute true script in up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_DOWN, &error_string); if (err) { printf("nozzle_run_updown failed to execite true script in down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if (err) { printf("nozzle_run_updown failed to execute true script in post-down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } out_clean: if (tmpdir) { snprintf(tmpstr, sizeof(tmpstr) - 1, "rm -rf %s", tmpdir); printf("Removing temporary dir: %s\n", tmpstr); err = execute_bin_sh_command(tmpstr, &error_string); if (err) { printf("Error removing directory: %s\n", error_string); } if (error_string) { free(error_string); } } if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_down.c b/libnozzle/tests/api_nozzle_set_down.c index c0eb6db2..946b3116 100644 --- a/libnozzle/tests/api_nozzle_set_down.c +++ b/libnozzle/tests/api_nozzle_set_down.c @@ -1,126 +1,127 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-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("Try to DOWN the same interface twice\n"); if (nozzle_set_down(nozzle) < 0) { printf("Interface was already DOWN, spurious error received from nozzle_set_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(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_mac.c b/libnozzle/tests/api_nozzle_set_mac.c index 31ebb4cf..7c50f949 100644 --- a/libnozzle/tests/api_nozzle_set_mac.c +++ b/libnozzle/tests/api_nozzle_set_mac.c @@ -1,158 +1,159 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include #include #endif #ifdef KNET_BSD #include #endif #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; char *original_mac = NULL, *current_mac = NULL, *temp_mac = NULL; struct ether_addr *orig_mac, *cur_mac, *tmp_mac; printf("Testing set MAC\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("Get current MAC\n"); if (nozzle_get_mac(nozzle, &original_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } orig_mac = ether_aton(original_mac); if (nozzle_get_mac(nozzle, ¤t_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", current_mac); printf("Setting MAC: 00:01:01:01:01:01\n"); if (nozzle_set_mac(nozzle, "00:01:01:01:01:01") < 0) { printf("Unable to set current MAC address.\n"); err = -1; goto out_clean; } if (nozzle_get_mac(nozzle, &temp_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", temp_mac); cur_mac = ether_aton(current_mac); tmp_mac = ether_aton(temp_mac); printf("Comparing MAC addresses\n"); if (memcmp(cur_mac, tmp_mac, sizeof(struct ether_addr))) { printf("Mac addresses are not the same?!\n"); err = -1; goto out_clean; } printf("Testing reset_mac\n"); if (nozzle_reset_mac(nozzle) < 0) { printf("Unable to reset mac address\n"); err = -1; goto out_clean; } if (current_mac) { free(current_mac); current_mac = NULL; } if (nozzle_get_mac(nozzle, ¤t_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } cur_mac = ether_aton(current_mac); if (memcmp(cur_mac, orig_mac, sizeof(struct ether_addr))) { printf("Mac addresses are not the same?!\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Pass NULL to set_mac (pass1)\n"); errno = 0; if ((nozzle_set_mac(nozzle, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_set_mac sanity checks\n"); err = -1; goto out_clean; } printf("Pass NULL to set_mac (pass2)\n"); errno = 0; if ((nozzle_set_mac(NULL, current_mac) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_set_mac sanity checks\n"); err = -1; goto out_clean; } out_clean: if (current_mac) free(current_mac); if (temp_mac) free(temp_mac); if (original_mac) free(original_mac); if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_mtu.c b/libnozzle/tests/api_nozzle_set_mtu.c index b69d1ecf..752a55cb 100644 --- a/libnozzle/tests/api_nozzle_set_mtu.c +++ b/libnozzle/tests/api_nozzle_set_mtu.c @@ -1,297 +1,298 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #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; int current_mtu = 0; int expected_mtu = 1500; printf("Testing set MTU\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("Comparing default MTU\n"); current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected default [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Setting MTU to 9000\n"); expected_mtu = 9000; if (nozzle_set_mtu(nozzle, expected_mtu) < 0) { printf("Unable to set MTU to %d\n", expected_mtu); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Restoring MTU to default\n"); expected_mtu = 1500; if (nozzle_reset_mtu(nozzle) < 0) { printf("Unable to reset mtu\n"); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Passing empty struct to set_mtu\n"); if (nozzle_set_mtu(NULL, 1500) == 0) { printf("Something is wrong in nozzle_set_mtu sanity checks\n"); err = -1; goto out_clean; } printf("Passing 0 mtu to set_mtu\n"); if (nozzle_set_mtu(nozzle, 0) == 0) { printf("Something is wrong in nozzle_set_mtu sanity checks\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } static int test_ipv6(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; char verifycmd[2048]; int err=0; nozzle_t nozzle; char *error_string = NULL; int current_mtu = 0; printf("Testing get/set MTU with IPv6 address\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/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err) { 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("Setting MTU to 1200\n"); if (nozzle_set_mtu(nozzle, 1200) < 0) { printf("Unable to set MTU to 1200\n"); err = -1; goto out_clean; } err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } #ifdef KNET_LINUX if (!err) { #endif #ifdef KNET_BSD if (err) { #endif printf("Unable to verify 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; } 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_2); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_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("Restoring MTU to default\n"); if (nozzle_reset_mtu(nozzle) < 0) { printf("Unable to reset mtu\n"); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu != 1500) { printf("current mtu [%d] does not match expected value [1500]\n", current_mtu); 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; } 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_2); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_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; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); + need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; if (test_ipv6() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_up.c b/libnozzle/tests/api_nozzle_set_up.c index 6d30fcd9..6c9e23d7 100644 --- a/libnozzle/tests/api_nozzle_set_up.c +++ b/libnozzle/tests/api_nozzle_set_up.c @@ -1,100 +1,101 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-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"); if (nozzle_set_up(nozzle) < 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"); if (nozzle_set_up(nozzle) < 0) { printf("Interface was already UP, spurious error received from nozzle_set_up\n"); err = -1; goto out_clean; } printf("Pass NULL to nozzle set_up\n"); 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(); + need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/test-common.c b/libnozzle/tests/test-common.c index 21663b18..d2bbde0b 100644 --- a/libnozzle/tests/test-common.c +++ b/libnozzle/tests/test-common.c @@ -1,159 +1,176 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" +#include +#include #include -#include +#include #include #include #include #include #include #include #include "test-common.h" void need_root(void) { if (geteuid() != 0) { printf("This test requires root privileges\n"); exit(SKIP); } } +void need_tun(void) +{ +#ifdef KNET_LINUX + const char *tundev = "/dev/net/tun"; +#else + const char *tundev = "/dev/tun"; +#endif + int fd = open(tundev, O_RDWR); + if (fd < 0) { + printf("Failed to open %s (errno=%d); this test requires TUN support\n", tundev, errno); + exit(SKIP); + } + close(fd); +} + int test_iface(char *name, size_t size, const char *updownpath) { nozzle_t nozzle; nozzle=nozzle_open(name, size, updownpath); if (!nozzle) { - printf("Unable to open nozzle.\n"); + printf("Unable to open nozzle (errno=%d).\n", errno); return -1; } printf("Created interface: %s\n", name); if (is_if_in_system(name) > 0) { printf("Found interface %s on the system\n", name); } else { printf("Unable to find interface %s on the system\n", name); } if (!nozzle_get_handle_by_name(name)) { printf("Unable to find interface %s in nozzle db\n", name); } else { printf("Found interface %s in nozzle db\n", name); } nozzle_close(nozzle); if (is_if_in_system(name) == 0) printf("Successfully removed interface %s from the system\n", name); return 0; } int is_if_in_system(char *name) { struct ifaddrs *ifap = NULL; struct ifaddrs *ifa; int found = 0; if (getifaddrs(&ifap) < 0) { printf("Unable to get interface list.\n"); return -1; } ifa = ifap; while (ifa) { if (!strncmp(name, ifa->ifa_name, IFNAMSIZ)) { found = 1; break; } ifa=ifa->ifa_next; } freeifaddrs(ifap); return found; } int get_random_byte(void) { pid_t mypid; uint8_t *pid; uint8_t randombyte = 0; uint8_t i; if (sizeof(pid_t) < 4) { printf("pid_t is smaller than 4 bytes?\n"); exit(77); } mypid = getpid(); pid = (uint8_t *)&mypid; for (i = 0; i < sizeof(pid_t); i++) { if (pid[i] == 0) { pid[i] = 128; } } randombyte = pid[1]; return randombyte; } void make_local_ips(char *testipv4_1, char *testipv4_2, char *testipv6_1, char *testipv6_2) { pid_t mypid; uint8_t *pid; uint8_t i; memset(testipv4_1, 0, IPBUFSIZE); memset(testipv4_2, 0, IPBUFSIZE); memset(testipv6_1, 0, IPBUFSIZE); memset(testipv6_2, 0, IPBUFSIZE); mypid = getpid(); pid = (uint8_t *)&mypid; for (i = 0; i < sizeof(pid_t); i++) { if ((pid[i] == 0) || (pid[i] == 255)) { pid[i] = 128; } } snprintf(testipv4_1, IPBUFSIZE - 1, "127.%u.%u.%u", pid[1], pid[2], pid[0]); snprintf(testipv4_2, IPBUFSIZE - 1, "127.%u.%d.%u", pid[1], pid[2]+1, pid[0]); snprintf(testipv6_1, IPBUFSIZE - 1, "fd%x:%x%x::1", pid[1], pid[2], pid[0]); snprintf(testipv6_2, IPBUFSIZE - 1, "fd%x:%x%x:1::1", pid[1], pid[2], pid[0]); } diff --git a/libnozzle/tests/test-common.h b/libnozzle/tests/test-common.h index b96349ff..04645b64 100644 --- a/libnozzle/tests/test-common.h +++ b/libnozzle/tests/test-common.h @@ -1,36 +1,37 @@ /* * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #ifndef __NOZZLE_TEST_COMMON_H__ #define __NOZZLE_TEST_COMMON_H__ #include "internals.h" #include "libnozzle.h" /* * error codes from automake test-driver */ #define PASS 0 #define SKIP 77 #define ERROR 99 #define FAIL -1 /* * common facilities */ #define IPBUFSIZE 1024 void need_root(void); +void need_tun(void); int test_iface(char *name, size_t size, const char *updownpath); int is_if_in_system(char *name); int get_random_byte(void); void make_local_ips(char *testipv4_1, char *testipv4_2, char *testipv6_1, char *testipv6_2); #endif