]> git.proxmox.com Git - mirror_kronosnet.git/commitdiff
[libnozzle] add api_nozzle_open test
authorFabio M. Di Nitto <fdinitto@redhat.com>
Wed, 5 Dec 2018 09:17:51 +0000 (10:17 +0100)
committerFabio M. Di Nitto <fdinitto@redhat.com>
Tue, 18 Dec 2018 08:30:22 +0000 (09:30 +0100)
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
libnozzle/tests/Makefile.am
libnozzle/tests/api_nozzle_open.c [new file with mode: 0644]
libnozzle/tests/nozzle_test.c
libnozzle/tests/test-common.c [new file with mode: 0644]
libnozzle/tests/test-common.h [new file with mode: 0644]

index 48ae874d082c3cc1dfcb72861cbbe915b603f1c4..e9621c398decf2e3b5df92fb48d05a7777aeff6c 100644 (file)
@@ -15,15 +15,22 @@ EXTRA_DIST          = \
                          tap_updown_good \
                          api-test-coverage
 
+noinst_HEADERS         = \
+                         test-common.h
+
 if BUILD_LIBNOZZLE
 
 check_PROGRAMS         = \
+                         api_nozzle_open_test \
                          nozzle_test
 
-TESTS                  = $(check_PROGRAMS)
-
 noinst_PROGRAMS                = $(check_PROGRAMS)
 
+noinst_SCRIPTS         = \
+                         api-test-coverage
+
+TESTS                  = $(check_PROGRAMS)
+
 check-local: check-api-test-coverage
 
 check-api-test-coverage:
@@ -34,7 +41,12 @@ 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 \
+                              ../internals.c
+
 nozzle_test_SOURCES    = nozzle_test.c \
+                         test-common.c \
                          ../internals.c
 
 endif
diff --git a/libnozzle/tests/api_nozzle_open.c b/libnozzle/tests/api_nozzle_open.c
new file mode 100644 (file)
index 0000000..4386212
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.  All rights reserved.
+ *
+ * Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
+ *
+ * This software licensed under GPL-2.0+, LGPL-2.0+
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+
+#include "test-common.h"
+
+/*
+ * use this one to randomize nozzle interface name
+ * for named creation test
+ */
+uint8_t randombyte = 0;
+
+static 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");
+               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;
+}
+
+static int test(void)
+{
+       char device_name[2*IFNAMSIZ];
+       char fakepath[PATH_MAX];
+       size_t size = IFNAMSIZ;
+
+       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();
+
+       if (test() < 0)
+               return FAIL;
+
+       return PASS;
+}
index 2c8c9855d2cca8fa8b6650deb14a6f316174b1f3..be441e7be1fe5f79323aad312173c7f2a492152e 100644 (file)
@@ -12,9 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <sys/types.h>
 #include <errno.h>
-#include <ifaddrs.h>
 #include <stdint.h>
 #include <limits.h>
 #include <sys/socket.h>
@@ -28,8 +26,7 @@
 #include <net/if_dl.h>
 #endif
 
-#include "libnozzle.h"
-#include "internals.h"
+#include "test-common.h"
 
 char testipv4_1[1024];
 char testipv4_2[1024];
@@ -41,148 +38,6 @@ char testipv6_2[1024];
  */
 uint8_t randombyte = 0;
 
-static 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;
-}
-
-static 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 knet.\n");
-               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;
-}
-
-static int check_nozzle_open_close(void)
-{
-       char device_name[2*IFNAMSIZ];
-       char fakepath[PATH_MAX];
-       size_t size = IFNAMSIZ;
-
-       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;
-}
-
 static int check_knet_multi_eth(void)
 {
        char device_name1[IFNAMSIZ];
@@ -1238,16 +1093,10 @@ static void make_local_ips(void)
 
 int main(void)
 {
-       if (geteuid() != 0) {
-               printf("This test requires root privileges\n");
-               exit(77);
-       }
+       need_root();
 
        make_local_ips();
 
-       if (check_nozzle_open_close() < 0)
-               return -1;
-
        if (check_knet_multi_eth() < 0)
                return -1;
 
diff --git a/libnozzle/tests/test-common.c b/libnozzle/tests/test-common.c
new file mode 100644 (file)
index 0000000..f136b90
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.  All rights reserved.
+ *
+ * Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
+ *
+ * This software licensed under GPL-2.0+, LGPL-2.0+
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#include "test-common.h"
+
+void need_root(void)
+{
+       if (geteuid() != 0) {
+               printf("This test requires root privileges\n");
+               exit(SKIP);
+       }
+}
+
+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;
+}
diff --git a/libnozzle/tests/test-common.h b/libnozzle/tests/test-common.h
new file mode 100644 (file)
index 0000000..fdf0814
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.  All rights reserved.
+ *
+ * Authors: Fabio M. Di Nitto <fabbione@kronosnet.org>
+ *
+ * This software licensed under GPL-2.0+, LGPL-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
+ */
+
+void need_root(void);
+int is_if_in_system(char *name);
+
+#endif