push e263afdf8dbf9f9408e8594e045d25c4af1d55cd
[wine/hacks.git] / dlls / ws2_32 / tests / sock.c
blobe2d21621303fc27d56bfff53c064179be04f6940
1 /*
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
22 #include <stdarg.h>
24 #include <windef.h>
25 #include <winbase.h>
26 #include <winsock2.h>
27 #include <ws2tcpip.h>
28 #include <mswsock.h>
29 #include "wine/test.h"
30 #include <winnt.h>
31 #include <winerror.h>
33 #define MAX_CLIENTS 4 /* Max number of clients */
34 #define NUM_TESTS 4 /* 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) \
50 do { \
51 int tmp, err = 0; \
52 tmp = op; \
53 if ( !(cond tmp) ) err = WSAGetLastError(); \
54 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
55 } while (0);
58 /**************** Structs and typedefs ***************/
60 typedef struct thread_info
62 HANDLE thread;
63 DWORD id;
64 } thread_info;
66 /* Information in the server about open client connections */
67 typedef struct sock_info
69 SOCKET s;
70 struct sockaddr_in addr;
71 struct sockaddr_in peer;
72 char *buf;
73 int n_recvd;
74 int n_sent;
75 } sock_info;
77 /* Test parameters for both server & client */
78 typedef struct test_params
80 int sock_type;
81 int sock_prot;
82 const char *inet_addr;
83 short inet_port;
84 int chunk_size;
85 int n_chunks;
86 int n_clients;
87 } test_params;
89 /* server-specific test parameters */
90 typedef struct server_params
92 test_params *general;
93 DWORD sock_flags;
94 int buflen;
95 } server_params;
97 /* client-specific test parameters */
98 typedef struct client_params
100 test_params *general;
101 DWORD sock_flags;
102 int buflen;
103 } client_params;
105 /* This type combines all information for setting up a test scenario */
106 typedef struct test_setup
108 test_params general;
109 LPVOID srv;
110 server_params srv_params;
111 LPVOID clt;
112 client_params clt_params;
113 } test_setup;
115 /* Thread local storage for server */
116 typedef struct server_memory
118 SOCKET s;
119 struct sockaddr_in addr;
120 sock_info sock[MAX_CLIENTS];
121 } server_memory;
123 /* Thread local storage for client */
124 typedef struct client_memory
126 SOCKET s;
127 struct sockaddr_in addr;
128 char *send_buf;
129 char *recv_buf;
130 } client_memory;
132 /* SelectReadThread thread parameters */
133 typedef struct select_thread_params
135 SOCKET s;
136 BOOL ReadKilled;
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;
154 int len;
155 int ret;
157 *src = INVALID_SOCKET;
158 *dst = INVALID_SOCKET;
160 *src = socket(AF_INET, SOCK_STREAM, 0);
161 if (*src == INVALID_SOCKET)
162 goto end;
164 server = socket(AF_INET, SOCK_STREAM, 0);
165 if (server == INVALID_SOCKET)
166 goto end;
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));
172 if (ret != 0)
173 goto end;
175 len = sizeof(addr);
176 ret = getsockname(server, (struct sockaddr*)&addr, &len);
177 if (ret != 0)
178 goto end;
180 ret = listen(server, 1);
181 if (ret != 0)
182 goto end;
184 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
185 if (ret != 0)
186 goto end;
188 len = sizeof(addr);
189 *dst = accept(server, (struct sockaddr*)&addr, &len);
191 end:
192 if (server != INVALID_SOCKET)
193 closesocket(server);
194 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
195 return 0;
196 closesocket(*src);
197 closesocket(*dst);
198 return -1;
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 )
222 char c, *p;
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 )
229 char c, *p;
230 int i;
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;
236 return NULL;
240 * This routine is called when a client / server does not expect any more data,
241 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
243 static void read_zero_bytes ( SOCKET s )
245 char buf[256];
246 int tmp, n = 0;
247 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
248 n += tmp;
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;
255 int n = 1;
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" );
259 return p - buf;
262 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
264 char* last = buf + buflen, *p;
265 int n = 1;
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" );
269 return p - buf;
272 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen,int flags,struct sockaddr *from, socklen_t *fromlen, int recvlen )
274 char* last = buf + buflen, *p;
275 int n = 1;
276 for ( p = buf; n > 0 && p < last; p += n )
277 n = recvfrom ( s, p, min ( recvlen, last - p ), 0, from, fromlen );
278 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
279 return p - buf;
283 * Call this routine right after thread startup.
284 * SO_OPENTYPE must by 0, regardless what the server did.
286 static void check_so_opentype (void)
288 int tmp = 1, len;
289 len = sizeof (tmp);
290 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
291 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
294 /**************** Server utility functions ***************/
297 * Even if we have closed our server socket cleanly,
298 * the OS may mark the address "in use" for some time -
299 * this happens with native Linux apps, too.
301 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
303 int err, wsaerr = 0, n_try = BIND_TRIES;
305 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
306 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
307 n_try-- >= 0)
309 trace ( "address in use, waiting ...\n" );
310 Sleep ( 1000 * BIND_SLEEP );
312 ok ( err == 0, "failed to bind: %d\n", wsaerr );
315 static void server_start ( server_params *par )
317 int i;
318 test_params *gen = par->general;
319 server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
321 TlsSetValue ( tls, mem );
322 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
323 NULL, 0, par->sock_flags );
324 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
326 mem->addr.sin_family = AF_INET;
327 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
328 mem->addr.sin_port = htons ( gen->inet_port );
330 for (i = 0; i < MAX_CLIENTS; i++)
332 mem->sock[i].s = INVALID_SOCKET;
333 mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
334 mem->sock[i].n_recvd = 0;
335 mem->sock[i].n_sent = 0;
338 if ( gen->sock_type == SOCK_STREAM )
339 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
342 static void server_stop (void)
344 int i;
345 server_memory *mem = TlsGetValue ( tls );
347 for (i = 0; i < MAX_CLIENTS; i++ )
349 LocalFree ( (HANDLE) mem->sock[i].buf );
350 if ( mem->sock[i].s != INVALID_SOCKET )
351 closesocket ( mem->sock[i].s );
353 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
354 LocalFree ( (HANDLE) mem );
355 ExitThread ( GetCurrentThreadId () );
358 /**************** Client utilitiy functions ***************/
360 static void client_start ( client_params *par )
362 test_params *gen = par->general;
363 client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
365 TlsSetValue ( tls, mem );
367 WaitForSingleObject ( server_ready, INFINITE );
369 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
370 NULL, 0, par->sock_flags );
372 mem->addr.sin_family = AF_INET;
373 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
374 mem->addr.sin_port = htons ( gen->inet_port );
376 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
378 mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
379 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
380 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
382 SetEvent ( client_ready[client_id] );
383 /* Wait for the other clients to come up */
384 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
387 static void client_stop (void)
389 client_memory *mem = TlsGetValue ( tls );
390 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
391 LocalFree ( (HANDLE) mem->send_buf );
392 LocalFree ( (HANDLE) mem );
393 ExitThread(0);
396 /**************** Servers ***************/
399 * simple_server: A very basic server doing synchronous IO.
401 static VOID WINAPI simple_server ( server_params *par )
403 test_params *gen = par->general;
404 server_memory *mem;
405 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
406 id = GetCurrentThreadId();
407 char *p;
409 trace ( "simple_server (%x) starting\n", id );
411 set_so_opentype ( FALSE ); /* non-overlapped */
412 server_start ( par );
413 mem = TlsGetValue ( tls );
415 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
416 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
418 trace ( "simple_server (%x) ready\n", id );
419 SetEvent ( server_ready ); /* notify clients */
421 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
423 trace ( "simple_server (%x): waiting for client\n", id );
425 /* accept a single connection */
426 tmp = sizeof ( mem->sock[0].peer );
427 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
428 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
430 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
431 "simple_server (%x): strange peer address\n", id );
433 /* Receive data & check it */
434 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
435 ok ( n_recvd == n_expected,
436 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
437 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
438 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
440 /* Echo data back */
441 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
442 ok ( n_sent == n_expected,
443 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
445 /* cleanup */
446 read_zero_bytes ( mem->sock[0].s );
447 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
448 mem->sock[0].s = INVALID_SOCKET;
451 trace ( "simple_server (%x) exiting\n", id );
452 server_stop ();
456 * select_server: A non-blocking server.
458 static VOID WINAPI select_server ( server_params *par )
460 test_params *gen = par->general;
461 server_memory *mem;
462 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
463 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
464 n_set, delta, n_ready;
465 char *p;
466 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
467 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
469 trace ( "select_server (%x) starting\n", id );
471 set_so_opentype ( FALSE ); /* non-overlapped */
472 server_start ( par );
473 mem = TlsGetValue ( tls );
475 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
476 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
478 trace ( "select_server (%x) ready\n", id );
479 SetEvent ( server_ready ); /* notify clients */
481 FD_ZERO ( &fds_openrecv );
482 FD_ZERO ( &fds_recv );
483 FD_ZERO ( &fds_send );
484 FD_ZERO ( &fds_opensend );
486 FD_SET ( mem->s, &fds_openrecv );
488 while(1)
490 fds_recv = fds_openrecv;
491 fds_send = fds_opensend;
493 n_set = 0;
495 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
496 "select_server (%x): select() failed: %d\n" );
498 /* check for incoming requests */
499 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
500 n_set += 1;
502 trace ( "select_server (%x): accepting client connection\n", id );
504 /* accept a single connection */
505 tmp = sizeof ( mem->sock[n_connections].peer );
506 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
507 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
509 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
510 "select_server (%x): strange peer address\n", id );
512 /* add to list of open connections */
513 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
514 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
516 n_connections++;
519 /* handle open requests */
521 for ( i = 0; i < n_connections; i++ )
523 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
524 n_set += 1;
526 if ( mem->sock[i].n_recvd < n_expected ) {
527 /* Receive data & check it */
528 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 );
529 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
530 mem->sock[i].n_recvd += n_recvd;
532 if ( mem->sock[i].n_recvd == n_expected ) {
533 p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
534 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
535 FD_CLR ( mem->sock[i].s, &fds_openrecv );
538 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
542 /* only echo back what we've received */
543 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
545 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
546 n_set += 1;
548 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
549 /* Echo data back */
550 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
551 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
552 mem->sock[i].n_sent += n_sent;
554 if ( mem->sock[i].n_sent == n_expected ) {
555 FD_CLR ( mem->sock[i].s, &fds_opensend );
558 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
563 /* check that select returned the correct number of ready sockets */
564 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
566 /* check if all clients are done */
567 if ( ( fds_opensend.fd_count == 0 )
568 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
569 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
570 break;
574 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
576 /* cleanup */
577 read_zero_bytes ( mem->sock[i].s );
578 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
579 mem->sock[i].s = INVALID_SOCKET;
582 trace ( "select_server (%x) exiting\n", id );
583 server_stop ();
586 /**************** Clients ***************/
589 * simple_client: A very basic client doing synchronous IO.
591 static VOID WINAPI simple_client ( client_params *par )
593 test_params *gen = par->general;
594 client_memory *mem;
595 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
596 char *p;
598 id = GetCurrentThreadId();
599 trace ( "simple_client (%x): starting\n", id );
600 /* wait here because we want to call set_so_opentype before creating a socket */
601 WaitForSingleObject ( server_ready, INFINITE );
602 trace ( "simple_client (%x): server ready\n", id );
604 check_so_opentype ();
605 set_so_opentype ( FALSE ); /* non-overlapped */
606 client_start ( par );
607 mem = TlsGetValue ( tls );
609 /* Connect */
610 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
611 0 ==, "simple_client (%x): connect error: %d\n" );
612 ok ( set_blocking ( mem->s, TRUE ) == 0,
613 "simple_client (%x): failed to set blocking mode\n", id );
614 trace ( "simple_client (%x) connected\n", id );
616 /* send data to server */
617 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
618 ok ( n_sent == n_expected,
619 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
621 /* shutdown send direction */
622 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
624 /* Receive data echoed back & check it */
625 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
626 ok ( n_recvd == n_expected,
627 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
629 /* check data */
630 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
631 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
633 /* cleanup */
634 read_zero_bytes ( mem->s );
635 trace ( "simple_client (%x) exiting\n", id );
636 client_stop ();
640 * simple_mixed_client: mixing send and recvfrom
642 static VOID WINAPI simple_mixed_client ( client_params *par )
644 test_params *gen = par->general;
645 client_memory *mem;
646 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
647 char *p;
648 socklen_t fromLen = sizeof(mem->addr);
649 struct sockaddr test;
651 id = GetCurrentThreadId();
652 trace ( "simple_client (%x): starting\n", id );
653 /* wait here because we want to call set_so_opentype before creating a socket */
654 WaitForSingleObject ( server_ready, INFINITE );
655 trace ( "simple_client (%x): server ready\n", id );
657 check_so_opentype ();
658 set_so_opentype ( FALSE ); /* non-overlapped */
659 client_start ( par );
660 mem = TlsGetValue ( tls );
662 /* Connect */
663 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
664 0 ==, "simple_client (%x): connect error: %d\n" );
665 ok ( set_blocking ( mem->s, TRUE ) == 0,
666 "simple_client (%x): failed to set blocking mode\n", id );
667 trace ( "simple_client (%x) connected\n", id );
669 /* send data to server */
670 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
671 ok ( n_sent == n_expected,
672 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
674 /* shutdown send direction */
675 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
677 /* this shouldn't change, since lpFrom, is not updated on
678 connection oriented sockets - exposed by bug 11640
680 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
682 /* Receive data echoed back & check it */
683 n_recvd = do_synchronous_recvfrom ( mem->s,
684 mem->recv_buf,
685 n_expected,
687 (struct sockaddr *)&test,
688 &fromLen,
689 par->buflen );
690 ok ( n_recvd == n_expected,
691 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
693 /* check that lpFrom was not updated */
694 ok(0 ==
695 strcmp(
696 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
697 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
699 /* check data */
700 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
701 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
703 /* cleanup */
704 read_zero_bytes ( mem->s );
705 trace ( "simple_client (%x) exiting\n", id );
706 client_stop ();
710 * event_client: An event-driven client
712 static void WINAPI event_client ( client_params *par )
714 test_params *gen = par->general;
715 client_memory *mem;
716 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
717 tmp, err, n;
718 HANDLE event;
719 WSANETWORKEVENTS wsa_events;
720 char *send_last, *recv_last, *send_p, *recv_p;
721 long mask = FD_READ | FD_WRITE | FD_CLOSE;
723 trace ( "event_client (%x): starting\n", id );
724 client_start ( par );
725 trace ( "event_client (%x): server ready\n", id );
727 mem = TlsGetValue ( tls );
729 /* Prepare event notification for connect, makes socket nonblocking */
730 event = WSACreateEvent ();
731 WSAEventSelect ( mem->s, event, FD_CONNECT );
732 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
733 if ( tmp != 0 ) {
734 err = WSAGetLastError ();
735 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
736 tmp = WaitForSingleObject ( event, INFINITE );
737 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
738 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
739 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
740 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
741 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
742 if ( err ) goto out;
745 trace ( "event_client (%x) connected\n", id );
747 WSAEventSelect ( mem->s, event, mask );
749 recv_p = mem->recv_buf;
750 recv_last = mem->recv_buf + n_expected;
751 send_p = mem->send_buf;
752 send_last = mem->send_buf + n_expected;
754 while ( TRUE )
756 err = WaitForSingleObject ( event, INFINITE );
757 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
759 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
760 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
762 if ( wsa_events.lNetworkEvents & FD_WRITE )
764 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
765 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
767 if ( err== 0 )
770 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
771 if ( n < 0 )
773 err = WSAGetLastError ();
774 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
776 else
777 send_p += n;
779 while ( n >= 0 && send_p < send_last );
781 if ( send_p == send_last )
783 trace ( "event_client (%x): all data sent - shutdown\n", id );
784 shutdown ( mem->s, SD_SEND );
785 mask &= ~FD_WRITE;
786 WSAEventSelect ( mem->s, event, mask );
789 if ( wsa_events.lNetworkEvents & FD_READ )
791 err = wsa_events.iErrorCode[ FD_READ_BIT ];
792 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
793 if ( err != 0 ) break;
795 /* First read must succeed */
796 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
797 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
799 while ( n >= 0 ) {
800 recv_p += n;
801 if ( recv_p == recv_last )
803 mask &= ~FD_READ;
804 trace ( "event_client (%x): all data received\n", id );
805 WSAEventSelect ( mem->s, event, mask );
806 break;
808 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
809 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
810 ok ( 0, "event_client (%x): read error: %d\n", id, err );
814 if ( wsa_events.lNetworkEvents & FD_CLOSE )
816 trace ( "event_client (%x): close event\n", id );
817 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
818 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
819 break;
823 ok ( send_p == send_last,
824 "simple_client (%x): sent less data than expected: %d of %d\n",
825 id, send_p - mem->send_buf, n_expected );
826 ok ( recv_p == recv_last,
827 "simple_client (%x): received less data than expected: %d of %d\n",
828 id, recv_p - mem->recv_buf, n_expected );
829 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
830 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
832 out:
833 WSACloseEvent ( event );
834 trace ( "event_client (%x) exiting\n", id );
835 client_stop ();
838 /**************** Main program utility functions ***************/
840 static void Init (void)
842 WORD ver = MAKEWORD (2, 2);
843 WSADATA data;
845 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
846 tls = TlsAlloc();
849 static void Exit (void)
851 INT ret, err;
852 TlsFree ( tls );
853 ret = WSACleanup();
854 err = WSAGetLastError();
855 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
856 ret = WSACleanup();
857 err = WSAGetLastError();
858 ok ( ret == SOCKET_ERROR && err == WSANOTINITIALISED,
859 "WSACleanup returned %d GetLastError is %d\n", ret, err);
862 static void StartServer (LPTHREAD_START_ROUTINE routine,
863 test_params *general, server_params *par)
865 par->general = general;
866 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
867 ok ( thread[0] != NULL, "Failed to create server thread\n" );
870 static void StartClients (LPTHREAD_START_ROUTINE routine,
871 test_params *general, client_params *par)
873 int i;
874 par->general = general;
875 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
877 client_id = i - 1;
878 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
879 ok ( thread[i] != NULL, "Failed to create client thread\n" );
880 /* Make sure the client is up and running */
881 WaitForSingleObject ( client_ready[client_id], INFINITE );
885 static void do_test( test_setup *test )
887 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
888 DWORD wait;
890 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
891 for (i = 0; i <= n; i++)
892 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
894 StartServer ( test->srv, &test->general, &test->srv_params );
895 StartClients ( test->clt, &test->general, &test->clt_params );
896 WaitForSingleObject ( server_ready, INFINITE );
898 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
899 ok ( wait <= WAIT_OBJECT_0 + n ,
900 "some threads have not completed: %x\n", wait );
902 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
904 for (i = 0; i <= n; i++)
906 trace ("terminating thread %08x\n", thread_id[i]);
907 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
908 TerminateThread ( thread [i], 0 );
911 CloseHandle ( server_ready );
912 for (i = 0; i <= n; i++)
913 CloseHandle ( client_ready[i] );
916 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
917 /* optname = SO_LINGER */
918 LINGER linger_testvals[] = {
919 {0,0},
920 {0,73},
921 {1,0},
922 {5,189}
925 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
926 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
927 bug in the linux kernel (fixed in 2.6.8) */
928 #define SOCKTIMEOUT2 997000 /* 997 seconds */
930 static void test_set_getsockopt(void)
932 SOCKET s;
933 int i, err, lasterr;
934 int timeout;
935 LINGER lingval;
936 int size;
938 s = socket(AF_INET, SOCK_STREAM, 0);
939 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
940 if( s == INVALID_SOCKET) return;
941 /* SO_RCVTIMEO */
942 timeout = SOCKTIMEOUT1;
943 size = sizeof(timeout);
944 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
945 if( !err)
946 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
947 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
948 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
949 /* SO_SNDTIMEO */
950 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
951 size = sizeof(timeout);
952 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
953 if( !err)
954 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
955 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
956 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
957 /* SO_LINGER */
958 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
959 size = sizeof(lingval);
960 lingval = linger_testvals[i];
961 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
962 if( !err)
963 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
964 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
965 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
966 (lingval.l_linger == linger_testvals[i].l_linger ||
967 (!lingval.l_linger && !linger_testvals[i].l_onoff))
968 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
969 lingval.l_onoff, lingval.l_linger,
970 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
972 /* Test for erroneously passing a value instead of a pointer as optval */
973 size = sizeof(char);
974 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
975 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
976 "instead of failing.\n");
977 lasterr = WSAGetLastError();
978 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
979 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
980 lasterr, WSAEFAULT);
981 closesocket(s);
984 static void test_so_reuseaddr(void)
986 struct sockaddr_in saddr;
987 SOCKET s1,s2;
988 unsigned int rc,reuse;
989 int size;
991 saddr.sin_family = AF_INET;
992 saddr.sin_port = htons(9375);
993 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
995 s1=socket(AF_INET, SOCK_STREAM, 0);
996 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
997 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
998 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1000 s2=socket(AF_INET, SOCK_STREAM, 0);
1001 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1003 reuse=0x1234;
1004 size=sizeof(reuse);
1005 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1006 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1008 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1009 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1011 reuse = 1;
1012 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1013 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1015 todo_wine {
1016 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1017 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1020 closesocket(s2);
1021 closesocket(s1);
1024 /************* Array containing the tests to run **********/
1026 #define STD_STREAM_SOCKET \
1027 SOCK_STREAM, \
1028 0, \
1029 SERVERIP, \
1030 SERVERPORT
1032 static test_setup tests [NUM_TESTS] =
1034 /* Test 0: synchronous client and server */
1037 STD_STREAM_SOCKET,
1038 2048,
1042 simple_server,
1044 NULL,
1048 simple_client,
1050 NULL,
1055 /* Test 1: event-driven client, synchronous server */
1058 STD_STREAM_SOCKET,
1059 2048,
1063 simple_server,
1065 NULL,
1069 event_client,
1071 NULL,
1072 WSA_FLAG_OVERLAPPED,
1076 /* Test 2: synchronous client, non-blocking server via select() */
1079 STD_STREAM_SOCKET,
1080 2048,
1084 select_server,
1086 NULL,
1090 simple_client,
1092 NULL,
1097 /* Test 3: synchronous mixed client and server */
1100 STD_STREAM_SOCKET,
1101 2048,
1105 simple_server,
1107 NULL,
1111 simple_mixed_client,
1113 NULL,
1120 static void test_UDP(void)
1122 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1123 possible that this test fails due to dropped packets. */
1125 /* peer 0 receives data from all other peers */
1126 struct sock_info peer[NUM_UDP_PEERS];
1127 char buf[16];
1128 int ss, i, n_recv, n_sent;
1130 memset (buf,0,sizeof(buf));
1131 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1132 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1134 peer[i].addr.sin_family = AF_INET;
1135 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1137 if ( i == 0 ) {
1138 peer[i].addr.sin_port = htons ( SERVERPORT );
1139 } else {
1140 peer[i].addr.sin_port = htons ( 0 );
1143 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1145 /* test getsockname() to get peer's port */
1146 ss = sizeof ( peer[i].addr );
1147 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1148 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1151 /* test getsockname() */
1152 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1154 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1155 /* send client's ip */
1156 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1157 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1158 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1161 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1162 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1163 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1164 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1168 static void WINAPI do_getservbyname( HANDLE *starttest )
1170 struct {
1171 const char *name;
1172 const char *proto;
1173 int port;
1174 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1176 int i, j;
1177 struct servent *pserv[2];
1179 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1181 /* ensure that necessary buffer resizes are completed */
1182 for ( j = 0; j < 2; j++) {
1183 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1186 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1187 for ( j = 0; j < 2; j++ ) {
1188 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1189 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1190 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) );
1191 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 );
1192 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 );
1195 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1199 static void test_getservbyname(void)
1201 int i;
1202 HANDLE starttest, thread[NUM_THREADS];
1203 DWORD thread_id[NUM_THREADS];
1205 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1207 /* create threads */
1208 for ( i = 0; i < NUM_THREADS; i++ ) {
1209 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1212 /* signal threads to start */
1213 SetEvent ( starttest );
1215 for ( i = 0; i < NUM_THREADS; i++) {
1216 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1220 static void test_WSASocket(void)
1222 SOCKET sock = INVALID_SOCKET;
1223 WSAPROTOCOL_INFOA *pi;
1224 int providers[] = {6, 0};
1225 int ret, err;
1226 UINT pi_size;
1228 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1229 * to avoid a crash on win98.
1231 pi_size = 0;
1232 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1233 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1234 ret);
1235 err = WSAGetLastError();
1236 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1237 err, WSAENOBUFS);
1239 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1240 ok(pi != NULL, "Failed to allocate memory\n");
1241 if (pi == NULL) {
1242 skip("Can't continue without memory.\n");
1243 return;
1246 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1247 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1248 WSAGetLastError());
1250 if (ret == 0) {
1251 skip("No protocols enumerated.\n");
1252 HeapFree(GetProcessHeap(), 0, pi);
1253 return;
1256 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1257 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1258 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1259 WSAGetLastError());
1261 closesocket(sock);
1262 HeapFree(GetProcessHeap(), 0, pi);
1265 static void test_WSAAddressToStringA(void)
1267 INT ret;
1268 DWORD len;
1269 int GLE;
1270 SOCKADDR_IN sockaddr;
1271 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1273 CHAR expect1[] = "0.0.0.0";
1274 CHAR expect2[] = "255.255.255.255";
1275 CHAR expect3[] = "0.0.0.0:65535";
1276 CHAR expect4[] = "255.255.255.255:65535";
1278 len = 0;
1280 sockaddr.sin_family = AF_INET;
1281 sockaddr.sin_port = 0;
1282 sockaddr.sin_addr.s_addr = 0;
1284 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1285 GLE = WSAGetLastError();
1286 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1287 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1288 GLE, ret );
1290 len = sizeof(address);
1292 sockaddr.sin_family = AF_INET;
1293 sockaddr.sin_port = 0;
1294 sockaddr.sin_addr.s_addr = 0;
1296 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1297 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1299 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1301 len = sizeof(address);
1303 sockaddr.sin_family = AF_INET;
1304 sockaddr.sin_port = 0;
1305 sockaddr.sin_addr.s_addr = 0xffffffff;
1307 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1308 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1310 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1312 len = sizeof(address);
1314 sockaddr.sin_family = AF_INET;
1315 sockaddr.sin_port = 0xffff;
1316 sockaddr.sin_addr.s_addr = 0;
1318 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1319 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1321 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1323 len = sizeof(address);
1325 sockaddr.sin_family = AF_INET;
1326 sockaddr.sin_port = 0xffff;
1327 sockaddr.sin_addr.s_addr = 0xffffffff;
1329 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1330 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1332 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1335 static void test_WSAAddressToStringW(void)
1337 INT ret;
1338 DWORD len;
1339 int GLE;
1340 SOCKADDR_IN sockaddr;
1341 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1343 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1344 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1345 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1346 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1347 '6', '5', '5', '3', '5', 0 };
1349 len = 0;
1351 sockaddr.sin_family = AF_INET;
1352 sockaddr.sin_port = 0;
1353 sockaddr.sin_addr.s_addr = 0;
1355 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1356 GLE = WSAGetLastError();
1357 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1358 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1359 GLE, ret );
1361 len = sizeof(address);
1363 sockaddr.sin_family = AF_INET;
1364 sockaddr.sin_port = 0;
1365 sockaddr.sin_addr.s_addr = 0;
1367 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1368 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1370 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1372 len = sizeof(address);
1374 sockaddr.sin_family = AF_INET;
1375 sockaddr.sin_port = 0;
1376 sockaddr.sin_addr.s_addr = 0xffffffff;
1378 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1379 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1381 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1383 len = sizeof(address);
1385 sockaddr.sin_family = AF_INET;
1386 sockaddr.sin_port = 0xffff;
1387 sockaddr.sin_addr.s_addr = 0;
1389 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1390 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1392 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1394 len = sizeof(address);
1396 sockaddr.sin_family = AF_INET;
1397 sockaddr.sin_port = 0xffff;
1398 sockaddr.sin_addr.s_addr = 0xffffffff;
1400 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1401 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1403 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1406 static void test_WSAStringToAddressA(void)
1408 INT ret, len;
1409 SOCKADDR_IN sockaddr;
1410 SOCKADDR_IN6 sockaddr6;
1411 int GLE;
1413 CHAR address1[] = "0.0.0.0";
1414 CHAR address2[] = "127.127.127.127";
1415 CHAR address3[] = "255.255.255.255";
1416 CHAR address4[] = "127.127.127.127:65535";
1417 CHAR address5[] = "255.255.255.255:65535";
1418 CHAR address6[] = "::1";
1419 CHAR address7[] = "[::1]";
1420 CHAR address8[] = "[::1]:65535";
1422 len = 0;
1423 sockaddr.sin_family = AF_INET;
1425 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1426 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1427 WSAGetLastError() );
1429 len = sizeof(sockaddr);
1430 sockaddr.sin_port = 0;
1431 sockaddr.sin_addr.s_addr = 0;
1433 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1434 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1435 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1437 len = sizeof(sockaddr);
1438 sockaddr.sin_port = 0;
1439 sockaddr.sin_addr.s_addr = 0;
1441 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1442 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1443 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1445 len = sizeof(sockaddr);
1446 sockaddr.sin_port = 0;
1447 sockaddr.sin_addr.s_addr = 0;
1449 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1450 GLE = WSAGetLastError();
1451 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1452 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1453 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1455 len = sizeof(sockaddr);
1456 sockaddr.sin_port = 0;
1457 sockaddr.sin_addr.s_addr = 0;
1459 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1460 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1461 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1463 len = sizeof(sockaddr);
1464 sockaddr.sin_port = 0;
1465 sockaddr.sin_addr.s_addr = 0;
1467 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1468 GLE = WSAGetLastError();
1469 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1470 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1471 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1473 len = sizeof(sockaddr6);
1474 memset(&sockaddr6, 0, len);
1475 sockaddr6.sin6_family = AF_INET6;
1477 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1478 &len );
1479 GLE = WSAGetLastError();
1480 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1481 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1483 len = sizeof(sockaddr6);
1484 memset(&sockaddr6, 0, len);
1485 sockaddr6.sin6_family = AF_INET6;
1487 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1488 &len );
1489 GLE = WSAGetLastError();
1490 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1491 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1493 len = sizeof(sockaddr6);
1494 memset(&sockaddr6, 0, len);
1495 sockaddr6.sin6_family = AF_INET6;
1497 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1498 &len );
1499 GLE = WSAGetLastError();
1500 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1501 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1502 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1506 static void test_WSAStringToAddressW(void)
1508 INT ret, len;
1509 SOCKADDR_IN sockaddr;
1510 SOCKADDR_IN6 sockaddr6;
1511 int GLE;
1513 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1514 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1515 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1516 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1517 ':', '6', '5', '5', '3', '5', 0 };
1518 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1519 '6', '5', '5', '3', '5', 0 };
1520 WCHAR address6[] = {':',':','1','\0'};
1521 WCHAR address7[] = {'[',':',':','1',']','\0'};
1522 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1524 len = 0;
1525 sockaddr.sin_family = AF_INET;
1527 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1528 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1529 WSAGetLastError() );
1531 len = sizeof(sockaddr);
1532 sockaddr.sin_port = 0;
1533 sockaddr.sin_addr.s_addr = 0;
1535 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1536 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1537 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1539 len = sizeof(sockaddr);
1540 sockaddr.sin_port = 0;
1541 sockaddr.sin_addr.s_addr = 0;
1543 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1544 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1545 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1547 len = sizeof(sockaddr);
1548 sockaddr.sin_port = 0;
1549 sockaddr.sin_addr.s_addr = 0;
1551 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1552 GLE = WSAGetLastError();
1553 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1554 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1555 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1557 len = sizeof(sockaddr);
1558 sockaddr.sin_port = 0;
1559 sockaddr.sin_addr.s_addr = 0;
1561 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1562 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1563 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1565 len = sizeof(sockaddr);
1566 sockaddr.sin_port = 0;
1567 sockaddr.sin_addr.s_addr = 0;
1569 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1570 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1571 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1572 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1574 len = sizeof(sockaddr6);
1575 memset(&sockaddr6, 0, len);
1576 sockaddr6.sin6_family = AF_INET6;
1578 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1579 &len );
1580 GLE = WSAGetLastError();
1581 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1582 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1584 len = sizeof(sockaddr6);
1585 memset(&sockaddr6, 0, len);
1586 sockaddr6.sin6_family = AF_INET6;
1588 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1589 &len );
1590 GLE = WSAGetLastError();
1591 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1592 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1594 len = sizeof(sockaddr6);
1595 memset(&sockaddr6, 0, len);
1596 sockaddr6.sin6_family = AF_INET6;
1598 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1599 &len );
1600 GLE = WSAGetLastError();
1601 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1602 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1603 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1607 static VOID WINAPI SelectReadThread(select_thread_params *par)
1609 fd_set readfds;
1610 int ret;
1611 struct sockaddr_in addr;
1612 struct timeval select_timeout;
1614 FD_ZERO(&readfds);
1615 FD_SET(par->s, &readfds);
1616 select_timeout.tv_sec=5;
1617 select_timeout.tv_usec=0;
1618 addr.sin_family = AF_INET;
1619 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1620 addr.sin_port = htons(SERVERPORT);
1622 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1623 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1625 SetEvent(server_ready);
1626 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1627 par->ReadKilled = (ret == 1);
1630 static void test_select(void)
1632 SOCKET fdRead, fdWrite;
1633 fd_set readfds, writefds, exceptfds;
1634 unsigned int maxfd;
1635 int ret;
1636 struct timeval select_timeout;
1637 select_thread_params thread_params;
1638 HANDLE thread_handle;
1639 DWORD id;
1641 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1642 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1643 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1644 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1646 FD_ZERO(&readfds);
1647 FD_ZERO(&writefds);
1648 FD_ZERO(&exceptfds);
1649 FD_SET(fdRead, &readfds);
1650 FD_SET(fdWrite, &writefds);
1651 FD_SET(fdRead, &exceptfds);
1652 FD_SET(fdWrite, &exceptfds);
1653 select_timeout.tv_sec=0;
1654 select_timeout.tv_usec=500;
1656 maxfd = fdRead;
1657 if (fdWrite > maxfd)
1658 maxfd = fdWrite;
1660 todo_wine {
1661 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1662 ok ( (ret == 0), "select should not return any socket handles\n");
1663 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1664 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1667 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1668 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1670 todo_wine {
1671 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1673 ret = closesocket(fdWrite);
1674 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1676 thread_params.s = fdRead;
1677 thread_params.ReadKilled = FALSE;
1678 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1679 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1680 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1682 WaitForSingleObject (server_ready, INFINITE);
1683 Sleep(200);
1684 ret = closesocket(fdRead);
1685 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1687 WaitForSingleObject (thread_handle, 1000);
1688 ok ( (thread_params.ReadKilled), "closesocket did not wakeup select\n");
1692 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1694 struct sockaddr_in address;
1695 int len = sizeof(address);
1696 SOCKET client_socket;
1698 SetEvent(server_ready);
1699 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1700 if (client_socket != INVALID_SOCKET)
1701 closesocket(client_socket);
1702 par->ReadKilled = (client_socket == INVALID_SOCKET);
1703 return 0;
1706 static void test_accept(void)
1708 int ret;
1709 SOCKET server_socket = INVALID_SOCKET;
1710 struct sockaddr_in address;
1711 select_thread_params thread_params;
1712 HANDLE thread_handle = NULL;
1713 DWORD id;
1715 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1716 if (server_socket == INVALID_SOCKET)
1718 trace("error creating server socket: %d\n", WSAGetLastError());
1719 goto done;
1722 memset(&address, 0, sizeof(address));
1723 address.sin_family = AF_INET;
1724 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1725 if (ret != 0)
1727 trace("error binding server socket: %d\n", WSAGetLastError());
1728 goto done;
1731 ret = listen(server_socket, 1);
1732 if (ret != 0)
1734 trace("error making server socket listen: %d\n", WSAGetLastError());
1735 goto done;
1738 server_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
1739 if (server_ready == INVALID_HANDLE_VALUE)
1741 trace("error creating event: %d\n", GetLastError());
1742 goto done;
1745 thread_params.s = server_socket;
1746 thread_params.ReadKilled = FALSE;
1747 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
1748 &thread_params, 0, &id);
1749 if (thread_handle == NULL)
1751 trace("error creating thread: %d\n", GetLastError());
1752 goto done;
1755 WaitForSingleObject(server_ready, INFINITE);
1756 Sleep(200);
1757 ret = closesocket(server_socket);
1758 if (ret != 0)
1760 trace("closesocket failed: %d\n", WSAGetLastError());
1761 goto done;
1764 WaitForSingleObject(thread_handle, 1000);
1765 ok(thread_params.ReadKilled, "closesocket did not wakeup accept\n");
1767 done:
1768 if (thread_handle != NULL)
1769 CloseHandle(thread_handle);
1770 if (server_ready != INVALID_HANDLE_VALUE)
1771 CloseHandle(server_ready);
1772 if (server_socket != INVALID_SOCKET)
1773 closesocket(server_socket);
1776 static void test_extendedSocketOptions(void)
1778 WSADATA wsa;
1779 SOCKET sock;
1780 struct sockaddr_in sa;
1781 int sa_len = sizeof(struct sockaddr_in);
1782 int optval, optlen = sizeof(int), ret;
1783 BOOL bool_opt_val;
1784 LINGER linger_val;
1786 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1787 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1788 return;
1791 memset(&sa, 0, sa_len);
1793 sa.sin_family = AF_INET;
1794 sa.sin_port = htons(0);
1795 sa.sin_addr.s_addr = htonl(INADDR_ANY);
1797 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
1798 trace("Creating the socket failed: %d\n", WSAGetLastError());
1799 WSACleanup();
1800 return;
1803 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1804 trace("Failed to bind socket: %d\n", WSAGetLastError());
1805 closesocket(sock);
1806 WSACleanup();
1807 return;
1810 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
1812 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
1813 ok((optval == 65507) || (optval == 65527),
1814 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
1816 optlen = sizeof(LINGER);
1817 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1818 todo_wine{
1819 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
1822 closesocket(sock);
1824 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
1825 trace("Creating the socket failed: %d\n", WSAGetLastError());
1826 WSACleanup();
1827 return;
1830 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
1831 trace("Failed to bind socket: %d\n", WSAGetLastError());
1832 closesocket(sock);
1833 WSACleanup();
1834 return;
1837 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
1838 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
1840 optlen = sizeof(BOOL);
1841 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
1842 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
1843 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
1844 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
1845 bool_opt_val, linger_val.l_onoff);
1847 closesocket(sock);
1848 WSACleanup();
1851 static void test_getsockname(void)
1853 WSADATA wsa;
1854 SOCKET sock;
1855 struct sockaddr_in sa_set, sa_get;
1856 int sa_set_len = sizeof(struct sockaddr_in);
1857 int sa_get_len = sa_set_len;
1858 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
1860 if(WSAStartup(MAKEWORD(2,0), &wsa)){
1861 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1862 return;
1865 memset(&sa_set, 0, sa_set_len);
1867 sa_set.sin_family = AF_INET;
1868 sa_set.sin_port = htons(0);
1869 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
1871 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
1872 trace("Creating the socket failed: %d\n", WSAGetLastError());
1873 WSACleanup();
1874 return;
1877 memcpy(&sa_get, &sa_set, sizeof(sa_set));
1878 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
1879 ok(0, "getsockname on unbound socket should fail\n");
1880 else {
1881 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
1882 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
1883 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
1884 "failed getsockname modified sockaddr when it shouldn't\n");
1887 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
1888 trace("Failed to bind socket: %d\n", WSAGetLastError());
1889 closesocket(sock);
1890 WSACleanup();
1891 return;
1894 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
1895 trace("Failed to call getsockname: %d\n", WSAGetLastError());
1896 closesocket(sock);
1897 WSACleanup();
1898 return;
1901 ok(memcmp(sa_get.sin_zero, null_padding, 8) == 0,
1902 "getsockname did not zero the sockaddr_in structure\n");
1904 closesocket(sock);
1905 WSACleanup();
1908 static void test_dns(void)
1910 struct hostent *h;
1912 h = gethostbyname("");
1913 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
1916 /* Our winsock headers don't define gethostname because it conflicts with the
1917 * definition in unistd.h. Define it here to get rid of the warning. */
1919 int WINAPI gethostname(char *name, int namelen);
1921 static void test_gethostbyname_hack(void)
1923 struct hostent *he;
1924 char name[256];
1925 static BYTE loopback[] = {127, 0, 0, 1};
1926 static BYTE magic_loopback[] = {127, 12, 34, 56};
1927 int ret;
1929 ret = gethostname(name, 256);
1930 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
1932 he = gethostbyname("localhost");
1933 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
1934 if(he)
1936 if(he->h_length != 4)
1938 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
1939 return;
1942 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
1943 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
1944 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
1945 he->h_addr_list[0][3]);
1948 /* No reason to test further with NULL hostname */
1949 if(name == NULL)
1950 return;
1952 if(strcmp(name, "localhost") == 0)
1954 skip("hostname seems to be \"localhost\", skipping test.\n");
1955 return;
1958 he = NULL;
1959 he = gethostbyname(name);
1960 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
1961 if(he)
1963 if(he->h_length != 4)
1965 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
1966 return;
1969 if (he->h_addr_list[0][0] == 127)
1971 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
1972 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
1973 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
1974 he->h_addr_list[0][2], he->h_addr_list[0][3]);
1978 he = NULL;
1979 he = gethostbyname("nonexistent.winehq.org");
1980 /* Don't check for the return value, as some braindead ISPs will kindly
1981 * resolve nonexistent host names to addresses of the ISP's spam pages. */
1984 static void test_inet_addr(void)
1986 u_long addr;
1988 addr = inet_addr(NULL);
1989 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
1992 static DWORD WINAPI drain_socket_thread(LPVOID arg)
1994 char buffer[1024];
1995 SOCKET sock = *(SOCKET*)arg;
1997 while (recv(sock, buffer, sizeof(buffer), 0) > 0)
1999 return 0;
2002 static void test_send(void)
2004 SOCKET src = INVALID_SOCKET;
2005 SOCKET dst = INVALID_SOCKET;
2006 HANDLE hThread = NULL;
2007 const int buflen = 1024*1024;
2008 char *buffer = NULL;
2009 int ret;
2010 DWORD id;
2012 if (tcp_socketpair(&src, &dst) != 0)
2014 ok(0, "creating socket pair failed, skipping test\n");
2015 return;
2018 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2019 if (hThread == NULL)
2021 ok(0, "CreateThread failed, error %d\n", GetLastError());
2022 goto end;
2025 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
2026 if (buffer == NULL)
2028 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2029 goto end;
2032 ret = send(src, buffer, buflen, 0);
2033 if (ret >= 0)
2034 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
2035 else
2036 ok(0, "send failed, error %d\n", WSAGetLastError());
2038 end:
2039 if (src != INVALID_SOCKET)
2040 closesocket(src);
2041 if (dst != INVALID_SOCKET)
2042 closesocket(dst);
2043 if (hThread != NULL)
2044 CloseHandle(hThread);
2045 HeapFree(GetProcessHeap(), 0, buffer);
2048 static void test_write_events(void)
2050 SOCKET src = INVALID_SOCKET;
2051 SOCKET dst = INVALID_SOCKET;
2052 HANDLE hThread = NULL;
2053 HANDLE hEvent = INVALID_HANDLE_VALUE;
2054 int len;
2055 u_long one = 1;
2056 int ret;
2057 DWORD id;
2059 if (tcp_socketpair(&src, &dst) != 0)
2061 ok(0, "creating socket pair failed, skipping test\n");
2062 return;
2065 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2066 if (hThread == NULL)
2068 ok(0, "CreateThread failed, error %d\n", GetLastError());
2069 goto end;
2072 hEvent = CreateEventW(NULL, FALSE, TRUE, NULL);
2073 if (hEvent == INVALID_HANDLE_VALUE)
2075 ok(0, "CreateEventW failed, error %d\n", GetLastError());
2076 goto end;
2079 ret = ioctlsocket(src, FIONBIO, &one);
2080 if (ret)
2082 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2083 goto end;
2086 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
2087 if (ret)
2089 ok(0, "WSAEventSelect failed, error %d\n", ret);
2090 goto end;
2093 for (len = 100; len > 0; --len)
2095 WSANETWORKEVENTS netEvents;
2096 DWORD dwRet = WaitForSingleObject(hEvent, 5000);
2097 if (dwRet != WAIT_OBJECT_0)
2099 ok(0, "WaitForSingleObject failed, error %d\n", dwRet);
2100 goto end;
2103 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2104 if (ret)
2106 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2107 goto end;
2110 if (netEvents.lNetworkEvents & FD_WRITE)
2112 ret = send(src, "a", 1, 0);
2113 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
2115 ok(0, "send failed, error %d\n", WSAGetLastError());
2116 goto end;
2120 if (netEvents.lNetworkEvents & FD_CLOSE)
2122 ok(0, "unexpected close\n");
2123 goto end;
2127 end:
2128 if (src != INVALID_SOCKET)
2129 closesocket(src);
2130 if (dst != INVALID_SOCKET)
2131 closesocket(dst);
2132 if (hThread != NULL)
2133 CloseHandle(hThread);
2134 CloseHandle(hEvent);
2137 static void test_ipv6only(void)
2139 SOCKET v4 = INVALID_SOCKET,
2140 v6 = INVALID_SOCKET;
2141 struct sockaddr_in sin4;
2142 struct sockaddr_in6 sin6;
2143 int ret;
2145 memset(&sin4, 0, sizeof(sin4));
2146 sin4.sin_family = AF_INET;
2147 sin4.sin_port = htons(SERVERPORT);
2149 memset(&sin6, 0, sizeof(sin6));
2150 sin6.sin6_family = AF_INET6;
2151 sin6.sin6_port = htons(SERVERPORT);
2153 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2154 if (v6 == INVALID_SOCKET) {
2155 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2156 WSAGetLastError(), WSAEAFNOSUPPORT);
2157 goto end;
2159 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
2160 if (ret) {
2161 skip("Could not bind IPv6 address (LastError: %d).\n",
2162 WSAGetLastError());
2163 goto end;
2166 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2167 if (v4 == INVALID_SOCKET) {
2168 skip("Could not create IPv4 socket (LastError: %d).\n",
2169 WSAGetLastError());
2170 goto end;
2172 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
2173 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2174 WSAGetLastError(), WSAEADDRINUSE);
2176 end:
2177 if (v4 != INVALID_SOCKET)
2178 closesocket(v4);
2179 if (v6 != INVALID_SOCKET)
2180 closesocket(v6);
2183 static void test_WSASendTo(void)
2185 SOCKET s;
2186 struct sockaddr_in addr;
2187 char buf[12] = "hello world";
2188 WSABUF data_buf;
2189 DWORD bytesSent;
2191 addr.sin_family = AF_INET;
2192 addr.sin_port = htons(139);
2193 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2194 data_buf.len = sizeof(buf);
2195 data_buf.buf = buf;
2197 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
2198 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2199 return;
2202 WSASetLastError(12345);
2203 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
2204 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2205 return;
2207 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2208 "a successful call to WSASendTo()\n");
2211 /**************** Main program ***************/
2213 START_TEST( sock )
2215 int i;
2216 Init();
2218 test_set_getsockopt();
2219 test_so_reuseaddr();
2220 test_extendedSocketOptions();
2222 for (i = 0; i < NUM_TESTS; i++)
2224 trace ( " **** STARTING TEST %d ****\n", i );
2225 do_test ( &tests[i] );
2226 trace ( " **** TEST %d COMPLETE ****\n", i );
2229 test_UDP();
2231 test_getservbyname();
2232 test_WSASocket();
2234 test_WSAAddressToStringA();
2235 test_WSAAddressToStringW();
2237 test_WSAStringToAddressA();
2238 test_WSAStringToAddressW();
2240 test_select();
2241 test_accept();
2242 test_getsockname();
2243 test_inet_addr();
2244 test_dns();
2245 test_gethostbyname_hack();
2247 test_send();
2248 test_write_events();
2250 test_WSASendTo();
2252 test_ipv6only();
2254 Exit();