hiredis 0.6.1 → 0.6.2

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