utils_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. "testing"
  13. "time"
  14. )
  15. func TestScanNullTime(t *testing.T) {
  16. var scanTests = []struct {
  17. in interface{}
  18. error bool
  19. valid bool
  20. time time.Time
  21. }{
  22. {tDate, false, true, tDate},
  23. {sDate, false, true, tDate},
  24. {[]byte(sDate), false, true, tDate},
  25. {tDateTime, false, true, tDateTime},
  26. {sDateTime, false, true, tDateTime},
  27. {[]byte(sDateTime), false, true, tDateTime},
  28. {tDate0, false, true, tDate0},
  29. {sDate0, false, true, tDate0},
  30. {[]byte(sDate0), false, true, tDate0},
  31. {sDateTime0, false, true, tDate0},
  32. {[]byte(sDateTime0), false, true, tDate0},
  33. {"", true, false, tDate0},
  34. {"1234", true, false, tDate0},
  35. {0, true, false, tDate0},
  36. }
  37. var nt = NullTime{}
  38. var err error
  39. for _, tst := range scanTests {
  40. err = nt.Scan(tst.in)
  41. if (err != nil) != tst.error {
  42. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  43. }
  44. if nt.Valid != tst.valid {
  45. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  46. }
  47. if nt.Time != tst.time {
  48. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  49. }
  50. }
  51. }
  52. func TestLengthEncodedInteger(t *testing.T) {
  53. var integerTests = []struct {
  54. num uint64
  55. encoded []byte
  56. }{
  57. {0x0000000000000000, []byte{0x00}},
  58. {0x0000000000000012, []byte{0x12}},
  59. {0x00000000000000fa, []byte{0xfa}},
  60. {0x0000000000000100, []byte{0xfc, 0x00, 0x01}},
  61. {0x0000000000001234, []byte{0xfc, 0x34, 0x12}},
  62. {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}},
  63. {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}},
  64. {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}},
  65. {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}},
  66. {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}},
  67. {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}},
  68. {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  69. }
  70. for _, tst := range integerTests {
  71. num, isNull, numLen := readLengthEncodedInteger(tst.encoded)
  72. if isNull {
  73. t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num)
  74. }
  75. if num != tst.num {
  76. t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num)
  77. }
  78. if numLen != len(tst.encoded) {
  79. t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen)
  80. }
  81. encoded := appendLengthEncodedInteger(nil, num)
  82. if !bytes.Equal(encoded, tst.encoded) {
  83. t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded)
  84. }
  85. }
  86. }
  87. func TestFormatBinaryDateTime(t *testing.T) {
  88. rawDate := [11]byte{}
  89. binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years
  90. rawDate[2] = 12 // months
  91. rawDate[3] = 30 // days
  92. rawDate[4] = 15 // hours
  93. rawDate[5] = 46 // minutes
  94. rawDate[6] = 23 // seconds
  95. binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds
  96. expect := func(expected string, inlen, outlen uint8) {
  97. actual, _ := formatBinaryDateTime(rawDate[:inlen], outlen, false)
  98. bytes, ok := actual.([]byte)
  99. if !ok {
  100. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  101. }
  102. if string(bytes) != expected {
  103. t.Errorf(
  104. "expected %q, got %q for length in %d, out %d",
  105. bytes, actual, inlen, outlen,
  106. )
  107. }
  108. }
  109. expect("0000-00-00", 0, 10)
  110. expect("0000-00-00 00:00:00", 0, 19)
  111. expect("1978-12-30", 4, 10)
  112. expect("1978-12-30 15:46:23", 7, 19)
  113. expect("1978-12-30 15:46:23.987654", 11, 26)
  114. }
  115. func TestEscapeBackslash(t *testing.T) {
  116. expect := func(expected, value string) {
  117. actual := string(escapeBytesBackslash([]byte{}, []byte(value)))
  118. if actual != expected {
  119. t.Errorf(
  120. "expected %s, got %s",
  121. expected, actual,
  122. )
  123. }
  124. actual = string(escapeStringBackslash([]byte{}, value))
  125. if actual != expected {
  126. t.Errorf(
  127. "expected %s, got %s",
  128. expected, actual,
  129. )
  130. }
  131. }
  132. expect("foo\\0bar", "foo\x00bar")
  133. expect("foo\\nbar", "foo\nbar")
  134. expect("foo\\rbar", "foo\rbar")
  135. expect("foo\\Zbar", "foo\x1abar")
  136. expect("foo\\\"bar", "foo\"bar")
  137. expect("foo\\\\bar", "foo\\bar")
  138. expect("foo\\'bar", "foo'bar")
  139. }
  140. func TestEscapeQuotes(t *testing.T) {
  141. expect := func(expected, value string) {
  142. actual := string(escapeBytesQuotes([]byte{}, []byte(value)))
  143. if actual != expected {
  144. t.Errorf(
  145. "expected %s, got %s",
  146. expected, actual,
  147. )
  148. }
  149. actual = string(escapeStringQuotes([]byte{}, value))
  150. if actual != expected {
  151. t.Errorf(
  152. "expected %s, got %s",
  153. expected, actual,
  154. )
  155. }
  156. }
  157. expect("foo\x00bar", "foo\x00bar") // not affected
  158. expect("foo\nbar", "foo\nbar") // not affected
  159. expect("foo\rbar", "foo\rbar") // not affected
  160. expect("foo\x1abar", "foo\x1abar") // not affected
  161. expect("foo''bar", "foo'bar") // affected
  162. expect("foo\"bar", "foo\"bar") // not affected
  163. }
  164. func TestAtomicBool(t *testing.T) {
  165. var ab atomicBool
  166. if ab.IsSet() {
  167. t.Fatal("Expected value to be false")
  168. }
  169. ab.Set(true)
  170. if ab.value != 1 {
  171. t.Fatal("Set(true) did not set value to 1")
  172. }
  173. if !ab.IsSet() {
  174. t.Fatal("Expected value to be true")
  175. }
  176. ab.Set(true)
  177. if !ab.IsSet() {
  178. t.Fatal("Expected value to be true")
  179. }
  180. ab.Set(false)
  181. if ab.value != 0 {
  182. t.Fatal("Set(false) did not set value to 0")
  183. }
  184. if ab.IsSet() {
  185. t.Fatal("Expected value to be false")
  186. }
  187. ab.Set(false)
  188. if ab.IsSet() {
  189. t.Fatal("Expected value to be false")
  190. }
  191. if ab.TrySet(false) {
  192. t.Fatal("Expected TrySet(false) to fail")
  193. }
  194. if !ab.TrySet(true) {
  195. t.Fatal("Expected TrySet(true) to succeed")
  196. }
  197. if !ab.IsSet() {
  198. t.Fatal("Expected value to be true")
  199. }
  200. ab.Set(true)
  201. if !ab.IsSet() {
  202. t.Fatal("Expected value to be true")
  203. }
  204. if ab.TrySet(true) {
  205. t.Fatal("Expected TrySet(true) to fail")
  206. }
  207. if !ab.TrySet(false) {
  208. t.Fatal("Expected TrySet(false) to succeed")
  209. }
  210. if ab.IsSet() {
  211. t.Fatal("Expected value to be false")
  212. }
  213. ab._noCopy.Lock() // we've "tested" it ¯\_(ツ)_/¯
  214. }
  215. func TestAtomicError(t *testing.T) {
  216. var ae atomicError
  217. if ae.Value() != nil {
  218. t.Fatal("Expected value to be nil")
  219. }
  220. ae.Set(ErrMalformPkt)
  221. if v := ae.Value(); v != ErrMalformPkt {
  222. if v == nil {
  223. t.Fatal("Value is still nil")
  224. }
  225. t.Fatal("Error did not match")
  226. }
  227. ae.Set(ErrPktSync)
  228. if ae.Value() == ErrMalformPkt {
  229. t.Fatal("Error still matches old error")
  230. }
  231. if v := ae.Value(); v != ErrPktSync {
  232. t.Fatal("Error did not match")
  233. }
  234. }