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
);
991 size
= sizeof(timeout
);
992 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
994 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
995 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
996 ok( timeout
== 0, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
999 timeout
= SOCKTIMEOUT2
; /* 997 seconds. See remark above */
1000 size
= sizeof(timeout
);
1001 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
1003 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
1004 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
1005 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
1007 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
1008 size
= sizeof(lingval
);
1009 lingval
= linger_testvals
[i
];
1010 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
1012 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
1013 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
1014 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
1015 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
1016 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
1017 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
1018 lingval
.l_onoff
, lingval
.l_linger
,
1019 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
1021 /* Test for erroneously passing a value instead of a pointer as optval */
1022 size
= sizeof(char);
1023 err
= setsockopt(s
, SOL_SOCKET
, SO_DONTROUTE
, (char *)1, size
);
1024 ok(err
== SOCKET_ERROR
, "setsockopt with optval being a value passed "
1025 "instead of failing.\n");
1026 lasterr
= WSAGetLastError();
1027 ok(lasterr
== WSAEFAULT
, "setsockopt with optval being a value "
1028 "returned 0x%08x, not WSAEFAULT(0x%08x)\n",
1029 lasterr
, WSAEFAULT
);
1031 /* SO_RCVTIMEO with invalid values for level */
1032 size
= sizeof(timeout
);
1033 timeout
= SOCKTIMEOUT1
;
1034 SetLastError(0xdeadbeef);
1035 err
= setsockopt(s
, 0xffffffff, SO_RCVTIMEO
, (char *) &timeout
, size
);
1036 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1037 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1038 err
, WSAGetLastError());
1040 timeout
= SOCKTIMEOUT1
;
1041 SetLastError(0xdeadbeef);
1042 err
= setsockopt(s
, 0x00008000, SO_RCVTIMEO
, (char *) &timeout
, size
);
1043 ok( (err
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
1044 "got %d with %d (expected SOCKET_ERROR with WSAEINVAL\n",
1045 err
, WSAGetLastError());
1050 static void test_so_reuseaddr(void)
1052 struct sockaddr_in saddr
;
1054 unsigned int rc
,reuse
;
1057 saddr
.sin_family
= AF_INET
;
1058 saddr
.sin_port
= htons(9375);
1059 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1061 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
1062 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1063 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1064 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
1066 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
1067 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
1071 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
1072 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
1074 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1075 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
1078 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
1079 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1081 /* On Win2k3 and above, all SO_REUSEADDR seems to do is to allow binding to
1082 * a port immediately after closing another socket on that port, so
1083 * basically following the BSD socket semantics here. */
1085 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1086 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
1091 /************* Array containing the tests to run **********/
1093 #define STD_STREAM_SOCKET \
1099 static test_setup tests
[NUM_TESTS
] =
1101 /* Test 0: synchronous client and server */
1122 /* Test 1: event-driven client, synchronous server */
1139 WSA_FLAG_OVERLAPPED
,
1143 /* Test 2: synchronous client, non-blocking server via select() */
1164 /* Test 3: synchronous mixed client and server */
1178 simple_mixed_client
,
1187 static void test_UDP(void)
1189 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1190 possible that this test fails due to dropped packets. */
1192 /* peer 0 receives data from all other peers */
1193 struct sock_info peer
[NUM_UDP_PEERS
];
1195 int ss
, i
, n_recv
, n_sent
;
1197 memset (buf
,0,sizeof(buf
));
1198 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
1199 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
1201 peer
[i
].addr
.sin_family
= AF_INET
;
1202 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
1205 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
1207 peer
[i
].addr
.sin_port
= htons ( 0 );
1210 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
1212 /* test getsockname() to get peer's port */
1213 ss
= sizeof ( peer
[i
].addr
);
1214 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
1215 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
1218 /* test getsockname() */
1219 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
1221 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1222 /* send client's ip */
1223 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
1224 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
1225 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
1228 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1229 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
1230 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
1231 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
1235 static void WINAPI
do_getservbyname( HANDLE
*starttest
)
1241 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1244 struct servent
*pserv
[2];
1246 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
, "test_getservbyname: timeout waiting for start signal\n");
1248 /* ensure that necessary buffer resizes are completed */
1249 for ( j
= 0; j
< 2; j
++) {
1250 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1253 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
1254 for ( j
= 0; j
< 2; j
++ ) {
1255 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1256 ok ( pserv
[j
] != NULL
, "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
1257 ok ( pserv
[j
]->s_port
== htons(serv
[j
].port
), "getservbyname returned the wrong port for %s: %d\n", serv
[j
].name
, ntohs(pserv
[j
]->s_port
) );
1258 ok ( !strcmp ( pserv
[j
]->s_proto
, serv
[j
].proto
), "getservbyname returned the wrong protocol for %s: %s\n", serv
[j
].name
, pserv
[j
]->s_proto
);
1259 ok ( !strcmp ( pserv
[j
]->s_name
, serv
[j
].name
), "getservbyname returned the wrong name for %s: %s\n", serv
[j
].name
, pserv
[j
]->s_name
);
1262 ok ( pserv
[0] == pserv
[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1266 static void test_getservbyname(void)
1269 HANDLE starttest
, thread
[NUM_THREADS
];
1270 DWORD thread_id
[NUM_THREADS
];
1272 starttest
= CreateEvent ( NULL
, 1, 0, "test_getservbyname_starttest" );
1274 /* create threads */
1275 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
1276 thread
[i
] = CreateThread ( NULL
, 0, (LPTHREAD_START_ROUTINE
) &do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
1279 /* signal threads to start */
1280 SetEvent ( starttest
);
1282 for ( i
= 0; i
< NUM_THREADS
; i
++) {
1283 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
1287 static void test_WSASocket(void)
1289 SOCKET sock
= INVALID_SOCKET
;
1290 WSAPROTOCOL_INFOA
*pi
;
1291 int providers
[] = {6, 0};
1295 /* Set pi_size explicitly to a value below 2*sizeof(WSAPROTOCOL_INFOA)
1296 * to avoid a crash on win98.
1299 ret
= WSAEnumProtocolsA(providers
, NULL
, &pi_size
);
1300 ok(ret
== SOCKET_ERROR
, "WSAEnumProtocolsA({6,0}, NULL, 0) returned %d\n",
1302 err
= WSAGetLastError();
1303 ok(err
== WSAENOBUFS
, "WSAEnumProtocolsA error is %d, not WSAENOBUFS(%d)\n",
1306 pi
= HeapAlloc(GetProcessHeap(), 0, pi_size
);
1307 ok(pi
!= NULL
, "Failed to allocate memory\n");
1309 skip("Can't continue without memory.\n");
1313 ret
= WSAEnumProtocolsA(providers
, pi
, &pi_size
);
1314 ok(ret
!= SOCKET_ERROR
, "WSAEnumProtocolsA failed, last error is %d\n",
1318 skip("No protocols enumerated.\n");
1319 HeapFree(GetProcessHeap(), 0, pi
);
1323 sock
= WSASocketA(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
1324 FROM_PROTOCOL_INFO
, &pi
[0], 0, 0);
1325 ok(sock
!= INVALID_SOCKET
, "Failed to create socket: %d\n",
1329 HeapFree(GetProcessHeap(), 0, pi
);
1332 static void test_WSAAddressToStringA(void)
1334 SOCKET v6
= INVALID_SOCKET
;
1338 SOCKADDR_IN sockaddr
;
1339 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1341 CHAR expect1
[] = "0.0.0.0";
1342 CHAR expect2
[] = "255.255.255.255";
1343 CHAR expect3
[] = "0.0.0.0:65535";
1344 CHAR expect4
[] = "255.255.255.255:65535";
1346 SOCKADDR_IN6 sockaddr6
;
1347 CHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1349 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1350 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1351 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1353 CHAR expect6_1
[] = "::1";
1354 CHAR expect6_2
[] = "20ab::1";
1355 CHAR expect6_3
[] = "[20ab::2001]:33274";
1356 CHAR expect6_3_2
[] = "[20ab::2001%4660]:33274";
1357 CHAR expect6_3_3
[] = "20ab::2001%4660";
1361 sockaddr
.sin_family
= AF_INET
;
1362 sockaddr
.sin_port
= 0;
1363 sockaddr
.sin_addr
.s_addr
= 0;
1365 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1366 GLE
= WSAGetLastError();
1367 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1368 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1371 len
= sizeof(address
);
1373 sockaddr
.sin_family
= AF_INET
;
1374 sockaddr
.sin_port
= 0;
1375 sockaddr
.sin_addr
.s_addr
= 0;
1377 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1378 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1380 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
1381 ok( len
== sizeof( expect1
), "Got size %d\n", len
);
1383 len
= sizeof(address
);
1385 sockaddr
.sin_family
= AF_INET
;
1386 sockaddr
.sin_port
= 0;
1387 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1389 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1390 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1392 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
1394 len
= sizeof(address
);
1396 sockaddr
.sin_family
= AF_INET
;
1397 sockaddr
.sin_port
= 0xffff;
1398 sockaddr
.sin_addr
.s_addr
= 0;
1400 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1401 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1403 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
1405 len
= sizeof(address
);
1407 sockaddr
.sin_family
= AF_INET
;
1408 sockaddr
.sin_port
= 0xffff;
1409 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1411 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1412 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1414 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
1415 ok( len
== sizeof( expect4
), "Got size %d\n", len
);
1417 /*check to see it IPv6 is available */
1418 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1419 if (v6
== INVALID_SOCKET
) {
1420 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1421 WSAGetLastError(), WSAEAFNOSUPPORT
);
1424 /* Test a short IPv6 address */
1425 len
= sizeof(address6
);
1427 sockaddr6
.sin6_family
= AF_INET6
;
1428 sockaddr6
.sin6_port
= 0x0000;
1429 sockaddr6
.sin6_scope_id
= 0;
1430 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1432 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1433 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1434 ok( !strcmp( address6
, expect6_1
), "Expected: %s, got: %s\n", expect6_1
, address6
);
1435 ok( len
== sizeof(expect6_1
), "Got size %d\n", len
);
1437 /* Test a longer IPv6 address */
1438 len
= sizeof(address6
);
1440 sockaddr6
.sin6_family
= AF_INET6
;
1441 sockaddr6
.sin6_port
= 0x0000;
1442 sockaddr6
.sin6_scope_id
= 0;
1443 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1445 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1446 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1447 ok( !strcmp( address6
, expect6_2
), "Expected: %s, got: %s\n", expect6_2
, address6
);
1448 ok( len
== sizeof(expect6_2
), "Got size %d\n", len
);
1450 /* Test IPv6 address and port number */
1451 len
= sizeof(address6
);
1453 sockaddr6
.sin6_family
= AF_INET6
;
1454 sockaddr6
.sin6_port
= 0xfa81;
1455 sockaddr6
.sin6_scope_id
= 0;
1456 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1458 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1459 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1460 ok( !strcmp( address6
, expect6_3
), "Expected: %s, got: %s\n", expect6_3
, address6
);
1461 ok( len
== sizeof(expect6_3
), "Got size %d\n", len
);
1463 /* Test IPv6 address, port number and scope_id */
1464 len
= sizeof(address6
);
1466 sockaddr6
.sin6_family
= AF_INET6
;
1467 sockaddr6
.sin6_port
= 0xfa81;
1468 sockaddr6
.sin6_scope_id
= 0x1234;
1469 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1471 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1472 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1473 ok( !strcmp( address6
, expect6_3_2
), "Expected: %s, got: %s\n", expect6_3_2
, address6
);
1474 ok( len
== sizeof(expect6_3_2
), "Got size %d\n", len
);
1476 /* Test IPv6 address and scope_id */
1477 len
= sizeof(address6
);
1479 sockaddr6
.sin6_family
= AF_INET6
;
1480 sockaddr6
.sin6_port
= 0x0000;
1481 sockaddr6
.sin6_scope_id
= 0x1234;
1482 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1484 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1485 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1486 ok( !strcmp( address6
, expect6_3_3
), "Expected: %s, got: %s\n", expect6_3_3
, address6
);
1487 ok( len
== sizeof(expect6_3_3
), "Got size %d\n", len
);
1490 if (v6
!= INVALID_SOCKET
)
1494 static void test_WSAAddressToStringW(void)
1496 SOCKET v6
= INVALID_SOCKET
;
1500 SOCKADDR_IN sockaddr
;
1501 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1503 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
1504 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1505 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1506 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1507 '6', '5', '5', '3', '5', 0 };
1509 SOCKADDR_IN6 sockaddr6
;
1510 WCHAR address6
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
1512 CHAR addr6_1
[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1513 CHAR addr6_2
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
1514 CHAR addr6_3
[] = {0x20,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x01};
1516 WCHAR expect6_1
[] = {':',':','1',0};
1517 WCHAR expect6_2
[] = {'2','0','a','b',':',':','1',0};
1518 WCHAR expect6_3
[] = {'[','2','0','a','b',':',':','2','0','0','1',']',':','3','3','2','7','4',0};
1519 WCHAR expect6_3_2
[] = {'[','2','0','a','b',':',':','2','0','0','1','%','4','6','6','0',']',':','3','3','2','7','4',0};
1520 WCHAR expect6_3_3
[] = {'2','0','a','b',':',':','2','0','0','1','%','6','5','5','3','4',0};
1524 sockaddr
.sin_family
= AF_INET
;
1525 sockaddr
.sin_port
= 0;
1526 sockaddr
.sin_addr
.s_addr
= 0;
1528 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1529 GLE
= WSAGetLastError();
1530 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1531 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1534 len
= sizeof(address
);
1536 sockaddr
.sin_family
= AF_INET
;
1537 sockaddr
.sin_port
= 0;
1538 sockaddr
.sin_addr
.s_addr
= 0;
1540 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1541 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1543 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
1544 ok( len
== sizeof( expect1
)/sizeof( WCHAR
), "Got size %d\n", len
);
1546 len
= sizeof(address
);
1548 sockaddr
.sin_family
= AF_INET
;
1549 sockaddr
.sin_port
= 0;
1550 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1552 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1553 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1555 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
1557 len
= sizeof(address
);
1559 sockaddr
.sin_family
= AF_INET
;
1560 sockaddr
.sin_port
= 0xffff;
1561 sockaddr
.sin_addr
.s_addr
= 0;
1563 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1564 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1566 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
1568 len
= sizeof(address
);
1570 sockaddr
.sin_family
= AF_INET
;
1571 sockaddr
.sin_port
= 0xffff;
1572 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1574 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1575 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1577 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
1578 ok( len
== sizeof( expect4
)/sizeof( WCHAR
), "Got %d\n", len
);
1580 /*check to see it IPv6 is available */
1581 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1582 if (v6
== INVALID_SOCKET
) {
1583 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
1584 WSAGetLastError(), WSAEAFNOSUPPORT
);
1588 /* Test a short IPv6 address */
1589 len
= sizeof(address6
)/sizeof(WCHAR
);
1591 sockaddr6
.sin6_family
= AF_INET6
;
1592 sockaddr6
.sin6_port
= 0x0000;
1593 sockaddr6
.sin6_scope_id
= 0;
1594 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_1
, sizeof(addr6_1
));
1596 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1597 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1598 ok( !lstrcmpW( address6
, expect6_1
), "Wrong string returned\n" );
1599 ok( len
== sizeof(expect6_1
)/sizeof(WCHAR
), "Got %d\n", len
);
1601 /* Test a longer IPv6 address */
1602 len
= sizeof(address6
)/sizeof(WCHAR
);
1604 sockaddr6
.sin6_family
= AF_INET6
;
1605 sockaddr6
.sin6_port
= 0x0000;
1606 sockaddr6
.sin6_scope_id
= 0;
1607 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_2
, sizeof(addr6_2
));
1609 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1610 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1612 ok( !lstrcmpW( address6
, expect6_2
), "Wrong string returned\n" );
1613 ok( len
== sizeof(expect6_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1615 /* Test IPv6 address and port number */
1616 len
= sizeof(address6
)/sizeof(WCHAR
);
1618 sockaddr6
.sin6_family
= AF_INET6
;
1619 sockaddr6
.sin6_port
= 0xfa81;
1620 sockaddr6
.sin6_scope_id
= 0;
1621 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1623 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1624 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1625 ok( !lstrcmpW( address6
, expect6_3
), "Wrong string returned\n" );
1626 ok( len
== sizeof(expect6_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1628 /* Test IPv6 address, port number and scope_id */
1629 len
= sizeof(address6
)/sizeof(WCHAR
);
1631 sockaddr6
.sin6_family
= AF_INET6
;
1632 sockaddr6
.sin6_port
= 0xfa81;
1633 sockaddr6
.sin6_scope_id
= 0x1234;
1634 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1636 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1637 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1638 ok( !lstrcmpW( address6
, expect6_3_2
), "Wrong string returned\n" );
1639 ok( len
== sizeof(expect6_3_2
)/sizeof(WCHAR
), "Got %d\n", len
);
1641 /* Test IPv6 address and scope_id */
1642 len
= sizeof(address6
)/sizeof(WCHAR
);
1644 sockaddr6
.sin6_family
= AF_INET6
;
1645 sockaddr6
.sin6_port
= 0x0000;
1646 sockaddr6
.sin6_scope_id
= 0xfffe;
1647 memcpy (sockaddr6
.sin6_addr
.s6_addr
, addr6_3
, sizeof(addr6_3
));
1649 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr6
, sizeof(sockaddr6
), NULL
, address6
, &len
);
1650 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1651 ok( !lstrcmpW( address6
, expect6_3_3
), "Wrong string returned\n" );
1652 ok( len
== sizeof(expect6_3_3
)/sizeof(WCHAR
), "Got %d\n", len
);
1655 if (v6
!= INVALID_SOCKET
)
1659 static void test_WSAStringToAddressA(void)
1662 SOCKADDR_IN sockaddr
;
1663 SOCKADDR_IN6 sockaddr6
;
1666 CHAR address1
[] = "0.0.0.0";
1667 CHAR address2
[] = "127.127.127.127";
1668 CHAR address3
[] = "255.255.255.255";
1669 CHAR address4
[] = "127.127.127.127:65535";
1670 CHAR address5
[] = "255.255.255.255:65535";
1671 CHAR address6
[] = "::1";
1672 CHAR address7
[] = "[::1]";
1673 CHAR address8
[] = "[::1]:65535";
1676 sockaddr
.sin_family
= AF_INET
;
1678 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1679 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1680 WSAGetLastError() );
1682 len
= sizeof(sockaddr
);
1683 sockaddr
.sin_port
= 0;
1684 sockaddr
.sin_addr
.s_addr
= 0;
1686 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1687 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1688 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1690 len
= sizeof(sockaddr
);
1691 sockaddr
.sin_port
= 0;
1692 sockaddr
.sin_addr
.s_addr
= 0;
1694 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1695 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1696 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1698 len
= sizeof(sockaddr
);
1699 sockaddr
.sin_port
= 0;
1700 sockaddr
.sin_addr
.s_addr
= 0;
1702 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1703 GLE
= WSAGetLastError();
1704 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1705 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1706 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1708 len
= sizeof(sockaddr
);
1709 sockaddr
.sin_port
= 0;
1710 sockaddr
.sin_addr
.s_addr
= 0;
1712 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1713 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1714 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1716 len
= sizeof(sockaddr
);
1717 sockaddr
.sin_port
= 0;
1718 sockaddr
.sin_addr
.s_addr
= 0;
1720 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1721 GLE
= WSAGetLastError();
1722 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1723 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1724 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1726 len
= sizeof(sockaddr6
);
1727 memset(&sockaddr6
, 0, len
);
1728 sockaddr6
.sin6_family
= AF_INET6
;
1730 ret
= WSAStringToAddressA( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1732 GLE
= WSAGetLastError();
1733 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1734 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1736 len
= sizeof(sockaddr6
);
1737 memset(&sockaddr6
, 0, len
);
1738 sockaddr6
.sin6_family
= AF_INET6
;
1740 ret
= WSAStringToAddressA( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1742 GLE
= WSAGetLastError();
1743 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1744 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1746 len
= sizeof(sockaddr6
);
1747 memset(&sockaddr6
, 0, len
);
1748 sockaddr6
.sin6_family
= AF_INET6
;
1750 ret
= WSAStringToAddressA( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1752 GLE
= WSAGetLastError();
1753 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1754 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1755 "WSAStringToAddressA() failed for IPv6 address: %d\n", GLE
);
1759 static void test_WSAStringToAddressW(void)
1762 SOCKADDR_IN sockaddr
;
1763 SOCKADDR_IN6 sockaddr6
;
1766 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
1767 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1768 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1769 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1770 ':', '6', '5', '5', '3', '5', 0 };
1771 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1772 '6', '5', '5', '3', '5', 0 };
1773 WCHAR address6
[] = {':',':','1','\0'};
1774 WCHAR address7
[] = {'[',':',':','1',']','\0'};
1775 WCHAR address8
[] = {'[',':',':','1',']',':','6','5','5','3','5','\0'};
1778 sockaddr
.sin_family
= AF_INET
;
1780 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1781 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
1782 WSAGetLastError() );
1784 len
= sizeof(sockaddr
);
1785 sockaddr
.sin_port
= 0;
1786 sockaddr
.sin_addr
.s_addr
= 0;
1788 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1789 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1790 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1792 len
= sizeof(sockaddr
);
1793 sockaddr
.sin_port
= 0;
1794 sockaddr
.sin_addr
.s_addr
= 0;
1796 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1797 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1798 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1800 len
= sizeof(sockaddr
);
1801 sockaddr
.sin_port
= 0;
1802 sockaddr
.sin_addr
.s_addr
= 0;
1804 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1805 GLE
= WSAGetLastError();
1806 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1807 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1808 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1810 len
= sizeof(sockaddr
);
1811 sockaddr
.sin_port
= 0;
1812 sockaddr
.sin_addr
.s_addr
= 0;
1814 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1815 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1816 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1818 len
= sizeof(sockaddr
);
1819 sockaddr
.sin_port
= 0;
1820 sockaddr
.sin_addr
.s_addr
= 0;
1822 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1823 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1824 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1825 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1827 len
= sizeof(sockaddr6
);
1828 memset(&sockaddr6
, 0, len
);
1829 sockaddr6
.sin6_family
= AF_INET6
;
1831 ret
= WSAStringToAddressW( address6
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1833 GLE
= WSAGetLastError();
1834 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1835 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1837 len
= sizeof(sockaddr6
);
1838 memset(&sockaddr6
, 0, len
);
1839 sockaddr6
.sin6_family
= AF_INET6
;
1841 ret
= WSAStringToAddressW( address7
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1843 GLE
= WSAGetLastError();
1844 ok( ret
== 0 || (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1845 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1847 len
= sizeof(sockaddr6
);
1848 memset(&sockaddr6
, 0, len
);
1849 sockaddr6
.sin6_family
= AF_INET6
;
1851 ret
= WSAStringToAddressW( address8
, AF_INET6
, NULL
, (SOCKADDR
*)&sockaddr6
,
1853 GLE
= WSAGetLastError();
1854 ok( (ret
== 0 && sockaddr6
.sin6_port
== 0xffff) ||
1855 (ret
== SOCKET_ERROR
&& GLE
== WSAEINVAL
),
1856 "WSAStringToAddressW() failed for IPv6 address: %d\n", GLE
);
1860 static VOID WINAPI
SelectReadThread(select_thread_params
*par
)
1864 struct sockaddr_in addr
;
1865 struct timeval select_timeout
;
1868 FD_SET(par
->s
, &readfds
);
1869 select_timeout
.tv_sec
=5;
1870 select_timeout
.tv_usec
=0;
1871 addr
.sin_family
= AF_INET
;
1872 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
1873 addr
.sin_port
= htons(SERVERPORT
);
1875 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
1876 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1878 SetEvent(server_ready
);
1879 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
1880 par
->ReadKilled
= (ret
== 1);
1883 static void test_select(void)
1885 SOCKET fdRead
, fdWrite
;
1886 fd_set readfds
, writefds
, exceptfds
;
1889 struct timeval select_timeout
;
1890 select_thread_params thread_params
;
1891 HANDLE thread_handle
;
1894 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
1895 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1896 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
1897 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1901 FD_ZERO(&exceptfds
);
1902 FD_SET(fdRead
, &readfds
);
1903 FD_SET(fdWrite
, &writefds
);
1904 FD_SET(fdRead
, &exceptfds
);
1905 FD_SET(fdWrite
, &exceptfds
);
1906 select_timeout
.tv_sec
=0;
1907 select_timeout
.tv_usec
=500;
1910 if (fdWrite
> maxfd
)
1914 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
1915 ok ( (ret
== 0), "select should not return any socket handles\n");
1916 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
1917 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
1920 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
1921 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
1924 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
1926 ret
= closesocket(fdWrite
);
1927 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1929 thread_params
.s
= fdRead
;
1930 thread_params
.ReadKilled
= FALSE
;
1931 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1932 thread_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) &SelectReadThread
, &thread_params
, 0, &id
);
1933 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
1935 WaitForSingleObject (server_ready
, INFINITE
);
1937 ret
= closesocket(fdRead
);
1938 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1940 WaitForSingleObject (thread_handle
, 1000);
1941 ok ( (thread_params
.ReadKilled
) ||
1942 broken(thread_params
.ReadKilled
== 0), /*Win98*/
1943 "closesocket did not wakeup select\n");
1947 static DWORD WINAPI
AcceptKillThread(select_thread_params
*par
)
1949 struct sockaddr_in address
;
1950 int len
= sizeof(address
);
1951 SOCKET client_socket
;
1953 SetEvent(server_ready
);
1954 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
1955 if (client_socket
!= INVALID_SOCKET
)
1956 closesocket(client_socket
);
1957 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
1962 static int CALLBACK
AlwaysDeferConditionFunc(LPWSABUF lpCallerId
, LPWSABUF lpCallerData
, LPQOS pQos
,
1963 LPQOS lpGQOS
, LPWSABUF lpCalleeId
, LPWSABUF lpCalleeData
,
1964 GROUP FAR
* g
, DWORD_PTR dwCallbackData
)
1969 static void test_accept(void)
1972 SOCKET server_socket
= INVALID_SOCKET
, accepted
= INVALID_SOCKET
, connector
= INVALID_SOCKET
;
1973 struct sockaddr_in address
;
1975 select_thread_params thread_params
;
1976 HANDLE thread_handle
= NULL
;
1979 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
1980 if (server_socket
== INVALID_SOCKET
)
1982 trace("error creating server socket: %d\n", WSAGetLastError());
1986 memset(&address
, 0, sizeof(address
));
1987 address
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
1988 address
.sin_family
= AF_INET
;
1989 ret
= bind(server_socket
, (struct sockaddr
*) &address
, sizeof(address
));
1992 trace("error binding server socket: %d\n", WSAGetLastError());
1996 socklen
= sizeof(address
);
1997 ret
= getsockname(server_socket
, (struct sockaddr
*)&address
, &socklen
);
1999 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2003 ret
= listen(server_socket
, 5);
2006 trace("error making server socket listen: %d\n", WSAGetLastError());
2010 trace("Blocking accept next\n");
2012 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2013 ok(connector
!= INVALID_SOCKET
, "Failed to create connector socket, error %d\n", WSAGetLastError());
2015 ret
= connect(connector
, (struct sockaddr
*)&address
, sizeof(address
));
2016 ok(ret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
2018 accepted
= WSAAccept(server_socket
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
2019 ok(accepted
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
2021 accepted
= accept(server_socket
, NULL
, 0);
2022 ok(accepted
!= INVALID_SOCKET
, "Failed to accept deferred connection, error %d\n", WSAGetLastError());
2024 server_ready
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
2025 if (server_ready
== INVALID_HANDLE_VALUE
)
2027 trace("error creating event: %d\n", GetLastError());
2031 thread_params
.s
= server_socket
;
2032 thread_params
.ReadKilled
= FALSE
;
2033 thread_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
) AcceptKillThread
,
2034 &thread_params
, 0, &id
);
2035 if (thread_handle
== NULL
)
2037 trace("error creating thread: %d\n", GetLastError());
2041 WaitForSingleObject(server_ready
, INFINITE
);
2043 ret
= closesocket(server_socket
);
2046 trace("closesocket failed: %d\n", WSAGetLastError());
2050 WaitForSingleObject(thread_handle
, 1000);
2051 ok(thread_params
.ReadKilled
|| broken(!thread_params
.ReadKilled
) /* Win98/ME, after accept */,
2052 "closesocket did not wakeup accept\n");
2055 if (accepted
!= INVALID_SOCKET
)
2056 closesocket(accepted
);
2057 if (connector
!= INVALID_SOCKET
)
2058 closesocket(connector
);
2059 if (thread_handle
!= NULL
)
2060 CloseHandle(thread_handle
);
2061 if (server_ready
!= INVALID_HANDLE_VALUE
)
2062 CloseHandle(server_ready
);
2063 if (server_socket
!= INVALID_SOCKET
)
2064 closesocket(server_socket
);
2067 static void test_extendedSocketOptions(void)
2071 struct sockaddr_in sa
;
2072 int sa_len
= sizeof(struct sockaddr_in
);
2073 int optval
, optlen
= sizeof(int), ret
;
2077 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2078 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2082 memset(&sa
, 0, sa_len
);
2084 sa
.sin_family
= AF_INET
;
2085 sa
.sin_port
= htons(0);
2086 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2088 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2089 trace("Creating the socket failed: %d\n", WSAGetLastError());
2094 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2095 trace("Failed to bind socket: %d\n", WSAGetLastError());
2101 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2103 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
2104 ok((optval
== 65507) || (optval
== 65527),
2105 "SO_MAX_MSG_SIZE reported %d, expected 65507 or 65527\n", optval
);
2107 /* IE 3 use 0xffffffff instead of SOL_SOCKET (0xffff) */
2108 SetLastError(0xdeadbeef);
2109 optval
= 0xdeadbeef;
2110 optlen
= sizeof(int);
2111 ret
= getsockopt(sock
, 0xffffffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2112 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2113 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2114 ret
, WSAGetLastError(), optval
, optval
);
2116 /* more invalid values for level */
2117 SetLastError(0xdeadbeef);
2118 optval
= 0xdeadbeef;
2119 optlen
= sizeof(int);
2120 ret
= getsockopt(sock
, 0x1234ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2121 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2122 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2123 ret
, WSAGetLastError(), optval
, optval
);
2125 SetLastError(0xdeadbeef);
2126 optval
= 0xdeadbeef;
2127 optlen
= sizeof(int);
2128 ret
= getsockopt(sock
, 0x8000ffff, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2129 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2130 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2131 ret
, WSAGetLastError(), optval
, optval
);
2133 SetLastError(0xdeadbeef);
2134 optval
= 0xdeadbeef;
2135 optlen
= sizeof(int);
2136 ret
= getsockopt(sock
, 0x00008000, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2137 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2138 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2139 ret
, WSAGetLastError(), optval
, optval
);
2141 SetLastError(0xdeadbeef);
2142 optval
= 0xdeadbeef;
2143 optlen
= sizeof(int);
2144 ret
= getsockopt(sock
, 0x00000800, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
2145 ok( (ret
== SOCKET_ERROR
) && (WSAGetLastError() == WSAEINVAL
),
2146 "got %d with %d and optval: 0x%x/%d (expected SOCKET_ERROR with WSAEINVAL)\n",
2147 ret
, WSAGetLastError(), optval
, optval
);
2149 optlen
= sizeof(LINGER
);
2150 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2152 ok(ret
== SOCKET_ERROR
, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret
);
2157 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2158 trace("Creating the socket failed: %d\n", WSAGetLastError());
2163 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
2164 trace("Failed to bind socket: %d\n", WSAGetLastError());
2170 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
2171 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
2173 optlen
= sizeof(BOOL
);
2174 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
2175 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
2176 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
2177 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
2178 bool_opt_val
, linger_val
.l_onoff
);
2184 static void test_getsockname(void)
2188 struct sockaddr_in sa_set
, sa_get
;
2189 int sa_set_len
= sizeof(struct sockaddr_in
);
2190 int sa_get_len
= sa_set_len
;
2191 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
2194 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
2195 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
2199 memset(&sa_set
, 0, sa_set_len
);
2201 sa_set
.sin_family
= AF_INET
;
2202 sa_set
.sin_port
= htons(0);
2203 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2205 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) == INVALID_SOCKET
) {
2206 trace("Creating the socket failed: %d\n", WSAGetLastError());
2211 memcpy(&sa_get
, &sa_set
, sizeof(sa_set
));
2212 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
2213 ok(0, "getsockname on unbound socket should fail\n");
2215 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
2216 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
2217 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
2218 "failed getsockname modified sockaddr when it shouldn't\n");
2221 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
2222 trace("Failed to bind socket: %d\n", WSAGetLastError());
2228 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
2229 trace("Failed to call getsockname: %d\n", WSAGetLastError());
2235 ret
= memcmp(sa_get
.sin_zero
, null_padding
, 8);
2236 ok(ret
== 0 || broken(ret
!= 0), /* NT4 */
2237 "getsockname did not zero the sockaddr_in structure\n");
2243 static void test_dns(void)
2247 h
= gethostbyname("");
2248 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
2251 /* Our winsock headers don't define gethostname because it conflicts with the
2252 * definition in unistd.h. Define it here to get rid of the warning. */
2254 int WINAPI
gethostname(char *name
, int namelen
);
2256 static void test_gethostbyname_hack(void)
2260 static BYTE loopback
[] = {127, 0, 0, 1};
2261 static BYTE magic_loopback
[] = {127, 12, 34, 56};
2264 ret
= gethostname(name
, 256);
2265 ok(ret
== 0, "gethostname() call failed: %d\n", WSAGetLastError());
2267 he
= gethostbyname("localhost");
2268 ok(he
!= NULL
, "gethostbyname(\"localhost\") failed: %d\n", h_errno
);
2271 if(he
->h_length
!= 4)
2273 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2277 ok(memcmp(he
->h_addr_list
[0], loopback
, he
->h_length
) == 0,
2278 "gethostbyname(\"localhost\") returned %d.%d.%d.%d\n",
2279 he
->h_addr_list
[0][0], he
->h_addr_list
[0][1], he
->h_addr_list
[0][2],
2280 he
->h_addr_list
[0][3]);
2283 /* No reason to test further with NULL hostname */
2287 if(strcmp(name
, "localhost") == 0)
2289 skip("hostname seems to be \"localhost\", skipping test.\n");
2294 he
= gethostbyname(name
);
2295 ok(he
!= NULL
, "gethostbyname(\"%s\") failed: %d\n", name
, h_errno
);
2298 if(he
->h_length
!= 4)
2300 skip("h_length is %d, not IPv4, skipping test.\n", he
->h_length
);
2304 if (he
->h_addr_list
[0][0] == 127)
2306 ok(memcmp(he
->h_addr_list
[0], magic_loopback
, he
->h_length
) == 0,
2307 "gethostbyname(\"%s\") returned %d.%d.%d.%d not 127.12.34.56\n",
2308 name
, he
->h_addr_list
[0][0], he
->h_addr_list
[0][1],
2309 he
->h_addr_list
[0][2], he
->h_addr_list
[0][3]);
2314 he
= gethostbyname("nonexistent.winehq.org");
2315 /* Don't check for the return value, as some braindead ISPs will kindly
2316 * resolve nonexistent host names to addresses of the ISP's spam pages. */
2319 static void test_inet_addr(void)
2323 addr
= inet_addr(NULL
);
2324 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
2327 static void test_addr_to_print(void)
2333 struct in6_addr in6
;
2335 u_long addr0_Num
= 0x00000000;
2336 PCSTR addr0_Str
= "0.0.0.0";
2337 u_long addr1_Num
= 0x20201015;
2338 PCSTR addr1_Str
= "21.16.32.32";
2339 u_char addr2_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xfe,0xcC,0x98,0xbd,0x74};
2340 PCSTR addr2_Str
= "::fffe:cc98:bd74";
2341 u_char addr3_Num
[16] = {0x20,0x30,0xa4,0xb1};
2342 PCSTR addr3_Str
= "2030:a4b1::";
2343 u_char addr4_Num
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0xcC,0x98,0xbd,0x74};
2344 PCSTR addr4_Str
= "::204.152.189.116";
2346 /* Test IPv4 addresses */
2347 in
.s_addr
= addr0_Num
;
2349 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2350 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2351 ok(!strcmp(pdst
, addr0_Str
),"Address %s != %s\n", pdst
, addr0_Str
);
2353 /* Test that inet_ntoa and inet_ntop return the same value */
2354 in
.S_un
.S_addr
= addr1_Num
;
2355 pdst
= inet_ntoa(*((struct in_addr
*)&in
.s_addr
));
2356 ok(pdst
!= NULL
, "inet_ntoa failed %s\n", dst
);
2357 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2359 /* InetNtop became available in Vista and Win2008 */
2362 win_skip("InetNtop not present, not executing tests\n");
2366 /* Second part of test */
2367 pdst
= pInetNtop(AF_INET
,(void*)&in
.s_addr
, dst
, sizeof(dst
));
2368 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst
);
2369 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2371 /* Test invalid parm conditions */
2372 pdst
= pInetNtop(1, (void*)&in
.s_addr
, dst
, sizeof(dst
));
2373 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2374 ok(WSAGetLastError() == WSAEAFNOSUPPORT
, "Should be WSAEAFNOSUPPORT\n");
2376 /* Test Null destination */
2378 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, NULL
, sizeof(dst
));
2379 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2380 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2381 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2383 /* Test zero length passed */
2386 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 0);
2387 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2388 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2389 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2391 /* Test length one shorter than the address length */
2394 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, 6);
2395 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2396 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2397 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2399 /* Test longer length is ok */
2402 pdst
= pInetNtop(AF_INET
, (void*)&in
.s_addr
, dst
, sizeof(dst
)+1);
2403 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2404 ok(!strcmp(pdst
, addr1_Str
),"Address %s != %s\n", pdst
, addr1_Str
);
2406 /* Test the IPv6 addresses */
2408 /* Test an zero prefixed IPV6 address */
2409 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2410 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2411 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2412 ok(!strcmp(pdst
, addr2_Str
),"Address %s != %s\n", pdst
, addr2_Str
);
2414 /* Test an zero suffixed IPV6 address */
2415 memcpy(in6
.s6_addr
, addr3_Num
, sizeof(addr3_Num
));
2416 pdst
= pInetNtop(AF_INET6
,(void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2417 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2418 ok(!strcmp(pdst
, addr3_Str
),"Address %s != %s\n", pdst
, addr3_Str
);
2420 /* Test the IPv6 address contains the IPv4 address in IPv4 notation */
2421 memcpy(in6
.s6_addr
, addr4_Num
, sizeof(addr4_Num
));
2422 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, sizeof(dst6
));
2423 ok(pdst
!= NULL
, "InetNtop failed %s\n", dst6
);
2424 ok(!strcmp(pdst
, addr4_Str
),"Address %s != %s\n", pdst
, addr4_Str
);
2426 /* Test invalid parm conditions */
2427 memcpy(in6
.u
.Byte
, addr2_Num
, sizeof(addr2_Num
));
2429 /* Test Null destination */
2431 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, NULL
, sizeof(dst6
));
2432 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2433 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2434 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2436 /* Test zero length passed */
2439 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 0);
2440 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2441 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2442 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2444 /* Test length one shorter than the address length */
2447 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 16);
2448 ok(pdst
== NULL
, "The pointer should not be returned (%p)\n", pdst
);
2449 ok(WSAGetLastError() == STATUS_INVALID_PARAMETER
|| WSAGetLastError() == WSAEINVAL
/* Win7 */,
2450 "Should be STATUS_INVALID_PARAMETER or WSAEINVAL not 0x%x\n", WSAGetLastError());
2452 /* Test longer length is ok */
2455 pdst
= pInetNtop(AF_INET6
, (void*)&in6
.s6_addr
, dst6
, 18);
2456 ok(pdst
!= NULL
, "The pointer should be returned (%p)\n", pdst
);
2459 static void test_ioctlsocket(void)
2463 static const LONG cmds
[] = {FIONBIO
, FIONREAD
, SIOCATMARK
};
2467 sock
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2468 ok(sock
!= INVALID_SOCKET
, "Creating the socket failed: %d\n", WSAGetLastError());
2469 if(sock
== INVALID_SOCKET
)
2471 skip("Can't continue without a socket.\n");
2475 for(i
= 0; i
< sizeof(cmds
)/sizeof(cmds
[0]); i
++)
2477 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2478 ret
= ioctlsocket(sock
, cmds
[i
], (u_long
*)1);
2479 ok(ret
== SOCKET_ERROR
, "ioctlsocket succeeded unexpectedly\n");
2480 ret
= WSAGetLastError();
2481 ok(ret
== WSAEFAULT
, "expected WSAEFAULT, got %d instead\n", ret
);
2484 /* A fresh and not connected socket has no urgent data, this test shows
2485 * that normal(not urgent) data returns a non-zero value for SIOCATMARK. */
2487 ret
= ioctlsocket(sock
, SIOCATMARK
, &arg
);
2488 if(ret
!= SOCKET_ERROR
)
2489 todo_wine
ok(arg
, "expected a non-zero value\n");
2492 static int drain_pause
=0;
2493 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
2496 SOCKET sock
= *(SOCKET
*)arg
;
2499 while ((ret
= recv(sock
, buffer
, sizeof(buffer
), 0)) != 0)
2503 if (WSAGetLastError() == WSAEWOULDBLOCK
)
2507 FD_SET(sock
, &readset
);
2508 select(0, &readset
, NULL
, NULL
, NULL
);
2519 static void test_send(void)
2521 SOCKET src
= INVALID_SOCKET
;
2522 SOCKET dst
= INVALID_SOCKET
;
2523 HANDLE hThread
= NULL
;
2524 const int buflen
= 1024*1024;
2525 char *buffer
= NULL
;
2529 if (tcp_socketpair(&src
, &dst
) != 0)
2531 ok(0, "creating socket pair failed, skipping test\n");
2535 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2536 if (hThread
== NULL
)
2538 ok(0, "CreateThread failed, error %d\n", GetLastError());
2542 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buflen
);
2545 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
2549 ret
= send(src
, buffer
, buflen
, 0);
2551 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
2553 ok(0, "send failed, error %d\n", WSAGetLastError());
2556 if (src
!= INVALID_SOCKET
)
2558 if (dst
!= INVALID_SOCKET
)
2560 if (hThread
!= NULL
)
2561 CloseHandle(hThread
);
2562 HeapFree(GetProcessHeap(), 0, buffer
);
2565 static void test_write_events(void)
2567 SOCKET src
= INVALID_SOCKET
;
2568 SOCKET dst
= INVALID_SOCKET
;
2569 HANDLE hThread
= NULL
;
2570 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
2571 char *buffer
= NULL
;
2572 int bufferSize
= 1024*1024;
2576 WSANETWORKEVENTS netEvents
;
2579 if (tcp_socketpair(&src
, &dst
) != 0)
2581 ok(0, "creating socket pair failed, skipping test\n");
2585 /* On Windows it seems when a non-blocking socket sends to a
2586 blocking socket on the same host, the send() is BLOCKING,
2587 so make both sockets non-blocking */
2588 ret
= ioctlsocket(src
, FIONBIO
, &one
);
2591 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2594 ret
= ioctlsocket(dst
, FIONBIO
, &one
);
2597 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
2601 buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, bufferSize
);
2604 ok(0, "could not allocate memory for test\n");
2608 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
2609 if (hThread
== NULL
)
2611 ok(0, "CreateThread failed, error %d\n", GetLastError());
2615 hEvent
= CreateEventA(NULL
, FALSE
, TRUE
, NULL
);
2616 if (hEvent
== INVALID_HANDLE_VALUE
)
2618 ok(0, "CreateEventA failed, error %d\n", GetLastError());
2622 ret
= WSAEventSelect(src
, hEvent
, FD_WRITE
| FD_CLOSE
);
2625 ok(0, "WSAEventSelect failed, error %d\n", ret
);
2629 /* FD_WRITE should be set initially, and allow us to send at least 1 byte */
2630 dwRet
= WaitForSingleObject(hEvent
, 5000);
2631 if (dwRet
!= WAIT_OBJECT_0
)
2633 ok(0, "Initial WaitForSingleObject failed, error %d\n", dwRet
);
2636 ret
= WSAEnumNetworkEvents(src
, NULL
, &netEvents
);
2639 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2642 if (netEvents
.lNetworkEvents
& FD_WRITE
)
2644 ret
= send(src
, "a", 1, 0);
2645 ok(ret
== 1, "sending 1 byte failed, error %d\n", WSAGetLastError());
2651 ok(0, "FD_WRITE not among initial events\n");
2655 /* Now FD_WRITE should not be set, because the socket send buffer isn't full yet */
2656 dwRet
= WaitForSingleObject(hEvent
, 2000);
2657 if (dwRet
== WAIT_OBJECT_0
)
2659 ok(0, "WaitForSingleObject should have timed out, but succeeded!\n");
2663 /* Now if we send a ton of data and the 'server' does not drain it fast
2664 * enough (set drain_pause to be sure), the socket send buffer will only
2665 * take some of it, and we will get a short write. This will trigger
2666 * another FD_WRITE event as soon as data is sent and more space becomes
2667 * available, but not any earlier. */
2671 ret
= send(src
, buffer
, bufferSize
, 0);
2672 } while (ret
== bufferSize
);
2674 if (ret
>= 0 || WSAGetLastError() == WSAEWOULDBLOCK
)
2676 dwRet
= WaitForSingleObject(hEvent
, 5000);
2677 ok(dwRet
== WAIT_OBJECT_0
, "Waiting failed with %d\n", dwRet
);
2678 if (dwRet
== WAIT_OBJECT_0
)
2680 ret
= WSAEnumNetworkEvents(src
, NULL
, &netEvents
);
2681 ok(ret
== 0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
2684 ok(netEvents
.lNetworkEvents
& FD_WRITE
,
2685 "FD_WRITE event not set as expected, events are 0x%x\n", netEvents
.lNetworkEvents
);
2692 ok(0, "sending a lot of data failed with error %d\n", WSAGetLastError());
2697 HeapFree(GetProcessHeap(), 0, buffer
);
2698 if (src
!= INVALID_SOCKET
)
2700 if (dst
!= INVALID_SOCKET
)
2702 if (hThread
!= NULL
)
2703 CloseHandle(hThread
);
2704 CloseHandle(hEvent
);
2707 static void test_ipv6only(void)
2709 SOCKET v4
= INVALID_SOCKET
,
2710 v6
= INVALID_SOCKET
;
2711 struct sockaddr_in sin4
;
2712 struct sockaddr_in6 sin6
;
2715 memset(&sin4
, 0, sizeof(sin4
));
2716 sin4
.sin_family
= AF_INET
;
2717 sin4
.sin_port
= htons(SERVERPORT
);
2719 memset(&sin6
, 0, sizeof(sin6
));
2720 sin6
.sin6_family
= AF_INET6
;
2721 sin6
.sin6_port
= htons(SERVERPORT
);
2723 v6
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
2724 if (v6
== INVALID_SOCKET
) {
2725 skip("Could not create IPv6 socket (LastError: %d; %d expected if IPv6 not available).\n",
2726 WSAGetLastError(), WSAEAFNOSUPPORT
);
2729 ret
= bind(v6
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
2731 skip("Could not bind IPv6 address (LastError: %d).\n",
2736 v4
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
2737 if (v4
== INVALID_SOCKET
) {
2738 skip("Could not create IPv4 socket (LastError: %d).\n",
2742 ret
= bind(v4
, (struct sockaddr
*)&sin4
, sizeof(sin4
));
2743 ok(!ret
, "Could not bind IPv4 address (LastError: %d; %d expected if IPv6 binds to IPv4 as well).\n",
2744 WSAGetLastError(), WSAEADDRINUSE
);
2747 if (v4
!= INVALID_SOCKET
)
2749 if (v6
!= INVALID_SOCKET
)
2753 static void test_WSASendTo(void)
2756 struct sockaddr_in addr
;
2757 char buf
[12] = "hello world";
2761 addr
.sin_family
= AF_INET
;
2762 addr
.sin_port
= htons(139);
2763 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2764 data_buf
.len
= sizeof(buf
);
2767 if( (s
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
) {
2768 ok(0, "socket() failed error: %d\n", WSAGetLastError());
2772 WSASetLastError(12345);
2773 if(WSASendTo(s
, &data_buf
, 1, &bytesSent
, 0, (struct sockaddr
*)&addr
, sizeof(addr
), NULL
, NULL
)) {
2774 ok(0, "WSASendTo() failed error: %d\n", WSAGetLastError());
2777 ok(!WSAGetLastError(), "WSAGetLastError() should return zero after "
2778 "a successful call to WSASendTo()\n");
2781 static void test_WSARecv(void)
2787 DWORD bytesReturned
;
2794 tcp_socketpair(&src
, &dest
);
2795 if (src
== INVALID_SOCKET
|| dest
== INVALID_SOCKET
)
2797 skip("failed to create sockets\n");
2801 bufs
.len
= sizeof(buf
);
2805 memset(&ov
, 0, sizeof(ov
));
2806 ov
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2807 ok(ov
.hEvent
!= NULL
, "could not create event object, errno = %d\n", GetLastError());
2813 ok(!setsockopt (src
, SOL_SOCKET
, SO_LINGER
, (char *) &ling
, sizeof(ling
)), "Failed to set linger %d\n", GetLastError());
2815 iret
= WSARecv(dest
, &bufs
, 1, &bytesReturned
, &flags
, &ov
, NULL
);
2816 ok(iret
== SOCKET_ERROR
&& GetLastError() == ERROR_IO_PENDING
, "WSARecv failed - %d error %d\n", iret
, GetLastError());
2819 src
= INVALID_SOCKET
;
2821 dwret
= WaitForSingleObject(ov
.hEvent
, 1000);
2822 ok(dwret
== WAIT_OBJECT_0
, "Waiting for disconnect event failed with %d + errno %d\n", dwret
, GetLastError());
2824 bret
= GetOverlappedResult((HANDLE
)dest
, &ov
, &bytesReturned
, FALSE
);
2825 todo_wine
ok(!bret
&& GetLastError() == ERROR_NETNAME_DELETED
, "Did not get disconnect event: %d, error %d\n", bret
, GetLastError());
2826 ok(bytesReturned
== 0, "Bytes received is %d\n", bytesReturned
);
2829 if (dest
!= INVALID_SOCKET
)
2831 if (src
!= INVALID_SOCKET
)
2834 WSACloseEvent(ov
.hEvent
);
2837 static void test_GetAddrInfoW(void)
2839 static const WCHAR port
[] = {'8','0',0};
2840 static const WCHAR empty
[] = {0};
2841 static const WCHAR localhost
[] = {'l','o','c','a','l','h','o','s','t',0};
2844 ADDRINFOW
*result
, hint
;
2846 if (!pGetAddrInfoW
|| !pFreeAddrInfoW
)
2848 win_skip("GetAddrInfoW and/or FreeAddrInfoW not present\n");
2852 memset(&hint
, 0, sizeof(ADDRINFOW
));
2854 ret
= pGetAddrInfoW(NULL
, NULL
, NULL
, &result
);
2855 ok(ret
== WSAHOST_NOT_FOUND
, "got %d expected WSAHOST_NOT_FOUND\n", ret
);
2858 ret
= pGetAddrInfoW(empty
, NULL
, NULL
, &result
);
2861 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2862 ok(result
!= NULL
, "GetAddrInfoW failed\n");
2864 pFreeAddrInfoW(result
);
2866 ret
= pGetAddrInfoW(localhost
, NULL
, NULL
, &result
);
2867 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2868 pFreeAddrInfoW(result
);
2870 ret
= pGetAddrInfoW(localhost
, port
, NULL
, &result
);
2871 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2872 pFreeAddrInfoW(result
);
2874 ret
= pGetAddrInfoW(localhost
, port
, &hint
, &result
);
2875 ok(!ret
, "GetAddrInfoW failed with %d\n", WSAGetLastError());
2876 pFreeAddrInfoW(result
);
2879 static void test_AcceptEx(void)
2881 SOCKET listener
= INVALID_SOCKET
;
2882 SOCKET acceptor
= INVALID_SOCKET
;
2883 SOCKET connector
= INVALID_SOCKET
;
2884 SOCKET connector2
= INVALID_SOCKET
;
2885 struct sockaddr_in bindAddress
;
2887 GUID acceptExGuid
= WSAID_ACCEPTEX
;
2888 LPFN_ACCEPTEX pAcceptEx
= NULL
;
2889 fd_set fds_accept
, fds_send
;
2890 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
2892 DWORD bytesReturned
;
2894 OVERLAPPED overlapped
;
2899 memset(&overlapped
, 0, sizeof(overlapped
));
2901 listener
= socket(AF_INET
, SOCK_STREAM
, 0);
2902 if (listener
== INVALID_SOCKET
) {
2903 skip("could not create listener socket, error %d\n", WSAGetLastError());
2907 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
2908 if (acceptor
== INVALID_SOCKET
) {
2909 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
2913 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
2914 if (connector
== INVALID_SOCKET
) {
2915 skip("could not create connector socket, error %d\n", WSAGetLastError());
2919 memset(&bindAddress
, 0, sizeof(bindAddress
));
2920 bindAddress
.sin_family
= AF_INET
;
2921 bindAddress
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
2922 iret
= bind(listener
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
2924 skip("failed to bind, error %d\n", WSAGetLastError());
2928 socklen
= sizeof(bindAddress
);
2929 iret
= getsockname(listener
, (struct sockaddr
*)&bindAddress
, &socklen
);
2931 skip("failed to lookup bind address, error %d\n", WSAGetLastError());
2935 if (set_blocking(listener
, FALSE
)) {
2936 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
2940 iret
= WSAIoctl(listener
, SIO_GET_EXTENSION_FUNCTION_POINTER
, &acceptExGuid
, sizeof(acceptExGuid
),
2941 &pAcceptEx
, sizeof(pAcceptEx
), &bytesReturned
, NULL
, NULL
);
2943 skip("WSAIoctl failed to get AcceptEx with ret %d + errno %d\n", iret
, WSAGetLastError());
2947 bret
= pAcceptEx(INVALID_SOCKET
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2948 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2949 &bytesReturned
, &overlapped
);
2950 ok(bret
== FALSE
&& WSAGetLastError() == WSAENOTSOCK
, "AcceptEx on invalid listening socket "
2951 "returned %d + errno %d\n", bret
, WSAGetLastError());
2953 bret
= pAcceptEx(listener
, INVALID_SOCKET
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2954 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2955 &bytesReturned
, &overlapped
);
2956 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on invalid accepting socket "
2957 "returned %d + errno %d\n", bret
, WSAGetLastError());
2959 bret
= pAcceptEx(listener
, acceptor
, NULL
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2960 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2961 &bytesReturned
, &overlapped
);
2963 (WSAGetLastError() == WSAEINVAL
||
2964 broken(WSAGetLastError() == WSAEFAULT
)), /* NT4 */
2965 "AcceptEx on NULL buffer returned %d + errno %d\n", bret
, WSAGetLastError());
2967 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, 0, sizeof(struct sockaddr_in
) + 16,
2968 &bytesReturned
, &overlapped
);
2969 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small local address size "
2970 "returned %d + errno %d\n", bret
, WSAGetLastError());
2972 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0, sizeof(struct sockaddr_in
) + 16, 0,
2973 &bytesReturned
, &overlapped
);
2974 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on too small remote address size "
2975 "returned %d + errno %d\n", bret
, WSAGetLastError());
2977 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
2978 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2979 &bytesReturned
, NULL
);
2980 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_INVALID_PARAMETER
, "AcceptEx on a NULL overlapped "
2981 "returned %d + errno %d\n", bret
, WSAGetLastError());
2983 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
2984 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
2985 &bytesReturned
, &overlapped
);
2986 ok(bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
, "AcceptEx on a non-listening socket "
2987 "returned %d + errno %d\n", bret
, WSAGetLastError());
2989 iret
= listen(listener
, 5);
2991 skip("listening failed, errno = %d\n", WSAGetLastError());
2995 overlapped
.hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2996 if (overlapped
.hEvent
== NULL
) {
2997 skip("could not create event object, errno = %d\n", GetLastError());
3001 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3002 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3003 &bytesReturned
, &overlapped
);
3004 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3006 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3007 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3008 &bytesReturned
, &overlapped
);
3009 ok((bret
== FALSE
&& WSAGetLastError() == WSAEINVAL
) || broken(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) /* NT4 */,
3010 "AcceptEx on already pending socket returned %d + errno %d\n", bret
, WSAGetLastError());
3011 if (bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
) {
3012 /* We need to cancel this call, otherwise things fail */
3013 bret
= CancelIo((HANDLE
) listener
);
3014 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3017 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3018 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3019 &bytesReturned
, &overlapped
);
3020 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3023 iret
= connect(acceptor
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3024 ok((iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINVAL
) || broken(!iret
) /* NT4 */,
3025 "connecting to acceptex acceptor succeeded? return %d + errno %d\n", iret
, WSAGetLastError());
3026 if (!iret
|| (iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
)) {
3027 /* We need to cancel this call, otherwise things fail */
3028 closesocket(acceptor
);
3029 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3030 if (acceptor
== INVALID_SOCKET
) {
3031 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3035 bret
= CancelIo((HANDLE
) listener
);
3036 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3039 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3040 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3041 &bytesReturned
, &overlapped
);
3042 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3045 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3046 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3048 dwret
= WaitForSingleObject(overlapped
.hEvent
, INFINITE
);
3049 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3051 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3052 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3053 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3055 closesocket(connector
);
3056 connector
= INVALID_SOCKET
;
3057 closesocket(acceptor
);
3058 acceptor
= INVALID_SOCKET
;
3060 /* Test short reads */
3062 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3063 if (acceptor
== INVALID_SOCKET
) {
3064 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3067 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3068 if (connector
== INVALID_SOCKET
) {
3069 skip("could not create connector socket, error %d\n", WSAGetLastError());
3072 bret
= pAcceptEx(listener
, acceptor
, buffer
, 2,
3073 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3074 &bytesReturned
, &overlapped
);
3075 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3077 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3078 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3080 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3081 ok(dwret
== WAIT_TIMEOUT
, "Waiting for accept event timeout failed with %d + errno %d\n", dwret
, GetLastError());
3083 iret
= send(connector
, buffer
, 1, 0);
3084 ok(iret
== 1, "could not send 1 byte: send %d errno %d\n", iret
, WSAGetLastError());
3086 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3087 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3089 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3090 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3091 ok(bytesReturned
== 1, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3093 closesocket(connector
);
3094 connector
= INVALID_SOCKET
;
3095 closesocket(acceptor
);
3096 acceptor
= INVALID_SOCKET
;
3098 /* Test CF_DEFER & AcceptEx interaction */
3100 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3101 if (acceptor
== INVALID_SOCKET
) {
3102 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3105 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3106 if (connector
== INVALID_SOCKET
) {
3107 skip("could not create connector socket, error %d\n", WSAGetLastError());
3110 connector2
= socket(AF_INET
, SOCK_STREAM
, 0);
3111 if (connector
== INVALID_SOCKET
) {
3112 skip("could not create connector socket, error %d\n", WSAGetLastError());
3116 if (set_blocking(connector
, FALSE
)) {
3117 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3121 if (set_blocking(connector2
, FALSE
)) {
3122 skip("couldn't make socket non-blocking, error %d\n", WSAGetLastError());
3126 /* Connect socket #1 */
3127 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3128 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3130 FD_ZERO ( &fds_accept
);
3131 FD_ZERO ( &fds_send
);
3133 FD_SET ( listener
, &fds_accept
);
3134 FD_SET ( connector
, &fds_send
);
3140 for (i
= 0; i
< 4000; ++i
)
3142 fd_set fds_openaccept
= fds_accept
, fds_opensend
= fds_send
;
3144 wsa_ok ( ( select ( 0, &fds_openaccept
, &fds_opensend
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
3145 "acceptex test(%d): could not select on socket, errno %d\n" );
3147 /* check for incoming requests */
3148 if ( FD_ISSET ( listener
, &fds_openaccept
) ) {
3151 SOCKET tmp
= WSAAccept(listener
, NULL
, NULL
, (LPCONDITIONPROC
) AlwaysDeferConditionFunc
, 0);
3152 ok(tmp
== INVALID_SOCKET
&& WSAGetLastError() == WSATRY_AGAIN
, "Failed to defer connection, %d\n", WSAGetLastError());
3153 bret
= pAcceptEx(listener
, acceptor
, buffer
, 0,
3154 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3155 &bytesReturned
, &overlapped
);
3156 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3158 else if (got
== 2) {
3159 /* this should be socket #2 */
3160 SOCKET tmp
= accept(listener
, NULL
, NULL
);
3161 ok(tmp
!= INVALID_SOCKET
, "accept failed %d\n", WSAGetLastError());
3165 ok(FALSE
, "Got more than 2 connections?\n");
3168 if ( conn1
&& FD_ISSET ( connector2
, &fds_opensend
) ) {
3169 /* Send data on second socket, and stop */
3170 send(connector2
, "2", 1, 0);
3171 FD_CLR ( connector2
, &fds_send
);
3175 if ( FD_ISSET ( connector
, &fds_opensend
) ) {
3176 /* Once #1 is connected, allow #2 to connect */
3179 send(connector
, "1", 1, 0);
3180 FD_CLR ( connector
, &fds_send
);
3182 iret
= connect(connector2
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3183 ok(iret
== SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK
, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3184 FD_SET ( connector2
, &fds_send
);
3188 ok (got
== 2 || broken(got
== 1) /* NT4 */,
3189 "Did not get both connections, got %d\n", got
);
3191 dwret
= WaitForSingleObject(overlapped
.hEvent
, 0);
3192 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3194 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3195 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3196 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3198 set_blocking(acceptor
, TRUE
);
3199 iret
= recv( acceptor
, buffer
, 2, 0);
3200 ok(iret
== 1, "Failed to get data, %d, errno: %d\n", iret
, WSAGetLastError());
3202 ok(buffer
[0] == '1', "The wrong first client was accepted by acceptex: %c != 1\n", buffer
[0]);
3204 closesocket(connector
);
3205 connector
= INVALID_SOCKET
;
3206 closesocket(acceptor
);
3207 acceptor
= INVALID_SOCKET
;
3209 /* clean up in case of failures */
3210 while ((acceptor
= accept(listener
, NULL
, NULL
)) != INVALID_SOCKET
)
3211 closesocket(acceptor
);
3213 /* Disconnect during receive? */
3215 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3216 if (acceptor
== INVALID_SOCKET
) {
3217 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3220 connector
= socket(AF_INET
, SOCK_STREAM
, 0);
3221 if (connector
== INVALID_SOCKET
) {
3222 skip("could not create connector socket, error %d\n", WSAGetLastError());
3225 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3226 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3227 &bytesReturned
, &overlapped
);
3228 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3230 iret
= connect(connector
, (struct sockaddr
*)&bindAddress
, sizeof(bindAddress
));
3231 ok(iret
== 0, "connecting to accepting socket failed, error %d\n", WSAGetLastError());
3233 closesocket(connector
);
3234 connector
= INVALID_SOCKET
;
3236 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3237 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3239 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3240 ok(bret
, "GetOverlappedResult failed, error %d\n", GetLastError());
3241 ok(bytesReturned
== 0, "bytesReturned isn't supposed to be %d\n", bytesReturned
);
3243 closesocket(acceptor
);
3244 acceptor
= INVALID_SOCKET
;
3246 /* Test closing with pending requests */
3248 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3249 if (acceptor
== INVALID_SOCKET
) {
3250 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3253 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3254 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3255 &bytesReturned
, &overlapped
);
3256 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3258 closesocket(acceptor
);
3259 acceptor
= INVALID_SOCKET
;
3261 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3262 ok(dwret
== WAIT_OBJECT_0
|| broken(dwret
== WAIT_TIMEOUT
) /* NT4/2000 */,
3263 "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3265 if (dwret
!= WAIT_TIMEOUT
) {
3266 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3267 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
3270 bret
= CancelIo((HANDLE
) listener
);
3271 ok(bret
, "Failed to cancel failed test. Bailing...\n");
3275 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3276 if (acceptor
== INVALID_SOCKET
) {
3277 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3280 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3281 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3282 &bytesReturned
, &overlapped
);
3283 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3285 CancelIo((HANDLE
) acceptor
);
3287 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3288 ok(dwret
== WAIT_TIMEOUT
, "Waiting for timeout failed with %d + errno %d\n", dwret
, GetLastError());
3290 closesocket(acceptor
);
3291 acceptor
= INVALID_SOCKET
;
3293 acceptor
= socket(AF_INET
, SOCK_STREAM
, 0);
3294 if (acceptor
== INVALID_SOCKET
) {
3295 skip("could not create acceptor socket, error %d\n", WSAGetLastError());
3298 bret
= pAcceptEx(listener
, acceptor
, buffer
, sizeof(buffer
) - 2*(sizeof(struct sockaddr_in
) + 16),
3299 sizeof(struct sockaddr_in
) + 16, sizeof(struct sockaddr_in
) + 16,
3300 &bytesReturned
, &overlapped
);
3301 ok(bret
== FALSE
&& WSAGetLastError() == ERROR_IO_PENDING
, "AcceptEx returned %d + errno %d\n", bret
, WSAGetLastError());
3303 closesocket(listener
);
3304 listener
= INVALID_SOCKET
;
3306 dwret
= WaitForSingleObject(overlapped
.hEvent
, 1000);
3307 ok(dwret
== WAIT_OBJECT_0
, "Waiting for accept event failed with %d + errno %d\n", dwret
, GetLastError());
3309 bret
= GetOverlappedResult((HANDLE
)listener
, &overlapped
, &bytesReturned
, FALSE
);
3310 ok(!bret
&& GetLastError() == ERROR_OPERATION_ABORTED
, "GetOverlappedResult failed, error %d\n", GetLastError());
3313 if (overlapped
.hEvent
)
3314 WSACloseEvent(overlapped
.hEvent
);
3315 if (listener
!= INVALID_SOCKET
)
3316 closesocket(listener
);
3317 if (acceptor
!= INVALID_SOCKET
)
3318 closesocket(acceptor
);
3319 if (connector
!= INVALID_SOCKET
)
3320 closesocket(connector
);
3321 if (connector2
!= INVALID_SOCKET
)
3322 closesocket(connector2
);
3325 static void test_getpeername(void)
3328 struct sockaddr_in sa
, sa_out
;
3330 const char buf
[] = "hello world";
3333 /* Test the parameter validation order. */
3334 ret
= getpeername(INVALID_SOCKET
, NULL
, NULL
);
3335 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3336 ok(WSAGetLastError() == WSAENOTSOCK
,
3337 "Expected WSAGetLastError() to return WSAENOTSOCK, got %d\n", WSAGetLastError());
3339 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
3340 ok(sock
!= INVALID_SOCKET
, "Expected socket to return a valid socket\n");
3341 if (sock
== INVALID_SOCKET
)
3343 skip("Socket creation failed with %d\n", WSAGetLastError());
3347 ret
= getpeername(sock
, NULL
, NULL
);
3348 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3349 ok(WSAGetLastError() == WSAENOTCONN
||
3350 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
3351 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
3353 memset(&sa
, 0, sizeof(sa
));
3354 sa
.sin_family
= AF_INET
;
3355 sa
.sin_port
= htons(139);
3356 sa
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3358 /* sendto does not change a socket's connection state. */
3359 ret
= sendto(sock
, buf
, sizeof(buf
), 0, (struct sockaddr
*)&sa
, sizeof(sa
));
3360 ok(ret
!= SOCKET_ERROR
,
3361 "Expected sendto to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
3363 ret
= getpeername(sock
, NULL
, NULL
);
3364 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3365 ok(WSAGetLastError() == WSAENOTCONN
||
3366 broken(WSAGetLastError() == WSAEFAULT
), /* Win9x and WinMe */
3367 "Expected WSAGetLastError() to return WSAENOTCONN, got %d\n", WSAGetLastError());
3369 ret
= connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
));
3371 "Expected connect to succeed, WSAGetLastError() = %d\n", WSAGetLastError());
3373 ret
= getpeername(sock
, NULL
, NULL
);
3374 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3375 ok(WSAGetLastError() == WSAEFAULT
,
3376 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
3378 /* Test crashes on Wine. */
3381 ret
= getpeername(sock
, (void*)0xdeadbeef, (void*)0xcafebabe);
3382 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return SOCKET_ERROR, got %d\n", ret
);
3383 ok(WSAGetLastError() == WSAEFAULT
,
3384 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
3388 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
3389 ok(ret
== SOCKET_ERROR
, "Expected getpeername to return 0, got %d\n", ret
);
3390 ok(WSAGetLastError() == WSAEFAULT
,
3391 "Expected WSAGetLastError() to return WSAEFAULT, got %d\n", WSAGetLastError());
3393 sa_len
= sizeof(sa_out
);
3394 ret
= getpeername(sock
, (struct sockaddr
*)&sa_out
, &sa_len
);
3395 ok(ret
== 0, "Expected getpeername to return 0, got %d\n", ret
);
3396 ok(!memcmp(&sa
, &sa_out
, sizeof(sa
)),
3397 "Expected the returned structure to be identical to the connect structure\n");
3402 /**************** Main program ***************/
3408 /* Leave these tests at the beginning. They depend on WSAStartup not having been
3409 * called, which is done by Init() below. */
3410 test_WithoutWSAStartup();
3411 test_WithWSAStartup();
3415 test_set_getsockopt();
3416 test_so_reuseaddr();
3417 test_extendedSocketOptions();
3419 for (i
= 0; i
< NUM_TESTS
; i
++)
3421 trace ( " **** STARTING TEST %d ****\n", i
);
3422 do_test ( &tests
[i
] );
3423 trace ( " **** TEST %d COMPLETE ****\n", i
);
3428 test_getservbyname();
3431 test_WSAAddressToStringA();
3432 test_WSAAddressToStringW();
3434 test_WSAStringToAddressA();
3435 test_WSAStringToAddressW();
3442 test_addr_to_print();
3445 test_gethostbyname_hack();
3448 test_write_events();
3454 test_GetAddrInfoW();