Fix for Chinese character sets.
[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 /* Check the validity of chinese key*/
2081 test_return user_supplied_bug17(memcached_st *memc)
2082 {
2083 memcached_return rc;
2084 char *key= "豆瓣";
2085 char *value="我们在炎热抑郁的夏天无法停止豆瓣";
2086 char *value2;
2087 size_t length;
2088 uint32_t flags;
2089
2090 rc= memcached_set(memc, key, strlen(key),
2091 value, strlen(value),
2092 (time_t)0, 0);
2093
2094 assert(rc == MEMCACHED_SUCCESS);
2095
2096 value2= memcached_get(memc, key, strlen(key),
2097 &length, &flags, &rc);
2098
2099 assert(length==strlen(value));
2100 assert(rc == MEMCACHED_SUCCESS);
2101 assert(memcmp(value, value2, length)==0);
2102
2103 return 0;
2104 }
2105
2106
2107 test_return result_static(memcached_st *memc)
2108 {
2109 memcached_result_st result;
2110 memcached_result_st *result_ptr;
2111
2112 result_ptr= memcached_result_create(memc, &result);
2113 assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
2114 assert(result_ptr);
2115 memcached_result_free(&result);
2116
2117 return 0;
2118 }
2119
2120 test_return result_alloc(memcached_st *memc)
2121 {
2122 memcached_result_st *result;
2123
2124 result= memcached_result_create(memc, NULL);
2125 assert(result);
2126 memcached_result_free(result);
2127
2128 return 0;
2129 }
2130
2131 test_return string_static_null(memcached_st *memc)
2132 {
2133 memcached_string_st string;
2134 memcached_string_st *string_ptr;
2135
2136 string_ptr= memcached_string_create(memc, &string, 0);
2137 assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
2138 assert(string_ptr);
2139 memcached_string_free(&string);
2140
2141 return 0;
2142 }
2143
2144 test_return string_alloc_null(memcached_st *memc)
2145 {
2146 memcached_string_st *string;
2147
2148 string= memcached_string_create(memc, NULL, 0);
2149 assert(string);
2150 memcached_string_free(string);
2151
2152 return 0;
2153 }
2154
2155 test_return string_alloc_with_size(memcached_st *memc)
2156 {
2157 memcached_string_st *string;
2158
2159 string= memcached_string_create(memc, NULL, 1024);
2160 assert(string);
2161 memcached_string_free(string);
2162
2163 return 0;
2164 }
2165
2166 test_return string_alloc_with_size_toobig(memcached_st *memc)
2167 {
2168 memcached_string_st *string;
2169
2170 string= memcached_string_create(memc, NULL, INT64_MAX);
2171 assert(string == NULL);
2172
2173 return 0;
2174 }
2175
2176 test_return string_alloc_append(memcached_st *memc)
2177 {
2178 unsigned int x;
2179 char buffer[SMALL_STRING_LEN];
2180 memcached_string_st *string;
2181
2182 /* Ring the bell! */
2183 memset(buffer, 6, SMALL_STRING_LEN);
2184
2185 string= memcached_string_create(memc, NULL, 100);
2186 assert(string);
2187
2188 for (x= 0; x < 1024; x++)
2189 {
2190 memcached_return rc;
2191 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2192 assert(rc == MEMCACHED_SUCCESS);
2193 }
2194 memcached_string_free(string);
2195
2196 return 0;
2197 }
2198
2199 test_return string_alloc_append_toobig(memcached_st *memc)
2200 {
2201 memcached_return rc;
2202 unsigned int x;
2203 char buffer[SMALL_STRING_LEN];
2204 memcached_string_st *string;
2205
2206 /* Ring the bell! */
2207 memset(buffer, 6, SMALL_STRING_LEN);
2208
2209 string= memcached_string_create(memc, NULL, 100);
2210 assert(string);
2211
2212 for (x= 0; x < 1024; x++)
2213 {
2214 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2215 assert(rc == MEMCACHED_SUCCESS);
2216 }
2217 rc= memcached_string_append(string, buffer, INT64_MAX);
2218 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
2219 memcached_string_free(string);
2220
2221 return 0;
2222 }
2223
2224 test_return cleanup_pairs(memcached_st *memc)
2225 {
2226 pairs_free(global_pairs);
2227
2228 return 0;
2229 }
2230
2231 test_return generate_pairs(memcached_st *memc)
2232 {
2233 unsigned long long x;
2234 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
2235 global_count= GLOBAL_COUNT;
2236
2237 for (x= 0; x < global_count; x++)
2238 {
2239 global_keys[x]= global_pairs[x].key;
2240 global_keys_length[x]= global_pairs[x].key_length;
2241 }
2242
2243 return 0;
2244 }
2245
2246 test_return generate_large_pairs(memcached_st *memc)
2247 {
2248 unsigned long long x;
2249 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
2250 global_count= GLOBAL2_COUNT;
2251
2252 for (x= 0; x < global_count; x++)
2253 {
2254 global_keys[x]= global_pairs[x].key;
2255 global_keys_length[x]= global_pairs[x].key_length;
2256 }
2257
2258 return 0;
2259 }
2260
2261 test_return generate_data(memcached_st *memc)
2262 {
2263 execute_set(memc, global_pairs, global_count);
2264
2265 return 0;
2266 }
2267
2268 test_return generate_data_with_stats(memcached_st *memc)
2269 {
2270 memcached_stat_st *stat_p= NULL;
2271 memcached_return rc;
2272 int host_index= 0;
2273 execute_set(memc, global_pairs, global_count);
2274
2275 //TODO: hosts used size stats
2276 stat_p = memcached_stat(memc, NULL, &rc);
2277 for (host_index = 0; host_index < SERVERS_TO_CREATE; ++host_index)
2278 {
2279 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);
2280 }
2281
2282
2283 return 0;
2284 }
2285 test_return generate_buffer_data(memcached_st *memc)
2286 {
2287 int latch= 0;
2288
2289 latch= 1;
2290 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2291 generate_data(memc);
2292
2293 return 0;
2294 }
2295
2296 test_return get_read_count(memcached_st *memc)
2297 {
2298 unsigned int x;
2299 memcached_return rc;
2300 memcached_st *clone;
2301
2302 clone= memcached_clone(NULL, memc);
2303 assert(clone);
2304
2305 memcached_server_add(clone, "localhost", 6666);
2306
2307 {
2308 char *return_value;
2309 size_t return_value_length;
2310 uint32_t flags;
2311 uint32_t count;
2312
2313 for (x= count= 0; x < global_count; x++)
2314 {
2315 return_value= memcached_get(clone, global_keys[x], global_keys_length[x],
2316 &return_value_length, &flags, &rc);
2317 if (rc == MEMCACHED_SUCCESS)
2318 {
2319 count++;
2320 if (return_value)
2321 free(return_value);
2322 }
2323 }
2324 fprintf(stderr, "\t%u -> %u", global_count, count);
2325 }
2326
2327 memcached_free(clone);
2328
2329 return 0;
2330 }
2331
2332 test_return get_read(memcached_st *memc)
2333 {
2334 unsigned int x;
2335 memcached_return rc;
2336
2337 {
2338 char *return_value;
2339 size_t return_value_length;
2340 uint32_t flags;
2341
2342 for (x= 0; x < global_count; x++)
2343 {
2344 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
2345 &return_value_length, &flags, &rc);
2346 /*
2347 assert(return_value);
2348 assert(rc == MEMCACHED_SUCCESS);
2349 */
2350 if (rc == MEMCACHED_SUCCESS && return_value)
2351 free(return_value);
2352 }
2353 }
2354
2355 return 0;
2356 }
2357
2358 test_return mget_read(memcached_st *memc)
2359 {
2360 memcached_return rc;
2361
2362 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2363 assert(rc == MEMCACHED_SUCCESS);
2364 /* Turn this into a help function */
2365 {
2366 char return_key[MEMCACHED_MAX_KEY];
2367 size_t return_key_length;
2368 char *return_value;
2369 size_t return_value_length;
2370 uint32_t flags;
2371
2372 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2373 &return_value_length, &flags, &rc)))
2374 {
2375 assert(return_value);
2376 assert(rc == MEMCACHED_SUCCESS);
2377 free(return_value);
2378 }
2379 }
2380
2381 return 0;
2382 }
2383
2384 test_return mget_read_result(memcached_st *memc)
2385 {
2386 memcached_return rc;
2387
2388 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2389 assert(rc == MEMCACHED_SUCCESS);
2390 /* Turn this into a help function */
2391 {
2392 memcached_result_st results_obj;
2393 memcached_result_st *results;
2394
2395 results= memcached_result_create(memc, &results_obj);
2396
2397 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
2398 {
2399 assert(results);
2400 assert(rc == MEMCACHED_SUCCESS);
2401 }
2402
2403 memcached_result_free(&results_obj);
2404 }
2405
2406 return 0;
2407 }
2408
2409 test_return mget_read_function(memcached_st *memc)
2410 {
2411 memcached_return rc;
2412 unsigned int counter;
2413 unsigned int (*callbacks[1])(memcached_st *, memcached_result_st *, void *);
2414
2415 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2416 assert(rc == MEMCACHED_SUCCESS);
2417
2418 callbacks[0]= &callback_counter;
2419 counter= 0;
2420 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
2421
2422 return 0;
2423 }
2424
2425 test_return delete_generate(memcached_st *memc)
2426 {
2427 unsigned int x;
2428
2429 for (x= 0; x < global_count; x++)
2430 {
2431 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2432 }
2433
2434 return 0;
2435 }
2436
2437 test_return delete_buffer_generate(memcached_st *memc)
2438 {
2439 int latch= 0;
2440 unsigned int x;
2441
2442 latch= 1;
2443 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2444
2445 for (x= 0; x < global_count; x++)
2446 {
2447 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2448 }
2449
2450 return 0;
2451 }
2452
2453 test_return free_data(memcached_st *memc)
2454 {
2455 pairs_free(global_pairs);
2456
2457 return 0;
2458 }
2459
2460 test_return add_host_test1(memcached_st *memc)
2461 {
2462 unsigned int x;
2463 memcached_return rc;
2464 char servername[]= "0.example.com";
2465 memcached_server_st *servers;
2466
2467 servers= memcached_server_list_append(NULL, servername, 400, &rc);
2468 assert(servers);
2469 assert(1 == memcached_server_list_count(servers));
2470
2471 for (x= 2; x < 20; x++)
2472 {
2473 char buffer[SMALL_STRING_LEN];
2474
2475 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
2476 servers= memcached_server_list_append(servers, buffer, 401,
2477 &rc);
2478 assert(rc == MEMCACHED_SUCCESS);
2479 assert(x == memcached_server_list_count(servers));
2480 }
2481
2482 rc= memcached_server_push(memc, servers);
2483 assert(rc == MEMCACHED_SUCCESS);
2484 rc= memcached_server_push(memc, servers);
2485 assert(rc == MEMCACHED_SUCCESS);
2486
2487 memcached_server_list_free(servers);
2488
2489 return 0;
2490 }
2491
2492 memcached_return pre_nonblock(memcached_st *memc)
2493 {
2494 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2495
2496 return MEMCACHED_SUCCESS;
2497 }
2498
2499 memcached_return pre_murmur(memcached_st *memc)
2500 {
2501 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
2502
2503 return MEMCACHED_SUCCESS;
2504 }
2505
2506 memcached_return pre_md5(memcached_st *memc)
2507 {
2508 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
2509
2510 return MEMCACHED_SUCCESS;
2511 }
2512
2513 memcached_return pre_crc(memcached_st *memc)
2514 {
2515 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
2516
2517 return MEMCACHED_SUCCESS;
2518 }
2519
2520 memcached_return pre_hsieh(memcached_st *memc)
2521 {
2522 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
2523
2524 return MEMCACHED_SUCCESS;
2525 }
2526
2527 memcached_return pre_hash_fnv1_64(memcached_st *memc)
2528 {
2529 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
2530
2531 return MEMCACHED_SUCCESS;
2532 }
2533
2534 memcached_return pre_hash_fnv1a_64(memcached_st *memc)
2535 {
2536 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
2537
2538 return MEMCACHED_SUCCESS;
2539 }
2540
2541 memcached_return pre_hash_fnv1_32(memcached_st *memc)
2542 {
2543 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
2544
2545 return MEMCACHED_SUCCESS;
2546 }
2547
2548 memcached_return pre_hash_fnv1a_32(memcached_st *memc)
2549 {
2550 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
2551
2552 return MEMCACHED_SUCCESS;
2553 }
2554
2555 memcached_return pre_behavior_ketama(memcached_st *memc)
2556 {
2557 memcached_return rc;
2558 uint64_t value;
2559
2560 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
2561 assert(rc == MEMCACHED_SUCCESS);
2562
2563 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
2564 assert(value == 1);
2565
2566 return MEMCACHED_SUCCESS;
2567 }
2568
2569 memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
2570 {
2571 memcached_return rc;
2572 uint64_t value;
2573
2574 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2575 assert(rc == MEMCACHED_SUCCESS);
2576
2577 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2578 assert(value == 1);
2579
2580 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2581 assert(rc == MEMCACHED_SUCCESS);
2582
2583 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2584 assert(value == MEMCACHED_HASH_MD5);
2585 return MEMCACHED_SUCCESS;
2586 }
2587 void my_free(memcached_st *ptr, void *mem)
2588 {
2589 free(mem);
2590 }
2591
2592 void *my_malloc(memcached_st *ptr, const size_t size)
2593 {
2594 return malloc(size);
2595 }
2596
2597 void *my_realloc(memcached_st *ptr, void *mem, const size_t size)
2598 {
2599 return realloc(mem, size);
2600 }
2601
2602 memcached_return set_prefix(memcached_st *memc)
2603 {
2604 memcached_return rc;
2605 const char *key= "mine";
2606 char *value;
2607
2608 /* Make sure be default none exists */
2609 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2610 assert(rc == MEMCACHED_FAILURE);
2611
2612 /* Test a clean set */
2613 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
2614 assert(rc == MEMCACHED_SUCCESS);
2615
2616 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2617 assert(memcmp(value, key, 4) == 0);
2618 assert(rc == MEMCACHED_SUCCESS);
2619
2620 /* Test that we can turn it off */
2621 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
2622 assert(rc == MEMCACHED_SUCCESS);
2623
2624 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2625 assert(rc == MEMCACHED_FAILURE);
2626
2627 /* Now setup for main test */
2628 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
2629 assert(rc == MEMCACHED_SUCCESS);
2630
2631 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2632 assert(rc == MEMCACHED_SUCCESS);
2633 assert(memcmp(value, key, 4) == 0);
2634
2635 /* Set to Zero, and then Set to something too large */
2636 {
2637 char *long_key;
2638 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
2639 assert(rc == MEMCACHED_SUCCESS);
2640
2641 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2642 assert(rc == MEMCACHED_FAILURE);
2643 assert(value == NULL);
2644
2645 /* Test a long key for failure */
2646 long_key= "Thisismorethentheallottednumberofcharacters";
2647 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
2648 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
2649
2650 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2651 long_key= "This is more then the allotted number of characters";
2652 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
2653 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
2654
2655 /* Test for a bad prefix, but with a short key */
2656 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
2657 assert(rc == MEMCACHED_SUCCESS);
2658
2659 long_key= "dog cat";
2660 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
2661 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
2662 }
2663
2664 return MEMCACHED_SUCCESS;
2665 }
2666
2667 memcached_return set_memory_alloc(memcached_st *memc)
2668 {
2669 {
2670 memcached_malloc_function test_ptr;
2671 memcached_return rc;
2672
2673 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &my_malloc);
2674 assert(rc == MEMCACHED_SUCCESS);
2675 test_ptr= (memcached_malloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
2676 assert(rc == MEMCACHED_SUCCESS);
2677 assert(test_ptr == my_malloc);
2678 }
2679
2680 {
2681 memcached_realloc_function test_ptr;
2682 memcached_return rc;
2683
2684 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &my_realloc);
2685 assert(rc == MEMCACHED_SUCCESS);
2686 test_ptr= (memcached_realloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
2687 assert(rc == MEMCACHED_SUCCESS);
2688 assert(test_ptr == my_realloc);
2689 }
2690
2691 {
2692 memcached_free_function test_ptr;
2693 memcached_return rc;
2694
2695 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, my_free);
2696 assert(rc == MEMCACHED_SUCCESS);
2697 test_ptr= (memcached_free_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
2698 assert(rc == MEMCACHED_SUCCESS);
2699 assert(test_ptr == my_free);
2700 }
2701
2702 return MEMCACHED_SUCCESS;
2703 }
2704
2705 memcached_return enable_consistent(memcached_st *memc)
2706 {
2707 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
2708 memcached_hash hash;
2709 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
2710 pre_hsieh(memc);
2711
2712 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
2713 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
2714
2715 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
2716 assert(hash == MEMCACHED_HASH_HSIEH);
2717
2718
2719 return MEMCACHED_SUCCESS;
2720 }
2721
2722 memcached_return enable_cas(memcached_st *memc)
2723 {
2724 unsigned int set= 1;
2725
2726 memcached_version(memc);
2727
2728 if (memc->hosts[0].major_version >= 1 &&
2729 memc->hosts[0].minor_version >= 2 &&
2730 memc->hosts[0].micro_version >= 4)
2731 {
2732 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
2733
2734 return MEMCACHED_SUCCESS;
2735 }
2736
2737 return MEMCACHED_FAILURE;
2738 }
2739
2740 memcached_return check_for_1_2_3(memcached_st *memc)
2741 {
2742 memcached_version(memc);
2743
2744 if (memc->hosts[0].major_version >= 1 &&
2745 memc->hosts[0].minor_version >= 2 &&
2746 memc->hosts[0].micro_version >= 4)
2747 return MEMCACHED_SUCCESS;
2748
2749 return MEMCACHED_FAILURE;
2750 }
2751
2752 memcached_return pre_unix_socket(memcached_st *memc)
2753 {
2754 memcached_return rc;
2755 struct stat buf;
2756
2757 memcached_server_list_free(memc->hosts);
2758 memc->hosts= NULL;
2759 memc->number_of_hosts= 0;
2760
2761 if (stat("/tmp/memcached.socket", &buf))
2762 return MEMCACHED_FAILURE;
2763
2764 rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket");
2765
2766 return rc;
2767 }
2768
2769 memcached_return pre_udp(memcached_st *memc)
2770 {
2771 memcached_return rc;
2772
2773 memcached_server_list_free(memc->hosts);
2774 memc->hosts= NULL;
2775 memc->number_of_hosts= 0;
2776
2777 if (0)
2778 return MEMCACHED_FAILURE;
2779
2780 rc= memcached_server_add_udp(memc, "localhost", MEMCACHED_DEFAULT_PORT);
2781
2782 return rc;
2783 }
2784
2785 memcached_return pre_nodelay(memcached_st *memc)
2786 {
2787 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2788 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
2789
2790 return MEMCACHED_SUCCESS;
2791 }
2792
2793 memcached_return poll_timeout(memcached_st *memc)
2794 {
2795 int32_t timeout;
2796
2797 timeout= 100;
2798
2799 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
2800
2801 timeout= (int32_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
2802
2803 assert(timeout == 100);
2804
2805 return MEMCACHED_SUCCESS;
2806 }
2807
2808
2809 /* Clean the server before beginning testing */
2810 test_st tests[] ={
2811 {"flush", 0, flush_test },
2812 {"init", 0, init_test },
2813 {"allocation", 0, allocation_test },
2814 {"server_list_null_test", 0, server_list_null_test},
2815 {"server_unsort", 0, server_unsort_test},
2816 {"server_sort", 0, server_sort_test},
2817 {"server_sort2", 0, server_sort2_test},
2818 {"clone_test", 0, clone_test },
2819 {"error", 0, error_test },
2820 {"set", 0, set_test },
2821 {"set2", 0, set_test2 },
2822 {"set3", 0, set_test3 },
2823 {"add", 1, add_test },
2824 {"replace", 1, replace_test },
2825 {"delete", 1, delete_test },
2826 {"get", 1, get_test },
2827 {"get2", 0, get_test2 },
2828 {"get3", 0, get_test3 },
2829 {"get4", 0, get_test4 },
2830 {"stats_servername", 0, stats_servername_test },
2831 {"increment", 0, increment_test },
2832 {"decrement", 0, decrement_test },
2833 {"quit", 0, quit_test },
2834 {"mget", 1, mget_test },
2835 {"mget_result", 1, mget_result_test },
2836 {"mget_result_alloc", 1, mget_result_alloc_test },
2837 {"mget_result_function", 1, mget_result_function },
2838 {"get_stats", 0, get_stats },
2839 {"add_host_test", 0, add_host_test },
2840 {"get_stats_keys", 0, get_stats_keys },
2841 {"behavior_test", 0, get_stats_keys },
2842 {"callback_test", 0, get_stats_keys },
2843 {"version_string_test", 0, version_string_test},
2844 {"bad_key", 1, bad_key_test },
2845 {"memcached_server_cursor", 1, memcached_server_cursor_test },
2846 {"read_through", 1, read_through },
2847 {"delete_through", 1, delete_through },
2848 {0, 0, 0}
2849 };
2850
2851 test_st async_tests[] ={
2852 {"add", 1, add_wrapper },
2853 {0, 0, 0}
2854 };
2855
2856 test_st string_tests[] ={
2857 {"string static with null", 0, string_static_null },
2858 {"string alloc with null", 0, string_alloc_null },
2859 {"string alloc with 1K", 0, string_alloc_with_size },
2860 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
2861 {"string append", 0, string_alloc_append },
2862 {"string append failure (too big)", 0, string_alloc_append_toobig },
2863 {0, 0, 0}
2864 };
2865
2866 test_st result_tests[] ={
2867 {"result static", 0, result_static},
2868 {"result alloc", 0, result_alloc},
2869 {0, 0, 0}
2870 };
2871
2872 test_st version_1_2_3[] ={
2873 {"append", 0, append_test },
2874 {"prepend", 0, prepend_test },
2875 {"cas", 0, cas_test },
2876 {"cas2", 0, cas2_test },
2877 {"append_binary", 0, append_binary_test },
2878 {0, 0, 0}
2879 };
2880
2881 test_st user_tests[] ={
2882 {"user_supplied_bug1", 0, user_supplied_bug1 },
2883 {"user_supplied_bug2", 0, user_supplied_bug2 },
2884 {"user_supplied_bug3", 0, user_supplied_bug3 },
2885 {"user_supplied_bug4", 0, user_supplied_bug4 },
2886 {"user_supplied_bug5", 1, user_supplied_bug5 },
2887 {"user_supplied_bug6", 1, user_supplied_bug6 },
2888 {"user_supplied_bug7", 1, user_supplied_bug7 },
2889 {"user_supplied_bug8", 1, user_supplied_bug8 },
2890 {"user_supplied_bug9", 1, user_supplied_bug9 },
2891 {"user_supplied_bug10", 1, user_supplied_bug10 },
2892 {"user_supplied_bug11", 1, user_supplied_bug11 },
2893 {"user_supplied_bug12", 1, user_supplied_bug12 },
2894 {"user_supplied_bug13", 1, user_supplied_bug13 },
2895 {"user_supplied_bug14", 1, user_supplied_bug14 },
2896 {"user_supplied_bug15", 1, user_supplied_bug15 },
2897 {"user_supplied_bug16", 1, user_supplied_bug16 },
2898 {"user_supplied_bug17", 1, user_supplied_bug17 },
2899 {0, 0, 0}
2900 };
2901
2902 test_st generate_tests[] ={
2903 {"generate_pairs", 1, generate_pairs },
2904 {"generate_data", 1, generate_data },
2905 {"get_read", 0, get_read },
2906 {"delete_generate", 0, delete_generate },
2907 {"generate_buffer_data", 1, generate_buffer_data },
2908 {"delete_buffer", 0, delete_buffer_generate},
2909 {"generate_data", 1, generate_data },
2910 {"mget_read", 0, mget_read },
2911 {"mget_read_result", 0, mget_read_result },
2912 {"mget_read_function", 0, mget_read_function },
2913 {"cleanup", 1, cleanup_pairs },
2914 {"generate_large_pairs", 1, generate_large_pairs },
2915 {"generate_data", 1, generate_data },
2916 {"generate_buffer_data", 1, generate_buffer_data },
2917 {"cleanup", 1, cleanup_pairs },
2918 {0, 0, 0}
2919 };
2920
2921 test_st consistent_tests[] ={
2922 {"generate_pairs", 1, generate_pairs },
2923 {"generate_data", 1, generate_data },
2924 {"get_read", 0, get_read_count },
2925 {"cleanup", 1, cleanup_pairs },
2926 {0, 0, 0}
2927 };
2928
2929 test_st consistent_weighted_tests[] ={
2930 {"generate_pairs", 1, generate_pairs },
2931 {"generate_data", 1, generate_data_with_stats },
2932 {"get_read", 0, get_read_count },
2933 {"cleanup", 1, cleanup_pairs },
2934 {0, 0, 0}
2935 };
2936
2937 collection_st collection[] ={
2938 {"block", 0, 0, tests},
2939 {"nonblock", pre_nonblock, 0, tests},
2940 {"nodelay", pre_nodelay, 0, tests},
2941 {"md5", pre_md5, 0, tests},
2942 {"crc", pre_crc, 0, tests},
2943 {"hsieh", pre_hsieh, 0, tests},
2944 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
2945 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
2946 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
2947 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
2948 {"ketama", pre_behavior_ketama, 0, tests},
2949 {"unix_socket", pre_unix_socket, 0, tests},
2950 {"unix_socket_nodelay", pre_nodelay, 0, tests},
2951 {"poll_timeout", poll_timeout, 0, tests},
2952 {"gets", enable_cas, 0, tests},
2953 {"consistent", enable_consistent, 0, tests},
2954 {"memory_allocators", set_memory_alloc, 0, tests},
2955 {"prefix", set_prefix, 0, tests},
2956 // {"udp", pre_udp, 0, tests},
2957 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
2958 {"string", 0, 0, string_tests},
2959 {"result", 0, 0, result_tests},
2960 {"async", pre_nonblock, 0, async_tests},
2961 {"user", 0, 0, user_tests},
2962 {"generate", 0, 0, generate_tests},
2963 {"generate_hsieh", pre_hsieh, 0, generate_tests},
2964 {"generate_ketama", pre_behavior_ketama, 0, generate_tests},
2965 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
2966 {"generate_md5", pre_md5, 0, generate_tests},
2967 {"generate_murmur", pre_murmur, 0, generate_tests},
2968 {"generate_nonblock", pre_nonblock, 0, generate_tests},
2969 {"consistent_not", 0, 0, consistent_tests},
2970 {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests},
2971 {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
2972 {0, 0, 0, 0}
2973 };
2974
2975 #define SERVERS_TO_CREATE 5
2976
2977 void *world_create(void)
2978 {
2979 server_startup_st *construct;
2980
2981 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
2982 memset(construct, 0, sizeof(server_startup_st));
2983 construct->count= SERVERS_TO_CREATE;
2984 construct->udp= 0;
2985 server_startup(construct);
2986
2987 return construct;
2988 }
2989
2990 void world_destroy(void *p)
2991 {
2992 server_startup_st *construct= (server_startup_st *)p;
2993 memcached_server_st *servers= (memcached_server_st *)construct->servers;
2994 memcached_server_list_free(servers);
2995
2996 server_shutdown(construct);
2997 free(construct);
2998 }
2999
3000 void get_world(world_st *world)
3001 {
3002 world->collections= collection;
3003 world->create= world_create;
3004 world->destroy= world_destroy;
3005 }