utils_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 TestCachingSha2Pass(t *testing.T) {
  107. scramble := []byte{10, 47, 74, 111, 75, 73, 34, 48, 88, 76, 114, 74, 37, 13, 3, 80, 82, 2, 23, 21}
  108. vectors := []struct {
  109. pass string
  110. out string
  111. }{
  112. {"secret", "f490e76f66d9d86665ce54d98c78d0acfe2fb0b08b423da807144873d30b312c"},
  113. {"secret2", "abc3934a012cf342e876071c8ee202de51785b430258a7a0138bc79c4d800bc6"},
  114. }
  115. for _, tuple := range vectors {
  116. ours := scrambleCachingSha2Password(scramble, []byte(tuple.pass))
  117. if tuple.out != fmt.Sprintf("%x", ours) {
  118. t.Errorf("Failed caching sha2 password %q", tuple.pass)
  119. }
  120. }
  121. }
  122. func TestFormatBinaryDateTime(t *testing.T) {
  123. rawDate := [11]byte{}
  124. binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years
  125. rawDate[2] = 12 // months
  126. rawDate[3] = 30 // days
  127. rawDate[4] = 15 // hours
  128. rawDate[5] = 46 // minutes
  129. rawDate[6] = 23 // seconds
  130. binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds
  131. expect := func(expected string, inlen, outlen uint8) {
  132. actual, _ := formatBinaryDateTime(rawDate[:inlen], outlen, false)
  133. bytes, ok := actual.([]byte)
  134. if !ok {
  135. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  136. }
  137. if string(bytes) != expected {
  138. t.Errorf(
  139. "expected %q, got %q for length in %d, out %d",
  140. bytes, actual, inlen, outlen,
  141. )
  142. }
  143. }
  144. expect("0000-00-00", 0, 10)
  145. expect("0000-00-00 00:00:00", 0, 19)
  146. expect("1978-12-30", 4, 10)
  147. expect("1978-12-30 15:46:23", 7, 19)
  148. expect("1978-12-30 15:46:23.987654", 11, 26)
  149. }
  150. func TestEscapeBackslash(t *testing.T) {
  151. expect := func(expected, value string) {
  152. actual := string(escapeBytesBackslash([]byte{}, []byte(value)))
  153. if actual != expected {
  154. t.Errorf(
  155. "expected %s, got %s",
  156. expected, actual,
  157. )
  158. }
  159. actual = string(escapeStringBackslash([]byte{}, value))
  160. if actual != expected {
  161. t.Errorf(
  162. "expected %s, got %s",
  163. expected, actual,
  164. )
  165. }
  166. }
  167. expect("foo\\0bar", "foo\x00bar")
  168. expect("foo\\nbar", "foo\nbar")
  169. expect("foo\\rbar", "foo\rbar")
  170. expect("foo\\Zbar", "foo\x1abar")
  171. expect("foo\\\"bar", "foo\"bar")
  172. expect("foo\\\\bar", "foo\\bar")
  173. expect("foo\\'bar", "foo'bar")
  174. }
  175. func TestEscapeQuotes(t *testing.T) {
  176. expect := func(expected, value string) {
  177. actual := string(escapeBytesQuotes([]byte{}, []byte(value)))
  178. if actual != expected {
  179. t.Errorf(
  180. "expected %s, got %s",
  181. expected, actual,
  182. )
  183. }
  184. actual = string(escapeStringQuotes([]byte{}, value))
  185. if actual != expected {
  186. t.Errorf(
  187. "expected %s, got %s",
  188. expected, actual,
  189. )
  190. }
  191. }
  192. expect("foo\x00bar", "foo\x00bar") // not affected
  193. expect("foo\nbar", "foo\nbar") // not affected
  194. expect("foo\rbar", "foo\rbar") // not affected
  195. expect("foo\x1abar", "foo\x1abar") // not affected
  196. expect("foo''bar", "foo'bar") // affected
  197. expect("foo\"bar", "foo\"bar") // not affected
  198. }
  199. func TestAtomicBool(t *testing.T) {
  200. var ab atomicBool
  201. if ab.IsSet() {
  202. t.Fatal("Expected value to be false")
  203. }
  204. ab.Set(true)
  205. if ab.value != 1 {
  206. t.Fatal("Set(true) did not set value to 1")
  207. }
  208. if !ab.IsSet() {
  209. t.Fatal("Expected value to be true")
  210. }
  211. ab.Set(true)
  212. if !ab.IsSet() {
  213. t.Fatal("Expected value to be true")
  214. }
  215. ab.Set(false)
  216. if ab.value != 0 {
  217. t.Fatal("Set(false) did not set value to 0")
  218. }
  219. if ab.IsSet() {
  220. t.Fatal("Expected value to be false")
  221. }
  222. ab.Set(false)
  223. if ab.IsSet() {
  224. t.Fatal("Expected value to be false")
  225. }
  226. if ab.TrySet(false) {
  227. t.Fatal("Expected TrySet(false) to fail")
  228. }
  229. if !ab.TrySet(true) {
  230. t.Fatal("Expected TrySet(true) to succeed")
  231. }
  232. if !ab.IsSet() {
  233. t.Fatal("Expected value to be true")
  234. }
  235. ab.Set(true)
  236. if !ab.IsSet() {
  237. t.Fatal("Expected value to be true")
  238. }
  239. if ab.TrySet(true) {
  240. t.Fatal("Expected TrySet(true) to fail")
  241. }
  242. if !ab.TrySet(false) {
  243. t.Fatal("Expected TrySet(false) to succeed")
  244. }
  245. if ab.IsSet() {
  246. t.Fatal("Expected value to be false")
  247. }
  248. ab._noCopy.Lock() // we've "tested" it ¯\_(ツ)_/¯
  249. }
  250. func TestAtomicError(t *testing.T) {
  251. var ae atomicError
  252. if ae.Value() != nil {
  253. t.Fatal("Expected value to be nil")
  254. }
  255. ae.Set(ErrMalformPkt)
  256. if v := ae.Value(); v != ErrMalformPkt {
  257. if v == nil {
  258. t.Fatal("Value is still nil")
  259. }
  260. t.Fatal("Error did not match")
  261. }
  262. ae.Set(ErrPktSync)
  263. if ae.Value() == ErrMalformPkt {
  264. t.Fatal("Error still matches old error")
  265. }
  266. if v := ae.Value(); v != ErrPktSync {
  267. t.Fatal("Error did not match")
  268. }
  269. }