annotate proxy_protocol.t @ 952:e9064d691790

Tests: converted tests to run in parallel.
author Andrey Zelenkov <zelenkov@nginx.com>
date Tue, 21 Jun 2016 16:39:13 +0300
parents adf5671391ac
children 882267679006
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
381
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for haproxy protocol.
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 use Socket qw/ CRLF /;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use lib 'lib';
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 ###############################################################################
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 select STDERR; $| = 1;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 select STDOUT; $| = 1;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 my $t = Test::Nginx->new()->has(qw/http access ipv6 realip/);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 $t->write_file_expand('nginx.conf', <<'EOF')->plan(18);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 %%TEST_GLOBALS%%
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 daemon off;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35 events {
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38 http {
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 %%TEST_GLOBALS_HTTP%%
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41 log_format pp '$remote_addr $request';
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 server {
952
e9064d691790 Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 908
diff changeset
44 listen 127.0.0.1:%%PORT_0%% proxy_protocol;
381
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 server_name localhost;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 set_real_ip_from 127.0.0.1/32;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 add_header X-IP $remote_addr;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 add_header X-PP $proxy_protocol_addr;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 location /pp {
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52 real_ip_header proxy_protocol;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 error_page 404 =200 /t1;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 access_log %%TESTDIR%%/pp.log pp;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 location /pp_4 {
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 deny 192.0.2.1/32;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 location /pp_6 {
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 deny 2001:DB8::1/128;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64 }
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 EOF
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68 $t->write_file('t1', 'SEE-THIS');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 $t->run();
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 ###############################################################################
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 my $tcp4 = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 my $tcp6 = 'PROXY TCP6 2001:Db8::1 2001:Db8::2 1234 5678' . CRLF;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 my $unk1 = 'PROXY UNKNOWN' . CRLF;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 my $unk2 = 'PROXY UNKNOWN 1 2 3 4 5 6' . CRLF;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 my $r;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 # no realip, just PROXY header parsing
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 $r = pp_get('/t1', $tcp4);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 like($r, qr/SEE-THIS/, 'tcp4 request');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 like($r, qr/X-PP: 192.0.2.1/, 'tcp4 proxy');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 unlike($r, qr/X-IP: 192.0.2.1/, 'tcp4 client');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 $r = pp_get('/t1', $tcp6);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87 like($r, qr/SEE-THIS/, 'tcp6 request');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 like($r, qr/X-PP: 2001:DB8::1/i, 'tcp6 proxy');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 unlike($r, qr/X-IP: 2001:DB8::1/i, 'tcp6 client');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91 like(pp_get('/t1', $unk1), qr/SEE-THIS/, 'unknown request 1');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 like(pp_get('/t1', $unk2), qr/SEE-THIS/, 'unknown request 2');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94 # realip
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96 $r = pp_get('/pp', $tcp4);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 like($r, qr/SEE-THIS/, 'tcp4 request realip');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98 like($r, qr/X-PP: 192.0.2.1/, 'tcp4 proxy realip');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 like($r, qr/X-IP: 192.0.2.1/, 'tcp4 client realip');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101 $r = pp_get('/pp', $tcp6);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 like($r, qr/SEE-THIS/, 'tcp6 request realip');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
103 like($r, qr/X-PP: 2001:DB8::1/i, 'tcp6 proxy realip');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104 like($r, qr/X-IP: 2001:DB8::1/i, 'tcp6 client realip');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
106 # access
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108 $r = pp_get('/pp_4', $tcp4);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
109 like($r, qr/403 Forbidden/, 'tcp4 access');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111 $r = pp_get('/pp_6', $tcp6);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
112 like($r, qr/403 Forbidden/, 'tcp6 access');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
114 # client address in access.log
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
115
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
116 $t->stop();
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
117
908
adf5671391ac Tests: use read_file subroutine in several tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 527
diff changeset
118 my $log = $t->read_file('pp.log');
381
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
119 like($log, qr!^192\.0\.2\.1 GET /pp_4!m, 'tcp4 access log');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
120 like($log, qr!^2001:DB8::1 GET /pp_6!mi, 'tcp6 access log');
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
121
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
122 ###############################################################################
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
123
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124 sub pp_get {
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
125 my ($url, $proxy) = @_;
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
126 return http($proxy . <<EOF);
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
127 GET $url HTTP/1.0
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
128 Host: localhost
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
129
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
130 EOF
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
131 }
527
a36290485719 Tests: whitespace fix.
Sergey Kandaurov <pluknet@nginx.com>
parents: 397
diff changeset
132
381
056d0ad0ef03 Tests: proxy protocol tests added.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
133 ###############################################################################