|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Aleksei Bavshin |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for ACME client: configuration parsing and validation. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +use IO::Select; |
| 16 | + |
| 17 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | + |
| 19 | +use lib 'lib'; |
| 20 | +use Test::Nginx; |
| 21 | + |
| 22 | +############################################################################### |
| 23 | + |
| 24 | +select STDERR; $| = 1; |
| 25 | +select STDOUT; $| = 1; |
| 26 | + |
| 27 | +my $t = Test::Nginx->new()->has(qw/http http_ssl/)->plan(8); |
| 28 | + |
| 29 | +use constant TEMPLATE_CONF => <<'EOF'; |
| 30 | +
|
| 31 | +%%TEST_GLOBALS%% |
| 32 | +
|
| 33 | +daemon off; |
| 34 | +
|
| 35 | +events { |
| 36 | +} |
| 37 | +
|
| 38 | +http { |
| 39 | + %%TEST_GLOBALS_HTTP%% |
| 40 | +
|
| 41 | + server { |
| 42 | + listen 127.0.0.1:8080; |
| 43 | + server_name example.test; |
| 44 | + } |
| 45 | +
|
| 46 | + server { |
| 47 | + listen 127.0.0.1:8443 ssl; |
| 48 | + server_name example.test; |
| 49 | +
|
| 50 | + acme_certificate example example.test; |
| 51 | +
|
| 52 | + ssl_certificate $acme_certificate; |
| 53 | + ssl_certificate_key $acme_certificate_key; |
| 54 | + } |
| 55 | +
|
| 56 | + %%ACME_ISSUER%% |
| 57 | +} |
| 58 | +
|
| 59 | +EOF |
| 60 | + |
| 61 | +############################################################################### |
| 62 | + |
| 63 | +is(check($t, <<'EOF' ), undef, 'valid'); |
| 64 | +
|
| 65 | +acme_shared_zone zone=ngx_acme_shared:1M; |
| 66 | +
|
| 67 | +acme_issuer example { |
| 68 | + uri https://localhost:%%PORT_9000%%/dir; |
| 69 | + account_key ecdsa:256; |
| 70 | + contact mailto:admin@example.test; |
| 71 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 72 | + resolver_timeout 5s; |
| 73 | + ssl_verify off; |
| 74 | + state_path %%TESTDIR%%; |
| 75 | +} |
| 76 | +
|
| 77 | +EOF |
| 78 | + |
| 79 | + |
| 80 | +is(check($t, <<'EOF' ), undef, 'valid - resolver in server'); |
| 81 | +
|
| 82 | +acme_issuer example { |
| 83 | + uri https://localhost:%%PORT_9000%%/dir; |
| 84 | + ssl_verify off; |
| 85 | +} |
| 86 | +
|
| 87 | +resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 88 | +
|
| 89 | +EOF |
| 90 | + |
| 91 | + |
| 92 | +like(check($t, <<'EOF' ), qr/\[emerg].*resolver is not/, 'no resolver'); |
| 93 | +
|
| 94 | +acme_issuer example { |
| 95 | + uri https://localhost:%%PORT_9000%%/dir; |
| 96 | + ssl_verify off; |
| 97 | +} |
| 98 | +
|
| 99 | +EOF |
| 100 | + |
| 101 | + |
| 102 | +like(check($t, <<'EOF' ), qr/\[emerg].*must have "zone"/, 'bad zone value'); |
| 103 | +
|
| 104 | +acme_shared_zone bad-value; |
| 105 | +
|
| 106 | +acme_issuer example { |
| 107 | + uri https://localhost:%%PORT_9000%%/dir; |
| 108 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 109 | + ssl_verify off; |
| 110 | +} |
| 111 | +
|
| 112 | +EOF |
| 113 | + |
| 114 | + |
| 115 | +like(check($t, <<'EOF' ), qr/\[emerg].*invalid zone size/, 'bad zone size'); |
| 116 | +
|
| 117 | +acme_shared_zone zone=test:bad-size; |
| 118 | +
|
| 119 | +acme_issuer example { |
| 120 | + uri https://localhost:%%PORT_9000%%/dir; |
| 121 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 122 | + ssl_verify off; |
| 123 | +} |
| 124 | +
|
| 125 | +EOF |
| 126 | + |
| 127 | + |
| 128 | +like(check($t, <<'EOF' ), qr/\[emerg].*cannot load/, 'bad key file'); |
| 129 | +
|
| 130 | +acme_issuer example { |
| 131 | + uri https://localhost:%%PORT_9000%%/dir; |
| 132 | + account_key no-such-file.key; |
| 133 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 134 | + ssl_verify off; |
| 135 | +} |
| 136 | +
|
| 137 | +EOF |
| 138 | + |
| 139 | + |
| 140 | +like(check($t, <<'EOF' ), qr/\[emerg].*unsupported curve/, 'bad key curve'); |
| 141 | +
|
| 142 | +acme_issuer example { |
| 143 | + uri https://localhost:%%PORT_9000%%/dir; |
| 144 | + account_key ecdsa:234; |
| 145 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 146 | + ssl_verify off; |
| 147 | +} |
| 148 | +
|
| 149 | +EOF |
| 150 | + |
| 151 | + |
| 152 | +like(check($t, <<'EOF' ), qr/\[emerg].*unsupported key size/, 'bad key size'); |
| 153 | +
|
| 154 | +acme_issuer example { |
| 155 | + uri https://localhost:%%PORT_9000%%/dir; |
| 156 | + account_key rsa:1024; |
| 157 | + resolver 127.0.0.1:%%PORT_8980_UDP%%; |
| 158 | + ssl_verify off; |
| 159 | +} |
| 160 | +
|
| 161 | +EOF |
| 162 | + |
| 163 | +# stop and clear the log to avoid triggering sanitizer checks |
| 164 | + |
| 165 | +$t->stop()->write_file('error.log', ''); |
| 166 | + |
| 167 | +############################################################################### |
| 168 | + |
| 169 | +sub check { |
| 170 | + my ($t, $issuer) = @_; |
| 171 | + |
| 172 | + $t->write_file_expand('nginx.conf', |
| 173 | + TEMPLATE_CONF =~ s/%%ACME_ISSUER%%/$issuer/r); |
| 174 | + |
| 175 | + return try_run($t); |
| 176 | +} |
| 177 | + |
| 178 | +sub try_run { |
| 179 | + my $t = shift; |
| 180 | + |
| 181 | + # clean up after a successfull try |
| 182 | + |
| 183 | + $t->stop(); |
| 184 | + unlink $t->testdir() . '/error.log'; |
| 185 | + |
| 186 | + eval { |
| 187 | + open OLDERR, ">&", \*STDERR; close STDERR; |
| 188 | + $t->run(); |
| 189 | + open STDERR, ">&", \*OLDERR; |
| 190 | + }; |
| 191 | + |
| 192 | + return unless $@; |
| 193 | + |
| 194 | + my $log = $t->read_file('error.log'); |
| 195 | + |
| 196 | + if ($ENV{TEST_NGINX_VERBOSE}) { |
| 197 | + map { Test::Nginx::log_core($_) } split(/^/m, $log); |
| 198 | + } |
| 199 | + |
| 200 | + return $log; |
| 201 | +} |
0 commit comments