forked from RedisLabs/eredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheredis_test.c
117 lines (90 loc) · 2.93 KB
/
eredis_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* A simple test for the embedded redis library.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include "redis/src/eredis.h"
#include "acutest/include/acutest.h"
/* acutest may or may not fork per test, we handle both cases */
int redis_initialized = 0;
#define BEGIN_TEST() \
if (!redis_initialized) { \
int ret = eredis_init(); \
TEST_CHECK(ret == 0); \
redis_initialized = 1; \
} \
eredis_client_t *c = eredis_create_client(); \
TEST_CHECK(c != NULL)
void test_set_command(void)
{
BEGIN_TEST();
int len;
char *cmd[] = { "SET", "mykey", "myvalue" };
eredis_prepare_request(c, 3, (const char **) &cmd, NULL);
TEST_CHECK(eredis_execute(c) == 0);
const char *reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 5);
TEST_CHECK(!memcmp(reply, "+OK\r\n", 5));
}
void test_binary_args(void)
{
BEGIN_TEST();
int len;
size_t cmd_lens[] = { 3, 5, 3 };
char *cmd[] = { "SET", "mykey", "\0\0\0" };
eredis_prepare_request(c, 3, (const char **) &cmd, cmd_lens);
TEST_CHECK(eredis_execute(c) == 0);
const char *reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 5);
TEST_CHECK(!memcmp(reply, "+OK\r\n", 5));
char *get_cmd[] = { "GET", "mykey" };
eredis_prepare_request(c, 2, (const char **) &get_cmd, NULL);
TEST_CHECK(eredis_execute(c) == 0);
reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 9);
TEST_CHECK(!memcmp(reply, "$3\r\n\0\0\0\r\n", 9));
}
void test_long_reply(void)
{
BEGIN_TEST();
int len;
char buf[65536];
char *set_cmd[] = { "SET", "mykey", buf };
memset(buf, 'x', sizeof(buf));
buf[sizeof(buf)-1] = '\0';
eredis_prepare_request(c, 3, (const char **) &set_cmd, NULL);
TEST_CHECK(eredis_execute(c) == 0);
const char *reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 5);
TEST_CHECK(!memcmp(reply, "+OK\r\n", 5));
TEST_CHECK(eredis_read_reply_chunk(c, &len) == NULL);
char *get_cmd[] = { "GET", "mykey" };
eredis_prepare_request(c, 2, (const char **) &get_cmd, NULL);
TEST_CHECK(eredis_execute(c) == 0);
reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 8);
TEST_CHECK(!memcmp(reply, "$65535\r\n", 8));
reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 65535);
TEST_CHECK(!memcmp(reply, buf, 65535));
}
void test_lua_reply(void)
{
BEGIN_TEST();
char *cmd[] = { "EVAL", "return {}", "0" };
eredis_prepare_request(c, 3, (const char **) &cmd, NULL);
TEST_CHECK(eredis_execute(c) == 0);
int len;
const char *reply = eredis_read_reply_chunk(c, &len);
TEST_CHECK(len == 4);
TEST_CHECK(!memcmp(reply, "*0\r\n", 4));
}
TEST_LIST = {
{ "test-set-command", test_set_command },
{ "test-binary-args", test_binary_args },
{ "test-long-reply", test_long_reply },
{ "test-lua-reply", test_lua_reply },
{ NULL, NULL }
};