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