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