version 1.16, 2005/03/10 07:36:31
|
version 1.19, 2005/03/15 16:35:46
|
|
|
* Unit test suite for winsock functions | * Unit test suite for winsock functions |
* | * |
* Copyright 2002 Martin Wilck | * Copyright 2002 Martin Wilck |
* select() server Copyright 2005 Thomas Kho |
* 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 |
|
|
#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_THREADS 3 /* Number of threads to run getservbyname */ |
#define NUM_QUERIES 500 /* Number of getservbyname queries per thread */ |
#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 { \ |
|
|
#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] = |
{ | { |
|
|
} | } |
}; | }; |
| |
|
static void test_UDP() |
|
{ |
|
/* peer 0 receives from all other peers */ |
|
struct sock_info peer[NUM_UDP_PEERS]; |
|
char buf[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 */ |
|
memcpy( buf, &peer[i].addr.sin_port, sizeof(peer[i].addr.sin_port) ); |
|
ok ( sendto ( peer[i].s, buf, sizeof(buf), 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, buf, sizeof(buf), 0,(struct sockaddr *) &peer[0].peer, &sl ) != SOCKET_ERROR, "UDP: socket error on recvfrom(): %d\n", WSAGetLastError() ); |
|
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 ) | static void WINAPI do_getservbyname( HANDLE *starttest ) |
{ | { |
int i; | int i; |
|
|
struct servent *pserv1, *pserv2; | struct servent *pserv1, *pserv2; |
| |
WaitForSingleObject ( *starttest, INFINITE ); | WaitForSingleObject ( *starttest, INFINITE ); |
| |
|
/* first time through, should resize buffer */ |
|
pserv1 = getservbyname ( "telnet", "tcp" ); |
|
pserv2 = getservbyname ( "chargen" , "udp" ); |
|
|
|
ok ( pserv1 != pserv2, "getservbyname: winsock did not reallocate servent buffer\n" ); |
|
|
for ( i = 0; i < NUM_QUERIES / 2; i++ ) { | for ( i = 0; i < NUM_QUERIES / 2; i++ ) { |
pserv1 = getservbyname ( "telnet", "tcp" ); | pserv1 = getservbyname ( "telnet", "tcp" ); |
ok ( pserv1 != NULL, "getservbyname could not retreive information for telnet: %d\n", WSAGetLastError() ); | ok ( pserv1 != NULL, "getservbyname could not retreive information for telnet: %d\n", WSAGetLastError() ); |
|
|
ok ( !strcmp ( pserv2->s_proto, "udp" ), "getservbyname returned the wrong protocol for domain: %s\n", pserv2->s_proto ); | 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 ( !strcmp ( pserv2->s_name, "domain" ), "getservbyname returned the wrong name for domain: %s\n", pserv2->s_name ); |
| |
ok ( pserv1 == pserv2, "winsock allocated more than one servent structure per thread\n" ); |
ok ( pserv1 == pserv2, "getservbyname: winsock resized servent buffer when not necessary\n" ); |
} | } |
} | } |
| |
|
|
trace ( " **** TEST %d COMPLETE **** \n", i ); | trace ( " **** TEST %d COMPLETE **** \n", i ); |
} | } |
| |
|
test_UDP(); |
|
|
test_getservbyname(); | test_getservbyname(); |
| |
test_WSAAddressToStringA(); | test_WSAAddressToStringA(); |