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