mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-03-04 02:30:23 +00:00
75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
// Copyright 2020-2023 Project Capsule Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//nolint:dupl
|
|
package api
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAllowedListSpec_ExactMatch(t *testing.T) {
|
|
type tc struct {
|
|
In []string
|
|
True []string
|
|
False []string
|
|
}
|
|
|
|
for _, tc := range []tc{
|
|
{
|
|
[]string{"foo", "bar", "bizz", "buzz"},
|
|
[]string{"foo", "bar", "bizz", "buzz"},
|
|
[]string{"bing", "bong"},
|
|
},
|
|
{
|
|
[]string{"one", "two", "three"},
|
|
[]string{"one", "two", "three"},
|
|
[]string{"a", "b", "c"},
|
|
},
|
|
{
|
|
nil,
|
|
nil,
|
|
[]string{"any", "value"},
|
|
},
|
|
} {
|
|
a := AllowedListSpec{
|
|
Exact: tc.In,
|
|
}
|
|
|
|
for _, ok := range tc.True {
|
|
assert.True(t, a.ExactMatch(ok))
|
|
}
|
|
|
|
for _, ko := range tc.False {
|
|
assert.False(t, a.ExactMatch(ko))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAllowedListSpec_RegexMatch(t *testing.T) {
|
|
type tc struct {
|
|
Regex string
|
|
True []string
|
|
False []string
|
|
}
|
|
|
|
for _, tc := range []tc{
|
|
{`first-\w+-pattern`, []string{"first-date-pattern", "first-year-pattern"}, []string{"broken", "first-year", "second-date-pattern"}},
|
|
{``, nil, []string{"any", "value"}},
|
|
} {
|
|
a := AllowedListSpec{
|
|
Regex: tc.Regex,
|
|
}
|
|
|
|
for _, ok := range tc.True {
|
|
assert.True(t, a.RegexMatch(ok))
|
|
}
|
|
|
|
for _, ko := range tc.False {
|
|
assert.False(t, a.RegexMatch(ko))
|
|
}
|
|
}
|
|
}
|