-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for acmetxt cidrslice and util funcs
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters