comparison smtp.t @ 5:4d75bdb05ecf

Tests: some generic code and ssi tests. Move generic code to _common.pm and add test for ssi big includes.
author Maxim Dounin <mdounin@mdounin.ru>
date Sun, 07 Sep 2008 05:00:28 +0400
parents 6c41dbb2954f
children 9eb509695651
comparison
equal deleted inserted replaced
4:6c41dbb2954f 5:4d75bdb05ecf
9 use warnings; 9 use warnings;
10 use strict; 10 use strict;
11 11
12 use Test::More tests => 28; 12 use Test::More tests => 28;
13 13
14 use File::Temp qw/ tempdir /;
15 use IO::Socket;
16 use MIME::Base64; 14 use MIME::Base64;
17 15
18 use constant CRLF => "\x0D\x0A"; 16 use _common;
17
18 ###############################################################################
19 19
20 select STDERR; $| = 1; 20 select STDERR; $| = 1;
21 select STDOUT; $| = 1; 21 select STDOUT; $| = 1;
22 22
23 ############################################################################### 23 start_nginx('smtp.conf');
24
25 # Create temp directory and run nginx instance.
26
27 my $tempdir = tempdir('nginx-test-XXXXXXXXXX', TMPDIR => 1, CLEANUP => 1)
28 or die "Can't create temp directory: $!\n";
29
30 my $pid = fork();
31 die "Unable to fork(): $!\n" unless defined $pid;
32
33 if ($pid == 0) {
34 exec('../nginx/objs/nginx', '-c', 'smtp.conf', '-g',
35 "pid $tempdir/nginx.pid; error_log $tempdir/nginx-error.log info;")
36 or die "Unable to exec(): $!\n";
37 print "# child after exec - not reached\n";
38 }
39
40 END {
41 # terminate nginx by SIGTERM
42 kill 15, $pid;
43 wait;
44 }
45
46 # Give nginx some time to start.
47
48 sleep 1;
49 24
50 ############################################################################### 25 ###############################################################################
51 26
52 my $s = smtp_connect(); 27 my $s = smtp_connect();
53 smtp_check(qr/^220 /, "greeting"); 28 smtp_check(qr/^220 /, "greeting");
169 smtp_send('HELO example.com'); 144 smtp_send('HELO example.com');
170 smtp_check(qr/^5.. /, "command before greeting - session must be rejected"); 145 smtp_check(qr/^5.. /, "command before greeting - session must be rejected");
171 ok($s->eof(), "session have to be closed"); 146 ok($s->eof(), "session have to be closed");
172 147
173 ############################################################################### 148 ###############################################################################
174
175 sub log_out {
176 my ($msg) = @_;
177 $msg =~ s/^/# >> /gm;
178 $msg .= "\n" unless $msg =~ /\n\Z/;
179 print $msg;
180 }
181
182 sub log_in {
183 my ($msg) = @_;
184 $msg =~ s/\x0d/\\x0d/gm;
185 $msg =~ s/\x0a/\\x0a/gm;
186 print '# << ' . $msg . "\n";
187 }
188
189 sub smtp_connect {
190 my $s = IO::Socket::INET->new(
191 Proto => "tcp",
192 PeerAddr => "localhost",
193 PeerPort => 10025,
194 @_
195 )
196 or die "Can't connect to nginx: $!\n";
197
198 $s->autoflush(1);
199
200 return $s;
201 }
202
203 sub smtp_send {
204 my ($cmd) = @_;
205 log_out($cmd);
206 $s->print($cmd . CRLF);
207 }
208
209 sub smtp_read {
210 my ($regex, $name) = @_;
211 eval {
212 alarm(2);
213 local $SIG{ALRM} = sub { die "alarm\n" };
214 while (<$s>) {
215 log_in($_);
216 next if m/^\d\d\d-/;
217 last;
218 }
219 alarm(0);
220 };
221 alarm(0);
222 if ($@) {
223 return undef;
224 }
225 return $_;
226 }
227
228 sub smtp_check {
229 my ($regex, $name) = @_;
230 like(smtp_read(), $regex, $name);
231 }
232
233 sub smtp_ok {
234 smtp_check(qr/^2\d\d /, @_);
235 }
236
237 ###############################################################################