utils_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "fmt"
  12. "testing"
  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. "user:pass@tcp(1.2.3.4:3306)", // no trailing slash
  58. //"/dbname?arg=/some/unescaped/path",
  59. }
  60. for i, tst := range invalidDSNs {
  61. if _, err := parseDSN(tst); err == nil {
  62. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  63. }
  64. }
  65. }
  66. func BenchmarkParseDSN(b *testing.B) {
  67. b.ReportAllocs()
  68. for i := 0; i < b.N; i++ {
  69. for _, tst := range testDSNs {
  70. if _, err := parseDSN(tst.in); err != nil {
  71. b.Error(err.Error())
  72. }
  73. }
  74. }
  75. }
  76. func TestScanNullTime(t *testing.T) {
  77. var scanTests = []struct {
  78. in interface{}
  79. error bool
  80. valid bool
  81. time time.Time
  82. }{
  83. {tDate, false, true, tDate},
  84. {sDate, false, true, tDate},
  85. {[]byte(sDate), false, true, tDate},
  86. {tDateTime, false, true, tDateTime},
  87. {sDateTime, false, true, tDateTime},
  88. {[]byte(sDateTime), false, true, tDateTime},
  89. {tDate0, false, true, tDate0},
  90. {sDate0, false, true, tDate0},
  91. {[]byte(sDate0), false, true, tDate0},
  92. {sDateTime0, false, true, tDate0},
  93. {[]byte(sDateTime0), false, true, tDate0},
  94. {"", true, false, tDate0},
  95. {"1234", true, false, tDate0},
  96. {0, true, false, tDate0},
  97. }
  98. var nt = NullTime{}
  99. var err error
  100. for _, tst := range scanTests {
  101. err = nt.Scan(tst.in)
  102. if (err != nil) != tst.error {
  103. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  104. }
  105. if nt.Valid != tst.valid {
  106. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  107. }
  108. if nt.Time != tst.time {
  109. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  110. }
  111. }
  112. }
  113. func TestLengthEncodedInteger(t *testing.T) {
  114. var integerTests = []struct {
  115. num uint64
  116. encoded []byte
  117. }{
  118. {0x0000000000000000, []byte{0x00}},
  119. {0x0000000000000012, []byte{0x12}},
  120. {0x00000000000000fa, []byte{0xfa}},
  121. {0x0000000000000100, []byte{0xfc, 0x00, 0x01}},
  122. {0x0000000000001234, []byte{0xfc, 0x34, 0x12}},
  123. {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}},
  124. {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}},
  125. {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}},
  126. {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}},
  127. {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}},
  128. {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}},
  129. {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  130. }
  131. for _, tst := range integerTests {
  132. num, isNull, numLen := readLengthEncodedInteger(tst.encoded)
  133. if isNull {
  134. t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num)
  135. }
  136. if num != tst.num {
  137. t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num)
  138. }
  139. if numLen != len(tst.encoded) {
  140. t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen)
  141. }
  142. encoded := appendLengthEncodedInteger(nil, num)
  143. if !bytes.Equal(encoded, tst.encoded) {
  144. t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded)
  145. }
  146. }
  147. }
  148. func TestOldPass(t *testing.T) {
  149. scramble := []byte{9, 8, 7, 6, 5, 4, 3, 2}
  150. vectors := []struct {
  151. pass string
  152. out string
  153. }{
  154. {" pass", "47575c5a435b4251"},
  155. {"pass ", "47575c5a435b4251"},
  156. {"123\t456", "575c47505b5b5559"},
  157. {"C0mpl!ca ted#PASS123", "5d5d554849584a45"},
  158. }
  159. for _, tuple := range vectors {
  160. ours := scrambleOldPassword(scramble, []byte(tuple.pass))
  161. if tuple.out != fmt.Sprintf("%x", ours) {
  162. t.Errorf("Failed old password %q", tuple.pass)
  163. }
  164. }
  165. }