1
18
19 package s2a
20
21 import (
22 "context"
23 "testing"
24
25 commonpb "github.com/google/s2a-go/internal/proto/common_go_proto"
26 "google.golang.org/grpc/credentials"
27 "google.golang.org/grpc/peer"
28 )
29
30 func TestAuthInfoFromContext(t *testing.T) {
31 ctx := context.Background()
32 s2aAuthInfo := &fakeS2AAuthInfo{}
33 p := &peer.Peer{
34 AuthInfo: s2aAuthInfo,
35 }
36 for _, tc := range []struct {
37 desc string
38 ctx context.Context
39 success bool
40 out AuthInfo
41 }{
42 {
43 "working case",
44 peer.NewContext(ctx, p),
45 true,
46 s2aAuthInfo,
47 },
48 } {
49 authInfo, err := AuthInfoFromContext(tc.ctx)
50 if got, want := (err == nil), tc.success; got != want {
51 t.Errorf("%v: AuthInfoFromContext(_)=(err=nil)=%v, want %v", tc.desc, got, want)
52 }
53 if got, want := authInfo, tc.out; got != want {
54 t.Errorf("%v:, AuthInfoFromContext(_)=(%v, _), want (%v, _)", tc.desc, got, want)
55 }
56 }
57 }
58
59 func TestAuthInfoFromPeer(t *testing.T) {
60 s2aAuthInfo := &fakeS2AAuthInfo{}
61 p := &peer.Peer{
62 AuthInfo: s2aAuthInfo,
63 }
64 for _, tc := range []struct {
65 desc string
66 p *peer.Peer
67 success bool
68 out AuthInfo
69 }{
70 {
71 "working case",
72 p,
73 true,
74 s2aAuthInfo,
75 },
76 } {
77 authInfo, err := AuthInfoFromPeer(tc.p)
78 if got, want := (err == nil), tc.success; got != want {
79 t.Errorf("%v: AuthInfoFromPeer(_)=(err=nil)=%v, want %v", tc.desc, got, want)
80 }
81 if got, want := authInfo, tc.out; got != want {
82 t.Errorf("%v:, AuthInfoFromPeer(_)=(%v, _), want (%v, _)", tc.desc, got, want)
83 }
84 }
85 }
86
87 type fakeS2AAuthInfo struct{}
88
89 func (*fakeS2AAuthInfo) AuthType() string { return "" }
90 func (*fakeS2AAuthInfo) ApplicationProtocol() string { return "" }
91 func (*fakeS2AAuthInfo) TLSVersion() commonpb.TLSVersion { return commonpb.TLSVersion_TLS1_3 }
92 func (*fakeS2AAuthInfo) Ciphersuite() commonpb.Ciphersuite {
93 return commonpb.Ciphersuite_AES_128_GCM_SHA256
94 }
95 func (*fakeS2AAuthInfo) PeerIdentity() *commonpb.Identity { return nil }
96 func (*fakeS2AAuthInfo) LocalIdentity() *commonpb.Identity { return nil }
97 func (*fakeS2AAuthInfo) PeerCertFingerprint() []byte { return nil }
98 func (*fakeS2AAuthInfo) LocalCertFingerprint() []byte { return nil }
99 func (*fakeS2AAuthInfo) IsHandshakeResumed() bool { return false }
100 func (*fakeS2AAuthInfo) SecurityLevel() credentials.SecurityLevel {
101 return credentials.PrivacyAndIntegrity
102 }
103
View as plain text