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