@@ -12,46 +12,70 @@ import (
12
12
"crypto/tls"
13
13
"fmt"
14
14
"net/url"
15
+ "reflect"
15
16
"testing"
17
+ "time"
16
18
)
17
19
18
20
var testDSNs = []struct {
19
21
in string
20
- out string
21
- }{
22
- {"username:password@protocol(address)/dbname?param=value" , "&{User:username Passwd:password Net:protocol Addr:address DBName:dbname Params:map[param:value] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
23
- {"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true" , "&{User:username Passwd:password Net:protocol Addr:address DBName:dbname Params:map[param:value] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:true InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
24
- {"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true&multiStatements=true" , "&{User:username Passwd:password Net:protocol Addr:address DBName:dbname Params:map[param:value] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:true InterpolateParams:false MultiStatements:true ParseTime:false Strict:false}" },
25
- {"user@unix(/path/to/socket)/dbname?charset=utf8" , "&{User:user Passwd: Net:unix Addr:/path/to/socket DBName:dbname Params:map[charset:utf8] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
26
- {"user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true" , "&{User:user Passwd:password Net:tcp Addr:localhost:5555 DBName:dbname Params:map[charset:utf8] Collation:utf8_general_ci Loc:UTC TLSConfig:true tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
27
- {"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify" , "&{User:user Passwd:password Net:tcp Addr:localhost:5555 DBName:dbname Params:map[charset:utf8mb4,utf8] Collation:utf8_general_ci Loc:UTC TLSConfig:skip-verify tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
28
- {"user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci" , "&{User:user Passwd:password Net:tcp Addr:127.0.0.1:3306 DBName:dbname Params:map[] Collation:utf8mb4_unicode_ci Loc:UTC TLSConfig: tls:<nil> Timeout:30s ReadTimeout:1s WriteTimeout:1s AllowAllFiles:true AllowCleartextPasswords:false AllowOldPasswords:true ClientFoundRows:true ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
29
- {"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local" , "&{User:user Passwd:p@ss(word) Net:tcp Addr:[de:ad:be:ef::ca:fe]:80 DBName:dbname Params:map[] Collation:utf8_general_ci Loc:Local TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
30
- {"/dbname" , "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName:dbname Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
31
- {"@/" , "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
32
- {"/" , "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
33
- {"" , "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
34
- {"user:p@/ssword@/" , "&{User:user Passwd:p@/ssword Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
35
- {"unix/?arg=%2Fsome%2Fpath.ext" , "&{User: Passwd: Net:unix Addr:/tmp/mysql.sock DBName: Params:map[arg:/some/path.ext] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}" },
36
- }
22
+ out * Config
23
+ }{{
24
+ "username:password@protocol(address)/dbname?param=value" ,
25
+ & Config {User : "username" , Passwd : "password" , Net : "protocol" , Addr : "address" , DBName : "dbname" , Params : map [string ]string {"param" : "value" }, Collation : "utf8_general_ci" , Loc : time .UTC },
26
+ }, {
27
+ "username:password@protocol(address)/dbname?param=value&columnsWithAlias=true" ,
28
+ & Config {User : "username" , Passwd : "password" , Net : "protocol" , Addr : "address" , DBName : "dbname" , Params : map [string ]string {"param" : "value" }, Collation : "utf8_general_ci" , Loc : time .UTC , ColumnsWithAlias : true },
29
+ }, {
30
+ "username:password@protocol(address)/dbname?param=value&columnsWithAlias=true&multiStatements=true" ,
31
+ & Config {User : "username" , Passwd : "password" , Net : "protocol" , Addr : "address" , DBName : "dbname" , Params : map [string ]string {"param" : "value" }, Collation : "utf8_general_ci" , Loc : time .UTC , ColumnsWithAlias : true , MultiStatements : true },
32
+ }, {
33
+ "user@unix(/path/to/socket)/dbname?charset=utf8" ,
34
+ & Config {User : "user" , Net : "unix" , Addr : "/path/to/socket" , DBName : "dbname" , Params : map [string ]string {"charset" : "utf8" }, Collation : "utf8_general_ci" , Loc : time .UTC },
35
+ }, {
36
+ "user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true" ,
37
+ & Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "localhost:5555" , DBName : "dbname" , Params : map [string ]string {"charset" : "utf8" }, Collation : "utf8_general_ci" , Loc : time .UTC , TLSConfig : "true" },
38
+ }, {
39
+ "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify" ,
40
+ & Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "localhost:5555" , DBName : "dbname" , Params : map [string ]string {"charset" : "utf8mb4,utf8" }, Collation : "utf8_general_ci" , Loc : time .UTC , TLSConfig : "skip-verify" },
41
+ }, {
42
+ "user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci" ,
43
+ & Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "127.0.0.1:3306" , DBName : "dbname" , Collation : "utf8mb4_unicode_ci" , Loc : time .UTC , Timeout : 30 * time .Second , ReadTimeout : time .Second , WriteTimeout : time .Second , AllowAllFiles : true , AllowOldPasswords : true , ClientFoundRows : true },
44
+ }, {
45
+ "user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local" ,
46
+ & Config {User : "user" , Passwd : "p@ss(word)" , Net : "tcp" , Addr : "[de:ad:be:ef::ca:fe]:80" , DBName : "dbname" , Collation : "utf8_general_ci" , Loc : time .Local },
47
+ }, {
48
+ "/dbname" ,
49
+ & Config {Net : "tcp" , Addr : "127.0.0.1:3306" , DBName : "dbname" , Collation : "utf8_general_ci" , Loc : time .UTC },
50
+ }, {
51
+ "@/" ,
52
+ & Config {Net : "tcp" , Addr : "127.0.0.1:3306" , Collation : "utf8_general_ci" , Loc : time .UTC },
53
+ }, {
54
+ "/" ,
55
+ & Config {Net : "tcp" , Addr : "127.0.0.1:3306" , Collation : "utf8_general_ci" , Loc : time .UTC },
56
+ }, {
57
+ "" ,
58
+ & Config {Net : "tcp" , Addr : "127.0.0.1:3306" , Collation : "utf8_general_ci" , Loc : time .UTC },
59
+ }, {
60
+ "user:p@/ssword@/" ,
61
+ & Config {User : "user" , Passwd : "p@/ssword" , Net : "tcp" , Addr : "127.0.0.1:3306" , Collation : "utf8_general_ci" , Loc : time .UTC },
62
+ }, {
63
+ "unix/?arg=%2Fsome%2Fpath.ext" ,
64
+ & Config {Net : "unix" , Addr : "/tmp/mysql.sock" , Params : map [string ]string {"arg" : "/some/path.ext" }, Collation : "utf8_general_ci" , Loc : time .UTC },
65
+ }}
37
66
38
67
func TestDSNParser (t * testing.T ) {
39
- var cfg * Config
40
- var err error
41
- var res string
42
-
43
68
for i , tst := range testDSNs {
44
- cfg , err = ParseDSN (tst .in )
69
+ cfg , err : = ParseDSN (tst .in )
45
70
if err != nil {
46
71
t .Error (err .Error ())
47
72
}
48
73
49
74
// pointer not static
50
75
cfg .tls = nil
51
76
52
- res = fmt .Sprintf ("%+v" , cfg )
53
- if res != tst .out {
54
- t .Errorf ("%d. ParseDSN(%q) => %q, want %q" , i , tst .in , res , tst .out )
77
+ if ! reflect .DeepEqual (cfg , tst .out ) {
78
+ t .Errorf ("%d. ParseDSN(%q) mismatch:\n got %+v\n want %+v" , i , tst .in , cfg , tst .out )
55
79
}
56
80
}
57
81
}
0 commit comments