-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathoption_test.go
More file actions
155 lines (140 loc) · 4.88 KB
/
Copy pathoption_test.go
File metadata and controls
155 lines (140 loc) · 4.88 KB
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
// Copyright 2017 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package option
import (
"testing"
"crypto/tls"
"math/big"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/oauth2/google"
"google.golang.org/api/internal"
"google.golang.org/grpc"
)
// Below is a dummy certificate/key pair taken from
// https://golang.org/src/crypto/tls/tls_test.go
const certPEM = `-----BEGIN CERTIFICATE-----
MIIB0zCCAX2gAwIBAgIJAI/M7BYjwB+uMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTIwOTEyMjE1MjAyWhcNMTUwOTEyMjE1MjAyWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANLJ
hPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wok/4xIA+ui35/MmNa
rtNuC+BdZ1tMuVCPFZcCAwEAAaNQME4wHQYDVR0OBBYEFJvKs8RfJaXTH08W+SGv
zQyKn0H8MB8GA1UdIwQYMBaAFJvKs8RfJaXTH08W+SGvzQyKn0H8MAwGA1UdEwQF
MAMBAf8wDQYJKoZIhvcNAQEFBQADQQBJlffJHybjDGxRMqaRmDhX0+6v02TUKZsW
r5QuVbpQhH6u+0UgcW0jp9QwpxoPTLTWGXEWBBBurxFwiCBhkQ+V
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
MIIBOwIBAAJBANLJhPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wo
k/4xIA+ui35/MmNartNuC+BdZ1tMuVCPFZcCAwEAAQJAEJ2N+zsR0Xn8/Q6twa4G
6OB1M1WO+k+ztnX/1SvNeWu8D6GImtupLTYgjZcHufykj09jiHmjHx8u8ZZB/o1N
MQIhAPW+eyZo7ay3lMz1V01WVjNKK9QSn1MJlb06h/LuYv9FAiEA25WPedKgVyCW
SmUwbPw8fnTcpqDWE3yTO3vKcebqMSsCIBF3UmVue8YU3jybC3NxuXq3wNm34R8T
xVLHwDXh/6NJAiEAl2oHGGLz64BuAfjKrqwz7qMYr9HCLIe/YsoWq/olzScCIQDi
D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g==
-----END PRIVATE KEY-----
`
// Check that the slice passed into WithScopes is copied.
func TestCopyScopes(t *testing.T) {
o := &internal.DialSettings{}
scopes := []string{"a", "b"}
WithScopes(scopes...).Apply(o)
// Modify after using.
scopes[1] = "c"
if o.Scopes[0] != "a" || o.Scopes[1] != "b" {
t.Errorf("want ['a', 'b'], got %+v", o.Scopes)
}
}
func TestApply(t *testing.T) {
conn := &grpc.ClientConn{}
opts := []ClientOption{
WithEndpoint("https://example.com:443"),
WithScopes("a"), // the next WithScopes should overwrite this one
WithScopes("https://example.com/auth/helloworld", "https://example.com/auth/otherthing"),
WithGRPCConn(conn),
WithUserAgent("ua"),
WithCredentialsFile("service-account.json"),
WithCredentialsJSON([]byte(`{some: "json"}`)),
WithCredentials(&google.DefaultCredentials{ProjectID: "p"}),
WithAPIKey("api-key"),
WithAudiences("https://example.com/"),
WithQuotaProject("user-project"),
WithRequestReason("Request Reason"),
WithTelemetryDisabled(),
WithUniverseDomain("universe.com"),
}
var got internal.DialSettings
for _, opt := range opts {
opt.Apply(&got)
}
want := internal.DialSettings{
Scopes: []string{"https://example.com/auth/helloworld", "https://example.com/auth/otherthing"},
UserAgent: "ua",
Endpoint: "https://example.com:443",
GRPCConn: conn,
Credentials: &google.DefaultCredentials{ProjectID: "p"},
CredentialsFile: "service-account.json",
CredentialsJSON: []byte(`{some: "json"}`),
APIKey: "api-key",
Audiences: []string{"https://example.com/"},
QuotaProject: "user-project",
RequestReason: "Request Reason",
TelemetryDisabled: true,
UniverseDomain: "universe.com",
}
ignore := []cmp.Option{
cmpopts.IgnoreUnexported(grpc.ClientConn{}),
cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain"),
}
if !cmp.Equal(got, want, ignore...) {
t.Error(cmp.Diff(got, want, ignore...))
}
}
func mockClientCertSource(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
cert, _ := tls.X509KeyPair([]byte(certPEM), []byte(certPEM))
return &cert, nil
}
func TestApplyClientCertSource(t *testing.T) {
opts := []ClientOption{
WithClientCertSource(mockClientCertSource),
}
var got internal.DialSettings
for _, opt := range opts {
opt.Apply(&got)
}
want := internal.DialSettings{
ClientCertSource: mockClientCertSource,
}
// Functions cannot be compared in Golang for equality, so we will compare the output of the functions instead.
certGot, err := got.ClientCertSource(nil)
if err != nil {
t.Error(err)
}
certWant, err := want.ClientCertSource(nil)
if err != nil {
t.Error(err)
}
if !cmp.Equal(certGot, certWant, cmpopts.IgnoreUnexported(big.Int{}), cmpopts.IgnoreFields(tls.Certificate{}, "Leaf")) {
t.Error(cmp.Diff(certGot, certWant, cmpopts.IgnoreUnexported(big.Int{}), cmpopts.IgnoreFields(tls.Certificate{}, "Leaf")))
}
}
func TestOpenTelemetryOpts(t *testing.T) {
otelOpts := []any{
"non-otelhttp-otelgrpc-option",
}
opts := []ClientOption{
WithOpenTelemetryOpts(otelOpts...),
}
var got internal.DialSettings
for _, opt := range opts {
opt.Apply(&got)
}
want := internal.DialSettings{
OpenTelemetryOpts: []any{},
}
if cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
}