Skip to content

Commit

Permalink
[libnozzle] add nozzle_get_handle_by_name tests
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 18, 2018
1 parent 8053d68 commit 35d2271
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libnozzle/tests/Makefile.am
Expand Up @@ -29,6 +29,7 @@ check_PROGRAMS = \
api_nozzle_set_mtu_test \
api_nozzle_get_mac_test \
api_nozzle_set_mac_test \
api_nozzle_get_handle_by_name_test \
nozzle_test

noinst_PROGRAMS = $(check_PROGRAMS)
Expand Down Expand Up @@ -75,6 +76,9 @@ api_nozzle_get_mac_test_SOURCES = api_nozzle_get_mac.c \
api_nozzle_set_mac_test_SOURCES = api_nozzle_set_mac.c \
test-common.c

api_nozzle_get_handle_by_name_test_SOURCES = api_nozzle_get_handle_by_name.c \
test-common.c

nozzle_test_SOURCES = nozzle_test.c \
test-common.c \
../internals.c
Expand Down
85 changes: 85 additions & 0 deletions libnozzle/tests/api_nozzle_get_handle_by_name.c
@@ -0,0 +1,85 @@
/*
* 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 <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#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();

if (test() < 0)
return FAIL;

return PASS;
}

0 comments on commit 35d2271

Please sign in to comment.