diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2009-02-16 17:03:31 +0100 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2009-02-16 17:03:31 +0100 |
commit | a10602d7b29336460e50bd50d683321595f0aada (patch) | |
tree | 0e7d50881d57cc807e27d3d25335f4d02635c33f /tests/port | |
parent | ee34e214a4f35439885fc797dd84bbf8f629c20e (diff) | |
download | wolfbones-a10602d7b29336460e50bd50d683321595f0aada.tar.gz wolfbones-a10602d7b29336460e50bd50d683321595f0aada.tar.bz2 |
strerror_r tests shows how we can leave libwolf.a sane and test the original and the stub function
Diffstat (limited to 'tests/port')
-rw-r--r-- | tests/port/GNUmakefile | 17 | ||||
-rw-r--r-- | tests/port/test_strdup.c | 162 | ||||
-rw-r--r-- | tests/port/test_strerror_r.c | 30 |
3 files changed, 53 insertions, 156 deletions
diff --git a/tests/port/GNUmakefile b/tests/port/GNUmakefile index d0af3cd..0d4f0bb 100644 --- a/tests/port/GNUmakefile +++ b/tests/port/GNUmakefile @@ -1,6 +1,14 @@ TOPDIR = ../.. -BINS = +INCLUDE_LIBS = \ + $(TOPDIR)/src/libwolf.a + +INCLUDE_DIRS = \ + -I$(TOPDIR)/include/wolf -I. -I$(TOPDIR)/src + +BINS = \ + test_strdup \ + test_strerror_r -include $(TOPDIR)/makefiles/sub.mk @@ -10,5 +18,8 @@ local_clean: local_distclean: -local_test: - +local_test: all + @echo "Testing strdup.." + @./test_strdup + @echo "Testing strerror_r..." + @./test_strerror_r diff --git a/tests/port/test_strdup.c b/tests/port/test_strdup.c index a1ac68d..98827d4 100644 --- a/tests/port/test_strdup.c +++ b/tests/port/test_strdup.c @@ -1,156 +1,12 @@ -#include "port/string.h" /* for strdup, memset */ +#include "port/sys.h" +#undef HAVE_STRDUP -#include <unistd.h> /* for exit, unistd, getuid, getppid */ -#include <stdlib.h> /* for EXIT_FAILURE, EXIT_SUCCESS */ +#include "port/string.c" /* for strdup */ +#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */ -#include "errors.h" /* global error codes */ -#include "log.h" /* logging facility */ -#include "daemon/daemon.h" /* Unix daemonizing code */ -#include "daemon/signals.h" /* signal suspension */ - -#include "testd_cmdline.h" /* for command line and option parsing (gengetopt) */ - -#define DEFAULT_CONFIG_FILE "/etc/" CMDLINE_PARSER_PACKAGE ".conf" - -static int parse_options_and_arguments( int argc, char *argv[], struct gengetopt_args_info *args_info ) { - cmdline_parser_init( args_info ); - - if( cmdline_parser2( argc, argv, args_info, 1, 0, 1 ) != 0 ) { - cmdline_parser_free( args_info ); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; -} - -static int test_config( const char *filename ) { - WOLF_LOG( WOLF_LOG_NOTICE, "Testing configuraton read from '%s'", filename ); - return EXIT_SUCCESS; -} - -static int read_config( const char *filename, struct gengetopt_args_info *args_info ) { - char *config_filename = strdup( filename ); - if( cmdline_parser_configfile( config_filename, args_info, 1, 0, 1 ) != 0 ) { - fprintf( stderr, "\n%s\n", gengetopt_args_info_usage ); - cmdline_parser_free( args_info ); - free( config_filename ); - return EXIT_FAILURE; - } - free( config_filename ); - - return EXIT_SUCCESS; -} - -int main( int argc, char *argv[] ) { - struct gengetopt_args_info args_info; - wolf_error_t error; - wolf_daemon_params_t daemon_params; - wolf_daemon_p demon = NULL; - int sig = 0; - - if( parse_options_and_arguments( argc, argv, &args_info ) == EXIT_FAILURE ) { - exit( EXIT_FAILURE ); - } - - if( read_config( args_info.config_file_given ? - args_info.config_file_arg : DEFAULT_CONFIG_FILE, &args_info ) == EXIT_FAILURE ) { - exit( EXIT_FAILURE ); - } - - if( args_info.test_given ) { - cmdline_parser_free( &args_info ); - exit( test_config( args_info.config_file_given ? - args_info.config_file_arg : DEFAULT_CONFIG_FILE ) ); - } - - wolf_log_openlogtostderr( WOLF_LOG_DEBUG - 1 + (int)args_info.debug_given ); - if( args_info.logfile_given ) - wolf_log_openlogtofile( args_info.logfile_arg, - args_info.logfile_level_given ? - wolf_log_str_to_level( args_info.logfile_level_arg ) : WOLF_LOG_NOTICE ); - - if( !args_info.foreground_given ) { - wolf_log_openlogtosyslog( CMDLINE_PARSER_PACKAGE, - args_info.syslog_facility_given ? - wolf_log_str_to_syslog_facility( args_info.syslog_facility_arg ) : WOLF_LOG_DAEMON, - args_info.syslog_level_given ? - wolf_log_str_to_level( args_info.syslog_level_arg ) : WOLF_LOG_NOTICE ); - - memset( &daemon_params, 0, sizeof( daemon_params ) ); - daemon_params.daemon_name = CMDLINE_PARSER_PACKAGE; - daemon_params.pid_filename = args_info.pidfile_given ? - args_info.pidfile_arg : NULL; - daemon_params.group_name = args_info.group_given ? - args_info.group_arg : NULL; - daemon_params.user_name = args_info.user_given ? - args_info.user_arg : NULL; - - demon = wolf_daemon_new( daemon_params, &error ); - if( demon == NULL ) { - cmdline_parser_free( &args_info ); - exit( EXIT_FAILURE ); - } - - if( ( error = wolf_daemon_start( demon ) ) != WOLF_OK ) { - cmdline_parser_free( &args_info ); - wolf_daemon_exit( demon ); - } - } else { - if( ( error = wolf_daemon_signals_initialize( ) ) != WOLF_OK ) { - cmdline_parser_free( &args_info ); - exit( EXIT_FAILURE ); - } - if( ( error = wolf_daemon_signals_install_handlers( ) ) != WOLF_OK ) { - wolf_daemon_signals_terminate( ); - cmdline_parser_free( &args_info ); - exit( EXIT_FAILURE ); - } - } - - WOLF_LOG( WOLF_LOG_NOTICE, "Started %s daemon", CMDLINE_PARSER_PACKAGE ); - while( ( sig != SIGTERM ) && ( sig != SIGINT ) && ( sig != -1 ) ) { - sig = wolf_daemon_signals_suspend( 60, &error ); - switch( sig ) { - case 0: /* timeout */ - break; - - case -1: /* internal error */ - WOLF_LOG( WOLF_LOG_CRIT, "Suspending on UNIX signal resulted in an error %d!", - error ); - break; - - case SIGHUP: - WOLF_LOG( WOLF_LOG_NOTICE, "Rereading configuration" ); - break; - - case SIGTERM: - case SIGINT: - WOLF_LOG( WOLF_LOG_NOTICE, "Got termination signal, shutting down the daemon" ); - break; - - case SIGUSR1: - break; - - case SIGUSR2: - break; - - default: - WOLF_LOG( WOLF_LOG_ERR, "Unexpected signal '%s' (%s, %d)", - wolf_signal_get_long_name( sig ), - wolf_signal_get_short_name( sig ), - sig ); - } - } - WOLF_LOG( WOLF_LOG_NOTICE, "Stopped %s daemon", CMDLINE_PARSER_PACKAGE ); - - if( !args_info.foreground_given ) { - cmdline_parser_free( &args_info ); - wolf_daemon_exit( demon ); - } else { - cmdline_parser_free( &args_info ); - wolf_daemon_signals_terminate( ); - exit( EXIT_SUCCESS ); - } - - exit( EXIT_FAILURE ); +int main( void ) { + const char *s = "test"; + char *d = wolf_strdup( s ); + free( d ); + exit( EXIT_SUCCESS ); } diff --git a/tests/port/test_strerror_r.c b/tests/port/test_strerror_r.c new file mode 100644 index 0000000..40079d8 --- /dev/null +++ b/tests/port/test_strerror_r.c @@ -0,0 +1,30 @@ +#include "port/sys.h" + +#undef HAVE_STRERROR_R +#include "port/string.c" /* for strerror_r */ +#undef strerror_r + +#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */ +#include <stdio.h> /* for printf */ + +int main( void ) { + int i; + int res_sys; + int res_stub; + char buf_sys[1024]; + char buf_stub[1024]; + + /* go through signal numbers (TODO: find out how many of them there are!) */ + for( i = 0; i < 255; i++ ) { + printf( "%d: ", i ); + errno = 0; + *buf_sys = '\0'; + res_sys = strerror_r( i, buf_sys, 1024 ); + printf( "sys: %d \"%s\"", res_sys, buf_sys ); + errno = 0; + *buf_stub = '\0'; + res_stub = wolf_strerror_r( i, buf_stub, 1024 ); + printf( ", stub: %d \"%s\"\n", res_stub, buf_stub ); + } + exit( EXIT_SUCCESS ); +} |