-
Notifications
You must be signed in to change notification settings - Fork 33
/
testapp.c
516 lines (446 loc) · 14.5 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#undef NDEBUG
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#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 <netinet/in.h>
#include "protocol_binary.h"
#include "config.h"
#include "cache.h"
#include "util.h"
#define TMP_TEMPLATE "/tmp/test_file.XXXXXXX"
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) {
(void)buffer;
(void)notused1;
(void)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) {
(void)buffer;
(void)notused1;
(void)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) {
(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_safe_strtoul(void) {
uint32_t val;
assert(safe_strtoul("123", &val));
assert(val == 123);
assert(safe_strtoul("+123", &val));
assert(val == 123);
assert(!safe_strtoul("", &val)); // empty
assert(!safe_strtoul("123BOGUS", &val)); // non-numeric
/* Not sure what it does, but this works with ICC :/
assert(!safe_strtoul("92837498237498237498029383", &val)); // out of range
*/
// extremes:
assert(safe_strtoul("4294967295", &val)); // 2**32 - 1
assert(val == 4294967295L);
/* This actually works on 64-bit ubuntu
assert(!safe_strtoul("4294967296", &val)); // 2**32
*/
assert(!safe_strtoul("-1", &val)); // negative
return TEST_PASS;
}
static enum test_return test_safe_strtoull(void) {
uint64_t val;
assert(safe_strtoull("123", &val));
assert(val == 123);
assert(safe_strtoull("+123", &val));
assert(val == 123);
assert(!safe_strtoull("", &val)); // empty
assert(!safe_strtoull("123BOGUS", &val)); // non-numeric
assert(!safe_strtoull("92837498237498237498029383", &val)); // out of range
// extremes:
assert(safe_strtoull("18446744073709551615", &val)); // 2**64 - 1
assert(val == 18446744073709551615ULL);
assert(!safe_strtoull("18446744073709551616", &val)); // 2**64
assert(!safe_strtoull("-1", &val)); // negative
return TEST_PASS;
}
static enum test_return test_safe_strtoll(void) {
int64_t val;
assert(safe_strtoll("123", &val));
assert(val == 123);
assert(safe_strtoll("+123", &val));
assert(val == 123);
assert(safe_strtoll("-123", &val));
assert(val == -123);
assert(!safe_strtoll("", &val)); // empty
assert(!safe_strtoll("123BOGUS", &val)); // non-numeric
assert(!safe_strtoll("92837498237498237498029383", &val)); // out of range
// extremes:
assert(!safe_strtoll("18446744073709551615", &val)); // 2**64 - 1
assert(safe_strtoll("9223372036854775807", &val)); // 2**63 - 1
assert(val == 9223372036854775807LL);
/*
assert(safe_strtoll("-9223372036854775808", &val)); // -2**63
assert(val == -9223372036854775808LL);
*/
assert(!safe_strtoll("-9223372036854775809", &val)); // -2**63 - 1
// We'll allow space to terminate the string. And leading space.
assert(safe_strtoll(" 123 foo", &val));
assert(val == 123);
return TEST_PASS;
}
static enum test_return test_safe_strtol(void) {
int32_t val;
assert(safe_strtol("123", &val));
assert(val == 123);
assert(safe_strtol("+123", &val));
assert(val == 123);
assert(safe_strtol("-123", &val));
assert(val == -123);
assert(!safe_strtol("", &val)); // empty
assert(!safe_strtol("123BOGUS", &val)); // non-numeric
assert(!safe_strtol("92837498237498237498029383", &val)); // out of range
// extremes:
/* This actually works on 64-bit ubuntu
assert(!safe_strtol("2147483648", &val)); // (expt 2.0 31.0)
*/
assert(safe_strtol("2147483647", &val)); // (- (expt 2.0 31) 1)
assert(val == 2147483647L);
/* This actually works on 64-bit ubuntu
assert(!safe_strtol("-2147483649", &val)); // (- (expt -2.0 31) 1)
*/
// We'll allow space to terminate the string. And leading space.
assert(safe_strtol(" 123 foo", &val));
assert(val == 123);
return TEST_PASS;
}
/**
* Function to start the server and let it listen on a random port
*
* @param port_out where to store the TCP port number the server is
* listening on
* @param is_daemon set to true if you want to run the memcached server
* as a daemon process
* @return the pid of the memcached server
*/
static pid_t start_server(in_port_t *port_out, bool is_daemon) {
char environment[80];
snprintf(environment, sizeof(environment),
"MEMCACHED_PORT_FILENAME=/tmp/ports.%lu", (long)getpid());
char *filename= environment + strlen("MEMCACHED_PORT_FILENAME=");
char pid_file[80];
snprintf(pid_file, sizeof(pid_file), "/tmp/pid.%lu", (long)getpid());
remove(filename);
remove(pid_file);
pid_t pid = fork();
assert(pid != -1);
if (pid == 0) {
/* Child */
char *argv[20];
int arg = 0;
putenv(environment);
#ifdef __sun
putenv("LD_PRELOAD=watchmalloc.so.1");
putenv("MALLOC_DEBUG=WATCH");
#endif
if (!is_daemon) {
argv[arg++] = "./timedrun";
argv[arg++] = "15";
}
argv[arg++] = "./memcached-debug";
argv[arg++] = "-p";
argv[arg++] = "-1";
argv[arg++] = "-U";
argv[arg++] = "0";
/* Handle rpmbuild and the like doing this as root */
if (getuid() == 0) {
argv[arg++] = "-u";
argv[arg++] = "root";
}
if (is_daemon) {
argv[arg++] = "-d";
argv[arg++] = "-P";
argv[arg++] = pid_file;
}
argv[arg++] = NULL;
assert(execv(argv[0], argv) != -1);
}
/* Yeah just let us "busy-wait" for the file to be created ;-) */
while (access(filename, F_OK) == -1) {
usleep(10);
}
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Failed to open the file containing port numbers: %s\n",
strerror(errno));
assert(false);
}
*port_out = (in_port_t)-1;
char buffer[80];
while ((fgets(buffer, sizeof(buffer), fp)) != NULL) {
if (strncmp(buffer, "TCP INET: ", 10) == 0) {
int32_t val;
assert(safe_strtol(buffer + 10, &val));
*port_out = (in_port_t)val;
}
}
fclose(fp);
assert(remove(filename) == 0);
if (is_daemon) {
/* loop and wait for the pid file.. There is a potential race
* condition that the server just created the file but isn't
* finished writing the content, but I'll take the chance....
*/
while (access(pid_file, F_OK) == -1) {
usleep(10);
}
fp = fopen(pid_file, "r");
if (fp == NULL) {
fprintf(stderr, "Failed to open pid file: %s\n",
strerror(errno));
assert(false);
}
assert(fgets(buffer, sizeof(buffer), fp) != NULL);
fclose(fp);
int32_t val;
assert(safe_strtol(buffer, &val));
pid = (pid_t)val;
}
return pid;
}
static enum test_return test_issue_44(void) {
in_port_t port;
pid_t pid = start_server(&port, true);
assert(kill(pid, SIGHUP) == 0);
sleep(1);
assert(kill(pid, SIGTERM) == 0);
return TEST_PASS;
}
/*
* static struct addrinfo *lookuphost(const char *hostname, in_port_t port)
* {
* struct addrinfo *ai = 0;
* struct addrinfo hints = { .ai_family = AF_UNSPEC,
* .ai_protocol = IPPROTO_TCP,
* .ai_socktype = SOCK_STREAM };
* char service[NI_MAXSERV];
* int error;
*
* (void)snprintf(service, NI_MAXSERV, "%d", port);
* if ((error = getaddrinfo(hostname, service, &hints, &ai)) != 0) {
* if (error != EAI_SYSTEM) {
* fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
* } else {
* perror("getaddrinfo()");
* }
* }
*
* return ai;
* }
*/
/*
* static int connect_server(const char *hostname, in_port_t port)
* {
* struct addrinfo *ai = lookuphost(hostname, port);
* int sock = -1;
* if (ai != NULL) {
* if ((sock = socket(ai->ai_family, ai->ai_socktype,
* ai->ai_protocol)) != -1) {
* if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
* fprintf(stderr, "Failed to connect socket: %s\n",
* strerror(errno));
* close(sock);
* sock = -1;
* }
* } else {
* fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
* }
*
* freeaddrinfo(ai);
* }
* return sock;
* }
*/
static enum test_return test_vperror(void) {
int rv = 0;
int oldstderr = dup(STDERR_FILENO);
char tmpl[sizeof(TMP_TEMPLATE)+1];
strncpy(tmpl, TMP_TEMPLATE, sizeof(TMP_TEMPLATE)+1);
int newfile = mkstemp(tmpl);
assert(newfile > 0);
rv = dup2(newfile, STDERR_FILENO);
assert(rv == STDERR_FILENO);
rv = close(newfile);
assert(rv == 0);
errno = EIO;
vperror("Old McDonald had a farm. %s", "EI EIO");
/* Restore stderr */
rv = dup2(oldstderr, STDERR_FILENO);
assert(rv == STDERR_FILENO);
/* Go read the file */
char buf[80] = { 0 };
FILE *efile = fopen(tmpl, "r");
assert(efile);
char *prv = fgets(buf, sizeof(buf), efile);
assert(prv);
fclose(efile);
unlink(tmpl);
char expected[80] = { 0 };
snprintf(expected, sizeof(expected),
"Old McDonald had a farm. EI EIO: %s\n", strerror(EIO));
/*
fprintf(stderr,
"\nExpected: ``%s''"
"\nGot: ``%s''\n", expected, buf);
*/
return strcmp(expected, buf) == 0 ? TEST_PASS : TEST_FAIL;
}
static enum test_return test_issue_72(void) {
// SKIP: moxi doesn't handle MEMCACHED_PORT_FILENAME now.
return TEST_SKIP;
}
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 },
{ "issue_72", test_issue_72 },
{ "vperror", test_vperror },
{ "safestrtoul", test_safe_strtoul },
{ "safestrtoull", test_safe_strtoull },
{ "safestrtoll", test_safe_strtoll },
{ "safestrtol", test_safe_strtol },
{ NULL, NULL }
};
int main(void)
{
int exitcode = 0;
int ii = 0, num_cases = 0;
for (num_cases = 0; testcases[num_cases].description; num_cases++) {
/* Just counting */
}
printf("1..%d\n", num_cases);
for (ii = 0; testcases[ii].description != NULL; ++ii) {
fflush(stdout);
alarm(60);
enum test_return ret = testcases[ii].function();
if (ret == TEST_SKIP) {
fprintf(stdout, "ok # SKIP %d - %s\n", ii + 1, testcases[ii].description);
} else if (ret == TEST_PASS) {
fprintf(stdout, "ok %d - %s\n", ii + 1, testcases[ii].description);
} else {
fprintf(stdout, "not ok %d - %s\n", ii + 1, testcases[ii].description);
exitcode = 1;
}
fflush(stdout);
}
return exitcode;
}