annotate mail_proxy_protocol.t @ 1659:d1c4059e1e72

Tests: smtp tests with proxy protocol and realip.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 04 Mar 2021 14:45:40 +0300
parents
children 068c30e9d2c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1659
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for mail proxy module, PROXY protocol with realip.
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 use MIME::Base64;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use lib 'lib';
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21 use Test::Nginx::SMTP;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23 ###############################################################################
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 select STDERR; $| = 1;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26 select STDOUT; $| = 1;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28 local $SIG{PIPE} = 'IGNORE';
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30 my $t = Test::Nginx->new()->has(qw/mail smtp http rewrite/)
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 ->write_file_expand('nginx.conf', <<'EOF');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 %%TEST_GLOBALS%%
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35 daemon off;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 events {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 mail {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41 proxy_pass_error_message on;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 proxy_smtp_auth on;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 auth_http http://127.0.0.1:8080/mail/auth;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 smtp_auth login plain;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 server {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 listen 127.0.0.1:8025 proxy_protocol;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 protocol smtp;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 auth_http_header X-Type proxy;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 server {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 listen 127.0.0.1:8027 proxy_protocol;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 protocol smtp;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 set_real_ip_from 127.0.0.1/32;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 auth_http_header X-Type realip;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 http {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 %%TEST_GLOBALS_HTTP%%
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 server {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 listen 127.0.0.1:8080;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 server_name localhost;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 location = /mail/auth {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 set $reply ERROR;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 set $test $http_x_type:$http_client_ip:$http_proxy_protocol_addr;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 if ($test = proxy:127.0.0.1:192.0.2.1) {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 set $reply OK;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 if ($test = realip:192.0.2.1:192.0.2.1) {
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 set $reply OK;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 add_header Auth-Status $reply;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 add_header Auth-Server 127.0.0.1;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 add_header Auth-Port %%PORT_8026%%;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 add_header Auth-Wait 1;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85 return 204;
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 }
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90 EOF
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 $t->run_daemon(\&Test::Nginx::SMTP::smtp_test_daemon);
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 $t->try_run('no proxy_protocol')->plan(6);
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 $t->waitforsocket('127.0.0.1:' . port(8026));
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 ###############################################################################
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 # connection with PROXY protocol
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101 my $s = Test::Nginx::SMTP->new(PeerAddr => '127.0.0.1:' . port(8025));
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 $s->send('PROXY TCP4 192.0.2.1 192.0.2.2 123 5678');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
103 $s->check(qr/^220 /, "greeting with proxy_protocol");
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105 $s->send('EHLO example.com');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
106 $s->check(qr/^250 /, "ehlo with proxy_protocol");
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108 $s->send('AUTH PLAIN ' . encode_base64("\0test\@example.com\0secret", ''));
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
109 $s->authok('auth with proxy_protocol');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111 # connection with PROXY protocol and set_realip_from
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
112
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113 $s = Test::Nginx::SMTP->new(PeerAddr => '127.0.0.1:' . port(8027));
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
114
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
115 $s->send('PROXY TCP4 192.0.2.1 192.0.2.2 123 5678');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
116 $s->check(qr/^220 /, "greeting with proxy_protocol and realip");
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
117
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
118 $s->send('EHLO example.com');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
119 $s->check(qr/^250 /, "ehlo with proxy_protocol and realip");
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
120
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
121 $s->send('AUTH PLAIN ' . encode_base64("\0test\@example.com\0secret", ''));
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
122 $s->authok('auth with proxy_protocol and realip');
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
123
d1c4059e1e72 Tests: smtp tests with proxy protocol and realip.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124 ###############################################################################