Skip to content

Commit

Permalink
update to redis 6.2
Browse files Browse the repository at this point in the history
And update code for changed error messages.
  • Loading branch information
alicebob committed Jun 9, 2021
1 parent 0c67b61 commit ecf92af
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
38 changes: 21 additions & 17 deletions cmd_connection.go
Expand Up @@ -108,30 +108,34 @@ func (m *Miniredis) cmdHello(c *server.Peer, cmd string, args []string) {
return
}

var opts struct {
version int
username, password string
}

versionArg, args := args[0], args[1:]
var version int
switch versionArg {
case "2":
version = 2
case "3":
version = 3
var err error
opts.version, err = strconv.Atoi(versionArg)
if err != nil {
c.WriteError("ERR Protocol version is not an integer or out of range")
return
}
switch opts.version {
case 2, 3:
default:
c.WriteError("NOPROTO unsupported protocol version")
return
}

var (
checkAuth bool
username, password string
)
var checkAuth bool
for len(args) > 0 {
switch strings.ToUpper(args[0]) {
case "AUTH":
if len(args) < 3 {
c.WriteError(fmt.Sprintf("ERR Syntax error in HELLO option '%s'", args[0]))
return
}
username, password, args = args[1], args[2], args[3:]
opts.username, opts.password, args = args[1], args[2], args[3:]
checkAuth = true
case "SETNAME":
if len(args) < 2 {
Expand All @@ -145,32 +149,32 @@ func (m *Miniredis) cmdHello(c *server.Peer, cmd string, args []string) {
}
}

if len(m.passwords) == 0 && username == "default" {
if len(m.passwords) == 0 && opts.username == "default" {
// redis ignores legacy "AUTH" if it's not enabled.
checkAuth = false
}
if checkAuth {
setPW, ok := m.passwords[username]
setPW, ok := m.passwords[opts.username]
if !ok {
c.WriteError("WRONGPASS invalid username-password pair")
return
}
if setPW != password {
if setPW != opts.password {
c.WriteError("WRONGPASS invalid username-password pair")
return
}
getCtx(c).authenticated = true
}

c.Resp3 = version == 3
c.Resp3 = opts.version == 3

c.WriteMapLen(7)
c.WriteBulk("server")
c.WriteBulk("miniredis")
c.WriteBulk("version")
c.WriteBulk("6.0.5")
c.WriteBulk("proto")
c.WriteInt(version)
c.WriteInt(opts.version)
c.WriteBulk("id")
c.WriteInt(42)
c.WriteBulk("mode")
Expand Down Expand Up @@ -218,7 +222,7 @@ func (m *Miniredis) cmdSelect(c *server.Peer, cmd string, args []string) {
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
id, err := strconv.Atoi(args[0])
if err != nil {
c.WriteError("ERR invalid DB index")
c.WriteError(msgInvalidInt)
setDirty(c)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd_connection_test.go
Expand Up @@ -323,7 +323,7 @@ func TestHello(t *testing.T) {
)
mustDo(t, c,
"HELLO", "foo",
proto.Error("NOPROTO unsupported protocol version"),
proto.Error("ERR Protocol version is not an integer or out of range"),
)
mustDo(t, c,
"HELLO", "3", "AUTH", "foo",
Expand Down
4 changes: 2 additions & 2 deletions cmd_string_test.go
Expand Up @@ -502,7 +502,7 @@ func TestIncrBy(t *testing.T) {
)
}

// Amount not an interger
// Amount not an integer
mustDo(t, c,
"INCRBY", "key", "noint",
proto.Error(msgInvalidInt),
Expand Down Expand Up @@ -643,7 +643,7 @@ func TestDecrBy(t *testing.T) {
)
}

// Amount not an interger
// Amount not an integer
mustDo(t, c,
"DECRBY", "key", "noint",
proto.Error(msgInvalidInt),
Expand Down
11 changes: 7 additions & 4 deletions integration/connection_test.go
Expand Up @@ -60,7 +60,7 @@ func TestSelect(t *testing.T) {

c.Error("wrong number", "SELECT")
c.Error("out of range", "SELECT", "-1")
c.Error("invalid DB", "SELECT", "aap")
c.Error("not an integer", "SELECT", "aap")
c.Error("wrong number", "SELECT", "1", "2")
})

Expand Down Expand Up @@ -153,17 +153,20 @@ func TestHello(t *testing.T) {
c.DoLoosely("HELLO", "2")
c.Do("SMEMBERS", "s")

c.Error("unsupported", "HELLO", "twoandahalf")
c.Error("not an integer", "HELLO", "twoandahalf")

c.DoLoosely("HELLO", "3", "AUTH", "default", "foo")
c.DoLoosely("HELLO", "3", "AUTH", "default", "foo", "SETNAME", "foo")
c.DoLoosely("HELLO", "3", "SETNAME", "foo")

// errors
c.Error("Syntax error", "HELLO", "3", "default", "foo")
c.Error("unsupported", "HELLO", "three", "AUTH", "default", "foo")
c.Error("not an integer", "HELLO", "three", "AUTH", "default", "foo")
c.Error("Syntax error", "HELLO", "3", "AUTH", "default")
c.Error("unsupported", "HELLO", "default", "foo")
c.Error("unsupported", "HELLO", "-1", "foo")
c.Error("unsupported", "HELLO", "0", "foo")
c.Error("unsupported", "HELLO", "1", "foo")
c.Error("unsupported", "HELLO", "4", "foo")
c.Error("Syntax error", "HELLO", "3", "default", "foo", "SETNAME")
c.Error("Syntax error", "HELLO", "3", "SETNAME")

Expand Down
2 changes: 1 addition & 1 deletion integration/get_redis.sh
Expand Up @@ -2,7 +2,7 @@

set -eu

VERSION=6.0.10
VERSION=6.2.4

rm -rf ./redis_src/
mkdir -p ./redis_src/
Expand Down

0 comments on commit ecf92af

Please sign in to comment.