utils_test.go 5.6 KB

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