Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xds: add unit tests for HandshakeInfo.Equal #7638

Merged
merged 7 commits into from
Sep 26, 2024
72 changes: 72 additions & 0 deletions internal/credentials/xds/handshake_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ import (
"regexp"
"testing"

"google.golang.org/grpc/credentials/tls/certprovider"
"google.golang.org/grpc/internal/xds/matcher"
)

type testCertProvider struct {
certprovider.Provider
}

func TestDNSMatch(t *testing.T) {
tests := []struct {
desc string
Expand Down Expand Up @@ -300,3 +305,70 @@ func TestMatchingSANExists_Success(t *testing.T) {
func newStringP(s string) *string {
return &s
}

func TestEqual(t *testing.T) {
tests := []struct {
desc string
hi1 *HandshakeInfo
hi2 *HandshakeInfo
wantMatch bool
}{
{
desc: "both HandshakeInfo are nil",
hi1: nil,
hi2: nil,
wantMatch: true,
},
{
desc: "one HandshakeInfo is nil",
hi1: nil,
hi2: NewHandshakeInfo(testCertProvider{}, nil, nil, false),
wantMatch: false,
},
{
desc: "same providers, same SAN matchers",
hi1: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, []matcher.StringMatcher{
matcher.StringMatcherForTesting(newStringP("foo.com"), nil, nil, nil, nil, false),
}, false),
hi2: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, []matcher.StringMatcher{
matcher.StringMatcherForTesting(newStringP("foo.com"), nil, nil, nil, nil, false),
}, false),
wantMatch: true,
},
{
desc: "same providers, different SAN matchers",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case seems to be subset of the next one. So, we can remove this test case.

hi1: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, []matcher.StringMatcher{
matcher.StringMatcherForTesting(newStringP("foo.com"), nil, nil, nil, nil, false),
}, false),
hi2: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, []matcher.StringMatcher{
matcher.StringMatcherForTesting(newStringP("bar.com"), nil, nil, nil, nil, false),
}, false),
wantMatch: false,
},
{
desc: "same SAN matchers with different content",
hi1: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, []matcher.StringMatcher{
matcher.StringMatcherForTesting(newStringP("foo.com"), nil, nil, nil, nil, false),
}, false),
hi2: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, []matcher.StringMatcher{
matcher.StringMatcherForTesting(newStringP("foo.com"), nil, nil, nil, nil, false),
matcher.StringMatcherForTesting(newStringP("bar.com"), nil, nil, nil, nil, false),
}, false),
wantMatch: false,
},
{
desc: "different requireClientCert flags",
hi1: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, nil, true),
hi2: NewHandshakeInfo(testCertProvider{}, testCertProvider{}, nil, false),
wantMatch: false,
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
if gotMatch := test.hi1.Equal(test.hi2); gotMatch != test.wantMatch {
t.Errorf("hi1.Equal(hi2) = %v; wantMatch %v", gotMatch, test.wantMatch)
}
})
}
}