-
Notifications
You must be signed in to change notification settings - Fork 125
/
connection_configuration_test.go
304 lines (253 loc) · 10.3 KB
/
connection_configuration_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
// Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"io/fs"
"os"
path "path/filepath"
"testing"
"time"
)
func TestTokenFilePermission(t *testing.T) {
if !isWindows {
os.Setenv(snowflakeHome, "./test_data")
_, err := loadConnectionConfig()
assertNotNilF(t, err, "The error should occur because the permission is not 0600")
driverErr, ok := err.(*SnowflakeError)
assertTrueF(t, ok, "This should be a Snowflake Error")
assertEqualF(t, driverErr.Number, ErrCodeInvalidFilePermission)
_, err = readToken("./test_data/snowflake/session")
assertNotNilF(t, err, "The error should occur because the permission is not 0600")
driverErr, ok = err.(*SnowflakeError)
assertTrueF(t, ok, "This should be a Snowflake Error")
assertEqualF(t, driverErr.Number, ErrCodeInvalidFilePermission)
err = os.Chmod("./test_data/connections.toml", 0666)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
err = os.Chmod("./test_data/snowflake/session/token", 0666)
assertNilF(t, err, "TThe error occurred because you cannot change the file permission")
_, err = loadConnectionConfig()
assertNotNilF(t, err, "The error should occur because the permission is not 0600")
driverErr, ok = err.(*SnowflakeError)
assertTrueF(t, ok, "This should be a Snowflake Error")
assertEqualF(t, driverErr.Number, ErrCodeInvalidFilePermission)
_, err = readToken("./test_data/snowflake/session")
assertNotNilF(t, err, "The error should occur because the permission is not 0600")
driverErr, ok = err.(*SnowflakeError)
assertTrueF(t, ok, "This should be a Snowflake Error")
assertEqualF(t, driverErr.Number, ErrCodeInvalidFilePermission)
err = os.Chmod("./test_data/connections.toml", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
err = os.Chmod("./test_data/snowflake/session/token", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
_, err = loadConnectionConfig()
assertNilF(t, err, "The error occurred because the permission is not 0600")
_, err = readToken("./test_data/snowflake/session/token")
assertNilF(t, err, "The error occurred because the permission is not 0600")
}
}
func TestLoadConnectionConfigForStandardAuth(t *testing.T) {
err := os.Chmod("./test_data/connections.toml", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
os.Setenv(snowflakeHome, "./test_data")
cfg, err := loadConnectionConfig()
assertNilF(t, err, "The error should not occur")
assertEqualF(t, cfg.Account, "snowdriverswarsaw.us-west-2.aws")
assertEqualF(t, cfg.User, "test_default_user")
assertEqualF(t, cfg.Password, "test_default_pass")
assertEqualF(t, cfg.Warehouse, "testw_default")
assertEqualF(t, cfg.Database, "test_default_db")
assertEqualF(t, cfg.Schema, "test_default_go")
assertEqualF(t, cfg.Protocol, "https")
assertEqualF(t, cfg.Port, 300)
}
func TestLoadConnectionConfigForOAuth(t *testing.T) {
err := os.Chmod("./test_data/connections.toml", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
os.Setenv(snowflakeHome, "./test_data")
os.Setenv(snowflakeConnectionName, "aws-oauth")
cfg, err := loadConnectionConfig()
assertNilF(t, err, "The error should not occur")
assertEqualF(t, cfg.Account, "snowdriverswarsaw.us-west-2.aws")
assertEqualF(t, cfg.User, "test_oauth_user")
assertEqualF(t, cfg.Password, "test_oauth_pass")
assertEqualF(t, cfg.Warehouse, "testw_oauth")
assertEqualF(t, cfg.Database, "test_oauth_db")
assertEqualF(t, cfg.Schema, "test_oauth_go")
assertEqualF(t, cfg.Protocol, "https")
assertEqualF(t, cfg.Authenticator, AuthTypeOAuth)
assertEqualF(t, cfg.Token, "token_value")
assertEqualF(t, cfg.Port, 443)
}
func TestReadTokenValueWithTokenFilePath(t *testing.T) {
err := os.Chmod("./test_data/connections.toml", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
err = os.Chmod("./test_data/snowflake/session/token", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
os.Setenv(snowflakeHome, "./test_data")
os.Setenv(snowflakeConnectionName, "read-token")
cfg, err := loadConnectionConfig()
assertNilF(t, err, "The error should not occur")
assertEqualF(t, cfg.Authenticator, AuthTypeOAuth)
assertEqualF(t, cfg.Token, "mock_token123456")
}
func TestLoadConnectionConfigWitNonExistingDSN(t *testing.T) {
err := os.Chmod("./test_data/connections.toml", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
os.Setenv(snowflakeHome, "./test_data")
os.Setenv(snowflakeConnectionName, "unavailableDSN")
_, err = loadConnectionConfig()
assertNotNilF(t, err, "The error should occur")
driverErr, ok := err.(*SnowflakeError)
assertTrueF(t, ok, "This should be a Snowflake Error")
assertEqualF(t, driverErr.Number, ErrCodeFailedToFindDSNInToml)
}
func TestLoadConnectionConfigWithTokenFileNotExist(t *testing.T) {
err := os.Chmod("./test_data/connections.toml", 0600)
assertNilF(t, err, "The error occurred because you cannot change the file permission")
os.Setenv(snowflakeHome, "./test_data")
os.Setenv(snowflakeConnectionName, "aws-oauth-file")
_, err = loadConnectionConfig()
assertNotNilF(t, err, "The error should occur")
_, ok := err.(*(fs.PathError))
assertTrueF(t, ok, "This error should be a path error")
}
func TestParseInt(t *testing.T) {
var i interface{}
i = 20
num, err := parseInt(i)
assertNilF(t, err, "This value should be parsed")
assertEqualF(t, num, 20)
i = "40"
num, err = parseInt(i)
assertNilF(t, err, "This value should be parsed")
assertEqualF(t, num, 40)
i = "wrong_num"
_, err = parseInt(i)
assertNotNilF(t, err, "should have failed")
}
func TestParseBool(t *testing.T) {
var i interface{}
i = true
b, err := parseBool(i)
assertNilF(t, err, "This value should be parsed")
assertEqualF(t, b, true)
i = "false"
b, err = parseBool(i)
assertNilF(t, err, "This value should be parsed")
assertEqualF(t, b, false)
i = "wrong_bool"
_, err = parseInt(i)
assertNotNilF(t, err, "should have failed")
}
func TestParseDuration(t *testing.T) {
var i interface{}
i = 300
dur, err := parseDuration(i)
assertNilF(t, err, "This value should be parsed")
assertEqualF(t, dur, time.Duration(300*int64(time.Second)))
i = "30"
dur, err = parseDuration(i)
assertNilF(t, err, "This value should be parsed")
assertEqualF(t, dur, time.Duration(int64(time.Minute)/2))
i = false
_, err = parseDuration(i)
assertNotNilF(t, err, "should have failed")
}
type paramList struct {
testParams []string
values []interface{}
}
func TestParseToml(t *testing.T) {
testCases := []paramList{
{
testParams: []string{"user", "password", "host", "account", "warehouse", "database",
"schema", "role", "region", "protocol", "passcode", "application", "token",
"tracing", "tmpDirPath", "clientConfigFile"},
values: []interface{}{"value"},
},
{
testParams: []string{"privatekey"},
values: []interface{}{generatePKCS8StringSupress(testPrivKey)},
},
{
testParams: []string{"port", "maxRetryCount", "clientTimeout", "jwtClientTimeout", "loginTimeout",
"requestTimeout", "jwtTimeout", "externalBrowserTimeout"},
values: []interface{}{"300", 500},
},
{
testParams: []string{"ocspFailOpen", "insecureMode", "PasscodeInPassword", "validateDEFAULTParameters", "clientRequestMFAtoken",
"clientStoreTemporaryCredential", "disableQueryContextCache", "includeRetryReason", "disableConsoleLogin", "disableSamlUrlCheck"},
values: []interface{}{true, "true", false, "false"},
},
}
for _, testCase := range testCases {
for _, param := range testCase.testParams {
for _, value := range testCase.values {
t.Run(param, func(t *testing.T) {
cfg := &Config{}
connectionMap := make(map[string]interface{})
connectionMap[param] = value
err := parseToml(cfg, connectionMap)
assertNilF(t, err, "The value should be parsed")
})
}
}
}
}
func TestParseTomlWithWrongValue(t *testing.T) {
testCases := []paramList{
{
testParams: []string{"user", "password", "host", "account", "warehouse", "database",
"schema", "role", "region", "protocol", "passcode", "application", "token", "privateKey",
"tracing", "tmpDirPath", "clientConfigFile", "wrongParams", "token_file_path"},
values: []interface{}{1, false},
},
{
testParams: []string{"port", "maxRetryCount", "clientTimeout", "jwtClientTimeout", "loginTimeout",
"requestTimeout", "jwtTimeout", "externalBrowserTimeout", "authenticator"},
values: []interface{}{"wrong_value", false},
},
{
testParams: []string{"ocspFailOpen", "insecureMode", "PasscodeInPassword", "validateDEFAULTParameters", "clientRequestMFAtoken",
"clientStoreTemporaryCredential", "disableQueryContextCache", "includeRetryReason", "disableConsoleLogin", "disableSamlUrlCheck"},
values: []interface{}{"wrong_value", 1},
},
}
for _, testCase := range testCases {
for _, param := range testCase.testParams {
for _, value := range testCase.values {
t.Run(param, func(t *testing.T) {
cfg := &Config{}
connectionMap := make(map[string]interface{})
connectionMap[param] = value
err := parseToml(cfg, connectionMap)
assertNotNilF(t, err, "should have failed")
driverErr, ok := err.(*SnowflakeError)
assertTrueF(t, ok, "This should be a Snowflake Error")
assertEqualF(t, driverErr.Number, ErrCodeTomlFileParsingFailed)
})
}
}
}
}
func TestGetTomlFilePath(t *testing.T) {
dir, err := getTomlFilePath("")
assertNilF(t, err, "should not have failed")
homeDir, err := os.UserHomeDir()
assertNilF(t, err, "The connection cannot find the user home directory")
assertEqualF(t, dir, path.Join(homeDir, ".snowflake"))
location := "../user//somelocation///b"
dir, err = getTomlFilePath(location)
assertNilF(t, err, "should not have failed")
result, err := path.Abs(location)
assertNilF(t, err, "should not have failed")
assertEqualF(t, dir, result)
//Absolute path for windows can be varied depend on which disk the driver is located.
// As a result, this test is available on non-Window machines.
if !isWindows {
result = "/user/somelocation/b"
location = "/user//somelocation///b"
dir, err = getTomlFilePath(location)
assertNilF(t, err, "should not have failed")
assertEqualF(t, dir, result)
}
}