-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
193 lines (183 loc) · 3.86 KB
/
command_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package resp
import (
"bytes"
"io"
"testing"
)
var commandTests = []struct {
input string
output []string
}{
{
`
test`,
[]string{"test"},
},
{
`test`,
[]string{"test"},
},
{
`test test`,
[]string{"test", "test"},
},
{
`SET somekey somevalue`,
[]string{"SET", "somekey", "somevalue"},
},
{
`SET 'some key' somevalue`,
[]string{"SET", "some key", "somevalue"},
},
{
` SET 'some key' somevalue `,
[]string{"SET", "some key", "somevalue"},
},
{
`SET 'some \' k\ey' somevalue`,
[]string{"SET", `some ' k\ey`, "somevalue"},
},
{
`SET "some key" somevalue`,
[]string{"SET", "some key", "somevalue"},
},
{
`"\x4a\x4B\xEF"`,
[]string{"JK\xef"},
},
{
`"test\n\t\t\t\t\"test"`,
[]string{`test
"test`},
},
{
`this'is' a"bit" str"ange"`,
[]string{"thisis", "abit", "strange"},
},
{
`"\x01\x23\x45\x67\x89\xab\xcd\xef"`,
[]string{"\x01\x23\x45\x67\x89\xab\xcd\xef"},
},
// Array of Bulk String variants
{
"*2\r\n$3\r\nGET\r\n$10\r\nsome value\r\n",
[]string{"GET", "some value"},
},
{
"\n\t \t\n \n*2\r\n$3\r\nGET\r\n$10\r\nsome value\r\n",
[]string{"GET", "some value"},
},
}
func TestParseCommand(t *testing.T) {
for _, test := range commandTests {
cmd := []byte(test.input)
if cmd[len(cmd)-1] != '\n' {
cmd = append(cmd, '\n')
}
length, out, err := parseCommand(cmd)
if err != nil {
t.Errorf("%q error: %v", string(cmd), err)
continue
}
var output []string
for _, b := range out {
output = append(output, string(b))
}
if len(test.output) != len(output) {
t.Errorf("%q expected %+#v, got %+#v", string(cmd), test.output, output)
continue
}
for i := range test.output {
if test.output[i] != string(output[i]) {
t.Errorf("%q expected %+#v, got %+#v", string(cmd), test.output, output)
break
}
}
if length != len(cmd) {
t.Errorf("%q expected length %d, got %d", string(cmd), len(test.input)+1, length)
}
}
}
func TestCommandReaderPerByte(t *testing.T) {
commandReaderTest(t, func(r io.Reader) io.Reader { return perByteReader{r: r} })
}
func TestCommandReaderFullRead(t *testing.T) {
commandReaderTest(t, func(r io.Reader) io.Reader { return r })
}
func commandReaderTest(t *testing.T, f func(io.Reader) io.Reader) {
input := new(bytes.Buffer)
for _, test := range commandTests {
in := test.input
if in[len(in)-1] != '\n' {
in += "\n"
}
_, err := input.WriteString(in)
if err != nil {
t.Fatal(err)
}
}
r := NewCommandReader(f(input))
for _, test := range commandTests {
_, out, err := r.Read()
if err != nil {
t.Error(err)
}
var output []string
for _, b := range out {
output = append(output, string(b))
}
if len(test.output) != len(output) {
t.Errorf("expected %+#v, got %+#v", test.output, output)
continue
}
for i := range test.output {
if test.output[i] != string(output[i]) {
t.Errorf("expected %+#v, got %+#v", test.output, output)
break
}
}
}
}
func TestUnbalancedQuotes(t *testing.T) {
tests := []string{
`SET 'some key`,
`SET 'some 'key`,
`SET "some key`,
`SET "some "key`,
}
for _, test := range tests {
_, _, err := parseCommand([]byte(test + "\n"))
if err != errUnbalancedQuotes {
t.Errorf("expected errUnbalancedQuotes, got %v", err)
}
}
}
func TestIncompleteCommand(t *testing.T) {
tests := []string{
`SET some' key' somevalue`,
"*2\r\n$3\r\nGET\r\n$4test\r",
}
for _, test := range tests {
for n := len(test); n > 0; n-- {
_, _, err := parseCommand([]byte(test[:n]))
if err != errIncomplete {
t.Errorf("expected errIncomplete, got %v", err)
}
}
}
}
func TestCommandErrors(t *testing.T) {
tests := []string{
"*\r\n",
"*1\r\n$\r\n",
"*1\r\n:1234\r\n",
"*1\r\n$asdf\r\n",
"*1\r\n$-1\r\n",
}
for _, test := range tests {
_, _, err := parseCommand([]byte(test))
if err == nil {
t.Errorf("expected an error, got nil for %q", test)
}
}
}