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 106 # open syslog with a "local0" facility 107 SKIP: { 108 # openlog() 109 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0; 110 skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/; 111 is( $@, '', "openlog() called with facility 'local0'" ); 112 ok( $r, "openlog() should return true: '$r'" ); 113 114 # syslog() 115 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0; 116 is( $@, '', "syslog() called with level 'info'" ); 117 ok( $r, "syslog() should return true: '$r'" ); 118 119 # closelog() 120 $r = eval { closelog() } || 0; 121 is( $@, '', "closelog()" ); 122 ok( $r, "closelog() should return true: '$r'" ); 123 } 124} 125 126# try to open a syslog using all the available connection methods 127# handle other connections in t/syslog.t 128 129my @passed = (); 130 131BEGIN { $tests += 22 * 2 } 132for my $sock_type (qw(inet udp)) { 133 SKIP: { 134 skip "the 'stream' mechanism because a previous mechanism with similar interface succeeded", 22 135 if $sock_type eq 'stream' and grep {/pipe|unix/} @passed; 136 # setlogsock() called with an arrayref 137 $r = eval { setlogsock([$sock_type]) } || 0; 138 skip "can't use '$sock_type' socket", 22 unless $r; 139 is( $@, '', "[$sock_type] setlogsock() called with ['$sock_type']" ); 140 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" ); 141 142 # setlogsock() called with a single argument 143 $r = eval { setlogsock($sock_type) } || 0; 144 skip "can't use '$sock_type' socket", 20 unless $r; 145 is( $@, '', "[$sock_type] setlogsock() called with '$sock_type'" ); 146 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" ); 147 148 # openlog() without option NDELAY 149 $r = eval { openlog('perl', '', 'local0') } || 0; 150 skip "can't connect to syslog", 18 if $@ =~ /^no connection to syslog available/; 151 is( $@, '', "[$sock_type] openlog() called with facility 'local0' and without option 'ndelay'" ); 152 ok( $r, "[$sock_type] openlog() should return true: '$r'" ); 153 154 # openlog() with the option NDELAY 155 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0; 156 skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/; 157 is( $@, '', "[$sock_type] openlog() called with facility 'local0' with option 'ndelay'" ); 158 ok( $r, "[$sock_type] openlog() should return true: '$r'" ); 159 160 # syslog() with negative level, should fail 161 $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0; 162 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level -1" ); 163 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 164 165 # syslog() with invalid level, should fail 166 $r = eval { syslog("plonk", "$test_string by connecting to a $sock_type socket") } || 0; 167 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level plonk" ); 168 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 169 170 # syslog() with levels "info" and "notice" (as a strings), should fail 171 $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0; 172 like( $@, '/^syslog: too many levels given: notice/', "[$sock_type] syslog() called with level 'info,notice'" ); 173 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 174 175 # syslog() with facilities "local0" and "local1" (as a strings), should fail 176 $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0; 177 like( $@, '/^syslog: too many facilities given: local1/', "[$sock_type] syslog() called with level 'local0,local1'" ); 178 ok( !$r, "[$sock_type] syslog() should return false: '$r'" ); 179 180 # syslog() with level "info" (as a string), should pass 181 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0; 182 is( $@, '', "[$sock_type] syslog() called with level 'info' (string)" ); 183 ok( $r, "[$sock_type] syslog() should return true: '$r'" ); 184 185 # syslog() with level "info" (as a macro), should pass 186 { local $! = 1; 187 $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket, setting a fake errno: %m") } || 0; 188 } 189 is( $@, '', "[$sock_type] syslog() called with level 'info' (macro)" ); 190 ok( $r, "[$sock_type] syslog() should return true: '$r'" ); 191 192 push @passed, $sock_type; 193 194 SKIP: { 195 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console'; 196 # closelog() 197 $r = eval { closelog() } || 0; 198 is( $@, '', "[$sock_type] closelog()" ); 199 ok( $r, "[$sock_type] closelog() should return true: '$r'" ); 200 } 201 } 202} 203 204 205 206