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