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