-
Notifications
You must be signed in to change notification settings - Fork 2
fix(redis): validate CLIENT subcommand arity + distinguish string/bulk in test stub #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+198
−22
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ import ( | |
| // array-header) is the only way to distinguish `proto` (integer 2) | ||
| // from `version` (bulk "7.0.0") or `modules` (empty array header). | ||
| type helloReplyElement struct { | ||
| kind string // "bulk" | "int" | "arrayHeader" | ||
| kind string // "bulk" | "string" | "int" | "arrayHeader" | "null" | ||
| str string | ||
| num int64 | ||
| } | ||
|
|
@@ -46,7 +46,10 @@ func (c *helloRecordingConn) Close() error { return nil } | |
| func (c *helloRecordingConn) WriteError(msg string) { c.err = msg } | ||
| func (c *helloRecordingConn) WriteString(s string) { | ||
| c.str = s | ||
| c.writes = append(c.writes, helloReplyElement{kind: "bulk", str: s}) | ||
| // Recorded as a distinct "string" kind so a future regression that | ||
| // swaps WriteBulkString for WriteString (or vice versa) fails the | ||
| // wire-shape assertions instead of passing silently. | ||
| c.writes = append(c.writes, helloReplyElement{kind: "string", str: s}) | ||
| } | ||
| func (c *helloRecordingConn) WriteBulk(bulk []byte) { | ||
| c.writes = append(c.writes, helloReplyElement{kind: "bulk", str: string(bulk)}) | ||
|
|
@@ -419,3 +422,110 @@ elastickv_redis_requests_total{command="HELLO",node_address="10.0.0.1:50051",nod | |
| ) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestClient_SetName_RejectsWrongArity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newHelloTestServer(t, true) | ||
| conn := &helloRecordingConn{} | ||
| state := getConnState(conn) | ||
| state.clientName = "prev" | ||
|
|
||
| // CLIENT SETNAME with no value (2 args total). | ||
| r.client(conn, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("SETNAME"), | ||
| }}) | ||
|
Comment on lines
+435
to
+437
|
||
| require.Contains(t, conn.err, "wrong number of arguments") | ||
| require.Equal(t, "prev", state.clientName, "malformed SETNAME must not clobber clientName") | ||
|
|
||
| // CLIENT SETNAME foo bar (4 args total) — also rejected. | ||
| conn2 := &helloRecordingConn{} | ||
| state2 := getConnState(conn2) | ||
| state2.clientName = "prev" | ||
| r.client(conn2, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("SETNAME"), []byte("foo"), []byte("bar"), | ||
| }}) | ||
| require.Contains(t, conn2.err, "wrong number of arguments") | ||
| require.Equal(t, "prev", state2.clientName) | ||
| } | ||
|
|
||
| func TestClient_GetName_RejectsWrongArity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newHelloTestServer(t, true) | ||
| conn := &helloRecordingConn{} | ||
|
|
||
| // CLIENT GETNAME extra (3 args total) — GETNAME takes no operand. | ||
| r.client(conn, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("GETNAME"), []byte("extra"), | ||
| }}) | ||
| require.Contains(t, conn.err, "wrong number of arguments") | ||
| } | ||
|
|
||
| func TestClient_ID_RejectsWrongArity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newHelloTestServer(t, true) | ||
| conn := &helloRecordingConn{} | ||
|
|
||
| // CLIENT ID junk (3 args total) — ID takes no operand. | ||
| r.client(conn, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("ID"), []byte("junk"), | ||
| }}) | ||
| require.Contains(t, conn.err, "wrong number of arguments") | ||
| } | ||
|
|
||
| func TestClient_Info_RejectsWrongArity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newHelloTestServer(t, true) | ||
| conn := &helloRecordingConn{} | ||
|
|
||
| // CLIENT INFO extra (3 args total) — INFO takes no operand. | ||
| r.client(conn, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("INFO"), []byte("extra"), | ||
| }}) | ||
| require.Contains(t, conn.err, "wrong number of arguments") | ||
| } | ||
|
|
||
| // CLIENT SETINFO must enforce exact arity too; the prior implementation | ||
| // returned OK for any arity including zero operands, which silently | ||
| // hid client bugs that real Redis would reject as wrong-arity. Pin the | ||
| // "missing operands" and "extra operand" cases so a regression cannot | ||
| // re-introduce the silent-success behaviour. | ||
| func TestClient_SetInfo_RejectsWrongArity(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| r := newHelloTestServer(t, true) | ||
|
|
||
| // CLIENT SETINFO with no operands (2 args total). | ||
| conn := &helloRecordingConn{} | ||
| r.client(conn, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("SETINFO"), | ||
| }}) | ||
| require.Contains(t, conn.err, "wrong number of arguments") | ||
| require.NotEqual(t, "OK", conn.str, | ||
| "missing-operand SETINFO must not silently return OK") | ||
|
|
||
| // CLIENT SETINFO attr (3 args total) — missing value. | ||
| conn2 := &helloRecordingConn{} | ||
| r.client(conn2, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("SETINFO"), []byte("lib-name"), | ||
| }}) | ||
| require.Contains(t, conn2.err, "wrong number of arguments") | ||
|
|
||
| // CLIENT SETINFO attr value extra (5 args total) — extra operand. | ||
| conn3 := &helloRecordingConn{} | ||
| r.client(conn3, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("SETINFO"), []byte("lib-name"), []byte("redis-go"), []byte("extra"), | ||
| }}) | ||
| require.Contains(t, conn3.err, "wrong number of arguments") | ||
|
|
||
| // CLIENT SETINFO attr value (4 args total) — accepted shape. | ||
| conn4 := &helloRecordingConn{} | ||
| r.client(conn4, redcon.Command{Args: [][]byte{ | ||
| []byte(cmdClient), []byte("SETINFO"), []byte("lib-name"), []byte("redis-go"), | ||
| }}) | ||
| require.Empty(t, conn4.err, "well-formed SETINFO must not produce an error") | ||
| require.Equal(t, "OK", conn4.str, "well-formed SETINFO replies OK") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CLIENT SETINFOstill returnsOKfor any arity (including missing operands). This leaves one CLIENT subcommand with the same silent-success behavior this PR is trying to eliminate and diverges from Redis’ wrong-arity behavior forclient|setinfo. Consider validatingSETINFO’s expected argument count (or at least rejecting obviously wrong arity) usingcheckClientArityfor consistency.