8 # we enable all Perl warnings, but we don't "use warnings 'all'" because
9 # we want to disable the warnings generated by Sys::Syslog
11 use warnings qw(closure deprecated exiting glob io misc numeric once overflow
12 pack portable recursion redefine regexp severe signal substr
13 syntax taint uninitialized unpack untie utf8 void);
15 # if someone is using warnings::compat, the previous trick won't work, so we
16 # must manually disable warnings
17 $^W = 0 if $] < 5.006;
19 my $is_Win32 = $^O =~ /win32/i;
20 my $is_Cygwin = $^O =~ /cygwin/i;
22 # if testing in core, check that the module is at least available
23 if ($ENV{PERL_CORE}) {
24 plan skip_all => "Sys::Syslog was not build"
25 unless $Config{'extensions'} =~ /\bSyslog\b/;
29 plan skip_all => "Socket was not build"
30 unless $Config{'extensions'} =~ /\bSocket\b/;
35 # any remaining warning should be severly punished
36 BEGIN { eval "use Test::NoWarnings"; $tests = $@ ? 0 : 1; }
41 use_ok('Sys::Syslog', ':standard', ':extended', ':macros');
44 # check that the documented functions are correctly provided
45 can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
49 # check the diagnostics
51 eval { setlogsock() };
52 like( $@, qr/^Invalid argument passed to setlogsock/,
53 "calling setlogsock() with no argument" );
58 like( $@, qr/^syslog: expecting argument \$priority/,
59 "calling syslog() with no argument" );
61 eval { syslog(undef) };
62 like( $@, qr/^syslog: expecting argument \$priority/,
63 "calling syslog() with one undef argument" );
66 like( $@, qr/^syslog: expecting argument \$format/,
67 "calling syslog() with one empty argument" );
70 my $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
74 # try to open a syslog using a Unix or stream socket
76 skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
77 unless -e Sys::Syslog::_PATH_LOG();
79 # The only known $^O eq 'svr4' that needs this is NCR MP-RAS,
80 # but assuming 'stream' in SVR4 is probably not that bad.
81 my $sock_type = $^O =~ /^(solaris|irix|svr4|powerux)$/ ? 'stream' : 'unix';
83 eval { setlogsock($sock_type) };
84 is( $@, '', "setlogsock() called with '$sock_type'" );
86 local $TODO = "minor bug";
87 ok( $r, "setlogsock() should return true: '$r'" );
90 # open syslog with a "local0" facility
93 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
94 skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/;
95 is( $@, '', "openlog() called with facility 'local0'" );
96 ok( $r, "openlog() should return true: '$r'" );
99 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
100 is( $@, '', "syslog() called with level 'info'" );
101 ok( $r, "syslog() should return true: '$r'" );
104 $r = eval { closelog() } || 0;
105 is( $@, '', "closelog()" );
106 ok( $r, "closelog() should return true: '$r'" );
111 BEGIN { $tests += 22 * 8 }
112 # try to open a syslog using all the available connection methods
114 for my $sock_type (qw(native eventlog unix pipe stream inet tcp udp)) {
116 skip "the 'stream' mechanism because a previous mechanism with similar interface succeeded", 22
117 if $sock_type eq 'stream' and grep {/pipe|unix/} @passed;
119 # setlogsock() called with an arrayref
120 $r = eval { setlogsock([$sock_type]) } || 0;
121 skip "can't use '$sock_type' socket", 22 unless $r;
122 is( $@, '', "[$sock_type] setlogsock() called with ['$sock_type']" );
123 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" );
125 # setlogsock() called with a single argument
126 $r = eval { setlogsock($sock_type) } || 0;
127 skip "can't use '$sock_type' socket", 20 unless $r;
128 is( $@, '', "[$sock_type] setlogsock() called with '$sock_type'" );
129 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" );
131 # openlog() without option NDELAY
132 $r = eval { openlog('perl', '', 'local0') } || 0;
133 skip "can't connect to syslog", 18 if $@ =~ /^no connection to syslog available/;
134 is( $@, '', "[$sock_type] openlog() called with facility 'local0' and without option 'ndelay'" );
135 ok( $r, "[$sock_type] openlog() should return true: '$r'" );
137 # openlog() with the option NDELAY
138 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
139 skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/;
140 is( $@, '', "[$sock_type] openlog() called with facility 'local0' with option 'ndelay'" );
141 ok( $r, "[$sock_type] openlog() should return true: '$r'" );
143 # syslog() with negative level, should fail
144 $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0;
145 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level -1" );
146 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
148 # syslog() with invalid level, should fail
149 $r = eval { syslog("plonk", "$test_string by connecting to a $sock_type socket") } || 0;
150 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level plonk" );
151 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
153 # syslog() with levels "info" and "notice" (as a strings), should fail
154 $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0;
155 like( $@, '/^syslog: too many levels given: notice/', "[$sock_type] syslog() called with level 'info,notice'" );
156 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
158 # syslog() with facilities "local0" and "local1" (as a strings), should fail
159 $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0;
160 like( $@, '/^syslog: too many facilities given: local1/', "[$sock_type] syslog() called with level 'local0,local1'" );
161 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
163 # syslog() with level "info" (as a string), should pass
164 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
165 is( $@, '', "[$sock_type] syslog() called with level 'info' (string)" );
166 ok( $r, "[$sock_type] syslog() should return true: '$r'" );
168 # syslog() with level "info" (as a macro), should pass
170 $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket, setting a fake errno: %m") } || 0;
172 is( $@, '', "[$sock_type] syslog() called with level 'info' (macro)" );
173 ok( $r, "[$sock_type] syslog() should return true: '$r'" );
175 push @passed, $sock_type;
178 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
180 $r = eval { closelog() } || 0;
181 is( $@, '', "[$sock_type] closelog()" );
182 ok( $r, "[$sock_type] closelog() should return true: '$r'" );
188 BEGIN { $tests += 10 }
190 skip "not testing setlogsock('stream') on Win32", 10 if $is_Win32;
191 skip "the 'unix' mechanism works, so the tests will likely fail with the 'stream' mechanism", 10
192 if grep {/unix/} @passed;
194 skip "not testing setlogsock('stream'): _PATH_LOG unavailable", 10
195 unless -e Sys::Syslog::_PATH_LOG();
197 # setlogsock() with "stream" and an undef path
198 $r = eval { setlogsock("stream", undef ) } || '';
199 is( $@, '', "setlogsock() called, with 'stream' and an undef path" );
201 if (-x "/usr/sbin/syslog-ng") {
202 ok( $r, "setlogsock() on Cygwin with syslog-ng should return true: '$r'" );
205 ok( !$r, "setlogsock() on Cygwin without syslog-ng should return false: '$r'" );
209 ok( $r, "setlogsock() should return true: '$r'" );
212 # setlogsock() with "stream" and an empty path
213 $r = eval { setlogsock("stream", '' ) } || '';
214 is( $@, '', "setlogsock() called, with 'stream' and an empty path" );
215 ok( !$r, "setlogsock() should return false: '$r'" );
217 # setlogsock() with "stream" and /dev/null
218 $r = eval { setlogsock("stream", '/dev/null' ) } || '';
219 is( $@, '', "setlogsock() called, with 'stream' and '/dev/null'" );
220 ok( $r, "setlogsock() should return true: '$r'" );
222 # setlogsock() with "stream" and a non-existing file
223 $r = eval { setlogsock("stream", 'test.log' ) } || '';
224 is( $@, '', "setlogsock() called, with 'stream' and 'test.log' (file does not exist)" );
225 ok( !$r, "setlogsock() should return false: '$r'" );
227 # setlogsock() with "stream" and a local file
229 my $logfile = "test.log";
230 open(LOG, ">$logfile") or skip "can't create file '$logfile': $!", 2;
232 $r = eval { setlogsock("stream", $logfile ) } || '';
233 is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" );
234 ok( $r, "setlogsock() should return true: '$r'" );
240 BEGIN { $tests += 3 + 4 * 3 }
245 $oldmask = eval { setlogmask(0) } || 0;
246 is( $@, '', "setlogmask() called with a null mask" );
247 $r = eval { setlogmask(0) } || 0;
248 is( $@, '', "setlogmask() called with a null mask (second time)" );
249 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
253 ~LOG_MASK(LOG_INFO()),
254 LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()),
257 for my $newmask (@masks) {
258 $r = eval { setlogmask($newmask) } || 0;
259 is( $@, '', "setlogmask() called with a new mask" );
260 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
261 $r = eval { setlogmask(0) } || 0;
262 is( $@, '', "setlogmask() called with a null mask" );
263 is( $r, $newmask, "setlogmask() must return the new mask");
264 setlogmask($oldmask);