From 3e0ee478b7017a682f93879ce931580420850b66 Mon Sep 17 00:00:00 2001 From: Joona Hoikkala Date: Mon, 26 Dec 2022 15:56:19 +0200 Subject: [PATCH] Add tests for acmetxt cidrslice and util funcs --- pkg/acmedns/acmetxt_test.go | 38 +++++++++++++++++++++++++++++++++++ pkg/acmedns/cidrslice_test.go | 35 ++++++++++++++++++++++++++++++++ pkg/acmedns/util_test.go | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 pkg/acmedns/acmetxt_test.go create mode 100644 pkg/acmedns/cidrslice_test.go diff --git a/pkg/acmedns/acmetxt_test.go b/pkg/acmedns/acmetxt_test.go new file mode 100644 index 0000000..5df9cad --- /dev/null +++ b/pkg/acmedns/acmetxt_test.go @@ -0,0 +1,38 @@ +package acmedns + +import "testing" + +func TestAllowedFrom(t *testing.T) { + testslice := NewACMETxt() + testslice.AllowFrom = []string{"192.168.1.0/24", "2001:db8::/32"} + for _, test := range []struct { + input string + expected bool + }{ + {"192.168.1.42", true}, + {"192.168.2.42", false}, + {"2001:db8:aaaa::", true}, + {"2001:db9:aaaa::", false}, + } { + if testslice.AllowedFrom(test.input) != test.expected { + t.Errorf("Was expecting AllowedFrom to return %t for %s but got %t instead.", test.expected, test.input, !test.expected) + } + } +} + +func TestAllowedFromList(t *testing.T) { + testslice := ACMETxt{AllowFrom: []string{"192.168.1.0/24", "2001:db8::/32"}} + if testslice.AllowedFromList([]string{"192.168.2.2", "1.1.1.1"}) != false { + t.Errorf("Was expecting AllowedFromList to return false") + } + if testslice.AllowedFromList([]string{"192.168.1.2", "1.1.1.1"}) != true { + t.Errorf("Was expecting AllowedFromList to return true") + } + allowfromall := ACMETxt{AllowFrom: []string{}} + if allowfromall.AllowedFromList([]string{"192.168.1.2", "1.1.1.1"}) != true { + t.Errorf("Expected non-restricted AlloFrom to be allowed") + } + if allowfromall.AllowedFromList([]string{}) != true { + t.Errorf("Expected non-restricted AlloFrom to be allowed for empty list") + } +} diff --git a/pkg/acmedns/cidrslice_test.go b/pkg/acmedns/cidrslice_test.go new file mode 100644 index 0000000..612fa85 --- /dev/null +++ b/pkg/acmedns/cidrslice_test.go @@ -0,0 +1,35 @@ +package acmedns + +import ( + "encoding/json" + "testing" +) + +func TestCidrSlice(t *testing.T) { + for i, test := range []struct { + input Cidrslice + expectedErr bool + expectedLen int + }{ + {[]string{"192.168.1.0/24"}, false, 1}, + {[]string{"shoulderror"}, true, 0}, + {[]string{"2001:db8:aaaaa::"}, true, 0}, + {[]string{"192.168.1.0/24", "2001:db8::/32"}, false, 2}, + } { + err := test.input.IsValid() + if test.expectedErr && err == nil { + t.Errorf("Expected test %d to generate IsValid() error but it didn't", i) + } + if !test.expectedErr && err != nil { + t.Errorf("Expected test %d to pass IsValid() but it generated an error %s", i, err) + } + outSlice := []string{} + err = json.Unmarshal([]byte(test.input.JSON()), &outSlice) + if err != nil { + t.Errorf("Unexpected error when unmarshaling Cidrslice JSON: %s", err) + } + if len(outSlice) != test.expectedLen { + t.Errorf("Expected cidrslice JSON to be of length %d, but got %d instead for test %d", test.expectedLen, len(outSlice), i) + } + } +} diff --git a/pkg/acmedns/util_test.go b/pkg/acmedns/util_test.go index f859309..e10fe2b 100644 --- a/pkg/acmedns/util_test.go +++ b/pkg/acmedns/util_test.go @@ -3,6 +3,7 @@ package acmedns import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" + "golang.org/x/crypto/bcrypt" "os" "syscall" "testing" @@ -160,3 +161,37 @@ func TestPrepareConfig(t *testing.T) { } } } + +func TestSanitizeString(t *testing.T) { + for i, test := range []struct { + input string + expected string + }{ + {"abcd!abcd", "abcdabcd"}, + {"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz0123456789", "ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz0123456789"}, + {"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopq=@rstuvwxyz0123456789", "ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz0123456789"}, + } { + if SanitizeString(test.input) != test.expected { + t.Errorf("Expected SanitizeString to return %s for test %d, but got %s instead", test.expected, i, SanitizeString(test.input)) + } + } +} + +func TestCorrectPassword(t *testing.T) { + testPass, _ := bcrypt.GenerateFromPassword([]byte("nevergonnagiveyouup"), 10) + for i, test := range []struct { + input string + expected bool + }{ + {"abcd", false}, + {"nevergonnagiveyouup", true}, + {"@rstuvwxyz0123456789", false}, + } { + if test.expected && !CorrectPassword(test.input, string(testPass)) { + t.Errorf("Expected CorrectPassword to return %t for test %d", test.expected, i) + } + if !test.expected && CorrectPassword(test.input, string(testPass)) { + t.Errorf("Expected CorrectPassword to return %t for test %d", test.expected, i) + } + } +}