utils_test.go 7.8 KB

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