-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathutils_test.go
More file actions
208 lines (193 loc) · 7.96 KB
/
Copy pathutils_test.go
File metadata and controls
208 lines (193 loc) · 7.96 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
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
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2013-2019 Hajime Nakagami
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
import (
"reflect"
"testing"
)
func TestSplitList(t *testing.T) {
cases := []struct {
in string
want []string
}{
{"Srp256,Srp,Legacy_Auth", []string{"Srp256", "Srp", "Legacy_Auth"}},
{"ChaCha64,ChaCha,Arc4", []string{"ChaCha64", "ChaCha", "Arc4"}},
{"Srp256, Srp", []string{"Srp256", "Srp"}},
{" Srp256 ,, Srp ,", []string{"Srp256", "Srp"}},
{"", []string{}},
}
for _, c := range cases {
got := splitList(c.in)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("splitList(%q)=%v, want %v", c.in, got, c.want)
}
}
}
func TestDSNParse(t *testing.T) {
var testDSNs = []struct {
dsn string
addr string
dbName string
user string
passwd string
role string
authPluginName string
wireCrypt string
}{
{"user:password@localhost:3000/dbname", "localhost:3000", "dbname", "user", "password", "", "Srp256", "true"},
{"user:password@localhost/dbname", "localhost:3050", "dbname", "user", "password", "", "Srp256", "true"},
{"user:password@localhost/dir/dbname", "localhost:3050", "/dir/dbname", "user", "password", "", "Srp256", "true"},
{"user:password@localhost/c:\\fbdata\\database.fdb", "localhost:3050", "c:\\fbdata\\database.fdb", "user", "password", "", "Srp256", "true"},
{"user:password@localhost/c:/fbdata/database.fdb", "localhost:3050", "c:/fbdata/database.fdb", "user", "password", "", "Srp256", "true"},
{"user:password@localhost/dbname?role=role", "localhost:3050", "dbname", "user", "password", "role", "Srp256", "true"},
{"user:password@localhost:3000/c:/fbdata/database.fdb?role=role&wire_crypt=false", "localhost:3000", "c:/fbdata/database.fdb", "user", "password", "role", "Srp256", "false"},
{"firebird://user:password@localhost:3000/dbname", "localhost:3000", "dbname", "user", "password", "", "Srp256", "true"},
{"firebird://user:%21p%40ssword%3F@localhost:3050/dbname", "localhost:3050", "dbname", "user", "!p@ssword?", "", "Srp256", "true"},
{"firebird://user:pass%20word@localhost:3050/dbname", "localhost:3050", "dbname", "user", "pass word", "", "Srp256", "true"},
{"firebird://user:pass%23word@localhost:3050/dbname", "localhost:3050", "dbname", "user", "pass#word", "", "Srp256", "true"},
{"firebird://user:pass%26word@localhost:3050/dbname", "localhost:3050", "dbname", "user", "pass&word", "", "Srp256", "true"},
{"firebird://user:pass%3Dword@localhost:3050/dbname", "localhost:3050", "dbname", "user", "pass=word", "", "Srp256", "true"},
{"firebird://user:pass%2Fword@localhost:3050/dbname", "localhost:3050", "dbname", "user", "pass/word", "", "Srp256", "true"},
}
for _, d := range testDSNs {
dsn, err := parseDSN(d.dsn)
if dsn == nil {
t.Fatal("parse DSN fail. firebirdDsn is nil.")
}
if err != nil {
t.Fatal(err)
}
if dsn.addr != d.addr {
t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.addr, d.addr)
}
if dsn.dbName != d.dbName {
t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.dbName, d.dbName)
}
if dsn.user != d.user {
t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.user, d.user)
}
if dsn.passwd != d.passwd {
t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.passwd, d.passwd)
}
if dsn.options["role"] != d.role {
t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.options["role"], d.role)
}
if dsn.options["auth_plugin_name"] != d.authPluginName {
t.Errorf("parse DSN fail:%s(%s != %s)", d.dsn, dsn.options["auth_plugin_name"], d.authPluginName)
}
if dsn.options["wire_crypt"] != d.wireCrypt {
t.Errorf("parse DSN fail:%s(%v != %v)", d.dsn, dsn.options["wire_crypt"], d.wireCrypt)
}
}
_, err := parseDSN("something wrong")
if err == nil {
t.Fatalf("Error Not occured")
}
_, err = parseDSN("SomethingWrongConnectionString")
if err == nil {
t.Fatalf("Error Not occured")
}
}
// TestDSNIPv6Formats mirrors Jaybird's DbAttachInfoTest IPv6 cases: a bracketed
// IPv6 host must keep its address intact and, when no port is given, still
// receive the default port 3050 (the ':' inside the brackets must not count
// as a port separator).
func TestDSNIPv6Formats(t *testing.T) {
cases := []struct {
dsn string
wantAddr string
}{
{"user:password@[::1]:3050/db.fdb", "[::1]:3050"},
{"user:password@[::1]/db.fdb", "[::1]:3050"},
{"firebird://user:password@[2001:db8::1]:3051/db.fdb", "[2001:db8::1]:3051"},
{"firebird://user:password@[2001:db8::1]/db.fdb", "[2001:db8::1]:3050"},
}
for _, c := range cases {
dsn, err := parseDSN(c.dsn)
if err != nil {
t.Errorf("parseDSN(%q): %v", c.dsn, err)
continue
}
if dsn.addr != c.wantAddr {
t.Errorf("parseDSN(%q) addr = %q, want %q", c.dsn, dsn.addr, c.wantAddr)
}
if dsn.dbName != "db.fdb" {
t.Errorf("parseDSN(%q) dbName = %q, want %q", c.dsn, dsn.dbName, "db.fdb")
}
}
}
// TestDSNOptionsDefaults mirrors Jaybird's FbConnectionPropertiesTest: every
// documented option must resolve to its documented default when absent.
func TestDSNOptionsDefaults(t *testing.T) {
dsn, err := parseDSN("user:password@localhost/db.fdb")
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"auth_plugin_name": "Srp256",
"auth_plugin_list": defaultAuthPlugins,
"charset": "UTF8",
"column_name_to_lower": "false",
"role": "",
"timezone": "",
"wire_crypt": "true",
"wire_crypt_plugin": defaultWireCryptPlugins,
"wire_compress": "false",
}
for k, v := range want {
if dsn.options[k] != v {
t.Errorf("option %q default = %q, want %q", k, dsn.options[k], v)
}
}
}
// TestDSNOptionsOverridesAndAliases checks that explicit options win over the
// defaults.
func TestDSNOptionsOverridesAndAliases(t *testing.T) {
dsn, err := parseDSN("user:password@localhost/db.fdb?charset=WIN1251&column_name_to_lower=true" +
"&role=MYROLE&timezone=Asia/Tokyo&wire_compress=true")
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"charset": "WIN1251",
"column_name_to_lower": "true",
"role": "MYROLE",
"timezone": "Asia/Tokyo",
"wire_compress": "true",
}
for k, v := range want {
if dsn.options[k] != v {
t.Errorf("option %q = %q, want %q", k, dsn.options[k], v)
}
}
}
// TestDSNOptionsFailFast mirrors Jaybird's invalid-property behavior: a bogus
// wire_crypt policy or an auth plugin outside the supported set must fail at
// parse time, before any network activity.
func TestDSNOptionsFailFast(t *testing.T) {
for _, dsn := range []string{
"user:password@localhost/db.fdb?wire_crypt=bogus",
"user:password@localhost/db.fdb?auth_plugin_name=Srp256&auth_plugin_list=NoSuchPlugin",
"user:password@localhost/db.fdb?auth_plugin_name=Unknown_Plugin",
} {
if _, err := parseDSN(dsn); err == nil {
t.Errorf("parseDSN(%q): expected fail-fast error, got nil", dsn)
}
}
}