Fw: CPAN Upload: S/SA/SAPER/Sys-Syslog-0.15.tar.gz
[p5sagit/p5-mst-13.2.git] / ext / Sys / Syslog / t / syslog.t
1 #!/usr/bin/perl -T
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't';
6         @INC = '../lib';
7     }
8 }
9
10 use strict;
11 use Config;
12 use File::Spec;
13 use Test::More;
14
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);
20
21 # check that the module is at least available
22 plan skip_all => "Sys::Syslog was not build" 
23   unless $Config{'extensions'} =~ /\bSyslog\b/;
24
25 # we also need Socket
26 plan skip_all => "Socket was not build" 
27   unless $Config{'extensions'} =~ /\bSocket\b/;
28
29 my $tests;
30 plan tests => $tests;
31
32 BEGIN { $tests = 1 }
33 # ok, now loads them
34 eval 'use Socket';
35 use_ok('Sys::Syslog', ':standard', ':extended', ':macros');
36
37 BEGIN { $tests += 1 }
38 # check that the documented functions are correctly provided
39 can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
40
41
42 BEGIN { $tests += 1 }
43 # check the diagnostics
44 # setlogsock()
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" );
48
49 BEGIN { $tests += 3 }
50 # syslog()
51 eval { syslog() };
52 like( $@, qr/^syslog: expecting argument \$priority/, 
53     "calling syslog() with no argument" );
54
55 eval { syslog(undef) };
56 like( $@, qr/^syslog: expecting argument \$priority/, 
57     "calling syslog() with one undef argument" );
58
59 eval { syslog('') };
60 like( $@, qr/^syslog: expecting argument \$format/, 
61     "calling syslog() with one empty argument" );
62
63 BEGIN { $tests += 1 }
64 # setlogsock()
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" );
68
69 my $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
70 my $r = 0;
71
72 BEGIN { $tests += 8 }
73 # try to open a syslog using a Unix or stream socket
74 SKIP: {
75     skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
76       unless -e Sys::Syslog::_PATH_LOG();
77
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';
81
82     eval { setlogsock($sock_type) };
83     is( $@, '', "setlogsock() called with '$sock_type'" );
84     TODO: {
85         local $TODO = "minor bug";
86         ok( $r, "setlogsock() should return true: '$r'" );
87     }
88
89     # open syslog with a "local0" facility
90     SKIP: {
91         # openlog()
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'" );
96
97         # syslog()
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'" );
101
102         # closelog()
103         $r = eval { closelog() } || 0;
104         is( $@, '', "closelog()" );
105         ok( $r, "closelog() should return true: '$r'" );
106     }
107 }
108
109
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)) {
113     SKIP: {
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'" );
119
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'" );
125
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'" );
131
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'" );
137
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'" );
142
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'" );
147
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'" );
152
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'" );
157
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'" );
162
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'" );
167
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'" );
172
173         SKIP: {
174             skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
175             # closelog()
176             $r = eval { closelog() } || 0;
177             is( $@, '', "closelog()" );
178             ok( $r, "closelog() should return true: '$r'" );
179         }
180     }
181 }
182
183
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'" );
189
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'" );
194
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'" );
199
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'" );
204
205 # setlogsock() with "stream" and a local file
206 SKIP: {
207     my $logfile = "test.log";
208     open(LOG, ">$logfile") or skip "can't create file '$logfile': $!", 2;
209     close(LOG);
210     $r = eval { setlogsock("stream", $logfile ) } || '';
211     is( $@, '', "setlogsock() called, with 'stream' and '$logfile' (file exists)" );
212     ok( $r, "setlogsock() should return true: '$r'" );
213     unlink($logfile);
214 }
215
216
217 BEGIN { $tests += 3 + 4 * 3 }
218 # setlogmask()
219 {
220     my $oldmask = 0;
221
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");
227
228     my @masks = (
229         LOG_MASK(LOG_ERR()), 
230         ~LOG_MASK(LOG_INFO()), 
231         LOG_MASK(LOG_CRIT()) | LOG_MASK(LOG_ERR()) | LOG_MASK(LOG_WARNING()), 
232     );
233
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);
242     }
243 }