check_udp.t 2.0 KB

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