basics.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. #define REDISMODULE_EXPERIMENTAL_API
  33. #include "redismodule.h"
  34. #include <string.h>
  35. /* --------------------------------- Helpers -------------------------------- */
  36. /* Return true if the reply and the C null term string matches. */
  37. int TestMatchReply(RedisModuleCallReply *reply, char *str) {
  38. RedisModuleString *mystr;
  39. mystr = RedisModule_CreateStringFromCallReply(reply);
  40. if (!mystr) return 0;
  41. const char *ptr = RedisModule_StringPtrLen(mystr,NULL);
  42. return strcmp(ptr,str) == 0;
  43. }
  44. /* ------------------------------- Test units ------------------------------- */
  45. /* TEST.CALL -- Test Call() API. */
  46. int TestCall(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  47. REDISMODULE_NOT_USED(argv);
  48. REDISMODULE_NOT_USED(argc);
  49. RedisModule_AutoMemory(ctx);
  50. RedisModuleCallReply *reply;
  51. RedisModule_Call(ctx,"DEL","c","mylist");
  52. RedisModuleString *mystr = RedisModule_CreateString(ctx,"foo",3);
  53. RedisModule_Call(ctx,"RPUSH","csl","mylist",mystr,(long long)1234);
  54. reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
  55. long long items = RedisModule_CallReplyLength(reply);
  56. if (items != 2) goto fail;
  57. RedisModuleCallReply *item0, *item1;
  58. item0 = RedisModule_CallReplyArrayElement(reply,0);
  59. item1 = RedisModule_CallReplyArrayElement(reply,1);
  60. if (!TestMatchReply(item0,"foo")) goto fail;
  61. if (!TestMatchReply(item1,"1234")) goto fail;
  62. RedisModule_ReplyWithSimpleString(ctx,"OK");
  63. return REDISMODULE_OK;
  64. fail:
  65. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  66. return REDISMODULE_OK;
  67. }
  68. int TestCallResp3Attribute(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  69. REDISMODULE_NOT_USED(argv);
  70. REDISMODULE_NOT_USED(argc);
  71. RedisModule_AutoMemory(ctx);
  72. RedisModuleCallReply *reply;
  73. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "attrib"); /* 3 stands for resp 3 reply */
  74. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) goto fail;
  75. /* make sure we can not reply to resp2 client with resp3 (it might be a string but it contains attribute) */
  76. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  77. if (!TestMatchReply(reply,"Some real reply following the attribute")) goto fail;
  78. reply = RedisModule_CallReplyAttribute(reply);
  79. if (!reply || RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ATTRIBUTE) goto fail;
  80. /* make sure we can not reply to resp2 client with resp3 attribute */
  81. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  82. if (RedisModule_CallReplyLength(reply) != 1) goto fail;
  83. RedisModuleCallReply *key, *val;
  84. if (RedisModule_CallReplyAttributeElement(reply,0,&key,&val) != REDISMODULE_OK) goto fail;
  85. if (!TestMatchReply(key,"key-popularity")) goto fail;
  86. if (RedisModule_CallReplyType(val) != REDISMODULE_REPLY_ARRAY) goto fail;
  87. if (RedisModule_CallReplyLength(val) != 2) goto fail;
  88. if (!TestMatchReply(RedisModule_CallReplyArrayElement(val, 0),"key:123")) goto fail;
  89. if (!TestMatchReply(RedisModule_CallReplyArrayElement(val, 1),"90")) goto fail;
  90. RedisModule_ReplyWithSimpleString(ctx,"OK");
  91. return REDISMODULE_OK;
  92. fail:
  93. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  94. return REDISMODULE_OK;
  95. }
  96. int TestGetResp(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  97. REDISMODULE_NOT_USED(argv);
  98. REDISMODULE_NOT_USED(argc);
  99. int flags = RedisModule_GetContextFlags(ctx);
  100. if (flags & REDISMODULE_CTX_FLAGS_RESP3) {
  101. RedisModule_ReplyWithLongLong(ctx, 3);
  102. } else {
  103. RedisModule_ReplyWithLongLong(ctx, 2);
  104. }
  105. return REDISMODULE_OK;
  106. }
  107. int TestCallRespAutoMode(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  108. REDISMODULE_NOT_USED(argv);
  109. REDISMODULE_NOT_USED(argc);
  110. RedisModule_AutoMemory(ctx);
  111. RedisModuleCallReply *reply;
  112. RedisModule_Call(ctx,"DEL","c","myhash");
  113. RedisModule_Call(ctx,"HSET","ccccc","myhash", "f1", "v1", "f2", "v2");
  114. /* 0 stands for auto mode, we will get the reply in the same format as the client */
  115. reply = RedisModule_Call(ctx,"HGETALL","0c" ,"myhash");
  116. RedisModule_ReplyWithCallReply(ctx, reply);
  117. return REDISMODULE_OK;
  118. }
  119. int TestCallResp3Map(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  120. REDISMODULE_NOT_USED(argv);
  121. REDISMODULE_NOT_USED(argc);
  122. RedisModule_AutoMemory(ctx);
  123. RedisModuleCallReply *reply;
  124. RedisModule_Call(ctx,"DEL","c","myhash");
  125. RedisModule_Call(ctx,"HSET","ccccc","myhash", "f1", "v1", "f2", "v2");
  126. reply = RedisModule_Call(ctx,"HGETALL","3c" ,"myhash"); /* 3 stands for resp 3 reply */
  127. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_MAP) goto fail;
  128. /* make sure we can not reply to resp2 client with resp3 map */
  129. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  130. long long items = RedisModule_CallReplyLength(reply);
  131. if (items != 2) goto fail;
  132. RedisModuleCallReply *key0, *key1;
  133. RedisModuleCallReply *val0, *val1;
  134. if (RedisModule_CallReplyMapElement(reply,0,&key0,&val0) != REDISMODULE_OK) goto fail;
  135. if (RedisModule_CallReplyMapElement(reply,1,&key1,&val1) != REDISMODULE_OK) goto fail;
  136. if (!TestMatchReply(key0,"f1")) goto fail;
  137. if (!TestMatchReply(key1,"f2")) goto fail;
  138. if (!TestMatchReply(val0,"v1")) goto fail;
  139. if (!TestMatchReply(val1,"v2")) goto fail;
  140. RedisModule_ReplyWithSimpleString(ctx,"OK");
  141. return REDISMODULE_OK;
  142. fail:
  143. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  144. return REDISMODULE_OK;
  145. }
  146. int TestCallResp3Bool(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  147. REDISMODULE_NOT_USED(argv);
  148. REDISMODULE_NOT_USED(argc);
  149. RedisModule_AutoMemory(ctx);
  150. RedisModuleCallReply *reply;
  151. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "true"); /* 3 stands for resp 3 reply */
  152. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BOOL) goto fail;
  153. /* make sure we can not reply to resp2 client with resp3 bool */
  154. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  155. if (!RedisModule_CallReplyBool(reply)) goto fail;
  156. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "false"); /* 3 stands for resp 3 reply */
  157. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BOOL) goto fail;
  158. if (RedisModule_CallReplyBool(reply)) goto fail;
  159. RedisModule_ReplyWithSimpleString(ctx,"OK");
  160. return REDISMODULE_OK;
  161. fail:
  162. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  163. return REDISMODULE_OK;
  164. }
  165. int TestCallResp3Null(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  166. REDISMODULE_NOT_USED(argv);
  167. REDISMODULE_NOT_USED(argc);
  168. RedisModule_AutoMemory(ctx);
  169. RedisModuleCallReply *reply;
  170. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "null"); /* 3 stands for resp 3 reply */
  171. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_NULL) goto fail;
  172. /* make sure we can not reply to resp2 client with resp3 null */
  173. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  174. RedisModule_ReplyWithSimpleString(ctx,"OK");
  175. return REDISMODULE_OK;
  176. fail:
  177. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  178. return REDISMODULE_OK;
  179. }
  180. int TestCallReplyWithNestedReply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  181. REDISMODULE_NOT_USED(argv);
  182. REDISMODULE_NOT_USED(argc);
  183. RedisModule_AutoMemory(ctx);
  184. RedisModuleCallReply *reply;
  185. RedisModule_Call(ctx,"DEL","c","mylist");
  186. RedisModule_Call(ctx,"RPUSH","ccl","mylist","test",(long long)1234);
  187. reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
  188. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
  189. if (RedisModule_CallReplyLength(reply) < 1) goto fail;
  190. RedisModuleCallReply *nestedReply = RedisModule_CallReplyArrayElement(reply, 0);
  191. RedisModule_ReplyWithCallReply(ctx,nestedReply);
  192. return REDISMODULE_OK;
  193. fail:
  194. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  195. return REDISMODULE_OK;
  196. }
  197. int TestCallReplyWithArrayReply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  198. REDISMODULE_NOT_USED(argv);
  199. REDISMODULE_NOT_USED(argc);
  200. RedisModule_AutoMemory(ctx);
  201. RedisModuleCallReply *reply;
  202. RedisModule_Call(ctx,"DEL","c","mylist");
  203. RedisModule_Call(ctx,"RPUSH","ccl","mylist","test",(long long)1234);
  204. reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
  205. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
  206. RedisModule_ReplyWithCallReply(ctx,reply);
  207. return REDISMODULE_OK;
  208. fail:
  209. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  210. return REDISMODULE_OK;
  211. }
  212. int TestCallResp3Double(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  213. REDISMODULE_NOT_USED(argv);
  214. REDISMODULE_NOT_USED(argc);
  215. RedisModule_AutoMemory(ctx);
  216. RedisModuleCallReply *reply;
  217. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "double"); /* 3 stands for resp 3 reply */
  218. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_DOUBLE) goto fail;
  219. /* make sure we can not reply to resp2 client with resp3 double*/
  220. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  221. double d = RedisModule_CallReplyDouble(reply);
  222. /* we compare strings, since comparing doubles directly can fail in various architectures, e.g. 32bit */
  223. char got[30], expected[30];
  224. sprintf(got, "%.17g", d);
  225. sprintf(expected, "%.17g", 3.14159265359);
  226. if (strcmp(got, expected) != 0) goto fail;
  227. RedisModule_ReplyWithSimpleString(ctx,"OK");
  228. return REDISMODULE_OK;
  229. fail:
  230. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  231. return REDISMODULE_OK;
  232. }
  233. int TestCallResp3BigNumber(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  234. REDISMODULE_NOT_USED(argv);
  235. REDISMODULE_NOT_USED(argc);
  236. RedisModule_AutoMemory(ctx);
  237. RedisModuleCallReply *reply;
  238. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "bignum"); /* 3 stands for resp 3 reply */
  239. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BIG_NUMBER) goto fail;
  240. /* make sure we can not reply to resp2 client with resp3 big number */
  241. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  242. size_t len;
  243. const char* big_num = RedisModule_CallReplyBigNumber(reply, &len);
  244. RedisModule_ReplyWithStringBuffer(ctx,big_num,len);
  245. return REDISMODULE_OK;
  246. fail:
  247. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  248. return REDISMODULE_OK;
  249. }
  250. int TestCallResp3Verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  251. REDISMODULE_NOT_USED(argv);
  252. REDISMODULE_NOT_USED(argc);
  253. RedisModule_AutoMemory(ctx);
  254. RedisModuleCallReply *reply;
  255. reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "verbatim"); /* 3 stands for resp 3 reply */
  256. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_VERBATIM_STRING) goto fail;
  257. /* make sure we can not reply to resp2 client with resp3 verbatim string */
  258. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  259. const char* format;
  260. size_t len;
  261. const char* str = RedisModule_CallReplyVerbatim(reply, &len, &format);
  262. RedisModuleString *s = RedisModule_CreateStringPrintf(ctx, "%.*s:%.*s", 3, format, (int)len, str);
  263. RedisModule_ReplyWithString(ctx,s);
  264. return REDISMODULE_OK;
  265. fail:
  266. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  267. return REDISMODULE_OK;
  268. }
  269. int TestCallResp3Set(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  270. REDISMODULE_NOT_USED(argv);
  271. REDISMODULE_NOT_USED(argc);
  272. RedisModule_AutoMemory(ctx);
  273. RedisModuleCallReply *reply;
  274. RedisModule_Call(ctx,"DEL","c","myset");
  275. RedisModule_Call(ctx,"sadd","ccc","myset", "v1", "v2");
  276. reply = RedisModule_Call(ctx,"smembers","3c" ,"myset"); // N stands for resp 3 reply
  277. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_SET) goto fail;
  278. /* make sure we can not reply to resp2 client with resp3 set */
  279. if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
  280. long long items = RedisModule_CallReplyLength(reply);
  281. if (items != 2) goto fail;
  282. RedisModuleCallReply *val0, *val1;
  283. val0 = RedisModule_CallReplySetElement(reply,0);
  284. val1 = RedisModule_CallReplySetElement(reply,1);
  285. /*
  286. * The order of elements on sets are not promised so we just
  287. * veridy that the reply matches one of the elements.
  288. */
  289. if (!TestMatchReply(val0,"v1") && !TestMatchReply(val0,"v2")) goto fail;
  290. if (!TestMatchReply(val1,"v1") && !TestMatchReply(val1,"v2")) goto fail;
  291. RedisModule_ReplyWithSimpleString(ctx,"OK");
  292. return REDISMODULE_OK;
  293. fail:
  294. RedisModule_ReplyWithSimpleString(ctx,"ERR");
  295. return REDISMODULE_OK;
  296. }
  297. /* TEST.STRING.APPEND -- Test appending to an existing string object. */
  298. int TestStringAppend(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  299. REDISMODULE_NOT_USED(argv);
  300. REDISMODULE_NOT_USED(argc);
  301. RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
  302. RedisModule_StringAppendBuffer(ctx,s,"bar",3);
  303. RedisModule_ReplyWithString(ctx,s);
  304. RedisModule_FreeString(ctx,s);
  305. return REDISMODULE_OK;
  306. }
  307. /* TEST.STRING.APPEND.AM -- Test append with retain when auto memory is on. */
  308. int TestStringAppendAM(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  309. REDISMODULE_NOT_USED(argv);
  310. REDISMODULE_NOT_USED(argc);
  311. RedisModule_AutoMemory(ctx);
  312. RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
  313. RedisModule_RetainString(ctx,s);
  314. RedisModule_StringAppendBuffer(ctx,s,"bar",3);
  315. RedisModule_ReplyWithString(ctx,s);
  316. RedisModule_FreeString(ctx,s);
  317. return REDISMODULE_OK;
  318. }
  319. /* TEST.STRING.PRINTF -- Test string formatting. */
  320. int TestStringPrintf(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  321. RedisModule_AutoMemory(ctx);
  322. if (argc < 3) {
  323. return RedisModule_WrongArity(ctx);
  324. }
  325. RedisModuleString *s = RedisModule_CreateStringPrintf(ctx,
  326. "Got %d args. argv[1]: %s, argv[2]: %s",
  327. argc,
  328. RedisModule_StringPtrLen(argv[1], NULL),
  329. RedisModule_StringPtrLen(argv[2], NULL)
  330. );
  331. RedisModule_ReplyWithString(ctx,s);
  332. return REDISMODULE_OK;
  333. }
  334. int failTest(RedisModuleCtx *ctx, const char *msg) {
  335. RedisModule_ReplyWithError(ctx, msg);
  336. return REDISMODULE_ERR;
  337. }
  338. int TestUnlink(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  339. RedisModule_AutoMemory(ctx);
  340. REDISMODULE_NOT_USED(argv);
  341. REDISMODULE_NOT_USED(argc);
  342. RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "unlinked"), REDISMODULE_WRITE | REDISMODULE_READ);
  343. if (!k) return failTest(ctx, "Could not create key");
  344. if (REDISMODULE_ERR == RedisModule_StringSet(k, RedisModule_CreateStringPrintf(ctx, "Foobar"))) {
  345. return failTest(ctx, "Could not set string value");
  346. }
  347. RedisModuleCallReply *rep = RedisModule_Call(ctx, "EXISTS", "c", "unlinked");
  348. if (!rep || RedisModule_CallReplyInteger(rep) != 1) {
  349. return failTest(ctx, "Key does not exist before unlink");
  350. }
  351. if (REDISMODULE_ERR == RedisModule_UnlinkKey(k)) {
  352. return failTest(ctx, "Could not unlink key");
  353. }
  354. rep = RedisModule_Call(ctx, "EXISTS", "c", "unlinked");
  355. if (!rep || RedisModule_CallReplyInteger(rep) != 0) {
  356. return failTest(ctx, "Could not verify key to be unlinked");
  357. }
  358. return RedisModule_ReplyWithSimpleString(ctx, "OK");
  359. }
  360. /* TEST.STRING.TRUNCATE -- Test truncating an existing string object. */
  361. int TestStringTruncate(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  362. RedisModule_AutoMemory(ctx);
  363. REDISMODULE_NOT_USED(argv);
  364. REDISMODULE_NOT_USED(argc);
  365. RedisModule_Call(ctx, "SET", "cc", "foo", "abcde");
  366. RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "foo"), REDISMODULE_READ | REDISMODULE_WRITE);
  367. if (!k) return failTest(ctx, "Could not create key");
  368. size_t len = 0;
  369. char* s;
  370. /* expand from 5 to 8 and check null pad */
  371. if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 8)) {
  372. return failTest(ctx, "Could not truncate string value (8)");
  373. }
  374. s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
  375. if (!s) {
  376. return failTest(ctx, "Failed to read truncated string (8)");
  377. } else if (len != 8) {
  378. return failTest(ctx, "Failed to expand string value (8)");
  379. } else if (0 != strncmp(s, "abcde\0\0\0", 8)) {
  380. return failTest(ctx, "Failed to null pad string value (8)");
  381. }
  382. /* shrink from 8 to 4 */
  383. if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 4)) {
  384. return failTest(ctx, "Could not truncate string value (4)");
  385. }
  386. s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
  387. if (!s) {
  388. return failTest(ctx, "Failed to read truncated string (4)");
  389. } else if (len != 4) {
  390. return failTest(ctx, "Failed to shrink string value (4)");
  391. } else if (0 != strncmp(s, "abcd", 4)) {
  392. return failTest(ctx, "Failed to truncate string value (4)");
  393. }
  394. /* shrink to 0 */
  395. if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 0)) {
  396. return failTest(ctx, "Could not truncate string value (0)");
  397. }
  398. s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
  399. if (!s) {
  400. return failTest(ctx, "Failed to read truncated string (0)");
  401. } else if (len != 0) {
  402. return failTest(ctx, "Failed to shrink string value to (0)");
  403. }
  404. return RedisModule_ReplyWithSimpleString(ctx, "OK");
  405. }
  406. int NotifyCallback(RedisModuleCtx *ctx, int type, const char *event,
  407. RedisModuleString *key) {
  408. RedisModule_AutoMemory(ctx);
  409. /* Increment a counter on the notifications: for each key notified we
  410. * increment a counter */
  411. RedisModule_Log(ctx, "notice", "Got event type %d, event %s, key %s", type,
  412. event, RedisModule_StringPtrLen(key, NULL));
  413. RedisModule_Call(ctx, "HINCRBY", "csc", "notifications", key, "1");
  414. return REDISMODULE_OK;
  415. }
  416. /* TEST.NOTIFICATIONS -- Test Keyspace Notifications. */
  417. int TestNotifications(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  418. RedisModule_AutoMemory(ctx);
  419. REDISMODULE_NOT_USED(argv);
  420. REDISMODULE_NOT_USED(argc);
  421. #define FAIL(msg, ...) \
  422. { \
  423. RedisModule_Log(ctx, "warning", "Failed NOTIFY Test. Reason: " #msg, ##__VA_ARGS__); \
  424. goto err; \
  425. }
  426. RedisModule_Call(ctx, "FLUSHDB", "");
  427. RedisModule_Call(ctx, "SET", "cc", "foo", "bar");
  428. RedisModule_Call(ctx, "SET", "cc", "foo", "baz");
  429. RedisModule_Call(ctx, "SADD", "cc", "bar", "x");
  430. RedisModule_Call(ctx, "SADD", "cc", "bar", "y");
  431. RedisModule_Call(ctx, "HSET", "ccc", "baz", "x", "y");
  432. /* LPUSH should be ignored and not increment any counters */
  433. RedisModule_Call(ctx, "LPUSH", "cc", "l", "y");
  434. RedisModule_Call(ctx, "LPUSH", "cc", "l", "y");
  435. /* Miss some keys intentionally so we will get a "keymiss" notification. */
  436. RedisModule_Call(ctx, "GET", "c", "nosuchkey");
  437. RedisModule_Call(ctx, "SMEMBERS", "c", "nosuchkey");
  438. size_t sz;
  439. const char *rep;
  440. RedisModuleCallReply *r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "foo");
  441. if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
  442. FAIL("Wrong or no reply for foo");
  443. } else {
  444. rep = RedisModule_CallReplyStringPtr(r, &sz);
  445. if (sz != 1 || *rep != '2') {
  446. FAIL("Got reply '%s'. expected '2'", RedisModule_CallReplyStringPtr(r, NULL));
  447. }
  448. }
  449. r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "bar");
  450. if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
  451. FAIL("Wrong or no reply for bar");
  452. } else {
  453. rep = RedisModule_CallReplyStringPtr(r, &sz);
  454. if (sz != 1 || *rep != '2') {
  455. FAIL("Got reply '%s'. expected '2'", rep);
  456. }
  457. }
  458. r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "baz");
  459. if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
  460. FAIL("Wrong or no reply for baz");
  461. } else {
  462. rep = RedisModule_CallReplyStringPtr(r, &sz);
  463. if (sz != 1 || *rep != '1') {
  464. FAIL("Got reply '%.*s'. expected '1'", (int)sz, rep);
  465. }
  466. }
  467. /* For l we expect nothing since we didn't subscribe to list events */
  468. r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "l");
  469. if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_NULL) {
  470. FAIL("Wrong reply for l");
  471. }
  472. r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "nosuchkey");
  473. if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
  474. FAIL("Wrong or no reply for nosuchkey");
  475. } else {
  476. rep = RedisModule_CallReplyStringPtr(r, &sz);
  477. if (sz != 1 || *rep != '2') {
  478. FAIL("Got reply '%.*s'. expected '2'", (int)sz, rep);
  479. }
  480. }
  481. RedisModule_Call(ctx, "FLUSHDB", "");
  482. return RedisModule_ReplyWithSimpleString(ctx, "OK");
  483. err:
  484. RedisModule_Call(ctx, "FLUSHDB", "");
  485. return RedisModule_ReplyWithSimpleString(ctx, "ERR");
  486. }
  487. /* TEST.CTXFLAGS -- Test GetContextFlags. */
  488. int TestCtxFlags(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  489. REDISMODULE_NOT_USED(argc);
  490. REDISMODULE_NOT_USED(argv);
  491. RedisModule_AutoMemory(ctx);
  492. int ok = 1;
  493. const char *errString = NULL;
  494. #undef FAIL
  495. #define FAIL(msg) \
  496. { \
  497. ok = 0; \
  498. errString = msg; \
  499. goto end; \
  500. }
  501. int flags = RedisModule_GetContextFlags(ctx);
  502. if (flags == 0) {
  503. FAIL("Got no flags");
  504. }
  505. if (flags & REDISMODULE_CTX_FLAGS_LUA) FAIL("Lua flag was set");
  506. if (flags & REDISMODULE_CTX_FLAGS_MULTI) FAIL("Multi flag was set");
  507. if (flags & REDISMODULE_CTX_FLAGS_AOF) FAIL("AOF Flag was set")
  508. /* Enable AOF to test AOF flags */
  509. RedisModule_Call(ctx, "config", "ccc", "set", "appendonly", "yes");
  510. flags = RedisModule_GetContextFlags(ctx);
  511. if (!(flags & REDISMODULE_CTX_FLAGS_AOF)) FAIL("AOF Flag not set after config set");
  512. /* Disable RDB saving and test the flag. */
  513. RedisModule_Call(ctx, "config", "ccc", "set", "save", "");
  514. flags = RedisModule_GetContextFlags(ctx);
  515. if (flags & REDISMODULE_CTX_FLAGS_RDB) FAIL("RDB Flag was set");
  516. /* Enable RDB to test RDB flags */
  517. RedisModule_Call(ctx, "config", "ccc", "set", "save", "900 1");
  518. flags = RedisModule_GetContextFlags(ctx);
  519. if (!(flags & REDISMODULE_CTX_FLAGS_RDB)) FAIL("RDB Flag was not set after config set");
  520. if (!(flags & REDISMODULE_CTX_FLAGS_MASTER)) FAIL("Master flag was not set");
  521. if (flags & REDISMODULE_CTX_FLAGS_SLAVE) FAIL("Slave flag was set");
  522. if (flags & REDISMODULE_CTX_FLAGS_READONLY) FAIL("Read-only flag was set");
  523. if (flags & REDISMODULE_CTX_FLAGS_CLUSTER) FAIL("Cluster flag was set");
  524. /* Disable maxmemory and test the flag. (it is implicitly set in 32bit builds. */
  525. RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "0");
  526. flags = RedisModule_GetContextFlags(ctx);
  527. if (flags & REDISMODULE_CTX_FLAGS_MAXMEMORY) FAIL("Maxmemory flag was set");
  528. /* Enable maxmemory and test the flag. */
  529. RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "100000000");
  530. flags = RedisModule_GetContextFlags(ctx);
  531. if (!(flags & REDISMODULE_CTX_FLAGS_MAXMEMORY))
  532. FAIL("Maxmemory flag was not set after config set");
  533. if (flags & REDISMODULE_CTX_FLAGS_EVICT) FAIL("Eviction flag was set");
  534. RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory-policy", "allkeys-lru");
  535. flags = RedisModule_GetContextFlags(ctx);
  536. if (!(flags & REDISMODULE_CTX_FLAGS_EVICT)) FAIL("Eviction flag was not set after config set");
  537. end:
  538. /* Revert config changes */
  539. RedisModule_Call(ctx, "config", "ccc", "set", "appendonly", "no");
  540. RedisModule_Call(ctx, "config", "ccc", "set", "save", "");
  541. RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "0");
  542. RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory-policy", "noeviction");
  543. if (!ok) {
  544. RedisModule_Log(ctx, "warning", "Failed CTXFLAGS Test. Reason: %s", errString);
  545. return RedisModule_ReplyWithSimpleString(ctx, "ERR");
  546. }
  547. return RedisModule_ReplyWithSimpleString(ctx, "OK");
  548. }
  549. /* ----------------------------- Test framework ----------------------------- */
  550. /* Return 1 if the reply matches the specified string, otherwise log errors
  551. * in the server log and return 0. */
  552. int TestAssertStringReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, char *str, size_t len) {
  553. RedisModuleString *mystr, *expected;
  554. if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
  555. RedisModule_Log(ctx,"warning","Test error reply: %s",
  556. RedisModule_CallReplyStringPtr(reply, NULL));
  557. return 0;
  558. } else if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) {
  559. RedisModule_Log(ctx,"warning","Unexpected reply type %d",
  560. RedisModule_CallReplyType(reply));
  561. return 0;
  562. }
  563. mystr = RedisModule_CreateStringFromCallReply(reply);
  564. expected = RedisModule_CreateString(ctx,str,len);
  565. if (RedisModule_StringCompare(mystr,expected) != 0) {
  566. const char *mystr_ptr = RedisModule_StringPtrLen(mystr,NULL);
  567. const char *expected_ptr = RedisModule_StringPtrLen(expected,NULL);
  568. RedisModule_Log(ctx,"warning",
  569. "Unexpected string reply '%s' (instead of '%s')",
  570. mystr_ptr, expected_ptr);
  571. return 0;
  572. }
  573. return 1;
  574. }
  575. /* Return 1 if the reply matches the specified integer, otherwise log errors
  576. * in the server log and return 0. */
  577. int TestAssertIntegerReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, long long expected) {
  578. if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
  579. RedisModule_Log(ctx,"warning","Test error reply: %s",
  580. RedisModule_CallReplyStringPtr(reply, NULL));
  581. return 0;
  582. } else if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_INTEGER) {
  583. RedisModule_Log(ctx,"warning","Unexpected reply type %d",
  584. RedisModule_CallReplyType(reply));
  585. return 0;
  586. }
  587. long long val = RedisModule_CallReplyInteger(reply);
  588. if (val != expected) {
  589. RedisModule_Log(ctx,"warning",
  590. "Unexpected integer reply '%lld' (instead of '%lld')",
  591. val, expected);
  592. return 0;
  593. }
  594. return 1;
  595. }
  596. #define T(name,...) \
  597. do { \
  598. RedisModule_Log(ctx,"warning","Testing %s", name); \
  599. reply = RedisModule_Call(ctx,name,__VA_ARGS__); \
  600. } while (0)
  601. /* TEST.BASICS -- Run all the tests.
  602. * Note: it is useful to run these tests from the module rather than TCL
  603. * since it's easier to check the reply types like that (make a distinction
  604. * between 0 and "0", etc. */
  605. int TestBasics(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  606. REDISMODULE_NOT_USED(argv);
  607. REDISMODULE_NOT_USED(argc);
  608. RedisModule_AutoMemory(ctx);
  609. RedisModuleCallReply *reply;
  610. /* Make sure the DB is empty before to proceed. */
  611. T("dbsize","");
  612. if (!TestAssertIntegerReply(ctx,reply,0)) goto fail;
  613. T("ping","");
  614. if (!TestAssertStringReply(ctx,reply,"PONG",4)) goto fail;
  615. T("test.call","");
  616. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  617. T("test.callresp3map","");
  618. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  619. T("test.callresp3set","");
  620. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  621. T("test.callresp3double","");
  622. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  623. T("test.callresp3bool","");
  624. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  625. T("test.callresp3null","");
  626. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  627. T("test.callreplywithnestedreply","");
  628. if (!TestAssertStringReply(ctx,reply,"test",4)) goto fail;
  629. T("test.callreplywithbignumberreply","");
  630. if (!TestAssertStringReply(ctx,reply,"1234567999999999999999999999999999999",37)) goto fail;
  631. T("test.callreplywithverbatimstringreply","");
  632. if (!TestAssertStringReply(ctx,reply,"txt:This is a verbatim\nstring",29)) goto fail;
  633. T("test.ctxflags","");
  634. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  635. T("test.string.append","");
  636. if (!TestAssertStringReply(ctx,reply,"foobar",6)) goto fail;
  637. T("test.string.truncate","");
  638. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  639. T("test.unlink","");
  640. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  641. T("test.string.append.am","");
  642. if (!TestAssertStringReply(ctx,reply,"foobar",6)) goto fail;
  643. T("test.string.printf", "cc", "foo", "bar");
  644. if (!TestAssertStringReply(ctx,reply,"Got 3 args. argv[1]: foo, argv[2]: bar",38)) goto fail;
  645. T("test.notify", "");
  646. if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
  647. T("test.callreplywitharrayreply", "");
  648. if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
  649. if (RedisModule_CallReplyLength(reply) != 2) goto fail;
  650. if (!TestAssertStringReply(ctx,RedisModule_CallReplyArrayElement(reply, 0),"test",4)) goto fail;
  651. if (!TestAssertStringReply(ctx,RedisModule_CallReplyArrayElement(reply, 1),"1234",4)) goto fail;
  652. RedisModule_ReplyWithSimpleString(ctx,"ALL TESTS PASSED");
  653. return REDISMODULE_OK;
  654. fail:
  655. RedisModule_ReplyWithSimpleString(ctx,
  656. "SOME TEST DID NOT PASS! Check server logs");
  657. return REDISMODULE_OK;
  658. }
  659. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  660. REDISMODULE_NOT_USED(argv);
  661. REDISMODULE_NOT_USED(argc);
  662. if (RedisModule_Init(ctx,"test",1,REDISMODULE_APIVER_1)
  663. == REDISMODULE_ERR) return REDISMODULE_ERR;
  664. if (RedisModule_CreateCommand(ctx,"test.call",
  665. TestCall,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  666. return REDISMODULE_ERR;
  667. if (RedisModule_CreateCommand(ctx,"test.callresp3map",
  668. TestCallResp3Map,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  669. return REDISMODULE_ERR;
  670. if (RedisModule_CreateCommand(ctx,"test.callresp3attribute",
  671. TestCallResp3Attribute,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  672. return REDISMODULE_ERR;
  673. if (RedisModule_CreateCommand(ctx,"test.callresp3set",
  674. TestCallResp3Set,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  675. return REDISMODULE_ERR;
  676. if (RedisModule_CreateCommand(ctx,"test.callresp3double",
  677. TestCallResp3Double,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  678. return REDISMODULE_ERR;
  679. if (RedisModule_CreateCommand(ctx,"test.callresp3bool",
  680. TestCallResp3Bool,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  681. return REDISMODULE_ERR;
  682. if (RedisModule_CreateCommand(ctx,"test.callresp3null",
  683. TestCallResp3Null,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  684. return REDISMODULE_ERR;
  685. if (RedisModule_CreateCommand(ctx,"test.callreplywitharrayreply",
  686. TestCallReplyWithArrayReply,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  687. return REDISMODULE_ERR;
  688. if (RedisModule_CreateCommand(ctx,"test.callreplywithnestedreply",
  689. TestCallReplyWithNestedReply,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  690. return REDISMODULE_ERR;
  691. if (RedisModule_CreateCommand(ctx,"test.callreplywithbignumberreply",
  692. TestCallResp3BigNumber,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  693. return REDISMODULE_ERR;
  694. if (RedisModule_CreateCommand(ctx,"test.callreplywithverbatimstringreply",
  695. TestCallResp3Verbatim,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  696. return REDISMODULE_ERR;
  697. if (RedisModule_CreateCommand(ctx,"test.string.append",
  698. TestStringAppend,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  699. return REDISMODULE_ERR;
  700. if (RedisModule_CreateCommand(ctx,"test.string.append.am",
  701. TestStringAppendAM,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  702. return REDISMODULE_ERR;
  703. if (RedisModule_CreateCommand(ctx,"test.string.truncate",
  704. TestStringTruncate,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  705. return REDISMODULE_ERR;
  706. if (RedisModule_CreateCommand(ctx,"test.string.printf",
  707. TestStringPrintf,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  708. return REDISMODULE_ERR;
  709. if (RedisModule_CreateCommand(ctx,"test.ctxflags",
  710. TestCtxFlags,"readonly",1,1,1) == REDISMODULE_ERR)
  711. return REDISMODULE_ERR;
  712. if (RedisModule_CreateCommand(ctx,"test.unlink",
  713. TestUnlink,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  714. return REDISMODULE_ERR;
  715. if (RedisModule_CreateCommand(ctx,"test.basics",
  716. TestBasics,"readonly",1,1,1) == REDISMODULE_ERR)
  717. return REDISMODULE_ERR;
  718. /* the following commands are used by an external test and should not be added to TestBasics */
  719. if (RedisModule_CreateCommand(ctx,"test.rmcallautomode",
  720. TestCallRespAutoMode,"readonly",1,1,1) == REDISMODULE_ERR)
  721. return REDISMODULE_ERR;
  722. if (RedisModule_CreateCommand(ctx,"test.getresp",
  723. TestGetResp,"readonly",1,1,1) == REDISMODULE_ERR)
  724. return REDISMODULE_ERR;
  725. RedisModule_SubscribeToKeyspaceEvents(ctx,
  726. REDISMODULE_NOTIFY_HASH |
  727. REDISMODULE_NOTIFY_SET |
  728. REDISMODULE_NOTIFY_STRING |
  729. REDISMODULE_NOTIFY_KEY_MISS,
  730. NotifyCallback);
  731. if (RedisModule_CreateCommand(ctx,"test.notify",
  732. TestNotifications,"write deny-oom",1,1,1) == REDISMODULE_ERR)
  733. return REDISMODULE_ERR;
  734. return REDISMODULE_OK;
  735. }