(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 cs130_ryutaro 1.4   /* Looping through all the possible clients */
 295 cs130_ryutaro 1.3   int clientNum;
 296                     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
 297                   
 298 cs130_ryutaro 1.4     /* When the match is found, return the pointer to that sock_info struct */
 299 cs130_ryutaro 1.3     if( servMem->sock[clientNum].s == sockToFind ){
 300                   
 301                         return &(servMem->sock[clientNum]);
 302                   
 303                       }
 304                   
 305                     }
 306                   
 307 cs130_ryutaro 1.4   /* Did not find a match. */
 308 cs130_ryutaro 1.3   return NULL;
 309                   
 310                   }
 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 cs130_ryutaro 1.4   /* Loop through sock array of servMem */
 318 cs130_ryutaro 1.3   int clientNum;
 319                     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
 320                   
 321 cs130_ryutaro 1.4     /* when an open slot is found, return */
 322 cs130_ryutaro 1.3     if( servMem->sock[clientNum].s == INVALID_SOCKET )
 323                         return &(servMem->sock[clientNum]);
 324                   
 325                     }
 326                     
 327 cs130_ryutaro 1.4   /* When there is no more open socket, return NULL */
 328 cs130_ryutaro 1.3   return NULL;
 329                   
 330                   }
 331                   
 332                   /* this function returns TRUE if all sockets in servMem are INVALID_SOCKET */
 333                   static BOOL noOpenSock( server_memory* servMem )
 334                   {
 335                   
 336 cs130_ryutaro 1.4   /* Loop through sock array of servMem */
 337 cs130_ryutaro 1.3   int clientNum;
 338                     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
 339                   
 340 cs130_ryutaro 1.4     /* if any socket != INVALID_SOCKET, there is an open socket */
 341 cs130_ryutaro 1.3     if( servMem->sock[clientNum].s != INVALID_SOCKET )
 342                         return FALSE;
 343                   
 344                     }
 345                   
 346 cs130_ryutaro 1.4   /* no open sockets */
 347 cs130_ryutaro 1.3   return TRUE;
 348                   
 349                   }
 350                   
 351                   /* This function takes in pointer to sock_info structure of a client 
 352                      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 cs130_ryutaro 1.4   /* send any data that hasn't been sent yet */
 359 cs130_ryutaro 1.3   while( sockInfo->nsent < sockInfo->nread ){
 360                   
 361 cs130_ryutaro 1.4     /* find out the buffer length to send */
 362 cs130_ryutaro 1.3     sendLen = min( sockInfo->nread - sockInfo->nsent, servBufLen );
 363                   
 364 cs130_ryutaro 1.4     /* send the data */
 365 cs130_ryutaro 1.3     n_sent = send( sockInfo->s, sockInfo->buf + sockInfo->nsent, sendLen, 0 );
 366                   	  
 367 cs130_ryutaro 1.4     /* If there was an error, break */
 368 cs130_ryutaro 1.3     if( n_sent == SOCKET_ERROR )
 369                         break;
 370                   
 371 cs130_ryutaro 1.4     /* update the number bytes sent */
 372 cs130_ryutaro 1.3     sockInfo->nsent += n_sent;
 373                   
 374                     }
 375                   
 376 cs130_ryutaro 1.4   /* check the error */
 377 cs130_ryutaro 1.3   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 cs130_ryutaro 1.4 	  /* Find the next available socket */
 512 cs130_ryutaro 1.3 	  sockInfo = GetNextOpenSock( mem );
 513                   	  ok( sockInfo != NULL, "async_server (%x): no more open socket\n", id );
 514                   	  
 515 cs130_ryutaro 1.4 	  /* 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 cs130_ryutaro 1.4 	  /* check the address retrieved from accept() is valid */
 520 cs130_ryutaro 1.3 	  ok( peer.sin_addr.s_addr == inet_addr( inetAddr ), "async_server (%x): strange peer address\n", id );
 521                   
 522 cs130_ryutaro 1.4 	  /* make this newly accepted socket asynchronous */
 523 cs130_ryutaro 1.3 	  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 cs130_ryutaro 1.4 	  /* get the sock_info* of this client socket */
 531 cs130_ryutaro 1.3 	  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.4 	  /* try sending any remaining data */
 535 cs130_ryutaro 1.3 	  try_send_all_buf( sockInfo, servBufLen );	  
 536 cs130_ryutaro 1.1 	  break;
 537                   
 538                   	case FD_READ:
 539                   	  
 540 cs130_ryutaro 1.4 	  /* get the sock_info* of this client socket */
 541 cs130_ryutaro 1.3 	  sockInfo = GetClientSockInfo( mem, clientSock );
 542                   	  ok( sockInfo != NULL, "async_server (%x): socket to read from is not open\n", id );
 543                   	  
 544 cs130_ryutaro 1.4 	  /* retrieve the data */
 545 cs130_ryutaro 1.3 	  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.4 	  /* record number of bytes read so far */
 550 cs130_ryutaro 1.3 	  if( n_recvd != SOCKET_ERROR )
 551                   	    sockInfo->nread += n_recvd;
 552 cs130_ryutaro 1.1 
 553 cs130_ryutaro 1.4 	  /* send the remaining data */
 554 cs130_ryutaro 1.3 	  try_send_all_buf( sockInfo, servBufLen );
 555 cs130_ryutaro 1.1 	  break;
 556                   
 557                   	case FD_CLOSE:
 558                   
 559 cs130_ryutaro 1.4 	  /* 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.4 	  /* Check the data it received */
 565 cs130_ryutaro 1.3 	  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 cs130_ryutaro 1.4 	  /* clean up */
 573 cs130_ryutaro 1.3 	  wsa_ok( closesocket( sockInfo->s ), 0 ==, "async_server (%lx): closesocket error: %d\n" );
 574                   	  sockInfo->s = INVALID_SOCKET;
 575                   
 576 cs130_ryutaro 1.4 	  /* If all sockets are closed, kill the server */
 577 cs130_ryutaro 1.3 	  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.4   /* set up async_params structure */
 601 cs130_ryutaro 1.3   memset( &async_params, 0, sizeof( async_params ) );
 602                     async_params.svc_params = par;
 603                     async_params.mem = TlsGetValue( tls );
 604                    
 605                     wsa_ok( listen ( (async_params.mem)->s, SOMAXCONN ), 0 ==, "async_server (%lx): listen failed: %d\n");
 606 cs130_ryutaro 1.1 
 607 cs130_ryutaro 1.4   /* Get the current instance */
 608 cs130_ryutaro 1.1   HINSTANCE instance = GetModuleHandle( NULL );
 609 cs130_ryutaro 1.3   ok( instance != NULL, "async_server (%x):GetModuleHandle error %ld\n", id, GetLastError() );
 610 cs130_ryutaro 1.1 
 611 cs130_ryutaro 1.4   /* Create a hiddent window to handle async events */
 612 cs130_ryutaro 1.1   WNDCLASS windowClass;
 613                     windowClass.lpszClassName = "Hidden_Winsock_Window";
 614                     windowClass.style = CS_HREDRAW | CS_VREDRAW;
 615                     windowClass.lpfnWndProc = async_server_proc;
 616                     windowClass.cbClsExtra = 0;
 617                     windowClass.cbWndExtra = 0;
 618                     windowClass.hInstance = instance;
 619                     windowClass.hIcon = NULL;
 620                     windowClass.hCursor = NULL;
 621                     windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 622                     windowClass.lpszMenuName = NULL;
 623                     ATOM classAtom = RegisterClass( &windowClass );
 624 cs130_ryutaro 1.3   ok( classAtom != 0, "async_server (%x): RegisterClass error %ld\n", id, GetLastError() );
 625 cs130_ryutaro 1.1 
 626                     HWND hwnd = CreateWindow( "Hidden_Winsock_Window", 
 627                   			    "Winsock Window", 
 628                   			    WS_OVERLAPPEDWINDOW,
 629                   			    CW_USEDEFAULT, 
 630                   			    CW_USEDEFAULT,
 631                   			    CW_USEDEFAULT,
 632                   			    CW_USEDEFAULT,
 633                   			    NULL,
 634                   			    NULL,
 635                   			    instance,
 636                   			    NULL );
 637 cs130_ryutaro 1.3   ok( hwnd != NULL, "async_server (%x): CreateWindowd error %ld\n", id, GetLastError() );
 638 cs130_ryutaro 1.1 
 639 cs130_ryutaro 1.3   /* save hwnd */
 640                     async_params.hwnd = hwnd;
 641                     
 642 cs130_ryutaro 1.1   /* make the socket asynchronous */
 643 cs130_ryutaro 1.3   int err;
 644                     err = WSAAsyncSelect( (async_params.mem)->s, hwnd, ASYNC_EVENT, FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE );
 645 cs130_ryutaro 1.1   wsa_ok( err, SOCKET_ERROR !=, "async_server (%lx): WSAAsyncSelect failed: %d\n" );
 646                   
 647                     trace( "async_server (%x) ready\n", id );
 648                     SetEvent( server_ready ); /* notify clients */
 649                   
 650                     /* Wait for any event and process it when it occurs */
 651                     BOOL retVal;
 652                     MSG msg;
 653                     while( ( retVal = GetMessage( &msg, NULL, 0, 0 ) ) != 0 ){ 
 654 cs130_ryutaro 1.3  
 655                       ok( retVal != -1, "async_server (%x): GetMessage error %ld\n", id, GetLastError() ); 
 656 cs130_ryutaro 1.1     if( retVal == -1 ){
 657                   
 658 cs130_ryutaro 1.4       /* exit so that the server won't block */
 659 cs130_ryutaro 1.3       trace( "async_server (%x): exiting\n", id );
 660 cs130_ryutaro 1.1       server_stop();
 661                   
 662                       }
 663                   
 664 cs130_ryutaro 1.4     /* Translate and dispatch the message */
 665 cs130_ryutaro 1.1     TranslateMessage( &msg ); 
 666                       DispatchMessage( &msg );
 667                   
 668                     }
 669                   }
 670                   
 671                   /**************** Clients ***************/
 672                   
 673                   /*
 674                    * simple_client: A very basic client doing synchronous IO.
 675                    */
 676                   static VOID WINAPI simple_client ( client_params *par )
 677                   {
 678                       test_params *gen = par->general;
 679                       client_memory *mem;
 680                       int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
 681                       char *p;
 682                   
 683                       id = GetCurrentThreadId();
 684                       trace ( "simple_client (%x): starting\n", id );
 685                       /* wait here because we want to call set_so_opentype before creating a socket */
 686 cs130_ryutaro 1.1     WaitForSingleObject ( server_ready, INFINITE );
 687                       trace ( "simple_client (%x): server ready\n", id );
 688                   
 689                       check_so_opentype ();
 690                       set_so_opentype ( FALSE ); /* non-overlapped */
 691                       client_start ( par );
 692                       mem = TlsGetValue ( tls );
 693                   
 694                       /* Connect */
 695                       wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
 696                                0 ==, "simple_client (%lx): connect error: %d\n" );
 697                       ok ( set_blocking ( mem->s, TRUE ) == 0,
 698                            "simple_client (%x): failed to set blocking mode\n", id );
 699                       trace ( "simple_client (%x) connected\n", id );
 700                   
 701                       /* send data to server */
 702                       n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
 703                       ok ( n_sent == n_expected,
 704                            "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
 705                   
 706                       /* shutdown send direction */
 707 cs130_ryutaro 1.1     wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
 708                   
 709                       /* Receive data echoed back & check it */
 710                       n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
 711                       ok ( n_recvd == n_expected,
 712                            "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
 713                   
 714                       /* check data */
 715                       p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
 716                       ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
 717                   
 718                       /* cleanup */
 719                       read_zero_bytes ( mem->s );
 720                       trace ( "simple_client (%x) exiting\n", id );
 721                       client_stop ();
 722                   }
 723                   
 724                   /*
 725                    * event_client: An event-driven client
 726                    */
 727                   static void WINAPI event_client ( client_params *par )
 728 cs130_ryutaro 1.1 {
 729                       test_params *gen = par->general;
 730                       client_memory *mem;
 731                       int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
 732                           tmp, err, n;
 733                       HANDLE event;
 734                       WSANETWORKEVENTS wsa_events;
 735                       char *send_last, *recv_last, *send_p, *recv_p;
 736                       long mask = FD_READ | FD_WRITE | FD_CLOSE;
 737                   
 738                       trace ( "event_client (%x): starting\n", id );
 739                       client_start ( par );
 740                       trace ( "event_client (%x): server ready\n", id );
 741                   
 742                       mem = TlsGetValue ( tls );
 743                   
 744                       /* Prepare event notification for connect, makes socket nonblocking */
 745                       event = WSACreateEvent ();
 746                       WSAEventSelect ( mem->s, event, FD_CONNECT );
 747                       tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
 748                       if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
 749 cs130_ryutaro 1.1         ok ( 0, "event_client (%x): connect error: %d\n", id, err );
 750                   
 751                       tmp = WaitForSingleObject ( event, INFINITE );
 752                       ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
 753                       err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 754                       wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 755                   
 756                       err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
 757                       ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
 758                       if ( err ) goto out;
 759                   
 760                       trace ( "event_client (%x) connected\n", id );
 761                   
 762                       WSAEventSelect ( mem->s, event, mask );
 763                   
 764                       recv_p = mem->recv_buf;
 765                       recv_last = mem->recv_buf + n_expected;
 766                       send_p = mem->send_buf;
 767                       send_last = mem->send_buf + n_expected;
 768                   
 769                       while ( TRUE )
 770 cs130_ryutaro 1.1     {
 771                           err = WaitForSingleObject ( event, INFINITE );
 772                           ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
 773                   
 774                           err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
 775                           wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
 776                   
 777                           if ( wsa_events.lNetworkEvents & FD_WRITE )
 778                           {
 779                               err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
 780                               ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
 781                   
 782                               if ( err== 0 )
 783                                   do
 784                                   {
 785                                       n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
 786                                       if ( n < 0 )
 787                                       {
 788                                           err = WSAGetLastError ();
 789                                           ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
 790                                       }
 791 cs130_ryutaro 1.1                     else
 792                                           send_p += n;
 793                                   }
 794                                   while ( n >= 0 && send_p < send_last );
 795                   
 796                               if ( send_p == send_last )
 797                               {
 798                                   trace ( "event_client (%x): all data sent - shutdown\n", id );
 799                                   shutdown ( mem->s, SD_SEND );
 800                                   mask &= ~FD_WRITE;
 801                                   WSAEventSelect ( mem->s, event, mask );
 802                               }
 803                           }
 804                           if ( wsa_events.lNetworkEvents & FD_READ )
 805                           {
 806                               err = wsa_events.iErrorCode[ FD_READ_BIT ];
 807                               ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
 808                               if ( err != 0 ) break;
 809                               
 810                               /* First read must succeed */
 811                               n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
 812 cs130_ryutaro 1.1             wsa_ok ( n, 0 <=, "event_client (%lx): recv error: %d\n" );
 813                   
 814                               while ( n >= 0 ) {
 815                                   recv_p += n;
 816                                   if ( recv_p == recv_last )
 817                                   {
 818                                       mask &= ~FD_READ;
 819                                       trace ( "event_client (%x): all data received\n", id );
 820                                       WSAEventSelect ( mem->s, event, mask );
 821                                       break;
 822                                   }
 823                                   n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
 824                                   if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
 825                                       ok ( 0, "event_client (%x): read error: %d\n", id, err );
 826                                   
 827                               }
 828                           }   
 829                           if ( wsa_events.lNetworkEvents & FD_CLOSE )
 830                           {
 831                               trace ( "event_client (%x): close event\n", id );
 832                               err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
 833 cs130_ryutaro 1.1             ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
 834                               break;
 835                           }
 836                       }
 837                   
 838                       ok ( send_p == send_last,
 839                            "simple_client (%x): sent less data than expected: %d of %d\n",
 840                            id, send_p - mem->send_buf, n_expected );
 841                       ok ( recv_p == recv_last,
 842                            "simple_client (%x): received less data than expected: %d of %d\n",
 843                            id, recv_p - mem->recv_buf, n_expected );
 844                       recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
 845                       ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
 846                   
 847                   out:
 848                       WSACloseEvent ( event );
 849                       trace ( "event_client (%x) exiting\n", id );
 850                       client_stop ();
 851                   }
 852                   
 853                   /**************** Main program utility functions ***************/
 854 cs130_ryutaro 1.1 
 855                   static void Init (void)
 856                   {
 857                       WORD ver = MAKEWORD (2, 2);
 858                       WSADATA data;
 859                   
 860                       ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
 861                       tls = TlsAlloc();
 862                   }
 863                   
 864                   static void Exit (void)
 865                   {
 866                       TlsFree ( tls );
 867                       ok ( WSACleanup() == 0, "WSACleanup failed\n" );
 868                   }
 869                   
 870                   static void StartServer (LPTHREAD_START_ROUTINE routine,
 871                                            test_params *general, server_params *par)
 872                   {
 873                       par->general = general;
 874                       thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
 875 cs130_ryutaro 1.1     ok ( thread[0] != NULL, "Failed to create server thread\n" );
 876                   }
 877                   
 878                   static void StartClients (LPTHREAD_START_ROUTINE routine,
 879                                             test_params *general, client_params *par)
 880                   {
 881                       int i;
 882                       par->general = general;
 883                       for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
 884                       {
 885                           client_id = i - 1;
 886                           thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
 887                           ok ( thread[i] != NULL, "Failed to create client thread\n" );
 888                           /* Make sure the client is up and running */
 889                           WaitForSingleObject ( client_ready[client_id], INFINITE );
 890                       };
 891                   }
 892                   
 893                   static void do_test( test_setup *test )
 894                   {
 895                       DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
 896 cs130_ryutaro 1.1     DWORD wait;
 897                   
 898                       server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
 899                       for (i = 0; i <= n; i++)
 900                           client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
 901                   
 902                       StartServer ( test->srv, &test->general, &test->srv_params );
 903                       StartClients ( test->clt, &test->general, &test->clt_params );
 904                       WaitForSingleObject ( server_ready, INFINITE );
 905                   
 906                       wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
 907                       ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n , 
 908                            "some threads have not completed: %lx\n", wait );
 909                   
 910                       if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
 911                       {
 912                           for (i = 0; i <= n; i++)
 913                           {
 914                               trace ("terminating thread %08lx\n", thread_id[i]);
 915                               if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
 916                                   TerminateThread ( thread [i], 0 );
 917 cs130_ryutaro 1.1         }
 918                       }
 919                       CloseHandle ( server_ready );
 920                       for (i = 0; i <= n; i++)
 921                           CloseHandle ( client_ready[i] );
 922                   }
 923                   
 924                   /********* some tests for getsockopt(setsockopt(X)) == X ***********/
 925                   /* optname = SO_LINGER */
 926                   LINGER linger_testvals[] = {
 927                       {0,0},
 928                       {0,73}, 
 929                       {1,0},
 930                       {5,189}
 931                   };
 932                   
 933                   /* optname = SO_RCVTIMEO, SOSNDTIMEO */
 934                   #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
 935                                           bug in the linux kernel (fixed in 2.6.8) */ 
 936                   #define SOCKTIMEOUT2 997000 /* 997 seconds */
 937                   
 938 cs130_ryutaro 1.1 static void test_set_getsockopt()
 939                   {
 940                       SOCKET s;
 941                       int i, err;
 942                       int timeout;
 943                       LINGER lingval;
 944                       int size;
 945                   
 946                       s = socket(AF_INET, SOCK_STREAM, 0);
 947                       ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 948                       if( s == INVALID_SOCKET) return;
 949                       /* SO_RCVTIMEO */
 950                       timeout = SOCKTIMEOUT1;
 951                       size = sizeof(timeout);
 952                       err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size); 
 953                       if( !err)
 954                           err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size); 
 955                       ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
 956                       ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
 957                       /* SO_SNDTIMEO */
 958                       timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */ 
 959 cs130_ryutaro 1.1     size = sizeof(timeout);
 960                       err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size); 
 961                       if( !err)
 962                           err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size); 
 963                       ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
 964                       ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
 965                       /* SO_LINGER */
 966                       for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
 967                           size =  sizeof(lingval);
 968                           lingval = linger_testvals[i];
 969                           err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size); 
 970                           if( !err)
 971                               err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size); 
 972                           ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
 973                           ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
 974                                   (lingval.l_linger == linger_testvals[i].l_linger ||
 975                                    (!lingval.l_linger && !linger_testvals[i].l_onoff))
 976                                   , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d \n", i, 
 977                                    lingval.l_onoff, lingval.l_linger,
 978                                    linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
 979                       }
 980 cs130_ryutaro 1.1     closesocket(s);
 981                   }
 982                   
 983                   static void test_so_reuseaddr()
 984                   {
 985                       struct sockaddr_in saddr;
 986                       SOCKET s1,s2;
 987                       unsigned int rc,reuse,size;
 988                   
 989                       saddr.sin_family      = AF_INET;
 990                       saddr.sin_port        = htons(9375);
 991                       saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 992                   
 993                       s1=socket(AF_INET, SOCK_STREAM, 0);
 994                       ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
 995                       rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
 996                       ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
 997                   
 998                       s2=socket(AF_INET, SOCK_STREAM, 0);
 999                       ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1000                   
1001 cs130_ryutaro 1.1     reuse=0x1234;
1002                       size=sizeof(reuse);
1003                       rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1004                       ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1005                   
1006                       rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1007                       ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1008                   
1009                       reuse = 1;
1010                       rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1011                       ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1012                   
1013                       todo_wine {
1014                       rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1015                       ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1016                       }
1017                   
1018                       closesocket(s2);
1019                       closesocket(s1);
1020                   }
1021                   
1022 cs130_ryutaro 1.1 /************* Array containing the tests to run **********/
1023                   
1024                   #define STD_STREAM_SOCKET \
1025                               SOCK_STREAM, \
1026                               0, \
1027                               "127.0.0.1", \
1028                               9374
1029                   
1030                   static test_setup tests [NUM_TESTS] =
1031                   {
1032                       /* Test 0: synchronous client and server */
1033                       {
1034                           {
1035                               STD_STREAM_SOCKET,
1036                               2048,
1037                               16,
1038                               2
1039                           },
1040                           simple_server,
1041                           {
1042                               NULL,
1043 cs130_ryutaro 1.1             0,
1044                               64
1045                           },
1046                           simple_client,
1047                           {
1048                               NULL,
1049                               0,
1050                               128
1051                           }
1052                       },
1053                       /* Test 1: event-driven client, synchronous server */
1054                       {
1055                           {
1056                               STD_STREAM_SOCKET,
1057                               2048,
1058                               16,
1059                               2
1060                           },
1061                           simple_server,
1062                           {
1063                               NULL,
1064 cs130_ryutaro 1.1             0,
1065                               64
1066                           },
1067                           event_client,
1068                           {
1069                               NULL,
1070                               WSA_FLAG_OVERLAPPED,
1071                               128
1072                           }
1073                       },
1074                   
1075                       /* Test 2: synchronous client and asynchronous server */
1076                       {
1077                           {
1078                               STD_STREAM_SOCKET,
1079                               2048,
1080                               16,
1081                               2
1082                           },
1083                           async_server,
1084                           {
1085 cs130_ryutaro 1.1             NULL,
1086                               0,
1087                               64
1088                           },
1089                           simple_client,
1090                           {
1091                               NULL,
1092                               0,
1093                               128
1094                           }
1095                       }
1096                   };
1097                   
1098                   static void test_WSAAddressToStringA()
1099                   {
1100                       INT ret;
1101                       DWORD len;
1102                       SOCKADDR_IN sockaddr;
1103                       CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1104                   
1105                       CHAR expect1[] = "0.0.0.0";
1106 cs130_ryutaro 1.1     CHAR expect2[] = "255.255.255.255";
1107                       CHAR expect3[] = "0.0.0.0:65535";
1108                       CHAR expect4[] = "255.255.255.255:65535";
1109                   
1110                       len = 0;
1111                   
1112                       sockaddr.sin_family = AF_INET;
1113                       sockaddr.sin_port = 0;
1114                       sockaddr.sin_addr.s_addr = 0;
1115                   
1116                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1117                       ok( ret == SOCKET_ERROR, "WSAAddressToStringA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1118                   
1119                       len = sizeof(address);
1120                   
1121                       sockaddr.sin_family = AF_INET;
1122                       sockaddr.sin_port = 0;
1123                       sockaddr.sin_addr.s_addr = 0;
1124                   
1125                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1126                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1127 cs130_ryutaro 1.1 
1128                       ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1129                   
1130                       len = sizeof(address);
1131                   
1132                       sockaddr.sin_family = AF_INET;
1133                       sockaddr.sin_port = 0;
1134                       sockaddr.sin_addr.s_addr = 0xffffffff;
1135                   
1136                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1137                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1138                   
1139                       ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1140                   
1141                       len = sizeof(address);
1142                   
1143                       sockaddr.sin_family = AF_INET;
1144                       sockaddr.sin_port = 0xffff;
1145                       sockaddr.sin_addr.s_addr = 0;
1146                   
1147                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1148 cs130_ryutaro 1.1     ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1149                   
1150                       ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1151                   
1152                       len = sizeof(address);
1153                   
1154                       sockaddr.sin_family = AF_INET;
1155                       sockaddr.sin_port = 0xffff;
1156                       sockaddr.sin_addr.s_addr = 0xffffffff;
1157                   
1158                       ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1159                       ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1160                   
1161                       ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1162                   }
1163                   
1164                   static void test_WSAAddressToStringW()
1165                   {
1166                       INT ret;
1167                       DWORD len;
1168                       SOCKADDR_IN sockaddr;
1169 cs130_ryutaro 1.1     WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1170                   
1171                       WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1172                       WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1173                       WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1174                       WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1175                                           '6', '5', '5', '3', '5', 0 };
1176                   
1177                       len = 0;
1178                   
1179                       sockaddr.sin_family = AF_INET;
1180                       sockaddr.sin_port = 0;
1181                       sockaddr.sin_addr.s_addr = 0;
1182                   
1183                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1184                       ok( ret == SOCKET_ERROR, "WSAAddressToStringW() succeeded unexpectedly: %x\n", WSAGetLastError() );
1185                   
1186                       len = sizeof(address);
1187                   
1188                       sockaddr.sin_family = AF_INET;
1189                       sockaddr.sin_port = 0;
1190 cs130_ryutaro 1.1     sockaddr.sin_addr.s_addr = 0;
1191                   
1192                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1193                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1194                   
1195                       ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1196                   
1197                       len = sizeof(address);
1198                   
1199                       sockaddr.sin_family = AF_INET;
1200                       sockaddr.sin_port = 0;
1201                       sockaddr.sin_addr.s_addr = 0xffffffff;
1202                   
1203                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1204                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1205                   
1206                       ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1207                   
1208                       len = sizeof(address);
1209                   
1210                       sockaddr.sin_family = AF_INET;
1211 cs130_ryutaro 1.1     sockaddr.sin_port = 0xffff;
1212                       sockaddr.sin_addr.s_addr = 0;
1213                   
1214                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1215                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1216                   
1217                       ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1218                   
1219                       len = sizeof(address);
1220                   
1221                       sockaddr.sin_family = AF_INET;
1222                       sockaddr.sin_port = 0xffff;
1223                       sockaddr.sin_addr.s_addr = 0xffffffff;
1224                   
1225                       ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1226                       ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1227                   
1228                       ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1229                   }
1230                   
1231                   static void test_WSAStringToAddressA()
1232 cs130_ryutaro 1.1 {
1233                       INT ret, len;
1234                       SOCKADDR_IN sockaddr;
1235                   
1236                       CHAR address1[] = "0.0.0.0";
1237                       CHAR address2[] = "127.127.127.127";
1238                       CHAR address3[] = "255.255.255.255";
1239                       CHAR address4[] = "127.127.127.127:65535";
1240                       CHAR address5[] = "255.255.255.255:65535";
1241                   
1242                       len = 0;
1243                       sockaddr.sin_family = AF_INET;
1244                   
1245                       ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1246                       ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1247                           WSAGetLastError() );
1248                   
1249                       len = sizeof(sockaddr);
1250                       sockaddr.sin_port = 0;
1251                       sockaddr.sin_addr.s_addr = 0;
1252                   
1253 cs130_ryutaro 1.1     ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1254                       ok( !ret && sockaddr.sin_addr.s_addr == 0,
1255                           "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1256                   
1257                       len = sizeof(sockaddr);
1258                       sockaddr.sin_port = 0;
1259                       sockaddr.sin_addr.s_addr = 0;
1260                   
1261                       ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1262                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1263                           "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1264                   
1265                       len = sizeof(sockaddr);
1266                   
1267                       ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1268                       ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1269                   
1270                       len = sizeof(sockaddr);
1271                       sockaddr.sin_port = 0;
1272                       sockaddr.sin_addr.s_addr = 0;
1273                   
1274 cs130_ryutaro 1.1     ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1275                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1276                           "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1277                   
1278                       len = sizeof(sockaddr);
1279                   
1280                       ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1281                       ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1282                   }
1283                   
1284                   static void test_WSAStringToAddressW()
1285                   {
1286                       INT ret, len;
1287                       SOCKADDR_IN sockaddr;
1288                   
1289                       WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1290                       WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1291                       WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1292                       WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1293                                            ':', '6', '5', '5', '3', '5', 0 };
1294                       WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1295 cs130_ryutaro 1.1                          '6', '5', '5', '3', '5', 0 };
1296                   
1297                       len = 0;
1298                       sockaddr.sin_family = AF_INET;
1299                   
1300                       ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1301                       ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1302                           WSAGetLastError() );
1303                   
1304                       len = sizeof(sockaddr);
1305                       sockaddr.sin_port = 0;
1306                       sockaddr.sin_addr.s_addr = 0;
1307                   
1308                       ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1309                       ok( !ret && sockaddr.sin_addr.s_addr == 0,
1310                           "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1311                   
1312                       len = sizeof(sockaddr);
1313                   
1314                       sockaddr.sin_port = 0;
1315                       sockaddr.sin_addr.s_addr = 0;
1316 cs130_ryutaro 1.1 
1317                       ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1318                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1319                           "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1320                   
1321                       len = sizeof(sockaddr);
1322                   
1323                       ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1324                       ok( ret, "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1325                   
1326                       len = sizeof(sockaddr);
1327                       sockaddr.sin_port = 0;
1328                       sockaddr.sin_addr.s_addr = 0;
1329                   
1330                       ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1331                       ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1332                           "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1333                   
1334                       len = sizeof(sockaddr);
1335                   
1336                       ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1337 cs130_ryutaro 1.1     ok( ret, "WSAStringToAddressW() succeeded unexpectedly: %d\n", WSAGetLastError() );
1338                   }
1339                   
1340                   /**************** Main program  ***************/
1341                   
1342                   START_TEST( sock )
1343                   {
1344                       int i;
1345                       Init();
1346                   
1347                       test_set_getsockopt();
1348                       test_so_reuseaddr();
1349                   
1350                       for (i = 0; i < NUM_TESTS; i++)
1351                       {
1352                           trace ( " **** STARTING TEST %d **** \n", i );
1353                           do_test (  &tests[i] );
1354                           trace ( " **** TEST %d COMPLETE **** \n", i );
1355                       }
1356                   
1357                       test_WSAAddressToStringA();
1358 cs130_ryutaro 1.1     test_WSAAddressToStringW();
1359                   
1360                       test_WSAStringToAddressA();
1361                       test_WSAStringToAddressW();
1362                   
1363                       Exit();
1364                   }

Rizwan Kassim
Powered by
ViewCVS 0.9.2