Subject: | setlogsock() doesn't return undef on failure |
Hi,
contrary to the documentation, since 0.28 setlogsock() returns true even
when the requested socket type could not be used.
Please see the attached proposed patch with test cases and a fix.
Thanks for your work on Sys-Syslog,
--
Niko Tyni
ntyni@debian.org
Subject: | 0001-Make-setlogsock-return-value-meaningful-on-failures-.patch |
From 4e79d8a2cb732dbe28f5759b0f3e896fd4fff3f2 Mon Sep 17 00:00:00 2001
From: Niko Tyni <ntyni@debian.org>
Date: Wed, 3 Aug 2011 19:11:43 +0300
Subject: [PATCH] Make setlogsock() return value meaningful on failures again
Changes to setlogsock() in 0.28 made the function return
true even when the requested socket type could not be used.
This includes cases where _PATH_LOG doesn't exist, the requested stream
path can't be opened or the requested network service (syslog/udp,
syslog/tcp, syslog-ng/tcp) can't be resolved.
Restore the return value behaviour to the documented one and include
test cases for the easily simulated or detected situations.
The syslog tcp services are not defined by default on at least Debian
and FreeBSD, so the relevant tests should give moderate coverage.
---
Syslog.pm | 4 +++-
t/syslog.t | 13 +++++++++++++
2 files changed, 16 insertions(+), 1 deletions(-)
diff --git a/Syslog.pm b/Syslog.pm
index a68f817..4fcedd7 100644
--- a/Syslog.pm
+++ b/Syslog.pm
@@ -310,10 +310,12 @@ sub setlogsock {
$transmit_ok = 0;
@fallbackMethods = ();
@connectMethods = @defaultMethods;
+ my $found = 0;
for my $sock_type (@sock_types) {
if ( $mechanism{$sock_type}{check}->() ) {
unshift @connectMethods, $sock_type;
+ $found = 1;
}
else {
warnings::warnif "setlogsock(): type='$sock_type': "
@@ -321,7 +323,7 @@ sub setlogsock {
}
}
- return 1;
+ return $found;
}
sub syslog {
diff --git a/t/syslog.t b/t/syslog.t
index c2bc7ff..2eabb00 100644
--- a/t/syslog.t
+++ b/t/syslog.t
@@ -283,3 +283,16 @@ BEGIN { $tests += 3 + 4 * 3 }
setlogmask($oldmask);
}
}
+
+BEGIN { $tests += 4 }
+eval { $r = setlogsock("stream", "foo/bar") };
+ok( !$r, "setlogsock failed correctly with a nonexistent stream path");
+is( $@, '', "setlogsock didn't croak");
+
+SKIP: {
+ my $service = getservbyname("syslog", "tcp") || getservbyname("syslog-ng", "tcp");
+ skip "can't test setlogsock() tcp failure", 2 if $service;
+ eval { $r = setlogsock("tcp") };
+ ok( !$r, "setlogsock failed correctly when tcp services can't be resolved");
+ is( $@, '', "setlogsock didn't croak");
+}
--
1.7.5.4