(file) Return to sock.c CVS log (file) (dir) Up to [RizwankCVS] / wine4 / wine / dlls / winsock / tests

   1 cs130_tom 1.1 /*
   2                * Unit test suite for winsock functions
   3                *
   4                * Copyright 2002 Martin Wilck
   5 cs130_tom 1.3  * select() server Copyright 2005 Thomas Kho
   6 cs130_tom 1.1  *
   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.
  11                *
  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.
  16                *
  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
  20                */
  21               
  22               #include <stdarg.h>
  23               
  24               #include <windef.h>
  25               #include <winbase.h>
  26               #include <winsock2.h>
  27 cs130_tom 1.1 #include <mswsock.h>
  28               #include "wine/test.h"
  29               #include <winnt.h>
  30               #include <winerror.h>
  31               
  32               #define MAX_CLIENTS 4      /* Max number of clients */
  33 cs130_tom 1.2 #define NUM_TESTS   3      /* Number of tests performed */
  34 cs130_tom 1.1 #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 */
  39               
  40               #define wsa_ok(op, cond, msg) \
  41                  do { \
  42                       int tmp, err = 0; \
  43                       tmp = op; \
  44                       if ( !(cond tmp) ) err = WSAGetLastError(); \
  45                       ok ( cond tmp, msg, GetCurrentThreadId(), err); \
  46                  } while (0);
  47               
  48               
  49               /**************** Structs and typedefs ***************/
  50               
  51               typedef struct thread_info
  52               {
  53                   HANDLE thread;
  54                   DWORD id;
  55 cs130_tom 1.1 } thread_info;
  56               
  57               /* Information in the server about open client connections */
  58               typedef struct sock_info
  59               {
  60                   SOCKET                 s;
  61                   struct sockaddr_in     addr;
  62                   struct sockaddr_in     peer;
  63                   char                  *buf;
  64 cs130_tom 1.4     int                    n_recvd;
  65 cs130_tom 1.7     int                    n_sent;
  66 cs130_tom 1.1 } sock_info;
  67               
  68               /* Test parameters for both server & client */
  69               typedef struct test_params
  70               {
  71                   int          sock_type;
  72                   int          sock_prot;
  73                   const char  *inet_addr;
  74                   short        inet_port;
  75                   int          chunk_size;
  76                   int          n_chunks;
  77                   int          n_clients;
  78               } test_params;
  79               
  80               /* server-specific test parameters */
  81               typedef struct server_params
  82               {
  83                   test_params   *general;
  84                   DWORD          sock_flags;
  85                   int            buflen;
  86               } server_params;
  87 cs130_tom 1.1 
  88               /* client-specific test parameters */
  89               typedef struct client_params
  90               {
  91                   test_params   *general;
  92                   DWORD          sock_flags;
  93                   int            buflen;
  94               } client_params;
  95               
  96               /* This type combines all information for setting up a test scenario */
  97               typedef struct test_setup
  98               {
  99                   test_params              general;
 100                   LPVOID                   srv;
 101                   server_params            srv_params;
 102                   LPVOID                   clt;
 103                   client_params            clt_params;
 104               } test_setup;
 105               
 106               /* Thread local storage for server */
 107               typedef struct server_memory
 108 cs130_tom 1.1 {
 109                   SOCKET                  s;
 110                   struct sockaddr_in      addr;
 111                   sock_info               sock[MAX_CLIENTS];
 112               } server_memory;
 113               
 114               /* Thread local storage for client */
 115               typedef struct client_memory
 116               {
 117                   SOCKET s;
 118                   struct sockaddr_in      addr;
 119                   char                   *send_buf;
 120                   char                   *recv_buf;
 121               } client_memory;
 122               
 123               /**************** Static variables ***************/
 124               
 125               static DWORD      tls;              /* Thread local storage index */
 126               static HANDLE     thread[1+MAX_CLIENTS];
 127               static DWORD      thread_id[1+MAX_CLIENTS];
 128               static HANDLE     server_ready;
 129 cs130_tom 1.1 static HANDLE     client_ready[MAX_CLIENTS];
 130               static int        client_id;
 131               
 132               /**************** General utility functions ***************/
 133               
 134               static void set_so_opentype ( BOOL overlapped )
 135               {
 136                   int optval = !overlapped, newval, len = sizeof (int);
 137               
 138                   ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
 139                                     (LPVOID) &optval, sizeof (optval) ) == 0,
 140                        "setting SO_OPENTYPE failed\n" );
 141                   ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
 142                                     (LPVOID) &newval, &len ) == 0,
 143                        "getting SO_OPENTYPE failed\n" );
 144                   ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
 145               }
 146               
 147               static int set_blocking ( SOCKET s, BOOL blocking )
 148               {
 149                   u_long val = !blocking;
 150 cs130_tom 1.1     return ioctlsocket ( s, FIONBIO, &val );
 151               }
 152               
 153               static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
 154               {
 155                   char c, *p;
 156                   for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
 157                       memset ( p, c, chunk_size );
 158               }
 159               
 160               static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
 161               {
 162                   char c, *p;
 163                   int i;
 164                   for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
 165                   {
 166                       for ( i = 0; i < chunk_size; i++ )
 167                           if ( p[i] != c ) return p + i;
 168                   }
 169                   return NULL;
 170               }
 171 cs130_tom 1.1 
 172               /*
 173                * This routine is called when a client / server does not expect any more data,
 174                * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
 175                */
 176               static void read_zero_bytes ( SOCKET s )
 177               {
 178                   char buf[256];
 179                   int tmp, n = 0;
 180                   while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
 181                       n += tmp;
 182                   ok ( n <= 0, "garbage data received: %d bytes\n", n );
 183               }
 184               
 185               static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
 186               {
 187                   char* last = buf + buflen, *p;
 188                   int n = 1;
 189                   for ( p = buf; n > 0 && p < last; p += n )
 190                       n = send ( s, p, min ( sendlen, last - p ), 0 );
 191                   wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" );
 192 cs130_tom 1.1     return p - buf;
 193               }
 194               
 195               static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
 196               {
 197                   char* last = buf + buflen, *p;
 198                   int n = 1;
 199                   for ( p = buf; n > 0 && p < last; p += n )
 200                       n = recv ( s, p, min ( recvlen, last - p ), 0 );
 201                   wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" );
 202                   return p - buf;
 203               }
 204               
 205 cs130_tom 1.4 static int do_nonblocking_send ( int id, int first_time, SOCKET s, char *buf, int buflen, int sendlen )
 206 cs130_tom 1.2 {
 207 cs130_tom 1.4     char *last = buf + buflen;
 208                   static char *p[MAX_CLIENTS];
 209 cs130_tom 1.2     int n;
 210 cs130_tom 1.3 
 211 cs130_tom 1.4     if ( first_time ) {
 212 cs130_tom 1.2         p[id] = buf;
 213                   }
 214                   
 215                   n = send ( s, p[id], min ( sendlen, last - p[id] ), 0 );
 216                   p[id] += n;
 217                   
 218 cs130_tom 1.4     return p[id] - buf;
 219 cs130_tom 1.2 }
 220               
 221 cs130_tom 1.4 static int do_nonblocking_recv ( int id, int first_time, SOCKET s, char *buf, int buflen, int recvlen )
 222 cs130_tom 1.2 {
 223 cs130_tom 1.4     char *last = buf + buflen;
 224                   static char *p[MAX_CLIENTS];
 225 cs130_tom 1.2     int n;
 226               
 227 cs130_tom 1.4     if ( first_time ) {
 228 cs130_tom 1.2         p[id] = buf;
 229                   }
 230               
 231                   n = recv ( s, p[id], min ( recvlen, last - p[id] ), 0 );
 232                   p[id] += n;
 233               
 234 cs130_tom 1.4     return p[id] - buf;
 235 cs130_tom 1.2 }
 236               
 237 cs130_tom 1.1 /*
 238                *  Call this routine right after thread startup.
 239                *  SO_OPENTYPE must by 0, regardless what the server did.
 240                */
 241               static void check_so_opentype (void)
 242               {
 243                   int tmp = 1, len;
 244                   len = sizeof (tmp);
 245                   getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
 246                   ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
 247               }
 248               
 249               /**************** Server utility functions ***************/
 250               
 251               /*
 252                *  Even if we have closed our server socket cleanly,
 253                *  the OS may mark the address "in use" for some time -
 254                *  this happens with native Linux apps, too.
 255                */
 256               static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
 257               {
 258 cs130_tom 1.1     int err, wsaerr = 0, n_try = BIND_TRIES;
 259               
 260                   while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
 261                           ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
 262                           n_try-- >= 0)
 263                   {
 264                       trace ( "address in use, waiting ...\n" );
 265                       Sleep ( 1000 * BIND_SLEEP );
 266                   }
 267                   ok ( err == 0, "failed to bind: %d\n", wsaerr );
 268               }
 269               
 270               static void server_start ( server_params *par )
 271               {
 272                   int i;
 273                   test_params *gen = par->general;
 274                   server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
 275               
 276                   TlsSetValue ( tls, mem );
 277                   mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
 278                                         NULL, 0, par->sock_flags );
 279 cs130_tom 1.1     ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
 280               
 281                   mem->addr.sin_family = AF_INET;
 282                   mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
 283                   mem->addr.sin_port = htons ( gen->inet_port );
 284               
 285                   for (i = 0; i < MAX_CLIENTS; i++)
 286                   {
 287                       mem->sock[i].s = INVALID_SOCKET;
 288                       mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
 289 cs130_tom 1.4         mem->sock[i].n_recvd = 0;
 290 cs130_tom 1.6         mem->sock[i].n_sent = 0;
 291 cs130_tom 1.1     }
 292               
 293                   if ( gen->sock_type == SOCK_STREAM )
 294                       do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
 295               }
 296               
 297               static void server_stop (void)
 298               {
 299                   int i;
 300                   server_memory *mem = TlsGetValue ( tls );
 301               
 302                   for (i = 0; i < MAX_CLIENTS; i++ )
 303                   {
 304                       LocalFree ( (HANDLE) mem->sock[i].buf );
 305                       if ( mem->sock[i].s != INVALID_SOCKET )
 306                           closesocket ( mem->sock[i].s );
 307                   }
 308                   ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
 309                   LocalFree ( (HANDLE) mem );
 310                   ExitThread ( GetCurrentThreadId () );
 311               }
 312 cs130_tom 1.1 
 313               /**************** Client utilitiy functions ***************/
 314               
 315               static void client_start ( client_params *par )
 316               {
 317                   test_params *gen = par->general;
 318                   client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
 319               
 320                   TlsSetValue ( tls, mem );
 321               
 322                   WaitForSingleObject ( server_ready, INFINITE );
 323               
 324                   mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
 325                                         NULL, 0, par->sock_flags );
 326               
 327                   mem->addr.sin_family = AF_INET;
 328                   mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
 329                   mem->addr.sin_port = htons ( gen->inet_port );
 330               
 331                   ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
 332               
 333 cs130_tom 1.1     mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
 334                   mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
 335                   fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
 336               
 337                   SetEvent ( client_ready[client_id] );
 338                   /* Wait for the other clients to come up */
 339                   WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
 340               }
 341               
 342               static void client_stop (void)
 343               {
 344                   client_memory *mem = TlsGetValue ( tls );
 345                   wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%lx): %d\n" );
 346                   LocalFree ( (HANDLE) mem->send_buf );
 347                   LocalFree ( (HANDLE) mem );
 348                   ExitThread(0);
 349               }
 350               
 351               /**************** Servers ***************/
 352               
 353               /*
 354 cs130_tom 1.1  * simple_server: A very basic server doing synchronous IO.
 355                */
 356               static VOID WINAPI simple_server ( server_params *par )
 357               {
 358                   test_params *gen = par->general;
 359                   server_memory *mem;
 360                   int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
 361                       id = GetCurrentThreadId();
 362                   char *p;
 363               
 364 cs130_tom 1.4 		trace ( "simple_server (%x) starting\n", id );
 365 cs130_tom 1.1 
 366                   set_so_opentype ( FALSE ); /* non-overlapped */
 367                   server_start ( par );
 368                   mem = TlsGetValue ( tls );
 369               
 370                   wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%lx): failed to set blocking mode: %d\n");
 371                   wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%lx): listen failed: %d\n");
 372               
 373 cs130_tom 1.4 		trace ( "simple_server (%x) ready\n", id );
 374 cs130_tom 1.1     SetEvent ( server_ready ); /* notify clients */
 375               
 376                   for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
 377                   {
 378 cs130_tom 1.4 				trace ( "simple_server (%x): waiting for client\n", id );
 379 cs130_tom 1.1 
 380                       /* accept a single connection */
 381                       tmp = sizeof ( mem->sock[0].peer );
 382                       mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
 383                       wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%lx): accept failed: %d\n" );
 384               
 385                       ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
 386                            "simple_server (%x): strange peer address\n", id );
 387               
 388                       /* Receive data & check it */
 389                       n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
 390                       ok ( n_recvd == n_expected,
 391                            "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
 392                       p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
 393                       ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
 394               
 395                       /* Echo data back */
 396                       n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
 397                       ok ( n_sent == n_expected,
 398                            "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
 399               
 400 cs130_tom 1.1         /* cleanup */
 401                       read_zero_bytes ( mem->sock[0].s );
 402                       wsa_ok ( closesocket ( mem->sock[0].s ),  0 ==, "simple_server (%lx): closesocket error: %d\n" );
 403                       mem->sock[0].s = INVALID_SOCKET;
 404                   }
 405               
 406 cs130_tom 1.4 		trace ( "simple_server (%x) exiting\n", id );
 407 cs130_tom 1.1     server_stop ();
 408               }
 409               
 410 cs130_tom 1.2 /*
 411                * select_server: A non-blocking server.
 412                */
 413               static VOID WINAPI select_server ( server_params *par )
 414               {
 415                   test_params *gen = par->general;
 416                   server_memory *mem;
 417 cs130_tom 1.4     int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
 418                       id = GetCurrentThreadId(), n_connections = 0;
 419 cs130_tom 1.2     char *p;
 420                   struct timeval zerotime = {0,0};
 421 cs130_tom 1.3     fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
 422 cs130_tom 1.2 
 423 cs130_tom 1.5     trace ( "select_server (%x) starting\n", id );
 424 cs130_tom 1.2 
 425                   set_so_opentype ( FALSE ); /* non-overlapped */
 426                   server_start ( par );
 427                   mem = TlsGetValue ( tls );
 428               
 429                   wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%lx): failed to set blocking mode: %d\n");
 430                   wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%lx): listen failed: %d\n");
 431               
 432 cs130_tom 1.5     trace ( "select_server (%x) ready\n", id );
 433 cs130_tom 1.2     SetEvent ( server_ready ); /* notify clients */
 434               
 435 cs130_tom 1.3     FD_ZERO ( &fds_openrecv );
 436                   FD_ZERO ( &fds_recv );
 437                   FD_ZERO ( &fds_send );
 438                   FD_ZERO ( &fds_opensend );
 439 cs130_tom 1.5 
 440 cs130_tom 1.3     FD_SET ( mem->s, &fds_openrecv );
 441 cs130_tom 1.2 
 442                   while(1)
 443                   {
 444 cs130_tom 1.3         fds_recv = fds_openrecv;
 445                       fds_send = fds_opensend;
 446 cs130_tom 1.2 
 447 cs130_tom 1.3         select ( 0, &fds_recv, &fds_send, NULL, &zerotime );
 448 cs130_tom 1.2 
 449                       /* check for incoming requests */
 450 cs130_tom 1.3         if ( FD_ISSET ( mem->s, &fds_recv ) ) {
 451 cs130_tom 1.2             trace ( "select_server (%x): accepting client connection\n", id );
 452               
 453                           /* accept a single connection */
 454                           tmp = sizeof ( mem->sock[i].peer );
 455                           mem->sock[i].s = accept ( mem->s, (struct sockaddr*) &mem->sock[i].peer, &tmp );
 456                           wsa_ok ( mem->sock[i].s, INVALID_SOCKET !=, "select_server (%lx): accept failed: %d\n" );
 457               
 458                           ok ( mem->sock[i].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
 459                               "select_server (%x): strange peer address\n", id );
 460               
 461                           /* add to list of open connections */
 462 cs130_tom 1.3             FD_SET ( mem->sock[i].s, &fds_openrecv );
 463                           FD_SET ( mem->sock[i].s, &fds_opensend );
 464 cs130_tom 1.2 
 465 cs130_tom 1.3             n_connections++;
 466 cs130_tom 1.2         }
 467               
 468                       /* handle open requests */
 469               
 470                       for ( i = 0; i < n_connections; i++ )
 471                       {
 472 cs130_tom 1.4             if ( (mem->sock[i].n_recvd < n_expected)
 473 cs130_tom 1.3                 && FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
 474 cs130_tom 1.2 
 475                               /* Receive data & check it */
 476 cs130_tom 1.4                 mem->sock[i].n_recvd = do_nonblocking_recv ( i, (mem->sock[i].n_recvd == 0), mem->sock[i].s, mem->sock[i].buf, n_expected, par->buflen );
 477 cs130_tom 1.3 
 478 cs130_tom 1.4                 if ( mem->sock[i].n_recvd == n_expected ) {
 479 cs130_tom 1.3                     p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
 480                                   ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf);
 481                                   FD_CLR ( mem->sock[i].s, &fds_openrecv );
 482 cs130_tom 1.2                 }
 483               
 484 cs130_tom 1.3             } else if ( !FD_ISSET ( mem->sock[i].s, &fds_openrecv ) 
 485                               && FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
 486 cs130_tom 1.5 
 487 cs130_tom 1.2                 /* Echo data back */
 488 cs130_tom 1.4                 mem->sock[i].n_sent = do_nonblocking_send ( i, (mem->sock[i].n_sent == 0), mem->sock[i].s, mem->sock[i].buf, n_expected, par->buflen );
 489 cs130_tom 1.2 
 490 cs130_tom 1.4                 if ( mem->sock[i].n_sent == n_expected ) {
 491 cs130_tom 1.3                     FD_CLR ( mem->sock[i].s, &fds_opensend );
 492                               }
 493                           } else if( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
 494                               ok ( 0, "select_server (%x): too many bytes read\n", id );
 495                           }
 496 cs130_tom 1.4         }
 497 cs130_tom 1.2 
 498 cs130_tom 1.4         /* check if all clients are done */
 499                       if ( ( fds_opensend.fd_count == 0 ) 
 500                           && ( n_connections  == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
 501 cs130_tom 1.5             break;
 502 cs130_tom 1.2         }
 503                   }
 504               
 505                   for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
 506                   {
 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;
 511                   }
 512               
 513                   trace ( "select_server (%x) exiting\n", id );
 514                   server_stop ();
 515               }
 516               
 517 cs130_tom 1.1 /**************** Clients ***************/
 518               
 519               /*
 520                * simple_client: A very basic client doing synchronous IO.
 521                */
 522               static VOID WINAPI simple_client ( client_params *par )
 523               {
 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;
 528               
 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 );
 534               
 535                   check_so_opentype ();
 536                   set_so_opentype ( FALSE ); /* non-overlapped */
 537                   client_start ( par );
 538 cs130_tom 1.1     mem = TlsGetValue ( tls );
 539               
 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 );
 546               
 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 );
 551               
 552                   /* shutdown send direction */
 553                   wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
 554               
 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 );
 559 cs130_tom 1.1 
 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);
 563               
 564                   /* cleanup */
 565                   read_zero_bytes ( mem->s );
 566                   trace ( "simple_client (%x) exiting\n", id );
 567                   client_stop ();
 568               }
 569               
 570               /*
 571                * event_client: An event-driven client
 572                */
 573               static void WINAPI event_client ( client_params *par )
 574               {
 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 cs130_tom 1.1     WSANETWORKEVENTS wsa_events;
 581                   char *send_last, *recv_last, *send_p, *recv_p;
 582                   long mask = FD_READ | FD_WRITE | FD_CLOSE;
 583               
 584                   trace ( "event_client (%x): starting\n", id );
 585                   client_start ( par );
 586                   trace ( "event_client (%x): server ready\n", id );
 587               
 588                   mem = TlsGetValue ( tls );
 589               
 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 );
 596               
 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" );
 601 cs130_tom 1.1 
 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;
 605               
 606                   trace ( "event_client (%x) connected\n", id );
 607               
 608                   WSAEventSelect ( mem->s, event, mask );
 609               
 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;
 614               
 615                   while ( TRUE )
 616                   {
 617                       err = WaitForSingleObject ( event, INFINITE );
 618                       ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
 619               
 620                       err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 621                       wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 622 cs130_tom 1.1 
 623                       if ( wsa_events.lNetworkEvents & FD_WRITE )
 624                       {
 625                           err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
 626                           ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
 627               
 628                           if ( err== 0 )
 629                               do
 630                               {
 631                                   n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
 632                                   if ( n < 0 )
 633                                   {
 634                                       err = WSAGetLastError ();
 635                                       ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
 636                                   }
 637                                   else
 638                                       send_p += n;
 639                               }
 640                               while ( n >= 0 && send_p < send_last );
 641               
 642                           if ( send_p == send_last )
 643 cs130_tom 1.1             {
 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 );
 648                           }
 649                       }
 650                       if ( wsa_events.lNetworkEvents & FD_READ )
 651                       {
 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;
 655                           
 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" );
 659               
 660                           while ( n >= 0 ) {
 661                               recv_p += n;
 662                               if ( recv_p == recv_last )
 663                               {
 664 cs130_tom 1.1                     mask &= ~FD_READ;
 665                                   trace ( "event_client (%x): all data received\n", id );
 666                                   WSAEventSelect ( mem->s, event, mask );
 667                                   break;
 668                               }
 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 );
 672                               
 673                           }
 674                       }   
 675                       if ( wsa_events.lNetworkEvents & FD_CLOSE )
 676                       {
 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;
 681                       }
 682                   }
 683               
 684                   ok ( send_p == send_last,
 685 cs130_tom 1.1          "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);
 692               
 693               out:
 694                   WSACloseEvent ( event );
 695                   trace ( "event_client (%x) exiting\n", id );
 696                   client_stop ();
 697               }
 698               
 699               /**************** Main program utility functions ***************/
 700               
 701               static void Init (void)
 702               {
 703                   WORD ver = MAKEWORD (2, 2);
 704                   WSADATA data;
 705               
 706 cs130_tom 1.1     ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
 707                   tls = TlsAlloc();
 708               }
 709               
 710               static void Exit (void)
 711               {
 712                   TlsFree ( tls );
 713                   ok ( WSACleanup() == 0, "WSACleanup failed\n" );
 714               }
 715               
 716               static void StartServer (LPTHREAD_START_ROUTINE routine,
 717                                        test_params *general, server_params *par)
 718               {
 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" );
 722               }
 723               
 724               static void StartClients (LPTHREAD_START_ROUTINE routine,
 725                                         test_params *general, client_params *par)
 726               {
 727 cs130_tom 1.1     int i;
 728                   par->general = general;
 729                   for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
 730                   {
 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 );
 736                   };
 737               }
 738               
 739               static void do_test( test_setup *test )
 740               {
 741                   DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
 742                   DWORD wait;
 743               
 744                   server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
 745                   for (i = 0; i <= n; i++)
 746                       client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
 747               
 748 cs130_tom 1.1     StartServer ( test->srv, &test->general, &test->srv_params );
 749                   StartClients ( test->clt, &test->general, &test->clt_params );
 750                   WaitForSingleObject ( server_ready, INFINITE );
 751               
 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 );
 755               
 756                   if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
 757                   {
 758                       for (i = 0; i <= n; i++)
 759                       {
 760                           trace ("terminating thread %08lx\n", thread_id[i]);
 761                           if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
 762                               TerminateThread ( thread [i], 0 );
 763                       }
 764                   }
 765                   CloseHandle ( server_ready );
 766                   for (i = 0; i <= n; i++)
 767                       CloseHandle ( client_ready[i] );
 768               }
 769 cs130_tom 1.1 
 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}
 777               };
 778               
 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 */
 783               
 784               static void test_set_getsockopt()
 785               {
 786                   SOCKET s;
 787                   int i, err;
 788                   int timeout;
 789                   LINGER lingval;
 790 cs130_tom 1.1     int size;
 791               
 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 cs130_tom 1.1     /* 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);
 825                   }
 826                   closesocket(s);
 827               }
 828               
 829               static void test_so_reuseaddr()
 830               {
 831                   struct sockaddr_in saddr;
 832 cs130_tom 1.1     SOCKET s1,s2;
 833                   unsigned int rc,reuse,size;
 834               
 835                   saddr.sin_family      = AF_INET;
 836                   saddr.sin_port        = htons(9375);
 837                   saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 838               
 839                   s1=socket(AF_INET, SOCK_STREAM, 0);
 840                   ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 841                   rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
 842                   ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
 843               
 844                   s2=socket(AF_INET, SOCK_STREAM, 0);
 845                   ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 846               
 847                   reuse=0x1234;
 848                   size=sizeof(reuse);
 849                   rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
 850                   ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
 851               
 852                   rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
 853 cs130_tom 1.1     ok(rc==SOCKET_ERROR, "bind() succeeded\n");
 854               
 855                   reuse = 1;
 856                   rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
 857                   ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
 858               
 859                   todo_wine {
 860                   rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
 861                   ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
 862                   }
 863               
 864                   closesocket(s2);
 865                   closesocket(s1);
 866               }
 867               
 868               /************* Array containing the tests to run **********/
 869               
 870               #define STD_STREAM_SOCKET \
 871                           SOCK_STREAM, \
 872                           0, \
 873                           "127.0.0.1", \
 874 cs130_tom 1.1             9374
 875               
 876               static test_setup tests [NUM_TESTS] =
 877               {
 878                   /* Test 0: synchronous client and server */
 879                   {
 880                       {
 881                           STD_STREAM_SOCKET,
 882                           2048,
 883                           16,
 884                           2
 885                       },
 886                       simple_server,
 887                       {
 888                           NULL,
 889                           0,
 890                           64
 891                       },
 892                       simple_client,
 893                       {
 894                           NULL,
 895 cs130_tom 1.1             0,
 896                           128
 897                       }
 898                   },
 899                   /* Test 1: event-driven client, synchronous server */
 900                   {
 901                       {
 902                           STD_STREAM_SOCKET,
 903                           2048,
 904                           16,
 905                           2
 906                       },
 907                       simple_server,
 908 cs130_tom 1.2         {
 909                           NULL,
 910                           0,
 911                           64
 912                       },
 913                       event_client,
 914                       {
 915                           NULL,
 916                           WSA_FLAG_OVERLAPPED,
 917                           128
 918                       }
 919                   },
 920 cs130_tom 1.6     /* Test 2: synchronous client, non-blocking server via select() */
 921 cs130_tom 1.2     {
 922                       {
 923                           STD_STREAM_SOCKET,
 924                           2048,
 925                           16,
 926                           2
 927                       },
 928                       select_server,
 929 cs130_tom 1.1         {
 930                           NULL,
 931                           0,
 932                           64
 933                       },
 934 cs130_tom 1.3         simple_client,
 935 cs130_tom 1.1         {
 936                           NULL,
 937 cs130_tom 1.3             0,
 938 cs130_tom 1.1             128
 939                       }
 940                   }
 941               };
 942               
 943               static void test_WSAAddressToStringA()
 944               {
 945                   INT ret;
 946                   DWORD len;
 947                   SOCKADDR_IN sockaddr;
 948                   CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
 949               
 950                   CHAR expect1[] = "0.0.0.0";
 951                   CHAR expect2[] = "255.255.255.255";
 952                   CHAR expect3[] = "0.0.0.0:65535";
 953                   CHAR expect4[] = "255.255.255.255:65535";
 954               
 955                   len = 0;
 956               
 957                   sockaddr.sin_family = AF_INET;
 958                   sockaddr.sin_port = 0;
 959 cs130_tom 1.1     sockaddr.sin_addr.s_addr = 0;
 960               
 961                   ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 962                   ok( ret == SOCKET_ERROR, "WSAAddressToStringA() succeeded unexpectedly: %d\n", WSAGetLastError() );
 963               
 964                   len = sizeof(address);
 965               
 966                   sockaddr.sin_family = AF_INET;
 967                   sockaddr.sin_port = 0;
 968                   sockaddr.sin_addr.s_addr = 0;
 969               
 970                   ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 971                   ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 972               
 973                   ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
 974               
 975                   len = sizeof(address);
 976               
 977                   sockaddr.sin_family = AF_INET;
 978                   sockaddr.sin_port = 0;
 979                   sockaddr.sin_addr.s_addr = 0xffffffff;
 980 cs130_tom 1.1 
 981                   ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 982                   ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 983               
 984                   ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
 985               
 986                   len = sizeof(address);
 987               
 988                   sockaddr.sin_family = AF_INET;
 989                   sockaddr.sin_port = 0xffff;
 990                   sockaddr.sin_addr.s_addr = 0;
 991               
 992                   ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 993                   ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 994               
 995                   ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
 996               
 997                   len = sizeof(address);
 998               
 999                   sockaddr.sin_family = AF_INET;
1000                   sockaddr.sin_port = 0xffff;
1001 cs130_tom 1.1     sockaddr.sin_addr.s_addr = 0xffffffff;
1002               
1003                   ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1004                   ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1005               
1006                   ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1007               }
1008               
1009               static void test_WSAAddressToStringW()
1010               {
1011                   INT ret;
1012                   DWORD len;
1013                   SOCKADDR_IN sockaddr;
1014                   WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1015               
1016                   WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1017                   WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1018                   WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1019                   WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1020                                       '6', '5', '5', '3', '5', 0 };
1021               
1022 cs130_tom 1.1     len = 0;
1023               
1024                   sockaddr.sin_family = AF_INET;
1025                   sockaddr.sin_port = 0;
1026                   sockaddr.sin_addr.s_addr = 0;
1027               
1028                   ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1029                   ok( ret == SOCKET_ERROR, "WSAAddressToStringW() succeeded unexpectedly: %x\n", WSAGetLastError() );
1030               
1031                   len = sizeof(address);
1032               
1033                   sockaddr.sin_family = AF_INET;
1034                   sockaddr.sin_port = 0;
1035                   sockaddr.sin_addr.s_addr = 0;
1036               
1037                   ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1038                   ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1039               
1040                   ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1041               
1042                   len = sizeof(address);
1043 cs130_tom 1.1 
1044                   sockaddr.sin_family = AF_INET;
1045                   sockaddr.sin_port = 0;
1046                   sockaddr.sin_addr.s_addr = 0xffffffff;
1047               
1048                   ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1049                   ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1050               
1051                   ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1052               
1053                   len = sizeof(address);
1054               
1055                   sockaddr.sin_family = AF_INET;
1056                   sockaddr.sin_port = 0xffff;
1057                   sockaddr.sin_addr.s_addr = 0;
1058               
1059                   ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1060                   ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1061               
1062                   ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1063               
1064 cs130_tom 1.1     len = sizeof(address);
1065               
1066                   sockaddr.sin_family = AF_INET;
1067                   sockaddr.sin_port = 0xffff;
1068                   sockaddr.sin_addr.s_addr = 0xffffffff;
1069               
1070                   ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1071                   ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1072               
1073                   ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1074               }
1075               
1076               static void test_WSAStringToAddressA()
1077               {
1078                   INT ret, len;
1079                   SOCKADDR_IN sockaddr;
1080               
1081                   CHAR address1[] = "0.0.0.0";
1082                   CHAR address2[] = "127.127.127.127";
1083                   CHAR address3[] = "255.255.255.255";
1084                   CHAR address4[] = "127.127.127.127:65535";
1085 cs130_tom 1.1     CHAR address5[] = "255.255.255.255:65535";
1086               
1087                   len = 0;
1088                   sockaddr.sin_family = AF_INET;
1089               
1090                   ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1091                   ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1092                       WSAGetLastError() );
1093               
1094                   len = sizeof(sockaddr);
1095                   sockaddr.sin_port = 0;
1096                   sockaddr.sin_addr.s_addr = 0;
1097               
1098                   ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1099                   ok( !ret && sockaddr.sin_addr.s_addr == 0,
1100                       "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1101               
1102                   len = sizeof(sockaddr);
1103                   sockaddr.sin_port = 0;
1104                   sockaddr.sin_addr.s_addr = 0;
1105               
1106 cs130_tom 1.1     ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1107                   ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1108                       "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1109               
1110                   len = sizeof(sockaddr);
1111               
1112                   ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1113                   ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1114               
1115                   len = sizeof(sockaddr);
1116                   sockaddr.sin_port = 0;
1117                   sockaddr.sin_addr.s_addr = 0;
1118               
1119                   ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1120                   ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1121                       "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1122               
1123                   len = sizeof(sockaddr);
1124               
1125                   ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1126                   ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1127 cs130_tom 1.1 }
1128               
1129               static void test_WSAStringToAddressW()
1130               {
1131                   INT ret, len;
1132                   SOCKADDR_IN sockaddr;
1133               
1134                   WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1135                   WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1136                   WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1137                   WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1138                                        ':', '6', '5', '5', '3', '5', 0 };
1139                   WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1140                                        '6', '5', '5', '3', '5', 0 };
1141               
1142                   len = 0;
1143                   sockaddr.sin_family = AF_INET;
1144               
1145                   ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1146                   ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1147                       WSAGetLastError() );
1148 cs130_tom 1.1 
1149                   len = sizeof(sockaddr);
1150                   sockaddr.sin_port = 0;
1151                   sockaddr.sin_addr.s_addr = 0;
1152               
1153                   ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1154                   ok( !ret && sockaddr.sin_addr.s_addr == 0,
1155                       "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1156               
1157                   len = sizeof(sockaddr);
1158               
1159                   sockaddr.sin_port = 0;
1160                   sockaddr.sin_addr.s_addr = 0;
1161               
1162                   ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1163                   ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1164                       "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1165               
1166                   len = sizeof(sockaddr);
1167               
1168                   ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1169 cs130_tom 1.1     ok( ret, "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1170               
1171                   len = sizeof(sockaddr);
1172                   sockaddr.sin_port = 0;
1173                   sockaddr.sin_addr.s_addr = 0;
1174               
1175                   ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1176                   ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1177                       "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1178               
1179                   len = sizeof(sockaddr);
1180               
1181                   ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1182                   ok( ret, "WSAStringToAddressW() succeeded unexpectedly: %d\n", WSAGetLastError() );
1183               }
1184               
1185               /**************** Main program  ***************/
1186               
1187               START_TEST( sock )
1188               {
1189                   int i;
1190 cs130_tom 1.1     Init();
1191               
1192                   test_set_getsockopt();
1193                   test_so_reuseaddr();
1194               
1195                   for (i = 0; i < NUM_TESTS; i++)
1196                   {
1197                       trace ( " **** STARTING TEST %d **** \n", i );
1198                       do_test (  &tests[i] );
1199                       trace ( " **** TEST %d COMPLETE **** \n", i );
1200                   }
1201               
1202                   test_WSAAddressToStringA();
1203                   test_WSAAddressToStringW();
1204               
1205                   test_WSAStringToAddressA();
1206                   test_WSAStringToAddressW();
1207               
1208                   Exit();
1209               }

Rizwan Kassim
Powered by
ViewCVS 0.9.2