1N/A#!perl -T
1N/A
1N/ABEGIN {
1N/A if ($ENV{PERL_CORE}) {
1N/A chdir 't';
1N/A @INC = '../lib';
1N/A }
1N/A}
1N/A
1N/Ause strict;
1N/Ause Config;
1N/Ause File::Spec;
1N/Ause Test::More;
1N/A
1N/A# we enable all Perl warnings, but we don't "use warnings 'all'" because
1N/A# we want to disable the warnings generated by Sys::Syslog
1N/Ano warnings;
1N/Ause warnings qw(closure deprecated exiting glob io misc numeric once overflow
1N/A pack portable recursion redefine regexp severe signal substr
1N/A syntax taint uninitialized unpack untie utf8 void);
1N/A
1N/A# if someone is using warnings::compat, the previous trick won't work, so we
1N/A# must manually disable warnings
1N/A$^W = 0 if $] < 5.006;
1N/A
1N/Amy $is_Win32 = $^O =~ /win32/i;
1N/Amy $is_Cygwin = $^O =~ /cygwin/i;
1N/A
1N/A# if testing in core, check that the module is at least available
1N/Aif ($ENV{PERL_CORE}) {
1N/A plan skip_all => "Sys::Syslog was not build"
1N/A unless $Config{'extensions'} =~ /\bSyslog\b/;
1N/A}
1N/A
1N/A# we also need Socket
1N/Aplan skip_all => "Socket was not build"
1N/A unless $Config{'extensions'} =~ /\bSocket\b/;
1N/A
1N/Amy $tests;
1N/Aplan tests => $tests;
1N/A
1N/A# any remaining warning should be severly punished
1N/ABEGIN { eval "use Test::NoWarnings"; $tests = $@ ? 0 : 1; }
1N/A
1N/ABEGIN { $tests += 1 }
1N/A# ok, now loads them
1N/Aeval 'use Socket';
1N/Ause_ok('Sys::Syslog', ':standard', ':extended', ':macros');
1N/A
1N/ABEGIN { $tests += 1 }
1N/A# check that the documented functions are correctly provided
1N/Acan_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
1N/A
1N/A
1N/ABEGIN { $tests += 1 }
1N/A# check the diagnostics
1N/A# setlogsock()
1N/Aeval { setlogsock() };
1N/Alike( $@, qr/^Invalid argument passed to setlogsock/,
1N/A "calling setlogsock() with no argument" );
1N/A
1N/ABEGIN { $tests += 3 }
1N/A# syslog()
1N/Aeval { syslog() };
1N/Alike( $@, qr/^syslog: expecting argument \$priority/,
1N/A "calling syslog() with no argument" );
1N/A
1N/Aeval { syslog(undef) };
1N/Alike( $@, qr/^syslog: expecting argument \$priority/,
1N/A "calling syslog() with one undef argument" );
1N/A
1N/Aeval { syslog('') };
1N/Alike( $@, qr/^syslog: expecting argument \$format/,
1N/A "calling syslog() with one empty argument" );
1N/A
1N/A
1N/Amy $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
1N/Amy $r = 0;
1N/A
1N/ABEGIN { $tests += 8 }
1N/A# try to open a syslog using a Unix or stream socket
1N/ASKIP: {
1N/A skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
1N/A unless -e Sys::Syslog::_PATH_LOG();
1N/A
1N/A # The only known $^O eq 'svr4' that needs this is NCR MP-RAS,
1N/A # but assuming 'stream' in SVR4 is probably not that bad.
1N/A my $sock_type = $^O =~ /^(solaris|irix|svr4|powerux)$/ ? 'stream' : 'unix';
1N/A
1N/A eval { setlogsock($sock_type) };
1N/A is( $@, '', "setlogsock() called with '$sock_type'" );
1N/A TODO: {
1N/A local $TODO = "minor bug";
1N/A ok( $r, "setlogsock() should return true: '$r'" );
1N/A }
1N/A
1N/A # open syslog with a "local0" facility
1N/A SKIP: {
1N/A # openlog()
1N/A $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
1N/A skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/;
1N/A is( $@, '', "openlog() called with facility 'local0'" );
1N/A ok( $r, "openlog() should return true: '$r'" );
1N/A
1N/A # syslog()
1N/A $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
1N/A is( $@, '', "syslog() called with level 'info'" );
1N/A ok( $r, "syslog() should return true: '$r'" );
1N/A
1N/A # closelog()
1N/A $r = eval { closelog() } || 0;
1N/A is( $@, '', "closelog()" );
1N/A ok( $r, "closelog() should return true: '$r'" );
1N/A }
1N/A}
1N/A
1N/A
1N/ABEGIN { $tests += 22 * 8 }
1N/A# try to open a syslog using all the available connection methods
1N/Amy @passed = ();
1N/Afor my $sock_type (qw(native eventlog unix pipe stream inet tcp udp)) {
1N/A SKIP: {
1N/A skip "the 'stream' mechanism because a previous mechanism with similar interface succeeded", 22
1N/A if $sock_type eq 'stream' and grep {/pipe|unix/} @passed;
1N/A
1N/A # setlogsock() called with an arrayref
1N/A $r = eval { setlogsock([$sock_type]) } || 0;
1N/A skip "can't use '$sock_type' socket", 22 unless $r;
1N/A is( $@, '', "[$sock_type] setlogsock() called with ['$sock_type']" );
1N/A ok( $r, "[$sock_type] setlogsock() should return true: '$r'" );
1N/A
1N/A # setlogsock() called with a single argument
1N/A $r = eval { setlogsock($sock_type) } || 0;
1N/A skip "can't use '$sock_type' socket", 20 unless $r;
1N/A is( $@, '', "[$sock_type] setlogsock() called with '$sock_type'" );
1N/A ok( $r, "[$sock_type] setlogsock() should return true: '$r'" );
1N/A
1N/A # openlog() without option NDELAY
1N/A $r = eval { openlog('perl', '', 'local0') } || 0;
1N/A skip "can't connect to syslog", 18 if $@ =~ /^no connection to syslog available/;
1N/A is( $@, '', "[$sock_type] openlog() called with facility 'local0' and without option 'ndelay'" );
1N/A ok( $r, "[$sock_type] openlog() should return true: '$r'" );
1N/A
1N/A # openlog() with the option NDELAY
1N/A $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
1N/A skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/;
1N/A is( $@, '', "[$sock_type] openlog() called with facility 'local0' with option 'ndelay'" );
1N/A ok( $r, "[$sock_type] openlog() should return true: '$r'" );
1N/A
1N/A # syslog() with negative level, should fail
1N/A $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0;
1N/A like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level -1" );
1N/A ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
1N/A
1N/A # syslog() with invalid level, should fail
1N/A $r = eval { syslog("plonk", "$test_string by connecting to a $sock_type socket") } || 0;
1N/A like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level plonk" );
1N/A ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
1N/A
1N/A # syslog() with levels "info" and "notice" (as a strings), should fail
1N/A $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0;
1N/A like( $@, '/^syslog: too many levels given: notice/', "[$sock_type] syslog() called with level 'info,notice'" );
1N/A ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
1N/A
1N/A # syslog() with facilities "local0" and "local1" (as a strings), should fail
1N/A $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0;
1N/A like( $@, '/^syslog: too many facilities given: local1/', "[$sock_type] syslog() called with level 'local0,local1'" );
1N/A ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
1N/A
1N/A # syslog() with level "info" (as a string), should pass
1N/A $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
1N/A is( $@, '', "[$sock_type] syslog() called with level 'info' (string)" );
1N/A ok( $r, "[$sock_type] syslog() should return true: '$r'" );
1N/A
1N/A # syslog() with level "info" (as a macro), should pass
1N/A { local $! = 1;
1N/A $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket, setting a fake errno: %m") } || 0;
1N/A }
1N/A is( $@, '', "[$sock_type] syslog() called with level 'info' (macro)" );
1N/A ok( $r, "[$sock_type] syslog() should return true: '$r'" );
1N/A
1N/A push @passed, $sock_type;
1N/A
1N/A SKIP: {
1N/A skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
1N/A # closelog()
1N/A $r = eval { closelog() } || 0;
1N/A is( $@, '', "[$sock_type] closelog()" );
1N/A ok( $r, "[$sock_type] closelog() should return true: '$r'" );
1N/A }
1N/A }
1N/A}
1N/A
1N/A
1N/ABEGIN { $tests += 10 }
1N/ASKIP: {
1N/A skip "not testing setlogsock('stream') on Win32", 10 if $is_Win32;
1N/A skip "the 'unix' mechanism works, so the tests will likely fail with the 'stream' mechanism", 10
1N/A if grep {/unix/} @passed;
1N/A
1N/A skip "not testing setlogsock('stream'): _PATH_LOG unavailable", 10
1N/A unless -e Sys::Syslog::_PATH_LOG();
1N/A
1N/A # setlogsock() with "stream" and an undef path
1N/A $r = eval { setlogsock("stream", undef ) } || '';
1N/A is( $@, '', "setlogsock() called, with 'stream' and an undef path" );
1N/A if ($is_Cygwin) {
1N/A if (-x "/usr/sbin/syslog-ng") {
1N/A ok( $r, "setlogsock() on Cygwin with syslog-ng should return true: '$r'" );
1N/A }
1N/A else {
1N/A ok( !$r, "setlogsock() on Cygwin without syslog-ng should return false: '$r'" );
1N/A }
1N/A }
1N/A else {
1N/A ok( $r, "setlogsock() should return true: '$r'" );
1N/A }
1N/A
1N/A # setlogsock() with "stream" and an empty path
1N/A $r = eval { setlogsock("stream", '' ) } || '';
1N/A is( $@, '', "setlogsock() called, with 'stream' and an empty path" );
1N/A ok( !$r, "setlogsock() should return false: '$r'" );
1N/A
1N/A # setlogsock() with "stream" and /dev/null
1N/A $r = eval { setlogsock("stream", '/dev/null' ) } || '';
1N/A is( $@, '', "setlogsock() called, with 'stream' and '/dev/null'" );
1N/A ok( $r, "setlogsock() should return true: '$r'" );
1N/A
1N/A # setlogsock() with "stream" and a non-existing file
1N/A $r = eval { setlogsock("stream", 'test.log' ) } || '';
1N/A is( $@, '', "setlogsock() called, with 'stream' and 'test.log' (file does not exist)" );
1N/A ok( !$r, "setlogsock() should return false: '$r'" );
1N/A
1N/A # setlogsock() with "stream" and a local file
1N/A SKIP: {
1N/A my $logfile = "test.log";
1N/A open(LOG, ">$logfile") or skip "can't create file '$logfile': $!", 2;
1N/A close(LOG);
1N/A $r = eval { setlogsock("stream", $logfile ) } || '';
1N/A is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" );
1N/A ok( $r, "setlogsock() should return true: '$r'" );
1N/A unlink($logfile);
1N/A }
1N/A}
1N/A
1N/A
1N/ABEGIN { $tests += 3 + 4 * 3 }
1N/A# setlogmask()
1N/A{
1N/A my $oldmask = 0;
1N/A
1N/A $oldmask = eval { setlogmask(0) } || 0;
1N/A is( $@, '', "setlogmask() called with a null mask" );
1N/A $r = eval { setlogmask(0) } || 0;
1N/A is( $@, '', "setlogmask() called with a null mask (second time)" );
1N/A is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
1N/A
1N/A my @masks = (
1N/A LOG_MASK(LOG_ERR()),
1N/A ~LOG_MASK(LOG_INFO()),
1N/A LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()),
1N/A );
1N/A
1N/A for my $newmask (@masks) {
1N/A $r = eval { setlogmask($newmask) } || 0;
1N/A is( $@, '', "setlogmask() called with a new mask" );
1N/A is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
1N/A $r = eval { setlogmask(0) } || 0;
1N/A is( $@, '', "setlogmask() called with a null mask" );
1N/A is( $r, $newmask, "setlogmask() must return the new mask");
1N/A setlogmask($oldmask);
1N/A }
1N/A}