forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_test.go
323 lines (290 loc) · 9.27 KB
/
script_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// +build int
package main
// Script commands
import (
"testing"
)
func TestEval(t *testing.T) {
testCommands(t,
succ("EVAL", "return 42", 0),
succ("EVAL", "", 0),
succ("EVAL", "return 42", 1, "foo"),
succ("EVAL", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second"),
succ("EVAL", "return {ARGV[1]}", 0, "first"),
succ("EVAL", "return {ARGV[1]}", 0, "first\nwith\nnewlines!\r\r\n\t!"),
succ("EVAL", "return redis.call('GET', 'nosuch')==false", 0),
succ("EVAL", "return redis.call('GET', 'nosuch')==nil", 0),
succ("EVAL", "local a = redis.call('MGET', 'bar'); return a[1] == false", 0),
succ("EVAL", "local a = redis.call('MGET', 'bar'); return a[1] == nil", 0),
// failure cases
fail("EVAL"),
fail("EVAL", "return 42"),
fail("EVAL", "["),
fail("EVAL", "return 42", "return 43"),
fail("EVAL", "return 42", 1),
fail("EVAL", "return 42", -1),
fail("EVAL", 42),
)
}
func TestScript(t *testing.T) {
testCommands(t,
succ("SCRIPT", "LOAD", "return 42"),
succ("SCRIPT", "LOAD", "return 42"),
succ("SCRIPT", "LOAD", "return 43"),
succ("SCRIPT", "EXISTS", "1fa00e76656cc152ad327c13fe365858fd7be306"),
succ("SCRIPT", "EXISTS", "0", "1fa00e76656cc152ad327c13fe365858fd7be306"),
succ("SCRIPT", "EXISTS", 0),
succ("SCRIPT", "EXISTS"),
succ("SCRIPT", "FLUSH"),
succ("SCRIPT", "EXISTS", "1fa00e76656cc152ad327c13fe365858fd7be306"),
fail("SCRIPT"),
fail("SCRIPT", "LOAD", "return 42", "return 42"),
failLoosely("SCRIPT", "LOAD", "]"),
fail("SCRIPT", "LOAD", "]", "foo"),
fail("SCRIPT", "LOAD"),
fail("SCRIPT", "FLUSH", "foo"),
fail("SCRIPT", "FOO"),
)
}
func TestEvalsha(t *testing.T) {
sha1 := "1fa00e76656cc152ad327c13fe365858fd7be306" // "return 42"
sha2 := "bfbf458525d6a0b19200bfd6db3af481156b367b" // keys[1], argv[1]
testCommands(t,
succ("SCRIPT", "LOAD", "return 42"),
succ("SCRIPT", "LOAD", "return {KEYS[1],ARGV[1]}"),
succ("EVALSHA", sha1, "0"),
succ("EVALSHA", sha2, "0"),
succ("EVALSHA", sha2, "0", "foo"),
succ("EVALSHA", sha2, "1", "foo"),
succ("EVALSHA", sha2, "1", "foo", "bar"),
succ("EVALSHA", sha2, "1", "foo", "bar", "baz"),
succ("SCRIPT", "FLUSH"),
fail("EVALSHA", sha1, "0"),
succ("SCRIPT", "LOAD", "return 42"),
fail("EVALSHA", sha1),
fail("EVALSHA"),
fail("EVALSHA", "nosuch"),
fail("EVALSHA", "nosuch", 0),
)
}
func TestLua(t *testing.T) {
// basic datatype things
testCommands(t,
succ("EVAL", "", 0),
succ("EVAL", "return 42", 0),
succ("EVAL", "return 42, 43", 0),
succ("EVAL", "return true", 0),
succ("EVAL", "return 'foo'", 0),
succ("EVAL", "return 3.1415", 0),
succ("EVAL", "return 3.9999", 0),
succ("EVAL", "return {1,'foo'}", 0),
succ("EVAL", "return {1,'foo',nil,'foo'}", 0),
succ("EVAL", "return 3.9999+3", 0),
succ("EVAL", "return 3.99+0.0001", 0),
succ("EVAL", "return 3.9999+0.201", 0),
succ("EVAL", "return {{1}}", 0),
succ("EVAL", "return {1,{1,{1,'bar'}}}", 0),
)
// special returns
testCommands(t,
fail("EVAL", "return {err = 'oops'}", 0),
succ("EVAL", "return {1,{err = 'oops'}}", 0),
fail("EVAL", "return redis.error_reply('oops')", 0),
succ("EVAL", "return {1,redis.error_reply('oops')}", 0),
fail("EVAL", "return {err = 'oops', noerr = true}", 0), // doc error?
fail("EVAL", "return {1, 2, err = 'oops'}", 0), // doc error?
succ("EVAL", "return {ok = 'great'}", 0),
succ("EVAL", "return {1,{ok = 'great'}}", 0),
succ("EVAL", "return redis.status_reply('great')", 0),
succ("EVAL", "return {1,redis.status_reply('great')}", 0),
succ("EVAL", "return {ok = 'great', notok = 'yes'}", 0), // doc error?
succ("EVAL", "return {1, 2, ok = 'great', notok = 'yes'}", 0), // doc error?
failLoosely("EVAL", "return redis.error_reply(1)", 0),
failLoosely("EVAL", "return redis.error_reply()", 0),
failLoosely("EVAL", "return redis.error_reply(redis.error_reply('foo'))", 0),
failLoosely("EVAL", "return redis.status_reply(1)", 0),
failLoosely("EVAL", "return redis.status_reply()", 0),
failLoosely("EVAL", "return redis.status_reply(redis.status_reply('foo'))", 0),
)
// state inside lua
testCommands(t,
succ("EVAL", "redis.call('SELECT', 3); redis.call('SET', 'foo', 'bar')", 0),
succ("GET", "foo"),
succ("SELECT", 3),
succ("GET", "foo"),
)
// lua env
testCommands(t,
// succ("EVAL", "print(1)", 0),
succ("EVAL", `return string.format('%q', "pretty string")`, 0),
failLoosely("EVAL", "os.clock()", 0),
failLoosely("EVAL", "os.exit(42)", 0),
succ("EVAL", "return table.concat({1,2,3})", 0),
succ("EVAL", "return math.abs(-42)", 0),
failLoosely("EVAL", `return utf8.len("hello world")`, 0),
failLoosely("EVAL", `require("utf8")`, 0),
succ("EVAL", `return coroutine.running()`, 0),
)
// sha1hex
testCommands(t,
succ("EVAL", `return redis.sha1hex("foo")`, 0),
succ("SET", "bar", "32"),
succ("EVAL", `return redis.sha1hex(KEYS["bar"])`, 0),
succ("EVAL", `return redis.sha1hex(KEYS[1])`, 1, "bar"),
succ("EVAL", `return redis.sha1hex(nil)`, 0),
succ("EVAL", `return redis.sha1hex(42)`, 0),
succ("EVAL", `return redis.sha1hex({})`, 0),
succ("EVAL", `return redis.sha1hex(KEYS[1])`, 0),
failWith(
"wrong number of arguments",
"EVAL", `return redis.sha1hex()`, 0,
),
failWith(
"wrong number of arguments",
"EVAL", `return redis.sha1hex(1, 2)`, 0,
),
)
// cjson module
testCommands(t,
succ("EVAL", `return cjson.decode('{"id":"foo"}')['id']`, 0),
// succ("SET", "foo", `{"value":42}`),
// succ("EVAL", `return KEYS[1]`, 1, "foo"),
// succ("EVAL", `return cjson.decode(KEYS[1])['value']`, 1, "foo"),
succ("EVAL", `return cjson.decode(ARGV[1])['value']`, 0, `{"value":"42"}`),
succ("EVAL", `return redis.call("SET", "enc", cjson.encode({["foo"]="bar"}))`, 0),
succ("EVAL", `return redis.call("SET", "enc", cjson.encode({["foo"]={["foo"]=42}}))`, 0),
succ("GET", "enc"),
failWith(
"bad argument #1 to ",
"EVAL", `return cjson.encode()`, 0,
),
failWith(
"bad argument #1 to ",
"EVAL", `return cjson.encode(1, 2)`, 0,
),
failWith(
"bad argument #1 to ",
"EVAL", `return cjson.decode()`, 0,
),
failWith(
"bad argument #1 to ",
"EVAL", `return cjson.decode(1, 2)`, 0,
),
)
}
func TestLuaCall(t *testing.T) {
testCommands(t,
succ("SET", "foo", 1),
succ("EVAL", `local foo = redis.call("GET", "foo"); redis.call("SET", "foo", foo+1)`, 0),
succ("GET", "foo"),
succ("EVAL", `return redis.call("GET", "foo")`, 0),
succ("EVAL", `return redis.call("SET", "foo", 42)`, 0),
)
// datatype errors
testCommands(t,
failWith(
"Please specify at least one argument for redis.call()",
"EVAL", `redis.call()`, 0,
),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `redis.call({})`, 0,
),
failWith(
"Unknown Redis command called from Lua script",
"EVAL", `redis.call(1)`, 0,
),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `redis.call("ECHO", true)`, 0,
),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `redis.call("ECHO", false)`, 0,
),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `redis.call("ECHO", nil)`, 0,
),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `redis.call("HELLO", {})`, 0,
),
failLoosely("EVAL", `redis.call("HELLO", 1)`, 0),
failLoosely("EVAL", `redis.call("HELLO", 3.14)`, 0),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `redis.call("GET", {})`, 0,
),
)
// call() errors
testCommands(t,
succ("SET", "foo", 1),
failLoosely("EVAL", `redis.call("HGET", "foo")`, 0),
succ("GET", "foo"),
failLoosely("EVAL", `local foo = redis.call("HGET", "foo"); redis.call("SET", "res", foo)`, 0),
succ("GET", "foo"),
succ("GET", "res"),
failLoosely("EVAL", `local foo = redis.call("HGET", "foo", "bar"); redis.call("SET", "res", foo)`, 0),
succ("GET", "foo"),
succ("GET", "res"),
)
// pcall() errors
testCommands(t,
succ("SET", "foo", 1),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `local foo = redis.pcall("HGET", "foo"); redis.call("SET", "res", foo)`, 0,
),
succ("GET", "foo"),
succ("GET", "res"),
failWith(
"Lua redis() command arguments must be strings or integers",
"EVAL", `local foo = redis.pcall("HGET", "foo", "bar"); redis.call("SET", "res", foo)`, 0,
),
succ("GET", "foo"),
succ("GET", "res"),
)
}
func TestScriptNoAuth(t *testing.T) {
testAuthCommands(t,
"supersecret",
failWith(
"NOAUTH Authentication required.",
"EVAL", `redis.call("ECHO", "foo")`, 0,
),
succ("AUTH", "supersecret"),
succ(
"EVAL", `redis.call("ECHO", "foo")`, 0,
),
)
}
func TestScriptReplicate(t *testing.T) {
testCommands(t,
succ(
"EVAL", `redis.replicate_commands();`, 0,
),
)
}
func TestScriptTx(t *testing.T) {
sha2 := "bfbf458525d6a0b19200bfd6db3af481156b367b" // keys[1], argv[1]
testCommands(t,
succ("SCRIPT", "LOAD", "return {KEYS[1],ARGV[1]}"),
succ("MULTI"),
succ("EVALSHA", sha2, "0"),
succ("EXEC"),
)
testCommands(t,
succ("MULTI"),
succ("SCRIPT", "LOAD", "return {KEYS[1],ARGV[1]}"),
succ("EVALSHA", sha2, "0"),
succ("EXEC"),
)
testCommands(t,
succ("MULTI"),
succ("SCRIPT", "LOAD", "return {"),
succ("SCRIPT", "FOO"),
succ("EVALSHA", "aaaa", "0"),
succLoosely("EXEC"),
)
}