From: Nick Mathewson Date: Thu, 20 Sep 2007 19:08:20 +0000 (+0000) Subject: Make the test/ subdirectory buildable under Windows. Well, mingw at least. The... X-Git-Tag: release-2.0.1-alpha~556 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1e1f77c5b02fca9622e9e1d249adec4b5428a245;p=libevent Make the test/ subdirectory buildable under Windows. Well, mingw at least. The tests still don't all pass, but at least now we know that. svn:r447 --- diff --git a/ChangeLog b/ChangeLog index 84879a33..18e93e53 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,3 +22,5 @@ Changes in current version: o Fix http module on windows to close sockets properly. o Make autogen.sh script run correctly on systems where /bin/sh isn't bash. (Patch from Trond Norbye, rewritten by Hagne Mahre and then Hannah Schroeter.) o Skip calling gettime() in timeout_process if we are not in fact waiting for any events. (Patch from Trond Norbye) + o Make test subdirectory compile under mingw. + diff --git a/test/bench.c b/test/bench.c index 61b05025..ed32ae06 100644 --- a/test/bench.c +++ b/test/bench.c @@ -40,9 +40,13 @@ #include #include #include +#ifdef WIN32 +#include +#else #include #include #include +#endif #include #include #include @@ -51,6 +55,7 @@ #include #include +#include static int count, writes, fired; @@ -117,7 +122,9 @@ run_once(void) int main (int argc, char **argv) { +#ifndef WIN32 struct rlimit rl; +#endif int i, c; struct timeval *tv; int *cp; @@ -143,11 +150,13 @@ main (int argc, char **argv) } } +#ifndef WIN32 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50; if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { perror("setrlimit"); exit(1); } +#endif events = calloc(num_pipes, sizeof(struct event)); pipes = calloc(num_pipes * 2, sizeof(int)); @@ -162,7 +171,7 @@ main (int argc, char **argv) #ifdef USE_PIPES if (pipe(cp) == -1) { #else - if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { #endif perror("pipe"); exit(1); diff --git a/test/regress.c b/test/regress.c index c672fe8d..41b78b90 100644 --- a/test/regress.c +++ b/test/regress.c @@ -45,8 +45,8 @@ #include #include #include -#endif #include +#endif #include #include #include @@ -55,6 +55,7 @@ #include #include "event.h" +#include "evutil.h" #include "event-internal.h" #include "log.h" @@ -76,6 +77,10 @@ static struct event_base *global_base; #define TEST1 "this is a test" #define SECONDS 1 +#ifndef SHUT_WR +#define SHUT_WR 1 +#endif + void simple_read_cb(int fd, short event, void *arg) { @@ -246,7 +251,7 @@ setup_test(char *name) fprintf(stdout, "%s", name); - if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { fprintf(stderr, "%s: socketpair\n", __func__); exit(1); } diff --git a/test/regress_dns.c b/test/regress_dns.c index 85f3855d..a674754a 100644 --- a/test/regress_dns.c +++ b/test/regress_dns.c @@ -50,7 +50,9 @@ #ifdef HAVE_NETINET_IN6_H #include #endif +#ifdef HAVE_NETDB_H #include +#endif #include #include #include @@ -341,7 +343,7 @@ dns_server(void) evdns_close_server_port(port); evdns_shutdown(0); /* remove ourself as nameserver. */ #ifdef WIN32 - CloseHandle(sock); + closesocket(sock); #else close(sock); #endif diff --git a/test/regress_http.c b/test/regress_http.c index 4a59dff4..03fd7ec3 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -44,8 +44,8 @@ #include #include #include -#endif #include +#endif #include #include #include @@ -96,14 +96,34 @@ http_setup(short *pport, struct event_base *base) return (myhttp); } +#ifndef NI_MAXSERV +#define NI_MAXSERV 1024 +#endif + int http_connect(const char *address, u_short port) { /* Stupid code for connecting */ +#ifdef WIN32 + struct hostent *he; + struct sockaddr_in sin; +#else struct addrinfo ai, *aitop; char strport[NI_MAXSERV]; +#endif + struct sockaddr *sa; + int slen; int fd; +#ifdef WIN32 + if (!(he = gethostbyname(address))) { + event_warn("gethostbyname"); + } + memcpy(&sin.sin_addr, &he->h_addr, sizeof(struct in_addr)); + sin.sin_port = htons(port); + slen = sizeof(struct sockaddr_in); + sa = (struct sockaddr*)&sin; +#else memset(&ai, 0, sizeof (ai)); ai.ai_family = AF_INET; ai.ai_socktype = SOCK_STREAM; @@ -112,15 +132,20 @@ http_connect(const char *address, u_short port) event_warn("getaddrinfo"); return (-1); } + sa = aitop->ai_addr; + slen = aitop->ai_addrlen; +#endif fd = socket(AF_INET, SOCK_STREAM, 0); if (fd == -1) event_err(1, "socket failed"); - if (connect(fd, aitop->ai_addr, aitop->ai_addrlen) == -1) + if (connect(fd, sa, slen) == -1) event_err(1, "connect failed"); +#ifndef WIN32 freeaddrinfo(aitop); +#endif return (fd); } diff --git a/test/regress_rpc.c b/test/regress_rpc.c index a2dd8564..3361f9e9 100644 --- a/test/regress_rpc.c +++ b/test/regress_rpc.c @@ -44,8 +44,8 @@ #include #include #include -#endif #include +#endif #include #include #include diff --git a/test/test-eof.c b/test/test-eof.c index 4b1883c2..2dd4c894 100644 --- a/test/test-eof.c +++ b/test/test-eof.c @@ -7,10 +7,15 @@ #endif +#ifdef WIN32 +#include +#endif #include #include #include +#ifdef HAVE_SYS_SOCKET_H #include +#endif #include #include #include @@ -19,6 +24,7 @@ #include #include +#include int test_okay = 1; int called = 0; @@ -43,6 +49,10 @@ read_cb(int fd, short event, void *arg) called++; } +#ifndef SHUT_WR +#define SHUT_WR 1 +#endif + int main (int argc, char **argv) { @@ -50,7 +60,7 @@ main (int argc, char **argv) char *test = "test string"; int pair[2]; - if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) return (1); diff --git a/test/test-init.c b/test/test-init.c index 7d22de55..c368715f 100644 --- a/test/test-init.c +++ b/test/test-init.c @@ -10,7 +10,9 @@ #include #include #include +#ifdef HAVE_SYS_SOCKET_H #include +#endif #include #include #include diff --git a/test/test-time.c b/test/test-time.c index e21e8e0f..901b39a4 100644 --- a/test/test-time.c +++ b/test/test-time.c @@ -25,6 +25,16 @@ int called = 0; struct event *ev[NEVENT]; +int +rand_int(int n) +{ +#ifdef WIN32 + return (int)(rand() * n); +#else + return (int)(random() % n); +#endif +} + void time_cb(int fd, short event, void *arg) { @@ -35,9 +45,9 @@ time_cb(int fd, short event, void *arg) if (called < 10*NEVENT) { for (i = 0; i < 10; i++) { - j = random() % NEVENT; + j = rand_int(NEVENT); tv.tv_sec = 0; - tv.tv_usec = random() % 50000L; + tv.tv_usec = rand_int(50000); if (tv.tv_usec % 2) evtimer_add(ev[j], &tv); else @@ -61,7 +71,7 @@ main (int argc, char **argv) /* Initalize one event */ evtimer_set(ev[i], time_cb, ev[i]); tv.tv_sec = 0; - tv.tv_usec = random() % 50000L; + tv.tv_usec = rand_int(50000); evtimer_add(ev[i], &tv); } diff --git a/test/test-weof.c b/test/test-weof.c index 19f591e2..db2894ca 100644 --- a/test/test-weof.c +++ b/test/test-weof.c @@ -7,10 +7,15 @@ #endif +#ifdef WIN32 +#include +#endif #include #include #include +#ifdef HAVE_SYS_SOCKET_H #include +#endif #include #include #include @@ -20,6 +25,7 @@ #include #include +#include int pair[2]; int test_okay = 1; @@ -51,10 +57,12 @@ main (int argc, char **argv) { struct event ev; +#ifndef WIN32 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return (1); +#endif - if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) return (1); /* Initalize the event library */