]> git.street.me.uk Git - andy/dehydrated.git/blame - test.sh
use different fake-ca for travis tests
[andy/dehydrated.git] / test.sh
CommitLineData
a4e7c43a
LS
1#!/bin/bash
2
3# Fail early
4set -eu -o pipefail
5
6# Check if running in CI environment
7if [[ ! "${CI:-false}" == "true" ]]; then
8 echo "ERROR: Not running in CI environment!"
9 exit 1
10fi
11
12_TEST() {
d3bc67eb 13 echo
40556950
LS
14 echo "${1} "
15}
16_SUBTEST() {
17 echo -n " + ${1} "
a4e7c43a
LS
18}
19_PASS() {
40556950 20 echo -e "[\u001B[32mPASS\u001B[0m]"
a4e7c43a
LS
21}
22_FAIL() {
23 echo -e "[\u001B[31mFAIL\u001B[0m]"
24 echo
25 echo "Problem: ${@}"
26 echo
27 echo "STDOUT:"
28 cat tmplog
29 echo
30 echo "STDERR:"
31 cat errorlog
32 exit 1
33}
34_CHECK_FILE() {
d3bc67eb 35 _SUBTEST "Checking if file '${1}' exists..."
40556950
LS
36 if [[ -e "${1}" ]]; then
37 _PASS
38 else
39 _FAIL "Missing file: ${1}"
40 fi
a4e7c43a
LS
41}
42_CHECK_LOG() {
d3bc67eb 43 _SUBTEST "Checking if log contains '${1}'..."
40556950
LS
44 if grep -- "${1}" tmplog > /dev/null; then
45 _PASS
46 else
47 _FAIL "Missing in log: ${1}"
48 fi
49}
341f5252 50_CHECK_NOT_LOG() {
d3bc67eb 51 _SUBTEST "Checking if log doesn't contain '${1}'..."
341f5252
LS
52 if grep -- "${1}" tmplog > /dev/null; then
53 _FAIL "Found in log: ${1}"
54 else
55 _PASS
56 fi
57}
40556950
LS
58_CHECK_ERRORLOG() {
59 _SUBTEST "Checking if errorlog is empty..."
60 if [[ -z "$(cat errorlog)" ]]; then
61 _PASS
62 else
63 _FAIL "Non-empty errorlog"
64 fi
a4e7c43a
LS
65}
66
67# If not found (should be cached in travis) download ngrok
68if [[ ! -e "ngrok/ngrok" ]]; then
69 (
70 mkdir -p ngrok
71 cd ngrog
72 wget https://dl.ngrok.com/ngrok_2.0.19_linux_amd64.zip -O ngrok.zip
73 unzip ngrok.zip ngrok
74 chmod +x ngrok
75 )
76fi
77
78# Run ngrok and grab temporary url from logfile
79ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp.log &
80sleep 2
81TMP_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp.log | head -1 | cut -d':' -f2)"
82if [[ -z "${TMP_URL}" ]]; then
83 echo "Couldn't get an url from ngrok, not a letsencrypt.sh bug, tests can't continue."
84 exit 1
85fi
86
87# Run python webserver in .acme-challenges directory to serve challenge responses
88mkdir -p .acme-challenges/.well-known/acme-challenge
89(
90 cd .acme-challenges
91 python -m SimpleHTTPServer 8080 > /dev/null 2> /dev/null
92) &
93
94# Generate config and create empty domains.txt
f6f77139
LS
95echo 'CA="https://testca.kurz.pw/directory"' > config.sh
96echo 'LICENSE="https://testca.kurz.pw/terms/v1"' >> config.sh
a4e7c43a
LS
97echo 'WELLKNOWN=".acme-challenges/.well-known/acme-challenge"' >> config.sh
98touch domains.txt
99
100# Check if help command is working
101_TEST "Checking if help command is working..."
6a8f4482 102./letsencrypt.sh --help > tmplog 2> errorlog || _FAIL "Script execution failed"
a4e7c43a 103_CHECK_LOG "Default command: help"
40556950
LS
104_CHECK_LOG "--help (-h)"
105_CHECK_LOG "--domain (-d) domain.tld"
106_CHECK_ERRORLOG
a4e7c43a
LS
107
108# Run in cron mode with empty domains.txt (should only generate private key and exit)
109_TEST "First run in cron mode, checking if private key is generated and registered"
6a8f4482 110./letsencrypt.sh --cron > tmplog 2> errorlog || _FAIL "Script execution failed"
a4e7c43a
LS
111_CHECK_LOG "Registering account key"
112_CHECK_FILE "private_key.pem"
40556950 113_CHECK_ERRORLOG
a4e7c43a
LS
114
115# Temporarily move config out of the way and try signing certificate by using temporary config location
116_TEST "Try signing using temporary config location and with domain as command line parameter"
117mv config.sh tmp_config.sh
d1d9d1f6 118./letsencrypt.sh --cron --domain "${TMP_URL}" -f tmp_config.sh > tmplog 2> errorlog || _FAIL "Script execution failed"
6a8f4482 119_CHECK_NOT_LOG "Checking domain name(s) of existing cert"
a4e7c43a
LS
120_CHECK_LOG "Generating private key"
121_CHECK_LOG "Requesting challenge for ${TMP_URL}"
122_CHECK_LOG "Challenge is valid!"
123_CHECK_LOG "Creating fullchain.pem"
124_CHECK_LOG "Done!"
40556950 125_CHECK_ERRORLOG
a4e7c43a
LS
126mv tmp_config.sh config.sh
127
128# Move private key and add new location to config
129mv private_key.pem account_key.pem
130echo 'PRIVATE_KEY="./account_key.pem"' >> config.sh
131
132# Add domain to domains.txt and run in cron mode again (should find a non-expiring certificate and do nothing)
133_TEST "Run in cron mode again, this time with domain in domains.txt, should find non-expiring certificate"
134echo "${TMP_URL}" >> domains.txt
6a8f4482
LS
135./letsencrypt.sh --cron > tmplog 2> errorlog || _FAIL "Script execution failed"
136_CHECK_LOG "Checking domain name(s) of existing cert... unchanged."
a4e7c43a 137_CHECK_LOG "Skipping!"
40556950 138_CHECK_ERRORLOG
a4e7c43a 139
341f5252
LS
140# Run in cron mode one last time, with domain in domains.txt and force-resign (should find certificate, resign anyway, and not generate private key)
141_TEST "Run in cron mode one last time, with domain in domains.txt and force-resign"
142echo "${TMP_URL}" >> domains.txt
6a8f4482
LS
143./letsencrypt.sh --cron --force > tmplog 2> errorlog || _FAIL "Script execution failed"
144_CHECK_LOG "Checking domain name(s) of existing cert... unchanged."
2d097c92 145_CHECK_LOG "Ignoring because renew was forced!"
341f5252
LS
146_CHECK_NOT_LOG "Generating private key"
147_CHECK_LOG "Requesting challenge for ${TMP_URL}"
148_CHECK_LOG "Challenge is valid!"
149_CHECK_LOG "Creating fullchain.pem"
150_CHECK_LOG "Done!"
151_CHECK_ERRORLOG
152
a4e7c43a
LS
153# Delete account key (not needed anymore)
154rm account_key.pem
155
156# Check if certificate is valid in various ways
157_TEST "Verifying certificate..."
d3bc67eb
LS
158_SUBTEST "Verifying certificate on its own..."
159openssl x509 -in "certs/${TMP_URL}/cert.pem" -noout -text > tmplog 2> errorlog && _PASS || _FAIL
a4e7c43a 160_CHECK_LOG "CN=${TMP_URL}"
d3bc67eb
LS
161_SUBTEST "Verifying file with full chain..."
162openssl x509 -in "certs/${TMP_URL}/fullchain.pem" -noout -text > /dev/null 2>> errorlog && _PASS || _FAIL
163_SUBTEST "Verifying certificate against CA certificate..."
164(openssl verify -verbose -CAfile "certs/${TMP_URL}/fullchain.pem" -purpose sslserver "certs/${TMP_URL}/fullchain.pem" 2>&1 || true) | (grep -v ': OK$' || true) >> errorlog 2>> errorlog && _PASS || _FAIL
40556950 165_CHECK_ERRORLOG
a4e7c43a
LS
166
167# Revoke certificate using certificate key
168_TEST "Revoking certificate..."
6a8f4482 169./letsencrypt.sh --revoke "certs/${TMP_URL}/cert.pem" --privkey "certs/${TMP_URL}/privkey.pem" > tmplog 2> errorlog || _FAIL "Script execution failed"
a4e7c43a
LS
170_CHECK_LOG "Revoking certs/${TMP_URL}/cert.pem"
171_CHECK_LOG "SUCCESS"
172_CHECK_FILE "certs/${TMP_URL}/cert.pem-revoked"
40556950 173_CHECK_ERRORLOG
a4e7c43a
LS
174
175# All done
176exit 0