1#!perl -T 2 3use strict; 4use Config; 5use FileHandle; 6use File::Spec; 7use Test::More; 8 9# we enable all Perl warnings, but we don't "use warnings 'all'" because 10# we want to disable the warnings generated by Sys::Syslog 11no warnings; 12use warnings qw(closure deprecated exiting glob io misc numeric once overflow 13 pack portable recursion redefine regexp severe signal substr 14 syntax taint uninitialized unpack untie utf8 void); 15 16# if someone is using warnings::compat, the previous trick won't work, so we 17# must manually disable warnings 18$^W = 0 if $] < 5.006; 19 20my $is_Win32 = $^O =~ /win32/i; 21my $is_Cygwin = $^O =~ /cygwin/i; 22 23# if testing in core, check that the module is at least available 24if ($ENV{PERL_CORE}) { 25 plan skip_all => "Sys::Syslog was not build" 26 unless $Config{'extensions'} =~ /\bSyslog\b/; 27} 28 29# we also need Socket 30plan skip_all => "Socket was not build" 31 unless $Config{'extensions'} =~ /\bSocket\b/; 32 33my $tests; 34plan tests => $tests; 35 36# any remaining warning should be severly punished 37BEGIN { eval "use Test::NoWarnings"; $tests = $@ ? 0 : 1; } 38 39BEGIN { $tests += 1 } 40# ok, now loads them 41eval 'use Socket'; 42use_ok('Sys::Syslog', ':standard', ':extended', ':macros'); 43 44BEGIN { $tests += 1 } 45# check that the documented functions are correctly provided 46can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) ); 47 48 49BEGIN { $tests += 4 } 50# check the diagnostics 51# setlogsock() 52eval { setlogsock() }; 53like( $@, qr/^setlogsock\(\): Invalid number of arguments/, 54 "calling setlogsock() with no argument" ); 55 56eval { setlogsock(undef) }; 57like( $@, qr/^setlogsock\(\): Invalid type; must be one of /, 58 "calling setlogsock() with undef" ); 59 60eval { setlogsock(\"") }; 61like( $@, qr/^setlogsock\(\): Unexpected scalar reference/, 62 "calling setlogsock() with a scalar reference" ); 63 64eval { setlogsock({}) }; 65like( $@, qr/^setlogsock\(\): No argument given/, 66 "calling setlogsock() with an empty hash reference" ); 67 68BEGIN { $tests += 3 } 69# syslog() 70eval { syslog() }; 71like( $@, qr/^syslog: expecting argument \$priority/, 72 "calling syslog() with no argument" ); 73 74eval { syslog(undef) }; 75like( $@, qr/^syslog: expecting argument \$priority/, 76 "calling syslog() with one undef argument" ); 77 78eval { syslog('') }; 79like( $@, qr/^syslog: expecting argument \$format/, 80 "calling syslog() with one empty argument" ); 81 82 83my $test_string = "uid $< is testing Perl $] syslog(3) capabilities"; 84my $r = 0; 85 86BEGIN { $tests += 8 } 87# try to open a syslog using a Unix or stream socket 88SKIP: { 89 skip "can't connect to Unix socket: _PATH_LOG unavailable", 8 90 unless -e Sys::Syslog::_PATH_LOG(); 91 92 # The only known $^O eq 'svr4' that needs this is NCR MP-RAS, 93 # but assuming 'stream' in SVR4 is probably not that bad. 94 my $sock_type = $^O =~ /^(solaris|irix|svr4|powerux)$/ ? 'stream' : 'unix'; 95 96 eval { setlogsock($sock_type) }; 97 is( $@, '', "setlogsock() called with '$sock_type'" ); 98 TODO: { 99 local $TODO = "minor bug"; 100 SKIP: { skip "TODO $TODO", 1 if $] < 5.006002; 101 ok( $r, "setlogsock() should return true: '$r'" ); 102 } 103 } 104 105 # open syslog with a "local0" facility 106 SKIP: { 107 # openlog() 108 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0; 109 skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/; 110 is( $@, '', "openlog() called with facility 'local0'" ); 111 ok( $r, "openlog() should return true: '$r'" ); 112 113 # syslog() 114 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0; 115 is( $@, '', "syslog() called with level 'info'" ); 116 ok( $r, "syslog() should return true: '$r'" ); 117 118 # closelog() 119 $r = eval { closelog() } || 0; 120 is( $@, '', "closelog()" ); 121 ok( $r, "closelog() should return true: '$r'" ); 122 } 123} 124 125 126BEGIN { $tests += 22 * 8 } 127# try to open a syslog using all the available connection methods 128my @passed = (); 129for my $sock_type (qw(native eventlog unix pipe stream inet tcp udp)) { 130 SKIP: { 131 skip "the 'stream' mechanism because a previous mechanism with similar interface succeeded", 22 132 if $sock_type eq 'stream' and grep {/pipe|unix/} @passed; 133 134 # setlogsock() called with an arrayref 135 $r = eval { setlogsock([$sock_type]) } || 0; 136 skip "can't use '$sock_type' socket", 22 unless $r; 137 is( $@, '', "[$sock_type] setlogsock() called with ['$sock_type']" ); 138 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" ); 139 140 # setlogsock() called with a single argument 141 $r = eval { setlogsock($sock_type) } || 0; 142 skip "can't use '$sock_type' socket", 20 unless $r; 143 is( $@, '', "[$sock_type] setlogsock() called with '$sock_type'" ); 144 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" ); 145 146 # openlog() without option NDELAY 147 $r = eval { openlog('perl', '', 'local0') } || 0; 148 skip "can't connect to syslog", 18 if $@ =~ /^no connection to syslog available/; 149 is( $@, '', "[$sock_type] openlog() called with facility 'local0' and without option 'ndelay'" ); 150 ok( $r, "[$sock_type] openlog() should return true: '$r'" ); 151 152 # openlog() with the option NDELAY 153 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0; 154 skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/; 155 is( $@, '', "[$sock_type] openlog() called with facility 'local0' with option 'ndelay'" ); 156 ok( $r, "[$sock_type] openlog() should return true: '$r'" ); 157 158 # syslog() with negative level, should fail 159 $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0; 160 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level -1" ); 161 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 162 163 # syslog() with invalid level, should fail 164 $r = eval { syslog("plonk", "$test_string by connecting to a $sock_type socket") } || 0; 165 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level plonk" ); 166 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 167 168 # syslog() with levels "info" and "notice" (as a strings), should fail 169 $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0; 170 like( $@, '/^syslog: too many levels given: notice/', "[$sock_type] syslog() called with level 'info,notice'" ); 171 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 172 173 # syslog() with facilities "local0" and "local1" (as a strings), should fail 174 $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0; 175 like( $@, '/^syslog: too many facilities given: local1/', "[$sock_type] syslog() called with level 'local0,local1'" ); 176 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 177 178 # syslog() with level "info" (as a string), should pass 179 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0; 180 is( $@, '', "[$sock_type] syslog() called with level 'info' (string)" ); 181 ok( $r, "[$sock_type] syslog() should return true: '$r'" ); 182 183 # syslog() with level "info" (as a macro), should pass 184 { local $! = 1; 185 $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket, setting a fake errno: %m") } || 0; 186 } 187 is( $@, '', "[$sock_type] syslog() called with level 'info' (macro)" ); 188 ok( $r, "[$sock_type] syslog() should return true: '$r'" ); 189 190 push @passed, $sock_type; 191 192 SKIP: { 193 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console'; 194 # closelog() 195 $r = eval { closelog() } || 0; 196 is( $@, '', "[$sock_type] closelog()" ); 197 ok( $r, "[$sock_type] closelog() should return true: '$r'" ); 198 } 199 } 200} 201 202 203BEGIN { $tests += 10 } 204SKIP: { 205 skip "not testing setlogsock('stream') on Win32", 10 if $is_Win32; 206 skip "the 'unix' mechanism works, so the tests will likely fail with the 'stream' mechanism", 10 207 if grep {/unix/} @passed; 208 209 skip "not testing setlogsock('stream'): _PATH_LOG unavailable", 10 210 unless -e Sys::Syslog::_PATH_LOG(); 211 212 # setlogsock() with "stream" and an undef path 213 $r = eval { setlogsock("stream", undef ) } || ''; 214 is( $@, '', "setlogsock() called, with 'stream' and an undef path" ); 215 if ($is_Cygwin) { 216 if (-x "/usr/sbin/syslog-ng") { 217 ok( $r, "setlogsock() on Cygwin with syslog-ng should return true: '$r'" ); 218 } 219 else { 220 ok( !$r, "setlogsock() on Cygwin without syslog-ng should return false: '$r'" ); 221 } 222 } 223 else { 224 ok( $r, "setlogsock() should return true: '$r'" ); 225 } 226 227 # setlogsock() with "stream" and an empty path 228 $r = eval { setlogsock("stream", '' ) } || ''; 229 is( $@, '', "setlogsock() called, with 'stream' and an empty path" ); 230 ok( !$r, "setlogsock() should return false: '$r'" ); 231 232 # setlogsock() with "stream" and /dev/null 233 $r = eval { setlogsock("stream", '/dev/null' ) } || ''; 234 is( $@, '', "setlogsock() called, with 'stream' and '/dev/null'" ); 235 ok( $r, "setlogsock() should return true: '$r'" ); 236 237 # setlogsock() with "stream" and a non-existing file 238 $r = eval { setlogsock("stream", 'test.log' ) } || ''; 239 is( $@, '', "setlogsock() called, with 'stream' and 'test.log' (file does not exist)" ); 240 ok( !$r, "setlogsock() should return false: '$r'" ); 241 242 # setlogsock() with "stream" and a local file 243 SKIP: { 244 my $logfile = "test.log"; 245 my $fh = FileHandle->new; 246 open $fh, ">$logfile" or skip "can't create file '$logfile': $!", 2; 247 close $fh; 248 $r = eval { setlogsock("stream", $logfile ) } || ''; 249 is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" ); 250 ok( $r, "setlogsock() should return true: '$r'" ); 251 unlink($logfile); 252 } 253} 254 255 256BEGIN { $tests += 3 + 4 * 3 } 257# setlogmask() 258{ 259 my $oldmask = 0; 260 261 $oldmask = eval { setlogmask(0) } || 0; 262 is( $@, '', "setlogmask() called with a null mask" ); 263 $r = eval { setlogmask(0) } || 0; 264 is( $@, '', "setlogmask() called with a null mask (second time)" ); 265 is( $r, $oldmask, "setlogmask() must return the same mask as previous call"); 266 267 my @masks = ( 268 LOG_MASK(LOG_ERR()), 269 ~LOG_MASK(LOG_INFO()), 270 LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()), 271 ); 272 273 for my $newmask (@masks) { 274 $r = eval { setlogmask($newmask) } || 0; 275 is( $@, '', "setlogmask() called with a new mask" ); 276 is( $r, $oldmask, "setlogmask() must return the same mask as previous call"); 277 $r = eval { setlogmask(0) } || 0; 278 is( $@, '', "setlogmask() called with a null mask" ); 279 is( $r, $newmask, "setlogmask() must return the new mask"); 280 setlogmask($oldmask); 281 } 282} 283 284BEGIN { $tests += 4 } 285SKIP: { 286 # case: test the return value of setlogsock() 287 288 # setlogsock("stream") on a non-existent file must fail 289 eval { $r = setlogsock("stream", "plonk/log") }; 290 is( $@, '', "setlogsock() didn't croak"); 291 ok( !$r, "setlogsock() correctly failed with a non-existent stream path"); 292 293 # setlogsock("tcp") must fail if the service is not declared 294 my $service = getservbyname("syslog", "tcp") || getservbyname("syslogng", "tcp"); 295 skip "can't test setlogsock() tcp failure", 2 if $service; 296 eval { $r = setlogsock("tcp") }; 297 is( $@, '', "setlogsock() didn't croak"); 298 ok( !$r, "setlogsock() correctly failed when tcp services can't be resolved"); 299} 300 301BEGIN { $tests += 3 } 302SKIP: { 303 # case: configure Sys::Syslog to use the stream mechanism on a 304 # given file, but remove the file before openlog() is called, 305 # so it fails. 306 307 # create the log file 308 my $log = "t/stream"; 309 my $fh = FileHandle->new; 310 open $fh, ">$log" or skip "can't write file '$log': $!", 3; 311 close $fh; 312 313 # configure Sys::Syslog to use it 314 $r = eval { setlogsock("stream", $log) }; 315 is( $@, "", "setlogsock('stream', '$log') -> $r" ); 316 skip "can't test openlog() failure with a missing stream", 2 if !$r; 317 318 # remove the log and check that openlog() fails 319 unlink $log; 320 $r = eval { openlog('perl', 'ndelay', 'local0') }; 321 ok( !$r, "openlog() correctly failed with a non-existent stream" ); 322 like( $@, '/not writable/', "openlog() correctly croaked with a non-existent stream" ); 323} 324 325