check_udp.t 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. alarm(120); # make sure tests don't hang
  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. my $nc;
  25. if(system("which netcat >/dev/null 2>&1") == 0) {
  26. $nc = 'netcat -w 3 -l -u -p 3333';
  27. }
  28. elsif(system("which nc >/dev/null 2>&1") == 0) {
  29. $nc = 'nc -w 3 -l -u -4 localhost 3333';
  30. }
  31. SKIP: {
  32. skip "No netcat available", 6 unless $nc;
  33. open (NC, "echo 'barbar' | $nc |");
  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;
  39. # Start up a udp server listening on port 3333, quit after 3 seconds
  40. # Otherwise will hang at close
  41. my $pid = open(NC, "$nc </dev/null |");
  42. sleep 1; # Allow nc to startup
  43. my $start = time;
  44. $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" );
  45. my $duration = time - $start;
  46. cmp_ok( $res->return_code, '==', '2', "Hung waiting for response");
  47. like ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message");
  48. like ( $duration, '/^[56]$/', "Timeout after 5 (possibly 6) seconds");
  49. my $read_nc = <NC>;
  50. close NC;
  51. cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" );
  52. }
  53. alarm(0); # disable alarm