|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Andrey Zelenkov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for HTTP/3 protocol with server_tokens directive. |
| 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 | +use Test::Nginx::HTTP3; |
| 20 | + |
| 21 | +############################################################################### |
| 22 | + |
| 23 | +select STDERR; $| = 1; |
| 24 | +select STDOUT; $| = 1; |
| 25 | + |
| 26 | +eval { require Crypt::Misc; die if $Crypt::Misc::VERSION < 0.067; }; |
| 27 | +plan(skip_all => 'CryptX version >= 0.067 required') if $@; |
| 28 | + |
| 29 | +my $t = Test::Nginx->new()->has(qw/http http_v3 rewrite/) |
| 30 | + ->has_daemon('openssl')->plan(12); |
| 31 | + |
| 32 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 33 | +
|
| 34 | +%%TEST_GLOBALS%% |
| 35 | +
|
| 36 | +daemon off; |
| 37 | +
|
| 38 | +events { |
| 39 | +} |
| 40 | +
|
| 41 | +http { |
| 42 | + %%TEST_GLOBALS_HTTP%% |
| 43 | +
|
| 44 | + ssl_certificate_key localhost.key; |
| 45 | + ssl_certificate localhost.crt; |
| 46 | +
|
| 47 | + server { |
| 48 | + listen 127.0.0.1:%%PORT_8980_UDP%% quic; |
| 49 | + server_name localhost; |
| 50 | +
|
| 51 | + location /200 { |
| 52 | + return 200; |
| 53 | + } |
| 54 | +
|
| 55 | + location /404 { |
| 56 | + return 404; |
| 57 | + } |
| 58 | +
|
| 59 | + location /off { |
| 60 | + server_tokens off; |
| 61 | +
|
| 62 | + location /off/200 { |
| 63 | + return 200; |
| 64 | + } |
| 65 | +
|
| 66 | + location /off/404 { |
| 67 | + return 404; |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + location /on { |
| 72 | + server_tokens on; |
| 73 | +
|
| 74 | + location /on/200 { |
| 75 | + return 200; |
| 76 | + } |
| 77 | +
|
| 78 | + location /on/404 { |
| 79 | + return 404; |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + location /b { |
| 84 | + server_tokens build; |
| 85 | +
|
| 86 | + location /b/200 { |
| 87 | + return 200; |
| 88 | + } |
| 89 | +
|
| 90 | + location /b/404 { |
| 91 | + return 404; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +
|
| 97 | +EOF |
| 98 | + |
| 99 | +$t->write_file('openssl.conf', <<EOF); |
| 100 | +[ req ] |
| 101 | +default_bits = 2048 |
| 102 | +encrypt_key = no |
| 103 | +distinguished_name = req_distinguished_name |
| 104 | +[ req_distinguished_name ] |
| 105 | +EOF |
| 106 | + |
| 107 | +my $d = $t->testdir(); |
| 108 | + |
| 109 | +foreach my $name ('localhost') { |
| 110 | + system('openssl req -x509 -new ' |
| 111 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 112 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 113 | + . ">>$d/openssl.out 2>&1") == 0 |
| 114 | + or die "Can't create certificate for $name: $!\n"; |
| 115 | +} |
| 116 | + |
| 117 | +$t->run(); |
| 118 | + |
| 119 | +############################################################################### |
| 120 | + |
| 121 | +my $re = qr/nginx\/\d+\.\d+\.\d+/; |
| 122 | + |
| 123 | +like(header_server('/200'), qr/^$re$/, 'http3 tokens default 200'); |
| 124 | +like(header_server('/404'), qr/^$re$/, 'http3 tokens default 404'); |
| 125 | +like(body('/404'), qr/$re/, 'http3 tokens default 404 body'); |
| 126 | + |
| 127 | +is(header_server('/off/200'), 'nginx', 'http3 tokens off 200'); |
| 128 | +is(header_server('/off/404'), 'nginx', 'http3 tokens off 404'); |
| 129 | +like(body('/off/404'), qr/nginx(?!\/)/, 'http3 tokens off 404 body'); |
| 130 | + |
| 131 | +like(header_server('/on/200'), qr/^$re$/, 'http3 tokens on 200'); |
| 132 | +like(header_server('/on/404'), qr/^$re$/, 'http3 tokens on 404'); |
| 133 | +like(body('/on/404'), $re, 'http3 tokens on 404 body'); |
| 134 | + |
| 135 | +$re = qr/$re \(.*\)/ if $t->has_module('--build='); |
| 136 | + |
| 137 | +like(header_server('/b/200'), qr/^$re$/, 'http3 tokens build 200'); |
| 138 | +like(header_server('/b/404'), qr/^$re$/, 'http3 tokens build 404'); |
| 139 | +like(body('/b/404'), qr/$re/, 'http3 tokens build 404 body'); |
| 140 | + |
| 141 | +############################################################################### |
| 142 | + |
| 143 | +sub header_server { |
| 144 | + my ($path) = shift; |
| 145 | + |
| 146 | + my $s = Test::Nginx::HTTP3->new(); |
| 147 | + my $sid = $s->new_stream({ path => $path }); |
| 148 | + my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); |
| 149 | + |
| 150 | + my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; |
| 151 | + return $frame->{headers}->{'server'}; |
| 152 | +} |
| 153 | + |
| 154 | +sub body { |
| 155 | + my ($path) = shift; |
| 156 | + |
| 157 | + my $s = Test::Nginx::HTTP3->new(); |
| 158 | + my $sid = $s->new_stream({ path => $path }); |
| 159 | + my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); |
| 160 | + |
| 161 | + my ($frame) = grep { $_->{type} eq "DATA" } @$frames; |
| 162 | + return $frame->{'data'}; |
| 163 | +} |
| 164 | + |
| 165 | +############################################################################### |
0 commit comments