Mercurial > hg > nginx-tests
comparison geoip.t @ 1061:83357211563e
Tests: basic tests for geoip module.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Fri, 14 Oct 2016 15:05:02 +0300 |
parents | |
children | 6508aa1cf9cb |
comparison
equal
deleted
inserted
replaced
1060:a07205e8bb76 | 1061:83357211563e |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for geoip module. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
16 | |
17 use lib 'lib'; | |
18 use Test::Nginx; | |
19 | |
20 ############################################################################### | |
21 | |
22 select STDERR; $| = 1; | |
23 select STDOUT; $| = 1; | |
24 | |
25 my $t = Test::Nginx->new()->has(qw/http http_geoip ipv6/)->plan(20) | |
26 ->write_file_expand('nginx.conf', <<'EOF'); | |
27 | |
28 %%TEST_GLOBALS%% | |
29 | |
30 daemon off; | |
31 | |
32 events { | |
33 } | |
34 | |
35 http { | |
36 %%TEST_GLOBALS_HTTP%% | |
37 | |
38 geoip_proxy 127.0.0.1/32; | |
39 | |
40 geoip_country %%TESTDIR%%/country.dat; | |
41 geoip_city %%TESTDIR%%/city.dat; | |
42 geoip_org %%TESTDIR%%/org.dat; | |
43 | |
44 server { | |
45 listen 127.0.0.1:8080; | |
46 server_name localhost; | |
47 | |
48 location / { | |
49 add_header X-Country-Code $geoip_country_code; | |
50 add_header X-Country-Code3 $geoip_country_code3; | |
51 add_header X-Country-Name $geoip_country_name; | |
52 | |
53 add_header X-Area-Code $geoip_area_code; | |
54 add_header X-C-Continent-Code $geoip_city_continent_code; | |
55 add_header X-C-Country-Code $geoip_city_country_code; | |
56 add_header X-C-Country-Code3 $geoip_city_country_code3; | |
57 add_header X-C-Country-Name $geoip_city_country_name; | |
58 add_header X-Dma-Code $geoip_dma_code; | |
59 add_header X-Latitude $geoip_latitude; | |
60 add_header X-Longitude $geoip_longitude; | |
61 add_header X-Region $geoip_region; | |
62 add_header X-Region-Name $geoip_region_name; | |
63 add_header X-City $geoip_city; | |
64 add_header X-Postal-Code $geoip_postal_code; | |
65 | |
66 add_header X-Org $geoip_org; | |
67 } | |
68 } | |
69 } | |
70 | |
71 EOF | |
72 | |
73 my $d = $t->testdir(); | |
74 | |
75 # country database: | |
76 # | |
77 # "10.0.0.1","10.0.0.1","RU","Russian Federation" | |
78 # "2001:db8::","2001:db8::","US","United States" | |
79 | |
80 my $data = ''; | |
81 | |
82 for my $i (0 .. 156) { | |
83 # skip to offset 32 if 1st bit set in ipv6 address wins | |
84 $data .= pack_node($i + 1) . pack_node(32), next if $i == 2; | |
85 # otherwise default to RU | |
86 $data .= pack_node(0xffffb9) . pack_node(0xffff00), next if $i == 31; | |
87 # continue checking bits set in ipv6 address | |
88 $data .= pack_node(0xffff00) . pack_node($i + 1), next | |
89 if grep $_ == $i, (44, 49, 50, 52, 53, 55, 56, 57); | |
90 # last bit set in ipv6 address | |
91 $data .= pack_node(0xffffe1) . pack_node(0xffff00), next if $i == 156; | |
92 $data .= pack_node($i + 1) . pack_node(0xffff00); | |
93 } | |
94 | |
95 $data .= chr(0x00) x 3; | |
96 $data .= chr(0xFF) x 3; | |
97 $data .= chr(12); | |
98 | |
99 $t->write_file('country.dat', $data); | |
100 | |
101 # city database: | |
102 # | |
103 # "167772161","167772161","RU","48","Moscow","119034","55.7543",37.6202",, | |
104 | |
105 $data = ''; | |
106 | |
107 for my $i (0 .. 31) { | |
108 $data .= pack_node(32) . pack_node($i + 1), next if $i == 4 or $i == 6; | |
109 $data .= pack_node(32) . pack_node($i + 2), next if $i == 31; | |
110 $data .= pack_node($i + 1) . pack_node(32); | |
111 } | |
112 | |
113 $data .= chr(42); | |
114 $data .= chr(185); | |
115 $data .= pack('Z*', 48); | |
116 $data .= pack('Z*', 'Moscow'); | |
117 $data .= pack('Z*', 119034); | |
118 $data .= pack_node(int((55.7543 + 180) * 10000)); | |
119 $data .= pack_node(int((37.6202 + 180) * 10000)); | |
120 $data .= chr(0) x 3; | |
121 $data .= chr(0xFF) x 3; | |
122 $data .= chr(2); | |
123 $data .= pack_node(32); | |
124 | |
125 $t->write_file('city.dat', $data); | |
126 | |
127 # organization database: | |
128 # | |
129 # "167772161","167772161","Nginx" | |
130 | |
131 $data = ''; | |
132 | |
133 for my $i (0 .. 31) { | |
134 $data .= pack_org(32) . pack_org($i + 1), next if $i == 4 or $i == 6; | |
135 $data .= pack_org(32) . pack_org($i + 2), next if $i == 31; | |
136 $data .= pack_org($i + 1) . pack_org(32); | |
137 } | |
138 | |
139 $data .= chr(42); | |
140 $data .= pack('Z*', 'Nginx'); | |
141 $data .= chr(0xFF) x 3; | |
142 $data .= chr(5); | |
143 $data .= pack_node(32); | |
144 | |
145 $t->write_file('org.dat', $data); | |
146 $t->write_file('index.html', ''); | |
147 $t->run(); | |
148 | |
149 ############################################################################### | |
150 | |
151 my $r = http_xff('10.0.0.1'); | |
152 like($r, qr/X-Country-Code: RU/, 'geoip country code'); | |
153 like($r, qr/X-Country-Code3: RUS/, 'geoip country code 3'); | |
154 like($r, qr/X-Country-Name: Russian Federation/, 'geoip country name'); | |
155 | |
156 like($r, qr/X-Area-Code: 0/, 'geoip area code'); | |
157 like($r, qr/X-C-Continent-Code: EU/, 'geoip city continent code'); | |
158 like($r, qr/X-C-Country-Code: RU/, 'geoip city country code'); | |
159 like($r, qr/X-C-Country-Code3: RUS/, 'geoip city country code 3'); | |
160 like($r, qr/X-C-Country-Name: Russian Federation/, 'geoip city country name'); | |
161 like($r, qr/X-Dma-Code: 0/, 'geoip dma code'); | |
162 like($r, qr/X-Latitude: 55.7543/, 'geoip latitude'); | |
163 like($r, qr/X-Longitude: 37.6202/, 'geoip longitude'); | |
164 like($r, qr/X-Region: 48/, 'geoip region'); | |
165 like($r, qr/X-Region-Name: Moscow City/, 'geoip region name'); | |
166 like($r, qr/X-City: Moscow/, 'geoip city'); | |
167 like($r, qr/X-Postal-Code: 119034/, 'geoip postal code'); | |
168 | |
169 like($r, qr/X-Org: Nginx/, 'geoip org'); | |
170 | |
171 like(http_xff('::ffff:10.0.0.1'), qr/X-Org: Nginx/, 'geoip ipv6 ipv4-mapped'); | |
172 | |
173 $r = http_xff('2001:db8::'); | |
174 like($r, qr/X-Country-Code: US/, 'geoip ipv6 country code'); | |
175 like($r, qr/X-Country-Code3: USA/, 'geoip ipv6 country code 3'); | |
176 like($r, qr/X-Country-Name: United States/, 'geoip ipv6 country name'); | |
177 | |
178 ############################################################################### | |
179 | |
180 sub http_xff { | |
181 my ($xff) = @_; | |
182 return http(<<EOF); | |
183 GET / HTTP/1.0 | |
184 Host: localhost | |
185 X-Forwarded-For: $xff | |
186 | |
187 EOF | |
188 } | |
189 | |
190 sub pack_node { | |
191 substr pack('V', shift), 0, 3; | |
192 } | |
193 | |
194 sub pack_org { | |
195 pack('V', shift); | |
196 } | |
197 | |
198 ############################################################################### |