Skip to content

Commit

Permalink
testsuite: decouple from utils.c
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 Mar 23, 2011
1 parent fca3cfb commit 4d69fc8
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 81 deletions.
2 changes: 1 addition & 1 deletion libtap/Makefile.am
Expand Up @@ -31,7 +31,7 @@ noinst_PROGRAMS = $(check_PROGRAMS)

check_PROGRAMS = tap_test

tap_test_SOURCES = $(sources) ../utils.c
tap_test_SOURCES = $(sources)

tap_test_CPPFLAGS = -DTEST \
-DABSBUILDDIR=\"$(abs_builddir)\"
Expand Down
24 changes: 6 additions & 18 deletions tests/Makefile.am
Expand Up @@ -33,26 +33,14 @@ AM_LDFLAGS = \
$(top_builddir)/libknet/libknet.a \
$(top_builddir)/libvty/libvty.a

ping_test_SOURCES = \
ping_test.c \
../utils.c
ping_test_SOURCES = ping_test.c

khandle_test_SOURCES = \
khandle_test.c \
../utils.c
khandle_test_SOURCES = khandle_test.c

lookup_bench_SOURCES = \
lookup_bench.c \
../utils.c
lookup_bench_SOURCES = lookup_bench.c

listener_test_SOURCES = \
listener_test.c \
../utils.c
listener_test_SOURCES = listener_test.c

netutils_test_SOURCES = \
netutils_test.c \
../utils.c
netutils_test_SOURCES = netutils_test.c

timediff_test_SOURCES = \
timediff_test.c \
../utils.c
timediff_test_SOURCES = timediff_test.c
6 changes: 3 additions & 3 deletions tests/khandle_test.c
@@ -1,12 +1,12 @@
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#include "libknet.h"
#include "utils.h"

#define HOST_LIST_SIZE 8192
#define HOST_LIST_LOOP 64
Expand All @@ -27,7 +27,7 @@ int main(int argc, char *argv[])
sock = socket(AF_UNIX, SOCK_STREAM, 0);

if (sock < 0) {
log_error("Unable to create new socket");
printf("Unable to create new socket\n");
exit(EXIT_FAILURE);
}

Expand All @@ -48,7 +48,7 @@ int main(int argc, char *argv[])
printf("Loop count: %u times\n", (unsigned int) search.param1);

if (knet_handle_free(knet_h) != 0) {
log_error("Unable to free knet_handle");
printf("Unable to free knet_handle\n");
exit(EXIT_FAILURE);
}

Expand Down
40 changes: 21 additions & 19 deletions tests/listener_test.c
@@ -1,10 +1,12 @@
#include "config.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/epoll.h>

#include "libknet-private.h"
#include "utils.h"

#define KNET_TEST_PORT 50000

Expand All @@ -18,7 +20,7 @@ static void test_add_listener(void)
listener = malloc(sizeof(struct knet_listener));

if (listener == NULL) {
log_error("Unable to create listener");
printf("Unable to create listener\n");
exit(EXIT_FAILURE);
}

Expand All @@ -31,7 +33,7 @@ static void test_add_listener(void)
address->sin_addr.s_addr = INADDR_ANY;

if (knet_listener_add(knet_h, listener) != 0) {
log_error("Unable to add listener");
printf("Unable to add listener\n");
exit(EXIT_FAILURE);
}
}
Expand All @@ -41,7 +43,7 @@ static void test_add_host(void)
struct knet_host *host;

if (knet_host_add(knet_h, 1) != 0) {
log_error("Unable to add host to knet_handle");
printf("Unable to add host to knet_handle\n");
exit(EXIT_FAILURE);
}

Expand All @@ -61,16 +63,16 @@ int main(int argc, char *argv[])
sock = socket(AF_UNIX, SOCK_STREAM, 0);

if (sock < 0) {
log_error("Unable to create new socket");
printf("Unable to create new socket\n");
exit(EXIT_FAILURE);
}

if ((knet_h = knet_handle_new(sock, 1)) == NULL) {
log_error("Unable to create new knet_handle_t");
printf("Unable to create new knet_handle_t\n");
exit(EXIT_FAILURE);
}

log_info("Adding listener to handle");
printf("Adding listener to handle\n");
test_add_listener();

memset(&ev, 0, sizeof(struct epoll_event));
Expand All @@ -79,52 +81,52 @@ int main(int argc, char *argv[])
err = epoll_ctl(knet_h->epollfd, EPOLL_CTL_ADD, listener->sock, &ev);

if (err != -1) {
log_error("Listener file descriptor not found in epollfd");
printf("Listener file descriptor not found in epollfd\n");
exit(EXIT_FAILURE);
}

log_error("Listener file descriptor was added to epollfd");
printf("Listener file descriptor was added to epollfd\n");

log_info("Adding host to handle");
printf("Adding host to handle\n");
test_add_host();

err = knet_listener_remove(knet_h, listener);

if (err != -EBUSY) {
log_error("Listener socket should be in use");
printf("Listener socket should be in use\n");
exit(EXIT_FAILURE);
}

log_error("Unable to remove listener with active links");
printf("Unable to remove listener with active links\n");

log_info("Removing host from handle");
printf("Removing host from handle\n");
err = knet_host_remove(knet_h, 1);

if (err != 0) {
log_error("Unable to remove host from knet_handle");
printf("Unable to remove host from knet_handle\n");
exit(EXIT_FAILURE);
}

log_info("Removing listener");
printf("Removing listener\n");
err = knet_listener_remove(knet_h, listener);

if (err != 0) {
log_error("Unable to remove listener from knet_handle");
printf("Unable to remove listener from knet_handle\n");
exit(EXIT_FAILURE);
}

/* don't try this at home :) */
err = epoll_ctl(knet_h->epollfd, EPOLL_CTL_DEL, listener->sock, &ev);

if (err != -1) {
log_error("Listener file was present in epollfd");
printf("Listener file was present in epollfd\n");
exit(EXIT_FAILURE);
}

log_error("Listener file descriptor was removed from epollfd");
printf("Listener file descriptor was removed from epollfd\n");

if (knet_handle_free(knet_h) != 0) {
log_error("Unable to free knet_handle");
printf("Unable to free knet_handle\n");
exit(EXIT_FAILURE);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/lookup_bench.c
@@ -1,12 +1,13 @@
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "libknet.h"
#include "libknet-private.h"
#include "netutils.h"
#include "utils.h"

#define KNET_PORT 50000
#define KNET_BENCH_LOOPNUM 100000000
Expand All @@ -21,7 +22,7 @@ int main(int argc, char *argv[])
head = malloc(sizeof(struct knet_link));

if (head == NULL) {
log_error("Unable to create knet_link");
printf("Unable to create knet_link\n");
exit(EXIT_FAILURE);
}

Expand Down
44 changes: 23 additions & 21 deletions tests/netutils_test.c
@@ -1,12 +1,14 @@
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include "netutils.h"
#include "utils.h"

static void check_ipv4(void)
{
Expand All @@ -17,7 +19,7 @@ static void check_ipv4(void)
memset(&addr, 0, sizeof(struct sockaddr_in));
memset(&addrck, 0, sizeof(struct sockaddr_in));

log_info("Checking strtoaddr on 192.168.0.1:50000");
printf("Checking strtoaddr on 192.168.0.1:50000\n");

addrck.sin_family = AF_INET;
addrck.sin_addr.s_addr = htonl(0xc0a80001); /* 192.168.0.1 */
Expand All @@ -27,35 +29,35 @@ static void check_ipv4(void)
(struct sockaddr *) &addr, sizeof(struct sockaddr_in));

if (err != 0) {
log_error("Unable to convert 192.168.0.1:50000");
printf("Unable to convert 192.168.0.1:50000\n");
exit(EXIT_FAILURE);
}

if (memcmp(&addr, &addrck, sizeof(struct sockaddr_in)) != 0) {
errno = EINVAL;
log_error("Check on 192.168.0.1:50000 failed");
printf("Check on 192.168.0.1:50000 failed\n");
exit(EXIT_FAILURE);
}

log_info("Checking addrtostr on 192.168.0.1:50000");
printf("Checking addrtostr on 192.168.0.1:50000\n");

err = addrtostr((struct sockaddr *) &addrck,
sizeof(struct sockaddr_in), buf);

if (err != 0) {
log_error("Unable to convert 192.168.0.1:50000");
printf("Unable to convert 192.168.0.1:50000\n");
exit(EXIT_FAILURE);
}

if (strcmp(buf[0], "192.168.0.1") != 0) {
errno = EINVAL;
log_error("Wrong address conversion: %s", buf[0]);
printf("Wrong address conversion: %s\n", buf[0]);
exit(EXIT_FAILURE);
}

if (strcmp(buf[1], "50000") != 0) {
errno = EINVAL;
log_error("Wrong port conversion: %s", buf[1]);
printf("Wrong port conversion: %s\n", buf[1]);
exit(EXIT_FAILURE);
}

Expand All @@ -71,7 +73,7 @@ static void check_ipv6(void)
memset(&addr, 0, sizeof(struct sockaddr_in6));
memset(&addrck, 0, sizeof(struct sockaddr_in6));

log_info("Checking strtoaddr on [fd00::1]:50000");
printf("Checking strtoaddr on [fd00::1]:50000\n");

addrck.sin6_family = AF_INET6;
addrck.sin6_addr.s6_addr16[0] = htons(0xfd00); /* fd00::1 */
Expand All @@ -82,35 +84,35 @@ static void check_ipv6(void)
(struct sockaddr *) &addr, sizeof(struct sockaddr_in6));

if (err != 0) {
log_error("Unable to convert [fd00::1]:50000");
printf("Unable to convert [fd00::1]:50000\n");
exit(EXIT_FAILURE);
}

if (memcmp(&addr, &addrck, sizeof(struct sockaddr_in6)) != 0) {
errno = EINVAL;
log_error("Check on 192.168.0.1:50000 failed");
printf("Check on 192.168.0.1:50000 failed\n");
exit(EXIT_FAILURE);
}

log_info("Checking addrtostr on [fd00::1]:50000");
printf("Checking addrtostr on [fd00::1]:50000\n");

err = addrtostr((struct sockaddr *) &addrck,
sizeof(struct sockaddr_in6), buf);

if (err != 0) {
log_error("Unable to convert 192.168.0.1:50000");
printf("Unable to convert 192.168.0.1:50000\n");
exit(EXIT_FAILURE);
}

if (strcmp(buf[0], "fd00::1") != 0) {
errno = EINVAL;
log_error("Wrong address conversion: %s", buf[0]);
printf("Wrong address conversion: %s\n", buf[0]);
exit(EXIT_FAILURE);
}

if (strcmp(buf[1], "50000") != 0) {
errno = EINVAL;
log_error("Wrong port conversion: %s", buf[1]);
printf("Wrong port conversion: %s\n", buf[1]);
exit(EXIT_FAILURE);
}

Expand All @@ -122,23 +124,23 @@ static void check_resolve(void)
int err;
struct sockaddr_in addr;

log_info("Checking host resolution");
printf("Checking host resolution\n");
err = strtoaddr("localhost", "50000",
(struct sockaddr *) &addr, sizeof(struct sockaddr_in));

if (err == 0) {
errno = EINVAL;
log_error("Host resolution should not be enabled");
printf("Host resolution should not be enabled\n");
exit(EXIT_FAILURE);
}

log_info("Checking port resolution");
printf("Checking port resolution\n");
err = strtoaddr("127.0.0.1", "ssh",
(struct sockaddr *) &addr, sizeof(struct sockaddr_in));

if (err == 0) {
errno = EINVAL;
log_error("Port resolution should not be enabled");
printf("Port resolution should not be enabled\n");
exit(EXIT_FAILURE);
}
}
Expand All @@ -163,15 +165,15 @@ int main(int argc, char *argv[])
sizeof(struct sockaddr_storage));

if (err != 0) {
log_error("Unable to convert strings to sockaddr");
printf("Unable to convert strings to sockaddr\n");
exit(EXIT_FAILURE);
}

err = addrtostr((struct sockaddr *) &address,
sizeof(struct sockaddr_storage), buf);

if (err != 0) {
log_error("Unable to convert sockaddr to strings");
printf("Unable to convert sockaddr to strings\n");
exit(EXIT_FAILURE);
}

Expand Down

0 comments on commit 4d69fc8

Please sign in to comment.