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

Diff for /wine4/wine/dlls/winsock/tests/sock.c between version 1.1 and 1.20

version 1.1, 2005/03/01 22:28:18 version 1.20, 2005/03/15 23:14:15
Line 2 
Line 2 
  * Unit test suite for winsock functions  * Unit test suite for winsock functions
  *  *
  * Copyright 2002 Martin Wilck  * Copyright 2002 Martin Wilck
    * Copyright 2005 Thomas Kho
  *  *
  * This library is free software; you can redistribute it and/or  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public  * modify it under the terms of the GNU Lesser General Public
Line 29 
Line 30 
 #include <winerror.h> #include <winerror.h>
  
 #define MAX_CLIENTS 4      /* Max number of clients */ #define MAX_CLIENTS 4      /* Max number of clients */
 #define NUM_TESTS   2      /* Number of tests performed */  #define NUM_TESTS   3      /* Number of tests performed */
 #define FIRST_CHAR 'A'     /* First character in transferred pattern */ #define FIRST_CHAR 'A'     /* First character in transferred pattern */
 #define BIND_SLEEP 10      /* seconds to wait between attempts to bind() */ #define BIND_SLEEP 10      /* seconds to wait between attempts to bind() */
 #define BIND_TRIES 6       /* Number of bind() attempts */ #define BIND_TRIES 6       /* Number of bind() attempts */
 #define TEST_TIMEOUT 30    /* seconds to wait before killing child threads #define TEST_TIMEOUT 30    /* seconds to wait before killing child threads
                               after server initialization, if something hangs */                               after server initialization, if something hangs */
  
   #define NUM_UDP_PEERS 3    /* Number of UDP sockets to create and test > 1 */
   
   #define NUM_THREADS 3      /* Number of threads to run getservbyname */
   #define NUM_QUERIES 250    /* Number of getservbyname queries per thread */
   
   #define SERVERIP "127.0.0.1"   /* IP to bind to */
   #define SERVERPORT 9374        /* Port number to bind to */
   
 #define wsa_ok(op, cond, msg) \ #define wsa_ok(op, cond, msg) \
    do { \    do { \
         int tmp, err = 0; \         int tmp, err = 0; \
Line 60 
Line 69 
     struct sockaddr_in     addr;     struct sockaddr_in     addr;
     struct sockaddr_in     peer;     struct sockaddr_in     peer;
     char                  *buf;     char                  *buf;
     int                    nread;      int                    n_recvd;
       int                    n_sent;
 } sock_info; } sock_info;
  
 /* Test parameters for both server & client */ /* Test parameters for both server & client */
Line 252 
Line 262 
     {     {
         mem->sock[i].s = INVALID_SOCKET;         mem->sock[i].s = INVALID_SOCKET;
         mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );         mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
         mem->sock[i].nread = 0;          mem->sock[i].n_recvd = 0;
           mem->sock[i].n_sent = 0;
     }     }
  
     if ( gen->sock_type == SOCK_STREAM )     if ( gen->sock_type == SOCK_STREAM )
Line 372 
Line 383 
     server_stop ();     server_stop ();
 } }
  
   /*
    * select_server: A non-blocking server.
    */
   static VOID WINAPI select_server ( server_params *par )
   {
       test_params *gen = par->general;
       server_memory *mem;
       int n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
           id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd,
           n_set, delta, n_ready;
       char *p;
       struct timeval timeout = {0,10}; /* wait for 10 milliseconds */
       fd_set fds_recv, fds_send, fds_openrecv, fds_opensend;
   
       trace ( "select_server (%x) starting\n", id );
   
       set_so_opentype ( FALSE ); /* non-overlapped */
       server_start ( par );
       mem = TlsGetValue ( tls );
   
       wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%lx): failed to set blocking mode: %d\n");
       wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%lx): listen failed: %d\n");
   
       trace ( "select_server (%x) ready\n", id );
       SetEvent ( server_ready ); /* notify clients */
   
       FD_ZERO ( &fds_openrecv );
       FD_ZERO ( &fds_recv );
       FD_ZERO ( &fds_send );
       FD_ZERO ( &fds_opensend );
   
       FD_SET ( mem->s, &fds_openrecv );
   
       while(1)
       {
           fds_recv = fds_openrecv;
           fds_send = fds_opensend;
   
           n_set = 0;
   
           wsa_ok ( ( n_ready = select ( 0, &fds_recv, &fds_send, NULL, &timeout ) ), SOCKET_ERROR !=,
               "select_server (%lx): select() failed: %d\n" );
   
           /* check for incoming requests */
           if ( FD_ISSET ( mem->s, &fds_recv ) ) {
               n_set += 1;
   
               trace ( "select_server (%x): accepting client connection\n", id );
   
               /* accept a single connection */
               tmp = sizeof ( mem->sock[n_connections].peer );
               mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp );
               wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%lx): accept() failed: %d\n" );
   
               ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
                   "select_server (%x): strange peer address\n", id );
   
               /* add to list of open connections */
               FD_SET ( mem->sock[n_connections].s, &fds_openrecv );
               FD_SET ( mem->sock[n_connections].s, &fds_opensend );
   
               n_connections++;
           }
   
           /* handle open requests */
   
           for ( i = 0; i < n_connections; i++ )
           {
               if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) {
                   n_set += 1;
   
                   if ( mem->sock[i].n_recvd < n_expected ) {
                       /* Receive data & check it */
                       n_recvd = recv ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_recvd, min ( n_expected - mem->sock[i].n_recvd, par->buflen ), 0 );
                       ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() );
                       mem->sock[i].n_recvd += n_recvd;
   
                       if ( mem->sock[i].n_recvd == n_expected ) {
                           p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks );
                           ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[i].buf );
                           FD_CLR ( mem->sock[i].s, &fds_openrecv );
                       }
   
                       ok ( mem->sock[i].n_recvd <= n_expected, "select_server (%x): received too many bytes: %d\n", id, mem->sock[i].n_recvd );
                   }
               }
   
               /* only echo back what we've received */
               delta = mem->sock[i].n_recvd - mem->sock[i].n_sent;
   
               if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) {
                   n_set += 1;
   
                   if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) {
                       /* Echo data back */
                       n_sent = send ( mem->sock[i].s, mem->sock[i].buf + mem->sock[i].n_sent, min ( delta, par->buflen ), 0 );
                       ok ( n_sent != SOCKET_ERROR, "select_server (%x): error in send(): %d\n", id, WSAGetLastError() );
                       mem->sock[i].n_sent += n_sent;
   
                       if ( mem->sock[i].n_sent == n_expected ) {
                           FD_CLR ( mem->sock[i].s, &fds_opensend );
                       }
   
                       ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent );
                   }
               }
           }
   
           /* check that select returned the correct number of ready sockets */
           ok ( ( n_set == n_ready ), "select_server (%x): select() returns wrong number of ready sockets\n", id );
   
           /* check if all clients are done */
           if ( ( fds_opensend.fd_count == 0 )
               && ( fds_openrecv.fd_count == 1 ) /* initial socket that accepts clients */
               && ( n_connections  == min ( gen->n_clients, MAX_CLIENTS ) ) ) {
               break;
           }
       }
   
       for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
       {
           /* cleanup */
           read_zero_bytes ( mem->sock[i].s );
           wsa_ok ( closesocket ( mem->sock[i].s ),  0 ==, "select_server (%lx): closesocket error: %d\n" );
           mem->sock[i].s = INVALID_SOCKET;
       }
   
       trace ( "select_server (%x) exiting\n", id );
       server_stop ();
   }
   
 /**************** Clients ***************/ /**************** Clients ***************/
  
 /* /*
Line 728 
Line 870 
 #define STD_STREAM_SOCKET \ #define STD_STREAM_SOCKET \
             SOCK_STREAM, \             SOCK_STREAM, \
             0, \             0, \
             "127.0.0.1", \              SERVERIP, \
             9374              SERVERPORT
  
 static test_setup tests [NUM_TESTS] = static test_setup tests [NUM_TESTS] =
 { {
Line 774 
Line 916 
             WSA_FLAG_OVERLAPPED,             WSA_FLAG_OVERLAPPED,
             128             128
         }         }
       },
       /* Test 2: synchronous client, non-blocking server via select() */
       {
           {
               STD_STREAM_SOCKET,
               2048,
               16,
               2
           },
           select_server,
           {
               NULL,
               0,
               64
           },
           simple_client,
           {
               NULL,
               0,
               128
           }
     }     }
 }; };
  
   static void test_UDP()
   {
       /* This function tests UDP sendto() and recvfrom(). UDP is unreliable, so it is
          possible that this test fails due to dropped packets. */
   
       /* peer 0 receives from all other peers */
       struct sock_info peer[NUM_UDP_PEERS];
       char buf[16];
       int ss, i, n_recv, n_sent;
   
       for ( i = NUM_UDP_PEERS - 1; i >= 0; i-- ) {
           ok ( ( peer[i].s = socket ( AF_INET, SOCK_DGRAM, 0 ) ) != INVALID_SOCKET, "UDP: socket failed\n" );
   
           peer[i].addr.sin_family         = AF_INET;
           peer[i].addr.sin_addr.s_addr    = inet_addr ( SERVERIP );
   
           if ( i == 0 ) {
               peer[i].addr.sin_port       = htons ( SERVERPORT + i );
           } else {
               peer[i].addr.sin_port       = htons ( 0 );
           }
   
           do_bind ( peer[i].s, (struct sockaddr *) &peer[i].addr, sizeof( peer[i].addr ) );
   
           /* test getsockname() to get peer's port */
           ss = sizeof ( peer[i].addr );
           ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &ss ) != SOCKET_ERROR, "UDP: could not getsockname()\n" );
           ok ( peer[i].addr.sin_port != htons ( 0 ), "UDP: bind() did not associate port\n" );
       }
   
       /* test getsockname() */
       ok ( peer[0].addr.sin_port == htons ( SERVERPORT ), "UDP: getsockname returned incorrect peer port\n" );
   
       for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
           /* send client's ip */
           memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) );
           n_sent = sendto ( peer[i].s, buf, sizeof(buf), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) );
           ok ( n_sent == sizeof(buf), "UDP: sendto() sent wrong amount of data or socket error: %d\n", n_sent );
       }
   
       for ( i = 1; i < NUM_UDP_PEERS; i++ ) {
           n_recv = recvfrom ( peer[0].s, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &ss );
           ok ( n_recv == sizeof(buf), "UDP: recvfrom() received wrong amount of data or socket error: %d\n", n_recv );
           ok ( memcmp ( &peer[0].peer.sin_port, buf, sizeof(peer[0].addr.sin_port) ) == 0, "UDP: port numbers do not match\n" );
       }
   }
   
   static void WINAPI do_getservbyname( HANDLE *starttest )
   {
       struct {
           char *name;
           char *proto;
           int port;
       } serv[2] = { {"domain", "udp", 53}, {"telnet", "tcp", 23} };
   
       int i, j;
       struct servent *pserv[2];
   
       ok ( WaitForSingleObject ( *starttest, TEST_TIMEOUT * 1000 ) != WAIT_TIMEOUT, "test_getservbyname: timeout waiting for start signal\n");
   
       // ensure that necessary buffer resizes are completed
       for ( j = 0; j < 2; j++) {
           pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
       }
   
       for ( i = 0; i < NUM_QUERIES / 2; i++ ) {
           for ( j = 0; j < 2; j++ ) {
               pserv[j] = getservbyname ( serv[j].name, serv[j].proto );
               ok ( pserv[j] != NULL, "getservbyname could not retreive information for %s: %d\n", serv[j].name, WSAGetLastError() );
               ok ( pserv[j]->s_port == htons(serv[j].port), "getservbyname returned the wrong port for %s: %d\n", serv[j].name, ntohs(pserv[j]->s_port) );
               ok ( !strcmp ( pserv[j]->s_proto, serv[j].proto ), "getservbyname returned the wrong protocol for %s: %s\n", serv[j].name, pserv[j]->s_proto );
               ok ( !strcmp ( pserv[j]->s_name, serv[j].name ), "getservbyname returned the wrong name for %s: %s\n", serv[j].name, pserv[j]->s_name );
           }
   
           ok ( pserv[0] == pserv[1], "getservbyname: winsock resized servent buffer when not necessary\n" );
       }
   }
   
   static void test_getservbyname()
   {
       int i;
       HANDLE starttest, thread[NUM_THREADS];
       DWORD thread_id[NUM_THREADS];
   
       starttest = CreateEvent ( NULL, 1, 0, "test_getservbyname_starttest" );
   
       /* create threads */
       for ( i = 0; i < NUM_THREADS; i++ ) {
           thread[i] = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE) &do_getservbyname, &starttest, 0, &thread_id[i] );
       }
   
       /* signal threads to start */
       SetEvent ( starttest );
   
       for ( i = 0; i < NUM_THREADS; i++) {
           WaitForSingleObject ( thread[i], TEST_TIMEOUT * 1000 );
       }
   }
   
 static void test_WSAAddressToStringA() static void test_WSAAddressToStringA()
 { {
     INT ret;     INT ret;
Line 1036 
Line 1298 
         trace ( " **** TEST %d COMPLETE **** \n", i );         trace ( " **** TEST %d COMPLETE **** \n", i );
     }     }
  
       test_UDP();
   
       test_getservbyname();
   
     test_WSAAddressToStringA();     test_WSAAddressToStringA();
     test_WSAAddressToStringW();     test_WSAAddressToStringW();
  


Legend:
Removed from v.1.1  
changed lines
  Added in v.1.20

Rizwan Kassim
Powered by
ViewCVS 0.9.2