(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               /*
 206                *  Call this routine right after thread startup.
 207                *  SO_OPENTYPE must by 0, regardless what the server did.
 208                */
 209               static void check_so_opentype (void)
 210               {
 211                   int tmp = 1, len;
 212                   len = sizeof (tmp);
 213 cs130_tom 1.1     getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
 214                   ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
 215               }
 216               
 217               /**************** Server utility functions ***************/
 218               
 219               /*
 220                *  Even if we have closed our server socket cleanly,
 221                *  the OS may mark the address "in use" for some time -
 222                *  this happens with native Linux apps, too.
 223                */
 224               static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
 225               {
 226                   int err, wsaerr = 0, n_try = BIND_TRIES;
 227               
 228                   while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
 229                           ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
 230                           n_try-- >= 0)
 231                   {
 232                       trace ( "address in use, waiting ...\n" );
 233                       Sleep ( 1000 * BIND_SLEEP );
 234 cs130_tom 1.1     }
 235                   ok ( err == 0, "failed to bind: %d\n", wsaerr );
 236               }
 237               
 238               static void server_start ( server_params *par )
 239               {
 240                   int i;
 241                   test_params *gen = par->general;
 242                   server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
 243               
 244                   TlsSetValue ( tls, mem );
 245                   mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
 246                                         NULL, 0, par->sock_flags );
 247                   ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
 248               
 249                   mem->addr.sin_family = AF_INET;
 250                   mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
 251                   mem->addr.sin_port = htons ( gen->inet_port );
 252               
 253                   for (i = 0; i < MAX_CLIENTS; i++)
 254                   {
 255 cs130_tom 1.1         mem->sock[i].s = INVALID_SOCKET;
 256                       mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
 257 cs130_tom 1.4         mem->sock[i].n_recvd = 0;
 258 cs130_tom 1.6         mem->sock[i].n_sent = 0;
 259 cs130_tom 1.1     }
 260               
 261                   if ( gen->sock_type == SOCK_STREAM )
 262                       do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
 263               }
 264               
 265               static void server_stop (void)
 266               {
 267                   int i;
 268                   server_memory *mem = TlsGetValue ( tls );
 269               
 270                   for (i = 0; i < MAX_CLIENTS; i++ )
 271                   {
 272                       LocalFree ( (HANDLE) mem->sock[i].buf );
 273                       if ( mem->sock[i].s != INVALID_SOCKET )
 274                           closesocket ( mem->sock[i].s );
 275                   }
 276                   ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
 277                   LocalFree ( (HANDLE) mem );
 278                   ExitThread ( GetCurrentThreadId () );
 279               }
 280 cs130_tom 1.1 
 281               /**************** Client utilitiy functions ***************/
 282               
 283               static void client_start ( client_params *par )
 284               {
 285                   test_params *gen = par->general;
 286                   client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
 287               
 288                   TlsSetValue ( tls, mem );
 289               
 290                   WaitForSingleObject ( server_ready, INFINITE );
 291               
 292                   mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
 293                                         NULL, 0, par->sock_flags );
 294               
 295                   mem->addr.sin_family = AF_INET;
 296                   mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
 297                   mem->addr.sin_port = htons ( gen->inet_port );
 298               
 299                   ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
 300               
 301 cs130_tom 1.1     mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
 302                   mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
 303                   fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
 304               
 305                   SetEvent ( client_ready[client_id] );
 306                   /* Wait for the other clients to come up */
 307                   WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
 308               }
 309               
 310               static void client_stop (void)
 311               {
 312                   client_memory *mem = TlsGetValue ( tls );
 313                   wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%lx): %d\n" );
 314                   LocalFree ( (HANDLE) mem->send_buf );
 315                   LocalFree ( (HANDLE) mem );
 316                   ExitThread(0);
 317               }
 318               
 319               /**************** Servers ***************/
 320               
 321               /*
 322 cs130_tom 1.1  * simple_server: A very basic server doing synchronous IO.
 323                */
 324               static VOID WINAPI simple_server ( server_params *par )
 325               {
 326                   test_params *gen = par->general;
 327                   server_memory *mem;
 328                   int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
 329                       id = GetCurrentThreadId();
 330                   char *p;
 331               
 332 cs130_tom 1.14     trace ( "simple_server (%x) starting\n", id );
 333 cs130_tom 1.1  
 334                    set_so_opentype ( FALSE ); /* non-overlapped */
 335                    server_start ( par );
 336                    mem = TlsGetValue ( tls );
 337                
 338                    wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%lx): failed to set blocking mode: %d\n");
 339                    wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%lx): listen failed: %d\n");
 340                
 341 cs130_tom 1.14     trace ( "simple_server (%x) ready\n", id );
 342 cs130_tom 1.1      SetEvent ( server_ready ); /* notify clients */
 343                
 344                    for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
 345                    {
 346 cs130_tom 1.14         trace ( "simple_server (%x): waiting for client\n", id );
 347 cs130_tom 1.1  
 348                        /* accept a single connection */
 349                        tmp = sizeof ( mem->sock[0].peer );
 350                        mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
 351                        wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%lx): accept failed: %d\n" );
 352                
 353                        ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
 354                             "simple_server (%x): strange peer address\n", id );
 355                
 356                        /* Receive data & check it */
 357                        n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
 358                        ok ( n_recvd == n_expected,
 359                             "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
 360                        p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
 361                        ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
 362                
 363                        /* Echo data back */
 364                        n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
 365                        ok ( n_sent == n_expected,
 366                             "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
 367                
 368 cs130_tom 1.1          /* cleanup */
 369                        read_zero_bytes ( mem->sock[0].s );
 370                        wsa_ok ( closesocket ( mem->sock[0].s ),  0 ==, "simple_server (%lx): closesocket error: %d\n" );
 371                        mem->sock[0].s = INVALID_SOCKET;
 372                    }
 373                
 374 cs130_tom 1.14     trace ( "simple_server (%x) exiting\n", id );
 375 cs130_tom 1.1      server_stop ();
 376                }
 377                
 378 cs130_tom 1.2  /*
 379                 * select_server: A non-blocking server.
 380                 */
 381                static VOID WINAPI select_server ( server_params *par )
 382                {
 383                    test_params *gen = par->general;
 384                    server_memory *mem;
 385 cs130_tom 1.4      int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
 386 cs130_tom 1.11         id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd;
 387 cs130_tom 1.2      char *p;
 388                    struct timeval zerotime = {0,0};
 389 cs130_tom 1.3      fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
 390 cs130_tom 1.2  
 391 cs130_tom 1.5      trace ( "select_server (%x) starting\n", id );
 392 cs130_tom 1.2  
 393                    set_so_opentype ( FALSE ); /* non-overlapped */
 394                    server_start ( par );
 395                    mem = TlsGetValue ( tls );
 396                
 397                    wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%lx): failed to set blocking mode: %d\n");
 398                    wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%lx): listen failed: %d\n");
 399                
 400 cs130_tom 1.5      trace ( "select_server (%x) ready\n", id );
 401 cs130_tom 1.2      SetEvent ( server_ready ); /* notify clients */
 402                
 403 cs130_tom 1.3      FD_ZERO ( &fds_openrecv );
 404                    FD_ZERO ( &fds_recv );
 405                    FD_ZERO ( &fds_send );
 406                    FD_ZERO ( &fds_opensend );
 407 cs130_tom 1.5  
 408 cs130_tom 1.3      FD_SET ( mem->s, &fds_openrecv );
 409 cs130_tom 1.2  
 410                    while(1)
 411                    {
 412 cs130_tom 1.3          fds_recv = fds_openrecv;
 413                        fds_send = fds_opensend;
 414 cs130_tom 1.2  
 415 cs130_tom 1.12         wsa_ok ( select ( 0, &fds_recv, &fds_send, NULL, &zerotime ), SOCKET_ERROR !=, 
 416                            "select_server (%lx): select() failed: %d\n" );
 417 cs130_tom 1.2  
 418                        /* check for incoming requests */
 419 cs130_tom 1.3          if ( FD_ISSET ( mem->s, &fds_recv ) ) {
 420 cs130_tom 1.2              trace ( "select_server (%x): accepting client connection\n", id );
 421                
 422                            /* accept a single connection */
 423 cs130_tom 1.13             tmp = sizeof ( mem->sock[n_connections].peer );
 424                            mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
 425                            wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%lx): accept() failed: %d\n" );
 426 cs130_tom 1.2  
 427 cs130_tom 1.13             ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
 428 cs130_tom 1.2                  "select_server (%x): strange peer address\n", id );
 429                
 430                            /* add to list of open connections */
 431 cs130_tom 1.13             FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
 432                            FD_SET ( mem->sock[n_connections].s, &fds_opensend );
 433 cs130_tom 1.2  
 434 cs130_tom 1.3              n_connections++;
 435 cs130_tom 1.2          }
 436                
 437                        /* handle open requests */
 438                
 439                        for ( i = 0; i < n_connections; i++ )
 440                        {
 441 cs130_tom 1.10             if ( ( mem->sock[i].n_recvd < n_expected ) && FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
 442 cs130_tom 1.2  
 443                                /* Receive data & check it */
 444 cs130_tom 1.11                 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 );
 445                                ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
 446                                mem->sock[i].n_recvd += n_recvd;
 447 cs130_tom 1.3  
 448 cs130_tom 1.4                  if ( mem->sock[i].n_recvd == n_expected ) {
 449 cs130_tom 1.3                      p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
 450 cs130_tom 1.8                      ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
 451 cs130_tom 1.3                      FD_CLR ( mem->sock[i].s, &fds_openrecv );
 452 cs130_tom 1.2                  }
 453                
 454 cs130_tom 1.8                  ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
 455                            }
 456                
 457 cs130_tom 1.13             if ( ( mem->sock[i].n_sent < n_expected ) && ( mem->sock[i].n_recvd == n_expected ) && FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
 458 cs130_tom 1.5  
 459 cs130_tom 1.2                  /* Echo data back */
 460 cs130_tom 1.11                 n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( n_expected - mem->sock[i].n_sent, par->buflen ), 0 );
 461                                ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
 462                                mem->sock[i].n_sent += n_sent;
 463 cs130_tom 1.2  
 464 cs130_tom 1.4                  if ( mem->sock[i].n_sent == n_expected ) {
 465 cs130_tom 1.3                      FD_CLR ( mem->sock[i].s, &fds_opensend );
 466                                }
 467 cs130_tom 1.8  
 468                                ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
 469 cs130_tom 1.3              }
 470 cs130_tom 1.4          }
 471 cs130_tom 1.2  
 472 cs130_tom 1.4          /* check if all clients are done */
 473                        if ( ( fds_opensend.fd_count == 0 ) 
 474 cs130_tom 1.9              && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
 475 cs130_tom 1.4              && ( n_connections  == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
 476 cs130_tom 1.5              break;
 477 cs130_tom 1.2          }
 478                    }
 479                
 480                    for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
 481                    {
 482                        /* cleanup */
 483                        read_zero_bytes ( mem->sock[i].s );
 484                        wsa_ok ( closesocket ( mem->sock[i].s ),  0 ==, "select_server (%lx): closesocket error: %d\n" );
 485                        mem->sock[i].s = INVALID_SOCKET;
 486                    }
 487                
 488                    trace ( "select_server (%x) exiting\n", id );
 489                    server_stop ();
 490                }
 491                
 492 cs130_tom 1.1  /**************** Clients ***************/
 493                
 494                /*
 495                 * simple_client: A very basic client doing synchronous IO.
 496                 */
 497                static VOID WINAPI simple_client ( client_params *par )
 498                {
 499                    test_params *gen = par->general;
 500                    client_memory *mem;
 501                    int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
 502                    char *p;
 503                
 504                    id = GetCurrentThreadId();
 505                    trace ( "simple_client (%x): starting\n", id );
 506                    /* wait here because we want to call set_so_opentype before creating a socket */
 507                    WaitForSingleObject ( server_ready, INFINITE );
 508                    trace ( "simple_client (%x): server ready\n", id );
 509                
 510                    check_so_opentype ();
 511                    set_so_opentype ( FALSE ); /* non-overlapped */
 512                    client_start ( par );
 513 cs130_tom 1.1      mem = TlsGetValue ( tls );
 514                
 515                    /* Connect */
 516                    wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
 517                             0 ==, "simple_client (%lx): connect error: %d\n" );
 518                    ok ( set_blocking ( mem->s, TRUE ) == 0,
 519                         "simple_client (%x): failed to set blocking mode\n", id );
 520                    trace ( "simple_client (%x) connected\n", id );
 521                
 522                    /* send data to server */
 523                    n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
 524                    ok ( n_sent == n_expected,
 525                         "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
 526                
 527                    /* shutdown send direction */
 528                    wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
 529                
 530                    /* Receive data echoed back & check it */
 531                    n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
 532                    ok ( n_recvd == n_expected,
 533                         "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
 534 cs130_tom 1.1  
 535                    /* check data */
 536                    p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
 537                    ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
 538                
 539                    /* cleanup */
 540                    read_zero_bytes ( mem->s );
 541                    trace ( "simple_client (%x) exiting\n", id );
 542                    client_stop ();
 543                }
 544                
 545                /*
 546                 * event_client: An event-driven client
 547                 */
 548                static void WINAPI event_client ( client_params *par )
 549                {
 550                    test_params *gen = par->general;
 551                    client_memory *mem;
 552                    int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
 553                        tmp, err, n;
 554                    HANDLE event;
 555 cs130_tom 1.1      WSANETWORKEVENTS wsa_events;
 556                    char *send_last, *recv_last, *send_p, *recv_p;
 557                    long mask = FD_READ | FD_WRITE | FD_CLOSE;
 558                
 559                    trace ( "event_client (%x): starting\n", id );
 560                    client_start ( par );
 561                    trace ( "event_client (%x): server ready\n", id );
 562                
 563                    mem = TlsGetValue ( tls );
 564                
 565                    /* Prepare event notification for connect, makes socket nonblocking */
 566                    event = WSACreateEvent ();
 567                    WSAEventSelect ( mem->s, event, FD_CONNECT );
 568                    tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
 569                    if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
 570                        ok ( 0, "event_client (%x): connect error: %d\n", id, err );
 571                
 572                    tmp = WaitForSingleObject ( event, INFINITE );
 573                    ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
 574                    err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 575                    wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 576 cs130_tom 1.1  
 577                    err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
 578                    ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
 579                    if ( err ) goto out;
 580                
 581                    trace ( "event_client (%x) connected\n", id );
 582                
 583                    WSAEventSelect ( mem->s, event, mask );
 584                
 585                    recv_p = mem->recv_buf;
 586                    recv_last = mem->recv_buf + n_expected;
 587                    send_p = mem->send_buf;
 588                    send_last = mem->send_buf + n_expected;
 589                
 590                    while ( TRUE )
 591                    {
 592                        err = WaitForSingleObject ( event, INFINITE );
 593                        ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
 594                
 595                        err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 596                        wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 597 cs130_tom 1.1  
 598                        if ( wsa_events.lNetworkEvents & FD_WRITE )
 599                        {
 600                            err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
 601                            ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
 602                
 603                            if ( err== 0 )
 604                                do
 605                                {
 606                                    n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
 607                                    if ( n < 0 )
 608                                    {
 609                                        err = WSAGetLastError ();
 610                                        ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
 611                                    }
 612                                    else
 613                                        send_p += n;
 614                                }
 615                                while ( n >= 0 && send_p < send_last );
 616                
 617                            if ( send_p == send_last )
 618 cs130_tom 1.1              {
 619                                trace ( "event_client (%x): all data sent - shutdown\n", id );
 620                                shutdown ( mem->s, SD_SEND );
 621                                mask &= ~FD_WRITE;
 622                                WSAEventSelect ( mem->s, event, mask );
 623                            }
 624                        }
 625                        if ( wsa_events.lNetworkEvents & FD_READ )
 626                        {
 627                            err = wsa_events.iErrorCode[ FD_READ_BIT ];
 628                            ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
 629                            if ( err != 0 ) break;
 630                            
 631                            /* First read must succeed */
 632                            n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
 633                            wsa_ok ( n, 0 <=, "event_client (%lx): recv error: %d\n" );
 634                
 635                            while ( n >= 0 ) {
 636                                recv_p += n;
 637                                if ( recv_p == recv_last )
 638                                {
 639 cs130_tom 1.1                      mask &= ~FD_READ;
 640                                    trace ( "event_client (%x): all data received\n", id );
 641                                    WSAEventSelect ( mem->s, event, mask );
 642                                    break;
 643                                }
 644                                n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
 645                                if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
 646                                    ok ( 0, "event_client (%x): read error: %d\n", id, err );
 647                                
 648                            }
 649                        }   
 650                        if ( wsa_events.lNetworkEvents & FD_CLOSE )
 651                        {
 652                            trace ( "event_client (%x): close event\n", id );
 653                            err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
 654                            ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
 655                            break;
 656                        }
 657                    }
 658                
 659                    ok ( send_p == send_last,
 660 cs130_tom 1.1           "simple_client (%x): sent less data than expected: %d of %d\n",
 661                         id, send_p - mem->send_buf, n_expected );
 662                    ok ( recv_p == recv_last,
 663                         "simple_client (%x): received less data than expected: %d of %d\n",
 664                         id, recv_p - mem->recv_buf, n_expected );
 665                    recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
 666                    ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
 667                
 668                out:
 669                    WSACloseEvent ( event );
 670                    trace ( "event_client (%x) exiting\n", id );
 671                    client_stop ();
 672                }
 673                
 674                /**************** Main program utility functions ***************/
 675                
 676                static void Init (void)
 677                {
 678                    WORD ver = MAKEWORD (2, 2);
 679                    WSADATA data;
 680                
 681 cs130_tom 1.1      ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
 682                    tls = TlsAlloc();
 683                }
 684                
 685                static void Exit (void)
 686                {
 687                    TlsFree ( tls );
 688                    ok ( WSACleanup() == 0, "WSACleanup failed\n" );
 689                }
 690                
 691                static void StartServer (LPTHREAD_START_ROUTINE routine,
 692                                         test_params *general, server_params *par)
 693                {
 694                    par->general = general;
 695                    thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
 696                    ok ( thread[0] != NULL, "Failed to create server thread\n" );
 697                }
 698                
 699                static void StartClients (LPTHREAD_START_ROUTINE routine,
 700                                          test_params *general, client_params *par)
 701                {
 702 cs130_tom 1.1      int i;
 703                    par->general = general;
 704                    for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
 705                    {
 706                        client_id = i - 1;
 707                        thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
 708                        ok ( thread[i] != NULL, "Failed to create client thread\n" );
 709                        /* Make sure the client is up and running */
 710                        WaitForSingleObject ( client_ready[client_id], INFINITE );
 711                    };
 712                }
 713                
 714                static void do_test( test_setup *test )
 715                {
 716                    DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
 717                    DWORD wait;
 718                
 719                    server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
 720                    for (i = 0; i <= n; i++)
 721                        client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
 722                
 723 cs130_tom 1.1      StartServer ( test->srv, &test->general, &test->srv_params );
 724                    StartClients ( test->clt, &test->general, &test->clt_params );
 725                    WaitForSingleObject ( server_ready, INFINITE );
 726                
 727                    wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
 728                    ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n , 
 729                         "some threads have not completed: %lx\n", wait );
 730                
 731                    if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
 732                    {
 733                        for (i = 0; i <= n; i++)
 734                        {
 735                            trace ("terminating thread %08lx\n", thread_id[i]);
 736                            if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
 737                                TerminateThread ( thread [i], 0 );
 738                        }
 739                    }
 740                    CloseHandle ( server_ready );
 741                    for (i = 0; i <= n; i++)
 742                        CloseHandle ( client_ready[i] );
 743                }
 744 cs130_tom 1.1  
 745                /********* some tests for getsockopt(setsockopt(X)) == X ***********/
 746                /* optname = SO_LINGER */
 747                LINGER linger_testvals[] = {
 748                    {0,0},
 749                    {0,73}, 
 750                    {1,0},
 751                    {5,189}
 752                };
 753                
 754                /* optname = SO_RCVTIMEO, SOSNDTIMEO */
 755                #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
 756                                        bug in the linux kernel (fixed in 2.6.8) */ 
 757                #define SOCKTIMEOUT2 997000 /* 997 seconds */
 758                
 759                static void test_set_getsockopt()
 760                {
 761                    SOCKET s;
 762                    int i, err;
 763                    int timeout;
 764                    LINGER lingval;
 765 cs130_tom 1.1      int size;
 766                
 767                    s = socket(AF_INET, SOCK_STREAM, 0);
 768                    ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 769                    if( s == INVALID_SOCKET) return;
 770                    /* SO_RCVTIMEO */
 771                    timeout = SOCKTIMEOUT1;
 772                    size = sizeof(timeout);
 773                    err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size); 
 774                    if( !err)
 775                        err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size); 
 776                    ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
 777                    ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
 778                    /* SO_SNDTIMEO */
 779                    timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */ 
 780                    size = sizeof(timeout);
 781                    err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size); 
 782                    if( !err)
 783                        err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size); 
 784                    ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
 785                    ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
 786 cs130_tom 1.1      /* SO_LINGER */
 787                    for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
 788                        size =  sizeof(lingval);
 789                        lingval = linger_testvals[i];
 790                        err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size); 
 791                        if( !err)
 792                            err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size); 
 793                        ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
 794                        ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
 795                                (lingval.l_linger == linger_testvals[i].l_linger ||
 796                                 (!lingval.l_linger && !linger_testvals[i].l_onoff))
 797                                , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d \n", i, 
 798                                 lingval.l_onoff, lingval.l_linger,
 799                                 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
 800                    }
 801                    closesocket(s);
 802                }
 803                
 804                static void test_so_reuseaddr()
 805                {
 806                    struct sockaddr_in saddr;
 807 cs130_tom 1.1      SOCKET s1,s2;
 808                    unsigned int rc,reuse,size;
 809                
 810                    saddr.sin_family      = AF_INET;
 811                    saddr.sin_port        = htons(9375);
 812                    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 813                
 814                    s1=socket(AF_INET, SOCK_STREAM, 0);
 815                    ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 816                    rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
 817                    ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
 818                
 819                    s2=socket(AF_INET, SOCK_STREAM, 0);
 820                    ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 821                
 822                    reuse=0x1234;
 823                    size=sizeof(reuse);
 824                    rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
 825                    ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
 826                
 827                    rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
 828 cs130_tom 1.1      ok(rc==SOCKET_ERROR, "bind() succeeded\n");
 829                
 830                    reuse = 1;
 831                    rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
 832                    ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
 833                
 834                    todo_wine {
 835                    rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
 836                    ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
 837                    }
 838                
 839                    closesocket(s2);
 840                    closesocket(s1);
 841                }
 842                
 843                /************* Array containing the tests to run **********/
 844                
 845                #define STD_STREAM_SOCKET \
 846                            SOCK_STREAM, \
 847                            0, \
 848                            "127.0.0.1", \
 849 cs130_tom 1.1              9374
 850                
 851                static test_setup tests [NUM_TESTS] =
 852                {
 853                    /* Test 0: synchronous client and server */
 854                    {
 855                        {
 856                            STD_STREAM_SOCKET,
 857                            2048,
 858                            16,
 859                            2
 860                        },
 861                        simple_server,
 862                        {
 863                            NULL,
 864                            0,
 865                            64
 866                        },
 867                        simple_client,
 868                        {
 869                            NULL,
 870 cs130_tom 1.1              0,
 871                            128
 872                        }
 873                    },
 874                    /* Test 1: event-driven client, synchronous server */
 875                    {
 876                        {
 877                            STD_STREAM_SOCKET,
 878                            2048,
 879                            16,
 880                            2
 881                        },
 882                        simple_server,
 883 cs130_tom 1.2          {
 884                            NULL,
 885                            0,
 886                            64
 887                        },
 888                        event_client,
 889                        {
 890                            NULL,
 891                            WSA_FLAG_OVERLAPPED,
 892                            128
 893                        }
 894                    },
 895 cs130_tom 1.6      /* Test 2: synchronous client, non-blocking server via select() */
 896 cs130_tom 1.2      {
 897                        {
 898                            STD_STREAM_SOCKET,
 899                            2048,
 900                            16,
 901                            2
 902                        },
 903                        select_server,
 904 cs130_tom 1.1          {
 905                            NULL,
 906                            0,
 907                            64
 908                        },
 909 cs130_tom 1.3          simple_client,
 910 cs130_tom 1.1          {
 911                            NULL,
 912 cs130_tom 1.3              0,
 913 cs130_tom 1.1              128
 914                        }
 915                    }
 916                };
 917                
 918                static void test_WSAAddressToStringA()
 919                {
 920                    INT ret;
 921                    DWORD len;
 922                    SOCKADDR_IN sockaddr;
 923                    CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
 924                
 925                    CHAR expect1[] = "0.0.0.0";
 926                    CHAR expect2[] = "255.255.255.255";
 927                    CHAR expect3[] = "0.0.0.0:65535";
 928                    CHAR expect4[] = "255.255.255.255:65535";
 929                
 930                    len = 0;
 931                
 932                    sockaddr.sin_family = AF_INET;
 933                    sockaddr.sin_port = 0;
 934 cs130_tom 1.1      sockaddr.sin_addr.s_addr = 0;
 935                
 936                    ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 937                    ok( ret == SOCKET_ERROR, "WSAAddressToStringA() succeeded unexpectedly: %d\n", WSAGetLastError() );
 938                
 939                    len = sizeof(address);
 940                
 941                    sockaddr.sin_family = AF_INET;
 942                    sockaddr.sin_port = 0;
 943                    sockaddr.sin_addr.s_addr = 0;
 944                
 945                    ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 946                    ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 947                
 948                    ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
 949                
 950                    len = sizeof(address);
 951                
 952                    sockaddr.sin_family = AF_INET;
 953                    sockaddr.sin_port = 0;
 954                    sockaddr.sin_addr.s_addr = 0xffffffff;
 955 cs130_tom 1.1  
 956                    ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 957                    ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 958                
 959                    ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
 960                
 961                    len = sizeof(address);
 962                
 963                    sockaddr.sin_family = AF_INET;
 964                    sockaddr.sin_port = 0xffff;
 965                    sockaddr.sin_addr.s_addr = 0;
 966                
 967                    ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 968                    ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 969                
 970                    ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
 971                
 972                    len = sizeof(address);
 973                
 974                    sockaddr.sin_family = AF_INET;
 975                    sockaddr.sin_port = 0xffff;
 976 cs130_tom 1.1      sockaddr.sin_addr.s_addr = 0xffffffff;
 977                
 978                    ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
 979                    ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
 980                
 981                    ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
 982                }
 983                
 984                static void test_WSAAddressToStringW()
 985                {
 986                    INT ret;
 987                    DWORD len;
 988                    SOCKADDR_IN sockaddr;
 989                    WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
 990                
 991                    WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
 992                    WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
 993                    WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
 994                    WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
 995                                        '6', '5', '5', '3', '5', 0 };
 996                
 997 cs130_tom 1.1      len = 0;
 998                
 999                    sockaddr.sin_family = AF_INET;
1000                    sockaddr.sin_port = 0;
1001                    sockaddr.sin_addr.s_addr = 0;
1002                
1003                    ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1004                    ok( ret == SOCKET_ERROR, "WSAAddressToStringW() succeeded unexpectedly: %x\n", WSAGetLastError() );
1005                
1006                    len = sizeof(address);
1007                
1008                    sockaddr.sin_family = AF_INET;
1009                    sockaddr.sin_port = 0;
1010                    sockaddr.sin_addr.s_addr = 0;
1011                
1012                    ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1013                    ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1014                
1015                    ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1016                
1017                    len = sizeof(address);
1018 cs130_tom 1.1  
1019                    sockaddr.sin_family = AF_INET;
1020                    sockaddr.sin_port = 0;
1021                    sockaddr.sin_addr.s_addr = 0xffffffff;
1022                
1023                    ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1024                    ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1025                
1026                    ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1027                
1028                    len = sizeof(address);
1029                
1030                    sockaddr.sin_family = AF_INET;
1031                    sockaddr.sin_port = 0xffff;
1032                    sockaddr.sin_addr.s_addr = 0;
1033                
1034                    ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1035                    ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1036                
1037                    ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1038                
1039 cs130_tom 1.1      len = sizeof(address);
1040                
1041                    sockaddr.sin_family = AF_INET;
1042                    sockaddr.sin_port = 0xffff;
1043                    sockaddr.sin_addr.s_addr = 0xffffffff;
1044                
1045                    ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1046                    ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1047                
1048                    ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1049                }
1050                
1051                static void test_WSAStringToAddressA()
1052                {
1053                    INT ret, len;
1054                    SOCKADDR_IN sockaddr;
1055                
1056                    CHAR address1[] = "0.0.0.0";
1057                    CHAR address2[] = "127.127.127.127";
1058                    CHAR address3[] = "255.255.255.255";
1059                    CHAR address4[] = "127.127.127.127:65535";
1060 cs130_tom 1.1      CHAR address5[] = "255.255.255.255:65535";
1061                
1062                    len = 0;
1063                    sockaddr.sin_family = AF_INET;
1064                
1065                    ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1066                    ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1067                        WSAGetLastError() );
1068                
1069                    len = sizeof(sockaddr);
1070                    sockaddr.sin_port = 0;
1071                    sockaddr.sin_addr.s_addr = 0;
1072                
1073                    ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1074                    ok( !ret && sockaddr.sin_addr.s_addr == 0,
1075                        "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1076                
1077                    len = sizeof(sockaddr);
1078                    sockaddr.sin_port = 0;
1079                    sockaddr.sin_addr.s_addr = 0;
1080                
1081 cs130_tom 1.1      ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1082                    ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1083                        "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1084                
1085                    len = sizeof(sockaddr);
1086                
1087                    ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1088                    ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1089                
1090                    len = sizeof(sockaddr);
1091                    sockaddr.sin_port = 0;
1092                    sockaddr.sin_addr.s_addr = 0;
1093                
1094                    ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1095                    ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1096                        "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1097                
1098                    len = sizeof(sockaddr);
1099                
1100                    ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1101                    ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1102 cs130_tom 1.1  }
1103                
1104                static void test_WSAStringToAddressW()
1105                {
1106                    INT ret, len;
1107                    SOCKADDR_IN sockaddr;
1108                
1109                    WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1110                    WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1111                    WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1112                    WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1113                                         ':', '6', '5', '5', '3', '5', 0 };
1114                    WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1115                                         '6', '5', '5', '3', '5', 0 };
1116                
1117                    len = 0;
1118                    sockaddr.sin_family = AF_INET;
1119                
1120                    ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1121                    ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1122                        WSAGetLastError() );
1123 cs130_tom 1.1  
1124                    len = sizeof(sockaddr);
1125                    sockaddr.sin_port = 0;
1126                    sockaddr.sin_addr.s_addr = 0;
1127                
1128                    ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1129                    ok( !ret && sockaddr.sin_addr.s_addr == 0,
1130                        "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1131                
1132                    len = sizeof(sockaddr);
1133                
1134                    sockaddr.sin_port = 0;
1135                    sockaddr.sin_addr.s_addr = 0;
1136                
1137                    ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1138                    ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1139                        "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1140                
1141                    len = sizeof(sockaddr);
1142                
1143                    ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1144 cs130_tom 1.1      ok( ret, "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1145                
1146                    len = sizeof(sockaddr);
1147                    sockaddr.sin_port = 0;
1148                    sockaddr.sin_addr.s_addr = 0;
1149                
1150                    ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1151                    ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1152                        "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1153                
1154                    len = sizeof(sockaddr);
1155                
1156                    ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1157                    ok( ret, "WSAStringToAddressW() succeeded unexpectedly: %d\n", WSAGetLastError() );
1158                }
1159                
1160                /**************** Main program  ***************/
1161                
1162                START_TEST( sock )
1163                {
1164                    int i;
1165 cs130_tom 1.1      Init();
1166                
1167                    test_set_getsockopt();
1168                    test_so_reuseaddr();
1169                
1170                    for (i = 0; i < NUM_TESTS; i++)
1171                    {
1172                        trace ( " **** STARTING TEST %d **** \n", i );
1173                        do_test (  &tests[i] );
1174                        trace ( " **** TEST %d COMPLETE **** \n", i );
1175                    }
1176                
1177                    test_WSAAddressToStringA();
1178                    test_WSAAddressToStringW();
1179                
1180                    test_WSAStringToAddressA();
1181                    test_WSAStringToAddressW();
1182                
1183                    Exit();
1184                }

Rizwan Kassim
Powered by
ViewCVS 0.9.2