Fix pod nit in perltodo.
[p5sagit/p5-mst-13.2.git] / ext / Sys / Syslog / t / syslog.t
CommitLineData
8168e71f 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
8168e71f 15# check that the module is at least available
16plan skip_all => "Sys::Syslog was not build"
17 unless $Config{'extensions'} =~ /\bSyslog\b/;
1b31946b 18
8168e71f 19# we also need Socket
20plan skip_all => "Socket was not build"
21 unless $Config{'extensions'} =~ /\bSocket\b/;
34b7e82b 22
68a5ccec 23BEGIN {
942974c1 24 plan tests => 119;
8168e71f 25
26 # ok, now loads them
27 eval 'use Socket';
942974c1 28 use_ok('Sys::Syslog', ':standard', ':extended', ':macros');
68a5ccec 29}
30
8168e71f 31# check that the documented functions are correctly provided
32can_ok( 'Sys::Syslog' => qw(openlog syslog syslog setlogmask setlogsock closelog) );
33
34
35# check the diagnostics
36# setlogsock()
37eval { setlogsock() };
38like( $@, qr/^Invalid argument passed to setlogsock; must be 'stream', 'unix', 'tcp', 'udp' or 'inet'/,
39 "calling setlogsock() with no argument" );
40
41# syslog()
42eval { syslog() };
43like( $@, qr/^syslog: expecting argument \$priority/,
44 "calling syslog() with no argument" );
45
46my $test_string = "uid $< is testing Perl $] syslog(3) capabilities";
47my $r = 0;
48
942974c1 49# try to open a syslog using a Unix or stream socket
8168e71f 50SKIP: {
942974c1 51 skip "can't connect to Unix socket: _PATH_LOG unavailable", 8
8168e71f 52 unless -e Sys::Syslog::_PATH_LOG();
53
54 # The only known $^O eq 'svr4' that needs this is NCR MP-RAS,
55 # but assuming 'stream' in SVR4 is probably not that bad.
56 my $sock_type = $^O =~ /^(solaris|irix|svr4|powerux)$/ ? 'stream' : 'unix';
57
58 eval { setlogsock($sock_type) };
59 is( $@, '', "setlogsock() called with '$sock_type'" );
60 TODO: {
61 local $TODO = "minor bug";
942974c1 62 ok( $r, "setlogsock() should return true: '$r'" );
f41ed1f7 63 }
34b7e82b 64
942974c1 65 # open syslog with a "local0" facility
8168e71f 66 SKIP: {
942974c1 67 # openlog()
68 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
69 skip "can't connect to syslog", 6 if $@ =~ /^no connection to syslog available/;
70 is( $@, '', "openlog() called with facility 'local0'" );
71 ok( $r, "openlog() should return true: '$r'" );
72
73 # syslog()
74 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
75 is( $@, '', "syslog() called with level 'info'" );
76 ok( $r, "syslog() should return true: '$r'" );
77
78 # closelog()
79 $r = eval { closelog() } || 0;
80 is( $@, '', "closelog()" );
81 ok( $r, "closelog() should return true: '$r'" );
8168e71f 82 }
b75c8c73 83}
8168e71f 84
942974c1 85# try to open a syslog using all the available connection methods
86for my $sock_type (qw(stream unix inet tcp udp console)) {
87 SKIP: {
88 # setlogsock()
89 $r = eval { setlogsock([$sock_type]) } || 0;
90 skip "can't use '$sock_type' socket", 16 unless $r;
91 is( $@, '', "setlogsock() called with '$sock_type'" );
92 ok( $r, "setlogsock() should return true: '$r'" );
93
94 # openlog() without option NDELAY
95 $r = eval { openlog('perl', '', 'local0') } || 0;
96 skip "can't connect to syslog", 14 if $@ =~ /^no connection to syslog available/;
97 is( $@, '', "openlog() called with facility 'local0' and without option 'ndelay'" );
98 ok( $r, "openlog() should return true: '$r'" );
99
100 # openlog() with the option NDELAY
101 $r = eval { openlog('perl', 'ndelay', 'local0') } || 0;
102 skip "can't connect to syslog", 12 if $@ =~ /^no connection to syslog available/;
103 is( $@, '', "openlog() called with facility 'local0' with option 'ndelay'" );
104 ok( $r, "openlog() should return true: '$r'" );
105
106 # syslog() with level "info" (as a string), should pass
107 $r = eval { syslog('info', "$test_string by connecting to a $sock_type socket") } || 0;
108 is( $@, '', "syslog() called with level 'info'" );
109 ok( $r, "syslog() should return true: '$r'" );
110
111 # syslog() with level "info" (as a macro), should pass
112 $r = eval { syslog(LOG_INFO, "$test_string by connecting to a $sock_type socket") } || 0;
113 is( $@, '', "syslog() called with level 'info'" );
114 ok( $r, "syslog() should return true: '$r'" );
115
116 # syslog() with facility "kern" (as a string), should fail
117 $r = eval { syslog('kern', "$test_string by connecting to a $sock_type socket") } || 0;
118 like( $@, '/^syslog: invalid level/facility: kern/', "syslog() called with facility 'kern'" );
119 ok( !$r, "syslog() should return false: '$r'" );
120
121 # syslog() with facility "kern" (as a macro), should fail
122 $r = eval { syslog(LOG_KERN, "$test_string by connecting to a $sock_type socket") } || 0;
123 like( $@, '/^syslog: invalid level/facility: 0/', "syslog() called with facility 'kern'" );
124 ok( !$r, "syslog() should return false: '$r'" );
125
126 SKIP: {
127 skip "skipping closelog() tests for 'console'", 2 if $sock_type eq 'console';
128 # closelog()
129 $r = eval { closelog() } || 0;
130 is( $@, '', "closelog()" );
131 ok( $r, "closelog() should return true: '$r'" );
132 }
133 }
b75c8c73 134}
8168e71f 135
942974c1 136# setlogmask()
137{
138 my $oldmask = 0;
139
140 $oldmask = eval { setlogmask(0) } || 0;
141 is( $@, '', "setlogmask() called with a null mask" );
142 $r = eval { setlogmask(0) } || 0;
143 is( $@, '', "setlogmask() called with a null mask (second time)" );
144 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
145
146 for my $newmask ( LOG_ERR , LOG_CRIT|LOG_ERR|LOG_WARNING ) {
147 $r = eval { setlogmask($newmask) } || 0;
148 is( $@, '', "setlogmask() called with a new mask" );
149 is( $r, $oldmask, "setlogmask() must return the same mask as previous call");
150 $r = eval { setlogmask(0) } || 0;
151 is( $@, '', "setlogmask() called with a null mask" );
152 is( $r, $newmask, "setlogmask() must return the new mask");
153 setlogmask($oldmask);
154 }
155}