Skip to content

Commit

Permalink
internal/credentials/xds: add unit tests for HandshakeInfo.Equal (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
janardhanvissa authored Sep 26, 2024
1 parent 3196f7a commit 9affdbb
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 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,88 @@ 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: "different root providers",
hi1: NewHandshakeInfo(&testCertProvider{}, nil, nil, false),
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",
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,
},
{
desc: "same identity provider, different root provider",
hi1: NewHandshakeInfo(&testCertProvider{}, testCertProvider{}, nil, false),
hi2: NewHandshakeInfo(&testCertProvider{}, testCertProvider{}, nil, false),
wantMatch: false,
},
{
desc: "different identity provider, same root provider",
hi1: NewHandshakeInfo(testCertProvider{}, &testCertProvider{}, nil, false),
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)
}
})
}
}

0 comments on commit 9affdbb

Please sign in to comment.