git » gofer » commit f3bc736

autocert: Add test for requests to an unknown domain

author Alberto Bertogli
2022-10-15 12:30:50 UTC
committer Alberto Bertogli
2022-10-15 12:30:50 UTC
parent 5edd5d4170a2dd2f858d83f9b1acf24283ec175d

autocert: Add test for requests to an unknown domain

When autocert is used and we get a request for a domain that is not
on the configured list, autocert should reject the request.

This patch adds a test case for that situation.

test/test.sh +12 -0
test/util/exp/exp.go +15 -1

diff --git a/test/test.sh b/test/test.sh
index 79a7a74..fa4cbe2 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -176,6 +176,18 @@ base="https://miau.com:8443"
 exp $base/file -forcelocalhost -body "ñaca\n"
 exp $base/dir/ñaca -forcelocalhost -body "tracañaca\n"
 
+# Request for a domain not in our list, check that the request is denied, and
+# also that we log it properly.
+exp "https://unknown-ac:8443/file" -forcelocalhost \
+	-clienterrorre "tls: internal error"
+if ! waitgrep \
+	-q 'request for "unknown-ac" -> acme/autocert:' \
+	.01-fe.log;
+then
+	echo "autocert error was not logged properly"
+	exit 1
+fi
+
 unset CACERT
 
 
diff --git a/test/util/exp/exp.go b/test/util/exp/exp.go
index e605602..13c3bff 100644
--- a/test/util/exp/exp.go
+++ b/test/util/exp/exp.go
@@ -38,6 +38,8 @@ func main() {
 			"enable verbose output")
 		hdrRE = flag.String("hdrre", "",
 			"expect a header matching these contents (regexp match)")
+		clientErrorRE = flag.String("clienterrorre", "",
+			"expect a client error matching these contents (regexp match)")
 		caCert = flag.String("cacert", "",
 			"file to read CA cert from")
 		forceLocalhost = flag.Bool("forcelocalhost", false,
@@ -51,7 +53,19 @@ func main() {
 	}
 
 	resp, err := client.Get(url)
-	if err != nil {
+	if *clientErrorRE != "" {
+		if err == nil {
+			errorf("expected client error, got nil")
+		}
+		matched, err := regexp.MatchString(*clientErrorRE, err.Error())
+		if err != nil {
+			errorf("regexp error: %q\n", err)
+		}
+		if !matched {
+			errorf("client error did not match regexp: %q\n", err.Error())
+		}
+		os.Exit(exitCode)
+	} else if err != nil {
 		fatalf("error getting %q: %v\n", url, err)
 	}
 	defer resp.Body.Close()