Skip to content

Commit

Permalink
[libnozzle] add nozzle_get_ips tests and kill nozzle_test
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 15, 2018
1 parent ff3aa8b commit 71a6820
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 249 deletions.
14 changes: 6 additions & 8 deletions libnozzle/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ api_checks = \
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 =

Expand Down Expand Up @@ -116,12 +116,10 @@ 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
182 changes: 182 additions & 0 deletions libnozzle/tests/api_nozzle_get_ips.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* 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 "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;
}

0 comments on commit 71a6820

Please sign in to comment.