2015-03-21 15:33:57 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
|
|
|
"github.com/siddontang/goredis"
|
2015-03-21 15:33:57 +03:00
|
|
|
)
|
|
|
|
|
2015-09-13 03:47:10 +03:00
|
|
|
func TestAuth(t *testing.T) {
|
|
|
|
c1 := getTestConn()
|
|
|
|
defer c1.Close()
|
|
|
|
|
|
|
|
// Should error, no params
|
|
|
|
_, err := c1.Do("AUTH")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should error, invalid pass
|
|
|
|
_, err = c1.Do("AUTH", "password")
|
|
|
|
if err.Error() != " authentication failure" {
|
|
|
|
t.Fatal("Expected authentication error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 := getTestConnAuth("password")
|
|
|
|
defer c2.Close()
|
|
|
|
|
2015-09-13 10:13:02 +03:00
|
|
|
// Should fail doing a command as we've not authed
|
|
|
|
_, err = c2.Do("GET", "tmp_select_key")
|
|
|
|
if err.Error() != " not authenticated" {
|
|
|
|
t.Fatal("Expected authentication error:", err)
|
|
|
|
}
|
|
|
|
|
2015-09-13 03:47:10 +03:00
|
|
|
// Login
|
|
|
|
_, err = c2.Do("AUTH", "password")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be ok doing a command
|
|
|
|
_, err = c2.Do("GET", "tmp_select_key")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log out by sending wrong pass
|
|
|
|
_, err = c2.Do("AUTH", "wrong password")
|
|
|
|
if err.Error() != " authentication failure" {
|
|
|
|
t.Fatal("Expected authentication error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fail doing a command as we're logged out
|
|
|
|
_, err = c2.Do("GET", "tmp_select_key")
|
|
|
|
if err.Error() != " not authenticated" {
|
|
|
|
t.Fatal("Expected authentication error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 03:39:20 +03:00
|
|
|
func TestXSelect(t *testing.T) {
|
2015-03-21 15:33:57 +03:00
|
|
|
c1 := getTestConn()
|
|
|
|
defer c1.Close()
|
|
|
|
|
|
|
|
c2 := getTestConn()
|
|
|
|
defer c2.Close()
|
|
|
|
|
2015-03-22 03:39:20 +03:00
|
|
|
_, err := c1.Do("XSELECT", "1", "THEN", "SET", "tmp_select_key", "1")
|
2015-03-21 15:33:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = goredis.Int(c2.Do("GET", "tmp_select_key"))
|
|
|
|
if err != goredis.ErrNil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-22 03:39:20 +03:00
|
|
|
n, _ := goredis.Int(c2.Do("XSELECT", "1", "THEN", "GET", "tmp_select_key"))
|
2015-03-21 15:33:57 +03:00
|
|
|
if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, _ = goredis.Int(c2.Do("GET", "tmp_select_key"))
|
|
|
|
if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
c1.Do("SELECT", 0)
|
|
|
|
c2.Do("SELECT", 0)
|
|
|
|
|
|
|
|
}
|