From bfa8d4130f63f9d11a861ebd9612923705649402 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sat, 21 May 2011 19:49:59 +0300 Subject: [PATCH] raw: Add test suite for sw_socket "syscall". --- src/raw/Makefile | 3 +- src/raw/test/Makefile | 5 ++- src/raw/test/test_sw_socket.c | 85 +++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/raw/Makefile b/src/raw/Makefile index 3ef0348..40b409d 100644 --- a/src/raw/Makefile +++ b/src/raw/Makefile @@ -1,4 +1,5 @@ -CPPFLAGS = -I. -Iinclude +CPPFLAGS = -DDEBUG -DLOG_LEVEL=LOG_DEBUG -I. -I.. -I../include +#CPPFLAGS = -I. -Iinclude CFLAGS = -Wall -g .PHONY: all clean diff --git a/src/raw/test/Makefile b/src/raw/test/Makefile index f24b39c..793a511 100644 --- a/src/raw/test/Makefile +++ b/src/raw/test/Makefile @@ -1,5 +1,5 @@ -#CPPFLAGS = -DDEBUG -DLOG_LEVEL=LOG_DEBUG -I. -I.. -I../include -CPPFLAGS = -I. -I.. -I../include +CPPFLAGS = -DDEBUG -DLOG_LEVEL=LOG_DEBUG -I. -I.. -I../include +#CPPFLAGS = -I. -I.. -I../include CFLAGS = -Wall -g .PHONY: all clean @@ -11,6 +11,7 @@ test: test.o test_sw_socket.o test_sw_bind.o test_sw_getsockname.o \ test_sw_sendmsg.o test_sw_recvmsg.o \ test_sw_setsockopt.o test_sw_getsockopt.o \ test_sw_close.o test_dummy.o + ../swift_raw.o ../swift_list.o clean: -rm -f *~ *.o diff --git a/src/raw/test/test_sw_socket.c b/src/raw/test/test_sw_socket.c index 4292044..3b4b4d7 100644 --- a/src/raw/test/test_sw_socket.c +++ b/src/raw/test/test_sw_socket.c @@ -2,6 +2,21 @@ * Test sw_socket "syscall". */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "swift_raw.h" +#include "swift_types.h" + +#include "debug.h" +#include "util.h" + #include "test_sw.h" #include "test.h" @@ -16,6 +31,11 @@ void socket_test_suite(void) { start_suite(); socket_dummy(); + socket_invalid_domain(); + socket_invalid_type(); + socket_invalid_protocol(); + socket_insufficient_file_descriptors(); + socket_ok(); } /* Dummy function for testing purposes only. */ @@ -23,3 +43,68 @@ static void socket_dummy(void) { test(1 == 1); } + +/* Use invalid domain when calling sw_socket. */ +static void socket_invalid_domain(void) +{ + int rc; + + rc = sw_socket(PF_UNIX, SOCK_DGRAM, IPPROTO_SWIFT); + + test(rc < 0 && errno == EINVAL); +} + +/* Use invalid type when calling sw_socket. */ +static void socket_invalid_type(void) +{ + int rc; + + rc = sw_socket(PF_INET, SOCK_STREAM, IPPROTO_SWIFT); + + test(rc < 0 && errno == EINVAL); +} + +/* Use invalid protocol when calling sw_socket. */ +static void socket_invalid_protocol(void) +{ + int rc; + + rc = sw_socket(PF_INET, SOCK_DGRAM, -1); + + test(rc < 0 && errno == EINVAL); +} + +/* + * Use dup to fill the number of file descriptors for current process. + * Calling sw_socket must result in error. + * + * File descriptors are not closed. Test processes must be restarted. + */ +static void socket_insufficient_file_descriptors(void) +{ + int fd; + int rc; + + while (1) { + /* Duplicate standard output. */ + fd = dup(STDOUT_FILENO); + if (fd < 0) + break; + } + + rc = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT); + + dprintf("errno = %d\n", errno); + test(rc < 0 && errno == EMFILE); +} + +/* Valid call of sw_socket. */ +static void socket_ok(void) +{ + int rc; + + rc = sw_socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT); + dprintf("rc = %d, errno = %d\n", rc, errno); + + test(rc > 0); +} -- 2.20.1