utils_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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)
  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. expected, 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 TestFormatBinaryTime(t *testing.T) {
  116. expect := func(expected string, src []byte, outlen uint8) {
  117. actual, _ := formatBinaryTime(src, outlen)
  118. bytes, ok := actual.([]byte)
  119. if !ok {
  120. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  121. }
  122. if string(bytes) != expected {
  123. t.Errorf(
  124. "expected %q, got %q for src=%q and outlen=%d",
  125. expected, actual, src, outlen)
  126. }
  127. }
  128. // binary format:
  129. // sign (0: positive, 1: negative), days(4), hours, minutes, seconds, micro(4)
  130. // Zeros
  131. expect("00:00:00", []byte{}, 8)
  132. expect("00:00:00.0", []byte{}, 10)
  133. expect("00:00:00.000000", []byte{}, 15)
  134. // Without micro(4)
  135. expect("12:34:56", []byte{0, 0, 0, 0, 0, 12, 34, 56}, 8)
  136. expect("-12:34:56", []byte{1, 0, 0, 0, 0, 12, 34, 56}, 8)
  137. expect("12:34:56.00", []byte{0, 0, 0, 0, 0, 12, 34, 56}, 11)
  138. expect("24:34:56", []byte{0, 1, 0, 0, 0, 0, 34, 56}, 8)
  139. expect("-99:34:56", []byte{1, 4, 0, 0, 0, 3, 34, 56}, 8)
  140. expect("103079215103:34:56", []byte{0, 255, 255, 255, 255, 23, 34, 56}, 8)
  141. // With micro(4)
  142. expect("12:34:56.00", []byte{0, 0, 0, 0, 0, 12, 34, 56, 99, 0, 0, 0}, 11)
  143. expect("12:34:56.000099", []byte{0, 0, 0, 0, 0, 12, 34, 56, 99, 0, 0, 0}, 15)
  144. }
  145. func TestEscapeBackslash(t *testing.T) {
  146. expect := func(expected, value string) {
  147. actual := string(escapeBytesBackslash([]byte{}, []byte(value)))
  148. if actual != expected {
  149. t.Errorf(
  150. "expected %s, got %s",
  151. expected, actual,
  152. )
  153. }
  154. actual = string(escapeStringBackslash([]byte{}, value))
  155. if actual != expected {
  156. t.Errorf(
  157. "expected %s, got %s",
  158. expected, actual,
  159. )
  160. }
  161. }
  162. expect("foo\\0bar", "foo\x00bar")
  163. expect("foo\\nbar", "foo\nbar")
  164. expect("foo\\rbar", "foo\rbar")
  165. expect("foo\\Zbar", "foo\x1abar")
  166. expect("foo\\\"bar", "foo\"bar")
  167. expect("foo\\\\bar", "foo\\bar")
  168. expect("foo\\'bar", "foo'bar")
  169. }
  170. func TestEscapeQuotes(t *testing.T) {
  171. expect := func(expected, value string) {
  172. actual := string(escapeBytesQuotes([]byte{}, []byte(value)))
  173. if actual != expected {
  174. t.Errorf(
  175. "expected %s, got %s",
  176. expected, actual,
  177. )
  178. }
  179. actual = string(escapeStringQuotes([]byte{}, value))
  180. if actual != expected {
  181. t.Errorf(
  182. "expected %s, got %s",
  183. expected, actual,
  184. )
  185. }
  186. }
  187. expect("foo\x00bar", "foo\x00bar") // not affected
  188. expect("foo\nbar", "foo\nbar") // not affected
  189. expect("foo\rbar", "foo\rbar") // not affected
  190. expect("foo\x1abar", "foo\x1abar") // not affected
  191. expect("foo''bar", "foo'bar") // affected
  192. expect("foo\"bar", "foo\"bar") // not affected
  193. }
  194. func TestAtomicBool(t *testing.T) {
  195. var ab atomicBool
  196. if ab.IsSet() {
  197. t.Fatal("Expected value to be false")
  198. }
  199. ab.Set(true)
  200. if ab.value != 1 {
  201. t.Fatal("Set(true) did not set value to 1")
  202. }
  203. if !ab.IsSet() {
  204. t.Fatal("Expected value to be true")
  205. }
  206. ab.Set(true)
  207. if !ab.IsSet() {
  208. t.Fatal("Expected value to be true")
  209. }
  210. ab.Set(false)
  211. if ab.value != 0 {
  212. t.Fatal("Set(false) did not set value to 0")
  213. }
  214. if ab.IsSet() {
  215. t.Fatal("Expected value to be false")
  216. }
  217. ab.Set(false)
  218. if ab.IsSet() {
  219. t.Fatal("Expected value to be false")
  220. }
  221. if ab.TrySet(false) {
  222. t.Fatal("Expected TrySet(false) to fail")
  223. }
  224. if !ab.TrySet(true) {
  225. t.Fatal("Expected TrySet(true) to succeed")
  226. }
  227. if !ab.IsSet() {
  228. t.Fatal("Expected value to be true")
  229. }
  230. ab.Set(true)
  231. if !ab.IsSet() {
  232. t.Fatal("Expected value to be true")
  233. }
  234. if ab.TrySet(true) {
  235. t.Fatal("Expected TrySet(true) to fail")
  236. }
  237. if !ab.TrySet(false) {
  238. t.Fatal("Expected TrySet(false) to succeed")
  239. }
  240. if ab.IsSet() {
  241. t.Fatal("Expected value to be false")
  242. }
  243. ab._noCopy.Lock() // we've "tested" it ¯\_(ツ)_/¯
  244. }
  245. func TestAtomicError(t *testing.T) {
  246. var ae atomicError
  247. if ae.Value() != nil {
  248. t.Fatal("Expected value to be nil")
  249. }
  250. ae.Set(ErrMalformPkt)
  251. if v := ae.Value(); v != ErrMalformPkt {
  252. if v == nil {
  253. t.Fatal("Value is still nil")
  254. }
  255. t.Fatal("Error did not match")
  256. }
  257. ae.Set(ErrPktSync)
  258. if ae.Value() == ErrMalformPkt {
  259. t.Fatal("Error still matches old error")
  260. }
  261. if v := ae.Value(); v != ErrPktSync {
  262. t.Fatal("Error did not match")
  263. }
  264. }