utils_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. "crypto/tls"
  12. "encoding/binary"
  13. "fmt"
  14. "net/url"
  15. "testing"
  16. "time"
  17. )
  18. var testDSNs = []struct {
  19. in string
  20. out string
  21. loc *time.Location
  22. }{
  23. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  24. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:true interpolateParams:false}", time.UTC},
  25. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  26. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  27. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  28. {"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 allowCleartextPasswords:false clientFoundRows:true columnsWithAlias:false interpolateParams:false}", time.UTC},
  29. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.Local},
  30. {"/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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams: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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  32. {"/", "&{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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  33. {"", "&{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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  34. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  35. {"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 allowCleartextPasswords:false clientFoundRows:false columnsWithAlias:false interpolateParams:false}", time.UTC},
  36. }
  37. func TestDSNParser(t *testing.T) {
  38. var cfg *config
  39. var err error
  40. var res string
  41. for i, tst := range testDSNs {
  42. cfg, err = parseDSN(tst.in)
  43. if err != nil {
  44. t.Error(err.Error())
  45. }
  46. // pointer not static
  47. cfg.tls = nil
  48. res = fmt.Sprintf("%+v", cfg)
  49. if res != fmt.Sprintf(tst.out, tst.loc) {
  50. t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc))
  51. }
  52. }
  53. }
  54. func TestDSNParserInvalid(t *testing.T) {
  55. var invalidDSNs = []string{
  56. "@net(addr/", // no closing brace
  57. "@tcp(/", // no closing brace
  58. "tcp(/", // no closing brace
  59. "(/", // no closing brace
  60. "net(addr)//", // unescaped
  61. "user:pass@tcp(1.2.3.4:3306)", // no trailing slash
  62. //"/dbname?arg=/some/unescaped/path",
  63. }
  64. for i, tst := range invalidDSNs {
  65. if _, err := parseDSN(tst); err == nil {
  66. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  67. }
  68. }
  69. }
  70. func TestDSNWithCustomTLS(t *testing.T) {
  71. baseDSN := "user:password@tcp(localhost:5555)/dbname?tls="
  72. tlsCfg := tls.Config{}
  73. RegisterTLSConfig("utils_test", &tlsCfg)
  74. // Custom TLS is missing
  75. tst := baseDSN + "invalid_tls"
  76. cfg, err := parseDSN(tst)
  77. if err == nil {
  78. t.Errorf("Invalid custom TLS in DSN (%s) but did not error. Got config: %#v", tst, cfg)
  79. }
  80. tst = baseDSN + "utils_test"
  81. // Custom TLS with a server name
  82. name := "foohost"
  83. tlsCfg.ServerName = name
  84. cfg, err = parseDSN(tst)
  85. if err != nil {
  86. t.Error(err.Error())
  87. } else if cfg.tls.ServerName != name {
  88. t.Errorf("Did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, tst)
  89. }
  90. // Custom TLS without a server name
  91. name = "localhost"
  92. tlsCfg.ServerName = ""
  93. cfg, err = parseDSN(tst)
  94. if err != nil {
  95. t.Error(err.Error())
  96. } else if cfg.tls.ServerName != name {
  97. t.Errorf("Did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
  98. }
  99. DeregisterTLSConfig("utils_test")
  100. }
  101. func TestDSNWithCustomTLS_queryEscape(t *testing.T) {
  102. const configKey = "&%!:"
  103. dsn := "user:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
  104. name := "foohost"
  105. tlsCfg := tls.Config{ServerName: name}
  106. RegisterTLSConfig(configKey, &tlsCfg)
  107. cfg, err := parseDSN(dsn)
  108. if err != nil {
  109. t.Error(err.Error())
  110. } else if cfg.tls.ServerName != name {
  111. t.Errorf("Did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
  112. }
  113. }
  114. func TestDSNUnsafeCollation(t *testing.T) {
  115. _, err := parseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
  116. if err != errInvalidDSNUnsafeCollation {
  117. t.Error("Expected %v, Got %v", errInvalidDSNUnsafeCollation, err)
  118. }
  119. _, err = parseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=false")
  120. if err != nil {
  121. t.Error("Expected %v, Got %v", nil, err)
  122. }
  123. _, err = parseDSN("/dbname?collation=gbk_chinese_ci")
  124. if err != nil {
  125. t.Error("Expected %v, Got %v", nil, err)
  126. }
  127. _, err = parseDSN("/dbname?collation=ascii_bin&interpolateParams=true")
  128. if err != nil {
  129. t.Error("Expected %v, Got %v", nil, err)
  130. }
  131. _, err = parseDSN("/dbname?collation=latin1_german1_ci&interpolateParams=true")
  132. if err != nil {
  133. t.Error("Expected %v, Got %v", nil, err)
  134. }
  135. _, err = parseDSN("/dbname?collation=utf8_general_ci&interpolateParams=true")
  136. if err != nil {
  137. t.Error("Expected %v, Got %v", nil, err)
  138. }
  139. _, err = parseDSN("/dbname?collation=utf8mb4_general_ci&interpolateParams=true")
  140. if err != nil {
  141. t.Error("Expected %v, Got %v", nil, err)
  142. }
  143. }
  144. func BenchmarkParseDSN(b *testing.B) {
  145. b.ReportAllocs()
  146. for i := 0; i < b.N; i++ {
  147. for _, tst := range testDSNs {
  148. if _, err := parseDSN(tst.in); err != nil {
  149. b.Error(err.Error())
  150. }
  151. }
  152. }
  153. }
  154. func TestScanNullTime(t *testing.T) {
  155. var scanTests = []struct {
  156. in interface{}
  157. error bool
  158. valid bool
  159. time time.Time
  160. }{
  161. {tDate, false, true, tDate},
  162. {sDate, false, true, tDate},
  163. {[]byte(sDate), false, true, tDate},
  164. {tDateTime, false, true, tDateTime},
  165. {sDateTime, false, true, tDateTime},
  166. {[]byte(sDateTime), false, true, tDateTime},
  167. {tDate0, false, true, tDate0},
  168. {sDate0, false, true, tDate0},
  169. {[]byte(sDate0), false, true, tDate0},
  170. {sDateTime0, false, true, tDate0},
  171. {[]byte(sDateTime0), false, true, tDate0},
  172. {"", true, false, tDate0},
  173. {"1234", true, false, tDate0},
  174. {0, true, false, tDate0},
  175. }
  176. var nt = NullTime{}
  177. var err error
  178. for _, tst := range scanTests {
  179. err = nt.Scan(tst.in)
  180. if (err != nil) != tst.error {
  181. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  182. }
  183. if nt.Valid != tst.valid {
  184. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  185. }
  186. if nt.Time != tst.time {
  187. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  188. }
  189. }
  190. }
  191. func TestLengthEncodedInteger(t *testing.T) {
  192. var integerTests = []struct {
  193. num uint64
  194. encoded []byte
  195. }{
  196. {0x0000000000000000, []byte{0x00}},
  197. {0x0000000000000012, []byte{0x12}},
  198. {0x00000000000000fa, []byte{0xfa}},
  199. {0x0000000000000100, []byte{0xfc, 0x00, 0x01}},
  200. {0x0000000000001234, []byte{0xfc, 0x34, 0x12}},
  201. {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}},
  202. {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}},
  203. {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}},
  204. {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}},
  205. {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}},
  206. {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}},
  207. {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  208. }
  209. for _, tst := range integerTests {
  210. num, isNull, numLen := readLengthEncodedInteger(tst.encoded)
  211. if isNull {
  212. t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num)
  213. }
  214. if num != tst.num {
  215. t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num)
  216. }
  217. if numLen != len(tst.encoded) {
  218. t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen)
  219. }
  220. encoded := appendLengthEncodedInteger(nil, num)
  221. if !bytes.Equal(encoded, tst.encoded) {
  222. t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded)
  223. }
  224. }
  225. }
  226. func TestOldPass(t *testing.T) {
  227. scramble := []byte{9, 8, 7, 6, 5, 4, 3, 2}
  228. vectors := []struct {
  229. pass string
  230. out string
  231. }{
  232. {" pass", "47575c5a435b4251"},
  233. {"pass ", "47575c5a435b4251"},
  234. {"123\t456", "575c47505b5b5559"},
  235. {"C0mpl!ca ted#PASS123", "5d5d554849584a45"},
  236. }
  237. for _, tuple := range vectors {
  238. ours := scrambleOldPassword(scramble, []byte(tuple.pass))
  239. if tuple.out != fmt.Sprintf("%x", ours) {
  240. t.Errorf("Failed old password %q", tuple.pass)
  241. }
  242. }
  243. }
  244. func TestFormatBinaryDateTime(t *testing.T) {
  245. rawDate := [11]byte{}
  246. binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years
  247. rawDate[2] = 12 // months
  248. rawDate[3] = 30 // days
  249. rawDate[4] = 15 // hours
  250. rawDate[5] = 46 // minutes
  251. rawDate[6] = 23 // seconds
  252. binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds
  253. expect := func(expected string, inlen, outlen uint8) {
  254. actual, _ := formatBinaryDateTime(rawDate[:inlen], outlen, false)
  255. bytes, ok := actual.([]byte)
  256. if !ok {
  257. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  258. }
  259. if string(bytes) != expected {
  260. t.Errorf(
  261. "expected %q, got %q for length in %d, out %d",
  262. bytes, actual, inlen, outlen,
  263. )
  264. }
  265. }
  266. expect("0000-00-00", 0, 10)
  267. expect("0000-00-00 00:00:00", 0, 19)
  268. expect("1978-12-30", 4, 10)
  269. expect("1978-12-30 15:46:23", 7, 19)
  270. expect("1978-12-30 15:46:23.987654", 11, 26)
  271. }
  272. func TestEscapeBackslash(t *testing.T) {
  273. expect := func(expected, value string) {
  274. actual := string(escapeBytesBackslash([]byte{}, []byte(value)))
  275. if actual != expected {
  276. t.Errorf(
  277. "expected %s, got %s",
  278. expected, actual,
  279. )
  280. }
  281. actual = string(escapeStringBackslash([]byte{}, value))
  282. if actual != expected {
  283. t.Errorf(
  284. "expected %s, got %s",
  285. expected, actual,
  286. )
  287. }
  288. }
  289. expect("foo\\0bar", "foo\x00bar")
  290. expect("foo\\nbar", "foo\nbar")
  291. expect("foo\\rbar", "foo\rbar")
  292. expect("foo\\Zbar", "foo\x1abar")
  293. expect("foo\\\"bar", "foo\"bar")
  294. expect("foo\\\\bar", "foo\\bar")
  295. expect("foo\\'bar", "foo'bar")
  296. }
  297. func TestEscapeQuotes(t *testing.T) {
  298. expect := func(expected, value string) {
  299. actual := string(escapeBytesQuotes([]byte{}, []byte(value)))
  300. if actual != expected {
  301. t.Errorf(
  302. "expected %s, got %s",
  303. expected, actual,
  304. )
  305. }
  306. actual = string(escapeStringQuotes([]byte{}, value))
  307. if actual != expected {
  308. t.Errorf(
  309. "expected %s, got %s",
  310. expected, actual,
  311. )
  312. }
  313. }
  314. expect("foo\x00bar", "foo\x00bar") // not affected
  315. expect("foo\nbar", "foo\nbar") // not affected
  316. expect("foo\rbar", "foo\rbar") // not affected
  317. expect("foo\x1abar", "foo\x1abar") // not affected
  318. expect("foo''bar", "foo'bar") // affected
  319. expect("foo\"bar", "foo\"bar") // not affected
  320. }