From c040e4b459fd927ddd2878c0f18521bee6ea52e4 Mon Sep 17 00:00:00 2001 From: max furman Date: Tue, 16 Aug 2022 15:48:23 -0700 Subject: [PATCH] Add unit tests --- acme/api/order_test.go | 61 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/acme/api/order_test.go b/acme/api/order_test.go index 7f67c72e..724357d8 100644 --- a/acme/api/order_test.go +++ b/acme/api/order_test.go @@ -49,6 +49,36 @@ func TestNewOrderRequest_Validate(t *testing.T) { err: acme.NewError(acme.ErrorMalformedType, "identifier type unsupported: foo"), } }, + "fail/bad-identifier/bad-dns": func(t *testing.T) test { + return test{ + nor: &NewOrderRequest{ + Identifiers: []acme.Identifier{ + {Type: "dns", Value: "xn--bücher.example.com"}, + }, + }, + err: acme.NewError(acme.ErrorMalformedType, "invalid DNS name: xn--bücher.example.com"), + } + }, + "fail/bad-identifier/dns-port": func(t *testing.T) test { + return test{ + nor: &NewOrderRequest{ + Identifiers: []acme.Identifier{ + {Type: "dns", Value: "example.com:8080"}, + }, + }, + err: acme.NewError(acme.ErrorMalformedType, "invalid DNS name: example.com:8080"), + } + }, + "fail/bad-identifier/dns-wildcard-port": func(t *testing.T) test { + return test{ + nor: &NewOrderRequest{ + Identifiers: []acme.Identifier{ + {Type: "dns", Value: "*.example.com:8080"}, + }, + }, + err: acme.NewError(acme.ErrorMalformedType, "invalid DNS name: *.example.com:8080"), + } + }, "fail/bad-ip": func(t *testing.T) test { nbf := time.Now().UTC().Add(time.Minute) naf := time.Now().UTC().Add(5 * time.Minute) @@ -72,7 +102,7 @@ func TestNewOrderRequest_Validate(t *testing.T) { nor: &NewOrderRequest{ Identifiers: []acme.Identifier{ {Type: "dns", Value: "example.com"}, - {Type: "dns", Value: "bar.com"}, + {Type: "dns", Value: "*.bar.com"}, }, NotAfter: naf, NotBefore: nbf, @@ -2097,3 +2127,32 @@ func TestHandler_challengeTypes(t *testing.T) { }) } } + +func TestTrimIfWildcard(t *testing.T) { + tests := []struct { + name string + arg string + wantValue string + wantBool bool + }{ + { + name: "no trim", + arg: "smallstep.com", + wantValue: "smallstep.com", + wantBool: false, + }, + { + name: "trim", + arg: "*.smallstep.com", + wantValue: "smallstep.com", + wantBool: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v, ok := trimIfWildcard(tt.arg) + assert.Equals(t, v, tt.wantValue) + assert.Equals(t, ok, tt.wantBool) + }) + } +}