64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package model
|
|
|
|
import "testing"
|
|
|
|
func TestParseElement(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
wantZone string
|
|
wantSel SelectorKind
|
|
wantRef string
|
|
wantErr bool
|
|
}{
|
|
{in: "loc", wantZone: "loc", wantSel: SelNone},
|
|
{in: " net ", wantZone: "net", wantSel: SelNone},
|
|
{in: "net:+asn_cloudflare", wantZone: "net", wantSel: SelIPSet, wantRef: "asn_cloudflare"},
|
|
{in: "dmz:&api.partner", wantZone: "dmz", wantSel: SelFQDN, wantRef: "api.partner"},
|
|
{in: "net: +office", wantZone: "net", wantSel: SelIPSet, wantRef: "office"},
|
|
|
|
// Bare selectors must be rejected: a selector always needs a zone.
|
|
{in: "+office", wantErr: true},
|
|
{in: "&host", wantErr: true},
|
|
{in: "asn:13335", wantErr: true}, // no + or & sigil -> invalid selector
|
|
{in: "", wantErr: true},
|
|
{in: "net:", wantErr: true},
|
|
{in: ":+office", wantErr: true},
|
|
{in: "net:+", wantErr: true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got, err := ParseElement(tt.in)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("ParseElement(%q): expected error, got %+v", tt.in, got)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Errorf("ParseElement(%q): unexpected error: %v", tt.in, err)
|
|
continue
|
|
}
|
|
if got.Zone != tt.wantZone || got.Selector != tt.wantSel || got.Ref != tt.wantRef {
|
|
t.Errorf("ParseElement(%q) = %+v, want zone=%q sel=%q ref=%q",
|
|
tt.in, got, tt.wantZone, tt.wantSel, tt.wantRef)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAddressGroupSetName(t *testing.T) {
|
|
cases := []struct {
|
|
group AddressGroup
|
|
want string
|
|
}{
|
|
{AddressGroup{Name: "asn_cloudflare", Type: GroupASN}, "asn_cloudflare"},
|
|
{AddressGroup{Name: "cloudflare", Type: GroupASN}, "asn_cloudflare"},
|
|
{AddressGroup{Name: "office", Type: GroupStatic}, "office"},
|
|
{AddressGroup{Name: "vpn", Type: GroupDNS}, "vpn"},
|
|
}
|
|
for _, c := range cases {
|
|
if got := c.group.SetName(); got != c.want {
|
|
t.Errorf("SetName(%+v) = %q, want %q", c.group, got, c.want)
|
|
}
|
|
}
|
|
}
|