forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testapp.c
2327 lines (2016 loc) · 76.2 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#undef NDEBUG
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.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 <unistd.h>
#include <netinet/in.h>
#include <fcntl.h>
#include "config.h"
#include "cache.h"
#include "hash.h"
#include "stats_prefix.h"
#include "util.h"
#include "protocol_binary.h"
#ifdef TLS
#include <openssl/ssl.h>
#endif
#define TMP_TEMPLATE "/tmp/test_file.XXXXXXX"
enum test_return { TEST_SKIP, TEST_PASS, TEST_FAIL };
struct conn {
int sock;
#ifdef TLS
SSL_CTX *ssl_ctx;
SSL *ssl;
#endif
ssize_t (*read)(struct conn *c, void *buf, size_t count);
ssize_t (*write)(struct conn *c, const void *buf, size_t count);
};
struct settings {
char *hash_algorithm;
};
struct settings settings;
static ssize_t tcp_read(struct conn *c, void *buf, size_t count);
static ssize_t tcp_write(struct conn *c, const void *buf, size_t count);
#ifdef TLS
static ssize_t ssl_read(struct conn *c, void *buf, size_t count);
static ssize_t ssl_write(struct conn *c, const void *buf, size_t count);
#endif
ssize_t tcp_read(struct conn *c, void *buf, size_t count) {
assert(c != NULL);
return read(c->sock, buf, count);
}
ssize_t tcp_write(struct conn *c, const void *buf, size_t count) {
assert(c != NULL);
return write(c->sock, buf, count);
}
#ifdef TLS
ssize_t ssl_read(struct conn *c, void *buf, size_t count) {
assert(c != NULL);
return SSL_read(c->ssl, buf, count);
}
ssize_t ssl_write(struct conn *c, const void *buf, size_t count) {
assert(c != NULL);
return SSL_write(c->ssl, buf, count);
}
#endif
static pid_t server_pid;
static in_port_t port;
static struct conn *con = NULL;
static bool allow_closed_read = false;
static bool enable_ssl = false;
static void close_conn() {
if (con == NULL) return;
#ifdef TLS
if (con->ssl) {
SSL_shutdown(con->ssl);
SSL_free(con->ssl);
}
if (con->ssl_ctx)
SSL_CTX_free(con->ssl_ctx);
#endif
if (con->sock > 0) close(con->sock);
free(con);
con = NULL;
}
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_bulkalloc(size_t datasize)
{
cache_t *cache = cache_create("test", datasize, sizeof(char*),
NULL, NULL);
#define ITERATIONS 1024
void *ptr[ITERATIONS];
for (int ii = 0; ii < ITERATIONS; ++ii) {
ptr[ii] = cache_alloc(cache);
assert(ptr[ii] != 0);
memset(ptr[ii], 0xff, datasize);
}
for (int ii = 0; ii < ITERATIONS; ++ii) {
cache_free(cache, ptr[ii]);
}
#undef ITERATIONS
cache_destroy(cache);
return TEST_PASS;
}
static enum test_return test_issue_161(void)
{
enum test_return ret = cache_bulkalloc(1);
if (ret == TEST_PASS) {
ret = cache_bulkalloc(512);
}
return ret;
}
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 SIGABRT */
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_stats_prefix_find(void) {
PREFIX_STATS *pfs1, *pfs2;
stats_prefix_clear();
pfs1 = stats_prefix_find("abc", 3);
assert(pfs1 == NULL);
pfs1 = stats_prefix_find("abc|", 4);
assert(pfs1 == NULL);
pfs1 = stats_prefix_find("abc:", 4);
assert(pfs1 != NULL);
assert(0ULL == (pfs1->num_gets + pfs1->num_sets + pfs1->num_deletes + pfs1->num_hits));
pfs2 = stats_prefix_find("abc:", 4);
assert(pfs1 == pfs2);
pfs2 = stats_prefix_find("abc:d", 5);
assert(pfs1 == pfs2);
pfs2 = stats_prefix_find("xyz123:", 6);
assert(pfs1 != pfs2);
pfs2 = stats_prefix_find("ab:", 3);
assert(pfs1 != pfs2);
return TEST_PASS;
}
static enum test_return test_stats_prefix_record_get(void) {
PREFIX_STATS *pfs;
stats_prefix_clear();
stats_prefix_record_get("abc:123", 7, false);
pfs = stats_prefix_find("abc:123", 7);
assert(1 == pfs->num_gets);
assert(0 == pfs->num_hits);
stats_prefix_record_get("abc:456", 7, false);
assert(2 == pfs->num_gets);
assert(0 == pfs->num_hits);
stats_prefix_record_get("abc:456", 7, true);
assert(3 == pfs->num_gets);
assert(1 == pfs->num_hits);
stats_prefix_record_get("def:", 4, true);
assert(3 == pfs->num_gets);
assert(1 == pfs->num_hits);
return TEST_PASS;
}
static enum test_return test_stats_prefix_record_delete(void) {
PREFIX_STATS *pfs;
stats_prefix_clear();
stats_prefix_record_delete("abc:123", 7);
pfs = stats_prefix_find("abc:123", 7);
assert(0 == pfs->num_gets);
assert(0 == pfs->num_hits);
assert(1 == pfs->num_deletes);
assert(0 == pfs->num_sets);
stats_prefix_record_delete("def:", 4);
assert(1 == pfs->num_deletes);
return TEST_PASS;
}
static enum test_return test_stats_prefix_record_set(void) {
PREFIX_STATS *pfs;
stats_prefix_clear();
stats_prefix_record_set("abc:123", 7);
pfs = stats_prefix_find("abc:123", 7);
assert(0 == pfs->num_gets);
assert(0 == pfs->num_hits);
assert(0 == pfs->num_deletes);
assert(1 == pfs->num_sets);
stats_prefix_record_delete("def:", 4);
assert(1 == pfs->num_sets);
return TEST_PASS;
}
static enum test_return test_stats_prefix_dump(void) {
int hashval = hash("abc", 3) % PREFIX_HASH_SIZE;
char tmp[500];
char *expected;
int keynum;
int length;
stats_prefix_clear();
assert(strcmp("END\r\n", stats_prefix_dump(&length)) == 0);
assert(5 == length);
stats_prefix_record_set("abc:123", 7);
expected = "PREFIX abc get 0 hit 0 set 1 del 0\r\nEND\r\n";
assert(strcmp(expected, stats_prefix_dump(&length)) == 0);
assert(strlen(expected) == length);
stats_prefix_record_get("abc:123", 7, false);
expected = "PREFIX abc get 1 hit 0 set 1 del 0\r\nEND\r\n";
assert(strcmp(expected, stats_prefix_dump(&length)) == 0);
assert(strlen(expected) == length);
stats_prefix_record_get("abc:123", 7, true);
expected = "PREFIX abc get 2 hit 1 set 1 del 0\r\nEND\r\n";
assert(strcmp(expected, stats_prefix_dump(&length)) == 0);
assert(strlen(expected) == length);
stats_prefix_record_delete("abc:123", 7);
expected = "PREFIX abc get 2 hit 1 set 1 del 1\r\nEND\r\n";
assert(strcmp(expected, stats_prefix_dump(&length)) == 0);
assert(strlen(expected) == length);
/* The order of results might change if we switch hash functions. */
stats_prefix_record_delete("def:123", 7);
expected = "PREFIX abc get 2 hit 1 set 1 del 1\r\n"
"PREFIX def get 0 hit 0 set 0 del 1\r\n"
"END\r\n";
assert(strcmp(expected, stats_prefix_dump(&length)) == 0);
assert(strlen(expected) == length);
/* Find a key that hashes to the same bucket as "abc" */
bool found_match = false;
for (keynum = 0; keynum < PREFIX_HASH_SIZE * 100; keynum++) {
snprintf(tmp, sizeof(tmp), "%d:", keynum);
/* -1 because only the prefix portion is used when hashing */
if (hashval == hash(tmp, strlen(tmp) - 1) % PREFIX_HASH_SIZE) {
found_match = true;
break;
}
}
assert(found_match);
stats_prefix_record_set(tmp, strlen(tmp));
snprintf(tmp, sizeof(tmp),
"PREFIX %d get 0 hit 0 set 1 del 0\r\n"
"PREFIX abc get 2 hit 1 set 1 del 1\r\n"
"PREFIX def get 0 hit 0 set 0 del 1\r\n"
"END\r\n", keynum);
assert(strcmp(tmp, stats_prefix_dump(&length)) == 0);
assert(strlen(tmp) == length);
return TEST_PASS;
}
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
assert(!safe_strtoul(" issue221", &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
assert(!safe_strtoull(" issue221", &val)); // non-numeric
// 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
assert(!safe_strtoll(" issue221", &val)); // non-numeric
// 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
assert(!safe_strtol(" issue221", &val)); // non-numeric
// 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 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 daemon, int timeout) {
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);
#ifdef __sun
/* I want to name the corefiles differently so that they don't
overwrite each other
*/
char coreadm[128];
snprintf(coreadm, sizeof(coreadm),
"coreadm -p core.%%f.%%p %lu", (unsigned long)getpid());
system(coreadm);
#endif
pid_t pid = fork();
assert(pid != -1);
if (pid == 0) {
/* Child */
char *argv[24];
int arg = 0;
char tmo[24];
snprintf(tmo, sizeof(tmo), "%u", timeout);
putenv(environment);
#ifdef __sun
putenv("LD_PRELOAD=watchmalloc.so.1");
putenv("MALLOC_DEBUG=WATCH");
#endif
if (!daemon) {
argv[arg++] = "./timedrun";
argv[arg++] = tmo;
}
argv[arg++] = "./memcached-debug";
argv[arg++] = "-A";
argv[arg++] = "-p";
argv[arg++] = "-1";
argv[arg++] = "-U";
argv[arg++] = "0";
#ifdef TLS
if (enable_ssl) {
argv[arg++] = "-Z";
argv[arg++] = "-o";
argv[arg++] = "ssl_chain_cert=t/server_crt.pem";
argv[arg++] = "-o";
argv[arg++] = "ssl_key=t/server_key.pem";
}
#endif
/* Handle rpmbuild and the like doing this as root */
if (getuid() == 0) {
argv[arg++] = "-u";
argv[arg++] = "root";
}
if (daemon) {
argv[arg++] = "-d";
argv[arg++] = "-P";
argv[arg++] = pid_file;
}
#ifdef MESSAGE_DEBUG
argv[arg++] = "-vvv";
#endif
#ifdef HAVE_DROP_PRIVILEGES
argv[arg++] = "-o";
argv[arg++] = "relaxed_privileges";
#endif
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 (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, so we loop a few times
* reading as well */
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);
}
/* Avoid race by retrying 20 times */
for (int x = 0; x < 20 && fgets(buffer, sizeof(buffer), fp) == NULL; x++) {
usleep(10);
}
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, 15);
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 struct conn *connect_server(const char *hostname, in_port_t port,
bool nonblock, const bool ssl)
{
struct conn *c;
if (!(c = (struct conn *)calloc(1, sizeof(struct conn)))) {
fprintf(stderr, "Failed to allocate the client connection: %s\n",
strerror(errno));
return NULL;
}
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 if (nonblock) {
int flags = fcntl(sock, F_GETFL, 0);
if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
fprintf(stderr, "Failed to enable nonblocking mode: %s\n",
strerror(errno));
close(sock);
sock = -1;
}
}
} else {
fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
}
freeaddrinfo(ai);
}
c->sock = sock;
#ifdef TLS
if (sock > 0 && ssl) {
c->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
if (c->ssl_ctx == NULL) {
fprintf(stderr, "Failed to create the SSL context: %s\n",
strerror(errno));
close(sock);
sock = -1;
}
c->ssl = SSL_new(c->ssl_ctx);
if (c->ssl == NULL) {
fprintf(stderr, "Failed to create the SSL object: %s\n",
strerror(errno));
close(sock);
sock = -1;
}
SSL_set_fd (c->ssl, c->sock);
int ret = SSL_connect(c->ssl);
if (ret < 0) {
int err = SSL_get_error(c->ssl, ret);
if (err == SSL_ERROR_SYSCALL || err == SSL_ERROR_SSL) {
fprintf(stderr, "SSL connection failed with error code : %s\n",
strerror(errno));
close(sock);
sock = -1;
}
}
c->read = ssl_read;
c->write = ssl_write;
} else
#endif
{
c->read = tcp_read;
c->write = tcp_write;
}
return c;
}
static enum test_return test_vperror(void) {
int rv = 0;
int oldstderr = dup(STDERR_FILENO);
assert(oldstderr >= 0);
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 void send_ascii_command(const char *buf) {
off_t offset = 0;
const char* ptr = buf;
size_t len = strlen(buf);
do {
ssize_t nw = con->write((void*)con, ptr + offset, len - offset);
if (nw == -1) {
if (errno != EINTR) {
fprintf(stderr, "Failed to write: %s\n", strerror(errno));
abort();
}
} else {
offset += nw;
}
} while (offset < len);
}
/*
* This is a dead slow single byte read, but it should only read out
* _one_ response and I don't have an input buffer... The current
* implementation only supports single-line responses, so if you want to use
* it for get commands you need to implement that first ;-)
*/
static void read_ascii_response(char *buffer, size_t size) {
off_t offset = 0;
bool need_more = true;
do {
ssize_t nr = con->read(con, buffer + offset, 1);
if (nr == -1) {
if (errno != EINTR) {
fprintf(stderr, "Failed to read: %s\n", strerror(errno));
abort();
}
} else {
assert(nr == 1);
if (buffer[offset] == '\n') {
need_more = false;
buffer[offset + 1] = '\0';
}
offset += nr;
assert(offset + 1 < size);
}
} while (need_more);
}
static enum test_return test_issue_92(void) {
char buffer[1024];
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
send_ascii_command("stats cachedump 1 0 0\r\n");
read_ascii_response(buffer, sizeof(buffer));
assert(strncmp(buffer, "END", strlen("END")) == 0);
send_ascii_command("stats cachedump 200 0 0\r\n");
read_ascii_response(buffer, sizeof(buffer));
assert(strncmp(buffer, "CLIENT_ERROR", strlen("CLIENT_ERROR")) == 0);
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
return TEST_PASS;
}
static enum test_return test_issue_102(void) {
char buffer[4096];
memset(buffer, ' ', sizeof(buffer));
buffer[sizeof(buffer) - 1] = '\0';
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
send_ascii_command(buffer);
/* verify that the server closed the connection */
assert(con->read(con, buffer, sizeof(buffer)) == 0);
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
snprintf(buffer, sizeof(buffer), "gets ");
size_t offset = 5;
while (offset < 4000) {
offset += snprintf(buffer + offset, sizeof(buffer) - offset,
"%010u ", (unsigned int)offset);
}
send_ascii_command(buffer);
usleep(250);
send_ascii_command("\r\n");
char rsp[80];
read_ascii_response(rsp, sizeof(rsp));
assert(strncmp(rsp, "END", strlen("END")) == 0);
buffer[3]= ' ';
send_ascii_command(buffer);
usleep(250);
send_ascii_command("\r\n");
read_ascii_response(rsp, sizeof(rsp));
assert(strncmp(rsp, "END", strlen("END")) == 0);
memset(buffer, ' ', sizeof(buffer));
int len = snprintf(buffer + 101, sizeof(buffer) - 101, "gets foo");
buffer[101 + len] = ' ';
buffer[sizeof(buffer) - 1] = '\0';
send_ascii_command(buffer);
/* verify that the server closed the connection */
assert(con->read(con, buffer, sizeof(buffer)) == 0);
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
return TEST_PASS;
}
static enum test_return start_memcached_server(void) {
server_pid = start_server(&port, false, 600);
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
return TEST_PASS;
}
static enum test_return stop_memcached_server(void) {
close_conn();
if (server_pid != -1) {
assert(kill(server_pid, SIGTERM) == 0);
}
return TEST_PASS;
}
static enum test_return shutdown_memcached_server(void) {
char buffer[1024];
close_conn();
con = connect_server("127.0.0.1", port, false, enable_ssl);
assert(con);
send_ascii_command("shutdown\r\n");
/* verify that the server closed the connection */
assert(con->read(con, buffer, sizeof(buffer)) == 0);
close_conn();
/* We set server_pid to -1 so that we don't later call kill() */
if (kill(server_pid, 0) == 0) {
server_pid = -1;
}
return TEST_PASS;
}
static void safe_send(const void* buf, size_t len, bool hickup)
{
off_t offset = 0;
const char* ptr = buf;
#ifdef MESSAGE_DEBUG
uint8_t val = *ptr;
assert(val == (uint8_t)0x80);
fprintf(stderr, "About to send %lu bytes:", (unsigned long)len);
for (int ii = 0; ii < len; ++ii) {
if (ii % 4 == 0) {
fprintf(stderr, "\n ");
}
val = *(ptr + ii);
fprintf(stderr, " 0x%02x", val);
}
fprintf(stderr, "\n");
usleep(500);
#endif
do {
size_t num_bytes = len - offset;
if (hickup) {
if (num_bytes > 1024) {
num_bytes = (rand() % 1023) + 1;
}
}
ssize_t nw = con->write(con, ptr + offset, num_bytes);
if (nw == -1) {
if (errno != EINTR) {
fprintf(stderr, "Failed to write: %s\n", strerror(errno));
abort();
}
} else {
if (hickup) {
usleep(100);
}
offset += nw;
}
} while (offset < len);
}
static bool safe_recv(void *buf, size_t len) {
if (len == 0) {
return true;
}
off_t offset = 0;
do {
ssize_t nr = con->read(con, ((char*)buf) + offset, len - offset);
if (nr == -1) {
if (errno != EINTR) {
fprintf(stderr, "Failed to read: %s\n", strerror(errno));
abort();
}
} else {
if (nr == 0 && allow_closed_read) {
return false;
}
assert(nr != 0);
offset += nr;
}
} while (offset < len);
return true;
}
static bool safe_recv_packet(void *buf, size_t size) {
protocol_binary_response_no_extras *response = buf;
assert(size > sizeof(*response));
if (!safe_recv(response, sizeof(*response))) {
return false;