Testing to make sure that partial reads do not break protocol (aka on active
[awesomized/libmemcached] / tests / function.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <memcached.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <time.h>
14 #include "../lib/common.h"
15 #include "../src/generator.h"
16 #include "../src/execute.h"
17
18 #include "test.h"
19
20 #define GLOBAL_COUNT 100000
21 static pairs_st *global_pairs;
22 static char *global_keys[GLOBAL_COUNT];
23 static size_t global_keys_length[GLOBAL_COUNT];
24
25 uint8_t init_test(memcached_st *not_used)
26 {
27 memcached_st memc;
28
29 (void)memcached_create(&memc);
30 memcached_free(&memc);
31
32 return 0;
33 }
34
35 uint8_t allocation_test(memcached_st *not_used)
36 {
37 memcached_st *memc;
38 memc= memcached_create(NULL);
39 assert(memc);
40 memcached_free(memc);
41
42 return 0;
43 }
44
45 uint8_t clone_test(memcached_st *memc)
46 {
47 /* All null? */
48 {
49 memcached_st *clone;
50 clone= memcached_clone(NULL, NULL);
51 assert(clone);
52 memcached_free(clone);
53 }
54
55 /* Can we init from null? */
56 {
57 memcached_st *clone;
58 clone= memcached_clone(NULL, memc);
59 assert(clone);
60 memcached_free(clone);
61 }
62
63 /* Can we init from struct? */
64 {
65 memcached_st declared_clone;
66 memcached_st *clone;
67 clone= memcached_clone(&declared_clone, NULL);
68 assert(clone);
69 memcached_free(clone);
70 }
71
72 /* Can we init from struct? */
73 {
74 memcached_st declared_clone;
75 memcached_st *clone;
76 clone= memcached_clone(&declared_clone, memc);
77 assert(clone);
78 memcached_free(clone);
79 }
80
81 return 0;
82 }
83
84 uint8_t connection_test(memcached_st *memc)
85 {
86 memcached_return rc;
87
88 rc= memcached_server_add(memc, "localhost", 0);
89 assert(rc == MEMCACHED_SUCCESS);
90
91 return 0;
92 }
93
94 uint8_t error_test(memcached_st *memc)
95 {
96 memcached_return rc;
97
98 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
99 {
100 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
101 }
102
103 return 0;
104 }
105
106 uint8_t set_test(memcached_st *memc)
107 {
108 memcached_return rc;
109 char *key= "foo";
110 char *value= "when we sanitize";
111
112 rc= memcached_set(memc, key, strlen(key),
113 value, strlen(value),
114 (time_t)0, (uint16_t)0);
115 assert(rc == MEMCACHED_SUCCESS);
116
117 return 0;
118 }
119
120 uint8_t add_test(memcached_st *memc)
121 {
122 memcached_return rc;
123 char *key= "foo";
124 char *value= "when we sanitize";
125
126 rc= memcached_add(memc, key, strlen(key),
127 value, strlen(value),
128 (time_t)0, (uint16_t)0);
129 assert(rc == MEMCACHED_NOTSTORED);
130
131 return 0;
132 }
133
134 uint8_t replace_test(memcached_st *memc)
135 {
136 memcached_return rc;
137 char *key= "foo";
138 char *value= "when we sanitize";
139
140 rc= memcached_replace(memc, key, strlen(key),
141 value, strlen(value),
142 (time_t)0, (uint16_t)0);
143 assert(rc == MEMCACHED_SUCCESS);
144
145 return 0;
146 }
147
148 uint8_t delete_test(memcached_st *memc)
149 {
150 memcached_return rc;
151 char *key= "foo";
152 char *value= "when we sanitize";
153
154 rc= memcached_set(memc, key, strlen(key),
155 value, strlen(value),
156 (time_t)0, (uint16_t)0);
157 assert(rc == MEMCACHED_SUCCESS);
158
159 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
160 assert(rc == MEMCACHED_SUCCESS);
161
162 return 0;
163 }
164
165 uint8_t flush_test(memcached_st *memc)
166 {
167 memcached_return rc;
168
169 rc= memcached_flush(memc, 0);
170 assert(rc == MEMCACHED_SUCCESS);
171
172 return 0;
173 }
174
175 uint8_t get_test(memcached_st *memc)
176 {
177 memcached_return rc;
178 char *key= "foo";
179 char *string;
180 size_t string_length;
181 uint16_t flags;
182
183 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
184 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND);
185
186 string= memcached_get(memc, key, strlen(key),
187 &string_length, &flags, &rc);
188
189 assert(rc == MEMCACHED_NOTFOUND);
190 assert(string_length == 0);
191 assert(!string);
192
193 return 0;
194 }
195
196 uint8_t get_test2(memcached_st *memc)
197 {
198 memcached_return rc;
199 char *key= "foo";
200 char *value= "when we sanitize";
201 char *string;
202 size_t string_length;
203 uint16_t flags;
204
205 rc= memcached_set(memc, key, strlen(key),
206 value, strlen(value),
207 (time_t)0, (uint16_t)0);
208 assert(rc == MEMCACHED_SUCCESS);
209
210 string= memcached_get(memc, key, strlen(key),
211 &string_length, &flags, &rc);
212
213 assert(string);
214 assert(rc == MEMCACHED_SUCCESS);
215 assert(string_length == strlen(value));
216 assert(!memcmp(string, value, string_length));
217
218 free(string);
219
220 return 0;
221 }
222
223 uint8_t set_test2(memcached_st *memc)
224 {
225 memcached_return rc;
226 char *key= "foo";
227 char *value= "train in the brain";
228 size_t value_length= strlen(value);
229 unsigned int x;
230
231 for (x= 0; x < 10; x++)
232 {
233 rc= memcached_set(memc, key, strlen(key),
234 value, value_length,
235 (time_t)0, (uint16_t)0);
236 assert(rc == MEMCACHED_SUCCESS);
237 }
238
239 return 0;
240 }
241
242 uint8_t set_test3(memcached_st *memc)
243 {
244 memcached_return rc;
245 char *key= "foo";
246 char *value;
247 size_t value_length= 8191;
248 unsigned int x;
249
250 value = (char*)malloc(value_length);
251 assert(value);
252
253 for (x= 0; x < value_length; x++)
254 value[x] = (char) (x % 127);
255
256 for (x= 0; x < 1; x++)
257 {
258 rc= memcached_set(memc, key, strlen(key),
259 value, value_length,
260 (time_t)0, (uint16_t)0);
261 assert(rc == MEMCACHED_SUCCESS);
262 }
263
264 free(value);
265
266 return 0;
267 }
268
269 uint8_t get_test3(memcached_st *memc)
270 {
271 memcached_return rc;
272 char *key= "foo";
273 char *value;
274 size_t value_length= 8191;
275 char *string;
276 size_t string_length;
277 uint16_t flags;
278 int x;
279
280 value = (char*)malloc(value_length);
281 assert(value);
282
283 for (x= 0; x < value_length; x++)
284 value[x] = (char) (x % 127);
285
286 rc= memcached_set(memc, key, strlen(key),
287 value, value_length,
288 (time_t)0, (uint16_t)0);
289 assert(rc == MEMCACHED_SUCCESS);
290
291 string= memcached_get(memc, key, strlen(key),
292 &string_length, &flags, &rc);
293
294 assert(rc == MEMCACHED_SUCCESS);
295 assert(string);
296 assert(string_length == value_length);
297 assert(!memcmp(string, value, string_length));
298
299 free(string);
300 free(value);
301
302 return 0;
303 }
304
305 uint8_t get_test4(memcached_st *memc)
306 {
307 memcached_return rc;
308 char *key= "foo";
309 char *value;
310 size_t value_length= 8191;
311 char *string;
312 size_t string_length;
313 uint16_t flags;
314 int x;
315
316 value = (char*)malloc(value_length);
317 assert(value);
318
319 for (x= 0; x < value_length; x++)
320 value[x] = (char) (x % 127);
321
322 rc= memcached_set(memc, key, strlen(key),
323 value, value_length,
324 (time_t)0, (uint16_t)0);
325 assert(rc == MEMCACHED_SUCCESS);
326
327 for (x= 0; x < 10; x++)
328 {
329 string= memcached_get(memc, key, strlen(key),
330 &string_length, &flags, &rc);
331
332 assert(rc == MEMCACHED_SUCCESS);
333 assert(string);
334 assert(string_length == value_length);
335 assert(!memcmp(string, value, string_length));
336 free(string);
337 }
338
339 free(value);
340
341 return 0;
342 }
343
344 uint8_t stats_servername_test(memcached_st *memc)
345 {
346 memcached_return rc;
347 memcached_stat_st stat;
348 rc= memcached_stat_servername(&stat, NULL,
349 "localhost",
350 MEMCACHED_DEFAULT_PORT);
351
352 return 0;
353 }
354
355 uint8_t increment_test(memcached_st *memc)
356 {
357 uint64_t new_number;
358 memcached_return rc;
359 char *key= "number";
360 char *value= "0";
361
362 rc= memcached_set(memc, key, strlen(key),
363 value, strlen(value),
364 (time_t)0, (uint16_t)0);
365 assert(rc == MEMCACHED_SUCCESS);
366
367 rc= memcached_increment(memc, key, strlen(key),
368 1, &new_number);
369 assert(rc == MEMCACHED_SUCCESS);
370 assert(new_number == 1);
371
372 rc= memcached_increment(memc, key, strlen(key),
373 1, &new_number);
374 assert(rc == MEMCACHED_SUCCESS);
375 assert(new_number == 2);
376
377 return 0;
378 }
379
380 uint8_t decrement_test(memcached_st *memc)
381 {
382 uint64_t new_number;
383 memcached_return rc;
384 char *key= "number";
385 char *value= "3";
386
387 rc= memcached_set(memc, key, strlen(key),
388 value, strlen(value),
389 (time_t)0, (uint16_t)0);
390 assert(rc == MEMCACHED_SUCCESS);
391
392 rc= memcached_decrement(memc, key, strlen(key),
393 1, &new_number);
394 assert(rc == MEMCACHED_SUCCESS);
395 assert(new_number == 2);
396
397 rc= memcached_decrement(memc, key, strlen(key),
398 1, &new_number);
399 assert(rc == MEMCACHED_SUCCESS);
400 assert(new_number == 1);
401
402 return 0;
403 }
404
405 uint8_t quit_test(memcached_st *memc)
406 {
407 memcached_return rc;
408 char *key= "fudge";
409 char *value= "sanford and sun";
410
411 rc= memcached_set(memc, key, strlen(key),
412 value, strlen(value),
413 (time_t)10, (uint16_t)3);
414 assert(rc == MEMCACHED_SUCCESS);
415 memcached_quit(memc);
416
417 rc= memcached_set(memc, key, strlen(key),
418 value, strlen(value),
419 (time_t)50, (uint16_t)9);
420 assert(rc == MEMCACHED_SUCCESS);
421
422 return 0;
423 }
424
425 uint8_t mget_result_test(memcached_st *memc)
426 {
427 memcached_return rc;
428 char *keys[]= {"fudge", "son", "food"};
429 size_t key_length[]= {5, 3, 4};
430 unsigned int x;
431
432 memcached_result_st results_obj;
433 memcached_result_st *results;
434
435 results= memcached_result_create(memc, &results_obj);
436 assert(results);
437 assert(&results_obj == results);
438
439 /* We need to empty the server before continueing test */
440 rc= memcached_flush(memc, 0);
441 assert(rc == MEMCACHED_SUCCESS);
442
443 rc= memcached_mget(memc, keys, key_length, 3);
444 assert(rc == MEMCACHED_SUCCESS);
445
446 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
447 {
448 assert(results);
449 }
450 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
451 assert(!results);
452 assert(rc == MEMCACHED_NOTFOUND);
453
454 for (x= 0; x < 3; x++)
455 {
456 rc= memcached_set(memc, keys[x], key_length[x],
457 keys[x], key_length[x],
458 (time_t)50, (uint16_t)9);
459 assert(rc == MEMCACHED_SUCCESS);
460 }
461
462 rc= memcached_mget(memc, keys, key_length, 3);
463 assert(rc == MEMCACHED_SUCCESS);
464
465 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
466 {
467 assert(results);
468 assert(&results_obj == results);
469 assert(rc == MEMCACHED_SUCCESS);
470 assert(memcached_result_key_length(results) == memcached_result_length(results));
471 assert(!memcmp(memcached_result_key_value(results),
472 memcached_result_value(results),
473 memcached_result_length(results)));
474 }
475
476 memcached_result_free(&results_obj);
477
478 return 0;
479 }
480
481 uint8_t mget_result_alloc_test(memcached_st *memc)
482 {
483 memcached_return rc;
484 char *keys[]= {"fudge", "son", "food"};
485 size_t key_length[]= {5, 3, 4};
486 unsigned int x;
487
488 memcached_result_st *results;
489
490 /* We need to empty the server before continueing test */
491 rc= memcached_flush(memc, 0);
492 assert(rc == MEMCACHED_SUCCESS);
493
494 rc= memcached_mget(memc, keys, key_length, 3);
495 assert(rc == MEMCACHED_SUCCESS);
496
497 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
498 {
499 assert(results);
500 }
501 assert(!results);
502 assert(rc == MEMCACHED_NOTFOUND);
503
504 for (x= 0; x < 3; x++)
505 {
506 rc= memcached_set(memc, keys[x], key_length[x],
507 keys[x], key_length[x],
508 (time_t)50, (uint16_t)9);
509 assert(rc == MEMCACHED_SUCCESS);
510 }
511
512 rc= memcached_mget(memc, keys, key_length, 3);
513 assert(rc == MEMCACHED_SUCCESS);
514
515 x= 0;
516 while ((results= memcached_fetch_result(memc, NULL, &rc)))
517 {
518 assert(results);
519 assert(rc == MEMCACHED_SUCCESS);
520 assert(memcached_result_key_length(results) == memcached_result_length(results));
521 assert(!memcmp(memcached_result_key_value(results),
522 memcached_result_value(results),
523 memcached_result_length(results)));
524 memcached_result_free(results);
525 x++;
526 }
527
528 return 0;
529 }
530
531 uint8_t mget_test(memcached_st *memc)
532 {
533 memcached_return rc;
534 char *keys[]= {"fudge", "son", "food"};
535 size_t key_length[]= {5, 3, 4};
536 unsigned int x;
537 uint16_t flags;
538
539 char return_key[MEMCACHED_MAX_KEY];
540 size_t return_key_length;
541 char *return_value;
542 size_t return_value_length;
543
544 /* We need to empty the server before continueing test */
545 rc= memcached_flush(memc, 0);
546 assert(rc == MEMCACHED_SUCCESS);
547
548 rc= memcached_mget(memc, keys, key_length, 3);
549 assert(rc == MEMCACHED_SUCCESS);
550
551 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
552 &return_value_length, &flags, &rc)) != NULL)
553 {
554 assert(return_value);
555 }
556 assert(!return_value);
557 assert(return_value_length == 0);
558 assert(rc == MEMCACHED_NOTFOUND);
559
560 for (x= 0; x < 3; x++)
561 {
562 rc= memcached_set(memc, keys[x], key_length[x],
563 keys[x], key_length[x],
564 (time_t)50, (uint16_t)9);
565 assert(rc == MEMCACHED_SUCCESS);
566 }
567
568 rc= memcached_mget(memc, keys, key_length, 3);
569 assert(rc == MEMCACHED_SUCCESS);
570
571 x= 0;
572 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
573 &return_value_length, &flags, &rc)))
574 {
575 assert(return_value);
576 assert(rc == MEMCACHED_SUCCESS);
577 assert(return_key_length == return_value_length);
578 assert(!memcmp(return_value, return_key, return_value_length));
579 free(return_value);
580 x++;
581 }
582
583 return 0;
584 }
585
586 uint8_t get_stats_keys(memcached_st *memc)
587 {
588 char **list;
589 char **ptr;
590 memcached_stat_st stat;
591 memcached_return rc;
592
593 list= memcached_stat_get_keys(memc, &stat, &rc);
594 assert(rc == MEMCACHED_SUCCESS);
595 for (ptr= list; *ptr; ptr++)
596 printf("Found key %s\n", *ptr);
597 fflush(stdout);
598
599 free(list);
600
601 return 0;
602 }
603
604 uint8_t get_stats(memcached_st *memc)
605 {
606 unsigned int x;
607 char **list;
608 char **ptr;
609 memcached_return rc;
610 memcached_stat_st *stat;
611
612 stat= memcached_stat(memc, NULL, &rc);
613 assert(rc == MEMCACHED_SUCCESS);
614
615 assert(rc == MEMCACHED_SUCCESS);
616 assert(stat);
617
618 for (x= 0; x < memcached_server_count(memc); x++)
619 {
620 list= memcached_stat_get_keys(memc, &stat[x], &rc);
621 assert(rc == MEMCACHED_SUCCESS);
622 for (ptr= list; *ptr; ptr++)
623 printf("Found key %s\n", *ptr);
624
625 free(list);
626 }
627
628 memcached_stat_free(NULL, stat);
629
630 return 0;
631 }
632
633 uint8_t add_host_test(memcached_st *memc)
634 {
635 unsigned int x;
636 memcached_server_st *servers;
637 memcached_return rc;
638 char servername[]= "0.example.com";
639
640 servers= memcached_server_list_append(NULL, servername, 400, &rc);
641 assert(servers);
642 assert(1 == memcached_server_list_count(servers));
643
644 for (x= 2; x < 20; x++)
645 {
646 char buffer[SMALL_STRING_LEN];
647
648 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
649 servers= memcached_server_list_append(servers, buffer, 401,
650 &rc);
651 assert(rc == MEMCACHED_SUCCESS);
652 assert(x == memcached_server_list_count(servers));
653 }
654
655 rc= memcached_server_push(memc, servers);
656 assert(rc == MEMCACHED_SUCCESS);
657 rc= memcached_server_push(memc, servers);
658 assert(rc == MEMCACHED_SUCCESS);
659
660 memcached_server_list_free(servers);
661
662 return 0;
663 }
664
665 /* We don't test the behavior itself, we test the switches */
666 uint8_t behavior_test(memcached_st *memc)
667 {
668 unsigned long long value;
669 unsigned int set= 1;
670
671 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
672 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
673 assert(value == 1);
674
675 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
676 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
677 assert(value == 1);
678
679 set= MEMCACHED_HASH_MD5;
680 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
681 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
682 assert(value == MEMCACHED_HASH_MD5);
683
684 set= 0;
685
686 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
687 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
688 assert(value == 0);
689
690 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
691 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
692 assert(value == 0);
693
694 set= MEMCACHED_HASH_DEFAULT;
695 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
696 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
697 assert(value == MEMCACHED_HASH_DEFAULT);
698
699 set= MEMCACHED_HASH_CRC;
700 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
701 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
702 assert(value == MEMCACHED_HASH_CRC);
703
704 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
705 assert(value > 0);
706
707 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
708 assert(value > 0);
709
710 return 0;
711 }
712
713 /* Test case provided by Cal Haldenbrand */
714 uint8_t user_supplied_bug1(memcached_st *memc)
715 {
716 unsigned int setter= 1;
717 unsigned int x;
718
719 unsigned long long total= 0;
720 int size= 0;
721 char key[10];
722 char randomstuff[6 * 1024];
723 memcached_return rc;
724
725 memset(randomstuff, 0, 6 * 1024);
726
727 /* We just keep looking at the same values over and over */
728 srandom(10);
729
730 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
731 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
732
733
734 /* add key */
735 for (x= 0 ; total < 20 * 1024576 ; x++ )
736 {
737 unsigned int j= 0;
738
739 size= (rand() % ( 5 * 1024 ) ) + 400;
740 memset(randomstuff, 0, 6 * 1024);
741 assert(size < 6 * 1024); /* Being safe here */
742
743 for (j= 0 ; j < size ;j++)
744 randomstuff[j] = (char) (rand() % 26) + 97;
745
746 total += size;
747 sprintf(key, "%d", x);
748 rc = memcached_set(memc, key, strlen(key),
749 randomstuff, strlen(randomstuff), 10, 0);
750 /* If we fail, lets try again */
751 if (rc != MEMCACHED_SUCCESS)
752 rc = memcached_set(memc, key, strlen(key),
753 randomstuff, strlen(randomstuff), 10, 0);
754 assert(rc == MEMCACHED_SUCCESS);
755 }
756
757 return 0;
758 }
759
760 /* Test case provided by Cal Haldenbrand */
761 uint8_t user_supplied_bug2(memcached_st *memc)
762 {
763 int errors;
764 unsigned int setter;
765 unsigned int x;
766 unsigned long long total;
767
768 setter= 1;
769 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
770 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
771 #ifdef NOT_YET
772 setter = 20 * 1024576;
773 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
774 setter = 20 * 1024576;
775 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
776 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
777 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
778
779 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
780 #endif
781
782 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
783 {
784 memcached_return rc= MEMCACHED_SUCCESS;
785 char buffer[SMALL_STRING_LEN];
786 uint16_t flags= 0;
787 size_t val_len= 0;
788 char *getval;
789
790 memset(buffer, 0, SMALL_STRING_LEN);
791
792 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
793 getval= memcached_get(memc, buffer, strlen(buffer),
794 &val_len, &flags, &rc);
795 if (rc != MEMCACHED_SUCCESS)
796 {
797 if (rc == MEMCACHED_NOTFOUND)
798 errors++;
799 else
800 assert(0);
801
802 continue;
803 }
804 total+= val_len;
805 errors= 0;
806 free(getval);
807 }
808
809 return 0;
810 }
811
812 /* Do a large mget() over all the keys we think exist */
813 #define KEY_COUNT 3000 // * 1024576
814 uint8_t user_supplied_bug3(memcached_st *memc)
815 {
816 memcached_return rc;
817 unsigned int setter;
818 unsigned int x;
819 char **keys;
820 size_t key_lengths[KEY_COUNT];
821
822 setter= 1;
823 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
824 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
825 #ifdef NOT_YET
826 setter = 20 * 1024576;
827 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
828 setter = 20 * 1024576;
829 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
830 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
831 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
832 #endif
833
834 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
835 assert(keys);
836 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
837 for (x= 0; x < KEY_COUNT; x++)
838 {
839 char buffer[30];
840
841 snprintf(buffer, 30, "%u", x);
842 keys[x]= strdup(buffer);
843 key_lengths[x]= strlen(keys[x]);
844 }
845
846 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
847 assert(rc == MEMCACHED_SUCCESS);
848
849 /* Turn this into a help function */
850 {
851 char return_key[MEMCACHED_MAX_KEY];
852 size_t return_key_length;
853 char *return_value;
854 size_t return_value_length;
855 uint16_t flags;
856
857 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
858 &return_value_length, &flags, &rc)))
859 {
860 assert(return_value);
861 assert(rc == MEMCACHED_SUCCESS);
862 free(return_value);
863 }
864 }
865
866 for (x= 0; x < KEY_COUNT; x++)
867 free(keys[x]);
868 free(keys);
869
870 return 0;
871 }
872
873 /* Make sure we behave properly if server list has no values */
874 uint8_t user_supplied_bug4(memcached_st *memc)
875 {
876 memcached_return rc;
877 char *keys[]= {"fudge", "son", "food"};
878 size_t key_length[]= {5, 3, 4};
879 unsigned int x;
880 uint16_t flags;
881
882 /* Here we free everything before running a bunch of mget tests */
883 {
884 memcached_server_list_free(memc->hosts);
885 memc->hosts= NULL;
886 memc->number_of_hosts= 0;
887 }
888
889 char return_key[MEMCACHED_MAX_KEY];
890 size_t return_key_length;
891 char *return_value;
892 size_t return_value_length;
893
894 /* We need to empty the server before continueing test */
895 rc= memcached_flush(memc, 0);
896 assert(rc == MEMCACHED_NO_SERVERS);
897
898 rc= memcached_mget(memc, keys, key_length, 3);
899 assert(rc == MEMCACHED_NO_SERVERS);
900
901 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
902 &return_value_length, &flags, &rc)) != NULL)
903 {
904 assert(return_value);
905 }
906 assert(!return_value);
907 assert(return_value_length == 0);
908 assert(rc == MEMCACHED_NO_SERVERS);
909
910 for (x= 0; x < 3; x++)
911 {
912 rc= memcached_set(memc, keys[x], key_length[x],
913 keys[x], key_length[x],
914 (time_t)50, (uint16_t)9);
915 assert(rc == MEMCACHED_NO_SERVERS);
916 }
917
918 rc= memcached_mget(memc, keys, key_length, 3);
919 assert(rc == MEMCACHED_NO_SERVERS);
920
921 x= 0;
922 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
923 &return_value_length, &flags, &rc)))
924 {
925 assert(return_value);
926 assert(rc == MEMCACHED_SUCCESS);
927 assert(return_key_length == return_value_length);
928 assert(!memcmp(return_value, return_key, return_value_length));
929 free(return_value);
930 x++;
931 }
932
933 return 0;
934 }
935
936 #define VALUE_SIZE_BUG5 1048064
937 uint8_t user_supplied_bug5(memcached_st *memc)
938 {
939 memcached_return rc;
940 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
941 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
942 char return_key[MEMCACHED_MAX_KEY];
943 size_t return_key_length;
944 char *value;
945 size_t value_length;
946 uint16_t flags;
947 unsigned int count;
948 unsigned int x;
949 char insert_data[VALUE_SIZE_BUG5];
950
951 for (x= 0; x < VALUE_SIZE_BUG5; x++)
952 insert_data[x]= rand();
953
954 memcached_flush(memc, 0);
955 value= memcached_get(memc, keys[0], key_length[0],
956 &value_length, &flags, &rc);
957 assert(value == NULL);
958 rc= memcached_mget(memc, keys, key_length, 4);
959
960 count= 0;
961 while ((value= memcached_fetch(memc, return_key, &return_key_length,
962 &value_length, &flags, &rc)))
963 count++;
964 assert(count == 0);
965
966 for (x= 0; x < 4; x++)
967 {
968 rc= memcached_set(memc, keys[x], key_length[x],
969 insert_data, VALUE_SIZE_BUG5,
970 (time_t)0, (uint16_t)0);
971 assert(rc == MEMCACHED_SUCCESS);
972 }
973
974 for (x= 0; x < 10; x++)
975 {
976 value= memcached_get(memc, keys[0], key_length[0],
977 &value_length, &flags, &rc);
978 assert(value);
979 free(value);
980
981 rc= memcached_mget(memc, keys, key_length, 4);
982 count= 0;
983 while ((value= memcached_fetch(memc, return_key, &return_key_length,
984 &value_length, &flags, &rc)))
985 {
986 count++;
987 free(value);
988 }
989 assert(count == 4);
990 }
991
992 return 0;
993 }
994
995 uint8_t user_supplied_bug6(memcached_st *memc)
996 {
997 memcached_return rc;
998 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
999 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1000 char return_key[MEMCACHED_MAX_KEY];
1001 size_t return_key_length;
1002 char *value;
1003 size_t value_length;
1004 uint16_t flags;
1005 unsigned int count;
1006 unsigned int x;
1007 char insert_data[VALUE_SIZE_BUG5];
1008
1009 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1010 insert_data[x]= rand();
1011
1012 memcached_flush(memc, 0);
1013 value= memcached_get(memc, keys[0], key_length[0],
1014 &value_length, &flags, &rc);
1015 assert(value == NULL);
1016 rc= memcached_mget(memc, keys, key_length, 4);
1017
1018 count= 0;
1019 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1020 &value_length, &flags, &rc)))
1021 count++;
1022 assert(count == 0);
1023
1024 for (x= 0; x < 4; x++)
1025 {
1026 rc= memcached_set(memc, keys[x], key_length[x],
1027 insert_data, VALUE_SIZE_BUG5,
1028 (time_t)0, (uint16_t)0);
1029 assert(rc == MEMCACHED_SUCCESS);
1030 }
1031
1032 for (x= 0; x < 10; x++)
1033 {
1034 value= memcached_get(memc, keys[0], key_length[0],
1035 &value_length, &flags, &rc);
1036 assert(value);
1037 free(value);
1038
1039 rc= memcached_mget(memc, keys, key_length, 4);
1040 count= 3;
1041 /* We test for purge of partial complete fetches */
1042 for (count= 3; count; count--)
1043 {
1044 value= memcached_fetch(memc, return_key, &return_key_length,
1045 &value_length, &flags, &rc);
1046 free(value);
1047 assert(rc == MEMCACHED_SUCCESS);
1048 }
1049 }
1050
1051 return 0;
1052 }
1053
1054 uint8_t result_static(memcached_st *memc)
1055 {
1056 memcached_result_st result;
1057 memcached_result_st *result_ptr;
1058
1059 result_ptr= memcached_result_create(memc, &result);
1060 assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
1061 assert(result_ptr);
1062 memcached_result_free(&result);
1063
1064 return 0;
1065 }
1066
1067 uint8_t result_alloc(memcached_st *memc)
1068 {
1069 memcached_result_st *result;
1070
1071 result= memcached_result_create(memc, NULL);
1072 assert(result);
1073 memcached_result_free(result);
1074
1075 return 0;
1076 }
1077
1078 uint8_t string_static_null(memcached_st *memc)
1079 {
1080 memcached_string_st string;
1081 memcached_string_st *string_ptr;
1082
1083 string_ptr= memcached_string_create(memc, &string, 0);
1084 assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
1085 assert(string_ptr);
1086 memcached_string_free(&string);
1087
1088 return 0;
1089 }
1090
1091 uint8_t string_alloc_null(memcached_st *memc)
1092 {
1093 memcached_string_st *string;
1094
1095 string= memcached_string_create(memc, NULL, 0);
1096 assert(string);
1097 memcached_string_free(string);
1098
1099 return 0;
1100 }
1101
1102 uint8_t string_alloc_with_size(memcached_st *memc)
1103 {
1104 memcached_string_st *string;
1105
1106 string= memcached_string_create(memc, NULL, 1024);
1107 assert(string);
1108 memcached_string_free(string);
1109
1110 return 0;
1111 }
1112
1113 uint8_t string_alloc_with_size_toobig(memcached_st *memc)
1114 {
1115 memcached_string_st *string;
1116
1117 string= memcached_string_create(memc, NULL, INT64_MAX);
1118 assert(string == NULL);
1119
1120 return 0;
1121 }
1122
1123 uint8_t string_alloc_append(memcached_st *memc)
1124 {
1125 unsigned int x;
1126 char buffer[SMALL_STRING_LEN];
1127 memcached_string_st *string;
1128
1129 /* Ring the bell! */
1130 memset(buffer, 6, SMALL_STRING_LEN);
1131
1132 string= memcached_string_create(memc, NULL, 100);
1133 assert(string);
1134
1135 for (x= 0; x < 1024; x++)
1136 {
1137 memcached_return rc;
1138 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1139 assert(rc == MEMCACHED_SUCCESS);
1140 }
1141 memcached_string_free(string);
1142
1143 return 0;
1144 }
1145
1146 uint8_t string_alloc_append_toobig(memcached_st *memc)
1147 {
1148 memcached_return rc;
1149 unsigned int x;
1150 char buffer[SMALL_STRING_LEN];
1151 memcached_string_st *string;
1152
1153 /* Ring the bell! */
1154 memset(buffer, 6, SMALL_STRING_LEN);
1155
1156 string= memcached_string_create(memc, NULL, 100);
1157 assert(string);
1158
1159 for (x= 0; x < 1024; x++)
1160 {
1161 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1162 assert(rc == MEMCACHED_SUCCESS);
1163 }
1164 rc= memcached_string_append(string, buffer, INT64_MAX);
1165 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
1166 memcached_string_free(string);
1167
1168 return 0;
1169 }
1170
1171 uint8_t generate_data(memcached_st *memc)
1172 {
1173 unsigned long long x;
1174 global_pairs= pairs_generate(GLOBAL_COUNT);
1175 execute_set(memc, global_pairs, GLOBAL_COUNT);
1176
1177 for (x= 0; x < GLOBAL_COUNT; x++)
1178 {
1179 global_keys[x]= global_pairs[x].key;
1180 global_keys_length[x]= global_pairs[x].key_length;
1181 }
1182
1183 return 0;
1184 }
1185
1186 uint8_t get_read(memcached_st *memc)
1187 {
1188 unsigned int x;
1189 memcached_return rc;
1190
1191 {
1192 char *return_value;
1193 size_t return_value_length;
1194 uint16_t flags;
1195
1196 for (x= 0; x < GLOBAL_COUNT; x++)
1197 {
1198 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
1199 &return_value_length, &flags, &rc);
1200 /*
1201 assert(return_value);
1202 assert(rc == MEMCACHED_SUCCESS);
1203 */
1204 if (rc == MEMCACHED_SUCCESS && return_value)
1205 free(return_value);
1206 }
1207 }
1208
1209 return 0;
1210 }
1211
1212 uint8_t mget_read(memcached_st *memc)
1213 {
1214 memcached_return rc;
1215
1216 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1217 assert(rc == MEMCACHED_SUCCESS);
1218 /* Turn this into a help function */
1219 {
1220 char return_key[MEMCACHED_MAX_KEY];
1221 size_t return_key_length;
1222 char *return_value;
1223 size_t return_value_length;
1224 uint16_t flags;
1225
1226 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1227 &return_value_length, &flags, &rc)))
1228 {
1229 assert(return_value);
1230 assert(rc == MEMCACHED_SUCCESS);
1231 free(return_value);
1232 }
1233 }
1234
1235 return 0;
1236 }
1237
1238 uint8_t mget_read_result(memcached_st *memc)
1239 {
1240 memcached_return rc;
1241
1242 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1243 assert(rc == MEMCACHED_SUCCESS);
1244 /* Turn this into a help function */
1245 {
1246 memcached_result_st results_obj;
1247 memcached_result_st *results;
1248
1249 results= memcached_result_create(memc, &results_obj);
1250
1251 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1252 {
1253 assert(results);
1254 assert(rc == MEMCACHED_SUCCESS);
1255 }
1256
1257 memcached_result_free(&results_obj);
1258 }
1259
1260 return 0;
1261 }
1262
1263 uint8_t free_data(memcached_st *memc)
1264 {
1265 pairs_free(global_pairs);
1266
1267 return 0;
1268 }
1269
1270 uint8_t add_host_test1(memcached_st *memc)
1271 {
1272 unsigned int x;
1273 memcached_return rc;
1274 char servername[]= "0.example.com";
1275 memcached_server_st *servers;
1276
1277 servers= memcached_server_list_append(NULL, servername, 400, &rc);
1278 assert(servers);
1279 assert(1 == memcached_server_list_count(servers));
1280
1281 for (x= 2; x < 20; x++)
1282 {
1283 char buffer[SMALL_STRING_LEN];
1284
1285 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1286 servers= memcached_server_list_append(servers, buffer, 401,
1287 &rc);
1288 assert(rc == MEMCACHED_SUCCESS);
1289 assert(x == memcached_server_list_count(servers));
1290 }
1291
1292 rc= memcached_server_push(memc, servers);
1293 assert(rc == MEMCACHED_SUCCESS);
1294 rc= memcached_server_push(memc, servers);
1295 assert(rc == MEMCACHED_SUCCESS);
1296
1297 memcached_server_list_free(servers);
1298
1299 return 0;
1300 }
1301
1302 memcached_return pre_nonblock(memcached_st *memc)
1303 {
1304 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1305
1306 return MEMCACHED_SUCCESS;
1307 }
1308
1309 memcached_return pre_md5(memcached_st *memc)
1310 {
1311 memcached_hash value= MEMCACHED_HASH_MD5;
1312 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1313
1314 return MEMCACHED_SUCCESS;
1315 }
1316
1317 memcached_return pre_crc(memcached_st *memc)
1318 {
1319 memcached_hash value= MEMCACHED_HASH_CRC;
1320 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1321
1322 return MEMCACHED_SUCCESS;
1323 }
1324
1325 memcached_return pre_hash_fnv1_64(memcached_st *memc)
1326 {
1327 memcached_hash value= MEMCACHED_HASH_FNV1_64;
1328 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1329
1330 return MEMCACHED_SUCCESS;
1331 }
1332
1333 memcached_return pre_hash_fnv1a_64(memcached_st *memc)
1334 {
1335 memcached_hash value= MEMCACHED_HASH_FNV1A_64;
1336 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1337
1338 return MEMCACHED_SUCCESS;
1339 }
1340
1341 memcached_return pre_hash_fnv1_32(memcached_st *memc)
1342 {
1343 memcached_hash value= MEMCACHED_HASH_FNV1_32;
1344 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1345
1346 return MEMCACHED_SUCCESS;
1347 }
1348
1349 memcached_return pre_hash_fnv1a_32(memcached_st *memc)
1350 {
1351 memcached_hash value= MEMCACHED_HASH_FNV1A_32;
1352 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1353
1354 return MEMCACHED_SUCCESS;
1355 }
1356
1357 memcached_return pre_hash_ketama(memcached_st *memc)
1358 {
1359 memcached_hash value= MEMCACHED_HASH_KETAMA;
1360 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1361
1362 return MEMCACHED_SUCCESS;
1363 }
1364
1365 memcached_return pre_unix_socket(memcached_st *memc)
1366 {
1367 memcached_return rc;
1368 struct stat buf;
1369
1370 memcached_server_list_free(memc->hosts);
1371 memc->hosts= NULL;
1372 memc->number_of_hosts= 0;
1373
1374 if (stat("/tmp/memcached.socket", &buf))
1375 return MEMCACHED_FAILURE;
1376
1377 rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket");
1378
1379 return rc;
1380 }
1381
1382 memcached_return pre_nodelay(memcached_st *memc)
1383 {
1384 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1385 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
1386
1387 return MEMCACHED_SUCCESS;
1388 }
1389
1390
1391 /* Clean the server before beginning testing */
1392 test_st tests[] ={
1393 {"flush", 0, flush_test },
1394 {"init", 0, init_test },
1395 {"allocation", 0, allocation_test },
1396 {"clone_test", 0, clone_test },
1397 {"error", 0, error_test },
1398 {"set", 0, set_test },
1399 {"set2", 0, set_test2 },
1400 {"set3", 0, set_test3 },
1401 {"add", 0, add_test },
1402 {"replace", 0, replace_test },
1403 {"delete", 1, delete_test },
1404 {"get", 1, get_test },
1405 {"get2", 0, get_test2 },
1406 {"get3", 0, get_test3 },
1407 {"get4", 0, get_test4 },
1408 {"stats_servername", 0, stats_servername_test },
1409 {"increment", 0, increment_test },
1410 {"decrement", 0, decrement_test },
1411 {"quit", 0, quit_test },
1412 {"mget", 1, mget_test },
1413 {"mget_result", 1, mget_result_test },
1414 {"mget_result_alloc", 1, mget_result_alloc_test },
1415 {"get_stats", 0, get_stats },
1416 {"add_host_test", 0, add_host_test },
1417 {"get_stats_keys", 0, get_stats_keys },
1418 {"behavior_test", 0, get_stats_keys },
1419 {0, 0, 0}
1420 };
1421
1422 test_st string_tests[] ={
1423 {"string static with null", 0, string_static_null },
1424 {"string alloc with null", 0, string_alloc_null },
1425 {"string alloc with 1K", 0, string_alloc_with_size },
1426 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
1427 {"string append", 0, string_alloc_append },
1428 {"string append failure (too big)", 0, string_alloc_append_toobig },
1429 {0, 0, 0}
1430 };
1431
1432 test_st result_tests[] ={
1433 {"result static", 0, result_static},
1434 {"result alloc", 0, result_alloc},
1435 {0, 0, 0}
1436 };
1437
1438 test_st user_tests[] ={
1439 {"user_supplied_bug1", 0, user_supplied_bug1 },
1440 {"user_supplied_bug2", 0, user_supplied_bug2 },
1441 {"user_supplied_bug3", 0, user_supplied_bug3 },
1442 {"user_supplied_bug4", 0, user_supplied_bug4 },
1443 {"user_supplied_bug5", 1, user_supplied_bug5 },
1444 {"user_supplied_bug6", 1, user_supplied_bug6 },
1445 {0, 0, 0}
1446 };
1447
1448 test_st generate_tests[] ={
1449 {"generate_data", 0, generate_data },
1450 {"get_read", 0, get_read },
1451 {"mget_read", 0, mget_read },
1452 {"mget_read_result", 0, mget_read_result },
1453 {0, 0, 0}
1454 };
1455
1456
1457 collection_st collection[] ={
1458 {"block", 0, 0, tests},
1459 {"nonblock", pre_nonblock, 0, tests},
1460 {"nodelay", pre_nodelay, 0, tests},
1461 {"md5", pre_md5, 0, tests},
1462 {"crc", pre_crc, 0, tests},
1463 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
1464 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
1465 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
1466 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
1467 {"ketama", pre_hash_ketama, 0, tests},
1468 {"unix_socket", pre_unix_socket, 0, tests},
1469 {"unix_socket_nodelay", pre_nodelay, 0, tests},
1470 {"string", 0, 0, string_tests},
1471 {"result", 0, 0, result_tests},
1472 {"user", 0, 0, user_tests},
1473 {"generate", 0, 0, generate_tests},
1474 {0, 0, 0, 0}
1475 };
1476
1477 collection_st *gets_collections(void)
1478 {
1479 return collection;
1480 }