unittests/lib/log_test.c in trema-0.2.2.1 vs unittests/lib/log_test.c in trema-0.2.3

- old
+ new

@@ -18,12 +18,14 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <syslog.h> #include "checks.h" #include "cmockery_trema.h" #include "log.h" #include "trema.h" #include "trema_wrapper.h" @@ -79,10 +81,39 @@ return 0; } +static bool syslog_initialized = false; + +static void +mock_openlog( const char *ident, int option, int facility ) { + check_expected( ident ); + check_expected( option ); + check_expected( facility ); + + syslog_initialized = true; +} + + +static void +mock_closelog( void ) { + syslog_initialized = false; +} + + +static void +mock_vsyslog( int priority, const char *format, va_list ap ) { + check_expected( priority ); + + char output[ 256 ]; + vsnprintf( output, sizeof( output ), format, ap ); + + check_expected( output ); +} + + /******************************************************************************** * Setup and teardown function. ********************************************************************************/ static void @@ -102,54 +133,100 @@ die = mock_die; trema_abort = mock_abort; trema_vprintf = mock_vprintf; trema_fprintf = mock_fprintf; + trema_openlog = mock_openlog; + trema_closelog = mock_closelog; + trema_vsyslog = mock_vsyslog; } static void -setup_logger() { +setup_logger_stdout() { setup(); - init_log( "log_test.c", get_trema_tmp(), false ); + init_log( "log_test.c", get_trema_tmp(), LOGGING_TYPE_STDOUT ); } static void -setup_daemon_logger() { +setup_logger_file() { setup(); - init_log( "log_test.c", get_trema_tmp(), true ); + init_log( "log_test.c", get_trema_tmp(), LOGGING_TYPE_FILE ); } static void +setup_logger_file_stdout() { + setup(); + init_log( "log_test.c", get_trema_tmp(), LOGGING_TYPE_FILE | LOGGING_TYPE_STDOUT ); +} + + +static void +setup_logger_syslog() { + setup(); + const char *ident = "log_test.c"; + expect_string( mock_openlog, ident, ident ); + expect_value( mock_openlog, option, LOG_NDELAY ); + expect_value( mock_openlog, facility, LOG_USER ); + init_log( ident, get_trema_tmp(), LOGGING_TYPE_SYSLOG ); +} + + +static void teardown() { finalize_log(); reset_LOGGING_LEVEL(); teardown_leak_detector(); die = original_die; trema_abort = abort; trema_vprintf = vprintf; trema_fprintf = fprintf; + trema_openlog = openlog; + trema_closelog = closelog; + trema_vsyslog = vsyslog; } /******************************************************************************** * Initialization tests. ********************************************************************************/ void test_init_log_reads_LOGING_LEVEL_environment_variable() { setenv( "LOGGING_LEVEL", "CRITICAL", 1 ); - init_log( "tetris", get_trema_tmp(), false ); - assert_int_equal( LOG_CRITICAL, get_logging_level() ); + init_log( "tetris", get_trema_tmp(), LOGGING_TYPE_FILE ); + assert_int_equal( LOG_CRIT, get_logging_level() ); } +void +test_init_log_opens_syslog() { + const char *ident = "tetris"; + expect_string( mock_openlog, ident, ident ); + expect_value( mock_openlog, option, LOG_NDELAY ); + expect_value( mock_openlog, facility, LOG_USER ); + init_log( ident, get_trema_tmp(), LOGGING_TYPE_SYSLOG ); + assert_true( syslog_initialized ); +} + + /******************************************************************************** + * Finalization test. + ********************************************************************************/ + +void +test_finalize_log_closes_syslog() { + finalize_log(); + assert_false( syslog_initialized ); +} + + +/******************************************************************************** * Logging level tests. ********************************************************************************/ void test_default_logging_level_is_INFO() { @@ -158,20 +235,37 @@ void test_set_logging_level_succeed() { set_logging_level( "critical" ); - assert_int_equal( LOG_CRITICAL, get_logging_level() ); + assert_int_equal( LOG_CRIT, get_logging_level() ); } void test_set_logging_level_fail_with_invalid_value() { expect_assert_failure( set_logging_level( "INVALID_LEVEL" ) ); } +void +test_set_logging_level_is_called_before_init_log() { + set_logging_level( "critical" ); + init_log( "tetris", get_trema_tmp(), LOGGING_TYPE_FILE ); + assert_int_equal( LOG_CRIT, get_logging_level() ); +} + + +void +test_LOGGING_LEVEL_overrides_logging_level() { + setenv( "LOGGING_LEVEL", "DEBUG", 1 ); + set_logging_level( "critical" ); + init_log( "tetris", get_trema_tmp(), LOGGING_TYPE_FILE ); + assert_int_equal( LOG_DEBUG, get_logging_level() ); +} + + /******************************************************************************** * critical() tests. ********************************************************************************/ void @@ -363,90 +457,120 @@ expect_assert_failure( debug( NULL ) ); } /******************************************************************************** - * Misc. + * Output type tests. ********************************************************************************/ void test_output_to_stdout() { expect_string( mock_vprintf, output, "Hello World\n" ); + + info( "Hello World" ); +} + + +void +test_output_to_file_stdout() { + expect_string( mock_vprintf, output, "Hello World\n" ); expect_string( mock_fprintf, output, "Hello World\n" ); info( "Hello World" ); } +void +test_output_to_syslog() { + expect_value( mock_vsyslog, priority, LOG_INFO ); + expect_string( mock_vsyslog, output, "Hello World" ); + + info( "Hello World" ); +} + + /******************************************************************************** * Run tests. ********************************************************************************/ int main() { const UnitTest tests[] = { unit_test_setup_teardown( test_init_log_reads_LOGING_LEVEL_environment_variable, reset_LOGGING_LEVEL, reset_LOGGING_LEVEL ), + unit_test_setup_teardown( test_init_log_opens_syslog, + setup, teardown ), + unit_test_setup_teardown( test_finalize_log_closes_syslog, + setup_logger_syslog, teardown ), + unit_test_setup_teardown( test_default_logging_level_is_INFO, - setup_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_set_logging_level_succeed, - setup_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_set_logging_level_fail_with_invalid_value, - setup_logger, teardown ), + setup_logger_file, teardown ), + unit_test_setup_teardown( test_set_logging_level_is_called_before_init_log, + setup, teardown ), + unit_test_setup_teardown( test_LOGGING_LEVEL_overrides_logging_level, + setup, teardown ), unit_test_setup_teardown( test_critical_logs_if_logging_level_is_CRITICAL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_critical_logs_if_logging_level_is_ERROR, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_critical_fail_if_NULL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_error_donothing_if_logging_level_is_CRITICAL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_error_logs_if_logging_level_is_ERROR, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_error_logs_if_logging_level_is_WARNING, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_error_fail_if_NULL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_warn_donothing_if_logging_level_is_ERROR, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_warn_logs_if_logging_level_is_WARNING, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_warn_logs_if_logging_level_is_NOTICE, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_warn_fail_if_NULL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_notice_donothing_if_logging_level_is_WARNING, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_notice_logs_if_logging_level_is_NOTICE, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_notice_logs_if_logging_level_is_INFO, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_notice_fail_if_NULL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_info_logs_if_logging_level_is_DEBUG, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_info_logs_if_logging_level_is_INFO, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_info_donothing_if_logging_level_is_NOTICE, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_info_fail_if_NULL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_DEBUG_donothing_if_logging_level_is_INFO, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_DEBUG_logs_if_logging_level_is_DEBUG, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_debug_fail_if_NULL, - setup_daemon_logger, teardown ), + setup_logger_file, teardown ), unit_test_setup_teardown( test_output_to_stdout, - setup_logger, teardown ), + setup_logger_stdout, teardown ), + unit_test_setup_teardown( test_output_to_file_stdout, + setup_logger_file_stdout, teardown ), + unit_test_setup_teardown( test_output_to_syslog, + setup_logger_syslog, teardown ), }; return run_tests( tests ); }