connection_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2016 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. "context"
  11. "database/sql/driver"
  12. "encoding/json"
  13. "errors"
  14. "net"
  15. "testing"
  16. )
  17. func TestInterpolateParams(t *testing.T) {
  18. mc := &mysqlConn{
  19. buf: newBuffer(nil),
  20. maxAllowedPacket: maxPacketSize,
  21. cfg: &Config{
  22. InterpolateParams: true,
  23. },
  24. }
  25. q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42), "gopher"})
  26. if err != nil {
  27. t.Errorf("Expected err=nil, got %#v", err)
  28. return
  29. }
  30. expected := `SELECT 42+'gopher'`
  31. if q != expected {
  32. t.Errorf("Expected: %q\nGot: %q", expected, q)
  33. }
  34. }
  35. func TestInterpolateParamsJSONRawMessage(t *testing.T) {
  36. mc := &mysqlConn{
  37. buf: newBuffer(nil),
  38. maxAllowedPacket: maxPacketSize,
  39. cfg: &Config{
  40. InterpolateParams: true,
  41. },
  42. }
  43. buf, err := json.Marshal(struct {
  44. Value int `json:"value"`
  45. }{Value: 42})
  46. if err != nil {
  47. t.Errorf("Expected err=nil, got %#v", err)
  48. return
  49. }
  50. q, err := mc.interpolateParams("SELECT ?", []driver.Value{json.RawMessage(buf)})
  51. if err != nil {
  52. t.Errorf("Expected err=nil, got %#v", err)
  53. return
  54. }
  55. expected := `SELECT '{\"value\":42}'`
  56. if q != expected {
  57. t.Errorf("Expected: %q\nGot: %q", expected, q)
  58. }
  59. }
  60. func TestInterpolateParamsTooManyPlaceholders(t *testing.T) {
  61. mc := &mysqlConn{
  62. buf: newBuffer(nil),
  63. maxAllowedPacket: maxPacketSize,
  64. cfg: &Config{
  65. InterpolateParams: true,
  66. },
  67. }
  68. q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42)})
  69. if err != driver.ErrSkip {
  70. t.Errorf("Expected err=driver.ErrSkip, got err=%#v, q=%#v", err, q)
  71. }
  72. }
  73. // We don't support placeholder in string literal for now.
  74. // https://github.com/go-sql-driver/mysql/pull/490
  75. func TestInterpolateParamsPlaceholderInString(t *testing.T) {
  76. mc := &mysqlConn{
  77. buf: newBuffer(nil),
  78. maxAllowedPacket: maxPacketSize,
  79. cfg: &Config{
  80. InterpolateParams: true,
  81. },
  82. }
  83. q, err := mc.interpolateParams("SELECT 'abc?xyz',?", []driver.Value{int64(42)})
  84. // When InterpolateParams support string literal, this should return `"SELECT 'abc?xyz', 42`
  85. if err != driver.ErrSkip {
  86. t.Errorf("Expected err=driver.ErrSkip, got err=%#v, q=%#v", err, q)
  87. }
  88. }
  89. func TestInterpolateParamsUint64(t *testing.T) {
  90. mc := &mysqlConn{
  91. buf: newBuffer(nil),
  92. maxAllowedPacket: maxPacketSize,
  93. cfg: &Config{
  94. InterpolateParams: true,
  95. },
  96. }
  97. q, err := mc.interpolateParams("SELECT ?", []driver.Value{uint64(42)})
  98. if err != nil {
  99. t.Errorf("Expected err=nil, got err=%#v, q=%#v", err, q)
  100. }
  101. if q != "SELECT 42" {
  102. t.Errorf("Expected uint64 interpolation to work, got q=%#v", q)
  103. }
  104. }
  105. func TestCheckNamedValue(t *testing.T) {
  106. value := driver.NamedValue{Value: ^uint64(0)}
  107. x := &mysqlConn{}
  108. err := x.CheckNamedValue(&value)
  109. if err != nil {
  110. t.Fatal("uint64 high-bit not convertible", err)
  111. }
  112. if value.Value != ^uint64(0) {
  113. t.Fatalf("uint64 high-bit converted, got %#v %T", value.Value, value.Value)
  114. }
  115. }
  116. // TestCleanCancel tests passed context is cancelled at start.
  117. // No packet should be sent. Connection should keep current status.
  118. func TestCleanCancel(t *testing.T) {
  119. mc := &mysqlConn{
  120. closech: make(chan struct{}),
  121. }
  122. mc.startWatcher()
  123. defer mc.cleanup()
  124. ctx, cancel := context.WithCancel(context.Background())
  125. cancel()
  126. for i := 0; i < 3; i++ { // Repeat same behavior
  127. err := mc.Ping(ctx)
  128. if err != context.Canceled {
  129. t.Errorf("expected context.Canceled, got %#v", err)
  130. }
  131. if mc.closed.IsSet() {
  132. t.Error("expected mc is not closed, closed actually")
  133. }
  134. if mc.watching {
  135. t.Error("expected watching is false, but true")
  136. }
  137. }
  138. }
  139. func TestPingMarkBadConnection(t *testing.T) {
  140. nc := badConnection{err: errors.New("boom")}
  141. ms := &mysqlConn{
  142. netConn: nc,
  143. buf: newBuffer(nc),
  144. maxAllowedPacket: defaultMaxAllowedPacket,
  145. }
  146. err := ms.Ping(context.Background())
  147. if err != driver.ErrBadConn {
  148. t.Errorf("expected driver.ErrBadConn, got %#v", err)
  149. }
  150. }
  151. func TestPingErrInvalidConn(t *testing.T) {
  152. nc := badConnection{err: errors.New("failed to write"), n: 10}
  153. ms := &mysqlConn{
  154. netConn: nc,
  155. buf: newBuffer(nc),
  156. maxAllowedPacket: defaultMaxAllowedPacket,
  157. closech: make(chan struct{}),
  158. }
  159. err := ms.Ping(context.Background())
  160. if err != ErrInvalidConn {
  161. t.Errorf("expected ErrInvalidConn, got %#v", err)
  162. }
  163. }
  164. type badConnection struct {
  165. n int
  166. err error
  167. net.Conn
  168. }
  169. func (bc badConnection) Write(b []byte) (n int, err error) {
  170. return bc.n, bc.err
  171. }
  172. func (bc badConnection) Close() error {
  173. return nil
  174. }