utils_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "bytes"
  13. "time"
  14. )
  15. var testDSNs = []struct {
  16. in string
  17. out string
  18. loc *time.Location
  19. }{
  20. {"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},
  21. {"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},
  22. {"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},
  23. {"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},
  24. {"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},
  25. {"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},
  26. {"/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},
  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: 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},
  30. {"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},
  31. {"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},
  32. }
  33. func TestDSNParser(t *testing.T) {
  34. var cfg *config
  35. var err error
  36. var res string
  37. for i, tst := range testDSNs {
  38. cfg, err = parseDSN(tst.in)
  39. if err != nil {
  40. t.Error(err.Error())
  41. }
  42. // pointer not static
  43. cfg.tls = nil
  44. res = fmt.Sprintf("%+v", cfg)
  45. if res != fmt.Sprintf(tst.out, tst.loc) {
  46. t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc))
  47. }
  48. }
  49. }
  50. func TestDSNParserInvalid(t *testing.T) {
  51. var invalidDSNs = []string{
  52. "@net(addr/", // no closing brace
  53. "@tcp(/", // no closing brace
  54. "tcp(/", // no closing brace
  55. "(/", // no closing brace
  56. "net(addr)//", // unescaped
  57. //"/dbname?arg=/some/unescaped/path",
  58. }
  59. for i, tst := range invalidDSNs {
  60. if _, err := parseDSN(tst); err == nil {
  61. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  62. }
  63. }
  64. }
  65. func BenchmarkParseDSN(b *testing.B) {
  66. b.ReportAllocs()
  67. for i := 0; i < b.N; i++ {
  68. for _, tst := range testDSNs {
  69. if _, err := parseDSN(tst.in); err != nil {
  70. b.Error(err.Error())
  71. }
  72. }
  73. }
  74. }
  75. func TestScanNullTime(t *testing.T) {
  76. var scanTests = []struct {
  77. in interface{}
  78. error bool
  79. valid bool
  80. time time.Time
  81. }{
  82. {tDate, false, true, tDate},
  83. {sDate, false, true, tDate},
  84. {[]byte(sDate), false, true, tDate},
  85. {tDateTime, false, true, tDateTime},
  86. {sDateTime, false, true, tDateTime},
  87. {[]byte(sDateTime), false, true, tDateTime},
  88. {tDate0, false, true, tDate0},
  89. {sDate0, false, true, tDate0},
  90. {[]byte(sDate0), false, true, tDate0},
  91. {sDateTime0, false, true, tDate0},
  92. {[]byte(sDateTime0), false, true, tDate0},
  93. {"", true, false, tDate0},
  94. {"1234", true, false, tDate0},
  95. {0, true, false, tDate0},
  96. }
  97. var nt = NullTime{}
  98. var err error
  99. for _, tst := range scanTests {
  100. err = nt.Scan(tst.in)
  101. if (err != nil) != tst.error {
  102. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  103. }
  104. if nt.Valid != tst.valid {
  105. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  106. }
  107. if nt.Time != tst.time {
  108. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  109. }
  110. }
  111. }
  112. func TestLengthEncodedInteger(t *testing.T) {
  113. var integerTests = []struct {
  114. num uint64
  115. encoded []byte
  116. }{
  117. {0x0000000000000000, []byte{0x00}},
  118. {0x0000000000000012, []byte{0x12}},
  119. {0x00000000000000fa, []byte{0xfa}},
  120. {0x0000000000000100, []byte{0xfc, 0x00, 0x01}},
  121. {0x0000000000001234, []byte{0xfc, 0x34, 0x12}},
  122. {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}},
  123. {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}},
  124. {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}},
  125. {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}},
  126. {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}},
  127. {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}},
  128. {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  129. }
  130. for _, tst := range integerTests {
  131. num, isNull, numLen := readLengthEncodedInteger(tst.encoded)
  132. if isNull {
  133. t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num)
  134. }
  135. if num != tst.num {
  136. t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num)
  137. }
  138. if numLen != len(tst.encoded) {
  139. t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen)
  140. }
  141. encoded := appendLengthEncodedInteger(nil, num)
  142. if (!bytes.Equal(encoded, tst.encoded)) {
  143. t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded)
  144. }
  145. }
  146. }