/
cba_test.go
357 lines (340 loc) · 9.49 KB
/
cba_test.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2020 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package internal
import (
"crypto/tls"
"net/http"
"os"
"testing"
"time"
)
const (
testMTLSEndpoint = "test.mtls.endpoint"
testRegularEndpoint = "test.endpoint"
testOverrideEndpoint = "test.override.endpoint"
)
var dummyClientCertSource = func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) { return nil, nil }
func TestGetEndpoint(t *testing.T) {
testCases := []struct {
UserEndpoint string
DefaultEndpoint string
Want string
WantErr bool
}{
{
DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
Want: "https://foo.googleapis.com/bar/baz",
},
{
UserEndpoint: "myhost:3999",
DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
Want: "https://myhost:3999/bar/baz",
},
{
UserEndpoint: "https://host/path/to/bar",
DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
Want: "https://host/path/to/bar",
},
{
UserEndpoint: "host:123",
DefaultEndpoint: "",
Want: "host:123",
},
{
UserEndpoint: "host:123",
DefaultEndpoint: "default:443",
Want: "host:123",
},
{
UserEndpoint: "host:123",
DefaultEndpoint: "default:443/bar/baz",
Want: "host:123/bar/baz",
},
}
for _, tc := range testCases {
got, err := getEndpoint(&DialSettings{
Endpoint: tc.UserEndpoint,
DefaultEndpoint: tc.DefaultEndpoint,
}, nil)
if tc.WantErr && err == nil {
t.Errorf("want err, got nil err")
continue
}
if !tc.WantErr && err != nil {
t.Errorf("want nil err, got %v", err)
continue
}
if tc.Want != got {
t.Errorf("getEndpoint(%q, %q): got %v; want %v", tc.UserEndpoint, tc.DefaultEndpoint, got, tc.Want)
}
}
}
func TestGetEndpointWithClientCertSource(t *testing.T) {
testCases := []struct {
UserEndpoint string
DefaultEndpoint string
DefaultMTLSEndpoint string
Want string
WantErr bool
}{
{
DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
DefaultMTLSEndpoint: "https://foo.mtls.googleapis.com/bar/baz",
Want: "https://foo.mtls.googleapis.com/bar/baz",
},
{
DefaultEndpoint: "https://staging-foo.sandbox.googleapis.com/bar/baz",
DefaultMTLSEndpoint: "https://staging-foo.mtls.sandbox.googleapis.com/bar/baz",
Want: "https://staging-foo.mtls.sandbox.googleapis.com/bar/baz",
},
{
UserEndpoint: "myhost:3999",
DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
Want: "https://myhost:3999/bar/baz",
},
{
UserEndpoint: "https://host/path/to/bar",
DefaultEndpoint: "https://foo.googleapis.com/bar/baz",
Want: "https://host/path/to/bar",
},
{
UserEndpoint: "host:port",
DefaultEndpoint: "",
Want: "host:port",
},
}
for _, tc := range testCases {
got, err := getEndpoint(&DialSettings{
Endpoint: tc.UserEndpoint,
DefaultEndpoint: tc.DefaultEndpoint,
DefaultMTLSEndpoint: tc.DefaultMTLSEndpoint,
}, dummyClientCertSource)
if tc.WantErr && err == nil {
t.Errorf("want err, got nil err")
continue
}
if !tc.WantErr && err != nil {
t.Errorf("want nil err, got %v", err)
continue
}
if tc.Want != got {
t.Errorf("getEndpoint(%q, %q): got %v; want %v", tc.UserEndpoint, tc.DefaultEndpoint, got, tc.Want)
}
}
}
func TestGetGRPCTransportConfigAndEndpoint(t *testing.T) {
testCases := []struct {
Desc string
InputSettings *DialSettings
S2ARespFunc func() (string, error)
MTLSEnabled func() bool
WantEndpoint string
}{
{
"no client cert, endpoint is MTLS enabled, S2A address not empty",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
},
validConfigResp,
func() bool { return true },
testMTLSEndpoint,
},
{
"has client cert",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
ClientCertSource: dummyClientCertSource,
},
validConfigResp,
func() bool { return true },
testMTLSEndpoint,
},
{
"no client cert, endpoint is not MTLS enabled",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
},
validConfigResp,
func() bool { return false },
testRegularEndpoint,
},
{
"no client cert, endpoint is MTLS enabled, S2A address empty",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
},
invalidConfigResp,
func() bool { return true },
testRegularEndpoint,
},
{
"no client cert, endpoint is MTLS enabled, S2A address not empty, override endpoint",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
Endpoint: testOverrideEndpoint,
},
validConfigResp,
func() bool { return true },
testOverrideEndpoint,
},
}
defer setupTest()()
for _, tc := range testCases {
httpGetMetadataMTLSConfig = tc.S2ARespFunc
mtlsEndpointEnabledForS2A = tc.MTLSEnabled
if tc.InputSettings.ClientCertSource != nil {
os.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true")
} else {
os.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
}
_, endpoint, _ := GetGRPCTransportConfigAndEndpoint(tc.InputSettings)
if tc.WantEndpoint != endpoint {
t.Errorf("%s: want endpoint: [%s], got [%s]", tc.Desc, tc.WantEndpoint, endpoint)
}
// Let the cached MTLS config expire at the end of each test case.
time.Sleep(2 * time.Millisecond)
}
}
func TestGetHTTPTransportConfigAndEndpoint(t *testing.T) {
testCases := []struct {
Desc string
InputSettings *DialSettings
S2ARespFunc func() (string, error)
MTLSEnabled func() bool
WantEndpoint string
DialFuncNil bool
}{
{
"no client cert, endpoint is MTLS enabled, S2A address not empty",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
},
validConfigResp,
func() bool { return true },
testMTLSEndpoint,
false,
},
{
"has client cert",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
ClientCertSource: dummyClientCertSource,
},
validConfigResp,
func() bool { return true },
testMTLSEndpoint,
true,
},
{
"no client cert, endpoint is not MTLS enabled",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
},
validConfigResp,
func() bool { return false },
testRegularEndpoint,
true,
},
{
"no client cert, endpoint is MTLS enabled, S2A address empty",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
},
invalidConfigResp,
func() bool { return true },
testRegularEndpoint,
true,
},
{
"no client cert, endpoint is MTLS enabled, S2A address not empty, override endpoint",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
Endpoint: testOverrideEndpoint,
},
validConfigResp,
func() bool { return true },
testOverrideEndpoint,
false,
},
{
"no client cert, S2A address not empty, but DefaultMTLSEndpoint is not set",
&DialSettings{
DefaultMTLSEndpoint: "",
DefaultEndpoint: testRegularEndpoint,
},
validConfigResp,
func() bool { return true },
testRegularEndpoint,
true,
},
{
"no client cert, S2A address not empty, override endpoint is set",
&DialSettings{
DefaultMTLSEndpoint: "",
Endpoint: testOverrideEndpoint,
},
validConfigResp,
func() bool { return true },
testOverrideEndpoint,
false,
},
{
"no client cert, endpoint is MTLS enabled, S2A address not empty, custom HTTP client",
&DialSettings{
DefaultMTLSEndpoint: testMTLSEndpoint,
DefaultEndpoint: testRegularEndpoint,
HTTPClient: http.DefaultClient,
},
validConfigResp,
func() bool { return true },
testRegularEndpoint,
true,
},
}
defer setupTest()()
for _, tc := range testCases {
httpGetMetadataMTLSConfig = tc.S2ARespFunc
mtlsEndpointEnabledForS2A = tc.MTLSEnabled
if tc.InputSettings.ClientCertSource != nil {
os.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true")
} else {
os.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
}
_, dialFunc, endpoint, _ := GetHTTPTransportConfigAndEndpoint(tc.InputSettings)
if tc.WantEndpoint != endpoint {
t.Errorf("%s: want endpoint: [%s], got [%s]", tc.Desc, tc.WantEndpoint, endpoint)
}
if want, got := tc.DialFuncNil, dialFunc == nil; want != got {
t.Errorf("%s: expecting returned dialFunc is nil: [%v], got [%v]", tc.Desc, tc.DialFuncNil, got)
}
// Let MTLS config expire at end of each test case.
time.Sleep(2 * time.Millisecond)
}
}
func setupTest() func() {
oldDefaultMTLSEnabled := mtlsEndpointEnabledForS2A
oldHTTPGet := httpGetMetadataMTLSConfig
oldExpiry := configExpiry
oldUseS2A := os.Getenv(googleAPIUseS2AEnv)
oldUseClientCert := os.Getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE")
configExpiry = time.Millisecond
os.Setenv(googleAPIUseS2AEnv, "true")
return func() {
httpGetMetadataMTLSConfig = oldHTTPGet
mtlsEndpointEnabledForS2A = oldDefaultMTLSEnabled
configExpiry = oldExpiry
os.Setenv(googleAPIUseS2AEnv, oldUseS2A)
os.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", oldUseClientCert)
}
}