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
26 #define WIN32_NO_STATUS
32 #include "wine/test.h"
36 #define MAX_CLIENTS 4 /* Max number of clients */
37 #define NUM_TESTS 4 /* Number of tests performed */
38 #define FIRST_CHAR 'A' /* First character in transferred pattern */
39 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
40 #define BIND_TRIES 6 /* Number of bind() attempts */
41 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
42 after server initialization, if something hangs */
44 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
46 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
47 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
49 #define SERVERIP "127.0.0.1" /* IP to bind to */
50 #define SERVERPORT 9374 /* Port number to bind to */
52 #define wsa_ok(op, cond, msg) \
56 if ( !(cond tmp) ) err = WSAGetLastError(); \
57 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
60 /* Function pointers */
61 static void (WINAPI
*pFreeAddrInfoW
)(PADDRINFOW
) = 0;
62 static int (WINAPI
*pGetAddrInfoW
)(LPCWSTR
,LPCWSTR
,const ADDRINFOW
*,PADDRINFOW
*) = 0;
63 static PCSTR (WINAPI
*pInetNtop
)(INT
,LPVOID
,LPSTR
,ULONG
) = 0;
65 /**************** Structs and typedefs ***************/
67 typedef struct thread_info
73 /* Information in the server about open client connections */
74 typedef struct sock_info
77 struct sockaddr_in addr
;
78 struct sockaddr_in peer
;
84 /* Test parameters for both server & client */
85 typedef struct test_params
89 const char *inet_addr
;
96 /* server-specific test parameters */
97 typedef struct server_params
104 /* client-specific test parameters */
105 typedef struct client_params
107 test_params
*general
;
112 /* This type combines all information for setting up a test scenario */
113 typedef struct test_setup
117 server_params srv_params
;
119 client_params clt_params
;
122 /* Thread local storage for server */
123 typedef struct server_memory
126 struct sockaddr_in addr
;
127 sock_info sock
[MAX_CLIENTS
];
130 /* Thread local storage for client */
131 typedef struct client_memory
134 struct sockaddr_in addr
;
139 /* SelectReadThread thread parameters */
140 typedef struct select_thread_params
144 } select_thread_params
;
146 /**************** Static variables ***************/
148 static DWORD tls
; /* Thread local storage index */
149 static HANDLE thread
[1+MAX_CLIENTS
];
150 static DWORD thread_id
[1+MAX_CLIENTS
];
151 static HANDLE server_ready
;
152 static HANDLE client_ready
[MAX_CLIENTS
];
153 static int client_id
;
155 /**************** General utility functions ***************/
157 static int tcp_socketpair(SOCKET
*src
, SOCKET
*dst
)
159 SOCKET server
= INVALID_SOCKET
;
160 struct sockaddr_in addr
;
164 *src
= INVALID_SOCKET
;
165 *dst
= INVALID_SOCKET
;
167 *src
= socket(AF_INET
, SOCK_STREAM
, 0);
168 if (*src
== INVALID_SOCKET
)
171 server
= socket(AF_INET
, SOCK_STREAM
, 0);
172 if (server
== INVALID_SOCKET
)
175 memset(&addr
, 0, sizeof(addr
));
176 addr
.sin_family
= AF_INET
;
177 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
178 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
183 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
187 ret
= listen(server
, 1);
191 ret
= connect(*src
, (struct sockaddr
*)&addr
, sizeof(addr
));
196 *dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
199 if (server
!= INVALID_SOCKET
)
201 if (*src
!= INVALID_SOCKET
&& *dst
!= INVALID_SOCKET
)
208 static void set_so_opentype ( BOOL overlapped
)
210 int optval
= !overlapped
, newval
, len
= sizeof (int);
212 ok ( setsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
213 (LPVOID
) &optval
, sizeof (optval
) ) == 0,
214 "setting SO_OPENTYPE failed\n" );
215 ok ( getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
216 (LPVOID
) &newval
, &len
) == 0,
217 "getting SO_OPENTYPE failed\n" );
218 ok ( optval
== newval
, "failed to set SO_OPENTYPE\n" );
221 static int set_blocking ( SOCKET s
, BOOL blocking
)
223 u_long val
= !blocking
;
224 return ioctlsocket ( s
, FIONBIO
, &val
);
227 static void fill_buffer ( char *buf
, int chunk_size
, int n_chunks
)
230 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
231 memset ( p
, c
, chunk_size
);
234 static int test_buffer ( char *buf
, int chunk_size
, int n_chunks
)
238 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
240 for ( i
= 0; i
< chunk_size
; i
++ )
241 if ( p
[i
] != c
) return i
;
247 * This routine is called when a client / server does not expect any more data,
248 * but needs to acknowledge the closing of the connection (by reading 0 bytes).
250 static void read_zero_bytes ( SOCKET s
)
254 while ( ( tmp
= recv ( s
, buf
, 256, 0 ) ) > 0 )
256 ok ( n
<= 0, "garbage data received: %d bytes\n", n
);
259 static int do_synchronous_send ( SOCKET s
, char *buf
, int buflen
, int sendlen
)
261 char* last
= buf
+ buflen
, *p
;
263 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
264 n
= send ( s
, p
, min ( sendlen
, last
- p
), 0 );
265 wsa_ok ( n
, 0 <=, "do_synchronous_send (%x): error %d\n" );
269 static int do_synchronous_recv ( SOCKET s
, char *buf
, int buflen
, int recvlen
)
271 char* last
= buf
+ buflen
, *p
;
273 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
274 n
= recv ( s
, p
, min ( recvlen
, last
- p
), 0 );
275 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
279 static int do_synchronous_recvfrom ( SOCKET s
, char *buf
, int buflen
,int flags
,struct sockaddr
*from
, int *fromlen
, int recvlen
)
281 char* last
= buf
+ buflen
, *p
;
283 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
284 n
= recvfrom ( s
, p
, min ( recvlen
, last
- p
), 0, from
, fromlen
);
285 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
290 * Call this routine right after thread startup.
291 * SO_OPENTYPE must by 0, regardless what the server did.
293 static void check_so_opentype (void)
297 getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (LPVOID
) &tmp
, &len
);
298 ok ( tmp
== 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp
);
301 /**************** Server utility functions ***************/
304 * Even if we have closed our server socket cleanly,
305 * the OS may mark the address "in use" for some time -
306 * this happens with native Linux apps, too.
308 static void do_bind ( SOCKET s
, struct sockaddr
* addr
, int addrlen
)
310 int err
, wsaerr
= 0, n_try
= BIND_TRIES
;
312 while ( ( err
= bind ( s
, addr
, addrlen
) ) != 0 &&
313 ( wsaerr
= WSAGetLastError () ) == WSAEADDRINUSE
&&
316 trace ( "address in use, waiting ...\n" );
317 Sleep ( 1000 * BIND_SLEEP
);
319 ok ( err
== 0, "failed to bind: %d\n", wsaerr
);
322 static void server_start ( server_params
*par
)
325 test_params
*gen
= par
->general
;
326 server_memory
*mem
= LocalAlloc ( LPTR
, sizeof ( server_memory
) );
328 TlsSetValue ( tls
, mem
);
329 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
330 NULL
, 0, par
->sock_flags
);
331 ok ( mem
->s
!= INVALID_SOCKET
, "Server: WSASocket failed\n" );
333 mem
->addr
.sin_family
= AF_INET
;
334 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
335 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
337 for (i
= 0; i
< MAX_CLIENTS
; i
++)
339 mem
->sock
[i
].s
= INVALID_SOCKET
;
340 mem
->sock
[i
].buf
= LocalAlloc ( LPTR
, gen
->n_chunks
* gen
->chunk_size
);
341 mem
->sock
[i
].n_recvd
= 0;
342 mem
->sock
[i
].n_sent
= 0;
345 if ( gen
->sock_type
== SOCK_STREAM
)
346 do_bind ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof (mem
->addr
) );
349 static void server_stop (void)
352 server_memory
*mem
= TlsGetValue ( tls
);
354 for (i
= 0; i
< MAX_CLIENTS
; i
++ )
356 LocalFree ( mem
->sock
[i
].buf
);
357 if ( mem
->sock
[i
].s
!= INVALID_SOCKET
)
358 closesocket ( mem
->sock
[i
].s
);
360 ok ( closesocket ( mem
->s
) == 0, "closesocket failed\n" );
362 ExitThread ( GetCurrentThreadId () );
365 /**************** Client utilitiy functions ***************/
367 static void client_start ( client_params
*par
)
369 test_params
*gen
= par
->general
;
370 client_memory
*mem
= LocalAlloc (LPTR
, sizeof (client_memory
));
372 TlsSetValue ( tls
, mem
);
374 WaitForSingleObject ( server_ready
, INFINITE
);
376 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
377 NULL
, 0, par
->sock_flags
);
379 mem
->addr
.sin_family
= AF_INET
;
380 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
381 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
383 ok ( mem
->s
!= INVALID_SOCKET
, "Client: WSASocket failed\n" );
385 mem
->send_buf
= LocalAlloc ( LPTR
, 2 * gen
->n_chunks
* gen
->chunk_size
);
386 mem
->recv_buf
= mem
->send_buf
+ gen
->n_chunks
* gen
->chunk_size
;
387 fill_buffer ( mem
->send_buf
, gen
->chunk_size
, gen
->n_chunks
);
389 SetEvent ( client_ready
[client_id
] );
390 /* Wait for the other clients to come up */
391 WaitForMultipleObjects ( min ( gen
->n_clients
, MAX_CLIENTS
), client_ready
, TRUE
, INFINITE
);
394 static void client_stop (void)
396 client_memory
*mem
= TlsGetValue ( tls
);
397 wsa_ok ( closesocket ( mem
->s
), 0 ==, "closesocket error (%x): %d\n" );
398 LocalFree ( mem
->send_buf
);
403 /**************** Servers ***************/
406 * simple_server: A very basic server doing synchronous IO.
408 static VOID WINAPI
simple_server ( server_params
*par
)
410 test_params
*gen
= par
->general
;
412 int pos
, n_recvd
, n_sent
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
413 id
= GetCurrentThreadId();
415 trace ( "simple_server (%x) starting\n", id
);
417 set_so_opentype ( FALSE
); /* non-overlapped */
418 server_start ( par
);
419 mem
= TlsGetValue ( tls
);
421 wsa_ok ( set_blocking ( mem
->s
, TRUE
), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
422 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "simple_server (%x): listen failed: %d\n");
424 trace ( "simple_server (%x) ready\n", id
);
425 SetEvent ( server_ready
); /* notify clients */
427 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
429 trace ( "simple_server (%x): waiting for client\n", id
);
431 /* accept a single connection */
432 tmp
= sizeof ( mem
->sock
[0].peer
);
433 mem
->sock
[0].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[0].peer
, &tmp
);
434 wsa_ok ( mem
->sock
[0].s
, INVALID_SOCKET
!=, "simple_server (%x): accept failed: %d\n" );
436 ok ( mem
->sock
[0].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
437 "simple_server (%x): strange peer address\n", id
);
439 /* Receive data & check it */
440 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
441 ok ( n_recvd
== n_expected
,
442 "simple_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
443 pos
= test_buffer ( mem
->sock
[0].buf
, gen
->chunk_size
, gen
->n_chunks
);
444 ok ( pos
== -1, "simple_server (%x): test pattern error: %d\n", id
, pos
);
447 n_sent
= do_synchronous_send ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
448 ok ( n_sent
== n_expected
,
449 "simple_server (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
452 read_zero_bytes ( mem
->sock
[0].s
);
453 wsa_ok ( closesocket ( mem
->sock
[0].s
), 0 ==, "simple_server (%x): closesocket error: %d\n" );
454 mem
->sock
[0].s
= INVALID_SOCKET
;
457 trace ( "simple_server (%x) exiting\n", id
);
462 * select_server: A non-blocking server.
464 static VOID WINAPI
select_server ( server_params
*par
)
466 test_params
*gen
= par
->general
;
468 int n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
469 id
= GetCurrentThreadId(), n_connections
= 0, n_sent
, n_recvd
,
470 n_set
, delta
, n_ready
;
471 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
472 fd_set fds_recv
, fds_send
, fds_openrecv
, fds_opensend
;
474 trace ( "select_server (%x) starting\n", id
);
476 set_so_opentype ( FALSE
); /* non-overlapped */
477 server_start ( par
);
478 mem
= TlsGetValue ( tls
);
480 wsa_ok ( set_blocking ( mem
->s
, FALSE
), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
481 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "select_server (%x): listen failed: %d\n");
483 trace ( "select_server (%x) ready\n", id
);
484 SetEvent ( server_ready
); /* notify clients */
486 FD_ZERO ( &fds_openrecv
);
487 FD_ZERO ( &fds_recv
);
488 FD_ZERO ( &fds_send
);
489 FD_ZERO ( &fds_opensend
);
491 FD_SET ( mem
->s
, &fds_openrecv
);
495 fds_recv
= fds_openrecv
;
496 fds_send
= fds_opensend
;
500 wsa_ok ( ( n_ready
= select ( 0, &fds_recv
, &fds_send
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
501 "select_server (%x): select() failed: %d\n" );
503 /* check for incoming requests */
504 if ( FD_ISSET ( mem
->s
, &fds_recv
) ) {
507 trace ( "select_server (%x): accepting client connection\n", id
);
509 /* accept a single connection */
510 tmp
= sizeof ( mem
->sock
[n_connections
].peer
);
511 mem
->sock
[n_connections
].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[n_connections
].peer
, &tmp
);
512 wsa_ok ( mem
->sock
[n_connections
].s
, INVALID_SOCKET
!=, "select_server (%x): accept() failed: %d\n" );
514 ok ( mem
->sock
[n_connections
].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
515 "select_server (%x): strange peer address\n", id
);
517 /* add to list of open connections */
518 FD_SET ( mem
->sock
[n_connections
].s
, &fds_openrecv
);
519 FD_SET ( mem
->sock
[n_connections
].s
, &fds_opensend
);
524 /* handle open requests */
526 for ( i
= 0; i
< n_connections
; i
++ )
528 if ( FD_ISSET( mem
->sock
[i
].s
, &fds_recv
) ) {
531 if ( mem
->sock
[i
].n_recvd
< n_expected
) {
532 /* Receive data & check it */
533 n_recvd
= recv ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_recvd
, min ( n_expected
- mem
->sock
[i
].n_recvd
, par
->buflen
), 0 );
534 ok ( n_recvd
!= SOCKET_ERROR
, "select_server (%x): error in recv(): %d\n", id
, WSAGetLastError() );
535 mem
->sock
[i
].n_recvd
+= n_recvd
;
537 if ( mem
->sock
[i
].n_recvd
== n_expected
) {
538 int pos
= test_buffer ( mem
->sock
[i
].buf
, gen
->chunk_size
, gen
->n_chunks
);
539 ok ( pos
== -1, "select_server (%x): test pattern error: %d\n", id
, pos
);
540 FD_CLR ( mem
->sock
[i
].s
, &fds_openrecv
);
543 ok ( mem
->sock
[i
].n_recvd
<= n_expected
, "select_server (%x): received too many bytes: %d\n", id
, mem
->sock
[i
].n_recvd
);
547 /* only echo back what we've received */
548 delta
= mem
->sock
[i
].n_recvd
- mem
->sock
[i
].n_sent
;
550 if ( FD_ISSET ( mem
->sock
[i
].s
, &fds_send
) ) {
553 if ( ( delta
> 0 ) && ( mem
->sock
[i
].n_sent
< n_expected
) ) {
555 n_sent
= send ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_sent
, min ( delta
, par
->buflen
), 0 );
556 ok ( n_sent
!= SOCKET_ERROR
, "select_server (%x): error in send(): %d\n", id
, WSAGetLastError() );
557 mem
->sock
[i
].n_sent
+= n_sent
;
559 if ( mem
->sock
[i
].n_sent
== n_expected
) {
560 FD_CLR ( mem
->sock
[i
].s
, &fds_opensend
);
563 ok ( mem
->sock
[i
].n_sent
<= n_expected
, "select_server (%x): sent too many bytes: %d\n", id
, mem
->sock
[i
].n_sent
);
568 /* check that select returned the correct number of ready sockets */
569 ok ( ( n_set
== n_ready
), "select_server (%x): select() returns wrong number of ready sockets\n", id
);
571 /* check if all clients are done */
572 if ( ( fds_opensend
.fd_count
== 0 )
573 && ( fds_openrecv
.fd_count
== 1 ) /* initial socket that accepts clients */
574 && ( n_connections
== min ( gen
->n_clients
, MAX_CLIENTS
) ) ) {
579 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
582 read_zero_bytes ( mem
->sock
[i
].s
);
583 wsa_ok ( closesocket ( mem
->sock
[i
].s
), 0 ==, "select_server (%x): closesocket error: %d\n" );
584 mem
->sock
[i
].s
= INVALID_SOCKET
;
587 trace ( "select_server (%x) exiting\n", id
);
591 /**************** Clients ***************/
594 * simple_client: A very basic client doing synchronous IO.
596 static VOID WINAPI
simple_client ( client_params
*par
)
598 test_params
*gen
= par
->general
;
600 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
602 id
= GetCurrentThreadId();
603 trace ( "simple_client (%x): starting\n", id
);
604 /* wait here because we want to call set_so_opentype before creating a socket */
605 WaitForSingleObject ( server_ready
, INFINITE
);
606 trace ( "simple_client (%x): server ready\n", id
);
608 check_so_opentype ();
609 set_so_opentype ( FALSE
); /* non-overlapped */
610 client_start ( par
);
611 mem
= TlsGetValue ( tls
);
614 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
615 0 ==, "simple_client (%x): connect error: %d\n" );
616 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
617 "simple_client (%x): failed to set blocking mode\n", id
);
618 trace ( "simple_client (%x) connected\n", id
);
620 /* send data to server */
621 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
622 ok ( n_sent
== n_expected
,
623 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
625 /* shutdown send direction */
626 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
628 /* Receive data echoed back & check it */
629 n_recvd
= do_synchronous_recv ( mem
->s
, mem
->recv_buf
, n_expected
, par
->buflen
);
630 ok ( n_recvd
== n_expected
,
631 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
634 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
635 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
638 read_zero_bytes ( mem
->s
);
639 trace ( "simple_client (%x) exiting\n", id
);
644 * simple_mixed_client: mixing send and recvfrom
646 static VOID WINAPI
simple_mixed_client ( client_params
*par
)
648 test_params
*gen
= par
->general
;
650 int pos
, n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
651 int fromLen
= sizeof(mem
->addr
);
652 struct sockaddr test
;
654 id
= GetCurrentThreadId();
655 trace ( "simple_client (%x): starting\n", id
);
656 /* wait here because we want to call set_so_opentype before creating a socket */
657 WaitForSingleObject ( server_ready
, INFINITE
);
658 trace ( "simple_client (%x): server ready\n", id
);
660 check_so_opentype ();
661 set_so_opentype ( FALSE
); /* non-overlapped */
662 client_start ( par
);
663 mem
= TlsGetValue ( tls
);
666 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
667 0 ==, "simple_client (%x): connect error: %d\n" );
668 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
669 "simple_client (%x): failed to set blocking mode\n", id
);
670 trace ( "simple_client (%x) connected\n", id
);
672 /* send data to server */
673 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
674 ok ( n_sent
== n_expected
,
675 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
677 /* shutdown send direction */
678 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
680 /* this shouldn't change, since lpFrom, is not updated on
681 connection oriented sockets - exposed by bug 11640
683 ((struct sockaddr_in
*)&test
)->sin_addr
.s_addr
= inet_addr("0.0.0.0");
685 /* Receive data echoed back & check it */
686 n_recvd
= do_synchronous_recvfrom ( mem
->s
,
690 (struct sockaddr
*)&test
,
693 ok ( n_recvd
== n_expected
,
694 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
696 /* check that lpFrom was not updated */
699 inet_ntoa(((struct sockaddr_in
*)&test
)->sin_addr
),
700 "0.0.0.0"), "lpFrom shouldn't be updated on connection oriented sockets\n");
703 pos
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
704 ok ( pos
== -1, "simple_client (%x): test pattern error: %d\n", id
, pos
);
707 read_zero_bytes ( mem
->s
);
708 trace ( "simple_client (%x) exiting\n", id
);
713 * event_client: An event-driven client
715 static void WINAPI
event_client ( client_params
*par
)
717 test_params
*gen
= par
->general
;
719 int id
= GetCurrentThreadId(), n_expected
= gen
->n_chunks
* gen
->chunk_size
,
722 WSANETWORKEVENTS wsa_events
;
723 char *send_last
, *recv_last
, *send_p
, *recv_p
;
724 LONG mask
= FD_READ
| FD_WRITE
| FD_CLOSE
;
726 trace ( "event_client (%x): starting\n", id
);
727 client_start ( par
);
728 trace ( "event_client (%x): server ready\n", id
);
730 mem
= TlsGetValue ( tls
);
732 /* Prepare event notification for connect, makes socket nonblocking */
733 event
= WSACreateEvent ();
734 WSAEventSelect ( mem
->s
, event
, FD_CONNECT
);
735 tmp
= connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) );
737 err
= WSAGetLastError ();
738 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): connect error: %d\n", id
, err
);
739 tmp
= WaitForSingleObject ( event
, INFINITE
);
740 ok ( tmp
== WAIT_OBJECT_0
, "event_client (%x): wait for connect event failed: %d\n", id
, tmp
);
741 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
742 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
743 err
= wsa_events
.iErrorCode
[ FD_CONNECT_BIT
];
744 ok ( err
== 0, "event_client (%x): connect error: %d\n", id
, err
);
748 trace ( "event_client (%x) connected\n", id
);
750 WSAEventSelect ( mem
->s
, event
, mask
);
752 recv_p
= mem
->recv_buf
;
753 recv_last
= mem
->recv_buf
+ n_expected
;
754 send_p
= mem
->send_buf
;
755 send_last
= mem
->send_buf
+ n_expected
;
759 err
= WaitForSingleObject ( event
, INFINITE
);
760 ok ( err
== WAIT_OBJECT_0
, "event_client (%x): wait failed\n", id
);
762 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
763 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
765 if ( wsa_events
.lNetworkEvents
& FD_WRITE
)
767 err
= wsa_events
.iErrorCode
[ FD_WRITE_BIT
];
768 ok ( err
== 0, "event_client (%x): FD_WRITE error code: %d\n", id
, err
);
773 n
= send ( mem
->s
, send_p
, min ( send_last
- send_p
, par
->buflen
), 0 );
776 err
= WSAGetLastError ();
777 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): send error: %d\n", id
, err
);
782 while ( n
>= 0 && send_p
< send_last
);
784 if ( send_p
== send_last
)
786 trace ( "event_client (%x): all data sent - shutdown\n", id
);
787 shutdown ( mem
->s
, SD_SEND
);
789 WSAEventSelect ( mem
->s
, event
, mask
);
792 if ( wsa_events
.lNetworkEvents
& FD_READ
)
794 err
= wsa_events
.iErrorCode
[ FD_READ_BIT
];
795 ok ( err
== 0, "event_client (%x): FD_READ error code: %d\n", id
, err
);
796 if ( err
!= 0 ) break;
798 /* First read must succeed */
799 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
800 wsa_ok ( n
, 0 <=, "event_client (%x): recv error: %d\n" );
804 if ( recv_p
== recv_last
)
807 trace ( "event_client (%x): all data received\n", id
);
808 WSAEventSelect ( mem
->s
, event
, mask
);
811 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
812 if ( n
< 0 && ( err
= WSAGetLastError()) != WSAEWOULDBLOCK
)
813 ok ( 0, "event_client (%x): read error: %d\n", id
, err
);
817 if ( wsa_events
.lNetworkEvents
& FD_CLOSE
)
819 trace ( "event_client (%x): close event\n", id
);
820 err
= wsa_events
.iErrorCode
[ FD_CLOSE_BIT
];
821 ok ( err
== 0, "event_client (%x): FD_CLOSE error code: %d\n", id
, err
);
826 n
= send_p
- mem
->send_buf
;
827 ok ( send_p
== send_last
,
828 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n
, n_expected
);
829 n
= recv_p
- mem
->recv_buf
;
830 ok ( recv_p
== recv_last
,
831 "simple_client (%x): received less data than expected: %d of %d\n", id
, n
, n_expected
);
832 n
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
833 ok ( n
== -1, "event_client (%x): test pattern error: %d\n", id
, n
);
836 WSACloseEvent ( event
);
837 trace ( "event_client (%x) exiting\n", id
);
841 /* Tests for WSAStartup */
843 /* This should fail. WSAStartup should be called before any network function is used. */
844 static void test_WithoutWSAStartup(void)
848 WSASetLastError(0xdeadbeef);
849 ptr
= gethostbyname("localhost");
851 todo_wine
ok(ptr
== NULL
, "gethostbyname() succeeded unexpectedly: %d\n", WSAGetLastError());
852 todo_wine
ok(WSAGetLastError() == WSANOTINITIALISED
, "gethostbyname() failed with unexpected error: %d\n",
856 static void test_WithWSAStartup(void)
859 WORD version
= MAKEWORD( 2, 2 );
863 res
= WSAStartup( version
, &data
);
864 ok(res
== 0, "WSAStartup() failed unexpectedly: %d\n", res
);
866 ptr
= gethostbyname("localhost");
867 ok(ptr
!= NULL
, "gethostbyname() failed unexpectedly: %d\n", WSAGetLastError());
872 /**************** Main program utility functions ***************/
874 static void Init (void)
876 WORD ver
= MAKEWORD (2, 2);
878 HMODULE hws2_32
= GetModuleHandle("ws2_32.dll");
880 pFreeAddrInfoW
= (void *)GetProcAddress(hws2_32
, "FreeAddrInfoW");
881 pGetAddrInfoW
= (void *)GetProcAddress(hws2_32
, "GetAddrInfoW");
882 pInetNtop
= (void *)GetProcAddress(hws2_32
, "inet_ntop");
884 ok ( WSAStartup ( ver
, &data
) == 0, "WSAStartup failed\n" );
888 static void Exit (void)
893 err
= WSAGetLastError();
894 ok ( ret
== 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret
, err
);
896 err
= WSAGetLastError();
897 ok ( (ret
== SOCKET_ERROR
&& err
== WSANOTINITIALISED
) ||
898 broken(ret
== 0), /* WinME */
899 "WSACleanup returned %d GetLastError is %d\n", ret
, err
);
902 static void StartServer (LPTHREAD_START_ROUTINE routine
,
903 test_params
*general
, server_params
*par
)
905 par
->general
= general
;
906 thread
[0] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[0] );
907 ok ( thread
[0] != NULL
, "Failed to create server thread\n" );
910 static void StartClients (LPTHREAD_START_ROUTINE routine
,
911 test_params
*general
, client_params
*par
)
914 par
->general
= general
;
915 for ( i
= 1; i
<= min ( general
->n_clients
, MAX_CLIENTS
); i
++ )
918 thread
[i
] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[i
] );
919 ok ( thread
[i
] != NULL
, "Failed to create client thread\n" );
920 /* Make sure the client is up and running */
921 WaitForSingleObject ( client_ready
[client_id
], INFINITE
);
925 static void do_test( test_setup
*test
)
927 DWORD i
, n
= min (test
->general
.n_clients
, MAX_CLIENTS
);
930 server_ready
= CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
931 for (i
= 0; i
<= n
; i
++)
932 client_ready
[i
] = CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
934 StartServer ( test
->srv
, &test
->general
, &test
->srv_params
);
935 StartClients ( test
->clt
, &test
->general
, &test
->clt_params
);
936 WaitForSingleObject ( server_ready
, INFINITE
);
938 wait
= WaitForMultipleObjects ( 1 + n
, thread
, TRUE
, 1000 * TEST_TIMEOUT
);
939 ok ( wait
<= WAIT_OBJECT_0
+ n
,
940 "some threads have not completed: %x\n", wait
);
942 if ( ! ( wait
<= WAIT_OBJECT_0
+ n
) )
944 for (i
= 0; i
<= n
; i
++)
946 trace ("terminating thread %08x\n", thread_id
[i
]);
947 if ( WaitForSingleObject ( thread
[i
], 0 ) != WAIT_OBJECT_0
)
948 TerminateThread ( thread
[i
], 0 );
951 CloseHandle ( server_ready
);
952 for (i
= 0; i
<= n
; i
++)
953 CloseHandle ( client_ready
[i
] );
956 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
957 /* optname = SO_LINGER */
958 LINGER linger_testvals
[] = {
965 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
966 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
967 bug in the linux kernel (fixed in 2.6.8) */
968 #define SOCKTIMEOUT2 997000 /* 997 seconds */
970 static void test_set_getsockopt(void)
978 s
= socket(AF_INET
, SOCK_STREAM
, 0);
979 ok(s
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
980 if( s
== INVALID_SOCKET
) return;
982 timeout
= SOCKTIMEOUT1
;
983 size
= sizeof(timeout
);
984 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
986 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
987 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
988 ok( timeout
== SOCKTIMEOUT1
, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
990 timeout
= SOCKTIMEOUT2
; /* 997 seconds. See remark above */
991 size
= sizeof(timeout
);
992 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
994 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
995 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
996 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
998 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
999 size
= sizeof(lingval
);
1000 lingval
= linger_testvals
[i
];
1001 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
1003 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
1004 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1005 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
1006 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
1007 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
1008 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
1009 lingval
.l_onoff
, lingval
.l_linger
,
1010 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
1012 /* Test for erroneously passing a value instead of a pointer as optval */
1013 size
= sizeof(char);
1014 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTROUTE
, (char *)1, size
);
1015 ok(err
== SOCKET_ERROR
, "setsockopt with optval being a value passed "
1016 "instead of failing.\n");
1017 lasterr
= WSAGetLastError();
1018 ok(lasterr
== WSAEFAULT
, "setsockopt with optval being a value "
1019 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1020 lasterr
, WSAEFAULT
);
1022 /* SO_RCVTIMEO with invalid values for level */
1023 size
= sizeof(timeout
);
1024 timeout
= SOCKTIMEOUT1
;
1025 SetLastError(0xdeadbeef);
1026 err
= setsockopt(s
, 0xffffffff, SO_RCVTIMEO
, (char *) &timeout
, size
);
1027 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1028 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1029 err
, WSAGetLastError());
1031 timeout
= SOCKTIMEOUT1
;
1032 SetLastError(0xdeadbeef);
1033 err
= setsockopt(s
, 0x00008000, SO_RCVTIMEO
, (char *) &timeout
, size
);
1034 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1035 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1036 err
, WSAGetLastError());
1041 static void test_so_reuseaddr(void)
1043 struct sockaddr_in saddr
;
1045 unsigned int rc
,reuse
;
1048 saddr
.sin_family
= AF_INET
;
1049 saddr
.sin_port
= htons(9375);
1050 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1052 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
1053 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1054 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1055 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
1057 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
1058 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1062 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
1063 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
1065 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1066 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
1069 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
1070 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1072 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1073 * a port immediately after closing another socket on that port, so
1074 * basically following the BSD socket semantics here. */
1076 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1077 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
1082 /************* Array containing the tests to run **********/
1084 #define STD_STREAM_SOCKET \
1090 static test_setup tests
[NUM_TESTS
] =
1092 /* Test 0: synchronous client and server */
1113 /* Test 1: event-driven client, synchronous server */
1130 WSA_FLAG_OVERLAPPED
,
1134 /* Test 2: synchronous client, non-blocking server via select() */
1155 /* Test 3: synchronous mixed client and server */
1169 simple_mixed_client
,
1178 static void test_UDP(void)
1180 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1181 possible that this test fails due to dropped packets. */
1183 /* peer 0 receives data from all other peers */
1184 struct sock_info peer
[NUM_UDP_PEERS
];
1186 int ss
, i
, n_recv
, n_sent
;
1188 memset (buf
,0,sizeof(buf
));
1189 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
1190 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
1192 peer
[i
].addr
.sin_family
= AF_INET
;
1193 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
1196 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
1198 peer
[i
].addr
.sin_port
= htons ( 0 );
1201 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
1203 /* test getsockname() to get peer's port */
1204 ss
= sizeof ( peer
[i
].addr
);
1205 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
1206 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
1209 /* test getsockname() */
1210 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
1212 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1213 /* send client's ip */
1214 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
1215 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
1216 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
1219 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1220 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
1221 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
1222 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
1226 static void WINAPI
do_getservbyname( HANDLE
*starttest
)
1232 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1235 struct servent
*pserv
[2];
1237 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
, "test_getservbyname: timeout waiting for start signal\n");
1239 /* ensure that necessary buffer resizes are completed */
1240 for ( j
= 0; j
< 2; j
++) {
1241 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1244 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
1245 for ( j
= 0; j
< 2; j
++ ) {
1246 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1247 ok ( pserv
[j
] != NULL
, "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
1248 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
) );
1249 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
);
1250 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
);
1253 ok ( pserv
[0] == pserv
[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1257 static void test_getservbyname(void)
1260 HANDLE starttest
, thread
[NUM_THREADS
];
1261 DWORD thread_id
[NUM_THREADS
];
1263 starttest
= CreateEvent ( NULL
, 1, 0, "test_getservbyname_starttest" );
1265 /* create threads */
1266 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
1267 thread
[i
] = CreateThread ( NULL
, 0, (LPTHREAD_START_ROUTINE
) &do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
1270 /* signal threads to start */
1271 SetEvent ( starttest
);
1273 for ( i
= 0; i
< NUM_THREADS
; i
++) {
1274 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
1278 static void test_WSASocket(void)
1280 SOCKET sock
= INVALID_SOCKET
;
1281 WSAPROTOCOL_INFOA
*pi
;
1282 int providers
[] = {6, 0};
1286 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1287 * to avoid a crash on win98.
1290 ret
= WSAEnumProtocolsA(providers
, NULL
, &pi_size
);
1291 ok(ret
== SOCKET_ERROR
, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1293 err
= WSAGetLastError();
1294 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1297 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
1298 ok(pi
!= NULL
, "Failed to allocate memory\n");
1300 skip("Can't continue without memory.\n");
1304 ret
= WSAEnumProtocolsA(providers
, pi
, &pi_size
);
1305 ok(ret
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
1309 skip("No protocols enumerated.\n");
1310 HeapFree(GetProcessHeap(), 0, pi
);
1314 sock
= WSASocketA(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
1315 FROM_PROTOCOL_INFO
, &pi
[0], 0, 0);
1316 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
1320 HeapFree(GetProcessHeap(), 0, pi
);
1323 static void test_WSAAddressToStringA(void)
1325 SOCKET v6
= INVALID_SOCKET
;
1329 SOCKADDR_IN sockaddr
;
1330 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1332 CHAR expect1
[] = "0.0.0.0";
1333 CHAR expect2
[] = "255.255.255.255";
1334 CHAR expect3
[] = "0.0.0.0:65535";
1335 CHAR expect4
[] = "255.255.255.255:65535";
1337 SOCKADDR_IN6 sockaddr6
;
1338 CHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1340 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1341 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1342 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1344 CHAR expect6_1
[] = "::1";
1345 CHAR expect6_2
[] = "20ab::1";
1346 CHAR expect6_3
[] = "[20ab::2001]:33274";
1347 CHAR expect6_3_2
[] = "[20ab::2001%4660]:33274";
1348 CHAR expect6_3_3
[] = "20ab::2001%4660";
1352 sockaddr
.sin_family
= AF_INET
;
1353 sockaddr
.sin_port
= 0;
1354 sockaddr
.sin_addr
.s_addr
= 0;
1356 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1357 GLE
= WSAGetLastError();
1358 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1359 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1362 len
= sizeof(address
);
1364 sockaddr
.sin_family
= AF_INET
;
1365 sockaddr
.sin_port
= 0;
1366 sockaddr
.sin_addr
.s_addr
= 0;
1368 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1369 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1371 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
1372 ok( len
== sizeof( expect1
), "Got size %d\n", len
);
1374 len
= sizeof(address
);
1376 sockaddr
.sin_family
= AF_INET
;
1377 sockaddr
.sin_port
= 0;
1378 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1380 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1381 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1383 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
1385 len
= sizeof(address
);
1387 sockaddr
.sin_family
= AF_INET
;
1388 sockaddr
.sin_port
= 0xffff;
1389 sockaddr
.sin_addr
.s_addr
= 0;
1391 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1392 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1394 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
1396 len
= sizeof(address
);
1398 sockaddr
.sin_family
= AF_INET
;
1399 sockaddr
.sin_port
= 0xffff;
1400 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1402 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1403 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1405 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
1406 ok( len
== sizeof( expect4
), "Got size %d\n", len
);
1408 /*check to see it IPv6 is available */
1409 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1410 if (v6
== INVALID_SOCKET
) {
1411 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1412 WSAGetLastError(), WSAEAFNOSUPPORT
);
1415 /* Test a short IPv6 address */
1416 len
= sizeof(address6
);
1418 sockaddr6
.sin6_family
= AF_INET6
;
1419 sockaddr6
.sin6_port
= 0x0000;
1420 sockaddr6
.sin6_scope_id
= 0;
1421 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1423 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1424 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1425 ok( !strcmp( address6
, expect6_1
), "Expected: %s, got: %s\n", expect6_1
, address6
);
1426 ok( len
== sizeof(expect6_1
), "Got size %d\n", len
);
1428 /* Test a longer IPv6 address */
1429 len
= sizeof(address6
);
1431 sockaddr6
.sin6_family
= AF_INET6
;
1432 sockaddr6
.sin6_port
= 0x0000;
1433 sockaddr6
.sin6_scope_id
= 0;
1434 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1436 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1437 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1438 ok( !strcmp( address6
, expect6_2
), "Expected: %s, got: %s\n", expect6_2
, address6
);
1439 ok( len
== sizeof(expect6_2
), "Got size %d\n", len
);
1441 /* Test IPv6 address and port number */
1442 len
= sizeof(address6
);
1444 sockaddr6
.sin6_family
= AF_INET6
;
1445 sockaddr6
.sin6_port
= 0xfa81;
1446 sockaddr6
.sin6_scope_id
= 0;
1447 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1449 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1450 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1451 ok( !strcmp( address6
, expect6_3
), "Expected: %s, got: %s\n", expect6_3
, address6
);
1452 ok( len
== sizeof(expect6_3
), "Got size %d\n", len
);
1454 /* Test IPv6 address, port number and scope_id */
1455 len
= sizeof(address6
);
1457 sockaddr6
.sin6_family
= AF_INET6
;
1458 sockaddr6
.sin6_port
= 0xfa81;
1459 sockaddr6
.sin6_scope_id
= 0x1234;
1460 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1462 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1463 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1464 ok( !strcmp( address6
, expect6_3_2
), "Expected: %s, got: %s\n", expect6_3_2
, address6
);
1465 ok( len
== sizeof(expect6_3_2
), "Got size %d\n", len
);
1467 /* Test IPv6 address and scope_id */
1468 len
= sizeof(address6
);
1470 sockaddr6
.sin6_family
= AF_INET6
;
1471 sockaddr6
.sin6_port
= 0x0000;
1472 sockaddr6
.sin6_scope_id
= 0x1234;
1473 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1475 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1476 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1477 ok( !strcmp( address6
, expect6_3_3
), "Expected: %s, got: %s\n", expect6_3_3
, address6
);
1478 ok( len
== sizeof(expect6_3_3
), "Got size %d\n", len
);
1481 if (v6
!= INVALID_SOCKET
)
1485 static void test_WSAAddressToStringW(void)
1487 SOCKET v6
= INVALID_SOCKET
;
1491 SOCKADDR_IN sockaddr
;
1492 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1494 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
1495 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1496 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1497 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1498 '6', '5', '5', '3', '5', 0 };
1500 SOCKADDR_IN6 sockaddr6
;
1501 WCHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1503 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1504 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1505 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1507 WCHAR expect6_1
[] = {':',':','1',0};
1508 WCHAR expect6_2
[] = {'2','0','a','b',':',':','1',0};
1509 WCHAR expect6_3
[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1510 WCHAR expect6_3_2
[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1511 WCHAR expect6_3_3
[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1515 sockaddr
.sin_family
= AF_INET
;
1516 sockaddr
.sin_port
= 0;
1517 sockaddr
.sin_addr
.s_addr
= 0;
1519 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1520 GLE
= WSAGetLastError();
1521 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1522 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1525 len
= sizeof(address
);
1527 sockaddr
.sin_family
= AF_INET
;
1528 sockaddr
.sin_port
= 0;
1529 sockaddr
.sin_addr
.s_addr
= 0;
1531 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1532 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1534 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
1535 ok( len
== sizeof( expect1
)/sizeof( WCHAR
), "Got size %d\n", len
);
1537 len
= sizeof(address
);
1539 sockaddr
.sin_family
= AF_INET
;
1540 sockaddr
.sin_port
= 0;
1541 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1543 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1544 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1546 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
1548 len
= sizeof(address
);
1550 sockaddr
.sin_family
= AF_INET
;
1551 sockaddr
.sin_port
= 0xffff;
1552 sockaddr
.sin_addr
.s_addr
= 0;
1554 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1555 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1557 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
1559 len
= sizeof(address
);
1561 sockaddr
.sin_family
= AF_INET
;
1562 sockaddr
.sin_port
= 0xffff;
1563 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1565 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1566 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1568 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
1569 ok( len
== sizeof( expect4
)/sizeof( WCHAR
), "Got %d\n", len
);
1571 /*check to see it IPv6 is available */
1572 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1573 if (v6
== INVALID_SOCKET
) {
1574 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1575 WSAGetLastError(), WSAEAFNOSUPPORT
);
1579 /* Test a short IPv6 address */
1580 len
= sizeof(address6
)/sizeof(WCHAR
);
1582 sockaddr6
.sin6_family
= AF_INET6
;
1583 sockaddr6
.sin6_port
= 0x0000;
1584 sockaddr6
.sin6_scope_id
= 0;
1585 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1587 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1588 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1589 ok( !lstrcmpW( address6
, expect6_1
), "Wrong string returned\n" );
1590 ok( len
== sizeof(expect6_1
)/sizeof(WCHAR
), "Got %d\n", len
);
1592 /* Test a longer IPv6 address */
1593 len
= sizeof(address6
)/sizeof(WCHAR
);
1595 sockaddr6
.sin6_family
= AF_INET6
;
1596 sockaddr6
.sin6_port
= 0x0000;
1597 sockaddr6
.sin6_scope_id
= 0;
1598 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1600 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1601 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1603 ok( !lstrcmpW( address6
, expect6_2
), "Wrong string returned\n" );
1604 ok( len
== sizeof(expect6_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1606 /* Test IPv6 address and port number */
1607 len
= sizeof(address6
)/sizeof(WCHAR
);
1609 sockaddr6
.sin6_family
= AF_INET6
;
1610 sockaddr6
.sin6_port
= 0xfa81;
1611 sockaddr6
.sin6_scope_id
= 0;
1612 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1614 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1615 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1616 ok( !lstrcmpW( address6
, expect6_3
), "Wrong string returned\n" );
1617 ok( len
== sizeof(expect6_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1619 /* Test IPv6 address, port number and scope_id */
1620 len
= sizeof(address6
)/sizeof(WCHAR
);
1622 sockaddr6
.sin6_family
= AF_INET6
;
1623 sockaddr6
.sin6_port
= 0xfa81;
1624 sockaddr6
.sin6_scope_id
= 0x1234;
1625 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1627 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1628 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1629 ok( !lstrcmpW( address6
, expect6_3_2
), "Wrong string returned\n" );
1630 ok( len
== sizeof(expect6_3_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1632 /* Test IPv6 address and scope_id */
1633 len
= sizeof(address6
)/sizeof(WCHAR
);
1635 sockaddr6
.sin6_family
= AF_INET6
;
1636 sockaddr6
.sin6_port
= 0x0000;
1637 sockaddr6
.sin6_scope_id
= 0xfffe;
1638 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1640 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1641 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1642 ok( !lstrcmpW( address6
, expect6_3_3
), "Wrong string returned\n" );
1643 ok( len
== sizeof(expect6_3_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1646 if (v6
!= INVALID_SOCKET
)
1650 static void test_WSAStringToAddressA(void)
1653 SOCKADDR_IN sockaddr
;
1654 SOCKADDR_IN6 sockaddr6
;
1657 CHAR address1
[] = "0.0.0.0";
1658 CHAR address2
[] = "127.127.127.127";
1659 CHAR address3
[] = "255.255.255.255";
1660 CHAR address4
[] = "127.127.127.127:65535";
1661 CHAR address5
[] = "255.255.255.255:65535";
1662 CHAR address6
[] = "::1";
1663 CHAR address7
[] = "[::1]";
1664 CHAR address8
[] = "[::1]:65535";
1667 sockaddr
.sin_family
= AF_INET
;
1669 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1670 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1671 WSAGetLastError() );
1673 len
= sizeof(sockaddr
);
1674 sockaddr
.sin_port
= 0;
1675 sockaddr
.sin_addr
.s_addr
= 0;
1677 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1678 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1679 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1681 len
= sizeof(sockaddr
);
1682 sockaddr
.sin_port
= 0;
1683 sockaddr
.sin_addr
.s_addr
= 0;
1685 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1686 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1687 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1689 len
= sizeof(sockaddr
);
1690 sockaddr
.sin_port
= 0;
1691 sockaddr
.sin_addr
.s_addr
= 0;
1693 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1694 GLE
= WSAGetLastError();
1695 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1696 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1697 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1699 len
= sizeof(sockaddr
);
1700 sockaddr
.sin_port
= 0;
1701 sockaddr
.sin_addr
.s_addr
= 0;
1703 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1704 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1705 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1707 len
= sizeof(sockaddr
);
1708 sockaddr
.sin_port
= 0;
1709 sockaddr
.sin_addr
.s_addr
= 0;
1711 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1712 GLE
= WSAGetLastError();
1713 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1714 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1715 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1717 len
= sizeof(sockaddr6
);
1718 memset(&sockaddr6
, 0, len
);
1719 sockaddr6
.sin6_family
= AF_INET6
;
1721 ret
= WSAStringToAddressA( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1723 GLE
= WSAGetLastError();
1724 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1725 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1727 len
= sizeof(sockaddr6
);
1728 memset(&sockaddr6
, 0, len
);
1729 sockaddr6
.sin6_family
= AF_INET6
;
1731 ret
= WSAStringToAddressA( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1733 GLE
= WSAGetLastError();
1734 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1735 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1737 len
= sizeof(sockaddr6
);
1738 memset(&sockaddr6
, 0, len
);
1739 sockaddr6
.sin6_family
= AF_INET6
;
1741 ret
= WSAStringToAddressA( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1743 GLE
= WSAGetLastError();
1744 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1745 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1746 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1750 static void test_WSAStringToAddressW(void)
1753 SOCKADDR_IN sockaddr
;
1754 SOCKADDR_IN6 sockaddr6
;
1757 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
1758 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1759 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1760 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1761 ':', '6', '5', '5', '3', '5', 0 };
1762 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1763 '6', '5', '5', '3', '5', 0 };
1764 WCHAR address6
[] = {':',':','1','\0'};
1765 WCHAR address7
[] = {'[',':',':','1',']','\0'};
1766 WCHAR address8
[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1769 sockaddr
.sin_family
= AF_INET
;
1771 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1772 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
1773 WSAGetLastError() );
1775 len
= sizeof(sockaddr
);
1776 sockaddr
.sin_port
= 0;
1777 sockaddr
.sin_addr
.s_addr
= 0;
1779 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1780 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1781 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1783 len
= sizeof(sockaddr
);
1784 sockaddr
.sin_port
= 0;
1785 sockaddr
.sin_addr
.s_addr
= 0;
1787 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1788 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1789 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1791 len
= sizeof(sockaddr
);
1792 sockaddr
.sin_port
= 0;
1793 sockaddr
.sin_addr
.s_addr
= 0;
1795 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1796 GLE
= WSAGetLastError();
1797 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1798 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1799 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1801 len
= sizeof(sockaddr
);
1802 sockaddr
.sin_port
= 0;
1803 sockaddr
.sin_addr
.s_addr
= 0;
1805 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1806 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1807 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1809 len
= sizeof(sockaddr
);
1810 sockaddr
.sin_port
= 0;
1811 sockaddr
.sin_addr
.s_addr
= 0;
1813 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1814 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1815 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1816 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1818 len
= sizeof(sockaddr6
);
1819 memset(&sockaddr6
, 0, len
);
1820 sockaddr6
.sin6_family
= AF_INET6
;
1822 ret
= WSAStringToAddressW( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1824 GLE
= WSAGetLastError();
1825 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1826 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1828 len
= sizeof(sockaddr6
);
1829 memset(&sockaddr6
, 0, len
);
1830 sockaddr6
.sin6_family
= AF_INET6
;
1832 ret
= WSAStringToAddressW( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1834 GLE
= WSAGetLastError();
1835 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1836 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1838 len
= sizeof(sockaddr6
);
1839 memset(&sockaddr6
, 0, len
);
1840 sockaddr6
.sin6_family
= AF_INET6
;
1842 ret
= WSAStringToAddressW( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1844 GLE
= WSAGetLastError();
1845 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1846 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1847 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1851 static VOID WINAPI
SelectReadThread(select_thread_params
*par
)
1855 struct sockaddr_in addr
;
1856 struct timeval select_timeout
;
1859 FD_SET(par
->s
, &readfds
);
1860 select_timeout
.tv_sec
=5;
1861 select_timeout
.tv_usec
=0;
1862 addr
.sin_family
= AF_INET
;
1863 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
1864 addr
.sin_port
= htons(SERVERPORT
);
1866 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
1867 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1869 SetEvent(server_ready
);
1870 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
1871 par
->ReadKilled
= (ret
== 1);
1874 static void test_select(void)
1876 SOCKET fdRead
, fdWrite
;
1877 fd_set readfds
, writefds
, exceptfds
;
1880 struct timeval select_timeout
;
1881 select_thread_params thread_params
;
1882 HANDLE thread_handle
;
1885 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
1886 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1887 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
1888 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1892 FD_ZERO(&exceptfds
);
1893 FD_SET(fdRead
, &readfds
);
1894 FD_SET(fdWrite
, &writefds
);
1895 FD_SET(fdRead
, &exceptfds
);
1896 FD_SET(fdWrite
, &exceptfds
);
1897 select_timeout
.tv_sec
=0;
1898 select_timeout
.tv_usec
=500;
1901 if (fdWrite
> maxfd
)
1905 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
1906 ok ( (ret
== 0), "select should not return any socket handles\n");
1907 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
1908 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
1911 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
1912 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
1915 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
1917 ret
= closesocket(fdWrite
);
1918 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1920 thread_params
.s
= fdRead
;
1921 thread_params
.ReadKilled
= FALSE
;
1922 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1923 thread_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) &SelectReadThread
, &thread_params
, 0, &id
);
1924 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
1926 WaitForSingleObject (server_ready
, INFINITE
);
1928 ret
= closesocket(fdRead
);
1929 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1931 WaitForSingleObject (thread_handle
, 1000);
1932 ok ( (thread_params
.ReadKilled
) ||
1933 broken(thread_params
.ReadKilled
== 0), /*Win98*/
1934 "closesocket did not wakeup select\n");
1938 static DWORD WINAPI
AcceptKillThread(select_thread_params
*par
)
1940 struct sockaddr_in address
;
1941 int len
= sizeof(address
);
1942 SOCKET client_socket
;
1944 SetEvent(server_ready
);
1945 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
1946 if (client_socket
!= INVALID_SOCKET
)
1947 closesocket(client_socket
);
1948 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
1953 static int CALLBACK
AlwaysDeferConditionFunc(LPWSABUF lpCallerId
, LPWSABUF lpCallerData
, LPQOS pQos
,
1954 LPQOS lpGQOS
, LPWSABUF lpCalleeId
, LPWSABUF lpCalleeData
,
1955 GROUP FAR
* g
, DWORD_PTR dwCallbackData
)
1960 static void test_accept(void)
1963 SOCKET server_socket
= INVALID_SOCKET
, accepted
= INVALID_SOCKET
, connector
= INVALID_SOCKET
;
1964 struct sockaddr_in address
;
1966 select_thread_params thread_params
;
1967 HANDLE thread_handle
= NULL
;
1970 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
1971 if (server_socket
== INVALID_SOCKET
)
1973 trace("error creating server socket: %d\n", WSAGetLastError());
1977 memset(&address
, 0, sizeof(address
));
1978 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1979 address
.sin_family
= AF_INET
;
1980 ret
= bind(server_socket
, (struct sockaddr
*) &address
, sizeof(address
));
1983 trace("error binding server socket: %d\n", WSAGetLastError());
1987 socklen
= sizeof(address
);
1988 ret
= getsockname(server_socket
, (struct sockaddr
*)&address
, &socklen
);
1990 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
1994 ret
= listen(server_socket
, 5);
1997 trace("error making server socket listen: %d\n", WSAGetLastError());
2001 trace("Blocking accept next\n");
2003 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2004 ok(connector
!= INVALID_SOCKET
, "Failed to create connector socket, error %d\n", WSAGetLastError());
2006 ret
= connect(connector
, (struct sockaddr
*)&address
, sizeof(address
));
2007 ok(ret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2009 accepted
= WSAAccept(server_socket
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
2010 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
2012 accepted
= accept(server_socket
, NULL
, 0);
2013 ok(accepted
!= INVALID_SOCKET
, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2015 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2016 if (server_ready
== INVALID_HANDLE_VALUE
)
2018 trace("error creating event: %d\n", GetLastError());
2022 thread_params
.s
= server_socket
;
2023 thread_params
.ReadKilled
= FALSE
;
2024 thread_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
) AcceptKillThread
,
2025 &thread_params
, 0, &id
);
2026 if (thread_handle
== NULL
)
2028 trace("error creating thread: %d\n", GetLastError());
2032 WaitForSingleObject(server_ready
, INFINITE
);
2034 ret
= closesocket(server_socket
);
2037 trace("closesocket failed: %d\n", WSAGetLastError());
2041 WaitForSingleObject(thread_handle
, 1000);
2042 ok(thread_params
.ReadKilled
|| broken(!thread_params
.ReadKilled
) /* Win98/ME, after accept */,
2043 "closesocket did not wakeup accept\n");
2046 if (accepted
!= INVALID_SOCKET
)
2047 closesocket(accepted
);
2048 if (connector
!= INVALID_SOCKET
)
2049 closesocket(connector
);
2050 if (thread_handle
!= NULL
)
2051 CloseHandle(thread_handle
);
2052 if (server_ready
!= INVALID_HANDLE_VALUE
)
2053 CloseHandle(server_ready
);
2054 if (server_socket
!= INVALID_SOCKET
)
2055 closesocket(server_socket
);
2058 static void test_extendedSocketOptions(void)
2062 struct sockaddr_in sa
;
2063 int sa_len
= sizeof(struct sockaddr_in
);
2064 int optval
, optlen
= sizeof(int), ret
;
2068 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2069 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2073 memset(&sa
, 0, sa_len
);
2075 sa
.sin_family
= AF_INET
;
2076 sa
.sin_port
= htons(0);
2077 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2079 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2080 trace("Creating the socket failed: %d\n", WSAGetLastError());
2085 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2086 trace("Failed to bind socket: %d\n", WSAGetLastError());
2092 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2094 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
2095 ok((optval
== 65507) || (optval
== 65527),
2096 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval
);
2098 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2099 SetLastError(0xdeadbeef);
2100 optval
= 0xdeadbeef;
2101 optlen
= sizeof(int);
2102 ret
= getsockopt(sock
, 0xffffffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2103 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2104 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2105 ret
, WSAGetLastError(), optval
, optval
);
2107 /* more invalid values for level */
2108 SetLastError(0xdeadbeef);
2109 optval
= 0xdeadbeef;
2110 optlen
= sizeof(int);
2111 ret
= getsockopt(sock
, 0x1234ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2112 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2113 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2114 ret
, WSAGetLastError(), optval
, optval
);
2116 SetLastError(0xdeadbeef);
2117 optval
= 0xdeadbeef;
2118 optlen
= sizeof(int);
2119 ret
= getsockopt(sock
, 0x8000ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2120 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2121 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2122 ret
, WSAGetLastError(), optval
, optval
);
2124 SetLastError(0xdeadbeef);
2125 optval
= 0xdeadbeef;
2126 optlen
= sizeof(int);
2127 ret
= getsockopt(sock
, 0x00008000, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2128 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2129 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2130 ret
, WSAGetLastError(), optval
, optval
);
2132 SetLastError(0xdeadbeef);
2133 optval
= 0xdeadbeef;
2134 optlen
= sizeof(int);
2135 ret
= getsockopt(sock
, 0x00000800, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2136 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2137 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2138 ret
, WSAGetLastError(), optval
, optval
);
2140 optlen
= sizeof(LINGER
);
2141 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2143 ok(ret
== SOCKET_ERROR
, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret
);
2148 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2149 trace("Creating the socket failed: %d\n", WSAGetLastError());
2154 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2155 trace("Failed to bind socket: %d\n", WSAGetLastError());
2161 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2162 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
2164 optlen
= sizeof(BOOL
);
2165 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
2166 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
2167 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
2168 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2169 bool_opt_val
, linger_val
.l_onoff
);
2175 static void test_getsockname(void)
2179 struct sockaddr_in sa_set
, sa_get
;
2180 int sa_set_len
= sizeof(struct sockaddr_in
);
2181 int sa_get_len
= sa_set_len
;
2182 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
2185 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2186 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2190 memset(&sa_set
, 0, sa_set_len
);
2192 sa_set
.sin_family
= AF_INET
;
2193 sa_set
.sin_port
= htons(0);
2194 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2196 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2197 trace("Creating the socket failed: %d\n", WSAGetLastError());
2202 memcpy(&sa_get
, &sa_set
, sizeof(sa_set
));
2203 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
2204 ok(0, "getsockname on unbound socket should fail\n");
2206 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
2207 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
2208 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
2209 "failed getsockname modified sockaddr when it shouldn't\n");
2212 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
2213 trace("Failed to bind socket: %d\n", WSAGetLastError());
2219 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
2220 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2226 ret
= memcmp(sa_get
.sin_zero
, null_padding
, 8);
2227 ok(ret
== 0 || broken(ret
!= 0), /* NT4 */
2228 "getsockname did not zero the sockaddr_in structure\n");
2234 static void test_dns(void)
2238 h
= gethostbyname("");
2239 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
2242 /* Our winsock headers don't define gethostname because it conflicts with the
2243 * definition in unistd.h. Define it here to get rid of the warning. */
2245 int WINAPI
gethostname(char *name
, int namelen
);
2247 static void test_gethostbyname_hack(void)
2251 static BYTE loopback
[] = {127, 0, 0, 1};
2252 static BYTE magic_loopback
[] = {127, 12, 34, 56};
2255 ret
= gethostname(name
, 256);
2256 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
2258 he
= gethostbyname("localhost");
2259 ok(he
!= NULL
, "gethostbyname(\"localhost\") failed: %d\n", h_errno
);
2262 if(he
->h_length
!= 4)
2264 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2268 ok(memcmp(he
->h_addr_list
[0], loopback
, he
->h_length
) == 0,
2269 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2270 he
->h_addr_list
[0][0], he
->h_addr_list
[0][1], he
->h_addr_list
[0][2],
2271 he
->h_addr_list
[0][3]);
2274 /* No reason to test further with NULL hostname */
2278 if(strcmp(name
, "localhost") == 0)
2280 skip("hostname seems to be \"localhost\", skipping test.\n");
2285 he
= gethostbyname(name
);
2286 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, h_errno
);
2289 if(he
->h_length
!= 4)
2291 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2295 if (he
->h_addr_list
[0][0] == 127)
2297 ok(memcmp(he
->h_addr_list
[0], magic_loopback
, he
->h_length
) == 0,
2298 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2299 name
, he
->h_addr_list
[0][0], he
->h_addr_list
[0][1],
2300 he
->h_addr_list
[0][2], he
->h_addr_list
[0][3]);
2305 he
= gethostbyname("nonexistent.winehq.org");
2306 /* Don't check for the return value, as some braindead ISPs will kindly
2307 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2310 static void test_inet_addr(void)
2314 addr
= inet_addr(NULL
);
2315 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
2318 static void test_addr_to_print(void)
2324 struct in6_addr in6
;
2326 u_long addr0_Num
= 0x00000000;
2327 PCSTR addr0_Str
= "0.0.0.0";
2328 u_long addr1_Num
= 0x20201015;
2329 PCSTR addr1_Str
= "21.16.32.32";
2330 u_char addr2_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2331 PCSTR addr2_Str
= "::fffe:cc98:bd74";
2332 u_char addr3_Num
[16] = {0x20,0x30,0xa4,0xb1};
2333 PCSTR addr3_Str
= "2030:a4b1::";
2334 u_char addr4_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2335 PCSTR addr4_Str
= "::204.152.189.116";
2337 /* Test IPv4 addresses */
2338 in
.s_addr
= addr0_Num
;
2340 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2341 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2342 ok(!strcmp(pdst
, addr0_Str
),"Address %s != %s\n", pdst
, addr0_Str
);
2344 /* Test that inet_ntoa and inet_ntop return the same value */
2345 in
.S_un
.S_addr
= addr1_Num
;
2346 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2347 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2348 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2350 /* InetNtop became available in Vista and Win2008 */
2353 win_skip("InetNtop not present, not executing tests\n");
2357 /* Second part of test */
2358 pdst
= pInetNtop(AF_INET
,(void*)&in
.s_addr
, dst
, sizeof(dst
));
2359 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst
);
2360 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2362 /* Test invalid parm conditions */
2363 pdst
= pInetNtop(1, (void*)&in
.s_addr
, dst
, sizeof(dst
));
2364 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2365 ok(WSAGetLastError() == WSAEAFNOSUPPORT
, "Should be WSAEAFNOSUPPORT\n");
2367 /* Test Null destination */
2369 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, NULL
, sizeof(dst
));
2370 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2371 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2372 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2374 /* Test zero length passed */
2377 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 0);
2378 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2379 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2380 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2382 /* Test length one shorter than the address length */
2385 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 6);
2386 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2387 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2388 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2390 /* Test longer length is ok */
2393 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, sizeof(dst
)+1);
2394 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2395 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2397 /* Test the IPv6 addresses */
2399 /* Test an zero prefixed IPV6 address */
2400 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2401 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2402 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2403 ok(!strcmp(pdst
, addr2_Str
),"Address %s != %s\n", pdst
, addr2_Str
);
2405 /* Test an zero suffixed IPV6 address */
2406 memcpy(in6
.s6_addr
, addr3_Num
, sizeof(addr3_Num
));
2407 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2408 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2409 ok(!strcmp(pdst
, addr3_Str
),"Address %s != %s\n", pdst
, addr3_Str
);
2411 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2412 memcpy(in6
.s6_addr
, addr4_Num
, sizeof(addr4_Num
));
2413 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2414 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2415 ok(!strcmp(pdst
, addr4_Str
),"Address %s != %s\n", pdst
, addr4_Str
);
2417 /* Test invalid parm conditions */
2418 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2420 /* Test Null destination */
2422 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, NULL
, sizeof(dst6
));
2423 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2424 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2425 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2427 /* Test zero length passed */
2430 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 0);
2431 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2432 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2433 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2435 /* Test length one shorter than the address length */
2438 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 16);
2439 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2440 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2441 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2443 /* Test longer length is ok */
2446 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 18);
2447 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2450 static void test_ioctlsocket(void)
2454 static const LONG cmds
[] = {FIONBIO
, FIONREAD
, SIOCATMARK
};
2458 sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2459 ok(sock
!= INVALID_SOCKET
, "Creating the socket failed: %d\n", WSAGetLastError());
2460 if(sock
== INVALID_SOCKET
)
2462 skip("Can't continue without a socket.\n");
2466 for(i
= 0; i
< sizeof(cmds
)/sizeof(cmds
[0]); i
++)
2468 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2469 ret
= ioctlsocket(sock
, cmds
[i
], (u_long
*)1);
2470 ok(ret
== SOCKET_ERROR
, "ioctlsocket succeeded unexpectedly\n");
2471 ret
= WSAGetLastError();
2472 ok(ret
== WSAEFAULT
, "expected WSAEFAULT, got %d instead\n", ret
);
2475 /* A fresh and not connected socket has no urgent data, this test shows
2476 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2478 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
2479 if(ret
!= SOCKET_ERROR
)
2480 todo_wine
ok(arg
, "expected a non-zero value\n");
2483 static int drain_pause
=0;
2484 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
2487 SOCKET sock
= *(SOCKET
*)arg
;
2490 while ((ret
= recv(sock
, buffer
, sizeof(buffer
), 0)) != 0)
2494 if (WSAGetLastError() == WSAEWOULDBLOCK
)
2498 FD_SET(sock
, &readset
);
2499 select(0, &readset
, NULL
, NULL
, NULL
);
2510 static void test_send(void)
2512 SOCKET src
= INVALID_SOCKET
;
2513 SOCKET dst
= INVALID_SOCKET
;
2514 HANDLE hThread
= NULL
;
2515 const int buflen
= 1024*1024;
2516 char *buffer
= NULL
;
2520 if (tcp_socketpair(&src
, &dst
) != 0)
2522 ok(0, "creating socket pair failed, skipping test\n");
2526 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2527 if (hThread
== NULL
)
2529 ok(0, "CreateThread failed, error %d\n", GetLastError());
2533 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buflen
);
2536 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2540 ret
= send(src
, buffer
, buflen
, 0);
2542 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
2544 ok(0, "send failed, error %d\n", WSAGetLastError());
2547 if (src
!= INVALID_SOCKET
)
2549 if (dst
!= INVALID_SOCKET
)
2551 if (hThread
!= NULL
)
2552 CloseHandle(hThread
);
2553 HeapFree(GetProcessHeap(), 0, buffer
);
2556 static void test_write_events(void)
2558 SOCKET src
= INVALID_SOCKET
;
2559 SOCKET dst
= INVALID_SOCKET
;
2560 HANDLE hThread
= NULL
;
2561 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
2562 char *buffer
= NULL
;
2563 int bufferSize
= 1024*1024;
2567 WSANETWORKEVENTS netEvents
;
2570 if (tcp_socketpair(&src
, &dst
) != 0)
2572 ok(0, "creating socket pair failed, skipping test\n");
2576 /* On Windows it seems when a non-blocking socket sends to a
2577 blocking socket on the same host, the send() is BLOCKING,
2578 so make both sockets non-blocking */
2579 ret
= ioctlsocket(src
, FIONBIO
, &one
);
2582 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2585 ret
= ioctlsocket(dst
, FIONBIO
, &one
);
2588 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2592 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bufferSize
);
2595 ok(0, "could not allocate memory for test\n");
2599 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2600 if (hThread
== NULL
)
2602 ok(0, "CreateThread failed, error %d\n", GetLastError());
2606 hEvent
= CreateEventA(NULL
, FALSE
, TRUE
, NULL
);
2607 if (hEvent
== INVALID_HANDLE_VALUE
)
2609 ok(0, "CreateEventA failed, error %d\n", GetLastError());
2613 ret
= WSAEventSelect(src
, hEvent
, FD_WRITE
| FD_CLOSE
);
2616 ok(0, "WSAEventSelect failed, error %d\n", ret
);
2620 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
2621 dwRet
= WaitForSingleObject(hEvent
, 5000);
2622 if (dwRet
!= WAIT_OBJECT_0
)
2624 ok(0, "Initial WaitForSingleObject failed, error %d\n", dwRet
);
2627 ret
= WSAEnumNetworkEvents(src
, NULL
, &netEvents
);
2630 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2633 if (netEvents
.lNetworkEvents
& FD_WRITE
)
2635 ret
= send(src
, "a", 1, 0);
2636 ok(ret
== 1, "sending 1 byte failed, error %d\n", WSAGetLastError());
2642 ok(0, "FD_WRITE not among initial events\n");
2646 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
2647 dwRet
= WaitForSingleObject(hEvent
, 2000);
2648 if (dwRet
== WAIT_OBJECT_0
)
2650 ok(0, "WaitForSingleObject should have timed out, but succeeded!\n");
2654 /* Now if we send a ton of data and the 'server' does not drain it fast
2655 * enough (set drain_pause to be sure), the socket send buffer will only
2656 * take some of it, and we will get a short write. This will trigger
2657 * another FD_WRITE event as soon as data is sent and more space becomes
2658 * available, but not any earlier. */
2662 ret
= send(src
, buffer
, bufferSize
, 0);
2663 } while (ret
== bufferSize
);
2665 if (ret
>= 0 || WSAGetLastError() == WSAEWOULDBLOCK
)
2667 dwRet
= WaitForSingleObject(hEvent
, 5000);
2668 ok(dwRet
== WAIT_OBJECT_0
, "Waiting failed with %d\n", dwRet
);
2669 if (dwRet
== WAIT_OBJECT_0
)
2671 ret
= WSAEnumNetworkEvents(src
, NULL
, &netEvents
);
2672 ok(ret
== 0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2675 ok(netEvents
.lNetworkEvents
& FD_WRITE
,
2676 "FD_WRITE event not set as expected, events are 0x%x\n", netEvents
.lNetworkEvents
);
2683 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
2688 HeapFree(GetProcessHeap(), 0, buffer
);
2689 if (src
!= INVALID_SOCKET
)
2691 if (dst
!= INVALID_SOCKET
)
2693 if (hThread
!= NULL
)
2694 CloseHandle(hThread
);
2695 CloseHandle(hEvent
);
2698 static void test_ipv6only(void)
2700 SOCKET v4
= INVALID_SOCKET
,
2701 v6
= INVALID_SOCKET
;
2702 struct sockaddr_in sin4
;
2703 struct sockaddr_in6 sin6
;
2706 memset(&sin4
, 0, sizeof(sin4
));
2707 sin4
.sin_family
= AF_INET
;
2708 sin4
.sin_port
= htons(SERVERPORT
);
2710 memset(&sin6
, 0, sizeof(sin6
));
2711 sin6
.sin6_family
= AF_INET6
;
2712 sin6
.sin6_port
= htons(SERVERPORT
);
2714 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
2715 if (v6
== INVALID_SOCKET
) {
2716 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2717 WSAGetLastError(), WSAEAFNOSUPPORT
);
2720 ret
= bind(v6
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
2722 skip("Could not bind IPv6 address (LastError: %d).\n",
2727 v4
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2728 if (v4
== INVALID_SOCKET
) {
2729 skip("Could not create IPv4 socket (LastError: %d).\n",
2733 ret
= bind(v4
, (struct sockaddr
*)&sin4
, sizeof(sin4
));
2734 ok(!ret
, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2735 WSAGetLastError(), WSAEADDRINUSE
);
2738 if (v4
!= INVALID_SOCKET
)
2740 if (v6
!= INVALID_SOCKET
)
2744 static void test_WSASendTo(void)
2747 struct sockaddr_in addr
;
2748 char buf
[12] = "hello world";
2752 addr
.sin_family
= AF_INET
;
2753 addr
.sin_port
= htons(139);
2754 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2755 data_buf
.len
= sizeof(buf
);
2758 if( (s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
) {
2759 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2763 WSASetLastError(12345);
2764 if(WSASendTo(s
, &data_buf
, 1, &bytesSent
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
)) {
2765 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2768 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2769 "a successful call to WSASendTo()\n");
2772 static void test_GetAddrInfoW(void)
2774 static const WCHAR port
[] = {'8','0',0};
2775 static const WCHAR empty
[] = {0};
2776 static const WCHAR localhost
[] = {'l','o','c','a','l','h','o','s','t',0};
2779 ADDRINFOW
*result
, hint
;
2781 if (!pGetAddrInfoW
|| !pFreeAddrInfoW
)
2783 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
2787 memset(&hint
, 0, sizeof(ADDRINFOW
));
2789 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
2790 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
2793 ret
= pGetAddrInfoW(empty
, NULL
, NULL
, &result
);
2796 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2797 ok(result
!= NULL
, "GetAddrInfoW failed\n");
2799 pFreeAddrInfoW(result
);
2801 ret
= pGetAddrInfoW(localhost
, NULL
, NULL
, &result
);
2802 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2803 pFreeAddrInfoW(result
);
2805 ret
= pGetAddrInfoW(localhost
, port
, NULL
, &result
);
2806 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2807 pFreeAddrInfoW(result
);
2809 ret
= pGetAddrInfoW(localhost
, port
, &hint
, &result
);
2810 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2811 pFreeAddrInfoW(result
);
2814 static void test_AcceptEx(void)
2816 SOCKET listener
= INVALID_SOCKET
;
2817 SOCKET acceptor
= INVALID_SOCKET
;
2818 SOCKET connector
= INVALID_SOCKET
;
2819 SOCKET connector2
= INVALID_SOCKET
;
2820 struct sockaddr_in bindAddress
;
2822 GUID acceptExGuid
= WSAID_ACCEPTEX
;
2823 LPFN_ACCEPTEX pAcceptEx
= NULL
;
2824 fd_set fds_accept
, fds_send
;
2825 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
2827 DWORD bytesReturned
;
2829 OVERLAPPED overlapped
;
2834 memset(&overlapped
, 0, sizeof(overlapped
));
2836 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
2837 if (listener
== INVALID_SOCKET
) {
2838 skip("could not create listener socket, error %d\n", WSAGetLastError());
2842 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
2843 if (acceptor
== INVALID_SOCKET
) {
2844 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2848 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2849 if (connector
== INVALID_SOCKET
) {
2850 skip("could not create connector socket, error %d\n", WSAGetLastError());
2854 memset(&bindAddress
, 0, sizeof(bindAddress
));
2855 bindAddress
.sin_family
= AF_INET
;
2856 bindAddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2857 iret
= bind(listener
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
2859 skip("failed to bind, error %d\n", WSAGetLastError());
2863 socklen
= sizeof(bindAddress
);
2864 iret
= getsockname(listener
, (struct sockaddr
*)&bindAddress
, &socklen
);
2866 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2870 if (set_blocking(listener
, FALSE
)) {
2871 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2875 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
2876 &pAcceptEx
, sizeof(pAcceptEx
), &bytesReturned
, NULL
, NULL
);
2878 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
2882 bret
= pAcceptEx(INVALID_SOCKET
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2883 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2884 &bytesReturned
, &overlapped
);
2885 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid listening socket "
2886 "returned %d + errno %d\n", bret
, WSAGetLastError());
2888 bret
= pAcceptEx(listener
, INVALID_SOCKET
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2889 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2890 &bytesReturned
, &overlapped
);
2891 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on invalid accepting socket "
2892 "returned %d + errno %d\n", bret
, WSAGetLastError());
2894 bret
= pAcceptEx(listener
, acceptor
, NULL
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2895 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2896 &bytesReturned
, &overlapped
);
2898 (WSAGetLastError() == WSAEINVAL
||
2899 broken(WSAGetLastError() == WSAEFAULT
)), /* NT4 */
2900 "AcceptEx on NULL buffer returned %d + errno %d\n", bret
, WSAGetLastError());
2902 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, sizeof(struct sockaddr_in
) + 16,
2903 &bytesReturned
, &overlapped
);
2904 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small local address size "
2905 "returned %d + errno %d\n", bret
, WSAGetLastError());
2907 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16, 0,
2908 &bytesReturned
, &overlapped
);
2909 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small remote address size "
2910 "returned %d + errno %d\n", bret
, WSAGetLastError());
2912 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
2913 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2914 &bytesReturned
, NULL
);
2915 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
2916 "returned %d + errno %d\n", bret
, WSAGetLastError());
2918 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2919 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2920 &bytesReturned
, &overlapped
);
2921 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on a non-listening socket "
2922 "returned %d + errno %d\n", bret
, WSAGetLastError());
2924 iret
= listen(listener
, 5);
2926 skip("listening failed, errno = %d\n", WSAGetLastError());
2930 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2931 if (overlapped
.hEvent
== NULL
) {
2932 skip("could not create event object, errno = %d\n", GetLastError());
2936 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
2937 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2938 &bytesReturned
, &overlapped
);
2939 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
2941 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
2942 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2943 &bytesReturned
, &overlapped
);
2944 ok((bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
) || broken(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) /* NT4 */,
2945 "AcceptEx on already pending socket returned %d + errno %d\n", bret
, WSAGetLastError());
2946 if (bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) {
2947 /* We need to cancel this call, otherwise things fail */
2948 bret
= CancelIo((HANDLE
) listener
);
2949 ok(bret
, "Failed to cancel failed test. Bailing...\n");
2952 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
2953 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2954 &bytesReturned
, &overlapped
);
2955 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
2958 iret
= connect(acceptor
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
2959 ok((iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINVAL
) || broken(!iret
) /* NT4 */,
2960 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret
, WSAGetLastError());
2961 if (!iret
|| (iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
)) {
2962 /* We need to cancel this call, otherwise things fail */
2963 closesocket(acceptor
);
2964 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
2965 if (acceptor
== INVALID_SOCKET
) {
2966 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2970 bret
= CancelIo((HANDLE
) listener
);
2971 ok(bret
, "Failed to cancel failed test. Bailing...\n");
2974 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
2975 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2976 &bytesReturned
, &overlapped
);
2977 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
2980 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
2981 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2983 dwret
= WaitForSingleObject(overlapped
.hEvent
, INFINITE
);
2984 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
2986 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
2987 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
2988 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
2990 closesocket(connector
);
2991 connector
= INVALID_SOCKET
;
2992 closesocket(acceptor
);
2993 acceptor
= INVALID_SOCKET
;
2995 /* Test short reads */
2997 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
2998 if (acceptor
== INVALID_SOCKET
) {
2999 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3002 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3003 if (connector
== INVALID_SOCKET
) {
3004 skip("could not create connector socket, error %d\n", WSAGetLastError());
3007 bret
= pAcceptEx(listener
, acceptor
, buffer
, 2,
3008 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3009 &bytesReturned
, &overlapped
);
3010 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3012 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3013 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3015 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3016 ok(dwret
== WAIT_TIMEOUT
, "Waiting for accept event timeout failed with %d + errno %d\n", dwret
, GetLastError());
3018 iret
= send(connector
, buffer
, 1, 0);
3019 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
3021 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3022 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3024 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3025 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3026 ok(bytesReturned
== 1, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3028 closesocket(connector
);
3029 connector
= INVALID_SOCKET
;
3030 closesocket(acceptor
);
3031 acceptor
= INVALID_SOCKET
;
3033 /* Test CF_DEFER & AcceptEx interaction */
3035 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3036 if (acceptor
== INVALID_SOCKET
) {
3037 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3040 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3041 if (connector
== INVALID_SOCKET
) {
3042 skip("could not create connector socket, error %d\n", WSAGetLastError());
3045 connector2
= socket(AF_INET
, SOCK_STREAM
, 0);
3046 if (connector
== INVALID_SOCKET
) {
3047 skip("could not create connector socket, error %d\n", WSAGetLastError());
3051 if (set_blocking(connector
, FALSE
)) {
3052 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3056 if (set_blocking(connector2
, FALSE
)) {
3057 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3061 /* Connect socket #1 */
3062 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3063 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3065 FD_ZERO ( &fds_accept
);
3066 FD_ZERO ( &fds_send
);
3068 FD_SET ( listener
, &fds_accept
);
3069 FD_SET ( connector
, &fds_send
);
3075 for (i
= 0; i
< 4000; ++i
)
3077 fd_set fds_openaccept
= fds_accept
, fds_opensend
= fds_send
;
3079 wsa_ok ( ( select ( 0, &fds_openaccept
, &fds_opensend
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
3080 "acceptex test(%d): could not select on socket, errno %d\n" );
3082 /* check for incoming requests */
3083 if ( FD_ISSET ( listener
, &fds_openaccept
) ) {
3086 SOCKET tmp
= WSAAccept(listener
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
3087 ok(tmp
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
3088 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3089 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3090 &bytesReturned
, &overlapped
);
3091 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3093 else if (got
== 2) {
3094 /* this should be socket #2 */
3095 SOCKET tmp
= accept(listener
, NULL
, NULL
);
3096 ok(tmp
!= INVALID_SOCKET
, "accept failed %d\n", WSAGetLastError());
3100 ok(FALSE
, "Got more than 2 connections?\n");
3103 if ( conn1
&& FD_ISSET ( connector2
, &fds_opensend
) ) {
3104 /* Send data on second socket, and stop */
3105 send(connector2
, "2", 1, 0);
3106 FD_CLR ( connector2
, &fds_send
);
3110 if ( FD_ISSET ( connector
, &fds_opensend
) ) {
3111 /* Once #1 is connected, allow #2 to connect */
3114 send(connector
, "1", 1, 0);
3115 FD_CLR ( connector
, &fds_send
);
3117 iret
= connect(connector2
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3118 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3119 FD_SET ( connector2
, &fds_send
);
3123 ok (got
== 2 || broken(got
== 1) /* NT4 */,
3124 "Did not get both connections, got %d\n", got
);
3126 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3127 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3129 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3130 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3131 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3133 set_blocking(acceptor
, TRUE
);
3134 iret
= recv( acceptor
, buffer
, 2, 0);
3135 ok(iret
== 1, "Failed to get data, %d, errno: %d\n", iret
, WSAGetLastError());
3137 ok(buffer
[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer
[0]);
3139 closesocket(connector
);
3140 connector
= INVALID_SOCKET
;
3141 closesocket(acceptor
);
3142 acceptor
= INVALID_SOCKET
;
3144 /* clean up in case of failures */
3145 while ((acceptor
= accept(listener
, NULL
, NULL
)) != INVALID_SOCKET
)
3146 closesocket(acceptor
);
3148 /* Disconnect during receive? */
3150 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3151 if (acceptor
== INVALID_SOCKET
) {
3152 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3155 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3156 if (connector
== INVALID_SOCKET
) {
3157 skip("could not create connector socket, error %d\n", WSAGetLastError());
3160 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3161 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3162 &bytesReturned
, &overlapped
);
3163 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3165 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3166 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3168 closesocket(connector
);
3169 connector
= INVALID_SOCKET
;
3171 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3172 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3174 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3175 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3176 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3178 closesocket(acceptor
);
3179 acceptor
= INVALID_SOCKET
;
3181 /* Test closing with pending requests */
3183 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3184 if (acceptor
== INVALID_SOCKET
) {
3185 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3188 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3189 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3190 &bytesReturned
, &overlapped
);
3191 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3193 closesocket(acceptor
);
3194 acceptor
= INVALID_SOCKET
;
3196 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3197 ok(dwret
== WAIT_OBJECT_0
|| broken(dwret
== WAIT_TIMEOUT
) /* NT4/2000 */,
3198 "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3200 if (dwret
!= WAIT_TIMEOUT
) {
3201 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3202 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
3205 bret
= CancelIo((HANDLE
) listener
);
3206 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3210 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3211 if (acceptor
== INVALID_SOCKET
) {
3212 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3215 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3216 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3217 &bytesReturned
, &overlapped
);
3218 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3220 CancelIo((HANDLE
) acceptor
);
3222 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3223 ok(dwret
== WAIT_TIMEOUT
, "Waiting for timeout failed with %d + errno %d\n", dwret
, GetLastError());
3225 closesocket(acceptor
);
3226 acceptor
= INVALID_SOCKET
;
3228 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3229 if (acceptor
== INVALID_SOCKET
) {
3230 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3233 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3234 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3235 &bytesReturned
, &overlapped
);
3236 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3238 closesocket(listener
);
3239 listener
= INVALID_SOCKET
;
3241 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3242 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3244 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3245 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
3248 if (overlapped
.hEvent
)
3249 WSACloseEvent(overlapped
.hEvent
);
3250 if (listener
!= INVALID_SOCKET
)
3251 closesocket(listener
);
3252 if (acceptor
!= INVALID_SOCKET
)
3253 closesocket(acceptor
);
3254 if (connector
!= INVALID_SOCKET
)
3255 closesocket(connector
);
3256 if (connector2
!= INVALID_SOCKET
)
3257 closesocket(connector2
);
3260 /**************** Main program ***************/
3266 /* Leave these tests at the beginning. They depend on WSAStartup not having been
3267 * called, which is done by Init() below. */
3268 test_WithoutWSAStartup();
3269 test_WithWSAStartup();
3273 test_set_getsockopt();
3274 test_so_reuseaddr();
3275 test_extendedSocketOptions();
3277 for (i
= 0; i
< NUM_TESTS
; i
++)
3279 trace ( " **** STARTING TEST %d ****\n", i
);
3280 do_test ( &tests
[i
] );
3281 trace ( " **** TEST %d COMPLETE ****\n", i
);
3286 test_getservbyname();
3289 test_WSAAddressToStringA();
3290 test_WSAAddressToStringW();
3292 test_WSAStringToAddressA();
3293 test_WSAStringToAddressW();
3299 test_addr_to_print();
3302 test_gethostbyname_hack();
3305 test_write_events();
3310 test_GetAddrInfoW();