This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Also IRIX wants setlogsock('stream').
[perl5.git] / ext / Sys / Syslog / t / syslog.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require Config; import Config;
7     if ($Config{'extensions'} !~ /\bSyslog\b/) {
8         print "1..0 # Skip: Sys::Syslog was not built\n";
9         exit 0;
10     }
11     if ($Config{'extensions'} !~ /\bSocket\b/) {
12         print "1..0 # Skip: Socket was not built\n";
13         exit 0;
14     }
15
16     require Socket;
17
18     # This code inspired by Sys::Syslog::connect():
19     require Sys::Hostname;
20     my ($host_uniq) = Sys::Hostname::hostname();
21     my ($host)      = $host_uniq =~ /([A-Za-z0-9_.-]+)/;
22
23     if (! defined Socket::inet_aton($host)) {
24         print "1..0 # Skip: Can't lookup $host\n";
25         exit 0;
26     }
27 }
28
29 BEGIN {
30   eval {require Sys::Syslog} or do {
31     if ($@ =~ /Your vendor has not/) {
32       print "1..0 # Skip: missing macros\n";
33       exit 0;
34     }
35   }
36 }
37
38 use Sys::Syslog qw(:DEFAULT setlogsock);
39
40 # Test this to 1 if your syslog accepts udp connections.
41 # Most don't (or at least shouldn't)
42 my $Test_Syslog_INET = 0;
43
44 my $test_string = "uid $< is testing perl $] syslog capabilities";
45
46 print "1..6\n";
47
48 if (Sys::Syslog::_PATH_LOG()) {
49     if (-e Sys::Syslog::_PATH_LOG()) {
50         if ($^O =~ /^(solaris|irix)$/) {
51             # we should check for stream support here, not for solaris/irix
52             print defined(eval { setlogsock('stream') }) ? "ok 1\n" : "not ok 1 # $!\n";
53         } else { 
54             print defined(eval { setlogsock('unix') }) ? "ok 1\n" : "not ok 1 # $!\n";
55         }
56         if (defined(eval { openlog('perl', 'ndelay', 'local0') })) {
57             print "ok 2\n";
58             print defined(eval { syslog('info', $test_string ) })
59                     ? "ok 3\n" : "not ok 3 # $!\n";
60         } else {
61             if ($@ =~ /no connection to syslog available/) {
62                 print "ok 2 # Skip: syslogd not running\n";
63             } else {
64                 print "not ok 2 # $@\n";
65             }
66             print "ok 3 # Skip: openlog failed\n";
67         }
68     } else {
69         for (1..3) {
70             print
71                 "ok $_ # Skip: file ",
72                 Sys::Syslog::_PATH_LOG(),
73                 " does not exist\n";
74         }
75     }
76 }
77 else {
78     for (1..3) { print "ok $_ # Skip: _PATH_LOG unavailable\n" }
79 }
80
81 if( $Test_Syslog_INET ) {
82     print defined(eval { setlogsock('inet') }) ? "ok 4\n" 
83                                                : "not ok 4\n";
84     print defined(eval { openlog('perl', 'ndelay', 'local0') }) ? "ok 5\n" 
85                                                                 : "not ok 5 # $!\n";
86     print defined(eval { syslog('info', $test_string ) }) ? "ok 6\n" 
87                                                    : "not ok 6 # $!\n";
88 }
89 else {
90     print "ok $_ # Skip: assuming syslog doesn't accept inet connections\n" 
91       foreach (4..6);
92 }