Skip to content

Commit 37cad21

Browse files
committed
Tests: ACME configuration parsing test.
1 parent 9da1121 commit 37cad21

File tree

2 files changed

+295
-0
lines changed

2 files changed

+295
-0
lines changed

t/acme_conf_certificate.t

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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(4);
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+
49+
%%ACME_CERTIFICATE%%
50+
51+
ssl_certificate $acme_certificate;
52+
ssl_certificate_key $acme_certificate_key;
53+
}
54+
55+
acme_issuer example {
56+
uri https://localhost:%%PORT_9000%%/dir;
57+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
58+
}
59+
}
60+
61+
EOF
62+
63+
###############################################################################
64+
65+
like(check($t, <<'EOF' ), qr/\[emerg].*no identifiers/, 'no identifiers');
66+
67+
acme_certificate issuer=example server_name;
68+
69+
EOF
70+
71+
72+
like(check($t, <<'EOF'), qr/\[emerg].*issuer "[^"]+" is missing/, 'no issuer');
73+
74+
acme_certificate issuer=no-such-issuer .example.test;
75+
76+
EOF
77+
78+
# run valid configuration tests last to suppress sanitizer errors
79+
80+
is(check($t, <<'EOF' ), undef, 'valid');
81+
82+
server_name .example.test;
83+
acme_certificate issuer=example .example.test;
84+
85+
EOF
86+
87+
88+
is(check($t, <<'EOF' ), undef, 'valid - server_name');
89+
90+
server_name .example.test;
91+
acme_certificate issuer=example server_name;
92+
93+
EOF
94+
95+
###############################################################################
96+
97+
sub check {
98+
my ($t, $cert) = @_;
99+
100+
$t->write_file_expand('nginx.conf',
101+
TEMPLATE_CONF =~ s/%%ACME_CERTIFICATE%%/$cert/r);
102+
103+
return try_run($t);
104+
}
105+
106+
sub try_run {
107+
my $t = shift;
108+
109+
# clean up after a successfull try
110+
111+
$t->stop();
112+
unlink $t->testdir() . '/error.log';
113+
114+
eval {
115+
open OLDERR, ">&", \*STDERR; close STDERR;
116+
$t->run();
117+
open STDERR, ">&", \*OLDERR;
118+
};
119+
120+
return unless $@;
121+
122+
return $t->read_file('error.log');
123+
}

t/acme_conf_issuer.t

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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(7);
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.test
51+
issuer=example;
52+
53+
ssl_certificate $acme_certificate;
54+
ssl_certificate_key $acme_certificate_key;
55+
}
56+
57+
%%ACME_ISSUER%%
58+
}
59+
60+
EOF
61+
62+
###############################################################################
63+
64+
like(check($t, <<'EOF' ), qr/\[emerg].*resolver is not/, 'no resolver');
65+
66+
acme_issuer example {
67+
uri https://localhost:%%PORT_9000%%/dir;
68+
}
69+
70+
EOF
71+
72+
like(check($t, <<'EOF' ), qr/\[emerg].*invalid zone size/, 'bad zone size');
73+
74+
acme_shared_zone bad-value;
75+
76+
acme_issuer example {
77+
uri https://localhost:%%PORT_9000%%/dir;
78+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
79+
}
80+
81+
EOF
82+
83+
84+
like(check($t, <<'EOF' ), qr/\[emerg].*cannot load/, 'bad key file');
85+
86+
acme_issuer example {
87+
uri https://localhost:%%PORT_9000%%/dir;
88+
account_key no-such-file.key;
89+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
90+
}
91+
92+
EOF
93+
94+
95+
like(check($t, <<'EOF' ), qr/\[emerg].*unsupported curve/, 'bad key curve');
96+
97+
acme_issuer example {
98+
uri https://localhost:%%PORT_9000%%/dir;
99+
account_key ecdsa:234;
100+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
101+
}
102+
103+
EOF
104+
105+
106+
like(check($t, <<'EOF' ), qr/\[emerg].*unsupported key size/, 'bad key size');
107+
108+
acme_issuer example {
109+
uri https://localhost:%%PORT_9000%%/dir;
110+
account_key rsa:1024;
111+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
112+
}
113+
114+
EOF
115+
116+
# run valid configuration tests last to suppress sanitizer errors
117+
118+
is(check($t, <<'EOF' ), undef, 'valid');
119+
120+
acme_shared_zone 1M;
121+
122+
acme_issuer example {
123+
uri https://localhost:%%PORT_9000%%/dir;
124+
account_key ecdsa:256;
125+
contact mailto:[email protected];
126+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
127+
resolver_timeout 5s;
128+
state_path %%TESTDIR%%;
129+
}
130+
131+
EOF
132+
133+
134+
is(check($t, <<'EOF' ), undef, 'valid - resolver in server');
135+
136+
acme_issuer example {
137+
uri https://localhost:%%PORT_9000%%/dir;
138+
}
139+
140+
resolver 127.0.0.1:%%PORT_8980_UDP%%;
141+
142+
EOF
143+
144+
###############################################################################
145+
146+
sub check {
147+
my ($t, $issuer) = @_;
148+
149+
$t->write_file_expand('nginx.conf',
150+
TEMPLATE_CONF =~ s/%%ACME_ISSUER%%/$issuer/r);
151+
152+
return try_run($t);
153+
}
154+
155+
sub try_run {
156+
my $t = shift;
157+
158+
# clean up after a successfull try
159+
160+
$t->stop();
161+
unlink $t->testdir() . '/error.log';
162+
163+
eval {
164+
open OLDERR, ">&", \*STDERR; close STDERR;
165+
$t->run();
166+
open STDERR, ">&", \*OLDERR;
167+
};
168+
169+
return unless $@;
170+
171+
return $t->read_file('error.log');
172+
}

0 commit comments

Comments
 (0)