utils_test.go 9.1 KB

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