BGPmon-core-1

 view release on metacpan or  search on metacpan

t/00-bgpmon-log.t  view on Meta::CPAN

$bad_log_file = "\x74\x65\x73\x74\x07";
$init_params{"log_file"} = $bad_log_file;
test_init_failure("Testing a log_file parameter with non-printable chars",
                  BGPmon::Log::LOG_INIT_FILE_PRINTABLE_CODE, 
                  BGPmon::Log::LOG_INIT_FILE_PRINTABLE_MSG );
delete($init_params{log_file});

# try a use_syslog that is not a number 
$init_params{"use_syslog"} = "foo";
test_init_failure("Testing a use_syslog that is not a number",
                  BGPmon::Log::LOG_INIT_SYSLOG_NOT_NUM_CODE, 
                  BGPmon::Log::LOG_INIT_SYSLOG_NOT_NUM_MSG );

# try a use_syslog that is not 0 or 1
$init_params{"use_syslog"} = 3;
test_init_failure("Testing a use_syslog that is not 0 or 1",
                  BGPmon::Log::LOG_INIT_SYSLOG_RANGE_CODE, 
                  BGPmon::Log::LOG_INIT_SYSLOG_RANGE_MSG );
delete($init_params{use_syslog});

# try a use_gmt that is not a number 
$init_params{"use_gmt"} = "foo";
test_init_failure("Testing a use_gmt that is not a number",
                  BGPmon::Log::LOG_INIT_GMT_NOT_NUM_CODE, 
                  BGPmon::Log::LOG_INIT_GMT_NOT_NUM_MSG );

# try a use_gmt that is not 0 or 1
$init_params{"use_gmt"} = 3;
test_init_failure("Testing a use_gmt that is not 0 or 1",
                  BGPmon::Log::LOG_INIT_GMT_RANGE_CODE, 
                  BGPmon::Log::LOG_INIT_GMT_RANGE_MSG );
delete($init_params{use_gmt});

# try a use_gmt when use_syslog is set
$init_params{"use_syslog"} = 1;
$init_params{"use_gmt"} = 0;
test_init_failure("Testing a use_gmt and use_syslog together",
                  BGPmon::Log::LOG_INIT_GMT_SYSLOG_CODE, 
                  BGPmon::Log::LOG_INIT_GMT_SYSLOG_MSG );
delete($init_params{use_gmt});

# try use_syslog and a file name 
$init_params{"log_file"} = $log_file;
test_init_failure("Testing setting both use_syslog and write to a file ",
                  BGPmon::Log::LOG_INIT_SYSLOG_AND_FILE_CODE, 
                  BGPmon::Log::LOG_INIT_SYSLOG_AND_FILE_MSG );
delete($init_params{use_syslog});
# not sure how we would test a syslog failure
# BGPmon::Log:: LOG_INIT_SYSLOG_OPEN_CODE 

# try a failure opening a file - directory doesn't exit
$init_params{"log_file"} = "/this/directory/cannot/exist/foo.txt";
test_init_failure("Testing unable to open log file ",
                  BGPmon::Log::LOG_INIT_FILE_OPEN_CODE, 
                  BGPmon::Log::LOG_INIT_FILE_OPEN_MSG );

# try a failure opening a file - bad permissions
my $retu = unlink ($log_file);
my $reto = open($log_fh, "> $log_file");
close($log_fh);
chmod(0000, $log_file);
ok($reto != 0, "Making unreadable log file : $log_file");
$init_params{"log_file"} = $log_file;
test_init_failure("Testing bad log file permissions",
                  BGPmon::Log::LOG_INIT_FILE_OPEN_CODE, 
                  BGPmon::Log::LOG_INIT_FILE_OPEN_MSG );
$retu = unlink ($log_file);

# test no init function
$ret = BGPmon::Log::log_close();
$ret = BGPmon::Log::log_notice($log_msg);
my $ret_code = BGPmon::Log::get_error_code("log_notice");
my $ret_msg = BGPmon::Log::get_error_msg("log_notice");
ok(($ret == 1) && 
   ($ret_code eq BGPmon::Log::LOG_NOT_INITIALIZED_CODE ) && 
   ($ret_msg eq BGPmon::Log::LOG_NOT_INITIALIZED_MSG), 
   "Trying to write before log is initialized, result is $ret_code: $ret_msg");

# open the log one last time for write error checking 
# reset my params for error checking
%init_params = ( prog_name => "BGPmon::Log - Testing",
                    log_level => BGPmon::Log::LOG_NOTICE,
                    log_facility => BGPmon::Log::LOG_LOCAL0,
                    log_file => $log_file, 
                    use_syslog => 0,
                    use_gmt => 1 );
$ret = BGPmon::Log::log_init(%init_params);
$ecode = BGPmon::Log::get_error_code("log_init");
$emsg = BGPmon::Log::get_error_msg("log_init");
ok($ret == 0, 
"Initializing log $log_file for error checks, result is $ecode: $emsg"); 

# test invalid function name
# call log_foo
# use constant LOG_UNKNOWN_FUNCTION_CODE => 17;
$ret = BGPmon::Log::log_garbage($log_msg);
$ret_code = BGPmon::Log::get_error_code("log_garbage");
$ret_msg = BGPmon::Log::get_error_msg("log_garbage");
ok(($ret == 1) && 
   ($ret_code eq BGPmon::Log::LOG_UNKNOWN_FUNCTION_CODE ) && 
   ($ret_msg eq BGPmon::Log::LOG_UNKNOWN_FUNCTION_MSG), 
   "Trying to write using invalid log function, result is $ret_code: $ret_msg");

# test failure to specify a log message
$ret = BGPmon::Log::log_notice();
$ret_code = BGPmon::Log::get_error_code("log_notice");
$ret_msg = BGPmon::Log::get_error_msg("log_notice");
ok(($ret == 1) && 
   ($ret_code eq BGPmon::Log::LOG_MISSING_MSG_CODE ) && 
   ($ret_msg eq BGPmon::Log::LOG_MISSING_MSG_MSG), 
   "Trying to write without providing a message, result is $ret_code: $ret_msg");

# test a very long message
my $bad_log_msg = "c";
for ($count = 0; $count < BGPmon::Log::MAX_STRING_LEN; $count++) {
    $bad_log_msg .= "c";
}
$ret = BGPmon::Log::log_notice($bad_log_msg);
$ret_code = BGPmon::Log::get_error_code("log_notice");
$ret_msg = BGPmon::Log::get_error_msg("log_notice");
ok(($ret == 1) && 
   ($ret_code eq BGPmon::Log::LOG_MSG_SIZE_CODE ) && 
   ($ret_msg eq BGPmon::Log::LOG_MSG_SIZE_MSG), 
   "Testing a long log message, result is $ret_code: $ret_msg");

# test a non_printable message
$bad_log_msg = "\x74\x65\x73\x74\x07";
$ret = BGPmon::Log::log_notice($bad_log_msg);
$ret_code = BGPmon::Log::get_error_code("log_notice");
$ret_msg = BGPmon::Log::get_error_msg("log_notice");
ok(($ret == 1) && 
   ($ret_code eq BGPmon::Log::LOG_MSG_PRINTABLE_CODE ) && 
   ($ret_msg eq BGPmon::Log::LOG_MSG_PRINTABLE_MSG), 
   "Testing a non-printable log message, result is $ret_code: $ret_msg");

# test write fails for log_file
# tried removing and unlinking the file,  but 
# both yield no errors
# remove the file we are logging to
#$ret = unlink($log_file);
#chmod(0000, $log_file);
#ok($ret == 1, "Removing test log_file $log_file");
#$ret = BGPmon::Log::log_notice("help");
#$ret_code = BGPmon::Log::get_error_code("log_notice");
#$ret_msg = BGPmon::Log::get_error_msg("log_notice");
#ok(($ret == 1) && 
#   ($ret_code eq BGPmon::Log::LOG_WRITE_FAILED_CODE ) && 
#   ($ret_msg eq BGPmon::Log::LOG_WRITE_FAILED_MSG), 
#   "Testing log message to deleted file");
#

# close the log file and clean-up
close($log_fh);
$ret = BGPmon::Log::log_close();
$ecode = BGPmon::Log::get_error_code("log_init");
$emsg = BGPmon::Log::get_error_msg("log_init");
$retu = unlink($log_file);
ok($ret == 0 && $retu == 1, 
"Closing and removing the log file $log_file"); 

# not sure how we would test a syslog write failure
# BGPmon::Log::LOG_WRITE_FAILED_CODE 

# not sure how we would test a stdout write failure
# BGPmon::Log::LOG_WRITE_FAILED_CODE 

done_testing();
1;



( run in 2.546 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )