-
Notifications
You must be signed in to change notification settings - Fork 27
/
dsn_test.go
275 lines (246 loc) · 8.07 KB
/
dsn_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
package gohive
import (
"encoding/base64"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
// This certificate was sourced from the examples in go's documentation
// for [x509 Certificate.Verify].
//
// [x509 Certificate.Verify]: https://pkg.go.dev/crypto/x509#example-Certificate.Verify
rootPEM = `-----BEGIN CERTIFICATE-----
MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR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dUDqB1O6gyHA43LL5Z/qHQF1hwFGPa4NrzQU6
yuGnBXj8ytqU0CwIPX4WecigUCAkVDNx
-----END CERTIFICATE-----`
)
func TestParseDSNWithSessionConf(t *testing.T) {
sc := make(map[string]string)
sc["mapreduce_job_quenename"] = "mr"
cfg := &Config{
User: "usr",
Passwd: "ps@wd",
Addr: "hiveserver",
DBName: "mydb",
Auth: "PLAIN",
Batch: 200,
SessionCfg: sc,
}
dsn := cfg.FormatDSN()
assert.Equal(t, dsn, "usr:ps@wd@hiveserver/mydb?batch=200&auth=PLAIN&session.mapreduce_job_quenename=mr")
cfg2, e := ParseDSN(dsn)
assert.Nil(t, e)
assert.Equal(t, cfg.User, cfg2.User)
assert.Equal(t, cfg.Passwd, cfg2.Passwd)
assert.Equal(t, cfg.Addr, cfg2.Addr)
assert.Equal(t, cfg.DBName, cfg2.DBName)
assert.Equal(t, cfg.Auth, cfg2.Auth)
assert.Equal(t, cfg.Batch, cfg2.Batch)
sc, sc2 := cfg.SessionCfg, cfg2.SessionCfg
assert.Equal(t, len(sc), len(sc2))
for k, v := range sc {
v2, found := sc2[k]
assert.True(t, found)
assert.Equal(t, v, v2)
}
}
func TestParseDSNWithAuth(t *testing.T) {
cfg, e := ParseDSN("root:[email protected]/mnist?auth=PLAIN")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "root")
assert.Equal(t, cfg.Passwd, "root")
assert.Equal(t, cfg.Addr, "127.0.0.1")
assert.Equal(t, cfg.DBName, "mnist")
assert.Equal(t, cfg.Auth, "PLAIN")
assert.Equal(t, cfg.Batch, 10000)
cfg, e = ParseDSN("[email protected]/mnist")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "root")
assert.Equal(t, cfg.Passwd, "")
assert.Equal(t, cfg.Addr, "127.0.0.1")
assert.Equal(t, cfg.DBName, "mnist")
assert.Equal(t, cfg.Auth, "NOSASL")
}
func TestParseDSNWithDBName(t *testing.T) {
cfg, e := ParseDSN("root:[email protected]/mnist")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "root")
assert.Equal(t, cfg.Passwd, "root")
assert.Equal(t, cfg.Addr, "127.0.0.1")
assert.Equal(t, cfg.DBName, "mnist")
cfg, e = ParseDSN("[email protected]/mnist")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "root")
assert.Equal(t, cfg.Passwd, "")
assert.Equal(t, cfg.Addr, "127.0.0.1")
assert.Equal(t, cfg.DBName, "mnist")
cfg, e = ParseDSN("127.0.0.1/mnist")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "")
assert.Equal(t, cfg.Passwd, "")
assert.Equal(t, cfg.Addr, "127.0.0.1")
assert.Equal(t, cfg.DBName, "mnist")
}
func TestParseDSNWithoutDBName(t *testing.T) {
cfg, e := ParseDSN("root:[email protected]")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "root")
assert.Equal(t, cfg.Passwd, "root")
assert.Equal(t, cfg.Addr, "127.0.0.1")
cfg, e = ParseDSN("[email protected]")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "root")
assert.Equal(t, cfg.Passwd, "")
assert.Equal(t, cfg.Addr, "127.0.0.1")
cfg, e = ParseDSN("127.0.0.1")
assert.Nil(t, e)
assert.Equal(t, cfg.User, "")
assert.Equal(t, cfg.Passwd, "")
assert.Equal(t, cfg.Addr, "127.0.0.1")
}
func TestParseDSNWithTLSConfig(t *testing.T) {
b64 := base64.URLEncoding.EncodeToString([]byte(rootPEM))
t.Run("no tls", func(t *testing.T) {
cfg, e := ParseDSN("127.0.0.1")
require.NoError(t, e)
require.Nil(t, cfg.TLSCfg)
})
t.Run("one ca", func(t *testing.T) {
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca=%s", b64))
require.NoError(t, e)
require.NotNil(t, cfg.TLSCfg)
require.Len(t, cfg.TLSCfg.RootCAs, 1)
require.Equal(t, cfg.TLSCfg.RootCAs[0], rootPEM)
})
t.Run("two cas", func(t *testing.T) {
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca=%s&tls.root_ca=%s", b64, b64))
require.NoError(t, e)
require.NotNil(t, cfg.TLSCfg)
require.Len(t, cfg.TLSCfg.RootCAs, 2)
require.Equal(t, cfg.TLSCfg.RootCAs[0], rootPEM)
require.Equal(t, cfg.TLSCfg.RootCAs[1], rootPEM)
})
t.Run("one ca fiel", func(t *testing.T) {
file := "cert.pem"
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca_file=%s", file))
require.NoError(t, e)
require.NotNil(t, cfg.TLSCfg)
require.Len(t, cfg.TLSCfg.RootCAFiles, 1)
require.Equal(t, cfg.TLSCfg.RootCAFiles[0], file)
})
t.Run("two ca files", func(t *testing.T) {
file := "cert.pem"
file2 := "cert2.pem"
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca_file=%s&tls.root_ca_file=%s", file, file2))
require.NoError(t, e)
require.NotNil(t, cfg.TLSCfg)
require.Len(t, cfg.TLSCfg.RootCAFiles, 2)
require.Equal(t, cfg.TLSCfg.RootCAFiles[0], file)
require.Equal(t, cfg.TLSCfg.RootCAFiles[1], file2)
})
t.Run("insecure skip verify", func(t *testing.T) {
cfg, e := ParseDSN("127.0.0.1?tls.insecure_skip_verify=true")
require.NoError(t, e)
require.NotNil(t, cfg.TLSCfg)
require.True(t, cfg.TLSCfg.InsecureSkipVerify)
})
}
func TestFormatDSNWithDBName(t *testing.T) {
ds := "user:[email protected]/mnist?batch=100000&auth=NOSASL"
cfg, e := ParseDSN(ds)
assert.Nil(t, e)
ds2 := cfg.FormatDSN()
assert.Equal(t, ds2, ds)
}
func TestFormatDSNWithoutDBName(t *testing.T) {
ds := "user:[email protected]?batch=100&auth=NOSASL"
cfg, e := ParseDSN(ds)
assert.Nil(t, e)
ds2 := cfg.FormatDSN()
assert.Equal(t, ds2, ds)
}
func TestFormatDSNWithTLSConfig(t *testing.T) {
b64 := base64.URLEncoding.EncodeToString([]byte(rootPEM))
t.Run("no tls", func(t *testing.T) {
require.Equal(t,
":@127.0.0.1?batch=0",
(&Config{
Addr: "127.0.0.1",
}).FormatDSN())
})
t.Run("one ca", func(t *testing.T) {
require.Equal(t,
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca=%s", b64),
(&Config{
Addr: "127.0.0.1",
TLSCfg: &TLSConfig{
RootCAs: []string{rootPEM},
},
}).FormatDSN())
})
t.Run("two cas", func(t *testing.T) {
require.Equal(t,
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca=%s&tls.root_ca=%s", b64, b64),
(&Config{
Addr: "127.0.0.1",
TLSCfg: &TLSConfig{
RootCAs: []string{rootPEM, rootPEM},
},
}).FormatDSN())
})
t.Run("one ca file", func(t *testing.T) {
file := "cert.pem"
require.Equal(t,
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca_file=%s", file),
(&Config{
Addr: "127.0.0.1",
TLSCfg: &TLSConfig{
RootCAFiles: []string{file},
},
}).FormatDSN())
})
t.Run("two ca files", func(t *testing.T) {
file := "cert.pem"
file2 := "cert2.pem"
require.Equal(t,
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca_file=%s&tls.root_ca_file=%s", file, file2),
(&Config{
Addr: "127.0.0.1",
TLSCfg: &TLSConfig{
RootCAFiles: []string{file, file2},
},
}).FormatDSN())
})
t.Run("insecure skip verify", func(t *testing.T) {
require.Equal(t,
":@127.0.0.1?batch=0&tls.insecure_skip_verify=true",
(&Config{
Addr: "127.0.0.1",
TLSCfg: &TLSConfig{
InsecureSkipVerify: true,
},
}).FormatDSN())
})
}