|
@@ -10,179 +10,12 @@ package mysql
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
- "crypto/tls"
|
|
|
|
|
"encoding/binary"
|
|
"encoding/binary"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
- "net/url"
|
|
|
|
|
"testing"
|
|
"testing"
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-var testDSNs = []struct {
|
|
|
|
|
- in string
|
|
|
|
|
- out string
|
|
|
|
|
- loc *time.Location
|
|
|
|
|
-}{
|
|
|
|
|
- {"username:password@protocol(address)/dbname?param=value", "&{user:username passwd:password net:protocol addr:address dbname:dbname params:map[param:value] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true", "&{user:username passwd:password net:protocol addr:address dbname:dbname params:map[param:value] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:true interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"user@unix(/path/to/socket)/dbname?charset=utf8", "&{user:user passwd: net:unix addr:/path/to/socket dbname:dbname params:map[charset:utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"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] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"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] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"user:password@/dbname?loc=UTC&timeout=30s&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[] loc:%p tls:<nil> timeout:30000000000 collation:224 allowAllFiles:true allowOldPasswords:true allowCleartextPasswords:false clientFoundRows:true columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"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[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.Local},
|
|
|
|
|
- {"/dbname", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"@/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"user:p@/ssword@/", "&{user:user passwd:p@/ssword net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
- {"unix/?arg=%2Fsome%2Fpath.ext", "&{user: passwd: net:unix addr:/tmp/mysql.sock dbname: params:map[arg:/some/path.ext] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestDSNParser(t *testing.T) {
|
|
|
|
|
- var cfg *config
|
|
|
|
|
- var err error
|
|
|
|
|
- var res string
|
|
|
|
|
-
|
|
|
|
|
- for i, tst := range testDSNs {
|
|
|
|
|
- cfg, err = parseDSN(tst.in)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Error(err.Error())
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // pointer not static
|
|
|
|
|
- cfg.tls = nil
|
|
|
|
|
-
|
|
|
|
|
- res = fmt.Sprintf("%+v", cfg)
|
|
|
|
|
- if res != fmt.Sprintf(tst.out, tst.loc) {
|
|
|
|
|
- t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc))
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestDSNParserInvalid(t *testing.T) {
|
|
|
|
|
- var invalidDSNs = []string{
|
|
|
|
|
- "@net(addr/", // no closing brace
|
|
|
|
|
- "@tcp(/", // no closing brace
|
|
|
|
|
- "tcp(/", // no closing brace
|
|
|
|
|
- "(/", // no closing brace
|
|
|
|
|
- "net(addr)//", // unescaped
|
|
|
|
|
- "user:pass@tcp(1.2.3.4:3306)", // no trailing slash
|
|
|
|
|
- //"/dbname?arg=/some/unescaped/path",
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for i, tst := range invalidDSNs {
|
|
|
|
|
- if _, err := parseDSN(tst); err == nil {
|
|
|
|
|
- t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestDSNWithCustomTLS(t *testing.T) {
|
|
|
|
|
- baseDSN := "user:password@tcp(localhost:5555)/dbname?tls="
|
|
|
|
|
- tlsCfg := tls.Config{}
|
|
|
|
|
-
|
|
|
|
|
- RegisterTLSConfig("utils_test", &tlsCfg)
|
|
|
|
|
-
|
|
|
|
|
- // Custom TLS is missing
|
|
|
|
|
- tst := baseDSN + "invalid_tls"
|
|
|
|
|
- cfg, err := parseDSN(tst)
|
|
|
|
|
- if err == nil {
|
|
|
|
|
- t.Errorf("Invalid custom TLS in DSN (%s) but did not error. Got config: %#v", tst, cfg)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- tst = baseDSN + "utils_test"
|
|
|
|
|
-
|
|
|
|
|
- // Custom TLS with a server name
|
|
|
|
|
- name := "foohost"
|
|
|
|
|
- tlsCfg.ServerName = name
|
|
|
|
|
- cfg, err = parseDSN(tst)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Error(err.Error())
|
|
|
|
|
- } else if cfg.tls.ServerName != name {
|
|
|
|
|
- t.Errorf("Did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, tst)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Custom TLS without a server name
|
|
|
|
|
- name = "localhost"
|
|
|
|
|
- tlsCfg.ServerName = ""
|
|
|
|
|
- cfg, err = parseDSN(tst)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Error(err.Error())
|
|
|
|
|
- } else if cfg.tls.ServerName != name {
|
|
|
|
|
- t.Errorf("Did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- DeregisterTLSConfig("utils_test")
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestDSNWithCustomTLS_queryEscape(t *testing.T) {
|
|
|
|
|
- const configKey = "&%!:"
|
|
|
|
|
- dsn := "user:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
|
|
|
|
|
- name := "foohost"
|
|
|
|
|
- tlsCfg := tls.Config{ServerName: name}
|
|
|
|
|
-
|
|
|
|
|
- RegisterTLSConfig(configKey, &tlsCfg)
|
|
|
|
|
-
|
|
|
|
|
- cfg, err := parseDSN(dsn)
|
|
|
|
|
-
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Error(err.Error())
|
|
|
|
|
- } else if cfg.tls.ServerName != name {
|
|
|
|
|
- t.Errorf("Did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func TestDSNUnsafeCollation(t *testing.T) {
|
|
|
|
|
- _, err := parseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
|
|
|
|
|
- if err != errInvalidDSNUnsafeCollation {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", errInvalidDSNUnsafeCollation, err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = parseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=false")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", nil, err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = parseDSN("/dbname?collation=gbk_chinese_ci")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", nil, err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = parseDSN("/dbname?collation=ascii_bin&interpolateParams=true")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", nil, err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = parseDSN("/dbname?collation=latin1_german1_ci&interpolateParams=true")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", nil, err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = parseDSN("/dbname?collation=utf8_general_ci&interpolateParams=true")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", nil, err)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = parseDSN("/dbname?collation=utf8mb4_general_ci&interpolateParams=true")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- t.Errorf("Expected %v, Got %v", nil, err)
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func BenchmarkParseDSN(b *testing.B) {
|
|
|
|
|
- b.ReportAllocs()
|
|
|
|
|
-
|
|
|
|
|
- for i := 0; i < b.N; i++ {
|
|
|
|
|
- for _, tst := range testDSNs {
|
|
|
|
|
- if _, err := parseDSN(tst.in); err != nil {
|
|
|
|
|
- b.Error(err.Error())
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func TestScanNullTime(t *testing.T) {
|
|
func TestScanNullTime(t *testing.T) {
|
|
|
var scanTests = []struct {
|
|
var scanTests = []struct {
|
|
|
in interface{}
|
|
in interface{}
|