summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2009-04-02 20:39:36 +0200
committerAndreas Baumann <abaumann@yahoo.com>2009-04-02 20:39:36 +0200
commit06096f9f9385cd3155a2c957e8ff0bf2da59ac56 (patch)
treeb951b645af6933ada5ffd445fed3db93d13fa109
parentbeaacec9990b0261b656a93d97d98bb17cba623d (diff)
downloadwolfbones-06096f9f9385cd3155a2c957e8ff0bf2da59ac56.tar.gz
wolfbones-06096f9f9385cd3155a2c957e8ff0bf2da59ac56.tar.bz2
added a simple network test
-rw-r--r--tests/GNUmakefile2
-rw-r--r--tests/Makefile.W322
-rw-r--r--tests/network/GNUmakefile20
-rw-r--r--tests/network/Makefile.W3219
-rw-r--r--tests/network/test1.c137
-rw-r--r--tests/network/test1.req3
6 files changed, 181 insertions, 2 deletions
diff --git a/tests/GNUmakefile b/tests/GNUmakefile
index 960755c..405e207 100644
--- a/tests/GNUmakefile
+++ b/tests/GNUmakefile
@@ -1,6 +1,6 @@
TOPDIR = ..
-SUBDIRS = threads port log gettext daemon service
+SUBDIRS = threads port log gettext daemon service network
-include $(TOPDIR)/makefiles/gmake/sub.mk
diff --git a/tests/Makefile.W32 b/tests/Makefile.W32
index 407eb13..f76c295 100644
--- a/tests/Makefile.W32
+++ b/tests/Makefile.W32
@@ -1,6 +1,6 @@
TOPDIR = ..
-SUBDIRS = threads port log service
+SUBDIRS = threads port log service network
!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk
diff --git a/tests/network/GNUmakefile b/tests/network/GNUmakefile
new file mode 100644
index 0000000..0b4f44d
--- /dev/null
+++ b/tests/network/GNUmakefile
@@ -0,0 +1,20 @@
+TOPDIR = ../..
+
+INCLUDE_DIRS = \
+ -I$(TOPDIR)/include/wolf -I.
+
+INCLUDE_LIBS = \
+ $(TOPDIR)/src/libwolf.a
+
+TEST_BINS = \
+ test1$(EXE)
+
+-include $(TOPDIR)/makefiles/gmake/sub.mk
+
+local_all:
+
+local_clean:
+
+local_distclean:
+
+local_test:
diff --git a/tests/network/Makefile.W32 b/tests/network/Makefile.W32
new file mode 100644
index 0000000..7c16df9
--- /dev/null
+++ b/tests/network/Makefile.W32
@@ -0,0 +1,19 @@
+TOPDIR = ..\..
+
+INCLUDE_DIRS = \
+ /I$(TOPDIR)\include\wolf /I.
+
+TEST_BINS =
+
+!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk
+
+# TODO: what is the autogeneration rule for NMAKE?
+#test_create_join.exe: test_create_join.obj
+
+local_all:
+
+local_clean:
+
+local_distclean:
+
+local_test:
diff --git a/tests/network/test1.c b/tests/network/test1.c
new file mode 100644
index 0000000..5c304fe
--- /dev/null
+++ b/tests/network/test1.c
@@ -0,0 +1,137 @@
+#include "port/netdb.h" /* for getaddrinfo */
+#include "port/stdlib.h" /* for EXIT_XXX */
+#include "port/stdio.h" /* for fprintf */
+#include "port/string.h" /* for memset */
+#include "port/stdbool.h" /* for bool, true, false */
+
+#include <unistd.h> /* for close */
+#include <errno.h> /* for errno */
+#include <fcntl.h> /* for fcntl */
+#include <sys/select.h> /* for select */
+
+static void wolf_network_sock_nonblocking( int fd ) {
+ int flags;
+
+ flags = fcntl( fd, F_GETFL, 0 /* ignored */ );
+ flags |= O_NONBLOCK;
+ flags = fcntl( fd, F_SETFL, flags );
+}
+
+#define max(a,b) ((a) < (b) ? (b) : (a))
+
+int main( int argc, char *argv[] ) {
+ char *host;
+ char *service;
+ struct addrinfo hints;
+ struct addrinfo *result;
+ int error;
+ int fd;
+ int res;
+ int idle_secs;
+
+ if( argc != 3 ) {
+ fprintf( stderr, "usage: test1 <host> <port>\n" );
+ goto FAIL;
+ }
+
+ host = argv[1];
+ service = argv[2];
+
+#ifdef _WIN32
+ WSADATA wsa_data;
+ WSAStartup( MAKEWORD( 2, 2 ), &wsa_data );
+#endif
+
+ /* tell getaddrinfo what we want */
+ memset( &hints, 0, sizeof( struct addrinfo ) );
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ /* resolve the domain name into a list of addresses */
+ error = getaddrinfo( host, service, &hints, &result );
+ if( error != 0 ) {
+ fprintf( stderr, "getaddrinfo failed: %s (%d)\n",
+ gai_strerror( error ), error );
+ goto FAIL;
+ }
+
+ fd = socket( result->ai_family, result->ai_socktype, result->ai_protocol );
+ if( fd < 0 ) {
+ fprintf( stderr, "socket failed: %s (%d)\n", strerror( errno ), errno );
+ goto FAIL;
+ }
+
+ res = connect( fd, result->ai_addr, result->ai_addrlen );
+ if( res < 0 ) {
+ (void)close( fd );
+ fprintf( stderr, "connect failed: %s (%d)\n", strerror( errno ), errno );
+ goto FAIL;
+ }
+
+ wolf_network_sock_nonblocking( fileno( stdin ) );
+ wolf_network_sock_nonblocking( fileno( stdout ) );
+ wolf_network_sock_nonblocking( fd );
+ idle_secs = 0;
+
+ do {
+ int max_fd;
+ fd_set read_set;
+ fd_set write_set;
+ fd_set error_set;
+ struct timeval timeout;
+
+ FD_ZERO( &read_set );
+ FD_ZERO( &write_set );
+ FD_ZERO( &error_set );
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+
+ /* set up select mask and compute highest file descriptor number */
+ max_fd = 0;
+ max_fd = max( max_fd, fileno( stdin ) );
+ max_fd = max( max_fd, fileno( stdout ) );
+ max_fd = max( max_fd, fd );
+
+ res = select( max_fd + 1, &read_set, &write_set, &error_set, &timeout );
+ if( res < 0 ) {
+ if( errno == EINTR || errno == EAGAIN ) {
+ /* skip */
+ } else {
+ /* fatal errors */
+ fprintf( stderr, "select failed: %s (%d)\n",
+ strerror( errno ), errno );
+ (void)close( fd );
+ goto FAIL;
+ }
+ } else if( res == 0 ) {
+ /* timeout */
+ idle_secs++;
+ fprintf( stderr, "Idle since %d seconds\n", idle_secs );
+ } else {
+ /* something happened */
+ idle_secs = 0;
+ }
+ } while( true );
+
+ res = close( fd );
+ if( res < 0 ) {
+ fprintf( stderr, "close failed: %s (%d)\n", strerror( errno ), errno );
+ goto FAIL;
+ }
+
+ freeaddrinfo( result );
+
+ goto OK;
+
+FAIL:
+#ifdef _WIN32
+ WSACleanup( );
+#endif
+
+ return EXIT_FAILURE;
+
+OK:
+ return EXIT_SUCCESS;
+}
diff --git a/tests/network/test1.req b/tests/network/test1.req
new file mode 100644
index 0000000..708e8b1
--- /dev/null
+++ b/tests/network/test1.req
@@ -0,0 +1,3 @@
+GET / HTTP/1.1
+Host: europa5
+