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