From f45e82e93926a673db2ef8b424e5a52abb10ba87 Mon Sep 17 00:00:00 2001 From: YangKeao Date: Mon, 13 Feb 2023 18:16:02 +0800 Subject: [PATCH] This is an automated cherry-pick of #41340 Signed-off-by: ti-chi-bot --- server/conn_stmt.go | 10 ++++++ server/conn_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/server/conn_stmt.go b/server/conn_stmt.go index 436b2d1721ca0..255cb046674ec 100644 --- a/server/conn_stmt.go +++ b/server/conn_stmt.go @@ -259,6 +259,16 @@ func (cc *clientConn) executePreparedStmtAndWriteResult(ctx context.Context, stm BinaryArgs: args, PrepStmt: prepStmt, } + + // For the combination of `ComPrepare` and `ComExecute`, the statement name is stored in the client side, and the + // TiDB only has the ID, so don't try to construct an `EXECUTE SOMETHING`. Use the original prepared statement here + // instead. + sql := "" + planCacheStmt, ok := prepStmt.(*plannercore.PlanCacheStmt) + if ok { + sql = planCacheStmt.StmtText + } + execStmt.SetText(charset.EncodingUTF8Impl, sql) rs, err := (&cc.ctx).ExecuteStmt(ctx, execStmt) if err != nil { return true, errors.Annotate(err, cc.preparedStmt2String(uint32(stmt.ID()))) diff --git a/server/conn_test.go b/server/conn_test.go index fcfdc2c9b5a42..be25c303fbe4a 100644 --- a/server/conn_test.go +++ b/server/conn_test.go @@ -1799,3 +1799,83 @@ func TestExtensionChangeUser(t *testing.T) { require.Equal(t, expectedConnInfo.Error, logInfo.Error) require.Equal(t, *(expectedConnInfo.ConnectionInfo), *(logInfo.ConnectionInfo)) } +<<<<<<< HEAD +======= + +func TestAuthSha(t *testing.T) { + store := testkit.CreateMockStore(t) + + var outBuffer bytes.Buffer + tidbdrv := NewTiDBDriver(store) + cfg := newTestConfig() + cfg.Port, cfg.Status.StatusPort = 0, 0 + cfg.Status.ReportStatus = false + server, err := NewServer(cfg, tidbdrv) + require.NoError(t, err) + defer server.Close() + + cc := &clientConn{ + connectionID: 1, + salt: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14}, + server: server, + pkt: &packetIO{ + bufWriter: bufio.NewWriter(&outBuffer), + }, + collation: mysql.DefaultCollationID, + peerHost: "localhost", + alloc: arena.NewAllocator(512), + chunkAlloc: chunk.NewAllocator(), + capability: mysql.ClientProtocol41, + } + + tk := testkit.NewTestKit(t, store) + ctx := &TiDBContext{Session: tk.Session()} + cc.setCtx(ctx) + + resp := handshakeResponse41{ + Capability: mysql.ClientProtocol41 | mysql.ClientPluginAuth, + AuthPlugin: mysql.AuthCachingSha2Password, + Auth: []byte{}, // No password + } + + authData, err := cc.authSha(context.Background(), resp) + require.NoError(t, err) + + // If no password is specified authSha() should return an empty byte slice + // which differs from when a password is specified as that should trigger + // fastAuthFail and the rest of the auth process. + require.Equal(t, authData, []byte{}) +} + +func TestProcessInfoForExecuteCommand(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + cc := &clientConn{ + alloc: arena.NewAllocator(1024), + chunkAlloc: chunk.NewAllocator(), + pkt: &packetIO{ + bufWriter: bufio.NewWriter(bytes.NewBuffer(nil)), + }, + } + ctx := context.Background() + + tk.MustExec("use test") + cc.setCtx(&TiDBContext{Session: tk.Session(), stmts: make(map[int]*TiDBStatement)}) + + tk.MustExec("create table t (col1 int)") + + // simple prepare and execute + require.NoError(t, cc.handleStmtPrepare(ctx, "select sum(col1) from t")) + require.NoError(t, cc.handleStmtExecute(ctx, []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0})) + require.Equal(t, cc.ctx.Session.ShowProcess().Info, "select sum(col1) from t") + + // prepare and execute with params + require.NoError(t, cc.handleStmtPrepare(ctx, "select sum(col1) from t where col1 < ? and col1 > 100")) + // 1 params, length of nullBitMap is 1, `0x8, 0x0` represents the type, and the following `0x10, 0x0....` is the param + // 10 + require.NoError(t, cc.handleStmtExecute(ctx, []byte{0x2, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, + 0x1, 0x8, 0x0, + 0x0A, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})) + require.Equal(t, cc.ctx.Session.ShowProcess().Info, "select sum(col1) from t where col1 < ? and col1 > 100") +} +>>>>>>> be740110f4 (server, execute: set text for execute command (#41340))