mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-25 12:10:03 +00:00
added unit testing for port monitor stuff
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@362 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
parent
80cc3bdc70
commit
7148272c49
41
tests/Makefile
Normal file
41
tests/Makefile
Normal file
@ -0,0 +1,41 @@
|
||||
# --------------------------------------------------------------------
|
||||
# Philip Kovacs (kovacsp3@comcast.net) 2005
|
||||
# Make programs for testing libtcp-portmon and hash stuff up in ../src
|
||||
# Sorry no autoconf/automake for this stuff as of now.
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
#CFLAGS = -g -Wall
|
||||
CFLAGS = -g -Wall -DHASH_DEBUG
|
||||
CC = gcc
|
||||
|
||||
all: test-hash test-portmon
|
||||
|
||||
test-hash: test-hash.o hash.o
|
||||
$(CC) $(CFLAGS) -o $@ test-hash.o hash.o
|
||||
|
||||
test-portmon: test-portmon.o libtcp-portmon.o hash.o
|
||||
$(CC) $(CFLAGS) -o $@ test-portmon.o libtcp-portmon.o hash.o
|
||||
|
||||
test-hash.o: test-hash.c ../src/hash.h
|
||||
test-portmon.o: test-portmon.c ../src/libtcp-portmon.h ../src/hash.h
|
||||
|
||||
hash.h: ../src/hash.h
|
||||
cp ../src/hash.h .
|
||||
|
||||
hash.c: ../src/hash.c
|
||||
cp ../src/hash.c .
|
||||
|
||||
libtcp-portmon.h: ../src/libtcp-portmon.h
|
||||
cp ../src/libtcp-portmon.h .
|
||||
|
||||
libtcp-portmon.c: ../src/libtcp-portmon.c
|
||||
cp ../src/libtcp-portmon.c .
|
||||
|
||||
libtcp-portmon.o: libtcp-portmon.c libtcp-portmon.h
|
||||
hash.o: hash.c hash.h
|
||||
|
||||
.c.o:
|
||||
${CC} ${CFLAGS} -c $<
|
||||
|
||||
clean:
|
||||
/bin/rm -f *.o test-portmon test-hash hash.? libtcp-portmon.?
|
136
tests/test-hash.c
Normal file
136
tests/test-hash.c
Normal file
@ -0,0 +1,136 @@
|
||||
/* ------------------------------------------------------
|
||||
* test-hash.c: unit testing for hash functions in hash.h
|
||||
*
|
||||
* Philip Kovacs kovacsp3@comcast.net 2005
|
||||
* ------------------------------------------------------*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../src/hash.h"
|
||||
|
||||
char *data[] = {
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
|
||||
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
|
||||
"eighteen", "nineteen", "twenty", "twenty-one", "twenty-two", "twenty-three",
|
||||
"twenty-four", "twenty-five", "twenty-six", "twenty-seven", "twenty-eight",
|
||||
"twenty-nine", "thirty", "thirty-one", "thirty-two"
|
||||
};
|
||||
|
||||
/* Primary hash function is DJB with a 65521 prime modulus to govern the range. */
|
||||
unsigned int DJBHash(const char* str, unsigned int len)
|
||||
{
|
||||
unsigned int hash = 5381;
|
||||
unsigned int i = 0;
|
||||
|
||||
for(i = 0; i < len; str++, i++)
|
||||
{
|
||||
hash = ((hash << 5) + hash) + (*str);
|
||||
}
|
||||
|
||||
return (hash & 0x7FFFFFFF) % 65521;
|
||||
}
|
||||
|
||||
/* Second hash function is DEK, modified to always return an odd number,
|
||||
as required for open-address hashing using double-hash probing and
|
||||
also with a 65521 prime modulus to govern the range. */
|
||||
unsigned int DEKHash(const char* str, unsigned int len)
|
||||
{
|
||||
unsigned int hash = len;
|
||||
unsigned int i = 0;
|
||||
|
||||
for(i = 0; i < len; str++, i++)
|
||||
{
|
||||
hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
|
||||
}
|
||||
return (( hash & 0x7FFFFFFF ) % 65521 ) | 0x00000001;
|
||||
}
|
||||
|
||||
int hash_fun1( const void *p_data )
|
||||
{
|
||||
char *p_item = (char *)p_data;
|
||||
|
||||
return DJBHash( p_item, strlen(p_item) );
|
||||
}
|
||||
|
||||
int hash_fun2( const void *p_data )
|
||||
{
|
||||
char *p_item = (char *)p_data;
|
||||
|
||||
return DEKHash( p_item, strlen(p_item) );
|
||||
}
|
||||
|
||||
/* must return non-zero if a match */
|
||||
int match_fun( const void *p_data1, const void *p_data2 )
|
||||
{
|
||||
int l1,l2;
|
||||
char *p_item1, *p_item2;
|
||||
|
||||
p_item1 = (char *)p_data1;
|
||||
p_item2 = (char *)p_data2;
|
||||
|
||||
l1=strlen( p_item1 );
|
||||
l2=strlen( p_item2 );
|
||||
|
||||
return (l1==l2) && (strncmp( p_item1, p_item2, l1 ) == 0);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i,size,modulus;
|
||||
size=128;
|
||||
modulus=32;
|
||||
|
||||
hash_table_t hash;
|
||||
|
||||
printf("testing hash functions 1 and 2...\n");
|
||||
for ( i=0; i<31; i++ ) {
|
||||
printf("(func 1,func 2): hash of \"%s\" mod %d is (%d,%d)\n",
|
||||
data[i], modulus, hash_fun1(data[i]) % modulus, hash_fun2(data[i]) % modulus );
|
||||
}
|
||||
|
||||
printf("creating hash table with %d positions...\n",size);
|
||||
if ( hash_create( &hash, size, &hash_fun1, &hash_fun2, &match_fun, NULL ) != 0 ) {
|
||||
fprintf(stderr,"error creating hash table!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("testing that double-hashes can visit all slots...\n");
|
||||
for ( i=0; i<31; i++ ) {
|
||||
printf("inserting \"%s\" into hash...\n", data[i]); fflush(stdout);
|
||||
if ( hash_insert( &hash, data[i] ) != 0 ) {
|
||||
fprintf(stderr, "error inserting value!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("ok\n");
|
||||
printf("looking up \"%s\"...\n", data[i]); fflush(stdout);
|
||||
if ( hash_lookup( &hash, (void **)&data[i] ) != 0 ) {
|
||||
fprintf(stderr, "error looking up value!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("found\n");
|
||||
printf("hash size now %d, vacated slots %d\n",hash_size(&hash),hash.vacated);
|
||||
}
|
||||
|
||||
printf("removing all items from hash...\n");
|
||||
for ( i=0; i<31; i++ ) {
|
||||
printf("deleting \"%s\" from hash...\n", data[i]); fflush(stdout);
|
||||
if ( hash_remove( &hash, (void **)&data[i] ) != 0 ) {
|
||||
fprintf(stderr, "error deleting value!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("ok\n");
|
||||
printf("looking up \"%s\"...\n", data[i]); fflush(stdout);
|
||||
if ( hash_lookup( &hash, (void **)&data[i] ) != -1 ) {
|
||||
fprintf(stderr, "error: deleted value still found in hash!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("not found, good\n");
|
||||
printf("hash size now %d, vacated slots %d\n",hash_size(&hash),hash.vacated);
|
||||
}
|
||||
|
||||
printf("destroying hash table...\n");
|
||||
hash_destroy(&hash);
|
||||
|
||||
return 0;
|
||||
}
|
87
tests/test-portmon.c
Normal file
87
tests/test-portmon.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include "../src/libtcp-portmon.h"
|
||||
|
||||
volatile int g_signal;
|
||||
|
||||
tcp_port_monitor_collection_t *p_collection = NULL;
|
||||
|
||||
void set_terminate(int);
|
||||
|
||||
int main()
|
||||
{
|
||||
tcp_port_monitor_t * p_monitor;
|
||||
int i;
|
||||
char buf[256];
|
||||
|
||||
g_signal=0;
|
||||
|
||||
(void) signal(SIGINT,set_terminate);
|
||||
|
||||
if ( (p_collection = create_tcp_port_monitor_collection()) == NULL) {
|
||||
fprintf(stderr,"error: create_tcp_port_monitor_collection()\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ( (p_monitor = create_tcp_port_monitor( 1, 65535 )) == NULL) {
|
||||
fprintf(stderr,"error: create_tcp_port_monitor()\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ( (insert_tcp_port_monitor_into_collection( p_collection, p_monitor )) != 0 ) {
|
||||
fprintf(stderr,"error: insert_tcp_port_monitor_into_collection()\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
update_tcp_port_monitor_collection( p_collection );
|
||||
|
||||
if ( (p_monitor = find_tcp_port_monitor( p_collection, 1, 65535 )) == NULL ) {
|
||||
fprintf(stderr,"error: find_tcp_port_monitor()\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
peek_tcp_port_monitor( p_monitor, COUNT, 0, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, "\n(%d,%d) COUNT=%s\n",
|
||||
p_monitor->port_range_begin, p_monitor->port_range_end, buf );
|
||||
for ( i=0; i<p_monitor->hash.size; i++ )
|
||||
{
|
||||
fprintf( stdout, "(%d,%d) -- connection %d -- ",
|
||||
p_monitor->port_range_begin, p_monitor->port_range_end, i );
|
||||
peek_tcp_port_monitor( p_monitor, REMOTEIP, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, "%s ", buf );
|
||||
peek_tcp_port_monitor( p_monitor, REMOTEHOST, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, "(%s) ", buf );
|
||||
peek_tcp_port_monitor( p_monitor, REMOTEPORT, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, ": %s ", buf );
|
||||
peek_tcp_port_monitor( p_monitor, LOCALIP, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, "on %s ", buf );
|
||||
peek_tcp_port_monitor( p_monitor, LOCALHOST, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, "(%s) ", buf );
|
||||
peek_tcp_port_monitor( p_monitor, LOCALPORT, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, ": %s\n", buf );
|
||||
/*
|
||||
peek_tcp_port_monitor( p_monitor, LOCALSERVICE, i, buf, sizeof(buf)-1 );
|
||||
fprintf( stdout, "(%s)\n", buf );
|
||||
*/
|
||||
}
|
||||
|
||||
printf("\n<< PRESS CNTL-C TO EXIT >>\n");
|
||||
|
||||
sleep(3);
|
||||
|
||||
if ( g_signal && p_collection ) {
|
||||
destroy_tcp_port_monitor_collection(p_collection);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("bye\n");
|
||||
return(0);
|
||||
}
|
||||
|
||||
void set_terminate(int sig)
|
||||
{
|
||||
g_signal=1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user