- Stricten cases for NT in a two tests.
[wine.git] / dlls / winsock / tests / sock.c
blob5714c7330167023ad5855bd4f9b00d93883855ba
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
24 #include <windef.h>
25 #include <winbase.h>
26 #include <winsock2.h>
27 #include <mswsock.h>
28 #include "wine/test.h"
29 #include <winnt.h>
30 #include <winerror.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) \
49 do { \
50 int tmp, err = 0; \
51 tmp = op; \
52 if ( !(cond tmp) ) err = WSAGetLastError(); \
53 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
54 } while (0);
57 /**************** Structs and typedefs ***************/
59 typedef struct thread_info
61 HANDLE thread;
62 DWORD id;
63 } thread_info;
65 /* Information in the server about open client connections */
66 typedef struct sock_info
68 SOCKET s;
69 struct sockaddr_in addr;
70 struct sockaddr_in peer;
71 char *buf;
72 int n_recvd;
73 int n_sent;
74 } sock_info;
76 /* Test parameters for both server & client */
77 typedef struct test_params
79 int sock_type;
80 int sock_prot;
81 const char *inet_addr;
82 short inet_port;
83 int chunk_size;
84 int n_chunks;
85 int n_clients;
86 } test_params;
88 /* server-specific test parameters */
89 typedef struct server_params
91 test_params *general;
92 DWORD sock_flags;
93 int buflen;
94 } server_params;
96 /* client-specific test parameters */
97 typedef struct client_params
99 test_params *general;
100 DWORD sock_flags;
101 int buflen;
102 } client_params;
104 /* This type combines all information for setting up a test scenario */
105 typedef struct test_setup
107 test_params general;
108 LPVOID srv;
109 server_params srv_params;
110 LPVOID clt;
111 client_params clt_params;
112 } test_setup;
114 /* Thread local storage for server */
115 typedef struct server_memory
117 SOCKET s;
118 struct sockaddr_in addr;
119 sock_info sock[MAX_CLIENTS];
120 } server_memory;
122 /* Thread local storage for client */
123 typedef struct client_memory
125 SOCKET s;
126 struct sockaddr_in addr;
127 char *send_buf;
128 char *recv_buf;
129 } client_memory;
131 /**************** Static variables ***************/
133 static DWORD tls; /* Thread local storage index */
134 static HANDLE thread[1+MAX_CLIENTS];
135 static DWORD thread_id[1+MAX_CLIENTS];
136 static HANDLE server_ready;
137 static HANDLE client_ready[MAX_CLIENTS];
138 static int client_id;
140 /**************** General utility functions ***************/
142 static void set_so_opentype ( BOOL overlapped )
144 int optval = !overlapped, newval, len = sizeof (int);
146 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
147 (LPVOID) &optval, sizeof (optval) ) == 0,
148 "setting SO_OPENTYPE failed\n" );
149 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
150 (LPVOID) &newval, &len ) == 0,
151 "getting SO_OPENTYPE failed\n" );
152 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
155 static int set_blocking ( SOCKET s, BOOL blocking )
157 u_long val = !blocking;
158 return ioctlsocket ( s, FIONBIO, &val );
161 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
163 char c, *p;
164 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
165 memset ( p, c, chunk_size );
168 static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
170 char c, *p;
171 int i;
172 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
174 for ( i = 0; i < chunk_size; i++ )
175 if ( p[i] != c ) return p + i;
177 return NULL;
181 * This routine is called when a client / server does not expect any more data,
182 * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
184 static void read_zero_bytes ( SOCKET s )
186 char buf[256];
187 int tmp, n = 0;
188 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
189 n += tmp;
190 ok ( n <= 0, "garbage data received: %d bytes\n", n );
193 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
195 char* last = buf + buflen, *p;
196 int n = 1;
197 for ( p = buf; n > 0 && p < last; p += n )
198 n = send ( s, p, min ( sendlen, last - p ), 0 );
199 wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" );
200 return p - buf;
203 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
205 char* last = buf + buflen, *p;
206 int n = 1;
207 for ( p = buf; n > 0 && p < last; p += n )
208 n = recv ( s, p, min ( recvlen, last - p ), 0 );
209 wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" );
210 return p - buf;
214 * Call this routine right after thread startup.
215 * SO_OPENTYPE must by 0, regardless what the server did.
217 static void check_so_opentype (void)
219 int tmp = 1, len;
220 len = sizeof (tmp);
221 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
222 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
225 /**************** Server utility functions ***************/
228 * Even if we have closed our server socket cleanly,
229 * the OS may mark the address "in use" for some time -
230 * this happens with native Linux apps, too.
232 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
234 int err, wsaerr = 0, n_try = BIND_TRIES;
236 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
237 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
238 n_try-- >= 0)
240 trace ( "address in use, waiting ...\n" );
241 Sleep ( 1000 * BIND_SLEEP );
243 ok ( err == 0, "failed to bind: %d\n", wsaerr );
246 static void server_start ( server_params *par )
248 int i;
249 test_params *gen = par->general;
250 server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
252 TlsSetValue ( tls, mem );
253 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
254 NULL, 0, par->sock_flags );
255 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
257 mem->addr.sin_family = AF_INET;
258 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
259 mem->addr.sin_port = htons ( gen->inet_port );
261 for (i = 0; i < MAX_CLIENTS; i++)
263 mem->sock[i].s = INVALID_SOCKET;
264 mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
265 mem->sock[i].n_recvd = 0;
266 mem->sock[i].n_sent = 0;
269 if ( gen->sock_type == SOCK_STREAM )
270 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
273 static void server_stop (void)
275 int i;
276 server_memory *mem = TlsGetValue ( tls );
278 for (i = 0; i < MAX_CLIENTS; i++ )
280 LocalFree ( (HANDLE) mem->sock[i].buf );
281 if ( mem->sock[i].s != INVALID_SOCKET )
282 closesocket ( mem->sock[i].s );
284 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
285 LocalFree ( (HANDLE) mem );
286 ExitThread ( GetCurrentThreadId () );
289 /**************** Client utilitiy functions ***************/
291 static void client_start ( client_params *par )
293 test_params *gen = par->general;
294 client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
296 TlsSetValue ( tls, mem );
298 WaitForSingleObject ( server_ready, INFINITE );
300 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
301 NULL, 0, par->sock_flags );
303 mem->addr.sin_family = AF_INET;
304 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
305 mem->addr.sin_port = htons ( gen->inet_port );
307 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
309 mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
310 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
311 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
313 SetEvent ( client_ready[client_id] );
314 /* Wait for the other clients to come up */
315 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
318 static void client_stop (void)
320 client_memory *mem = TlsGetValue ( tls );
321 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%lx): %d\n" );
322 LocalFree ( (HANDLE) mem->send_buf );
323 LocalFree ( (HANDLE) mem );
324 ExitThread(0);
327 /**************** Servers ***************/
330 * simple_server: A very basic server doing synchronous IO.
332 static VOID WINAPI simple_server ( server_params *par )
334 test_params *gen = par->general;
335 server_memory *mem;
336 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
337 id = GetCurrentThreadId();
338 char *p;
340 trace ( "simple_server (%x) starting\n", id );
342 set_so_opentype ( FALSE ); /* non-overlapped */
343 server_start ( par );
344 mem = TlsGetValue ( tls );
346 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%lx): failed to set blocking mode: %d\n");
347 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%lx): listen failed: %d\n");
349 trace ( "simple_server (%x) ready\n", id );
350 SetEvent ( server_ready ); /* notify clients */
352 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
354 trace ( "simple_server (%x): waiting for client\n", id );
356 /* accept a single connection */
357 tmp = sizeof ( mem->sock[0].peer );
358 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
359 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%lx): accept failed: %d\n" );
361 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
362 "simple_server (%x): strange peer address\n", id );
364 /* Receive data & check it */
365 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
366 ok ( n_recvd == n_expected,
367 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
368 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
369 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
371 /* Echo data back */
372 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
373 ok ( n_sent == n_expected,
374 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
376 /* cleanup */
377 read_zero_bytes ( mem->sock[0].s );
378 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%lx): closesocket error: %d\n" );
379 mem->sock[0].s = INVALID_SOCKET;
382 trace ( "simple_server (%x) exiting\n", id );
383 server_stop ();
387 * select_server: A non-blocking server.
389 static VOID WINAPI select_server ( server_params *par )
391 test_params *gen = par->general;
392 server_memory *mem;
393 int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
394 id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
395 n_set, delta, n_ready;
396 char *p;
397 struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
398 fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
400 trace ( "select_server (%x) starting\n", id );
402 set_so_opentype ( FALSE ); /* non-overlapped */
403 server_start ( par );
404 mem = TlsGetValue ( tls );
406 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%lx): failed to set blocking mode: %d\n");
407 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%lx): listen failed: %d\n");
409 trace ( "select_server (%x) ready\n", id );
410 SetEvent ( server_ready ); /* notify clients */
412 FD_ZERO ( &fds_openrecv );
413 FD_ZERO ( &fds_recv );
414 FD_ZERO ( &fds_send );
415 FD_ZERO ( &fds_opensend );
417 FD_SET ( mem->s, &fds_openrecv );
419 while(1)
421 fds_recv = fds_openrecv;
422 fds_send = fds_opensend;
424 n_set = 0;
426 wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
427 "select_server (%lx): select() failed: %d\n" );
429 /* check for incoming requests */
430 if ( FD_ISSET ( mem->s, &fds_recv ) ) {
431 n_set += 1;
433 trace ( "select_server (%x): accepting client connection\n", id );
435 /* accept a single connection */
436 tmp = sizeof ( mem->sock[n_connections].peer );
437 mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
438 wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%lx): accept() failed: %d\n" );
440 ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
441 "select_server (%x): strange peer address\n", id );
443 /* add to list of open connections */
444 FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
445 FD_SET ( mem->sock[n_connections].s, &fds_opensend );
447 n_connections++;
450 /* handle open requests */
452 for ( i = 0; i < n_connections; i++ )
454 if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
455 n_set += 1;
457 if ( mem->sock[i].n_recvd < n_expected ) {
458 /* Receive data & check it */
459 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 );
460 ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
461 mem->sock[i].n_recvd += n_recvd;
463 if ( mem->sock[i].n_recvd == n_expected ) {
464 p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
465 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
466 FD_CLR ( mem->sock[i].s, &fds_openrecv );
469 ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
473 /* only echo back what we've received */
474 delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
476 if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
477 n_set += 1;
479 if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
480 /* Echo data back */
481 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
482 ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
483 mem->sock[i].n_sent += n_sent;
485 if ( mem->sock[i].n_sent == n_expected ) {
486 FD_CLR ( mem->sock[i].s, &fds_opensend );
489 ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
494 /* check that select returned the correct number of ready sockets */
495 ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
497 /* check if all clients are done */
498 if ( ( fds_opensend.fd_count == 0 )
499 && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
500 && ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
501 break;
505 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
507 /* cleanup */
508 read_zero_bytes ( mem->sock[i].s );
509 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%lx): closesocket error: %d\n" );
510 mem->sock[i].s = INVALID_SOCKET;
513 trace ( "select_server (%x) exiting\n", id );
514 server_stop ();
517 /**************** Clients ***************/
520 * simple_client: A very basic client doing synchronous IO.
522 static VOID WINAPI simple_client ( client_params *par )
524 test_params *gen = par->general;
525 client_memory *mem;
526 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
527 char *p;
529 id = GetCurrentThreadId();
530 trace ( "simple_client (%x): starting\n", id );
531 /* wait here because we want to call set_so_opentype before creating a socket */
532 WaitForSingleObject ( server_ready, INFINITE );
533 trace ( "simple_client (%x): server ready\n", id );
535 check_so_opentype ();
536 set_so_opentype ( FALSE ); /* non-overlapped */
537 client_start ( par );
538 mem = TlsGetValue ( tls );
540 /* Connect */
541 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
542 0 ==, "simple_client (%lx): connect error: %d\n" );
543 ok ( set_blocking ( mem->s, TRUE ) == 0,
544 "simple_client (%x): failed to set blocking mode\n", id );
545 trace ( "simple_client (%x) connected\n", id );
547 /* send data to server */
548 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
549 ok ( n_sent == n_expected,
550 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
552 /* shutdown send direction */
553 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
555 /* Receive data echoed back & check it */
556 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
557 ok ( n_recvd == n_expected,
558 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
560 /* check data */
561 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
562 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
564 /* cleanup */
565 read_zero_bytes ( mem->s );
566 trace ( "simple_client (%x) exiting\n", id );
567 client_stop ();
571 * event_client: An event-driven client
573 static void WINAPI event_client ( client_params *par )
575 test_params *gen = par->general;
576 client_memory *mem;
577 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
578 tmp, err, n;
579 HANDLE event;
580 WSANETWORKEVENTS wsa_events;
581 char *send_last, *recv_last, *send_p, *recv_p;
582 long mask = FD_READ | FD_WRITE | FD_CLOSE;
584 trace ( "event_client (%x): starting\n", id );
585 client_start ( par );
586 trace ( "event_client (%x): server ready\n", id );
588 mem = TlsGetValue ( tls );
590 /* Prepare event notification for connect, makes socket nonblocking */
591 event = WSACreateEvent ();
592 WSAEventSelect ( mem->s, event, FD_CONNECT );
593 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
594 if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
595 ok ( 0, "event_client (%x): connect error: %d\n", id, err );
597 tmp = WaitForSingleObject ( event, INFINITE );
598 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
599 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
600 wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
602 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
603 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
604 if ( err ) goto out;
606 trace ( "event_client (%x) connected\n", id );
608 WSAEventSelect ( mem->s, event, mask );
610 recv_p = mem->recv_buf;
611 recv_last = mem->recv_buf + n_expected;
612 send_p = mem->send_buf;
613 send_last = mem->send_buf + n_expected;
615 while ( TRUE )
617 err = WaitForSingleObject ( event, INFINITE );
618 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
620 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
621 wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
623 if ( wsa_events.lNetworkEvents & FD_WRITE )
625 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
626 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
628 if ( err== 0 )
631 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
632 if ( n < 0 )
634 err = WSAGetLastError ();
635 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
637 else
638 send_p += n;
640 while ( n >= 0 && send_p < send_last );
642 if ( send_p == send_last )
644 trace ( "event_client (%x): all data sent - shutdown\n", id );
645 shutdown ( mem->s, SD_SEND );
646 mask &= ~FD_WRITE;
647 WSAEventSelect ( mem->s, event, mask );
650 if ( wsa_events.lNetworkEvents & FD_READ )
652 err = wsa_events.iErrorCode[ FD_READ_BIT ];
653 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
654 if ( err != 0 ) break;
656 /* First read must succeed */
657 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
658 wsa_ok ( n, 0 <=, "event_client (%lx): recv error: %d\n" );
660 while ( n >= 0 ) {
661 recv_p += n;
662 if ( recv_p == recv_last )
664 mask &= ~FD_READ;
665 trace ( "event_client (%x): all data received\n", id );
666 WSAEventSelect ( mem->s, event, mask );
667 break;
669 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
670 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
671 ok ( 0, "event_client (%x): read error: %d\n", id, err );
675 if ( wsa_events.lNetworkEvents & FD_CLOSE )
677 trace ( "event_client (%x): close event\n", id );
678 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
679 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
680 break;
684 ok ( send_p == send_last,
685 "simple_client (%x): sent less data than expected: %d of %d\n",
686 id, send_p - mem->send_buf, n_expected );
687 ok ( recv_p == recv_last,
688 "simple_client (%x): received less data than expected: %d of %d\n",
689 id, recv_p - mem->recv_buf, n_expected );
690 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
691 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
693 out:
694 WSACloseEvent ( event );
695 trace ( "event_client (%x) exiting\n", id );
696 client_stop ();
699 /**************** Main program utility functions ***************/
701 static void Init (void)
703 WORD ver = MAKEWORD (2, 2);
704 WSADATA data;
706 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
707 tls = TlsAlloc();
710 static void Exit (void)
712 TlsFree ( tls );
713 ok ( WSACleanup() == 0, "WSACleanup failed\n" );
716 static void StartServer (LPTHREAD_START_ROUTINE routine,
717 test_params *general, server_params *par)
719 par->general = general;
720 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
721 ok ( thread[0] != NULL, "Failed to create server thread\n" );
724 static void StartClients (LPTHREAD_START_ROUTINE routine,
725 test_params *general, client_params *par)
727 int i;
728 par->general = general;
729 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
731 client_id = i - 1;
732 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
733 ok ( thread[i] != NULL, "Failed to create client thread\n" );
734 /* Make sure the client is up and running */
735 WaitForSingleObject ( client_ready[client_id], INFINITE );
739 static void do_test( test_setup *test )
741 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
742 DWORD wait;
744 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
745 for (i = 0; i <= n; i++)
746 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
748 StartServer ( test->srv, &test->general, &test->srv_params );
749 StartClients ( test->clt, &test->general, &test->clt_params );
750 WaitForSingleObject ( server_ready, INFINITE );
752 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
753 ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ,
754 "some threads have not completed: %lx\n", wait );
756 if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
758 for (i = 0; i <= n; i++)
760 trace ("terminating thread %08lx\n", thread_id[i]);
761 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
762 TerminateThread ( thread [i], 0 );
765 CloseHandle ( server_ready );
766 for (i = 0; i <= n; i++)
767 CloseHandle ( client_ready[i] );
770 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
771 /* optname = SO_LINGER */
772 LINGER linger_testvals[] = {
773 {0,0},
774 {0,73},
775 {1,0},
776 {5,189}
779 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
780 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
781 bug in the linux kernel (fixed in 2.6.8) */
782 #define SOCKTIMEOUT2 997000 /* 997 seconds */
784 static void test_set_getsockopt(void)
786 SOCKET s;
787 int i, err;
788 int timeout;
789 LINGER lingval;
790 int size;
792 s = socket(AF_INET, SOCK_STREAM, 0);
793 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
794 if( s == INVALID_SOCKET) return;
795 /* SO_RCVTIMEO */
796 timeout = SOCKTIMEOUT1;
797 size = sizeof(timeout);
798 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
799 if( !err)
800 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
801 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
802 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
803 /* SO_SNDTIMEO */
804 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
805 size = sizeof(timeout);
806 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
807 if( !err)
808 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
809 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
810 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
811 /* SO_LINGER */
812 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
813 size = sizeof(lingval);
814 lingval = linger_testvals[i];
815 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
816 if( !err)
817 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
818 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
819 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
820 (lingval.l_linger == linger_testvals[i].l_linger ||
821 (!lingval.l_linger && !linger_testvals[i].l_onoff))
822 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d \n", i,
823 lingval.l_onoff, lingval.l_linger,
824 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
826 closesocket(s);
829 static void test_so_reuseaddr(void)
831 struct sockaddr_in saddr;
832 SOCKET s1,s2;
833 unsigned int rc,reuse;
834 int size;
836 saddr.sin_family = AF_INET;
837 saddr.sin_port = htons(9375);
838 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
840 s1=socket(AF_INET, SOCK_STREAM, 0);
841 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
842 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
843 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
845 s2=socket(AF_INET, SOCK_STREAM, 0);
846 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
848 reuse=0x1234;
849 size=sizeof(reuse);
850 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
851 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
853 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
854 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
856 reuse = 1;
857 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
858 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
860 todo_wine {
861 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
862 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
865 closesocket(s2);
866 closesocket(s1);
869 /************* Array containing the tests to run **********/
871 #define STD_STREAM_SOCKET \
872 SOCK_STREAM, \
873 0, \
874 SERVERIP, \
875 SERVERPORT
877 static test_setup tests [NUM_TESTS] =
879 /* Test 0: synchronous client and server */
882 STD_STREAM_SOCKET,
883 2048,
887 simple_server,
889 NULL,
893 simple_client,
895 NULL,
900 /* Test 1: event-driven client, synchronous server */
903 STD_STREAM_SOCKET,
904 2048,
908 simple_server,
910 NULL,
914 event_client,
916 NULL,
917 WSA_FLAG_OVERLAPPED,
921 /* Test 2: synchronous client, non-blocking server via select() */
924 STD_STREAM_SOCKET,
925 2048,
929 select_server,
931 NULL,
935 simple_client,
937 NULL,
944 static void test_UDP(void)
946 /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
947 possible that this test fails due to dropped packets. */
949 /* peer 0 receives data from all other peers */
950 struct sock_info peer[NUM_UDP_PEERS];
951 char buf[16];
952 int ss, i, n_recv, n_sent;
954 for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
955 ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
957 peer[i].addr.sin_family = AF_INET;
958 peer[i].addr.sin_addr.s_addr = inet_addr ( SERVERIP );
960 if ( i == 0 ) {
961 peer[i].addr.sin_port = htons ( SERVERPORT );
962 } else {
963 peer[i].addr.sin_port = htons ( 0 );
966 do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
968 /* test getsockname() to get peer's port */
969 ss = sizeof ( peer[i].addr );
970 ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
971 ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
974 /* test getsockname() */
975 ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
977 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
978 /* send client's ip */
979 memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
980 n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
981 ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
984 for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
985 n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
986 ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
987 ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
991 static void WINAPI do_getservbyname( HANDLE *starttest )
993 struct {
994 const char *name;
995 const char *proto;
996 int port;
997 } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
999 int i, j;
1000 struct servent *pserv[2];
1002 ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
1004 /* ensure that necessary buffer resizes are completed */
1005 for ( j = 0; j < 2; j++) {
1006 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1009 for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
1010 for ( j = 0; j < 2; j++ ) {
1011 pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
1012 ok ( pserv[j] != NULL, "getservbyname could not retrieve information for %s: %d\n", serv[j].name, WSAGetLastError() );
1013 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) );
1014 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 );
1015 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 );
1018 ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
1022 static void test_getservbyname(void)
1024 int i;
1025 HANDLE starttest, thread[NUM_THREADS];
1026 DWORD thread_id[NUM_THREADS];
1028 starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
1030 /* create threads */
1031 for ( i = 0; i < NUM_THREADS; i++ ) {
1032 thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
1035 /* signal threads to start */
1036 SetEvent ( starttest );
1038 for ( i = 0; i < NUM_THREADS; i++) {
1039 WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
1043 static void test_WSAAddressToStringA(void)
1045 INT ret;
1046 DWORD len;
1047 int GLE;
1048 SOCKADDR_IN sockaddr;
1049 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1051 CHAR expect1[] = "0.0.0.0";
1052 CHAR expect2[] = "255.255.255.255";
1053 CHAR expect3[] = "0.0.0.0:65535";
1054 CHAR expect4[] = "255.255.255.255:65535";
1056 len = 0;
1058 sockaddr.sin_family = AF_INET;
1059 sockaddr.sin_port = 0;
1060 sockaddr.sin_addr.s_addr = 0;
1062 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1063 GLE = WSAGetLastError();
1064 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1065 "WSAAddressToStringA() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1066 GLE, ret );
1068 len = sizeof(address);
1070 sockaddr.sin_family = AF_INET;
1071 sockaddr.sin_port = 0;
1072 sockaddr.sin_addr.s_addr = 0;
1074 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1075 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1077 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1079 len = sizeof(address);
1081 sockaddr.sin_family = AF_INET;
1082 sockaddr.sin_port = 0;
1083 sockaddr.sin_addr.s_addr = 0xffffffff;
1085 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1086 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1088 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1090 len = sizeof(address);
1092 sockaddr.sin_family = AF_INET;
1093 sockaddr.sin_port = 0xffff;
1094 sockaddr.sin_addr.s_addr = 0;
1096 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1097 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1099 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1101 len = sizeof(address);
1103 sockaddr.sin_family = AF_INET;
1104 sockaddr.sin_port = 0xffff;
1105 sockaddr.sin_addr.s_addr = 0xffffffff;
1107 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1108 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1110 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1113 static void test_WSAAddressToStringW(void)
1115 INT ret;
1116 DWORD len;
1117 int GLE;
1118 SOCKADDR_IN sockaddr;
1119 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1121 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1122 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1123 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1124 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1125 '6', '5', '5', '3', '5', 0 };
1127 len = 0;
1129 sockaddr.sin_family = AF_INET;
1130 sockaddr.sin_port = 0;
1131 sockaddr.sin_addr.s_addr = 0;
1133 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1134 GLE = WSAGetLastError();
1135 ok( (ret == SOCKET_ERROR && GLE == WSAEFAULT) || (ret == 0),
1136 "WSAAddressToStringW() failed unexpectedly: WSAGetLastError()=%d, ret=%d\n",
1137 GLE, ret );
1139 len = sizeof(address);
1141 sockaddr.sin_family = AF_INET;
1142 sockaddr.sin_port = 0;
1143 sockaddr.sin_addr.s_addr = 0;
1145 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1146 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1148 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1150 len = sizeof(address);
1152 sockaddr.sin_family = AF_INET;
1153 sockaddr.sin_port = 0;
1154 sockaddr.sin_addr.s_addr = 0xffffffff;
1156 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1157 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1159 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1161 len = sizeof(address);
1163 sockaddr.sin_family = AF_INET;
1164 sockaddr.sin_port = 0xffff;
1165 sockaddr.sin_addr.s_addr = 0;
1167 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1168 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1170 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1172 len = sizeof(address);
1174 sockaddr.sin_family = AF_INET;
1175 sockaddr.sin_port = 0xffff;
1176 sockaddr.sin_addr.s_addr = 0xffffffff;
1178 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1179 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1181 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1184 static void test_WSAStringToAddressA(void)
1186 INT ret, len;
1187 SOCKADDR_IN sockaddr;
1188 int GLE;
1190 CHAR address1[] = "0.0.0.0";
1191 CHAR address2[] = "127.127.127.127";
1192 CHAR address3[] = "255.255.255.255";
1193 CHAR address4[] = "127.127.127.127:65535";
1194 CHAR address5[] = "255.255.255.255:65535";
1196 len = 0;
1197 sockaddr.sin_family = AF_INET;
1199 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1200 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1201 WSAGetLastError() );
1203 len = sizeof(sockaddr);
1204 sockaddr.sin_port = 0;
1205 sockaddr.sin_addr.s_addr = 0;
1207 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1208 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1209 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1211 len = sizeof(sockaddr);
1212 sockaddr.sin_port = 0;
1213 sockaddr.sin_addr.s_addr = 0;
1215 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1216 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1217 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1219 len = sizeof(sockaddr);
1220 sockaddr.sin_port = 0;
1221 sockaddr.sin_addr.s_addr = 0;
1223 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1224 GLE = WSAGetLastError();
1225 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1226 (ret == SOCKET_ERROR && GLE == ERROR_INVALID_PARAMETER),
1227 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1229 len = sizeof(sockaddr);
1230 sockaddr.sin_port = 0;
1231 sockaddr.sin_addr.s_addr = 0;
1233 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1234 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1235 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1237 len = sizeof(sockaddr);
1238 sockaddr.sin_port = 0;
1239 sockaddr.sin_addr.s_addr = 0;
1241 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1242 GLE = WSAGetLastError();
1243 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff && sockaddr.sin_port == 0xffff) ||
1244 (ret == SOCKET_ERROR && GLE == ERROR_INVALID_PARAMETER),
1245 "WSAStringToAddressA() failed unexpectedly: %d\n", GLE );
1248 static void test_WSAStringToAddressW(void)
1250 INT ret, len;
1251 SOCKADDR_IN sockaddr;
1252 int GLE;
1254 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1255 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1256 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1257 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1258 ':', '6', '5', '5', '3', '5', 0 };
1259 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1260 '6', '5', '5', '3', '5', 0 };
1262 len = 0;
1263 sockaddr.sin_family = AF_INET;
1265 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1266 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1267 WSAGetLastError() );
1269 len = sizeof(sockaddr);
1270 sockaddr.sin_port = 0;
1271 sockaddr.sin_addr.s_addr = 0;
1273 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1274 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1275 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1277 len = sizeof(sockaddr);
1278 sockaddr.sin_port = 0;
1279 sockaddr.sin_addr.s_addr = 0;
1281 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1282 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1283 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1285 len = sizeof(sockaddr);
1286 sockaddr.sin_port = 0;
1287 sockaddr.sin_addr.s_addr = 0;
1289 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1290 GLE = WSAGetLastError();
1291 ok( (ret == 0 && sockaddr.sin_addr.s_addr == 0xffffffff) ||
1292 (ret == SOCKET_ERROR && GLE == ERROR_INVALID_PARAMETER),
1293 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1295 len = sizeof(sockaddr);
1296 sockaddr.sin_port = 0;
1297 sockaddr.sin_addr.s_addr = 0;
1299 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1300 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1301 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1303 len = sizeof(sockaddr);
1304 sockaddr.sin_port = 0;
1305 sockaddr.sin_addr.s_addr = 0;
1307 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
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 "WSAStringToAddressW() failed unexpectedly: %d\n", GLE );
1313 /**************** Main program ***************/
1315 START_TEST( sock )
1317 int i;
1318 Init();
1320 test_set_getsockopt();
1321 test_so_reuseaddr();
1323 for (i = 0; i < NUM_TESTS; i++)
1325 trace ( " **** STARTING TEST %d **** \n", i );
1326 do_test ( &tests[i] );
1327 trace ( " **** TEST %d COMPLETE **** \n", i );
1330 test_UDP();
1332 test_getservbyname();
1334 test_WSAAddressToStringA();
1335 test_WSAAddressToStringW();
1337 test_WSAStringToAddressA();
1338 test_WSAStringToAddressW();
1340 Exit();