Bugfix for memcached_connect() so that it will not always start up servers.
[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
649 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
650 assert(!results);
651 assert(rc == MEMCACHED_NOTFOUND);
652
653 for (x= 0; x < 3; x++)
654 {
655 rc= memcached_set(memc, keys[x], key_length[x],
656 keys[x], key_length[x],
657 (time_t)50, (uint16_t)9);
658 assert(rc == MEMCACHED_SUCCESS);
659 }
660
661 rc= memcached_mget(memc, keys, key_length, 3);
662 assert(rc == MEMCACHED_SUCCESS);
663
664 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
665 {
666 assert(results);
667 assert(&results_obj == results);
668 assert(rc == MEMCACHED_SUCCESS);
669 assert(memcached_result_key_length(results) == memcached_result_length(results));
670 assert(!memcmp(memcached_result_key_value(results),
671 memcached_result_value(results),
672 memcached_result_length(results)));
673 }
674
675 memcached_result_free(&results_obj);
676
677 return 0;
678 }
679
680 uint8_t mget_result_alloc_test(memcached_st *memc)
681 {
682 memcached_return rc;
683 char *keys[]= {"fudge", "son", "food"};
684 size_t key_length[]= {5, 3, 4};
685 unsigned int x;
686
687 memcached_result_st *results;
688
689 /* We need to empty the server before continueing test */
690 rc= memcached_flush(memc, 0);
691 assert(rc == MEMCACHED_SUCCESS);
692
693 rc= memcached_mget(memc, keys, key_length, 3);
694 assert(rc == MEMCACHED_SUCCESS);
695
696 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
697 {
698 assert(results);
699 }
700 assert(!results);
701 assert(rc == MEMCACHED_NOTFOUND);
702
703 for (x= 0; x < 3; x++)
704 {
705 rc= memcached_set(memc, keys[x], key_length[x],
706 keys[x], key_length[x],
707 (time_t)50, (uint16_t)9);
708 assert(rc == MEMCACHED_SUCCESS);
709 }
710
711 rc= memcached_mget(memc, keys, key_length, 3);
712 assert(rc == MEMCACHED_SUCCESS);
713
714 x= 0;
715 while ((results= memcached_fetch_result(memc, NULL, &rc)))
716 {
717 assert(results);
718 assert(rc == MEMCACHED_SUCCESS);
719 assert(memcached_result_key_length(results) == memcached_result_length(results));
720 assert(!memcmp(memcached_result_key_value(results),
721 memcached_result_value(results),
722 memcached_result_length(results)));
723 memcached_result_free(results);
724 x++;
725 }
726
727 return 0;
728 }
729
730 uint8_t mget_test(memcached_st *memc)
731 {
732 memcached_return rc;
733 char *keys[]= {"fudge", "son", "food"};
734 size_t key_length[]= {5, 3, 4};
735 unsigned int x;
736 uint16_t flags;
737
738 char return_key[MEMCACHED_MAX_KEY];
739 size_t return_key_length;
740 char *return_value;
741 size_t return_value_length;
742
743 /* We need to empty the server before continueing test */
744 rc= memcached_flush(memc, 0);
745 assert(rc == MEMCACHED_SUCCESS);
746
747 rc= memcached_mget(memc, keys, key_length, 3);
748 assert(rc == MEMCACHED_SUCCESS);
749
750 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
751 &return_value_length, &flags, &rc)) != NULL)
752 {
753 assert(return_value);
754 }
755 assert(!return_value);
756 assert(return_value_length == 0);
757 assert(rc == MEMCACHED_NOTFOUND);
758
759 for (x= 0; x < 3; x++)
760 {
761 rc= memcached_set(memc, keys[x], key_length[x],
762 keys[x], key_length[x],
763 (time_t)50, (uint16_t)9);
764 assert(rc == MEMCACHED_SUCCESS);
765 }
766
767 rc= memcached_mget(memc, keys, key_length, 3);
768 assert(rc == MEMCACHED_SUCCESS);
769
770 x= 0;
771 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
772 &return_value_length, &flags, &rc)))
773 {
774 assert(return_value);
775 assert(rc == MEMCACHED_SUCCESS);
776 assert(return_key_length == return_value_length);
777 assert(!memcmp(return_value, return_key, return_value_length));
778 free(return_value);
779 x++;
780 }
781
782 return 0;
783 }
784
785 uint8_t get_stats_keys(memcached_st *memc)
786 {
787 char **list;
788 char **ptr;
789 memcached_stat_st stat;
790 memcached_return rc;
791
792 list= memcached_stat_get_keys(memc, &stat, &rc);
793 assert(rc == MEMCACHED_SUCCESS);
794 for (ptr= list; *ptr; ptr++)
795 printf("Found key %s\n", *ptr);
796 fflush(stdout);
797
798 free(list);
799
800 return 0;
801 }
802
803 uint8_t get_stats(memcached_st *memc)
804 {
805 unsigned int x;
806 char **list;
807 char **ptr;
808 memcached_return rc;
809 memcached_stat_st *stat;
810
811 stat= memcached_stat(memc, NULL, &rc);
812 assert(rc == MEMCACHED_SUCCESS);
813
814 assert(rc == MEMCACHED_SUCCESS);
815 assert(stat);
816
817 for (x= 0; x < memcached_server_count(memc); x++)
818 {
819 list= memcached_stat_get_keys(memc, &stat[x], &rc);
820 assert(rc == MEMCACHED_SUCCESS);
821 for (ptr= list; *ptr; ptr++)
822 printf("Found key %s\n", *ptr);
823
824 free(list);
825 }
826
827 memcached_stat_free(NULL, stat);
828
829 return 0;
830 }
831
832 uint8_t add_host_test(memcached_st *memc)
833 {
834 unsigned int x;
835 memcached_server_st *servers;
836 memcached_return rc;
837 char servername[]= "0.example.com";
838
839 servers= memcached_server_list_append(NULL, servername, 400, &rc);
840 assert(servers);
841 assert(1 == memcached_server_list_count(servers));
842
843 for (x= 2; x < 20; x++)
844 {
845 char buffer[SMALL_STRING_LEN];
846
847 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
848 servers= memcached_server_list_append(servers, buffer, 401,
849 &rc);
850 assert(rc == MEMCACHED_SUCCESS);
851 assert(x == memcached_server_list_count(servers));
852 }
853
854 rc= memcached_server_push(memc, servers);
855 assert(rc == MEMCACHED_SUCCESS);
856 rc= memcached_server_push(memc, servers);
857 assert(rc == MEMCACHED_SUCCESS);
858
859 memcached_server_list_free(servers);
860
861 return 0;
862 }
863
864 /* We don't test the behavior itself, we test the switches */
865 uint8_t behavior_test(memcached_st *memc)
866 {
867 unsigned long long value;
868 unsigned int set= 1;
869
870 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
871 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
872 assert(value == 1);
873
874 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
875 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
876 assert(value == 1);
877
878 set= MEMCACHED_HASH_MD5;
879 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
880 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
881 assert(value == MEMCACHED_HASH_MD5);
882
883 set= 0;
884
885 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
886 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
887 assert(value == 0);
888
889 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
890 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
891 assert(value == 0);
892
893 set= MEMCACHED_HASH_DEFAULT;
894 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
895 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
896 assert(value == MEMCACHED_HASH_DEFAULT);
897
898 set= MEMCACHED_HASH_CRC;
899 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
900 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
901 assert(value == MEMCACHED_HASH_CRC);
902
903 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
904 assert(value > 0);
905
906 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
907 assert(value > 0);
908
909 return 0;
910 }
911
912 /* Test case provided by Cal Haldenbrand */
913 uint8_t user_supplied_bug1(memcached_st *memc)
914 {
915 unsigned int setter= 1;
916 unsigned int x;
917
918 unsigned long long total= 0;
919 int size= 0;
920 char key[10];
921 char randomstuff[6 * 1024];
922 memcached_return rc;
923
924 memset(randomstuff, 0, 6 * 1024);
925
926 /* We just keep looking at the same values over and over */
927 srandom(10);
928
929 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
930 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
931
932
933 /* add key */
934 for (x= 0 ; total < 20 * 1024576 ; x++ )
935 {
936 unsigned int j= 0;
937
938 size= (rand() % ( 5 * 1024 ) ) + 400;
939 memset(randomstuff, 0, 6 * 1024);
940 assert(size < 6 * 1024); /* Being safe here */
941
942 for (j= 0 ; j < size ;j++)
943 randomstuff[j] = (char) (rand() % 26) + 97;
944
945 total += size;
946 sprintf(key, "%d", x);
947 rc = memcached_set(memc, key, strlen(key),
948 randomstuff, strlen(randomstuff), 10, 0);
949 /* If we fail, lets try again */
950 if (rc != MEMCACHED_SUCCESS)
951 rc = memcached_set(memc, key, strlen(key),
952 randomstuff, strlen(randomstuff), 10, 0);
953 assert(rc == MEMCACHED_SUCCESS);
954 }
955
956 return 0;
957 }
958
959 /* Test case provided by Cal Haldenbrand */
960 uint8_t user_supplied_bug2(memcached_st *memc)
961 {
962 int errors;
963 unsigned int setter;
964 unsigned int x;
965 unsigned long long total;
966
967 setter= 1;
968 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
969 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
970 #ifdef NOT_YET
971 setter = 20 * 1024576;
972 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
973 setter = 20 * 1024576;
974 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
975 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
976 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
977
978 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
979 #endif
980
981 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
982 {
983 memcached_return rc= MEMCACHED_SUCCESS;
984 char buffer[SMALL_STRING_LEN];
985 uint16_t flags= 0;
986 size_t val_len= 0;
987 char *getval;
988
989 memset(buffer, 0, SMALL_STRING_LEN);
990
991 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
992 getval= memcached_get(memc, buffer, strlen(buffer),
993 &val_len, &flags, &rc);
994 if (rc != MEMCACHED_SUCCESS)
995 {
996 if (rc == MEMCACHED_NOTFOUND)
997 errors++;
998 else
999 assert(0);
1000
1001 continue;
1002 }
1003 total+= val_len;
1004 errors= 0;
1005 free(getval);
1006 }
1007
1008 return 0;
1009 }
1010
1011 /* Do a large mget() over all the keys we think exist */
1012 #define KEY_COUNT 3000 // * 1024576
1013 uint8_t user_supplied_bug3(memcached_st *memc)
1014 {
1015 memcached_return rc;
1016 unsigned int setter;
1017 unsigned int x;
1018 char **keys;
1019 size_t key_lengths[KEY_COUNT];
1020
1021 setter= 1;
1022 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
1023 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
1024 #ifdef NOT_YET
1025 setter = 20 * 1024576;
1026 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
1027 setter = 20 * 1024576;
1028 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
1029 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1030 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1031 #endif
1032
1033 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
1034 assert(keys);
1035 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
1036 for (x= 0; x < KEY_COUNT; x++)
1037 {
1038 char buffer[30];
1039
1040 snprintf(buffer, 30, "%u", x);
1041 keys[x]= strdup(buffer);
1042 key_lengths[x]= strlen(keys[x]);
1043 }
1044
1045 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
1046 assert(rc == MEMCACHED_SUCCESS);
1047
1048 /* Turn this into a help function */
1049 {
1050 char return_key[MEMCACHED_MAX_KEY];
1051 size_t return_key_length;
1052 char *return_value;
1053 size_t return_value_length;
1054 uint16_t flags;
1055
1056 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1057 &return_value_length, &flags, &rc)))
1058 {
1059 assert(return_value);
1060 assert(rc == MEMCACHED_SUCCESS);
1061 free(return_value);
1062 }
1063 }
1064
1065 for (x= 0; x < KEY_COUNT; x++)
1066 free(keys[x]);
1067 free(keys);
1068
1069 return 0;
1070 }
1071
1072 /* Make sure we behave properly if server list has no values */
1073 uint8_t user_supplied_bug4(memcached_st *memc)
1074 {
1075 memcached_return rc;
1076 char *keys[]= {"fudge", "son", "food"};
1077 size_t key_length[]= {5, 3, 4};
1078 unsigned int x;
1079 uint16_t flags;
1080
1081 /* Here we free everything before running a bunch of mget tests */
1082 {
1083 memcached_server_list_free(memc->hosts);
1084 memc->hosts= NULL;
1085 memc->number_of_hosts= 0;
1086 }
1087
1088 char return_key[MEMCACHED_MAX_KEY];
1089 size_t return_key_length;
1090 char *return_value;
1091 size_t return_value_length;
1092
1093 /* We need to empty the server before continueing test */
1094 rc= memcached_flush(memc, 0);
1095 assert(rc == MEMCACHED_NO_SERVERS);
1096
1097 rc= memcached_mget(memc, keys, key_length, 3);
1098 assert(rc == MEMCACHED_NO_SERVERS);
1099
1100 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1101 &return_value_length, &flags, &rc)) != NULL)
1102 {
1103 assert(return_value);
1104 }
1105 assert(!return_value);
1106 assert(return_value_length == 0);
1107 assert(rc == MEMCACHED_NO_SERVERS);
1108
1109 for (x= 0; x < 3; x++)
1110 {
1111 rc= memcached_set(memc, keys[x], key_length[x],
1112 keys[x], key_length[x],
1113 (time_t)50, (uint16_t)9);
1114 assert(rc == MEMCACHED_NO_SERVERS);
1115 }
1116
1117 rc= memcached_mget(memc, keys, key_length, 3);
1118 assert(rc == MEMCACHED_NO_SERVERS);
1119
1120 x= 0;
1121 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1122 &return_value_length, &flags, &rc)))
1123 {
1124 assert(return_value);
1125 assert(rc == MEMCACHED_SUCCESS);
1126 assert(return_key_length == return_value_length);
1127 assert(!memcmp(return_value, return_key, return_value_length));
1128 free(return_value);
1129 x++;
1130 }
1131
1132 return 0;
1133 }
1134
1135 #define VALUE_SIZE_BUG5 1048064
1136 uint8_t user_supplied_bug5(memcached_st *memc)
1137 {
1138 memcached_return rc;
1139 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1140 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1141 char return_key[MEMCACHED_MAX_KEY];
1142 size_t return_key_length;
1143 char *value;
1144 size_t value_length;
1145 uint16_t flags;
1146 unsigned int count;
1147 unsigned int x;
1148 char insert_data[VALUE_SIZE_BUG5];
1149
1150 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1151 insert_data[x]= rand();
1152
1153 memcached_flush(memc, 0);
1154 value= memcached_get(memc, keys[0], key_length[0],
1155 &value_length, &flags, &rc);
1156 assert(value == NULL);
1157 rc= memcached_mget(memc, keys, key_length, 4);
1158
1159 count= 0;
1160 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1161 &value_length, &flags, &rc)))
1162 count++;
1163 assert(count == 0);
1164
1165 for (x= 0; x < 4; x++)
1166 {
1167 rc= memcached_set(memc, keys[x], key_length[x],
1168 insert_data, VALUE_SIZE_BUG5,
1169 (time_t)0, (uint16_t)0);
1170 assert(rc == MEMCACHED_SUCCESS);
1171 }
1172
1173 for (x= 0; x < 10; x++)
1174 {
1175 value= memcached_get(memc, keys[0], key_length[0],
1176 &value_length, &flags, &rc);
1177 assert(value);
1178 free(value);
1179
1180 rc= memcached_mget(memc, keys, key_length, 4);
1181 count= 0;
1182 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1183 &value_length, &flags, &rc)))
1184 {
1185 count++;
1186 free(value);
1187 }
1188 assert(count == 4);
1189 }
1190
1191 return 0;
1192 }
1193
1194 uint8_t user_supplied_bug6(memcached_st *memc)
1195 {
1196 memcached_return rc;
1197 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1198 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1199 char return_key[MEMCACHED_MAX_KEY];
1200 size_t return_key_length;
1201 char *value;
1202 size_t value_length;
1203 uint16_t flags;
1204 unsigned int count;
1205 unsigned int x;
1206 char insert_data[VALUE_SIZE_BUG5];
1207
1208 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1209 insert_data[x]= rand();
1210
1211 memcached_flush(memc, 0);
1212 value= memcached_get(memc, keys[0], key_length[0],
1213 &value_length, &flags, &rc);
1214 assert(value == NULL);
1215 rc= memcached_mget(memc, keys, key_length, 4);
1216
1217 count= 0;
1218 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1219 &value_length, &flags, &rc)))
1220 count++;
1221 assert(count == 0);
1222
1223 for (x= 0; x < 4; x++)
1224 {
1225 rc= memcached_set(memc, keys[x], key_length[x],
1226 insert_data, VALUE_SIZE_BUG5,
1227 (time_t)0, (uint16_t)0);
1228 assert(rc == MEMCACHED_SUCCESS);
1229 }
1230
1231 for (x= 0; x < 10; x++)
1232 {
1233 value= memcached_get(memc, keys[0], key_length[0],
1234 &value_length, &flags, &rc);
1235 assert(value);
1236 free(value);
1237
1238 rc= memcached_mget(memc, keys, key_length, 4);
1239 count= 3;
1240 /* We test for purge of partial complete fetches */
1241 for (count= 3; count; count--)
1242 {
1243 value= memcached_fetch(memc, return_key, &return_key_length,
1244 &value_length, &flags, &rc);
1245 free(value);
1246 assert(rc == MEMCACHED_SUCCESS);
1247 }
1248 }
1249
1250 return 0;
1251 }
1252
1253 /* Test flag store/retrieve */
1254 uint8_t user_supplied_bug7(memcached_st *memc)
1255 {
1256 memcached_return rc;
1257 char *keys= "036790384900";
1258 size_t key_length= strlen("036790384900");
1259 char return_key[MEMCACHED_MAX_KEY];
1260 size_t return_key_length;
1261 char *value;
1262 size_t value_length;
1263 uint16_t flags;
1264 unsigned int x;
1265 char insert_data[VALUE_SIZE_BUG5];
1266
1267 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1268 insert_data[x]= rand();
1269
1270 memcached_flush(memc, 0);
1271
1272 flags= 245;
1273 rc= memcached_set(memc, keys, key_length,
1274 insert_data, VALUE_SIZE_BUG5,
1275 (time_t)0, flags);
1276 assert(rc == MEMCACHED_SUCCESS);
1277
1278 flags= 0;
1279 value= memcached_get(memc, keys, key_length,
1280 &value_length, &flags, &rc);
1281 assert(flags == 245);
1282 assert(value);
1283 free(value);
1284
1285 rc= memcached_mget(memc, &keys, &key_length, 1);
1286
1287 flags= 0;
1288 value= memcached_fetch(memc, return_key, &return_key_length,
1289 &value_length, &flags, &rc);
1290 assert(flags == 245);
1291 assert(value);
1292 free(value);
1293
1294
1295 return 0;
1296 }
1297
1298 uint8_t result_static(memcached_st *memc)
1299 {
1300 memcached_result_st result;
1301 memcached_result_st *result_ptr;
1302
1303 result_ptr= memcached_result_create(memc, &result);
1304 assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
1305 assert(result_ptr);
1306 memcached_result_free(&result);
1307
1308 return 0;
1309 }
1310
1311 uint8_t result_alloc(memcached_st *memc)
1312 {
1313 memcached_result_st *result;
1314
1315 result= memcached_result_create(memc, NULL);
1316 assert(result);
1317 memcached_result_free(result);
1318
1319 return 0;
1320 }
1321
1322 uint8_t string_static_null(memcached_st *memc)
1323 {
1324 memcached_string_st string;
1325 memcached_string_st *string_ptr;
1326
1327 string_ptr= memcached_string_create(memc, &string, 0);
1328 assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
1329 assert(string_ptr);
1330 memcached_string_free(&string);
1331
1332 return 0;
1333 }
1334
1335 uint8_t string_alloc_null(memcached_st *memc)
1336 {
1337 memcached_string_st *string;
1338
1339 string= memcached_string_create(memc, NULL, 0);
1340 assert(string);
1341 memcached_string_free(string);
1342
1343 return 0;
1344 }
1345
1346 uint8_t string_alloc_with_size(memcached_st *memc)
1347 {
1348 memcached_string_st *string;
1349
1350 string= memcached_string_create(memc, NULL, 1024);
1351 assert(string);
1352 memcached_string_free(string);
1353
1354 return 0;
1355 }
1356
1357 uint8_t string_alloc_with_size_toobig(memcached_st *memc)
1358 {
1359 memcached_string_st *string;
1360
1361 string= memcached_string_create(memc, NULL, INT64_MAX);
1362 assert(string == NULL);
1363
1364 return 0;
1365 }
1366
1367 uint8_t string_alloc_append(memcached_st *memc)
1368 {
1369 unsigned int x;
1370 char buffer[SMALL_STRING_LEN];
1371 memcached_string_st *string;
1372
1373 /* Ring the bell! */
1374 memset(buffer, 6, SMALL_STRING_LEN);
1375
1376 string= memcached_string_create(memc, NULL, 100);
1377 assert(string);
1378
1379 for (x= 0; x < 1024; x++)
1380 {
1381 memcached_return rc;
1382 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1383 assert(rc == MEMCACHED_SUCCESS);
1384 }
1385 memcached_string_free(string);
1386
1387 return 0;
1388 }
1389
1390 uint8_t string_alloc_append_toobig(memcached_st *memc)
1391 {
1392 memcached_return rc;
1393 unsigned int x;
1394 char buffer[SMALL_STRING_LEN];
1395 memcached_string_st *string;
1396
1397 /* Ring the bell! */
1398 memset(buffer, 6, SMALL_STRING_LEN);
1399
1400 string= memcached_string_create(memc, NULL, 100);
1401 assert(string);
1402
1403 for (x= 0; x < 1024; x++)
1404 {
1405 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1406 assert(rc == MEMCACHED_SUCCESS);
1407 }
1408 rc= memcached_string_append(string, buffer, INT64_MAX);
1409 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
1410 memcached_string_free(string);
1411
1412 return 0;
1413 }
1414
1415 uint8_t generate_data(memcached_st *memc)
1416 {
1417 unsigned long long x;
1418 global_pairs= pairs_generate(GLOBAL_COUNT);
1419 execute_set(memc, global_pairs, GLOBAL_COUNT);
1420
1421 for (x= 0; x < GLOBAL_COUNT; x++)
1422 {
1423 global_keys[x]= global_pairs[x].key;
1424 global_keys_length[x]= global_pairs[x].key_length;
1425 }
1426
1427 return 0;
1428 }
1429
1430 uint8_t get_read(memcached_st *memc)
1431 {
1432 unsigned int x;
1433 memcached_return rc;
1434
1435 {
1436 char *return_value;
1437 size_t return_value_length;
1438 uint16_t flags;
1439
1440 for (x= 0; x < GLOBAL_COUNT; x++)
1441 {
1442 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
1443 &return_value_length, &flags, &rc);
1444 /*
1445 assert(return_value);
1446 assert(rc == MEMCACHED_SUCCESS);
1447 */
1448 if (rc == MEMCACHED_SUCCESS && return_value)
1449 free(return_value);
1450 }
1451 }
1452
1453 return 0;
1454 }
1455
1456 uint8_t mget_read(memcached_st *memc)
1457 {
1458 memcached_return rc;
1459
1460 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1461 assert(rc == MEMCACHED_SUCCESS);
1462 /* Turn this into a help function */
1463 {
1464 char return_key[MEMCACHED_MAX_KEY];
1465 size_t return_key_length;
1466 char *return_value;
1467 size_t return_value_length;
1468 uint16_t flags;
1469
1470 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1471 &return_value_length, &flags, &rc)))
1472 {
1473 assert(return_value);
1474 assert(rc == MEMCACHED_SUCCESS);
1475 free(return_value);
1476 }
1477 }
1478
1479 return 0;
1480 }
1481
1482 uint8_t mget_read_result(memcached_st *memc)
1483 {
1484 memcached_return rc;
1485
1486 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1487 assert(rc == MEMCACHED_SUCCESS);
1488 /* Turn this into a help function */
1489 {
1490 memcached_result_st results_obj;
1491 memcached_result_st *results;
1492
1493 results= memcached_result_create(memc, &results_obj);
1494
1495 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1496 {
1497 assert(results);
1498 assert(rc == MEMCACHED_SUCCESS);
1499 }
1500
1501 memcached_result_free(&results_obj);
1502 }
1503
1504 return 0;
1505 }
1506
1507 uint8_t free_data(memcached_st *memc)
1508 {
1509 pairs_free(global_pairs);
1510
1511 return 0;
1512 }
1513
1514 uint8_t add_host_test1(memcached_st *memc)
1515 {
1516 unsigned int x;
1517 memcached_return rc;
1518 char servername[]= "0.example.com";
1519 memcached_server_st *servers;
1520
1521 servers= memcached_server_list_append(NULL, servername, 400, &rc);
1522 assert(servers);
1523 assert(1 == memcached_server_list_count(servers));
1524
1525 for (x= 2; x < 20; x++)
1526 {
1527 char buffer[SMALL_STRING_LEN];
1528
1529 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1530 servers= memcached_server_list_append(servers, buffer, 401,
1531 &rc);
1532 assert(rc == MEMCACHED_SUCCESS);
1533 assert(x == memcached_server_list_count(servers));
1534 }
1535
1536 rc= memcached_server_push(memc, servers);
1537 assert(rc == MEMCACHED_SUCCESS);
1538 rc= memcached_server_push(memc, servers);
1539 assert(rc == MEMCACHED_SUCCESS);
1540
1541 memcached_server_list_free(servers);
1542
1543 return 0;
1544 }
1545
1546 memcached_return pre_nonblock(memcached_st *memc)
1547 {
1548 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1549
1550 return MEMCACHED_SUCCESS;
1551 }
1552
1553 memcached_return pre_md5(memcached_st *memc)
1554 {
1555 memcached_hash value= MEMCACHED_HASH_MD5;
1556 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1557
1558 return MEMCACHED_SUCCESS;
1559 }
1560
1561 memcached_return pre_crc(memcached_st *memc)
1562 {
1563 memcached_hash value= MEMCACHED_HASH_CRC;
1564 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1565
1566 return MEMCACHED_SUCCESS;
1567 }
1568
1569 memcached_return pre_hash_fnv1_64(memcached_st *memc)
1570 {
1571 memcached_hash value= MEMCACHED_HASH_FNV1_64;
1572 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1573
1574 return MEMCACHED_SUCCESS;
1575 }
1576
1577 memcached_return pre_hash_fnv1a_64(memcached_st *memc)
1578 {
1579 memcached_hash value= MEMCACHED_HASH_FNV1A_64;
1580 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1581
1582 return MEMCACHED_SUCCESS;
1583 }
1584
1585 memcached_return pre_hash_fnv1_32(memcached_st *memc)
1586 {
1587 memcached_hash value= MEMCACHED_HASH_FNV1_32;
1588 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1589
1590 return MEMCACHED_SUCCESS;
1591 }
1592
1593 memcached_return pre_hash_fnv1a_32(memcached_st *memc)
1594 {
1595 memcached_hash value= MEMCACHED_HASH_FNV1A_32;
1596 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1597
1598 return MEMCACHED_SUCCESS;
1599 }
1600
1601 memcached_return pre_hash_ketama(memcached_st *memc)
1602 {
1603 memcached_hash value= MEMCACHED_HASH_KETAMA;
1604 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1605
1606 return MEMCACHED_SUCCESS;
1607 }
1608
1609 memcached_return enable_cas(memcached_st *memc)
1610 {
1611 unsigned int set= 1;
1612
1613 memcached_version(memc);
1614
1615 if (memc->hosts[0].major_version >= 1 &&
1616 memc->hosts[0].minor_version >= 2 &&
1617 memc->hosts[0].micro_version >= 4)
1618 {
1619 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, &set);
1620
1621 return MEMCACHED_SUCCESS;
1622 }
1623
1624 return MEMCACHED_FAILURE;
1625 }
1626
1627 memcached_return check_for_1_2_3(memcached_st *memc)
1628 {
1629 memcached_version(memc);
1630
1631 if (memc->hosts[0].major_version >= 1 &&
1632 memc->hosts[0].minor_version >= 2 &&
1633 memc->hosts[0].micro_version >= 4)
1634 return MEMCACHED_SUCCESS;
1635
1636 return MEMCACHED_FAILURE;
1637 }
1638
1639 memcached_return pre_unix_socket(memcached_st *memc)
1640 {
1641 memcached_return rc;
1642 struct stat buf;
1643
1644 memcached_server_list_free(memc->hosts);
1645 memc->hosts= NULL;
1646 memc->number_of_hosts= 0;
1647
1648 if (stat("/tmp/memcached.socket", &buf))
1649 return MEMCACHED_FAILURE;
1650
1651 rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket");
1652
1653 return rc;
1654 }
1655
1656 memcached_return pre_udp(memcached_st *memc)
1657 {
1658 memcached_return rc;
1659
1660 memcached_server_list_free(memc->hosts);
1661 memc->hosts= NULL;
1662 memc->number_of_hosts= 0;
1663
1664 if (0)
1665 return MEMCACHED_FAILURE;
1666
1667 rc= memcached_server_add_udp(memc, "localhost", MEMCACHED_DEFAULT_PORT);
1668
1669 return rc;
1670 }
1671
1672 memcached_return pre_nodelay(memcached_st *memc)
1673 {
1674 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1675 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
1676
1677 return MEMCACHED_SUCCESS;
1678 }
1679
1680 memcached_return poll_timeout(memcached_st *memc)
1681 {
1682 int32_t timeout;
1683
1684 timeout= 100;
1685
1686 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, &timeout);
1687
1688 timeout= (int32_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
1689
1690 assert(timeout == 100);
1691
1692 return MEMCACHED_SUCCESS;
1693 }
1694
1695
1696 /* Clean the server before beginning testing */
1697 test_st tests[] ={
1698 {"flush", 0, flush_test },
1699 {"init", 0, init_test },
1700 {"allocation", 0, allocation_test },
1701 {"clone_test", 0, clone_test },
1702 {"error", 0, error_test },
1703 {"set", 0, set_test },
1704 {"set2", 0, set_test2 },
1705 {"set3", 0, set_test3 },
1706 {"add", 0, add_test },
1707 {"replace", 0, replace_test },
1708 {"delete", 1, delete_test },
1709 {"get", 1, get_test },
1710 {"get2", 0, get_test2 },
1711 {"get3", 0, get_test3 },
1712 {"get4", 0, get_test4 },
1713 {"stats_servername", 0, stats_servername_test },
1714 {"increment", 0, increment_test },
1715 {"decrement", 0, decrement_test },
1716 {"quit", 0, quit_test },
1717 {"mget", 1, mget_test },
1718 {"mget_result", 1, mget_result_test },
1719 {"mget_result_alloc", 1, mget_result_alloc_test },
1720 {"get_stats", 0, get_stats },
1721 {"add_host_test", 0, add_host_test },
1722 {"get_stats_keys", 0, get_stats_keys },
1723 {"behavior_test", 0, get_stats_keys },
1724 {0, 0, 0}
1725 };
1726
1727 test_st string_tests[] ={
1728 {"string static with null", 0, string_static_null },
1729 {"string alloc with null", 0, string_alloc_null },
1730 {"string alloc with 1K", 0, string_alloc_with_size },
1731 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
1732 {"string append", 0, string_alloc_append },
1733 {"string append failure (too big)", 0, string_alloc_append_toobig },
1734 {0, 0, 0}
1735 };
1736
1737 test_st result_tests[] ={
1738 {"result static", 0, result_static},
1739 {"result alloc", 0, result_alloc},
1740 {0, 0, 0}
1741 };
1742
1743 test_st version_1_2_3[] ={
1744 {"append", 0, append_test },
1745 {"prepend", 0, prepend_test },
1746 {"cas", 0, cas_test },
1747 {"cas2", 0, cas2_test },
1748 {"append_binary", 0, append_binary_test },
1749 {0, 0, 0}
1750 };
1751
1752 test_st user_tests[] ={
1753 {"user_supplied_bug1", 0, user_supplied_bug1 },
1754 {"user_supplied_bug2", 0, user_supplied_bug2 },
1755 {"user_supplied_bug3", 0, user_supplied_bug3 },
1756 {"user_supplied_bug4", 0, user_supplied_bug4 },
1757 {"user_supplied_bug5", 1, user_supplied_bug5 },
1758 {"user_supplied_bug6", 1, user_supplied_bug6 },
1759 {"user_supplied_bug7", 1, user_supplied_bug7 },
1760 {0, 0, 0}
1761 };
1762
1763 test_st generate_tests[] ={
1764 {"generate_data", 0, generate_data },
1765 {"get_read", 0, get_read },
1766 {"mget_read", 0, mget_read },
1767 {"mget_read_result", 0, mget_read_result },
1768 {0, 0, 0}
1769 };
1770
1771
1772 collection_st collection[] ={
1773 {"block", 0, 0, tests},
1774 {"nonblock", pre_nonblock, 0, tests},
1775 {"nodelay", pre_nodelay, 0, tests},
1776 {"md5", pre_md5, 0, tests},
1777 {"crc", pre_crc, 0, tests},
1778 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
1779 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
1780 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
1781 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
1782 {"ketama", pre_hash_ketama, 0, tests},
1783 {"unix_socket", pre_unix_socket, 0, tests},
1784 {"unix_socket_nodelay", pre_nodelay, 0, tests},
1785 {"poll_timeout", poll_timeout, 0, tests},
1786 {"gets", enable_cas, 0, tests},
1787 // {"udp", pre_udp, 0, tests},
1788 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
1789 {"string", 0, 0, string_tests},
1790 {"result", 0, 0, result_tests},
1791 {"user", 0, 0, user_tests},
1792 {"generate", 0, 0, generate_tests},
1793 {"generate_nonblock", pre_nonblock, 0, generate_tests},
1794 {0, 0, 0, 0}
1795 };
1796
1797 collection_st *gets_collections(void)
1798 {
1799 return collection;
1800 }