1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package spiffeid_test
import (
"testing"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/stretchr/testify/assert"
)
var (
zero = spiffeid.ID{}
foo = spiffeid.RequireFromString("spiffe://foo.test")
fooA = spiffeid.RequireFromString("spiffe://foo.test/A")
fooB = spiffeid.RequireFromString("spiffe://foo.test/B")
fooC = spiffeid.RequireFromString("spiffe://foo.test/sub/C")
barA = spiffeid.RequireFromString("spiffe://bar.test/A")
)
func TestMatchAny(t *testing.T) {
testMatch(t, spiffeid.MatchAny(),
"",
"",
"",
"",
"",
"",
)
}
func TestMatchID_AgainstIDWithPath(t *testing.T) {
testMatch(t, spiffeid.MatchID(fooA),
`unexpected ID ""`,
`unexpected ID "spiffe://foo.test"`,
``,
`unexpected ID "spiffe://foo.test/B"`,
`unexpected ID "spiffe://foo.test/sub/C"`,
`unexpected ID "spiffe://bar.test/A"`,
)
}
func TestMatchID_AgainstIDWithoutPath(t *testing.T) {
testMatch(t, spiffeid.MatchID(foo),
`unexpected ID ""`,
``,
`unexpected ID "spiffe://foo.test/A"`,
`unexpected ID "spiffe://foo.test/B"`,
`unexpected ID "spiffe://foo.test/sub/C"`,
`unexpected ID "spiffe://bar.test/A"`,
)
}
func TestMatchOneOf_OnAListOfIDs(t *testing.T) {
testMatch(t, spiffeid.MatchOneOf(foo, fooB, fooC, barA),
`unexpected ID ""`,
``,
`unexpected ID "spiffe://foo.test/A"`,
``,
``,
``,
)
}
func TestMatchOneOf_OnAnEmptyListOfIDs(t *testing.T) {
testMatch(t, spiffeid.MatchOneOf(),
`unexpected ID ""`,
`unexpected ID "spiffe://foo.test"`,
`unexpected ID "spiffe://foo.test/A"`,
`unexpected ID "spiffe://foo.test/B"`,
`unexpected ID "spiffe://foo.test/sub/C"`,
`unexpected ID "spiffe://bar.test/A"`,
)
}
func TestMatchMemberOf_AgainstNonEmptyTrustDomain(t *testing.T) {
testMatch(t, spiffeid.MatchMemberOf(foo.TrustDomain()),
`unexpected trust domain ""`,
``,
``,
``,
``,
`unexpected trust domain "bar.test"`,
)
}
func TestMatchMemberOf_AgainstEmptyTrustDomain(t *testing.T) {
testMatch(t, spiffeid.MatchMemberOf(spiffeid.TrustDomain{}),
``,
`unexpected trust domain "foo.test"`,
`unexpected trust domain "foo.test"`,
`unexpected trust domain "foo.test"`,
`unexpected trust domain "foo.test"`,
`unexpected trust domain "bar.test"`,
)
}
func testMatch(t *testing.T, matcher spiffeid.Matcher, zeroErr, fooErr, fooAErr, fooBErr, fooCErr, barAErr string) {
test := func(id spiffeid.ID, expectErr string, msgAndArgs ...interface{}) {
err := matcher(id)
if expectErr != "" {
assert.EqualError(t, err, expectErr, msgAndArgs...)
} else {
assert.NoError(t, err, msgAndArgs...)
}
}
test(zero, zeroErr, "unexpected result for zero ID")
test(foo, fooErr, "unexpected result for foo ID")
test(fooA, fooAErr, "unexpected result for fooA ID")
test(fooB, fooBErr, "unexpected result for fooB ID")
test(fooC, fooCErr, "unexpected result for fooC ID")
test(barA, barAErr, "unexpected result for fooD ID")
}
|