Skip to content

Commit

Permalink
[libnozzle] add api_nozzle_close_test
Browse files Browse the repository at this point in the history
also, share more code in test-common, fix another BSD build issue

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 15, 2018
1 parent 67408a6 commit 6fc9c56
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 105 deletions.
7 changes: 5 additions & 2 deletions libnozzle/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if BUILD_LIBNOZZLE

check_PROGRAMS = \
api_nozzle_open_test \
api_nozzle_close_test \
nozzle_test

noinst_PROGRAMS = $(check_PROGRAMS)
Expand All @@ -42,8 +43,10 @@ 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
test-common.c

api_nozzle_close_test_SOURCES = api_nozzle_close.c \
test-common.c

nozzle_test_SOURCES = nozzle_test.c \
test-common.c \
Expand Down
130 changes: 130 additions & 0 deletions libnozzle/tests/api_nozzle_close.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2010-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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>
#include <sys/socket.h>
#include <net/ethernet.h>

#ifdef KNET_LINUX
#include <linux/if_tun.h>
#include <netinet/ether.h>
#endif
#ifdef KNET_BSD
#include <net/if_dl.h>
#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();

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;
}
38 changes: 1 addition & 37 deletions libnozzle/tests/api_nozzle_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,12 @@

#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;
uint8_t randombyte = get_random_byte();

memset(device_name, 0, sizeof(device_name));

Expand Down
71 changes: 5 additions & 66 deletions libnozzle/tests/nozzle_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,10 @@

#include "test-common.h"

char testipv4_1[1024];
char testipv4_2[1024];
char testipv6_1[1024];
char testipv6_2[1024];
/*
* use this one to randomize knet interface name
* for named creation test
*/
uint8_t randombyte = 0;
char testipv4_1[IPBUFSIZE];
char testipv4_2[IPBUFSIZE];
char testipv6_1[IPBUFSIZE];
char testipv6_2[IPBUFSIZE];

static int check_knet_multi_eth(void)
{
Expand Down Expand Up @@ -1035,67 +1030,11 @@ static int check_knet_set_del_ip(void)
return err;
}

static void make_local_ips(void)
{
pid_t mypid;
uint8_t *pid;
uint8_t i;

if (sizeof(pid_t) < 4) {
printf("pid_t is smaller than 4 bytes?\n");
exit(77);
}

memset(testipv4_1, 0, sizeof(testipv4_1));
memset(testipv4_2, 0, sizeof(testipv4_2));
memset(testipv6_1, 0, sizeof(testipv6_1));
memset(testipv6_2, 0, sizeof(testipv6_2));

mypid = getpid();
pid = (uint8_t *)&mypid;

for (i = 0; i < sizeof(pid_t); i++) {
if (pid[i] == 0) {
pid[i] = 128;
}
}

randombyte = pid[1];

snprintf(testipv4_1,
sizeof(testipv4_1) - 1,
"127.%u.%u.%u",
pid[1],
pid[2],
pid[0]);

snprintf(testipv4_2,
sizeof(testipv4_2) - 1,
"127.%u.%d.%u",
pid[1],
pid[2]+1,
pid[0]);

snprintf(testipv6_1,
sizeof(testipv6_1) - 1,
"fd%x:%x%x::1",
pid[1],
pid[2],
pid[0]);

snprintf(testipv6_2,
sizeof(testipv6_2) - 1,
"fd%x:%x%x:1::1",
pid[1],
pid[2],
pid[0]);
}

int main(void)
{
need_root();

make_local_ips();
make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2);

if (check_knet_multi_eth() < 0)
return -1;
Expand Down

0 comments on commit 6fc9c56

Please sign in to comment.