POP3.pm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package Test::Nginx::POP3;
  2. # (C) Maxim Dounin
  3. # Module for nginx pop3 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:8110",
  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. # XXX
  36. next if m/^\d\d\d-/;
  37. last;
  38. }
  39. alarm(0);
  40. };
  41. alarm(0);
  42. if ($@) {
  43. return undef;
  44. }
  45. return $_;
  46. }
  47. sub check {
  48. my ($self, $regex, $name) = @_;
  49. Test::More->builder->like($self->read(), $regex, $name);
  50. }
  51. sub ok {
  52. my $self = shift;
  53. Test::More->builder->like($self->read(), qr/^\+OK/, @_);
  54. }
  55. ###############################################################################
  56. sub pop3_test_daemon {
  57. my $server = IO::Socket::INET->new(
  58. Proto => 'tcp',
  59. LocalAddr => '127.0.0.1:8111',
  60. Listen => 5,
  61. Reuse => 1
  62. )
  63. or die "Can't create listening socket: $!\n";
  64. while (my $client = $server->accept()) {
  65. $client->autoflush(1);
  66. print $client "+OK fake pop3 server ready" . CRLF;
  67. while (<$client>) {
  68. if (/^quit/i) {
  69. print $client '+OK quit ok' . CRLF;
  70. } elsif (/^user test\@example.com/i) {
  71. print $client '+OK user ok' . CRLF;
  72. } elsif (/^pass secret/i) {
  73. print $client '+OK pass ok' . CRLF;
  74. } else {
  75. print $client "-ERR unknown command" . CRLF;
  76. }
  77. }
  78. close $client;
  79. }
  80. }
  81. ###############################################################################
  82. 1;
  83. ###############################################################################