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