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

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

Rizwan Kassim
Powered by
ViewCVS 0.9.2