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

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

Rizwan Kassim
Powered by
ViewCVS 0.9.2