PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/go-sql-driver/mysql/utils_test.go

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