2
0

test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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("Works with NULL functions for reply: ");
  254. reader = redisReaderCreate();
  255. reader->fn = NULL;
  256. redisReaderFeed(reader,(char*)"+OK\r\n",5);
  257. ret = redisReaderGetReply(reader,&reply);
  258. test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
  259. redisReaderFree(reader);
  260. test("Works when a single newline (\\r\\n) covers two calls to feed: ");
  261. reader = redisReaderCreate();
  262. reader->fn = NULL;
  263. redisReaderFeed(reader,(char*)"+OK\r",4);
  264. ret = redisReaderGetReply(reader,&reply);
  265. assert(ret == REDIS_OK && reply == NULL);
  266. redisReaderFeed(reader,(char*)"\n",1);
  267. ret = redisReaderGetReply(reader,&reply);
  268. test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
  269. redisReaderFree(reader);
  270. test("Don't reset state after protocol error: ");
  271. reader = redisReaderCreate();
  272. reader->fn = NULL;
  273. redisReaderFeed(reader,(char*)"x",1);
  274. ret = redisReaderGetReply(reader,&reply);
  275. assert(ret == REDIS_ERR);
  276. ret = redisReaderGetReply(reader,&reply);
  277. test_cond(ret == REDIS_ERR && reply == NULL);
  278. redisReaderFree(reader);
  279. /* Regression test for issue #45 on GitHub. */
  280. test("Don't do empty allocation for empty multi bulk: ");
  281. reader = redisReaderCreate();
  282. redisReaderFeed(reader,(char*)"*0\r\n",4);
  283. ret = redisReaderGetReply(reader,&reply);
  284. test_cond(ret == REDIS_OK &&
  285. ((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&
  286. ((redisReply*)reply)->elements == 0);
  287. freeReplyObject(reply);
  288. redisReaderFree(reader);
  289. }
  290. static void test_free_null(void) {
  291. void *redisCtx = NULL;
  292. void *reply = NULL;
  293. test("Don't fail when redisFree is passed a NULL value: ");
  294. redisFree(redisCtx);
  295. test_cond(redisCtx == NULL);
  296. test("Don't fail when freeReplyObject is passed a NULL value: ");
  297. freeReplyObject(reply);
  298. test_cond(reply == NULL);
  299. }
  300. static void test_blocking_connection_errors(void) {
  301. redisContext *c;
  302. test("Returns error when host cannot be resolved: ");
  303. c = redisConnect((char*)"idontexist.test", 6379);
  304. test_cond(c->err == REDIS_ERR_OTHER &&
  305. (strcmp(c->errstr,"Name or service not known") == 0 ||
  306. strcmp(c->errstr,"Can't resolve: idontexist.test") == 0 ||
  307. strcmp(c->errstr,"nodename nor servname provided, or not known") == 0 ||
  308. strcmp(c->errstr,"No address associated with hostname") == 0 ||
  309. strcmp(c->errstr,"Temporary failure in name resolution") == 0 ||
  310. strcmp(c->errstr,"hostname nor servname provided, or not known") == 0 ||
  311. strcmp(c->errstr,"no address associated with name") == 0));
  312. redisFree(c);
  313. test("Returns error when the port is not open: ");
  314. c = redisConnect((char*)"localhost", 1);
  315. test_cond(c->err == REDIS_ERR_IO &&
  316. strcmp(c->errstr,"Connection refused") == 0);
  317. redisFree(c);
  318. test("Returns error when the unix_sock socket path doesn't accept connections: ");
  319. c = redisConnectUnix((char*)"/tmp/idontexist.sock");
  320. test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */
  321. redisFree(c);
  322. }
  323. static void test_blocking_connection(struct config config) {
  324. redisContext *c;
  325. redisReply *reply;
  326. c = connect(config);
  327. test("Is able to deliver commands: ");
  328. reply = redisCommand(c,"PING");
  329. test_cond(reply->type == REDIS_REPLY_STATUS &&
  330. strcasecmp(reply->str,"pong") == 0)
  331. freeReplyObject(reply);
  332. test("Is a able to send commands verbatim: ");
  333. reply = redisCommand(c,"SET foo bar");
  334. test_cond (reply->type == REDIS_REPLY_STATUS &&
  335. strcasecmp(reply->str,"ok") == 0)
  336. freeReplyObject(reply);
  337. test("%%s String interpolation works: ");
  338. reply = redisCommand(c,"SET %s %s","foo","hello world");
  339. freeReplyObject(reply);
  340. reply = redisCommand(c,"GET foo");
  341. test_cond(reply->type == REDIS_REPLY_STRING &&
  342. strcmp(reply->str,"hello world") == 0);
  343. freeReplyObject(reply);
  344. test("%%b String interpolation works: ");
  345. reply = redisCommand(c,"SET %b %b","foo",(size_t)3,"hello\x00world",(size_t)11);
  346. freeReplyObject(reply);
  347. reply = redisCommand(c,"GET foo");
  348. test_cond(reply->type == REDIS_REPLY_STRING &&
  349. memcmp(reply->str,"hello\x00world",11) == 0)
  350. test("Binary reply length is correct: ");
  351. test_cond(reply->len == 11)
  352. freeReplyObject(reply);
  353. test("Can parse nil replies: ");
  354. reply = redisCommand(c,"GET nokey");
  355. test_cond(reply->type == REDIS_REPLY_NIL)
  356. freeReplyObject(reply);
  357. /* test 7 */
  358. test("Can parse integer replies: ");
  359. reply = redisCommand(c,"INCR mycounter");
  360. test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
  361. freeReplyObject(reply);
  362. test("Can parse multi bulk replies: ");
  363. freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
  364. freeReplyObject(redisCommand(c,"LPUSH mylist bar"));
  365. reply = redisCommand(c,"LRANGE mylist 0 -1");
  366. test_cond(reply->type == REDIS_REPLY_ARRAY &&
  367. reply->elements == 2 &&
  368. !memcmp(reply->element[0]->str,"bar",3) &&
  369. !memcmp(reply->element[1]->str,"foo",3))
  370. freeReplyObject(reply);
  371. /* m/e with multi bulk reply *before* other reply.
  372. * specifically test ordering of reply items to parse. */
  373. test("Can handle nested multi bulk replies: ");
  374. freeReplyObject(redisCommand(c,"MULTI"));
  375. freeReplyObject(redisCommand(c,"LRANGE mylist 0 -1"));
  376. freeReplyObject(redisCommand(c,"PING"));
  377. reply = (redisCommand(c,"EXEC"));
  378. test_cond(reply->type == REDIS_REPLY_ARRAY &&
  379. reply->elements == 2 &&
  380. reply->element[0]->type == REDIS_REPLY_ARRAY &&
  381. reply->element[0]->elements == 2 &&
  382. !memcmp(reply->element[0]->element[0]->str,"bar",3) &&
  383. !memcmp(reply->element[0]->element[1]->str,"foo",3) &&
  384. reply->element[1]->type == REDIS_REPLY_STATUS &&
  385. strcasecmp(reply->element[1]->str,"pong") == 0);
  386. freeReplyObject(reply);
  387. disconnect(c, 0);
  388. }
  389. static void test_blocking_connection_timeouts(struct config config) {
  390. redisContext *c;
  391. redisReply *reply;
  392. ssize_t s;
  393. const char *cmd = "DEBUG SLEEP 3\r\n";
  394. struct timeval tv;
  395. c = connect(config);
  396. test("Successfully completes a command when the timeout is not exceeded: ");
  397. reply = redisCommand(c,"SET foo fast");
  398. freeReplyObject(reply);
  399. tv.tv_sec = 0;
  400. tv.tv_usec = 10000;
  401. redisSetTimeout(c, tv);
  402. reply = redisCommand(c, "GET foo");
  403. test_cond(reply != NULL && reply->type == REDIS_REPLY_STRING && memcmp(reply->str, "fast", 4) == 0);
  404. freeReplyObject(reply);
  405. disconnect(c, 0);
  406. c = connect(config);
  407. test("Does not return a reply when the command times out: ");
  408. s = write(c->fd, cmd, strlen(cmd));
  409. tv.tv_sec = 0;
  410. tv.tv_usec = 10000;
  411. redisSetTimeout(c, tv);
  412. reply = redisCommand(c, "GET foo");
  413. test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_IO && strcmp(c->errstr, "Resource temporarily unavailable") == 0);
  414. freeReplyObject(reply);
  415. test("Reconnect properly reconnects after a timeout: ");
  416. redisReconnect(c);
  417. reply = redisCommand(c, "PING");
  418. test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
  419. freeReplyObject(reply);
  420. test("Reconnect properly uses owned parameters: ");
  421. config.tcp.host = "foo";
  422. config.unix_sock.path = "foo";
  423. redisReconnect(c);
  424. reply = redisCommand(c, "PING");
  425. test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
  426. freeReplyObject(reply);
  427. disconnect(c, 0);
  428. }
  429. static void test_blocking_io_errors(struct config config) {
  430. redisContext *c;
  431. redisReply *reply;
  432. void *_reply;
  433. int major, minor;
  434. /* Connect to target given by config. */
  435. c = connect(config);
  436. {
  437. /* Find out Redis version to determine the path for the next test */
  438. const char *field = "redis_version:";
  439. char *p, *eptr;
  440. reply = redisCommand(c,"INFO");
  441. p = strstr(reply->str,field);
  442. major = strtol(p+strlen(field),&eptr,10);
  443. p = eptr+1; /* char next to the first "." */
  444. minor = strtol(p,&eptr,10);
  445. freeReplyObject(reply);
  446. }
  447. test("Returns I/O error when the connection is lost: ");
  448. reply = redisCommand(c,"QUIT");
  449. if (major > 2 || (major == 2 && minor > 0)) {
  450. /* > 2.0 returns OK on QUIT and read() should be issued once more
  451. * to know the descriptor is at EOF. */
  452. test_cond(strcasecmp(reply->str,"OK") == 0 &&
  453. redisGetReply(c,&_reply) == REDIS_ERR);
  454. freeReplyObject(reply);
  455. } else {
  456. test_cond(reply == NULL);
  457. }
  458. /* On 2.0, QUIT will cause the connection to be closed immediately and
  459. * the read(2) for the reply on QUIT will set the error to EOF.
  460. * On >2.0, QUIT will return with OK and another read(2) needed to be
  461. * issued to find out the socket was closed by the server. In both
  462. * conditions, the error will be set to EOF. */
  463. assert(c->err == REDIS_ERR_EOF &&
  464. strcmp(c->errstr,"Server closed the connection") == 0);
  465. redisFree(c);
  466. c = connect(config);
  467. test("Returns I/O error on socket timeout: ");
  468. struct timeval tv = { 0, 1000 };
  469. assert(redisSetTimeout(c,tv) == REDIS_OK);
  470. test_cond(redisGetReply(c,&_reply) == REDIS_ERR &&
  471. c->err == REDIS_ERR_IO && errno == EAGAIN);
  472. redisFree(c);
  473. }
  474. static void test_invalid_timeout_errors(struct config config) {
  475. redisContext *c;
  476. test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: ");
  477. config.tcp.timeout.tv_sec = 0;
  478. config.tcp.timeout.tv_usec = 10000001;
  479. c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
  480. test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0);
  481. redisFree(c);
  482. test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: ");
  483. config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1;
  484. config.tcp.timeout.tv_usec = 0;
  485. c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);
  486. test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0);
  487. redisFree(c);
  488. }
  489. static void test_throughput(struct config config) {
  490. redisContext *c = connect(config);
  491. redisReply **replies;
  492. int i, num;
  493. long long t1, t2;
  494. test("Throughput:\n");
  495. for (i = 0; i < 500; i++)
  496. freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
  497. num = 1000;
  498. replies = malloc(sizeof(redisReply*)*num);
  499. t1 = usec();
  500. for (i = 0; i < num; i++) {
  501. replies[i] = redisCommand(c,"PING");
  502. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
  503. }
  504. t2 = usec();
  505. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  506. free(replies);
  507. printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0);
  508. replies = malloc(sizeof(redisReply*)*num);
  509. t1 = usec();
  510. for (i = 0; i < num; i++) {
  511. replies[i] = redisCommand(c,"LRANGE mylist 0 499");
  512. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
  513. assert(replies[i] != NULL && replies[i]->elements == 500);
  514. }
  515. t2 = usec();
  516. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  517. free(replies);
  518. printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0);
  519. num = 10000;
  520. replies = malloc(sizeof(redisReply*)*num);
  521. for (i = 0; i < num; i++)
  522. redisAppendCommand(c,"PING");
  523. t1 = usec();
  524. for (i = 0; i < num; i++) {
  525. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  526. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
  527. }
  528. t2 = usec();
  529. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  530. free(replies);
  531. printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  532. replies = malloc(sizeof(redisReply*)*num);
  533. for (i = 0; i < num; i++)
  534. redisAppendCommand(c,"LRANGE mylist 0 499");
  535. t1 = usec();
  536. for (i = 0; i < num; i++) {
  537. assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
  538. assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
  539. assert(replies[i] != NULL && replies[i]->elements == 500);
  540. }
  541. t2 = usec();
  542. for (i = 0; i < num; i++) freeReplyObject(replies[i]);
  543. free(replies);
  544. printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
  545. disconnect(c, 0);
  546. }
  547. // static long __test_callback_flags = 0;
  548. // static void __test_callback(redisContext *c, void *privdata) {
  549. // ((void)c);
  550. // /* Shift to detect execution order */
  551. // __test_callback_flags <<= 8;
  552. // __test_callback_flags |= (long)privdata;
  553. // }
  554. //
  555. // static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {
  556. // ((void)c);
  557. // /* Shift to detect execution order */
  558. // __test_callback_flags <<= 8;
  559. // __test_callback_flags |= (long)privdata;
  560. // if (reply) freeReplyObject(reply);
  561. // }
  562. //
  563. // static redisContext *__connect_nonblock() {
  564. // /* Reset callback flags */
  565. // __test_callback_flags = 0;
  566. // return redisConnectNonBlock("127.0.0.1", port, NULL);
  567. // }
  568. //
  569. // static void test_nonblocking_connection() {
  570. // redisContext *c;
  571. // int wdone = 0;
  572. //
  573. // test("Calls command callback when command is issued: ");
  574. // c = __connect_nonblock();
  575. // redisSetCommandCallback(c,__test_callback,(void*)1);
  576. // redisCommand(c,"PING");
  577. // test_cond(__test_callback_flags == 1);
  578. // redisFree(c);
  579. //
  580. // test("Calls disconnect callback on redisDisconnect: ");
  581. // c = __connect_nonblock();
  582. // redisSetDisconnectCallback(c,__test_callback,(void*)2);
  583. // redisDisconnect(c);
  584. // test_cond(__test_callback_flags == 2);
  585. // redisFree(c);
  586. //
  587. // test("Calls disconnect callback and free callback on redisFree: ");
  588. // c = __connect_nonblock();
  589. // redisSetDisconnectCallback(c,__test_callback,(void*)2);
  590. // redisSetFreeCallback(c,__test_callback,(void*)4);
  591. // redisFree(c);
  592. // test_cond(__test_callback_flags == ((2 << 8) | 4));
  593. //
  594. // test("redisBufferWrite against empty write buffer: ");
  595. // c = __connect_nonblock();
  596. // test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
  597. // redisFree(c);
  598. //
  599. // test("redisBufferWrite against not yet connected fd: ");
  600. // c = __connect_nonblock();
  601. // redisCommand(c,"PING");
  602. // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
  603. // strncmp(c->error,"write:",6) == 0);
  604. // redisFree(c);
  605. //
  606. // test("redisBufferWrite against closed fd: ");
  607. // c = __connect_nonblock();
  608. // redisCommand(c,"PING");
  609. // redisDisconnect(c);
  610. // test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
  611. // strncmp(c->error,"write:",6) == 0);
  612. // redisFree(c);
  613. //
  614. // test("Process callbacks in the right sequence: ");
  615. // c = __connect_nonblock();
  616. // redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
  617. // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
  618. // redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
  619. //
  620. // /* Write output buffer */
  621. // wdone = 0;
  622. // while(!wdone) {
  623. // usleep(500);
  624. // redisBufferWrite(c,&wdone);
  625. // }
  626. //
  627. // /* Read until at least one callback is executed (the 3 replies will
  628. // * arrive in a single packet, causing all callbacks to be executed in
  629. // * a single pass). */
  630. // while(__test_callback_flags == 0) {
  631. // assert(redisBufferRead(c) == REDIS_OK);
  632. // redisProcessCallbacks(c);
  633. // }
  634. // test_cond(__test_callback_flags == 0x010203);
  635. // redisFree(c);
  636. //
  637. // test("redisDisconnect executes pending callbacks with NULL reply: ");
  638. // c = __connect_nonblock();
  639. // redisSetDisconnectCallback(c,__test_callback,(void*)1);
  640. // redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
  641. // redisDisconnect(c);
  642. // test_cond(__test_callback_flags == 0x0201);
  643. // redisFree(c);
  644. // }
  645. int main(int argc, char **argv) {
  646. struct config cfg = {
  647. .tcp = {
  648. .host = "127.0.0.1",
  649. .port = 6379
  650. },
  651. .unix_sock = {
  652. .path = "/tmp/redis.sock"
  653. }
  654. };
  655. int throughput = 1;
  656. int test_inherit_fd = 1;
  657. /* Ignore broken pipe signal (for I/O error tests). */
  658. signal(SIGPIPE, SIG_IGN);
  659. /* Parse command line options. */
  660. argv++; argc--;
  661. while (argc) {
  662. if (argc >= 2 && !strcmp(argv[0],"-h")) {
  663. argv++; argc--;
  664. cfg.tcp.host = argv[0];
  665. } else if (argc >= 2 && !strcmp(argv[0],"-p")) {
  666. argv++; argc--;
  667. cfg.tcp.port = atoi(argv[0]);
  668. } else if (argc >= 2 && !strcmp(argv[0],"-s")) {
  669. argv++; argc--;
  670. cfg.unix_sock.path = argv[0];
  671. } else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) {
  672. throughput = 0;
  673. } else if (argc >= 1 && !strcmp(argv[0],"--skip-inherit-fd")) {
  674. test_inherit_fd = 0;
  675. } else {
  676. fprintf(stderr, "Invalid argument: %s\n", argv[0]);
  677. exit(1);
  678. }
  679. argv++; argc--;
  680. }
  681. test_format_commands();
  682. test_reply_reader();
  683. test_blocking_connection_errors();
  684. test_free_null();
  685. printf("\nTesting against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port);
  686. cfg.type = CONN_TCP;
  687. test_blocking_connection(cfg);
  688. test_blocking_connection_timeouts(cfg);
  689. test_blocking_io_errors(cfg);
  690. test_invalid_timeout_errors(cfg);
  691. test_append_formatted_commands(cfg);
  692. if (throughput) test_throughput(cfg);
  693. printf("\nTesting against Unix socket connection (%s):\n", cfg.unix_sock.path);
  694. cfg.type = CONN_UNIX;
  695. test_blocking_connection(cfg);
  696. test_blocking_connection_timeouts(cfg);
  697. test_blocking_io_errors(cfg);
  698. if (throughput) test_throughput(cfg);
  699. if (test_inherit_fd) {
  700. printf("\nTesting against inherited fd (%s):\n", cfg.unix_sock.path);
  701. cfg.type = CONN_FD;
  702. test_blocking_connection(cfg);
  703. }
  704. if (fails) {
  705. printf("*** %d TESTS FAILED ***\n", fails);
  706. return 1;
  707. }
  708. printf("ALL TESTS PASSED\n");
  709. return 0;
  710. }