From fe881ba03ca8bb37d41b9ab877760261cb0cbed1 Mon Sep 17 00:00:00 2001 From: holys Date: Fri, 25 Jul 2014 17:26:29 +0800 Subject: [PATCH] add errer input params test for kv; fix typo for t_kv.go --- ledis/t_kv.go | 2 +- server/cmd_bit.go | 4 +-- server/cmd_hash.go | 6 ++-- server/cmd_kv.go | 10 +++--- server/cmd_kv_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++ server/cmd_list.go | 10 +++--- server/cmd_zset.go | 8 ++--- server/const.go | 7 ++-- 8 files changed, 103 insertions(+), 22 deletions(-) diff --git a/ledis/t_kv.go b/ledis/t_kv.go index 116aa87..136772b 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -197,7 +197,7 @@ func (db *DB) Incr(key []byte) (int64, error) { return db.incr(key, 1) } -func (db *DB) IncryBy(key []byte, increment int64) (int64, error) { +func (db *DB) IncrBy(key []byte, increment int64) (int64, error) { return db.incr(key, increment) } diff --git a/server/cmd_bit.go b/server/cmd_bit.go index cebb8dc..8fdd678 100644 --- a/server/cmd_bit.go +++ b/server/cmd_bit.go @@ -196,7 +196,7 @@ func bexpireCommand(c *client) error { duration, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.BExpire(args[0], duration); err != nil { @@ -216,7 +216,7 @@ func bexpireatCommand(c *client) error { when, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.BExpireAt(args[0], when); err != nil { diff --git a/server/cmd_hash.go b/server/cmd_hash.go index cb3d752..b705d17 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -91,7 +91,7 @@ func hincrbyCommand(c *client) error { delta, err := ledis.StrInt64(args[2], nil) if err != nil { - return err + return ErrValue } var n int64 @@ -230,7 +230,7 @@ func hexpireCommand(c *client) error { duration, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.HExpire(args[0], duration); err != nil { @@ -250,7 +250,7 @@ func hexpireAtCommand(c *client) error { when, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.HExpireAt(args[0], when); err != nil { diff --git a/server/cmd_kv.go b/server/cmd_kv.go index 1f61f5e..f526045 100644 --- a/server/cmd_kv.go +++ b/server/cmd_kv.go @@ -116,10 +116,10 @@ func incrbyCommand(c *client) error { delta, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } - if n, err := c.db.IncryBy(c.args[0], delta); err != nil { + if n, err := c.db.IncrBy(c.args[0], delta); err != nil { return err } else { c.writeInteger(n) @@ -136,7 +136,7 @@ func decrbyCommand(c *client) error { delta, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if n, err := c.db.DecrBy(c.args[0], delta); err != nil { @@ -211,7 +211,7 @@ func expireCommand(c *client) error { duration, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.Expire(args[0], duration); err != nil { @@ -231,7 +231,7 @@ func expireAtCommand(c *client) error { when, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.ExpireAt(args[0], when); err != nil { diff --git a/server/cmd_kv_test.go b/server/cmd_kv_test.go index 4f3cf23..56b2022 100644 --- a/server/cmd_kv_test.go +++ b/server/cmd_kv_test.go @@ -137,3 +137,81 @@ func TestKVIncrDecr(t *testing.T) { t.Fatal(n) } } + +func TestErrorParams(t *testing.T) { + c := getTestConn() + defer c.Close() + + if _, err := c.Do("get", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("set", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("getset", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("setnx", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("exists", "a", "b"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("incr", "a", "b"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("incrby", "a"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("decrby", "a"); err == nil || err.Error() != SErrCmdParams { + t.Fatalf("invalid err %v", err) + } + + if _, err := c.Do("del"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("mset"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("mset", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("mget"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("expire"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("expire", "a", "b"); err == nil || err.Error() != SErrValue { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("expireat"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("expireat", "a", "b"); err == nil || err.Error() != SErrValue { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("ttl"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + + if _, err := c.Do("persist"); err == nil || err.Error() != SErrCmdParams { + t.Fatal("invalid err of %v", err) + } + +} diff --git a/server/cmd_list.go b/server/cmd_list.go index bc059ec..cafd824 100644 --- a/server/cmd_list.go +++ b/server/cmd_list.go @@ -87,7 +87,7 @@ func lindexCommand(c *client) error { index, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.LIndex(args[0], int32(index)); err != nil { @@ -111,12 +111,12 @@ func lrangeCommand(c *client) error { start, err = ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } stop, err = ledis.StrInt64(args[2], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.LRange(args[0], int32(start), int32(stop)); err != nil { @@ -166,7 +166,7 @@ func lexpireCommand(c *client) error { duration, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.LExpire(args[0], duration); err != nil { @@ -186,7 +186,7 @@ func lexpireAtCommand(c *client) error { when, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.LExpireAt(args[0], when); err != nil { diff --git a/server/cmd_zset.go b/server/cmd_zset.go index e8e8014..8dc465e 100644 --- a/server/cmd_zset.go +++ b/server/cmd_zset.go @@ -29,7 +29,7 @@ func zaddCommand(c *client) error { for i := 0; i < len(params); i++ { score, err := ledis.StrInt64(args[2*i], nil) if err != nil { - return err + return ErrValue } params[i].Score = score @@ -104,7 +104,7 @@ func zincrbyCommand(c *client) error { delta, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.ZIncrBy(key, delta, args[2]); err != nil { @@ -444,7 +444,7 @@ func zexpireCommand(c *client) error { duration, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.ZExpire(args[0], duration); err != nil { @@ -464,7 +464,7 @@ func zexpireAtCommand(c *client) error { when, err := ledis.StrInt64(args[1], nil) if err != nil { - return err + return ErrValue } if v, err := c.db.ZExpireAt(args[0], when); err != nil { diff --git a/server/const.go b/server/const.go index 36235a8..9be33fa 100644 --- a/server/const.go +++ b/server/const.go @@ -8,6 +8,7 @@ var ( ErrEmptyCommand = errors.New("empty command") ErrNotFound = errors.New("command not found") ErrCmdParams = errors.New("invalid command param") + ErrValue = errors.New("value is not an integer or out of range") ) var ( @@ -16,6 +17,8 @@ var ( NullBulk = []byte("-1") NullArray = []byte("-1") - PONG = "PONG" - OK = "OK" + PONG = "PONG" + OK = "OK" + SErrCmdParams = "ERR invalid command param" + SErrValue = "ERR value is not an integer or out of range" )