|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-mysql-org/go-mysql/mysql" |
| 10 | + "github.com/go-mysql-org/go-mysql/packet" |
| 11 | +) |
| 12 | + |
| 13 | +func captureAttributeRequest(t *testing.T, c *Conn, peer *packet.Conn, send func() error) []byte { |
| 14 | + t.Helper() |
| 15 | + done := make(chan error, 1) |
| 16 | + go func() { done <- send() }() |
| 17 | + peer.ResetSequence() |
| 18 | + payload, err := peer.ReadPacket() |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + if err := peer.WritePacket([]byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0}); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + if err := <-done; err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + return payload |
| 29 | +} |
| 30 | + |
| 31 | +func attributeConnection(t *testing.T) (*Conn, *packet.Conn) { |
| 32 | + a, b := net.Pipe() |
| 33 | + _ = a.SetDeadline(time.Now().Add(5 * time.Second)) |
| 34 | + _ = b.SetDeadline(time.Now().Add(5 * time.Second)) |
| 35 | + t.Cleanup(func() { a.Close(); b.Close() }) |
| 36 | + return &Conn{Conn: packet.NewConn(a), capability: mysql.CLIENT_QUERY_ATTRIBUTES, includeLine: -1}, packet.NewConn(b) |
| 37 | +} |
| 38 | + |
| 39 | +func TestQueryAttributeAttributesConsumed(t *testing.T) { |
| 40 | + c, p := attributeConnection(t) |
| 41 | + c.SetQueryAttributes(mysql.QueryAttribute{Name: "trace", Value: "first"}) |
| 42 | + first := captureAttributeRequest(t, c, p, func() error { _, err := c.Execute("SELECT 1"); return err }) |
| 43 | + second := captureAttributeRequest(t, c, p, func() error { _, err := c.Execute("SELECT 2"); return err }) |
| 44 | + t.Logf("first=%x second=%x", first, second) |
| 45 | + if second[1] != 0 { |
| 46 | + t.Fatalf("next query still announces %d attributes; want zero", second[1]) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestQueryAttributeZeroParameterAttributes(t *testing.T) { |
| 51 | + c, p := attributeConnection(t) |
| 52 | + c.SetQueryAttributes(mysql.QueryAttribute{Name: "trace", Value: "marker"}) |
| 53 | + s := &Stmt{conn: c} |
| 54 | + data := captureAttributeRequest(t, c, p, func() error { _, err := s.Execute(); return err }) |
| 55 | + t.Logf("execute=%x", data) |
| 56 | + if !bytes.Contains(data, []byte("marker")) { |
| 57 | + t.Fatal("zero-parameter execution lost its query attribute") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestQueryAttributeNullParameterAttributes(t *testing.T) { |
| 62 | + c, p := attributeConnection(t) |
| 63 | + c.SetQueryAttributes(mysql.QueryAttribute{Name: "trace", Value: "marker"}) |
| 64 | + s := &Stmt{conn: c} |
| 65 | + s.Params = 1 |
| 66 | + data := captureAttributeRequest(t, c, p, func() error { _, err := s.Execute(nil); return err }) |
| 67 | + t.Logf("execute=%x", data) |
| 68 | + if !bytes.Contains(data, []byte("marker")) { |
| 69 | + t.Fatal("NULL-parameter execution lost its query attribute") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestQueryAttributeCallerAttributesReusable(t *testing.T) { |
| 74 | + c, p := attributeConnection(t) |
| 75 | + attrs := []mysql.QueryAttribute{{Name: "trace", Value: "marker"}} |
| 76 | + c.SetQueryAttributes(attrs...) |
| 77 | + captureAttributeRequest(t, c, p, func() error { _, err := c.Execute("SELECT 1"); return err }) |
| 78 | + if attrs[0].Name != "trace" || attrs[0].Value != "marker" { |
| 79 | + t.Fatal("sending query mutated caller attribute slice") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestQueryAttributeLineAttributeDoesNotAccumulate(t *testing.T) { |
| 84 | + c, p := attributeConnection(t) |
| 85 | + c.IncludeLine(0) |
| 86 | + for i := 0; i < 3; i++ { |
| 87 | + data := captureAttributeRequest(t, c, p, func() error { _, err := c.Execute("SELECT 1"); return err }) |
| 88 | + if data[1] != 1 { |
| 89 | + t.Fatalf("query %d has %d attributes; want one line attribute", i, data[1]) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestQueryAttributeUnsupportedCapability(t *testing.T) { |
| 95 | + c, p := attributeConnection(t) |
| 96 | + c.capability = 0 |
| 97 | + c.SetQueryAttributes(mysql.QueryAttribute{Name: "trace", Value: "marker"}) |
| 98 | + s := &Stmt{conn: c} |
| 99 | + s.Params = 1 |
| 100 | + data := captureAttributeRequest(t, c, p, func() error { _, err := s.Execute(7); return err }) |
| 101 | + if data[5] != 0 { |
| 102 | + t.Fatalf("legacy execute has unsupported flags %x", data[5]) |
| 103 | + } |
| 104 | + if bytes.Contains(data, []byte("marker")) { |
| 105 | + t.Fatal("attributes encoded without negotiated support") |
| 106 | + } |
| 107 | +} |
0 commit comments