fa739863248ed5045904a75c23c311e44ea81723
[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 #ifndef INT64_MAX
19 #define INT64_MAX LONG_MAX
20 #endif
21 #ifndef INT32_MAX
22 #define INT32_MAX INT_MAX
23 #endif
24
25
26 #include "test.h"
27
28 #define GLOBAL_COUNT 100000
29 #define GLOBAL2_COUNT 1000
30 static uint32_t global_count;
31
32 static pairs_st *global_pairs;
33 static char *global_keys[GLOBAL_COUNT];
34 static size_t global_keys_length[GLOBAL_COUNT];
35
36 uint8_t init_test(memcached_st *not_used)
37 {
38 memcached_st memc;
39
40 (void)memcached_create(&memc);
41 memcached_free(&memc);
42
43 return 0;
44 }
45
46 uint8_t server_list_null_test(memcached_st *ptr)
47 {
48 memcached_server_st *server_list;
49 memcached_return rc;
50
51 server_list= memcached_server_list_append(NULL, NULL, 0, NULL);
52 assert(server_list == NULL);
53
54 server_list= memcached_server_list_append(NULL, "localhost", 0, NULL);
55 assert(server_list == NULL);
56
57 server_list= memcached_server_list_append(NULL, NULL, 0, &rc);
58 assert(server_list == NULL);
59
60 return 0;
61 }
62
63 uint8_t allocation_test(memcached_st *not_used)
64 {
65 memcached_st *memc;
66 memc= memcached_create(NULL);
67 assert(memc);
68 memcached_free(memc);
69
70 return 0;
71 }
72
73 uint8_t clone_test(memcached_st *memc)
74 {
75 /* All null? */
76 {
77 memcached_st *clone;
78 clone= memcached_clone(NULL, NULL);
79 assert(clone);
80 memcached_free(clone);
81 }
82
83 /* Can we init from null? */
84 {
85 memcached_st *clone;
86 clone= memcached_clone(NULL, memc);
87 assert(clone);
88 memcached_free(clone);
89 }
90
91 /* Can we init from struct? */
92 {
93 memcached_st declared_clone;
94 memcached_st *clone;
95 clone= memcached_clone(&declared_clone, NULL);
96 assert(clone);
97 memcached_free(clone);
98 }
99
100 /* Can we init from struct? */
101 {
102 memcached_st declared_clone;
103 memcached_st *clone;
104 clone= memcached_clone(&declared_clone, memc);
105 assert(clone);
106 memcached_free(clone);
107 }
108
109 return 0;
110 }
111
112 uint8_t connection_test(memcached_st *memc)
113 {
114 memcached_return rc;
115
116 rc= memcached_server_add(memc, "localhost", 0);
117 assert(rc == MEMCACHED_SUCCESS);
118
119 return 0;
120 }
121
122 uint8_t error_test(memcached_st *memc)
123 {
124 memcached_return rc;
125
126 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
127 {
128 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
129 }
130
131 return 0;
132 }
133
134 uint8_t set_test(memcached_st *memc)
135 {
136 memcached_return rc;
137 char *key= "foo";
138 char *value= "when we sanitize";
139
140 rc= memcached_set(memc, key, strlen(key),
141 value, strlen(value),
142 (time_t)0, (uint32_t)0);
143 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
144
145 return 0;
146 }
147
148 uint8_t append_test(memcached_st *memc)
149 {
150 memcached_return rc;
151 char *key= "fig";
152 char *value= "we";
153 size_t value_length;
154 uint32_t flags;
155
156 rc= memcached_flush(memc, 0);
157 assert(rc == MEMCACHED_SUCCESS);
158
159 rc= memcached_set(memc, key, strlen(key),
160 value, strlen(value),
161 (time_t)0, (uint32_t)0);
162 assert(rc == MEMCACHED_SUCCESS);
163
164 rc= memcached_append(memc, key, strlen(key),
165 " the", strlen(" the"),
166 (time_t)0, (uint32_t)0);
167 assert(rc == MEMCACHED_SUCCESS);
168
169 rc= memcached_append(memc, key, strlen(key),
170 " people", strlen(" people"),
171 (time_t)0, (uint32_t)0);
172 assert(rc == MEMCACHED_SUCCESS);
173
174 value= memcached_get(memc, key, strlen(key),
175 &value_length, &flags, &rc);
176 assert(!memcmp(value, "we the people", strlen("we the people")));
177 assert(strlen("we the people") == value_length);
178 assert(rc == MEMCACHED_SUCCESS);
179 free(value);
180
181 return 0;
182 }
183
184 uint8_t append_binary_test(memcached_st *memc)
185 {
186 memcached_return rc;
187 char *key= "numbers";
188 unsigned int *store_ptr;
189 unsigned int store_list[] = { 23, 56, 499, 98, 32847, 0 };
190 char *value;
191 size_t value_length;
192 uint32_t flags;
193 unsigned int x;
194
195 rc= memcached_flush(memc, 0);
196 assert(rc == MEMCACHED_SUCCESS);
197
198 rc= memcached_set(memc,
199 key, strlen(key),
200 NULL, 0,
201 (time_t)0, (uint32_t)0);
202 assert(rc == MEMCACHED_SUCCESS);
203
204 for (x= 0; store_list[x] ; x++)
205 {
206 rc= memcached_append(memc,
207 key, strlen(key),
208 (char *)&store_list[x], sizeof(unsigned int),
209 (time_t)0, (uint32_t)0);
210 assert(rc == MEMCACHED_SUCCESS);
211 }
212
213 value= memcached_get(memc, key, strlen(key),
214 &value_length, &flags, &rc);
215 assert((value_length == (sizeof(unsigned int) * x)));
216 assert(rc == MEMCACHED_SUCCESS);
217
218 store_ptr= (unsigned int *)value;
219 x= 0;
220 while ((size_t)store_ptr < (size_t)(value + value_length))
221 {
222 assert(*store_ptr == store_list[x++]);
223 store_ptr++;
224 }
225 free(value);
226
227 return 0;
228 }
229
230 uint8_t cas2_test(memcached_st *memc)
231 {
232 memcached_return rc;
233 char *keys[]= {"fudge", "son", "food"};
234 size_t key_length[]= {5, 3, 4};
235 char *value= "we the people";
236 size_t value_length= strlen("we the people");
237 unsigned int x;
238 memcached_result_st results_obj;
239 memcached_result_st *results;
240 unsigned int set= 1;
241
242 rc= memcached_flush(memc, 0);
243 assert(rc == MEMCACHED_SUCCESS);
244
245 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, &set);
246
247 for (x= 0; x < 3; x++)
248 {
249 rc= memcached_set(memc, keys[x], key_length[x],
250 keys[x], key_length[x],
251 (time_t)50, (uint32_t)9);
252 assert(rc == MEMCACHED_SUCCESS);
253 }
254
255 rc= memcached_mget(memc, keys, key_length, 3);
256
257 results= memcached_result_create(memc, &results_obj);
258
259 results= memcached_fetch_result(memc, &results_obj, &rc);
260 assert(results);
261 assert(results->cas);
262 assert(rc == MEMCACHED_SUCCESS);
263 WATCHPOINT_ASSERT(memcached_result_cas(results));
264
265 assert(!memcmp(value, "we the people", strlen("we the people")));
266 assert(strlen("we the people") == value_length);
267 assert(rc == MEMCACHED_SUCCESS);
268
269 memcached_result_free(&results_obj);
270
271 return 0;
272 }
273
274 uint8_t cas_test(memcached_st *memc)
275 {
276 memcached_return rc;
277 char *key= "fun";
278 size_t key_length= strlen("fun");
279 char *value= "we the people";
280 size_t value_length= strlen("we the people");
281 memcached_result_st results_obj;
282 memcached_result_st *results;
283 unsigned int set= 1;
284
285 rc= memcached_flush(memc, 0);
286 assert(rc == MEMCACHED_SUCCESS);
287
288 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, &set);
289
290 rc= memcached_set(memc, key, strlen(key),
291 value, strlen(value),
292 (time_t)0, (uint32_t)0);
293 assert(rc == MEMCACHED_SUCCESS);
294
295 rc= memcached_mget(memc, &key, &key_length, 1);
296
297 results= memcached_result_create(memc, &results_obj);
298
299 results= memcached_fetch_result(memc, &results_obj, &rc);
300 assert(results);
301 assert(rc == MEMCACHED_SUCCESS);
302 WATCHPOINT_ASSERT(memcached_result_cas(results));
303
304 assert(!memcmp(value, "we the people", strlen("we the people")));
305 assert(strlen("we the people") == value_length);
306 assert(rc == MEMCACHED_SUCCESS);
307
308 memcached_result_free(&results_obj);
309
310 return 0;
311 }
312
313 uint8_t prepend_test(memcached_st *memc)
314 {
315 memcached_return rc;
316 char *key= "fig";
317 char *value= "people";
318 size_t value_length;
319 uint32_t flags;
320
321 rc= memcached_flush(memc, 0);
322 assert(rc == MEMCACHED_SUCCESS);
323
324 rc= memcached_set(memc, key, strlen(key),
325 value, strlen(value),
326 (time_t)0, (uint32_t)0);
327 assert(rc == MEMCACHED_SUCCESS);
328
329 rc= memcached_prepend(memc, key, strlen(key),
330 "the ", strlen("the "),
331 (time_t)0, (uint32_t)0);
332 assert(rc == MEMCACHED_SUCCESS);
333
334 rc= memcached_prepend(memc, key, strlen(key),
335 "we ", strlen("we "),
336 (time_t)0, (uint32_t)0);
337 assert(rc == MEMCACHED_SUCCESS);
338
339 value= memcached_get(memc, key, strlen(key),
340 &value_length, &flags, &rc);
341 assert(!memcmp(value, "we the people", strlen("we the people")));
342 assert(strlen("we the people") == value_length);
343 assert(rc == MEMCACHED_SUCCESS);
344 free(value);
345
346 return 0;
347 }
348
349 /*
350 Set the value, then quit to make sure it is flushed.
351 Come back in and test that add fails.
352 */
353 uint8_t add_test(memcached_st *memc)
354 {
355 memcached_return rc;
356 char *key= "foo";
357 char *value= "when we sanitize";
358
359 rc= memcached_set(memc, key, strlen(key),
360 value, strlen(value),
361 (time_t)0, (uint32_t)0);
362 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
363 memcached_quit(memc);
364 rc= memcached_add(memc, key, strlen(key),
365 value, strlen(value),
366 (time_t)0, (uint32_t)0);
367 assert(rc == MEMCACHED_NOTSTORED);
368
369 return 0;
370 }
371
372 uint8_t add_wrapper(memcached_st *memc)
373 {
374 unsigned int x;
375
376 for (x= 0; x < 10000; x++)
377 add_test(memc);
378
379 return 0;
380 }
381
382 uint8_t replace_test(memcached_st *memc)
383 {
384 memcached_return rc;
385 char *key= "foo";
386 char *value= "when we sanitize";
387
388 rc= memcached_replace(memc, key, strlen(key),
389 value, strlen(value),
390 (time_t)0, (uint32_t)0);
391 assert(rc == MEMCACHED_SUCCESS);
392
393 return 0;
394 }
395
396 uint8_t delete_test(memcached_st *memc)
397 {
398 memcached_return rc;
399 char *key= "foo";
400 char *value= "when we sanitize";
401
402 rc= memcached_set(memc, key, strlen(key),
403 value, strlen(value),
404 (time_t)0, (uint32_t)0);
405 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
406
407 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
408 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
409
410 return 0;
411 }
412
413 uint8_t flush_test(memcached_st *memc)
414 {
415 memcached_return rc;
416
417 rc= memcached_flush(memc, 0);
418 assert(rc == MEMCACHED_SUCCESS);
419
420 return 0;
421 }
422
423 uint8_t get_test(memcached_st *memc)
424 {
425 memcached_return rc;
426 char *key= "foo";
427 char *string;
428 size_t string_length;
429 uint32_t flags;
430
431 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
432 assert(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
433
434 string= memcached_get(memc, key, strlen(key),
435 &string_length, &flags, &rc);
436
437 assert(rc == MEMCACHED_NOTFOUND);
438 assert(string_length == 0);
439 assert(!string);
440
441 return 0;
442 }
443
444 uint8_t get_test2(memcached_st *memc)
445 {
446 memcached_return rc;
447 char *key= "foo";
448 char *value= "when we sanitize";
449 char *string;
450 size_t string_length;
451 uint32_t flags;
452
453 rc= memcached_set(memc, key, strlen(key),
454 value, strlen(value),
455 (time_t)0, (uint32_t)0);
456 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
457
458 string= memcached_get(memc, key, strlen(key),
459 &string_length, &flags, &rc);
460
461 assert(string);
462 assert(rc == MEMCACHED_SUCCESS);
463 assert(string_length == strlen(value));
464 assert(!memcmp(string, value, string_length));
465
466 free(string);
467
468 return 0;
469 }
470
471 uint8_t set_test2(memcached_st *memc)
472 {
473 memcached_return rc;
474 char *key= "foo";
475 char *value= "train in the brain";
476 size_t value_length= strlen(value);
477 unsigned int x;
478
479 for (x= 0; x < 10; x++)
480 {
481 rc= memcached_set(memc, key, strlen(key),
482 value, value_length,
483 (time_t)0, (uint32_t)0);
484 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
485 }
486
487 return 0;
488 }
489
490 uint8_t set_test3(memcached_st *memc)
491 {
492 memcached_return rc;
493 char *key= "foo";
494 char *value;
495 size_t value_length= 8191;
496 unsigned int x;
497
498 value = (char*)malloc(value_length);
499 assert(value);
500
501 for (x= 0; x < value_length; x++)
502 value[x] = (char) (x % 127);
503
504 for (x= 0; x < 1; x++)
505 {
506 rc= memcached_set(memc, key, strlen(key),
507 value, value_length,
508 (time_t)0, (uint32_t)0);
509 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
510 }
511
512 free(value);
513
514 return 0;
515 }
516
517 uint8_t get_test3(memcached_st *memc)
518 {
519 memcached_return rc;
520 char *key= "foo";
521 char *value;
522 size_t value_length= 8191;
523 char *string;
524 size_t string_length;
525 uint32_t flags;
526 int x;
527
528 value = (char*)malloc(value_length);
529 assert(value);
530
531 for (x= 0; x < value_length; x++)
532 value[x] = (char) (x % 127);
533
534 rc= memcached_set(memc, key, strlen(key),
535 value, value_length,
536 (time_t)0, (uint32_t)0);
537 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
538
539 string= memcached_get(memc, key, strlen(key),
540 &string_length, &flags, &rc);
541
542 assert(rc == MEMCACHED_SUCCESS);
543 assert(string);
544 assert(string_length == value_length);
545 assert(!memcmp(string, value, string_length));
546
547 free(string);
548 free(value);
549
550 return 0;
551 }
552
553 uint8_t get_test4(memcached_st *memc)
554 {
555 memcached_return rc;
556 char *key= "foo";
557 char *value;
558 size_t value_length= 8191;
559 char *string;
560 size_t string_length;
561 uint32_t flags;
562 int x;
563
564 value = (char*)malloc(value_length);
565 assert(value);
566
567 for (x= 0; x < value_length; x++)
568 value[x] = (char) (x % 127);
569
570 rc= memcached_set(memc, key, strlen(key),
571 value, value_length,
572 (time_t)0, (uint32_t)0);
573 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
574
575 for (x= 0; x < 10; x++)
576 {
577 string= memcached_get(memc, key, strlen(key),
578 &string_length, &flags, &rc);
579
580 assert(rc == MEMCACHED_SUCCESS);
581 assert(string);
582 assert(string_length == value_length);
583 assert(!memcmp(string, value, string_length));
584 free(string);
585 }
586
587 free(value);
588
589 return 0;
590 }
591
592 /* Do not copy the style of this code, I just access hosts to testthis function */
593 uint8_t stats_servername_test(memcached_st *memc)
594 {
595 memcached_return rc;
596 memcached_stat_st stat;
597 rc= memcached_stat_servername(&stat, NULL,
598 memc->hosts[0].hostname,
599 memc->hosts[0].port);
600
601 return 0;
602 }
603
604 uint8_t increment_test(memcached_st *memc)
605 {
606 uint64_t new_number;
607 memcached_return rc;
608 char *key= "number";
609 char *value= "0";
610
611 rc= memcached_set(memc, key, strlen(key),
612 value, strlen(value),
613 (time_t)0, (uint32_t)0);
614 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
615
616 rc= memcached_increment(memc, key, strlen(key),
617 1, &new_number);
618 assert(rc == MEMCACHED_SUCCESS);
619 assert(new_number == 1);
620
621 rc= memcached_increment(memc, key, strlen(key),
622 1, &new_number);
623 assert(rc == MEMCACHED_SUCCESS);
624 assert(new_number == 2);
625
626 return 0;
627 }
628
629 uint8_t decrement_test(memcached_st *memc)
630 {
631 uint64_t new_number;
632 memcached_return rc;
633 char *key= "number";
634 char *value= "3";
635
636 rc= memcached_set(memc, key, strlen(key),
637 value, strlen(value),
638 (time_t)0, (uint32_t)0);
639 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
640
641 rc= memcached_decrement(memc, key, strlen(key),
642 1, &new_number);
643 assert(rc == MEMCACHED_SUCCESS);
644 assert(new_number == 2);
645
646 rc= memcached_decrement(memc, key, strlen(key),
647 1, &new_number);
648 assert(rc == MEMCACHED_SUCCESS);
649 assert(new_number == 1);
650
651 return 0;
652 }
653
654 uint8_t quit_test(memcached_st *memc)
655 {
656 memcached_return rc;
657 char *key= "fudge";
658 char *value= "sanford and sun";
659
660 rc= memcached_set(memc, key, strlen(key),
661 value, strlen(value),
662 (time_t)10, (uint32_t)3);
663 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
664 memcached_quit(memc);
665
666 rc= memcached_set(memc, key, strlen(key),
667 value, strlen(value),
668 (time_t)50, (uint32_t)9);
669 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
670
671 return 0;
672 }
673
674 uint8_t mget_result_test(memcached_st *memc)
675 {
676 memcached_return rc;
677 char *keys[]= {"fudge", "son", "food"};
678 size_t key_length[]= {5, 3, 4};
679 unsigned int x;
680
681 memcached_result_st results_obj;
682 memcached_result_st *results;
683
684 results= memcached_result_create(memc, &results_obj);
685 assert(results);
686 assert(&results_obj == results);
687
688 /* We need to empty the server before continueing test */
689 rc= memcached_flush(memc, 0);
690 assert(rc == MEMCACHED_SUCCESS);
691
692 rc= memcached_mget(memc, keys, key_length, 3);
693 assert(rc == MEMCACHED_SUCCESS);
694
695 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
696 {
697 assert(results);
698 }
699
700 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
701 assert(!results);
702 assert(rc == MEMCACHED_END);
703
704 for (x= 0; x < 3; x++)
705 {
706 rc= memcached_set(memc, keys[x], key_length[x],
707 keys[x], key_length[x],
708 (time_t)50, (uint32_t)9);
709 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
710 }
711
712 rc= memcached_mget(memc, keys, key_length, 3);
713 assert(rc == MEMCACHED_SUCCESS);
714
715 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
716 {
717 assert(results);
718 assert(&results_obj == results);
719 assert(rc == MEMCACHED_SUCCESS);
720 assert(memcached_result_key_length(results) == memcached_result_length(results));
721 assert(!memcmp(memcached_result_key_value(results),
722 memcached_result_value(results),
723 memcached_result_length(results)));
724 }
725
726 memcached_result_free(&results_obj);
727
728 return 0;
729 }
730
731 uint8_t mget_result_alloc_test(memcached_st *memc)
732 {
733 memcached_return rc;
734 char *keys[]= {"fudge", "son", "food"};
735 size_t key_length[]= {5, 3, 4};
736 unsigned int x;
737
738 memcached_result_st *results;
739
740 /* We need to empty the server before continueing test */
741 rc= memcached_flush(memc, 0);
742 assert(rc == MEMCACHED_SUCCESS);
743
744 rc= memcached_mget(memc, keys, key_length, 3);
745 assert(rc == MEMCACHED_SUCCESS);
746
747 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
748 {
749 assert(results);
750 }
751 assert(!results);
752 assert(rc == MEMCACHED_END);
753
754 for (x= 0; x < 3; x++)
755 {
756 rc= memcached_set(memc, keys[x], key_length[x],
757 keys[x], key_length[x],
758 (time_t)50, (uint32_t)9);
759 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
760 }
761
762 rc= memcached_mget(memc, keys, key_length, 3);
763 assert(rc == MEMCACHED_SUCCESS);
764
765 x= 0;
766 while ((results= memcached_fetch_result(memc, NULL, &rc)))
767 {
768 assert(results);
769 assert(rc == MEMCACHED_SUCCESS);
770 assert(memcached_result_key_length(results) == memcached_result_length(results));
771 assert(!memcmp(memcached_result_key_value(results),
772 memcached_result_value(results),
773 memcached_result_length(results)));
774 memcached_result_free(results);
775 x++;
776 }
777
778 return 0;
779 }
780
781 /* Count the results */
782 unsigned int callback_counter(memcached_st *ptr, memcached_result_st *result, void *context)
783 {
784 unsigned int *counter= (unsigned int *)context;
785
786 *counter= *counter + 1;
787
788 return 0;
789 }
790
791 uint8_t mget_result_function(memcached_st *memc)
792 {
793 memcached_return rc;
794 char *keys[]= {"fudge", "son", "food"};
795 size_t key_length[]= {5, 3, 4};
796 unsigned int x;
797 unsigned int counter;
798 unsigned int (*callbacks[1])(memcached_st *, memcached_result_st *, void *);
799
800 /* We need to empty the server before continueing test */
801 rc= memcached_flush(memc, 0);
802 for (x= 0; x < 3; x++)
803 {
804 rc= memcached_set(memc, keys[x], key_length[x],
805 keys[x], key_length[x],
806 (time_t)50, (uint32_t)9);
807 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
808 }
809
810 rc= memcached_mget(memc, keys, key_length, 3);
811 assert(rc == MEMCACHED_SUCCESS);
812
813 callbacks[0]= &callback_counter;
814 counter= 0;
815 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
816
817 assert(counter == 3);
818
819 return 0;
820 }
821
822 uint8_t mget_test(memcached_st *memc)
823 {
824 memcached_return rc;
825 char *keys[]= {"fudge", "son", "food"};
826 size_t key_length[]= {5, 3, 4};
827 unsigned int x;
828 uint32_t flags;
829
830 char return_key[MEMCACHED_MAX_KEY];
831 size_t return_key_length;
832 char *return_value;
833 size_t return_value_length;
834
835 /* We need to empty the server before continueing test */
836 rc= memcached_flush(memc, 0);
837 assert(rc == MEMCACHED_SUCCESS);
838
839 rc= memcached_mget(memc, keys, key_length, 3);
840 assert(rc == MEMCACHED_SUCCESS);
841
842 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
843 &return_value_length, &flags, &rc)) != NULL)
844 {
845 assert(return_value);
846 }
847 assert(!return_value);
848 assert(return_value_length == 0);
849 assert(rc == MEMCACHED_END);
850
851 for (x= 0; x < 3; x++)
852 {
853 rc= memcached_set(memc, keys[x], key_length[x],
854 keys[x], key_length[x],
855 (time_t)50, (uint32_t)9);
856 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
857 }
858
859 rc= memcached_mget(memc, keys, key_length, 3);
860 assert(rc == MEMCACHED_SUCCESS);
861
862 x= 0;
863 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
864 &return_value_length, &flags, &rc)))
865 {
866 assert(return_value);
867 assert(rc == MEMCACHED_SUCCESS);
868 assert(return_key_length == return_value_length);
869 assert(!memcmp(return_value, return_key, return_value_length));
870 free(return_value);
871 x++;
872 }
873
874 return 0;
875 }
876
877 uint8_t get_stats_keys(memcached_st *memc)
878 {
879 char **list;
880 char **ptr;
881 memcached_stat_st stat;
882 memcached_return rc;
883
884 list= memcached_stat_get_keys(memc, &stat, &rc);
885 assert(rc == MEMCACHED_SUCCESS);
886 for (ptr= list; *ptr; ptr++)
887 assert(*ptr);
888 fflush(stdout);
889
890 free(list);
891
892 return 0;
893 }
894
895 uint8_t version_string_test(memcached_st *memc)
896 {
897 const char *version_string;
898
899 version_string= memcached_lib_version();
900
901 assert(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
902
903 return 0;
904 }
905
906 uint8_t get_stats(memcached_st *memc)
907 {
908 unsigned int x;
909 char **list;
910 char **ptr;
911 memcached_return rc;
912 memcached_stat_st *stat;
913
914 stat= memcached_stat(memc, NULL, &rc);
915 assert(rc == MEMCACHED_SUCCESS);
916
917 assert(rc == MEMCACHED_SUCCESS);
918 assert(stat);
919
920 for (x= 0; x < memcached_server_count(memc); x++)
921 {
922 list= memcached_stat_get_keys(memc, stat+x, &rc);
923 assert(rc == MEMCACHED_SUCCESS);
924 for (ptr= list; *ptr; ptr++);
925
926 free(list);
927 }
928
929 memcached_stat_free(NULL, stat);
930
931 return 0;
932 }
933
934 uint8_t add_host_test(memcached_st *memc)
935 {
936 unsigned int x;
937 memcached_server_st *servers;
938 memcached_return rc;
939 char servername[]= "0.example.com";
940
941 servers= memcached_server_list_append(NULL, servername, 400, &rc);
942 assert(servers);
943 assert(1 == memcached_server_list_count(servers));
944
945 for (x= 2; x < 20; x++)
946 {
947 char buffer[SMALL_STRING_LEN];
948
949 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
950 servers= memcached_server_list_append(servers, buffer, 401,
951 &rc);
952 assert(rc == MEMCACHED_SUCCESS);
953 assert(x == memcached_server_list_count(servers));
954 }
955
956 rc= memcached_server_push(memc, servers);
957 assert(rc == MEMCACHED_SUCCESS);
958 rc= memcached_server_push(memc, servers);
959 assert(rc == MEMCACHED_SUCCESS);
960
961 memcached_server_list_free(servers);
962
963 return 0;
964 }
965
966 memcached_return clone_test_callback(memcached_st *parent, memcached_st *clone)
967 {
968 return MEMCACHED_SUCCESS;
969 }
970
971 memcached_return cleanup_test_callback(memcached_st *ptr)
972 {
973 return MEMCACHED_SUCCESS;
974 }
975
976 uint8_t callback_test(memcached_st *memc)
977 {
978 /* Test User Data */
979 {
980 int x= 5;
981 int *test_ptr;
982 memcached_return rc;
983
984 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
985 assert(rc == MEMCACHED_SUCCESS);
986 test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
987 assert(*test_ptr == x);
988 }
989
990 /* Test Clone Callback */
991 {
992 memcached_clone_func temp_function;
993 memcached_return rc;
994
995 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, clone_test_callback);
996 assert(rc == MEMCACHED_SUCCESS);
997 temp_function= (memcached_clone_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
998 assert(temp_function == clone_test_callback);
999 }
1000
1001 /* Test Cleanup Callback */
1002 {
1003 memcached_cleanup_func temp_function;
1004 memcached_return rc;
1005
1006 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, cleanup_test_callback);
1007 assert(rc == MEMCACHED_SUCCESS);
1008 temp_function= (memcached_cleanup_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1009 assert(temp_function == cleanup_test_callback);
1010 }
1011
1012 return 0;
1013 }
1014
1015 /* We don't test the behavior itself, we test the switches */
1016 uint8_t behavior_test(memcached_st *memc)
1017 {
1018 unsigned long long value;
1019 unsigned int set= 1;
1020
1021 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
1022 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1023 assert(value == 1);
1024
1025 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
1026 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1027 assert(value == 1);
1028
1029 set= MEMCACHED_HASH_MD5;
1030 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
1031 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1032 assert(value == MEMCACHED_HASH_MD5);
1033
1034 set= 0;
1035
1036 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
1037 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1038 assert(value == 0);
1039
1040 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
1041 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1042 assert(value == 0);
1043
1044 set= MEMCACHED_HASH_DEFAULT;
1045 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
1046 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1047 assert(value == MEMCACHED_HASH_DEFAULT);
1048
1049 set= MEMCACHED_HASH_CRC;
1050 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
1051 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1052 assert(value == MEMCACHED_HASH_CRC);
1053
1054 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1055 assert(value > 0);
1056
1057 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1058 assert(value > 0);
1059
1060 return 0;
1061 }
1062
1063 /* Test case provided by Cal Haldenbrand */
1064 uint8_t user_supplied_bug1(memcached_st *memc)
1065 {
1066 unsigned int setter= 1;
1067 unsigned int x;
1068
1069 unsigned long long total= 0;
1070 int size= 0;
1071 char key[10];
1072 char randomstuff[6 * 1024];
1073 memcached_return rc;
1074
1075 memset(randomstuff, 0, 6 * 1024);
1076
1077 /* We just keep looking at the same values over and over */
1078 srandom(10);
1079
1080 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
1081 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
1082
1083
1084 /* add key */
1085 for (x= 0 ; total < 20 * 1024576 ; x++ )
1086 {
1087 unsigned int j= 0;
1088
1089 size= (rand() % ( 5 * 1024 ) ) + 400;
1090 memset(randomstuff, 0, 6 * 1024);
1091 assert(size < 6 * 1024); /* Being safe here */
1092
1093 for (j= 0 ; j < size ;j++)
1094 randomstuff[j] = (char) (rand() % 26) + 97;
1095
1096 total += size;
1097 sprintf(key, "%d", x);
1098 rc = memcached_set(memc, key, strlen(key),
1099 randomstuff, strlen(randomstuff), 10, 0);
1100 /* If we fail, lets try again */
1101 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
1102 rc = memcached_set(memc, key, strlen(key),
1103 randomstuff, strlen(randomstuff), 10, 0);
1104 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1105 }
1106
1107 return 0;
1108 }
1109
1110 /* Test case provided by Cal Haldenbrand */
1111 uint8_t user_supplied_bug2(memcached_st *memc)
1112 {
1113 int errors;
1114 unsigned int setter;
1115 unsigned int x;
1116 unsigned long long total;
1117
1118 setter= 1;
1119 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
1120 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
1121 #ifdef NOT_YET
1122 setter = 20 * 1024576;
1123 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
1124 setter = 20 * 1024576;
1125 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
1126 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1127 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1128
1129 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
1130 #endif
1131
1132 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
1133 {
1134 memcached_return rc= MEMCACHED_SUCCESS;
1135 char buffer[SMALL_STRING_LEN];
1136 uint32_t flags= 0;
1137 size_t val_len= 0;
1138 char *getval;
1139
1140 memset(buffer, 0, SMALL_STRING_LEN);
1141
1142 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
1143 getval= memcached_get(memc, buffer, strlen(buffer),
1144 &val_len, &flags, &rc);
1145 if (rc != MEMCACHED_SUCCESS)
1146 {
1147 if (rc == MEMCACHED_NOTFOUND)
1148 errors++;
1149 else
1150 {
1151 WATCHPOINT_ERROR(rc);
1152 assert(0);
1153 }
1154
1155 continue;
1156 }
1157 total+= val_len;
1158 errors= 0;
1159 free(getval);
1160 }
1161
1162 return 0;
1163 }
1164
1165 /* Do a large mget() over all the keys we think exist */
1166 #define KEY_COUNT 3000 // * 1024576
1167 uint8_t user_supplied_bug3(memcached_st *memc)
1168 {
1169 memcached_return rc;
1170 unsigned int setter;
1171 unsigned int x;
1172 char **keys;
1173 size_t key_lengths[KEY_COUNT];
1174
1175 setter= 1;
1176 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
1177 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
1178 #ifdef NOT_YET
1179 setter = 20 * 1024576;
1180 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
1181 setter = 20 * 1024576;
1182 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
1183 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1184 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1185 #endif
1186
1187 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
1188 assert(keys);
1189 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
1190 for (x= 0; x < KEY_COUNT; x++)
1191 {
1192 char buffer[30];
1193
1194 snprintf(buffer, 30, "%u", x);
1195 keys[x]= strdup(buffer);
1196 key_lengths[x]= strlen(keys[x]);
1197 }
1198
1199 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
1200 assert(rc == MEMCACHED_SUCCESS);
1201
1202 /* Turn this into a help function */
1203 {
1204 char return_key[MEMCACHED_MAX_KEY];
1205 size_t return_key_length;
1206 char *return_value;
1207 size_t return_value_length;
1208 uint32_t flags;
1209
1210 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1211 &return_value_length, &flags, &rc)))
1212 {
1213 assert(return_value);
1214 assert(rc == MEMCACHED_SUCCESS);
1215 free(return_value);
1216 }
1217 }
1218
1219 for (x= 0; x < KEY_COUNT; x++)
1220 free(keys[x]);
1221 free(keys);
1222
1223 return 0;
1224 }
1225
1226 /* Make sure we behave properly if server list has no values */
1227 uint8_t user_supplied_bug4(memcached_st *memc)
1228 {
1229 memcached_return rc;
1230 char *keys[]= {"fudge", "son", "food"};
1231 size_t key_length[]= {5, 3, 4};
1232 unsigned int x;
1233 uint32_t flags;
1234 char return_key[MEMCACHED_MAX_KEY];
1235 size_t return_key_length;
1236 char *return_value;
1237 size_t return_value_length;
1238
1239 /* Here we free everything before running a bunch of mget tests */
1240 {
1241 memcached_server_list_free(memc->hosts);
1242 memc->hosts= NULL;
1243 memc->number_of_hosts= 0;
1244 }
1245
1246
1247 /* We need to empty the server before continueing test */
1248 rc= memcached_flush(memc, 0);
1249 assert(rc == MEMCACHED_NO_SERVERS);
1250
1251 rc= memcached_mget(memc, keys, key_length, 3);
1252 assert(rc == MEMCACHED_NO_SERVERS);
1253
1254 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1255 &return_value_length, &flags, &rc)) != NULL)
1256 {
1257 assert(return_value);
1258 }
1259 assert(!return_value);
1260 assert(return_value_length == 0);
1261 assert(rc == MEMCACHED_NO_SERVERS);
1262
1263 for (x= 0; x < 3; x++)
1264 {
1265 rc= memcached_set(memc, keys[x], key_length[x],
1266 keys[x], key_length[x],
1267 (time_t)50, (uint32_t)9);
1268 assert(rc == MEMCACHED_NO_SERVERS);
1269 }
1270
1271 rc= memcached_mget(memc, keys, key_length, 3);
1272 assert(rc == MEMCACHED_NO_SERVERS);
1273
1274 x= 0;
1275 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1276 &return_value_length, &flags, &rc)))
1277 {
1278 assert(return_value);
1279 assert(rc == MEMCACHED_SUCCESS);
1280 assert(return_key_length == return_value_length);
1281 assert(!memcmp(return_value, return_key, return_value_length));
1282 free(return_value);
1283 x++;
1284 }
1285
1286 return 0;
1287 }
1288
1289 #define VALUE_SIZE_BUG5 1048064
1290 uint8_t user_supplied_bug5(memcached_st *memc)
1291 {
1292 memcached_return rc;
1293 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1294 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1295 char return_key[MEMCACHED_MAX_KEY];
1296 size_t return_key_length;
1297 char *value;
1298 size_t value_length;
1299 uint32_t flags;
1300 unsigned int count;
1301 unsigned int x;
1302 char insert_data[VALUE_SIZE_BUG5];
1303
1304 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1305 insert_data[x]= rand();
1306
1307 memcached_flush(memc, 0);
1308 value= memcached_get(memc, keys[0], key_length[0],
1309 &value_length, &flags, &rc);
1310 assert(value == NULL);
1311 rc= memcached_mget(memc, keys, key_length, 4);
1312
1313 count= 0;
1314 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1315 &value_length, &flags, &rc)))
1316 count++;
1317 assert(count == 0);
1318
1319 for (x= 0; x < 4; x++)
1320 {
1321 rc= memcached_set(memc, keys[x], key_length[x],
1322 insert_data, VALUE_SIZE_BUG5,
1323 (time_t)0, (uint32_t)0);
1324 assert(rc == MEMCACHED_SUCCESS);
1325 }
1326
1327 for (x= 0; x < 10; x++)
1328 {
1329 value= memcached_get(memc, keys[0], key_length[0],
1330 &value_length, &flags, &rc);
1331 assert(value);
1332 free(value);
1333
1334 rc= memcached_mget(memc, keys, key_length, 4);
1335 count= 0;
1336 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1337 &value_length, &flags, &rc)))
1338 {
1339 count++;
1340 free(value);
1341 }
1342 assert(count == 4);
1343 }
1344
1345 return 0;
1346 }
1347
1348 uint8_t user_supplied_bug6(memcached_st *memc)
1349 {
1350 memcached_return rc;
1351 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1352 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1353 char return_key[MEMCACHED_MAX_KEY];
1354 size_t return_key_length;
1355 char *value;
1356 size_t value_length;
1357 uint32_t flags;
1358 unsigned int count;
1359 unsigned int x;
1360 char insert_data[VALUE_SIZE_BUG5];
1361
1362 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1363 insert_data[x]= rand();
1364
1365 memcached_flush(memc, 0);
1366 value= memcached_get(memc, keys[0], key_length[0],
1367 &value_length, &flags, &rc);
1368 assert(value == NULL);
1369 assert(rc == MEMCACHED_NOTFOUND);
1370 rc= memcached_mget(memc, keys, key_length, 4);
1371 assert(rc == MEMCACHED_SUCCESS);
1372
1373 count= 0;
1374 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1375 &value_length, &flags, &rc)))
1376 count++;
1377 assert(count == 0);
1378 assert(rc == MEMCACHED_END);
1379
1380 for (x= 0; x < 4; x++)
1381 {
1382 rc= memcached_set(memc, keys[x], key_length[x],
1383 insert_data, VALUE_SIZE_BUG5,
1384 (time_t)0, (uint32_t)0);
1385 assert(rc == MEMCACHED_SUCCESS);
1386 }
1387
1388 for (x= 0; x < 2; x++)
1389 {
1390 value= memcached_get(memc, keys[0], key_length[0],
1391 &value_length, &flags, &rc);
1392 assert(value);
1393 free(value);
1394
1395 rc= memcached_mget(memc, keys, key_length, 4);
1396 assert(rc == MEMCACHED_SUCCESS);
1397 count= 3;
1398 /* We test for purge of partial complete fetches */
1399 for (count= 3; count; count--)
1400 {
1401 value= memcached_fetch(memc, return_key, &return_key_length,
1402 &value_length, &flags, &rc);
1403 assert(rc == MEMCACHED_SUCCESS);
1404 assert(!(memcmp(value, insert_data, value_length)));
1405 assert(value_length);
1406 free(value);
1407 }
1408 }
1409
1410 return 0;
1411 }
1412
1413 uint8_t user_supplied_bug8(memcached_st *memc)
1414 {
1415 memcached_return rc;
1416 memcached_st *mine;
1417 memcached_st *clone;
1418
1419 memcached_server_st *servers;
1420 char *server_list= "memcache1.memcache.bk.sapo.pt:11211, memcache1.memcache.bk.sapo.pt:11212, memcache1.memcache.bk.sapo.pt:11213, memcache1.memcache.bk.sapo.pt:11214, memcache2.memcache.bk.sapo.pt:11211, memcache2.memcache.bk.sapo.pt:11212, memcache2.memcache.bk.sapo.pt:11213, memcache2.memcache.bk.sapo.pt:11214";
1421
1422 servers= memcached_servers_parse(server_list);
1423 assert(servers);
1424
1425 mine= memcached_create(NULL);
1426 rc= memcached_server_push(mine, servers);
1427 assert(rc == MEMCACHED_SUCCESS);
1428 memcached_server_list_free(servers);
1429
1430 assert(mine);
1431 clone= memcached_clone(NULL, mine);
1432
1433 memcached_quit(mine);
1434 memcached_quit(clone);
1435
1436
1437 memcached_free(mine);
1438 memcached_free(clone);
1439
1440 return 0;
1441 }
1442
1443 /* Test flag store/retrieve */
1444 uint8_t user_supplied_bug7(memcached_st *memc)
1445 {
1446 memcached_return rc;
1447 char *keys= "036790384900";
1448 size_t key_length= strlen("036790384900");
1449 char return_key[MEMCACHED_MAX_KEY];
1450 size_t return_key_length;
1451 char *value;
1452 size_t value_length;
1453 uint32_t flags;
1454 unsigned int x;
1455 char insert_data[VALUE_SIZE_BUG5];
1456
1457 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1458 insert_data[x]= rand();
1459
1460 memcached_flush(memc, 0);
1461
1462 flags= 245;
1463 rc= memcached_set(memc, keys, key_length,
1464 insert_data, VALUE_SIZE_BUG5,
1465 (time_t)0, flags);
1466 assert(rc == MEMCACHED_SUCCESS);
1467
1468 flags= 0;
1469 value= memcached_get(memc, keys, key_length,
1470 &value_length, &flags, &rc);
1471 assert(flags == 245);
1472 assert(value);
1473 free(value);
1474
1475 rc= memcached_mget(memc, &keys, &key_length, 1);
1476
1477 flags= 0;
1478 value= memcached_fetch(memc, return_key, &return_key_length,
1479 &value_length, &flags, &rc);
1480 assert(flags == 245);
1481 assert(value);
1482 free(value);
1483
1484
1485 return 0;
1486 }
1487
1488 uint8_t user_supplied_bug9(memcached_st *memc)
1489 {
1490 memcached_return rc;
1491 char *keys[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1492 size_t key_length[3];
1493 unsigned int x;
1494 uint32_t flags;
1495 unsigned count= 0;
1496
1497 char return_key[MEMCACHED_MAX_KEY];
1498 size_t return_key_length;
1499 char *return_value;
1500 size_t return_value_length;
1501
1502
1503 key_length[0]= strlen("UDATA:edevil@sapo.pt");
1504 key_length[1]= strlen("fudge&*@#");
1505 key_length[2]= strlen("for^#@&$not");
1506
1507
1508 for (x= 0; x < 3; x++)
1509 {
1510 rc= memcached_set(memc, keys[x], key_length[x],
1511 keys[x], key_length[x],
1512 (time_t)50, (uint32_t)9);
1513 assert(rc == MEMCACHED_SUCCESS);
1514 }
1515
1516 rc= memcached_mget(memc, keys, key_length, 3);
1517 assert(rc == MEMCACHED_SUCCESS);
1518
1519 /* We need to empty the server before continueing test */
1520 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1521 &return_value_length, &flags, &rc)) != NULL)
1522 {
1523 assert(return_value);
1524 free(return_value);
1525 count++;
1526 }
1527 assert(count == 3);
1528
1529 return 0;
1530 }
1531
1532 /* We are testing with aggressive timeout to get failures */
1533 uint8_t user_supplied_bug10(memcached_st *memc)
1534 {
1535 char *key= "foo";
1536 char *value;
1537 size_t value_length= 512;
1538 unsigned int x;
1539 int key_len= 3;
1540 memcached_return rc;
1541 unsigned int set= 1;
1542 memcached_st *mclone= memcached_clone(NULL, memc);
1543 int32_t timeout;
1544
1545 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
1546 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
1547 timeout= 2;
1548 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, &timeout);
1549
1550 value = (char*)malloc(value_length * sizeof(char));
1551
1552 for (x= 0; x < value_length; x++)
1553 value[x]= (char) (x % 127);
1554
1555 for (x= 1; x <= 100000; ++x)
1556 {
1557 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
1558
1559 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_BUFFERED);
1560
1561 if (rc == MEMCACHED_WRITE_FAILURE)
1562 x--;
1563 }
1564
1565 free(value);
1566 memcached_free(mclone);
1567
1568 return 0;
1569 }
1570
1571 /*
1572 We are looking failures in the async protocol
1573 */
1574 uint8_t user_supplied_bug11(memcached_st *memc)
1575 {
1576 char *key= "foo";
1577 char *value;
1578 size_t value_length= 512;
1579 unsigned int x;
1580 int key_len= 3;
1581 memcached_return rc;
1582 unsigned int set= 1;
1583 int32_t timeout;
1584 memcached_st *mclone= memcached_clone(NULL, memc);
1585
1586 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
1587 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
1588 timeout= -1;
1589 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, &timeout);
1590
1591 timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
1592
1593 assert(timeout == -1);
1594
1595 value = (char*)malloc(value_length * sizeof(char));
1596
1597 for (x= 0; x < value_length; x++)
1598 value[x]= (char) (x % 127);
1599
1600 for (x= 1; x <= 100000; ++x)
1601 {
1602 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
1603 }
1604
1605 free(value);
1606 memcached_free(mclone);
1607
1608 return 0;
1609 }
1610
1611 /*
1612 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1613 */
1614 uint8_t user_supplied_bug12(memcached_st *memc)
1615 {
1616 memcached_return rc;
1617 uint32_t flags;
1618 size_t value_length;
1619 char *value;
1620 uint64_t number_value;
1621
1622 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
1623 &value_length, &flags, &rc);
1624 assert(value == NULL);
1625 assert(rc == MEMCACHED_NOTFOUND);
1626
1627 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
1628 1, &number_value);
1629
1630 assert(value == NULL);
1631 assert(rc == MEMCACHED_NOTFOUND);
1632
1633 rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1634
1635 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
1636 &value_length, &flags, &rc);
1637 assert(value);
1638 assert(rc == MEMCACHED_SUCCESS);
1639 free(value);
1640
1641 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
1642 1, &number_value);
1643 assert(number_value == 2);
1644 assert(rc == MEMCACHED_SUCCESS);
1645
1646 return 0;
1647 }
1648
1649 /*
1650 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1651 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1652 */
1653 uint8_t user_supplied_bug13(memcached_st *memc)
1654 {
1655 char key[] = "key34567890";
1656 char *overflow;
1657 memcached_return rc;
1658 size_t overflowSize;
1659
1660 char commandFirst[]= "set key34567890 0 0 ";
1661 char commandLast[] = " \r\n"; /* first line of command sent to server */
1662 size_t commandLength;
1663 size_t testSize;
1664
1665 commandLength = strlen(commandFirst) + strlen(commandLast) + 4; /* 4 is number of characters in size, probably 8196 */
1666
1667 overflowSize = MEMCACHED_MAX_BUFFER - commandLength;
1668
1669 for (testSize= overflowSize - 1; testSize < overflowSize + 1; testSize++)
1670 {
1671 overflow= malloc(testSize);
1672 assert(overflow != NULL);
1673
1674 memset(overflow, 'x', testSize);
1675 rc= memcached_set(memc, key, strlen(key),
1676 overflow, testSize, 0, 0);
1677 assert(rc == MEMCACHED_SUCCESS);
1678 free(overflow);
1679 }
1680
1681 return 0;
1682 }
1683
1684
1685 /*
1686 Test values of many different sizes
1687 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1688 set key34567890 0 0 8169 \r\n
1689 is sent
1690 followed by buffer of size 8169, followed by 8169
1691 */
1692 uint8_t user_supplied_bug14(memcached_st *memc)
1693 {
1694 int setter= 1;
1695 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
1696 memcached_return rc;
1697 char *key= "foo";
1698 char *value;
1699 size_t value_length= 18000;
1700 char *string;
1701 size_t string_length;
1702 uint32_t flags;
1703 unsigned int x;
1704 size_t current_length;
1705
1706 value = (char*)malloc(value_length);
1707 assert(value);
1708
1709 for (x= 0; x < value_length; x++)
1710 value[x] = (char) (x % 127);
1711
1712 for (current_length = 0; current_length < value_length; current_length++)
1713 {
1714 rc= memcached_set(memc, key, strlen(key),
1715 value, current_length,
1716 (time_t)0, (uint32_t)0);
1717 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1718
1719 string= memcached_get(memc, key, strlen(key),
1720 &string_length, &flags, &rc);
1721
1722 assert(rc == MEMCACHED_SUCCESS);
1723 assert(string);
1724 assert(string_length == current_length);
1725 assert(!memcmp(string, value, string_length));
1726
1727 free(string);
1728 }
1729
1730 free(value);
1731
1732 return 0;
1733 }
1734
1735 uint8_t result_static(memcached_st *memc)
1736 {
1737 memcached_result_st result;
1738 memcached_result_st *result_ptr;
1739
1740 result_ptr= memcached_result_create(memc, &result);
1741 assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
1742 assert(result_ptr);
1743 memcached_result_free(&result);
1744
1745 return 0;
1746 }
1747
1748 uint8_t result_alloc(memcached_st *memc)
1749 {
1750 memcached_result_st *result;
1751
1752 result= memcached_result_create(memc, NULL);
1753 assert(result);
1754 memcached_result_free(result);
1755
1756 return 0;
1757 }
1758
1759 uint8_t string_static_null(memcached_st *memc)
1760 {
1761 memcached_string_st string;
1762 memcached_string_st *string_ptr;
1763
1764 string_ptr= memcached_string_create(memc, &string, 0);
1765 assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
1766 assert(string_ptr);
1767 memcached_string_free(&string);
1768
1769 return 0;
1770 }
1771
1772 uint8_t string_alloc_null(memcached_st *memc)
1773 {
1774 memcached_string_st *string;
1775
1776 string= memcached_string_create(memc, NULL, 0);
1777 assert(string);
1778 memcached_string_free(string);
1779
1780 return 0;
1781 }
1782
1783 uint8_t string_alloc_with_size(memcached_st *memc)
1784 {
1785 memcached_string_st *string;
1786
1787 string= memcached_string_create(memc, NULL, 1024);
1788 assert(string);
1789 memcached_string_free(string);
1790
1791 return 0;
1792 }
1793
1794 uint8_t string_alloc_with_size_toobig(memcached_st *memc)
1795 {
1796 memcached_string_st *string;
1797
1798 string= memcached_string_create(memc, NULL, INT64_MAX);
1799 assert(string == NULL);
1800
1801 return 0;
1802 }
1803
1804 uint8_t string_alloc_append(memcached_st *memc)
1805 {
1806 unsigned int x;
1807 char buffer[SMALL_STRING_LEN];
1808 memcached_string_st *string;
1809
1810 /* Ring the bell! */
1811 memset(buffer, 6, SMALL_STRING_LEN);
1812
1813 string= memcached_string_create(memc, NULL, 100);
1814 assert(string);
1815
1816 for (x= 0; x < 1024; x++)
1817 {
1818 memcached_return rc;
1819 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1820 assert(rc == MEMCACHED_SUCCESS);
1821 }
1822 memcached_string_free(string);
1823
1824 return 0;
1825 }
1826
1827 uint8_t string_alloc_append_toobig(memcached_st *memc)
1828 {
1829 memcached_return rc;
1830 unsigned int x;
1831 char buffer[SMALL_STRING_LEN];
1832 memcached_string_st *string;
1833
1834 /* Ring the bell! */
1835 memset(buffer, 6, SMALL_STRING_LEN);
1836
1837 string= memcached_string_create(memc, NULL, 100);
1838 assert(string);
1839
1840 for (x= 0; x < 1024; x++)
1841 {
1842 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1843 assert(rc == MEMCACHED_SUCCESS);
1844 }
1845 rc= memcached_string_append(string, buffer, INT64_MAX);
1846 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
1847 memcached_string_free(string);
1848
1849 return 0;
1850 }
1851
1852 uint8_t cleanup_pairs(memcached_st *memc)
1853 {
1854 pairs_free(global_pairs);
1855
1856 return 0;
1857 }
1858
1859 uint8_t generate_pairs(memcached_st *memc)
1860 {
1861 unsigned long long x;
1862 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
1863 global_count= GLOBAL_COUNT;
1864
1865 for (x= 0; x < global_count; x++)
1866 {
1867 global_keys[x]= global_pairs[x].key;
1868 global_keys_length[x]= global_pairs[x].key_length;
1869 }
1870
1871 return 0;
1872 }
1873
1874 uint8_t generate_large_pairs(memcached_st *memc)
1875 {
1876 unsigned long long x;
1877 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
1878 global_count= GLOBAL2_COUNT;
1879
1880 for (x= 0; x < global_count; x++)
1881 {
1882 global_keys[x]= global_pairs[x].key;
1883 global_keys_length[x]= global_pairs[x].key_length;
1884 }
1885
1886 return 0;
1887 }
1888
1889 uint8_t generate_data(memcached_st *memc)
1890 {
1891 execute_set(memc, global_pairs, global_count);
1892
1893 return 0;
1894 }
1895
1896 uint8_t generate_buffer_data(memcached_st *memc)
1897 {
1898 int latch= 0;
1899
1900 latch= 1;
1901 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, &latch);
1902 generate_data(memc);
1903
1904 return 0;
1905 }
1906
1907 uint8_t get_read(memcached_st *memc)
1908 {
1909 unsigned int x;
1910 memcached_return rc;
1911
1912 {
1913 char *return_value;
1914 size_t return_value_length;
1915 uint32_t flags;
1916
1917 for (x= 0; x < global_count; x++)
1918 {
1919 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
1920 &return_value_length, &flags, &rc);
1921 /*
1922 assert(return_value);
1923 assert(rc == MEMCACHED_SUCCESS);
1924 */
1925 if (rc == MEMCACHED_SUCCESS && return_value)
1926 free(return_value);
1927 }
1928 }
1929
1930 return 0;
1931 }
1932
1933 uint8_t mget_read(memcached_st *memc)
1934 {
1935 memcached_return rc;
1936
1937 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
1938 assert(rc == MEMCACHED_SUCCESS);
1939 /* Turn this into a help function */
1940 {
1941 char return_key[MEMCACHED_MAX_KEY];
1942 size_t return_key_length;
1943 char *return_value;
1944 size_t return_value_length;
1945 uint32_t flags;
1946
1947 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1948 &return_value_length, &flags, &rc)))
1949 {
1950 assert(return_value);
1951 assert(rc == MEMCACHED_SUCCESS);
1952 free(return_value);
1953 }
1954 }
1955
1956 return 0;
1957 }
1958
1959 uint8_t mget_read_result(memcached_st *memc)
1960 {
1961 memcached_return rc;
1962
1963 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
1964 assert(rc == MEMCACHED_SUCCESS);
1965 /* Turn this into a help function */
1966 {
1967 memcached_result_st results_obj;
1968 memcached_result_st *results;
1969
1970 results= memcached_result_create(memc, &results_obj);
1971
1972 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1973 {
1974 assert(results);
1975 assert(rc == MEMCACHED_SUCCESS);
1976 }
1977
1978 memcached_result_free(&results_obj);
1979 }
1980
1981 return 0;
1982 }
1983
1984 uint8_t mget_read_function(memcached_st *memc)
1985 {
1986 memcached_return rc;
1987 unsigned int counter;
1988 unsigned int (*callbacks[1])(memcached_st *, memcached_result_st *, void *);
1989
1990 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
1991 assert(rc == MEMCACHED_SUCCESS);
1992
1993 callbacks[0]= &callback_counter;
1994 counter= 0;
1995 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1996
1997 return 0;
1998 }
1999
2000 uint8_t delete_generate(memcached_st *memc)
2001 {
2002 unsigned int x;
2003
2004 for (x= 0; x < global_count; x++)
2005 {
2006 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2007 }
2008
2009 return 0;
2010 }
2011
2012 uint8_t delete_buffer_generate(memcached_st *memc)
2013 {
2014 int latch= 0;
2015 unsigned int x;
2016
2017 latch= 1;
2018 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, &latch);
2019
2020 for (x= 0; x < global_count; x++)
2021 {
2022 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2023 }
2024
2025 return 0;
2026 }
2027
2028 uint8_t free_data(memcached_st *memc)
2029 {
2030 pairs_free(global_pairs);
2031
2032 return 0;
2033 }
2034
2035 uint8_t add_host_test1(memcached_st *memc)
2036 {
2037 unsigned int x;
2038 memcached_return rc;
2039 char servername[]= "0.example.com";
2040 memcached_server_st *servers;
2041
2042 servers= memcached_server_list_append(NULL, servername, 400, &rc);
2043 assert(servers);
2044 assert(1 == memcached_server_list_count(servers));
2045
2046 for (x= 2; x < 20; x++)
2047 {
2048 char buffer[SMALL_STRING_LEN];
2049
2050 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
2051 servers= memcached_server_list_append(servers, buffer, 401,
2052 &rc);
2053 assert(rc == MEMCACHED_SUCCESS);
2054 assert(x == memcached_server_list_count(servers));
2055 }
2056
2057 rc= memcached_server_push(memc, servers);
2058 assert(rc == MEMCACHED_SUCCESS);
2059 rc= memcached_server_push(memc, servers);
2060 assert(rc == MEMCACHED_SUCCESS);
2061
2062 memcached_server_list_free(servers);
2063
2064 return 0;
2065 }
2066
2067 memcached_return pre_nonblock(memcached_st *memc)
2068 {
2069 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
2070
2071 return MEMCACHED_SUCCESS;
2072 }
2073
2074 memcached_return pre_md5(memcached_st *memc)
2075 {
2076 memcached_hash value= MEMCACHED_HASH_MD5;
2077 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2078
2079 return MEMCACHED_SUCCESS;
2080 }
2081
2082 memcached_return pre_crc(memcached_st *memc)
2083 {
2084 memcached_hash value= MEMCACHED_HASH_CRC;
2085 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2086
2087 return MEMCACHED_SUCCESS;
2088 }
2089
2090 memcached_return pre_hsieh(memcached_st *memc)
2091 {
2092 memcached_hash value= MEMCACHED_HASH_HSIEH;
2093 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2094
2095 return MEMCACHED_SUCCESS;
2096 }
2097
2098 memcached_return pre_hash_fnv1_64(memcached_st *memc)
2099 {
2100 memcached_hash value= MEMCACHED_HASH_FNV1_64;
2101 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2102
2103 return MEMCACHED_SUCCESS;
2104 }
2105
2106 memcached_return pre_hash_fnv1a_64(memcached_st *memc)
2107 {
2108 memcached_hash value= MEMCACHED_HASH_FNV1A_64;
2109 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2110
2111 return MEMCACHED_SUCCESS;
2112 }
2113
2114 memcached_return pre_hash_fnv1_32(memcached_st *memc)
2115 {
2116 memcached_hash value= MEMCACHED_HASH_FNV1_32;
2117 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2118
2119 return MEMCACHED_SUCCESS;
2120 }
2121
2122 memcached_return pre_hash_fnv1a_32(memcached_st *memc)
2123 {
2124 memcached_hash value= MEMCACHED_HASH_FNV1A_32;
2125 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2126
2127 return MEMCACHED_SUCCESS;
2128 }
2129
2130 memcached_return pre_hash_ketama(memcached_st *memc)
2131 {
2132 memcached_hash value= MEMCACHED_HASH_KETAMA;
2133 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
2134
2135 return MEMCACHED_SUCCESS;
2136 }
2137
2138 void my_free(memcached_st *ptr, void *mem)
2139 {
2140 free(mem);
2141 }
2142
2143 void *my_malloc(memcached_st *ptr, const size_t size)
2144 {
2145 return malloc(size);
2146 }
2147
2148 void *my_realloc(memcached_st *ptr, void *mem, const size_t size)
2149 {
2150 return realloc(mem, size);
2151 }
2152
2153 memcached_return set_memory_alloc(memcached_st *memc)
2154 {
2155 {
2156 memcached_malloc_function test_ptr;
2157 memcached_return rc;
2158
2159 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &my_malloc);
2160 assert(rc == MEMCACHED_SUCCESS);
2161 test_ptr= (memcached_malloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
2162 assert(rc == MEMCACHED_SUCCESS);
2163 assert(test_ptr == my_malloc);
2164 }
2165
2166 {
2167 memcached_realloc_function test_ptr;
2168 memcached_return rc;
2169
2170 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &my_realloc);
2171 assert(rc == MEMCACHED_SUCCESS);
2172 test_ptr= (memcached_realloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
2173 assert(rc == MEMCACHED_SUCCESS);
2174 assert(test_ptr == my_realloc);
2175 }
2176
2177 {
2178 memcached_free_function test_ptr;
2179 memcached_return rc;
2180
2181 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, my_free);
2182 assert(rc == MEMCACHED_SUCCESS);
2183 test_ptr= (memcached_free_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
2184 assert(rc == MEMCACHED_SUCCESS);
2185 assert(test_ptr == my_free);
2186 }
2187
2188 return MEMCACHED_SUCCESS;
2189 }
2190
2191 memcached_return enable_consistent(memcached_st *memc)
2192 {
2193 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
2194 memcached_hash hash;
2195 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, &value);
2196 pre_hsieh(memc);
2197
2198 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
2199 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
2200
2201 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
2202 assert(hash == MEMCACHED_HASH_HSIEH);
2203
2204
2205 return MEMCACHED_SUCCESS;
2206 }
2207
2208 memcached_return enable_cas(memcached_st *memc)
2209 {
2210 unsigned int set= 1;
2211
2212 memcached_version(memc);
2213
2214 if (memc->hosts[0].major_version >= 1 &&
2215 memc->hosts[0].minor_version >= 2 &&
2216 memc->hosts[0].micro_version >= 4)
2217 {
2218 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, &set);
2219
2220 return MEMCACHED_SUCCESS;
2221 }
2222
2223 return MEMCACHED_FAILURE;
2224 }
2225
2226 memcached_return check_for_1_2_3(memcached_st *memc)
2227 {
2228 memcached_version(memc);
2229
2230 if (memc->hosts[0].major_version >= 1 &&
2231 memc->hosts[0].minor_version >= 2 &&
2232 memc->hosts[0].micro_version >= 4)
2233 return MEMCACHED_SUCCESS;
2234
2235 return MEMCACHED_FAILURE;
2236 }
2237
2238 memcached_return pre_unix_socket(memcached_st *memc)
2239 {
2240 memcached_return rc;
2241 struct stat buf;
2242
2243 memcached_server_list_free(memc->hosts);
2244 memc->hosts= NULL;
2245 memc->number_of_hosts= 0;
2246
2247 if (stat("/tmp/memcached.socket", &buf))
2248 return MEMCACHED_FAILURE;
2249
2250 rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket");
2251
2252 return rc;
2253 }
2254
2255 memcached_return pre_udp(memcached_st *memc)
2256 {
2257 memcached_return rc;
2258
2259 memcached_server_list_free(memc->hosts);
2260 memc->hosts= NULL;
2261 memc->number_of_hosts= 0;
2262
2263 if (0)
2264 return MEMCACHED_FAILURE;
2265
2266 rc= memcached_server_add_udp(memc, "localhost", MEMCACHED_DEFAULT_PORT);
2267
2268 return rc;
2269 }
2270
2271 memcached_return pre_nodelay(memcached_st *memc)
2272 {
2273 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
2274 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
2275
2276 return MEMCACHED_SUCCESS;
2277 }
2278
2279 memcached_return poll_timeout(memcached_st *memc)
2280 {
2281 int32_t timeout;
2282
2283 timeout= 100;
2284
2285 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, &timeout);
2286
2287 timeout= (int32_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
2288
2289 assert(timeout == 100);
2290
2291 return MEMCACHED_SUCCESS;
2292 }
2293
2294
2295 /* Clean the server before beginning testing */
2296 test_st tests[] ={
2297 {"flush", 0, flush_test },
2298 {"init", 0, init_test },
2299 {"allocation", 0, allocation_test },
2300 {"server_list_null_test", 0, server_list_null_test},
2301 {"clone_test", 0, clone_test },
2302 {"error", 0, error_test },
2303 {"set", 0, set_test },
2304 {"set2", 0, set_test2 },
2305 {"set3", 0, set_test3 },
2306 {"add", 1, add_test },
2307 {"replace", 0, replace_test },
2308 {"delete", 1, delete_test },
2309 {"get", 1, get_test },
2310 {"get2", 0, get_test2 },
2311 {"get3", 0, get_test3 },
2312 {"get4", 0, get_test4 },
2313 {"stats_servername", 0, stats_servername_test },
2314 {"increment", 0, increment_test },
2315 {"decrement", 0, decrement_test },
2316 {"quit", 0, quit_test },
2317 {"mget", 1, mget_test },
2318 {"mget_result", 1, mget_result_test },
2319 {"mget_result_alloc", 1, mget_result_alloc_test },
2320 {"mget_result_function", 1, mget_result_function },
2321 {"get_stats", 0, get_stats },
2322 {"add_host_test", 0, add_host_test },
2323 {"get_stats_keys", 0, get_stats_keys },
2324 {"behavior_test", 0, get_stats_keys },
2325 {"callback_test", 0, get_stats_keys },
2326 {"version_string_test", 0, version_string_test},
2327 {0, 0, 0}
2328 };
2329
2330 test_st async_tests[] ={
2331 {"add", 1, add_wrapper },
2332 {0, 0, 0}
2333 };
2334
2335 test_st string_tests[] ={
2336 {"string static with null", 0, string_static_null },
2337 {"string alloc with null", 0, string_alloc_null },
2338 {"string alloc with 1K", 0, string_alloc_with_size },
2339 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
2340 {"string append", 0, string_alloc_append },
2341 {"string append failure (too big)", 0, string_alloc_append_toobig },
2342 {0, 0, 0}
2343 };
2344
2345 test_st result_tests[] ={
2346 {"result static", 0, result_static},
2347 {"result alloc", 0, result_alloc},
2348 {0, 0, 0}
2349 };
2350
2351 test_st version_1_2_3[] ={
2352 {"append", 0, append_test },
2353 {"prepend", 0, prepend_test },
2354 {"cas", 0, cas_test },
2355 {"cas2", 0, cas2_test },
2356 {"append_binary", 0, append_binary_test },
2357 {0, 0, 0}
2358 };
2359
2360 test_st user_tests[] ={
2361 {"user_supplied_bug1", 0, user_supplied_bug1 },
2362 {"user_supplied_bug2", 0, user_supplied_bug2 },
2363 {"user_supplied_bug3", 0, user_supplied_bug3 },
2364 {"user_supplied_bug4", 0, user_supplied_bug4 },
2365 {"user_supplied_bug5", 1, user_supplied_bug5 },
2366 {"user_supplied_bug6", 1, user_supplied_bug6 },
2367 {"user_supplied_bug7", 1, user_supplied_bug7 },
2368 {"user_supplied_bug8", 1, user_supplied_bug8 },
2369 {"user_supplied_bug9", 1, user_supplied_bug9 },
2370 {"user_supplied_bug10", 1, user_supplied_bug10 },
2371 {"user_supplied_bug11", 1, user_supplied_bug11 },
2372 {"user_supplied_bug12", 1, user_supplied_bug12 },
2373 {"user_supplied_bug13", 1, user_supplied_bug13 },
2374 {"user_supplied_bug14", 1, user_supplied_bug14 },
2375 {0, 0, 0}
2376 };
2377
2378 test_st generate_tests[] ={
2379 {"generate_pairs", 1, generate_pairs },
2380 {"generate_data", 1, generate_data },
2381 {"get_read", 0, get_read },
2382 {"delete_generate", 0, delete_generate },
2383 {"generate_buffer_data", 1, generate_buffer_data },
2384 {"delete_buffer", 0, delete_buffer_generate},
2385 {"generate_data", 1, generate_data },
2386 {"mget_read", 0, mget_read },
2387 {"mget_read_result", 0, mget_read_result },
2388 {"mget_read_function", 0, mget_read_function },
2389 {"cleanup", 1, cleanup_pairs },
2390 {"generate_large_pairs", 1, generate_large_pairs },
2391 {"generate_data", 1, generate_data },
2392 {"generate_buffer_data", 1, generate_buffer_data },
2393 {"cleanup", 1, cleanup_pairs },
2394 {0, 0, 0}
2395 };
2396
2397
2398 collection_st collection[] ={
2399 {"block", 0, 0, tests},
2400 {"nonblock", pre_nonblock, 0, tests},
2401 {"nodelay", pre_nodelay, 0, tests},
2402 {"md5", pre_md5, 0, tests},
2403 {"crc", pre_crc, 0, tests},
2404 {"hsieh", pre_hsieh, 0, tests},
2405 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
2406 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
2407 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
2408 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
2409 {"ketama", pre_hash_ketama, 0, tests},
2410 {"unix_socket", pre_unix_socket, 0, tests},
2411 {"unix_socket_nodelay", pre_nodelay, 0, tests},
2412 {"poll_timeout", poll_timeout, 0, tests},
2413 {"gets", enable_cas, 0, tests},
2414 {"consistent", enable_consistent, 0, tests},
2415 {"memory_allocators", set_memory_alloc, 0, tests},
2416 // {"udp", pre_udp, 0, tests},
2417 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
2418 {"string", 0, 0, string_tests},
2419 {"result", 0, 0, result_tests},
2420 {"async", pre_nonblock, 0, async_tests},
2421 {"user", 0, 0, user_tests},
2422 {"generate", 0, 0, generate_tests},
2423 {"generate_hsieh", pre_hsieh, 0, generate_tests},
2424 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
2425 {"generate_md5", pre_md5, 0, generate_tests},
2426 {"generate_nonblock", pre_nonblock, 0, generate_tests},
2427 {0, 0, 0, 0}
2428 };
2429
2430 collection_st *gets_collections(void)
2431 {
2432 return collection;
2433 }