2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
6 * Copyright 2008 Jeff Zaroyko
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
34 #define MAX_CLIENTS 4 /* Max number of clients */
35 #define NUM_TESTS 4 /* Number of tests performed */
36 #define FIRST_CHAR 'A' /* First character in transferred pattern */
37 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
38 #define BIND_TRIES 6 /* Number of bind() attempts */
39 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
40 after server initialization, if something hangs */
42 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
44 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
45 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
47 #define SERVERIP "127.0.0.1" /* IP to bind to */
48 #define SERVERPORT 9374 /* Port number to bind to */
50 #define wsa_ok(op, cond, msg) \
54 if ( !(cond tmp) ) err = WSAGetLastError(); \
55 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
59 /**************** Structs and typedefs ***************/
61 typedef struct thread_info
67 /* Information in the server about open client connections */
68 typedef struct sock_info
71 struct sockaddr_in addr;
72 struct sockaddr_in peer;
78 /* Test parameters for both server & client */
79 typedef struct test_params
83 const char *inet_addr;
90 /* server-specific test parameters */
91 typedef struct server_params
98 /* client-specific test parameters */
99 typedef struct client_params
101 test_params *general;
106 /* This type combines all information for setting up a test scenario */
107 typedef struct test_setup
111 server_params srv_params;
113 client_params clt_params;
116 /* Thread local storage for server */
117 typedef struct server_memory
120 struct sockaddr_in addr;
121 sock_info sock[MAX_CLIENTS];
124 /* Thread local storage for client */
125 typedef struct client_memory
128 struct sockaddr_in addr;
133 /* SelectReadThread thread parameters */
134 typedef struct select_thread_params
138 } select_thread_params;
140 /**************** Static variables ***************/
142 static DWORD tls; /* Thread local storage index */
143 static HANDLE thread[1+MAX_CLIENTS];
144 static DWORD thread_id[1+MAX_CLIENTS];
145 static HANDLE server_ready;
146 static HANDLE client_ready[MAX_CLIENTS];
147 static int client_id;
149 /**************** General utility functions ***************/
151 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
153 SOCKET server = INVALID_SOCKET;
154 struct sockaddr_in addr;
158 *src = INVALID_SOCKET;
159 *dst = INVALID_SOCKET;
161 *src = socket(AF_INET, SOCK_STREAM, 0);
162 if (*src == INVALID_SOCKET)
165 server = socket(AF_INET, SOCK_STREAM, 0);
166 if (server == INVALID_SOCKET)
169 memset(&addr, 0, sizeof(addr));
170 addr.sin_family = AF_INET;
171 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
172 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
177 ret = getsockname(server, (struct sockaddr*)&addr, &len);
181 ret = listen(server, 1);
185 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
190 *dst = accept(server, (struct sockaddr*)&addr, &len);
193 if (server != INVALID_SOCKET)
195 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
202 static void set_so_opentype ( BOOL overlapped )
204 int optval = !overlapped, newval, len = sizeof (int);
206 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
207 (LPVOID) &optval, sizeof (optval) ) == 0,
208 "setting SO_OPENTYPE failed\n" );
209 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
210 (LPVOID) &newval, &len ) == 0,
211 "getting SO_OPENTYPE failed\n" );
212 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
215 static int set_blocking ( SOCKET s, BOOL blocking )
217 u_long val = !blocking;
218 return ioctlsocket ( s, FIONBIO, &val );
221 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
224 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
225 memset ( p, c, chunk_size );
228 static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
232 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
234 for ( i = 0; i < chunk_size; i++ )
235 if ( p[i] != c ) return p + i;
241 * This routine is called when a client / server does not expect any more data,
242 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
244 static void read_zero_bytes ( SOCKET s )
248 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
250 ok ( n <= 0, "garbage data received: %d bytes\n", n );
253 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
255 char* last = buf + buflen, *p;
257 for ( p = buf; n > 0 && p < last; p += n )
258 n = send ( s, p, min ( sendlen, last - p ), 0 );
259 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
263 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
265 char* last = buf + buflen, *p;
267 for ( p = buf; n > 0 && p < last; p += n )
268 n = recv ( s, p, min ( recvlen, last - p ), 0 );
269 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
273 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen,int flags,struct sockaddr *from, socklen_t *fromlen, int recvlen )
275 char* last = buf + buflen, *p;
277 for ( p = buf; n > 0 && p < last; p += n )
278 n = recvfrom ( s, p, min ( recvlen, last - p ), 0, from, fromlen );
279 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
284 * Call this routine right after thread startup.
285 * SO_OPENTYPE must by 0, regardless what the server did.
287 static void check_so_opentype (void)
291 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
292 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
295 /**************** Server utility functions ***************/
298 * Even if we have closed our server socket cleanly,
299 * the OS may mark the address "in use" for some time -
300 * this happens with native Linux apps, too.
302 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
304 int err, wsaerr = 0, n_try = BIND_TRIES;
306 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
307 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
310 trace ( "address in use, waiting ...\n" );
311 Sleep ( 1000 * BIND_SLEEP );
313 ok ( err == 0, "failed to bind: %d\n", wsaerr );
316 static void server_start ( server_params *par )
319 test_params *gen = par->general;
320 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
322 TlsSetValue ( tls, mem );
323 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
324 NULL, 0, par->sock_flags );
325 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
327 mem->addr.sin_family = AF_INET;
328 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
329 mem->addr.sin_port = htons ( gen->inet_port );
331 for (i = 0; i < MAX_CLIENTS; i++)
333 mem->sock[i].s = INVALID_SOCKET;
334 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
335 mem->sock[i].n_recvd = 0;
336 mem->sock[i].n_sent = 0;
339 if ( gen->sock_type == SOCK_STREAM )
340 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
343 static void server_stop (void)
346 server_memory *mem = TlsGetValue ( tls );
348 for (i = 0; i < MAX_CLIENTS; i++ )
350 LocalFree ( mem->sock[i].buf );
351 if ( mem->sock[i].s != INVALID_SOCKET )
352 closesocket ( mem->sock[i].s );
354 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
356 ExitThread ( GetCurrentThreadId () );
359 /**************** Client utilitiy functions ***************/
361 static void client_start ( client_params *par )
363 test_params *gen = par->general;
364 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
366 TlsSetValue ( tls, mem );
368 WaitForSingleObject ( server_ready, INFINITE );
370 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
371 NULL, 0, par->sock_flags );
373 mem->addr.sin_family = AF_INET;
374 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
375 mem->addr.sin_port = htons ( gen->inet_port );
377 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
379 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
380 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
381 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
383 SetEvent ( client_ready[client_id] );
384 /* Wait for the other clients to come up */
385 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
388 static void client_stop (void)
390 client_memory *mem = TlsGetValue ( tls );
391 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
392 LocalFree ( mem->send_buf );
397 /**************** Servers ***************/
400 * simple_server: A very basic server doing synchronous IO.
402 static VOID WINAPI simple_server ( server_params *par )
404 test_params *gen = par->general;
406 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
407 id = GetCurrentThreadId();
410 trace ( "simple_server (%x) starting\n", id );
412 set_so_opentype ( FALSE ); /* non-overlapped */
413 server_start ( par );
414 mem = TlsGetValue ( tls );
416 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
417 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
419 trace ( "simple_server (%x) ready\n", id );
420 SetEvent ( server_ready ); /* notify clients */
422 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
424 trace ( "simple_server (%x): waiting for client\n", id );
426 /* accept a single connection */
427 tmp = sizeof ( mem->sock[0].peer );
428 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
429 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
431 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
432 "simple_server (%x): strange peer address\n", id );
434 /* Receive data & check it */
435 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
436 ok ( n_recvd == n_expected,
437 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
438 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
439 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
442 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
443 ok ( n_sent == n_expected,
444 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
447 read_zero_bytes ( mem->sock[0].s );
448 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
449 mem->sock[0].s = INVALID_SOCKET;
452 trace ( "simple_server (%x) exiting\n", id );
457 * select_server: A non-blocking server.
459 static VOID WINAPI select_server ( server_params *par )
461 test_params *gen = par->general;
463 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
464 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
465 n_set, delta, n_ready;
467 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
468 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
470 trace ( "select_server (%x) starting\n", id );
472 set_so_opentype ( FALSE ); /* non-overlapped */
473 server_start ( par );
474 mem = TlsGetValue ( tls );
476 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
477 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
479 trace ( "select_server (%x) ready\n", id );
480 SetEvent ( server_ready ); /* notify clients */
482 FD_ZERO ( &fds_openrecv );
483 FD_ZERO ( &fds_recv );
484 FD_ZERO ( &fds_send );
485 FD_ZERO ( &fds_opensend );
487 FD_SET ( mem->s, &fds_openrecv );
491 fds_recv = fds_openrecv;
492 fds_send = fds_opensend;
496 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
497 "select_server (%x): select() failed: %d\n" );
499 /* check for incoming requests */
500 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
503 trace ( "select_server (%x): accepting client connection\n", id );
505 /* accept a single connection */
506 tmp = sizeof ( mem->sock[n_connections].peer );
507 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
508 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
510 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
511 "select_server (%x): strange peer address\n", id );
513 /* add to list of open connections */
514 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
515 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
520 /* handle open requests */
522 for ( i = 0; i < n_connections; i++ )
524 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
527 if ( mem->sock[i].n_recvd < n_expected ) {
528 /* Receive data & check it */
529 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 );
530 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
531 mem->sock[i].n_recvd += n_recvd;
533 if ( mem->sock[i].n_recvd == n_expected ) {
534 p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
535 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
536 FD_CLR ( mem->sock[i].s, &fds_openrecv );
539 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
543 /* only echo back what we've received */
544 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
546 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
549 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
551 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
552 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
553 mem->sock[i].n_sent += n_sent;
555 if ( mem->sock[i].n_sent == n_expected ) {
556 FD_CLR ( mem->sock[i].s, &fds_opensend );
559 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
564 /* check that select returned the correct number of ready sockets */
565 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
567 /* check if all clients are done */
568 if ( ( fds_opensend.fd_count == 0 )
569 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
570 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
575 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
578 read_zero_bytes ( mem->sock[i].s );
579 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
580 mem->sock[i].s = INVALID_SOCKET;
583 trace ( "select_server (%x) exiting\n", id );
587 /**************** Clients ***************/
590 * simple_client: A very basic client doing synchronous IO.
592 static VOID WINAPI simple_client ( client_params *par )
594 test_params *gen = par->general;
596 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
599 id = GetCurrentThreadId();
600 trace ( "simple_client (%x): starting\n", id );
601 /* wait here because we want to call set_so_opentype before creating a socket */
602 WaitForSingleObject ( server_ready, INFINITE );
603 trace ( "simple_client (%x): server ready\n", id );
605 check_so_opentype ();
606 set_so_opentype ( FALSE ); /* non-overlapped */
607 client_start ( par );
608 mem = TlsGetValue ( tls );
611 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
612 0 ==, "simple_client (%x): connect error: %d\n" );
613 ok ( set_blocking ( mem->s, TRUE ) == 0,
614 "simple_client (%x): failed to set blocking mode\n", id );
615 trace ( "simple_client (%x) connected\n", id );
617 /* send data to server */
618 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
619 ok ( n_sent == n_expected,
620 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
622 /* shutdown send direction */
623 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
625 /* Receive data echoed back & check it */
626 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
627 ok ( n_recvd == n_expected,
628 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
631 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
632 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
635 read_zero_bytes ( mem->s );
636 trace ( "simple_client (%x) exiting\n", id );
641 * simple_mixed_client: mixing send and recvfrom
643 static VOID WINAPI simple_mixed_client ( client_params *par )
645 test_params *gen = par->general;
647 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
649 socklen_t fromLen = sizeof(mem->addr);
650 struct sockaddr test;
652 id = GetCurrentThreadId();
653 trace ( "simple_client (%x): starting\n", id );
654 /* wait here because we want to call set_so_opentype before creating a socket */
655 WaitForSingleObject ( server_ready, INFINITE );
656 trace ( "simple_client (%x): server ready\n", id );
658 check_so_opentype ();
659 set_so_opentype ( FALSE ); /* non-overlapped */
660 client_start ( par );
661 mem = TlsGetValue ( tls );
664 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
665 0 ==, "simple_client (%x): connect error: %d\n" );
666 ok ( set_blocking ( mem->s, TRUE ) == 0,
667 "simple_client (%x): failed to set blocking mode\n", id );
668 trace ( "simple_client (%x) connected\n", id );
670 /* send data to server */
671 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
672 ok ( n_sent == n_expected,
673 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
675 /* shutdown send direction */
676 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
678 /* this shouldn't change, since lpFrom, is not updated on
679 connection oriented sockets - exposed by bug 11640
681 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
683 /* Receive data echoed back & check it */
684 n_recvd = do_synchronous_recvfrom ( mem->s,
688 (struct sockaddr *)&test,
691 ok ( n_recvd == n_expected,
692 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
694 /* check that lpFrom was not updated */
697 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
698 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
701 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
702 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
705 read_zero_bytes ( mem->s );
706 trace ( "simple_client (%x) exiting\n", id );
711 * event_client: An event-driven client
713 static void WINAPI event_client ( client_params *par )
715 test_params *gen = par->general;
717 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
720 WSANETWORKEVENTS wsa_events;
721 char *send_last, *recv_last, *send_p, *recv_p;
722 long mask = FD_READ | FD_WRITE | FD_CLOSE;
724 trace ( "event_client (%x): starting\n", id );
725 client_start ( par );
726 trace ( "event_client (%x): server ready\n", id );
728 mem = TlsGetValue ( tls );
730 /* Prepare event notification for connect, makes socket nonblocking */
731 event = WSACreateEvent ();
732 WSAEventSelect ( mem->s, event, FD_CONNECT );
733 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
735 err = WSAGetLastError ();
736 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
737 tmp = WaitForSingleObject ( event, INFINITE );
738 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
739 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
740 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
741 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
742 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
746 trace ( "event_client (%x) connected\n", id );
748 WSAEventSelect ( mem->s, event, mask );
750 recv_p = mem->recv_buf;
751 recv_last = mem->recv_buf + n_expected;
752 send_p = mem->send_buf;
753 send_last = mem->send_buf + n_expected;
757 err = WaitForSingleObject ( event, INFINITE );
758 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
760 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
761 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
763 if ( wsa_events.lNetworkEvents & FD_WRITE )
765 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
766 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
771 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
774 err = WSAGetLastError ();
775 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
780 while ( n >= 0 && send_p < send_last );
782 if ( send_p == send_last )
784 trace ( "event_client (%x): all data sent - shutdown\n", id );
785 shutdown ( mem->s, SD_SEND );
787 WSAEventSelect ( mem->s, event, mask );
790 if ( wsa_events.lNetworkEvents & FD_READ )
792 err = wsa_events.iErrorCode[ FD_READ_BIT ];
793 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
794 if ( err != 0 ) break;
796 /* First read must succeed */
797 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
798 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
802 if ( recv_p == recv_last )
805 trace ( "event_client (%x): all data received\n", id );
806 WSAEventSelect ( mem->s, event, mask );
809 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
810 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
811 ok ( 0, "event_client (%x): read error: %d\n", id, err );
815 if ( wsa_events.lNetworkEvents & FD_CLOSE )
817 trace ( "event_client (%x): close event\n", id );
818 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
819 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
824 ok ( send_p == send_last,
825 "simple_client (%x): sent less data than expected: %d of %d\n",
826 id, send_p - mem->send_buf, n_expected );
827 ok ( recv_p == recv_last,
828 "simple_client (%x): received less data than expected: %d of %d\n",
829 id, recv_p - mem->recv_buf, n_expected );
830 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
831 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
834 WSACloseEvent ( event );
835 trace ( "event_client (%x) exiting\n", id );
839 /**************** Main program utility functions ***************/
841 static void Init (void)
843 WORD ver = MAKEWORD (2, 2);
846 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
850 static void Exit (void)
855 err = WSAGetLastError();
856 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
858 err = WSAGetLastError();
859 ok ( ret == SOCKET_ERROR && err == WSANOTINITIALISED,
860 "WSACleanup returned %d GetLastError is %d\n", ret, err);
863 static void StartServer (LPTHREAD_START_ROUTINE routine,
864 test_params *general, server_params *par)
866 par->general = general;
867 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
868 ok ( thread[0] != NULL, "Failed to create server thread\n" );
871 static void StartClients (LPTHREAD_START_ROUTINE routine,
872 test_params *general, client_params *par)
875 par->general = general;
876 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
879 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
880 ok ( thread[i] != NULL, "Failed to create client thread\n" );
881 /* Make sure the client is up and running */
882 WaitForSingleObject ( client_ready[client_id], INFINITE );
886 static void do_test( test_setup *test )
888 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
891 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
892 for (i = 0; i <= n; i++)
893 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
895 StartServer ( test->srv, &test->general, &test->srv_params );
896 StartClients ( test->clt, &test->general, &test->clt_params );
897 WaitForSingleObject ( server_ready, INFINITE );
899 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
900 ok ( wait <= WAIT_OBJECT_0 + n ,
901 "some threads have not completed: %x\n", wait );
903 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
905 for (i = 0; i <= n; i++)
907 trace ("terminating thread %08x\n", thread_id[i]);
908 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
909 TerminateThread ( thread [i], 0 );
912 CloseHandle ( server_ready );
913 for (i = 0; i <= n; i++)
914 CloseHandle ( client_ready[i] );
917 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
918 /* optname = SO_LINGER */
919 LINGER linger_testvals[] = {
926 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
927 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
928 bug in the linux kernel (fixed in 2.6.8) */
929 #define SOCKTIMEOUT2 997000 /* 997 seconds */
931 static void test_set_getsockopt(void)
939 s = socket(AF_INET, SOCK_STREAM, 0);
940 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
941 if( s == INVALID_SOCKET) return;
943 timeout = SOCKTIMEOUT1;
944 size = sizeof(timeout);
945 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
947 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
948 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
949 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
951 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
952 size = sizeof(timeout);
953 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
955 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
956 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
957 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
959 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
960 size = sizeof(lingval);
961 lingval = linger_testvals[i];
962 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
964 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
965 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
966 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
967 (lingval.l_linger == linger_testvals[i].l_linger ||
968 (!lingval.l_linger && !linger_testvals[i].l_onoff))
969 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
970 lingval.l_onoff, lingval.l_linger,
971 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
973 /* Test for erroneously passing a value instead of a pointer as optval */
975 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
976 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
977 "instead of failing.\n");
978 lasterr = WSAGetLastError();
979 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
980 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
985 static void test_so_reuseaddr(void)
987 struct sockaddr_in saddr;
989 unsigned int rc,reuse;
992 saddr.sin_family = AF_INET;
993 saddr.sin_port = htons(9375);
994 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
996 s1=socket(AF_INET, SOCK_STREAM, 0);
997 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
998 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
999 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1001 s2=socket(AF_INET, SOCK_STREAM, 0);
1002 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1006 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1007 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1009 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1010 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1013 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1014 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1016 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1017 * a port immediately after closing another socket on that port, so
1018 * basically following the BSD socket semantics here. */
1020 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1021 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1026 /************* Array containing the tests to run **********/
1028 #define STD_STREAM_SOCKET \
1034 static test_setup tests [NUM_TESTS] =
1036 /* Test 0: synchronous client and server */
1057 /* Test 1: event-driven client, synchronous server */
1074 WSA_FLAG_OVERLAPPED,
1078 /* Test 2: synchronous client, non-blocking server via select() */
1099 /* Test 3: synchronous mixed client and server */
1113 simple_mixed_client,
1122 static void test_UDP(void)
1124 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1125 possible that this test fails due to dropped packets. */
1127 /* peer 0 receives data from all other peers */
1128 struct sock_info peer[NUM_UDP_PEERS];
1130 int ss, i, n_recv, n_sent;
1132 memset (buf,0,sizeof(buf));
1133 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1134 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1136 peer[i].addr.sin_family = AF_INET;
1137 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1140 peer[i].addr.sin_port = htons ( SERVERPORT );
1142 peer[i].addr.sin_port = htons ( 0 );
1145 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1147 /* test getsockname() to get peer's port */
1148 ss = sizeof ( peer[i].addr );
1149 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1150 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1153 /* test getsockname() */
1154 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1156 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1157 /* send client's ip */
1158 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1159 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1160 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1163 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1164 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1165 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1166 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1170 static void WINAPI do_getservbyname( HANDLE *starttest )
1176 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1179 struct servent *pserv[2];
1181 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1183 /* ensure that necessary buffer resizes are completed */
1184 for ( j = 0; j < 2; j++) {
1185 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1188 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1189 for ( j = 0; j < 2; j++ ) {
1190 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1191 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1192 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) );
1193 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 );
1194 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 );
1197 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1201 static void test_getservbyname(void)
1204 HANDLE starttest, thread[NUM_THREADS];
1205 DWORD thread_id[NUM_THREADS];
1207 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1209 /* create threads */
1210 for ( i = 0; i < NUM_THREADS; i++ ) {
1211 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1214 /* signal threads to start */
1215 SetEvent ( starttest );
1217 for ( i = 0; i < NUM_THREADS; i++) {
1218 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1222 static void test_WSASocket(void)
1224 SOCKET sock = INVALID_SOCKET;
1225 WSAPROTOCOL_INFOA *pi;
1226 int providers[] = {6, 0};
1230 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1231 * to avoid a crash on win98.
1234 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1235 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1237 err = WSAGetLastError();
1238 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1241 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1242 ok(pi != NULL, "Failed to allocate memory\n");
1244 skip("Can't continue without memory.\n");
1248 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1249 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1253 skip("No protocols enumerated.\n");
1254 HeapFree(GetProcessHeap(), 0, pi);
1258 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1259 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1260 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1264 HeapFree(GetProcessHeap(), 0, pi);
1267 static void test_WSAAddressToStringA(void)
1272 SOCKADDR_IN sockaddr;
1273 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1275 CHAR expect1[] = "0.0.0.0";
1276 CHAR expect2[] = "255.255.255.255";
1277 CHAR expect3[] = "0.0.0.0:65535";
1278 CHAR expect4[] = "255.255.255.255:65535";
1282 sockaddr.sin_family = AF_INET;
1283 sockaddr.sin_port = 0;
1284 sockaddr.sin_addr.s_addr = 0;
1286 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1287 GLE = WSAGetLastError();
1288 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1289 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1292 len = sizeof(address);
1294 sockaddr.sin_family = AF_INET;
1295 sockaddr.sin_port = 0;
1296 sockaddr.sin_addr.s_addr = 0;
1298 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1299 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1301 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1303 len = sizeof(address);
1305 sockaddr.sin_family = AF_INET;
1306 sockaddr.sin_port = 0;
1307 sockaddr.sin_addr.s_addr = 0xffffffff;
1309 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1310 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1312 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1314 len = sizeof(address);
1316 sockaddr.sin_family = AF_INET;
1317 sockaddr.sin_port = 0xffff;
1318 sockaddr.sin_addr.s_addr = 0;
1320 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1321 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1323 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1325 len = sizeof(address);
1327 sockaddr.sin_family = AF_INET;
1328 sockaddr.sin_port = 0xffff;
1329 sockaddr.sin_addr.s_addr = 0xffffffff;
1331 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1332 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1334 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1337 static void test_WSAAddressToStringW(void)
1342 SOCKADDR_IN sockaddr;
1343 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1345 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1346 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1347 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1348 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1349 '6', '5', '5', '3', '5', 0 };
1353 sockaddr.sin_family = AF_INET;
1354 sockaddr.sin_port = 0;
1355 sockaddr.sin_addr.s_addr = 0;
1357 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1358 GLE = WSAGetLastError();
1359 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1360 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1363 len = sizeof(address);
1365 sockaddr.sin_family = AF_INET;
1366 sockaddr.sin_port = 0;
1367 sockaddr.sin_addr.s_addr = 0;
1369 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1370 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1372 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1374 len = sizeof(address);
1376 sockaddr.sin_family = AF_INET;
1377 sockaddr.sin_port = 0;
1378 sockaddr.sin_addr.s_addr = 0xffffffff;
1380 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1381 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1383 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1385 len = sizeof(address);
1387 sockaddr.sin_family = AF_INET;
1388 sockaddr.sin_port = 0xffff;
1389 sockaddr.sin_addr.s_addr = 0;
1391 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1392 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1394 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1396 len = sizeof(address);
1398 sockaddr.sin_family = AF_INET;
1399 sockaddr.sin_port = 0xffff;
1400 sockaddr.sin_addr.s_addr = 0xffffffff;
1402 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1403 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1405 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1408 static void test_WSAStringToAddressA(void)
1411 SOCKADDR_IN sockaddr;
1412 SOCKADDR_IN6 sockaddr6;
1415 CHAR address1[] = "0.0.0.0";
1416 CHAR address2[] = "127.127.127.127";
1417 CHAR address3[] = "255.255.255.255";
1418 CHAR address4[] = "127.127.127.127:65535";
1419 CHAR address5[] = "255.255.255.255:65535";
1420 CHAR address6[] = "::1";
1421 CHAR address7[] = "[::1]";
1422 CHAR address8[] = "[::1]:65535";
1425 sockaddr.sin_family = AF_INET;
1427 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1428 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1429 WSAGetLastError() );
1431 len = sizeof(sockaddr);
1432 sockaddr.sin_port = 0;
1433 sockaddr.sin_addr.s_addr = 0;
1435 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1436 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1437 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1439 len = sizeof(sockaddr);
1440 sockaddr.sin_port = 0;
1441 sockaddr.sin_addr.s_addr = 0;
1443 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1444 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1445 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1447 len = sizeof(sockaddr);
1448 sockaddr.sin_port = 0;
1449 sockaddr.sin_addr.s_addr = 0;
1451 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1452 GLE = WSAGetLastError();
1453 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1454 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1455 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1457 len = sizeof(sockaddr);
1458 sockaddr.sin_port = 0;
1459 sockaddr.sin_addr.s_addr = 0;
1461 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1462 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1463 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1465 len = sizeof(sockaddr);
1466 sockaddr.sin_port = 0;
1467 sockaddr.sin_addr.s_addr = 0;
1469 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1470 GLE = WSAGetLastError();
1471 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1472 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1473 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1475 len = sizeof(sockaddr6);
1476 memset(&sockaddr6, 0, len);
1477 sockaddr6.sin6_family = AF_INET6;
1479 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1481 GLE = WSAGetLastError();
1482 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1483 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1485 len = sizeof(sockaddr6);
1486 memset(&sockaddr6, 0, len);
1487 sockaddr6.sin6_family = AF_INET6;
1489 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1491 GLE = WSAGetLastError();
1492 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1493 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1495 len = sizeof(sockaddr6);
1496 memset(&sockaddr6, 0, len);
1497 sockaddr6.sin6_family = AF_INET6;
1499 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1501 GLE = WSAGetLastError();
1502 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1503 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1504 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1508 static void test_WSAStringToAddressW(void)
1511 SOCKADDR_IN sockaddr;
1512 SOCKADDR_IN6 sockaddr6;
1515 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1516 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1517 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1518 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1519 ':', '6', '5', '5', '3', '5', 0 };
1520 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1521 '6', '5', '5', '3', '5', 0 };
1522 WCHAR address6[] = {':',':','1','\0'};
1523 WCHAR address7[] = {'[',':',':','1',']','\0'};
1524 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1527 sockaddr.sin_family = AF_INET;
1529 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1530 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1531 WSAGetLastError() );
1533 len = sizeof(sockaddr);
1534 sockaddr.sin_port = 0;
1535 sockaddr.sin_addr.s_addr = 0;
1537 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1538 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1539 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1541 len = sizeof(sockaddr);
1542 sockaddr.sin_port = 0;
1543 sockaddr.sin_addr.s_addr = 0;
1545 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1546 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1547 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1549 len = sizeof(sockaddr);
1550 sockaddr.sin_port = 0;
1551 sockaddr.sin_addr.s_addr = 0;
1553 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1554 GLE = WSAGetLastError();
1555 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1556 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1557 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1559 len = sizeof(sockaddr);
1560 sockaddr.sin_port = 0;
1561 sockaddr.sin_addr.s_addr = 0;
1563 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1564 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1565 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1567 len = sizeof(sockaddr);
1568 sockaddr.sin_port = 0;
1569 sockaddr.sin_addr.s_addr = 0;
1571 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1572 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1573 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1574 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1576 len = sizeof(sockaddr6);
1577 memset(&sockaddr6, 0, len);
1578 sockaddr6.sin6_family = AF_INET6;
1580 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1582 GLE = WSAGetLastError();
1583 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1584 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1586 len = sizeof(sockaddr6);
1587 memset(&sockaddr6, 0, len);
1588 sockaddr6.sin6_family = AF_INET6;
1590 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1592 GLE = WSAGetLastError();
1593 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1594 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1596 len = sizeof(sockaddr6);
1597 memset(&sockaddr6, 0, len);
1598 sockaddr6.sin6_family = AF_INET6;
1600 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1602 GLE = WSAGetLastError();
1603 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1604 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1605 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1609 static VOID WINAPI SelectReadThread(select_thread_params *par)
1613 struct sockaddr_in addr;
1614 struct timeval select_timeout;
1617 FD_SET(par->s, &readfds);
1618 select_timeout.tv_sec=5;
1619 select_timeout.tv_usec=0;
1620 addr.sin_family = AF_INET;
1621 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1622 addr.sin_port = htons(SERVERPORT);
1624 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1625 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1627 SetEvent(server_ready);
1628 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1629 par->ReadKilled = (ret == 1);
1632 static void test_select(void)
1634 SOCKET fdRead, fdWrite;
1635 fd_set readfds, writefds, exceptfds;
1638 struct timeval select_timeout;
1639 select_thread_params thread_params;
1640 HANDLE thread_handle;
1643 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1644 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1645 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1646 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1650 FD_ZERO(&exceptfds);
1651 FD_SET(fdRead, &readfds);
1652 FD_SET(fdWrite, &writefds);
1653 FD_SET(fdRead, &exceptfds);
1654 FD_SET(fdWrite, &exceptfds);
1655 select_timeout.tv_sec=0;
1656 select_timeout.tv_usec=500;
1659 if (fdWrite > maxfd)
1663 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1664 ok ( (ret == 0), "select should not return any socket handles\n");
1665 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1666 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1669 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1670 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1673 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1675 ret = closesocket(fdWrite);
1676 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1678 thread_params.s = fdRead;
1679 thread_params.ReadKilled = FALSE;
1680 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1681 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1682 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1684 WaitForSingleObject (server_ready, INFINITE);
1686 ret = closesocket(fdRead);
1687 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1689 WaitForSingleObject (thread_handle, 1000);
1690 ok ( (thread_params.ReadKilled) ||
1691 broken(thread_params.ReadKilled == 0), /*Win98*/
1692 "closesocket did not wakeup select\n");
1696 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1698 struct sockaddr_in address;
1699 int len = sizeof(address);
1700 SOCKET client_socket;
1702 SetEvent(server_ready);
1703 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1704 if (client_socket != INVALID_SOCKET)
1705 closesocket(client_socket);
1706 par->ReadKilled = (client_socket == INVALID_SOCKET);
1710 static void test_accept(void)
1713 SOCKET server_socket = INVALID_SOCKET;
1714 struct sockaddr_in address;
1715 select_thread_params thread_params;
1716 HANDLE thread_handle = NULL;
1719 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1720 if (server_socket == INVALID_SOCKET)
1722 trace("error creating server socket: %d\n", WSAGetLastError());
1726 memset(&address, 0, sizeof(address));
1727 address.sin_family = AF_INET;
1728 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1731 trace("error binding server socket: %d\n", WSAGetLastError());
1735 ret = listen(server_socket, 1);
1738 trace("error making server socket listen: %d\n", WSAGetLastError());
1742 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1743 if (server_ready == INVALID_HANDLE_VALUE)
1745 trace("error creating event: %d\n", GetLastError());
1749 thread_params.s = server_socket;
1750 thread_params.ReadKilled = FALSE;
1751 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
1752 &thread_params, 0, &id);
1753 if (thread_handle == NULL)
1755 trace("error creating thread: %d\n", GetLastError());
1759 WaitForSingleObject(server_ready, INFINITE);
1761 ret = closesocket(server_socket);
1764 trace("closesocket failed: %d\n", WSAGetLastError());
1768 WaitForSingleObject(thread_handle, 1000);
1769 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
1772 if (thread_handle != NULL)
1773 CloseHandle(thread_handle);
1774 if (server_ready != INVALID_HANDLE_VALUE)
1775 CloseHandle(server_ready);
1776 if (server_socket != INVALID_SOCKET)
1777 closesocket(server_socket);
1780 static void test_extendedSocketOptions(void)
1784 struct sockaddr_in sa;
1785 int sa_len = sizeof(struct sockaddr_in);
1786 int optval, optlen = sizeof(int), ret;
1790 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1791 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1795 memset(&sa, 0, sa_len);
1797 sa.sin_family = AF_INET;
1798 sa.sin_port = htons(0);
1799 sa.sin_addr.s_addr = htonl(INADDR_ANY);
1801 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
1802 trace("Creating the socket failed: %d\n", WSAGetLastError());
1807 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1808 trace("Failed to bind socket: %d\n", WSAGetLastError());
1814 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
1816 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
1817 ok((optval == 65507) || (optval == 65527),
1818 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
1820 optlen = sizeof(LINGER);
1821 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1823 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
1828 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
1829 trace("Creating the socket failed: %d\n", WSAGetLastError());
1834 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1835 trace("Failed to bind socket: %d\n", WSAGetLastError());
1841 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1842 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
1844 optlen = sizeof(BOOL);
1845 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
1846 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
1847 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
1848 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
1849 bool_opt_val, linger_val.l_onoff);
1855 static void test_getsockname(void)
1859 struct sockaddr_in sa_set, sa_get;
1860 int sa_set_len = sizeof(struct sockaddr_in);
1861 int sa_get_len = sa_set_len;
1862 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
1865 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1866 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1870 memset(&sa_set, 0, sa_set_len);
1872 sa_set.sin_family = AF_INET;
1873 sa_set.sin_port = htons(0);
1874 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
1876 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
1877 trace("Creating the socket failed: %d\n", WSAGetLastError());
1882 memcpy(&sa_get, &sa_set, sizeof(sa_set));
1883 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
1884 ok(0, "getsockname on unbound socket should fail\n");
1886 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
1887 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
1888 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
1889 "failed getsockname modified sockaddr when it shouldn't\n");
1892 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
1893 trace("Failed to bind socket: %d\n", WSAGetLastError());
1899 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
1900 trace("Failed to call getsockname: %d\n", WSAGetLastError());
1906 ret = memcmp(sa_get.sin_zero, null_padding, 8);
1907 ok(ret == 0 || broken(ret != 0), /* NT4 */
1908 "getsockname did not zero the sockaddr_in structure\n");
1914 static void test_dns(void)
1918 h = gethostbyname("");
1919 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
1922 /* Our winsock headers don't define gethostname because it conflicts with the
1923 * definition in unistd.h. Define it here to get rid of the warning. */
1925 int WINAPI gethostname(char *name, int namelen);
1927 static void test_gethostbyname_hack(void)
1931 static BYTE loopback[] = {127, 0, 0, 1};
1932 static BYTE magic_loopback[] = {127, 12, 34, 56};
1935 ret = gethostname(name, 256);
1936 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
1938 he = gethostbyname("localhost");
1939 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
1942 if(he->h_length != 4)
1944 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
1948 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
1949 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
1950 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
1951 he->h_addr_list[0][3]);
1954 /* No reason to test further with NULL hostname */
1958 if(strcmp(name, "localhost") == 0)
1960 skip("hostname seems to be \"localhost\", skipping test.\n");
1965 he = gethostbyname(name);
1966 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
1969 if(he->h_length != 4)
1971 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
1975 if (he->h_addr_list[0][0] == 127)
1977 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
1978 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
1979 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
1980 he->h_addr_list[0][2], he->h_addr_list[0][3]);
1985 he = gethostbyname("nonexistent.winehq.org");
1986 /* Don't check for the return value, as some braindead ISPs will kindly
1987 * resolve nonexistent host names to addresses of the ISP's spam pages. */
1990 static void test_inet_addr(void)
1994 addr = inet_addr(NULL);
1995 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
1998 static void test_ioctlsocket(void)
2002 long cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
2005 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2006 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
2007 if(sock == INVALID_SOCKET)
2009 skip("Can't continue without a socket.\n");
2013 for(i = 0; i < sizeof(cmds)/sizeof(long); i++)
2015 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2016 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
2017 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
2018 ret = WSAGetLastError();
2019 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
2023 static int drain_pause=0;
2024 static DWORD WINAPI drain_socket_thread(LPVOID arg)
2027 SOCKET sock = *(SOCKET*)arg;
2030 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
2034 if (WSAGetLastError() == WSAEWOULDBLOCK)
2038 FD_SET(sock, &readset);
2039 select(0, &readset, NULL, NULL, NULL);
2050 static void test_send(void)
2052 SOCKET src = INVALID_SOCKET;
2053 SOCKET dst = INVALID_SOCKET;
2054 HANDLE hThread = NULL;
2055 const int buflen = 1024*1024;
2056 char *buffer = NULL;
2060 if (tcp_socketpair(&src, &dst) != 0)
2062 ok(0, "creating socket pair failed, skipping test\n");
2066 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2067 if (hThread == NULL)
2069 ok(0, "CreateThread failed, error %d\n", GetLastError());
2073 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
2076 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2080 ret = send(src, buffer, buflen, 0);
2082 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
2084 ok(0, "send failed, error %d\n", WSAGetLastError());
2087 if (src != INVALID_SOCKET)
2089 if (dst != INVALID_SOCKET)
2091 if (hThread != NULL)
2092 CloseHandle(hThread);
2093 HeapFree(GetProcessHeap(), 0, buffer);
2096 static void test_write_events(void)
2098 SOCKET src = INVALID_SOCKET;
2099 SOCKET dst = INVALID_SOCKET;
2100 HANDLE hThread = NULL;
2101 HANDLE hEvent = INVALID_HANDLE_VALUE;
2102 char *buffer = NULL;
2103 int bufferSize = 1024*1024;
2107 WSANETWORKEVENTS netEvents;
2110 if (tcp_socketpair(&src, &dst) != 0)
2112 ok(0, "creating socket pair failed, skipping test\n");
2116 /* On Windows it seems when a non-blocking socket sends to a
2117 blocking socket on the same host, the send() is BLOCKING,
2118 so make both sockets non-blocking */
2119 ret = ioctlsocket(src, FIONBIO, &one);
2122 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2125 ret = ioctlsocket(dst, FIONBIO, &one);
2128 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2132 buffer = HeapAlloc(GetProcessHeap(), 0, bufferSize);
2135 ok(0, "could not allocate memory for test\n");
2139 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2140 if (hThread == NULL)
2142 ok(0, "CreateThread failed, error %d\n", GetLastError());
2146 hEvent = CreateEventA(NULL, FALSE, TRUE, NULL);
2147 if (hEvent == INVALID_HANDLE_VALUE)
2149 ok(0, "CreateEventA failed, error %d\n", GetLastError());
2153 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
2156 ok(0, "WSAEventSelect failed, error %d\n", ret);
2160 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
2161 dwRet = WaitForSingleObject(hEvent, 5000);
2162 if (dwRet != WAIT_OBJECT_0)
2164 ok(0, "Initial WaitForSingleObject failed, error %d\n", dwRet);
2167 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2170 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2173 if (netEvents.lNetworkEvents & FD_WRITE)
2175 ret = send(src, "a", 1, 0);
2176 ok(ret == 1, "sending 1 byte failed, error %d\n", WSAGetLastError());
2182 ok(0, "FD_WRITE not among initial events\n");
2186 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
2187 dwRet = WaitForSingleObject(hEvent, 2000);
2188 if (dwRet == WAIT_OBJECT_0)
2190 ok(0, "WaitForSingleObject should have timed out, but succeeded!\n");
2194 /* Now if we send a ton of data and the 'server' does not drain it fast
2195 * enough (set drain_pause to be sure), the socket send buffer will only
2196 * take some of it, and we will get a short write. This will trigger
2197 * another FD_WRITE event as soon as data is sent and more space becomes
2198 * available, but not any earlier. */
2202 ret = send(src, buffer, bufferSize, 0);
2203 } while (ret == bufferSize);
2205 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
2207 dwRet = WaitForSingleObject(hEvent, 5000);
2208 ok(dwRet == WAIT_OBJECT_0, "Waiting failed with %d\n", dwRet);
2209 if (dwRet == WAIT_OBJECT_0)
2211 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2212 ok(ret == 0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2215 ok(netEvents.lNetworkEvents & FD_WRITE,
2216 "FD_WRITE event not set as expected, events are 0x%x\n", netEvents.lNetworkEvents);
2223 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
2228 HeapFree(GetProcessHeap(), 0, buffer);
2229 if (src != INVALID_SOCKET)
2231 if (dst != INVALID_SOCKET)
2233 if (hThread != NULL)
2234 CloseHandle(hThread);
2235 CloseHandle(hEvent);
2238 static void test_ipv6only(void)
2240 SOCKET v4 = INVALID_SOCKET,
2241 v6 = INVALID_SOCKET;
2242 struct sockaddr_in sin4;
2243 struct sockaddr_in6 sin6;
2246 memset(&sin4, 0, sizeof(sin4));
2247 sin4.sin_family = AF_INET;
2248 sin4.sin_port = htons(SERVERPORT);
2250 memset(&sin6, 0, sizeof(sin6));
2251 sin6.sin6_family = AF_INET6;
2252 sin6.sin6_port = htons(SERVERPORT);
2254 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2255 if (v6 == INVALID_SOCKET) {
2256 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2257 WSAGetLastError(), WSAEAFNOSUPPORT);
2260 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
2262 skip("Could not bind IPv6 address (LastError: %d).\n",
2267 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2268 if (v4 == INVALID_SOCKET) {
2269 skip("Could not create IPv4 socket (LastError: %d).\n",
2273 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
2274 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2275 WSAGetLastError(), WSAEADDRINUSE);
2278 if (v4 != INVALID_SOCKET)
2280 if (v6 != INVALID_SOCKET)
2284 static void test_WSASendTo(void)
2287 struct sockaddr_in addr;
2288 char buf[12] = "hello world";
2292 addr.sin_family = AF_INET;
2293 addr.sin_port = htons(139);
2294 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2295 data_buf.len = sizeof(buf);
2298 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
2299 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2303 WSASetLastError(12345);
2304 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
2305 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2308 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2309 "a successful call to WSASendTo()\n");
2312 /**************** Main program ***************/
2319 test_set_getsockopt();
2320 test_so_reuseaddr();
2321 test_extendedSocketOptions();
2323 for (i = 0; i < NUM_TESTS; i++)
2325 trace ( " **** STARTING TEST %d ****\n", i );
2326 do_test ( &tests[i] );
2327 trace ( " **** TEST %d COMPLETE ****\n", i );
2332 test_getservbyname();
2335 test_WSAAddressToStringA();
2336 test_WSAAddressToStringW();
2338 test_WSAStringToAddressA();
2339 test_WSAStringToAddressW();
2347 test_gethostbyname_hack();
2350 test_write_events();