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