-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
270 lines (230 loc) · 7.89 KB
/
main_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
package main
import (
"testing"
)
var ca = `-----BEGIN CERTIFICATE-----
MIIDBjCCAe6gAwIBAgIBATANBgkqhkiG9w0BAQsFADA8MTowOAYDVQQDDDFNeVNR
TF9TZXJ2ZXJfNS43LjMzX0F1dG9fR2VuZXJhdGVkX0NBX0NlcnRpZmljYXRlMB4X
DTIxMDYyNDEzNTM1OVoXDTMxMDYyMjEzNTM1OVowPDE6MDgGA1UEAwwxTXlTUUxf
U2VydmVyXzUuNy4zM19BdXRvX0dlbmVyYXRlZF9DQV9DZXJ0aWZpY2F0ZTCCASIw
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANoxVz08XCRnA6r44LoqNnW2ODE+
F+WrFhsXvUm8eyIBzTcbhc8jrR4bBjx5JmtVTf4aSySB4YYqswqTkIDBvSyITCAq
4nfTn/EgBH6W0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S
+8Qq6QKBgBysxdqOzgYoiuWqFDn7AxbWBZiqUTTDir5G8j/i+6cteywpurv9cOpa
h+6Z4j6yNRt9iMiFeKlAr65YrF68hIic5ZnwsDJY9uT6r8PKQxoPOWcIBvddCH1O
8Na4Pqe1NQetPs3bHCCt/iDa7kUVzI5E6X1/QtWHopVW1b9Ih6r1
-----END RSA PRIVATE KEY-----
`
func TestGenerateDSNFromConfig(t *testing.T) {
cfg := &ClientConfig{
FullDsnEmpty: true,
User: "user",
Pass: "pass",
Host: "localhost",
Port: 1234,
Database: "db",
}
want := "user:pass@tcp(localhost:1234)/db?multiStatements=true"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNFromConfigNoPass(t *testing.T) {
cfg := &ClientConfig{
FullDsnEmpty: true,
User: "user",
Host: "localhost",
Port: 1234,
Database: "db",
}
want := "user@tcp(localhost:1234)/db?multiStatements=true"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNWithTLSFromConfig(t *testing.T) {
cfg := &ClientConfig{
CA: ca,
ClientKey: clientKey,
ClientCert: clientCert,
FullDsnEmpty: true,
User: "user",
Pass: "pass",
Host: "localhost",
Port: 1234,
Database: "db",
}
want := "user:pass@tcp(localhost:1234)/db?multiStatements=true&tls=custom"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNWithDSN(t *testing.T) {
cfg := &ClientConfig{
FullDsn: "user:pass@tcp(localhost:1234)/db?multiStatements=true",
}
want := "user:pass@tcp(localhost:1234)/db?multiStatements=true"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNWithDSNWithTLS(t *testing.T) {
cfg := &ClientConfig{
FullDsn: "user:pass@tcp(localhost:1234)/db?multiStatements=true&tls=custom",
CA: ca,
ClientKey: clientKey,
ClientCert: clientCert,
PortEmpty: true,
}
want := "user:pass@tcp(localhost:1234)/db?multiStatements=true&tls=custom"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNWithUDS(t *testing.T) {
cfg := &ClientConfig{
FullDsnEmpty: true,
Database: "db",
PortEmpty: true,
User: "user",
Pass: "pass",
SocketPath: "/var/run/socket.sock",
}
want := "user:pass@unix(/var/run/socket.sock)/db?multiStatements=true"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNWithUDSWithTLS(t *testing.T) {
cfg := &ClientConfig{
FullDsnEmpty: true,
Database: "db",
User: "user",
Pass: "pass",
PortEmpty: true,
SocketPath: "/var/run/socket.sock",
CA: ca,
ClientKey: clientKey,
ClientCert: clientCert,
}
want := "user:pass@unix(/var/run/socket.sock)/db?multiStatements=true&tls=custom"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q want: %q", got, want)
}
}
func TestGenerateDSNWithUDSNoPass(t *testing.T) {
cfg := &ClientConfig{
FullDsnEmpty: true,
Database: "db",
PortEmpty: true,
User: "user",
SocketPath: "/var/run/socket.sock",
}
want := "user@unix(/var/run/socket.sock)/db?multiStatements=true"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q; want: %q", got, want)
}
}
func TestGenerateDSNWithUDSDefaults(t *testing.T) {
cfg := &ClientConfig{
FullDsnEmpty: true,
Database: "db",
PortEmpty: true,
User: "user",
}
want := "user@unix(/var/run/mysqld/mysqld.sock)/db?multiStatements=true"
got, err := cfg.MySQLConfig()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("generated dsn unexpected, got: %q; want: %q", got, want)
}
}