comparison mail_auth_wait.t @ 1977:913d96252b7a

Tests: added test for WAIT mail auth response.
author Rob Mueller <robm@fastmailteam.com>
date Wed, 15 May 2024 21:54:16 +0300
parents
children
comparison
equal deleted inserted replaced
1976:4e79bd25642f 1977:913d96252b7a
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for nginx mail module for WAIT auth response.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
15
16 use lib 'lib';
17 use Test::Nginx;
18 use Test::Nginx::IMAP;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 local $SIG{PIPE} = 'IGNORE';
26
27 my $t = Test::Nginx->new()->has(qw/mail imap http map rewrite/)
28 ->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 mail {
38 proxy_pass_error_message on;
39 proxy_timeout 15s;
40 auth_http http://127.0.0.1:8080/mail/auth;
41
42 server {
43 listen 127.0.0.1:8143;
44 protocol imap;
45 imap_auth plain cram-md5 external;
46 }
47 }
48
49 http {
50 %%TEST_GLOBALS_HTTP%%
51
52 map $upstream_http_count $reply {
53 # Each wait is 1 second, so wait 4 times, which should get us
54 # to after the sleep(3), but then after another sleep(2) we
55 # should have completed with a success
56
57 1 WAIT;
58 2 WAIT;
59 3 WAIT;
60 4 WAIT;
61 5 OK;
62
63 default ERROR;
64 }
65
66 log_format test "reply=$reply";
67
68 server {
69 listen 127.0.0.1:8080;
70 server_name localhost;
71
72 access_log %%TESTDIR%%/auth.log test;
73
74 location = /mail/auth {
75 add_header Auth-Status $reply;
76 add_header Auth-Server 127.0.0.1;
77 add_header Auth-Port %%PORT_8144%%;
78 add_header Auth-Pass "";
79 add_header Auth-Wait 1;
80 proxy_pass http://127.0.0.1:8081;
81 }
82 }
83 }
84
85 EOF
86
87 $t->run_daemon(\&Test::Nginx::IMAP::imap_test_daemon);
88 $t->run_daemon(\&http_daemon);
89 $t->run()->plan(4);
90
91 $t->waitforsocket('127.0.0.1:' . port(8144));
92 $t->waitforsocket('127.0.0.1:' . port(8081));
93
94 ###############################################################################
95
96 # WAIT response
97
98 my $s = Test::Nginx::IMAP->new();
99 $s->read();
100 $s->send('a01 LOGIN test@example.com wait');
101
102 sleep(3);
103
104 my $f = $t->read_file('auth.log');
105 my @waits = $f =~ /^reply=WAIT/mg;
106 ok(@waits >= 2, "found multiple WAIT responses in log");
107
108 my @ready = $s->can_read(0.1);
109 is(scalar @ready, 0, "nothing to read while waiting");
110
111 sleep(2);
112
113 @ready = $s->can_read(0);
114 is(scalar @ready, 1, "ready for reading");
115
116 $s->ok('login success after waiting');
117
118 ###############################################################################
119
120 sub http_daemon {
121 my $server = IO::Socket::INET->new(
122 Proto => 'tcp',
123 LocalHost => '127.0.0.1',
124 LocalPort => port(8081),
125 Listen => 5,
126 Reuse => 1
127 )
128 or die "Can't create listening socket: $!\n";
129
130 local $SIG{PIPE} = 'IGNORE';
131
132 my $count = 0;
133
134 while (my $client = $server->accept()) {
135 $client->autoflush(1);
136
137 my $headers = '';
138 my $uri = '';
139
140 while (<$client>) {
141 $headers .= $_;
142 last if (/^\x0d?\x0a?$/);
143 }
144
145 next if $headers eq '';
146 $count++;
147
148 Test::Nginx::log_core('||', "response, $count");
149 print $client <<EOF;
150 HTTP/1.1 204 No content
151 Count: $count
152 Connection: close
153
154 EOF
155
156 } continue {
157 close $client;
158 }
159 }
160
161 ###############################################################################