|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Maxim Dounin |
| 4 | +# (C) Andrey Zelenkov |
| 5 | +# (C) Sergey Kandaurov |
| 6 | +# (C) Nginx, Inc. |
| 7 | + |
| 8 | +# Tests for server_name selection. |
| 9 | + |
| 10 | +############################################################################### |
| 11 | + |
| 12 | +use warnings; |
| 13 | +use strict; |
| 14 | + |
| 15 | +use Test::More; |
| 16 | + |
| 17 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | + |
| 19 | +use lib 'lib'; |
| 20 | +use Test::Nginx; |
| 21 | +use Test::Nginx::Stream qw/ stream /; |
| 22 | + |
| 23 | +############################################################################### |
| 24 | + |
| 25 | +select STDERR; $| = 1; |
| 26 | +select STDOUT; $| = 1; |
| 27 | + |
| 28 | +plan(skip_all => 'win32') if $^O eq 'MSWin32'; |
| 29 | + |
| 30 | +my $t = Test::Nginx->new()->has(qw/http rewrite/) |
| 31 | + ->has(qw/stream stream_ssl stream_return sni socket_ssl_sni/) |
| 32 | + ->has_daemon('openssl') |
| 33 | + ->write_file_expand('nginx.conf', <<'EOF'); |
| 34 | +
|
| 35 | +%%TEST_GLOBALS%% |
| 36 | +
|
| 37 | +daemon off; |
| 38 | +
|
| 39 | +events { |
| 40 | +} |
| 41 | +
|
| 42 | +stream { |
| 43 | + %%TEST_GLOBALS_STREAM%% |
| 44 | +
|
| 45 | + server_names_hash_bucket_size 64; |
| 46 | +
|
| 47 | + ssl_certificate_key localhost.key; |
| 48 | + ssl_certificate localhost.crt; |
| 49 | +
|
| 50 | + server { |
| 51 | + listen 127.0.0.1:8080 ssl; |
| 52 | + server_name localhost; |
| 53 | +
|
| 54 | + return $server_name; |
| 55 | + } |
| 56 | +
|
| 57 | + server { |
| 58 | + listen 127.0.0.1:8080; |
| 59 | + server_name www.example.com; |
| 60 | +
|
| 61 | + return $server_name; |
| 62 | + } |
| 63 | +
|
| 64 | + server { |
| 65 | + listen 127.0.0.1:8080; |
| 66 | + server_name ~^EXAMPLE\.COM$; |
| 67 | +
|
| 68 | + return $server_name; |
| 69 | + } |
| 70 | +
|
| 71 | + server { |
| 72 | + listen 127.0.0.1:8080; |
| 73 | + server_name ~^(?P<name>[a-z]+)\Q.example.com\E$; |
| 74 | +
|
| 75 | + return $name; |
| 76 | + } |
| 77 | +
|
| 78 | + server { |
| 79 | + listen 127.0.0.1:8080; |
| 80 | + server_name "~^(?P<name>www\p{N}+)\.example\.com$"; |
| 81 | +
|
| 82 | + return $name; |
| 83 | + } |
| 84 | +
|
| 85 | + server { |
| 86 | + listen 127.0.0.1:8080; |
| 87 | + server_name many.example.com many2.example.com; |
| 88 | +
|
| 89 | + return $server_name; |
| 90 | + } |
| 91 | +
|
| 92 | + server { |
| 93 | + listen 127.0.0.1:8080; |
| 94 | + server_name many3.example.com; |
| 95 | + server_name many4.example.com; |
| 96 | +
|
| 97 | + return $server_name; |
| 98 | + } |
| 99 | +
|
| 100 | + server { |
| 101 | + listen 127.0.0.1:8080; |
| 102 | + server_name *.wc.example.com; |
| 103 | +
|
| 104 | + return $server_name; |
| 105 | + } |
| 106 | +
|
| 107 | + server { |
| 108 | + listen 127.0.0.1:8080; |
| 109 | + server_name *.pref.wc.example.com; |
| 110 | +
|
| 111 | + return $server_name; |
| 112 | + } |
| 113 | +
|
| 114 | + server { |
| 115 | + listen 127.0.0.1:8080; |
| 116 | + server_name wc2.example.*; |
| 117 | +
|
| 118 | + return $server_name; |
| 119 | + } |
| 120 | +
|
| 121 | + server { |
| 122 | + listen 127.0.0.1:8080; |
| 123 | + server_name wc2.example.com.*; |
| 124 | +
|
| 125 | + return $server_name; |
| 126 | + } |
| 127 | +
|
| 128 | + server { |
| 129 | + listen 127.0.0.1:8080; |
| 130 | + server_name .dot.example.com; |
| 131 | +
|
| 132 | + return $server_name; |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | +EOF |
| 137 | + |
| 138 | +$t->write_file('openssl.conf', <<EOF); |
| 139 | +[ req ] |
| 140 | +default_bits = 2048 |
| 141 | +encrypt_key = no |
| 142 | +distinguished_name = req_distinguished_name |
| 143 | +[ req_distinguished_name ] |
| 144 | +EOF |
| 145 | + |
| 146 | +my $d = $t->testdir(); |
| 147 | + |
| 148 | +foreach my $name ('localhost') { |
| 149 | + system('openssl req -x509 -new ' |
| 150 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 151 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 152 | + . ">>$d/openssl.out 2>&1") == 0 |
| 153 | + or die "Can't create certificate for $name: $!\n"; |
| 154 | +} |
| 155 | + |
| 156 | +$t->try_run('no server_name')->plan(19); |
| 157 | + |
| 158 | +############################################################################### |
| 159 | + |
| 160 | +is(get_server('xxx'), 'localhost', 'default'); |
| 161 | + |
| 162 | +is(get_server('www.example.com'), 'www.example.com', |
| 163 | + 'www.example.com'); |
| 164 | +is(get_server('WWW.EXAMPLE.COM'), 'www.example.com', |
| 165 | + 'www.example.com uppercase'); |
| 166 | + |
| 167 | +is(get_server('example.com'), '~^EXAMPLE\.COM$', |
| 168 | + 'example.com regex'); |
| 169 | +is(get_server('EXAMPLE.COM'), '~^EXAMPLE\.COM$', |
| 170 | + 'example.com regex uppercase'); |
| 171 | + |
| 172 | +is(get_match('blah.example.com'), 'blah', |
| 173 | + '(P<name>.*).example.com named capture'); |
| 174 | +is(get_match('BLAH.EXAMPLE.COM'), 'blah', |
| 175 | + '(P<name>.*).example.com named capture uppercase'); |
| 176 | + |
| 177 | +is(get_match('www01.example.com'), 'www01', |
| 178 | + '\p{N} in named capture'); |
| 179 | +is(get_match('WWW01.EXAMPLE.COM'), 'www01', |
| 180 | + '\p{N} in named capture uppercase'); |
| 181 | + |
| 182 | +is(get_server('many.example.com'), 'many.example.com', |
| 183 | + 'name row - first'); |
| 184 | +is(get_server('many2.example.com'), 'many.example.com', |
| 185 | + 'name row - second'); |
| 186 | + |
| 187 | +is(get_server('many3.example.com'), 'many3.example.com', |
| 188 | + 'name list - first'); |
| 189 | +is(get_server('many4.example.com'), 'many3.example.com', |
| 190 | + 'name list - second'); |
| 191 | + |
| 192 | +is(get_server('www.wc.example.com'), |
| 193 | + '*.wc.example.com', 'wildcard first'); |
| 194 | +is(get_server('www.pref.wc.example.com'), |
| 195 | + '*.pref.wc.example.com', 'wildcard first most specific'); |
| 196 | +is(get_server('wc2.example.net'), |
| 197 | + 'wc2.example.*', 'wildcard last'); |
| 198 | +is(get_server('wc2.example.com.pref'), |
| 199 | + 'wc2.example.com.*', 'wildcard last most specific'); |
| 200 | + |
| 201 | +is(get_server('www.dot.example.com'), 'dot.example.com', |
| 202 | + 'wildcard dot'); |
| 203 | +is(get_server('dot.example.com'), 'dot.example.com', |
| 204 | + 'wildcard dot empty'); |
| 205 | + |
| 206 | +############################################################################### |
| 207 | + |
| 208 | +sub get_server { |
| 209 | + my ($host) = @_; |
| 210 | + |
| 211 | + my $s = stream( |
| 212 | + PeerAddr => '127.0.0.1:' . port(8080), |
| 213 | + SSL => 1, |
| 214 | + SSL_hostname => $host |
| 215 | + ); |
| 216 | + |
| 217 | + log_in("ssl sni: $host") if defined $host; |
| 218 | + |
| 219 | + return $s->read(); |
| 220 | +} |
| 221 | + |
| 222 | +sub get_match { |
| 223 | + &get_server; |
| 224 | +} |
| 225 | + |
| 226 | +############################################################################### |
0 commit comments