1 /* Module designed to test the Redis modules subsystem.
2  *
3  * -----------------------------------------------------------------------------
4  *
5  * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  *   * Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *   * Neither the name of Redis nor the names of its contributors may be used
17  *     to endorse or promote products derived from this software without
18  *     specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #define REDISMODULE_EXPERIMENTAL_API
34 #include "redismodule.h"
35 #include <string.h>
36 #include <stdlib.h>
37 
38 /* --------------------------------- Helpers -------------------------------- */
39 
40 /* Return true if the reply and the C null term string matches. */
TestMatchReply(RedisModuleCallReply * reply,char * str)41 int TestMatchReply(RedisModuleCallReply *reply, char *str) {
42     RedisModuleString *mystr;
43     mystr = RedisModule_CreateStringFromCallReply(reply);
44     if (!mystr) return 0;
45     const char *ptr = RedisModule_StringPtrLen(mystr,NULL);
46     return strcmp(ptr,str) == 0;
47 }
48 
49 /* ------------------------------- Test units ------------------------------- */
50 
51 /* TEST.CALL -- Test Call() API. */
TestCall(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)52 int TestCall(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
53     REDISMODULE_NOT_USED(argv);
54     REDISMODULE_NOT_USED(argc);
55 
56     RedisModule_AutoMemory(ctx);
57     RedisModuleCallReply *reply;
58 
59     RedisModule_Call(ctx,"DEL","c","mylist");
60     RedisModuleString *mystr = RedisModule_CreateString(ctx,"foo",3);
61     RedisModule_Call(ctx,"RPUSH","csl","mylist",mystr,(long long)1234);
62     reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
63     long long items = RedisModule_CallReplyLength(reply);
64     if (items != 2) goto fail;
65 
66     RedisModuleCallReply *item0, *item1;
67 
68     item0 = RedisModule_CallReplyArrayElement(reply,0);
69     item1 = RedisModule_CallReplyArrayElement(reply,1);
70     if (!TestMatchReply(item0,"foo")) goto fail;
71     if (!TestMatchReply(item1,"1234")) goto fail;
72 
73     RedisModule_ReplyWithSimpleString(ctx,"OK");
74     return REDISMODULE_OK;
75 
76 fail:
77     RedisModule_ReplyWithSimpleString(ctx,"ERR");
78     return REDISMODULE_OK;
79 }
80 
TestCallResp3Attribute(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)81 int TestCallResp3Attribute(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
82     REDISMODULE_NOT_USED(argv);
83     REDISMODULE_NOT_USED(argc);
84 
85     RedisModule_AutoMemory(ctx);
86     RedisModuleCallReply *reply;
87 
88     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "attrib"); /* 3 stands for resp 3 reply */
89     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) goto fail;
90 
91     /* make sure we can not reply to resp2 client with resp3 (it might be a string but it contains attribute) */
92     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
93 
94     if (!TestMatchReply(reply,"Some real reply following the attribute")) goto fail;
95 
96     reply = RedisModule_CallReplyAttribute(reply);
97     if (!reply || RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ATTRIBUTE) goto fail;
98     /* make sure we can not reply to resp2 client with resp3 attribute */
99     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
100     if (RedisModule_CallReplyLength(reply) != 1) goto fail;
101 
102     RedisModuleCallReply *key, *val;
103     if (RedisModule_CallReplyAttributeElement(reply,0,&key,&val) != REDISMODULE_OK) goto fail;
104     if (!TestMatchReply(key,"key-popularity")) goto fail;
105     if (RedisModule_CallReplyType(val) != REDISMODULE_REPLY_ARRAY) goto fail;
106     if (RedisModule_CallReplyLength(val) != 2) goto fail;
107     if (!TestMatchReply(RedisModule_CallReplyArrayElement(val, 0),"key:123")) goto fail;
108     if (!TestMatchReply(RedisModule_CallReplyArrayElement(val, 1),"90")) goto fail;
109 
110     RedisModule_ReplyWithSimpleString(ctx,"OK");
111     return REDISMODULE_OK;
112 
113 fail:
114     RedisModule_ReplyWithSimpleString(ctx,"ERR");
115     return REDISMODULE_OK;
116 }
117 
TestGetResp(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)118 int TestGetResp(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
119     REDISMODULE_NOT_USED(argv);
120     REDISMODULE_NOT_USED(argc);
121 
122     int flags = RedisModule_GetContextFlags(ctx);
123 
124     if (flags & REDISMODULE_CTX_FLAGS_RESP3) {
125         RedisModule_ReplyWithLongLong(ctx, 3);
126     } else {
127         RedisModule_ReplyWithLongLong(ctx, 2);
128     }
129 
130     return REDISMODULE_OK;
131 }
132 
TestCallRespAutoMode(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)133 int TestCallRespAutoMode(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
134     REDISMODULE_NOT_USED(argv);
135     REDISMODULE_NOT_USED(argc);
136 
137     RedisModule_AutoMemory(ctx);
138     RedisModuleCallReply *reply;
139 
140     RedisModule_Call(ctx,"DEL","c","myhash");
141     RedisModule_Call(ctx,"HSET","ccccc","myhash", "f1", "v1", "f2", "v2");
142     /* 0 stands for auto mode, we will get the reply in the same format as the client */
143     reply = RedisModule_Call(ctx,"HGETALL","0c" ,"myhash");
144     RedisModule_ReplyWithCallReply(ctx, reply);
145     return REDISMODULE_OK;
146 }
147 
TestCallResp3Map(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)148 int TestCallResp3Map(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
149     REDISMODULE_NOT_USED(argv);
150     REDISMODULE_NOT_USED(argc);
151 
152     RedisModule_AutoMemory(ctx);
153     RedisModuleCallReply *reply;
154 
155     RedisModule_Call(ctx,"DEL","c","myhash");
156     RedisModule_Call(ctx,"HSET","ccccc","myhash", "f1", "v1", "f2", "v2");
157     reply = RedisModule_Call(ctx,"HGETALL","3c" ,"myhash"); /* 3 stands for resp 3 reply */
158     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_MAP) goto fail;
159 
160     /* make sure we can not reply to resp2 client with resp3 map */
161     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
162 
163     long long items = RedisModule_CallReplyLength(reply);
164     if (items != 2) goto fail;
165 
166     RedisModuleCallReply *key0, *key1;
167     RedisModuleCallReply *val0, *val1;
168     if (RedisModule_CallReplyMapElement(reply,0,&key0,&val0) != REDISMODULE_OK) goto fail;
169     if (RedisModule_CallReplyMapElement(reply,1,&key1,&val1) != REDISMODULE_OK) goto fail;
170     if (!TestMatchReply(key0,"f1")) goto fail;
171     if (!TestMatchReply(key1,"f2")) goto fail;
172     if (!TestMatchReply(val0,"v1")) goto fail;
173     if (!TestMatchReply(val1,"v2")) goto fail;
174 
175     RedisModule_ReplyWithSimpleString(ctx,"OK");
176     return REDISMODULE_OK;
177 
178 fail:
179     RedisModule_ReplyWithSimpleString(ctx,"ERR");
180     return REDISMODULE_OK;
181 }
182 
TestCallResp3Bool(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)183 int TestCallResp3Bool(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
184     REDISMODULE_NOT_USED(argv);
185     REDISMODULE_NOT_USED(argc);
186 
187     RedisModule_AutoMemory(ctx);
188     RedisModuleCallReply *reply;
189 
190     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "true"); /* 3 stands for resp 3 reply */
191     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BOOL) goto fail;
192     /* make sure we can not reply to resp2 client with resp3 bool */
193     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
194 
195     if (!RedisModule_CallReplyBool(reply)) goto fail;
196     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "false"); /* 3 stands for resp 3 reply */
197     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BOOL) goto fail;
198     if (RedisModule_CallReplyBool(reply)) goto fail;
199 
200     RedisModule_ReplyWithSimpleString(ctx,"OK");
201     return REDISMODULE_OK;
202 
203 fail:
204     RedisModule_ReplyWithSimpleString(ctx,"ERR");
205     return REDISMODULE_OK;
206 }
207 
TestCallResp3Null(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)208 int TestCallResp3Null(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
209     REDISMODULE_NOT_USED(argv);
210     REDISMODULE_NOT_USED(argc);
211 
212     RedisModule_AutoMemory(ctx);
213     RedisModuleCallReply *reply;
214 
215     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "null"); /* 3 stands for resp 3 reply */
216     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_NULL) goto fail;
217 
218     /* make sure we can not reply to resp2 client with resp3 null */
219     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
220 
221     RedisModule_ReplyWithSimpleString(ctx,"OK");
222     return REDISMODULE_OK;
223 
224 fail:
225     RedisModule_ReplyWithSimpleString(ctx,"ERR");
226     return REDISMODULE_OK;
227 }
228 
TestCallReplyWithNestedReply(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)229 int TestCallReplyWithNestedReply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
230     REDISMODULE_NOT_USED(argv);
231     REDISMODULE_NOT_USED(argc);
232 
233     RedisModule_AutoMemory(ctx);
234     RedisModuleCallReply *reply;
235 
236     RedisModule_Call(ctx,"DEL","c","mylist");
237     RedisModule_Call(ctx,"RPUSH","ccl","mylist","test",(long long)1234);
238     reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
239     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
240     if (RedisModule_CallReplyLength(reply) < 1) goto fail;
241     RedisModuleCallReply *nestedReply = RedisModule_CallReplyArrayElement(reply, 0);
242 
243     RedisModule_ReplyWithCallReply(ctx,nestedReply);
244     return REDISMODULE_OK;
245 
246 fail:
247     RedisModule_ReplyWithSimpleString(ctx,"ERR");
248     return REDISMODULE_OK;
249 }
250 
TestCallReplyWithArrayReply(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)251 int TestCallReplyWithArrayReply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
252     REDISMODULE_NOT_USED(argv);
253     REDISMODULE_NOT_USED(argc);
254 
255     RedisModule_AutoMemory(ctx);
256     RedisModuleCallReply *reply;
257 
258     RedisModule_Call(ctx,"DEL","c","mylist");
259     RedisModule_Call(ctx,"RPUSH","ccl","mylist","test",(long long)1234);
260     reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
261     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
262 
263     RedisModule_ReplyWithCallReply(ctx,reply);
264     return REDISMODULE_OK;
265 
266 fail:
267     RedisModule_ReplyWithSimpleString(ctx,"ERR");
268     return REDISMODULE_OK;
269 }
270 
TestCallResp3Double(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)271 int TestCallResp3Double(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
272     REDISMODULE_NOT_USED(argv);
273     REDISMODULE_NOT_USED(argc);
274 
275     RedisModule_AutoMemory(ctx);
276     RedisModuleCallReply *reply;
277 
278     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "double"); /* 3 stands for resp 3 reply */
279     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_DOUBLE) goto fail;
280 
281     /* make sure we can not reply to resp2 client with resp3 double*/
282     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
283 
284     double d = RedisModule_CallReplyDouble(reply);
285     /* we compare strings, since comparing doubles directly can fail in various architectures, e.g. 32bit */
286     char got[30], expected[30];
287     sprintf(got, "%.17g", d);
288     sprintf(expected, "%.17g", 3.141);
289     if (strcmp(got, expected) != 0) goto fail;
290     RedisModule_ReplyWithSimpleString(ctx,"OK");
291     return REDISMODULE_OK;
292 
293 fail:
294     RedisModule_ReplyWithSimpleString(ctx,"ERR");
295     return REDISMODULE_OK;
296 }
297 
TestCallResp3BigNumber(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)298 int TestCallResp3BigNumber(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
299     REDISMODULE_NOT_USED(argv);
300     REDISMODULE_NOT_USED(argc);
301 
302     RedisModule_AutoMemory(ctx);
303     RedisModuleCallReply *reply;
304 
305     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "bignum"); /* 3 stands for resp 3 reply */
306     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BIG_NUMBER) goto fail;
307 
308     /* make sure we can not reply to resp2 client with resp3 big number */
309     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
310 
311     size_t len;
312     const char* big_num = RedisModule_CallReplyBigNumber(reply, &len);
313     RedisModule_ReplyWithStringBuffer(ctx,big_num,len);
314     return REDISMODULE_OK;
315 
316 fail:
317     RedisModule_ReplyWithSimpleString(ctx,"ERR");
318     return REDISMODULE_OK;
319 }
320 
TestCallResp3Verbatim(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)321 int TestCallResp3Verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
322     REDISMODULE_NOT_USED(argv);
323     REDISMODULE_NOT_USED(argc);
324 
325     RedisModule_AutoMemory(ctx);
326     RedisModuleCallReply *reply;
327 
328     reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "verbatim"); /* 3 stands for resp 3 reply */
329     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_VERBATIM_STRING) goto fail;
330 
331     /* make sure we can not reply to resp2 client with resp3 verbatim string */
332     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
333 
334     const char* format;
335     size_t len;
336     const char* str = RedisModule_CallReplyVerbatim(reply, &len, &format);
337     RedisModuleString *s = RedisModule_CreateStringPrintf(ctx, "%.*s:%.*s", 3, format, (int)len, str);
338     RedisModule_ReplyWithString(ctx,s);
339     return REDISMODULE_OK;
340 
341 fail:
342     RedisModule_ReplyWithSimpleString(ctx,"ERR");
343     return REDISMODULE_OK;
344 }
345 
TestCallResp3Set(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)346 int TestCallResp3Set(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
347     REDISMODULE_NOT_USED(argv);
348     REDISMODULE_NOT_USED(argc);
349 
350     RedisModule_AutoMemory(ctx);
351     RedisModuleCallReply *reply;
352 
353     RedisModule_Call(ctx,"DEL","c","myset");
354     RedisModule_Call(ctx,"sadd","ccc","myset", "v1", "v2");
355     reply = RedisModule_Call(ctx,"smembers","3c" ,"myset"); // N stands for resp 3 reply
356     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_SET) goto fail;
357 
358     /* make sure we can not reply to resp2 client with resp3 set */
359     if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
360 
361     long long items = RedisModule_CallReplyLength(reply);
362     if (items != 2) goto fail;
363 
364     RedisModuleCallReply *val0, *val1;
365 
366     val0 = RedisModule_CallReplySetElement(reply,0);
367     val1 = RedisModule_CallReplySetElement(reply,1);
368 
369     /*
370      * The order of elements on sets are not promised so we just
371      * veridy that the reply matches one of the elements.
372      */
373     if (!TestMatchReply(val0,"v1") && !TestMatchReply(val0,"v2")) goto fail;
374     if (!TestMatchReply(val1,"v1") && !TestMatchReply(val1,"v2")) goto fail;
375 
376     RedisModule_ReplyWithSimpleString(ctx,"OK");
377     return REDISMODULE_OK;
378 
379 fail:
380     RedisModule_ReplyWithSimpleString(ctx,"ERR");
381     return REDISMODULE_OK;
382 }
383 
384 /* TEST.STRING.APPEND -- Test appending to an existing string object. */
TestStringAppend(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)385 int TestStringAppend(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
386     REDISMODULE_NOT_USED(argv);
387     REDISMODULE_NOT_USED(argc);
388 
389     RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
390     RedisModule_StringAppendBuffer(ctx,s,"bar",3);
391     RedisModule_ReplyWithString(ctx,s);
392     RedisModule_FreeString(ctx,s);
393     return REDISMODULE_OK;
394 }
395 
396 /* TEST.STRING.APPEND.AM -- Test append with retain when auto memory is on. */
TestStringAppendAM(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)397 int TestStringAppendAM(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
398     REDISMODULE_NOT_USED(argv);
399     REDISMODULE_NOT_USED(argc);
400 
401     RedisModule_AutoMemory(ctx);
402     RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
403     RedisModule_RetainString(ctx,s);
404     RedisModule_TrimStringAllocation(s);    /* Mostly NOP, but exercises the API function */
405     RedisModule_StringAppendBuffer(ctx,s,"bar",3);
406     RedisModule_ReplyWithString(ctx,s);
407     RedisModule_FreeString(ctx,s);
408     return REDISMODULE_OK;
409 }
410 
411 /* TEST.STRING.PRINTF -- Test string formatting. */
TestStringPrintf(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)412 int TestStringPrintf(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
413     RedisModule_AutoMemory(ctx);
414     if (argc < 3) {
415         return RedisModule_WrongArity(ctx);
416     }
417     RedisModuleString *s = RedisModule_CreateStringPrintf(ctx,
418         "Got %d args. argv[1]: %s, argv[2]: %s",
419         argc,
420         RedisModule_StringPtrLen(argv[1], NULL),
421         RedisModule_StringPtrLen(argv[2], NULL)
422     );
423 
424     RedisModule_ReplyWithString(ctx,s);
425 
426     return REDISMODULE_OK;
427 }
428 
failTest(RedisModuleCtx * ctx,const char * msg)429 int failTest(RedisModuleCtx *ctx, const char *msg) {
430     RedisModule_ReplyWithError(ctx, msg);
431     return REDISMODULE_ERR;
432 }
433 
TestUnlink(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)434 int TestUnlink(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
435     RedisModule_AutoMemory(ctx);
436     REDISMODULE_NOT_USED(argv);
437     REDISMODULE_NOT_USED(argc);
438 
439     RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "unlinked"), REDISMODULE_WRITE | REDISMODULE_READ);
440     if (!k) return failTest(ctx, "Could not create key");
441 
442     if (REDISMODULE_ERR == RedisModule_StringSet(k, RedisModule_CreateStringPrintf(ctx, "Foobar"))) {
443         return failTest(ctx, "Could not set string value");
444     }
445 
446     RedisModuleCallReply *rep = RedisModule_Call(ctx, "EXISTS", "c", "unlinked");
447     if (!rep || RedisModule_CallReplyInteger(rep) != 1) {
448         return failTest(ctx, "Key does not exist before unlink");
449     }
450 
451     if (REDISMODULE_ERR == RedisModule_UnlinkKey(k)) {
452         return failTest(ctx, "Could not unlink key");
453     }
454 
455     rep = RedisModule_Call(ctx, "EXISTS", "c", "unlinked");
456     if (!rep || RedisModule_CallReplyInteger(rep) != 0) {
457         return failTest(ctx, "Could not verify key to be unlinked");
458     }
459     return RedisModule_ReplyWithSimpleString(ctx, "OK");
460 }
461 
TestNestedCallReplyArrayElement(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)462 int TestNestedCallReplyArrayElement(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
463     RedisModule_AutoMemory(ctx);
464     REDISMODULE_NOT_USED(argv);
465     REDISMODULE_NOT_USED(argc);
466 
467     RedisModuleString *expect_key = RedisModule_CreateString(ctx, "mykey", strlen("mykey"));
468     RedisModule_SelectDb(ctx, 1);
469     RedisModule_Call(ctx, "LPUSH", "sc", expect_key, "myvalue");
470 
471     RedisModuleCallReply *scan_reply = RedisModule_Call(ctx, "SCAN", "l", (long long)0);
472     RedisModule_Assert(scan_reply != NULL && RedisModule_CallReplyType(scan_reply) == REDISMODULE_REPLY_ARRAY);
473     RedisModule_Assert(RedisModule_CallReplyLength(scan_reply) == 2);
474 
475     long long scan_cursor;
476     RedisModuleCallReply *cursor_reply = RedisModule_CallReplyArrayElement(scan_reply, 0);
477     RedisModule_Assert(RedisModule_CallReplyType(cursor_reply) == REDISMODULE_REPLY_STRING);
478     RedisModule_Assert(RedisModule_StringToLongLong(RedisModule_CreateStringFromCallReply(cursor_reply), &scan_cursor) == REDISMODULE_OK);
479     RedisModule_Assert(scan_cursor == 0);
480 
481     RedisModuleCallReply *keys_reply = RedisModule_CallReplyArrayElement(scan_reply, 1);
482     RedisModule_Assert(RedisModule_CallReplyType(keys_reply) == REDISMODULE_REPLY_ARRAY);
483     RedisModule_Assert( RedisModule_CallReplyLength(keys_reply) == 1);
484 
485     RedisModuleCallReply *key_reply = RedisModule_CallReplyArrayElement(keys_reply, 0);
486     RedisModule_Assert(RedisModule_CallReplyType(key_reply) == REDISMODULE_REPLY_STRING);
487     RedisModuleString *key = RedisModule_CreateStringFromCallReply(key_reply);
488     RedisModule_Assert(RedisModule_StringCompare(key, expect_key) == 0);
489 
490     RedisModule_ReplyWithSimpleString(ctx, "OK");
491     return REDISMODULE_OK;
492 }
493 
494 /* TEST.STRING.TRUNCATE -- Test truncating an existing string object. */
TestStringTruncate(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)495 int TestStringTruncate(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
496     RedisModule_AutoMemory(ctx);
497     REDISMODULE_NOT_USED(argv);
498     REDISMODULE_NOT_USED(argc);
499 
500     RedisModule_Call(ctx, "SET", "cc", "foo", "abcde");
501     RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "foo"), REDISMODULE_READ | REDISMODULE_WRITE);
502     if (!k) return failTest(ctx, "Could not create key");
503 
504     size_t len = 0;
505     char* s;
506 
507     /* expand from 5 to 8 and check null pad */
508     if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 8)) {
509         return failTest(ctx, "Could not truncate string value (8)");
510     }
511     s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
512     if (!s) {
513         return failTest(ctx, "Failed to read truncated string (8)");
514     } else if (len != 8) {
515         return failTest(ctx, "Failed to expand string value (8)");
516     } else if (0 != strncmp(s, "abcde\0\0\0", 8)) {
517         return failTest(ctx, "Failed to null pad string value (8)");
518     }
519 
520     /* shrink from 8 to 4 */
521     if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 4)) {
522         return failTest(ctx, "Could not truncate string value (4)");
523     }
524     s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
525     if (!s) {
526         return failTest(ctx, "Failed to read truncated string (4)");
527     } else if (len != 4) {
528         return failTest(ctx, "Failed to shrink string value (4)");
529     } else if (0 != strncmp(s, "abcd", 4)) {
530         return failTest(ctx, "Failed to truncate string value (4)");
531     }
532 
533     /* shrink to 0 */
534     if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 0)) {
535         return failTest(ctx, "Could not truncate string value (0)");
536     }
537     s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
538     if (!s) {
539         return failTest(ctx, "Failed to read truncated string (0)");
540     } else if (len != 0) {
541         return failTest(ctx, "Failed to shrink string value to (0)");
542     }
543 
544     return RedisModule_ReplyWithSimpleString(ctx, "OK");
545 }
546 
NotifyCallback(RedisModuleCtx * ctx,int type,const char * event,RedisModuleString * key)547 int NotifyCallback(RedisModuleCtx *ctx, int type, const char *event,
548                    RedisModuleString *key) {
549   RedisModule_AutoMemory(ctx);
550   /* Increment a counter on the notifications: for each key notified we
551    * increment a counter */
552   RedisModule_Log(ctx, "notice", "Got event type %d, event %s, key %s", type,
553                   event, RedisModule_StringPtrLen(key, NULL));
554 
555   RedisModule_Call(ctx, "HINCRBY", "csc", "notifications", key, "1");
556   return REDISMODULE_OK;
557 }
558 
559 /* TEST.NOTIFICATIONS -- Test Keyspace Notifications. */
TestNotifications(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)560 int TestNotifications(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
561     RedisModule_AutoMemory(ctx);
562     REDISMODULE_NOT_USED(argv);
563     REDISMODULE_NOT_USED(argc);
564 
565 #define FAIL(msg, ...)                                                                       \
566     {                                                                                        \
567         RedisModule_Log(ctx, "warning", "Failed NOTIFY Test. Reason: " #msg, ##__VA_ARGS__); \
568         goto err;                                                                            \
569     }
570     RedisModule_Call(ctx, "FLUSHDB", "");
571 
572     RedisModule_Call(ctx, "SET", "cc", "foo", "bar");
573     RedisModule_Call(ctx, "SET", "cc", "foo", "baz");
574     RedisModule_Call(ctx, "SADD", "cc", "bar", "x");
575     RedisModule_Call(ctx, "SADD", "cc", "bar", "y");
576 
577     RedisModule_Call(ctx, "HSET", "ccc", "baz", "x", "y");
578     /* LPUSH should be ignored and not increment any counters */
579     RedisModule_Call(ctx, "LPUSH", "cc", "l", "y");
580     RedisModule_Call(ctx, "LPUSH", "cc", "l", "y");
581 
582     /* Miss some keys intentionally so we will get a "keymiss" notification. */
583     RedisModule_Call(ctx, "GET", "c", "nosuchkey");
584     RedisModule_Call(ctx, "SMEMBERS", "c", "nosuchkey");
585 
586     size_t sz;
587     const char *rep;
588     RedisModuleCallReply *r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "foo");
589     if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
590         FAIL("Wrong or no reply for foo");
591     } else {
592         rep = RedisModule_CallReplyStringPtr(r, &sz);
593         if (sz != 1 || *rep != '2') {
594             FAIL("Got reply '%s'. expected '2'", RedisModule_CallReplyStringPtr(r, NULL));
595         }
596     }
597 
598     r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "bar");
599     if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
600         FAIL("Wrong or no reply for bar");
601     } else {
602         rep = RedisModule_CallReplyStringPtr(r, &sz);
603         if (sz != 1 || *rep != '2') {
604             FAIL("Got reply '%s'. expected '2'", rep);
605         }
606     }
607 
608     r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "baz");
609     if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
610         FAIL("Wrong or no reply for baz");
611     } else {
612         rep = RedisModule_CallReplyStringPtr(r, &sz);
613         if (sz != 1 || *rep != '1') {
614             FAIL("Got reply '%.*s'. expected '1'", (int)sz, rep);
615         }
616     }
617     /* For l we expect nothing since we didn't subscribe to list events */
618     r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "l");
619     if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_NULL) {
620         FAIL("Wrong reply for l");
621     }
622 
623     r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "nosuchkey");
624     if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
625         FAIL("Wrong or no reply for nosuchkey");
626     } else {
627         rep = RedisModule_CallReplyStringPtr(r, &sz);
628         if (sz != 1 || *rep != '2') {
629             FAIL("Got reply '%.*s'. expected '2'", (int)sz, rep);
630         }
631     }
632 
633     RedisModule_Call(ctx, "FLUSHDB", "");
634 
635     return RedisModule_ReplyWithSimpleString(ctx, "OK");
636 err:
637     RedisModule_Call(ctx, "FLUSHDB", "");
638 
639     return RedisModule_ReplyWithSimpleString(ctx, "ERR");
640 }
641 
642 /* TEST.CTXFLAGS -- Test GetContextFlags. */
TestCtxFlags(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)643 int TestCtxFlags(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
644     REDISMODULE_NOT_USED(argc);
645     REDISMODULE_NOT_USED(argv);
646 
647     RedisModule_AutoMemory(ctx);
648 
649     int ok = 1;
650     const char *errString = NULL;
651 #undef FAIL
652 #define FAIL(msg)        \
653     {                    \
654         ok = 0;          \
655         errString = msg; \
656         goto end;        \
657     }
658 
659     int flags = RedisModule_GetContextFlags(ctx);
660     if (flags == 0) {
661         FAIL("Got no flags");
662     }
663 
664     if (flags & REDISMODULE_CTX_FLAGS_LUA) FAIL("Lua flag was set");
665     if (flags & REDISMODULE_CTX_FLAGS_MULTI) FAIL("Multi flag was set");
666 
667     if (flags & REDISMODULE_CTX_FLAGS_AOF) FAIL("AOF Flag was set")
668     /* Enable AOF to test AOF flags */
669     RedisModule_Call(ctx, "config", "ccc", "set", "appendonly", "yes");
670     flags = RedisModule_GetContextFlags(ctx);
671     if (!(flags & REDISMODULE_CTX_FLAGS_AOF)) FAIL("AOF Flag not set after config set");
672 
673     /* Disable RDB saving and test the flag. */
674     RedisModule_Call(ctx, "config", "ccc", "set", "save", "");
675     flags = RedisModule_GetContextFlags(ctx);
676     if (flags & REDISMODULE_CTX_FLAGS_RDB) FAIL("RDB Flag was set");
677     /* Enable RDB to test RDB flags */
678     RedisModule_Call(ctx, "config", "ccc", "set", "save", "900 1");
679     flags = RedisModule_GetContextFlags(ctx);
680     if (!(flags & REDISMODULE_CTX_FLAGS_RDB)) FAIL("RDB Flag was not set after config set");
681 
682     if (!(flags & REDISMODULE_CTX_FLAGS_MASTER)) FAIL("Master flag was not set");
683     if (flags & REDISMODULE_CTX_FLAGS_SLAVE) FAIL("Slave flag was set");
684     if (flags & REDISMODULE_CTX_FLAGS_READONLY) FAIL("Read-only flag was set");
685     if (flags & REDISMODULE_CTX_FLAGS_CLUSTER) FAIL("Cluster flag was set");
686 
687     /* Disable maxmemory and test the flag. (it is implicitly set in 32bit builds. */
688     RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "0");
689     flags = RedisModule_GetContextFlags(ctx);
690     if (flags & REDISMODULE_CTX_FLAGS_MAXMEMORY) FAIL("Maxmemory flag was set");
691 
692     /* Enable maxmemory and test the flag. */
693     RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "100000000");
694     flags = RedisModule_GetContextFlags(ctx);
695     if (!(flags & REDISMODULE_CTX_FLAGS_MAXMEMORY))
696         FAIL("Maxmemory flag was not set after config set");
697 
698     if (flags & REDISMODULE_CTX_FLAGS_EVICT) FAIL("Eviction flag was set");
699     RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory-policy", "allkeys-lru");
700     flags = RedisModule_GetContextFlags(ctx);
701     if (!(flags & REDISMODULE_CTX_FLAGS_EVICT)) FAIL("Eviction flag was not set after config set");
702 
703 end:
704     /* Revert config changes */
705     RedisModule_Call(ctx, "config", "ccc", "set", "appendonly", "no");
706     RedisModule_Call(ctx, "config", "ccc", "set", "save", "");
707     RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "0");
708     RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory-policy", "noeviction");
709 
710     if (!ok) {
711         RedisModule_Log(ctx, "warning", "Failed CTXFLAGS Test. Reason: %s", errString);
712         return RedisModule_ReplyWithSimpleString(ctx, "ERR");
713     }
714 
715     return RedisModule_ReplyWithSimpleString(ctx, "OK");
716 }
717 
718 /* ----------------------------- Test framework ----------------------------- */
719 
720 /* Return 1 if the reply matches the specified string, otherwise log errors
721  * in the server log and return 0. */
TestAssertStringReply(RedisModuleCtx * ctx,RedisModuleCallReply * reply,char * str,size_t len)722 int TestAssertStringReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, char *str, size_t len) {
723     RedisModuleString *mystr, *expected;
724 
725     if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
726         RedisModule_Log(ctx,"warning","Test error reply: %s",
727             RedisModule_CallReplyStringPtr(reply, NULL));
728         return 0;
729     } else if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) {
730         RedisModule_Log(ctx,"warning","Unexpected reply type %d",
731             RedisModule_CallReplyType(reply));
732         return 0;
733     }
734     mystr = RedisModule_CreateStringFromCallReply(reply);
735     expected = RedisModule_CreateString(ctx,str,len);
736     if (RedisModule_StringCompare(mystr,expected) != 0) {
737         const char *mystr_ptr = RedisModule_StringPtrLen(mystr,NULL);
738         const char *expected_ptr = RedisModule_StringPtrLen(expected,NULL);
739         RedisModule_Log(ctx,"warning",
740             "Unexpected string reply '%s' (instead of '%s')",
741             mystr_ptr, expected_ptr);
742         return 0;
743     }
744     return 1;
745 }
746 
747 /* Return 1 if the reply matches the specified integer, otherwise log errors
748  * in the server log and return 0. */
TestAssertIntegerReply(RedisModuleCtx * ctx,RedisModuleCallReply * reply,long long expected)749 int TestAssertIntegerReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, long long expected) {
750     if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
751         RedisModule_Log(ctx,"warning","Test error reply: %s",
752             RedisModule_CallReplyStringPtr(reply, NULL));
753         return 0;
754     } else if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_INTEGER) {
755         RedisModule_Log(ctx,"warning","Unexpected reply type %d",
756             RedisModule_CallReplyType(reply));
757         return 0;
758     }
759     long long val = RedisModule_CallReplyInteger(reply);
760     if (val != expected) {
761         RedisModule_Log(ctx,"warning",
762             "Unexpected integer reply '%lld' (instead of '%lld')",
763             val, expected);
764         return 0;
765     }
766     return 1;
767 }
768 
769 #define T(name,...) \
770     do { \
771         RedisModule_Log(ctx,"warning","Testing %s", name); \
772         reply = RedisModule_Call(ctx,name,__VA_ARGS__); \
773     } while (0)
774 
775 /* TEST.BASICS -- Run all the tests.
776  * Note: it is useful to run these tests from the module rather than TCL
777  * since it's easier to check the reply types like that (make a distinction
778  * between 0 and "0", etc. */
TestBasics(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)779 int TestBasics(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
780     REDISMODULE_NOT_USED(argv);
781     REDISMODULE_NOT_USED(argc);
782 
783     RedisModule_AutoMemory(ctx);
784     RedisModuleCallReply *reply;
785 
786     /* Make sure the DB is empty before to proceed. */
787     T("dbsize","");
788     if (!TestAssertIntegerReply(ctx,reply,0)) goto fail;
789 
790     T("ping","");
791     if (!TestAssertStringReply(ctx,reply,"PONG",4)) goto fail;
792 
793     T("test.call","");
794     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
795 
796     T("test.callresp3map","");
797     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
798 
799     T("test.callresp3set","");
800     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
801 
802     T("test.callresp3double","");
803     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
804 
805     T("test.callresp3bool","");
806     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
807 
808     T("test.callresp3null","");
809     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
810 
811     T("test.callreplywithnestedreply","");
812     if (!TestAssertStringReply(ctx,reply,"test",4)) goto fail;
813 
814     T("test.callreplywithbignumberreply","");
815     if (!TestAssertStringReply(ctx,reply,"1234567999999999999999999999999999999",37)) goto fail;
816 
817     T("test.callreplywithverbatimstringreply","");
818     if (!TestAssertStringReply(ctx,reply,"txt:This is a verbatim\nstring",29)) goto fail;
819 
820     T("test.ctxflags","");
821     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
822 
823     T("test.string.append","");
824     if (!TestAssertStringReply(ctx,reply,"foobar",6)) goto fail;
825 
826     T("test.string.truncate","");
827     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
828 
829     T("test.unlink","");
830     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
831 
832     T("test.nestedcallreplyarray","");
833     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
834 
835     T("test.string.append.am","");
836     if (!TestAssertStringReply(ctx,reply,"foobar",6)) goto fail;
837 
838     T("test.string.printf", "cc", "foo", "bar");
839     if (!TestAssertStringReply(ctx,reply,"Got 3 args. argv[1]: foo, argv[2]: bar",38)) goto fail;
840 
841     T("test.notify", "");
842     if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
843 
844     T("test.callreplywitharrayreply", "");
845     if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
846     if (RedisModule_CallReplyLength(reply) != 2) goto fail;
847     if (!TestAssertStringReply(ctx,RedisModule_CallReplyArrayElement(reply, 0),"test",4)) goto fail;
848     if (!TestAssertStringReply(ctx,RedisModule_CallReplyArrayElement(reply, 1),"1234",4)) goto fail;
849 
850     RedisModule_ReplyWithSimpleString(ctx,"ALL TESTS PASSED");
851     return REDISMODULE_OK;
852 
853 fail:
854     RedisModule_ReplyWithSimpleString(ctx,
855         "SOME TEST DID NOT PASS! Check server logs");
856     return REDISMODULE_OK;
857 }
858 
RedisModule_OnLoad(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)859 int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
860     REDISMODULE_NOT_USED(argv);
861     REDISMODULE_NOT_USED(argc);
862 
863     if (RedisModule_Init(ctx,"test",1,REDISMODULE_APIVER_1)
864         == REDISMODULE_ERR) return REDISMODULE_ERR;
865 
866     if (RedisModule_CreateCommand(ctx,"test.call",
867         TestCall,"write deny-oom",1,1,1) == REDISMODULE_ERR)
868         return REDISMODULE_ERR;
869 
870     if (RedisModule_CreateCommand(ctx,"test.callresp3map",
871         TestCallResp3Map,"write deny-oom",1,1,1) == REDISMODULE_ERR)
872         return REDISMODULE_ERR;
873 
874     if (RedisModule_CreateCommand(ctx,"test.callresp3attribute",
875         TestCallResp3Attribute,"write deny-oom",1,1,1) == REDISMODULE_ERR)
876         return REDISMODULE_ERR;
877 
878     if (RedisModule_CreateCommand(ctx,"test.callresp3set",
879         TestCallResp3Set,"write deny-oom",1,1,1) == REDISMODULE_ERR)
880         return REDISMODULE_ERR;
881 
882     if (RedisModule_CreateCommand(ctx,"test.callresp3double",
883         TestCallResp3Double,"write deny-oom",1,1,1) == REDISMODULE_ERR)
884         return REDISMODULE_ERR;
885 
886     if (RedisModule_CreateCommand(ctx,"test.callresp3bool",
887         TestCallResp3Bool,"write deny-oom",1,1,1) == REDISMODULE_ERR)
888         return REDISMODULE_ERR;
889 
890     if (RedisModule_CreateCommand(ctx,"test.callresp3null",
891         TestCallResp3Null,"write deny-oom",1,1,1) == REDISMODULE_ERR)
892         return REDISMODULE_ERR;
893 
894     if (RedisModule_CreateCommand(ctx,"test.callreplywitharrayreply",
895         TestCallReplyWithArrayReply,"write deny-oom",1,1,1) == REDISMODULE_ERR)
896         return REDISMODULE_ERR;
897 
898     if (RedisModule_CreateCommand(ctx,"test.callreplywithnestedreply",
899         TestCallReplyWithNestedReply,"write deny-oom",1,1,1) == REDISMODULE_ERR)
900         return REDISMODULE_ERR;
901 
902     if (RedisModule_CreateCommand(ctx,"test.callreplywithbignumberreply",
903         TestCallResp3BigNumber,"write deny-oom",1,1,1) == REDISMODULE_ERR)
904         return REDISMODULE_ERR;
905 
906     if (RedisModule_CreateCommand(ctx,"test.callreplywithverbatimstringreply",
907         TestCallResp3Verbatim,"write deny-oom",1,1,1) == REDISMODULE_ERR)
908         return REDISMODULE_ERR;
909 
910     if (RedisModule_CreateCommand(ctx,"test.string.append",
911         TestStringAppend,"write deny-oom",1,1,1) == REDISMODULE_ERR)
912         return REDISMODULE_ERR;
913 
914     if (RedisModule_CreateCommand(ctx,"test.string.append.am",
915         TestStringAppendAM,"write deny-oom",1,1,1) == REDISMODULE_ERR)
916         return REDISMODULE_ERR;
917 
918     if (RedisModule_CreateCommand(ctx,"test.string.truncate",
919         TestStringTruncate,"write deny-oom",1,1,1) == REDISMODULE_ERR)
920         return REDISMODULE_ERR;
921 
922     if (RedisModule_CreateCommand(ctx,"test.string.printf",
923         TestStringPrintf,"write deny-oom",1,1,1) == REDISMODULE_ERR)
924         return REDISMODULE_ERR;
925 
926     if (RedisModule_CreateCommand(ctx,"test.ctxflags",
927         TestCtxFlags,"readonly",1,1,1) == REDISMODULE_ERR)
928         return REDISMODULE_ERR;
929 
930     if (RedisModule_CreateCommand(ctx,"test.unlink",
931         TestUnlink,"write deny-oom",1,1,1) == REDISMODULE_ERR)
932         return REDISMODULE_ERR;
933 
934     if (RedisModule_CreateCommand(ctx,"test.nestedcallreplyarray",
935         TestNestedCallReplyArrayElement,"write deny-oom",1,1,1) == REDISMODULE_ERR)
936         return REDISMODULE_ERR;
937 
938     if (RedisModule_CreateCommand(ctx,"test.basics",
939         TestBasics,"write",1,1,1) == REDISMODULE_ERR)
940         return REDISMODULE_ERR;
941 
942     /* the following commands are used by an external test and should not be added to TestBasics */
943     if (RedisModule_CreateCommand(ctx,"test.rmcallautomode",
944         TestCallRespAutoMode,"write",1,1,1) == REDISMODULE_ERR)
945         return REDISMODULE_ERR;
946 
947     if (RedisModule_CreateCommand(ctx,"test.getresp",
948         TestGetResp,"readonly",1,1,1) == REDISMODULE_ERR)
949         return REDISMODULE_ERR;
950 
951     RedisModule_SubscribeToKeyspaceEvents(ctx,
952                                             REDISMODULE_NOTIFY_HASH |
953                                             REDISMODULE_NOTIFY_SET |
954                                             REDISMODULE_NOTIFY_STRING |
955                                             REDISMODULE_NOTIFY_KEY_MISS,
956                                         NotifyCallback);
957     if (RedisModule_CreateCommand(ctx,"test.notify",
958         TestNotifications,"write deny-oom",1,1,1) == REDISMODULE_ERR)
959         return REDISMODULE_ERR;
960 
961     return REDISMODULE_OK;
962 }
963