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