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