Add tests for acmetxt cidrslice and util funcs
This commit is contained in:
parent
f90ef442a3
commit
3e0ee478b7
38
pkg/acmedns/acmetxt_test.go
Normal file
38
pkg/acmedns/acmetxt_test.go
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
35
pkg/acmedns/cidrslice_test.go
Normal file
35
pkg/acmedns/cidrslice_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user