fastagi-test 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl
  2. use strict;
  3. use Socket;
  4. use Carp;
  5. use IO::Handle;
  6. my $port = 4573;
  7. $|=1;
  8. # Setup some variables
  9. my %AGI; my $tests = 0; my $fail = 0; my $pass = 0;
  10. sub checkresult {
  11. my ($res) = @_;
  12. my $retval;
  13. $tests++;
  14. chomp $res;
  15. if ($res =~ /^200/) {
  16. $res =~ /result=(-?\d+)/;
  17. if (!length($1)) {
  18. print STDERR "FAIL ($res)\n";
  19. $fail++;
  20. } else {
  21. print STDERR "PASS ($1)\n";
  22. $pass++;
  23. }
  24. } else {
  25. print STDERR "FAIL (unexpected result '$res')\n";
  26. $fail++;
  27. }
  28. }
  29. socket(SERVER, PF_INET, SOCK_STREAM, 0);
  30. setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
  31. bind(SERVER, sockaddr_in($port, INADDR_ANY)) || die("can't bind\n");
  32. listen(SERVER, SOMAXCONN);
  33. for(;;) {
  34. my $raddr = accept(CLIENT, SERVER);
  35. my ($s, $p) = sockaddr_in($raddr);
  36. CLIENT->autoflush(1);
  37. while(<CLIENT>) {
  38. chomp;
  39. last unless length($_);
  40. if (/^agi_(\w+)\:\s+(.*)$/) {
  41. $AGI{$1} = $2;
  42. }
  43. }
  44. print STDERR "AGI Environment Dump from $s:$p --\n";
  45. foreach my $i (sort keys %AGI) {
  46. print STDERR " -- $i = $AGI{$i}\n";
  47. }
  48. print STDERR "1. Testing 'sendfile'...";
  49. print CLIENT "STREAM FILE beep \"\"\n";
  50. my $result = <CLIENT>;
  51. &checkresult($result);
  52. print STDERR "2. Testing 'sendtext'...";
  53. print CLIENT "SEND TEXT \"hello world\"\n";
  54. my $result = <CLIENT>;
  55. &checkresult($result);
  56. print STDERR "3. Testing 'sendimage'...";
  57. print CLIENT "SEND IMAGE asterisk-image\n";
  58. my $result = <CLIENT>;
  59. &checkresult($result);
  60. print STDERR "4. Testing 'saynumber'...";
  61. print CLIENT "SAY NUMBER 192837465 \"\"\n";
  62. my $result = <CLIENT>;
  63. &checkresult($result);
  64. print STDERR "5. Testing 'waitdtmf'...";
  65. print CLIENT "WAIT FOR DIGIT 1000\n";
  66. my $result = <CLIENT>;
  67. &checkresult($result);
  68. print STDERR "6. Testing 'record'...";
  69. print CLIENT "RECORD FILE testagi gsm 1234 3000\n";
  70. my $result = <CLIENT>;
  71. &checkresult($result);
  72. print STDERR "6a. Testing 'record' playback...";
  73. print CLIENT "STREAM FILE testagi \"\"\n";
  74. my $result = <CLIENT>;
  75. &checkresult($result);
  76. close(CLIENT);
  77. print STDERR "================== Complete ======================\n";
  78. print STDERR "$tests tests completed, $pass passed, $fail failed\n";
  79. print STDERR "==================================================\n";
  80. }