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