2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
32 #define MAX_CLIENTS 4 /* Max number of clients */
33 #define NUM_TESTS 3 /* Number of tests performed */
34 #define FIRST_CHAR 'A' /* First character in transferred pattern */
35 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
36 #define BIND_TRIES 6 /* Number of bind() attempts */
37 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
38 after server initialization, if something hangs */
40 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
42 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
43 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
45 #define SERVERIP "127.0.0.1" /* IP to bind to */
46 #define SERVERPORT 9374 /* Port number to bind to */
48 #define wsa_ok(op, cond, msg) \
52 if ( !(cond tmp) ) err = WSAGetLastError(); \
53 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
57 /**************** Structs and typedefs ***************/
59 typedef struct thread_info
65 /* Information in the server about open client connections */
66 typedef struct sock_info
69 struct sockaddr_in addr;
70 struct sockaddr_in peer;
76 /* Test parameters for both server & client */
77 typedef struct test_params
81 const char *inet_addr;
88 /* server-specific test parameters */
89 typedef struct server_params
96 /* client-specific test parameters */
97 typedef struct client_params
104 /* This type combines all information for setting up a test scenario */
105 typedef struct test_setup
109 server_params srv_params;
111 client_params clt_params;
114 /* Thread local storage for server */
115 typedef struct server_memory
118 struct sockaddr_in addr;
119 sock_info sock[MAX_CLIENTS];
122 /* Thread local storage for client */
123 typedef struct client_memory
126 struct sockaddr_in addr;
131 /* SelectReadThread thread parameters */
132 typedef struct select_thread_params
136 } select_thread_params;
138 /**************** Static variables ***************/
140 static DWORD tls; /* Thread local storage index */
141 static HANDLE thread[1+MAX_CLIENTS];
142 static DWORD thread_id[1+MAX_CLIENTS];
143 static HANDLE server_ready;
144 static HANDLE client_ready[MAX_CLIENTS];
145 static int client_id;
147 /**************** General utility functions ***************/
149 static void set_so_opentype ( BOOL overlapped )
151 int optval = !overlapped, newval, len = sizeof (int);
153 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
154 (LPVOID) &optval, sizeof (optval) ) == 0,
155 "setting SO_OPENTYPE failed\n" );
156 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
157 (LPVOID) &newval, &len ) == 0,
158 "getting SO_OPENTYPE failed\n" );
159 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
162 static int set_blocking ( SOCKET s, BOOL blocking )
164 u_long val = !blocking;
165 return ioctlsocket ( s, FIONBIO, &val );
168 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
171 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
172 memset ( p, c, chunk_size );
175 static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
179 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
181 for ( i = 0; i < chunk_size; i++ )
182 if ( p[i] != c ) return p + i;
188 * This routine is called when a client / server does not expect any more data,
189 * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
191 static void read_zero_bytes ( SOCKET s )
195 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
197 ok ( n <= 0, "garbage data received: %d bytes\n", n );
200 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
202 char* last = buf + buflen, *p;
204 for ( p = buf; n > 0 && p < last; p += n )
205 n = send ( s, p, min ( sendlen, last - p ), 0 );
206 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
210 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
212 char* last = buf + buflen, *p;
214 for ( p = buf; n > 0 && p < last; p += n )
215 n = recv ( s, p, min ( recvlen, last - p ), 0 );
216 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
221 * Call this routine right after thread startup.
222 * SO_OPENTYPE must by 0, regardless what the server did.
224 static void check_so_opentype (void)
228 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
229 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
232 /**************** Server utility functions ***************/
235 * Even if we have closed our server socket cleanly,
236 * the OS may mark the address "in use" for some time -
237 * this happens with native Linux apps, too.
239 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
241 int err, wsaerr = 0, n_try = BIND_TRIES;
243 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
244 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
247 trace ( "address in use, waiting ...\n" );
248 Sleep ( 1000 * BIND_SLEEP );
250 ok ( err == 0, "failed to bind: %d\n", wsaerr );
253 static void server_start ( server_params *par )
256 test_params *gen = par->general;
257 server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
259 TlsSetValue ( tls, mem );
260 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
261 NULL, 0, par->sock_flags );
262 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
264 mem->addr.sin_family = AF_INET;
265 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
266 mem->addr.sin_port = htons ( gen->inet_port );
268 for (i = 0; i < MAX_CLIENTS; i++)
270 mem->sock[i].s = INVALID_SOCKET;
271 mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
272 mem->sock[i].n_recvd = 0;
273 mem->sock[i].n_sent = 0;
276 if ( gen->sock_type == SOCK_STREAM )
277 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
280 static void server_stop (void)
283 server_memory *mem = TlsGetValue ( tls );
285 for (i = 0; i < MAX_CLIENTS; i++ )
287 LocalFree ( (HANDLE) mem->sock[i].buf );
288 if ( mem->sock[i].s != INVALID_SOCKET )
289 closesocket ( mem->sock[i].s );
291 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
292 LocalFree ( (HANDLE) mem );
293 ExitThread ( GetCurrentThreadId () );
296 /**************** Client utilitiy functions ***************/
298 static void client_start ( client_params *par )
300 test_params *gen = par->general;
301 client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
303 TlsSetValue ( tls, mem );
305 WaitForSingleObject ( server_ready, INFINITE );
307 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
308 NULL, 0, par->sock_flags );
310 mem->addr.sin_family = AF_INET;
311 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
312 mem->addr.sin_port = htons ( gen->inet_port );
314 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
316 mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
317 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
318 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
320 SetEvent ( client_ready[client_id] );
321 /* Wait for the other clients to come up */
322 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
325 static void client_stop (void)
327 client_memory *mem = TlsGetValue ( tls );
328 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
329 LocalFree ( (HANDLE) mem->send_buf );
330 LocalFree ( (HANDLE) mem );
334 /**************** Servers ***************/
337 * simple_server: A very basic server doing synchronous IO.
339 static VOID WINAPI simple_server ( server_params *par )
341 test_params *gen = par->general;
343 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
344 id = GetCurrentThreadId();
347 trace ( "simple_server (%x) starting\n", id );
349 set_so_opentype ( FALSE ); /* non-overlapped */
350 server_start ( par );
351 mem = TlsGetValue ( tls );
353 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
354 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
356 trace ( "simple_server (%x) ready\n", id );
357 SetEvent ( server_ready ); /* notify clients */
359 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
361 trace ( "simple_server (%x): waiting for client\n", id );
363 /* accept a single connection */
364 tmp = sizeof ( mem->sock[0].peer );
365 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
366 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
368 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
369 "simple_server (%x): strange peer address\n", id );
371 /* Receive data & check it */
372 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
373 ok ( n_recvd == n_expected,
374 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
375 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
376 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
379 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
380 ok ( n_sent == n_expected,
381 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
384 read_zero_bytes ( mem->sock[0].s );
385 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
386 mem->sock[0].s = INVALID_SOCKET;
389 trace ( "simple_server (%x) exiting\n", id );
394 * select_server: A non-blocking server.
396 static VOID WINAPI select_server ( server_params *par )
398 test_params *gen = par->general;
400 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
401 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
402 n_set, delta, n_ready;
404 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
405 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
407 trace ( "select_server (%x) starting\n", id );
409 set_so_opentype ( FALSE ); /* non-overlapped */
410 server_start ( par );
411 mem = TlsGetValue ( tls );
413 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
414 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
416 trace ( "select_server (%x) ready\n", id );
417 SetEvent ( server_ready ); /* notify clients */
419 FD_ZERO ( &fds_openrecv );
420 FD_ZERO ( &fds_recv );
421 FD_ZERO ( &fds_send );
422 FD_ZERO ( &fds_opensend );
424 FD_SET ( mem->s, &fds_openrecv );
428 fds_recv = fds_openrecv;
429 fds_send = fds_opensend;
433 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
434 "select_server (%x): select() failed: %d\n" );
436 /* check for incoming requests */
437 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
440 trace ( "select_server (%x): accepting client connection\n", id );
442 /* accept a single connection */
443 tmp = sizeof ( mem->sock[n_connections].peer );
444 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
445 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
447 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
448 "select_server (%x): strange peer address\n", id );
450 /* add to list of open connections */
451 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
452 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
457 /* handle open requests */
459 for ( i = 0; i < n_connections; i++ )
461 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
464 if ( mem->sock[i].n_recvd < n_expected ) {
465 /* Receive data & check it */
466 n_recvd = recv ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_recvd, min ( n_expected - mem->sock[i].n_recvd, par->buflen ), 0 );
467 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
468 mem->sock[i].n_recvd += n_recvd;
470 if ( mem->sock[i].n_recvd == n_expected ) {
471 p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
472 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
473 FD_CLR ( mem->sock[i].s, &fds_openrecv );
476 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
480 /* only echo back what we've received */
481 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
483 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
486 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
488 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
489 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
490 mem->sock[i].n_sent += n_sent;
492 if ( mem->sock[i].n_sent == n_expected ) {
493 FD_CLR ( mem->sock[i].s, &fds_opensend );
496 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
501 /* check that select returned the correct number of ready sockets */
502 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
504 /* check if all clients are done */
505 if ( ( fds_opensend.fd_count == 0 )
506 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
507 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
512 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
515 read_zero_bytes ( mem->sock[i].s );
516 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
517 mem->sock[i].s = INVALID_SOCKET;
520 trace ( "select_server (%x) exiting\n", id );
524 /**************** Clients ***************/
527 * simple_client: A very basic client doing synchronous IO.
529 static VOID WINAPI simple_client ( client_params *par )
531 test_params *gen = par->general;
533 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
536 id = GetCurrentThreadId();
537 trace ( "simple_client (%x): starting\n", id );
538 /* wait here because we want to call set_so_opentype before creating a socket */
539 WaitForSingleObject ( server_ready, INFINITE );
540 trace ( "simple_client (%x): server ready\n", id );
542 check_so_opentype ();
543 set_so_opentype ( FALSE ); /* non-overlapped */
544 client_start ( par );
545 mem = TlsGetValue ( tls );
548 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
549 0 ==, "simple_client (%x): connect error: %d\n" );
550 ok ( set_blocking ( mem->s, TRUE ) == 0,
551 "simple_client (%x): failed to set blocking mode\n", id );
552 trace ( "simple_client (%x) connected\n", id );
554 /* send data to server */
555 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
556 ok ( n_sent == n_expected,
557 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
559 /* shutdown send direction */
560 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
562 /* Receive data echoed back & check it */
563 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
564 ok ( n_recvd == n_expected,
565 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
568 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
569 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
572 read_zero_bytes ( mem->s );
573 trace ( "simple_client (%x) exiting\n", id );
578 * event_client: An event-driven client
580 static void WINAPI event_client ( client_params *par )
582 test_params *gen = par->general;
584 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
587 WSANETWORKEVENTS wsa_events;
588 char *send_last, *recv_last, *send_p, *recv_p;
589 long mask = FD_READ | FD_WRITE | FD_CLOSE;
591 trace ( "event_client (%x): starting\n", id );
592 client_start ( par );
593 trace ( "event_client (%x): server ready\n", id );
595 mem = TlsGetValue ( tls );
597 /* Prepare event notification for connect, makes socket nonblocking */
598 event = WSACreateEvent ();
599 WSAEventSelect ( mem->s, event, FD_CONNECT );
600 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
601 if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
602 ok ( 0, "event_client (%x): connect error: %d\n", id, err );
604 tmp = WaitForSingleObject ( event, INFINITE );
605 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
606 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
607 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
609 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
610 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
613 trace ( "event_client (%x) connected\n", id );
615 WSAEventSelect ( mem->s, event, mask );
617 recv_p = mem->recv_buf;
618 recv_last = mem->recv_buf + n_expected;
619 send_p = mem->send_buf;
620 send_last = mem->send_buf + n_expected;
624 err = WaitForSingleObject ( event, INFINITE );
625 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
627 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
628 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
630 if ( wsa_events.lNetworkEvents & FD_WRITE )
632 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
633 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
638 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
641 err = WSAGetLastError ();
642 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
647 while ( n >= 0 && send_p < send_last );
649 if ( send_p == send_last )
651 trace ( "event_client (%x): all data sent - shutdown\n", id );
652 shutdown ( mem->s, SD_SEND );
654 WSAEventSelect ( mem->s, event, mask );
657 if ( wsa_events.lNetworkEvents & FD_READ )
659 err = wsa_events.iErrorCode[ FD_READ_BIT ];
660 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
661 if ( err != 0 ) break;
663 /* First read must succeed */
664 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
665 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
669 if ( recv_p == recv_last )
672 trace ( "event_client (%x): all data received\n", id );
673 WSAEventSelect ( mem->s, event, mask );
676 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
677 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
678 ok ( 0, "event_client (%x): read error: %d\n", id, err );
682 if ( wsa_events.lNetworkEvents & FD_CLOSE )
684 trace ( "event_client (%x): close event\n", id );
685 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
686 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
691 ok ( send_p == send_last,
692 "simple_client (%x): sent less data than expected: %d of %d\n",
693 id, send_p - mem->send_buf, n_expected );
694 ok ( recv_p == recv_last,
695 "simple_client (%x): received less data than expected: %d of %d\n",
696 id, recv_p - mem->recv_buf, n_expected );
697 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
698 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
701 WSACloseEvent ( event );
702 trace ( "event_client (%x) exiting\n", id );
706 /**************** Main program utility functions ***************/
708 static void Init (void)
710 WORD ver = MAKEWORD (2, 2);
713 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
717 static void Exit (void)
722 err = WSAGetLastError();
723 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
725 err = WSAGetLastError();
726 ok ( ret == SOCKET_ERROR && err == WSANOTINITIALISED,
727 "WSACleanup returned %d GetLastError is %d\n", ret, err);
730 static void StartServer (LPTHREAD_START_ROUTINE routine,
731 test_params *general, server_params *par)
733 par->general = general;
734 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
735 ok ( thread[0] != NULL, "Failed to create server thread\n" );
738 static void StartClients (LPTHREAD_START_ROUTINE routine,
739 test_params *general, client_params *par)
742 par->general = general;
743 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
746 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
747 ok ( thread[i] != NULL, "Failed to create client thread\n" );
748 /* Make sure the client is up and running */
749 WaitForSingleObject ( client_ready[client_id], INFINITE );
753 static void do_test( test_setup *test )
755 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
758 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
759 for (i = 0; i <= n; i++)
760 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
762 StartServer ( test->srv, &test->general, &test->srv_params );
763 StartClients ( test->clt, &test->general, &test->clt_params );
764 WaitForSingleObject ( server_ready, INFINITE );
766 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
767 ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ,
768 "some threads have not completed: %x\n", wait );
770 if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
772 for (i = 0; i <= n; i++)
774 trace ("terminating thread %08x\n", thread_id[i]);
775 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
776 TerminateThread ( thread [i], 0 );
779 CloseHandle ( server_ready );
780 for (i = 0; i <= n; i++)
781 CloseHandle ( client_ready[i] );
784 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
785 /* optname = SO_LINGER */
786 LINGER linger_testvals[] = {
793 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
794 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
795 bug in the linux kernel (fixed in 2.6.8) */
796 #define SOCKTIMEOUT2 997000 /* 997 seconds */
798 static void test_set_getsockopt(void)
806 s = socket(AF_INET, SOCK_STREAM, 0);
807 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
808 if( s == INVALID_SOCKET) return;
810 timeout = SOCKTIMEOUT1;
811 size = sizeof(timeout);
812 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
814 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
815 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
816 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
818 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
819 size = sizeof(timeout);
820 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
822 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
823 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
824 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
826 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
827 size = sizeof(lingval);
828 lingval = linger_testvals[i];
829 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
831 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
832 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
833 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
834 (lingval.l_linger == linger_testvals[i].l_linger ||
835 (!lingval.l_linger && !linger_testvals[i].l_onoff))
836 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
837 lingval.l_onoff, lingval.l_linger,
838 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
843 static void test_so_reuseaddr(void)
845 struct sockaddr_in saddr;
847 unsigned int rc,reuse;
850 saddr.sin_family = AF_INET;
851 saddr.sin_port = htons(9375);
852 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
854 s1=socket(AF_INET, SOCK_STREAM, 0);
855 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
856 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
857 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
859 s2=socket(AF_INET, SOCK_STREAM, 0);
860 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
864 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
865 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
867 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
868 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
871 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
872 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
875 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
876 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
883 /************* Array containing the tests to run **********/
885 #define STD_STREAM_SOCKET \
891 static test_setup tests [NUM_TESTS] =
893 /* Test 0: synchronous client and server */
914 /* Test 1: event-driven client, synchronous server */
935 /* Test 2: synchronous client, non-blocking server via select() */
958 static void test_UDP(void)
960 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
961 possible that this test fails due to dropped packets. */
963 /* peer 0 receives data from all other peers */
964 struct sock_info peer[NUM_UDP_PEERS];
966 int ss, i, n_recv, n_sent;
968 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
969 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
971 peer[i].addr.sin_family = AF_INET;
972 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
975 peer[i].addr.sin_port = htons ( SERVERPORT );
977 peer[i].addr.sin_port = htons ( 0 );
980 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
982 /* test getsockname() to get peer's port */
983 ss = sizeof ( peer[i].addr );
984 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
985 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
988 /* test getsockname() */
989 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
991 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
992 /* send client's ip */
993 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
994 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
995 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
998 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
999 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1000 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1001 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1005 static void WINAPI do_getservbyname( HANDLE *starttest )
1011 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1014 struct servent *pserv[2];
1016 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1018 /* ensure that necessary buffer resizes are completed */
1019 for ( j = 0; j < 2; j++) {
1020 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1023 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1024 for ( j = 0; j < 2; j++ ) {
1025 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1026 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1027 ok ( pserv[j]->s_port == htons(serv[j].port), "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
1028 ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ), "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
1029 ok ( !strcmp ( pserv[j]->s_name, serv[j].name ), "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
1032 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1036 static void test_getservbyname(void)
1039 HANDLE starttest, thread[NUM_THREADS];
1040 DWORD thread_id[NUM_THREADS];
1042 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1044 /* create threads */
1045 for ( i = 0; i < NUM_THREADS; i++ ) {
1046 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1049 /* signal threads to start */
1050 SetEvent ( starttest );
1052 for ( i = 0; i < NUM_THREADS; i++) {
1053 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1057 static void test_WSAAddressToStringA(void)
1062 SOCKADDR_IN sockaddr;
1063 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1065 CHAR expect1[] = "0.0.0.0";
1066 CHAR expect2[] = "255.255.255.255";
1067 CHAR expect3[] = "0.0.0.0:65535";
1068 CHAR expect4[] = "255.255.255.255:65535";
1072 sockaddr.sin_family = AF_INET;
1073 sockaddr.sin_port = 0;
1074 sockaddr.sin_addr.s_addr = 0;
1076 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1077 GLE = WSAGetLastError();
1078 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1079 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1082 len = sizeof(address);
1084 sockaddr.sin_family = AF_INET;
1085 sockaddr.sin_port = 0;
1086 sockaddr.sin_addr.s_addr = 0;
1088 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1089 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1091 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1093 len = sizeof(address);
1095 sockaddr.sin_family = AF_INET;
1096 sockaddr.sin_port = 0;
1097 sockaddr.sin_addr.s_addr = 0xffffffff;
1099 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1100 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1102 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1104 len = sizeof(address);
1106 sockaddr.sin_family = AF_INET;
1107 sockaddr.sin_port = 0xffff;
1108 sockaddr.sin_addr.s_addr = 0;
1110 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1111 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1113 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1115 len = sizeof(address);
1117 sockaddr.sin_family = AF_INET;
1118 sockaddr.sin_port = 0xffff;
1119 sockaddr.sin_addr.s_addr = 0xffffffff;
1121 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1122 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1124 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1127 static void test_WSAAddressToStringW(void)
1132 SOCKADDR_IN sockaddr;
1133 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1135 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1136 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1137 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1138 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1139 '6', '5', '5', '3', '5', 0 };
1143 sockaddr.sin_family = AF_INET;
1144 sockaddr.sin_port = 0;
1145 sockaddr.sin_addr.s_addr = 0;
1147 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1148 GLE = WSAGetLastError();
1149 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1150 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1153 len = sizeof(address);
1155 sockaddr.sin_family = AF_INET;
1156 sockaddr.sin_port = 0;
1157 sockaddr.sin_addr.s_addr = 0;
1159 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1160 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1162 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1164 len = sizeof(address);
1166 sockaddr.sin_family = AF_INET;
1167 sockaddr.sin_port = 0;
1168 sockaddr.sin_addr.s_addr = 0xffffffff;
1170 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1171 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1173 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1175 len = sizeof(address);
1177 sockaddr.sin_family = AF_INET;
1178 sockaddr.sin_port = 0xffff;
1179 sockaddr.sin_addr.s_addr = 0;
1181 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1182 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1184 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1186 len = sizeof(address);
1188 sockaddr.sin_family = AF_INET;
1189 sockaddr.sin_port = 0xffff;
1190 sockaddr.sin_addr.s_addr = 0xffffffff;
1192 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1193 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1195 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1198 static void test_WSAStringToAddressA(void)
1201 SOCKADDR_IN sockaddr;
1204 CHAR address1[] = "0.0.0.0";
1205 CHAR address2[] = "127.127.127.127";
1206 CHAR address3[] = "255.255.255.255";
1207 CHAR address4[] = "127.127.127.127:65535";
1208 CHAR address5[] = "255.255.255.255:65535";
1211 sockaddr.sin_family = AF_INET;
1213 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1214 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1215 WSAGetLastError() );
1217 len = sizeof(sockaddr);
1218 sockaddr.sin_port = 0;
1219 sockaddr.sin_addr.s_addr = 0;
1221 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1222 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1223 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1225 len = sizeof(sockaddr);
1226 sockaddr.sin_port = 0;
1227 sockaddr.sin_addr.s_addr = 0;
1229 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1230 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1231 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1233 len = sizeof(sockaddr);
1234 sockaddr.sin_port = 0;
1235 sockaddr.sin_addr.s_addr = 0;
1237 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1238 GLE = WSAGetLastError();
1239 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1240 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1241 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1243 len = sizeof(sockaddr);
1244 sockaddr.sin_port = 0;
1245 sockaddr.sin_addr.s_addr = 0;
1247 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1248 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1249 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1251 len = sizeof(sockaddr);
1252 sockaddr.sin_port = 0;
1253 sockaddr.sin_addr.s_addr = 0;
1255 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1256 GLE = WSAGetLastError();
1257 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1258 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1259 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1262 static void test_WSAStringToAddressW(void)
1265 SOCKADDR_IN sockaddr;
1268 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1269 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1270 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1271 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1272 ':', '6', '5', '5', '3', '5', 0 };
1273 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1274 '6', '5', '5', '3', '5', 0 };
1277 sockaddr.sin_family = AF_INET;
1279 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1280 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1281 WSAGetLastError() );
1283 len = sizeof(sockaddr);
1284 sockaddr.sin_port = 0;
1285 sockaddr.sin_addr.s_addr = 0;
1287 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1288 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1289 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1291 len = sizeof(sockaddr);
1292 sockaddr.sin_port = 0;
1293 sockaddr.sin_addr.s_addr = 0;
1295 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1296 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1297 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1299 len = sizeof(sockaddr);
1300 sockaddr.sin_port = 0;
1301 sockaddr.sin_addr.s_addr = 0;
1303 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1304 GLE = WSAGetLastError();
1305 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1306 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1307 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1309 len = sizeof(sockaddr);
1310 sockaddr.sin_port = 0;
1311 sockaddr.sin_addr.s_addr = 0;
1313 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1314 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1315 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1317 len = sizeof(sockaddr);
1318 sockaddr.sin_port = 0;
1319 sockaddr.sin_addr.s_addr = 0;
1321 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1322 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1323 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1324 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1327 static VOID WINAPI SelectReadThread(select_thread_params *par)
1331 struct sockaddr_in addr;
1332 struct timeval select_timeout;
1335 FD_SET(par->s, &readfds);
1336 select_timeout.tv_sec=5;
1337 select_timeout.tv_usec=0;
1338 addr.sin_family = AF_INET;
1339 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1340 addr.sin_port = htons(SERVERPORT);
1342 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1343 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1345 SetEvent(server_ready);
1346 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1347 par->ReadKilled = (ret == 1);
1350 static void test_select(void)
1352 SOCKET fdRead, fdWrite;
1353 fd_set readfds, writefds, exceptfds;
1356 struct timeval select_timeout;
1357 select_thread_params thread_params;
1358 HANDLE thread_handle;
1360 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1361 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1362 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1363 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1367 FD_ZERO(&exceptfds);
1368 FD_SET(fdRead, &readfds);
1369 FD_SET(fdWrite, &writefds);
1370 FD_SET(fdRead, &exceptfds);
1371 FD_SET(fdWrite, &exceptfds);
1372 select_timeout.tv_sec=0;
1373 select_timeout.tv_usec=500;
1376 if (fdWrite > maxfd)
1380 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1381 ok ( (ret == 0), "select should not return any socket handles\n");
1382 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1383 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1386 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1387 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1390 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1392 ret = closesocket(fdWrite);
1393 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1395 thread_params.s = fdRead;
1396 thread_params.ReadKilled = FALSE;
1397 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1398 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, NULL );
1399 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1401 WaitForSingleObject (server_ready, INFINITE);
1403 ret = closesocket(fdRead);
1404 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1406 WaitForSingleObject (thread_handle, 1000);
1407 ok ( (thread_params.ReadKilled), "closesocket did not wakeup select\n");
1411 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1413 struct sockaddr_in address;
1414 int len = sizeof(address);
1415 SOCKET client_socket;
1417 SetEvent(server_ready);
1418 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1419 if (client_socket != INVALID_SOCKET)
1420 closesocket(client_socket);
1421 par->ReadKilled = (client_socket == INVALID_SOCKET);
1425 static void test_accept(void)
1428 SOCKET server_socket = INVALID_SOCKET;
1429 struct sockaddr_in address;
1430 select_thread_params thread_params;
1431 HANDLE thread_handle = NULL;
1433 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1434 if (server_socket == INVALID_SOCKET)
1436 trace("error creating server socket: %d\n", WSAGetLastError());
1440 memset(&address, 0, sizeof(address));
1441 address.sin_family = AF_INET;
1442 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1445 trace("error binding server socket: %d\n", WSAGetLastError());
1449 ret = listen(server_socket, 1);
1452 trace("error making server socket listen: %d\n", WSAGetLastError());
1456 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1457 if (server_ready == INVALID_HANDLE_VALUE)
1459 trace("error creating event: %d\n", GetLastError());
1463 thread_params.s = server_socket;
1464 thread_params.ReadKilled = FALSE;
1465 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
1466 &thread_params, 0, NULL);
1467 if (thread_handle == NULL)
1469 trace("error creating thread: %d\n", GetLastError());
1473 WaitForSingleObject(server_ready, INFINITE);
1475 ret = closesocket(server_socket);
1478 trace("closesocket failed: %d\n", WSAGetLastError());
1482 WaitForSingleObject(thread_handle, 1000);
1483 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
1486 if (thread_handle != NULL)
1487 CloseHandle(thread_handle);
1488 if (server_ready != INVALID_HANDLE_VALUE)
1489 CloseHandle(server_ready);
1490 if (server_socket != INVALID_SOCKET)
1491 closesocket(server_socket);
1494 static void test_extendedSocketOptions(void)
1498 struct sockaddr_in sa;
1499 int sa_len = sizeof(struct sockaddr_in);
1500 int optval, optlen = sizeof(int), ret;
1504 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1505 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1509 memset(&sa, 0, sa_len);
1511 sa.sin_family = AF_INET;
1512 sa.sin_port = htons(0);
1513 sa.sin_addr.s_addr = htonl(INADDR_ANY);
1515 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0){
1516 trace("Creating the socket failed: %d\n", WSAGetLastError());
1521 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1522 trace("Failed to bind socket: %d\n", WSAGetLastError());
1528 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
1530 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
1531 ok(optval == 65507, "SO_MAX_MSG_SIZE reported %d, expected 65507\n", optval);
1533 optlen = sizeof(LINGER);
1534 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1536 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
1541 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) < 0){
1542 trace("Creating the socket failed: %d\n", WSAGetLastError());
1547 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1548 trace("Failed to bind socket: %d\n", WSAGetLastError());
1554 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1555 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
1557 optlen = sizeof(BOOL);
1558 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
1559 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
1560 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
1561 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
1562 bool_opt_val, linger_val.l_onoff);
1568 static void test_getsockname(void)
1572 struct sockaddr_in sa_set, sa_get;
1573 int sa_set_len = sizeof(struct sockaddr_in);
1574 int sa_get_len = sa_set_len;
1575 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
1577 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1578 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1582 memset(&sa_set, 0, sa_set_len);
1584 sa_set.sin_family = AF_INET;
1585 sa_set.sin_port = htons(0);
1586 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
1588 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) < 0){
1589 trace("Creating the socket failed: %d\n", WSAGetLastError());
1594 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
1595 trace("Failed to bind socket: %d\n", WSAGetLastError());
1601 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
1602 trace("Failed to call getsockname: %d\n", WSAGetLastError());
1608 ok(memcmp(sa_get.sin_zero, null_padding, 8) == 0,
1609 "getsockname did not zero the sockaddr_in structure\n");
1615 static void test_inet_addr(void)
1619 addr = inet_addr(NULL);
1620 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
1623 static DWORD WINAPI drain_socket_thread(LPVOID arg)
1626 SOCKET sock = *(SOCKET*)arg;
1628 while (recv(sock, buffer, sizeof(buffer), 0) > 0)
1633 static void test_send(void)
1635 SOCKET src = INVALID_SOCKET;
1636 SOCKET server = INVALID_SOCKET;
1637 SOCKET dst = INVALID_SOCKET;
1638 HANDLE hThread = NULL;
1639 struct sockaddr_in addr;
1641 const int buflen = 1024*1024;
1642 char *buffer = NULL;
1645 src = socket(AF_INET, SOCK_STREAM, 0);
1646 if (src == INVALID_SOCKET)
1648 ok(0, "socket failed, error %d\n", WSAGetLastError());
1652 server = socket(AF_INET, SOCK_STREAM, 0);
1653 if (server == INVALID_SOCKET)
1655 ok(0, "socket failed, error %d\n", WSAGetLastError());
1659 memset(&addr, 0, sizeof(addr));
1660 addr.sin_family = AF_INET;
1661 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1662 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
1665 ok(0, "bind failed, error %d\n", WSAGetLastError());
1670 ret = getsockname(server, (struct sockaddr*)&addr, &len);
1673 ok(0, "getsockname failed, error %d\n", WSAGetLastError());
1677 ret = listen(server, 1);
1680 ok(0, "listen failed, error %d\n", WSAGetLastError());
1684 ret = connect(src, (struct sockaddr*)&addr, sizeof(addr));
1687 ok(0, "connect failed, error %d\n", WSAGetLastError());
1692 dst = accept(server, (struct sockaddr*)&addr, &len);
1693 if (dst == INVALID_SOCKET)
1695 ok(0, "accept failed, error %d\n", WSAGetLastError());
1699 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, NULL);
1700 if (hThread == NULL)
1702 ok(0, "CreateThread failed, error %d\n", GetLastError());
1706 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
1709 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
1713 ret = send(src, buffer, buflen, 0);
1715 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
1717 ok(0, "send failed, error %d\n", WSAGetLastError());
1720 if (src != INVALID_SOCKET)
1722 if (server != INVALID_SOCKET)
1723 closesocket(server);
1724 if (dst != INVALID_SOCKET)
1726 if (hThread != NULL)
1727 CloseHandle(hThread);
1729 HeapFree(GetProcessHeap(), 0, buffer);
1732 /**************** Main program ***************/
1739 test_set_getsockopt();
1740 test_so_reuseaddr();
1741 test_extendedSocketOptions();
1743 for (i = 0; i < NUM_TESTS; i++)
1745 trace ( " **** STARTING TEST %d ****\n", i );
1746 do_test ( &tests[i] );
1747 trace ( " **** TEST %d COMPLETE ****\n", i );
1752 test_getservbyname();
1754 test_WSAAddressToStringA();
1755 test_WSAAddressToStringW();
1757 test_WSAStringToAddressA();
1758 test_WSAStringToAddressW();