SMTP.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package Test::Nginx::SMTP;
  2. # (C) Maxim Dounin
  3. # Module for nginx smtp tests.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More qw//;
  8. use IO::Socket;
  9. use Socket qw/ CRLF /;
  10. use Test::Nginx;
  11. use base qw/ IO::Socket::INET /;
  12. sub new {
  13. my $class = shift;
  14. my $self = return $class->SUPER::new(
  15. Proto => "tcp",
  16. PeerAddr => "127.0.0.1:8025",
  17. @_
  18. )
  19. or die "Can't connect to nginx: $!\n";
  20. $self->autoflush(1);
  21. return $self;
  22. }
  23. sub send {
  24. my ($self, $cmd) = @_;
  25. log_out($cmd);
  26. $self->print($cmd . CRLF);
  27. }
  28. sub read {
  29. my ($self) = @_;
  30. eval {
  31. local $SIG{ALRM} = sub { die "alarm\n" };
  32. alarm(2);
  33. while (<$self>) {
  34. log_in($_);
  35. next if m/^\d\d\d-/;
  36. last;
  37. }
  38. alarm(0);
  39. };
  40. alarm(0);
  41. if ($@) {
  42. return undef;
  43. }
  44. return $_;
  45. }
  46. sub check {
  47. my ($self, $regex, $name) = @_;
  48. Test::More->builder->like($self->read(), $regex, $name);
  49. }
  50. sub ok {
  51. my $self = shift;
  52. Test::More->builder->like($self->read(), qr/^2\d\d /, @_);
  53. }
  54. sub authok {
  55. my $self = shift;
  56. Test::More->builder->like($self->read(), qr/^235 /, @_);
  57. }
  58. ###############################################################################
  59. sub smtp_test_daemon {
  60. my $server = IO::Socket::INET->new(
  61. Proto => 'tcp',
  62. LocalAddr => '127.0.0.1:8026',
  63. Listen => 5,
  64. Reuse => 1
  65. )
  66. or die "Can't create listening socket: $!\n";
  67. while (my $client = $server->accept()) {
  68. $client->autoflush(1);
  69. print $client "220 fake esmtp server ready" . CRLF;
  70. while (<$client>) {
  71. Test::Nginx::log_core('||', $_);
  72. if (/^quit/i) {
  73. print $client '221 quit ok' . CRLF;
  74. } elsif (/^(ehlo|helo)/i) {
  75. print $client '250 hello ok' . CRLF;
  76. } elsif (/^rset/i) {
  77. print $client '250 rset ok' . CRLF;
  78. } elsif (/^mail from:[^@]+$/i) {
  79. print $client '500 mail from error' . CRLF;
  80. } elsif (/^mail from:/i) {
  81. print $client '250 mail from ok' . CRLF;
  82. } elsif (/^rcpt to:[^@]+$/i) {
  83. print $client '500 rcpt to error' . CRLF;
  84. } elsif (/^rcpt to:/i) {
  85. print $client '250 rcpt to ok' . CRLF;
  86. } elsif (/^xclient/i) {
  87. print $client '220 xclient ok' . CRLF;
  88. } else {
  89. print $client "500 unknown command" . CRLF;
  90. }
  91. }
  92. close $client;
  93. }
  94. }
  95. ###############################################################################
  96. 1;
  97. ###############################################################################