1 #! /usr/bin/perl -w -I ..
2 #
3 # UDP Connection Based Tests via check_udp
4 #
5 #
7 use strict;
8 use Test::More;
9 use NPTest;
11 my $res;
13 plan tests => 14;
15 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333" );
16 cmp_ok( $res->return_code, '==', 3, "Need send/expect string");
17 like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
19 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s send" );
20 cmp_ok( $res->return_code, '==', 3, "Need expect string");
21 like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
23 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -e expect" );
24 cmp_ok( $res->return_code, '==', 3, "Need send string");
25 like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
27 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foo -e bar" );
28 cmp_ok( $res->return_code, '==', 2, "Errors correctly because no udp service running" );
29 like ( $res->output, '/No data received from host/', "Output OK");
31 SKIP: {
32 skip "No netcat available", 6 unless (system("which nc > /dev/null") == 0);
33 open (NC, "echo 'barbar' | nc -l -p 3333 -u |");
34 sleep 1;
35 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s '' -e barbar -4" );
36 cmp_ok( $res->return_code, '==', 0, "Got barbar response back" );
37 like ( $res->output, '/\[barbar\]/', "Output OK");
38 close NC;
40 # Start up a udp server listening on port 3333, quit after 3 seconds
41 # Otherwise will hang at close
42 my $pid = open(NC, "nc -l -p 3333 -u -w 3 </dev/null |");
43 sleep 1; # Allow nc to startup
45 my $start = time;
46 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" );
47 my $duration = time - $start;
48 cmp_ok( $res->return_code, '==', '2', "Hung waiting for response");
49 like ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message");
50 like ( $duration, '/^[56]$/', "Timeout after 5 (possibly 6) seconds");
51 my $read_nc = <NC>;
52 close NC;
53 cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" );
54 }