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