-
Notifications
You must be signed in to change notification settings - Fork 113
/
conn_test.go
137 lines (116 loc) · 3.63 KB
/
conn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package radix
import (
"regexp"
"strings"
. "testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCloseBehavior(t *T) {
c := dial()
// sanity check
var out string
require.Nil(t, c.Do(Cmd(&out, "ECHO", "foo")))
assert.Equal(t, "foo", out)
c.Close()
require.NotNil(t, c.Do(Cmd(&out, "ECHO", "foo")))
require.NotNil(t, c.NetConn().SetDeadline(time.Now()))
}
func TestDialURI(t *T) {
c, err := Dial("tcp", "redis://127.0.0.1:6379")
if err != nil {
t.Fatal(err)
} else if err := c.Do(Cmd(nil, "PING")); err != nil {
t.Fatal(err)
}
}
func TestDialAuth(t *T) {
type testCase struct {
url, dialOptUser, dialOptPass string
}
runTests := func(t *T, tests []testCase, allowedErrs []string) {
for _, test := range tests {
var opts []DialOpt
if test.dialOptUser != "" {
opts = append(opts, DialAuthUser(test.dialOptUser, test.dialOptPass))
} else if test.dialOptPass != "" {
opts = append(opts, DialAuthPass(test.dialOptPass))
}
_, err := Dial("tcp", test.url, opts...)
// It's difficult to test _which_ password is being sent, but it's easy
// enough to tell that one was sent because redis returns an error if one
// isn't set in the config
assert.Errorf(t, err, "expected authentication error, got nil")
assert.Containsf(t, allowedErrs, err.Error(), "one of %v expected, got %v (test:%#v)", allowedErrs, err, test)
}
}
t.Run("Password only", func(t *T) {
runTests(t, []testCase{
{url: "redis://:[email protected]:6379"},
{url: "redis://127.0.0.1:6379?password=myPass"},
{url: "127.0.0.1:6379", dialOptPass: "myPass"},
}, []string{
"ERR Client sent AUTH, but no password is set",
// Redis 6 only
"ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?",
})
})
t.Run("Username and password", func(t *T) {
conn := dial()
defer conn.Close()
requireRedisVersion(t, conn, 6, 0, 0)
runTests(t, []testCase{
{url: "redis://user:[email protected]:6379"},
{url: "redis://127.0.0.1:6379?username=mediocregopher"},
{url: "127.0.0.1:6379", dialOptUser: "mediocregopher"},
{url: "redis://127.0.0.1:6379?username=mediocregopher&password=myPass"},
{url: "127.0.0.1:6379", dialOptUser: "mediocregopher", dialOptPass: "myPass"},
}, []string{
"WRONGPASS invalid username-password pair",
"WRONGPASS invalid username-password pair or user is disabled.",
})
})
}
func TestDialSelect(t *T) {
// unfortunately this is the best way to discover the currently selected
// database, and it's janky af
assertDB := func(c Conn) bool {
name := randStr()
if err := c.Do(Cmd(nil, "CLIENT", "SETNAME", name)); err != nil {
t.Fatal(err)
}
var list string
if err := c.Do(Cmd(&list, "CLIENT", "LIST")); err != nil {
t.Fatal(err)
}
line := regexp.MustCompile(".*name=" + name + ".*").FindString(list)
if line == "" {
t.Fatalf("line messed up:%q (list:%q name:%q)", line, list, name)
}
return strings.Index(line, " db=9 ") > 0
}
tests := []struct {
url string
dialOptSelect int
}{
{url: "redis://127.0.0.1:6379/9"},
{url: "redis://127.0.0.1:6379?db=9"},
{url: "redis://127.0.0.1:6379", dialOptSelect: 9},
// DialOpt should overwrite URI
{url: "redis://127.0.0.1:6379/8", dialOptSelect: 9},
}
for _, test := range tests {
var opts []DialOpt
if test.dialOptSelect > 0 {
opts = append(opts, DialSelectDB(test.dialOptSelect))
}
c, err := Dial("tcp", test.url, opts...)
if err != nil {
t.Fatalf("got err connecting:%v (test:%#v)", err, test)
}
if !assertDB(c) {
t.Fatalf("db not set to 9 (test:%#v)", test)
}
}
}