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