Skip to content

Commit 8b8a070

Browse files
committed
Tests: ACME configuration parsing test.
1 parent 798881a commit 8b8a070

File tree

2 files changed

+304
-0
lines changed

2 files changed

+304
-0
lines changed

t/acme_conf_certificate.t

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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(5);
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+
is(check($t, <<'EOF' ), undef, 'valid');
66+
67+
acme_certificate example .example.test;
68+
69+
EOF
70+
71+
72+
is(check($t, <<'EOF' ), undef, 'valid - server_name');
73+
74+
server_name .example.test;
75+
acme_certificate example;
76+
77+
EOF
78+
79+
like(check($t, <<'EOF' ), qr/\[emerg].*no identifiers/, 'no identifiers');
80+
81+
acme_certificate example;
82+
83+
EOF
84+
85+
86+
like(check($t, <<'EOF'), qr/\[emerg].*issuer "[^"]+" is missing/, 'no issuer');
87+
88+
acme_certificate no-such-issuer .example.test;
89+
90+
EOF
91+
92+
like(check($t, <<'EOF'), qr/\[emerg].*no identifiers/, 'no server_name');
93+
94+
acme_certificate example;
95+
96+
EOF
97+
98+
# stop and clear the log to avoid triggering sanitizer checks
99+
100+
$t->stop()->write_file('error.log', '');
101+
102+
###############################################################################
103+
104+
sub check {
105+
my ($t, $cert) = @_;
106+
107+
$t->write_file_expand('nginx.conf',
108+
TEMPLATE_CONF =~ s/%%ACME_CERTIFICATE%%/$cert/r);
109+
110+
return try_run($t);
111+
}
112+
113+
sub try_run {
114+
my $t = shift;
115+
116+
# clean up after a successfull try
117+
118+
$t->stop();
119+
unlink $t->testdir() . '/error.log';
120+
121+
eval {
122+
open OLDERR, ">&", \*STDERR; close STDERR;
123+
$t->run();
124+
open STDERR, ">&", \*OLDERR;
125+
};
126+
127+
return unless $@;
128+
129+
return $t->read_file('error.log');
130+
}

t/acme_conf_issuer.t

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

0 commit comments

Comments
 (0)