quartz: Don't cast WSTR to BSTR, convert properly instead.
[wine/testsucceed.git] / dlls / ws2_32 / tests / sock.c
blob5b519b88d23c07388e8ee232889fa3b83ef3954c
1 /*
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
23 #include <stdarg.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winsock2.h>
30 #include <ws2tcpip.h>
31 #include <mswsock.h>
32 #include "wine/test.h"
33 #include <winnt.h>
34 #include <winerror.h>
36 #define MAX_CLIENTS 4 /* Max number of clients */
37 #define NUM_TESTS 4 /* Number of tests performed */
38 #define FIRST_CHAR 'A' /* First character in transferred pattern */
39 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
40 #define BIND_TRIES 6 /* Number of bind() attempts */
41 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
42 after server initialization, if something hangs */
44 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
46 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
47 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
49 #define SERVERIP "127.0.0.1" /* IP to bind to */
50 #define SERVERPORT 9374 /* Port number to bind to */
52 #define wsa_ok(op, cond, msg) \
53 do { \
54 int tmp, err = 0; \
55 tmp = op; \
56 if ( !(cond tmp) ) err = WSAGetLastError(); \
57 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
58 } while (0);
60 /* Function pointers */
61 static void (WINAPI *pFreeAddrInfoW)(PADDRINFOW) = 0;
62 static int (WINAPI *pGetAddrInfoW)(LPCWSTR,LPCWSTR,const ADDRINFOW *,PADDRINFOW *) = 0;
63 static PCSTR (WINAPI *pInetNtop)(INT,LPVOID,LPSTR,ULONG) = 0;
65 /**************** Structs and typedefs ***************/
67 typedef struct thread_info
69 HANDLE thread;
70 DWORD id;
71 } thread_info;
73 /* Information in the server about open client connections */
74 typedef struct sock_info
76 SOCKET s;
77 struct sockaddr_in addr;
78 struct sockaddr_in peer;
79 char *buf;
80 int n_recvd;
81 int n_sent;
82 } sock_info;
84 /* Test parameters for both server & client */
85 typedef struct test_params
87 int sock_type;
88 int sock_prot;
89 const char *inet_addr;
90 short inet_port;
91 int chunk_size;
92 int n_chunks;
93 int n_clients;
94 } test_params;
96 /* server-specific test parameters */
97 typedef struct server_params
99 test_params *general;
100 DWORD sock_flags;
101 int buflen;
102 } server_params;
104 /* client-specific test parameters */
105 typedef struct client_params
107 test_params *general;
108 DWORD sock_flags;
109 int buflen;
110 } client_params;
112 /* This type combines all information for setting up a test scenario */
113 typedef struct test_setup
115 test_params general;
116 LPVOID srv;
117 server_params srv_params;
118 LPVOID clt;
119 client_params clt_params;
120 } test_setup;
122 /* Thread local storage for server */
123 typedef struct server_memory
125 SOCKET s;
126 struct sockaddr_in addr;
127 sock_info sock[MAX_CLIENTS];
128 } server_memory;
130 /* Thread local storage for client */
131 typedef struct client_memory
133 SOCKET s;
134 struct sockaddr_in addr;
135 char *send_buf;
136 char *recv_buf;
137 } client_memory;
139 /* SelectReadThread thread parameters */
140 typedef struct select_thread_params
142 SOCKET s;
143 BOOL ReadKilled;
144 } select_thread_params;
146 /**************** Static variables ***************/
148 static DWORD tls; /* Thread local storage index */
149 static HANDLE thread[1+MAX_CLIENTS];
150 static DWORD thread_id[1+MAX_CLIENTS];
151 static HANDLE server_ready;
152 static HANDLE client_ready[MAX_CLIENTS];
153 static int client_id;
155 /**************** General utility functions ***************/
157 static int tcp_socketpair(SOCKET *src, SOCKET *dst)
159 SOCKET server = INVALID_SOCKET;
160 struct sockaddr_in addr;
161 int len;
162 int ret;
164 *src = INVALID_SOCKET;
165 *dst = INVALID_SOCKET;
167 *src = socket(AF_INET, SOCK_STREAM, 0);
168 if (*src == INVALID_SOCKET)
169 goto end;
171 server = socket(AF_INET, SOCK_STREAM, 0);
172 if (server == INVALID_SOCKET)
173 goto end;
175 memset(&addr, 0, sizeof(addr));
176 addr.sin_family = AF_INET;
177 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
178 ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
179 if (ret != 0)
180 goto end;
182 len = sizeof(addr);
183 ret = getsockname(server, (struct sockaddr*)&addr, &len);
184 if (ret != 0)
185 goto end;
187 ret = listen(server, 1);
188 if (ret != 0)
189 goto end;
191 ret = connect(*src, (struct sockaddr*)&addr, sizeof(addr));
192 if (ret != 0)
193 goto end;
195 len = sizeof(addr);
196 *dst = accept(server, (struct sockaddr*)&addr, &len);
198 end:
199 if (server != INVALID_SOCKET)
200 closesocket(server);
201 if (*src != INVALID_SOCKET && *dst != INVALID_SOCKET)
202 return 0;
203 closesocket(*src);
204 closesocket(*dst);
205 return -1;
208 static void set_so_opentype ( BOOL overlapped )
210 int optval = !overlapped, newval, len = sizeof (int);
212 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
213 (LPVOID) &optval, sizeof (optval) ) == 0,
214 "setting SO_OPENTYPE failed\n" );
215 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
216 (LPVOID) &newval, &len ) == 0,
217 "getting SO_OPENTYPE failed\n" );
218 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
221 static int set_blocking ( SOCKET s, BOOL blocking )
223 u_long val = !blocking;
224 return ioctlsocket ( s, FIONBIO, &val );
227 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
229 char c, *p;
230 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
231 memset ( p, c, chunk_size );
234 static int test_buffer ( char *buf, int chunk_size, int n_chunks )
236 char c, *p;
237 int i;
238 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
240 for ( i = 0; i < chunk_size; i++ )
241 if ( p[i] != c ) return i;
243 return -1;
247 * This routine is called when a client / server does not expect any more data,
248 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
250 static void read_zero_bytes ( SOCKET s )
252 char buf[256];
253 int tmp, n = 0;
254 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
255 n += tmp;
256 ok ( n <= 0, "garbage data received: %d bytes\n", n );
259 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
261 char* last = buf + buflen, *p;
262 int n = 1;
263 for ( p = buf; n > 0 && p < last; p += n )
264 n = send ( s, p, min ( sendlen, last - p ), 0 );
265 wsa_ok ( n, 0 <=, "do_synchronous_send (%x): error %d\n" );
266 return p - buf;
269 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
271 char* last = buf + buflen, *p;
272 int n = 1;
273 for ( p = buf; n > 0 && p < last; p += n )
274 n = recv ( s, p, min ( recvlen, last - p ), 0 );
275 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
276 return p - buf;
279 static int do_synchronous_recvfrom ( SOCKET s, char *buf, int buflen,int flags,struct sockaddr *from, int *fromlen, int recvlen )
281 char* last = buf + buflen, *p;
282 int n = 1;
283 for ( p = buf; n > 0 && p < last; p += n )
284 n = recvfrom ( s, p, min ( recvlen, last - p ), 0, from, fromlen );
285 wsa_ok ( n, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
286 return p - buf;
290 * Call this routine right after thread startup.
291 * SO_OPENTYPE must by 0, regardless what the server did.
293 static void check_so_opentype (void)
295 int tmp = 1, len;
296 len = sizeof (tmp);
297 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
298 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
301 /**************** Server utility functions ***************/
304 * Even if we have closed our server socket cleanly,
305 * the OS may mark the address "in use" for some time -
306 * this happens with native Linux apps, too.
308 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
310 int err, wsaerr = 0, n_try = BIND_TRIES;
312 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
313 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
314 n_try-- >= 0)
316 trace ( "address in use, waiting ...\n" );
317 Sleep ( 1000 * BIND_SLEEP );
319 ok ( err == 0, "failed to bind: %d\n", wsaerr );
322 static void server_start ( server_params *par )
324 int i;
325 test_params *gen = par->general;
326 server_memory *mem = LocalAlloc ( LPTR, sizeof ( server_memory ) );
328 TlsSetValue ( tls, mem );
329 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
330 NULL, 0, par->sock_flags );
331 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
333 mem->addr.sin_family = AF_INET;
334 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
335 mem->addr.sin_port = htons ( gen->inet_port );
337 for (i = 0; i < MAX_CLIENTS; i++)
339 mem->sock[i].s = INVALID_SOCKET;
340 mem->sock[i].buf = LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
341 mem->sock[i].n_recvd = 0;
342 mem->sock[i].n_sent = 0;
345 if ( gen->sock_type == SOCK_STREAM )
346 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
349 static void server_stop (void)
351 int i;
352 server_memory *mem = TlsGetValue ( tls );
354 for (i = 0; i < MAX_CLIENTS; i++ )
356 LocalFree ( mem->sock[i].buf );
357 if ( mem->sock[i].s != INVALID_SOCKET )
358 closesocket ( mem->sock[i].s );
360 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
361 LocalFree ( mem );
362 ExitThread ( GetCurrentThreadId () );
365 /**************** Client utilitiy functions ***************/
367 static void client_start ( client_params *par )
369 test_params *gen = par->general;
370 client_memory *mem = LocalAlloc (LPTR, sizeof (client_memory));
372 TlsSetValue ( tls, mem );
374 WaitForSingleObject ( server_ready, INFINITE );
376 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
377 NULL, 0, par->sock_flags );
379 mem->addr.sin_family = AF_INET;
380 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
381 mem->addr.sin_port = htons ( gen->inet_port );
383 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
385 mem->send_buf = LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
386 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
387 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
389 SetEvent ( client_ready[client_id] );
390 /* Wait for the other clients to come up */
391 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
394 static void client_stop (void)
396 client_memory *mem = TlsGetValue ( tls );
397 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%x): %d\n" );
398 LocalFree ( mem->send_buf );
399 LocalFree ( mem );
400 ExitThread(0);
403 /**************** Servers ***************/
406 * simple_server: A very basic server doing synchronous IO.
408 static VOID WINAPI simple_server ( server_params *par )
410 test_params *gen = par->general;
411 server_memory *mem;
412 int pos, n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
413 id = GetCurrentThreadId();
415 trace ( "simple_server (%x) starting\n", id );
417 set_so_opentype ( FALSE ); /* non-overlapped */
418 server_start ( par );
419 mem = TlsGetValue ( tls );
421 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
422 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%x): listen failed: %d\n");
424 trace ( "simple_server (%x) ready\n", id );
425 SetEvent ( server_ready ); /* notify clients */
427 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
429 trace ( "simple_server (%x): waiting for client\n", id );
431 /* accept a single connection */
432 tmp = sizeof ( mem->sock[0].peer );
433 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
434 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%x): accept failed: %d\n" );
436 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
437 "simple_server (%x): strange peer address\n", id );
439 /* Receive data & check it */
440 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
441 ok ( n_recvd == n_expected,
442 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
443 pos = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
444 ok ( pos == -1, "simple_server (%x): test pattern error: %d\n", id, pos);
446 /* Echo data back */
447 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
448 ok ( n_sent == n_expected,
449 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
451 /* cleanup */
452 read_zero_bytes ( mem->sock[0].s );
453 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%x): closesocket error: %d\n" );
454 mem->sock[0].s = INVALID_SOCKET;
457 trace ( "simple_server (%x) exiting\n", id );
458 server_stop ();
462 * select_server: A non-blocking server.
464 static VOID WINAPI select_server ( server_params *par )
466 test_params *gen = par->general;
467 server_memory *mem;
468 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
469 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
470 n_set, delta, n_ready;
471 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
472 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
474 trace ( "select_server (%x) starting\n", id );
476 set_so_opentype ( FALSE ); /* non-overlapped */
477 server_start ( par );
478 mem = TlsGetValue ( tls );
480 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
481 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%x): listen failed: %d\n");
483 trace ( "select_server (%x) ready\n", id );
484 SetEvent ( server_ready ); /* notify clients */
486 FD_ZERO ( &fds_openrecv );
487 FD_ZERO ( &fds_recv );
488 FD_ZERO ( &fds_send );
489 FD_ZERO ( &fds_opensend );
491 FD_SET ( mem->s, &fds_openrecv );
493 while(1)
495 fds_recv = fds_openrecv;
496 fds_send = fds_opensend;
498 n_set = 0;
500 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
501 "select_server (%x): select() failed: %d\n" );
503 /* check for incoming requests */
504 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
505 n_set += 1;
507 trace ( "select_server (%x): accepting client connection\n", id );
509 /* accept a single connection */
510 tmp = sizeof ( mem->sock[n_connections].peer );
511 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
512 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%x): accept() failed: %d\n" );
514 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
515 "select_server (%x): strange peer address\n", id );
517 /* add to list of open connections */
518 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
519 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
521 n_connections++;
524 /* handle open requests */
526 for ( i = 0; i < n_connections; i++ )
528 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
529 n_set += 1;
531 if ( mem->sock[i].n_recvd < n_expected ) {
532 /* Receive data & check it */
533 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 );
534 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
535 mem->sock[i].n_recvd += n_recvd;
537 if ( mem->sock[i].n_recvd == n_expected ) {
538 int pos = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
539 ok ( pos == -1, "select_server (%x): test pattern error: %d\n", id, pos );
540 FD_CLR ( mem->sock[i].s, &fds_openrecv );
543 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
547 /* only echo back what we've received */
548 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
550 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
551 n_set += 1;
553 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
554 /* Echo data back */
555 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
556 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
557 mem->sock[i].n_sent += n_sent;
559 if ( mem->sock[i].n_sent == n_expected ) {
560 FD_CLR ( mem->sock[i].s, &fds_opensend );
563 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
568 /* check that select returned the correct number of ready sockets */
569 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
571 /* check if all clients are done */
572 if ( ( fds_opensend.fd_count == 0 )
573 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
574 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
575 break;
579 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
581 /* cleanup */
582 read_zero_bytes ( mem->sock[i].s );
583 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%x): closesocket error: %d\n" );
584 mem->sock[i].s = INVALID_SOCKET;
587 trace ( "select_server (%x) exiting\n", id );
588 server_stop ();
591 /**************** Clients ***************/
594 * simple_client: A very basic client doing synchronous IO.
596 static VOID WINAPI simple_client ( client_params *par )
598 test_params *gen = par->general;
599 client_memory *mem;
600 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
602 id = GetCurrentThreadId();
603 trace ( "simple_client (%x): starting\n", id );
604 /* wait here because we want to call set_so_opentype before creating a socket */
605 WaitForSingleObject ( server_ready, INFINITE );
606 trace ( "simple_client (%x): server ready\n", id );
608 check_so_opentype ();
609 set_so_opentype ( FALSE ); /* non-overlapped */
610 client_start ( par );
611 mem = TlsGetValue ( tls );
613 /* Connect */
614 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
615 0 ==, "simple_client (%x): connect error: %d\n" );
616 ok ( set_blocking ( mem->s, TRUE ) == 0,
617 "simple_client (%x): failed to set blocking mode\n", id );
618 trace ( "simple_client (%x) connected\n", id );
620 /* send data to server */
621 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
622 ok ( n_sent == n_expected,
623 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
625 /* shutdown send direction */
626 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
628 /* Receive data echoed back & check it */
629 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
630 ok ( n_recvd == n_expected,
631 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
633 /* check data */
634 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
635 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
637 /* cleanup */
638 read_zero_bytes ( mem->s );
639 trace ( "simple_client (%x) exiting\n", id );
640 client_stop ();
644 * simple_mixed_client: mixing send and recvfrom
646 static VOID WINAPI simple_mixed_client ( client_params *par )
648 test_params *gen = par->general;
649 client_memory *mem;
650 int pos, n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
651 int fromLen = sizeof(mem->addr);
652 struct sockaddr test;
654 id = GetCurrentThreadId();
655 trace ( "simple_client (%x): starting\n", id );
656 /* wait here because we want to call set_so_opentype before creating a socket */
657 WaitForSingleObject ( server_ready, INFINITE );
658 trace ( "simple_client (%x): server ready\n", id );
660 check_so_opentype ();
661 set_so_opentype ( FALSE ); /* non-overlapped */
662 client_start ( par );
663 mem = TlsGetValue ( tls );
665 /* Connect */
666 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
667 0 ==, "simple_client (%x): connect error: %d\n" );
668 ok ( set_blocking ( mem->s, TRUE ) == 0,
669 "simple_client (%x): failed to set blocking mode\n", id );
670 trace ( "simple_client (%x) connected\n", id );
672 /* send data to server */
673 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
674 ok ( n_sent == n_expected,
675 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
677 /* shutdown send direction */
678 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
680 /* this shouldn't change, since lpFrom, is not updated on
681 connection oriented sockets - exposed by bug 11640
683 ((struct sockaddr_in*)&test)->sin_addr.s_addr = inet_addr("0.0.0.0");
685 /* Receive data echoed back & check it */
686 n_recvd = do_synchronous_recvfrom ( mem->s,
687 mem->recv_buf,
688 n_expected,
690 (struct sockaddr *)&test,
691 &fromLen,
692 par->buflen );
693 ok ( n_recvd == n_expected,
694 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
696 /* check that lpFrom was not updated */
697 ok(0 ==
698 strcmp(
699 inet_ntoa(((struct sockaddr_in*)&test)->sin_addr),
700 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
702 /* check data */
703 pos = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
704 ok ( pos == -1, "simple_client (%x): test pattern error: %d\n", id, pos);
706 /* cleanup */
707 read_zero_bytes ( mem->s );
708 trace ( "simple_client (%x) exiting\n", id );
709 client_stop ();
713 * event_client: An event-driven client
715 static void WINAPI event_client ( client_params *par )
717 test_params *gen = par->general;
718 client_memory *mem;
719 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
720 tmp, err, n;
721 HANDLE event;
722 WSANETWORKEVENTS wsa_events;
723 char *send_last, *recv_last, *send_p, *recv_p;
724 LONG mask = FD_READ | FD_WRITE | FD_CLOSE;
726 trace ( "event_client (%x): starting\n", id );
727 client_start ( par );
728 trace ( "event_client (%x): server ready\n", id );
730 mem = TlsGetValue ( tls );
732 /* Prepare event notification for connect, makes socket nonblocking */
733 event = WSACreateEvent ();
734 WSAEventSelect ( mem->s, event, FD_CONNECT );
735 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
736 if ( tmp != 0 ) {
737 err = WSAGetLastError ();
738 ok ( err == WSAEWOULDBLOCK, "event_client (%x): connect error: %d\n", id, err );
739 tmp = WaitForSingleObject ( event, INFINITE );
740 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
741 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
742 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
743 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
744 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
745 if ( err ) goto out;
748 trace ( "event_client (%x) connected\n", id );
750 WSAEventSelect ( mem->s, event, mask );
752 recv_p = mem->recv_buf;
753 recv_last = mem->recv_buf + n_expected;
754 send_p = mem->send_buf;
755 send_last = mem->send_buf + n_expected;
757 while ( TRUE )
759 err = WaitForSingleObject ( event, INFINITE );
760 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
762 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
763 wsa_ok ( err, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
765 if ( wsa_events.lNetworkEvents & FD_WRITE )
767 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
768 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
770 if ( err== 0 )
773 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
774 if ( n < 0 )
776 err = WSAGetLastError ();
777 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
779 else
780 send_p += n;
782 while ( n >= 0 && send_p < send_last );
784 if ( send_p == send_last )
786 trace ( "event_client (%x): all data sent - shutdown\n", id );
787 shutdown ( mem->s, SD_SEND );
788 mask &= ~FD_WRITE;
789 WSAEventSelect ( mem->s, event, mask );
792 if ( wsa_events.lNetworkEvents & FD_READ )
794 err = wsa_events.iErrorCode[ FD_READ_BIT ];
795 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
796 if ( err != 0 ) break;
798 /* First read must succeed */
799 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
800 wsa_ok ( n, 0 <=, "event_client (%x): recv error: %d\n" );
802 while ( n >= 0 ) {
803 recv_p += n;
804 if ( recv_p == recv_last )
806 mask &= ~FD_READ;
807 trace ( "event_client (%x): all data received\n", id );
808 WSAEventSelect ( mem->s, event, mask );
809 break;
811 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
812 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
813 ok ( 0, "event_client (%x): read error: %d\n", id, err );
817 if ( wsa_events.lNetworkEvents & FD_CLOSE )
819 trace ( "event_client (%x): close event\n", id );
820 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
821 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
822 break;
826 n = send_p - mem->send_buf;
827 ok ( send_p == send_last,
828 "simple_client (%x): sent less data than expected: %d of %d\n", id, n, n_expected );
829 n = recv_p - mem->recv_buf;
830 ok ( recv_p == recv_last,
831 "simple_client (%x): received less data than expected: %d of %d\n", id, n, n_expected );
832 n = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
833 ok ( n == -1, "event_client (%x): test pattern error: %d\n", id, n);
835 out:
836 WSACloseEvent ( event );
837 trace ( "event_client (%x) exiting\n", id );
838 client_stop ();
841 /* Tests for WSAStartup */
843 /* This should fail. WSAStartup should be called before any network function is used. */
844 static void test_WithoutWSAStartup(void)
846 LPVOID ptr;
848 WSASetLastError(0xdeadbeef);
849 ptr = gethostbyname("localhost");
851 todo_wine ok(ptr == NULL, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
852 todo_wine ok(WSAGetLastError() == WSANOTINITIALISED, "gethostbyname() failed with unexpected error: %d\n",
853 WSAGetLastError());
856 static void test_WithWSAStartup(void)
858 WSADATA data;
859 WORD version = MAKEWORD( 2, 2 );
860 INT res;
861 LPVOID ptr;
863 res = WSAStartup( version, &data );
864 ok(res == 0, "WSAStartup() failed unexpectedly: %d\n", res);
866 ptr = gethostbyname("localhost");
867 ok(ptr != NULL, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
869 WSACleanup();
872 /**************** Main program utility functions ***************/
874 static void Init (void)
876 WORD ver = MAKEWORD (2, 2);
877 WSADATA data;
878 HMODULE hws2_32 = GetModuleHandle("ws2_32.dll");
880 pFreeAddrInfoW = (void *)GetProcAddress(hws2_32, "FreeAddrInfoW");
881 pGetAddrInfoW = (void *)GetProcAddress(hws2_32, "GetAddrInfoW");
882 pInetNtop = (void *)GetProcAddress(hws2_32, "inet_ntop");
884 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
885 tls = TlsAlloc();
888 static void Exit (void)
890 INT ret, err;
891 TlsFree ( tls );
892 ret = WSACleanup();
893 err = WSAGetLastError();
894 ok ( ret == 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret, err);
895 ret = WSACleanup();
896 err = WSAGetLastError();
897 ok ( (ret == SOCKET_ERROR && err == WSANOTINITIALISED) ||
898 broken(ret == 0), /* WinME */
899 "WSACleanup returned %d GetLastError is %d\n", ret, err);
902 static void StartServer (LPTHREAD_START_ROUTINE routine,
903 test_params *general, server_params *par)
905 par->general = general;
906 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
907 ok ( thread[0] != NULL, "Failed to create server thread\n" );
910 static void StartClients (LPTHREAD_START_ROUTINE routine,
911 test_params *general, client_params *par)
913 int i;
914 par->general = general;
915 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
917 client_id = i - 1;
918 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
919 ok ( thread[i] != NULL, "Failed to create client thread\n" );
920 /* Make sure the client is up and running */
921 WaitForSingleObject ( client_ready[client_id], INFINITE );
925 static void do_test( test_setup *test )
927 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
928 DWORD wait;
930 server_ready = CreateEventA ( NULL, TRUE, FALSE, NULL );
931 for (i = 0; i <= n; i++)
932 client_ready[i] = CreateEventA ( NULL, TRUE, FALSE, NULL );
934 StartServer ( test->srv, &test->general, &test->srv_params );
935 StartClients ( test->clt, &test->general, &test->clt_params );
936 WaitForSingleObject ( server_ready, INFINITE );
938 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
939 ok ( wait <= WAIT_OBJECT_0 + n ,
940 "some threads have not completed: %x\n", wait );
942 if ( ! ( wait <= WAIT_OBJECT_0 + n ) )
944 for (i = 0; i <= n; i++)
946 trace ("terminating thread %08x\n", thread_id[i]);
947 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
948 TerminateThread ( thread [i], 0 );
951 CloseHandle ( server_ready );
952 for (i = 0; i <= n; i++)
953 CloseHandle ( client_ready[i] );
956 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
957 /* optname = SO_LINGER */
958 LINGER linger_testvals[] = {
959 {0,0},
960 {0,73},
961 {1,0},
962 {5,189}
965 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
966 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
967 bug in the linux kernel (fixed in 2.6.8) */
968 #define SOCKTIMEOUT2 997000 /* 997 seconds */
970 static void test_set_getsockopt(void)
972 SOCKET s;
973 int i, err, lasterr;
974 int timeout;
975 LINGER lingval;
976 int size;
978 s = socket(AF_INET, SOCK_STREAM, 0);
979 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
980 if( s == INVALID_SOCKET) return;
981 /* SO_RCVTIMEO */
982 timeout = SOCKTIMEOUT1;
983 size = sizeof(timeout);
984 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
985 if( !err)
986 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
987 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
988 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
990 timeout = 0;
991 size = sizeof(timeout);
992 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
993 if( !err)
994 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
995 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
996 ok( timeout == 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
998 /* SO_SNDTIMEO */
999 timeout = SOCKTIMEOUT2; /* 997 seconds. See remark above */
1000 size = sizeof(timeout);
1001 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
1002 if( !err)
1003 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
1004 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1005 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
1006 /* SO_LINGER */
1007 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
1008 size = sizeof(lingval);
1009 lingval = linger_testvals[i];
1010 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
1011 if( !err)
1012 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
1013 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1014 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
1015 (lingval.l_linger == linger_testvals[i].l_linger ||
1016 (!lingval.l_linger && !linger_testvals[i].l_onoff))
1017 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i,
1018 lingval.l_onoff, lingval.l_linger,
1019 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
1021 /* Test for erroneously passing a value instead of a pointer as optval */
1022 size = sizeof(char);
1023 err = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)1, size);
1024 ok(err == SOCKET_ERROR, "setsockopt with optval being a value passed "
1025 "instead of failing.\n");
1026 lasterr = WSAGetLastError();
1027 ok(lasterr == WSAEFAULT, "setsockopt with optval being a value "
1028 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1029 lasterr, WSAEFAULT);
1031 /* SO_RCVTIMEO with invalid values for level */
1032 size = sizeof(timeout);
1033 timeout = SOCKTIMEOUT1;
1034 SetLastError(0xdeadbeef);
1035 err = setsockopt(s, 0xffffffff, SO_RCVTIMEO, (char *) &timeout, size);
1036 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1037 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1038 err, WSAGetLastError());
1040 timeout = SOCKTIMEOUT1;
1041 SetLastError(0xdeadbeef);
1042 err = setsockopt(s, 0x00008000, SO_RCVTIMEO, (char *) &timeout, size);
1043 ok( (err == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
1044 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1045 err, WSAGetLastError());
1047 closesocket(s);
1050 static void test_so_reuseaddr(void)
1052 struct sockaddr_in saddr;
1053 SOCKET s1,s2;
1054 unsigned int rc,reuse;
1055 int size;
1057 saddr.sin_family = AF_INET;
1058 saddr.sin_port = htons(9375);
1059 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
1061 s1=socket(AF_INET, SOCK_STREAM, 0);
1062 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1063 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
1064 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
1066 s2=socket(AF_INET, SOCK_STREAM, 0);
1067 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1069 reuse=0x1234;
1070 size=sizeof(reuse);
1071 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1072 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1074 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1075 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1077 reuse = 1;
1078 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1079 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1081 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1082 * a port immediately after closing another socket on that port, so
1083 * basically following the BSD socket semantics here. */
1084 closesocket(s1);
1085 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1086 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1088 closesocket(s2);
1091 /************* Array containing the tests to run **********/
1093 #define STD_STREAM_SOCKET \
1094 SOCK_STREAM, \
1095 0, \
1096 SERVERIP, \
1097 SERVERPORT
1099 static test_setup tests [NUM_TESTS] =
1101 /* Test 0: synchronous client and server */
1104 STD_STREAM_SOCKET,
1105 2048,
1109 simple_server,
1111 NULL,
1115 simple_client,
1117 NULL,
1122 /* Test 1: event-driven client, synchronous server */
1125 STD_STREAM_SOCKET,
1126 2048,
1130 simple_server,
1132 NULL,
1136 event_client,
1138 NULL,
1139 WSA_FLAG_OVERLAPPED,
1143 /* Test 2: synchronous client, non-blocking server via select() */
1146 STD_STREAM_SOCKET,
1147 2048,
1151 select_server,
1153 NULL,
1157 simple_client,
1159 NULL,
1164 /* Test 3: synchronous mixed client and server */
1167 STD_STREAM_SOCKET,
1168 2048,
1172 simple_server,
1174 NULL,
1178 simple_mixed_client,
1180 NULL,
1187 static void test_UDP(void)
1189 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1190 possible that this test fails due to dropped packets. */
1192 /* peer 0 receives data from all other peers */
1193 struct sock_info peer[NUM_UDP_PEERS];
1194 char buf[16];
1195 int ss, i, n_recv, n_sent;
1197 memset (buf,0,sizeof(buf));
1198 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
1199 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
1201 peer[i].addr.sin_family = AF_INET;
1202 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
1204 if ( i == 0 ) {
1205 peer[i].addr.sin_port = htons ( SERVERPORT );
1206 } else {
1207 peer[i].addr.sin_port = htons ( 0 );
1210 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
1212 /* test getsockname() to get peer's port */
1213 ss = sizeof ( peer[i].addr );
1214 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
1215 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
1218 /* test getsockname() */
1219 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
1221 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1222 /* send client's ip */
1223 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
1224 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
1225 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
1228 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
1229 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
1230 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
1231 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
1235 static void WINAPI do_getservbyname( HANDLE *starttest )
1237 struct {
1238 const char *name;
1239 const char *proto;
1240 int port;
1241 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1243 int i, j;
1244 struct servent *pserv[2];
1246 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1248 /* ensure that necessary buffer resizes are completed */
1249 for ( j = 0; j < 2; j++) {
1250 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1253 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1254 for ( j = 0; j < 2; j++ ) {
1255 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1256 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1257 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) );
1258 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 );
1259 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 );
1262 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1266 static void test_getservbyname(void)
1268 int i;
1269 HANDLE starttest, thread[NUM_THREADS];
1270 DWORD thread_id[NUM_THREADS];
1272 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1274 /* create threads */
1275 for ( i = 0; i < NUM_THREADS; i++ ) {
1276 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1279 /* signal threads to start */
1280 SetEvent ( starttest );
1282 for ( i = 0; i < NUM_THREADS; i++) {
1283 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1287 static void test_WSASocket(void)
1289 SOCKET sock = INVALID_SOCKET;
1290 WSAPROTOCOL_INFOA *pi;
1291 int providers[] = {6, 0};
1292 int ret, err;
1293 UINT pi_size;
1295 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1296 * to avoid a crash on win98.
1298 pi_size = 0;
1299 ret = WSAEnumProtocolsA(providers, NULL, &pi_size);
1300 ok(ret == SOCKET_ERROR, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1301 ret);
1302 err = WSAGetLastError();
1303 ok(err == WSAENOBUFS, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1304 err, WSAENOBUFS);
1306 pi = HeapAlloc(GetProcessHeap(), 0, pi_size);
1307 ok(pi != NULL, "Failed to allocate memory\n");
1308 if (pi == NULL) {
1309 skip("Can't continue without memory.\n");
1310 return;
1313 ret = WSAEnumProtocolsA(providers, pi, &pi_size);
1314 ok(ret != SOCKET_ERROR, "WSAEnumProtocolsA failed, last error is %d\n",
1315 WSAGetLastError());
1317 if (ret == 0) {
1318 skip("No protocols enumerated.\n");
1319 HeapFree(GetProcessHeap(), 0, pi);
1320 return;
1323 sock = WSASocketA(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
1324 FROM_PROTOCOL_INFO, &pi[0], 0, 0);
1325 ok(sock != INVALID_SOCKET, "Failed to create socket: %d\n",
1326 WSAGetLastError());
1328 closesocket(sock);
1329 HeapFree(GetProcessHeap(), 0, pi);
1332 static void test_WSAAddressToStringA(void)
1334 SOCKET v6 = INVALID_SOCKET;
1335 INT ret;
1336 DWORD len;
1337 int GLE;
1338 SOCKADDR_IN sockaddr;
1339 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1341 CHAR expect1[] = "0.0.0.0";
1342 CHAR expect2[] = "255.255.255.255";
1343 CHAR expect3[] = "0.0.0.0:65535";
1344 CHAR expect4[] = "255.255.255.255:65535";
1346 SOCKADDR_IN6 sockaddr6;
1347 CHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1349 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1350 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1351 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1353 CHAR expect6_1[] = "::1";
1354 CHAR expect6_2[] = "20ab::1";
1355 CHAR expect6_3[] = "[20ab::2001]:33274";
1356 CHAR expect6_3_2[] = "[20ab::2001%4660]:33274";
1357 CHAR expect6_3_3[] = "20ab::2001%4660";
1359 len = 0;
1361 sockaddr.sin_family = AF_INET;
1362 sockaddr.sin_port = 0;
1363 sockaddr.sin_addr.s_addr = 0;
1365 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1366 GLE = WSAGetLastError();
1367 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1368 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1369 GLE, ret );
1371 len = sizeof(address);
1373 sockaddr.sin_family = AF_INET;
1374 sockaddr.sin_port = 0;
1375 sockaddr.sin_addr.s_addr = 0;
1377 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1378 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1380 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1381 ok( len == sizeof( expect1 ), "Got size %d\n", len);
1383 len = sizeof(address);
1385 sockaddr.sin_family = AF_INET;
1386 sockaddr.sin_port = 0;
1387 sockaddr.sin_addr.s_addr = 0xffffffff;
1389 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1390 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1392 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1394 len = sizeof(address);
1396 sockaddr.sin_family = AF_INET;
1397 sockaddr.sin_port = 0xffff;
1398 sockaddr.sin_addr.s_addr = 0;
1400 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1401 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1403 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1405 len = sizeof(address);
1407 sockaddr.sin_family = AF_INET;
1408 sockaddr.sin_port = 0xffff;
1409 sockaddr.sin_addr.s_addr = 0xffffffff;
1411 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1412 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1414 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1415 ok( len == sizeof( expect4 ), "Got size %d\n", len);
1417 /*check to see it IPv6 is available */
1418 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1419 if (v6 == INVALID_SOCKET) {
1420 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1421 WSAGetLastError(), WSAEAFNOSUPPORT);
1422 goto end;
1424 /* Test a short IPv6 address */
1425 len = sizeof(address6);
1427 sockaddr6.sin6_family = AF_INET6;
1428 sockaddr6.sin6_port = 0x0000;
1429 sockaddr6.sin6_scope_id = 0;
1430 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1432 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1433 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1434 ok( !strcmp( address6, expect6_1 ), "Expected: %s, got: %s\n", expect6_1, address6 );
1435 ok( len == sizeof(expect6_1), "Got size %d\n", len);
1437 /* Test a longer IPv6 address */
1438 len = sizeof(address6);
1440 sockaddr6.sin6_family = AF_INET6;
1441 sockaddr6.sin6_port = 0x0000;
1442 sockaddr6.sin6_scope_id = 0;
1443 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1445 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1446 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1447 ok( !strcmp( address6, expect6_2 ), "Expected: %s, got: %s\n", expect6_2, address6 );
1448 ok( len == sizeof(expect6_2), "Got size %d\n", len);
1450 /* Test IPv6 address and port number */
1451 len = sizeof(address6);
1453 sockaddr6.sin6_family = AF_INET6;
1454 sockaddr6.sin6_port = 0xfa81;
1455 sockaddr6.sin6_scope_id = 0;
1456 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1458 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1459 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1460 ok( !strcmp( address6, expect6_3 ), "Expected: %s, got: %s\n", expect6_3, address6 );
1461 ok( len == sizeof(expect6_3), "Got size %d\n", len);
1463 /* Test IPv6 address, port number and scope_id */
1464 len = sizeof(address6);
1466 sockaddr6.sin6_family = AF_INET6;
1467 sockaddr6.sin6_port = 0xfa81;
1468 sockaddr6.sin6_scope_id = 0x1234;
1469 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1471 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1472 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1473 ok( !strcmp( address6, expect6_3_2 ), "Expected: %s, got: %s\n", expect6_3_2, address6 );
1474 ok( len == sizeof(expect6_3_2), "Got size %d\n", len);
1476 /* Test IPv6 address and scope_id */
1477 len = sizeof(address6);
1479 sockaddr6.sin6_family = AF_INET6;
1480 sockaddr6.sin6_port = 0x0000;
1481 sockaddr6.sin6_scope_id = 0x1234;
1482 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1484 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1485 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1486 ok( !strcmp( address6, expect6_3_3 ), "Expected: %s, got: %s\n", expect6_3_3, address6 );
1487 ok( len == sizeof(expect6_3_3), "Got size %d\n", len);
1489 end:
1490 if (v6 != INVALID_SOCKET)
1491 closesocket(v6);
1494 static void test_WSAAddressToStringW(void)
1496 SOCKET v6 = INVALID_SOCKET;
1497 INT ret;
1498 DWORD len;
1499 int GLE;
1500 SOCKADDR_IN sockaddr;
1501 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1503 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1504 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1505 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1506 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1507 '6', '5', '5', '3', '5', 0 };
1509 SOCKADDR_IN6 sockaddr6;
1510 WCHAR address6[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1512 CHAR addr6_1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1513 CHAR addr6_2[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1514 CHAR addr6_3[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1516 WCHAR expect6_1[] = {':',':','1',0};
1517 WCHAR expect6_2[] = {'2','0','a','b',':',':','1',0};
1518 WCHAR expect6_3[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1519 WCHAR expect6_3_2[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1520 WCHAR expect6_3_3[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1522 len = 0;
1524 sockaddr.sin_family = AF_INET;
1525 sockaddr.sin_port = 0;
1526 sockaddr.sin_addr.s_addr = 0;
1528 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1529 GLE = WSAGetLastError();
1530 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1531 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1532 GLE, ret );
1534 len = sizeof(address);
1536 sockaddr.sin_family = AF_INET;
1537 sockaddr.sin_port = 0;
1538 sockaddr.sin_addr.s_addr = 0;
1540 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1541 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1543 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1544 ok( len == sizeof( expect1 )/sizeof( WCHAR ), "Got size %d\n", len);
1546 len = sizeof(address);
1548 sockaddr.sin_family = AF_INET;
1549 sockaddr.sin_port = 0;
1550 sockaddr.sin_addr.s_addr = 0xffffffff;
1552 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1553 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1555 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1557 len = sizeof(address);
1559 sockaddr.sin_family = AF_INET;
1560 sockaddr.sin_port = 0xffff;
1561 sockaddr.sin_addr.s_addr = 0;
1563 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1564 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1566 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1568 len = sizeof(address);
1570 sockaddr.sin_family = AF_INET;
1571 sockaddr.sin_port = 0xffff;
1572 sockaddr.sin_addr.s_addr = 0xffffffff;
1574 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1575 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1577 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1578 ok( len == sizeof( expect4 )/sizeof( WCHAR ), "Got %d\n", len);
1580 /*check to see it IPv6 is available */
1581 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1582 if (v6 == INVALID_SOCKET) {
1583 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1584 WSAGetLastError(), WSAEAFNOSUPPORT);
1585 goto end;
1588 /* Test a short IPv6 address */
1589 len = sizeof(address6)/sizeof(WCHAR);
1591 sockaddr6.sin6_family = AF_INET6;
1592 sockaddr6.sin6_port = 0x0000;
1593 sockaddr6.sin6_scope_id = 0;
1594 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_1, sizeof(addr6_1));
1596 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1597 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1598 ok( !lstrcmpW( address6, expect6_1 ), "Wrong string returned\n" );
1599 ok( len == sizeof(expect6_1)/sizeof(WCHAR), "Got %d\n", len);
1601 /* Test a longer IPv6 address */
1602 len = sizeof(address6)/sizeof(WCHAR);
1604 sockaddr6.sin6_family = AF_INET6;
1605 sockaddr6.sin6_port = 0x0000;
1606 sockaddr6.sin6_scope_id = 0;
1607 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_2, sizeof(addr6_2));
1609 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1610 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1612 ok( !lstrcmpW( address6, expect6_2 ), "Wrong string returned\n" );
1613 ok( len == sizeof(expect6_2)/sizeof(WCHAR), "Got %d\n", len);
1615 /* Test IPv6 address and port number */
1616 len = sizeof(address6)/sizeof(WCHAR);
1618 sockaddr6.sin6_family = AF_INET6;
1619 sockaddr6.sin6_port = 0xfa81;
1620 sockaddr6.sin6_scope_id = 0;
1621 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1623 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1624 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1625 ok( !lstrcmpW( address6, expect6_3 ), "Wrong string returned\n" );
1626 ok( len == sizeof(expect6_3)/sizeof(WCHAR), "Got %d\n", len);
1628 /* Test IPv6 address, port number and scope_id */
1629 len = sizeof(address6)/sizeof(WCHAR);
1631 sockaddr6.sin6_family = AF_INET6;
1632 sockaddr6.sin6_port = 0xfa81;
1633 sockaddr6.sin6_scope_id = 0x1234;
1634 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1636 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1637 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1638 ok( !lstrcmpW( address6, expect6_3_2 ), "Wrong string returned\n" );
1639 ok( len == sizeof(expect6_3_2)/sizeof(WCHAR), "Got %d\n", len);
1641 /* Test IPv6 address and scope_id */
1642 len = sizeof(address6)/sizeof(WCHAR);
1644 sockaddr6.sin6_family = AF_INET6;
1645 sockaddr6.sin6_port = 0x0000;
1646 sockaddr6.sin6_scope_id = 0xfffe;
1647 memcpy (sockaddr6.sin6_addr.s6_addr, addr6_3, sizeof(addr6_3));
1649 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr6, sizeof(sockaddr6), NULL, address6, &len );
1650 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1651 ok( !lstrcmpW( address6, expect6_3_3 ), "Wrong string returned\n" );
1652 ok( len == sizeof(expect6_3_3)/sizeof(WCHAR), "Got %d\n", len);
1654 end:
1655 if (v6 != INVALID_SOCKET)
1656 closesocket(v6);
1659 static void test_WSAStringToAddressA(void)
1661 INT ret, len;
1662 SOCKADDR_IN sockaddr;
1663 SOCKADDR_IN6 sockaddr6;
1664 int GLE;
1666 CHAR address1[] = "0.0.0.0";
1667 CHAR address2[] = "127.127.127.127";
1668 CHAR address3[] = "255.255.255.255";
1669 CHAR address4[] = "127.127.127.127:65535";
1670 CHAR address5[] = "255.255.255.255:65535";
1671 CHAR address6[] = "::1";
1672 CHAR address7[] = "[::1]";
1673 CHAR address8[] = "[::1]:65535";
1675 len = 0;
1676 sockaddr.sin_family = AF_INET;
1678 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1679 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1680 WSAGetLastError() );
1682 len = sizeof(sockaddr);
1683 sockaddr.sin_port = 0;
1684 sockaddr.sin_addr.s_addr = 0;
1686 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1687 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1688 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1690 len = sizeof(sockaddr);
1691 sockaddr.sin_port = 0;
1692 sockaddr.sin_addr.s_addr = 0;
1694 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1695 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1696 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1698 len = sizeof(sockaddr);
1699 sockaddr.sin_port = 0;
1700 sockaddr.sin_addr.s_addr = 0;
1702 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1703 GLE = WSAGetLastError();
1704 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1705 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1706 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1708 len = sizeof(sockaddr);
1709 sockaddr.sin_port = 0;
1710 sockaddr.sin_addr.s_addr = 0;
1712 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1713 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1714 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1716 len = sizeof(sockaddr);
1717 sockaddr.sin_port = 0;
1718 sockaddr.sin_addr.s_addr = 0;
1720 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1721 GLE = WSAGetLastError();
1722 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1723 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1724 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1726 len = sizeof(sockaddr6);
1727 memset(&sockaddr6, 0, len);
1728 sockaddr6.sin6_family = AF_INET6;
1730 ret = WSAStringToAddressA( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1731 &len );
1732 GLE = WSAGetLastError();
1733 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1734 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1736 len = sizeof(sockaddr6);
1737 memset(&sockaddr6, 0, len);
1738 sockaddr6.sin6_family = AF_INET6;
1740 ret = WSAStringToAddressA( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1741 &len );
1742 GLE = WSAGetLastError();
1743 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1744 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1746 len = sizeof(sockaddr6);
1747 memset(&sockaddr6, 0, len);
1748 sockaddr6.sin6_family = AF_INET6;
1750 ret = WSAStringToAddressA( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1751 &len );
1752 GLE = WSAGetLastError();
1753 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1754 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1755 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE);
1759 static void test_WSAStringToAddressW(void)
1761 INT ret, len;
1762 SOCKADDR_IN sockaddr;
1763 SOCKADDR_IN6 sockaddr6;
1764 int GLE;
1766 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1767 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1768 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1769 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1770 ':', '6', '5', '5', '3', '5', 0 };
1771 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1772 '6', '5', '5', '3', '5', 0 };
1773 WCHAR address6[] = {':',':','1','\0'};
1774 WCHAR address7[] = {'[',':',':','1',']','\0'};
1775 WCHAR address8[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1777 len = 0;
1778 sockaddr.sin_family = AF_INET;
1780 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1781 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1782 WSAGetLastError() );
1784 len = sizeof(sockaddr);
1785 sockaddr.sin_port = 0;
1786 sockaddr.sin_addr.s_addr = 0;
1788 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1789 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1790 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1792 len = sizeof(sockaddr);
1793 sockaddr.sin_port = 0;
1794 sockaddr.sin_addr.s_addr = 0;
1796 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1797 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1798 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1800 len = sizeof(sockaddr);
1801 sockaddr.sin_port = 0;
1802 sockaddr.sin_addr.s_addr = 0;
1804 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1805 GLE = WSAGetLastError();
1806 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1807 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1808 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1810 len = sizeof(sockaddr);
1811 sockaddr.sin_port = 0;
1812 sockaddr.sin_addr.s_addr = 0;
1814 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1815 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1816 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1818 len = sizeof(sockaddr);
1819 sockaddr.sin_port = 0;
1820 sockaddr.sin_addr.s_addr = 0;
1822 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1823 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1824 (ret == SOCKET_ERROR && (GLE == ERROR_INVALID_PARAMETER || GLE == WSAEINVAL)),
1825 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1827 len = sizeof(sockaddr6);
1828 memset(&sockaddr6, 0, len);
1829 sockaddr6.sin6_family = AF_INET6;
1831 ret = WSAStringToAddressW( address6, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1832 &len );
1833 GLE = WSAGetLastError();
1834 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1835 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1837 len = sizeof(sockaddr6);
1838 memset(&sockaddr6, 0, len);
1839 sockaddr6.sin6_family = AF_INET6;
1841 ret = WSAStringToAddressW( address7, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1842 &len );
1843 GLE = WSAGetLastError();
1844 ok( ret == 0 || (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1845 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1847 len = sizeof(sockaddr6);
1848 memset(&sockaddr6, 0, len);
1849 sockaddr6.sin6_family = AF_INET6;
1851 ret = WSAStringToAddressW( address8, AF_INET6, NULL, (SOCKADDR*)&sockaddr6,
1852 &len );
1853 GLE = WSAGetLastError();
1854 ok( (ret == 0 && sockaddr6.sin6_port == 0xffff) ||
1855 (ret == SOCKET_ERROR && GLE == WSAEINVAL),
1856 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE);
1860 static VOID WINAPI SelectReadThread(select_thread_params *par)
1862 fd_set readfds;
1863 int ret;
1864 struct sockaddr_in addr;
1865 struct timeval select_timeout;
1867 FD_ZERO(&readfds);
1868 FD_SET(par->s, &readfds);
1869 select_timeout.tv_sec=5;
1870 select_timeout.tv_usec=0;
1871 addr.sin_family = AF_INET;
1872 addr.sin_addr.s_addr = inet_addr(SERVERIP);
1873 addr.sin_port = htons(SERVERPORT);
1875 do_bind(par->s, (struct sockaddr *)&addr, sizeof(addr));
1876 wsa_ok(listen(par->s, SOMAXCONN ), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1878 SetEvent(server_ready);
1879 ret = select(par->s+1, &readfds, NULL, NULL, &select_timeout);
1880 par->ReadKilled = (ret == 1);
1883 static void test_select(void)
1885 SOCKET fdRead, fdWrite;
1886 fd_set readfds, writefds, exceptfds;
1887 unsigned int maxfd;
1888 int ret;
1889 struct timeval select_timeout;
1890 select_thread_params thread_params;
1891 HANDLE thread_handle;
1892 DWORD id;
1894 fdRead = socket(AF_INET, SOCK_STREAM, 0);
1895 ok( (fdRead != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1896 fdWrite = socket(AF_INET, SOCK_STREAM, 0);
1897 ok( (fdWrite != INVALID_SOCKET), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1899 FD_ZERO(&readfds);
1900 FD_ZERO(&writefds);
1901 FD_ZERO(&exceptfds);
1902 FD_SET(fdRead, &readfds);
1903 FD_SET(fdWrite, &writefds);
1904 FD_SET(fdRead, &exceptfds);
1905 FD_SET(fdWrite, &exceptfds);
1906 select_timeout.tv_sec=0;
1907 select_timeout.tv_usec=500;
1909 maxfd = fdRead;
1910 if (fdWrite > maxfd)
1911 maxfd = fdWrite;
1913 todo_wine {
1914 ret = select(maxfd+1, &readfds, &writefds, &exceptfds, &select_timeout);
1915 ok ( (ret == 0), "select should not return any socket handles\n");
1916 ok ( !FD_ISSET(fdRead, &readfds), "FD should not be set\n");
1917 ok ( !FD_ISSET(fdWrite, &writefds), "FD should not be set\n");
1920 ok ( !FD_ISSET(fdRead, &exceptfds), "FD should not be set\n");
1921 ok ( !FD_ISSET(fdWrite, &exceptfds), "FD should not be set\n");
1923 todo_wine {
1924 ok ((listen(fdWrite, SOMAXCONN) == SOCKET_ERROR), "listen did not fail\n");
1926 ret = closesocket(fdWrite);
1927 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1929 thread_params.s = fdRead;
1930 thread_params.ReadKilled = FALSE;
1931 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
1932 thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) &SelectReadThread, &thread_params, 0, &id );
1933 ok ( (thread_handle != NULL), "CreateThread failed unexpectedly: %d\n", GetLastError());
1935 WaitForSingleObject (server_ready, INFINITE);
1936 Sleep(200);
1937 ret = closesocket(fdRead);
1938 ok ( (ret == 0), "closesocket failed unexpectedly: %d\n", ret);
1940 WaitForSingleObject (thread_handle, 1000);
1941 ok ( (thread_params.ReadKilled) ||
1942 broken(thread_params.ReadKilled == 0), /*Win98*/
1943 "closesocket did not wakeup select\n");
1947 static DWORD WINAPI AcceptKillThread(select_thread_params *par)
1949 struct sockaddr_in address;
1950 int len = sizeof(address);
1951 SOCKET client_socket;
1953 SetEvent(server_ready);
1954 client_socket = accept(par->s, (struct sockaddr*) &address, &len);
1955 if (client_socket != INVALID_SOCKET)
1956 closesocket(client_socket);
1957 par->ReadKilled = (client_socket == INVALID_SOCKET);
1958 return 0;
1962 static int CALLBACK AlwaysDeferConditionFunc(LPWSABUF lpCallerId, LPWSABUF lpCallerData, LPQOS pQos,
1963 LPQOS lpGQOS, LPWSABUF lpCalleeId, LPWSABUF lpCalleeData,
1964 GROUP FAR * g, DWORD_PTR dwCallbackData)
1966 return CF_DEFER;
1969 static void test_accept(void)
1971 int ret;
1972 SOCKET server_socket = INVALID_SOCKET, accepted = INVALID_SOCKET, connector = INVALID_SOCKET;
1973 struct sockaddr_in address;
1974 int socklen;
1975 select_thread_params thread_params;
1976 HANDLE thread_handle = NULL;
1977 DWORD id;
1979 server_socket = socket(AF_INET, SOCK_STREAM, 0);
1980 if (server_socket == INVALID_SOCKET)
1982 trace("error creating server socket: %d\n", WSAGetLastError());
1983 goto done;
1986 memset(&address, 0, sizeof(address));
1987 address.sin_addr.s_addr = inet_addr("127.0.0.1");
1988 address.sin_family = AF_INET;
1989 ret = bind(server_socket, (struct sockaddr*) &address, sizeof(address));
1990 if (ret != 0)
1992 trace("error binding server socket: %d\n", WSAGetLastError());
1993 goto done;
1996 socklen = sizeof(address);
1997 ret = getsockname(server_socket, (struct sockaddr*)&address, &socklen);
1998 if (ret != 0) {
1999 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2000 goto done;
2003 ret = listen(server_socket, 5);
2004 if (ret != 0)
2006 trace("error making server socket listen: %d\n", WSAGetLastError());
2007 goto done;
2010 trace("Blocking accept next\n");
2012 connector = socket(AF_INET, SOCK_STREAM, 0);
2013 ok(connector != INVALID_SOCKET, "Failed to create connector socket, error %d\n", WSAGetLastError());
2015 ret = connect(connector, (struct sockaddr*)&address, sizeof(address));
2016 ok(ret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2018 accepted = WSAAccept(server_socket, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
2019 ok(accepted == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
2021 accepted = accept(server_socket, NULL, 0);
2022 ok(accepted != INVALID_SOCKET, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2024 server_ready = CreateEventA(NULL, TRUE, FALSE, NULL);
2025 if (server_ready == INVALID_HANDLE_VALUE)
2027 trace("error creating event: %d\n", GetLastError());
2028 goto done;
2031 thread_params.s = server_socket;
2032 thread_params.ReadKilled = FALSE;
2033 thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptKillThread,
2034 &thread_params, 0, &id);
2035 if (thread_handle == NULL)
2037 trace("error creating thread: %d\n", GetLastError());
2038 goto done;
2041 WaitForSingleObject(server_ready, INFINITE);
2042 Sleep(200);
2043 ret = closesocket(server_socket);
2044 if (ret != 0)
2046 trace("closesocket failed: %d\n", WSAGetLastError());
2047 goto done;
2050 WaitForSingleObject(thread_handle, 1000);
2051 ok(thread_params.ReadKilled || broken(!thread_params.ReadKilled) /* Win98/ME, after accept */,
2052 "closesocket did not wakeup accept\n");
2054 done:
2055 if (accepted != INVALID_SOCKET)
2056 closesocket(accepted);
2057 if (connector != INVALID_SOCKET)
2058 closesocket(connector);
2059 if (thread_handle != NULL)
2060 CloseHandle(thread_handle);
2061 if (server_ready != INVALID_HANDLE_VALUE)
2062 CloseHandle(server_ready);
2063 if (server_socket != INVALID_SOCKET)
2064 closesocket(server_socket);
2067 static void test_extendedSocketOptions(void)
2069 WSADATA wsa;
2070 SOCKET sock;
2071 struct sockaddr_in sa;
2072 int sa_len = sizeof(struct sockaddr_in);
2073 int optval, optlen = sizeof(int), ret;
2074 BOOL bool_opt_val;
2075 LINGER linger_val;
2077 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2078 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2079 return;
2082 memset(&sa, 0, sa_len);
2084 sa.sin_family = AF_INET;
2085 sa.sin_port = htons(0);
2086 sa.sin_addr.s_addr = htonl(INADDR_ANY);
2088 if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) {
2089 trace("Creating the socket failed: %d\n", WSAGetLastError());
2090 WSACleanup();
2091 return;
2094 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2095 trace("Failed to bind socket: %d\n", WSAGetLastError());
2096 closesocket(sock);
2097 WSACleanup();
2098 return;
2101 ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2103 ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
2104 ok((optval == 65507) || (optval == 65527),
2105 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval);
2107 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2108 SetLastError(0xdeadbeef);
2109 optval = 0xdeadbeef;
2110 optlen = sizeof(int);
2111 ret = getsockopt(sock, 0xffffffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2112 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2113 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2114 ret, WSAGetLastError(), optval, optval);
2116 /* more invalid values for level */
2117 SetLastError(0xdeadbeef);
2118 optval = 0xdeadbeef;
2119 optlen = sizeof(int);
2120 ret = getsockopt(sock, 0x1234ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2121 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2122 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2123 ret, WSAGetLastError(), optval, optval);
2125 SetLastError(0xdeadbeef);
2126 optval = 0xdeadbeef;
2127 optlen = sizeof(int);
2128 ret = getsockopt(sock, 0x8000ffff, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2129 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2130 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2131 ret, WSAGetLastError(), optval, optval);
2133 SetLastError(0xdeadbeef);
2134 optval = 0xdeadbeef;
2135 optlen = sizeof(int);
2136 ret = getsockopt(sock, 0x00008000, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2137 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2138 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2139 ret, WSAGetLastError(), optval, optval);
2141 SetLastError(0xdeadbeef);
2142 optval = 0xdeadbeef;
2143 optlen = sizeof(int);
2144 ret = getsockopt(sock, 0x00000800, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
2145 ok( (ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEINVAL),
2146 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2147 ret, WSAGetLastError(), optval, optval);
2149 optlen = sizeof(LINGER);
2150 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2151 todo_wine{
2152 ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
2155 closesocket(sock);
2157 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2158 trace("Creating the socket failed: %d\n", WSAGetLastError());
2159 WSACleanup();
2160 return;
2163 if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
2164 trace("Failed to bind socket: %d\n", WSAGetLastError());
2165 closesocket(sock);
2166 WSACleanup();
2167 return;
2170 ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
2171 ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
2173 optlen = sizeof(BOOL);
2174 ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
2175 ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
2176 ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
2177 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2178 bool_opt_val, linger_val.l_onoff);
2180 closesocket(sock);
2181 WSACleanup();
2184 static void test_getsockname(void)
2186 WSADATA wsa;
2187 SOCKET sock;
2188 struct sockaddr_in sa_set, sa_get;
2189 int sa_set_len = sizeof(struct sockaddr_in);
2190 int sa_get_len = sa_set_len;
2191 static const unsigned char null_padding[] = {0,0,0,0,0,0,0,0};
2192 int ret;
2194 if(WSAStartup(MAKEWORD(2,0), &wsa)){
2195 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2196 return;
2199 memset(&sa_set, 0, sa_set_len);
2201 sa_set.sin_family = AF_INET;
2202 sa_set.sin_port = htons(0);
2203 sa_set.sin_addr.s_addr = htonl(INADDR_ANY);
2205 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
2206 trace("Creating the socket failed: %d\n", WSAGetLastError());
2207 WSACleanup();
2208 return;
2211 memcpy(&sa_get, &sa_set, sizeof(sa_set));
2212 if (getsockname(sock, (struct sockaddr*) &sa_get, &sa_get_len) == 0)
2213 ok(0, "getsockname on unbound socket should fail\n");
2214 else {
2215 ok(WSAGetLastError() == WSAEINVAL, "getsockname on unbound socket "
2216 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL);
2217 ok(memcmp(&sa_get, &sa_set, sizeof(sa_get)) == 0,
2218 "failed getsockname modified sockaddr when it shouldn't\n");
2221 if(bind(sock, (struct sockaddr *) &sa_set, sa_set_len) < 0){
2222 trace("Failed to bind socket: %d\n", WSAGetLastError());
2223 closesocket(sock);
2224 WSACleanup();
2225 return;
2228 if(getsockname(sock, (struct sockaddr *) &sa_get, &sa_get_len) != 0){
2229 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2230 closesocket(sock);
2231 WSACleanup();
2232 return;
2235 ret = memcmp(sa_get.sin_zero, null_padding, 8);
2236 ok(ret == 0 || broken(ret != 0), /* NT4 */
2237 "getsockname did not zero the sockaddr_in structure\n");
2239 closesocket(sock);
2240 WSACleanup();
2243 static void test_dns(void)
2245 struct hostent *h;
2247 h = gethostbyname("");
2248 ok(h != NULL, "gethostbyname(\"\") failed with %d\n", h_errno);
2251 /* Our winsock headers don't define gethostname because it conflicts with the
2252 * definition in unistd.h. Define it here to get rid of the warning. */
2254 int WINAPI gethostname(char *name, int namelen);
2256 static void test_gethostbyname_hack(void)
2258 struct hostent *he;
2259 char name[256];
2260 static BYTE loopback[] = {127, 0, 0, 1};
2261 static BYTE magic_loopback[] = {127, 12, 34, 56};
2262 int ret;
2264 ret = gethostname(name, 256);
2265 ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError());
2267 he = gethostbyname("localhost");
2268 ok(he != NULL, "gethostbyname(\"localhost\") failed: %d\n", h_errno);
2269 if(he)
2271 if(he->h_length != 4)
2273 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2274 return;
2277 ok(memcmp(he->h_addr_list[0], loopback, he->h_length) == 0,
2278 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2279 he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2],
2280 he->h_addr_list[0][3]);
2283 /* No reason to test further with NULL hostname */
2284 if(name == NULL)
2285 return;
2287 if(strcmp(name, "localhost") == 0)
2289 skip("hostname seems to be \"localhost\", skipping test.\n");
2290 return;
2293 he = NULL;
2294 he = gethostbyname(name);
2295 ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, h_errno);
2296 if(he)
2298 if(he->h_length != 4)
2300 skip("h_length is %d, not IPv4, skipping test.\n", he->h_length);
2301 return;
2304 if (he->h_addr_list[0][0] == 127)
2306 ok(memcmp(he->h_addr_list[0], magic_loopback, he->h_length) == 0,
2307 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2308 name, he->h_addr_list[0][0], he->h_addr_list[0][1],
2309 he->h_addr_list[0][2], he->h_addr_list[0][3]);
2313 he = NULL;
2314 he = gethostbyname("nonexistent.winehq.org");
2315 /* Don't check for the return value, as some braindead ISPs will kindly
2316 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2319 static void test_inet_addr(void)
2321 u_long addr;
2323 addr = inet_addr(NULL);
2324 ok(addr == INADDR_NONE, "inet_addr succeeded unexpectedly\n");
2327 static void test_addr_to_print(void)
2329 char dst[16];
2330 char dst6[64];
2331 const char * pdst;
2332 struct in_addr in;
2333 struct in6_addr in6;
2335 u_long addr0_Num = 0x00000000;
2336 PCSTR addr0_Str = "0.0.0.0";
2337 u_long addr1_Num = 0x20201015;
2338 PCSTR addr1_Str = "21.16.32.32";
2339 u_char addr2_Num[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2340 PCSTR addr2_Str = "::fffe:cc98:bd74";
2341 u_char addr3_Num[16] = {0x20,0x30,0xa4,0xb1};
2342 PCSTR addr3_Str = "2030:a4b1::";
2343 u_char addr4_Num[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2344 PCSTR addr4_Str = "::204.152.189.116";
2346 /* Test IPv4 addresses */
2347 in.s_addr = addr0_Num;
2349 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2350 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2351 ok(!strcmp(pdst, addr0_Str),"Address %s != %s\n", pdst, addr0_Str);
2353 /* Test that inet_ntoa and inet_ntop return the same value */
2354 in.S_un.S_addr = addr1_Num;
2355 pdst = inet_ntoa(*((struct in_addr*)&in.s_addr));
2356 ok(pdst != NULL, "inet_ntoa failed %s\n", dst);
2357 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2359 /* InetNtop became available in Vista and Win2008 */
2360 if (!pInetNtop)
2362 win_skip("InetNtop not present, not executing tests\n");
2363 return;
2366 /* Second part of test */
2367 pdst = pInetNtop(AF_INET,(void*)&in.s_addr, dst, sizeof(dst));
2368 ok(pdst != NULL, "InetNtop failed %s\n", dst);
2369 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2371 /* Test invalid parm conditions */
2372 pdst = pInetNtop(1, (void*)&in.s_addr, dst, sizeof(dst));
2373 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2374 ok(WSAGetLastError() == WSAEAFNOSUPPORT, "Should be WSAEAFNOSUPPORT\n");
2376 /* Test Null destination */
2377 pdst = NULL;
2378 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, NULL, sizeof(dst));
2379 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2380 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2381 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2383 /* Test zero length passed */
2384 WSASetLastError(0);
2385 pdst = NULL;
2386 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 0);
2387 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2388 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2389 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2391 /* Test length one shorter than the address length */
2392 WSASetLastError(0);
2393 pdst = NULL;
2394 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, 6);
2395 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2396 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2397 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2399 /* Test longer length is ok */
2400 WSASetLastError(0);
2401 pdst = NULL;
2402 pdst = pInetNtop(AF_INET, (void*)&in.s_addr, dst, sizeof(dst)+1);
2403 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
2404 ok(!strcmp(pdst, addr1_Str),"Address %s != %s\n", pdst, addr1_Str);
2406 /* Test the IPv6 addresses */
2408 /* Test an zero prefixed IPV6 address */
2409 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2410 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2411 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2412 ok(!strcmp(pdst, addr2_Str),"Address %s != %s\n", pdst, addr2_Str);
2414 /* Test an zero suffixed IPV6 address */
2415 memcpy(in6.s6_addr, addr3_Num, sizeof(addr3_Num));
2416 pdst = pInetNtop(AF_INET6,(void*)&in6.s6_addr, dst6, sizeof(dst6));
2417 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2418 ok(!strcmp(pdst, addr3_Str),"Address %s != %s\n", pdst, addr3_Str);
2420 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2421 memcpy(in6.s6_addr, addr4_Num, sizeof(addr4_Num));
2422 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, sizeof(dst6));
2423 ok(pdst != NULL, "InetNtop failed %s\n", dst6);
2424 ok(!strcmp(pdst, addr4_Str),"Address %s != %s\n", pdst, addr4_Str);
2426 /* Test invalid parm conditions */
2427 memcpy(in6.u.Byte, addr2_Num, sizeof(addr2_Num));
2429 /* Test Null destination */
2430 pdst = NULL;
2431 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, NULL, sizeof(dst6));
2432 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2433 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2434 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2436 /* Test zero length passed */
2437 WSASetLastError(0);
2438 pdst = NULL;
2439 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 0);
2440 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2441 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2442 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2444 /* Test length one shorter than the address length */
2445 WSASetLastError(0);
2446 pdst = NULL;
2447 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 16);
2448 ok(pdst == NULL, "The pointer should not be returned (%p)\n", pdst);
2449 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER || WSAGetLastError() == WSAEINVAL /* Win7 */,
2450 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2452 /* Test longer length is ok */
2453 WSASetLastError(0);
2454 pdst = NULL;
2455 pdst = pInetNtop(AF_INET6, (void*)&in6.s6_addr, dst6, 18);
2456 ok(pdst != NULL, "The pointer should be returned (%p)\n", pdst);
2459 static void test_ioctlsocket(void)
2461 SOCKET sock;
2462 int ret;
2463 static const LONG cmds[] = {FIONBIO, FIONREAD, SIOCATMARK};
2464 UINT i;
2465 u_long arg = 0;
2467 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2468 ok(sock != INVALID_SOCKET, "Creating the socket failed: %d\n", WSAGetLastError());
2469 if(sock == INVALID_SOCKET)
2471 skip("Can't continue without a socket.\n");
2472 return;
2475 for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++)
2477 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2478 ret = ioctlsocket(sock, cmds[i], (u_long *)1);
2479 ok(ret == SOCKET_ERROR, "ioctlsocket succeeded unexpectedly\n");
2480 ret = WSAGetLastError();
2481 ok(ret == WSAEFAULT, "expected WSAEFAULT, got %d instead\n", ret);
2484 /* A fresh and not connected socket has no urgent data, this test shows
2485 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2487 ret = ioctlsocket(sock, SIOCATMARK, &arg);
2488 if(ret != SOCKET_ERROR)
2489 todo_wine ok(arg, "expected a non-zero value\n");
2492 static int drain_pause=0;
2493 static DWORD WINAPI drain_socket_thread(LPVOID arg)
2495 char buffer[1024];
2496 SOCKET sock = *(SOCKET*)arg;
2497 int ret;
2499 while ((ret = recv(sock, buffer, sizeof(buffer), 0)) != 0)
2501 if (ret < 0)
2503 if (WSAGetLastError() == WSAEWOULDBLOCK)
2505 fd_set readset;
2506 FD_ZERO(&readset);
2507 FD_SET(sock, &readset);
2508 select(0, &readset, NULL, NULL, NULL);
2509 while (drain_pause)
2510 Sleep(100);
2512 else
2513 break;
2516 return 0;
2519 static void test_send(void)
2521 SOCKET src = INVALID_SOCKET;
2522 SOCKET dst = INVALID_SOCKET;
2523 HANDLE hThread = NULL;
2524 const int buflen = 1024*1024;
2525 char *buffer = NULL;
2526 int ret;
2527 DWORD id;
2529 if (tcp_socketpair(&src, &dst) != 0)
2531 ok(0, "creating socket pair failed, skipping test\n");
2532 return;
2535 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2536 if (hThread == NULL)
2538 ok(0, "CreateThread failed, error %d\n", GetLastError());
2539 goto end;
2542 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
2543 if (buffer == NULL)
2545 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2546 goto end;
2549 ret = send(src, buffer, buflen, 0);
2550 if (ret >= 0)
2551 ok(ret == buflen, "send should have sent %d bytes, but it only sent %d\n", buflen, ret);
2552 else
2553 ok(0, "send failed, error %d\n", WSAGetLastError());
2555 end:
2556 if (src != INVALID_SOCKET)
2557 closesocket(src);
2558 if (dst != INVALID_SOCKET)
2559 closesocket(dst);
2560 if (hThread != NULL)
2561 CloseHandle(hThread);
2562 HeapFree(GetProcessHeap(), 0, buffer);
2565 static void test_write_events(void)
2567 SOCKET src = INVALID_SOCKET;
2568 SOCKET dst = INVALID_SOCKET;
2569 HANDLE hThread = NULL;
2570 HANDLE hEvent = INVALID_HANDLE_VALUE;
2571 char *buffer = NULL;
2572 int bufferSize = 1024*1024;
2573 u_long one = 1;
2574 int ret;
2575 DWORD id;
2576 WSANETWORKEVENTS netEvents;
2577 DWORD dwRet;
2579 if (tcp_socketpair(&src, &dst) != 0)
2581 ok(0, "creating socket pair failed, skipping test\n");
2582 return;
2585 /* On Windows it seems when a non-blocking socket sends to a
2586 blocking socket on the same host, the send() is BLOCKING,
2587 so make both sockets non-blocking */
2588 ret = ioctlsocket(src, FIONBIO, &one);
2589 if (ret)
2591 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2592 goto end;
2594 ret = ioctlsocket(dst, FIONBIO, &one);
2595 if (ret)
2597 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2598 goto end;
2601 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize);
2602 if (buffer == NULL)
2604 ok(0, "could not allocate memory for test\n");
2605 goto end;
2608 hThread = CreateThread(NULL, 0, drain_socket_thread, &dst, 0, &id);
2609 if (hThread == NULL)
2611 ok(0, "CreateThread failed, error %d\n", GetLastError());
2612 goto end;
2615 hEvent = CreateEventA(NULL, FALSE, TRUE, NULL);
2616 if (hEvent == INVALID_HANDLE_VALUE)
2618 ok(0, "CreateEventA failed, error %d\n", GetLastError());
2619 goto end;
2622 ret = WSAEventSelect(src, hEvent, FD_WRITE | FD_CLOSE);
2623 if (ret)
2625 ok(0, "WSAEventSelect failed, error %d\n", ret);
2626 goto end;
2629 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
2630 dwRet = WaitForSingleObject(hEvent, 5000);
2631 if (dwRet != WAIT_OBJECT_0)
2633 ok(0, "Initial WaitForSingleObject failed, error %d\n", dwRet);
2634 goto end;
2636 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2637 if (ret)
2639 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2640 goto end;
2642 if (netEvents.lNetworkEvents & FD_WRITE)
2644 ret = send(src, "a", 1, 0);
2645 ok(ret == 1, "sending 1 byte failed, error %d\n", WSAGetLastError());
2646 if (ret != 1)
2647 goto end;
2649 else
2651 ok(0, "FD_WRITE not among initial events\n");
2652 goto end;
2655 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
2656 dwRet = WaitForSingleObject(hEvent, 2000);
2657 if (dwRet == WAIT_OBJECT_0)
2659 ok(0, "WaitForSingleObject should have timed out, but succeeded!\n");
2660 goto end;
2663 /* Now if we send a ton of data and the 'server' does not drain it fast
2664 * enough (set drain_pause to be sure), the socket send buffer will only
2665 * take some of it, and we will get a short write. This will trigger
2666 * another FD_WRITE event as soon as data is sent and more space becomes
2667 * available, but not any earlier. */
2668 drain_pause=1;
2671 ret = send(src, buffer, bufferSize, 0);
2672 } while (ret == bufferSize);
2673 drain_pause=0;
2674 if (ret >= 0 || WSAGetLastError() == WSAEWOULDBLOCK)
2676 dwRet = WaitForSingleObject(hEvent, 5000);
2677 ok(dwRet == WAIT_OBJECT_0, "Waiting failed with %d\n", dwRet);
2678 if (dwRet == WAIT_OBJECT_0)
2680 ret = WSAEnumNetworkEvents(src, NULL, &netEvents);
2681 ok(ret == 0, "WSAEnumNetworkEvents failed, error %d\n", ret);
2682 if (ret == 0)
2683 goto end;
2684 ok(netEvents.lNetworkEvents & FD_WRITE,
2685 "FD_WRITE event not set as expected, events are 0x%x\n", netEvents.lNetworkEvents);
2687 else
2688 goto end;
2690 else
2692 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
2693 goto end;
2696 end:
2697 HeapFree(GetProcessHeap(), 0, buffer);
2698 if (src != INVALID_SOCKET)
2699 closesocket(src);
2700 if (dst != INVALID_SOCKET)
2701 closesocket(dst);
2702 if (hThread != NULL)
2703 CloseHandle(hThread);
2704 CloseHandle(hEvent);
2707 static void test_ipv6only(void)
2709 SOCKET v4 = INVALID_SOCKET,
2710 v6 = INVALID_SOCKET;
2711 struct sockaddr_in sin4;
2712 struct sockaddr_in6 sin6;
2713 int ret;
2715 memset(&sin4, 0, sizeof(sin4));
2716 sin4.sin_family = AF_INET;
2717 sin4.sin_port = htons(SERVERPORT);
2719 memset(&sin6, 0, sizeof(sin6));
2720 sin6.sin6_family = AF_INET6;
2721 sin6.sin6_port = htons(SERVERPORT);
2723 v6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
2724 if (v6 == INVALID_SOCKET) {
2725 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2726 WSAGetLastError(), WSAEAFNOSUPPORT);
2727 goto end;
2729 ret = bind(v6, (struct sockaddr*)&sin6, sizeof(sin6));
2730 if (ret) {
2731 skip("Could not bind IPv6 address (LastError: %d).\n",
2732 WSAGetLastError());
2733 goto end;
2736 v4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2737 if (v4 == INVALID_SOCKET) {
2738 skip("Could not create IPv4 socket (LastError: %d).\n",
2739 WSAGetLastError());
2740 goto end;
2742 ret = bind(v4, (struct sockaddr*)&sin4, sizeof(sin4));
2743 ok(!ret, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2744 WSAGetLastError(), WSAEADDRINUSE);
2746 end:
2747 if (v4 != INVALID_SOCKET)
2748 closesocket(v4);
2749 if (v6 != INVALID_SOCKET)
2750 closesocket(v6);
2753 static void test_WSASendTo(void)
2755 SOCKET s;
2756 struct sockaddr_in addr;
2757 char buf[12] = "hello world";
2758 WSABUF data_buf;
2759 DWORD bytesSent;
2761 addr.sin_family = AF_INET;
2762 addr.sin_port = htons(139);
2763 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2764 data_buf.len = sizeof(buf);
2765 data_buf.buf = buf;
2767 if( (s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
2768 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2769 return;
2772 WSASetLastError(12345);
2773 if(WSASendTo(s, &data_buf, 1, &bytesSent, 0, (struct sockaddr*)&addr, sizeof(addr), NULL, NULL)) {
2774 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2775 return;
2777 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2778 "a successful call to WSASendTo()\n");
2781 static void test_GetAddrInfoW(void)
2783 static const WCHAR port[] = {'8','0',0};
2784 static const WCHAR empty[] = {0};
2785 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
2787 int ret;
2788 ADDRINFOW *result, hint;
2790 if (!pGetAddrInfoW || !pFreeAddrInfoW)
2792 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
2793 return;
2796 memset(&hint, 0, sizeof(ADDRINFOW));
2798 ret = pGetAddrInfoW(NULL, NULL, NULL, &result);
2799 ok(ret == WSAHOST_NOT_FOUND, "got %d expected WSAHOST_NOT_FOUND\n", ret);
2801 result = NULL;
2802 ret = pGetAddrInfoW(empty, NULL, NULL, &result);
2803 todo_wine
2805 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2806 ok(result != NULL, "GetAddrInfoW failed\n");
2808 pFreeAddrInfoW(result);
2810 ret = pGetAddrInfoW(localhost, NULL, NULL, &result);
2811 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2812 pFreeAddrInfoW(result);
2814 ret = pGetAddrInfoW(localhost, port, NULL, &result);
2815 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2816 pFreeAddrInfoW(result);
2818 ret = pGetAddrInfoW(localhost, port, &hint, &result);
2819 ok(!ret, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2820 pFreeAddrInfoW(result);
2823 static void test_AcceptEx(void)
2825 SOCKET listener = INVALID_SOCKET;
2826 SOCKET acceptor = INVALID_SOCKET;
2827 SOCKET connector = INVALID_SOCKET;
2828 SOCKET connector2 = INVALID_SOCKET;
2829 struct sockaddr_in bindAddress;
2830 int socklen;
2831 GUID acceptExGuid = WSAID_ACCEPTEX;
2832 LPFN_ACCEPTEX pAcceptEx = NULL;
2833 fd_set fds_accept, fds_send;
2834 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
2835 int got, conn1, i;
2836 DWORD bytesReturned;
2837 char buffer[1024];
2838 OVERLAPPED overlapped;
2839 int iret;
2840 BOOL bret;
2841 DWORD dwret;
2843 memset(&overlapped, 0, sizeof(overlapped));
2845 listener = socket(AF_INET, SOCK_STREAM, 0);
2846 if (listener == INVALID_SOCKET) {
2847 skip("could not create listener socket, error %d\n", WSAGetLastError());
2848 goto end;
2851 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2852 if (acceptor == INVALID_SOCKET) {
2853 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2854 goto end;
2857 connector = socket(AF_INET, SOCK_STREAM, 0);
2858 if (connector == INVALID_SOCKET) {
2859 skip("could not create connector socket, error %d\n", WSAGetLastError());
2860 goto end;
2863 memset(&bindAddress, 0, sizeof(bindAddress));
2864 bindAddress.sin_family = AF_INET;
2865 bindAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
2866 iret = bind(listener, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2867 if (iret != 0) {
2868 skip("failed to bind, error %d\n", WSAGetLastError());
2869 goto end;
2872 socklen = sizeof(bindAddress);
2873 iret = getsockname(listener, (struct sockaddr*)&bindAddress, &socklen);
2874 if (iret != 0) {
2875 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2876 goto end;
2879 if (set_blocking(listener, FALSE)) {
2880 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2881 goto end;
2884 iret = WSAIoctl(listener, SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptExGuid, sizeof(acceptExGuid),
2885 &pAcceptEx, sizeof(pAcceptEx), &bytesReturned, NULL, NULL);
2886 if (iret) {
2887 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret, WSAGetLastError());
2888 goto end;
2891 bret = pAcceptEx(INVALID_SOCKET, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2892 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2893 &bytesReturned, &overlapped);
2894 ok(bret == FALSE && WSAGetLastError() == WSAENOTSOCK, "AcceptEx on invalid listening socket "
2895 "returned %d + errno %d\n", bret, WSAGetLastError());
2897 bret = pAcceptEx(listener, INVALID_SOCKET, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2898 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2899 &bytesReturned, &overlapped);
2900 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on invalid accepting socket "
2901 "returned %d + errno %d\n", bret, WSAGetLastError());
2903 bret = pAcceptEx(listener, acceptor, NULL, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2904 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2905 &bytesReturned, &overlapped);
2906 ok(bret == FALSE &&
2907 (WSAGetLastError() == WSAEINVAL ||
2908 broken(WSAGetLastError() == WSAEFAULT)), /* NT4 */
2909 "AcceptEx on NULL buffer returned %d + errno %d\n", bret, WSAGetLastError());
2911 bret = pAcceptEx(listener, acceptor, buffer, 0, 0, sizeof(struct sockaddr_in) + 16,
2912 &bytesReturned, &overlapped);
2913 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small local address size "
2914 "returned %d + errno %d\n", bret, WSAGetLastError());
2916 bret = pAcceptEx(listener, acceptor, buffer, 0, sizeof(struct sockaddr_in) + 16, 0,
2917 &bytesReturned, &overlapped);
2918 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on too small remote address size "
2919 "returned %d + errno %d\n", bret, WSAGetLastError());
2921 bret = pAcceptEx(listener, acceptor, buffer, 0,
2922 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2923 &bytesReturned, NULL);
2924 ok(bret == FALSE && WSAGetLastError() == ERROR_INVALID_PARAMETER, "AcceptEx on a NULL overlapped "
2925 "returned %d + errno %d\n", bret, WSAGetLastError());
2927 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
2928 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2929 &bytesReturned, &overlapped);
2930 ok(bret == FALSE && WSAGetLastError() == WSAEINVAL, "AcceptEx on a non-listening socket "
2931 "returned %d + errno %d\n", bret, WSAGetLastError());
2933 iret = listen(listener, 5);
2934 if (iret != 0) {
2935 skip("listening failed, errno = %d\n", WSAGetLastError());
2936 goto end;
2939 overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2940 if (overlapped.hEvent == NULL) {
2941 skip("could not create event object, errno = %d\n", GetLastError());
2942 goto end;
2945 bret = pAcceptEx(listener, acceptor, buffer, 0,
2946 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2947 &bytesReturned, &overlapped);
2948 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2950 bret = pAcceptEx(listener, acceptor, buffer, 0,
2951 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2952 &bytesReturned, &overlapped);
2953 ok((bret == FALSE && WSAGetLastError() == WSAEINVAL) || broken(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) /* NT4 */,
2954 "AcceptEx on already pending socket returned %d + errno %d\n", bret, WSAGetLastError());
2955 if (bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING) {
2956 /* We need to cancel this call, otherwise things fail */
2957 bret = CancelIo((HANDLE) listener);
2958 ok(bret, "Failed to cancel failed test. Bailing...\n");
2959 if (!bret) return;
2961 bret = pAcceptEx(listener, acceptor, buffer, 0,
2962 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2963 &bytesReturned, &overlapped);
2964 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2967 iret = connect(acceptor, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2968 ok((iret == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL) || broken(!iret) /* NT4 */,
2969 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret, WSAGetLastError());
2970 if (!iret || (iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)) {
2971 /* We need to cancel this call, otherwise things fail */
2972 closesocket(acceptor);
2973 acceptor = socket(AF_INET, SOCK_STREAM, 0);
2974 if (acceptor == INVALID_SOCKET) {
2975 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2976 goto end;
2979 bret = CancelIo((HANDLE) listener);
2980 ok(bret, "Failed to cancel failed test. Bailing...\n");
2981 if (!bret) return;
2983 bret = pAcceptEx(listener, acceptor, buffer, 0,
2984 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
2985 &bytesReturned, &overlapped);
2986 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
2989 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
2990 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2992 dwret = WaitForSingleObject(overlapped.hEvent, INFINITE);
2993 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
2995 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
2996 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
2997 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
2999 closesocket(connector);
3000 connector = INVALID_SOCKET;
3001 closesocket(acceptor);
3002 acceptor = INVALID_SOCKET;
3004 /* Test short reads */
3006 acceptor = socket(AF_INET, SOCK_STREAM, 0);
3007 if (acceptor == INVALID_SOCKET) {
3008 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3009 goto end;
3011 connector = socket(AF_INET, SOCK_STREAM, 0);
3012 if (connector == INVALID_SOCKET) {
3013 skip("could not create connector socket, error %d\n", WSAGetLastError());
3014 goto end;
3016 bret = pAcceptEx(listener, acceptor, buffer, 2,
3017 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
3018 &bytesReturned, &overlapped);
3019 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3021 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
3022 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3024 dwret = WaitForSingleObject(overlapped.hEvent, 0);
3025 ok(dwret == WAIT_TIMEOUT, "Waiting for accept event timeout failed with %d + errno %d\n", dwret, GetLastError());
3027 iret = send(connector, buffer, 1, 0);
3028 ok(iret == 1, "could not send 1 byte: send %d errno %d\n", iret, WSAGetLastError());
3030 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
3031 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
3033 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
3034 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
3035 ok(bytesReturned == 1, "bytesReturned isn't supposed to be %d\n", bytesReturned);
3037 closesocket(connector);
3038 connector = INVALID_SOCKET;
3039 closesocket(acceptor);
3040 acceptor = INVALID_SOCKET;
3042 /* Test CF_DEFER & AcceptEx interaction */
3044 acceptor = socket(AF_INET, SOCK_STREAM, 0);
3045 if (acceptor == INVALID_SOCKET) {
3046 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3047 goto end;
3049 connector = socket(AF_INET, SOCK_STREAM, 0);
3050 if (connector == INVALID_SOCKET) {
3051 skip("could not create connector socket, error %d\n", WSAGetLastError());
3052 goto end;
3054 connector2 = socket(AF_INET, SOCK_STREAM, 0);
3055 if (connector == INVALID_SOCKET) {
3056 skip("could not create connector socket, error %d\n", WSAGetLastError());
3057 goto end;
3060 if (set_blocking(connector, FALSE)) {
3061 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3062 goto end;
3065 if (set_blocking(connector2, FALSE)) {
3066 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3067 goto end;
3070 /* Connect socket #1 */
3071 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
3072 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3074 FD_ZERO ( &fds_accept );
3075 FD_ZERO ( &fds_send );
3077 FD_SET ( listener, &fds_accept );
3078 FD_SET ( connector, &fds_send );
3080 buffer[0] = '0';
3081 got = 0;
3082 conn1 = 0;
3084 for (i = 0; i < 4000; ++i)
3086 fd_set fds_openaccept = fds_accept, fds_opensend = fds_send;
3088 wsa_ok ( ( select ( 0, &fds_openaccept, &fds_opensend, NULL, &timeout ) ), SOCKET_ERROR !=,
3089 "acceptex test(%d): could not select on socket, errno %d\n" );
3091 /* check for incoming requests */
3092 if ( FD_ISSET ( listener, &fds_openaccept ) ) {
3093 got++;
3094 if (got == 1) {
3095 SOCKET tmp = WSAAccept(listener, NULL, NULL, (LPCONDITIONPROC) AlwaysDeferConditionFunc, 0);
3096 ok(tmp == INVALID_SOCKET && WSAGetLastError() == WSATRY_AGAIN, "Failed to defer connection, %d\n", WSAGetLastError());
3097 bret = pAcceptEx(listener, acceptor, buffer, 0,
3098 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
3099 &bytesReturned, &overlapped);
3100 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3102 else if (got == 2) {
3103 /* this should be socket #2 */
3104 SOCKET tmp = accept(listener, NULL, NULL);
3105 ok(tmp != INVALID_SOCKET, "accept failed %d\n", WSAGetLastError());
3106 closesocket(tmp);
3108 else {
3109 ok(FALSE, "Got more than 2 connections?\n");
3112 if ( conn1 && FD_ISSET ( connector2, &fds_opensend ) ) {
3113 /* Send data on second socket, and stop */
3114 send(connector2, "2", 1, 0);
3115 FD_CLR ( connector2, &fds_send );
3117 break;
3119 if ( FD_ISSET ( connector, &fds_opensend ) ) {
3120 /* Once #1 is connected, allow #2 to connect */
3121 conn1 = 1;
3123 send(connector, "1", 1, 0);
3124 FD_CLR ( connector, &fds_send );
3126 iret = connect(connector2, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
3127 ok(iret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3128 FD_SET ( connector2, &fds_send );
3132 ok (got == 2 || broken(got == 1) /* NT4 */,
3133 "Did not get both connections, got %d\n", got);
3135 dwret = WaitForSingleObject(overlapped.hEvent, 0);
3136 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
3138 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
3139 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
3140 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
3142 set_blocking(acceptor, TRUE);
3143 iret = recv( acceptor, buffer, 2, 0);
3144 ok(iret == 1, "Failed to get data, %d, errno: %d\n", iret, WSAGetLastError());
3146 ok(buffer[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer[0]);
3148 closesocket(connector);
3149 connector = INVALID_SOCKET;
3150 closesocket(acceptor);
3151 acceptor = INVALID_SOCKET;
3153 /* clean up in case of failures */
3154 while ((acceptor = accept(listener, NULL, NULL)) != INVALID_SOCKET)
3155 closesocket(acceptor);
3157 /* Disconnect during receive? */
3159 acceptor = socket(AF_INET, SOCK_STREAM, 0);
3160 if (acceptor == INVALID_SOCKET) {
3161 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3162 goto end;
3164 connector = socket(AF_INET, SOCK_STREAM, 0);
3165 if (connector == INVALID_SOCKET) {
3166 skip("could not create connector socket, error %d\n", WSAGetLastError());
3167 goto end;
3169 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
3170 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
3171 &bytesReturned, &overlapped);
3172 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3174 iret = connect(connector, (struct sockaddr*)&bindAddress, sizeof(bindAddress));
3175 ok(iret == 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3177 closesocket(connector);
3178 connector = INVALID_SOCKET;
3180 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
3181 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
3183 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
3184 ok(bret, "GetOverlappedResult failed, error %d\n", GetLastError());
3185 ok(bytesReturned == 0, "bytesReturned isn't supposed to be %d\n", bytesReturned);
3187 closesocket(acceptor);
3188 acceptor = INVALID_SOCKET;
3190 /* Test closing with pending requests */
3192 acceptor = socket(AF_INET, SOCK_STREAM, 0);
3193 if (acceptor == INVALID_SOCKET) {
3194 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3195 goto end;
3197 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
3198 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
3199 &bytesReturned, &overlapped);
3200 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3202 closesocket(acceptor);
3203 acceptor = INVALID_SOCKET;
3205 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
3206 ok(dwret == WAIT_OBJECT_0 || broken(dwret == WAIT_TIMEOUT) /* NT4/2000 */,
3207 "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
3209 if (dwret != WAIT_TIMEOUT) {
3210 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
3211 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
3213 else {
3214 bret = CancelIo((HANDLE) listener);
3215 ok(bret, "Failed to cancel failed test. Bailing...\n");
3216 if (!bret) return;
3219 acceptor = socket(AF_INET, SOCK_STREAM, 0);
3220 if (acceptor == INVALID_SOCKET) {
3221 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3222 goto end;
3224 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
3225 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
3226 &bytesReturned, &overlapped);
3227 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3229 CancelIo((HANDLE) acceptor);
3231 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
3232 ok(dwret == WAIT_TIMEOUT, "Waiting for timeout failed with %d + errno %d\n", dwret, GetLastError());
3234 closesocket(acceptor);
3235 acceptor = INVALID_SOCKET;
3237 acceptor = socket(AF_INET, SOCK_STREAM, 0);
3238 if (acceptor == INVALID_SOCKET) {
3239 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3240 goto end;
3242 bret = pAcceptEx(listener, acceptor, buffer, sizeof(buffer) - 2*(sizeof(struct sockaddr_in) + 16),
3243 sizeof(struct sockaddr_in) + 16, sizeof(struct sockaddr_in) + 16,
3244 &bytesReturned, &overlapped);
3245 ok(bret == FALSE && WSAGetLastError() == ERROR_IO_PENDING, "AcceptEx returned %d + errno %d\n", bret, WSAGetLastError());
3247 closesocket(listener);
3248 listener = INVALID_SOCKET;
3250 dwret = WaitForSingleObject(overlapped.hEvent, 1000);
3251 ok(dwret == WAIT_OBJECT_0, "Waiting for accept event failed with %d + errno %d\n", dwret, GetLastError());
3253 bret = GetOverlappedResult((HANDLE)listener, &overlapped, &bytesReturned, FALSE);
3254 ok(!bret && GetLastError() == ERROR_OPERATION_ABORTED, "GetOverlappedResult failed, error %d\n", GetLastError());
3256 end:
3257 if (overlapped.hEvent)
3258 WSACloseEvent(overlapped.hEvent);
3259 if (listener != INVALID_SOCKET)
3260 closesocket(listener);
3261 if (acceptor != INVALID_SOCKET)
3262 closesocket(acceptor);
3263 if (connector != INVALID_SOCKET)
3264 closesocket(connector);
3265 if (connector2 != INVALID_SOCKET)
3266 closesocket(connector2);
3269 static void test_getpeername(void)
3271 SOCKET sock;
3272 struct sockaddr_in sa, sa_out;
3273 int sa_len;
3274 const char buf[] = "hello world";
3275 int ret;
3277 /* Test the parameter validation order. */
3278 ret = getpeername(INVALID_SOCKET, NULL, NULL);
3279 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
3280 ok(WSAGetLastError() == WSAENOTSOCK,
3281 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
3283 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
3284 ok(sock != INVALID_SOCKET, "Expected socket to return a valid socket\n");
3285 if (sock == INVALID_SOCKET)
3287 skip("Socket creation failed with %d\n", WSAGetLastError());
3288 return;
3291 ret = getpeername(sock, NULL, NULL);
3292 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
3293 ok(WSAGetLastError() == WSAENOTCONN ||
3294 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
3295 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
3297 memset(&sa, 0, sizeof(sa));
3298 sa.sin_family = AF_INET;
3299 sa.sin_port = htons(139);
3300 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
3302 /* sendto does not change a socket's connection state. */
3303 ret = sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&sa, sizeof(sa));
3304 ok(ret != SOCKET_ERROR,
3305 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
3307 ret = getpeername(sock, NULL, NULL);
3308 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
3309 ok(WSAGetLastError() == WSAENOTCONN ||
3310 broken(WSAGetLastError() == WSAEFAULT), /* Win9x and WinMe */
3311 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
3313 ret = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
3314 ok(ret == 0,
3315 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
3317 ret = getpeername(sock, NULL, NULL);
3318 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
3319 ok(WSAGetLastError() == WSAEFAULT,
3320 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
3322 /* Test crashes on Wine. */
3323 if (0)
3325 ret = getpeername(sock, (void*)0xdeadbeef, (void*)0xcafebabe);
3326 ok(ret == SOCKET_ERROR, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret);
3327 ok(WSAGetLastError() == WSAEFAULT,
3328 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
3331 sa_len = 0;
3332 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
3333 ok(ret == SOCKET_ERROR, "Expected getpeername to return 0, got %d\n", ret);
3334 ok(WSAGetLastError() == WSAEFAULT,
3335 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
3337 sa_len = sizeof(sa_out);
3338 ret = getpeername(sock, (struct sockaddr*)&sa_out, &sa_len);
3339 ok(ret == 0, "Expected getpeername to return 0, got %d\n", ret);
3340 ok(!memcmp(&sa, &sa_out, sizeof(sa)),
3341 "Expected the returned structure to be identical to the connect structure\n");
3343 closesocket(sock);
3346 /**************** Main program ***************/
3348 START_TEST( sock )
3350 int i;
3352 /* Leave these tests at the beginning. They depend on WSAStartup not having been
3353 * called, which is done by Init() below. */
3354 test_WithoutWSAStartup();
3355 test_WithWSAStartup();
3357 Init();
3359 test_set_getsockopt();
3360 test_so_reuseaddr();
3361 test_extendedSocketOptions();
3363 for (i = 0; i < NUM_TESTS; i++)
3365 trace ( " **** STARTING TEST %d ****\n", i );
3366 do_test ( &tests[i] );
3367 trace ( " **** TEST %d COMPLETE ****\n", i );
3370 test_UDP();
3372 test_getservbyname();
3373 test_WSASocket();
3375 test_WSAAddressToStringA();
3376 test_WSAAddressToStringW();
3378 test_WSAStringToAddressA();
3379 test_WSAStringToAddressW();
3381 test_select();
3382 test_accept();
3383 test_getpeername();
3384 test_getsockname();
3385 test_inet_addr();
3386 test_addr_to_print();
3387 test_ioctlsocket();
3388 test_dns();
3389 test_gethostbyname_hack();
3391 test_send();
3392 test_write_events();
3394 test_WSASendTo();
3396 test_ipv6only();
3397 test_GetAddrInfoW();
3399 test_AcceptEx();
3401 Exit();