Mercurial > hg > nginx-tests
annotate stream_geoip.t @ 1990:85d88cd5091c
Tests: SSI include stub tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 04 Jul 2024 17:42:55 +0300 |
parents | f3ba4c74de31 |
children |
rev | line source |
---|---|
1062
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
2 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
3 # (C) Andrey Zelenkov |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
5 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
6 # Tests for stream geoip module. |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
7 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
9 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
11 use strict; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
12 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
14 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
15 use Socket qw/ $CRLF /; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
16 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
17 BEGIN { use FindBin; chdir($FindBin::Bin); } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
18 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
19 use lib 'lib'; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
20 use Test::Nginx; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
21 use Test::Nginx::Stream qw/ stream /; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
22 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
23 ############################################################################### |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
24 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
25 select STDERR; $| = 1; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
26 select STDOUT; $| = 1; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
27 |
1170
cf14cfe9ec8c
Tests: dropped obsolete ipv6 prerequisite.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1064
diff
changeset
|
28 my $t = Test::Nginx->new()->has(qw/stream stream_geoip stream_return/) |
1064
6508aa1cf9cb
Tests: skip geoip tests on hosts without GeoIP IPv6 support.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1062
diff
changeset
|
29 ->has('stream_realip'); |
1062
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
30 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
31 $t->write_file_expand('nginx.conf', <<'EOF'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
32 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
33 %%TEST_GLOBALS%% |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
34 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
35 daemon off; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
36 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
37 events { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
38 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
39 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
40 stream { |
1609
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1235
diff
changeset
|
41 %%TEST_GLOBALS_STREAM%% |
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1235
diff
changeset
|
42 |
1062
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
43 set_real_ip_from 127.0.0.1/32; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
44 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
45 geoip_country %%TESTDIR%%/country.dat; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
46 geoip_city %%TESTDIR%%/city.dat; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
47 geoip_org %%TESTDIR%%/org.dat; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
48 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
49 server { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
50 listen 127.0.0.1:8080 proxy_protocol; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
51 return "country_code:$geoip_country_code |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
52 country_code3:$geoip_country_code3 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
53 country_name:$geoip_country_name |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
54 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
55 area_code:$geoip_area_code |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
56 city_continent_code:$geoip_city_continent_code |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
57 city_country_code:$geoip_city_country_code |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
58 city_country_code3:$geoip_city_country_code3 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
59 city_country_name:$geoip_city_country_name |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
60 dma_code:$geoip_dma_code |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
61 latitude:$geoip_latitude |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
62 longitude:$geoip_longitude |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
63 region:$geoip_region |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
64 region_name:$geoip_region_name |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
65 city:$geoip_city |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
66 postal_code:$geoip_postal_code |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
67 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
68 org:$geoip_org"; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
69 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
70 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
71 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
72 EOF |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
73 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
74 my $d = $t->testdir(); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
75 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
76 # country database: |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
77 # |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
78 # "10.0.0.1","10.0.0.1","RU","Russian Federation" |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
79 # "2001:db8::","2001:db8::","US","United States" |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
80 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
81 my $data = ''; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
82 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
83 for my $i (0 .. 156) { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
84 # skip to offset 32 if 1st bit set in ipv6 address wins |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
85 $data .= pack_node($i + 1) . pack_node(32), next if $i == 2; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
86 # otherwise default to RU |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
87 $data .= pack_node(0xffffb9) . pack_node(0xffff00), next if $i == 31; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
88 # continue checking bits set in ipv6 address |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
89 $data .= pack_node(0xffff00) . pack_node($i + 1), next |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
90 if grep $_ == $i, (44, 49, 50, 52, 53, 55, 56, 57); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
91 # last bit set in ipv6 address |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
92 $data .= pack_node(0xffffe1) . pack_node(0xffff00), next if $i == 156; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
93 $data .= pack_node($i + 1) . pack_node(0xffff00); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
94 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
95 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
96 $data .= chr(0x00) x 3; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
97 $data .= chr(0xFF) x 3; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
98 $data .= chr(12); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
99 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
100 $t->write_file('country.dat', $data); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
101 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
102 # city database: |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
103 # |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
104 # "167772161","167772161","RU","48","Moscow","119034","55.7543",37.6202",, |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
105 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
106 $data = ''; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
107 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
108 for my $i (0 .. 31) { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
109 $data .= pack_node(32) . pack_node($i + 1), next if $i == 4 or $i == 6; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
110 $data .= pack_node(32) . pack_node($i + 2), next if $i == 31; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
111 $data .= pack_node($i + 1) . pack_node(32); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
112 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
113 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
114 $data .= chr(42); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
115 $data .= chr(185); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
116 $data .= pack('Z*', 48); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
117 $data .= pack('Z*', 'Moscow'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
118 $data .= pack('Z*', 119034); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
119 $data .= pack_node(int((55.7543 + 180) * 10000)); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
120 $data .= pack_node(int((37.6202 + 180) * 10000)); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
121 $data .= chr(0) x 3; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
122 $data .= chr(0xFF) x 3; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
123 $data .= chr(2); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
124 $data .= pack_node(32); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
125 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
126 $t->write_file('city.dat', $data); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
127 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
128 # organization database: |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
129 # |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
130 # "167772161","167772161","Nginx" |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
131 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
132 $data = ''; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
133 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
134 for my $i (0 .. 31) { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
135 $data .= pack_org(32) . pack_org($i + 1), next if $i == 4 or $i == 6; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
136 $data .= pack_org(32) . pack_org($i + 2), next if $i == 31; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
137 $data .= pack_org($i + 1) . pack_org(32); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
138 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
139 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
140 $data .= chr(42); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
141 $data .= pack('Z*', 'Nginx'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
142 $data .= chr(0xFF) x 3; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
143 $data .= chr(5); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
144 $data .= pack_node(32); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
145 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
146 $t->write_file('org.dat', $data); |
1064
6508aa1cf9cb
Tests: skip geoip tests on hosts without GeoIP IPv6 support.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1062
diff
changeset
|
147 $t->try_run('no inet6 support')->plan(20); |
1062
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
148 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
149 ############################################################################### |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
150 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
151 my %data = stream_pp('10.0.0.1') =~ /(\w+):(.*)/g; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
152 is($data{country_code}, 'RU', 'geoip country code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
153 is($data{country_code3}, 'RUS', 'geoip country code 3'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
154 is($data{country_name}, 'Russian Federation', 'geoip country name'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
155 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
156 is($data{area_code}, 0, 'geoip area code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
157 is($data{city_continent_code}, 'EU', 'geoip city continent code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
158 is($data{city_country_code}, 'RU', 'geoip city country code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
159 is($data{city_country_code3}, 'RUS', 'geoip city country code 3'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
160 is($data{city_country_name}, 'Russian Federation', 'geoip city country name'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
161 is($data{dma_code}, 0, 'geoip dma code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
162 is($data{latitude}, 55.7543, 'geoip latitude'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
163 is($data{longitude}, 37.6202, 'geoip longitude'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
164 is($data{region}, 48, 'geoip region'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
165 is($data{region_name}, 'Moscow City', 'geoip region name'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
166 is($data{city}, 'Moscow', 'geoip city'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
167 is($data{postal_code}, 119034, 'geoip postal code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
168 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
169 is($data{org}, 'Nginx', 'geoip org'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
170 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
171 like(stream_pp('::ffff:10.0.0.1'), qr/org:Nginx/, 'geoip ipv6 ipv4-mapped'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
172 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
173 %data = stream_pp('2001:db8::') =~ /(\w+):(.*)/g; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
174 is($data{country_code}, 'US', 'geoip ipv6 country code'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
175 is($data{country_code3}, 'USA', 'geoip ipv6 country code 3'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
176 is($data{country_name}, 'United States', 'geoip ipv6 country name'); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
177 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
178 ############################################################################### |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
179 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
180 sub stream_pp { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
181 my ($ip) = @_; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
182 my $type = ($ip =~ ':' ? 'TCP6' : 'TCP4'); |
1235
3fc6817cd84a
Tests: explicit peer port in stream tests now required.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1170
diff
changeset
|
183 return stream('127.0.0.1:' . port(8080)) |
3fc6817cd84a
Tests: explicit peer port in stream tests now required.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1170
diff
changeset
|
184 ->io("PROXY $type $ip 127.0.0.1 8080 8080${CRLF}"); |
1062
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
185 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
186 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
187 sub pack_node { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
188 substr pack('V', shift), 0, 3; |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
189 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
190 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
191 sub pack_org { |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
192 pack('V', shift); |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
193 } |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
194 |
f1cc64e4e67d
Tests: basic tests for stream geoip module.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
195 ############################################################################### |