@@ -30,6 +30,7 @@ import (
30
30
"google.golang.org/grpc/codes"
31
31
"google.golang.org/grpc/credentials"
32
32
"google.golang.org/grpc/credentials/insecure"
33
+ "google.golang.org/grpc/credentials/local"
33
34
"google.golang.org/grpc/internal/stubserver"
34
35
"google.golang.org/grpc/metadata"
35
36
"google.golang.org/grpc/status"
@@ -57,45 +58,88 @@ func authorityChecker(ctx context.Context, wantAuthority string) error {
57
58
return nil
58
59
}
59
60
60
- // Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies
61
- // that the provided authority is correctly propagated to the server when a
62
- // correct authority is used.
63
- func (s ) TestCorrectAuthorityWithTLSCreds (t * testing.T ) {
61
+ func loadTLSCreds (t * testing.T ) (grpc.ServerOption , grpc.DialOption ) {
62
+ t .Helper ()
64
63
cert , err := tls .LoadX509KeyPair (testdata .Path ("x509/server1_cert.pem" ), testdata .Path ("x509/server1_key.pem" ))
65
64
if err != nil {
66
- t .Fatalf ("Failed to load key pair: %s" , err )
65
+ t .Fatalf ("Failed to load key pair: %v" , err )
66
+ return nil , nil
67
67
}
68
- creds , err := credentials .NewClientTLSFromFile (testdata .Path ("x509/server_ca_cert.pem" ), "x.test.example.com" )
68
+ serverCreds := grpc .Creds (credentials .NewServerTLSFromCert (& cert ))
69
+
70
+ clientCreds , err := credentials .NewClientTLSFromFile (testdata .Path ("x509/server_ca_cert.pem" ), "x.test.example.com" )
69
71
if err != nil {
70
- t .Fatalf ("Failed to create credentials %v" , err )
72
+ t .Fatalf ("Failed to create client credentials: %v" , err )
71
73
}
74
+ return serverCreds , grpc .WithTransportCredentials (clientCreds )
75
+ }
76
+
77
+ // Tests the scenario where the `grpc.CallAuthority` call option is used with
78
+ // different transport credentials. The test verifies that the specified
79
+ // authority is correctly propagated to the serve when a correct authority is
80
+ // used.
81
+ func (s ) TestCorrectAuthorityWithCreds (t * testing.T ) {
72
82
const authority = "auth.test.example.com"
73
- ss := & stubserver.StubServer {
74
- EmptyCallF : func (ctx context.Context , _ * testpb.Empty ) (* testpb.Empty , error ) {
75
- if err := authorityChecker (ctx , authority ); err != nil {
76
- return nil , err
77
- }
78
- return & testpb.Empty {}, nil
83
+
84
+ tests := []struct {
85
+ name string
86
+ creds func (t * testing.T ) (grpc.ServerOption , grpc.DialOption )
87
+ expectedAuth string
88
+ }{
89
+ {
90
+ name : "Insecure" ,
91
+ creds : func (t * testing.T ) (grpc.ServerOption , grpc.DialOption ) {
92
+ c := insecure .NewCredentials ()
93
+ return grpc .Creds (c ), grpc .WithTransportCredentials (c )
94
+ },
95
+ expectedAuth : authority ,
96
+ },
97
+ {
98
+ name : "Local" ,
99
+ creds : func (t * testing.T ) (grpc.ServerOption , grpc.DialOption ) {
100
+ c := local .NewCredentials ()
101
+ return grpc .Creds (c ), grpc .WithTransportCredentials (c )
102
+ },
103
+ expectedAuth : authority ,
104
+ },
105
+ {
106
+ name : "TLS" ,
107
+ creds : func (t * testing.T ) (grpc.ServerOption , grpc.DialOption ) {
108
+ return loadTLSCreds (t )
109
+ },
110
+ expectedAuth : authority ,
79
111
},
80
112
}
81
- if err := ss .StartServer (grpc .Creds (credentials .NewServerTLSFromCert (& cert ))); err != nil {
82
- t .Fatalf ("Error starting endpoint server: %v" , err )
83
- }
84
- defer ss .Stop ()
85
113
86
- cc , err := grpc .NewClient (ss .Address , grpc .WithTransportCredentials (creds ))
87
- if err != nil {
88
- t .Fatalf ("grpc.NewClient(%q) = %v" , ss .Address , err )
89
- }
90
- defer cc .Close ()
114
+ for _ , tt := range tests {
115
+ t .Run (tt .name , func (t * testing.T ) {
116
+ ss := & stubserver.StubServer {
117
+ EmptyCallF : func (ctx context.Context , _ * testpb.Empty ) (* testpb.Empty , error ) {
118
+ if err := authorityChecker (ctx , tt .expectedAuth ); err != nil {
119
+ return nil , err
120
+ }
121
+ return & testpb.Empty {}, nil
122
+ },
123
+ }
124
+ serverOpt , dialOpt := tt .creds (t )
125
+ if err := ss .StartServer (serverOpt ); err != nil {
126
+ t .Fatalf ("Error starting endpoint server: %v" , err )
127
+ }
128
+ defer ss .Stop ()
91
129
92
- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
93
- defer cancel ()
130
+ cc , err := grpc .NewClient (ss .Address , dialOpt )
131
+ if err != nil {
132
+ t .Fatalf ("grpc.NewClient(%q) = %v" , ss .Address , err )
133
+ }
134
+ defer cc .Close ()
94
135
95
- if _ , err = testgrpc .NewTestServiceClient (cc ).EmptyCall (ctx , & testpb.Empty {}, grpc .CallAuthority (authority )); status .Code (err ) != codes .OK {
96
- t .Fatalf ("EmptyCall() returned status %v, want %v" , status .Code (err ), codes .OK )
136
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
137
+ defer cancel ()
138
+ if _ , err = testgrpc .NewTestServiceClient (cc ).EmptyCall (ctx , & testpb.Empty {}, grpc .CallAuthority (tt .expectedAuth )); err != nil {
139
+ t .Fatalf ("EmptyCall() rpc failed: %v" , err )
140
+ }
141
+ })
97
142
}
98
-
99
143
}
100
144
101
145
// Tests the `grpc.CallAuthority` option with TLS credentials. This test verifies
@@ -143,38 +187,6 @@ func (s) TestIncorrectAuthorityWithTLS(t *testing.T) {
143
187
}
144
188
}
145
189
146
- // Tests the scenario where the `grpc.CallAuthority` call option is used with
147
- // insecure transport credentials. The test verifies that the specified
148
- // authority is correctly propagated to the server.
149
- func (s ) TestAuthorityCallOptionWithInsecureCreds (t * testing.T ) {
150
- const authority = "test.server.name"
151
-
152
- ss := & stubserver.StubServer {
153
- EmptyCallF : func (ctx context.Context , _ * testpb.Empty ) (* testpb.Empty , error ) {
154
- if err := authorityChecker (ctx , authority ); err != nil {
155
- return nil , err
156
- }
157
- return & testpb.Empty {}, nil
158
- },
159
- }
160
- if err := ss .Start (nil ); err != nil {
161
- t .Fatalf ("Error starting endpoint server: %v" , err )
162
- }
163
- defer ss .Stop ()
164
-
165
- cc , err := grpc .NewClient (ss .Address , grpc .WithTransportCredentials (insecure .NewCredentials ()))
166
- if err != nil {
167
- t .Fatalf ("grpc.NewClient(%q) = %v" , ss .Address , err )
168
- }
169
- defer cc .Close ()
170
-
171
- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
172
- defer cancel ()
173
- if _ , err = testgrpc .NewTestServiceClient (cc ).EmptyCall (ctx , & testpb.Empty {}, grpc .CallAuthority (authority )); err != nil {
174
- t .Fatalf ("EmptyCall() rpc failed: %v" , err )
175
- }
176
- }
177
-
178
190
// testAuthInfoNoValidator implements only credentials.AuthInfo and not
179
191
// credentials.AuthorityValidator.
180
192
type testAuthInfoNoValidator struct {}
0 commit comments