No Socket, no Syslog.
[p5sagit/p5-mst-13.2.git] / ext / Sys / Syslog / 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     # This code inspired by Sys::Syslog::connect():
17     require Sys::Hostname;
18     my ($host_uniq) = Sys::Hostname::hostname();
19     my ($host)      = $host_uniq =~ /([A-Za-z0-9_.-]+)/;
20
21     if (! defined Socket::inet_aton($host)) {
22         print "1..0 # Skip: Can't lookup $host\n";
23         exit 0;
24     }
25 }
26
27 BEGIN {
28   eval {require Sys::Syslog} or do {
29     if ($@ =~ /Your vendor has not/) {
30       print "1..0 # Skip: missing macros\n";
31       exit 0;
32     }
33   }
34 }
35
36 use Sys::Syslog qw(:DEFAULT setlogsock);
37
38 # Test this to 1 if your syslog accepts udp connections.
39 # Most don't (or at least shouldn't)
40 my $Test_Syslog_INET = 0;
41
42 my $test_string = "uid $< is testing perl $] syslog capabilities";
43
44 print "1..6\n";
45
46 if (Sys::Syslog::_PATH_LOG()) {
47     if (-e Sys::Syslog::_PATH_LOG()) {
48         print defined(eval { setlogsock('unix') }) ? "ok 1\n" : "not ok 1 # $!\n";
49         print defined(eval { openlog('perl', 'ndelay', 'local0') }) ? "ok 2\n" : "not ok 2 # $!\n";
50         print defined(eval { syslog('info', $test_string ) }) ? "ok 3\n" : "not ok 3 # $!\n";
51     }
52     else {
53         for (1..3) {
54             print
55                 "ok $_ # Skip: file ",
56                 Sys::Syslog::_PATH_LOG(),
57                 " does not exist\n";
58         }
59     }
60 }
61 else {
62     for (1..3) { print "ok $_ # Skip: _PATH_LOG unavailable\n" }
63 }
64
65 if( $Test_Syslog_INET ) {
66     print defined(eval { setlogsock('inet') }) ? "ok 4\n" 
67                                                : "not ok 4\n";
68     print defined(eval { openlog('perl', 'ndelay', 'local0') }) ? "ok 5\n" 
69                                                                 : "not ok 5 # $!\n";
70     print defined(eval { syslog('info', $test_string ) }) ? "ok 6\n" 
71                                                    : "not ok 6 # $!\n";
72 }
73 else {
74     print "ok $_ # Skip: assuming syslog doesn't accept inet connections\n" 
75       foreach (4..6);
76 }