4 if( $ENV{PERL_CORE} ) {
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
17 use warnings qw(closure deprecated exiting glob io misc numeric once overflow
18 pack portable recursion redefine regexp severe signal substr
19 syntax taint uninitialized unpack untie utf8 void);
21 # check that the module is at least available
22 plan skip_all => "Sys::Syslog was not build"
23 unless $Config{'extensions'} =~ /\bSyslog\b/;
26 plan skip_all => "Socket was not build"
27 unless $Config{'extensions'} =~ /\bSocket\b/;
35 use_ok('Sys::Syslog', ':standard', ':extended', ':macros');
38 # check that the documented functions are correctly provided
39 can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
43 # check the diagnostics
45 eval { setlogsock() };
46 like( $@, qr/^Invalid argument passed to setlogsock; must be 'stream', 'unix', 'native', 'tcp', 'udp' or 'inet'/,
47 "calling setlogsock() with no argument" );
52 like( $@, qr/^syslog: expecting argument \$priority/,
53 "calling syslog() with no argument" );
55 eval { syslog(undef) };
56 like( $@, qr/^syslog: expecting argument \$priority/,
57 "calling syslog() with one undef argument" );
60 like( $@, qr/^syslog: expecting argument \$format/,
61 "calling syslog() with one empty argument" );
65 eval { setlogsock() };
66 like( $@, qr/^Invalid argument passed to setlogsock; must be 'stream', 'unix', 'native', 'tcp', 'udp' or 'inet'/,
67 "calling setlogsock() with no argument" );
69 my $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
73 # try to open a syslog using a Unix or stream socket
75 skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
76 unless -e Sys::Syslog::_PATH_LOG();
78 # The only known $^O eq 'svr4' that needs this is NCR MP-RAS,
79 # but assuming 'stream' in SVR4 is probably not that bad.
80 my $sock_type = $^O =~ /^(solaris|irix|svr4|powerux)$/ ? 'stream' : 'unix';
82 eval { setlogsock($sock_type) };
83 is( $@, '', "setlogsock() called with '$sock_type'" );
85 local $TODO = "minor bug";
86 ok( $r, "setlogsock() should return true: '$r'" );
89 # open syslog with a "local0" facility
92 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
93 skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/;
94 is( $@, '', "openlog() called with facility 'local0'" );
95 ok( $r, "openlog() should return true: '$r'" );
98 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
99 is( $@, '', "syslog() called with level 'info'" );
100 ok( $r, "syslog() should return true: '$r'" );
103 $r = eval { closelog() } || 0;
104 is( $@, '', "closelog()" );
105 ok( $r, "closelog() should return true: '$r'" );
110 BEGIN { $tests += 20 * 7 }
111 # try to open a syslog using all the available connection methods
112 for my $sock_type (qw(stream unix native inet tcp udp console)) {
114 # setlogsock() called with an arrayref
115 $r = eval { setlogsock([$sock_type]) } || 0;
116 skip "can't use '$sock_type' socket", 20 unless $r;
117 is( $@, '', "setlogsock() called with ['$sock_type']" );
118 ok( $r, "setlogsock() should return true: '$r'" );
120 # setlogsock() called with a single argument
121 $r = eval { setlogsock($sock_type) } || 0;
122 skip "can't use '$sock_type' socket", 18 unless $r;
123 is( $@, '', "setlogsock() called with '$sock_type'" );
124 ok( $r, "setlogsock() should return true: '$r'" );
126 # openlog() without option NDELAY
127 $r = eval { openlog('perl', '', 'local0') } || 0;
128 skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/;
129 is( $@, '', "openlog() called with facility 'local0' and without option 'ndelay'" );
130 ok( $r, "openlog() should return true: '$r'" );
132 # openlog() with the option NDELAY
133 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
134 skip "can't connect to syslog", 14 if $@ =~ /^no connection to syslog available/;
135 is( $@, '', "openlog() called with facility 'local0' with option 'ndelay'" );
136 ok( $r, "openlog() should return true: '$r'" );
138 # syslog() with negative level, should fail
139 $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0;
140 like( $@, '/^syslog: invalid level\/facility: /', "syslog() called with level -1" );
141 ok( !$r, "syslog() should return false: '$r'" );
143 # syslog() with levels "info" and "notice" (as a strings), should fail
144 $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0;
145 like( $@, '/^syslog: too many levels given: notice/', "syslog() called with level 'info,notice'" );
146 ok( !$r, "syslog() should return false: '$r'" );
148 # syslog() with facilities "local0" and "local1" (as a strings), should fail
149 $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0;
150 like( $@, '/^syslog: too many facilities given: local1/', "syslog() called with level 'info,notice'" );
151 ok( !$r, "syslog() should return false: '$r'" );
153 # syslog() with level "info" (as a string), should pass
154 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket (errno=%m)") } || 0;
155 is( $@, '', "syslog() called with level 'info' (string)" );
156 ok( $r, "syslog() should return true: '$r'" );
158 # syslog() with level "info" (as a macro), should pass
159 $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket (errno=%m)") } || 0;
160 is( $@, '', "syslog() called with level 'info' (macro)" );
161 ok( $r, "syslog() should return true: '$r'" );
163 # syslog() with facility "kern" (as a string), should fail
164 #$r = eval { syslog('kern', "$test_string by connecting to a $sock_type socket") } || 0;
165 #like( $@, '/^syslog: invalid level/facility: kern/', "syslog() called with facility 'kern'" );
166 #ok( !$r, "syslog() should return false: '$r'" );
168 # syslog() with facility "kern" (as a macro), should fail
169 #$r = eval { syslog(LOG_KERN, "$test_string by connecting to a $sock_type socket") } || 0;
170 #like( $@, '/^syslog: invalid level/facility: 0/', "syslog() called with facility 'kern'" );
171 #ok( !$r, "syslog() should return false: '$r'" );
174 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
176 $r = eval { closelog() } || 0;
177 is( $@, '', "closelog()" );
178 ok( $r, "closelog() should return true: '$r'" );
184 BEGIN { $tests += 10 }
185 # setlogsock() with "stream" and an undef path
186 $r = eval { setlogsock("stream", undef ) } || '';
187 is( $@, '', "setlogsock() called, with 'stream' and an undef path" );
188 ok( $r, "setlogsock() should return true: '$r'" );
190 # setlogsock() with "stream" and an empty path
191 $r = eval { setlogsock("stream", '' ) } || '';
192 is( $@, '', "setlogsock() called, with 'stream' and an empty path" );
193 ok( !$r, "setlogsock() should return false: '$r'" );
195 # setlogsock() with "stream" and /dev/null
196 $r = eval { setlogsock("stream", '/dev/null' ) } || '';
197 is( $@, '', "setlogsock() called, with 'stream' and '/dev/null'" );
198 ok( $r, "setlogsock() should return true: '$r'" );
200 # setlogsock() with "stream" and a non-existing file
201 $r = eval { setlogsock("stream", 'test.log' ) } || '';
202 is( $@, '', "setlogsock() called, with 'stream' and 'test.log' (file does not exist)" );
203 ok( !$r, "setlogsock() should return false: '$r'" );
205 # setlogsock() with "stream" and a local file
207 my $logfile = "test.log";
208 open(LOG, ">$logfile") or skip "can't create file '$logfile': $!", 2;
210 $r = eval { setlogsock("stream", $logfile ) } || '';
211 is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" );
212 ok( $r, "setlogsock() should return true: '$r'" );
217 BEGIN { $tests += 3 + 4 * 3 }
222 $oldmask = eval { setlogmask(0) } || 0;
223 is( $@, '', "setlogmask() called with a null mask" );
224 $r = eval { setlogmask(0) } || 0;
225 is( $@, '', "setlogmask() called with a null mask (second time)" );
226 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
230 ~LOG_MASK(LOG_INFO()),
231 LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()),
234 for my $newmask (@masks) {
235 $r = eval { setlogmask($newmask) } || 0;
236 is( $@, '', "setlogmask() called with a new mask" );
237 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
238 $r = eval { setlogmask(0) } || 0;
239 is( $@, '', "setlogmask() called with a null mask" );
240 is( $r, $newmask, "setlogmask() must return the new mask");
241 setlogmask($oldmask);