utils_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. "database/sql"
  12. "database/sql/driver"
  13. "encoding/binary"
  14. "testing"
  15. "time"
  16. )
  17. func TestScanNullTime(t *testing.T) {
  18. var scanTests = []struct {
  19. in interface{}
  20. error bool
  21. valid bool
  22. time time.Time
  23. }{
  24. {tDate, false, true, tDate},
  25. {sDate, false, true, tDate},
  26. {[]byte(sDate), false, true, tDate},
  27. {tDateTime, false, true, tDateTime},
  28. {sDateTime, false, true, tDateTime},
  29. {[]byte(sDateTime), false, true, tDateTime},
  30. {tDate0, false, true, tDate0},
  31. {sDate0, false, true, tDate0},
  32. {[]byte(sDate0), false, true, tDate0},
  33. {sDateTime0, false, true, tDate0},
  34. {[]byte(sDateTime0), false, true, tDate0},
  35. {"", true, false, tDate0},
  36. {"1234", true, false, tDate0},
  37. {0, true, false, tDate0},
  38. }
  39. var nt = NullTime{}
  40. var err error
  41. for _, tst := range scanTests {
  42. err = nt.Scan(tst.in)
  43. if (err != nil) != tst.error {
  44. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  45. }
  46. if nt.Valid != tst.valid {
  47. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  48. }
  49. if nt.Time != tst.time {
  50. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  51. }
  52. }
  53. }
  54. func TestLengthEncodedInteger(t *testing.T) {
  55. var integerTests = []struct {
  56. num uint64
  57. encoded []byte
  58. }{
  59. {0x0000000000000000, []byte{0x00}},
  60. {0x0000000000000012, []byte{0x12}},
  61. {0x00000000000000fa, []byte{0xfa}},
  62. {0x0000000000000100, []byte{0xfc, 0x00, 0x01}},
  63. {0x0000000000001234, []byte{0xfc, 0x34, 0x12}},
  64. {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}},
  65. {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}},
  66. {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}},
  67. {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}},
  68. {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}},
  69. {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}},
  70. {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  71. }
  72. for _, tst := range integerTests {
  73. num, isNull, numLen := readLengthEncodedInteger(tst.encoded)
  74. if isNull {
  75. t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num)
  76. }
  77. if num != tst.num {
  78. t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num)
  79. }
  80. if numLen != len(tst.encoded) {
  81. t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen)
  82. }
  83. encoded := appendLengthEncodedInteger(nil, num)
  84. if !bytes.Equal(encoded, tst.encoded) {
  85. t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded)
  86. }
  87. }
  88. }
  89. func TestFormatBinaryDateTime(t *testing.T) {
  90. rawDate := [11]byte{}
  91. binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years
  92. rawDate[2] = 12 // months
  93. rawDate[3] = 30 // days
  94. rawDate[4] = 15 // hours
  95. rawDate[5] = 46 // minutes
  96. rawDate[6] = 23 // seconds
  97. binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds
  98. expect := func(expected string, inlen, outlen uint8) {
  99. actual, _ := formatBinaryDateTime(rawDate[:inlen], outlen)
  100. bytes, ok := actual.([]byte)
  101. if !ok {
  102. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  103. }
  104. if string(bytes) != expected {
  105. t.Errorf(
  106. "expected %q, got %q for length in %d, out %d",
  107. expected, actual, inlen, outlen,
  108. )
  109. }
  110. }
  111. expect("0000-00-00", 0, 10)
  112. expect("0000-00-00 00:00:00", 0, 19)
  113. expect("1978-12-30", 4, 10)
  114. expect("1978-12-30 15:46:23", 7, 19)
  115. expect("1978-12-30 15:46:23.987654", 11, 26)
  116. }
  117. func TestFormatBinaryTime(t *testing.T) {
  118. expect := func(expected string, src []byte, outlen uint8) {
  119. actual, _ := formatBinaryTime(src, outlen)
  120. bytes, ok := actual.([]byte)
  121. if !ok {
  122. t.Errorf("formatBinaryDateTime must return []byte, was %T", actual)
  123. }
  124. if string(bytes) != expected {
  125. t.Errorf(
  126. "expected %q, got %q for src=%q and outlen=%d",
  127. expected, actual, src, outlen)
  128. }
  129. }
  130. // binary format:
  131. // sign (0: positive, 1: negative), days(4), hours, minutes, seconds, micro(4)
  132. // Zeros
  133. expect("00:00:00", []byte{}, 8)
  134. expect("00:00:00.0", []byte{}, 10)
  135. expect("00:00:00.000000", []byte{}, 15)
  136. // Without micro(4)
  137. expect("12:34:56", []byte{0, 0, 0, 0, 0, 12, 34, 56}, 8)
  138. expect("-12:34:56", []byte{1, 0, 0, 0, 0, 12, 34, 56}, 8)
  139. expect("12:34:56.00", []byte{0, 0, 0, 0, 0, 12, 34, 56}, 11)
  140. expect("24:34:56", []byte{0, 1, 0, 0, 0, 0, 34, 56}, 8)
  141. expect("-99:34:56", []byte{1, 4, 0, 0, 0, 3, 34, 56}, 8)
  142. expect("103079215103:34:56", []byte{0, 255, 255, 255, 255, 23, 34, 56}, 8)
  143. // With micro(4)
  144. expect("12:34:56.00", []byte{0, 0, 0, 0, 0, 12, 34, 56, 99, 0, 0, 0}, 11)
  145. expect("12:34:56.000099", []byte{0, 0, 0, 0, 0, 12, 34, 56, 99, 0, 0, 0}, 15)
  146. }
  147. func TestEscapeBackslash(t *testing.T) {
  148. expect := func(expected, value string) {
  149. actual := string(escapeBytesBackslash([]byte{}, []byte(value)))
  150. if actual != expected {
  151. t.Errorf(
  152. "expected %s, got %s",
  153. expected, actual,
  154. )
  155. }
  156. actual = string(escapeStringBackslash([]byte{}, value))
  157. if actual != expected {
  158. t.Errorf(
  159. "expected %s, got %s",
  160. expected, actual,
  161. )
  162. }
  163. }
  164. expect("foo\\0bar", "foo\x00bar")
  165. expect("foo\\nbar", "foo\nbar")
  166. expect("foo\\rbar", "foo\rbar")
  167. expect("foo\\Zbar", "foo\x1abar")
  168. expect("foo\\\"bar", "foo\"bar")
  169. expect("foo\\\\bar", "foo\\bar")
  170. expect("foo\\'bar", "foo'bar")
  171. }
  172. func TestEscapeQuotes(t *testing.T) {
  173. expect := func(expected, value string) {
  174. actual := string(escapeBytesQuotes([]byte{}, []byte(value)))
  175. if actual != expected {
  176. t.Errorf(
  177. "expected %s, got %s",
  178. expected, actual,
  179. )
  180. }
  181. actual = string(escapeStringQuotes([]byte{}, value))
  182. if actual != expected {
  183. t.Errorf(
  184. "expected %s, got %s",
  185. expected, actual,
  186. )
  187. }
  188. }
  189. expect("foo\x00bar", "foo\x00bar") // not affected
  190. expect("foo\nbar", "foo\nbar") // not affected
  191. expect("foo\rbar", "foo\rbar") // not affected
  192. expect("foo\x1abar", "foo\x1abar") // not affected
  193. expect("foo''bar", "foo'bar") // affected
  194. expect("foo\"bar", "foo\"bar") // not affected
  195. }
  196. func TestAtomicBool(t *testing.T) {
  197. var ab atomicBool
  198. if ab.IsSet() {
  199. t.Fatal("Expected value to be false")
  200. }
  201. ab.Set(true)
  202. if ab.value != 1 {
  203. t.Fatal("Set(true) did not set value to 1")
  204. }
  205. if !ab.IsSet() {
  206. t.Fatal("Expected value to be true")
  207. }
  208. ab.Set(true)
  209. if !ab.IsSet() {
  210. t.Fatal("Expected value to be true")
  211. }
  212. ab.Set(false)
  213. if ab.value != 0 {
  214. t.Fatal("Set(false) did not set value to 0")
  215. }
  216. if ab.IsSet() {
  217. t.Fatal("Expected value to be false")
  218. }
  219. ab.Set(false)
  220. if ab.IsSet() {
  221. t.Fatal("Expected value to be false")
  222. }
  223. if ab.TrySet(false) {
  224. t.Fatal("Expected TrySet(false) to fail")
  225. }
  226. if !ab.TrySet(true) {
  227. t.Fatal("Expected TrySet(true) to succeed")
  228. }
  229. if !ab.IsSet() {
  230. t.Fatal("Expected value to be true")
  231. }
  232. ab.Set(true)
  233. if !ab.IsSet() {
  234. t.Fatal("Expected value to be true")
  235. }
  236. if ab.TrySet(true) {
  237. t.Fatal("Expected TrySet(true) to fail")
  238. }
  239. if !ab.TrySet(false) {
  240. t.Fatal("Expected TrySet(false) to succeed")
  241. }
  242. if ab.IsSet() {
  243. t.Fatal("Expected value to be false")
  244. }
  245. ab._noCopy.Lock() // we've "tested" it ¯\_(ツ)_/¯
  246. }
  247. func TestAtomicError(t *testing.T) {
  248. var ae atomicError
  249. if ae.Value() != nil {
  250. t.Fatal("Expected value to be nil")
  251. }
  252. ae.Set(ErrMalformPkt)
  253. if v := ae.Value(); v != ErrMalformPkt {
  254. if v == nil {
  255. t.Fatal("Value is still nil")
  256. }
  257. t.Fatal("Error did not match")
  258. }
  259. ae.Set(ErrPktSync)
  260. if ae.Value() == ErrMalformPkt {
  261. t.Fatal("Error still matches old error")
  262. }
  263. if v := ae.Value(); v != ErrPktSync {
  264. t.Fatal("Error did not match")
  265. }
  266. }
  267. func TestIsolationLevelMapping(t *testing.T) {
  268. data := []struct {
  269. level driver.IsolationLevel
  270. expected string
  271. }{
  272. {
  273. level: driver.IsolationLevel(sql.LevelReadCommitted),
  274. expected: "READ COMMITTED",
  275. },
  276. {
  277. level: driver.IsolationLevel(sql.LevelRepeatableRead),
  278. expected: "REPEATABLE READ",
  279. },
  280. {
  281. level: driver.IsolationLevel(sql.LevelReadUncommitted),
  282. expected: "READ UNCOMMITTED",
  283. },
  284. {
  285. level: driver.IsolationLevel(sql.LevelSerializable),
  286. expected: "SERIALIZABLE",
  287. },
  288. }
  289. for i, td := range data {
  290. if actual, err := mapIsolationLevel(td.level); actual != td.expected || err != nil {
  291. t.Fatal(i, td.expected, actual, err)
  292. }
  293. }
  294. // check unsupported mapping
  295. expectedErr := "mysql: unsupported isolation level: 7"
  296. actual, err := mapIsolationLevel(driver.IsolationLevel(sql.LevelLinearizable))
  297. if actual != "" || err == nil {
  298. t.Fatal("Expected error on unsupported isolation level")
  299. }
  300. if err.Error() != expectedErr {
  301. t.Fatalf("Expected error to be %q, got %q", expectedErr, err)
  302. }
  303. }