From 8c3e7428ae21999e813b288c3d55d3870c9ef258 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Fri, 24 Mar 2006 16:12:05 +0000 Subject: udp checks require and send and receive option. Tests updated so if nc is available, will check send and receive working correctly git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1353 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 5c287b7..d8fc26e 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c @@ -198,13 +198,17 @@ main (int argc, char **argv) if(flags & FLAG_VERBOSE) { printf("Using service %s\n", SERVICE); - printf("Port: %d\n", PORT); + printf("Port: %d\n", server_port); printf("flags: 0x%x\n", (int)flags); } if(EXPECT && !server_expect_count) server_expect_count++; + if(PROTOCOL==IPPROTO_UDP && !(server_expect_count && server_send)){ + usage(_("With UDP checks, a send/expect string must be specified.")); + } + /* set up the timer */ signal (SIGALRM, socket_timeout_alarm_handler); alarm (socket_timeout); diff --git a/plugins/t/check_udp.t b/plugins/t/check_udp.t index c80e08a..c9228ad 100644 --- a/plugins/t/check_udp.t +++ b/plugins/t/check_udp.t @@ -6,28 +6,51 @@ # use strict; -use Test; +use Test::More; use NPTest; -use vars qw($tests); -BEGIN {$tests = 3; plan tests => $tests} #TODO# Update to 4 when the commented out test is fixed +my $res; + +plan tests => 14; + +$res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333" ); +cmp_ok( $res->return_code, '==', 3, "Need send/expect string"); +like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); + +$res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s send" ); +cmp_ok( $res->return_code, '==', 3, "Need expect string"); +like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); + +$res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -e expect" ); +cmp_ok( $res->return_code, '==', 3, "Need send string"); +like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK"); + +$res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s foo -e bar" ); +cmp_ok( $res->return_code, '==', 2, "Errors correctly because no udp service running" ); +like ( $res->output, '/No data received from host/', "Output OK"); + +SKIP: { + skip "No netcat available", 6 unless (system("which nc > /dev/null") == 0); + open (NC, "echo 'barbar' | nc -l -p 3333 -u |"); + sleep 1; + $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s '' -e barbar -4" ); + cmp_ok( $res->return_code, '==', 0, "Got barbar response back" ); + like ( $res->output, '/\[barbar\]/', "Output OK"); + close NC; + + my $pid = open(NC, "nc -l -p 3333 -u |"); # Start up a udp server listening on port 3333 + alarm(7); + sleep 1; + $SIG{ALRM} = sub { kill 'INT', $pid }; + my $start = time; + $res = NPTest->testCmd( "./check_udp2 -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" ); + my $duration = time - $start; + cmp_ok( $res->return_code, '==', '2', "Hung waiting for response"); + like ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message"); + cmp_ok( $duration, '==', 5, "Timeout exactly right"); + my $read_nc = ; + # nc gets killed here - I think expects a linefeed from stdin, so doesn't exit itself + close NC; + cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" ); +} -my $host_udp_time = getTestParameter( "host_udp_time", "NP_HOST_UDP_TIME", "localhost", - "A host providing the UDP Time Service" ); - -my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", - "The hostname of system not responsive to network requests" ); - -my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", - "An invalid (not known to DNS) hostname" ); - -my $successOutput = '/^Connection accepted on port [0-9]+ - [0-9]+ second response time$/'; - -my $t; - -$t += checkCmd( "./check_udp -H $host_udp_time -p 37 -wt 300 -ct 600", 0, $successOutput ); -$t += checkCmd( "./check_udp $host_nonresponsive -p 37 -wt 0 -ct 0 -to 1", 2 ); -#TODO# $t += checkCmd( "./check_udp $hostname_invalid -p 37 -wt 0 -ct 0 -to 1", 2 ); # Currently returns 0 (ie success) - -exit(0) if defined($Test::Harness::VERSION); -exit($tests - $t); -- cgit v0.10-9-g596f