/
testapp.c
207 lines (178 loc) · 5.47 KB
/
testapp.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#undef NDEBUG
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include <stdbool.h>
#include <unistd.h>
#include "config.h"
#include "cache.h"
#include "util.h"
enum test_return { TEST_SKIP, TEST_PASS, TEST_FAIL };
static enum test_return cache_create_test(void)
{
cache_t *cache = cache_create("test", sizeof(uint32_t), sizeof(char*),
NULL, NULL);
assert(cache != NULL);
cache_destroy(cache);
return TEST_PASS;
}
const uint64_t constructor_pattern = 0xdeadcafebabebeef;
static int cache_constructor(void *buffer, void *notused1, int notused2) {
uint64_t *ptr = buffer;
*ptr = constructor_pattern;
return 0;
}
static enum test_return cache_constructor_test(void)
{
cache_t *cache = cache_create("test", sizeof(uint64_t), sizeof(uint64_t),
cache_constructor, NULL);
assert(cache != NULL);
uint64_t *ptr = cache_alloc(cache);
uint64_t pattern = *ptr;
cache_free(cache, ptr);
cache_destroy(cache);
return (pattern == constructor_pattern) ? TEST_PASS : TEST_FAIL;
}
static int cache_fail_constructor(void *buffer, void *notused1, int notused2) {
return 1;
}
static enum test_return cache_fail_constructor_test(void)
{
enum test_return ret = TEST_PASS;
cache_t *cache = cache_create("test", sizeof(uint64_t), sizeof(uint64_t),
cache_fail_constructor, NULL);
assert(cache != NULL);
uint64_t *ptr = cache_alloc(cache);
if (ptr != NULL) {
ret = TEST_FAIL;
}
cache_destroy(cache);
return ret;
}
static void *destruct_data = 0;
static void cache_destructor(void *buffer, void *notused) {
destruct_data = buffer;
}
static enum test_return cache_destructor_test(void)
{
cache_t *cache = cache_create("test", sizeof(uint32_t), sizeof(char*),
NULL, cache_destructor);
assert(cache != NULL);
char *ptr = cache_alloc(cache);
cache_free(cache, ptr);
cache_destroy(cache);
return (ptr == destruct_data) ? TEST_PASS : TEST_FAIL;
}
static enum test_return cache_reuse_test(void)
{
int ii;
cache_t *cache = cache_create("test", sizeof(uint32_t), sizeof(char*),
NULL, NULL);
char *ptr = cache_alloc(cache);
cache_free(cache, ptr);
for (ii = 0; ii < 100; ++ii) {
char *p = cache_alloc(cache);
assert(p == ptr);
cache_free(cache, ptr);
}
cache_destroy(cache);
return TEST_PASS;
}
static enum test_return cache_redzone_test(void)
{
#ifndef HAVE_UMEM_H
cache_t *cache = cache_create("test", sizeof(uint32_t), sizeof(char*),
NULL, NULL);
/* Ignore SIGABORT */
struct sigaction old_action;
struct sigaction action = { .sa_handler = SIG_IGN, .sa_flags = 0};
sigemptyset(&action.sa_mask);
sigaction(SIGABRT, &action, &old_action);
/* check memory debug.. */
char *p = cache_alloc(cache);
char old = *(p - 1);
*(p - 1) = 0;
cache_free(cache, p);
assert(cache_error == -1);
*(p - 1) = old;
p[sizeof(uint32_t)] = 0;
cache_free(cache, p);
assert(cache_error == 1);
/* restore signal handler */
sigaction(SIGABRT, &old_action, NULL);
cache_destroy(cache);
return TEST_PASS;
#else
return TEST_SKIP;
#endif
}
static enum test_return test_issue_44(void) {
return TEST_SKIP; // TODO: moxi does not pass this test.
char pidfile[80];
char buffer[256];
sprintf(pidfile, "/tmp/memcached.%d", getpid());
sprintf(buffer, "./memcached-debug -p 0 -P %s -d", pidfile);
assert(system(buffer) == 0);
sleep(1);
FILE *fp = fopen(pidfile, "r");
assert(fp);
assert(fgets(buffer, sizeof(buffer), fp));
fclose(fp);
pid_t pid = atol(buffer);
assert(kill(pid, 0) == 0);
assert(kill(pid, SIGHUP) == 0);
sleep(1);
assert(kill(pid, 0) == 0);
assert(kill(pid, SIGTERM) == 0);
assert(remove(pidfile) == 0);
return TEST_PASS;
}
typedef enum test_return (*TEST_FUNC)(void);
struct testcase {
const char *description;
TEST_FUNC function;
};
struct testcase testcases[] = {
{ "cache_create", cache_create_test },
{ "cache_constructor", cache_constructor_test },
{ "cache_constructor_fail", cache_fail_constructor_test },
{ "cache_destructor", cache_destructor_test },
{ "cache_reuse", cache_reuse_test },
{ "cache_redzone", cache_redzone_test },
{ "issue_44", test_issue_44 },
{ NULL, NULL }
};
int main(int argc, char **argv)
{
int exitcode = 0;
int ii;
const int paddingsz = 60;
char padding[paddingsz + 1];
memset(padding, ' ', paddingsz);
for (ii = 0; testcases[ii].description != NULL; ++ii) {
int len = strlen(testcases[ii].description);
if (len > paddingsz) {
len = paddingsz;
}
padding[paddingsz - len] = '\0';
fprintf(stdout, "%s:%s", testcases[ii].description, padding);
padding[paddingsz - len] = ' ';
fflush(stdout);
enum test_return ret = testcases[ii].function();
if (ret == TEST_SKIP) {
fprintf(stdout, "[skipped]\n");
} else if (ret == TEST_PASS) {
fprintf(stdout, "[ok]\n");
} else {
fprintf(stdout, "[failed]\n");
exitcode = 1;
}
fflush(stdout);
}
return exitcode;
}