2 * Unit test suite for winsock functions
4 * Copyright 2002 Martin Wilck
5 * Copyright 2005 Thomas Kho
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
32 #define MAX_CLIENTS 4 /* Max number of clients */
33 #define NUM_TESTS 3 /* Number of tests performed */
34 #define FIRST_CHAR 'A' /* First character in transferred pattern */
35 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
36 #define BIND_TRIES 6 /* Number of bind() attempts */
37 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
38 after server initialization, if something hangs */
40 #define NUM_UDP_PEERS 3 /* Number of UDP sockets to create and test > 1 */
42 #define NUM_THREADS 3 /* Number of threads to run getservbyname */
43 #define NUM_QUERIES 250 /* Number of getservbyname queries per thread */
45 #define SERVERIP "127.0.0.1" /* IP to bind to */
46 #define SERVERPORT 9374 /* Port number to bind to */
48 #define wsa_ok(op, cond, msg) \
52 if ( !(cond tmp) ) err = WSAGetLastError(); \
53 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
57 /**************** Structs and typedefs ***************/
59 typedef struct thread_info
65 /* Information in the server about open client connections */
66 typedef struct sock_info
69 struct sockaddr_in addr
;
70 struct sockaddr_in peer
;
76 /* Test parameters for both server & client */
77 typedef struct test_params
81 const char *inet_addr
;
88 /* server-specific test parameters */
89 typedef struct server_params
96 /* client-specific test parameters */
97 typedef struct client_params
104 /* This type combines all information for setting up a test scenario */
105 typedef struct test_setup
109 server_params srv_params
;
111 client_params clt_params
;
114 /* Thread local storage for server */
115 typedef struct server_memory
118 struct sockaddr_in addr
;
119 sock_info sock
[MAX_CLIENTS
];
122 /* Thread local storage for client */
123 typedef struct client_memory
126 struct sockaddr_in addr
;
131 /* SelectReadThread thread parameters */
132 typedef struct select_thread_params
136 } select_thread_params
;
138 /**************** Static variables ***************/
140 static DWORD tls
; /* Thread local storage index */
141 static HANDLE thread
[1+MAX_CLIENTS
];
142 static DWORD thread_id
[1+MAX_CLIENTS
];
143 static HANDLE server_ready
;
144 static HANDLE client_ready
[MAX_CLIENTS
];
145 static int client_id
;
147 /**************** General utility functions ***************/
149 static int tcp_socketpair(SOCKET
*src
, SOCKET
*dst
)
151 SOCKET server
= INVALID_SOCKET
;
152 struct sockaddr_in addr
;
156 *src
= INVALID_SOCKET
;
157 *dst
= INVALID_SOCKET
;
159 *src
= socket(AF_INET
, SOCK_STREAM
, 0);
160 if (*src
== INVALID_SOCKET
)
163 server
= socket(AF_INET
, SOCK_STREAM
, 0);
164 if (server
== INVALID_SOCKET
)
167 memset(&addr
, 0, sizeof(addr
));
168 addr
.sin_family
= AF_INET
;
169 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
170 ret
= bind(server
, (struct sockaddr
*)&addr
, sizeof(addr
));
175 ret
= getsockname(server
, (struct sockaddr
*)&addr
, &len
);
179 ret
= listen(server
, 1);
183 ret
= connect(*src
, (struct sockaddr
*)&addr
, sizeof(addr
));
188 *dst
= accept(server
, (struct sockaddr
*)&addr
, &len
);
191 if (server
!= INVALID_SOCKET
)
193 if (*src
!= INVALID_SOCKET
&& *dst
!= INVALID_SOCKET
)
200 static void set_so_opentype ( BOOL overlapped
)
202 int optval
= !overlapped
, newval
, len
= sizeof (int);
204 ok ( setsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
205 (LPVOID
) &optval
, sizeof (optval
) ) == 0,
206 "setting SO_OPENTYPE failed\n" );
207 ok ( getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
,
208 (LPVOID
) &newval
, &len
) == 0,
209 "getting SO_OPENTYPE failed\n" );
210 ok ( optval
== newval
, "failed to set SO_OPENTYPE\n" );
213 static int set_blocking ( SOCKET s
, BOOL blocking
)
215 u_long val
= !blocking
;
216 return ioctlsocket ( s
, FIONBIO
, &val
);
219 static void fill_buffer ( char *buf
, int chunk_size
, int n_chunks
)
222 for ( c
= FIRST_CHAR
, p
= buf
; c
< FIRST_CHAR
+ n_chunks
; c
++, p
+= chunk_size
)
223 memset ( p
, c
, chunk_size
);
226 static char* test_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
)
232 for ( i
= 0; i
< chunk_size
; i
++ )
233 if ( p
[i
] != c
) return p
+ i
;
239 * This routine is called when a client / server does not expect any more data,
240 * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
242 static void read_zero_bytes ( SOCKET s
)
246 while ( ( tmp
= recv ( s
, buf
, 256, 0 ) ) > 0 )
248 ok ( n
<= 0, "garbage data received: %d bytes\n", n
);
251 static int do_synchronous_send ( SOCKET s
, char *buf
, int buflen
, int sendlen
)
253 char* last
= buf
+ buflen
, *p
;
255 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
256 n
= send ( s
, p
, min ( sendlen
, last
- p
), 0 );
257 wsa_ok ( n
, 0 <=, "do_synchronous_send (%x): error %d\n" );
261 static int do_synchronous_recv ( SOCKET s
, char *buf
, int buflen
, int recvlen
)
263 char* last
= buf
+ buflen
, *p
;
265 for ( p
= buf
; n
> 0 && p
< last
; p
+= n
)
266 n
= recv ( s
, p
, min ( recvlen
, last
- p
), 0 );
267 wsa_ok ( n
, 0 <=, "do_synchronous_recv (%x): error %d:\n" );
272 * Call this routine right after thread startup.
273 * SO_OPENTYPE must by 0, regardless what the server did.
275 static void check_so_opentype (void)
279 getsockopt ( INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (LPVOID
) &tmp
, &len
);
280 ok ( tmp
== 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp
);
283 /**************** Server utility functions ***************/
286 * Even if we have closed our server socket cleanly,
287 * the OS may mark the address "in use" for some time -
288 * this happens with native Linux apps, too.
290 static void do_bind ( SOCKET s
, struct sockaddr
* addr
, int addrlen
)
292 int err
, wsaerr
= 0, n_try
= BIND_TRIES
;
294 while ( ( err
= bind ( s
, addr
, addrlen
) ) != 0 &&
295 ( wsaerr
= WSAGetLastError () ) == WSAEADDRINUSE
&&
298 trace ( "address in use, waiting ...\n" );
299 Sleep ( 1000 * BIND_SLEEP
);
301 ok ( err
== 0, "failed to bind: %d\n", wsaerr
);
304 static void server_start ( server_params
*par
)
307 test_params
*gen
= par
->general
;
308 server_memory
*mem
= (LPVOID
) LocalAlloc ( LPTR
, sizeof (server_memory
));
310 TlsSetValue ( tls
, mem
);
311 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
312 NULL
, 0, par
->sock_flags
);
313 ok ( mem
->s
!= INVALID_SOCKET
, "Server: WSASocket failed\n" );
315 mem
->addr
.sin_family
= AF_INET
;
316 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
317 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
319 for (i
= 0; i
< MAX_CLIENTS
; i
++)
321 mem
->sock
[i
].s
= INVALID_SOCKET
;
322 mem
->sock
[i
].buf
= (LPVOID
) LocalAlloc ( LPTR
, gen
->n_chunks
* gen
->chunk_size
);
323 mem
->sock
[i
].n_recvd
= 0;
324 mem
->sock
[i
].n_sent
= 0;
327 if ( gen
->sock_type
== SOCK_STREAM
)
328 do_bind ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof (mem
->addr
) );
331 static void server_stop (void)
334 server_memory
*mem
= TlsGetValue ( tls
);
336 for (i
= 0; i
< MAX_CLIENTS
; i
++ )
338 LocalFree ( (HANDLE
) mem
->sock
[i
].buf
);
339 if ( mem
->sock
[i
].s
!= INVALID_SOCKET
)
340 closesocket ( mem
->sock
[i
].s
);
342 ok ( closesocket ( mem
->s
) == 0, "closesocket failed\n" );
343 LocalFree ( (HANDLE
) mem
);
344 ExitThread ( GetCurrentThreadId () );
347 /**************** Client utilitiy functions ***************/
349 static void client_start ( client_params
*par
)
351 test_params
*gen
= par
->general
;
352 client_memory
*mem
= (LPVOID
) LocalAlloc (LPTR
, sizeof (client_memory
));
354 TlsSetValue ( tls
, mem
);
356 WaitForSingleObject ( server_ready
, INFINITE
);
358 mem
->s
= WSASocketA ( AF_INET
, gen
->sock_type
, gen
->sock_prot
,
359 NULL
, 0, par
->sock_flags
);
361 mem
->addr
.sin_family
= AF_INET
;
362 mem
->addr
.sin_addr
.s_addr
= inet_addr ( gen
->inet_addr
);
363 mem
->addr
.sin_port
= htons ( gen
->inet_port
);
365 ok ( mem
->s
!= INVALID_SOCKET
, "Client: WSASocket failed\n" );
367 mem
->send_buf
= (LPVOID
) LocalAlloc ( LPTR
, 2 * gen
->n_chunks
* gen
->chunk_size
);
368 mem
->recv_buf
= mem
->send_buf
+ gen
->n_chunks
* gen
->chunk_size
;
369 fill_buffer ( mem
->send_buf
, gen
->chunk_size
, gen
->n_chunks
);
371 SetEvent ( client_ready
[client_id
] );
372 /* Wait for the other clients to come up */
373 WaitForMultipleObjects ( min ( gen
->n_clients
, MAX_CLIENTS
), client_ready
, TRUE
, INFINITE
);
376 static void client_stop (void)
378 client_memory
*mem
= TlsGetValue ( tls
);
379 wsa_ok ( closesocket ( mem
->s
), 0 ==, "closesocket error (%x): %d\n" );
380 LocalFree ( (HANDLE
) mem
->send_buf
);
381 LocalFree ( (HANDLE
) mem
);
385 /**************** Servers ***************/
388 * simple_server: A very basic server doing synchronous IO.
390 static VOID WINAPI
simple_server ( server_params
*par
)
392 test_params
*gen
= par
->general
;
394 int n_recvd
, n_sent
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
395 id
= GetCurrentThreadId();
398 trace ( "simple_server (%x) starting\n", id
);
400 set_so_opentype ( FALSE
); /* non-overlapped */
401 server_start ( par
);
402 mem
= TlsGetValue ( tls
);
404 wsa_ok ( set_blocking ( mem
->s
, TRUE
), 0 ==, "simple_server (%x): failed to set blocking mode: %d\n");
405 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "simple_server (%x): listen failed: %d\n");
407 trace ( "simple_server (%x) ready\n", id
);
408 SetEvent ( server_ready
); /* notify clients */
410 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
412 trace ( "simple_server (%x): waiting for client\n", id
);
414 /* accept a single connection */
415 tmp
= sizeof ( mem
->sock
[0].peer
);
416 mem
->sock
[0].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[0].peer
, &tmp
);
417 wsa_ok ( mem
->sock
[0].s
, INVALID_SOCKET
!=, "simple_server (%x): accept failed: %d\n" );
419 ok ( mem
->sock
[0].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
420 "simple_server (%x): strange peer address\n", id
);
422 /* Receive data & check it */
423 n_recvd
= do_synchronous_recv ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
424 ok ( n_recvd
== n_expected
,
425 "simple_server (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
426 p
= test_buffer ( mem
->sock
[0].buf
, gen
->chunk_size
, gen
->n_chunks
);
427 ok ( p
== NULL
, "simple_server (%x): test pattern error: %d\n", id
, p
- mem
->sock
[0].buf
);
430 n_sent
= do_synchronous_send ( mem
->sock
[0].s
, mem
->sock
[0].buf
, n_expected
, par
->buflen
);
431 ok ( n_sent
== n_expected
,
432 "simple_server (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
435 read_zero_bytes ( mem
->sock
[0].s
);
436 wsa_ok ( closesocket ( mem
->sock
[0].s
), 0 ==, "simple_server (%x): closesocket error: %d\n" );
437 mem
->sock
[0].s
= INVALID_SOCKET
;
440 trace ( "simple_server (%x) exiting\n", id
);
445 * select_server: A non-blocking server.
447 static VOID WINAPI
select_server ( server_params
*par
)
449 test_params
*gen
= par
->general
;
451 int n_expected
= gen
->n_chunks
* gen
->chunk_size
, tmp
, i
,
452 id
= GetCurrentThreadId(), n_connections
= 0, n_sent
, n_recvd
,
453 n_set
, delta
, n_ready
;
455 struct timeval timeout
= {0,10}; /* wait for 10 milliseconds */
456 fd_set fds_recv
, fds_send
, fds_openrecv
, fds_opensend
;
458 trace ( "select_server (%x) starting\n", id
);
460 set_so_opentype ( FALSE
); /* non-overlapped */
461 server_start ( par
);
462 mem
= TlsGetValue ( tls
);
464 wsa_ok ( set_blocking ( mem
->s
, FALSE
), 0 ==, "select_server (%x): failed to set blocking mode: %d\n");
465 wsa_ok ( listen ( mem
->s
, SOMAXCONN
), 0 ==, "select_server (%x): listen failed: %d\n");
467 trace ( "select_server (%x) ready\n", id
);
468 SetEvent ( server_ready
); /* notify clients */
470 FD_ZERO ( &fds_openrecv
);
471 FD_ZERO ( &fds_recv
);
472 FD_ZERO ( &fds_send
);
473 FD_ZERO ( &fds_opensend
);
475 FD_SET ( mem
->s
, &fds_openrecv
);
479 fds_recv
= fds_openrecv
;
480 fds_send
= fds_opensend
;
484 wsa_ok ( ( n_ready
= select ( 0, &fds_recv
, &fds_send
, NULL
, &timeout
) ), SOCKET_ERROR
!=,
485 "select_server (%x): select() failed: %d\n" );
487 /* check for incoming requests */
488 if ( FD_ISSET ( mem
->s
, &fds_recv
) ) {
491 trace ( "select_server (%x): accepting client connection\n", id
);
493 /* accept a single connection */
494 tmp
= sizeof ( mem
->sock
[n_connections
].peer
);
495 mem
->sock
[n_connections
].s
= accept ( mem
->s
, (struct sockaddr
*) &mem
->sock
[n_connections
].peer
, &tmp
);
496 wsa_ok ( mem
->sock
[n_connections
].s
, INVALID_SOCKET
!=, "select_server (%x): accept() failed: %d\n" );
498 ok ( mem
->sock
[n_connections
].peer
.sin_addr
.s_addr
== inet_addr ( gen
->inet_addr
),
499 "select_server (%x): strange peer address\n", id
);
501 /* add to list of open connections */
502 FD_SET ( mem
->sock
[n_connections
].s
, &fds_openrecv
);
503 FD_SET ( mem
->sock
[n_connections
].s
, &fds_opensend
);
508 /* handle open requests */
510 for ( i
= 0; i
< n_connections
; i
++ )
512 if ( FD_ISSET( mem
->sock
[i
].s
, &fds_recv
) ) {
515 if ( mem
->sock
[i
].n_recvd
< n_expected
) {
516 /* Receive data & check it */
517 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 );
518 ok ( n_recvd
!= SOCKET_ERROR
, "select_server (%x): error in recv(): %d\n", id
, WSAGetLastError() );
519 mem
->sock
[i
].n_recvd
+= n_recvd
;
521 if ( mem
->sock
[i
].n_recvd
== n_expected
) {
522 p
= test_buffer ( mem
->sock
[i
].buf
, gen
->chunk_size
, gen
->n_chunks
);
523 ok ( p
== NULL
, "select_server (%x): test pattern error: %d\n", id
, p
- mem
->sock
[i
].buf
);
524 FD_CLR ( mem
->sock
[i
].s
, &fds_openrecv
);
527 ok ( mem
->sock
[i
].n_recvd
<= n_expected
, "select_server (%x): received too many bytes: %d\n", id
, mem
->sock
[i
].n_recvd
);
531 /* only echo back what we've received */
532 delta
= mem
->sock
[i
].n_recvd
- mem
->sock
[i
].n_sent
;
534 if ( FD_ISSET ( mem
->sock
[i
].s
, &fds_send
) ) {
537 if ( ( delta
> 0 ) && ( mem
->sock
[i
].n_sent
< n_expected
) ) {
539 n_sent
= send ( mem
->sock
[i
].s
, mem
->sock
[i
].buf
+ mem
->sock
[i
].n_sent
, min ( delta
, par
->buflen
), 0 );
540 ok ( n_sent
!= SOCKET_ERROR
, "select_server (%x): error in send(): %d\n", id
, WSAGetLastError() );
541 mem
->sock
[i
].n_sent
+= n_sent
;
543 if ( mem
->sock
[i
].n_sent
== n_expected
) {
544 FD_CLR ( mem
->sock
[i
].s
, &fds_opensend
);
547 ok ( mem
->sock
[i
].n_sent
<= n_expected
, "select_server (%x): sent too many bytes: %d\n", id
, mem
->sock
[i
].n_sent
);
552 /* check that select returned the correct number of ready sockets */
553 ok ( ( n_set
== n_ready
), "select_server (%x): select() returns wrong number of ready sockets\n", id
);
555 /* check if all clients are done */
556 if ( ( fds_opensend
.fd_count
== 0 )
557 && ( fds_openrecv
.fd_count
== 1 ) /* initial socket that accepts clients */
558 && ( n_connections
== min ( gen
->n_clients
, MAX_CLIENTS
) ) ) {
563 for ( i
= 0; i
< min ( gen
->n_clients
, MAX_CLIENTS
); i
++ )
566 read_zero_bytes ( mem
->sock
[i
].s
);
567 wsa_ok ( closesocket ( mem
->sock
[i
].s
), 0 ==, "select_server (%x): closesocket error: %d\n" );
568 mem
->sock
[i
].s
= INVALID_SOCKET
;
571 trace ( "select_server (%x) exiting\n", id
);
575 /**************** Clients ***************/
578 * simple_client: A very basic client doing synchronous IO.
580 static VOID WINAPI
simple_client ( client_params
*par
)
582 test_params
*gen
= par
->general
;
584 int n_sent
, n_recvd
, n_expected
= gen
->n_chunks
* gen
->chunk_size
, id
;
587 id
= GetCurrentThreadId();
588 trace ( "simple_client (%x): starting\n", id
);
589 /* wait here because we want to call set_so_opentype before creating a socket */
590 WaitForSingleObject ( server_ready
, INFINITE
);
591 trace ( "simple_client (%x): server ready\n", id
);
593 check_so_opentype ();
594 set_so_opentype ( FALSE
); /* non-overlapped */
595 client_start ( par
);
596 mem
= TlsGetValue ( tls
);
599 wsa_ok ( connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) ),
600 0 ==, "simple_client (%x): connect error: %d\n" );
601 ok ( set_blocking ( mem
->s
, TRUE
) == 0,
602 "simple_client (%x): failed to set blocking mode\n", id
);
603 trace ( "simple_client (%x) connected\n", id
);
605 /* send data to server */
606 n_sent
= do_synchronous_send ( mem
->s
, mem
->send_buf
, n_expected
, par
->buflen
);
607 ok ( n_sent
== n_expected
,
608 "simple_client (%x): sent less data than expected: %d of %d\n", id
, n_sent
, n_expected
);
610 /* shutdown send direction */
611 wsa_ok ( shutdown ( mem
->s
, SD_SEND
), 0 ==, "simple_client (%x): shutdown failed: %d\n" );
613 /* Receive data echoed back & check it */
614 n_recvd
= do_synchronous_recv ( mem
->s
, mem
->recv_buf
, n_expected
, par
->buflen
);
615 ok ( n_recvd
== n_expected
,
616 "simple_client (%x): received less data than expected: %d of %d\n", id
, n_recvd
, n_expected
);
619 p
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
620 ok ( p
== NULL
, "simple_client (%x): test pattern error: %d\n", id
, p
- mem
->recv_buf
);
623 read_zero_bytes ( mem
->s
);
624 trace ( "simple_client (%x) exiting\n", id
);
629 * event_client: An event-driven client
631 static void WINAPI
event_client ( client_params
*par
)
633 test_params
*gen
= par
->general
;
635 int id
= GetCurrentThreadId(), n_expected
= gen
->n_chunks
* gen
->chunk_size
,
638 WSANETWORKEVENTS wsa_events
;
639 char *send_last
, *recv_last
, *send_p
, *recv_p
;
640 long mask
= FD_READ
| FD_WRITE
| FD_CLOSE
;
642 trace ( "event_client (%x): starting\n", id
);
643 client_start ( par
);
644 trace ( "event_client (%x): server ready\n", id
);
646 mem
= TlsGetValue ( tls
);
648 /* Prepare event notification for connect, makes socket nonblocking */
649 event
= WSACreateEvent ();
650 WSAEventSelect ( mem
->s
, event
, FD_CONNECT
);
651 tmp
= connect ( mem
->s
, (struct sockaddr
*) &mem
->addr
, sizeof ( mem
->addr
) );
652 if ( tmp
!= 0 && ( err
= WSAGetLastError () ) != WSAEWOULDBLOCK
)
653 ok ( 0, "event_client (%x): connect error: %d\n", id
, err
);
655 tmp
= WaitForSingleObject ( event
, INFINITE
);
656 ok ( tmp
== WAIT_OBJECT_0
, "event_client (%x): wait for connect event failed: %d\n", id
, tmp
);
657 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
658 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
660 err
= wsa_events
.iErrorCode
[ FD_CONNECT_BIT
];
661 ok ( err
== 0, "event_client (%x): connect error: %d\n", id
, err
);
664 trace ( "event_client (%x) connected\n", id
);
666 WSAEventSelect ( mem
->s
, event
, mask
);
668 recv_p
= mem
->recv_buf
;
669 recv_last
= mem
->recv_buf
+ n_expected
;
670 send_p
= mem
->send_buf
;
671 send_last
= mem
->send_buf
+ n_expected
;
675 err
= WaitForSingleObject ( event
, INFINITE
);
676 ok ( err
== WAIT_OBJECT_0
, "event_client (%x): wait failed\n", id
);
678 err
= WSAEnumNetworkEvents ( mem
->s
, event
, &wsa_events
);
679 wsa_ok ( err
, 0 ==, "event_client (%x): WSAEnumNetworkEvents error: %d\n" );
681 if ( wsa_events
.lNetworkEvents
& FD_WRITE
)
683 err
= wsa_events
.iErrorCode
[ FD_WRITE_BIT
];
684 ok ( err
== 0, "event_client (%x): FD_WRITE error code: %d\n", id
, err
);
689 n
= send ( mem
->s
, send_p
, min ( send_last
- send_p
, par
->buflen
), 0 );
692 err
= WSAGetLastError ();
693 ok ( err
== WSAEWOULDBLOCK
, "event_client (%x): send error: %d\n", id
, err
);
698 while ( n
>= 0 && send_p
< send_last
);
700 if ( send_p
== send_last
)
702 trace ( "event_client (%x): all data sent - shutdown\n", id
);
703 shutdown ( mem
->s
, SD_SEND
);
705 WSAEventSelect ( mem
->s
, event
, mask
);
708 if ( wsa_events
.lNetworkEvents
& FD_READ
)
710 err
= wsa_events
.iErrorCode
[ FD_READ_BIT
];
711 ok ( err
== 0, "event_client (%x): FD_READ error code: %d\n", id
, err
);
712 if ( err
!= 0 ) break;
714 /* First read must succeed */
715 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
716 wsa_ok ( n
, 0 <=, "event_client (%x): recv error: %d\n" );
720 if ( recv_p
== recv_last
)
723 trace ( "event_client (%x): all data received\n", id
);
724 WSAEventSelect ( mem
->s
, event
, mask
);
727 n
= recv ( mem
->s
, recv_p
, min ( recv_last
- recv_p
, par
->buflen
), 0 );
728 if ( n
< 0 && ( err
= WSAGetLastError()) != WSAEWOULDBLOCK
)
729 ok ( 0, "event_client (%x): read error: %d\n", id
, err
);
733 if ( wsa_events
.lNetworkEvents
& FD_CLOSE
)
735 trace ( "event_client (%x): close event\n", id
);
736 err
= wsa_events
.iErrorCode
[ FD_CLOSE_BIT
];
737 ok ( err
== 0, "event_client (%x): FD_CLOSE error code: %d\n", id
, err
);
742 ok ( send_p
== send_last
,
743 "simple_client (%x): sent less data than expected: %d of %d\n",
744 id
, send_p
- mem
->send_buf
, n_expected
);
745 ok ( recv_p
== recv_last
,
746 "simple_client (%x): received less data than expected: %d of %d\n",
747 id
, recv_p
- mem
->recv_buf
, n_expected
);
748 recv_p
= test_buffer ( mem
->recv_buf
, gen
->chunk_size
, gen
->n_chunks
);
749 ok ( recv_p
== NULL
, "event_client (%x): test pattern error: %d\n", id
, recv_p
- mem
->recv_buf
);
752 WSACloseEvent ( event
);
753 trace ( "event_client (%x) exiting\n", id
);
757 /**************** Main program utility functions ***************/
759 static void Init (void)
761 WORD ver
= MAKEWORD (2, 2);
764 ok ( WSAStartup ( ver
, &data
) == 0, "WSAStartup failed\n" );
768 static void Exit (void)
773 err
= WSAGetLastError();
774 ok ( ret
== 0, "WSACleanup failed ret = %d GetLastError is %d\n", ret
, err
);
776 err
= WSAGetLastError();
777 ok ( ret
== SOCKET_ERROR
&& err
== WSANOTINITIALISED
,
778 "WSACleanup returned %d GetLastError is %d\n", ret
, err
);
781 static void StartServer (LPTHREAD_START_ROUTINE routine
,
782 test_params
*general
, server_params
*par
)
784 par
->general
= general
;
785 thread
[0] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[0] );
786 ok ( thread
[0] != NULL
, "Failed to create server thread\n" );
789 static void StartClients (LPTHREAD_START_ROUTINE routine
,
790 test_params
*general
, client_params
*par
)
793 par
->general
= general
;
794 for ( i
= 1; i
<= min ( general
->n_clients
, MAX_CLIENTS
); i
++ )
797 thread
[i
] = CreateThread ( NULL
, 0, routine
, par
, 0, &thread_id
[i
] );
798 ok ( thread
[i
] != NULL
, "Failed to create client thread\n" );
799 /* Make sure the client is up and running */
800 WaitForSingleObject ( client_ready
[client_id
], INFINITE
);
804 static void do_test( test_setup
*test
)
806 DWORD i
, n
= min (test
->general
.n_clients
, MAX_CLIENTS
);
809 server_ready
= CreateEventW ( NULL
, TRUE
, FALSE
, NULL
);
810 for (i
= 0; i
<= n
; i
++)
811 client_ready
[i
] = CreateEventW ( NULL
, TRUE
, FALSE
, NULL
);
813 StartServer ( test
->srv
, &test
->general
, &test
->srv_params
);
814 StartClients ( test
->clt
, &test
->general
, &test
->clt_params
);
815 WaitForSingleObject ( server_ready
, INFINITE
);
817 wait
= WaitForMultipleObjects ( 1 + n
, thread
, TRUE
, 1000 * TEST_TIMEOUT
);
818 ok ( wait
>= WAIT_OBJECT_0
&& wait
<= WAIT_OBJECT_0
+ n
,
819 "some threads have not completed: %x\n", wait
);
821 if ( ! ( wait
>= WAIT_OBJECT_0
&& wait
<= WAIT_OBJECT_0
+ n
) )
823 for (i
= 0; i
<= n
; i
++)
825 trace ("terminating thread %08x\n", thread_id
[i
]);
826 if ( WaitForSingleObject ( thread
[i
], 0 ) != WAIT_OBJECT_0
)
827 TerminateThread ( thread
[i
], 0 );
830 CloseHandle ( server_ready
);
831 for (i
= 0; i
<= n
; i
++)
832 CloseHandle ( client_ready
[i
] );
835 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
836 /* optname = SO_LINGER */
837 LINGER linger_testvals
[] = {
844 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
845 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
846 bug in the linux kernel (fixed in 2.6.8) */
847 #define SOCKTIMEOUT2 997000 /* 997 seconds */
849 static void test_set_getsockopt(void)
857 s
= socket(AF_INET
, SOCK_STREAM
, 0);
858 ok(s
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
859 if( s
== INVALID_SOCKET
) return;
861 timeout
= SOCKTIMEOUT1
;
862 size
= sizeof(timeout
);
863 err
= setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, size
);
865 err
= getsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *) &timeout
, &size
);
866 ok( !err
, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
867 ok( timeout
== SOCKTIMEOUT1
, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout
);
869 timeout
= SOCKTIMEOUT2
; /* 54 seconds. See remark above */
870 size
= sizeof(timeout
);
871 err
= setsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, size
);
873 err
= getsockopt(s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *) &timeout
, &size
);
874 ok( !err
, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
875 ok( timeout
== SOCKTIMEOUT2
, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout
);
877 for( i
= 0; i
< sizeof(linger_testvals
)/sizeof(LINGER
);i
++) {
878 size
= sizeof(lingval
);
879 lingval
= linger_testvals
[i
];
880 err
= setsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, size
);
882 err
= getsockopt(s
, SOL_SOCKET
, SO_LINGER
, (char *) &lingval
, &size
);
883 ok( !err
, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
884 ok( !lingval
.l_onoff
== !linger_testvals
[i
].l_onoff
&&
885 (lingval
.l_linger
== linger_testvals
[i
].l_linger
||
886 (!lingval
.l_linger
&& !linger_testvals
[i
].l_onoff
))
887 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d\n", i
,
888 lingval
.l_onoff
, lingval
.l_linger
,
889 linger_testvals
[i
].l_onoff
, linger_testvals
[i
].l_linger
);
894 static void test_so_reuseaddr(void)
896 struct sockaddr_in saddr
;
898 unsigned int rc
,reuse
;
901 saddr
.sin_family
= AF_INET
;
902 saddr
.sin_port
= htons(9375);
903 saddr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
905 s1
=socket(AF_INET
, SOCK_STREAM
, 0);
906 ok(s1
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
907 rc
= bind(s1
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
908 ok(rc
!=SOCKET_ERROR
, "bind(s1) failed error: %d\n", WSAGetLastError());
910 s2
=socket(AF_INET
, SOCK_STREAM
, 0);
911 ok(s2
!=INVALID_SOCKET
, "socket() failed error: %d\n", WSAGetLastError());
915 rc
=getsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, &size
);
916 ok(rc
==0 && reuse
==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc
,reuse
);
918 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
919 ok(rc
==SOCKET_ERROR
, "bind() succeeded\n");
922 rc
= setsockopt(s2
, SOL_SOCKET
, SO_REUSEADDR
, (char*)&reuse
, sizeof(reuse
));
923 ok(rc
==0, "setsockopt() failed error: %d\n", WSAGetLastError());
926 rc
= bind(s2
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
927 ok(rc
==0, "bind() failed error: %d\n", WSAGetLastError());
934 /************* Array containing the tests to run **********/
936 #define STD_STREAM_SOCKET \
942 static test_setup tests
[NUM_TESTS
] =
944 /* Test 0: synchronous client and server */
965 /* Test 1: event-driven client, synchronous server */
986 /* Test 2: synchronous client, non-blocking server via select() */
1009 static void test_UDP(void)
1011 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
1012 possible that this test fails due to dropped packets. */
1014 /* peer 0 receives data from all other peers */
1015 struct sock_info peer
[NUM_UDP_PEERS
];
1017 int ss
, i
, n_recv
, n_sent
;
1019 for ( i
= NUM_UDP_PEERS
- 1; i
>= 0; i
-- ) {
1020 ok ( ( peer
[i
].s
= socket ( AF_INET
, SOCK_DGRAM
, 0 ) ) != INVALID_SOCKET
, "UDP: socket failed\n" );
1022 peer
[i
].addr
.sin_family
= AF_INET
;
1023 peer
[i
].addr
.sin_addr
.s_addr
= inet_addr ( SERVERIP
);
1026 peer
[i
].addr
.sin_port
= htons ( SERVERPORT
);
1028 peer
[i
].addr
.sin_port
= htons ( 0 );
1031 do_bind ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, sizeof( peer
[i
].addr
) );
1033 /* test getsockname() to get peer's port */
1034 ss
= sizeof ( peer
[i
].addr
);
1035 ok ( getsockname ( peer
[i
].s
, (struct sockaddr
*) &peer
[i
].addr
, &ss
) != SOCKET_ERROR
, "UDP: could not getsockname()\n" );
1036 ok ( peer
[i
].addr
.sin_port
!= htons ( 0 ), "UDP: bind() did not associate port\n" );
1039 /* test getsockname() */
1040 ok ( peer
[0].addr
.sin_port
== htons ( SERVERPORT
), "UDP: getsockname returned incorrect peer port\n" );
1042 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1043 /* send client's ip */
1044 memcpy( buf
, &peer
[i
].addr
.sin_port
, sizeof(peer
[i
].addr
.sin_port
) );
1045 n_sent
= sendto ( peer
[i
].s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &peer
[0].addr
, sizeof(peer
[0].addr
) );
1046 ok ( n_sent
== sizeof(buf
), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent
);
1049 for ( i
= 1; i
< NUM_UDP_PEERS
; i
++ ) {
1050 n_recv
= recvfrom ( peer
[0].s
, buf
, sizeof(buf
), 0,(struct sockaddr
*) &peer
[0].peer
, &ss
);
1051 ok ( n_recv
== sizeof(buf
), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv
);
1052 ok ( memcmp ( &peer
[0].peer
.sin_port
, buf
, sizeof(peer
[0].addr
.sin_port
) ) == 0, "UDP: port numbers do not match\n" );
1056 static void WINAPI
do_getservbyname( HANDLE
*starttest
)
1062 } serv
[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
1065 struct servent
*pserv
[2];
1067 ok ( WaitForSingleObject ( *starttest
, TEST_TIMEOUT
* 1000 ) != WAIT_TIMEOUT
, "test_getservbyname: timeout waiting for start signal\n");
1069 /* ensure that necessary buffer resizes are completed */
1070 for ( j
= 0; j
< 2; j
++) {
1071 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1074 for ( i
= 0; i
< NUM_QUERIES
/ 2; i
++ ) {
1075 for ( j
= 0; j
< 2; j
++ ) {
1076 pserv
[j
] = getservbyname ( serv
[j
].name
, serv
[j
].proto
);
1077 ok ( pserv
[j
] != NULL
, "getservbyname could not retrieve information for %s: %d\n", serv
[j
].name
, WSAGetLastError() );
1078 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
) );
1079 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
);
1080 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
);
1083 ok ( pserv
[0] == pserv
[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1087 static void test_getservbyname(void)
1090 HANDLE starttest
, thread
[NUM_THREADS
];
1091 DWORD thread_id
[NUM_THREADS
];
1093 starttest
= CreateEvent ( NULL
, 1, 0, "test_getservbyname_starttest" );
1095 /* create threads */
1096 for ( i
= 0; i
< NUM_THREADS
; i
++ ) {
1097 thread
[i
] = CreateThread ( NULL
, 0, (LPTHREAD_START_ROUTINE
) &do_getservbyname
, &starttest
, 0, &thread_id
[i
] );
1100 /* signal threads to start */
1101 SetEvent ( starttest
);
1103 for ( i
= 0; i
< NUM_THREADS
; i
++) {
1104 WaitForSingleObject ( thread
[i
], TEST_TIMEOUT
* 1000 );
1108 static void test_WSAAddressToStringA(void)
1113 SOCKADDR_IN sockaddr
;
1114 CHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1116 CHAR expect1
[] = "0.0.0.0";
1117 CHAR expect2
[] = "255.255.255.255";
1118 CHAR expect3
[] = "0.0.0.0:65535";
1119 CHAR expect4
[] = "255.255.255.255:65535";
1123 sockaddr
.sin_family
= AF_INET
;
1124 sockaddr
.sin_port
= 0;
1125 sockaddr
.sin_addr
.s_addr
= 0;
1127 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1128 GLE
= WSAGetLastError();
1129 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1130 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1133 len
= sizeof(address
);
1135 sockaddr
.sin_family
= AF_INET
;
1136 sockaddr
.sin_port
= 0;
1137 sockaddr
.sin_addr
.s_addr
= 0;
1139 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1140 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1142 ok( !strcmp( address
, expect1
), "Expected: %s, got: %s\n", expect1
, address
);
1144 len
= sizeof(address
);
1146 sockaddr
.sin_family
= AF_INET
;
1147 sockaddr
.sin_port
= 0;
1148 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1150 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1151 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1153 ok( !strcmp( address
, expect2
), "Expected: %s, got: %s\n", expect2
, address
);
1155 len
= sizeof(address
);
1157 sockaddr
.sin_family
= AF_INET
;
1158 sockaddr
.sin_port
= 0xffff;
1159 sockaddr
.sin_addr
.s_addr
= 0;
1161 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1162 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1164 ok( !strcmp( address
, expect3
), "Expected: %s, got: %s\n", expect3
, address
);
1166 len
= sizeof(address
);
1168 sockaddr
.sin_family
= AF_INET
;
1169 sockaddr
.sin_port
= 0xffff;
1170 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1172 ret
= WSAAddressToStringA( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1173 ok( !ret
, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1175 ok( !strcmp( address
, expect4
), "Expected: %s, got: %s\n", expect4
, address
);
1178 static void test_WSAAddressToStringW(void)
1183 SOCKADDR_IN sockaddr
;
1184 WCHAR address
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1186 WCHAR expect1
[] = { '0','.','0','.','0','.','0', 0 };
1187 WCHAR expect2
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1188 WCHAR expect3
[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1189 WCHAR expect4
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1190 '6', '5', '5', '3', '5', 0 };
1194 sockaddr
.sin_family
= AF_INET
;
1195 sockaddr
.sin_port
= 0;
1196 sockaddr
.sin_addr
.s_addr
= 0;
1198 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1199 GLE
= WSAGetLastError();
1200 ok( (ret
== SOCKET_ERROR
&& GLE
== WSAEFAULT
) || (ret
== 0),
1201 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1204 len
= sizeof(address
);
1206 sockaddr
.sin_family
= AF_INET
;
1207 sockaddr
.sin_port
= 0;
1208 sockaddr
.sin_addr
.s_addr
= 0;
1210 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1211 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1213 ok( !lstrcmpW( address
, expect1
), "Expected different address string\n" );
1215 len
= sizeof(address
);
1217 sockaddr
.sin_family
= AF_INET
;
1218 sockaddr
.sin_port
= 0;
1219 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1221 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1222 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1224 ok( !lstrcmpW( address
, expect2
), "Expected different address string\n" );
1226 len
= sizeof(address
);
1228 sockaddr
.sin_family
= AF_INET
;
1229 sockaddr
.sin_port
= 0xffff;
1230 sockaddr
.sin_addr
.s_addr
= 0;
1232 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1233 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1235 ok( !lstrcmpW( address
, expect3
), "Expected different address string\n" );
1237 len
= sizeof(address
);
1239 sockaddr
.sin_family
= AF_INET
;
1240 sockaddr
.sin_port
= 0xffff;
1241 sockaddr
.sin_addr
.s_addr
= 0xffffffff;
1243 ret
= WSAAddressToStringW( (SOCKADDR
*)&sockaddr
, sizeof(sockaddr
), NULL
, address
, &len
);
1244 ok( !ret
, "WSAAddressToStringW() failed unexpectedly: %d\n", WSAGetLastError() );
1246 ok( !lstrcmpW( address
, expect4
), "Expected different address string\n" );
1249 static void test_WSAStringToAddressA(void)
1252 SOCKADDR_IN sockaddr
;
1255 CHAR address1
[] = "0.0.0.0";
1256 CHAR address2
[] = "127.127.127.127";
1257 CHAR address3
[] = "255.255.255.255";
1258 CHAR address4
[] = "127.127.127.127:65535";
1259 CHAR address5
[] = "255.255.255.255:65535";
1262 sockaddr
.sin_family
= AF_INET
;
1264 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1265 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressA() succeeded unexpectedly: %d\n",
1266 WSAGetLastError() );
1268 len
= sizeof(sockaddr
);
1269 sockaddr
.sin_port
= 0;
1270 sockaddr
.sin_addr
.s_addr
= 0;
1272 ret
= WSAStringToAddressA( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1273 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1274 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1276 len
= sizeof(sockaddr
);
1277 sockaddr
.sin_port
= 0;
1278 sockaddr
.sin_addr
.s_addr
= 0;
1280 ret
= WSAStringToAddressA( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1281 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1282 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1284 len
= sizeof(sockaddr
);
1285 sockaddr
.sin_port
= 0;
1286 sockaddr
.sin_addr
.s_addr
= 0;
1288 ret
= WSAStringToAddressA( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1289 GLE
= WSAGetLastError();
1290 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1291 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1292 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1294 len
= sizeof(sockaddr
);
1295 sockaddr
.sin_port
= 0;
1296 sockaddr
.sin_addr
.s_addr
= 0;
1298 ret
= WSAStringToAddressA( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1299 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1300 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1302 len
= sizeof(sockaddr
);
1303 sockaddr
.sin_port
= 0;
1304 sockaddr
.sin_addr
.s_addr
= 0;
1306 ret
= WSAStringToAddressA( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1307 GLE
= WSAGetLastError();
1308 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1309 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1310 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE
);
1313 static void test_WSAStringToAddressW(void)
1316 SOCKADDR_IN sockaddr
;
1319 WCHAR address1
[] = { '0','.','0','.','0','.','0', 0 };
1320 WCHAR address2
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1321 WCHAR address3
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1322 WCHAR address4
[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1323 ':', '6', '5', '5', '3', '5', 0 };
1324 WCHAR address5
[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1325 '6', '5', '5', '3', '5', 0 };
1328 sockaddr
.sin_family
= AF_INET
;
1330 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1331 ok( ret
== SOCKET_ERROR
, "WSAStringToAddressW() failed unexpectedly: %d\n",
1332 WSAGetLastError() );
1334 len
= sizeof(sockaddr
);
1335 sockaddr
.sin_port
= 0;
1336 sockaddr
.sin_addr
.s_addr
= 0;
1338 ret
= WSAStringToAddressW( address1
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1339 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0,
1340 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1342 len
= sizeof(sockaddr
);
1343 sockaddr
.sin_port
= 0;
1344 sockaddr
.sin_addr
.s_addr
= 0;
1346 ret
= WSAStringToAddressW( address2
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1347 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f,
1348 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1350 len
= sizeof(sockaddr
);
1351 sockaddr
.sin_port
= 0;
1352 sockaddr
.sin_addr
.s_addr
= 0;
1354 ret
= WSAStringToAddressW( address3
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1355 GLE
= WSAGetLastError();
1356 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff) ||
1357 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1358 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1360 len
= sizeof(sockaddr
);
1361 sockaddr
.sin_port
= 0;
1362 sockaddr
.sin_addr
.s_addr
= 0;
1364 ret
= WSAStringToAddressW( address4
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1365 ok( !ret
&& sockaddr
.sin_addr
.s_addr
== 0x7f7f7f7f && sockaddr
.sin_port
== 0xffff,
1366 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1368 len
= sizeof(sockaddr
);
1369 sockaddr
.sin_port
= 0;
1370 sockaddr
.sin_addr
.s_addr
= 0;
1372 ret
= WSAStringToAddressW( address5
, AF_INET
, NULL
, (SOCKADDR
*)&sockaddr
, &len
);
1373 ok( (ret
== 0 && sockaddr
.sin_addr
.s_addr
== 0xffffffff && sockaddr
.sin_port
== 0xffff) ||
1374 (ret
== SOCKET_ERROR
&& (GLE
== ERROR_INVALID_PARAMETER
|| GLE
== WSAEINVAL
)),
1375 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE
);
1378 static VOID WINAPI
SelectReadThread(select_thread_params
*par
)
1382 struct sockaddr_in addr
;
1383 struct timeval select_timeout
;
1386 FD_SET(par
->s
, &readfds
);
1387 select_timeout
.tv_sec
=5;
1388 select_timeout
.tv_usec
=0;
1389 addr
.sin_family
= AF_INET
;
1390 addr
.sin_addr
.s_addr
= inet_addr(SERVERIP
);
1391 addr
.sin_port
= htons(SERVERPORT
);
1393 do_bind(par
->s
, (struct sockaddr
*)&addr
, sizeof(addr
));
1394 wsa_ok(listen(par
->s
, SOMAXCONN
), 0 ==, "SelectReadThread (%x): listen failed: %d\n");
1396 SetEvent(server_ready
);
1397 ret
= select(par
->s
+1, &readfds
, NULL
, NULL
, &select_timeout
);
1398 par
->ReadKilled
= (ret
== 1);
1401 static void test_select(void)
1403 SOCKET fdRead
, fdWrite
;
1404 fd_set readfds
, writefds
, exceptfds
;
1407 struct timeval select_timeout
;
1408 select_thread_params thread_params
;
1409 HANDLE thread_handle
;
1412 fdRead
= socket(AF_INET
, SOCK_STREAM
, 0);
1413 ok( (fdRead
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1414 fdWrite
= socket(AF_INET
, SOCK_STREAM
, 0);
1415 ok( (fdWrite
!= INVALID_SOCKET
), "socket failed unexpectedly: %d\n", WSAGetLastError() );
1419 FD_ZERO(&exceptfds
);
1420 FD_SET(fdRead
, &readfds
);
1421 FD_SET(fdWrite
, &writefds
);
1422 FD_SET(fdRead
, &exceptfds
);
1423 FD_SET(fdWrite
, &exceptfds
);
1424 select_timeout
.tv_sec
=0;
1425 select_timeout
.tv_usec
=500;
1428 if (fdWrite
> maxfd
)
1432 ret
= select(maxfd
+1, &readfds
, &writefds
, &exceptfds
, &select_timeout
);
1433 ok ( (ret
== 0), "select should not return any socket handles\n");
1434 ok ( !FD_ISSET(fdRead
, &readfds
), "FD should not be set\n");
1435 ok ( !FD_ISSET(fdWrite
, &writefds
), "FD should not be set\n");
1438 ok ( !FD_ISSET(fdRead
, &exceptfds
), "FD should not be set\n");
1439 ok ( !FD_ISSET(fdWrite
, &exceptfds
), "FD should not be set\n");
1442 ok ((listen(fdWrite
, SOMAXCONN
) == SOCKET_ERROR
), "listen did not fail\n");
1444 ret
= closesocket(fdWrite
);
1445 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1447 thread_params
.s
= fdRead
;
1448 thread_params
.ReadKilled
= FALSE
;
1449 server_ready
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1450 thread_handle
= CreateThread (NULL
, 0, (LPTHREAD_START_ROUTINE
) &SelectReadThread
, &thread_params
, 0, &id
);
1451 ok ( (thread_handle
!= NULL
), "CreateThread failed unexpectedly: %d\n", GetLastError());
1453 WaitForSingleObject (server_ready
, INFINITE
);
1455 ret
= closesocket(fdRead
);
1456 ok ( (ret
== 0), "closesocket failed unexpectedly: %d\n", ret
);
1458 WaitForSingleObject (thread_handle
, 1000);
1459 ok ( (thread_params
.ReadKilled
), "closesocket did not wakeup select\n");
1463 static DWORD WINAPI
AcceptKillThread(select_thread_params
*par
)
1465 struct sockaddr_in address
;
1466 int len
= sizeof(address
);
1467 SOCKET client_socket
;
1469 SetEvent(server_ready
);
1470 client_socket
= accept(par
->s
, (struct sockaddr
*) &address
, &len
);
1471 if (client_socket
!= INVALID_SOCKET
)
1472 closesocket(client_socket
);
1473 par
->ReadKilled
= (client_socket
== INVALID_SOCKET
);
1477 static void test_accept(void)
1480 SOCKET server_socket
= INVALID_SOCKET
;
1481 struct sockaddr_in address
;
1482 select_thread_params thread_params
;
1483 HANDLE thread_handle
= NULL
;
1486 server_socket
= socket(AF_INET
, SOCK_STREAM
, 0);
1487 if (server_socket
== INVALID_SOCKET
)
1489 trace("error creating server socket: %d\n", WSAGetLastError());
1493 memset(&address
, 0, sizeof(address
));
1494 address
.sin_family
= AF_INET
;
1495 ret
= bind(server_socket
, (struct sockaddr
*) &address
, sizeof(address
));
1498 trace("error binding server socket: %d\n", WSAGetLastError());
1502 ret
= listen(server_socket
, 1);
1505 trace("error making server socket listen: %d\n", WSAGetLastError());
1509 server_ready
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1510 if (server_ready
== INVALID_HANDLE_VALUE
)
1512 trace("error creating event: %d\n", GetLastError());
1516 thread_params
.s
= server_socket
;
1517 thread_params
.ReadKilled
= FALSE
;
1518 thread_handle
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
) AcceptKillThread
,
1519 &thread_params
, 0, &id
);
1520 if (thread_handle
== NULL
)
1522 trace("error creating thread: %d\n", GetLastError());
1526 WaitForSingleObject(server_ready
, INFINITE
);
1528 ret
= closesocket(server_socket
);
1531 trace("closesocket failed: %d\n", WSAGetLastError());
1535 WaitForSingleObject(thread_handle
, 1000);
1536 ok(thread_params
.ReadKilled
, "closesocket did not wakeup accept\n");
1539 if (thread_handle
!= NULL
)
1540 CloseHandle(thread_handle
);
1541 if (server_ready
!= INVALID_HANDLE_VALUE
)
1542 CloseHandle(server_ready
);
1543 if (server_socket
!= INVALID_SOCKET
)
1544 closesocket(server_socket
);
1547 static void test_extendedSocketOptions(void)
1551 struct sockaddr_in sa
;
1552 int sa_len
= sizeof(struct sockaddr_in
);
1553 int optval
, optlen
= sizeof(int), ret
;
1557 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
1558 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1562 memset(&sa
, 0, sa_len
);
1564 sa
.sin_family
= AF_INET
;
1565 sa
.sin_port
= htons(0);
1566 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1568 if((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) < 0){
1569 trace("Creating the socket failed: %d\n", WSAGetLastError());
1574 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
1575 trace("Failed to bind socket: %d\n", WSAGetLastError());
1581 ret
= getsockopt(sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char *)&optval
, &optlen
);
1583 ok(ret
== 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret
);
1584 ok(optval
== 65507, "SO_MAX_MSG_SIZE reported %d, expected 65507\n", optval
);
1586 optlen
= sizeof(LINGER
);
1587 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
1589 ok(ret
== SOCKET_ERROR
, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret
);
1594 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) < 0){
1595 trace("Creating the socket failed: %d\n", WSAGetLastError());
1600 if(bind(sock
, (struct sockaddr
*) &sa
, sa_len
) < 0){
1601 trace("Failed to bind socket: %d\n", WSAGetLastError());
1607 ret
= getsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (char *)&linger_val
, &optlen
);
1608 ok(ret
== 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret
);
1610 optlen
= sizeof(BOOL
);
1611 ret
= getsockopt(sock
, SOL_SOCKET
, SO_DONTLINGER
, (char *)&bool_opt_val
, &optlen
);
1612 ok(ret
== 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret
);
1613 ok((linger_val
.l_onoff
&& !bool_opt_val
) || (!linger_val
.l_onoff
&& bool_opt_val
),
1614 "Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
1615 bool_opt_val
, linger_val
.l_onoff
);
1621 static void test_getsockname(void)
1625 struct sockaddr_in sa_set
, sa_get
;
1626 int sa_set_len
= sizeof(struct sockaddr_in
);
1627 int sa_get_len
= sa_set_len
;
1628 static const unsigned char null_padding
[] = {0,0,0,0,0,0,0,0};
1630 if(WSAStartup(MAKEWORD(2,0), &wsa
)){
1631 trace("Winsock failed: %d. Aborting test\n", WSAGetLastError());
1635 memset(&sa_set
, 0, sa_set_len
);
1637 sa_set
.sin_family
= AF_INET
;
1638 sa_set
.sin_port
= htons(0);
1639 sa_set
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1641 if((sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
)) < 0){
1642 trace("Creating the socket failed: %d\n", WSAGetLastError());
1647 memcpy(&sa_get
, &sa_set
, sizeof(sa_set
));
1648 if (getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) == 0)
1649 ok(0, "getsockname on unbound socket should fail\n");
1651 ok(WSAGetLastError() == WSAEINVAL
, "getsockname on unbound socket "
1652 "failed with %d, expected %d\n", WSAGetLastError(), WSAEINVAL
);
1653 ok(memcmp(&sa_get
, &sa_set
, sizeof(sa_get
)) == 0,
1654 "failed getsockname modified sockaddr when it shouldn't\n");
1657 if(bind(sock
, (struct sockaddr
*) &sa_set
, sa_set_len
) < 0){
1658 trace("Failed to bind socket: %d\n", WSAGetLastError());
1664 if(getsockname(sock
, (struct sockaddr
*) &sa_get
, &sa_get_len
) != 0){
1665 trace("Failed to call getsockname: %d\n", WSAGetLastError());
1671 ok(memcmp(sa_get
.sin_zero
, null_padding
, 8) == 0,
1672 "getsockname did not zero the sockaddr_in structure\n");
1678 static void test_dns(void)
1682 h
= gethostbyname("");
1683 ok(h
!= NULL
, "gethostbyname(\"\") failed with %d\n", h_errno
);
1686 static void test_inet_addr(void)
1690 addr
= inet_addr(NULL
);
1691 ok(addr
== INADDR_NONE
, "inet_addr succeeded unexpectedly\n");
1694 static DWORD WINAPI
drain_socket_thread(LPVOID arg
)
1697 SOCKET sock
= *(SOCKET
*)arg
;
1699 while (recv(sock
, buffer
, sizeof(buffer
), 0) > 0)
1704 static void test_send(void)
1706 SOCKET src
= INVALID_SOCKET
;
1707 SOCKET dst
= INVALID_SOCKET
;
1708 HANDLE hThread
= NULL
;
1709 const int buflen
= 1024*1024;
1710 char *buffer
= NULL
;
1714 if (tcp_socketpair(&src
, &dst
) != 0)
1716 ok(0, "creating socket pair failed, skipping test\n");
1720 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
1721 if (hThread
== NULL
)
1723 ok(0, "CreateThread failed, error %d\n", GetLastError());
1727 buffer
= HeapAlloc(GetProcessHeap(), 0, buflen
);
1730 ok(0, "HeapAlloc failed, error %d\n", GetLastError());
1734 ret
= send(src
, buffer
, buflen
, 0);
1736 ok(ret
== buflen
, "send should have sent %d bytes, but it only sent %d\n", buflen
, ret
);
1738 ok(0, "send failed, error %d\n", WSAGetLastError());
1741 if (src
!= INVALID_SOCKET
)
1743 if (dst
!= INVALID_SOCKET
)
1745 if (hThread
!= NULL
)
1746 CloseHandle(hThread
);
1747 HeapFree(GetProcessHeap(), 0, buffer
);
1750 static void test_write_events(void)
1752 SOCKET src
= INVALID_SOCKET
;
1753 SOCKET dst
= INVALID_SOCKET
;
1754 HANDLE hThread
= NULL
;
1755 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
1761 if (tcp_socketpair(&src
, &dst
) != 0)
1763 ok(0, "creating socket pair failed, skipping test\n");
1767 hThread
= CreateThread(NULL
, 0, drain_socket_thread
, &dst
, 0, &id
);
1768 if (hThread
== NULL
)
1770 ok(0, "CreateThread failed, error %d\n", GetLastError());
1774 hEvent
= CreateEventW(NULL
, FALSE
, TRUE
, NULL
);
1775 if (hEvent
== INVALID_HANDLE_VALUE
)
1777 ok(0, "CreateEventW failed, error %d\n", GetLastError());
1781 ret
= ioctlsocket(src
, FIONBIO
, &one
);
1784 ok(0, "ioctlsocket failed, error %d\n", WSAGetLastError());
1788 ret
= WSAEventSelect(src
, hEvent
, FD_WRITE
| FD_CLOSE
);
1791 ok(0, "WSAEventSelect failed, error %d\n", ret
);
1795 for (len
= 100; len
> 0; --len
)
1797 WSANETWORKEVENTS netEvents
;
1798 DWORD dwRet
= WaitForSingleObject(hEvent
, 5000);
1799 if (dwRet
!= WAIT_OBJECT_0
)
1801 ok(0, "WaitForSingleObject failed, error %d\n", dwRet
);
1805 ret
= WSAEnumNetworkEvents(src
, NULL
, &netEvents
);
1808 ok(0, "WSAEnumNetworkEvents failed, error %d\n", ret
);
1812 if (netEvents
.lNetworkEvents
& FD_WRITE
)
1814 ret
= send(src
, "a", 1, 0);
1815 if (ret
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
)
1817 ok(0, "send failed, error %d\n", WSAGetLastError());
1822 if (netEvents
.lNetworkEvents
& FD_CLOSE
)
1824 ok(0, "unexpected close\n");
1830 if (src
!= INVALID_SOCKET
)
1832 if (dst
!= INVALID_SOCKET
)
1834 if (hThread
!= NULL
)
1835 CloseHandle(hThread
);
1836 CloseHandle(hEvent
);
1839 /**************** Main program ***************/
1846 test_set_getsockopt();
1847 test_so_reuseaddr();
1848 test_extendedSocketOptions();
1850 for (i
= 0; i
< NUM_TESTS
; i
++)
1852 trace ( " **** STARTING TEST %d ****\n", i
);
1853 do_test ( &tests
[i
] );
1854 trace ( " **** TEST %d COMPLETE ****\n", i
);
1859 test_getservbyname();
1861 test_WSAAddressToStringA();
1862 test_WSAAddressToStringW();
1864 test_WSAStringToAddressA();
1865 test_WSAStringToAddressW();
1874 test_write_events();