check_udp.t 2.0 KB

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