annotate realip.t @ 1236:93f749c1d5c5

Tests: fixed parallel tests execution with UDP. Previously, when checking ports availability, a UDP socket was always created first, then a TCP socket was created. On success, one of UDP and TCP sockets was closed (depending on the "udp" option) and the second one was used to busy this port in other scripts. This lead to the following problem: in an attempt to reopen a UDP socket used in a given testing script it could be stolen by another script as part of checking ports availability. To solve this problem, UDP and TCP ports were split into two non-overlapping ranges: TCP ports are only used in the range 8000-8499, and UDP ports - in the range 8500-8999. In addition, the order of creating sockets in UDP tests has been reversed: now a TCP socket used as a lock precedes a UDP socket.
author Andrey Zelenkov <zelenkov@nginx.com>
date Thu, 26 Oct 2017 18:00:21 +0300
parents 882267679006
children c0075a0a2ba9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
1 #!/usr/bin/perl
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
2
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
3 # (C) Maxim Dounin
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
4
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
5 # Tests for nginx realip module.
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
6
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
7 ###############################################################################
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
8
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
9 use warnings;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
10 use strict;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
11
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
12 use Test::More;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
13
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
15
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
16 use lib 'lib';
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
17 use Test::Nginx;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
18
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
19 ###############################################################################
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
20
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
21 select STDERR; $| = 1;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
22 select STDOUT; $| = 1;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
23
217
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
24 my $t = Test::Nginx->new()->has(qw/http realip/);
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
25
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
26 $t->write_file_expand('nginx.conf', <<'EOF');
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
27
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
28 %%TEST_GLOBALS%%
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
29
249
6a0d934950bc Tests: remove extra spaces in "daemon off".
Maxim Dounin <mdounin@mdounin.ru>
parents: 247
diff changeset
30 daemon off;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
31
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
32 events {
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
33 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
34
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
35 http {
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
36 %%TEST_GLOBALS_HTTP%%
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
37
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
38 add_header X-IP $remote_addr;
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
39 set_real_ip_from 127.0.0.1/32;
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
40 set_real_ip_from 10.0.1.0/24;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
41
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
42 server {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
43 listen 127.0.0.1:8080;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
44 server_name localhost;
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
45
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
46 location / { }
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
47 location /custom {
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
48 real_ip_header X-Real-IP-Custom;
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
49 }
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
50
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
51 location /1 {
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
52 real_ip_header X-Forwarded-For;
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
53 real_ip_recursive off;
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
54 }
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
55
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
56 location /2 {
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
57 real_ip_header X-Forwarded-For;
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
58 real_ip_recursive on;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
62
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
63 EOF
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
65 $t->write_file('index.html', '');
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
66 $t->write_file('custom', '');
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67 $t->write_file('1', '');
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
68 $t->write_file('2', '');
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 $t->run();
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
70
217
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
71 plan(skip_all => 'no 127.0.0.1 on host')
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
72 if http_get('/') !~ /X-IP: 127.0.0.1/m;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
74 $t->plan(7);
217
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
75
ea574aae77cb Tests: unbreak geo and realip tests in jails.
Maxim Dounin <mdounin@mdounin.ru>
parents: 216
diff changeset
76 ###############################################################################
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
77
690
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
78 like(http(<<EOF), qr/^X-IP: 192.0.2.1/m, 'realip');
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
79 GET / HTTP/1.0
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
80 Host: localhost
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
81 X-Real-IP: 192.0.2.1
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
82
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
83 EOF
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
84
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
85 like(http(<<EOF), qr/^X-IP: 192.0.2.1/m, 'realip custom');
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
86 GET /custom HTTP/1.0
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
87 Host: localhost
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
88 X-Real-IP-Custom: 192.0.2.1
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
89
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
90 EOF
a894f041244e Tests: corrected simple realip tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 590
diff changeset
91
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
92 like(http_xff('/1', '10.0.0.1, 192.0.2.1'), qr/^X-IP: 192.0.2.1/m,
590
dc2f8aac0553 Tests: whitespace fixes.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 317
diff changeset
93 'realip multi');
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
94 like(http_xff('/1', '192.0.2.1, 10.0.1.1, 127.0.0.1'),
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
95 qr/^X-IP: 127.0.0.1/m, 'realip recursive off');
247
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
96 like(http_xff('/2', '10.0.1.1, 192.0.2.1, 127.0.0.1'),
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
97 qr/^X-IP: 192.0.2.1/m, 'realip recursive on');
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
98
247
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
99 like(http(<<EOF), qr/^X-IP: 10.0.1.1/m, 'realip multi xff recursive off');
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
100 GET /1 HTTP/1.0
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
101 Host: localhost
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
102 X-Forwarded-For: 192.0.2.1
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
103 X-Forwarded-For: 127.0.0.1, 10.0.1.1
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
104
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
105 EOF
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
106
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
107 like(http(<<EOF), qr/^X-IP: 192.0.2.1/m, 'realip multi xff recursive on');
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
108 GET /2 HTTP/1.0
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
109 Host: localhost
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
110 X-Forwarded-For: 10.0.1.1
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
111 X-Forwarded-For: 192.0.2.1
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
112 X-Forwarded-For: 127.0.0.1
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
113
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
114 EOF
bf6b05f03458 Tests: add realip tests for multiple X-Forwarded-For headers.
Andrei Belov <defan@nginx.com>
parents: 245
diff changeset
115
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
116 ###############################################################################
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
117
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
118 sub http_xff {
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
119 my ($uri, $xff) = @_;
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
120 return http(<<EOF);
245
7189255c1b91 Tests: added real_ip_recursive test.
Andrei Belov <defan@nginx.com>
parents: 217
diff changeset
121 GET $uri HTTP/1.0
216
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
122 Host: localhost
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
123 X-Forwarded-For: $xff
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
124
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
125 EOF
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
126 }
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
127
f2f37a52e42e Tests: basic tests for geo and realip modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
128 ###############################################################################