Skip to content

Commit 5fb666b

Browse files
committed
Tests: ACME configuration parsing test.
1 parent ea6a17f commit 5fb666b

File tree

2 files changed

+293
-0
lines changed

2 files changed

+293
-0
lines changed

t/acme_conf_certificate.t

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

t/acme_conf_issuer.t

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

0 commit comments

Comments
 (0)