15 # we enable all Perl warnings, but we don't "use warnings 'all'" because
16 # we want to disable the warnings generated by Sys::Syslog
18 use warnings qw(closure deprecated exiting glob io misc numeric once overflow
19 pack portable recursion redefine regexp severe signal substr
20 syntax taint uninitialized unpack untie utf8 void);
22 my $is_Win32 = $^O =~ /win32/i;
23 my $is_Cygwin = $^O =~ /cygwin/i;
25 # if testing in core, check that the module is at least available
26 if ($ENV{PERL_CORE}) {
27 plan skip_all => "Sys::Syslog was not build"
28 unless $Config{'extensions'} =~ /\bSyslog\b/;
32 plan skip_all => "Socket was not build"
33 unless $Config{'extensions'} =~ /\bSocket\b/;
38 # any remaining warning should be severly punished
39 BEGIN { eval "use Test::NoWarnings"; $tests = $@ ? 0 : 1; }
44 use_ok('Sys::Syslog', ':standard', ':extended', ':macros');
47 # check that the documented functions are correctly provided
48 can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
52 # check the diagnostics
54 eval { setlogsock() };
55 like( $@, qr/^Invalid argument passed to setlogsock/,
56 "calling setlogsock() with no argument" );
61 like( $@, qr/^syslog: expecting argument \$priority/,
62 "calling syslog() with no argument" );
64 eval { syslog(undef) };
65 like( $@, qr/^syslog: expecting argument \$priority/,
66 "calling syslog() with one undef argument" );
69 like( $@, qr/^syslog: expecting argument \$format/,
70 "calling syslog() with one empty argument" );
73 my $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
77 # try to open a syslog using a Unix or stream socket
79 skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
80 unless -e Sys::Syslog::_PATH_LOG();
82 # The only known $^O eq 'svr4' that needs this is NCR MP-RAS,
83 # but assuming 'stream' in SVR4 is probably not that bad.
84 my $sock_type = $^O =~ /^(solaris|irix|svr4|powerux)$/ ? 'stream' : 'unix';
86 eval { setlogsock($sock_type) };
87 is( $@, '', "setlogsock() called with '$sock_type'" );
89 local $TODO = "minor bug";
90 ok( $r, "setlogsock() should return true: '$r'" );
93 # open syslog with a "local0" facility
96 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
97 skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/;
98 is( $@, '', "openlog() called with facility 'local0'" );
99 ok( $r, "openlog() should return true: '$r'" );
102 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
103 is( $@, '', "syslog() called with level 'info'" );
104 ok( $r, "syslog() should return true: '$r'" );
107 $r = eval { closelog() } || 0;
108 is( $@, '', "closelog()" );
109 ok( $r, "closelog() should return true: '$r'" );
114 BEGIN { $tests += 20 * 8 }
115 # try to open a syslog using all the available connection methods
117 for my $sock_type (qw(native eventlog unix pipe stream inet tcp udp)) {
119 skip "the 'stream' mechanism because a previous mechanism with similar interface succeeded", 20
120 if $sock_type eq 'stream' and grep {/pipe|unix/} @passed;
122 # setlogsock() called with an arrayref
123 $r = eval { setlogsock([$sock_type]) } || 0;
124 skip "can't use '$sock_type' socket", 20 unless $r;
125 is( $@, '', "[$sock_type] setlogsock() called with ['$sock_type']" );
126 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" );
128 # setlogsock() called with a single argument
129 $r = eval { setlogsock($sock_type) } || 0;
130 skip "can't use '$sock_type' socket", 18 unless $r;
131 is( $@, '', "[$sock_type] setlogsock() called with '$sock_type'" );
132 ok( $r, "[$sock_type] setlogsock() should return true: '$r'" );
134 # openlog() without option NDELAY
135 $r = eval { openlog('perl', '', 'local0') } || 0;
136 skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/;
137 is( $@, '', "[$sock_type] openlog() called with facility 'local0' and without option 'ndelay'" );
138 ok( $r, "[$sock_type] openlog() should return true: '$r'" );
140 # openlog() with the option NDELAY
141 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
142 skip "can't connect to syslog", 14 if $@ =~ /^no connection to syslog available/;
143 is( $@, '', "[$sock_type] openlog() called with facility 'local0' with option 'ndelay'" );
144 ok( $r, "[$sock_type] openlog() should return true: '$r'" );
146 # syslog() with negative level, should fail
147 $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0;
148 like( $@, '/^syslog: invalid level\/facility: /', "[$sock_type] syslog() called with level -1" );
149 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
151 # syslog() with levels "info" and "notice" (as a strings), should fail
152 $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0;
153 like( $@, '/^syslog: too many levels given: notice/', "[$sock_type] syslog() called with level 'info,notice'" );
154 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
156 # syslog() with facilities "local0" and "local1" (as a strings), should fail
157 $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0;
158 like( $@, '/^syslog: too many facilities given: local1/', "[$sock_type] syslog() called with level 'local0,local1'" );
159 ok( !$r, "[$sock_type] syslog() should return false: '$r'" );
161 # syslog() with level "info" (as a string), should pass
162 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
163 is( $@, '', "[$sock_type] syslog() called with level 'info' (string)" );
164 ok( $r, "[$sock_type] syslog() should return true: '$r'" );
166 # syslog() with level "info" (as a macro), should pass
168 $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket, setting a fake errno: %m") } || 0;
170 is( $@, '', "[$sock_type] syslog() called with level 'info' (macro)" );
171 ok( $r, "[$sock_type] syslog() should return true: '$r'" );
173 push @passed, $sock_type;
176 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
178 $r = eval { closelog() } || 0;
179 is( $@, '', "[$sock_type] closelog()" );
180 ok( $r, "[$sock_type] closelog() should return true: '$r'" );
186 BEGIN { $tests += 10 }
188 skip "not testing setlogsock('stream') on Win32", 10 if $is_Win32;
189 skip "the 'unix' mechanism works, so the tests will likely fail with the 'stream' mechanism", 10
190 if grep {/unix/} @passed;
192 # setlogsock() with "stream" and an undef path
193 $r = eval { setlogsock("stream", undef ) } || '';
194 is( $@, '', "setlogsock() called, with 'stream' and an undef path" );
196 if (-x "/usr/sbin/syslog-ng") {
197 ok( $r, "setlogsock() on Cygwin with syslog-ng should return true: '$r'" );
200 ok( !$r, "setlogsock() on Cygwin without syslog-ng should return false: '$r'" );
204 ok( $r, "setlogsock() should return true: '$r'" );
207 # setlogsock() with "stream" and an empty path
208 $r = eval { setlogsock("stream", '' ) } || '';
209 is( $@, '', "setlogsock() called, with 'stream' and an empty path" );
210 ok( !$r, "setlogsock() should return false: '$r'" );
212 # setlogsock() with "stream" and /dev/null
213 $r = eval { setlogsock("stream", '/dev/null' ) } || '';
214 is( $@, '', "setlogsock() called, with 'stream' and '/dev/null'" );
215 ok( $r, "setlogsock() should return true: '$r'" );
217 # setlogsock() with "stream" and a non-existing file
218 $r = eval { setlogsock("stream", 'test.log' ) } || '';
219 is( $@, '', "setlogsock() called, with 'stream' and 'test.log' (file does not exist)" );
220 ok( !$r, "setlogsock() should return false: '$r'" );
222 # setlogsock() with "stream" and a local file
224 my $logfile = "test.log";
225 open(LOG, ">$logfile") or skip "can't create file '$logfile': $!", 2;
227 $r = eval { setlogsock("stream", $logfile ) } || '';
228 is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" );
229 ok( $r, "setlogsock() should return true: '$r'" );
235 BEGIN { $tests += 3 + 4 * 3 }
240 $oldmask = eval { setlogmask(0) } || 0;
241 is( $@, '', "setlogmask() called with a null mask" );
242 $r = eval { setlogmask(0) } || 0;
243 is( $@, '', "setlogmask() called with a null mask (second time)" );
244 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
248 ~LOG_MASK(LOG_INFO()),
249 LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()),
252 for my $newmask (@masks) {
253 $r = eval { setlogmask($newmask) } || 0;
254 is( $@, '', "setlogmask() called with a new mask" );
255 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
256 $r = eval { setlogmask(0) } || 0;
257 is( $@, '', "setlogmask() called with a null mask" );
258 is( $r, $newmask, "setlogmask() must return the new mask");
259 setlogmask($oldmask);