version 1.3, 2005/03/06 21:46:38
|
version 1.17, 2005/03/15 15:48:17
|
|
|
#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; \ |
|
|
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 */ |
|
|
return p - buf; | return p - buf; |
} | } |
| |
static int do_nonblocking_send ( int id, SOCKET s, char *buf, int buflen, int sendlen ) |
|
{ |
|
char* last = buf + buflen, *p; |
|
int n = 1; |
|
for ( p = buf; p < last; p += n ) |
|
n = send ( s, p, min ( sendlen, last - p ), 0 ); |
|
wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" ); |
|
return p - buf; |
|
/* char *last = buf + buflen; |
|
static char *p[MAX_CLIENTS] = { NULL, NULL }; |
|
int n; |
|
|
|
trace ( "id = %d\n", id ); |
|
|
|
if ( p[id] == NULL ) { |
|
p[id] = buf; |
|
} |
|
|
|
n = send ( s, p[id], min ( sendlen, last - p[id] ), 0 ); |
|
p[id] += n; |
|
|
|
//wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" ); |
|
return p[id] - buf;*/ |
|
} |
|
|
|
static int do_nonblocking_recv ( int id, SOCKET s, char *buf, int buflen, int recvlen ) |
|
{ |
|
char* last = buf + buflen, *p; |
|
int n = 1; |
|
for ( p = buf; p < last; p += n ) |
|
n = recv ( s, p, min ( recvlen, last - p ), 0 ); |
|
wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" ); |
|
return p - buf; |
|
/* char *last = buf + buflen; |
|
static char *p[MAX_CLIENTS] = { NULL, NULL }; |
|
int n; |
|
|
|
if ( p[id] == NULL ) { |
|
p[id] = buf; |
|
} |
|
|
|
n = recv ( s, p[id], min ( recvlen, last - p[id] ), 0 ); |
|
p[id] += n; |
|
|
|
//wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" ); |
|
return p[id] - buf;*/ |
|
} |
|
|
|
/* | /* |
* Call this routine right after thread startup. | * Call this routine right after thread startup. |
* SO_OPENTYPE must by 0, regardless what the server did. | * SO_OPENTYPE must by 0, regardless what the server did. |
|
|
{ | { |
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 ) |
|
|
{ | { |
test_params *gen = par->general; | test_params *gen = par->general; |
server_memory *mem; | server_memory *mem; |
int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i, |
int n_expected = gen->n_chunks * gen->chunk_size, tmp, i, |
id = GetCurrentThreadId(), n_connections = 0, done = 0; |
id = GetCurrentThreadId(), n_connections = 0, n_sent, n_recvd, |
|
n_set, delta, n_ready; |
char *p; | char *p; |
struct timeval zerotime = {0,0}; |
struct timeval timeout = {0,10}; /* wait for 10 milliseconds */ |
fd_set fds_recv, fds_send, fds_openrecv, fds_opensend; | fd_set fds_recv, fds_send, fds_openrecv, fds_opensend; |
| |
trace ( "select_server (%x) starting\n", id ); | trace ( "select_server (%x) starting\n", id ); |
|
|
FD_ZERO ( &fds_recv ); | FD_ZERO ( &fds_recv ); |
FD_ZERO ( &fds_send ); | FD_ZERO ( &fds_send ); |
FD_ZERO ( &fds_opensend ); | FD_ZERO ( &fds_opensend ); |
|
|
FD_SET ( mem->s, &fds_openrecv ); | FD_SET ( mem->s, &fds_openrecv ); |
| |
while(1) | while(1) |
|
|
fds_recv = fds_openrecv; | fds_recv = fds_openrecv; |
fds_send = fds_opensend; | fds_send = fds_opensend; |
| |
select ( 0, &fds_recv, &fds_send, NULL, &zerotime ); |
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 */ | /* check for incoming requests */ |
if ( FD_ISSET ( mem->s, &fds_recv ) ) { | if ( FD_ISSET ( mem->s, &fds_recv ) ) { |
|
n_set += 1; |
|
|
trace ( "select_server (%x): accepting client connection\n", id ); | trace ( "select_server (%x): accepting client connection\n", id ); |
| |
/* accept a single connection */ | /* accept a single connection */ |
tmp = sizeof ( mem->sock[i].peer ); |
tmp = sizeof ( mem->sock[n_connections].peer ); |
mem->sock[i].s = accept ( mem->s, (struct sockaddr*) &mem->sock[i].peer, &tmp ); |
mem->sock[n_connections].s = accept ( mem->s, (struct sockaddr*) &mem->sock[n_connections].peer, &tmp ); |
wsa_ok ( mem->sock[i].s, INVALID_SOCKET !=, "select_server (%lx): accept failed: %d\n" ); |
wsa_ok ( mem->sock[n_connections].s, INVALID_SOCKET !=, "select_server (%lx): accept() failed: %d\n" ); |
| |
ok ( mem->sock[i].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ), |
ok ( mem->sock[n_connections].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ), |
"select_server (%x): strange peer address\n", id ); | "select_server (%x): strange peer address\n", id ); |
| |
/* add to list of open connections */ | /* add to list of open connections */ |
FD_SET ( mem->sock[i].s, &fds_openrecv ); |
FD_SET ( mem->sock[n_connections].s, &fds_openrecv ); |
FD_SET ( mem->sock[i].s, &fds_opensend ); |
FD_SET ( mem->sock[n_connections].s, &fds_opensend ); |
| |
n_connections++; | n_connections++; |
} | } |
|
|
| |
for ( i = 0; i < n_connections; i++ ) | for ( i = 0; i < n_connections; i++ ) |
{ | { |
if ( (mem->sock[i].nread < n_expected) |
if ( FD_ISSET( mem->sock[i].s, &fds_recv ) ) { |
&& FD_ISSET( mem->sock[i].s, &fds_recv ) ) { |
n_set += 1; |
/* more data to receive */ |
|
| |
|
if ( mem->sock[i].n_recvd < n_expected ) { |
/* Receive data & check it */ | /* Receive data & check it */ |
n_recvd = do_nonblocking_recv ( i, mem->sock[i].s, mem->sock[i].buf, n_expected, par->buflen ); |
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 == n_expected, |
ok ( n_recvd != SOCKET_ERROR, "select_server (%x): error in recv(): %d\n", id, WSAGetLastError() ); |
// "select_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); |
mem->sock[i].n_recvd += n_recvd; |
| |
if ( n_recvd == n_expected ) { |
if ( mem->sock[i].n_recvd == n_expected ) { |
p = test_buffer ( mem->sock[i].buf, gen->chunk_size, gen->n_chunks ); | 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); | 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 ); | FD_CLR ( mem->sock[i].s, &fds_openrecv ); |
} | } |
| |
} else if ( !FD_ISSET ( 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 ); |
&& FD_ISSET ( mem->sock[i].s, &fds_send ) ) { |
} |
/* send data */ |
} |
| |
/* Echo data back */ |
/* only echo back what we've received */ |
n_sent = do_nonblocking_send ( i, mem->sock[i].s, mem->sock[i].buf, n_expected, par->buflen ); |
delta = mem->sock[i].n_recvd - mem->sock[i].n_sent; |
|
|
|
if ( FD_ISSET ( mem->sock[i].s, &fds_send ) ) { |
|
n_set += 1; |
| |
//ok ( n_sent == n_expected, |
if ( ( delta > 0 ) && ( mem->sock[i].n_sent < n_expected ) ) { |
// "select_server (%x): sent less data than expected: %d of %d\n", id, 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 ( n_sent == n_expected ) { |
if ( mem->sock[i].n_sent == n_expected ) { |
FD_CLR ( mem->sock[i].s, &fds_opensend ); | FD_CLR ( mem->sock[i].s, &fds_opensend ); |
} | } |
} else if( FD_ISSET( mem->sock[i].s, &fds_recv ) ) { |
|
ok ( 0, "select_server (%x): too many bytes read\n", id ); |
|
} |
|
| |
if ( ( fds_opensend.fd_count == 0 ) |
ok ( mem->sock[i].n_sent <= n_expected, "select_server (%x): sent too many bytes: %d\n", id, mem->sock[i].n_sent ); |
&& ( n_connections == min ( gen->n_clients, MAX_CLIENTS ) ) ) { |
|
done = 1; |
|
break; |
|
} | } |
} | } |
|
} |
|
|
|
/* 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 ); |
| |
if ( done == 1 ) { |
/* 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; | break; |
} | } |
} | } |
|
|
#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] = |
{ | { |
|
|
128 | 128 |
} | } |
}, | }, |
/* Test 2: event-driven client, non-blocking server via select() */ |
/* Test 2: synchronous client, non-blocking server via select() */ |
{ | { |
{ | { |
STD_STREAM_SOCKET, | STD_STREAM_SOCKET, |
|
|
} | } |
}; | }; |
| |
|
static void test_UDP() |
|
{ |
|
/* peer 0 receives from all other peers */ |
|
struct sock_info peer[NUM_UDP_PEERS]; |
|
char testdata[16]; |
|
char recvbuf[16]; |
|
int sl, i; |
|
|
|
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 */ |
|
sl = sizeof ( peer[i].addr ); |
|
ok ( getsockname ( peer[i].s, (struct sockaddr *) &peer[i].addr, &sl ) != 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 */ |
|
strcpy ( testdata, inet_ntoa ( peer[i].addr.sin_addr ) ); |
|
ok ( sendto ( peer[i].s, testdata, sizeof(testdata), 0, (struct sockaddr*) &peer[0].addr, sizeof(peer[0].addr) ) != SOCKET_ERROR, "UDP: socket error on sendto(): %d\n", WSAGetLastError() ); |
|
} |
|
|
|
for ( i = 1; i < NUM_UDP_PEERS; i++ ) { |
|
ok ( recvfrom ( peer[0].s, recvbuf, sizeof(recvbuf), 0,(struct sockaddr *) &peer[0].peer, &sl ) != SOCKET_ERROR, "UDP: socket error on recvfrom(): %d\n", WSAGetLastError() ); |
|
ok ( ! strcmp ( inet_ntoa ( peer[0].peer.sin_addr), recvbuf ), "UDP: port numbers do not match\n" ); |
|
} |
|
} |
|
|
|
static void WINAPI do_getservbyname( HANDLE *starttest ) |
|
{ |
|
int i; |
|
struct servent *pserv1, *pserv2; |
|
|
|
WaitForSingleObject ( *starttest, INFINITE ); |
|
|
|
/* first time through, should resize buffer */ |
|
pserv1 = getservbyname ( "telnet", "tcp" ); |
|
pserv2 = getservbyname ( "kerberos" , "udp" ); |
|
ok ( pserv1 != pserv2, "getservbyname: winsock did not reallocate servent buffer\n" ); |
|
|
|
for ( i = 0; i < NUM_QUERIES / 2; i++ ) { |
|
pserv1 = getservbyname ( "telnet", "tcp" ); |
|
ok ( pserv1 != NULL, "getservbyname could not retreive information for telnet: %d\n", WSAGetLastError() ); |
|
ok ( pserv1->s_port == htons(23), "getservbyname returned the wrong port for telnet: %d\n", ntohs(pserv1->s_port) ); |
|
ok ( !strcmp ( pserv1->s_proto, "tcp" ), "getservbyname returned the wrong protocol for telnet: %s\n", pserv1->s_proto ); |
|
ok ( !strcmp ( pserv1->s_name, "telnet" ), "getservbyname returned the wrong name for telnet: %s\n", pserv1->s_name ); |
|
|
|
pserv2 = getservbyname ( "domain", "udp" ); |
|
ok ( pserv2 != NULL, "getservbyname could not retreive information for domain: %d\n", WSAGetLastError() ); |
|
ok ( pserv2->s_port == htons(53), "getservbyname returned the wrong port for domain: %d\n", ntohs(pserv2->s_port) ); |
|
ok ( !strcmp ( pserv2->s_proto, "udp" ), "getservbyname returned the wrong protocol for domain: %s\n", pserv2->s_proto ); |
|
ok ( !strcmp ( pserv2->s_name, "domain" ), "getservbyname returned the wrong name for domain: %s\n", pserv2->s_name ); |
|
|
|
ok ( pserv1 == pserv2, "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; |
|
|
trace ( " **** TEST %d COMPLETE **** \n", i ); | trace ( " **** TEST %d COMPLETE **** \n", i ); |
} | } |
| |
|
test_UDP(); |
|
|
|
test_getservbyname(); |
|
|
test_WSAAddressToStringA(); | test_WSAAddressToStringA(); |
test_WSAAddressToStringW(); | test_WSAAddressToStringW(); |
| |