utils_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "fmt"
  11. "testing"
  12. "time"
  13. )
  14. var testDSNs = []struct {
  15. in string
  16. out string
  17. loc *time.Location
  18. }{
  19. {"username:password@protocol(address)/dbname?param=value", "&{user:username passwd:password net:protocol addr:address dbname:dbname params:map[param:value] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  20. {"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 timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  21. {"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 timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  22. {"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 timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  23. {"user:password@/dbname?loc=UTC&timeout=30s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE", "&{user:user passwd:password net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p timeout:30000000000 tls:<nil> allowAllFiles:true allowOldPasswords:true clientFoundRows:true}", time.UTC},
  24. {"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 timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.Local},
  25. {"/dbname", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  26. {"@/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  27. {"/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  28. {"", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  29. {"user:p@/ssword@/", "&{user:user passwd:p@/ssword net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  30. {"unix/?arg=%2Fsome%2Fpath.ext", "&{user: passwd: net:unix addr:/tmp/mysql.sock dbname: params:map[arg:/some/path.ext] loc:%p timeout:0 tls:<nil> allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
  31. }
  32. func TestDSNParser(t *testing.T) {
  33. var cfg *config
  34. var err error
  35. var res string
  36. for i, tst := range testDSNs {
  37. cfg, err = parseDSN(tst.in)
  38. if err != nil {
  39. t.Error(err.Error())
  40. }
  41. // pointer not static
  42. cfg.tls = nil
  43. res = fmt.Sprintf("%+v", cfg)
  44. if res != fmt.Sprintf(tst.out, tst.loc) {
  45. t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc))
  46. }
  47. }
  48. }
  49. func TestDSNParserInvalid(t *testing.T) {
  50. var invalidDSNs = []string{
  51. "@net(addr/", // no closing brace
  52. "@tcp(/", // no closing brace
  53. "tcp(/", // no closing brace
  54. "(/", // no closing brace
  55. "net(addr)//", // unescaped
  56. //"/dbname?arg=/some/unescaped/path",
  57. }
  58. for i, tst := range invalidDSNs {
  59. if _, err := parseDSN(tst); err == nil {
  60. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  61. }
  62. }
  63. }
  64. func BenchmarkParseDSN(b *testing.B) {
  65. b.ReportAllocs()
  66. for i := 0; i < b.N; i++ {
  67. for _, tst := range testDSNs {
  68. if _, err := parseDSN(tst.in); err != nil {
  69. b.Error(err.Error())
  70. }
  71. }
  72. }
  73. }
  74. func TestScanNullTime(t *testing.T) {
  75. var scanTests = []struct {
  76. in interface{}
  77. error bool
  78. valid bool
  79. time time.Time
  80. }{
  81. {tDate, false, true, tDate},
  82. {sDate, false, true, tDate},
  83. {[]byte(sDate), false, true, tDate},
  84. {tDateTime, false, true, tDateTime},
  85. {sDateTime, false, true, tDateTime},
  86. {[]byte(sDateTime), false, true, tDateTime},
  87. {tDate0, false, true, tDate0},
  88. {sDate0, false, true, tDate0},
  89. {[]byte(sDate0), false, true, tDate0},
  90. {sDateTime0, false, true, tDate0},
  91. {[]byte(sDateTime0), false, true, tDate0},
  92. {"", true, false, tDate0},
  93. {"1234", true, false, tDate0},
  94. {0, true, false, tDate0},
  95. }
  96. var nt = NullTime{}
  97. var err error
  98. for _, tst := range scanTests {
  99. err = nt.Scan(tst.in)
  100. if (err != nil) != tst.error {
  101. t.Errorf("%v: expected error status %b, got %b", tst.in, tst.error, (err != nil))
  102. }
  103. if nt.Valid != tst.valid {
  104. t.Errorf("%v: expected valid status %b, got %b", tst.in, tst.valid, nt.Valid)
  105. }
  106. if nt.Time != tst.time {
  107. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  108. }
  109. }
  110. }