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
29 #include "wine/test.h"
33 #define MAX_CLIENTS 4 /* Max number of clients */
34 #define NUM_TESTS 3 /* Number of tests performed */
35 #define FIRST_CHAR 'A' /* First character in transferred pattern */
36 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
37 #define BIND_TRIES 6 /* Number of bind() attempts */
38 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
39 after server initialization, if something hangs */
41 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
43 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
44 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
46 #define SERVERIP "127.0.0.1" /* IP to bind to */
47 #define SERVERPORT 9374 /* Port number to bind to */
49 #define wsa_ok(op, cond, msg) \
53 if ( !(cond tmp) ) err = WSAGetLastError(); \
54 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
58 /**************** Structs and typedefs ***************/
60 typedef struct thread_info
66 /* Information in the server about open client connections */
67 typedef struct sock_info
70 struct sockaddr_in addr;
71 struct sockaddr_in peer;
77 /* Test parameters for both server & client */
78 typedef struct test_params
82 const char *inet_addr;
89 /* server-specific test parameters */
90 typedef struct server_params
97 /* client-specific test parameters */
98 typedef struct client_params
100 test_params *general;
105 /* This type combines all information for setting up a test scenario */
106 typedef struct test_setup
110 server_params srv_params;
112 client_params clt_params;
115 /* Thread local storage for server */
116 typedef struct server_memory
119 struct sockaddr_in addr;
120 sock_info sock[MAX_CLIENTS];
123 /* Thread local storage for client */
124 typedef struct client_memory
127 struct sockaddr_in addr;
132 /* SelectReadThread thread parameters */
133 typedef struct select_thread_params
137 } select_thread_params;
139 /**************** Static variables ***************/
141 static DWORD tls; /* Thread local storage index */
142 static HANDLE thread[1+MAX_CLIENTS];
143 static DWORD thread_id[1+MAX_CLIENTS];
144 static HANDLE server_ready;
145 static HANDLE client_ready[MAX_CLIENTS];
146 static int client_id;
148 /**************** General utility functions ***************/
150 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
152 SOCKET server = INVALID_SOCKET;
153 struct sockaddr_in addr;
157 *src = INVALID_SOCKET;
158 *dst = INVALID_SOCKET;
160 *src = socket(AF_INET, SOCK_STREAM, 0);
161 if (*src == INVALID_SOCKET)
164 server = socket(AF_INET, SOCK_STREAM, 0);
165 if (server == INVALID_SOCKET)
168 memset(&addr, 0, sizeof(addr));
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
171 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
176 ret = getsockname(server, (struct sockaddr*)&addr, &len);
180 ret = listen(server, 1);
184 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
189 *dst = accept(server, (struct sockaddr*)&addr, &len);
192 if (server != INVALID_SOCKET)
194 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
201 static void set_so_opentype ( BOOL overlapped )
203 int optval = !overlapped, newval, len = sizeof (int);
205 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
206 (LPVOID) &optval, sizeof (optval) ) == 0,
207 "setting SO_OPENTYPE failed\n" );
208 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
209 (LPVOID) &newval, &len ) == 0,
210 "getting SO_OPENTYPE failed\n" );
211 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
214 static int set_blocking ( SOCKET s, BOOL blocking )
216 u_long val = !blocking;
217 return ioctlsocket ( s, FIONBIO, &val );
220 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
223 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
224 memset ( p, c, chunk_size );
227 static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
231 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
233 for ( i = 0; i < chunk_size; i++ )
234 if ( p[i] != c ) return p + i;
240 * This routine is called when a client / server does not expect any more data,
241 * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
243 static void read_zero_bytes ( SOCKET s )
247 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
249 ok ( n <= 0, "garbage data received: %d bytes\n", n );
252 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
254 char* last = buf + buflen, *p;
256 for ( p = buf; n > 0 && p < last; p += n )
257 n = send ( s, p, min ( sendlen, last - p ), 0 );
258 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
262 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
264 char* last = buf + buflen, *p;
266 for ( p = buf; n > 0 && p < last; p += n )
267 n = recv ( s, p, min ( recvlen, last - p ), 0 );
268 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
273 * Call this routine right after thread startup.
274 * SO_OPENTYPE must by 0, regardless what the server did.
276 static void check_so_opentype (void)
280 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
281 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
284 /**************** Server utility functions ***************/
287 * Even if we have closed our server socket cleanly,
288 * the OS may mark the address "in use" for some time -
289 * this happens with native Linux apps, too.
291 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
293 int err, wsaerr = 0, n_try = BIND_TRIES;
295 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
296 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
299 trace ( "address in use, waiting ...\n" );
300 Sleep ( 1000 * BIND_SLEEP );
302 ok ( err == 0, "failed to bind: %d\n", wsaerr );
305 static void server_start ( server_params *par )
308 test_params *gen = par->general;
309 server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
311 TlsSetValue ( tls, mem );
312 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
313 NULL, 0, par->sock_flags );
314 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
316 mem->addr.sin_family = AF_INET;
317 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
318 mem->addr.sin_port = htons ( gen->inet_port );
320 for (i = 0; i < MAX_CLIENTS; i++)
322 mem->sock[i].s = INVALID_SOCKET;
323 mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
324 mem->sock[i].n_recvd = 0;
325 mem->sock[i].n_sent = 0;
328 if ( gen->sock_type == SOCK_STREAM )
329 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
332 static void server_stop (void)
335 server_memory *mem = TlsGetValue ( tls );
337 for (i = 0; i < MAX_CLIENTS; i++ )
339 LocalFree ( (HANDLE) mem->sock[i].buf );
340 if ( mem->sock[i].s != INVALID_SOCKET )
341 closesocket ( mem->sock[i].s );
343 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
344 LocalFree ( (HANDLE) mem );
345 ExitThread ( GetCurrentThreadId () );
348 /**************** Client utilitiy functions ***************/
350 static void client_start ( client_params *par )
352 test_params *gen = par->general;
353 client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
355 TlsSetValue ( tls, mem );
357 WaitForSingleObject ( server_ready, INFINITE );
359 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
360 NULL, 0, par->sock_flags );
362 mem->addr.sin_family = AF_INET;
363 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
364 mem->addr.sin_port = htons ( gen->inet_port );
366 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
368 mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
369 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
370 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
372 SetEvent ( client_ready[client_id] );
373 /* Wait for the other clients to come up */
374 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
377 static void client_stop (void)
379 client_memory *mem = TlsGetValue ( tls );
380 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
381 LocalFree ( (HANDLE) mem->send_buf );
382 LocalFree ( (HANDLE) mem );
386 /**************** Servers ***************/
389 * simple_server: A very basic server doing synchronous IO.
391 static VOID WINAPI simple_server ( server_params *par )
393 test_params *gen = par->general;
395 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
396 id = GetCurrentThreadId();
399 trace ( "simple_server (%x) starting\n", id );
401 set_so_opentype ( FALSE ); /* non-overlapped */
402 server_start ( par );
403 mem = TlsGetValue ( tls );
405 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
406 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
408 trace ( "simple_server (%x) ready\n", id );
409 SetEvent ( server_ready ); /* notify clients */
411 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
413 trace ( "simple_server (%x): waiting for client\n", id );
415 /* accept a single connection */
416 tmp = sizeof ( mem->sock[0].peer );
417 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
418 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
420 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
421 "simple_server (%x): strange peer address\n", id );
423 /* Receive data & check it */
424 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
425 ok ( n_recvd == n_expected,
426 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
427 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
428 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
431 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
432 ok ( n_sent == n_expected,
433 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
436 read_zero_bytes ( mem->sock[0].s );
437 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
438 mem->sock[0].s = INVALID_SOCKET;
441 trace ( "simple_server (%x) exiting\n", id );
446 * select_server: A non-blocking server.
448 static VOID WINAPI select_server ( server_params *par )
450 test_params *gen = par->general;
452 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
453 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
454 n_set, delta, n_ready;
456 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
457 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
459 trace ( "select_server (%x) starting\n", id );
461 set_so_opentype ( FALSE ); /* non-overlapped */
462 server_start ( par );
463 mem = TlsGetValue ( tls );
465 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
466 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
468 trace ( "select_server (%x) ready\n", id );
469 SetEvent ( server_ready ); /* notify clients */
471 FD_ZERO ( &fds_openrecv );
472 FD_ZERO ( &fds_recv );
473 FD_ZERO ( &fds_send );
474 FD_ZERO ( &fds_opensend );
476 FD_SET ( mem->s, &fds_openrecv );
480 fds_recv = fds_openrecv;
481 fds_send = fds_opensend;
485 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
486 "select_server (%x): select() failed: %d\n" );
488 /* check for incoming requests */
489 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
492 trace ( "select_server (%x): accepting client connection\n", id );
494 /* accept a single connection */
495 tmp = sizeof ( mem->sock[n_connections].peer );
496 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
497 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
499 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
500 "select_server (%x): strange peer address\n", id );
502 /* add to list of open connections */
503 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
504 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
509 /* handle open requests */
511 for ( i = 0; i < n_connections; i++ )
513 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
516 if ( mem->sock[i].n_recvd < n_expected ) {
517 /* Receive data & check it */
518 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 );
519 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
520 mem->sock[i].n_recvd += n_recvd;
522 if ( mem->sock[i].n_recvd == n_expected ) {
523 p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
524 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
525 FD_CLR ( mem->sock[i].s, &fds_openrecv );
528 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
532 /* only echo back what we've received */
533 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
535 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
538 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
540 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
541 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
542 mem->sock[i].n_sent += n_sent;
544 if ( mem->sock[i].n_sent == n_expected ) {
545 FD_CLR ( mem->sock[i].s, &fds_opensend );
548 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
553 /* check that select returned the correct number of ready sockets */
554 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
556 /* check if all clients are done */
557 if ( ( fds_opensend.fd_count == 0 )
558 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
559 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
564 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
567 read_zero_bytes ( mem->sock[i].s );
568 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
569 mem->sock[i].s = INVALID_SOCKET;
572 trace ( "select_server (%x) exiting\n", id );
576 /**************** Clients ***************/
579 * simple_client: A very basic client doing synchronous IO.
581 static VOID WINAPI simple_client ( client_params *par )
583 test_params *gen = par->general;
585 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
588 id = GetCurrentThreadId();
589 trace ( "simple_client (%x): starting\n", id );
590 /* wait here because we want to call set_so_opentype before creating a socket */
591 WaitForSingleObject ( server_ready, INFINITE );
592 trace ( "simple_client (%x): server ready\n", id );
594 check_so_opentype ();
595 set_so_opentype ( FALSE ); /* non-overlapped */
596 client_start ( par );
597 mem = TlsGetValue ( tls );
600 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
601 0 ==, "simple_client (%x): connect error: %d\n" );
602 ok ( set_blocking ( mem->s, TRUE ) == 0,
603 "simple_client (%x): failed to set blocking mode\n", id );
604 trace ( "simple_client (%x) connected\n", id );
606 /* send data to server */
607 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
608 ok ( n_sent == n_expected,
609 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
611 /* shutdown send direction */
612 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
614 /* Receive data echoed back & check it */
615 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
616 ok ( n_recvd == n_expected,
617 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
620 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
621 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
624 read_zero_bytes ( mem->s );
625 trace ( "simple_client (%x) exiting\n", id );
630 * event_client: An event-driven client
632 static void WINAPI event_client ( client_params *par )
634 test_params *gen = par->general;
636 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
639 WSANETWORKEVENTS wsa_events;
640 char *send_last, *recv_last, *send_p, *recv_p;
641 long mask = FD_READ | FD_WRITE | FD_CLOSE;
643 trace ( "event_client (%x): starting\n", id );
644 client_start ( par );
645 trace ( "event_client (%x): server ready\n", id );
647 mem = TlsGetValue ( tls );
649 /* Prepare event notification for connect, makes socket nonblocking */
650 event = WSACreateEvent ();
651 WSAEventSelect ( mem->s, event, FD_CONNECT );
652 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
654 err = WSAGetLastError ();
655 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
656 tmp = WaitForSingleObject ( event, INFINITE );
657 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
658 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
659 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
660 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
661 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
665 trace ( "event_client (%x) connected\n", id );
667 WSAEventSelect ( mem->s, event, mask );
669 recv_p = mem->recv_buf;
670 recv_last = mem->recv_buf + n_expected;
671 send_p = mem->send_buf;
672 send_last = mem->send_buf + n_expected;
676 err = WaitForSingleObject ( event, INFINITE );
677 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
679 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
680 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
682 if ( wsa_events.lNetworkEvents & FD_WRITE )
684 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
685 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
690 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
693 err = WSAGetLastError ();
694 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
699 while ( n >= 0 && send_p < send_last );
701 if ( send_p == send_last )
703 trace ( "event_client (%x): all data sent - shutdown\n", id );
704 shutdown ( mem->s, SD_SEND );
706 WSAEventSelect ( mem->s, event, mask );
709 if ( wsa_events.lNetworkEvents & FD_READ )
711 err = wsa_events.iErrorCode[ FD_READ_BIT ];
712 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
713 if ( err != 0 ) break;
715 /* First read must succeed */
716 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
717 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
721 if ( recv_p == recv_last )
724 trace ( "event_client (%x): all data received\n", id );
725 WSAEventSelect ( mem->s, event, mask );
728 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
729 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
730 ok ( 0, "event_client (%x): read error: %d\n", id, err );
734 if ( wsa_events.lNetworkEvents & FD_CLOSE )
736 trace ( "event_client (%x): close event\n", id );
737 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
738 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
743 ok ( send_p == send_last,
744 "simple_client (%x): sent less data than expected: %d of %d\n",
745 id, send_p - mem->send_buf, n_expected );
746 ok ( recv_p == recv_last,
747 "simple_client (%x): received less data than expected: %d of %d\n",
748 id, recv_p - mem->recv_buf, n_expected );
749 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
750 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
753 WSACloseEvent ( event );
754 trace ( "event_client (%x) exiting\n", id );
758 /**************** Main program utility functions ***************/
760 static void Init (void)
762 WORD ver = MAKEWORD (2, 2);
765 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
769 static void Exit (void)
774 err = WSAGetLastError();
775 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
777 err = WSAGetLastError();
778 ok ( ret == SOCKET_ERROR && err == WSANOTINITIALISED,
779 "WSACleanup returned %d GetLastError is %d\n", ret, err);
782 static void StartServer (LPTHREAD_START_ROUTINE routine,
783 test_params *general, server_params *par)
785 par->general = general;
786 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
787 ok ( thread[0] != NULL, "Failed to create server thread\n" );
790 static void StartClients (LPTHREAD_START_ROUTINE routine,
791 test_params *general, client_params *par)
794 par->general = general;
795 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
798 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
799 ok ( thread[i] != NULL, "Failed to create client thread\n" );
800 /* Make sure the client is up and running */
801 WaitForSingleObject ( client_ready[client_id], INFINITE );
805 static void do_test( test_setup *test )
807 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
810 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
811 for (i = 0; i <= n; i++)
812 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
814 StartServer ( test->srv, &test->general, &test->srv_params );
815 StartClients ( test->clt, &test->general, &test->clt_params );
816 WaitForSingleObject ( server_ready, INFINITE );
818 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
819 ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ,
820 "some threads have not completed: %x\n", wait );
822 if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
824 for (i = 0; i <= n; i++)
826 trace ("terminating thread %08x\n", thread_id[i]);
827 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
828 TerminateThread ( thread [i], 0 );
831 CloseHandle ( server_ready );
832 for (i = 0; i <= n; i++)
833 CloseHandle ( client_ready[i] );
836 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
837 /* optname = SO_LINGER */
838 LINGER linger_testvals[] = {
845 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
846 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
847 bug in the linux kernel (fixed in 2.6.8) */
848 #define SOCKTIMEOUT2 997000 /* 997 seconds */
850 static void test_set_getsockopt(void)
858 s = socket(AF_INET, SOCK_STREAM, 0);
859 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
860 if( s == INVALID_SOCKET) return;
862 timeout = SOCKTIMEOUT1;
863 size = sizeof(timeout);
864 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
866 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
867 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
868 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
870 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
871 size = sizeof(timeout);
872 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
874 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
875 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
876 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
878 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
879 size = sizeof(lingval);
880 lingval = linger_testvals[i];
881 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
883 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
884 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
885 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
886 (lingval.l_linger == linger_testvals[i].l_linger ||
887 (!lingval.l_linger && !linger_testvals[i].l_onoff))
888 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
889 lingval.l_onoff, lingval.l_linger,
890 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
895 static void test_so_reuseaddr(void)
897 struct sockaddr_in saddr;
899 unsigned int rc,reuse;
902 saddr.sin_family = AF_INET;
903 saddr.sin_port = htons(9375);
904 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
906 s1=socket(AF_INET, SOCK_STREAM, 0);
907 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
908 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
909 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
911 s2=socket(AF_INET, SOCK_STREAM, 0);
912 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
916 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
917 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
919 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
920 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
923 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
924 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
927 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
928 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
935 /************* Array containing the tests to run **********/
937 #define STD_STREAM_SOCKET \
943 static test_setup tests [NUM_TESTS] =
945 /* Test 0: synchronous client and server */
966 /* Test 1: event-driven client, synchronous server */
987 /* Test 2: synchronous client, non-blocking server via select() */
1010 static void test_UDP(void)
1012 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1013 possible that this test fails due to dropped packets. */
1015 /* peer 0 receives data from all other peers */
1016 struct sock_info peer[NUM_UDP_PEERS];
1018 int ss, i, n_recv, n_sent;
1020 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1021 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1023 peer[i].addr.sin_family = AF_INET;
1024 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1027 peer[i].addr.sin_port = htons ( SERVERPORT );
1029 peer[i].addr.sin_port = htons ( 0 );
1032 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1034 /* test getsockname() to get peer's port */
1035 ss = sizeof ( peer[i].addr );
1036 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1037 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1040 /* test getsockname() */
1041 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1043 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1044 /* send client's ip */
1045 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1046 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1047 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1050 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1051 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1052 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1053 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1057 static void WINAPI do_getservbyname( HANDLE *starttest )
1063 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1066 struct servent *pserv[2];
1068 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1070 /* ensure that necessary buffer resizes are completed */
1071 for ( j = 0; j < 2; j++) {
1072 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1075 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1076 for ( j = 0; j < 2; j++ ) {
1077 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1078 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1079 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) );
1080 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 );
1081 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 );
1084 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1088 static void test_getservbyname(void)
1091 HANDLE starttest, thread[NUM_THREADS];
1092 DWORD thread_id[NUM_THREADS];
1094 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1096 /* create threads */
1097 for ( i = 0; i < NUM_THREADS; i++ ) {
1098 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1101 /* signal threads to start */
1102 SetEvent ( starttest );
1104 for ( i = 0; i < NUM_THREADS; i++) {
1105 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1109 static void test_WSAAddressToStringA(void)
1114 SOCKADDR_IN sockaddr;
1115 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1117 CHAR expect1[] = "0.0.0.0";
1118 CHAR expect2[] = "255.255.255.255";
1119 CHAR expect3[] = "0.0.0.0:65535";
1120 CHAR expect4[] = "255.255.255.255:65535";
1124 sockaddr.sin_family = AF_INET;
1125 sockaddr.sin_port = 0;
1126 sockaddr.sin_addr.s_addr = 0;
1128 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1129 GLE = WSAGetLastError();
1130 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1131 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1134 len = sizeof(address);
1136 sockaddr.sin_family = AF_INET;
1137 sockaddr.sin_port = 0;
1138 sockaddr.sin_addr.s_addr = 0;
1140 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1141 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1143 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1145 len = sizeof(address);
1147 sockaddr.sin_family = AF_INET;
1148 sockaddr.sin_port = 0;
1149 sockaddr.sin_addr.s_addr = 0xffffffff;
1151 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1152 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1154 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1156 len = sizeof(address);
1158 sockaddr.sin_family = AF_INET;
1159 sockaddr.sin_port = 0xffff;
1160 sockaddr.sin_addr.s_addr = 0;
1162 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1163 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1165 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1167 len = sizeof(address);
1169 sockaddr.sin_family = AF_INET;
1170 sockaddr.sin_port = 0xffff;
1171 sockaddr.sin_addr.s_addr = 0xffffffff;
1173 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1174 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1176 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1179 static void test_WSAAddressToStringW(void)
1184 SOCKADDR_IN sockaddr;
1185 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1187 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1188 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1189 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1190 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1191 '6', '5', '5', '3', '5', 0 };
1195 sockaddr.sin_family = AF_INET;
1196 sockaddr.sin_port = 0;
1197 sockaddr.sin_addr.s_addr = 0;
1199 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1200 GLE = WSAGetLastError();
1201 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1202 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1205 len = sizeof(address);
1207 sockaddr.sin_family = AF_INET;
1208 sockaddr.sin_port = 0;
1209 sockaddr.sin_addr.s_addr = 0;
1211 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1212 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1214 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1216 len = sizeof(address);
1218 sockaddr.sin_family = AF_INET;
1219 sockaddr.sin_port = 0;
1220 sockaddr.sin_addr.s_addr = 0xffffffff;
1222 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1223 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1225 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1227 len = sizeof(address);
1229 sockaddr.sin_family = AF_INET;
1230 sockaddr.sin_port = 0xffff;
1231 sockaddr.sin_addr.s_addr = 0;
1233 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1234 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1236 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1238 len = sizeof(address);
1240 sockaddr.sin_family = AF_INET;
1241 sockaddr.sin_port = 0xffff;
1242 sockaddr.sin_addr.s_addr = 0xffffffff;
1244 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1245 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1247 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1250 static void test_WSAStringToAddressA(void)
1253 SOCKADDR_IN sockaddr;
1254 SOCKADDR_IN6 sockaddr6;
1257 CHAR address1[] = "0.0.0.0";
1258 CHAR address2[] = "127.127.127.127";
1259 CHAR address3[] = "255.255.255.255";
1260 CHAR address4[] = "127.127.127.127:65535";
1261 CHAR address5[] = "255.255.255.255:65535";
1262 CHAR address6[] = "::1";
1263 CHAR address7[] = "[::1]";
1264 CHAR address8[] = "[::1]:65535";
1267 sockaddr.sin_family = AF_INET;
1269 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1270 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1271 WSAGetLastError() );
1273 len = sizeof(sockaddr);
1274 sockaddr.sin_port = 0;
1275 sockaddr.sin_addr.s_addr = 0;
1277 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1278 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1279 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1281 len = sizeof(sockaddr);
1282 sockaddr.sin_port = 0;
1283 sockaddr.sin_addr.s_addr = 0;
1285 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1286 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1287 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1289 len = sizeof(sockaddr);
1290 sockaddr.sin_port = 0;
1291 sockaddr.sin_addr.s_addr = 0;
1293 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1294 GLE = WSAGetLastError();
1295 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1296 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1297 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1299 len = sizeof(sockaddr);
1300 sockaddr.sin_port = 0;
1301 sockaddr.sin_addr.s_addr = 0;
1303 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1304 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1305 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1307 len = sizeof(sockaddr);
1308 sockaddr.sin_port = 0;
1309 sockaddr.sin_addr.s_addr = 0;
1311 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1312 GLE = WSAGetLastError();
1313 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1314 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1315 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1317 len = sizeof(sockaddr6);
1318 memset(&sockaddr6, 0, len);
1319 sockaddr6.sin6_family = AF_INET6;
1321 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1323 GLE = WSAGetLastError();
1324 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1325 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1327 len = sizeof(sockaddr6);
1328 memset(&sockaddr6, 0, len);
1329 sockaddr6.sin6_family = AF_INET6;
1331 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1333 GLE = WSAGetLastError();
1334 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1335 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1337 len = sizeof(sockaddr6);
1338 memset(&sockaddr6, 0, len);
1339 sockaddr6.sin6_family = AF_INET6;
1341 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1343 GLE = WSAGetLastError();
1344 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1345 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1346 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1350 static void test_WSAStringToAddressW(void)
1353 SOCKADDR_IN sockaddr;
1354 SOCKADDR_IN6 sockaddr6;
1357 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1358 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1359 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1360 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1361 ':', '6', '5', '5', '3', '5', 0 };
1362 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1363 '6', '5', '5', '3', '5', 0 };
1364 WCHAR address6[] = {':',':','1','\0'};
1365 WCHAR address7[] = {'[',':',':','1',']','\0'};
1366 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1369 sockaddr.sin_family = AF_INET;
1371 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1372 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1373 WSAGetLastError() );
1375 len = sizeof(sockaddr);
1376 sockaddr.sin_port = 0;
1377 sockaddr.sin_addr.s_addr = 0;
1379 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1380 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1381 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1383 len = sizeof(sockaddr);
1384 sockaddr.sin_port = 0;
1385 sockaddr.sin_addr.s_addr = 0;
1387 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1388 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1389 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1391 len = sizeof(sockaddr);
1392 sockaddr.sin_port = 0;
1393 sockaddr.sin_addr.s_addr = 0;
1395 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1396 GLE = WSAGetLastError();
1397 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1398 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1399 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1401 len = sizeof(sockaddr);
1402 sockaddr.sin_port = 0;
1403 sockaddr.sin_addr.s_addr = 0;
1405 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1406 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1407 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1409 len = sizeof(sockaddr);
1410 sockaddr.sin_port = 0;
1411 sockaddr.sin_addr.s_addr = 0;
1413 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1414 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1415 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1416 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1418 len = sizeof(sockaddr6);
1419 memset(&sockaddr6, 0, len);
1420 sockaddr6.sin6_family = AF_INET6;
1422 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1424 GLE = WSAGetLastError();
1425 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1426 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1428 len = sizeof(sockaddr6);
1429 memset(&sockaddr6, 0, len);
1430 sockaddr6.sin6_family = AF_INET6;
1432 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1434 GLE = WSAGetLastError();
1435 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1436 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1438 len = sizeof(sockaddr6);
1439 memset(&sockaddr6, 0, len);
1440 sockaddr6.sin6_family = AF_INET6;
1442 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1444 GLE = WSAGetLastError();
1445 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1446 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1447 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1451 static VOID WINAPI SelectReadThread(select_thread_params *par)
1455 struct sockaddr_in addr;
1456 struct timeval select_timeout;
1459 FD_SET(par->s, &readfds);
1460 select_timeout.tv_sec=5;
1461 select_timeout.tv_usec=0;
1462 addr.sin_family = AF_INET;
1463 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1464 addr.sin_port = htons(SERVERPORT);
1466 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1467 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1469 SetEvent(server_ready);
1470 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1471 par->ReadKilled = (ret == 1);
1474 static void test_select(void)
1476 SOCKET fdRead, fdWrite;
1477 fd_set readfds, writefds, exceptfds;
1480 struct timeval select_timeout;
1481 select_thread_params thread_params;
1482 HANDLE thread_handle;
1485 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1486 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1487 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1488 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1492 FD_ZERO(&exceptfds);
1493 FD_SET(fdRead, &readfds);
1494 FD_SET(fdWrite, &writefds);
1495 FD_SET(fdRead, &exceptfds);
1496 FD_SET(fdWrite, &exceptfds);
1497 select_timeout.tv_sec=0;
1498 select_timeout.tv_usec=500;
1501 if (fdWrite > maxfd)
1505 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1506 ok ( (ret == 0), "select should not return any socket handles\n");
1507 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1508 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1511 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1512 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1515 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1517 ret = closesocket(fdWrite);
1518 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1520 thread_params.s = fdRead;
1521 thread_params.ReadKilled = FALSE;
1522 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1523 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1524 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1526 WaitForSingleObject (server_ready, INFINITE);
1528 ret = closesocket(fdRead);
1529 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1531 WaitForSingleObject (thread_handle, 1000);
1532 ok ( (thread_params.ReadKilled), "closesocket did not wakeup select\n");
1536 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1538 struct sockaddr_in address;
1539 int len = sizeof(address);
1540 SOCKET client_socket;
1542 SetEvent(server_ready);
1543 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1544 if (client_socket != INVALID_SOCKET)
1545 closesocket(client_socket);
1546 par->ReadKilled = (client_socket == INVALID_SOCKET);
1550 static void test_accept(void)
1553 SOCKET server_socket = INVALID_SOCKET;
1554 struct sockaddr_in address;
1555 select_thread_params thread_params;
1556 HANDLE thread_handle = NULL;
1559 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1560 if (server_socket == INVALID_SOCKET)
1562 trace("error creating server socket: %d\n", WSAGetLastError());
1566 memset(&address, 0, sizeof(address));
1567 address.sin_family = AF_INET;
1568 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1571 trace("error binding server socket: %d\n", WSAGetLastError());
1575 ret = listen(server_socket, 1);
1578 trace("error making server socket listen: %d\n", WSAGetLastError());
1582 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1583 if (server_ready == INVALID_HANDLE_VALUE)
1585 trace("error creating event: %d\n", GetLastError());
1589 thread_params.s = server_socket;
1590 thread_params.ReadKilled = FALSE;
1591 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
1592 &thread_params, 0, &id);
1593 if (thread_handle == NULL)
1595 trace("error creating thread: %d\n", GetLastError());
1599 WaitForSingleObject(server_ready, INFINITE);
1601 ret = closesocket(server_socket);
1604 trace("closesocket failed: %d\n", WSAGetLastError());
1608 WaitForSingleObject(thread_handle, 1000);
1609 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
1612 if (thread_handle != NULL)
1613 CloseHandle(thread_handle);
1614 if (server_ready != INVALID_HANDLE_VALUE)
1615 CloseHandle(server_ready);
1616 if (server_socket != INVALID_SOCKET)
1617 closesocket(server_socket);
1620 static void test_extendedSocketOptions(void)
1624 struct sockaddr_in sa;
1625 int sa_len = sizeof(struct sockaddr_in);
1626 int optval, optlen = sizeof(int), ret;
1630 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1631 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1635 memset(&sa, 0, sa_len);
1637 sa.sin_family = AF_INET;
1638 sa.sin_port = htons(0);
1639 sa.sin_addr.s_addr = htonl(INADDR_ANY);
1641 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0){
1642 trace("Creating the socket failed: %d\n", WSAGetLastError());
1647 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1648 trace("Failed to bind socket: %d\n", WSAGetLastError());
1654 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
1656 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
1657 ok(optval == 65507, "SO_MAX_MSG_SIZE reported %d, expected 65507\n", optval);
1659 optlen = sizeof(LINGER);
1660 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1662 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
1667 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) < 0){
1668 trace("Creating the socket failed: %d\n", WSAGetLastError());
1673 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1674 trace("Failed to bind socket: %d\n", WSAGetLastError());
1680 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1681 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
1683 optlen = sizeof(BOOL);
1684 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
1685 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
1686 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
1687 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
1688 bool_opt_val, linger_val.l_onoff);
1694 static void test_getsockname(void)
1698 struct sockaddr_in sa_set, sa_get;
1699 int sa_set_len = sizeof(struct sockaddr_in);
1700 int sa_get_len = sa_set_len;
1701 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
1703 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1704 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1708 memset(&sa_set, 0, sa_set_len);
1710 sa_set.sin_family = AF_INET;
1711 sa_set.sin_port = htons(0);
1712 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
1714 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) < 0){
1715 trace("Creating the socket failed: %d\n", WSAGetLastError());
1720 memcpy(&sa_get, &sa_set, sizeof(sa_set));
1721 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
1722 ok(0, "getsockname on unbound socket should fail\n");
1724 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
1725 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
1726 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
1727 "failed getsockname modified sockaddr when it shouldn't\n");
1730 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
1731 trace("Failed to bind socket: %d\n", WSAGetLastError());
1737 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
1738 trace("Failed to call getsockname: %d\n", WSAGetLastError());
1744 ok(memcmp(sa_get.sin_zero, null_padding, 8) == 0,
1745 "getsockname did not zero the sockaddr_in structure\n");
1751 static void test_dns(void)
1755 h = gethostbyname("");
1756 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
1759 static void test_inet_addr(void)
1763 addr = inet_addr(NULL);
1764 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
1767 static DWORD WINAPI drain_socket_thread(LPVOID arg)
1770 SOCKET sock = *(SOCKET*)arg;
1772 while (recv(sock, buffer, sizeof(buffer), 0) > 0)
1777 static void test_send(void)
1779 SOCKET src = INVALID_SOCKET;
1780 SOCKET dst = INVALID_SOCKET;
1781 HANDLE hThread = NULL;
1782 const int buflen = 1024*1024;
1783 char *buffer = NULL;
1787 if (tcp_socketpair(&src, &dst) != 0)
1789 ok(0, "creating socket pair failed, skipping test\n");
1793 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
1794 if (hThread == NULL)
1796 ok(0, "CreateThread failed, error %d\n", GetLastError());
1800 buffer = HeapAlloc(GetProcessHeap(), 0, buflen);
1803 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
1807 ret = send(src, buffer, buflen, 0);
1809 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
1811 ok(0, "send failed, error %d\n", WSAGetLastError());
1814 if (src != INVALID_SOCKET)
1816 if (dst != INVALID_SOCKET)
1818 if (hThread != NULL)
1819 CloseHandle(hThread);
1820 HeapFree(GetProcessHeap(), 0, buffer);
1823 static void test_write_events(void)
1825 SOCKET src = INVALID_SOCKET;
1826 SOCKET dst = INVALID_SOCKET;
1827 HANDLE hThread = NULL;
1828 HANDLE hEvent = INVALID_HANDLE_VALUE;
1834 if (tcp_socketpair(&src, &dst) != 0)
1836 ok(0, "creating socket pair failed, skipping test\n");
1840 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
1841 if (hThread == NULL)
1843 ok(0, "CreateThread failed, error %d\n", GetLastError());
1847 hEvent = CreateEventW(NULL, FALSE, TRUE, NULL);
1848 if (hEvent == INVALID_HANDLE_VALUE)
1850 ok(0, "CreateEventW failed, error %d\n", GetLastError());
1854 ret = ioctlsocket(src, FIONBIO, &one);
1857 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
1861 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
1864 ok(0, "WSAEventSelect failed, error %d\n", ret);
1868 for (len = 100; len > 0; --len)
1870 WSANETWORKEVENTS netEvents;
1871 DWORD dwRet = WaitForSingleObject(hEvent, 5000);
1872 if (dwRet != WAIT_OBJECT_0)
1874 ok(0, "WaitForSingleObject failed, error %d\n", dwRet);
1878 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
1881 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
1885 if (netEvents.lNetworkEvents & FD_WRITE)
1887 ret = send(src, "a", 1, 0);
1888 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
1890 ok(0, "send failed, error %d\n", WSAGetLastError());
1895 if (netEvents.lNetworkEvents & FD_CLOSE)
1897 ok(0, "unexpected close\n");
1903 if (src != INVALID_SOCKET)
1905 if (dst != INVALID_SOCKET)
1907 if (hThread != NULL)
1908 CloseHandle(hThread);
1909 CloseHandle(hEvent);
1912 /**************** Main program ***************/
1919 test_set_getsockopt();
1920 test_so_reuseaddr();
1921 test_extendedSocketOptions();
1923 for (i = 0; i < NUM_TESTS; i++)
1925 trace ( " **** STARTING TEST %d ****\n", i );
1926 do_test ( &tests[i] );
1927 trace ( " **** TEST %d COMPLETE ****\n", i );
1932 test_getservbyname();
1934 test_WSAAddressToStringA();
1935 test_WSAAddressToStringW();
1937 test_WSAStringToAddressA();
1938 test_WSAStringToAddressW();
1947 test_write_events();