utils_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "bytes"
  11. "encoding/binary"
  12. "fmt"
  13. "testing"
  14. "time"
  15. )
  16. var testDSNs = []struct {
  17. in string
  18. out string
  19. loc *time.Location
  20. }{
  21. {"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 clientFoundRows:false}", time.UTC},
  22. {"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 clientFoundRows:false}", time.UTC},
  23. {"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 clientFoundRows:false}", time.UTC},
  24. {"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 clientFoundRows:false}", time.UTC},
  25. {"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 clientFoundRows:true}", time.UTC},
  26. {"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 clientFoundRows:false}", time.Local},
  27. {"/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 clientFoundRows:false}", time.UTC},
  28. {"@/", "&{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 clientFoundRows:false}", time.UTC},
  29. {"/", "&{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 clientFoundRows:false}", time.UTC},
  30. {"", "&{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 clientFoundRows:false}", time.UTC},
  31. {"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 clientFoundRows:false}", time.UTC},
  32. {"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 clientFoundRows:false}", time.UTC},
  33. }
  34. func TestDSNParser(t *testing.T) {
  35. var cfg *config
  36. var err error
  37. var res string
  38. for i, tst := range testDSNs {
  39. cfg, err = parseDSN(tst.in)
  40. if err != nil {
  41. t.Error(err.Error())
  42. }
  43. // pointer not static
  44. cfg.tls = nil
  45. res = fmt.Sprintf("%+v", cfg)
  46. if res != fmt.Sprintf(tst.out, tst.loc) {
  47. t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc))
  48. }
  49. }
  50. }
  51. func TestDSNParserInvalid(t *testing.T) {
  52. var invalidDSNs = []string{
  53. "@net(addr/", // no closing brace
  54. "@tcp(/", // no closing brace
  55. "tcp(/", // no closing brace
  56. "(/", // no closing brace
  57. "net(addr)//", // unescaped
  58. "user:pass@tcp(1.2.3.4:3306)", // no trailing slash
  59. //"/dbname?arg=/some/unescaped/path",
  60. }
  61. for i, tst := range invalidDSNs {
  62. if _, err := parseDSN(tst); err == nil {
  63. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  64. }
  65. }
  66. }
  67. func BenchmarkParseDSN(b *testing.B) {
  68. b.ReportAllocs()
  69. for i := 0; i < b.N; i++ {
  70. for _, tst := range testDSNs {
  71. if _, err := parseDSN(tst.in); err != nil {
  72. b.Error(err.Error())
  73. }
  74. }
  75. }
  76. }
  77. func TestScanNullTime(t *testing.T) {
  78. var scanTests = []struct {
  79. in interface{}
  80. error bool
  81. valid bool
  82. time time.Time
  83. }{
  84. {tDate, false, true, tDate},
  85. {sDate, false, true, tDate},
  86. {[]byte(sDate), false, true, tDate},
  87. {tDateTime, false, true, tDateTime},
  88. {sDateTime, false, true, tDateTime},
  89. {[]byte(sDateTime), false, true, tDateTime},
  90. {tDate0, false, true, tDate0},
  91. {sDate0, false, true, tDate0},
  92. {[]byte(sDate0), false, true, tDate0},
  93. {sDateTime0, false, true, tDate0},
  94. {[]byte(sDateTime0), false, true, tDate0},
  95. {"", true, false, tDate0},
  96. {"1234", true, false, tDate0},
  97. {0, true, false, tDate0},
  98. }
  99. var nt = NullTime{}
  100. var err error
  101. for _, tst := range scanTests {
  102. err = nt.Scan(tst.in)
  103. if (err != nil) != tst.error {
  104. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  105. }
  106. if nt.Valid != tst.valid {
  107. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  108. }
  109. if nt.Time != tst.time {
  110. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  111. }
  112. }
  113. }
  114. func TestLengthEncodedInteger(t *testing.T) {
  115. var integerTests = []struct {
  116. num uint64
  117. encoded []byte
  118. }{
  119. {0x0000000000000000, []byte{0x00}},
  120. {0x0000000000000012, []byte{0x12}},
  121. {0x00000000000000fa, []byte{0xfa}},
  122. {0x0000000000000100, []byte{0xfc, 0x00, 0x01}},
  123. {0x0000000000001234, []byte{0xfc, 0x34, 0x12}},
  124. {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}},
  125. {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}},
  126. {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}},
  127. {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}},
  128. {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}},
  129. {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}},
  130. {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  131. }
  132. for _, tst := range integerTests {
  133. num, isNull, numLen := readLengthEncodedInteger(tst.encoded)
  134. if isNull {
  135. t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num)
  136. }
  137. if num != tst.num {
  138. t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num)
  139. }
  140. if numLen != len(tst.encoded) {
  141. t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen)
  142. }
  143. encoded := appendLengthEncodedInteger(nil, num)
  144. if !bytes.Equal(encoded, tst.encoded) {
  145. t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded)
  146. }
  147. }
  148. }
  149. func TestOldPass(t *testing.T) {
  150. scramble := []byte{9, 8, 7, 6, 5, 4, 3, 2}
  151. vectors := []struct {
  152. pass string
  153. out string
  154. }{
  155. {" pass", "47575c5a435b4251"},
  156. {"pass ", "47575c5a435b4251"},
  157. {"123\t456", "575c47505b5b5559"},
  158. {"C0mpl!ca ted#PASS123", "5d5d554849584a45"},
  159. }
  160. for _, tuple := range vectors {
  161. ours := scrambleOldPassword(scramble, []byte(tuple.pass))
  162. if tuple.out != fmt.Sprintf("%x", ours) {
  163. t.Errorf("Failed old password %q", tuple.pass)
  164. }
  165. }
  166. }
  167. func TestFormatBinaryDateTime(t *testing.T) {
  168. rawDate := [11]byte{}
  169. binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years
  170. rawDate[2] = 12 // months
  171. rawDate[3] = 30 // days
  172. rawDate[4] = 15 // hours
  173. rawDate[5] = 46 // minutes
  174. rawDate[6] = 23 // seconds
  175. binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds
  176. expect := func(expected string, length int, withTime bool) {
  177. actual, _ := formatBinaryDateTime(rawDate[:length], withTime)
  178. bytes, ok := actual.([]byte)
  179. if !ok {
  180. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  181. }
  182. if string(bytes) != expected {
  183. t.Errorf(
  184. "expected %q, got %q for length %d, withTime %v",
  185. bytes, actual, length, withTime,
  186. )
  187. }
  188. }
  189. expect("0000-00-00", 0, false)
  190. expect("0000-00-00 00:00:00", 0, true)
  191. expect("1978-12-30", 4, false)
  192. expect("1978-12-30 15:46:23", 7, true)
  193. expect("1978-12-30 15:46:23.987654", 11, true)
  194. }