check_udp.t 2.4 KB

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