(file) Return to sock.c CVS log (file) (dir) Up to [RizwankCVS] / wine1 / wine

   1 cs130_ryutaro 1.1 /*
   2                    * Unit test suite for winsock functions
   3                    *
   4                    * Copyright 2002 Martin Wilck
   5                    *
   6                    * This library is free software; you can redistribute it and/or
   7                    * modify it under the terms of the GNU Lesser General Public
   8                    * License as published by the Free Software Foundation; either
   9                    * version 2.1 of the License, or (at your option) any later version.
  10                    *
  11                    * This library is distributed in the hope that it will be useful,
  12                    * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13                    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14                    * Lesser General Public License for more details.
  15                    *
  16                    * You should have received a copy of the GNU Lesser General Public
  17                    * License along with this library; if not, write to the Free Software
  18                    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19                    */
  20                   
  21                   #include <stdarg.h>
  22 cs130_ryutaro 1.1 
  23                   #include <windef.h>
  24                   #include <winbase.h>
  25                   #include <winsock2.h>
  26                   #include <mswsock.h>
  27                   #include "wine/test.h"
  28                   #include <winnt.h>
  29                   #include <winerror.h>
  30                   
  31                   #define MAX_CLIENTS 4      /* Max number of clients */
  32                   #define NUM_TESTS   3      /* Number of tests performed */
  33                   #define FIRST_CHAR 'A'     /* First character in transferred pattern */
  34                   #define BIND_SLEEP 10      /* seconds to wait between attempts to bind() */
  35                   #define BIND_TRIES 6       /* Number of bind() attempts */
  36                   #define TEST_TIMEOUT 30    /* seconds to wait before killing child threads
  37                                                 after server initialization, if something hangs */
  38                   #define ASYNC_EVENT (WM_USER +5)     /* the message we'll use for our async notification */
  39                   
  40                   #define wsa_ok(op, cond, msg) \
  41                      do { \
  42                           int tmp, err = 0; \
  43 cs130_ryutaro 1.1         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                   } 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_ryutaro 1.1     int                    nread;
  65 cs130_ryutaro 1.3     int                    nsent;
  66 cs130_ryutaro 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_ryutaro 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_ryutaro 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                   /* Things specific to async tests */
 124                   typedef struct async_param_t
 125                   {
 126                     server_params            *svc_params;
 127 cs130_ryutaro 1.3   server_memory            *mem;
 128                     HWND                     hwnd;
 129 cs130_ryutaro 1.1 } async_param_t;
 130                   
 131                   /**************** Static variables ***************/
 132                   
 133                   static DWORD      tls;              /* Thread local storage index */
 134                   static HANDLE     thread[1+MAX_CLIENTS];
 135                   static DWORD      thread_id[1+MAX_CLIENTS];
 136                   static HANDLE     server_ready;
 137                   static HANDLE     client_ready[MAX_CLIENTS];
 138                   static int        client_id;
 139                   static async_param_t async_params;    /* async structure */
 140                   
 141                   /**************** General utility functions ***************/
 142                   
 143                   static void set_so_opentype ( BOOL overlapped )
 144                   {
 145                       int optval = !overlapped, newval, len = sizeof (int);
 146                   
 147                       ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
 148                                         (LPVOID) &optval, sizeof (optval) ) == 0,
 149                            "setting SO_OPENTYPE failed\n" );
 150 cs130_ryutaro 1.1     ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
 151                                         (LPVOID) &newval, &len ) == 0,
 152                            "getting SO_OPENTYPE failed\n" );
 153                       ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
 154                   }
 155                   
 156                   static int set_blocking ( SOCKET s, BOOL blocking )
 157                   {
 158                       u_long val = !blocking;
 159                       return ioctlsocket ( s, FIONBIO, &val );
 160                   }
 161                   
 162                   static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
 163                   {
 164                       char c, *p;
 165                       for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
 166                           memset ( p, c, chunk_size );
 167                   }
 168                   
 169                   static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
 170                   {
 171 cs130_ryutaro 1.1     char c, *p;
 172                       int i;
 173                       for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
 174                       {
 175                           for ( i = 0; i < chunk_size; i++ )
 176                               if ( p[i] != c ) return p + i;
 177                       }
 178                       return NULL;
 179                   }
 180                   
 181                   /*
 182                    * This routine is called when a client / server does not expect any more data,
 183                    * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
 184                    */
 185                   static void read_zero_bytes ( SOCKET s )
 186                   {
 187                       char buf[256];
 188                       int tmp, n = 0;
 189                       while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
 190                           n += tmp;
 191                       ok ( n <= 0, "garbage data received: %d bytes\n", n );
 192 cs130_ryutaro 1.1 }
 193                   
 194                   static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
 195                   {
 196                       char* last = buf + buflen, *p;
 197                       int n = 1;
 198                       for ( p = buf; n > 0 && p < last; p += n )
 199                           n = send ( s, p, min ( sendlen, last - p ), 0 );
 200                       wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" );
 201                       return p - buf;
 202                   }
 203                   
 204                   static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
 205                   {
 206                       char* last = buf + buflen, *p;
 207                       int n = 1;
 208                       for ( p = buf; n > 0 && p < last; p += n )
 209                           n = recv ( s, p, min ( recvlen, last - p ), 0 );
 210                       wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" );
 211                       return p - buf;
 212                   }
 213 cs130_ryutaro 1.1 
 214                   /*
 215                    *  Call this routine right after thread startup.
 216                    *  SO_OPENTYPE must by 0, regardless what the server did.
 217                    */
 218                   static void check_so_opentype (void)
 219                   {
 220                       int tmp = 1, len;
 221                       len = sizeof (tmp);
 222                       getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
 223                       ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
 224                   }
 225                   
 226                   /**************** Server utility functions ***************/
 227                   
 228                   /*
 229                    *  Even if we have closed our server socket cleanly,
 230                    *  the OS may mark the address "in use" for some time -
 231                    *  this happens with native Linux apps, too.
 232                    */
 233                   static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
 234 cs130_ryutaro 1.1 {
 235                       int err, wsaerr = 0, n_try = BIND_TRIES;
 236                   
 237                       while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
 238                               ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
 239                               n_try-- >= 0)
 240                       {
 241                           trace ( "address in use, waiting ...\n" );
 242                           Sleep ( 1000 * BIND_SLEEP );
 243                       }
 244                       ok ( err == 0, "failed to bind: %d\n", wsaerr );
 245                   }
 246                   
 247                   static void server_start ( server_params *par )
 248                   {
 249                       int i;
 250                       test_params *gen = par->general;
 251                       server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
 252                   
 253                       TlsSetValue ( tls, mem );
 254                       mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
 255 cs130_ryutaro 1.1                           NULL, 0, par->sock_flags );
 256                       ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
 257                   
 258                       mem->addr.sin_family = AF_INET;
 259                       mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
 260                       mem->addr.sin_port = htons ( gen->inet_port );
 261                   
 262                       for (i = 0; i < MAX_CLIENTS; i++)
 263                       {
 264                           mem->sock[i].s = INVALID_SOCKET;
 265                           mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
 266                           mem->sock[i].nread = 0;
 267 cs130_ryutaro 1.3 	mem->sock[i].nsent = 0;
 268 cs130_ryutaro 1.1     }
 269                   
 270                       if ( gen->sock_type == SOCK_STREAM )
 271                           do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
 272                   }
 273                   
 274                   static void server_stop (void)
 275                   {
 276                       int i;
 277                       server_memory *mem = TlsGetValue ( tls );
 278                       for (i = 0; i < MAX_CLIENTS; i++ )
 279                       {
 280                           LocalFree ( (HANDLE) mem->sock[i].buf );
 281                           if ( mem->sock[i].s != INVALID_SOCKET )
 282                               closesocket ( mem->sock[i].s );
 283                       }
 284                       ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
 285                       LocalFree ( (HANDLE) mem );
 286                       ExitThread ( GetCurrentThreadId () );
 287                   }
 288                   
 289 cs130_ryutaro 1.3 /* This function takes in server_memory struct of the running server
 290                      and looks for the first existence of sockToFind socket in sock[] array. */
 291                   static sock_info* GetClientSockInfo( server_memory* servMem, SOCKET sockToFind )
 292                   {
 293                   
 294                     // Looping through all the possible clients
 295                     int clientNum;
 296                     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
 297                   
 298                       // When the match is found, return the pointer to that sock_info struct
 299                       if( servMem->sock[clientNum].s == sockToFind ){
 300                   
 301                         return &(servMem->sock[clientNum]);
 302                   
 303                       }
 304                   
 305                     }
 306                   
 307                     // Did not find a match. 
 308                     return NULL;
 309                   
 310 cs130_ryutaro 1.3 }
 311                   
 312                   /* This function takes in server_memory structure of the running
 313                      server and finds the first socket that is available */
 314                   static sock_info* GetNextOpenSock( server_memory* servMem )
 315                   {
 316                     
 317                     // Loop through sock array of servMem
 318                     int clientNum;
 319                     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
 320                   
 321                       // when an open slot is found, return
 322                       if( servMem->sock[clientNum].s == INVALID_SOCKET )
 323                         return &(servMem->sock[clientNum]);
 324                   
 325                     }
 326                     
 327                     // When there is no more open socket, return NULL
 328                     return NULL;
 329                   
 330                   }
 331 cs130_ryutaro 1.3 
 332                   /* this function returns TRUE if all sockets in servMem are INVALID_SOCKET */
 333                   static BOOL noOpenSock( server_memory* servMem )
 334                   {
 335                   
 336                     // Loop through sock array of servMem
 337                     int clientNum;
 338                     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
 339                   
 340                       // if any socket != INVALID_SOCKET, there is an open socket
 341                       if( servMem->sock[clientNum].s != INVALID_SOCKET )
 342                         return FALSE;
 343                   
 344                     }
 345                   
 346                     // no open sockets
 347                     return TRUE;
 348                   
 349                   }
 350                   
 351                   /* This function takes in pointer to sock_info structure of a client 
 352 cs130_ryutaro 1.3    and attempts to send all the data that hasn't been sent back yet.  */
 353                   static void try_send_all_buf( sock_info* sockInfo, int servBufLen )
 354                   {
 355                     int n_sent = 0, sendLen, err;
 356                     int id = GetCurrentThreadId();
 357                   
 358                     // send any data that hasn't been sent yet
 359                     while( sockInfo->nsent < sockInfo->nread ){
 360                   
 361                       // find out the buffer length to send
 362                       sendLen = min( sockInfo->nread - sockInfo->nsent, servBufLen );
 363                   
 364                       // send the data
 365                       n_sent = send( sockInfo->s, sockInfo->buf + sockInfo->nsent, sendLen, 0 );
 366                   	  
 367                       // If there was an error, break
 368                       if( n_sent == SOCKET_ERROR )
 369                         break;
 370                   
 371                       // update the number bytes sent
 372                       sockInfo->nsent += n_sent;
 373 cs130_ryutaro 1.3 
 374                     }
 375                   
 376                     // check the error
 377                     if( n_sent == SOCKET_ERROR && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
 378                       ok( 0, "async_server (%x): send error: %d\n", id, err );
 379                     
 380                   }
 381                   
 382 cs130_ryutaro 1.1 /**************** Client utilitiy functions ***************/
 383                   
 384                   static void client_start ( client_params *par )
 385                   {
 386                       test_params *gen = par->general;
 387                       client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
 388                   
 389                       TlsSetValue ( tls, mem );
 390                   
 391                       WaitForSingleObject ( server_ready, INFINITE );
 392                   
 393                       mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
 394                                             NULL, 0, par->sock_flags );
 395                   
 396                       mem->addr.sin_family = AF_INET;
 397                       mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
 398                       mem->addr.sin_port = htons ( gen->inet_port );
 399                   
 400                       ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
 401                   
 402                       mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
 403 cs130_ryutaro 1.1     mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
 404                       fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
 405                   
 406                       SetEvent ( client_ready[client_id] );
 407                       /* Wait for the other clients to come up */
 408                       WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
 409                   }
 410                   
 411                   static void client_stop (void)
 412                   {
 413                       client_memory *mem = TlsGetValue ( tls );
 414                       wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%lx): %d\n" );
 415                       LocalFree ( (HANDLE) mem->send_buf );
 416                       LocalFree ( (HANDLE) mem );
 417                       ExitThread(0);
 418                   }
 419                   
 420                   /**************** Servers ***************/
 421                   
 422                   /*
 423                    * simple_server: A very basic server doing synchronous IO.
 424 cs130_ryutaro 1.1  */
 425                   static VOID WINAPI simple_server ( server_params *par )
 426                   {
 427                       test_params *gen = par->general;
 428                       server_memory *mem;
 429                       int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
 430                           id = GetCurrentThreadId();
 431                       char *p;
 432                   
 433                       trace ( "simple_server (%x) starting\n", id );
 434                   
 435                       set_so_opentype ( FALSE ); /* non-overlapped */
 436                       server_start ( par );
 437                       mem = TlsGetValue ( tls );
 438                   
 439                       wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%lx): failed to set blocking mode: %d\n");
 440                       wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%lx): listen failed: %d\n");
 441                   
 442                       trace ( "simple_server (%x) ready\n", id );
 443                       SetEvent ( server_ready ); /* notify clients */
 444                   
 445 cs130_ryutaro 1.1     for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
 446                       {
 447                           trace ( "simple_server (%x): waiting for client\n", id );
 448                   
 449                           /* accept a single connection */
 450                           tmp = sizeof ( mem->sock[0].peer );
 451                           mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
 452                           wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%lx): accept failed: %d\n" );
 453                   
 454                           ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
 455                                "simple_server (%x): strange peer address\n", id );
 456                   
 457                           /* Receive data & check it */
 458                           n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
 459                           ok ( n_recvd == n_expected,
 460                                "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
 461                           p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
 462                           ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
 463                   
 464                           /* Echo data back */
 465                           n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
 466 cs130_ryutaro 1.1         ok ( n_sent == n_expected,
 467                                "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
 468                   
 469                           /* cleanup */
 470                           read_zero_bytes ( mem->sock[0].s );
 471                           wsa_ok ( closesocket ( mem->sock[0].s ),  0 ==, "simple_server (%lx): closesocket error: %d\n" );
 472                           mem->sock[0].s = INVALID_SOCKET;
 473                       }
 474                   
 475                       trace ( "simple_server (%x) exiting\n", id );
 476                       server_stop ();
 477                   }
 478                   
 479                   /* 
 480                      Call back event handler for asynchronous server 
 481                   */
 482                   static LRESULT CALLBACK async_server_proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
 483                   {
 484 cs130_ryutaro 1.3 
 485                     server_memory* mem = async_params.mem;
 486                     SOCKET clientSock = (SOCKET) wParam;
 487                     test_params* gen = (async_params.svc_params)->general;
 488                     char* inetAddr = (char*)gen->inet_addr; 
 489                   
 490                     sock_info* sockInfo;
 491 cs130_ryutaro 1.1   struct sockaddr_in peer;
 492 cs130_ryutaro 1.3   int addrLen = sizeof( peer );
 493                     int eventCode, err, id = GetCurrentThreadId();
 494                     int servBufLen = (async_params.svc_params)->buflen;
 495                     int n_recvd = 0, n_expected;
 496                     char *p;
 497 cs130_ryutaro 1.1   
 498 cs130_ryutaro 1.3   switch( msg )
 499                       {
 500 cs130_ryutaro 1.1 
 501                       case ASYNC_EVENT: 
 502                   
 503                         eventCode = WSAGETSELECTEVENT( lParam );
 504 cs130_ryutaro 1.3       switch( eventCode )
 505                   	{
 506 cs130_ryutaro 1.1 
 507                   	case FD_ACCEPT:	
 508                   	  
 509 cs130_ryutaro 1.3 	  trace( "async_server (%x): accept event\n", id );
 510                   
 511                   	  // Find the next available socket
 512                   	  sockInfo = GetNextOpenSock( mem );
 513                   	  ok( sockInfo != NULL, "async_server (%x): no more open socket\n", id );
 514                   	  
 515 cs130_ryutaro 1.1 	  // accept connection
 516 cs130_ryutaro 1.3 	  sockInfo->s = accept( mem->s, (struct sockaddr*) &peer, &addrLen );
 517                   	  wsa_ok( sockInfo->s, INVALID_SOCKET !=, "async_server (%lx): accept failed: %d\n" );      
 518                   
 519                   	  // check the address retrieved from accept() is valid
 520                   	  ok( peer.sin_addr.s_addr == inet_addr( inetAddr ), "async_server (%x): strange peer address\n", id );
 521                   
 522                   	  // make this newly accepted socket asynchronous
 523                   	  err = WSAAsyncSelect( sockInfo->s, async_params.hwnd, ASYNC_EVENT, FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE );
 524                   	  wsa_ok( err, SOCKET_ERROR !=, "async_server (%lx): WSAAsyncSelect failed: %d\n" );
 525 cs130_ryutaro 1.1 
 526 cs130_ryutaro 1.3 	  break;
 527 cs130_ryutaro 1.1 
 528 cs130_ryutaro 1.3 	case FD_WRITE:
 529                   	  
 530                   	  // get the sock_info* of this client socket
 531                   	  sockInfo = GetClientSockInfo( mem, clientSock );
 532                   	  ok( sockInfo != NULL, "async_server (%x): socket to write to is not open\n", id );
 533 cs130_ryutaro 1.1 	  
 534 cs130_ryutaro 1.3 	  // try sending any remaining data
 535                   	  try_send_all_buf( sockInfo, servBufLen );	  
 536 cs130_ryutaro 1.1 	  break;
 537                   
 538                   	case FD_READ:
 539                   	  
 540 cs130_ryutaro 1.3 	  // get the sock_info* of this client socket
 541                   	  sockInfo = GetClientSockInfo( mem, clientSock );
 542                   	  ok( sockInfo != NULL, "async_server (%x): socket to read from is not open\n", id );
 543                   	  
 544                   	  // retrieve the data
 545                   	  n_recvd = recv( sockInfo->s, sockInfo->buf + sockInfo->nread, servBufLen, 0 );
 546                   	  if( n_recvd == SOCKET_ERROR && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
 547                   	    ok( 0, "async_server (%x): recv error: %d\n", id, err );
 548 cs130_ryutaro 1.1 	  
 549 cs130_ryutaro 1.3 	  // record number of bytes read so far
 550                   	  if( n_recvd != SOCKET_ERROR )
 551                   	    sockInfo->nread += n_recvd;
 552 cs130_ryutaro 1.1 
 553 cs130_ryutaro 1.3 	  // send the remaining data
 554                   	  try_send_all_buf( sockInfo, servBufLen );
 555 cs130_ryutaro 1.1 	  break;
 556                   
 557                   	case FD_CLOSE:
 558                   
 559 cs130_ryutaro 1.3 	  // Client closed connection - close the connection with this server.
 560 cs130_ryutaro 1.1 	  trace( "async_server (%x): close event\n", id );
 561 cs130_ryutaro 1.3 	  sockInfo = GetClientSockInfo( mem, clientSock );
 562                   	  ok( sockInfo != NULL, "async_server (%x): socket to close is not open\n", id );
 563 cs130_ryutaro 1.1 
 564 cs130_ryutaro 1.3 	  // Check the data it received
 565                   	  n_expected = gen->n_chunks * gen->chunk_size;
 566                   	  ok( sockInfo->nread == n_expected,
 567                   	      "async_server (%x): received less data than expected: %d of %d\n", id, sockInfo->nread, n_expected );	  
 568                   
 569                   	  p = test_buffer( sockInfo->buf, gen->chunk_size, gen->n_chunks );
 570                   	  ok( p == NULL, "async_server (%x): test pattern error: %d\n", id, p - sockInfo->buf);
 571                   
 572                   	  // clean up
 573                   	  wsa_ok( closesocket( sockInfo->s ), 0 ==, "async_server (%lx): closesocket error: %d\n" );
 574                   	  sockInfo->s = INVALID_SOCKET;
 575                   
 576                   	  // If all sockets are closed, kill the server
 577                   	  if( noOpenSock( mem ) ){
 578 cs130_ryutaro 1.1 
 579                   	    trace( "async_server (%x) exiting\n", id );
 580                   	    server_stop();
 581                   
 582                   	  }
 583                         }
 584                     }
 585                     
 586                     return DefWindowProc( hWnd, msg, wParam, lParam );
 587                   }
 588                   
 589                   /*
 590                    * async_server: An event-driven server
 591                    */
 592                   static void WINAPI async_server( server_params *par )
 593                   {
 594 cs130_ryutaro 1.3   int id = GetCurrentThreadId();
 595 cs130_ryutaro 1.1 
 596                     trace( "async_server (%x) starting\n", id );
 597                     set_so_opentype( FALSE ); /* non-overlapped */
 598                     server_start( par );
 599                   
 600 cs130_ryutaro 1.3   // set up async_params structure
 601                     memset( &async_params, 0, sizeof( async_params ) );
 602                     async_params.svc_params = par;
 603                     async_params.mem = TlsGetValue( tls );
 604                    
 605                     // Listen to socket
 606                     wsa_ok( listen ( (async_params.mem)->s, SOMAXCONN ), 0 ==, "async_server (%lx): listen failed: %d\n");
 607 cs130_ryutaro 1.1 
 608                     // Get the current instance
 609                     HINSTANCE instance = GetModuleHandle( NULL );
 610 cs130_ryutaro 1.3   ok( instance != NULL, "async_server (%x):GetModuleHandle error %ld\n", id, GetLastError() );
 611 cs130_ryutaro 1.1 
 612                     // Create a hiddent window to handle async events
 613                     WNDCLASS windowClass;
 614                     windowClass.lpszClassName = "Hidden_Winsock_Window";
 615                     windowClass.style = CS_HREDRAW | CS_VREDRAW;
 616                     windowClass.lpfnWndProc = async_server_proc;
 617                     windowClass.cbClsExtra = 0;
 618                     windowClass.cbWndExtra = 0;
 619                     windowClass.hInstance = instance;
 620                     windowClass.hIcon = NULL;
 621                     windowClass.hCursor = NULL;
 622                     windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 623                     windowClass.lpszMenuName = NULL;
 624                     ATOM classAtom = RegisterClass( &windowClass );
 625 cs130_ryutaro 1.3   ok( classAtom != 0, "async_server (%x): RegisterClass error %ld\n", id, GetLastError() );
 626 cs130_ryutaro 1.1 
 627                     HWND hwnd = CreateWindow( "Hidden_Winsock_Window", 
 628                   			    "Winsock Window", 
 629                   			    WS_OVERLAPPEDWINDOW,
 630                   			    CW_USEDEFAULT, 
 631                   			    CW_USEDEFAULT,
 632                   			    CW_USEDEFAULT,
 633                   			    CW_USEDEFAULT,
 634                   			    NULL,
 635                   			    NULL,
 636                   			    instance,
 637                   			    NULL );
 638 cs130_ryutaro 1.3   ok( hwnd != NULL, "async_server (%x): CreateWindowd error %ld\n", id, GetLastError() );
 639 cs130_ryutaro 1.1 
 640 cs130_ryutaro 1.3   /* save hwnd */
 641                     async_params.hwnd = hwnd;
 642                     
 643 cs130_ryutaro 1.1   /* make the socket asynchronous */
 644 cs130_ryutaro 1.3   int err;
 645                     err = WSAAsyncSelect( (async_params.mem)->s, hwnd, ASYNC_EVENT, FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE );
 646 cs130_ryutaro 1.1   wsa_ok( err, SOCKET_ERROR !=, "async_server (%lx): WSAAsyncSelect failed: %d\n" );
 647                   
 648                     trace( "async_server (%x) ready\n", id );
 649                     SetEvent( server_ready ); /* notify clients */
 650                   
 651                     /* Wait for any event and process it when it occurs */
 652                     BOOL retVal;
 653                     MSG msg;
 654                     while( ( retVal = GetMessage( &msg, NULL, 0, 0 ) ) != 0 ){ 
 655 cs130_ryutaro 1.3  
 656                       ok( retVal != -1, "async_server (%x): GetMessage error %ld\n", id, GetLastError() ); 
 657 cs130_ryutaro 1.1     if( retVal == -1 ){
 658                   
 659 cs130_ryutaro 1.3       // exit so that the server won't block
 660                         trace( "async_server (%x): exiting\n", id );
 661 cs130_ryutaro 1.1       server_stop();
 662                   
 663                       }
 664                   
 665                       // Translate and dispatch the message
 666                       TranslateMessage( &msg ); 
 667                       DispatchMessage( &msg );
 668                   
 669                     }
 670                   }
 671                   
 672                   /**************** Clients ***************/
 673                   
 674                   /*
 675                    * simple_client: A very basic client doing synchronous IO.
 676                    */
 677                   static VOID WINAPI simple_client ( client_params *par )
 678                   {
 679                       test_params *gen = par->general;
 680                       client_memory *mem;
 681                       int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
 682 cs130_ryutaro 1.1     char *p;
 683                   
 684                       id = GetCurrentThreadId();
 685                       trace ( "simple_client (%x): starting\n", id );
 686                       /* wait here because we want to call set_so_opentype before creating a socket */
 687                       WaitForSingleObject ( server_ready, INFINITE );
 688                       trace ( "simple_client (%x): server ready\n", id );
 689                   
 690                       check_so_opentype ();
 691                       set_so_opentype ( FALSE ); /* non-overlapped */
 692                       client_start ( par );
 693                       mem = TlsGetValue ( tls );
 694                   
 695                       /* Connect */
 696                       wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
 697                                0 ==, "simple_client (%lx): connect error: %d\n" );
 698                       ok ( set_blocking ( mem->s, TRUE ) == 0,
 699                            "simple_client (%x): failed to set blocking mode\n", id );
 700                       trace ( "simple_client (%x) connected\n", id );
 701                   
 702                       /* send data to server */
 703 cs130_ryutaro 1.1     n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
 704                       ok ( n_sent == n_expected,
 705                            "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
 706                   
 707                       /* shutdown send direction */
 708                       wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
 709                   
 710                       /* Receive data echoed back & check it */
 711                       n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
 712                       ok ( n_recvd == n_expected,
 713                            "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
 714                   
 715                       /* check data */
 716                       p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
 717                       ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
 718                   
 719                       /* cleanup */
 720                       read_zero_bytes ( mem->s );
 721                       trace ( "simple_client (%x) exiting\n", id );
 722                       client_stop ();
 723                   }
 724 cs130_ryutaro 1.1 
 725                   /*
 726                    * event_client: An event-driven client
 727                    */
 728                   static void WINAPI event_client ( client_params *par )
 729                   {
 730                       test_params *gen = par->general;
 731                       client_memory *mem;
 732                       int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
 733                           tmp, err, n;
 734                       HANDLE event;
 735                       WSANETWORKEVENTS wsa_events;
 736                       char *send_last, *recv_last, *send_p, *recv_p;
 737                       long mask = FD_READ | FD_WRITE | FD_CLOSE;
 738                   
 739                       trace ( "event_client (%x): starting\n", id );
 740                       client_start ( par );
 741                       trace ( "event_client (%x): server ready\n", id );
 742                   
 743                       mem = TlsGetValue ( tls );
 744                   
 745 cs130_ryutaro 1.1     /* Prepare event notification for connect, makes socket nonblocking */
 746                       event = WSACreateEvent ();
 747                       WSAEventSelect ( mem->s, event, FD_CONNECT );
 748                       tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
 749                       if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
 750                           ok ( 0, "event_client (%x): connect error: %d\n", id, err );
 751                   
 752                       tmp = WaitForSingleObject ( event, INFINITE );
 753                       ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
 754                       err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 755                       wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 756                   
 757                       err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
 758                       ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
 759                       if ( err ) goto out;
 760                   
 761                       trace ( "event_client (%x) connected\n", id );
 762                   
 763                       WSAEventSelect ( mem->s, event, mask );
 764                   
 765                       recv_p = mem->recv_buf;
 766 cs130_ryutaro 1.1     recv_last = mem->recv_buf + n_expected;
 767                       send_p = mem->send_buf;
 768                       send_last = mem->send_buf + n_expected;
 769                   
 770                       while ( TRUE )
 771                       {
 772                           err = WaitForSingleObject ( event, INFINITE );
 773                           ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
 774                   
 775                           err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 776                           wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 777                   
 778                           if ( wsa_events.lNetworkEvents & FD_WRITE )
 779                           {
 780                               err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
 781                               ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
 782                   
 783                               if ( err== 0 )
 784                                   do
 785                                   {
 786                                       n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
 787 cs130_ryutaro 1.1                     if ( n < 0 )
 788                                       {
 789                                           err = WSAGetLastError ();
 790                                           ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
 791                                       }
 792                                       else
 793                                           send_p += n;
 794                                   }
 795                                   while ( n >= 0 && send_p < send_last );
 796                   
 797                               if ( send_p == send_last )
 798                               {
 799                                   trace ( "event_client (%x): all data sent - shutdown\n", id );
 800                                   shutdown ( mem->s, SD_SEND );
 801                                   mask &= ~FD_WRITE;
 802                                   WSAEventSelect ( mem->s, event, mask );
 803                               }
 804                           }
 805                           if ( wsa_events.lNetworkEvents & FD_READ )
 806                           {
 807                               err = wsa_events.iErrorCode[ FD_READ_BIT ];
 808 cs130_ryutaro 1.1             ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
 809                               if ( err != 0 ) break;
 810                               
 811                               /* First read must succeed */
 812                               n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
 813                               wsa_ok ( n, 0 <=, "event_client (%lx): recv error: %d\n" );
 814                   
 815                               while ( n >= 0 ) {
 816                                   recv_p += n;
 817                                   if ( recv_p == recv_last )
 818                                   {
 819                                       mask &= ~FD_READ;
 820                                       trace ( "event_client (%x): all data received\n", id );
 821                                       WSAEventSelect ( mem->s, event, mask );
 822                                       break;
 823                                   }
 824                                   n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
 825                                   if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
 826                                       ok ( 0, "event_client (%x): read error: %d\n", id, err );
 827                                   
 828                               }
 829 cs130_ryutaro 1.1         }   
 830                           if ( wsa_events.lNetworkEvents & FD_CLOSE )
 831                           {
 832                               trace ( "event_client (%x): close event\n", id );
 833                               err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
 834                               ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
 835                               break;
 836                           }
 837                       }
 838                   
 839                       ok ( send_p == send_last,
 840                            "simple_client (%x): sent less data than expected: %d of %d\n",
 841                            id, send_p - mem->send_buf, n_expected );
 842                       ok ( recv_p == recv_last,
 843                            "simple_client (%x): received less data than expected: %d of %d\n",
 844                            id, recv_p - mem->recv_buf, n_expected );
 845                       recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
 846                       ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
 847                   
 848                   out:
 849                       WSACloseEvent ( event );
 850 cs130_ryutaro 1.1     trace ( "event_client (%x) exiting\n", id );
 851                       client_stop ();
 852                   }
 853                   
 854                   /**************** Main program utility functions ***************/
 855                   
 856                   static void Init (void)
 857                   {
 858                       WORD ver = MAKEWORD (2, 2);
 859                       WSADATA data;
 860                   
 861                       ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
 862                       tls = TlsAlloc();
 863                   }
 864                   
 865                   static void Exit (void)
 866                   {
 867                       TlsFree ( tls );
 868                       ok ( WSACleanup() == 0, "WSACleanup failed\n" );
 869                   }
 870                   
 871 cs130_ryutaro 1.1 static void StartServer (LPTHREAD_START_ROUTINE routine,
 872                                            test_params *general, server_params *par)
 873                   {
 874                       par->general = general;
 875                       thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
 876                       ok ( thread[0] != NULL, "Failed to create server thread\n" );
 877                   }
 878                   
 879                   static void StartClients (LPTHREAD_START_ROUTINE routine,
 880                                             test_params *general, client_params *par)
 881                   {
 882                       int i;
 883                       par->general = general;
 884                       for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
 885                       {
 886                           client_id = i - 1;
 887                           thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
 888                           ok ( thread[i] != NULL, "Failed to create client thread\n" );
 889                           /* Make sure the client is up and running */
 890                           WaitForSingleObject ( client_ready[client_id], INFINITE );
 891                       };
 892 cs130_ryutaro 1.1 }
 893                   
 894                   static void do_test( test_setup *test )
 895                   {
 896                       DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
 897                       DWORD wait;
 898                   
 899                       server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
 900                       for (i = 0; i <= n; i++)
 901                           client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
 902                   
 903                       StartServer ( test->srv, &test->general, &test->srv_params );
 904                       StartClients ( test->clt, &test->general, &test->clt_params );
 905                       WaitForSingleObject ( server_ready, INFINITE );
 906                   
 907                       wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
 908                       ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n , 
 909                            "some threads have not completed: %lx\n", wait );
 910                   
 911                       if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
 912                       {
 913 cs130_ryutaro 1.1         for (i = 0; i <= n; i++)
 914                           {
 915                               trace ("terminating thread %08lx\n", thread_id[i]);
 916                               if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
 917                                   TerminateThread ( thread [i], 0 );
 918                           }
 919                       }
 920                       CloseHandle ( server_ready );
 921                       for (i = 0; i <= n; i++)
 922                           CloseHandle ( client_ready[i] );
 923                   }
 924                   
 925                   /********* some tests for getsockopt(setsockopt(X)) == X ***********/
 926                   /* optname = SO_LINGER */
 927                   LINGER linger_testvals[] = {
 928                       {0,0},
 929                       {0,73}, 
 930                       {1,0},
 931                       {5,189}
 932                   };
 933                   
 934 cs130_ryutaro 1.1 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
 935                   #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
 936                                           bug in the linux kernel (fixed in 2.6.8) */ 
 937                   #define SOCKTIMEOUT2 997000 /* 997 seconds */
 938                   
 939                   static void test_set_getsockopt()
 940                   {
 941                       SOCKET s;
 942                       int i, err;
 943                       int timeout;
 944                       LINGER lingval;
 945                       int size;
 946                   
 947                       s = socket(AF_INET, SOCK_STREAM, 0);
 948                       ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 949                       if( s == INVALID_SOCKET) return;
 950                       /* SO_RCVTIMEO */
 951                       timeout = SOCKTIMEOUT1;
 952                       size = sizeof(timeout);
 953                       err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size); 
 954                       if( !err)
 955 cs130_ryutaro 1.1         err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size); 
 956                       ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
 957                       ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
 958                       /* SO_SNDTIMEO */
 959                       timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */ 
 960                       size = sizeof(timeout);
 961                       err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size); 
 962                       if( !err)
 963                           err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size); 
 964                       ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
 965                       ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
 966                       /* SO_LINGER */
 967                       for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
 968                           size =  sizeof(lingval);
 969                           lingval = linger_testvals[i];
 970                           err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size); 
 971                           if( !err)
 972                               err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size); 
 973                           ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
 974                           ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
 975                                   (lingval.l_linger == linger_testvals[i].l_linger ||
 976 cs130_ryutaro 1.1                  (!lingval.l_linger && !linger_testvals[i].l_onoff))
 977                                   , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d \n", i, 
 978                                    lingval.l_onoff, lingval.l_linger,
 979                                    linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
 980                       }
 981                       closesocket(s);
 982                   }
 983                   
 984                   static void test_so_reuseaddr()
 985                   {
 986                       struct sockaddr_in saddr;
 987                       SOCKET s1,s2;
 988                       unsigned int rc,reuse,size;
 989                   
 990                       saddr.sin_family      = AF_INET;
 991                       saddr.sin_port        = htons(9375);
 992                       saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 993                   
 994                       s1=socket(AF_INET, SOCK_STREAM, 0);
 995                       ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 996                       rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
 997 cs130_ryutaro 1.1     ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
 998                   
 999                       s2=socket(AF_INET, SOCK_STREAM, 0);
1000                       ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1001                   
1002                       reuse=0x1234;
1003                       size=sizeof(reuse);
1004                       rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1005                       ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1006                   
1007                       rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1008                       ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1009                   
1010                       reuse = 1;
1011                       rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1012                       ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1013                   
1014                       todo_wine {
1015                       rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1016                       ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1017                       }
1018 cs130_ryutaro 1.1 
1019                       closesocket(s2);
1020                       closesocket(s1);
1021                   }
1022                   
1023                   /************* Array containing the tests to run **********/
1024                   
1025                   #define STD_STREAM_SOCKET \
1026                               SOCK_STREAM, \
1027                               0, \
1028                               "127.0.0.1", \
1029                               9374
1030                   
1031                   static test_setup tests [NUM_TESTS] =
1032                   {
1033                       /* Test 0: synchronous client and server */
1034                       {
1035                           {
1036                               STD_STREAM_SOCKET,
1037                               2048,
1038                               16,
1039 cs130_ryutaro 1.1             2
1040                           },
1041                           simple_server,
1042                           {
1043                               NULL,
1044                               0,
1045                               64
1046                           },
1047                           simple_client,
1048                           {
1049                               NULL,
1050                               0,
1051                               128
1052                           }
1053                       },
1054                       /* Test 1: event-driven client, synchronous server */
1055                       {
1056                           {
1057                               STD_STREAM_SOCKET,
1058                               2048,
1059                               16,
1060 cs130_ryutaro 1.1             2
1061                           },
1062                           simple_server,
1063                           {
1064                               NULL,
1065                               0,
1066                               64
1067                           },
1068                           event_client,
1069                           {
1070                               NULL,
1071                               WSA_FLAG_OVERLAPPED,
1072                               128
1073                           }
1074                       },
1075                   
1076                       /* Test 2: synchronous client and asynchronous server */
1077                       {
1078                           {
1079                               STD_STREAM_SOCKET,
1080                               2048,
1081 cs130_ryutaro 1.1             16,
1082                               2
1083                           },
1084                           async_server,
1085                           {
1086                               NULL,
1087                               0,
1088                               64
1089                           },
1090                           simple_client,
1091                           {
1092                               NULL,
1093                               0,
1094                               128
1095                           }
1096                       }
1097                   };
1098                   
1099                   static void test_WSAAddressToStringA()
1100                   {
1101                       INT ret;
1102 cs130_ryutaro 1.1     DWORD len;
1103                       SOCKADDR_IN sockaddr;
1104                       CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1105                   
1106                       CHAR expect1[] = "0.0.0.0";
1107                       CHAR expect2[] = "255.255.255.255";
1108                       CHAR expect3[] = "0.0.0.0:65535";
1109                       CHAR expect4[] = "255.255.255.255:65535";
1110                   
1111                       len = 0;
1112                   
1113                       sockaddr.sin_family = AF_INET;
1114                       sockaddr.sin_port = 0;
1115                       sockaddr.sin_addr.s_addr = 0;
1116                   
1117                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1118                       ok( ret == SOCKET_ERROR, "WSAAddressToStringA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1119                   
1120                       len = sizeof(address);
1121                   
1122                       sockaddr.sin_family = AF_INET;
1123 cs130_ryutaro 1.1     sockaddr.sin_port = 0;
1124                       sockaddr.sin_addr.s_addr = 0;
1125                   
1126                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1127                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1128                   
1129                       ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1130                   
1131                       len = sizeof(address);
1132                   
1133                       sockaddr.sin_family = AF_INET;
1134                       sockaddr.sin_port = 0;
1135                       sockaddr.sin_addr.s_addr = 0xffffffff;
1136                   
1137                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1138                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1139                   
1140                       ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1141                   
1142                       len = sizeof(address);
1143                   
1144 cs130_ryutaro 1.1     sockaddr.sin_family = AF_INET;
1145                       sockaddr.sin_port = 0xffff;
1146                       sockaddr.sin_addr.s_addr = 0;
1147                   
1148                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1149                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1150                   
1151                       ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1152                   
1153                       len = sizeof(address);
1154                   
1155                       sockaddr.sin_family = AF_INET;
1156                       sockaddr.sin_port = 0xffff;
1157                       sockaddr.sin_addr.s_addr = 0xffffffff;
1158                   
1159                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1160                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1161                   
1162                       ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1163                   }
1164                   
1165 cs130_ryutaro 1.1 static void test_WSAAddressToStringW()
1166                   {
1167                       INT ret;
1168                       DWORD len;
1169                       SOCKADDR_IN sockaddr;
1170                       WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1171                   
1172                       WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1173                       WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1174                       WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1175                       WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1176                                           '6', '5', '5', '3', '5', 0 };
1177                   
1178                       len = 0;
1179                   
1180                       sockaddr.sin_family = AF_INET;
1181                       sockaddr.sin_port = 0;
1182                       sockaddr.sin_addr.s_addr = 0;
1183                   
1184                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1185                       ok( ret == SOCKET_ERROR, "WSAAddressToStringW() succeeded unexpectedly: %x\n", WSAGetLastError() );
1186 cs130_ryutaro 1.1 
1187                       len = sizeof(address);
1188                   
1189                       sockaddr.sin_family = AF_INET;
1190                       sockaddr.sin_port = 0;
1191                       sockaddr.sin_addr.s_addr = 0;
1192                   
1193                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1194                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1195                   
1196                       ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1197                   
1198                       len = sizeof(address);
1199                   
1200                       sockaddr.sin_family = AF_INET;
1201                       sockaddr.sin_port = 0;
1202                       sockaddr.sin_addr.s_addr = 0xffffffff;
1203                   
1204                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1205                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1206                   
1207 cs130_ryutaro 1.1     ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1208                   
1209                       len = sizeof(address);
1210                   
1211                       sockaddr.sin_family = AF_INET;
1212                       sockaddr.sin_port = 0xffff;
1213                       sockaddr.sin_addr.s_addr = 0;
1214                   
1215                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1216                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1217                   
1218                       ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1219                   
1220                       len = sizeof(address);
1221                   
1222                       sockaddr.sin_family = AF_INET;
1223                       sockaddr.sin_port = 0xffff;
1224                       sockaddr.sin_addr.s_addr = 0xffffffff;
1225                   
1226                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1227                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1228 cs130_ryutaro 1.1 
1229                       ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1230                   }
1231                   
1232                   static void test_WSAStringToAddressA()
1233                   {
1234                       INT ret, len;
1235                       SOCKADDR_IN sockaddr;
1236                   
1237                       CHAR address1[] = "0.0.0.0";
1238                       CHAR address2[] = "127.127.127.127";
1239                       CHAR address3[] = "255.255.255.255";
1240                       CHAR address4[] = "127.127.127.127:65535";
1241                       CHAR address5[] = "255.255.255.255:65535";
1242                   
1243                       len = 0;
1244                       sockaddr.sin_family = AF_INET;
1245                   
1246                       ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1247                       ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1248                           WSAGetLastError() );
1249 cs130_ryutaro 1.1 
1250                       len = sizeof(sockaddr);
1251                       sockaddr.sin_port = 0;
1252                       sockaddr.sin_addr.s_addr = 0;
1253                   
1254                       ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1255                       ok( !ret && sockaddr.sin_addr.s_addr == 0,
1256                           "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1257                   
1258                       len = sizeof(sockaddr);
1259                       sockaddr.sin_port = 0;
1260                       sockaddr.sin_addr.s_addr = 0;
1261                   
1262                       ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1263                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1264                           "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1265                   
1266                       len = sizeof(sockaddr);
1267                   
1268                       ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1269                       ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1270 cs130_ryutaro 1.1 
1271                       len = sizeof(sockaddr);
1272                       sockaddr.sin_port = 0;
1273                       sockaddr.sin_addr.s_addr = 0;
1274                   
1275                       ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1276                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1277                           "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1278                   
1279                       len = sizeof(sockaddr);
1280                   
1281                       ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1282                       ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1283                   }
1284                   
1285                   static void test_WSAStringToAddressW()
1286                   {
1287                       INT ret, len;
1288                       SOCKADDR_IN sockaddr;
1289                   
1290                       WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1291 cs130_ryutaro 1.1     WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1292                       WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1293                       WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1294                                            ':', '6', '5', '5', '3', '5', 0 };
1295                       WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1296                                            '6', '5', '5', '3', '5', 0 };
1297                   
1298                       len = 0;
1299                       sockaddr.sin_family = AF_INET;
1300                   
1301                       ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1302                       ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1303                           WSAGetLastError() );
1304                   
1305                       len = sizeof(sockaddr);
1306                       sockaddr.sin_port = 0;
1307                       sockaddr.sin_addr.s_addr = 0;
1308                   
1309                       ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1310                       ok( !ret && sockaddr.sin_addr.s_addr == 0,
1311                           "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1312 cs130_ryutaro 1.1 
1313                       len = sizeof(sockaddr);
1314                   
1315                       sockaddr.sin_port = 0;
1316                       sockaddr.sin_addr.s_addr = 0;
1317                   
1318                       ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1319                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1320                           "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1321                   
1322                       len = sizeof(sockaddr);
1323                   
1324                       ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1325                       ok( ret, "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1326                   
1327                       len = sizeof(sockaddr);
1328                       sockaddr.sin_port = 0;
1329                       sockaddr.sin_addr.s_addr = 0;
1330                   
1331                       ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1332                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1333 cs130_ryutaro 1.1         "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1334                   
1335                       len = sizeof(sockaddr);
1336                   
1337                       ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1338                       ok( ret, "WSAStringToAddressW() succeeded unexpectedly: %d\n", WSAGetLastError() );
1339                   }
1340                   
1341                   /**************** Main program  ***************/
1342                   
1343                   START_TEST( sock )
1344                   {
1345                       int i;
1346                       Init();
1347                   
1348                       test_set_getsockopt();
1349                       test_so_reuseaddr();
1350                   
1351                       for (i = 0; i < NUM_TESTS; i++)
1352                       {
1353                           trace ( " **** STARTING TEST %d **** \n", i );
1354 cs130_ryutaro 1.1         do_test (  &tests[i] );
1355                           trace ( " **** TEST %d COMPLETE **** \n", i );
1356                       }
1357                   
1358                       test_WSAAddressToStringA();
1359                       test_WSAAddressToStringW();
1360                   
1361                       test_WSAStringToAddressA();
1362                       test_WSAStringToAddressW();
1363                   
1364                       Exit();
1365                   }

Rizwan Kassim
Powered by
ViewCVS 0.9.2