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
24 #define WIN32_NO_STATUS
29 #include "wine/test.h"
31 #define MAX_CLIENTS 4 /* Max number of clients */
32 #define NUM_TESTS 4 /* Number of tests performed */
33 #define FIRST_CHAR 'A' /* First character in transferred pattern */
34 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
35 #define BIND_TRIES 6 /* Number of bind() attempts */
36 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
37 after server initialization, if something hangs */
39 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
41 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
42 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
44 #define SERVERIP "127.0.0.1" /* IP to bind to */
45 #define SERVERPORT 9374 /* Port number to bind to */
47 #define wsa_ok(op, cond, msg) \
51 if ( !(cond tmp) ) err = WSAGetLastError(); \
52 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
55 /* Function pointers */
56 static void (WINAPI
*pFreeAddrInfoW
)(PADDRINFOW
) = 0;
57 static int (WINAPI
*pGetAddrInfoW
)(LPCWSTR
,LPCWSTR
,const ADDRINFOW
*,PADDRINFOW
*) = 0;
58 static PCSTR (WINAPI
*pInetNtop
)(INT
,LPVOID
,LPSTR
,ULONG
) = 0;
60 /**************** Structs and typedefs ***************/
62 typedef struct thread_info
68 /* Information in the server about open client connections */
69 typedef struct sock_info
72 struct sockaddr_in addr
;
73 struct sockaddr_in peer
;
79 /* Test parameters for both server & client */
80 typedef struct test_params
84 const char *inet_addr
;
91 /* server-specific test parameters */
92 typedef struct server_params
99 /* client-specific test parameters */
100 typedef struct client_params
102 test_params
*general
;
107 /* This type combines all information for setting up a test scenario */
108 typedef struct test_setup
112 server_params srv_params
;
114 client_params clt_params
;
117 /* Thread local storage for server */
118 typedef struct server_memory
121 struct sockaddr_in addr
;
122 sock_info sock
[MAX_CLIENTS
];
125 /* Thread local storage for client */
126 typedef struct client_memory
129 struct sockaddr_in addr
;
134 /* SelectReadThread thread parameters */
135 typedef struct select_thread_params
139 } select_thread_params
;
141 /**************** Static variables ***************/
143 static DWORD tls
; /* Thread local storage index */
144 static HANDLE thread
[1+MAX_CLIENTS
];
145 static DWORD thread_id
[1+MAX_CLIENTS
];
146 static HANDLE server_ready
;
147 static HANDLE client_ready
[MAX_CLIENTS
];
148 static int client_id
;
150 /**************** General utility functions ***************/
152 static int tcp_socketpair(SOCKET
*src
, SOCKET
*dst
)
154 SOCKET server
= INVALID_SOCKET
;
155 struct sockaddr_in addr
;
159 *src
= INVALID_SOCKET
;
160 *dst
= INVALID_SOCKET
;
162 *src
= socket(AF_INET
, SOCK_STREAM
, 0);
163 if (*src
== INVALID_SOCKET
)
166 server
= socket(AF_INET
, SOCK_STREAM
, 0);
167 if (server
== INVALID_SOCKET
)
170 memset(&addr
, 0, sizeof(addr
));
171 addr
.sin_family
= AF_INET
;
172 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
173 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
178 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
182 ret
= listen(server
, 1);
186 ret
= connect(*src
, (struct sockaddr
*)&addr
, sizeof(addr
));
191 *dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
194 if (server
!= INVALID_SOCKET
)
196 if (*src
!= INVALID_SOCKET
&& *dst
!= INVALID_SOCKET
)
203 static void set_so_opentype ( BOOL overlapped
)
205 int optval
= !overlapped
, newval
, len
= sizeof (int);
207 ok ( setsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
208 (LPVOID
) &optval
, sizeof (optval
) ) == 0,
209 "setting SO_OPENTYPE failed\n" );
210 ok ( getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
211 (LPVOID
) &newval
, &len
) == 0,
212 "getting SO_OPENTYPE failed\n" );
213 ok ( optval
== newval
, "failed to set SO_OPENTYPE\n" );
216 static int set_blocking ( SOCKET s
, BOOL blocking
)
218 u_long val
= !blocking
;
219 return ioctlsocket ( s
, FIONBIO
, &val
);
222 static void fill_buffer ( char *buf
, int chunk_size
, int n_chunks
)
225 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
226 memset ( p
, c
, chunk_size
);
229 static int test_buffer ( char *buf
, int chunk_size
, int n_chunks
)
233 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
235 for ( i
= 0; i
< chunk_size
; i
++ )
236 if ( p
[i
] != c
) return i
;
242 * This routine is called when a client / server does not expect any more data,
243 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
245 static void read_zero_bytes ( SOCKET s
)
249 while ( ( tmp
= recv ( s
, buf
, 256, 0 ) ) > 0 )
251 ok ( n
<= 0, "garbage data received: %d bytes\n", n
);
254 static int do_synchronous_send ( SOCKET s
, char *buf
, int buflen
, int sendlen
)
256 char* last
= buf
+ buflen
, *p
;
258 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
259 n
= send ( s
, p
, min ( sendlen
, last
- p
), 0 );
260 wsa_ok ( n
, 0 <=, "do_synchronous_send (%x): error %d\n" );
264 static int do_synchronous_recv ( SOCKET s
, char *buf
, int buflen
, int recvlen
)
266 char* last
= buf
+ buflen
, *p
;
268 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
269 n
= recv ( s
, p
, min ( recvlen
, last
- p
), 0 );
270 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
274 static int do_synchronous_recvfrom ( SOCKET s
, char *buf
, int buflen
,int flags
,struct sockaddr
*from
, int *fromlen
, int recvlen
)
276 char* last
= buf
+ buflen
, *p
;
278 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
279 n
= recvfrom ( s
, p
, min ( recvlen
, last
- p
), 0, from
, fromlen
);
280 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
285 * Call this routine right after thread startup.
286 * SO_OPENTYPE must by 0, regardless what the server did.
288 static void check_so_opentype (void)
292 getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (LPVOID
) &tmp
, &len
);
293 ok ( tmp
== 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp
);
296 /**************** Server utility functions ***************/
299 * Even if we have closed our server socket cleanly,
300 * the OS may mark the address "in use" for some time -
301 * this happens with native Linux apps, too.
303 static void do_bind ( SOCKET s
, struct sockaddr
* addr
, int addrlen
)
305 int err
, wsaerr
= 0, n_try
= BIND_TRIES
;
307 while ( ( err
= bind ( s
, addr
, addrlen
) ) != 0 &&
308 ( wsaerr
= WSAGetLastError () ) == WSAEADDRINUSE
&&
311 trace ( "address in use, waiting ...\n" );
312 Sleep ( 1000 * BIND_SLEEP
);
314 ok ( err
== 0, "failed to bind: %d\n", wsaerr
);
317 static void server_start ( server_params
*par
)
320 test_params
*gen
= par
->general
;
321 server_memory
*mem
= LocalAlloc ( LPTR
, sizeof ( server_memory
) );
323 TlsSetValue ( tls
, mem
);
324 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
325 NULL
, 0, par
->sock_flags
);
326 ok ( mem
->s
!= INVALID_SOCKET
, "Server: WSASocket failed\n" );
328 mem
->addr
.sin_family
= AF_INET
;
329 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
330 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
332 for (i
= 0; i
< MAX_CLIENTS
; i
++)
334 mem
->sock
[i
].s
= INVALID_SOCKET
;
335 mem
->sock
[i
].buf
= LocalAlloc ( LPTR
, gen
->n_chunks
* gen
->chunk_size
);
336 mem
->sock
[i
].n_recvd
= 0;
337 mem
->sock
[i
].n_sent
= 0;
340 if ( gen
->sock_type
== SOCK_STREAM
)
341 do_bind ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof (mem
->addr
) );
344 static void server_stop (void)
347 server_memory
*mem
= TlsGetValue ( tls
);
349 for (i
= 0; i
< MAX_CLIENTS
; i
++ )
351 LocalFree ( mem
->sock
[i
].buf
);
352 if ( mem
->sock
[i
].s
!= INVALID_SOCKET
)
353 closesocket ( mem
->sock
[i
].s
);
355 ok ( closesocket ( mem
->s
) == 0, "closesocket failed\n" );
357 ExitThread ( GetCurrentThreadId () );
360 /**************** Client utilitiy functions ***************/
362 static void client_start ( client_params
*par
)
364 test_params
*gen
= par
->general
;
365 client_memory
*mem
= LocalAlloc (LPTR
, sizeof (client_memory
));
367 TlsSetValue ( tls
, mem
);
369 WaitForSingleObject ( server_ready
, INFINITE
);
371 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
372 NULL
, 0, par
->sock_flags
);
374 mem
->addr
.sin_family
= AF_INET
;
375 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
376 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
378 ok ( mem
->s
!= INVALID_SOCKET
, "Client: WSASocket failed\n" );
380 mem
->send_buf
= LocalAlloc ( LPTR
, 2 * gen
->n_chunks
* gen
->chunk_size
);
381 mem
->recv_buf
= mem
->send_buf
+ gen
->n_chunks
* gen
->chunk_size
;
382 fill_buffer ( mem
->send_buf
, gen
->chunk_size
, gen
->n_chunks
);
384 SetEvent ( client_ready
[client_id
] );
385 /* Wait for the other clients to come up */
386 WaitForMultipleObjects ( min ( gen
->n_clients
, MAX_CLIENTS
), client_ready
, TRUE
, INFINITE
);
389 static void client_stop (void)
391 client_memory
*mem
= TlsGetValue ( tls
);
392 wsa_ok ( closesocket ( mem
->s
), 0 ==, "closesocket error (%x): %d\n" );
393 LocalFree ( mem
->send_buf
);
398 /**************** Servers ***************/
401 * simple_server: A very basic server doing synchronous IO.
403 static VOID WINAPI
simple_server ( server_params
*par
)
405 test_params
*gen
= par
->general
;
407 int pos
, n_recvd
, n_sent
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
408 id
= GetCurrentThreadId();
410 trace ( "simple_server (%x) starting\n", id
);
412 set_so_opentype ( FALSE
); /* non-overlapped */
413 server_start ( par
);
414 mem
= TlsGetValue ( tls
);
416 wsa_ok ( set_blocking ( mem
->s
, TRUE
), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
417 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "simple_server (%x): listen failed: %d\n");
419 trace ( "simple_server (%x) ready\n", id
);
420 SetEvent ( server_ready
); /* notify clients */
422 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
424 trace ( "simple_server (%x): waiting for client\n", id
);
426 /* accept a single connection */
427 tmp
= sizeof ( mem
->sock
[0].peer
);
428 mem
->sock
[0].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[0].peer
, &tmp
);
429 wsa_ok ( mem
->sock
[0].s
, INVALID_SOCKET
!=, "simple_server (%x): accept failed: %d\n" );
431 ok ( mem
->sock
[0].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
432 "simple_server (%x): strange peer address\n", id
);
434 /* Receive data & check it */
435 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
436 ok ( n_recvd
== n_expected
,
437 "simple_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
438 pos
= test_buffer ( mem
->sock
[0].buf
, gen
->chunk_size
, gen
->n_chunks
);
439 ok ( pos
== -1, "simple_server (%x): test pattern error: %d\n", id
, pos
);
442 n_sent
= do_synchronous_send ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
443 ok ( n_sent
== n_expected
,
444 "simple_server (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
447 read_zero_bytes ( mem
->sock
[0].s
);
448 wsa_ok ( closesocket ( mem
->sock
[0].s
), 0 ==, "simple_server (%x): closesocket error: %d\n" );
449 mem
->sock
[0].s
= INVALID_SOCKET
;
452 trace ( "simple_server (%x) exiting\n", id
);
457 * select_server: A non-blocking server.
459 static VOID WINAPI
select_server ( server_params
*par
)
461 test_params
*gen
= par
->general
;
463 int n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
464 id
= GetCurrentThreadId(), n_connections
= 0, n_sent
, n_recvd
,
465 n_set
, delta
, n_ready
;
466 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
467 fd_set fds_recv
, fds_send
, fds_openrecv
, fds_opensend
;
469 trace ( "select_server (%x) starting\n", id
);
471 set_so_opentype ( FALSE
); /* non-overlapped */
472 server_start ( par
);
473 mem
= TlsGetValue ( tls
);
475 wsa_ok ( set_blocking ( mem
->s
, FALSE
), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
476 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "select_server (%x): listen failed: %d\n");
478 trace ( "select_server (%x) ready\n", id
);
479 SetEvent ( server_ready
); /* notify clients */
481 FD_ZERO ( &fds_openrecv
);
482 FD_ZERO ( &fds_recv
);
483 FD_ZERO ( &fds_send
);
484 FD_ZERO ( &fds_opensend
);
486 FD_SET ( mem
->s
, &fds_openrecv
);
490 fds_recv
= fds_openrecv
;
491 fds_send
= fds_opensend
;
495 wsa_ok ( ( n_ready
= select ( 0, &fds_recv
, &fds_send
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
496 "select_server (%x): select() failed: %d\n" );
498 /* check for incoming requests */
499 if ( FD_ISSET ( mem
->s
, &fds_recv
) ) {
502 trace ( "select_server (%x): accepting client connection\n", id
);
504 /* accept a single connection */
505 tmp
= sizeof ( mem
->sock
[n_connections
].peer
);
506 mem
->sock
[n_connections
].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[n_connections
].peer
, &tmp
);
507 wsa_ok ( mem
->sock
[n_connections
].s
, INVALID_SOCKET
!=, "select_server (%x): accept() failed: %d\n" );
509 ok ( mem
->sock
[n_connections
].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
510 "select_server (%x): strange peer address\n", id
);
512 /* add to list of open connections */
513 FD_SET ( mem
->sock
[n_connections
].s
, &fds_openrecv
);
514 FD_SET ( mem
->sock
[n_connections
].s
, &fds_opensend
);
519 /* handle open requests */
521 for ( i
= 0; i
< n_connections
; i
++ )
523 if ( FD_ISSET( mem
->sock
[i
].s
, &fds_recv
) ) {
526 if ( mem
->sock
[i
].n_recvd
< n_expected
) {
527 /* Receive data & check it */
528 n_recvd
= recv ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_recvd
, min ( n_expected
- mem
->sock
[i
].n_recvd
, par
->buflen
), 0 );
529 ok ( n_recvd
!= SOCKET_ERROR
, "select_server (%x): error in recv(): %d\n", id
, WSAGetLastError() );
530 mem
->sock
[i
].n_recvd
+= n_recvd
;
532 if ( mem
->sock
[i
].n_recvd
== n_expected
) {
533 int pos
= test_buffer ( mem
->sock
[i
].buf
, gen
->chunk_size
, gen
->n_chunks
);
534 ok ( pos
== -1, "select_server (%x): test pattern error: %d\n", id
, pos
);
535 FD_CLR ( mem
->sock
[i
].s
, &fds_openrecv
);
538 ok ( mem
->sock
[i
].n_recvd
<= n_expected
, "select_server (%x): received too many bytes: %d\n", id
, mem
->sock
[i
].n_recvd
);
542 /* only echo back what we've received */
543 delta
= mem
->sock
[i
].n_recvd
- mem
->sock
[i
].n_sent
;
545 if ( FD_ISSET ( mem
->sock
[i
].s
, &fds_send
) ) {
548 if ( ( delta
> 0 ) && ( mem
->sock
[i
].n_sent
< n_expected
) ) {
550 n_sent
= send ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_sent
, min ( delta
, par
->buflen
), 0 );
551 ok ( n_sent
!= SOCKET_ERROR
, "select_server (%x): error in send(): %d\n", id
, WSAGetLastError() );
552 mem
->sock
[i
].n_sent
+= n_sent
;
554 if ( mem
->sock
[i
].n_sent
== n_expected
) {
555 FD_CLR ( mem
->sock
[i
].s
, &fds_opensend
);
558 ok ( mem
->sock
[i
].n_sent
<= n_expected
, "select_server (%x): sent too many bytes: %d\n", id
, mem
->sock
[i
].n_sent
);
563 /* check that select returned the correct number of ready sockets */
564 ok ( ( n_set
== n_ready
), "select_server (%x): select() returns wrong number of ready sockets\n", id
);
566 /* check if all clients are done */
567 if ( ( fds_opensend
.fd_count
== 0 )
568 && ( fds_openrecv
.fd_count
== 1 ) /* initial socket that accepts clients */
569 && ( n_connections
== min ( gen
->n_clients
, MAX_CLIENTS
) ) ) {
574 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
577 read_zero_bytes ( mem
->sock
[i
].s
);
578 wsa_ok ( closesocket ( mem
->sock
[i
].s
), 0 ==, "select_server (%x): closesocket error: %d\n" );
579 mem
->sock
[i
].s
= INVALID_SOCKET
;
582 trace ( "select_server (%x) exiting\n", id
);
586 /**************** Clients ***************/
589 * simple_client: A very basic client doing synchronous IO.
591 static VOID WINAPI
simple_client ( client_params
*par
)
593 test_params
*gen
= par
->general
;
595 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
597 id
= GetCurrentThreadId();
598 trace ( "simple_client (%x): starting\n", id
);
599 /* wait here because we want to call set_so_opentype before creating a socket */
600 WaitForSingleObject ( server_ready
, INFINITE
);
601 trace ( "simple_client (%x): server ready\n", id
);
603 check_so_opentype ();
604 set_so_opentype ( FALSE
); /* non-overlapped */
605 client_start ( par
);
606 mem
= TlsGetValue ( tls
);
609 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
610 0 ==, "simple_client (%x): connect error: %d\n" );
611 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
612 "simple_client (%x): failed to set blocking mode\n", id
);
613 trace ( "simple_client (%x) connected\n", id
);
615 /* send data to server */
616 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
617 ok ( n_sent
== n_expected
,
618 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
620 /* shutdown send direction */
621 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
623 /* Receive data echoed back & check it */
624 n_recvd
= do_synchronous_recv ( mem
->s
, mem
->recv_buf
, n_expected
, par
->buflen
);
625 ok ( n_recvd
== n_expected
,
626 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
629 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
630 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
633 read_zero_bytes ( mem
->s
);
634 trace ( "simple_client (%x) exiting\n", id
);
639 * simple_mixed_client: mixing send and recvfrom
641 static VOID WINAPI
simple_mixed_client ( client_params
*par
)
643 test_params
*gen
= par
->general
;
645 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
646 int fromLen
= sizeof(mem
->addr
);
647 struct sockaddr test
;
649 id
= GetCurrentThreadId();
650 trace ( "simple_client (%x): starting\n", id
);
651 /* wait here because we want to call set_so_opentype before creating a socket */
652 WaitForSingleObject ( server_ready
, INFINITE
);
653 trace ( "simple_client (%x): server ready\n", id
);
655 check_so_opentype ();
656 set_so_opentype ( FALSE
); /* non-overlapped */
657 client_start ( par
);
658 mem
= TlsGetValue ( tls
);
661 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
662 0 ==, "simple_client (%x): connect error: %d\n" );
663 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
664 "simple_client (%x): failed to set blocking mode\n", id
);
665 trace ( "simple_client (%x) connected\n", id
);
667 /* send data to server */
668 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
669 ok ( n_sent
== n_expected
,
670 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
672 /* shutdown send direction */
673 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
675 /* this shouldn't change, since lpFrom, is not updated on
676 connection oriented sockets - exposed by bug 11640
678 ((struct sockaddr_in
*)&test
)->sin_addr
.s_addr
= inet_addr("0.0.0.0");
680 /* Receive data echoed back & check it */
681 n_recvd
= do_synchronous_recvfrom ( mem
->s
,
685 (struct sockaddr
*)&test
,
688 ok ( n_recvd
== n_expected
,
689 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
691 /* check that lpFrom was not updated */
694 inet_ntoa(((struct sockaddr_in
*)&test
)->sin_addr
),
695 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
698 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
699 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
702 read_zero_bytes ( mem
->s
);
703 trace ( "simple_client (%x) exiting\n", id
);
708 * event_client: An event-driven client
710 static void WINAPI
event_client ( client_params
*par
)
712 test_params
*gen
= par
->general
;
714 int id
= GetCurrentThreadId(), n_expected
= gen
->n_chunks
* gen
->chunk_size
,
717 WSANETWORKEVENTS wsa_events
;
718 char *send_last
, *recv_last
, *send_p
, *recv_p
;
719 LONG mask
= FD_READ
| FD_WRITE
| FD_CLOSE
;
721 trace ( "event_client (%x): starting\n", id
);
722 client_start ( par
);
723 trace ( "event_client (%x): server ready\n", id
);
725 mem
= TlsGetValue ( tls
);
727 /* Prepare event notification for connect, makes socket nonblocking */
728 event
= WSACreateEvent ();
729 WSAEventSelect ( mem
->s
, event
, FD_CONNECT
);
730 tmp
= connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) );
732 err
= WSAGetLastError ();
733 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): connect error: %d\n", id
, err
);
734 tmp
= WaitForSingleObject ( event
, INFINITE
);
735 ok ( tmp
== WAIT_OBJECT_0
, "event_client (%x): wait for connect event failed: %d\n", id
, tmp
);
736 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
737 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
738 err
= wsa_events
.iErrorCode
[ FD_CONNECT_BIT
];
739 ok ( err
== 0, "event_client (%x): connect error: %d\n", id
, err
);
743 trace ( "event_client (%x) connected\n", id
);
745 WSAEventSelect ( mem
->s
, event
, mask
);
747 recv_p
= mem
->recv_buf
;
748 recv_last
= mem
->recv_buf
+ n_expected
;
749 send_p
= mem
->send_buf
;
750 send_last
= mem
->send_buf
+ n_expected
;
754 err
= WaitForSingleObject ( event
, INFINITE
);
755 ok ( err
== WAIT_OBJECT_0
, "event_client (%x): wait failed\n", id
);
757 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
758 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
760 if ( wsa_events
.lNetworkEvents
& FD_WRITE
)
762 err
= wsa_events
.iErrorCode
[ FD_WRITE_BIT
];
763 ok ( err
== 0, "event_client (%x): FD_WRITE error code: %d\n", id
, err
);
768 n
= send ( mem
->s
, send_p
, min ( send_last
- send_p
, par
->buflen
), 0 );
771 err
= WSAGetLastError ();
772 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): send error: %d\n", id
, err
);
777 while ( n
>= 0 && send_p
< send_last
);
779 if ( send_p
== send_last
)
781 trace ( "event_client (%x): all data sent - shutdown\n", id
);
782 shutdown ( mem
->s
, SD_SEND
);
784 WSAEventSelect ( mem
->s
, event
, mask
);
787 if ( wsa_events
.lNetworkEvents
& FD_READ
)
789 err
= wsa_events
.iErrorCode
[ FD_READ_BIT
];
790 ok ( err
== 0, "event_client (%x): FD_READ error code: %d\n", id
, err
);
791 if ( err
!= 0 ) break;
793 /* First read must succeed */
794 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
795 wsa_ok ( n
, 0 <=, "event_client (%x): recv error: %d\n" );
799 if ( recv_p
== recv_last
)
802 trace ( "event_client (%x): all data received\n", id
);
803 WSAEventSelect ( mem
->s
, event
, mask
);
806 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
807 if ( n
< 0 && ( err
= WSAGetLastError()) != WSAEWOULDBLOCK
)
808 ok ( 0, "event_client (%x): read error: %d\n", id
, err
);
812 if ( wsa_events
.lNetworkEvents
& FD_CLOSE
)
814 trace ( "event_client (%x): close event\n", id
);
815 err
= wsa_events
.iErrorCode
[ FD_CLOSE_BIT
];
816 ok ( err
== 0, "event_client (%x): FD_CLOSE error code: %d\n", id
, err
);
821 n
= send_p
- mem
->send_buf
;
822 ok ( send_p
== send_last
,
823 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n
, n_expected
);
824 n
= recv_p
- mem
->recv_buf
;
825 ok ( recv_p
== recv_last
,
826 "simple_client (%x): received less data than expected: %d of %d\n", id
, n
, n_expected
);
827 n
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
828 ok ( n
== -1, "event_client (%x): test pattern error: %d\n", id
, n
);
831 WSACloseEvent ( event
);
832 trace ( "event_client (%x) exiting\n", id
);
836 /* Tests for WSAStartup */
838 /* This should fail. WSAStartup should be called before any network function is used. */
839 static void test_WithoutWSAStartup(void)
843 WSASetLastError(0xdeadbeef);
844 ptr
= gethostbyname("localhost");
846 todo_wine
ok(ptr
== NULL
, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
847 todo_wine
ok(WSAGetLastError() == WSANOTINITIALISED
, "gethostbyname() failed with unexpected error: %d\n",
851 static void test_WithWSAStartup(void)
854 WORD version
= MAKEWORD( 2, 2 );
858 res
= WSAStartup( version
, &data
);
859 ok(res
== 0, "WSAStartup() failed unexpectedly: %d\n", res
);
861 ptr
= gethostbyname("localhost");
862 ok(ptr
!= NULL
, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
867 /**************** Main program utility functions ***************/
869 static void Init (void)
871 WORD ver
= MAKEWORD (2, 2);
873 HMODULE hws2_32
= GetModuleHandle("ws2_32.dll");
875 pFreeAddrInfoW
= (void *)GetProcAddress(hws2_32
, "FreeAddrInfoW");
876 pGetAddrInfoW
= (void *)GetProcAddress(hws2_32
, "GetAddrInfoW");
877 pInetNtop
= (void *)GetProcAddress(hws2_32
, "inet_ntop");
879 ok ( WSAStartup ( ver
, &data
) == 0, "WSAStartup failed\n" );
883 static void Exit (void)
888 err
= WSAGetLastError();
889 ok ( ret
== 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret
, err
);
891 err
= WSAGetLastError();
892 ok ( (ret
== SOCKET_ERROR
&& err
== WSANOTINITIALISED
) ||
893 broken(ret
== 0), /* WinME */
894 "WSACleanup returned %d GetLastError is %d\n", ret
, err
);
897 static void StartServer (LPTHREAD_START_ROUTINE routine
,
898 test_params
*general
, server_params
*par
)
900 par
->general
= general
;
901 thread
[0] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[0] );
902 ok ( thread
[0] != NULL
, "Failed to create server thread\n" );
905 static void StartClients (LPTHREAD_START_ROUTINE routine
,
906 test_params
*general
, client_params
*par
)
909 par
->general
= general
;
910 for ( i
= 1; i
<= min ( general
->n_clients
, MAX_CLIENTS
); i
++ )
913 thread
[i
] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[i
] );
914 ok ( thread
[i
] != NULL
, "Failed to create client thread\n" );
915 /* Make sure the client is up and running */
916 WaitForSingleObject ( client_ready
[client_id
], INFINITE
);
920 static void do_test( test_setup
*test
)
922 DWORD i
, n
= min (test
->general
.n_clients
, MAX_CLIENTS
);
925 server_ready
= CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
926 for (i
= 0; i
<= n
; i
++)
927 client_ready
[i
] = CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
929 StartServer ( test
->srv
, &test
->general
, &test
->srv_params
);
930 StartClients ( test
->clt
, &test
->general
, &test
->clt_params
);
931 WaitForSingleObject ( server_ready
, INFINITE
);
933 wait
= WaitForMultipleObjects ( 1 + n
, thread
, TRUE
, 1000 * TEST_TIMEOUT
);
934 ok ( wait
<= WAIT_OBJECT_0
+ n
,
935 "some threads have not completed: %x\n", wait
);
937 if ( ! ( wait
<= WAIT_OBJECT_0
+ n
) )
939 for (i
= 0; i
<= n
; i
++)
941 trace ("terminating thread %08x\n", thread_id
[i
]);
942 if ( WaitForSingleObject ( thread
[i
], 0 ) != WAIT_OBJECT_0
)
943 TerminateThread ( thread
[i
], 0 );
946 CloseHandle ( server_ready
);
947 for (i
= 0; i
<= n
; i
++)
948 CloseHandle ( client_ready
[i
] );
951 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
952 /* optname = SO_LINGER */
953 LINGER linger_testvals
[] = {
960 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
961 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
962 bug in the linux kernel (fixed in 2.6.8) */
963 #define SOCKTIMEOUT2 997000 /* 997 seconds */
965 static void test_set_getsockopt(void)
973 s
= socket(AF_INET
, SOCK_STREAM
, 0);
974 ok(s
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
975 if( s
== INVALID_SOCKET
) return;
977 timeout
= SOCKTIMEOUT1
;
978 size
= sizeof(timeout
);
979 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
981 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
982 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
983 ok( timeout
== SOCKTIMEOUT1
, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
986 size
= sizeof(timeout
);
987 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
989 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
990 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
991 ok( timeout
== 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
994 timeout
= SOCKTIMEOUT2
; /* 997 seconds. See remark above */
995 size
= sizeof(timeout
);
996 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
998 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
999 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1000 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
1002 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
1003 size
= sizeof(lingval
);
1004 lingval
= linger_testvals
[i
];
1005 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
1007 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
1008 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1009 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
1010 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
1011 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
1012 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
1013 lingval
.l_onoff
, lingval
.l_linger
,
1014 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
1016 /* Test for erroneously passing a value instead of a pointer as optval */
1017 size
= sizeof(char);
1018 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTROUTE
, (char *)1, size
);
1019 ok(err
== SOCKET_ERROR
, "setsockopt with optval being a value passed "
1020 "instead of failing.\n");
1021 lasterr
= WSAGetLastError();
1022 ok(lasterr
== WSAEFAULT
, "setsockopt with optval being a value "
1023 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1024 lasterr
, WSAEFAULT
);
1026 /* SO_RCVTIMEO with invalid values for level */
1027 size
= sizeof(timeout
);
1028 timeout
= SOCKTIMEOUT1
;
1029 SetLastError(0xdeadbeef);
1030 err
= setsockopt(s
, 0xffffffff, SO_RCVTIMEO
, (char *) &timeout
, size
);
1031 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1032 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1033 err
, WSAGetLastError());
1035 timeout
= SOCKTIMEOUT1
;
1036 SetLastError(0xdeadbeef);
1037 err
= setsockopt(s
, 0x00008000, SO_RCVTIMEO
, (char *) &timeout
, size
);
1038 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1039 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1040 err
, WSAGetLastError());
1045 static void test_so_reuseaddr(void)
1047 struct sockaddr_in saddr
;
1049 unsigned int rc
,reuse
;
1052 saddr
.sin_family
= AF_INET
;
1053 saddr
.sin_port
= htons(9375);
1054 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1056 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
1057 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1058 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1059 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
1061 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
1062 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1066 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
1067 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
1069 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1070 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
1073 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
1074 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1076 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1077 * a port immediately after closing another socket on that port, so
1078 * basically following the BSD socket semantics here. */
1080 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1081 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
1086 /************* Array containing the tests to run **********/
1088 #define STD_STREAM_SOCKET \
1094 static test_setup tests
[NUM_TESTS
] =
1096 /* Test 0: synchronous client and server */
1117 /* Test 1: event-driven client, synchronous server */
1134 WSA_FLAG_OVERLAPPED
,
1138 /* Test 2: synchronous client, non-blocking server via select() */
1159 /* Test 3: synchronous mixed client and server */
1173 simple_mixed_client
,
1182 static void test_UDP(void)
1184 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1185 possible that this test fails due to dropped packets. */
1187 /* peer 0 receives data from all other peers */
1188 struct sock_info peer
[NUM_UDP_PEERS
];
1190 int ss
, i
, n_recv
, n_sent
;
1192 memset (buf
,0,sizeof(buf
));
1193 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
1194 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
1196 peer
[i
].addr
.sin_family
= AF_INET
;
1197 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
1200 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
1202 peer
[i
].addr
.sin_port
= htons ( 0 );
1205 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
1207 /* test getsockname() to get peer's port */
1208 ss
= sizeof ( peer
[i
].addr
);
1209 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
1210 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
1213 /* test getsockname() */
1214 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
1216 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1217 /* send client's ip */
1218 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
1219 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
1220 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
1223 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1224 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
1225 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
1226 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
1230 static void WINAPI
do_getservbyname( HANDLE
*starttest
)
1236 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1239 struct servent
*pserv
[2];
1241 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
, "test_getservbyname: timeout waiting for start signal\n");
1243 /* ensure that necessary buffer resizes are completed */
1244 for ( j
= 0; j
< 2; j
++) {
1245 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1248 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
1249 for ( j
= 0; j
< 2; j
++ ) {
1250 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1251 ok ( pserv
[j
] != NULL
, "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
1252 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
) );
1253 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
);
1254 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
);
1257 ok ( pserv
[0] == pserv
[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1261 static void test_getservbyname(void)
1264 HANDLE starttest
, thread
[NUM_THREADS
];
1265 DWORD thread_id
[NUM_THREADS
];
1267 starttest
= CreateEvent ( NULL
, 1, 0, "test_getservbyname_starttest" );
1269 /* create threads */
1270 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
1271 thread
[i
] = CreateThread ( NULL
, 0, (LPTHREAD_START_ROUTINE
) &do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
1274 /* signal threads to start */
1275 SetEvent ( starttest
);
1277 for ( i
= 0; i
< NUM_THREADS
; i
++) {
1278 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
1282 static void test_WSASocket(void)
1284 SOCKET sock
= INVALID_SOCKET
;
1285 WSAPROTOCOL_INFOA
*pi
;
1286 int providers
[] = {6, 0};
1290 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1291 * to avoid a crash on win98.
1294 ret
= WSAEnumProtocolsA(providers
, NULL
, &pi_size
);
1295 ok(ret
== SOCKET_ERROR
, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1297 err
= WSAGetLastError();
1298 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1301 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
1302 ok(pi
!= NULL
, "Failed to allocate memory\n");
1304 skip("Can't continue without memory.\n");
1308 ret
= WSAEnumProtocolsA(providers
, pi
, &pi_size
);
1309 ok(ret
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
1313 skip("No protocols enumerated.\n");
1314 HeapFree(GetProcessHeap(), 0, pi
);
1318 sock
= WSASocketA(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
1319 FROM_PROTOCOL_INFO
, &pi
[0], 0, 0);
1320 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
1324 HeapFree(GetProcessHeap(), 0, pi
);
1327 static void test_WSAAddressToStringA(void)
1329 SOCKET v6
= INVALID_SOCKET
;
1333 SOCKADDR_IN sockaddr
;
1334 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1336 CHAR expect1
[] = "0.0.0.0";
1337 CHAR expect2
[] = "255.255.255.255";
1338 CHAR expect3
[] = "0.0.0.0:65535";
1339 CHAR expect4
[] = "255.255.255.255:65535";
1341 SOCKADDR_IN6 sockaddr6
;
1342 CHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1344 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1345 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1346 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1348 CHAR expect6_1
[] = "::1";
1349 CHAR expect6_2
[] = "20ab::1";
1350 CHAR expect6_3
[] = "[20ab::2001]:33274";
1351 CHAR expect6_3_2
[] = "[20ab::2001%4660]:33274";
1352 CHAR expect6_3_3
[] = "20ab::2001%4660";
1356 sockaddr
.sin_family
= AF_INET
;
1357 sockaddr
.sin_port
= 0;
1358 sockaddr
.sin_addr
.s_addr
= 0;
1360 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1361 GLE
= WSAGetLastError();
1362 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1363 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1366 len
= sizeof(address
);
1368 sockaddr
.sin_family
= AF_INET
;
1369 sockaddr
.sin_port
= 0;
1370 sockaddr
.sin_addr
.s_addr
= 0;
1372 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1373 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1375 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
1376 ok( len
== sizeof( expect1
), "Got size %d\n", len
);
1378 len
= sizeof(address
);
1380 sockaddr
.sin_family
= AF_INET
;
1381 sockaddr
.sin_port
= 0;
1382 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1384 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1385 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1387 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
1389 len
= sizeof(address
);
1391 sockaddr
.sin_family
= AF_INET
;
1392 sockaddr
.sin_port
= 0xffff;
1393 sockaddr
.sin_addr
.s_addr
= 0;
1395 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1396 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1398 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
1400 len
= sizeof(address
);
1402 sockaddr
.sin_family
= AF_INET
;
1403 sockaddr
.sin_port
= 0xffff;
1404 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1406 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1407 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1409 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
1410 ok( len
== sizeof( expect4
), "Got size %d\n", len
);
1412 /*check to see it IPv6 is available */
1413 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1414 if (v6
== INVALID_SOCKET
) {
1415 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1416 WSAGetLastError(), WSAEAFNOSUPPORT
);
1419 /* Test a short IPv6 address */
1420 len
= sizeof(address6
);
1422 sockaddr6
.sin6_family
= AF_INET6
;
1423 sockaddr6
.sin6_port
= 0x0000;
1424 sockaddr6
.sin6_scope_id
= 0;
1425 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1427 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1428 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1429 ok( !strcmp( address6
, expect6_1
), "Expected: %s, got: %s\n", expect6_1
, address6
);
1430 ok( len
== sizeof(expect6_1
), "Got size %d\n", len
);
1432 /* Test a longer IPv6 address */
1433 len
= sizeof(address6
);
1435 sockaddr6
.sin6_family
= AF_INET6
;
1436 sockaddr6
.sin6_port
= 0x0000;
1437 sockaddr6
.sin6_scope_id
= 0;
1438 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1440 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1441 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1442 ok( !strcmp( address6
, expect6_2
), "Expected: %s, got: %s\n", expect6_2
, address6
);
1443 ok( len
== sizeof(expect6_2
), "Got size %d\n", len
);
1445 /* Test IPv6 address and port number */
1446 len
= sizeof(address6
);
1448 sockaddr6
.sin6_family
= AF_INET6
;
1449 sockaddr6
.sin6_port
= 0xfa81;
1450 sockaddr6
.sin6_scope_id
= 0;
1451 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1453 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1454 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1455 ok( !strcmp( address6
, expect6_3
), "Expected: %s, got: %s\n", expect6_3
, address6
);
1456 ok( len
== sizeof(expect6_3
), "Got size %d\n", len
);
1458 /* Test IPv6 address, port number and scope_id */
1459 len
= sizeof(address6
);
1461 sockaddr6
.sin6_family
= AF_INET6
;
1462 sockaddr6
.sin6_port
= 0xfa81;
1463 sockaddr6
.sin6_scope_id
= 0x1234;
1464 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1466 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1467 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1468 ok( !strcmp( address6
, expect6_3_2
), "Expected: %s, got: %s\n", expect6_3_2
, address6
);
1469 ok( len
== sizeof(expect6_3_2
), "Got size %d\n", len
);
1471 /* Test IPv6 address and scope_id */
1472 len
= sizeof(address6
);
1474 sockaddr6
.sin6_family
= AF_INET6
;
1475 sockaddr6
.sin6_port
= 0x0000;
1476 sockaddr6
.sin6_scope_id
= 0x1234;
1477 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1479 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1480 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1481 ok( !strcmp( address6
, expect6_3_3
), "Expected: %s, got: %s\n", expect6_3_3
, address6
);
1482 ok( len
== sizeof(expect6_3_3
), "Got size %d\n", len
);
1485 if (v6
!= INVALID_SOCKET
)
1489 static void test_WSAAddressToStringW(void)
1491 SOCKET v6
= INVALID_SOCKET
;
1495 SOCKADDR_IN sockaddr
;
1496 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1498 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
1499 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1500 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1501 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1502 '6', '5', '5', '3', '5', 0 };
1504 SOCKADDR_IN6 sockaddr6
;
1505 WCHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1507 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1508 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1509 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1511 WCHAR expect6_1
[] = {':',':','1',0};
1512 WCHAR expect6_2
[] = {'2','0','a','b',':',':','1',0};
1513 WCHAR expect6_3
[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1514 WCHAR expect6_3_2
[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1515 WCHAR expect6_3_3
[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1519 sockaddr
.sin_family
= AF_INET
;
1520 sockaddr
.sin_port
= 0;
1521 sockaddr
.sin_addr
.s_addr
= 0;
1523 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1524 GLE
= WSAGetLastError();
1525 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1526 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1529 len
= sizeof(address
);
1531 sockaddr
.sin_family
= AF_INET
;
1532 sockaddr
.sin_port
= 0;
1533 sockaddr
.sin_addr
.s_addr
= 0;
1535 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1536 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1538 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
1539 ok( len
== sizeof( expect1
)/sizeof( WCHAR
), "Got size %d\n", len
);
1541 len
= sizeof(address
);
1543 sockaddr
.sin_family
= AF_INET
;
1544 sockaddr
.sin_port
= 0;
1545 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1547 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1548 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1550 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
1552 len
= sizeof(address
);
1554 sockaddr
.sin_family
= AF_INET
;
1555 sockaddr
.sin_port
= 0xffff;
1556 sockaddr
.sin_addr
.s_addr
= 0;
1558 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1559 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1561 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
1563 len
= sizeof(address
);
1565 sockaddr
.sin_family
= AF_INET
;
1566 sockaddr
.sin_port
= 0xffff;
1567 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1569 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1570 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1572 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
1573 ok( len
== sizeof( expect4
)/sizeof( WCHAR
), "Got %d\n", len
);
1575 /*check to see it IPv6 is available */
1576 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1577 if (v6
== INVALID_SOCKET
) {
1578 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1579 WSAGetLastError(), WSAEAFNOSUPPORT
);
1583 /* Test a short IPv6 address */
1584 len
= sizeof(address6
)/sizeof(WCHAR
);
1586 sockaddr6
.sin6_family
= AF_INET6
;
1587 sockaddr6
.sin6_port
= 0x0000;
1588 sockaddr6
.sin6_scope_id
= 0;
1589 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1591 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1592 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1593 ok( !lstrcmpW( address6
, expect6_1
), "Wrong string returned\n" );
1594 ok( len
== sizeof(expect6_1
)/sizeof(WCHAR
), "Got %d\n", len
);
1596 /* Test a longer IPv6 address */
1597 len
= sizeof(address6
)/sizeof(WCHAR
);
1599 sockaddr6
.sin6_family
= AF_INET6
;
1600 sockaddr6
.sin6_port
= 0x0000;
1601 sockaddr6
.sin6_scope_id
= 0;
1602 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1604 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1605 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1607 ok( !lstrcmpW( address6
, expect6_2
), "Wrong string returned\n" );
1608 ok( len
== sizeof(expect6_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1610 /* Test IPv6 address and port number */
1611 len
= sizeof(address6
)/sizeof(WCHAR
);
1613 sockaddr6
.sin6_family
= AF_INET6
;
1614 sockaddr6
.sin6_port
= 0xfa81;
1615 sockaddr6
.sin6_scope_id
= 0;
1616 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1618 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1619 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1620 ok( !lstrcmpW( address6
, expect6_3
), "Wrong string returned\n" );
1621 ok( len
== sizeof(expect6_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1623 /* Test IPv6 address, port number and scope_id */
1624 len
= sizeof(address6
)/sizeof(WCHAR
);
1626 sockaddr6
.sin6_family
= AF_INET6
;
1627 sockaddr6
.sin6_port
= 0xfa81;
1628 sockaddr6
.sin6_scope_id
= 0x1234;
1629 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1631 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1632 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1633 ok( !lstrcmpW( address6
, expect6_3_2
), "Wrong string returned\n" );
1634 ok( len
== sizeof(expect6_3_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1636 /* Test IPv6 address and scope_id */
1637 len
= sizeof(address6
)/sizeof(WCHAR
);
1639 sockaddr6
.sin6_family
= AF_INET6
;
1640 sockaddr6
.sin6_port
= 0x0000;
1641 sockaddr6
.sin6_scope_id
= 0xfffe;
1642 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1644 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1645 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1646 ok( !lstrcmpW( address6
, expect6_3_3
), "Wrong string returned\n" );
1647 ok( len
== sizeof(expect6_3_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1650 if (v6
!= INVALID_SOCKET
)
1654 static void test_WSAStringToAddressA(void)
1657 SOCKADDR_IN sockaddr
;
1658 SOCKADDR_IN6 sockaddr6
;
1661 CHAR address1
[] = "0.0.0.0";
1662 CHAR address2
[] = "127.127.127.127";
1663 CHAR address3
[] = "255.255.255.255";
1664 CHAR address4
[] = "127.127.127.127:65535";
1665 CHAR address5
[] = "255.255.255.255:65535";
1666 CHAR address6
[] = "::1";
1667 CHAR address7
[] = "[::1]";
1668 CHAR address8
[] = "[::1]:65535";
1671 sockaddr
.sin_family
= AF_INET
;
1673 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1674 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1675 WSAGetLastError() );
1677 len
= sizeof(sockaddr
);
1678 sockaddr
.sin_port
= 0;
1679 sockaddr
.sin_addr
.s_addr
= 0;
1681 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1682 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1683 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1685 len
= sizeof(sockaddr
);
1686 sockaddr
.sin_port
= 0;
1687 sockaddr
.sin_addr
.s_addr
= 0;
1689 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1690 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1691 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1693 len
= sizeof(sockaddr
);
1694 sockaddr
.sin_port
= 0;
1695 sockaddr
.sin_addr
.s_addr
= 0;
1697 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1698 GLE
= WSAGetLastError();
1699 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1700 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1701 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1703 len
= sizeof(sockaddr
);
1704 sockaddr
.sin_port
= 0;
1705 sockaddr
.sin_addr
.s_addr
= 0;
1707 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1708 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1709 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1711 len
= sizeof(sockaddr
);
1712 sockaddr
.sin_port
= 0;
1713 sockaddr
.sin_addr
.s_addr
= 0;
1715 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1716 GLE
= WSAGetLastError();
1717 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1718 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1719 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1721 len
= sizeof(sockaddr6
);
1722 memset(&sockaddr6
, 0, len
);
1723 sockaddr6
.sin6_family
= AF_INET6
;
1725 ret
= WSAStringToAddressA( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1727 GLE
= WSAGetLastError();
1728 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1729 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1731 len
= sizeof(sockaddr6
);
1732 memset(&sockaddr6
, 0, len
);
1733 sockaddr6
.sin6_family
= AF_INET6
;
1735 ret
= WSAStringToAddressA( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1737 GLE
= WSAGetLastError();
1738 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1739 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1741 len
= sizeof(sockaddr6
);
1742 memset(&sockaddr6
, 0, len
);
1743 sockaddr6
.sin6_family
= AF_INET6
;
1745 ret
= WSAStringToAddressA( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1747 GLE
= WSAGetLastError();
1748 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1749 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1750 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1754 static void test_WSAStringToAddressW(void)
1757 SOCKADDR_IN sockaddr
;
1758 SOCKADDR_IN6 sockaddr6
;
1761 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
1762 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1763 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1764 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1765 ':', '6', '5', '5', '3', '5', 0 };
1766 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1767 '6', '5', '5', '3', '5', 0 };
1768 WCHAR address6
[] = {':',':','1','\0'};
1769 WCHAR address7
[] = {'[',':',':','1',']','\0'};
1770 WCHAR address8
[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1773 sockaddr
.sin_family
= AF_INET
;
1775 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1776 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
1777 WSAGetLastError() );
1779 len
= sizeof(sockaddr
);
1780 sockaddr
.sin_port
= 0;
1781 sockaddr
.sin_addr
.s_addr
= 0;
1783 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1784 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1785 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1787 len
= sizeof(sockaddr
);
1788 sockaddr
.sin_port
= 0;
1789 sockaddr
.sin_addr
.s_addr
= 0;
1791 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1792 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1793 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1795 len
= sizeof(sockaddr
);
1796 sockaddr
.sin_port
= 0;
1797 sockaddr
.sin_addr
.s_addr
= 0;
1799 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1800 GLE
= WSAGetLastError();
1801 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1802 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1803 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1805 len
= sizeof(sockaddr
);
1806 sockaddr
.sin_port
= 0;
1807 sockaddr
.sin_addr
.s_addr
= 0;
1809 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1810 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1811 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1813 len
= sizeof(sockaddr
);
1814 sockaddr
.sin_port
= 0;
1815 sockaddr
.sin_addr
.s_addr
= 0;
1817 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1818 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1819 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1820 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1822 len
= sizeof(sockaddr6
);
1823 memset(&sockaddr6
, 0, len
);
1824 sockaddr6
.sin6_family
= AF_INET6
;
1826 ret
= WSAStringToAddressW( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1828 GLE
= WSAGetLastError();
1829 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1830 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1832 len
= sizeof(sockaddr6
);
1833 memset(&sockaddr6
, 0, len
);
1834 sockaddr6
.sin6_family
= AF_INET6
;
1836 ret
= WSAStringToAddressW( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1838 GLE
= WSAGetLastError();
1839 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1840 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1842 len
= sizeof(sockaddr6
);
1843 memset(&sockaddr6
, 0, len
);
1844 sockaddr6
.sin6_family
= AF_INET6
;
1846 ret
= WSAStringToAddressW( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1848 GLE
= WSAGetLastError();
1849 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1850 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1851 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1855 static VOID WINAPI
SelectReadThread(select_thread_params
*par
)
1859 struct sockaddr_in addr
;
1860 struct timeval select_timeout
;
1863 FD_SET(par
->s
, &readfds
);
1864 select_timeout
.tv_sec
=5;
1865 select_timeout
.tv_usec
=0;
1866 addr
.sin_family
= AF_INET
;
1867 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
1868 addr
.sin_port
= htons(SERVERPORT
);
1870 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
1871 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1873 SetEvent(server_ready
);
1874 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
1875 par
->ReadKilled
= (ret
== 1);
1878 static void test_select(void)
1880 SOCKET fdRead
, fdWrite
;
1881 fd_set readfds
, writefds
, exceptfds
;
1884 struct timeval select_timeout
;
1885 select_thread_params thread_params
;
1886 HANDLE thread_handle
;
1889 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
1890 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1891 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
1892 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1896 FD_ZERO(&exceptfds
);
1897 FD_SET(fdRead
, &readfds
);
1898 FD_SET(fdWrite
, &writefds
);
1899 FD_SET(fdRead
, &exceptfds
);
1900 FD_SET(fdWrite
, &exceptfds
);
1901 select_timeout
.tv_sec
=0;
1902 select_timeout
.tv_usec
=500;
1905 if (fdWrite
> maxfd
)
1909 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
1910 ok ( (ret
== 0), "select should not return any socket handles\n");
1911 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
1912 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
1915 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
1916 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
1919 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
1921 ret
= closesocket(fdWrite
);
1922 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1924 thread_params
.s
= fdRead
;
1925 thread_params
.ReadKilled
= FALSE
;
1926 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1927 thread_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) &SelectReadThread
, &thread_params
, 0, &id
);
1928 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
1930 WaitForSingleObject (server_ready
, INFINITE
);
1932 ret
= closesocket(fdRead
);
1933 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1935 WaitForSingleObject (thread_handle
, 1000);
1936 ok ( (thread_params
.ReadKilled
) ||
1937 broken(thread_params
.ReadKilled
== 0), /*Win98*/
1938 "closesocket did not wakeup select\n");
1942 static DWORD WINAPI
AcceptKillThread(select_thread_params
*par
)
1944 struct sockaddr_in address
;
1945 int len
= sizeof(address
);
1946 SOCKET client_socket
;
1948 SetEvent(server_ready
);
1949 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
1950 if (client_socket
!= INVALID_SOCKET
)
1951 closesocket(client_socket
);
1952 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
1957 static int CALLBACK
AlwaysDeferConditionFunc(LPWSABUF lpCallerId
, LPWSABUF lpCallerData
, LPQOS pQos
,
1958 LPQOS lpGQOS
, LPWSABUF lpCalleeId
, LPWSABUF lpCalleeData
,
1959 GROUP FAR
* g
, DWORD_PTR dwCallbackData
)
1964 static void test_accept(void)
1967 SOCKET server_socket
= INVALID_SOCKET
, accepted
= INVALID_SOCKET
, connector
= INVALID_SOCKET
;
1968 struct sockaddr_in address
;
1970 select_thread_params thread_params
;
1971 HANDLE thread_handle
= NULL
;
1974 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
1975 if (server_socket
== INVALID_SOCKET
)
1977 trace("error creating server socket: %d\n", WSAGetLastError());
1981 memset(&address
, 0, sizeof(address
));
1982 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1983 address
.sin_family
= AF_INET
;
1984 ret
= bind(server_socket
, (struct sockaddr
*) &address
, sizeof(address
));
1987 trace("error binding server socket: %d\n", WSAGetLastError());
1991 socklen
= sizeof(address
);
1992 ret
= getsockname(server_socket
, (struct sockaddr
*)&address
, &socklen
);
1994 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
1998 ret
= listen(server_socket
, 5);
2001 trace("error making server socket listen: %d\n", WSAGetLastError());
2005 trace("Blocking accept next\n");
2007 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2008 ok(connector
!= INVALID_SOCKET
, "Failed to create connector socket, error %d\n", WSAGetLastError());
2010 ret
= connect(connector
, (struct sockaddr
*)&address
, sizeof(address
));
2011 ok(ret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2013 accepted
= WSAAccept(server_socket
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
2014 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
2016 accepted
= accept(server_socket
, NULL
, 0);
2017 ok(accepted
!= INVALID_SOCKET
, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2019 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2020 if (server_ready
== INVALID_HANDLE_VALUE
)
2022 trace("error creating event: %d\n", GetLastError());
2026 thread_params
.s
= server_socket
;
2027 thread_params
.ReadKilled
= FALSE
;
2028 thread_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
) AcceptKillThread
,
2029 &thread_params
, 0, &id
);
2030 if (thread_handle
== NULL
)
2032 trace("error creating thread: %d\n", GetLastError());
2036 WaitForSingleObject(server_ready
, INFINITE
);
2038 ret
= closesocket(server_socket
);
2041 trace("closesocket failed: %d\n", WSAGetLastError());
2045 WaitForSingleObject(thread_handle
, 1000);
2046 ok(thread_params
.ReadKilled
|| broken(!thread_params
.ReadKilled
) /* Win98/ME, after accept */,
2047 "closesocket did not wakeup accept\n");
2050 if (accepted
!= INVALID_SOCKET
)
2051 closesocket(accepted
);
2052 if (connector
!= INVALID_SOCKET
)
2053 closesocket(connector
);
2054 if (thread_handle
!= NULL
)
2055 CloseHandle(thread_handle
);
2056 if (server_ready
!= INVALID_HANDLE_VALUE
)
2057 CloseHandle(server_ready
);
2058 if (server_socket
!= INVALID_SOCKET
)
2059 closesocket(server_socket
);
2062 static void test_extendedSocketOptions(void)
2066 struct sockaddr_in sa
;
2067 int sa_len
= sizeof(struct sockaddr_in
);
2068 int optval
, optlen
= sizeof(int), ret
;
2072 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2073 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2077 memset(&sa
, 0, sa_len
);
2079 sa
.sin_family
= AF_INET
;
2080 sa
.sin_port
= htons(0);
2081 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2083 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2084 trace("Creating the socket failed: %d\n", WSAGetLastError());
2089 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2090 trace("Failed to bind socket: %d\n", WSAGetLastError());
2096 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2098 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
2099 ok((optval
== 65507) || (optval
== 65527),
2100 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval
);
2102 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2103 SetLastError(0xdeadbeef);
2104 optval
= 0xdeadbeef;
2105 optlen
= sizeof(int);
2106 ret
= getsockopt(sock
, 0xffffffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2107 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2108 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2109 ret
, WSAGetLastError(), optval
, optval
);
2111 /* more invalid values for level */
2112 SetLastError(0xdeadbeef);
2113 optval
= 0xdeadbeef;
2114 optlen
= sizeof(int);
2115 ret
= getsockopt(sock
, 0x1234ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2116 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2117 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2118 ret
, WSAGetLastError(), optval
, optval
);
2120 SetLastError(0xdeadbeef);
2121 optval
= 0xdeadbeef;
2122 optlen
= sizeof(int);
2123 ret
= getsockopt(sock
, 0x8000ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2124 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2125 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2126 ret
, WSAGetLastError(), optval
, optval
);
2128 SetLastError(0xdeadbeef);
2129 optval
= 0xdeadbeef;
2130 optlen
= sizeof(int);
2131 ret
= getsockopt(sock
, 0x00008000, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2132 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2133 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2134 ret
, WSAGetLastError(), optval
, optval
);
2136 SetLastError(0xdeadbeef);
2137 optval
= 0xdeadbeef;
2138 optlen
= sizeof(int);
2139 ret
= getsockopt(sock
, 0x00000800, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2140 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2141 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2142 ret
, WSAGetLastError(), optval
, optval
);
2144 optlen
= sizeof(LINGER
);
2145 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2147 ok(ret
== SOCKET_ERROR
, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret
);
2152 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2153 trace("Creating the socket failed: %d\n", WSAGetLastError());
2158 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2159 trace("Failed to bind socket: %d\n", WSAGetLastError());
2165 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2166 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
2168 optlen
= sizeof(BOOL
);
2169 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
2170 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
2171 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
2172 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2173 bool_opt_val
, linger_val
.l_onoff
);
2179 static void test_getsockname(void)
2183 struct sockaddr_in sa_set
, sa_get
;
2184 int sa_set_len
= sizeof(struct sockaddr_in
);
2185 int sa_get_len
= sa_set_len
;
2186 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
2189 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2190 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2194 memset(&sa_set
, 0, sa_set_len
);
2196 sa_set
.sin_family
= AF_INET
;
2197 sa_set
.sin_port
= htons(0);
2198 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2200 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2201 trace("Creating the socket failed: %d\n", WSAGetLastError());
2206 memcpy(&sa_get
, &sa_set
, sizeof(sa_set
));
2207 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
2208 ok(0, "getsockname on unbound socket should fail\n");
2210 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
2211 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
2212 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
2213 "failed getsockname modified sockaddr when it shouldn't\n");
2216 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
2217 trace("Failed to bind socket: %d\n", WSAGetLastError());
2223 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
2224 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2230 ret
= memcmp(sa_get
.sin_zero
, null_padding
, 8);
2231 ok(ret
== 0 || broken(ret
!= 0), /* NT4 */
2232 "getsockname did not zero the sockaddr_in structure\n");
2238 static void test_dns(void)
2242 h
= gethostbyname("");
2243 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
2246 /* Our winsock headers don't define gethostname because it conflicts with the
2247 * definition in unistd.h. Define it here to get rid of the warning. */
2249 int WINAPI
gethostname(char *name
, int namelen
);
2251 static void test_gethostbyname_hack(void)
2255 static BYTE loopback
[] = {127, 0, 0, 1};
2256 static BYTE magic_loopback
[] = {127, 12, 34, 56};
2259 ret
= gethostname(name
, 256);
2260 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
2262 he
= gethostbyname("localhost");
2263 ok(he
!= NULL
, "gethostbyname(\"localhost\") failed: %d\n", h_errno
);
2266 if(he
->h_length
!= 4)
2268 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2272 ok(memcmp(he
->h_addr_list
[0], loopback
, he
->h_length
) == 0,
2273 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2274 he
->h_addr_list
[0][0], he
->h_addr_list
[0][1], he
->h_addr_list
[0][2],
2275 he
->h_addr_list
[0][3]);
2278 /* No reason to test further with NULL hostname */
2282 if(strcmp(name
, "localhost") == 0)
2284 skip("hostname seems to be \"localhost\", skipping test.\n");
2289 he
= gethostbyname(name
);
2290 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, h_errno
);
2293 if(he
->h_length
!= 4)
2295 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2299 if (he
->h_addr_list
[0][0] == 127)
2301 ok(memcmp(he
->h_addr_list
[0], magic_loopback
, he
->h_length
) == 0,
2302 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2303 name
, he
->h_addr_list
[0][0], he
->h_addr_list
[0][1],
2304 he
->h_addr_list
[0][2], he
->h_addr_list
[0][3]);
2309 he
= gethostbyname("nonexistent.winehq.org");
2310 /* Don't check for the return value, as some braindead ISPs will kindly
2311 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2314 static void test_inet_addr(void)
2318 addr
= inet_addr(NULL
);
2319 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
2322 static void test_addr_to_print(void)
2328 struct in6_addr in6
;
2330 u_long addr0_Num
= 0x00000000;
2331 PCSTR addr0_Str
= "0.0.0.0";
2332 u_long addr1_Num
= 0x20201015;
2333 PCSTR addr1_Str
= "21.16.32.32";
2334 u_char addr2_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2335 PCSTR addr2_Str
= "::fffe:cc98:bd74";
2336 u_char addr3_Num
[16] = {0x20,0x30,0xa4,0xb1};
2337 PCSTR addr3_Str
= "2030:a4b1::";
2338 u_char addr4_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2339 PCSTR addr4_Str
= "::204.152.189.116";
2341 /* Test IPv4 addresses */
2342 in
.s_addr
= addr0_Num
;
2344 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2345 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2346 ok(!strcmp(pdst
, addr0_Str
),"Address %s != %s\n", pdst
, addr0_Str
);
2348 /* Test that inet_ntoa and inet_ntop return the same value */
2349 in
.S_un
.S_addr
= addr1_Num
;
2350 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2351 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2352 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2354 /* InetNtop became available in Vista and Win2008 */
2357 win_skip("InetNtop not present, not executing tests\n");
2361 /* Second part of test */
2362 pdst
= pInetNtop(AF_INET
,(void*)&in
.s_addr
, dst
, sizeof(dst
));
2363 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst
);
2364 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2366 /* Test invalid parm conditions */
2367 pdst
= pInetNtop(1, (void*)&in
.s_addr
, dst
, sizeof(dst
));
2368 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2369 ok(WSAGetLastError() == WSAEAFNOSUPPORT
, "Should be WSAEAFNOSUPPORT\n");
2371 /* Test Null destination */
2373 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, NULL
, sizeof(dst
));
2374 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2375 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2376 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2378 /* Test zero length passed */
2381 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 0);
2382 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2383 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2384 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2386 /* Test length one shorter than the address length */
2389 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 6);
2390 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2391 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2392 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2394 /* Test longer length is ok */
2397 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, sizeof(dst
)+1);
2398 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2399 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2401 /* Test the IPv6 addresses */
2403 /* Test an zero prefixed IPV6 address */
2404 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2405 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2406 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2407 ok(!strcmp(pdst
, addr2_Str
),"Address %s != %s\n", pdst
, addr2_Str
);
2409 /* Test an zero suffixed IPV6 address */
2410 memcpy(in6
.s6_addr
, addr3_Num
, sizeof(addr3_Num
));
2411 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2412 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2413 ok(!strcmp(pdst
, addr3_Str
),"Address %s != %s\n", pdst
, addr3_Str
);
2415 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2416 memcpy(in6
.s6_addr
, addr4_Num
, sizeof(addr4_Num
));
2417 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2418 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2419 ok(!strcmp(pdst
, addr4_Str
),"Address %s != %s\n", pdst
, addr4_Str
);
2421 /* Test invalid parm conditions */
2422 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2424 /* Test Null destination */
2426 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, NULL
, sizeof(dst6
));
2427 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2428 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2429 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2431 /* Test zero length passed */
2434 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 0);
2435 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2436 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2437 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2439 /* Test length one shorter than the address length */
2442 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 16);
2443 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2444 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2445 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2447 /* Test longer length is ok */
2450 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 18);
2451 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2454 static void test_ioctlsocket(void)
2458 static const LONG cmds
[] = {FIONBIO
, FIONREAD
, SIOCATMARK
};
2462 sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2463 ok(sock
!= INVALID_SOCKET
, "Creating the socket failed: %d\n", WSAGetLastError());
2464 if(sock
== INVALID_SOCKET
)
2466 skip("Can't continue without a socket.\n");
2470 for(i
= 0; i
< sizeof(cmds
)/sizeof(cmds
[0]); i
++)
2472 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2473 ret
= ioctlsocket(sock
, cmds
[i
], (u_long
*)1);
2474 ok(ret
== SOCKET_ERROR
, "ioctlsocket succeeded unexpectedly\n");
2475 ret
= WSAGetLastError();
2476 ok(ret
== WSAEFAULT
, "expected WSAEFAULT, got %d instead\n", ret
);
2479 /* A fresh and not connected socket has no urgent data, this test shows
2480 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2482 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
2483 if(ret
!= SOCKET_ERROR
)
2484 todo_wine
ok(arg
, "expected a non-zero value\n");
2487 static int drain_pause
=0;
2488 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
2491 SOCKET sock
= *(SOCKET
*)arg
;
2494 while ((ret
= recv(sock
, buffer
, sizeof(buffer
), 0)) != 0)
2498 if (WSAGetLastError() == WSAEWOULDBLOCK
)
2502 FD_SET(sock
, &readset
);
2503 select(0, &readset
, NULL
, NULL
, NULL
);
2514 static void test_send(void)
2516 SOCKET src
= INVALID_SOCKET
;
2517 SOCKET dst
= INVALID_SOCKET
;
2518 HANDLE hThread
= NULL
;
2519 const int buflen
= 1024*1024;
2520 char *buffer
= NULL
;
2524 if (tcp_socketpair(&src
, &dst
) != 0)
2526 ok(0, "creating socket pair failed, skipping test\n");
2530 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2531 if (hThread
== NULL
)
2533 ok(0, "CreateThread failed, error %d\n", GetLastError());
2537 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buflen
);
2540 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2544 ret
= send(src
, buffer
, buflen
, 0);
2546 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
2548 ok(0, "send failed, error %d\n", WSAGetLastError());
2551 if (src
!= INVALID_SOCKET
)
2553 if (dst
!= INVALID_SOCKET
)
2555 if (hThread
!= NULL
)
2556 CloseHandle(hThread
);
2557 HeapFree(GetProcessHeap(), 0, buffer
);
2560 typedef struct async_message
2564 struct async_message
*next
;
2567 static struct async_message
*messages_received
;
2569 #define WM_SOCKET (WM_USER+100)
2570 static LRESULT CALLBACK
ws2_test_WndProc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
2572 struct async_message
*message
;
2577 message
= HeapAlloc(GetProcessHeap(), 0, sizeof(*message
));
2578 message
->socket
= (SOCKET
) wparam
;
2579 message
->lparam
= lparam
;
2580 message
->next
= NULL
;
2582 if (messages_received
)
2584 struct async_message
*last
= messages_received
;
2585 while (last
->next
) last
= last
->next
;
2586 last
->next
= message
;
2589 messages_received
= message
;
2593 return DefWindowProc(hwnd
, msg
, wparam
, lparam
);
2596 static void get_event_details(int event
, int *bit
, char *name
)
2601 if (bit
) *bit
= FD_ACCEPT_BIT
;
2602 if (name
) strcpy(name
, "FD_ACCEPT");
2605 if (bit
) *bit
= FD_CONNECT_BIT
;
2606 if (name
) strcpy(name
, "FD_CONNECT");
2609 if (bit
) *bit
= FD_READ_BIT
;
2610 if (name
) strcpy(name
, "FD_READ");
2613 if (bit
) *bit
= FD_OOB_BIT
;
2614 if (name
) strcpy(name
, "FD_OOB");
2617 if (bit
) *bit
= FD_WRITE_BIT
;
2618 if (name
) strcpy(name
, "FD_WRITE");
2621 if (bit
) *bit
= FD_CLOSE_BIT
;
2622 if (name
) strcpy(name
, "FD_CLOSE");
2626 if (name
) sprintf(name
, "bad%x", event
);
2630 static char *dbgstr_event_seq(const LPARAM
*seq
)
2632 static char message
[1024];
2639 get_event_details(WSAGETSELECTEVENT(*seq
), NULL
, name
);
2641 sprintf(message
, "%s%s%s(%d)", message
, message
[1] == 0 ? "" : " ",
2642 name
, WSAGETSELECTERROR(*seq
));
2646 strcat(message
, "]");
2650 static char *dbgstr_event_seq_result(SOCKET s
, WSANETWORKEVENTS
*netEvents
)
2652 static char message
[1024];
2653 struct async_message
*curr
= messages_received
;
2654 int index
, error
, bit
= 0;
2663 if (bit
>= FD_MAX_EVENTS
) break;
2664 if ( !(netEvents
->lNetworkEvents
& (1 << bit
)) )
2669 get_event_details(1 << bit
, &index
, name
);
2670 error
= netEvents
->iErrorCode
[index
];
2676 if (curr
->socket
!= s
)
2681 get_event_details(WSAGETSELECTEVENT(curr
->lparam
), NULL
, name
);
2682 error
= WSAGETSELECTERROR(curr
->lparam
);
2686 sprintf(message
, "%s%s%s(%d)", message
, message
[1] == 0 ? "" : " ",
2689 strcat(message
, "]");
2693 static void flush_events(SOCKET s
, HANDLE hEvent
)
2695 WSANETWORKEVENTS netEvents
;
2696 struct async_message
*prev
= NULL
, *curr
= messages_received
;
2700 if (hEvent
!= INVALID_HANDLE_VALUE
)
2702 dwRet
= WaitForSingleObject(hEvent
, 100);
2703 if (dwRet
== WAIT_OBJECT_0
)
2705 ret
= WSAEnumNetworkEvents(s
, hEvent
, &netEvents
);
2707 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2714 if (curr
->socket
== s
)
2716 if (prev
) prev
->next
= curr
->next
;
2717 else messages_received
= curr
->next
;
2719 HeapFree(GetProcessHeap(), 0, curr
);
2721 if (prev
) curr
= prev
->next
;
2722 else curr
= messages_received
;
2733 static int match_event_sequence(SOCKET s
, WSANETWORKEVENTS
*netEvents
, const LPARAM
*seq
)
2735 int event
, index
, error
, events
;
2736 struct async_message
*curr
;
2740 events
= netEvents
->lNetworkEvents
;
2743 event
= WSAGETSELECTEVENT(*seq
);
2744 error
= WSAGETSELECTERROR(*seq
);
2745 get_event_details(event
, &index
, NULL
);
2747 if (!(events
& event
) && index
!= -1)
2749 if (events
& event
&& index
!= -1)
2751 if (netEvents
->iErrorCode
[index
] != error
)
2762 curr
= messages_received
;
2765 if (curr
->socket
== s
)
2767 if (!*seq
) return 0;
2768 if (*seq
!= curr
->lparam
) return 0;
2779 /* checks for a sequence of events, (order only checked if window is used) */
2780 static void ok_event_sequence(SOCKET s
, HANDLE hEvent
, const LPARAM
*seq
, const LPARAM
**broken_seqs
, int completelyBroken
)
2783 WSANETWORKEVENTS events
, *netEvents
= NULL
;
2787 if (hEvent
!= INVALID_HANDLE_VALUE
)
2789 netEvents
= &events
;
2791 dwRet
= WaitForSingleObject(hEvent
, 200);
2792 if (dwRet
== WAIT_OBJECT_0
)
2794 ret
= WSAEnumNetworkEvents(s
, hEvent
, netEvents
);
2797 winetest_ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2802 memset(netEvents
, 0, sizeof(*netEvents
));
2807 /* Run the message loop a little */
2808 while (PeekMessageA( &msg
, 0, 0, 0, PM_REMOVE
))
2810 DispatchMessageA(&msg
);
2814 if (match_event_sequence(s
, netEvents
, seq
))
2816 winetest_ok(1, "Sequence matches expected: %s\n", dbgstr_event_seq(seq
));
2817 flush_events(s
, hEvent
);
2823 for (; *broken_seqs
; broken_seqs
++)
2825 if (match_event_sequence(s
, netEvents
, *broken_seqs
))
2827 winetest_ok(broken(1), "Sequence matches broken: %s, expected %s\n", dbgstr_event_seq_result(s
, netEvents
), dbgstr_event_seq(seq
));
2828 flush_events(s
, hEvent
);
2834 winetest_ok(broken(completelyBroken
), "Expected event sequence %s, got %s\n", dbgstr_event_seq(seq
),
2835 dbgstr_event_seq_result(s
, netEvents
));
2836 flush_events(s
, hEvent
);
2839 #define ok_event_seq (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_event_sequence
2841 static void test_events(int useMessages
)
2843 SOCKET server
= INVALID_SOCKET
;
2844 SOCKET src
= INVALID_SOCKET
, src2
= INVALID_SOCKET
;
2845 SOCKET dst
= INVALID_SOCKET
, dst2
= INVALID_SOCKET
;
2846 struct sockaddr_in addr
;
2847 HANDLE hThread
= NULL
;
2848 HANDLE hEvent
= INVALID_HANDLE_VALUE
, hEvent2
= INVALID_HANDLE_VALUE
;
2849 WNDCLASSEX wndclass
;
2851 char *buffer
= NULL
;
2852 int bufferSize
= 1024*1024;
2856 DWORD bytesReturned
;
2862 static char szClassName
[] = "wstestclass";
2863 const LPARAM
*broken_seq
[3];
2864 static const LPARAM empty_seq
[] = { 0 };
2865 static const LPARAM close_seq
[] = { WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
2866 static const LPARAM write_seq
[] = { WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
2867 static const LPARAM read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
2868 static const LPARAM oob_seq
[] = { WSAMAKESELECTREPLY(FD_OOB
, 0), 0 };
2869 static const LPARAM connect_seq
[] = { WSAMAKESELECTREPLY(FD_CONNECT
, 0),
2870 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
2871 static const LPARAM read_read_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
2872 WSAMAKESELECTREPLY(FD_READ
, 0), 0 };
2873 static const LPARAM read_write_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
2874 WSAMAKESELECTREPLY(FD_WRITE
, 0), 0 };
2875 static const LPARAM read_close_seq
[] = { WSAMAKESELECTREPLY(FD_READ
, 0),
2876 WSAMAKESELECTREPLY(FD_CLOSE
, 0), 0 };
2878 memset(&ov
, 0, sizeof(ov
));
2879 memset(&ov2
, 0, sizeof(ov2
));
2881 /* don't use socketpair, we want connection event */
2882 src
= socket(AF_INET
, SOCK_STREAM
, 0);
2883 if (src
== INVALID_SOCKET
)
2885 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2889 src2
= socket(AF_INET
, SOCK_STREAM
, 0);
2890 if (src2
== INVALID_SOCKET
)
2892 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2897 if (getsockopt(src
, SOL_SOCKET
, SO_OOBINLINE
, (void *)&bret
, &len
) == SOCKET_ERROR
)
2899 ok(0, "failed to get oobinline status, %d\n", GetLastError());
2902 ok(bret
== FALSE
, "OOB not inline\n");
2906 trace("Event test using messages\n");
2908 wndclass
.cbSize
= sizeof(wndclass
);
2909 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
2910 wndclass
.lpfnWndProc
= ws2_test_WndProc
;
2911 wndclass
.cbClsExtra
= 0;
2912 wndclass
.cbWndExtra
= 0;
2913 wndclass
.hInstance
= GetModuleHandle(NULL
);
2914 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
2915 wndclass
.hIconSm
= LoadIcon(NULL
, IDI_APPLICATION
);
2916 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
2917 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
2918 wndclass
.lpszClassName
= szClassName
;
2919 wndclass
.lpszMenuName
= NULL
;
2920 RegisterClassEx(&wndclass
);
2922 hWnd
= CreateWindow(szClassName
, "WS2Test", WS_OVERLAPPEDWINDOW
, 0, 0, 500, 500, NULL
, NULL
, GetModuleHandle(NULL
), NULL
);
2925 ok(0, "failed to create window: %d\n", GetLastError());
2929 ret
= WSAAsyncSelect(src
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2932 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
2936 ret
= WSAAsyncSelect(src2
, hWnd
, WM_SOCKET
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2939 ok(0, "WSAAsyncSelect failed, error %d\n", ret
);
2945 trace("Event test using events\n");
2947 hEvent
= WSACreateEvent();
2948 if (hEvent
== INVALID_HANDLE_VALUE
)
2950 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
2954 hEvent2
= WSACreateEvent();
2955 if (hEvent2
== INVALID_HANDLE_VALUE
)
2957 ok(0, "WSACreateEvent failed, error %d\n", GetLastError());
2961 ret
= WSAEventSelect(src
, hEvent
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2964 ok(0, "WSAEventSelect failed, error %d\n", ret
);
2968 ret
= WSAEventSelect(src2
, hEvent2
, FD_CONNECT
| FD_READ
| FD_OOB
| FD_WRITE
| FD_CLOSE
);
2971 ok(0, "WSAEventSelect failed, error %d\n", ret
);
2976 server
= socket(AF_INET
, SOCK_STREAM
, 0);
2977 if (server
== INVALID_SOCKET
)
2979 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2983 memset(&addr
, 0, sizeof(addr
));
2984 addr
.sin_family
= AF_INET
;
2985 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2986 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
2989 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
2994 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
2997 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3001 ret
= listen(server
, 2);
3004 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3008 ret
= connect(src
, (struct sockaddr
*)&addr
, sizeof(addr
));
3009 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
3011 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3015 ret
= connect(src2
, (struct sockaddr
*)&addr
, sizeof(addr
));
3016 if (ret
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
3018 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3023 dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
3024 if (dst
== INVALID_SOCKET
)
3026 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3031 dst2
= accept(server
, (struct sockaddr
*)&addr
, &len
);
3032 if (dst2
== INVALID_SOCKET
)
3034 ok(0, "creating socket pair failed (%d), skipping test\n", GetLastError());
3038 closesocket(server
);
3039 server
= INVALID_SOCKET
;
3041 /* On Windows it seems when a non-blocking socket sends to a
3042 blocking socket on the same host, the send() is BLOCKING,
3043 so make both sockets non-blocking. src is already non-blocking
3044 from the async select */
3046 if (set_blocking(dst
, FALSE
))
3048 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
3052 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bufferSize
);
3055 ok(0, "could not allocate memory for test\n");
3059 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3060 if (ov
.hEvent
== NULL
)
3062 ok(0, "could not create event object, errno = %d\n", GetLastError());
3066 ov2
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3067 if (ov2
.hEvent
== NULL
)
3069 ok(0, "could not create event object, errno = %d\n", GetLastError());
3073 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
3074 ok_event_seq(src
, hEvent
, connect_seq
, NULL
, 1);
3075 ok_event_seq(src2
, hEvent2
, connect_seq
, NULL
, 1);
3076 /* broken on all windows - FD_CONNECT error is garbage */
3078 /* Test simple send/recv */
3079 ret
= send(dst
, buffer
, 100, 0);
3080 ok(ret
== 100, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3081 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3083 ret
= recv(src
, buffer
, 50, 0);
3084 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3085 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3087 ret
= recv(src
, buffer
, 50, 0);
3088 ok(ret
== 50, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3089 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3091 /* fun fact - events are reenabled even on failure, but only for messages */
3092 ret
= send(dst
, "1", 1, 0);
3093 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3094 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3096 ret
= recv(src
, buffer
, -1, 0);
3097 ok(ret
== SOCKET_ERROR
&& (GetLastError() == WSAEFAULT
|| GetLastError() == WSAENOBUFS
),
3098 "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3101 broken_seq
[0] = empty_seq
; /* win9x */
3102 broken_seq
[1] = NULL
;
3103 todo_wine
ok_event_seq(src
, hEvent
, read_seq
, broken_seq
, 0);
3106 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3108 ret
= recv(src
, buffer
, 1, 0);
3109 ok(ret
== 1, "Failed to recv buffer %d err %d\n", ret
, GetLastError());
3110 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3112 /* Interaction with overlapped */
3113 bufs
.len
= sizeof(char);
3115 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3116 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3117 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3119 bufs
.len
= sizeof(char);
3120 bufs
.buf
= buffer
+1;
3121 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov2
, NULL
);
3122 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3123 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3125 ret
= send(dst
, "12", 2, 0);
3126 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3127 broken_seq
[0] = read_read_seq
; /* win9x */
3128 broken_seq
[1] = NULL
;
3129 /* we like to erase pmask in server, so we have varying behavior here *
3130 * it is only fixed for now because we refuse to send notifications with
3131 * any kind of asyncs requests running */
3132 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
3134 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3135 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3136 if (dwRet
== WAIT_OBJECT_0
)
3138 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
3139 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3140 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3141 ok(buffer
[0] == '1', "Got %c instead of 1\n", buffer
[0]);
3144 dwRet
= WaitForSingleObject(ov2
.hEvent
, 100);
3145 ok(dwRet
== WAIT_OBJECT_0
, "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3146 if (dwRet
== WAIT_OBJECT_0
)
3148 bret
= GetOverlappedResult((HANDLE
)src
, &ov2
, &bytesReturned
, FALSE
);
3149 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3150 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3151 ok(buffer
[1] == '2', "Got %c instead of 2\n", buffer
[1]);
3154 ret
= send(dst
, "1", 1, 0);
3155 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3156 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3158 ret
= recv(src
, buffer
, 1, 0);
3159 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3160 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3162 /* Notifications are delivered as soon as possible, blocked only on
3163 * async requests on the same type */
3164 bufs
.len
= sizeof(char);
3166 ret
= WSARecv(src
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3167 ok(ret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
,
3168 "WSARecv failed - %d error %d\n", ret
, GetLastError());
3171 ret
= send(dst
, "1", 1, MSG_OOB
);
3172 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3173 ok_event_seq(src
, hEvent
, oob_seq
, NULL
, 0);
3176 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3177 ok(dwRet
== WAIT_TIMEOUT
, "OOB message activated read?: %d - %d\n", dwRet
, GetLastError());
3179 ret
= send(dst
, "2", 1, 0);
3180 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3181 broken_seq
[0] = read_seq
; /* win98 */
3182 broken_seq
[1] = NULL
;
3183 ok_event_seq(src
, hEvent
, empty_seq
, broken_seq
, 0);
3185 dwRet
= WaitForSingleObject(ov
.hEvent
, 100);
3186 ok(dwRet
== WAIT_OBJECT_0
|| broken(dwRet
== WAIT_TIMEOUT
),
3187 "Failed to wait for recv message: %d - %d\n", dwRet
, GetLastError());
3188 if (dwRet
== WAIT_OBJECT_0
)
3190 bret
= GetOverlappedResult((HANDLE
)src
, &ov
, &bytesReturned
, FALSE
);
3191 ok((bret
&& bytesReturned
== 1) || broken(!bret
&& GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */,
3192 "Got %d instead of 1 (%d - %d)\n", bytesReturned
, bret
, GetLastError());
3193 ok(buffer
[0] == '2', "Got %c instead of 2\n", buffer
[1]);
3195 else if (dwRet
== WAIT_TIMEOUT
)
3197 /* this happens on win98. We get an FD_READ later on the next test */
3198 CancelIo((HANDLE
) src
);
3202 ret
= recv(src
, buffer
, 1, MSG_OOB
);
3203 todo_wine
ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3204 /* We get OOB notification, but no data on wine */
3205 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3208 /* wine gets a stale notifications because of the async ops, clear them.
3209 * remove when sending messages during pending asyncs is fixed */
3210 ret
= send(dst
, "2", 1, 0);
3211 ok(ret
== 1, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3212 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3214 ret
= recv(src
, buffer
, 1, 0);
3215 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3216 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3218 /* Flood the send queue */
3219 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
3220 if (hThread
== NULL
)
3222 ok(0, "CreateThread failed, error %d\n", GetLastError());
3226 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
3227 ok_event_seq(src
, hEvent
, empty_seq
, NULL
, 0);
3229 /* Now if we send a ton of data and the 'server' does not drain it fast
3230 * enough (set drain_pause to be sure), the socket send buffer will only
3231 * take some of it, and we will get a short write. This will trigger
3232 * another FD_WRITE event as soon as data is sent and more space becomes
3233 * available, but not any earlier. */
3237 ret
= send(src
, buffer
, bufferSize
, 0);
3238 } while (ret
== bufferSize
);
3240 if (ret
>= 0 || WSAGetLastError() == WSAEWOULDBLOCK
)
3242 Sleep(400); /* win9x */
3243 broken_seq
[0] = read_write_seq
;
3244 broken_seq
[1] = NULL
;
3245 ok_event_seq(src
, hEvent
, write_seq
, broken_seq
, 0);
3249 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
3252 /* Test how FD_CLOSE is handled */
3253 ret
= send(dst
, "12", 2, 0);
3254 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3256 /* Wait a little and let the send complete */
3259 dst
= INVALID_SOCKET
;
3262 /* We can never implement this in wine, best we can hope for is
3263 sending FD_CLOSE after the reads complete */
3264 broken_seq
[0] = read_seq
; /* win9x */
3265 broken_seq
[1] = NULL
;
3266 todo_wine
ok_event_seq(src
, hEvent
, read_close_seq
, broken_seq
, 0);
3268 ret
= recv(src
, buffer
, 1, 0);
3269 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3270 ok_event_seq(src
, hEvent
, read_seq
, NULL
, 0);
3272 ret
= recv(src
, buffer
, 1, 0);
3273 ok(ret
== 1, "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3274 /* want it? it's here, but you can't have it */
3275 broken_seq
[0] = close_seq
; /* win9x */
3276 broken_seq
[1] = NULL
;
3277 todo_wine
ok_event_seq(src
, hEvent
, empty_seq
, /* wine sends FD_CLOSE here */
3280 /* Test how FD_CLOSE is handled */
3281 ret
= send(dst2
, "12", 2, 0);
3282 ok(ret
== 2, "Failed to send buffer %d err %d\n", ret
, GetLastError());
3285 shutdown(dst2
, SD_SEND
);
3288 /* Some of the below are technically todo_wine, but our event sequence is still valid, so to prevent
3289 regressions, don't mark them as todo_wine, and mark windows as broken */
3290 broken_seq
[0] = read_close_seq
;
3291 broken_seq
[1] = close_seq
;
3292 broken_seq
[2] = NULL
;
3293 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
3295 ret
= recv(src2
, buffer
, 1, 0);
3296 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3297 broken_seq
[0] = close_seq
; /* win98 */
3298 broken_seq
[1] = NULL
;
3299 ok_event_seq(src2
, hEvent2
, read_seq
, broken_seq
, 0);
3301 ret
= recv(src2
, buffer
, 1, 0);
3302 ok(ret
== 1 || broken(!ret
), "Failed to empty buffer: %d - %d\n", ret
, GetLastError());
3303 broken_seq
[0] = empty_seq
;
3304 broken_seq
[1] = NULL
;
3305 ok_event_seq(src2
, hEvent2
, close_seq
, broken_seq
, 0);
3307 ret
= send(src2
, "1", 1, 0);
3308 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
3309 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
3311 ret
= send(src2
, "1", 1, 0);
3312 ok(ret
== 1, "Sending to half-closed socket failed %d err %d\n", ret
, GetLastError());
3313 ok_event_seq(src2
, hEvent2
, empty_seq
, NULL
, 0);
3316 if (src
!= INVALID_SOCKET
)
3318 flush_events(src
, hEvent
);
3321 if (src2
!= INVALID_SOCKET
)
3323 flush_events(src2
, hEvent2
);
3326 HeapFree(GetProcessHeap(), 0, buffer
);
3327 if (server
!= INVALID_SOCKET
)
3328 closesocket(server
);
3329 if (dst
!= INVALID_SOCKET
)
3331 if (dst2
!= INVALID_SOCKET
)
3333 if (hThread
!= NULL
)
3334 CloseHandle(hThread
);
3338 CloseHandle(hEvent
);
3339 if (hEvent2
!= NULL
)
3340 CloseHandle(hEvent2
);
3341 if (ov
.hEvent
!= NULL
)
3342 CloseHandle(ov
.hEvent
);
3343 if (ov2
.hEvent
!= NULL
)
3344 CloseHandle(ov2
.hEvent
);
3347 static void test_ipv6only(void)
3349 SOCKET v4
= INVALID_SOCKET
,
3350 v6
= INVALID_SOCKET
;
3351 struct sockaddr_in sin4
;
3352 struct sockaddr_in6 sin6
;
3355 memset(&sin4
, 0, sizeof(sin4
));
3356 sin4
.sin_family
= AF_INET
;
3357 sin4
.sin_port
= htons(SERVERPORT
);
3359 memset(&sin6
, 0, sizeof(sin6
));
3360 sin6
.sin6_family
= AF_INET6
;
3361 sin6
.sin6_port
= htons(SERVERPORT
);
3363 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
3364 if (v6
== INVALID_SOCKET
) {
3365 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
3366 WSAGetLastError(), WSAEAFNOSUPPORT
);
3369 ret
= bind(v6
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
3371 skip("Could not bind IPv6 address (LastError: %d).\n",
3376 v4
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
3377 if (v4
== INVALID_SOCKET
) {
3378 skip("Could not create IPv4 socket (LastError: %d).\n",
3382 ret
= bind(v4
, (struct sockaddr
*)&sin4
, sizeof(sin4
));
3383 ok(!ret
, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
3384 WSAGetLastError(), WSAEADDRINUSE
);
3387 if (v4
!= INVALID_SOCKET
)
3389 if (v6
!= INVALID_SOCKET
)
3393 static void test_WSASendTo(void)
3396 struct sockaddr_in addr
;
3397 char buf
[12] = "hello world";
3401 addr
.sin_family
= AF_INET
;
3402 addr
.sin_port
= htons(139);
3403 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3404 data_buf
.len
= sizeof(buf
);
3407 if( (s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
) {
3408 ok(0, "socket() failed error: %d\n", WSAGetLastError());
3412 WSASetLastError(12345);
3413 if(WSASendTo(s
, &data_buf
, 1, &bytesSent
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
)) {
3414 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
3417 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
3418 "a successful call to WSASendTo()\n");
3421 static void test_WSARecv(void)
3427 DWORD bytesReturned
;
3434 tcp_socketpair(&src
, &dest
);
3435 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
3437 skip("failed to create sockets\n");
3441 bufs
.len
= sizeof(buf
);
3445 memset(&ov
, 0, sizeof(ov
));
3446 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3447 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
3453 ok(!setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
)), "Failed to set linger %d\n", GetLastError());
3455 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
3456 ok(iret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
, "WSARecv failed - %d error %d\n", iret
, GetLastError());
3459 src
= INVALID_SOCKET
;
3461 dwret
= WaitForSingleObject(ov
.hEvent
, 1000);
3462 ok(dwret
== WAIT_OBJECT_0
, "Waiting for disconnect event failed with %d + errno %d\n", dwret
, GetLastError());
3464 bret
= GetOverlappedResult((HANDLE
)dest
, &ov
, &bytesReturned
, FALSE
);
3465 todo_wine
ok(!bret
&& (GetLastError() == ERROR_NETNAME_DELETED
|| broken(GetLastError() == ERROR_IO_INCOMPLETE
) /* win9x */),
3466 "Did not get disconnect event: %d, error %d\n", bret
, GetLastError());
3467 ok(bytesReturned
== 0, "Bytes received is %d\n", bytesReturned
);
3470 if (dest
!= INVALID_SOCKET
)
3472 if (src
!= INVALID_SOCKET
)
3475 WSACloseEvent(ov
.hEvent
);
3478 static void test_GetAddrInfoW(void)
3480 static const WCHAR port
[] = {'8','0',0};
3481 static const WCHAR empty
[] = {0};
3482 static const WCHAR localhost
[] = {'l','o','c','a','l','h','o','s','t',0};
3485 ADDRINFOW
*result
, hint
;
3487 if (!pGetAddrInfoW
|| !pFreeAddrInfoW
)
3489 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
3493 memset(&hint
, 0, sizeof(ADDRINFOW
));
3495 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
3496 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
3499 ret
= pGetAddrInfoW(empty
, NULL
, NULL
, &result
);
3502 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3503 ok(result
!= NULL
, "GetAddrInfoW failed\n");
3505 pFreeAddrInfoW(result
);
3507 ret
= pGetAddrInfoW(localhost
, NULL
, NULL
, &result
);
3508 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3509 pFreeAddrInfoW(result
);
3511 ret
= pGetAddrInfoW(localhost
, port
, NULL
, &result
);
3512 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3513 pFreeAddrInfoW(result
);
3515 ret
= pGetAddrInfoW(localhost
, port
, &hint
, &result
);
3516 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
3517 pFreeAddrInfoW(result
);
3520 static void test_AcceptEx(void)
3522 SOCKET listener
= INVALID_SOCKET
;
3523 SOCKET acceptor
= INVALID_SOCKET
;
3524 SOCKET connector
= INVALID_SOCKET
;
3525 SOCKET connector2
= INVALID_SOCKET
;
3526 struct sockaddr_in bindAddress
;
3528 GUID acceptExGuid
= WSAID_ACCEPTEX
;
3529 LPFN_ACCEPTEX pAcceptEx
= NULL
;
3530 fd_set fds_accept
, fds_send
;
3531 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
3533 DWORD bytesReturned
;
3535 OVERLAPPED overlapped
;
3540 memset(&overlapped
, 0, sizeof(overlapped
));
3542 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
3543 if (listener
== INVALID_SOCKET
) {
3544 skip("could not create listener socket, error %d\n", WSAGetLastError());
3548 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3549 if (acceptor
== INVALID_SOCKET
) {
3550 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3554 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3555 if (connector
== INVALID_SOCKET
) {
3556 skip("could not create connector socket, error %d\n", WSAGetLastError());
3560 memset(&bindAddress
, 0, sizeof(bindAddress
));
3561 bindAddress
.sin_family
= AF_INET
;
3562 bindAddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3563 iret
= bind(listener
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3565 skip("failed to bind, error %d\n", WSAGetLastError());
3569 socklen
= sizeof(bindAddress
);
3570 iret
= getsockname(listener
, (struct sockaddr
*)&bindAddress
, &socklen
);
3572 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
3576 if (set_blocking(listener
, FALSE
)) {
3577 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3581 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
3582 &pAcceptEx
, sizeof(pAcceptEx
), &bytesReturned
, NULL
, NULL
);
3584 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
3588 bret
= pAcceptEx(INVALID_SOCKET
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3589 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3590 &bytesReturned
, &overlapped
);
3591 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid listening socket "
3592 "returned %d + errno %d\n", bret
, WSAGetLastError());
3594 bret
= pAcceptEx(listener
, INVALID_SOCKET
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3595 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3596 &bytesReturned
, &overlapped
);
3597 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on invalid accepting socket "
3598 "returned %d + errno %d\n", bret
, WSAGetLastError());
3600 bret
= pAcceptEx(listener
, acceptor
, NULL
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3601 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3602 &bytesReturned
, &overlapped
);
3604 (WSAGetLastError() == WSAEINVAL
||
3605 broken(WSAGetLastError() == WSAEFAULT
)), /* NT4 */
3606 "AcceptEx on NULL buffer returned %d + errno %d\n", bret
, WSAGetLastError());
3608 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, sizeof(struct sockaddr_in
) + 16,
3609 &bytesReturned
, &overlapped
);
3610 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small local address size "
3611 "returned %d + errno %d\n", bret
, WSAGetLastError());
3613 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16, 0,
3614 &bytesReturned
, &overlapped
);
3615 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small remote address size "
3616 "returned %d + errno %d\n", bret
, WSAGetLastError());
3618 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3619 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3620 &bytesReturned
, NULL
);
3621 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
3622 "returned %d + errno %d\n", bret
, WSAGetLastError());
3624 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3625 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3626 &bytesReturned
, &overlapped
);
3627 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on a non-listening socket "
3628 "returned %d + errno %d\n", bret
, WSAGetLastError());
3630 iret
= listen(listener
, 5);
3632 skip("listening failed, errno = %d\n", WSAGetLastError());
3636 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3637 if (overlapped
.hEvent
== NULL
) {
3638 skip("could not create event object, errno = %d\n", GetLastError());
3642 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3643 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3644 &bytesReturned
, &overlapped
);
3645 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3647 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3648 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3649 &bytesReturned
, &overlapped
);
3650 ok((bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
) || broken(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) /* NT4 */,
3651 "AcceptEx on already pending socket returned %d + errno %d\n", bret
, WSAGetLastError());
3652 if (bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) {
3653 /* We need to cancel this call, otherwise things fail */
3654 bret
= CancelIo((HANDLE
) listener
);
3655 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3658 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3659 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3660 &bytesReturned
, &overlapped
);
3661 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3664 iret
= connect(acceptor
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3665 ok((iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINVAL
) || broken(!iret
) /* NT4 */,
3666 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret
, WSAGetLastError());
3667 if (!iret
|| (iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
)) {
3668 /* We need to cancel this call, otherwise things fail */
3669 closesocket(acceptor
);
3670 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3671 if (acceptor
== INVALID_SOCKET
) {
3672 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3676 bret
= CancelIo((HANDLE
) listener
);
3677 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3680 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3681 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3682 &bytesReturned
, &overlapped
);
3683 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3686 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3687 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3689 dwret
= WaitForSingleObject(overlapped
.hEvent
, INFINITE
);
3690 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3692 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3693 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3694 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3696 closesocket(connector
);
3697 connector
= INVALID_SOCKET
;
3698 closesocket(acceptor
);
3699 acceptor
= INVALID_SOCKET
;
3701 /* Test short reads */
3703 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3704 if (acceptor
== INVALID_SOCKET
) {
3705 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3708 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3709 if (connector
== INVALID_SOCKET
) {
3710 skip("could not create connector socket, error %d\n", WSAGetLastError());
3713 bret
= pAcceptEx(listener
, acceptor
, buffer
, 2,
3714 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3715 &bytesReturned
, &overlapped
);
3716 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3718 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3719 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3721 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3722 ok(dwret
== WAIT_TIMEOUT
, "Waiting for accept event timeout failed with %d + errno %d\n", dwret
, GetLastError());
3724 iret
= send(connector
, buffer
, 1, 0);
3725 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
3727 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3728 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3730 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3731 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3732 ok(bytesReturned
== 1, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3734 closesocket(connector
);
3735 connector
= INVALID_SOCKET
;
3736 closesocket(acceptor
);
3737 acceptor
= INVALID_SOCKET
;
3739 /* Test CF_DEFER & AcceptEx interaction */
3741 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3742 if (acceptor
== INVALID_SOCKET
) {
3743 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3746 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3747 if (connector
== INVALID_SOCKET
) {
3748 skip("could not create connector socket, error %d\n", WSAGetLastError());
3751 connector2
= socket(AF_INET
, SOCK_STREAM
, 0);
3752 if (connector
== INVALID_SOCKET
) {
3753 skip("could not create connector socket, error %d\n", WSAGetLastError());
3757 if (set_blocking(connector
, FALSE
)) {
3758 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3762 if (set_blocking(connector2
, FALSE
)) {
3763 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3767 /* Connect socket #1 */
3768 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3769 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3771 FD_ZERO ( &fds_accept
);
3772 FD_ZERO ( &fds_send
);
3774 FD_SET ( listener
, &fds_accept
);
3775 FD_SET ( connector
, &fds_send
);
3781 for (i
= 0; i
< 4000; ++i
)
3783 fd_set fds_openaccept
= fds_accept
, fds_opensend
= fds_send
;
3785 wsa_ok ( ( select ( 0, &fds_openaccept
, &fds_opensend
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
3786 "acceptex test(%d): could not select on socket, errno %d\n" );
3788 /* check for incoming requests */
3789 if ( FD_ISSET ( listener
, &fds_openaccept
) ) {
3792 SOCKET tmp
= WSAAccept(listener
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
3793 ok(tmp
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
3794 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3795 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3796 &bytesReturned
, &overlapped
);
3797 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3799 else if (got
== 2) {
3800 /* this should be socket #2 */
3801 SOCKET tmp
= accept(listener
, NULL
, NULL
);
3802 ok(tmp
!= INVALID_SOCKET
, "accept failed %d\n", WSAGetLastError());
3806 ok(FALSE
, "Got more than 2 connections?\n");
3809 if ( conn1
&& FD_ISSET ( connector2
, &fds_opensend
) ) {
3810 /* Send data on second socket, and stop */
3811 send(connector2
, "2", 1, 0);
3812 FD_CLR ( connector2
, &fds_send
);
3816 if ( FD_ISSET ( connector
, &fds_opensend
) ) {
3817 /* Once #1 is connected, allow #2 to connect */
3820 send(connector
, "1", 1, 0);
3821 FD_CLR ( connector
, &fds_send
);
3823 iret
= connect(connector2
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3824 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3825 FD_SET ( connector2
, &fds_send
);
3829 ok (got
== 2 || broken(got
== 1) /* NT4 */,
3830 "Did not get both connections, got %d\n", got
);
3832 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3833 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3835 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3836 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3837 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3839 set_blocking(acceptor
, TRUE
);
3840 iret
= recv( acceptor
, buffer
, 2, 0);
3841 ok(iret
== 1, "Failed to get data, %d, errno: %d\n", iret
, WSAGetLastError());
3843 ok(buffer
[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer
[0]);
3845 closesocket(connector
);
3846 connector
= INVALID_SOCKET
;
3847 closesocket(acceptor
);
3848 acceptor
= INVALID_SOCKET
;
3850 /* clean up in case of failures */
3851 while ((acceptor
= accept(listener
, NULL
, NULL
)) != INVALID_SOCKET
)
3852 closesocket(acceptor
);
3854 /* Disconnect during receive? */
3856 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3857 if (acceptor
== INVALID_SOCKET
) {
3858 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3861 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3862 if (connector
== INVALID_SOCKET
) {
3863 skip("could not create connector socket, error %d\n", WSAGetLastError());
3866 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3867 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3868 &bytesReturned
, &overlapped
);
3869 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3871 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3872 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3874 closesocket(connector
);
3875 connector
= INVALID_SOCKET
;
3877 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3878 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3880 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3881 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3882 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3884 closesocket(acceptor
);
3885 acceptor
= INVALID_SOCKET
;
3887 /* Test closing with pending requests */
3889 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3890 if (acceptor
== INVALID_SOCKET
) {
3891 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3894 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3895 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3896 &bytesReturned
, &overlapped
);
3897 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3899 closesocket(acceptor
);
3900 acceptor
= INVALID_SOCKET
;
3902 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3903 ok(dwret
== WAIT_OBJECT_0
|| broken(dwret
== WAIT_TIMEOUT
) /* NT4/2000 */,
3904 "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3906 if (dwret
!= WAIT_TIMEOUT
) {
3907 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3908 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
3911 bret
= CancelIo((HANDLE
) listener
);
3912 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3916 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3917 if (acceptor
== INVALID_SOCKET
) {
3918 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3921 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3922 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3923 &bytesReturned
, &overlapped
);
3924 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3926 CancelIo((HANDLE
) acceptor
);
3928 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3929 ok(dwret
== WAIT_TIMEOUT
, "Waiting for timeout failed with %d + errno %d\n", dwret
, GetLastError());
3931 closesocket(acceptor
);
3932 acceptor
= INVALID_SOCKET
;
3934 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3935 if (acceptor
== INVALID_SOCKET
) {
3936 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3939 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3940 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3941 &bytesReturned
, &overlapped
);
3942 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3944 closesocket(listener
);
3945 listener
= INVALID_SOCKET
;
3947 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3948 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3950 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3951 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
3954 if (overlapped
.hEvent
)
3955 WSACloseEvent(overlapped
.hEvent
);
3956 if (listener
!= INVALID_SOCKET
)
3957 closesocket(listener
);
3958 if (acceptor
!= INVALID_SOCKET
)
3959 closesocket(acceptor
);
3960 if (connector
!= INVALID_SOCKET
)
3961 closesocket(connector
);
3962 if (connector2
!= INVALID_SOCKET
)
3963 closesocket(connector2
);
3966 static void test_getpeername(void)
3969 struct sockaddr_in sa
, sa_out
;
3971 const char buf
[] = "hello world";
3974 /* Test the parameter validation order. */
3975 ret
= getpeername(INVALID_SOCKET
, NULL
, NULL
);
3976 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3977 ok(WSAGetLastError() == WSAENOTSOCK
,
3978 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
3980 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
3981 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
3982 if (sock
== INVALID_SOCKET
)
3984 skip("Socket creation failed with %d\n", WSAGetLastError());
3988 ret
= getpeername(sock
, NULL
, NULL
);
3989 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3990 ok(WSAGetLastError() == WSAENOTCONN
||
3991 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
3992 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
3994 memset(&sa
, 0, sizeof(sa
));
3995 sa
.sin_family
= AF_INET
;
3996 sa
.sin_port
= htons(139);
3997 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3999 /* sendto does not change a socket's connection state. */
4000 ret
= sendto(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sa
, sizeof(sa
));
4001 ok(ret
!= SOCKET_ERROR
,
4002 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4004 ret
= getpeername(sock
, NULL
, NULL
);
4005 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4006 ok(WSAGetLastError() == WSAENOTCONN
||
4007 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
4008 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
4010 ret
= connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
));
4012 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
4014 ret
= getpeername(sock
, NULL
, NULL
);
4015 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4016 ok(WSAGetLastError() == WSAEFAULT
,
4017 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4019 /* Test crashes on Wine. */
4022 ret
= getpeername(sock
, (void*)0xdeadbeef, (void*)0xcafebabe);
4023 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
4024 ok(WSAGetLastError() == WSAEFAULT
,
4025 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4029 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
4030 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
4031 ok(WSAGetLastError() == WSAEFAULT
,
4032 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
4034 sa_len
= sizeof(sa_out
);
4035 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
4036 ok(ret
== 0, "Expected getpeername to return 0, got %d\n", ret
);
4037 ok(!memcmp(&sa
, &sa_out
, sizeof(sa
)),
4038 "Expected the returned structure to be identical to the connect structure\n");
4043 /**************** Main program ***************/
4049 /* Leave these tests at the beginning. They depend on WSAStartup not having been
4050 * called, which is done by Init() below. */
4051 test_WithoutWSAStartup();
4052 test_WithWSAStartup();
4056 test_set_getsockopt();
4057 test_so_reuseaddr();
4058 test_extendedSocketOptions();
4060 for (i
= 0; i
< NUM_TESTS
; i
++)
4062 trace ( " **** STARTING TEST %d ****\n", i
);
4063 do_test ( &tests
[i
] );
4064 trace ( " **** TEST %d COMPLETE ****\n", i
);
4069 test_getservbyname();
4072 test_WSAAddressToStringA();
4073 test_WSAAddressToStringW();
4075 test_WSAStringToAddressA();
4076 test_WSAStringToAddressW();
4083 test_addr_to_print();
4086 test_gethostbyname_hack();
4095 test_GetAddrInfoW();
4099 /* this is a io heavy test, do it at the end so the kernel doesn't start dropping packets */