Upgrade to Sys-Syslog-0.17
[p5sagit/p5-mst-13.2.git] / ext / Sys / Syslog / t / syslog.t
CommitLineData
89c3c464 1#!/usr/bin/perl -T
34b7e82b 2
3BEGIN {
8168e71f 4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
6 @INC = '../lib';
cc8876c3 7 }
8168e71f 8}
1b31946b 9
8168e71f 10use strict;
8168e71f 11use Config;
942974c1 12use File::Spec;
13use Test::More;
02d52598 14
89c3c464 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
8edeb3ad 17no warnings;
89c3c464 18use 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);
21
8168e71f 22# check that the module is at least available
23plan skip_all => "Sys::Syslog was not build"
24 unless $Config{'extensions'} =~ /\bSyslog\b/;
1b31946b 25
8168e71f 26# we also need Socket
27plan skip_all => "Socket was not build"
28 unless $Config{'extensions'} =~ /\bSocket\b/;
34b7e82b 29
6e4ef777 30my $tests;
31plan tests => $tests;
8168e71f 32
6e4ef777 33BEGIN { $tests = 1 }
34# ok, now loads them
35eval 'use Socket';
36use_ok('Sys::Syslog', ':standard', ':extended', ':macros');
68a5ccec 37
6e4ef777 38BEGIN { $tests += 1 }
8168e71f 39# check that the documented functions are correctly provided
40can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
41
42
6e4ef777 43BEGIN { $tests += 1 }
8168e71f 44# check the diagnostics
45# setlogsock()
46eval { setlogsock() };
89c3c464 47like( $@, qr/^Invalid argument passed to setlogsock; must be 'stream', 'unix', 'native', 'tcp', 'udp' or 'inet'/,
8168e71f 48 "calling setlogsock() with no argument" );
49
6e4ef777 50BEGIN { $tests += 3 }
8168e71f 51# syslog()
52eval { syslog() };
53like( $@, qr/^syslog: expecting argument \$priority/,
54 "calling syslog() with no argument" );
55
6e4ef777 56eval { syslog(undef) };
57like( $@, qr/^syslog: expecting argument \$priority/,
58 "calling syslog() with one undef argument" );
59
60eval { syslog('') };
61like( $@, qr/^syslog: expecting argument \$format/,
62 "calling syslog() with one empty argument" );
63
64BEGIN { $tests += 1 }
65# setlogsock()
66eval { setlogsock() };
89c3c464 67like( $@, qr/^Invalid argument passed to setlogsock; must be 'stream', 'unix', 'native', 'tcp', 'udp' or 'inet'/,
6e4ef777 68 "calling setlogsock() with no argument" );
69
8168e71f 70my $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
71my $r = 0;
72
6e4ef777 73BEGIN { $tests += 8 }
942974c1 74# try to open a syslog using a Unix or stream socket
8168e71f 75SKIP: {
942974c1 76 skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
8168e71f 77 unless -e Sys::Syslog::_PATH_LOG();
78
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';
82
83 eval { setlogsock($sock_type) };
84 is( $@, '', "setlogsock() called with '$sock_type'" );
85 TODO: {
86 local $TODO = "minor bug";
942974c1 87 ok( $r, "setlogsock() should return true: '$r'" );
f41ed1f7 88 }
34b7e82b 89
942974c1 90 # open syslog with a "local0" facility
8168e71f 91 SKIP: {
942974c1 92 # openlog()
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'" );
97
98 # syslog()
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'" );
102
103 # closelog()
104 $r = eval { closelog() } || 0;
105 is( $@, '', "closelog()" );
106 ok( $r, "closelog() should return true: '$r'" );
8168e71f 107 }
b75c8c73 108}
8168e71f 109
6e4ef777 110
89c3c464 111BEGIN { $tests += 20 * 7 }
942974c1 112# try to open a syslog using all the available connection methods
89c3c464 113for my $sock_type (qw(stream unix native inet tcp udp console)) {
942974c1 114 SKIP: {
6e4ef777 115 # setlogsock() called with an arrayref
942974c1 116 $r = eval { setlogsock([$sock_type]) } || 0;
6e4ef777 117 skip "can't use '$sock_type' socket", 20 unless $r;
118 is( $@, '', "setlogsock() called with ['$sock_type']" );
119 ok( $r, "setlogsock() should return true: '$r'" );
120
121 # setlogsock() called with a single argument
122 $r = eval { setlogsock($sock_type) } || 0;
123 skip "can't use '$sock_type' socket", 18 unless $r;
942974c1 124 is( $@, '', "setlogsock() called with '$sock_type'" );
125 ok( $r, "setlogsock() should return true: '$r'" );
126
127 # openlog() without option NDELAY
128 $r = eval { openlog('perl', '', 'local0') } || 0;
6e4ef777 129 skip "can't connect to syslog", 16 if $@ =~ /^no connection to syslog available/;
942974c1 130 is( $@, '', "openlog() called with facility 'local0' and without option 'ndelay'" );
131 ok( $r, "openlog() should return true: '$r'" );
132
133 # openlog() with the option NDELAY
134 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
6e4ef777 135 skip "can't connect to syslog", 14 if $@ =~ /^no connection to syslog available/;
942974c1 136 is( $@, '', "openlog() called with facility 'local0' with option 'ndelay'" );
137 ok( $r, "openlog() should return true: '$r'" );
138
6e4ef777 139 # syslog() with negative level, should fail
140 $r = eval { syslog(-1, "$test_string by connecting to a $sock_type socket") } || 0;
141 like( $@, '/^syslog: invalid level\/facility: /', "syslog() called with level -1" );
142 ok( !$r, "syslog() should return false: '$r'" );
143
144 # syslog() with levels "info" and "notice" (as a strings), should fail
145 $r = eval { syslog('info,notice', "$test_string by connecting to a $sock_type socket") } || 0;
146 like( $@, '/^syslog: too many levels given: notice/', "syslog() called with level 'info,notice'" );
147 ok( !$r, "syslog() should return false: '$r'" );
148
149 # syslog() with facilities "local0" and "local1" (as a strings), should fail
150 $r = eval { syslog('local0,local1', "$test_string by connecting to a $sock_type socket") } || 0;
151 like( $@, '/^syslog: too many facilities given: local1/', "syslog() called with level 'info,notice'" );
152 ok( !$r, "syslog() should return false: '$r'" );
153
942974c1 154 # syslog() with level "info" (as a string), should pass
6e4ef777 155 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket (errno=%m)") } || 0;
156 is( $@, '', "syslog() called with level 'info' (string)" );
942974c1 157 ok( $r, "syslog() should return true: '$r'" );
158
159 # syslog() with level "info" (as a macro), should pass
6e4ef777 160 $r = eval { syslog(LOG_INFO(), "$test_string by connecting to a $sock_type socket (errno=%m)") } || 0;
161 is( $@, '', "syslog() called with level 'info' (macro)" );
942974c1 162 ok( $r, "syslog() should return true: '$r'" );
163
164 # syslog() with facility "kern" (as a string), should fail
6e4ef777 165 #$r = eval { syslog('kern', "$test_string by connecting to a $sock_type socket") } || 0;
166 #like( $@, '/^syslog: invalid level/facility: kern/', "syslog() called with facility 'kern'" );
167 #ok( !$r, "syslog() should return false: '$r'" );
942974c1 168
169 # syslog() with facility "kern" (as a macro), should fail
6e4ef777 170 #$r = eval { syslog(LOG_KERN, "$test_string by connecting to a $sock_type socket") } || 0;
171 #like( $@, '/^syslog: invalid level/facility: 0/', "syslog() called with facility 'kern'" );
172 #ok( !$r, "syslog() should return false: '$r'" );
942974c1 173
174 SKIP: {
175 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
176 # closelog()
177 $r = eval { closelog() } || 0;
178 is( $@, '', "closelog()" );
179 ok( $r, "closelog() should return true: '$r'" );
180 }
181 }
b75c8c73 182}
8168e71f 183
6e4ef777 184
185BEGIN { $tests += 10 }
186# setlogsock() with "stream" and an undef path
187$r = eval { setlogsock("stream", undef ) } || '';
188is( $@, '', "setlogsock() called, with 'stream' and an undef path" );
189ok( $r, "setlogsock() should return true: '$r'" );
190
191# setlogsock() with "stream" and an empty path
192$r = eval { setlogsock("stream", '' ) } || '';
193is( $@, '', "setlogsock() called, with 'stream' and an empty path" );
194ok( !$r, "setlogsock() should return false: '$r'" );
195
196# setlogsock() with "stream" and /dev/null
197$r = eval { setlogsock("stream", '/dev/null' ) } || '';
198is( $@, '', "setlogsock() called, with 'stream' and '/dev/null'" );
199ok( $r, "setlogsock() should return true: '$r'" );
200
201# setlogsock() with "stream" and a non-existing file
202$r = eval { setlogsock("stream", 'test.log' ) } || '';
203is( $@, '', "setlogsock() called, with 'stream' and 'test.log' (file does not exist)" );
204ok( !$r, "setlogsock() should return false: '$r'" );
205
206# setlogsock() with "stream" and a local file
207SKIP: {
208 my $logfile = "test.log";
209 open(LOG, ">$logfile") or skip "can't create file '$logfile': $!", 2;
210 close(LOG);
211 $r = eval { setlogsock("stream", $logfile ) } || '';
212 is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" );
213 ok( $r, "setlogsock() should return true: '$r'" );
214 unlink($logfile);
215}
216
217
218BEGIN { $tests += 3 + 4 * 3 }
942974c1 219# setlogmask()
220{
221 my $oldmask = 0;
222
223 $oldmask = eval { setlogmask(0) } || 0;
224 is( $@, '', "setlogmask() called with a null mask" );
225 $r = eval { setlogmask(0) } || 0;
226 is( $@, '', "setlogmask() called with a null mask (second time)" );
227 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
228
6e4ef777 229 my @masks = (
230 LOG_MASK(LOG_ERR()),
231 ~LOG_MASK(LOG_INFO()),
232 LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()),
233 );
234
235 for my $newmask (@masks) {
942974c1 236 $r = eval { setlogmask($newmask) } || 0;
237 is( $@, '', "setlogmask() called with a new mask" );
238 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
239 $r = eval { setlogmask(0) } || 0;
240 is( $@, '', "setlogmask() called with a null mask" );
241 is( $r, $newmask, "setlogmask() must return the new mask");
242 setlogmask($oldmask);
243 }
244}