test.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #include "fmacros.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <strings.h>
  6. #include <sys/time.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <errno.h>
  11. #include <limits.h>
  12. #include "hiredis.h"
  13. #include "net.h"
  14. enum connection_type {
  15. CONN_TCP,
  16. CONN_UNIX,
  17. CONN_FD
  18. };
  19. struct config {
  20. enum connection_type type;
  21. struct {
  22. const char *host;
  23. int port;
  24. struct timeval timeout;
  25. } tcp;
  26. struct {
  27. const char *path;
  28. } unix_sock;
  29. };
  30. /* The following lines make up our testing "framework" :) */
  31. static int tests = 0, fails = 0;
  32. #define test(_s) { printf("#%02d ", ++tests); printf(_s); }
  33. #define test_cond(_c) if(_c) printf("\033[0;32mPASSED\033[0;0m\n"); else {printf("\033[0;31mFAILED\033[0;0m\n"); fails++;}
  34. static long long usec(void) {
  35. struct timeval tv;
  36. gettimeofday(&tv,NULL);
  37. return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
  38. }
  39. /* The assert() calls below have side effects, so we need assert()
  40. * even if we are compiling without asserts (-DNDEBUG). */
  41. #ifdef NDEBUG
  42. #undef assert
  43. #define assert(e) (void)(e)
  44. #endif
  45. static redisContext *select_database(redisContext *c) {
  46. redisReply *reply;
  47. /* Switch to DB 9 for testing, now that we know we can chat. */
  48. reply = redisCommand(c,"SELECT 9");
  49. assert(reply != NULL);
  50. freeReplyObject(reply);
  51. /* Make sure the DB is emtpy */
  52. reply = redisCommand(c,"DBSIZE");
  53. assert(reply != NULL);
  54. if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0) {
  55. /* Awesome, DB 9 is empty and we can continue. */
  56. freeReplyObject(reply);
  57. } else {
  58. printf("Database #9 is not empty, test can not continue\n");
  59. exit(1);
  60. }
  61. return c;
  62. }
  63. static int disconnect(redisContext *c, int keep_fd) {
  64. redisReply *reply;
  65. /* Make sure we're on DB 9. */
  66. reply = redisCommand(c,"SELECT 9");
  67. assert(reply != NULL);
  68. freeReplyObject(reply);
  69. reply = redisCommand(c,"FLUSHDB");
  70. assert(reply != NULL);
  71. freeReplyObject(reply);
  72. /* Free the context as well, but keep the fd if requested. */
  73. if (keep_fd)
  74. return redisFreeKeepFd(c);
  75. redisFree(c);
  76. return -1;
  77. }
  78. static redisContext *connect(struct config config) {
  79. redisContext *c = NULL;
  80. if (config.type == CONN_TCP) {
  81. c = redisConnect(config.tcp.host, config.tcp.port);
  82. } else if (config.type == CONN_UNIX) {
  83. c = redisConnectUnix(config.unix_sock.path);
  84. } else if (config.type == CONN_FD) {
  85. /* Create a dummy connection just to get an fd to inherit */
  86. redisContext *dummy_ctx = redisConnectUnix(config.unix_sock.path);
  87. if (dummy_ctx) {
  88. int fd = disconnect(dummy_ctx, 1);
  89. printf("Connecting to inherited fd %d\n", fd);
  90. c = redisConnectFd(fd);
  91. }
  92. } else {
  93. assert(NULL);
  94. }
  95. if (c == NULL) {
  96. printf("Connection error: can't allocate redis context\n");
  97. exit(1);
  98. } else if (c->err) {
  99. printf("Connection error: %s\n", c->errstr);
  100. redisFree(c);
  101. exit(1);
  102. }
  103. return select_database(c);
  104. }
  105. static void test_format_commands(void) {
  106. char *cmd;
  107. int len;
  108. test("Format command without interpolation: ");
  109. len = redisFormatCommand(&cmd,"SET foo bar");
  110. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  111. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  112. free(cmd);
  113. test("Format command with %%s string interpolation: ");
  114. len = redisFormatCommand(&cmd,"SET %s %s","foo","bar");
  115. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  116. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  117. free(cmd);
  118. test("Format command with %%s and an empty string: ");
  119. len = redisFormatCommand(&cmd,"SET %s %s","foo","");
  120. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
  121. len == 4+4+(3+2)+4+(3+2)+4+(0+2));
  122. free(cmd);
  123. test("Format command with an empty string in between proper interpolations: ");
  124. len = redisFormatCommand(&cmd,"SET %s %s","","foo");
  125. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 &&
  126. len == 4+4+(3+2)+4+(0+2)+4+(3+2));
  127. free(cmd);
  128. test("Format command with %%b string interpolation: ");
  129. len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"b\0r",(size_t)3);
  130. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 &&
  131. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  132. free(cmd);
  133. test("Format command with %%b and an empty string: ");
  134. len = redisFormatCommand(&cmd,"SET %b %b","foo",(size_t)3,"",(size_t)0);
  135. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$0\r\n\r\n",len) == 0 &&
  136. len == 4+4+(3+2)+4+(3+2)+4+(0+2));
  137. free(cmd);
  138. test("Format command with literal %%: ");
  139. len = redisFormatCommand(&cmd,"SET %% %%");
  140. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$1\r\n%\r\n$1\r\n%\r\n",len) == 0 &&
  141. len == 4+4+(3+2)+4+(1+2)+4+(1+2));
  142. free(cmd);
  143. /* Vararg width depends on the type. These tests make sure that the
  144. * width is correctly determined using the format and subsequent varargs
  145. * can correctly be interpolated. */
  146. #define INTEGER_WIDTH_TEST(fmt, type) do { \
  147. type value = 123; \
  148. test("Format command with printf-delegation (" #type "): "); \
  149. len = redisFormatCommand(&cmd,"key:%08" fmt " str:%s", value, "hello"); \
  150. test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:00000123\r\n$9\r\nstr:hello\r\n",len) == 0 && \
  151. len == 4+5+(12+2)+4+(9+2)); \
  152. free(cmd); \
  153. } while(0)
  154. #define FLOAT_WIDTH_TEST(type) do { \
  155. type value = 123.0; \
  156. test("Format command with printf-delegation (" #type "): "); \
  157. len = redisFormatCommand(&cmd,"key:%08.3f str:%s", value, "hello"); \
  158. test_cond(strncmp(cmd,"*2\r\n$12\r\nkey:0123.000\r\n$9\r\nstr:hello\r\n",len) == 0 && \
  159. len == 4+5+(12+2)+4+(9+2)); \
  160. free(cmd); \
  161. } while(0)
  162. INTEGER_WIDTH_TEST("d", int);
  163. INTEGER_WIDTH_TEST("hhd", char);
  164. INTEGER_WIDTH_TEST("hd", short);
  165. INTEGER_WIDTH_TEST("ld", long);
  166. INTEGER_WIDTH_TEST("lld", long long);
  167. INTEGER_WIDTH_TEST("u", unsigned int);
  168. INTEGER_WIDTH_TEST("hhu", unsigned char);
  169. INTEGER_WIDTH_TEST("hu", unsigned short);
  170. INTEGER_WIDTH_TEST("lu", unsigned long);
  171. INTEGER_WIDTH_TEST("llu", unsigned long long);
  172. FLOAT_WIDTH_TEST(float);
  173. FLOAT_WIDTH_TEST(double);
  174. test("Format command with invalid printf format: ");
  175. len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",(size_t)3);
  176. test_cond(len == -1);
  177. const char *argv[3];
  178. argv[0] = "SET";
  179. argv[1] = "foo\0xxx";
  180. argv[2] = "bar";
  181. size_t lens[3] = { 3, 7, 3 };
  182. int argc = 3;
  183. test("Format command by passing argc/argv without lengths: ");
  184. len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
  185. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  186. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  187. free(cmd);
  188. test("Format command by passing argc/argv with lengths: ");
  189. len = redisFormatCommandArgv(&cmd,argc,argv,lens);
  190. test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$7\r\nfoo\0xxx\r\n$3\r\nbar\r\n",len) == 0 &&
  191. len == 4+4+(3+2)+4+(7+2)+4+(3+2));
  192. free(cmd);
  193. sds sds_cmd;
  194. sds_cmd = sdsempty();
  195. test("Format command into sds by passing argc/argv without lengths: ");
  196. len = redisFormatSdsCommandArgv(&sds_cmd,argc,argv,NULL);
  197. test_cond(strncmp(sds_cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",len) == 0 &&
  198. len == 4+4+(3+2)+4+(3+2)+4+(3+2));
  199. sdsfree(sds_cmd);
  200. sds_cmd = sdsempty();
  201. test("Format command into sds by passing argc/argv with lengths: ");
  202. len = redisFormatSdsCommandArgv(&sds_cmd,argc,argv,lens);
  203. test_cond(strncmp(sds_cmd,"*3\r\n$3\r\nSET\r\n$7\r\nfoo\0xxx\r\n$3\r\nbar\r\n",len) == 0 &&
  204. len == 4+4+(3+2)+4+(7+2)+4+(3+2));
  205. sdsfree(sds_cmd);
  206. }
  207. static void test_append_formatted_commands(struct config config) {
  208. redisContext *c;
  209. redisReply *reply;
  210. char *cmd;
  211. int len;
  212. c = connect(config);
  213. test("Append format command: ");
  214. len = redisFormatCommand(&cmd, "SET foo bar");
  215. test_cond(redisAppendFormattedCommand(c, cmd, len) == REDIS_OK);
  216. assert(redisGetReply(c, (void*)&reply) == REDIS_OK);
  217. free(cmd);
  218. freeReplyObject(reply);
  219. disconnect(c, 0);
  220. }
  221. static void test_reply_reader(void) {
  222. redisReader *reader;
  223. void *reply;
  224. int ret;
  225. int i;
  226. test("Error handling in reply parser: ");
  227. reader = redisReaderCreate();
  228. redisReaderFeed(reader,(char*)"@foo\r\n",6);
  229. ret = redisReaderGetReply(reader,NULL);
  230. test_cond(ret == REDIS_ERR &&
  231. strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
  232. redisReaderFree(reader);
  233. /* when the reply already contains multiple items, they must be free'd
  234. * on an error. valgrind will bark when this doesn't happen. */
  235. test("Memory cleanup in reply parser: ");
  236. reader = redisReaderCreate();
  237. redisReaderFeed(reader,(char*)"*2\r\n",4);
  238. redisReaderFeed(reader,(char*)"$5\r\nhello\r\n",11);
  239. redisReaderFeed(reader,(char*)"@foo\r\n",6);
  240. ret = redisReaderGetReply(reader,NULL);
  241. test_cond(ret == REDIS_ERR &&
  242. strcasecmp(reader->errstr,"Protocol error, got \"@\" as reply type byte") == 0);
  243. redisReaderFree(reader);
  244. test("Set error on nested multi bulks with depth > 7: ");
  245. reader = redisReaderCreate();
  246. for (i = 0; i < 9; i++) {
  247. redisReaderFeed(reader,(char*)"*1\r\n",4);
  248. }
  249. ret = redisReaderGetReply(reader,NULL);
  250. test_cond(ret == REDIS_ERR &&
  251. strncasecmp(reader->errstr,"No support for",14) == 0);
  252. redisReaderFree(reader);
  253. test("Correctly parses LLONG_MAX: ");
  254. reader = redisReaderCreate();
  255. redisReaderFeed(reader, ":9223372036854775807\r\n",22);
  256. ret = redisReaderGetReply(reader,&reply);
  257. test_cond(ret == REDIS_OK &&
  258. ((redisReply*)reply)->type == REDIS_REPLY_INTEGER &&
  259. ((redisReply*)reply)->integer == LLONG_MAX);
  260. freeReplyObject(reply);
  261. redisReaderFree(reader);
  262. test("Set error when > LLONG_MAX: ");
  263. reader = redisReaderCreate();
  264. redisReaderFeed(reader, ":9223372036854775808\r\n",22);
  265. ret = redisReaderGetReply(reader,&reply);
  266. test_cond(ret == REDIS_ERR &&
  267. strcasecmp(reader->errstr,"Bad integer value") == 0);
  268. freeReplyObject(reply);
  269. redisReaderFree(reader);
  270. test("Correctly parses LLONG_MIN: ");
  271. reader = redisReaderCreate();
  272. redisReaderFeed(reader, ":-9223372036854775808\r\n",23);
  273. ret = redisReaderGetReply(reader,&reply);
  274. test_cond(ret == REDIS_OK &&
  275. ((redisReply*)reply)->type == REDIS_REPLY_INTEGER &&
  276. ((redisReply*)reply)->integer == LLONG_MIN);
  277. freeReplyObject(reply);
  278. redisReaderFree(reader);
  279. test("Set error when < LLONG_MIN: ");
  280. reader = redisReaderCreate();
  281. redisReaderFeed(reader, ":-9223372036854775809\r\n",23);
  282. ret = redisReaderGetReply(reader,&reply);
  283. test_cond(ret == REDIS_ERR &&
  284. strcasecmp(reader->errstr,"Bad integer value") == 0);
  285. freeReplyObject(reply);
  286. redisReaderFree(reader);
  287. test("Set error when array < -1: ");
  288. reader = redisReaderCreate();
  289. redisReaderFeed(reader, "*-2\r\n+asdf\r\n",12);
  290. ret = redisReaderGetReply(reader,&reply);
  291. test_cond(ret == REDIS_ERR &&
  292. strcasecmp(reader->errstr,"Multi-bulk length out of range") == 0);
  293. freeReplyObject(reply);
  294. redisReaderFree(reader);
  295. test("Set error when bulk < -1: ");
  296. reader = redisReaderCreate();
  297. redisReaderFeed(reader, "$-2\r\nasdf\r\n",11);
  298. ret = redisReaderGetReply(reader,&reply);
  299. test_cond(ret == REDIS_ERR &&
  300. strcasecmp(reader->errstr,"Bulk string length out of range") == 0);
  301. freeReplyObject(reply);
  302. redisReaderFree(reader);
  303. test("Set error when array > INT_MAX: ");
  304. reader = redisReaderCreate();
  305. redisReaderFeed(reader, "*9223372036854775807\r\n+asdf\r\n",29);
  306. ret = redisReaderGetReply(reader,&reply);
  307. test_cond(ret == REDIS_ERR &&
  308. strcasecmp(reader->errstr,"Multi-bulk length out of range") == 0);
  309. freeReplyObject(reply);
  310. redisReaderFree(reader);
  311. #if LLONG_MAX > SIZE_MAX
  312. test("Set error when bulk > SIZE_MAX: ");
  313. reader = redisReaderCreate();
  314. redisReaderFeed(reader, "$9223372036854775807\r\nasdf\r\n",28);
  315. ret = redisReaderGetReply(reader,&reply);
  316. test_cond(ret == REDIS_ERR &&
  317. strcasecmp(reader->errstr,"Bulk string length out of range") == 0);
  318. freeReplyObject(reply);
  319. redisReaderFree(reader);
  320. #endif
  321. test("Works with NULL functions for reply: ");
  322. reader = redisReaderCreate();
  323. reader->fn = NULL;
  324. redisReaderFeed(reader,(char*)"+OK\r\n",5);
  325. ret = redisReaderGetReply(reader,&reply);
  326. test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
  327. redisReaderFree(reader);
  328. test("Works when a single newline (\\r\\n) covers two calls to feed: ");
  329. reader = redisReaderCreate();
  330. reader->fn = NULL;
  331. redisReaderFeed(reader,(char*)"+OK\r",4);
  332. ret = redisReaderGetReply(reader,&reply);
  333. assert(ret == REDIS_OK && reply == NULL);
  334. redisReaderFeed(reader,(char*)"\n",1);
  335. ret = redisReaderGetReply(reader,&reply);
  336. test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
  337. redisReaderFree(reader);
  338. test("Don't reset state after protocol error: ");
  339. reader = redisReaderCreate();
  340. reader->fn = NULL;
  341. redisReaderFeed(reader,(char*)"x",1);
  342. ret = redisReaderGetReply(reader,&reply);
  343. assert(ret == REDIS_ERR);
  344. ret = redisReaderGetReply(reader,&reply);
  345. test_cond(ret == REDIS_ERR && reply == NULL);
  346. redisReaderFree(reader);
  347. /* Regression test for issue #45 on GitHub. */
  348. test("Don't do empty allocation for empty multi bulk: ");
  349. reader = redisReaderCreate();
  350. redisReaderFeed(reader,(char*)"*0\r\n",4);
  351. ret = redisReaderGetReply(reader,&reply);
  352. test_cond(ret == REDIS_OK &&
  353. ((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&
  354. ((redisReply*)reply)->elements == 0);
  355. freeReplyObject(reply);
  356. redisReaderFree(reader);
  357. }
  358. static void test_free_null(void) {
  359. void *redisCtx = NULL;
  360. void *reply = NULL;
  361. test("Don't fail when redisFree is passed a NULL value: ");
  362. redisFree(redisCtx);
  363. test_cond(redisCtx == NULL);
  364. test("Don't fail when freeReplyObject is passed a NULL value: ");
  365. freeReplyObject(reply);
  366. test_cond(reply == NULL);
  367. }
  368. static void test_blocking_connection_errors(void) {
  369. redisContext *c;
  370. test("Returns error when host cannot be resolved: ");
  371. c = redisConnect((char*)"idontexist.test", 6379);
  372. test_cond(c->err == REDIS_ERR_OTHER &&
  373. (strcmp(c->errstr,"Name or service not known") == 0 ||
  374. strcmp(c->errstr,"Can't resolve: idontexist.test") == 0 ||
  375. strcmp(c->errstr,"nodename nor servname provided, or not known") == 0 ||
  376. strcmp(c->errstr,"No address associated with hostname") == 0 ||
  377. strcmp(c->errstr,"Temporary failure in name resolution") == 0 ||
  378. strcmp(c->errstr,"hostname nor servname provided, or not known") == 0 ||
  379. strcmp(c->errstr,"no address associated with name") == 0));
  380. redisFree(c);
  381. test("Returns error when the port is not open: ");
  382. c = redisConnect((char*)"localhost", 1);
  383. test_cond(c->err == REDIS_ERR_IO &&
  384. strcmp(c->errstr,"Connection refused") == 0);
  385. redisFree(c);
  386. test("Returns error when the unix_sock socket path doesn't accept connections: ");
  387. c = redisConnectUnix((char*)"/tmp/idontexist.sock");
  388. test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */
  389. redisFree(c);
  390. }
  391. static void test_blocking_connection(struct config config) {
  392. redisContext *c;
  393. redisReply *reply;
  394. c = connect(config);
  395. test("Is able to deliver commands: ");
  396. reply = redisCommand(c,"PING");
  397. test_cond(reply->type == REDIS_REPLY_STATUS &&
  398. strcasecmp(reply->str,"pong") == 0)
  399. freeReplyObject(reply);
  400. test("Is a able to send commands verbatim: ");
  401. reply = redisCommand(c,"SET foo bar");
  402. test_cond (reply->type == REDIS_REPLY_STATUS &&
  403. strcasecmp(reply->str,"ok") == 0)
  404. freeReplyObject(reply);
  405. test("%%s String interpolation works: ");
  406. reply = redisCommand(c,"SET %s %s","foo","hello world");
  407. freeReplyObject(reply);
  408. reply = redisCommand(c,"GET foo");
  409. test_cond(reply->type == REDIS_REPLY_STRING &&
  410. strcmp(reply->str,"hello world") == 0);
  411. freeReplyObject(reply);
  412. test("%%b String interpolation works: ");
  413. reply = redisCommand(c,"SET %b %b","foo",(size_t)3,"hello\x00world",(size_t)11);
  414. freeReplyObject(reply);
  415. reply = redisCommand(c,"GET foo");
  416. test_cond(reply->type == REDIS_REPLY_STRING &&
  417. memcmp(reply->str,"hello\x00world",11) == 0)
  418. test("Binary reply length is correct: ");
  419. test_cond(reply->len == 11)
  420. freeReplyObject(reply);
  421. test("Can parse nil replies: ");
  422. reply = redisCommand(c,"GET nokey");
  423. test_cond(reply->type == REDIS_REPLY_NIL)
  424. freeReplyObject(reply);
  425. /* test 7 */
  426. test("Can parse integer replies: ");
  427. reply = redisCommand(c,"INCR mycounter");
  428. test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
  429. freeReplyObject(reply);
  430. test("Can parse multi bulk replies: ");
  431. freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
  432. freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
  433. reply = redisCommand(c,"LRANGE mylist 0 -1");
  434. test_cond(reply->type == REDIS_REPLY_ARRAY &&
  435. reply->elements == 2 &&
  436. !memcmp(reply->element[0]->str,"bar",3) &&
  437. !memcmp(reply->element[1]->str,"foo",3))
  438. freeReplyObject(reply);
  439. /* m/e with multi bulk reply *before* other reply.
  440. * specifically test ordering of reply items to parse. */
  441. test("Can handle nested multi bulk replies: ");
  442. freeReplyObject(redisCommand(c,"MULTI"));
  443. freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1"));
  444. freeReplyObject(redisCommand(c,"PING"));
  445. reply = (redisCommand(c,"EXEC"));
  446. test_cond(reply->type == REDIS_REPLY_ARRAY &&
  447. reply->elements == 2 &&
  448. reply->element[0]->type == REDIS_REPLY_ARRAY &&
  449. reply->element[0]->elements == 2 &&
  450. !memcmp(reply->element[0]->element[0]->str,"bar",3) &&
  451. !memcmp(reply->element[0]->element[1]->str,"foo",3) &&
  452. reply->element[1]->type == REDIS_REPLY_STATUS &&
  453. strcasecmp(reply->element[1]->str,"pong") == 0);
  454. freeReplyObject(reply);
  455. disconnect(c, 0);
  456. }
  457. static void test_blocking_connection_timeouts(struct config config) {
  458. redisContext *c;
  459. redisReply *reply;
  460. ssize_t s;
  461. const char *cmd = "DEBUG SLEEP 3\r\n";
  462. struct timeval tv;
  463. c = connect(config);
  464. test("Successfully completes a command when the timeout is not exceeded: ");
  465. reply = redisCommand(c,"SET foo fast");
  466. freeReplyObject(reply);
  467. tv.tv_sec = 0;
  468. tv.tv_usec = 10000;
  469. redisSetTimeout(c, tv);
  470. reply = redisCommand(c, "GET foo");
  471. test_cond(reply != NULL && reply->type == REDIS_REPLY_STRING && memcmp(reply->str, "fast", 4) == 0);
  472. freeReplyObject(reply);
  473. disconnect(c, 0);
  474. c = connect(config);
  475. test("Does not return a reply when the command times out: ");
  476. s = write(c->fd, cmd, strlen(cmd));
  477. tv.tv_sec = 0;
  478. tv.tv_usec = 10000;
  479. redisSetTimeout(c, tv);
  480. reply = redisCommand(c, "GET foo");
  481. test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_IO && strcmp(c->errstr, "Resource temporarily unavailable") == 0);
  482. freeReplyObject(reply);
  483. test("Reconnect properly reconnects after a timeout: ");
  484. redisReconnect(c);
  485. reply = redisCommand(c, "PING");
  486. test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
  487. freeReplyObject(reply);
  488. test("Reconnect properly uses owned parameters: ");
  489. config.tcp.host = "foo";
  490. config.unix_sock.path = "foo";
  491. redisReconnect(c);
  492. reply = redisCommand(c, "PING");
  493. test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
  494. freeReplyObject(reply);
  495. disconnect(c, 0);
  496. }
  497. static void test_blocking_io_errors(struct config config) {
  498. redisContext *c;
  499. redisReply *reply;
  500. void *_reply;
  501. int major, minor;
  502. /* Connect to target given by config. */
  503. c = connect(config);
  504. {
  505. /* Find out Redis version to determine the path for the next test */
  506. const char *field = "redis_version:";
  507. char *p, *eptr;
  508. reply = redisCommand(c,"INFO");
  509. p = strstr(reply->str,field);
  510. major = strtol(p+strlen(field),&eptr,10);
  511. p = eptr+1; /* char next to the first "." */
  512. minor = strtol(p,&eptr,10);
  513. freeReplyObject(reply);
  514. }
  515. test("Returns I/O error when the connection is lost: ");
  516. reply = redisCommand(c,"QUIT");
  517. if (major > 2 || (major == 2 && minor > 0)) {
  518. /* > 2.0 returns OK on QUIT and read() should be issued once more
  519. * to know the descriptor is at EOF. */
  520. test_cond(strcasecmp(reply->str,"OK") == 0 &&
  521. redisGetReply(c,&_reply) == REDIS_ERR);
  522. freeReplyObject(reply);
  523. } else {
  524. test_cond(reply == NULL);
  525. }
  526. /* On 2.0, QUIT will cause the connection to be closed immediately and
  527. * the read(2) for the reply on QUIT will set the error to EOF.
  528. * On >2.0, QUIT will return with OK and another read(2) needed to be
  529. * issued to find out the socket was closed by the server. In both
  530. * conditions, the error will be set to EOF. */
  531. assert(c->err == REDIS_ERR_EOF &&
  532. strcmp(c->errstr,"Server closed the connection") == 0);
  533. redisFree(c);
  534. c = connect(config);
  535. test("Returns I/O error on socket timeout: ");
  536. struct timeval tv = { 0, 1000 };
  537. assert(redisSetTimeout(c,tv) == REDIS_OK);
  538. test_cond(redisGetReply(c,&_reply) == REDIS_ERR &&
  539. c->err == REDIS_ERR_IO && errno == EAGAIN);
  540. redisFree(c);
  541. }
  542. static void test_invalid_timeout_errors(struct config config) {
  543. redisContext *c;
  544. test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: ");
  545. config.tcp.timeout.tv_sec = 0;
  546. config.tcp.timeout.tv_usec = 10000001;
  547. c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
  548. test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0);
  549. redisFree(c);
  550. test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: ");
  551. config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1;
  552. config.tcp.timeout.tv_usec = 0;
  553. c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
  554. test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0);
  555. redisFree(c);
  556. }
  557. static void test_throughput(struct config config) {
  558. redisContext *c = connect(config);
  559. redisReply **replies;
  560. int i, num;
  561. long long t1, t2;
  562. test("Throughput:\n");
  563. for (i = 0; i < 500; i++)
  564. freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
  565. num = 1000;
  566. replies = malloc(sizeof(redisReply*)*num);
  567. t1 = usec();
  568. for (i = 0; i < num; i++) {
  569. replies[i] = redisCommand(c,"PING");
  570. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
  571. }
  572. t2 = usec();
  573. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  574. free(replies);
  575. printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0);
  576. replies = malloc(sizeof(redisReply*)*num);
  577. t1 = usec();
  578. for (i = 0; i < num; i++) {
  579. replies[i] = redisCommand(c,"LRANGE mylist 0 499");
  580. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
  581. assert(replies[i] != NULL && replies[i]->elements == 500);
  582. }
  583. t2 = usec();
  584. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  585. free(replies);
  586. printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0);
  587. replies = malloc(sizeof(redisReply*)*num);
  588. t1 = usec();
  589. for (i = 0; i < num; i++) {
  590. replies[i] = redisCommand(c, "INCRBY incrkey %d", 1000000);
  591. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_INTEGER);
  592. }
  593. t2 = usec();
  594. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  595. free(replies);
  596. printf("\t(%dx INCRBY: %.3fs)\n", num, (t2-t1)/1000000.0);
  597. num = 10000;
  598. replies = malloc(sizeof(redisReply*)*num);
  599. for (i = 0; i < num; i++)
  600. redisAppendCommand(c,"PING");
  601. t1 = usec();
  602. for (i = 0; i < num; i++) {
  603. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  604. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
  605. }
  606. t2 = usec();
  607. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  608. free(replies);
  609. printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  610. replies = malloc(sizeof(redisReply*)*num);
  611. for (i = 0; i < num; i++)
  612. redisAppendCommand(c,"LRANGE mylist 0 499");
  613. t1 = usec();
  614. for (i = 0; i < num; i++) {
  615. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  616. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
  617. assert(replies[i] != NULL && replies[i]->elements == 500);
  618. }
  619. t2 = usec();
  620. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  621. free(replies);
  622. printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  623. replies = malloc(sizeof(redisReply*)*num);
  624. for (i = 0; i < num; i++)
  625. redisAppendCommand(c,"INCRBY incrkey %d", 1000000);
  626. t1 = usec();
  627. for (i = 0; i < num; i++) {
  628. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  629. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_INTEGER);
  630. }
  631. t2 = usec();
  632. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  633. free(replies);
  634. printf("\t(%dx INCRBY (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  635. disconnect(c, 0);
  636. }
  637. // static long __test_callback_flags = 0;
  638. // static void __test_callback(redisContext *c, void *privdata) {
  639. // ((void)c);
  640. // /* Shift to detect execution order */
  641. // __test_callback_flags <<= 8;
  642. // __test_callback_flags |= (long)privdata;
  643. // }
  644. //
  645. // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
  646. // ((void)c);
  647. // /* Shift to detect execution order */
  648. // __test_callback_flags <<= 8;
  649. // __test_callback_flags |= (long)privdata;
  650. // if (reply) freeReplyObject(reply);
  651. // }
  652. //
  653. // static redisContext *__connect_nonblock() {
  654. // /* Reset callback flags */
  655. // __test_callback_flags = 0;
  656. // return redisConnectNonBlock("127.0.0.1", port, NULL);
  657. // }
  658. //
  659. // static void test_nonblocking_connection() {
  660. // redisContext *c;
  661. // int wdone = 0;
  662. //
  663. // test("Calls command callback when command is issued: ");
  664. // c = __connect_nonblock();
  665. // redisSetCommandCallback(c,__test_callback,(void*)1);
  666. // redisCommand(c,"PING");
  667. // test_cond(__test_callback_flags == 1);
  668. // redisFree(c);
  669. //
  670. // test("Calls disconnect callback on redisDisconnect: ");
  671. // c = __connect_nonblock();
  672. // redisSetDisconnectCallback(c,__test_callback,(void*)2);
  673. // redisDisconnect(c);
  674. // test_cond(__test_callback_flags == 2);
  675. // redisFree(c);
  676. //
  677. // test("Calls disconnect callback and free callback on redisFree: ");
  678. // c = __connect_nonblock();
  679. // redisSetDisconnectCallback(c,__test_callback,(void*)2);
  680. // redisSetFreeCallback(c,__test_callback,(void*)4);
  681. // redisFree(c);
  682. // test_cond(__test_callback_flags == ((2 << 8) | 4));
  683. //
  684. // test("redisBufferWrite against empty write buffer: ");
  685. // c = __connect_nonblock();
  686. // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
  687. // redisFree(c);
  688. //
  689. // test("redisBufferWrite against not yet connected fd: ");
  690. // c = __connect_nonblock();
  691. // redisCommand(c,"PING");
  692. // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
  693. // strncmp(c->error,"write:",6) == 0);
  694. // redisFree(c);
  695. //
  696. // test("redisBufferWrite against closed fd: ");
  697. // c = __connect_nonblock();
  698. // redisCommand(c,"PING");
  699. // redisDisconnect(c);
  700. // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
  701. // strncmp(c->error,"write:",6) == 0);
  702. // redisFree(c);
  703. //
  704. // test("Process callbacks in the right sequence: ");
  705. // c = __connect_nonblock();
  706. // redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
  707. // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
  708. // redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
  709. //
  710. // /* Write output buffer */
  711. // wdone = 0;
  712. // while(!wdone) {
  713. // usleep(500);
  714. // redisBufferWrite(c,&wdone);
  715. // }
  716. //
  717. // /* Read until at least one callback is executed (the 3 replies will
  718. // * arrive in a single packet, causing all callbacks to be executed in
  719. // * a single pass). */
  720. // while(__test_callback_flags == 0) {
  721. // assert(redisBufferRead(c) == REDIS_OK);
  722. // redisProcessCallbacks(c);
  723. // }
  724. // test_cond(__test_callback_flags == 0x010203);
  725. // redisFree(c);
  726. //
  727. // test("redisDisconnect executes pending callbacks with NULL reply: ");
  728. // c = __connect_nonblock();
  729. // redisSetDisconnectCallback(c,__test_callback,(void*)1);
  730. // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
  731. // redisDisconnect(c);
  732. // test_cond(__test_callback_flags == 0x0201);
  733. // redisFree(c);
  734. // }
  735. int main(int argc, char **argv) {
  736. struct config cfg = {
  737. .tcp = {
  738. .host = "127.0.0.1",
  739. .port = 6379
  740. },
  741. .unix_sock = {
  742. .path = "/tmp/redis.sock"
  743. }
  744. };
  745. int throughput = 1;
  746. int test_inherit_fd = 1;
  747. /* Ignore broken pipe signal (for I/O error tests). */
  748. signal(SIGPIPE, SIG_IGN);
  749. /* Parse command line options. */
  750. argv++; argc--;
  751. while (argc) {
  752. if (argc >= 2 && !strcmp(argv[0],"-h")) {
  753. argv++; argc--;
  754. cfg.tcp.host = argv[0];
  755. } else if (argc >= 2 && !strcmp(argv[0],"-p")) {
  756. argv++; argc--;
  757. cfg.tcp.port = atoi(argv[0]);
  758. } else if (argc >= 2 && !strcmp(argv[0],"-s")) {
  759. argv++; argc--;
  760. cfg.unix_sock.path = argv[0];
  761. } else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) {
  762. throughput = 0;
  763. } else if (argc >= 1 && !strcmp(argv[0],"--skip-inherit-fd")) {
  764. test_inherit_fd = 0;
  765. } else {
  766. fprintf(stderr, "Invalid argument: %s\n", argv[0]);
  767. exit(1);
  768. }
  769. argv++; argc--;
  770. }
  771. test_format_commands();
  772. test_reply_reader();
  773. test_blocking_connection_errors();
  774. test_free_null();
  775. printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port);
  776. cfg.type = CONN_TCP;
  777. test_blocking_connection(cfg);
  778. test_blocking_connection_timeouts(cfg);
  779. test_blocking_io_errors(cfg);
  780. test_invalid_timeout_errors(cfg);
  781. test_append_formatted_commands(cfg);
  782. if (throughput) test_throughput(cfg);
  783. printf("\nTesting against Unix socket connection (%s):\n", cfg.unix_sock.path);
  784. cfg.type = CONN_UNIX;
  785. test_blocking_connection(cfg);
  786. test_blocking_connection_timeouts(cfg);
  787. test_blocking_io_errors(cfg);
  788. if (throughput) test_throughput(cfg);
  789. if (test_inherit_fd) {
  790. printf("\nTesting against inherited fd (%s):\n", cfg.unix_sock.path);
  791. cfg.type = CONN_FD;
  792. test_blocking_connection(cfg);
  793. }
  794. if (fails) {
  795. printf("*** %d TESTS FAILED ***\n", fails);
  796. return 1;
  797. }
  798. printf("ALL TESTS PASSED\n");
  799. return 0;
  800. }