Skip to content

Commit

Permalink
[libnozzle] add nozzle_get_mac / nozzle_set_mac / nozzle_reset_mac 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 15, 2018
1 parent df31e0b commit ccd664e
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 119 deletions.
8 changes: 8 additions & 0 deletions libnozzle/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ check_PROGRAMS = \
api_nozzle_set_down_test \
api_nozzle_get_mtu_test \
api_nozzle_set_mtu_test \
api_nozzle_get_mac_test \
api_nozzle_set_mac_test \
nozzle_test

noinst_PROGRAMS = $(check_PROGRAMS)
Expand Down Expand Up @@ -67,6 +69,12 @@ api_nozzle_set_mtu_test_SOURCES = api_nozzle_set_mtu.c \
test-common.c \
../internals.c

api_nozzle_get_mac_test_SOURCES = api_nozzle_get_mac.c \
test-common.c

api_nozzle_set_mac_test_SOURCES = api_nozzle_set_mac.c \
test-common.c

nozzle_test_SOURCES = nozzle_test.c \
test-common.c \
../internals.c
Expand Down
1 change: 1 addition & 0 deletions libnozzle/tests/api-test-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ missing=0

for i in $headerapicalls; do
[ "$i" = nozzle_reset_mtu ] && i=nozzle_set_mtu # tested together
[ "$i" = nozzle_reset_mac ] && i=nozzle_set_mac # tested together
numapicalls=$((numapicalls + 1))
if [ -f $srcdir/api_${i}.c ]; then
found=$((found + 1))
Expand Down
130 changes: 130 additions & 0 deletions libnozzle/tests/api_nozzle_get_mac.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"

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, &current_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();

if (test() < 0)
return FAIL;

return PASS;
}
162 changes: 162 additions & 0 deletions libnozzle/tests/api_nozzle_set_mac.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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"

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, *err_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, &current_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);

if (nozzle_get_mac(nozzle, &current_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, err_mac) >= 0) || (errno != EINVAL)) {
printf("Something is wrong in nozzle_set_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 (original_mac)
free(original_mac);

if (nozzle) {
nozzle_close(nozzle);
}

return err;
}

int main(void)
{
need_root();

if (test() < 0)
return FAIL;

return PASS;
}

0 comments on commit ccd664e

Please sign in to comment.