Merge Monty
[awesomized/libmemcached] / tests / function.c
1 /*
2 Sample test application.
3 */
4 #include "libmemcached/common.h"
5
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <time.h>
15 #include "server.h"
16 #include "clients/generator.h"
17 #include "clients/execute.h"
18
19 #ifndef INT64_MAX
20 #define INT64_MAX LONG_MAX
21 #endif
22 #ifndef INT32_MAX
23 #define INT32_MAX INT_MAX
24 #endif
25
26
27 #include "test.h"
28
29 #ifdef HAVE_LIBMEMCACHEDUTIL
30 #include <pthread.h>
31 #include "libmemcached/memcached_util.h"
32 #endif
33
34 #define GLOBAL_COUNT 10000
35 #define GLOBAL2_COUNT 100
36 #define SERVERS_TO_CREATE 5
37 static uint32_t global_count;
38
39 static pairs_st *global_pairs;
40 static char *global_keys[GLOBAL_COUNT];
41 static size_t global_keys_length[GLOBAL_COUNT];
42
43 static test_return init_test(memcached_st *not_used __attribute__((unused)))
44 {
45 memcached_st memc;
46
47 (void)memcached_create(&memc);
48 memcached_free(&memc);
49
50 return 0;
51 }
52
53 static test_return server_list_null_test(memcached_st *ptr __attribute__((unused)))
54 {
55 memcached_server_st *server_list;
56 memcached_return rc;
57
58 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, NULL);
59 assert(server_list == NULL);
60
61 server_list= memcached_server_list_append_with_weight(NULL, "localhost", 0, 0, NULL);
62 assert(server_list == NULL);
63
64 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, &rc);
65 assert(server_list == NULL);
66
67 return 0;
68 }
69
70 #define TEST_PORT_COUNT 7
71 uint32_t test_ports[TEST_PORT_COUNT];
72
73 static memcached_return server_display_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
74 {
75 /* Do Nothing */
76 uint32_t bigger= *((uint32_t *)(context));
77 assert(bigger <= server->port);
78 *((uint32_t *)(context))= server->port;
79
80 return MEMCACHED_SUCCESS;
81 }
82
83 static test_return server_sort_test(memcached_st *ptr __attribute__((unused)))
84 {
85 uint32_t x;
86 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
87 memcached_return rc;
88 memcached_server_function callbacks[1];
89 memcached_st *local_memc;
90
91 local_memc= memcached_create(NULL);
92 assert(local_memc);
93 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
94
95 for (x= 0; x < TEST_PORT_COUNT; x++)
96 {
97 test_ports[x]= (uint32_t)random() % 64000;
98 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
99 assert(local_memc->number_of_hosts == x + 1);
100 assert(local_memc->hosts[0].count == x+1);
101 assert(rc == MEMCACHED_SUCCESS);
102 }
103
104 callbacks[0]= server_display_function;
105 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
106
107
108 memcached_free(local_memc);
109
110 return 0;
111 }
112
113 static test_return server_sort2_test(memcached_st *ptr __attribute__((unused)))
114 {
115 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
116 memcached_return rc;
117 memcached_server_function callbacks[1];
118 memcached_st *local_memc;
119
120 local_memc= memcached_create(NULL);
121 assert(local_memc);
122 rc= memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
123 assert(rc == MEMCACHED_SUCCESS);
124
125 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
126 assert(rc == MEMCACHED_SUCCESS);
127 assert(local_memc->hosts[0].port == 43043);
128
129 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
130 assert(rc == MEMCACHED_SUCCESS);
131 assert(local_memc->hosts[0].port == 43042);
132 assert(local_memc->hosts[1].port == 43043);
133
134 callbacks[0]= server_display_function;
135 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
136
137
138 memcached_free(local_memc);
139
140 return 0;
141 }
142
143 static memcached_return server_display_unsort_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
144 {
145 /* Do Nothing */
146 uint32_t x= *((uint32_t *)(context));
147
148 assert(test_ports[x] == server->port);
149 *((uint32_t *)(context))= ++x;
150
151 return MEMCACHED_SUCCESS;
152 }
153
154 static test_return server_unsort_test(memcached_st *ptr __attribute__((unused)))
155 {
156 uint32_t x;
157 uint32_t counter= 0; /* Prime the value for the assert in server_display_function */
158 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
159 memcached_return rc;
160 memcached_server_function callbacks[1];
161 memcached_st *local_memc;
162
163 local_memc= memcached_create(NULL);
164 assert(local_memc);
165
166 for (x= 0; x < TEST_PORT_COUNT; x++)
167 {
168 test_ports[x]= (uint32_t)(random() % 64000);
169 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
170 assert(local_memc->number_of_hosts == x+1);
171 assert(local_memc->hosts[0].count == x+1);
172 assert(rc == MEMCACHED_SUCCESS);
173 }
174
175 callbacks[0]= server_display_unsort_function;
176 memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1);
177
178 /* Now we sort old data! */
179 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
180 callbacks[0]= server_display_function;
181 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
182
183
184 memcached_free(local_memc);
185
186 return 0;
187 }
188
189 static test_return allocation_test(memcached_st *not_used __attribute__((unused)))
190 {
191 memcached_st *memc;
192 memc= memcached_create(NULL);
193 assert(memc);
194 memcached_free(memc);
195
196 return 0;
197 }
198
199 static test_return clone_test(memcached_st *memc)
200 {
201 /* All null? */
202 {
203 memcached_st *memc_clone;
204 memc_clone= memcached_clone(NULL, NULL);
205 assert(memc_clone);
206 memcached_free(memc_clone);
207 }
208
209 /* Can we init from null? */
210 {
211 memcached_st *memc_clone;
212 memc_clone= memcached_clone(NULL, memc);
213 assert(memc_clone);
214
215 assert(memc_clone->call_free == memc->call_free);
216 assert(memc_clone->call_malloc == memc->call_malloc);
217 assert(memc_clone->call_realloc == memc->call_realloc);
218 assert(memc_clone->call_calloc == memc->call_calloc);
219 assert(memc_clone->connect_timeout == memc->connect_timeout);
220 assert(memc_clone->delete_trigger == memc->delete_trigger);
221 assert(memc_clone->distribution == memc->distribution);
222 assert(memc_clone->flags == memc->flags);
223 assert(memc_clone->get_key_failure == memc->get_key_failure);
224 assert(memc_clone->hash == memc->hash);
225 assert(memc_clone->hash_continuum == memc->hash_continuum);
226 assert(memc_clone->io_bytes_watermark == memc->io_bytes_watermark);
227 assert(memc_clone->io_msg_watermark == memc->io_msg_watermark);
228 assert(memc_clone->io_key_prefetch == memc->io_key_prefetch);
229 assert(memc_clone->on_cleanup == memc->on_cleanup);
230 assert(memc_clone->on_clone == memc->on_clone);
231 assert(memc_clone->poll_timeout == memc->poll_timeout);
232 assert(memc_clone->rcv_timeout == memc->rcv_timeout);
233 assert(memc_clone->recv_size == memc->recv_size);
234 assert(memc_clone->retry_timeout == memc->retry_timeout);
235 assert(memc_clone->send_size == memc->send_size);
236 assert(memc_clone->server_failure_limit == memc->server_failure_limit);
237 assert(memc_clone->snd_timeout == memc->snd_timeout);
238 assert(memc_clone->user_data == memc->user_data);
239
240 memcached_free(memc_clone);
241 }
242
243 /* Can we init from struct? */
244 {
245 memcached_st declared_clone;
246 memcached_st *memc_clone;
247 memset(&declared_clone, 0 , sizeof(memcached_st));
248 memc_clone= memcached_clone(&declared_clone, NULL);
249 assert(memc_clone);
250 memcached_free(memc_clone);
251 }
252
253 /* Can we init from struct? */
254 {
255 memcached_st declared_clone;
256 memcached_st *memc_clone;
257 memset(&declared_clone, 0 , sizeof(memcached_st));
258 memc_clone= memcached_clone(&declared_clone, memc);
259 assert(memc_clone);
260 memcached_free(memc_clone);
261 }
262
263 return 0;
264 }
265
266 static test_return userdata_test(memcached_st *memc)
267 {
268 void* foo= NULL;
269 assert(memcached_set_user_data(memc, foo) == NULL);
270 assert(memcached_get_user_data(memc) == foo);
271 assert(memcached_set_user_data(memc, NULL) == foo);
272
273 return TEST_SUCCESS;
274 }
275
276 static test_return connection_test(memcached_st *memc)
277 {
278 memcached_return rc;
279
280 rc= memcached_server_add_with_weight(memc, "localhost", 0, 0);
281 assert(rc == MEMCACHED_SUCCESS);
282
283 return 0;
284 }
285
286 static test_return error_test(memcached_st *memc)
287 {
288 memcached_return rc;
289
290 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
291 {
292 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
293 }
294
295 return 0;
296 }
297
298 static test_return set_test(memcached_st *memc)
299 {
300 memcached_return rc;
301 char *key= "foo";
302 char *value= "when we sanitize";
303
304 rc= memcached_set(memc, key, strlen(key),
305 value, strlen(value),
306 (time_t)0, (uint32_t)0);
307 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
308
309 return 0;
310 }
311
312 static test_return append_test(memcached_st *memc)
313 {
314 memcached_return rc;
315 char *key= "fig";
316 char *value= "we";
317 size_t value_length;
318 uint32_t flags;
319
320 rc= memcached_flush(memc, 0);
321 assert(rc == MEMCACHED_SUCCESS);
322
323 rc= memcached_set(memc, key, strlen(key),
324 value, strlen(value),
325 (time_t)0, (uint32_t)0);
326 assert(rc == MEMCACHED_SUCCESS);
327
328 rc= memcached_append(memc, key, strlen(key),
329 " the", strlen(" the"),
330 (time_t)0, (uint32_t)0);
331 assert(rc == MEMCACHED_SUCCESS);
332
333 rc= memcached_append(memc, key, strlen(key),
334 " people", strlen(" people"),
335 (time_t)0, (uint32_t)0);
336 assert(rc == MEMCACHED_SUCCESS);
337
338 value= memcached_get(memc, key, strlen(key),
339 &value_length, &flags, &rc);
340 assert(!memcmp(value, "we the people", strlen("we the people")));
341 assert(strlen("we the people") == value_length);
342 assert(rc == MEMCACHED_SUCCESS);
343 free(value);
344
345 return 0;
346 }
347
348 static test_return append_binary_test(memcached_st *memc)
349 {
350 memcached_return rc;
351 char *key= "numbers";
352 unsigned int *store_ptr;
353 unsigned int store_list[] = { 23, 56, 499, 98, 32847, 0 };
354 char *value;
355 size_t value_length;
356 uint32_t flags;
357 unsigned int x;
358
359 rc= memcached_flush(memc, 0);
360 assert(rc == MEMCACHED_SUCCESS);
361
362 rc= memcached_set(memc,
363 key, strlen(key),
364 NULL, 0,
365 (time_t)0, (uint32_t)0);
366 assert(rc == MEMCACHED_SUCCESS);
367
368 for (x= 0; store_list[x] ; x++)
369 {
370 rc= memcached_append(memc,
371 key, strlen(key),
372 (char *)&store_list[x], sizeof(unsigned int),
373 (time_t)0, (uint32_t)0);
374 assert(rc == MEMCACHED_SUCCESS);
375 }
376
377 value= memcached_get(memc, key, strlen(key),
378 &value_length, &flags, &rc);
379 assert((value_length == (sizeof(unsigned int) * x)));
380 assert(rc == MEMCACHED_SUCCESS);
381
382 store_ptr= (unsigned int *)value;
383 x= 0;
384 while ((size_t)store_ptr < (size_t)(value + value_length))
385 {
386 assert(*store_ptr == store_list[x++]);
387 store_ptr++;
388 }
389 free(value);
390
391 return 0;
392 }
393
394 static test_return cas2_test(memcached_st *memc)
395 {
396 memcached_return rc;
397 char *keys[]= {"fudge", "son", "food"};
398 size_t key_length[]= {5, 3, 4};
399 char *value= "we the people";
400 size_t value_length= strlen("we the people");
401 unsigned int x;
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 for (x= 0; x < 3; x++)
412 {
413 rc= memcached_set(memc, keys[x], key_length[x],
414 keys[x], key_length[x],
415 (time_t)50, (uint32_t)9);
416 assert(rc == MEMCACHED_SUCCESS);
417 }
418
419 rc= memcached_mget(memc, keys, key_length, 3);
420
421 results= memcached_result_create(memc, &results_obj);
422
423 results= memcached_fetch_result(memc, &results_obj, &rc);
424 assert(results);
425 assert(results->cas);
426 assert(rc == MEMCACHED_SUCCESS);
427 WATCHPOINT_ASSERT(memcached_result_cas(results));
428
429 assert(!memcmp(value, "we the people", strlen("we the people")));
430 assert(strlen("we the people") == value_length);
431 assert(rc == MEMCACHED_SUCCESS);
432
433 memcached_result_free(&results_obj);
434
435 return 0;
436 }
437
438 static test_return cas_test(memcached_st *memc)
439 {
440 memcached_return rc;
441 const char *key= "fun";
442 size_t key_length= strlen(key);
443 const char *value= "we the people";
444 char* keys[2] = { (char*)key, NULL };
445 size_t keylengths[2] = { strlen(key), 0 };
446 size_t value_length= strlen(value);
447 const char *value2= "change the value";
448 size_t value2_length= strlen(value2);
449
450 memcached_result_st results_obj;
451 memcached_result_st *results;
452 unsigned int set= 1;
453
454 rc= memcached_flush(memc, 0);
455 assert(rc == MEMCACHED_SUCCESS);
456
457 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
458
459 rc= memcached_set(memc, key, strlen(key),
460 value, strlen(value),
461 (time_t)0, (uint32_t)0);
462 assert(rc == MEMCACHED_SUCCESS);
463
464 rc= memcached_mget(memc, keys, keylengths, 1);
465
466 results= memcached_result_create(memc, &results_obj);
467
468 results= memcached_fetch_result(memc, &results_obj, &rc);
469 assert(results);
470 assert(rc == MEMCACHED_SUCCESS);
471 WATCHPOINT_ASSERT(memcached_result_cas(results));
472 assert(!memcmp(value, memcached_result_value(results), value_length));
473 assert(strlen(memcached_result_value(results)) == value_length);
474 assert(rc == MEMCACHED_SUCCESS);
475 uint64_t cas = memcached_result_cas(results);
476
477 #if 0
478 results= memcached_fetch_result(memc, &results_obj, &rc);
479 assert(rc == MEMCACHED_END);
480 assert(results == NULL);
481 #endif
482
483 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
484 assert(rc == MEMCACHED_SUCCESS);
485
486 /*
487 * The item will have a new cas value, so try to set it again with the old
488 * value. This should fail!
489 */
490 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
491 assert(rc == MEMCACHED_DATA_EXISTS);
492
493 memcached_result_free(&results_obj);
494
495 return 0;
496 }
497
498 static test_return prepend_test(memcached_st *memc)
499 {
500 memcached_return rc;
501 char *key= "fig";
502 char *value= "people";
503 size_t value_length;
504 uint32_t flags;
505
506 rc= memcached_flush(memc, 0);
507 assert(rc == MEMCACHED_SUCCESS);
508
509 rc= memcached_set(memc, key, strlen(key),
510 value, strlen(value),
511 (time_t)0, (uint32_t)0);
512 assert(rc == MEMCACHED_SUCCESS);
513
514 rc= memcached_prepend(memc, key, strlen(key),
515 "the ", strlen("the "),
516 (time_t)0, (uint32_t)0);
517 assert(rc == MEMCACHED_SUCCESS);
518
519 rc= memcached_prepend(memc, key, strlen(key),
520 "we ", strlen("we "),
521 (time_t)0, (uint32_t)0);
522 assert(rc == MEMCACHED_SUCCESS);
523
524 value= memcached_get(memc, key, strlen(key),
525 &value_length, &flags, &rc);
526 assert(!memcmp(value, "we the people", strlen("we the people")));
527 assert(strlen("we the people") == value_length);
528 assert(rc == MEMCACHED_SUCCESS);
529 free(value);
530
531 return 0;
532 }
533
534 /*
535 Set the value, then quit to make sure it is flushed.
536 Come back in and test that add fails.
537 */
538 static test_return add_test(memcached_st *memc)
539 {
540 memcached_return rc;
541 char *key= "foo";
542 char *value= "when we sanitize";
543 unsigned long long setting_value;
544
545 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
546
547 rc= memcached_set(memc, key, strlen(key),
548 value, strlen(value),
549 (time_t)0, (uint32_t)0);
550 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
551 memcached_quit(memc);
552 rc= memcached_add(memc, key, strlen(key),
553 value, strlen(value),
554 (time_t)0, (uint32_t)0);
555
556 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
557 if (setting_value)
558 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
559 else
560 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_DATA_EXISTS);
561
562 return 0;
563 }
564
565 /*
566 ** There was a problem of leaking filedescriptors in the initial release
567 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
568 ** systems it seems that the kernel is slow on reclaiming the resources
569 ** because the connects starts to time out (the test doesn't do much
570 ** anyway, so just loop 10 iterations)
571 */
572 static test_return add_wrapper(memcached_st *memc)
573 {
574 unsigned int x;
575 unsigned int max= 10000;
576 #ifdef __sun
577 max= 10;
578 #endif
579
580 for (x= 0; x < max; x++)
581 add_test(memc);
582
583 return 0;
584 }
585
586 static test_return replace_test(memcached_st *memc)
587 {
588 memcached_return rc;
589 char *key= "foo";
590 char *value= "when we sanitize";
591 char *original= "first we insert some data";
592
593 rc= memcached_set(memc, key, strlen(key),
594 original, strlen(original),
595 (time_t)0, (uint32_t)0);
596 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
597
598 rc= memcached_replace(memc, key, strlen(key),
599 value, strlen(value),
600 (time_t)0, (uint32_t)0);
601 assert(rc == MEMCACHED_SUCCESS);
602
603 return 0;
604 }
605
606 static test_return delete_test(memcached_st *memc)
607 {
608 memcached_return rc;
609 char *key= "foo";
610 char *value= "when we sanitize";
611
612 rc= memcached_set(memc, key, strlen(key),
613 value, strlen(value),
614 (time_t)0, (uint32_t)0);
615 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
616
617 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
618 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
619
620 return 0;
621 }
622
623 static test_return flush_test(memcached_st *memc)
624 {
625 memcached_return rc;
626
627 rc= memcached_flush(memc, 0);
628 assert(rc == MEMCACHED_SUCCESS);
629
630 return 0;
631 }
632
633 static memcached_return server_function(memcached_st *ptr __attribute__((unused)),
634 memcached_server_st *server __attribute__((unused)),
635 void *context __attribute__((unused)))
636 {
637 /* Do Nothing */
638
639 return MEMCACHED_SUCCESS;
640 }
641
642 static test_return memcached_server_cursor_test(memcached_st *memc)
643 {
644 char *context= "foo bad";
645 memcached_server_function callbacks[1];
646
647 callbacks[0]= server_function;
648 memcached_server_cursor(memc, callbacks, context, 1);
649
650 return 0;
651 }
652
653 static test_return bad_key_test(memcached_st *memc)
654 {
655 memcached_return rc;
656 char *key= "foo bad";
657 char *string;
658 size_t string_length;
659 uint32_t flags;
660 memcached_st *memc_clone;
661 unsigned int set= 1;
662 size_t max_keylen= 0xffff;
663
664 memc_clone= memcached_clone(NULL, memc);
665 assert(memc_clone);
666
667 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
668 assert(rc == MEMCACHED_SUCCESS);
669
670 /* All keys are valid in the binary protocol (except for length) */
671 if (memcached_behavior_get(memc_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0)
672 {
673 string= memcached_get(memc_clone, key, strlen(key),
674 &string_length, &flags, &rc);
675 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
676 assert(string_length == 0);
677 assert(!string);
678
679 set= 0;
680 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
681 assert(rc == MEMCACHED_SUCCESS);
682 string= memcached_get(memc_clone, key, strlen(key),
683 &string_length, &flags, &rc);
684 assert(rc == MEMCACHED_NOTFOUND);
685 assert(string_length == 0);
686 assert(!string);
687
688 /* Test multi key for bad keys */
689 char *keys[] = { "GoodKey", "Bad Key", "NotMine" };
690 size_t key_lengths[] = { 7, 7, 7 };
691 set= 1;
692 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
693 assert(rc == MEMCACHED_SUCCESS);
694
695 rc= memcached_mget(memc_clone, keys, key_lengths, 3);
696 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
697
698 rc= memcached_mget_by_key(memc_clone, "foo daddy", 9, keys, key_lengths, 1);
699 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
700
701 max_keylen= 250;
702
703 /* The following test should be moved to the end of this function when the
704 memcached server is updated to allow max size length of the keys in the
705 binary protocol
706 */
707 rc= memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
708 assert(rc == MEMCACHED_SUCCESS);
709
710 char *longkey= malloc(max_keylen + 1);
711 if (longkey != NULL)
712 {
713 memset(longkey, 'a', max_keylen + 1);
714 string= memcached_get(memc_clone, longkey, max_keylen,
715 &string_length, &flags, &rc);
716 assert(rc == MEMCACHED_NOTFOUND);
717 assert(string_length == 0);
718 assert(!string);
719
720 string= memcached_get(memc_clone, longkey, max_keylen + 1,
721 &string_length, &flags, &rc);
722 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
723 assert(string_length == 0);
724 assert(!string);
725
726 free(longkey);
727 }
728 }
729
730 /* Make sure zero length keys are marked as bad */
731 set= 1;
732 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
733 assert(rc == MEMCACHED_SUCCESS);
734 string= memcached_get(memc_clone, key, 0,
735 &string_length, &flags, &rc);
736 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
737 assert(string_length == 0);
738 assert(!string);
739
740 memcached_free(memc_clone);
741
742 return 0;
743 }
744
745 #define READ_THROUGH_VALUE "set for me"
746 static memcached_return read_through_trigger(memcached_st *memc __attribute__((unused)),
747 char *key __attribute__((unused)),
748 size_t key_length __attribute__((unused)),
749 memcached_result_st *result)
750 {
751
752 return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
753 }
754
755 static test_return read_through(memcached_st *memc)
756 {
757 memcached_return rc;
758 char *key= "foo";
759 char *string;
760 size_t string_length;
761 uint32_t flags;
762 memcached_trigger_key cb= (memcached_trigger_key)read_through_trigger;
763
764 string= memcached_get(memc, key, strlen(key),
765 &string_length, &flags, &rc);
766
767 assert(rc == MEMCACHED_NOTFOUND);
768 assert(string_length == 0);
769 assert(!string);
770
771 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE,
772 *(void **)&cb);
773 assert(rc == MEMCACHED_SUCCESS);
774
775 string= memcached_get(memc, key, strlen(key),
776 &string_length, &flags, &rc);
777
778 assert(rc == MEMCACHED_SUCCESS);
779 assert(string_length == strlen(READ_THROUGH_VALUE));
780 assert(!strcmp(READ_THROUGH_VALUE, string));
781 free(string);
782
783 string= memcached_get(memc, key, strlen(key),
784 &string_length, &flags, &rc);
785
786 assert(rc == MEMCACHED_SUCCESS);
787 assert(string_length == strlen(READ_THROUGH_VALUE));
788 assert(!strcmp(READ_THROUGH_VALUE, string));
789 free(string);
790
791 return 0;
792 }
793
794 static memcached_return delete_trigger(memcached_st *ptr __attribute__((unused)),
795 const char *key,
796 size_t key_length __attribute__((unused)))
797 {
798 assert(key);
799
800 return MEMCACHED_SUCCESS;
801 }
802
803 static test_return delete_through(memcached_st *memc)
804 {
805 memcached_trigger_delete_key callback;
806 memcached_return rc;
807
808 callback= (memcached_trigger_delete_key)delete_trigger;
809
810 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, *(void**)&callback);
811 assert(rc == MEMCACHED_SUCCESS);
812
813 return 0;
814 }
815
816 static test_return get_test(memcached_st *memc)
817 {
818 memcached_return rc;
819 char *key= "foo";
820 char *string;
821 size_t string_length;
822 uint32_t flags;
823
824 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
825 assert(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
826
827 string= memcached_get(memc, key, strlen(key),
828 &string_length, &flags, &rc);
829
830 assert(rc == MEMCACHED_NOTFOUND);
831 assert(string_length == 0);
832 assert(!string);
833
834 return 0;
835 }
836
837 static test_return get_test2(memcached_st *memc)
838 {
839 memcached_return rc;
840 char *key= "foo";
841 char *value= "when we sanitize";
842 char *string;
843 size_t string_length;
844 uint32_t flags;
845
846 rc= memcached_set(memc, key, strlen(key),
847 value, strlen(value),
848 (time_t)0, (uint32_t)0);
849 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
850
851 string= memcached_get(memc, key, strlen(key),
852 &string_length, &flags, &rc);
853
854 assert(string);
855 assert(rc == MEMCACHED_SUCCESS);
856 assert(string_length == strlen(value));
857 assert(!memcmp(string, value, string_length));
858
859 free(string);
860
861 return 0;
862 }
863
864 static test_return set_test2(memcached_st *memc)
865 {
866 memcached_return rc;
867 char *key= "foo";
868 char *value= "train in the brain";
869 size_t value_length= strlen(value);
870 unsigned int x;
871
872 for (x= 0; x < 10; x++)
873 {
874 rc= memcached_set(memc, key, strlen(key),
875 value, value_length,
876 (time_t)0, (uint32_t)0);
877 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
878 }
879
880 return 0;
881 }
882
883 static test_return set_test3(memcached_st *memc)
884 {
885 memcached_return rc;
886 char *value;
887 size_t value_length= 8191;
888 unsigned int x;
889
890 value = (char*)malloc(value_length);
891 assert(value);
892
893 for (x= 0; x < value_length; x++)
894 value[x] = (char) (x % 127);
895
896 /* The dump test relies on there being at least 32 items in memcached */
897 for (x= 0; x < 32; x++)
898 {
899 char key[16];
900
901 sprintf(key, "foo%u", x);
902
903 rc= memcached_set(memc, key, strlen(key),
904 value, value_length,
905 (time_t)0, (uint32_t)0);
906 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
907 }
908
909 free(value);
910
911 return 0;
912 }
913
914 static test_return get_test3(memcached_st *memc)
915 {
916 memcached_return rc;
917 char *key= "foo";
918 char *value;
919 size_t value_length= 8191;
920 char *string;
921 size_t string_length;
922 uint32_t flags;
923 uint32_t x;
924
925 value = (char*)malloc(value_length);
926 assert(value);
927
928 for (x= 0; x < value_length; x++)
929 value[x] = (char) (x % 127);
930
931 rc= memcached_set(memc, key, strlen(key),
932 value, value_length,
933 (time_t)0, (uint32_t)0);
934 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
935
936 string= memcached_get(memc, key, strlen(key),
937 &string_length, &flags, &rc);
938
939 assert(rc == MEMCACHED_SUCCESS);
940 assert(string);
941 assert(string_length == value_length);
942 assert(!memcmp(string, value, string_length));
943
944 free(string);
945 free(value);
946
947 return 0;
948 }
949
950 static test_return get_test4(memcached_st *memc)
951 {
952 memcached_return rc;
953 char *key= "foo";
954 char *value;
955 size_t value_length= 8191;
956 char *string;
957 size_t string_length;
958 uint32_t flags;
959 uint32_t x;
960
961 value = (char*)malloc(value_length);
962 assert(value);
963
964 for (x= 0; x < value_length; x++)
965 value[x] = (char) (x % 127);
966
967 rc= memcached_set(memc, key, strlen(key),
968 value, value_length,
969 (time_t)0, (uint32_t)0);
970 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
971
972 for (x= 0; x < 10; x++)
973 {
974 string= memcached_get(memc, key, strlen(key),
975 &string_length, &flags, &rc);
976
977 assert(rc == MEMCACHED_SUCCESS);
978 assert(string);
979 assert(string_length == value_length);
980 assert(!memcmp(string, value, string_length));
981 free(string);
982 }
983
984 free(value);
985
986 return 0;
987 }
988
989 /*
990 * This test verifies that memcached_read_one_response doesn't try to
991 * dereference a NIL-pointer if you issue a multi-get and don't read out all
992 * responses before you execute a storage command.
993 */
994 static test_return get_test5(memcached_st *memc)
995 {
996 /*
997 ** Request the same key twice, to ensure that we hash to the same server
998 ** (so that we have multiple response values queued up) ;-)
999 */
1000 char *keys[]= { "key", "key" };
1001 size_t lengths[]= { 3, 3 };
1002 uint32_t flags;
1003 size_t rlen;
1004
1005 memcached_return rc= memcached_set(memc, keys[0], lengths[0],
1006 keys[0], lengths[0], 0, 0);
1007 assert(rc == MEMCACHED_SUCCESS);
1008 rc= memcached_mget(memc, keys, lengths, 2);
1009
1010 memcached_result_st results_obj;
1011 memcached_result_st *results;
1012 results=memcached_result_create(memc, &results_obj);
1013 assert(results);
1014 results=memcached_fetch_result(memc, &results_obj, &rc);
1015 assert(results);
1016 memcached_result_free(&results_obj);
1017
1018 /* Don't read out the second result, but issue a set instead.. */
1019 rc= memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0);
1020 assert(rc == MEMCACHED_SUCCESS);
1021
1022 char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
1023 &rlen, &flags, &rc);
1024 assert(val == NULL);
1025 assert(rc == MEMCACHED_NOTFOUND);
1026 val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
1027 assert(val != NULL);
1028 assert(rc == MEMCACHED_SUCCESS);
1029 free(val);
1030
1031 return TEST_SUCCESS;
1032 }
1033
1034 /* Do not copy the style of this code, I just access hosts to testthis function */
1035 static test_return stats_servername_test(memcached_st *memc)
1036 {
1037 memcached_return rc;
1038 memcached_stat_st memc_stat;
1039 rc= memcached_stat_servername(&memc_stat, NULL,
1040 memc->hosts[0].hostname,
1041 memc->hosts[0].port);
1042
1043 return 0;
1044 }
1045
1046 static test_return increment_test(memcached_st *memc)
1047 {
1048 uint64_t new_number;
1049 memcached_return rc;
1050 char *key= "number";
1051 char *value= "0";
1052
1053 rc= memcached_set(memc, key, strlen(key),
1054 value, strlen(value),
1055 (time_t)0, (uint32_t)0);
1056 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1057
1058 rc= memcached_increment(memc, key, strlen(key),
1059 1, &new_number);
1060 assert(rc == MEMCACHED_SUCCESS);
1061 assert(new_number == 1);
1062
1063 rc= memcached_increment(memc, key, strlen(key),
1064 1, &new_number);
1065 assert(rc == MEMCACHED_SUCCESS);
1066 assert(new_number == 2);
1067
1068 return 0;
1069 }
1070
1071 static test_return increment_with_initial_test(memcached_st *memc)
1072 {
1073 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1074 {
1075 uint64_t new_number;
1076 memcached_return rc;
1077 char *key= "number";
1078 uint64_t initial= 0;
1079
1080 rc= memcached_increment_with_initial(memc, key, strlen(key),
1081 1, initial, 0, &new_number);
1082 assert(rc == MEMCACHED_SUCCESS);
1083 assert(new_number == initial);
1084
1085 rc= memcached_increment_with_initial(memc, key, strlen(key),
1086 1, initial, 0, &new_number);
1087 assert(rc == MEMCACHED_SUCCESS);
1088 assert(new_number == (initial + 1));
1089 }
1090 return 0;
1091 }
1092
1093 static test_return decrement_test(memcached_st *memc)
1094 {
1095 uint64_t new_number;
1096 memcached_return rc;
1097 char *key= "number";
1098 char *value= "3";
1099
1100 rc= memcached_set(memc, key, strlen(key),
1101 value, strlen(value),
1102 (time_t)0, (uint32_t)0);
1103 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1104
1105 rc= memcached_decrement(memc, key, strlen(key),
1106 1, &new_number);
1107 assert(rc == MEMCACHED_SUCCESS);
1108 assert(new_number == 2);
1109
1110 rc= memcached_decrement(memc, key, strlen(key),
1111 1, &new_number);
1112 assert(rc == MEMCACHED_SUCCESS);
1113 assert(new_number == 1);
1114
1115 return 0;
1116 }
1117
1118 static test_return decrement_with_initial_test(memcached_st *memc)
1119 {
1120 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1121 {
1122 uint64_t new_number;
1123 memcached_return rc;
1124 char *key= "number";
1125 uint64_t initial= 3;
1126
1127 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1128 1, initial, 0, &new_number);
1129 assert(rc == MEMCACHED_SUCCESS);
1130 assert(new_number == initial);
1131
1132 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1133 1, initial, 0, &new_number);
1134 assert(rc == MEMCACHED_SUCCESS);
1135 assert(new_number == (initial - 1));
1136 }
1137 return 0;
1138 }
1139
1140 static test_return quit_test(memcached_st *memc)
1141 {
1142 memcached_return rc;
1143 char *key= "fudge";
1144 char *value= "sanford and sun";
1145
1146 rc= memcached_set(memc, key, strlen(key),
1147 value, strlen(value),
1148 (time_t)10, (uint32_t)3);
1149 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1150 memcached_quit(memc);
1151
1152 rc= memcached_set(memc, key, strlen(key),
1153 value, strlen(value),
1154 (time_t)50, (uint32_t)9);
1155 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1156
1157 return 0;
1158 }
1159
1160 static test_return mget_result_test(memcached_st *memc)
1161 {
1162 memcached_return rc;
1163 char *keys[]= {"fudge", "son", "food"};
1164 size_t key_length[]= {5, 3, 4};
1165 unsigned int x;
1166
1167 memcached_result_st results_obj;
1168 memcached_result_st *results;
1169
1170 results= memcached_result_create(memc, &results_obj);
1171 assert(results);
1172 assert(&results_obj == results);
1173
1174 /* We need to empty the server before continueing test */
1175 rc= memcached_flush(memc, 0);
1176 assert(rc == MEMCACHED_SUCCESS);
1177
1178 rc= memcached_mget(memc, keys, key_length, 3);
1179 assert(rc == MEMCACHED_SUCCESS);
1180
1181 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1182 {
1183 assert(results);
1184 }
1185
1186 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1187 assert(!results);
1188 assert(rc == MEMCACHED_END);
1189
1190 for (x= 0; x < 3; x++)
1191 {
1192 rc= memcached_set(memc, keys[x], key_length[x],
1193 keys[x], key_length[x],
1194 (time_t)50, (uint32_t)9);
1195 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1196 }
1197
1198 rc= memcached_mget(memc, keys, key_length, 3);
1199 assert(rc == MEMCACHED_SUCCESS);
1200
1201 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1202 {
1203 assert(results);
1204 assert(&results_obj == results);
1205 assert(rc == MEMCACHED_SUCCESS);
1206 assert(memcached_result_key_length(results) == memcached_result_length(results));
1207 assert(!memcmp(memcached_result_key_value(results),
1208 memcached_result_value(results),
1209 memcached_result_length(results)));
1210 }
1211
1212 memcached_result_free(&results_obj);
1213
1214 return 0;
1215 }
1216
1217 static test_return mget_result_alloc_test(memcached_st *memc)
1218 {
1219 memcached_return rc;
1220 char *keys[]= {"fudge", "son", "food"};
1221 size_t key_length[]= {5, 3, 4};
1222 unsigned int x;
1223
1224 memcached_result_st *results;
1225
1226 /* We need to empty the server before continueing test */
1227 rc= memcached_flush(memc, 0);
1228 assert(rc == MEMCACHED_SUCCESS);
1229
1230 rc= memcached_mget(memc, keys, key_length, 3);
1231 assert(rc == MEMCACHED_SUCCESS);
1232
1233 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
1234 {
1235 assert(results);
1236 }
1237 assert(!results);
1238 assert(rc == MEMCACHED_END);
1239
1240 for (x= 0; x < 3; x++)
1241 {
1242 rc= memcached_set(memc, keys[x], key_length[x],
1243 keys[x], key_length[x],
1244 (time_t)50, (uint32_t)9);
1245 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1246 }
1247
1248 rc= memcached_mget(memc, keys, key_length, 3);
1249 assert(rc == MEMCACHED_SUCCESS);
1250
1251 x= 0;
1252 while ((results= memcached_fetch_result(memc, NULL, &rc)))
1253 {
1254 assert(results);
1255 assert(rc == MEMCACHED_SUCCESS);
1256 assert(memcached_result_key_length(results) == memcached_result_length(results));
1257 assert(!memcmp(memcached_result_key_value(results),
1258 memcached_result_value(results),
1259 memcached_result_length(results)));
1260 memcached_result_free(results);
1261 x++;
1262 }
1263
1264 return 0;
1265 }
1266
1267 /* Count the results */
1268 static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
1269 memcached_result_st *result __attribute__((unused)),
1270 void *context)
1271 {
1272 unsigned int *counter= (unsigned int *)context;
1273
1274 *counter= *counter + 1;
1275
1276 return MEMCACHED_SUCCESS;
1277 }
1278
1279 static test_return mget_result_function(memcached_st *memc)
1280 {
1281 memcached_return rc;
1282 char *keys[]= {"fudge", "son", "food"};
1283 size_t key_length[]= {5, 3, 4};
1284 unsigned int x;
1285 unsigned int counter;
1286 memcached_execute_function callbacks[1];
1287
1288 /* We need to empty the server before continueing test */
1289 rc= memcached_flush(memc, 0);
1290 for (x= 0; x < 3; x++)
1291 {
1292 rc= memcached_set(memc, keys[x], key_length[x],
1293 keys[x], key_length[x],
1294 (time_t)50, (uint32_t)9);
1295 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1296 }
1297
1298 rc= memcached_mget(memc, keys, key_length, 3);
1299 assert(rc == MEMCACHED_SUCCESS);
1300
1301 callbacks[0]= &callback_counter;
1302 counter= 0;
1303 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1304
1305 assert(counter == 3);
1306
1307 return 0;
1308 }
1309
1310 static test_return mget_test(memcached_st *memc)
1311 {
1312 memcached_return rc;
1313 char *keys[]= {"fudge", "son", "food"};
1314 size_t key_length[]= {5, 3, 4};
1315 unsigned int x;
1316 uint32_t flags;
1317
1318 char return_key[MEMCACHED_MAX_KEY];
1319 size_t return_key_length;
1320 char *return_value;
1321 size_t return_value_length;
1322
1323 /* We need to empty the server before continueing test */
1324 rc= memcached_flush(memc, 0);
1325 assert(rc == MEMCACHED_SUCCESS);
1326
1327 rc= memcached_mget(memc, keys, key_length, 3);
1328 assert(rc == MEMCACHED_SUCCESS);
1329
1330 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1331 &return_value_length, &flags, &rc)) != NULL)
1332 {
1333 assert(return_value);
1334 }
1335 assert(!return_value);
1336 assert(return_value_length == 0);
1337 assert(rc == MEMCACHED_END);
1338
1339 for (x= 0; x < 3; x++)
1340 {
1341 rc= memcached_set(memc, keys[x], key_length[x],
1342 keys[x], key_length[x],
1343 (time_t)50, (uint32_t)9);
1344 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1345 }
1346
1347 rc= memcached_mget(memc, keys, key_length, 3);
1348 assert(rc == MEMCACHED_SUCCESS);
1349
1350 x= 0;
1351 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1352 &return_value_length, &flags, &rc)))
1353 {
1354 assert(return_value);
1355 assert(rc == MEMCACHED_SUCCESS);
1356 assert(return_key_length == return_value_length);
1357 assert(!memcmp(return_value, return_key, return_value_length));
1358 free(return_value);
1359 x++;
1360 }
1361
1362 return 0;
1363 }
1364
1365 static test_return get_stats_keys(memcached_st *memc)
1366 {
1367 char **list;
1368 char **ptr;
1369 memcached_stat_st memc_stat;
1370 memcached_return rc;
1371
1372 list= memcached_stat_get_keys(memc, &memc_stat, &rc);
1373 assert(rc == MEMCACHED_SUCCESS);
1374 for (ptr= list; *ptr; ptr++)
1375 assert(*ptr);
1376 fflush(stdout);
1377
1378 free(list);
1379
1380 return 0;
1381 }
1382
1383 static test_return version_string_test(memcached_st *memc __attribute__((unused)))
1384 {
1385 const char *version_string;
1386
1387 version_string= memcached_lib_version();
1388
1389 assert(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
1390
1391 return 0;
1392 }
1393
1394 static test_return get_stats(memcached_st *memc)
1395 {
1396 unsigned int x;
1397 char **list;
1398 char **ptr;
1399 memcached_return rc;
1400 memcached_stat_st *memc_stat;
1401
1402 memc_stat= memcached_stat(memc, NULL, &rc);
1403 assert(rc == MEMCACHED_SUCCESS);
1404
1405 assert(rc == MEMCACHED_SUCCESS);
1406 assert(memc_stat);
1407
1408 for (x= 0; x < memcached_server_count(memc); x++)
1409 {
1410 list= memcached_stat_get_keys(memc, memc_stat+x, &rc);
1411 assert(rc == MEMCACHED_SUCCESS);
1412 for (ptr= list; *ptr; ptr++);
1413
1414 free(list);
1415 }
1416
1417 memcached_stat_free(NULL, memc_stat);
1418
1419 return 0;
1420 }
1421
1422 static test_return add_host_test(memcached_st *memc)
1423 {
1424 unsigned int x;
1425 memcached_server_st *servers;
1426 memcached_return rc;
1427 char servername[]= "0.example.com";
1428
1429 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
1430 assert(servers);
1431 assert(1 == memcached_server_list_count(servers));
1432
1433 for (x= 2; x < 20; x++)
1434 {
1435 char buffer[SMALL_STRING_LEN];
1436
1437 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1438 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
1439 &rc);
1440 assert(rc == MEMCACHED_SUCCESS);
1441 assert(x == memcached_server_list_count(servers));
1442 }
1443
1444 rc= memcached_server_push(memc, servers);
1445 assert(rc == MEMCACHED_SUCCESS);
1446 rc= memcached_server_push(memc, servers);
1447 assert(rc == MEMCACHED_SUCCESS);
1448
1449 memcached_server_list_free(servers);
1450
1451 return 0;
1452 }
1453
1454 static memcached_return clone_test_callback(memcached_st *parent __attribute__((unused)), memcached_st *memc_clone __attribute__((unused)))
1455 {
1456 return MEMCACHED_SUCCESS;
1457 }
1458
1459 static memcached_return cleanup_test_callback(memcached_st *ptr __attribute__((unused)))
1460 {
1461 return MEMCACHED_SUCCESS;
1462 }
1463
1464 static test_return callback_test(memcached_st *memc)
1465 {
1466 /* Test User Data */
1467 {
1468 int x= 5;
1469 int *test_ptr;
1470 memcached_return rc;
1471
1472 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
1473 assert(rc == MEMCACHED_SUCCESS);
1474 test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
1475 assert(*test_ptr == x);
1476 }
1477
1478 /* Test Clone Callback */
1479 {
1480 memcached_clone_func clone_cb= (memcached_clone_func)clone_test_callback;
1481 void *clone_cb_ptr= *(void **)&clone_cb;
1482 void *temp_function= NULL;
1483 memcached_return rc;
1484
1485 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1486 clone_cb_ptr);
1487 assert(rc == MEMCACHED_SUCCESS);
1488 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1489 assert(temp_function == clone_cb_ptr);
1490 }
1491
1492 /* Test Cleanup Callback */
1493 {
1494 memcached_cleanup_func cleanup_cb=
1495 (memcached_cleanup_func)cleanup_test_callback;
1496 void *cleanup_cb_ptr= *(void **)&cleanup_cb;
1497 void *temp_function= NULL;
1498 memcached_return rc;
1499
1500 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1501 cleanup_cb_ptr);
1502 assert(rc == MEMCACHED_SUCCESS);
1503 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1504 assert(temp_function == cleanup_cb_ptr);
1505 }
1506
1507 return 0;
1508 }
1509
1510 /* We don't test the behavior itself, we test the switches */
1511 static test_return behavior_test(memcached_st *memc)
1512 {
1513 uint64_t value;
1514 uint32_t set= 1;
1515
1516 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1517 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1518 assert(value == 1);
1519
1520 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1521 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1522 assert(value == 1);
1523
1524 set= MEMCACHED_HASH_MD5;
1525 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1526 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1527 assert(value == MEMCACHED_HASH_MD5);
1528
1529 set= 0;
1530
1531 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1532 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1533 assert(value == 0);
1534
1535 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1536 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1537 assert(value == 0);
1538
1539 set= MEMCACHED_HASH_DEFAULT;
1540 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1541 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1542 assert(value == MEMCACHED_HASH_DEFAULT);
1543
1544 set= MEMCACHED_HASH_CRC;
1545 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1546 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1547 assert(value == MEMCACHED_HASH_CRC);
1548
1549 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1550 assert(value > 0);
1551
1552 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1553 assert(value > 0);
1554
1555 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
1556 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, value + 1);
1557 assert((value + 1) == memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
1558 return 0;
1559 }
1560
1561 /* Test case provided by Cal Haldenbrand */
1562 static test_return user_supplied_bug1(memcached_st *memc)
1563 {
1564 unsigned int setter= 1;
1565 unsigned int x;
1566
1567 unsigned long long total= 0;
1568 uint32_t size= 0;
1569 char key[10];
1570 char randomstuff[6 * 1024];
1571 memcached_return rc;
1572
1573 memset(randomstuff, 0, 6 * 1024);
1574
1575 /* We just keep looking at the same values over and over */
1576 srandom(10);
1577
1578 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1579 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1580
1581
1582 /* add key */
1583 for (x= 0 ; total < 20 * 1024576 ; x++ )
1584 {
1585 unsigned int j= 0;
1586
1587 size= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1588 memset(randomstuff, 0, 6 * 1024);
1589 assert(size < 6 * 1024); /* Being safe here */
1590
1591 for (j= 0 ; j < size ;j++)
1592 randomstuff[j] = (signed char) ((rand() % 26) + 97);
1593
1594 total += size;
1595 sprintf(key, "%d", x);
1596 rc = memcached_set(memc, key, strlen(key),
1597 randomstuff, strlen(randomstuff), 10, 0);
1598 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1599 /* If we fail, lets try again */
1600 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
1601 rc = memcached_set(memc, key, strlen(key),
1602 randomstuff, strlen(randomstuff), 10, 0);
1603 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1604 }
1605
1606 return 0;
1607 }
1608
1609 /* Test case provided by Cal Haldenbrand */
1610 static test_return user_supplied_bug2(memcached_st *memc)
1611 {
1612 int errors;
1613 unsigned int setter;
1614 unsigned int x;
1615 unsigned long long total;
1616
1617 setter= 1;
1618 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1619 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1620 #ifdef NOT_YET
1621 setter = 20 * 1024576;
1622 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1623 setter = 20 * 1024576;
1624 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1625 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1626 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1627
1628 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
1629 #endif
1630
1631 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
1632 {
1633 memcached_return rc= MEMCACHED_SUCCESS;
1634 char buffer[SMALL_STRING_LEN];
1635 uint32_t flags= 0;
1636 size_t val_len= 0;
1637 char *getval;
1638
1639 memset(buffer, 0, SMALL_STRING_LEN);
1640
1641 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
1642 getval= memcached_get(memc, buffer, strlen(buffer),
1643 &val_len, &flags, &rc);
1644 if (rc != MEMCACHED_SUCCESS)
1645 {
1646 if (rc == MEMCACHED_NOTFOUND)
1647 errors++;
1648 else
1649 {
1650 WATCHPOINT_ERROR(rc);
1651 assert(0);
1652 }
1653
1654 continue;
1655 }
1656 total+= val_len;
1657 errors= 0;
1658 free(getval);
1659 }
1660
1661 return 0;
1662 }
1663
1664 /* Do a large mget() over all the keys we think exist */
1665 #define KEY_COUNT 3000 // * 1024576
1666 static test_return user_supplied_bug3(memcached_st *memc)
1667 {
1668 memcached_return rc;
1669 unsigned int setter;
1670 unsigned int x;
1671 char **keys;
1672 size_t key_lengths[KEY_COUNT];
1673
1674 setter= 1;
1675 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1676 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1677 #ifdef NOT_YET
1678 setter = 20 * 1024576;
1679 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1680 setter = 20 * 1024576;
1681 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1682 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1683 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1684 #endif
1685
1686 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
1687 assert(keys);
1688 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
1689 for (x= 0; x < KEY_COUNT; x++)
1690 {
1691 char buffer[30];
1692
1693 snprintf(buffer, 30, "%u", x);
1694 keys[x]= strdup(buffer);
1695 key_lengths[x]= strlen(keys[x]);
1696 }
1697
1698 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
1699 assert(rc == MEMCACHED_SUCCESS);
1700
1701 /* Turn this into a help function */
1702 {
1703 char return_key[MEMCACHED_MAX_KEY];
1704 size_t return_key_length;
1705 char *return_value;
1706 size_t return_value_length;
1707 uint32_t flags;
1708
1709 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1710 &return_value_length, &flags, &rc)))
1711 {
1712 assert(return_value);
1713 assert(rc == MEMCACHED_SUCCESS);
1714 free(return_value);
1715 }
1716 }
1717
1718 for (x= 0; x < KEY_COUNT; x++)
1719 free(keys[x]);
1720 free(keys);
1721
1722 return 0;
1723 }
1724
1725 /* Make sure we behave properly if server list has no values */
1726 static test_return user_supplied_bug4(memcached_st *memc)
1727 {
1728 memcached_return rc;
1729 char *keys[]= {"fudge", "son", "food"};
1730 size_t key_length[]= {5, 3, 4};
1731 unsigned int x;
1732 uint32_t flags;
1733 char return_key[MEMCACHED_MAX_KEY];
1734 size_t return_key_length;
1735 char *return_value;
1736 size_t return_value_length;
1737
1738 /* Here we free everything before running a bunch of mget tests */
1739 {
1740 memcached_server_list_free(memc->hosts);
1741 memc->hosts= NULL;
1742 memc->number_of_hosts= 0;
1743 }
1744
1745
1746 /* We need to empty the server before continueing test */
1747 rc= memcached_flush(memc, 0);
1748 assert(rc == MEMCACHED_NO_SERVERS);
1749
1750 rc= memcached_mget(memc, keys, key_length, 3);
1751 assert(rc == MEMCACHED_NO_SERVERS);
1752
1753 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1754 &return_value_length, &flags, &rc)) != NULL)
1755 {
1756 assert(return_value);
1757 }
1758 assert(!return_value);
1759 assert(return_value_length == 0);
1760 assert(rc == MEMCACHED_NO_SERVERS);
1761
1762 for (x= 0; x < 3; x++)
1763 {
1764 rc= memcached_set(memc, keys[x], key_length[x],
1765 keys[x], key_length[x],
1766 (time_t)50, (uint32_t)9);
1767 assert(rc == MEMCACHED_NO_SERVERS);
1768 }
1769
1770 rc= memcached_mget(memc, keys, key_length, 3);
1771 assert(rc == MEMCACHED_NO_SERVERS);
1772
1773 x= 0;
1774 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1775 &return_value_length, &flags, &rc)))
1776 {
1777 assert(return_value);
1778 assert(rc == MEMCACHED_SUCCESS);
1779 assert(return_key_length == return_value_length);
1780 assert(!memcmp(return_value, return_key, return_value_length));
1781 free(return_value);
1782 x++;
1783 }
1784
1785 return 0;
1786 }
1787
1788 #define VALUE_SIZE_BUG5 1048064
1789 static test_return user_supplied_bug5(memcached_st *memc)
1790 {
1791 memcached_return rc;
1792 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1793 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1794 char return_key[MEMCACHED_MAX_KEY];
1795 size_t return_key_length;
1796 char *value;
1797 size_t value_length;
1798 uint32_t flags;
1799 unsigned int count;
1800 unsigned int x;
1801 char insert_data[VALUE_SIZE_BUG5];
1802
1803 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1804 insert_data[x]= (signed char)rand();
1805
1806 memcached_flush(memc, 0);
1807 value= memcached_get(memc, keys[0], key_length[0],
1808 &value_length, &flags, &rc);
1809 assert(value == NULL);
1810 rc= memcached_mget(memc, keys, key_length, 4);
1811
1812 count= 0;
1813 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1814 &value_length, &flags, &rc)))
1815 count++;
1816 assert(count == 0);
1817
1818 for (x= 0; x < 4; x++)
1819 {
1820 rc= memcached_set(memc, keys[x], key_length[x],
1821 insert_data, VALUE_SIZE_BUG5,
1822 (time_t)0, (uint32_t)0);
1823 assert(rc == MEMCACHED_SUCCESS);
1824 }
1825
1826 for (x= 0; x < 10; x++)
1827 {
1828 value= memcached_get(memc, keys[0], key_length[0],
1829 &value_length, &flags, &rc);
1830 assert(value);
1831 free(value);
1832
1833 rc= memcached_mget(memc, keys, key_length, 4);
1834 count= 0;
1835 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1836 &value_length, &flags, &rc)))
1837 {
1838 count++;
1839 free(value);
1840 }
1841 assert(count == 4);
1842 }
1843
1844 return 0;
1845 }
1846
1847 static test_return user_supplied_bug6(memcached_st *memc)
1848 {
1849 memcached_return rc;
1850 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1851 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1852 char return_key[MEMCACHED_MAX_KEY];
1853 size_t return_key_length;
1854 char *value;
1855 size_t value_length;
1856 uint32_t flags;
1857 unsigned int count;
1858 unsigned int x;
1859 char insert_data[VALUE_SIZE_BUG5];
1860
1861 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1862 insert_data[x]= (signed char)rand();
1863
1864 memcached_flush(memc, 0);
1865 value= memcached_get(memc, keys[0], key_length[0],
1866 &value_length, &flags, &rc);
1867 assert(value == NULL);
1868 assert(rc == MEMCACHED_NOTFOUND);
1869 rc= memcached_mget(memc, keys, key_length, 4);
1870 assert(rc == MEMCACHED_SUCCESS);
1871
1872 count= 0;
1873 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1874 &value_length, &flags, &rc)))
1875 count++;
1876 assert(count == 0);
1877 assert(rc == MEMCACHED_END);
1878
1879 for (x= 0; x < 4; x++)
1880 {
1881 rc= memcached_set(memc, keys[x], key_length[x],
1882 insert_data, VALUE_SIZE_BUG5,
1883 (time_t)0, (uint32_t)0);
1884 assert(rc == MEMCACHED_SUCCESS);
1885 }
1886
1887 for (x= 0; x < 2; x++)
1888 {
1889 value= memcached_get(memc, keys[0], key_length[0],
1890 &value_length, &flags, &rc);
1891 assert(value);
1892 free(value);
1893
1894 rc= memcached_mget(memc, keys, key_length, 4);
1895 assert(rc == MEMCACHED_SUCCESS);
1896 count= 3;
1897 /* We test for purge of partial complete fetches */
1898 for (count= 3; count; count--)
1899 {
1900 value= memcached_fetch(memc, return_key, &return_key_length,
1901 &value_length, &flags, &rc);
1902 assert(rc == MEMCACHED_SUCCESS);
1903 assert(!(memcmp(value, insert_data, value_length)));
1904 assert(value_length);
1905 free(value);
1906 }
1907 }
1908
1909 return 0;
1910 }
1911
1912 static test_return user_supplied_bug8(memcached_st *memc __attribute__((unused)))
1913 {
1914 memcached_return rc;
1915 memcached_st *mine;
1916 memcached_st *memc_clone;
1917
1918 memcached_server_st *servers;
1919 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";
1920
1921 servers= memcached_servers_parse(server_list);
1922 assert(servers);
1923
1924 mine= memcached_create(NULL);
1925 rc= memcached_server_push(mine, servers);
1926 assert(rc == MEMCACHED_SUCCESS);
1927 memcached_server_list_free(servers);
1928
1929 assert(mine);
1930 memc_clone= memcached_clone(NULL, mine);
1931
1932 memcached_quit(mine);
1933 memcached_quit(memc_clone);
1934
1935
1936 memcached_free(mine);
1937 memcached_free(memc_clone);
1938
1939 return 0;
1940 }
1941
1942 /* Test flag store/retrieve */
1943 static test_return user_supplied_bug7(memcached_st *memc)
1944 {
1945 memcached_return rc;
1946 char *keys= "036790384900";
1947 size_t key_length= strlen("036790384900");
1948 char return_key[MEMCACHED_MAX_KEY];
1949 size_t return_key_length;
1950 char *value;
1951 size_t value_length;
1952 uint32_t flags;
1953 unsigned int x;
1954 char insert_data[VALUE_SIZE_BUG5];
1955
1956 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1957 insert_data[x]= (signed char)rand();
1958
1959 memcached_flush(memc, 0);
1960
1961 flags= 245;
1962 rc= memcached_set(memc, keys, key_length,
1963 insert_data, VALUE_SIZE_BUG5,
1964 (time_t)0, flags);
1965 assert(rc == MEMCACHED_SUCCESS);
1966
1967 flags= 0;
1968 value= memcached_get(memc, keys, key_length,
1969 &value_length, &flags, &rc);
1970 assert(flags == 245);
1971 assert(value);
1972 free(value);
1973
1974 rc= memcached_mget(memc, &keys, &key_length, 1);
1975
1976 flags= 0;
1977 value= memcached_fetch(memc, return_key, &return_key_length,
1978 &value_length, &flags, &rc);
1979 assert(flags == 245);
1980 assert(value);
1981 free(value);
1982
1983
1984 return 0;
1985 }
1986
1987 static test_return user_supplied_bug9(memcached_st *memc)
1988 {
1989 memcached_return rc;
1990 char *keys[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1991 size_t key_length[3];
1992 unsigned int x;
1993 uint32_t flags;
1994 unsigned count= 0;
1995
1996 char return_key[MEMCACHED_MAX_KEY];
1997 size_t return_key_length;
1998 char *return_value;
1999 size_t return_value_length;
2000
2001
2002 key_length[0]= strlen("UDATA:edevil@sapo.pt");
2003 key_length[1]= strlen("fudge&*@#");
2004 key_length[2]= strlen("for^#@&$not");
2005
2006
2007 for (x= 0; x < 3; x++)
2008 {
2009 rc= memcached_set(memc, keys[x], key_length[x],
2010 keys[x], key_length[x],
2011 (time_t)50, (uint32_t)9);
2012 assert(rc == MEMCACHED_SUCCESS);
2013 }
2014
2015 rc= memcached_mget(memc, keys, key_length, 3);
2016 assert(rc == MEMCACHED_SUCCESS);
2017
2018 /* We need to empty the server before continueing test */
2019 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2020 &return_value_length, &flags, &rc)) != NULL)
2021 {
2022 assert(return_value);
2023 free(return_value);
2024 count++;
2025 }
2026 assert(count == 3);
2027
2028 return 0;
2029 }
2030
2031 /* We are testing with aggressive timeout to get failures */
2032 static test_return user_supplied_bug10(memcached_st *memc)
2033 {
2034 char *key= "foo";
2035 char *value;
2036 size_t value_length= 512;
2037 unsigned int x;
2038 size_t key_len= 3;
2039 memcached_return rc;
2040 unsigned int set= 1;
2041 memcached_st *mclone= memcached_clone(NULL, memc);
2042 int32_t timeout;
2043
2044 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
2045 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
2046 timeout= 2;
2047 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
2048 (uint64_t)timeout);
2049
2050 value = (char*)malloc(value_length * sizeof(char));
2051
2052 for (x= 0; x < value_length; x++)
2053 value[x]= (char) (x % 127);
2054
2055 for (x= 1; x <= 100000; ++x)
2056 {
2057 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
2058
2059 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE ||
2060 rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT);
2061
2062 if (rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_TIMEOUT)
2063 x--;
2064 }
2065
2066 free(value);
2067 memcached_free(mclone);
2068
2069 return 0;
2070 }
2071
2072 /*
2073 We are looking failures in the async protocol
2074 */
2075 static test_return user_supplied_bug11(memcached_st *memc)
2076 {
2077 char *key= "foo";
2078 char *value;
2079 size_t value_length= 512;
2080 unsigned int x;
2081 size_t key_len= 3;
2082 memcached_return rc;
2083 unsigned int set= 1;
2084 int32_t timeout;
2085 memcached_st *mclone= memcached_clone(NULL, memc);
2086
2087 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
2088 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
2089 timeout= -1;
2090 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
2091 (size_t)timeout);
2092
2093 timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
2094
2095 assert(timeout == -1);
2096
2097 value = (char*)malloc(value_length * sizeof(char));
2098
2099 for (x= 0; x < value_length; x++)
2100 value[x]= (char) (x % 127);
2101
2102 for (x= 1; x <= 100000; ++x)
2103 {
2104 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
2105 }
2106
2107 free(value);
2108 memcached_free(mclone);
2109
2110 return 0;
2111 }
2112
2113 /*
2114 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2115 */
2116 static test_return user_supplied_bug12(memcached_st *memc)
2117 {
2118 memcached_return rc;
2119 uint32_t flags;
2120 size_t value_length;
2121 char *value;
2122 uint64_t number_value;
2123
2124 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
2125 &value_length, &flags, &rc);
2126 assert(value == NULL);
2127 assert(rc == MEMCACHED_NOTFOUND);
2128
2129 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
2130 1, &number_value);
2131
2132 assert(value == NULL);
2133 /* The binary protocol will set the key if it doesn't exist */
2134 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1)
2135 assert(rc == MEMCACHED_SUCCESS);
2136 else
2137 assert(rc == MEMCACHED_NOTFOUND);
2138
2139 rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2140
2141 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
2142 &value_length, &flags, &rc);
2143 assert(value);
2144 assert(rc == MEMCACHED_SUCCESS);
2145 free(value);
2146
2147 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
2148 1, &number_value);
2149 assert(number_value == 2);
2150 assert(rc == MEMCACHED_SUCCESS);
2151
2152 return 0;
2153 }
2154
2155 /*
2156 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2157 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2158 */
2159 static test_return user_supplied_bug13(memcached_st *memc)
2160 {
2161 char key[] = "key34567890";
2162 char *overflow;
2163 memcached_return rc;
2164 size_t overflowSize;
2165
2166 char commandFirst[]= "set key34567890 0 0 ";
2167 char commandLast[] = " \r\n"; /* first line of command sent to server */
2168 size_t commandLength;
2169 size_t testSize;
2170
2171 commandLength = strlen(commandFirst) + strlen(commandLast) + 4; /* 4 is number of characters in size, probably 8196 */
2172
2173 overflowSize = MEMCACHED_MAX_BUFFER - commandLength;
2174
2175 for (testSize= overflowSize - 1; testSize < overflowSize + 1; testSize++)
2176 {
2177 overflow= malloc(testSize);
2178 assert(overflow != NULL);
2179
2180 memset(overflow, 'x', testSize);
2181 rc= memcached_set(memc, key, strlen(key),
2182 overflow, testSize, 0, 0);
2183 assert(rc == MEMCACHED_SUCCESS);
2184 free(overflow);
2185 }
2186
2187 return 0;
2188 }
2189
2190
2191 /*
2192 Test values of many different sizes
2193 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2194 set key34567890 0 0 8169 \r\n
2195 is sent followed by buffer of size 8169, followed by 8169
2196 */
2197 static test_return user_supplied_bug14(memcached_st *memc)
2198 {
2199 size_t setter= 1;
2200 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2201 memcached_return rc;
2202 char *key= "foo";
2203 char *value;
2204 size_t value_length= 18000;
2205 char *string;
2206 size_t string_length;
2207 uint32_t flags;
2208 unsigned int x;
2209 size_t current_length;
2210
2211 value = (char*)malloc(value_length);
2212 assert(value);
2213
2214 for (x= 0; x < value_length; x++)
2215 value[x] = (char) (x % 127);
2216
2217 for (current_length= 0; current_length < value_length; current_length++)
2218 {
2219 rc= memcached_set(memc, key, strlen(key),
2220 value, current_length,
2221 (time_t)0, (uint32_t)0);
2222 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
2223
2224 string= memcached_get(memc, key, strlen(key),
2225 &string_length, &flags, &rc);
2226
2227 assert(rc == MEMCACHED_SUCCESS);
2228 assert(string_length == current_length);
2229 assert(!memcmp(string, value, string_length));
2230
2231 free(string);
2232 }
2233
2234 free(value);
2235
2236 return 0;
2237 }
2238
2239 /*
2240 Look for zero length value problems
2241 */
2242 static test_return user_supplied_bug15(memcached_st *memc)
2243 {
2244 uint32_t x;
2245 memcached_return rc;
2246 char *key= "mykey";
2247 char *value;
2248 size_t length;
2249 uint32_t flags;
2250
2251 for (x= 0; x < 2; x++)
2252 {
2253 rc= memcached_set(memc, key, strlen(key),
2254 NULL, 0,
2255 (time_t)0, (uint32_t)0);
2256
2257 assert(rc == MEMCACHED_SUCCESS);
2258
2259 value= memcached_get(memc, key, strlen(key),
2260 &length, &flags, &rc);
2261
2262 assert(rc == MEMCACHED_SUCCESS);
2263 assert(value == NULL);
2264 assert(length == 0);
2265 assert(flags == 0);
2266
2267 value= memcached_get(memc, key, strlen(key),
2268 &length, &flags, &rc);
2269
2270 assert(rc == MEMCACHED_SUCCESS);
2271 assert(value == NULL);
2272 assert(length == 0);
2273 assert(flags == 0);
2274 }
2275
2276 return 0;
2277 }
2278
2279 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2280 static test_return user_supplied_bug16(memcached_st *memc)
2281 {
2282 memcached_return rc;
2283 char *key= "mykey";
2284 char *value;
2285 size_t length;
2286 uint32_t flags;
2287
2288 rc= memcached_set(memc, key, strlen(key),
2289 NULL, 0,
2290 (time_t)0, UINT32_MAX);
2291
2292 assert(rc == MEMCACHED_SUCCESS);
2293
2294 value= memcached_get(memc, key, strlen(key),
2295 &length, &flags, &rc);
2296
2297 assert(rc == MEMCACHED_SUCCESS);
2298 assert(value == NULL);
2299 assert(length == 0);
2300 assert(flags == UINT32_MAX);
2301
2302 return 0;
2303 }
2304
2305 /* Check the validity of chinese key*/
2306 static test_return user_supplied_bug17(memcached_st *memc)
2307 {
2308 memcached_return rc;
2309 char *key= "豆瓣";
2310 char *value="我们在炎热抑郁的夏天无法停止豆瓣";
2311 char *value2;
2312 size_t length;
2313 uint32_t flags;
2314
2315 rc= memcached_set(memc, key, strlen(key),
2316 value, strlen(value),
2317 (time_t)0, 0);
2318
2319 assert(rc == MEMCACHED_SUCCESS);
2320
2321 value2= memcached_get(memc, key, strlen(key),
2322 &length, &flags, &rc);
2323
2324 assert(length==strlen(value));
2325 assert(rc == MEMCACHED_SUCCESS);
2326 assert(memcmp(value, value2, length)==0);
2327 free(value2);
2328
2329 return 0;
2330 }
2331
2332 /*
2333 From Andrei on IRC
2334 */
2335
2336 static test_return user_supplied_bug19(memcached_st *memc)
2337 {
2338 memcached_st *m;
2339 memcached_server_st *s;
2340 memcached_return res;
2341
2342 (void)memc;
2343
2344 m= memcached_create(NULL);
2345 memcached_server_add_with_weight(m, "localhost", 11311, 100);
2346 memcached_server_add_with_weight(m, "localhost", 11312, 100);
2347
2348 s= memcached_server_by_key(m, "a", 1, &res);
2349 memcached_server_free(s);
2350
2351 memcached_free(m);
2352
2353 return 0;
2354 }
2355
2356 /* CAS test from Andei */
2357 static test_return user_supplied_bug20(memcached_st *memc)
2358 {
2359 memcached_return status;
2360 memcached_result_st *result, result_obj;
2361 char *key = "abc";
2362 size_t key_len = strlen("abc");
2363 char *value = "foobar";
2364 size_t value_len = strlen(value);
2365
2366 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
2367
2368 status = memcached_set(memc, key, key_len, value, value_len, (time_t)0, (uint32_t)0);
2369 assert(status == MEMCACHED_SUCCESS);
2370
2371 status = memcached_mget(memc, &key, &key_len, 1);
2372 assert(status == MEMCACHED_SUCCESS);
2373
2374 result= memcached_result_create(memc, &result_obj);
2375 assert(result);
2376
2377 memcached_result_create(memc, &result_obj);
2378 result= memcached_fetch_result(memc, &result_obj, &status);
2379
2380 assert(result);
2381 assert(status == MEMCACHED_SUCCESS);
2382
2383 memcached_result_free(result);
2384
2385 return 0;
2386 }
2387
2388 #include "ketama_test_cases.h"
2389 static test_return user_supplied_bug18(memcached_st *trash)
2390 {
2391 memcached_return rc;
2392 uint64_t value;
2393 int x;
2394 memcached_server_st *server_pool;
2395 memcached_st *memc;
2396
2397 (void)trash;
2398
2399 memc= memcached_create(NULL);
2400 assert(memc);
2401
2402 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2403 assert(rc == MEMCACHED_SUCCESS);
2404
2405 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2406 assert(value == 1);
2407
2408 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2409 assert(rc == MEMCACHED_SUCCESS);
2410
2411 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2412 assert(value == MEMCACHED_HASH_MD5);
2413
2414 server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
2415 memcached_server_push(memc, server_pool);
2416
2417 /* verify that the server list was parsed okay. */
2418 assert(memc->number_of_hosts == 8);
2419 assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
2420 assert(server_pool[0].port == 11211);
2421 assert(server_pool[0].weight == 600);
2422 assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
2423 assert(server_pool[2].port == 11211);
2424 assert(server_pool[2].weight == 200);
2425 assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
2426 assert(server_pool[7].port == 11211);
2427 assert(server_pool[7].weight == 100);
2428
2429 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2430 * us test the boundary wraparound.
2431 */
2432 assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
2433
2434 /* verify the standard ketama set. */
2435 for (x= 0; x < 99; x++)
2436 {
2437 uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key));
2438 char *hostname = memc->hosts[server_idx].hostname;
2439 assert(strcmp(hostname, test_cases[x].server) == 0);
2440 }
2441
2442 memcached_server_list_free(server_pool);
2443 memcached_free(memc);
2444
2445 return 0;
2446 }
2447
2448 static test_return auto_eject_hosts(memcached_st *trash)
2449 {
2450 (void) trash;
2451
2452 memcached_return rc;
2453 memcached_st *memc= memcached_create(NULL);
2454 assert(memc);
2455
2456 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2457 assert(rc == MEMCACHED_SUCCESS);
2458
2459 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2460 assert(value == 1);
2461
2462 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2463 assert(rc == MEMCACHED_SUCCESS);
2464
2465 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2466 assert(value == MEMCACHED_HASH_MD5);
2467
2468 /* server should be removed when in delay */
2469 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS, 1);
2470 assert(rc == MEMCACHED_SUCCESS);
2471
2472 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
2473 assert(value == 1);
2474
2475 memcached_server_st *server_pool;
2476 server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
2477 memcached_server_push(memc, server_pool);
2478
2479 /* verify that the server list was parsed okay. */
2480 assert(memc->number_of_hosts == 8);
2481 assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
2482 assert(server_pool[0].port == 11211);
2483 assert(server_pool[0].weight == 600);
2484 assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
2485 assert(server_pool[2].port == 11211);
2486 assert(server_pool[2].weight == 200);
2487 assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
2488 assert(server_pool[7].port == 11211);
2489 assert(server_pool[7].weight == 100);
2490
2491 memc->hosts[2].next_retry = time(NULL) + 15;
2492 memc->next_distribution_rebuild= time(NULL) - 1;
2493
2494 for (int x= 0; x < 99; x++)
2495 {
2496 uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key));
2497 assert(server_idx != 2);
2498 }
2499
2500 /* and re-added when it's back. */
2501 memc->hosts[2].next_retry = time(NULL) - 1;
2502 memc->next_distribution_rebuild= time(NULL) - 1;
2503 run_distribution(memc);
2504 for (int x= 0; x < 99; x++)
2505 {
2506 uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key));
2507 char *hostname = memc->hosts[server_idx].hostname;
2508 assert(strcmp(hostname, test_cases[x].server) == 0);
2509 }
2510
2511 memcached_server_list_free(server_pool);
2512 memcached_free(memc);
2513
2514 return TEST_SUCCESS;
2515 }
2516
2517 static test_return result_static(memcached_st *memc)
2518 {
2519 memcached_result_st result;
2520 memcached_result_st *result_ptr;
2521
2522 result_ptr= memcached_result_create(memc, &result);
2523 assert(result.is_allocated == false);
2524 assert(result_ptr);
2525 memcached_result_free(&result);
2526
2527 return 0;
2528 }
2529
2530 static test_return result_alloc(memcached_st *memc)
2531 {
2532 memcached_result_st *result;
2533
2534 result= memcached_result_create(memc, NULL);
2535 assert(result);
2536 memcached_result_free(result);
2537
2538 return 0;
2539 }
2540
2541 static test_return string_static_null(memcached_st *memc)
2542 {
2543 memcached_string_st string;
2544 memcached_string_st *string_ptr;
2545
2546 string_ptr= memcached_string_create(memc, &string, 0);
2547 assert(string.is_allocated == false);
2548 assert(string_ptr);
2549 memcached_string_free(&string);
2550
2551 return 0;
2552 }
2553
2554 static test_return string_alloc_null(memcached_st *memc)
2555 {
2556 memcached_string_st *string;
2557
2558 string= memcached_string_create(memc, NULL, 0);
2559 assert(string);
2560 memcached_string_free(string);
2561
2562 return 0;
2563 }
2564
2565 static test_return string_alloc_with_size(memcached_st *memc)
2566 {
2567 memcached_string_st *string;
2568
2569 string= memcached_string_create(memc, NULL, 1024);
2570 assert(string);
2571 memcached_string_free(string);
2572
2573 return 0;
2574 }
2575
2576 static test_return string_alloc_with_size_toobig(memcached_st *memc)
2577 {
2578 memcached_string_st *string;
2579
2580 string= memcached_string_create(memc, NULL, SIZE_MAX);
2581 assert(string == NULL);
2582
2583 return 0;
2584 }
2585
2586 static test_return string_alloc_append(memcached_st *memc)
2587 {
2588 unsigned int x;
2589 char buffer[SMALL_STRING_LEN];
2590 memcached_string_st *string;
2591
2592 /* Ring the bell! */
2593 memset(buffer, 6, SMALL_STRING_LEN);
2594
2595 string= memcached_string_create(memc, NULL, 100);
2596 assert(string);
2597
2598 for (x= 0; x < 1024; x++)
2599 {
2600 memcached_return rc;
2601 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2602 assert(rc == MEMCACHED_SUCCESS);
2603 }
2604 memcached_string_free(string);
2605
2606 return 0;
2607 }
2608
2609 static test_return string_alloc_append_toobig(memcached_st *memc)
2610 {
2611 memcached_return rc;
2612 unsigned int x;
2613 char buffer[SMALL_STRING_LEN];
2614 memcached_string_st *string;
2615
2616 /* Ring the bell! */
2617 memset(buffer, 6, SMALL_STRING_LEN);
2618
2619 string= memcached_string_create(memc, NULL, 100);
2620 assert(string);
2621
2622 for (x= 0; x < 1024; x++)
2623 {
2624 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2625 assert(rc == MEMCACHED_SUCCESS);
2626 }
2627 rc= memcached_string_append(string, buffer, SIZE_MAX);
2628 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
2629 memcached_string_free(string);
2630
2631 return 0;
2632 }
2633
2634 static test_return cleanup_pairs(memcached_st *memc __attribute__((unused)))
2635 {
2636 pairs_free(global_pairs);
2637
2638 return 0;
2639 }
2640
2641 static test_return generate_pairs(memcached_st *memc __attribute__((unused)))
2642 {
2643 unsigned long long x;
2644 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
2645 global_count= GLOBAL_COUNT;
2646
2647 for (x= 0; x < global_count; x++)
2648 {
2649 global_keys[x]= global_pairs[x].key;
2650 global_keys_length[x]= global_pairs[x].key_length;
2651 }
2652
2653 return 0;
2654 }
2655
2656 static test_return generate_large_pairs(memcached_st *memc __attribute__((unused)))
2657 {
2658 unsigned long long x;
2659 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
2660 global_count= GLOBAL2_COUNT;
2661
2662 for (x= 0; x < global_count; x++)
2663 {
2664 global_keys[x]= global_pairs[x].key;
2665 global_keys_length[x]= global_pairs[x].key_length;
2666 }
2667
2668 return 0;
2669 }
2670
2671 static test_return generate_data(memcached_st *memc)
2672 {
2673 execute_set(memc, global_pairs, global_count);
2674
2675 return 0;
2676 }
2677
2678 static test_return generate_data_with_stats(memcached_st *memc)
2679 {
2680 memcached_stat_st *stat_p;
2681 memcached_return rc;
2682 uint32_t host_index= 0;
2683 execute_set(memc, global_pairs, global_count);
2684
2685 //TODO: hosts used size stats
2686 stat_p= memcached_stat(memc, NULL, &rc);
2687 assert(stat_p);
2688
2689 for (host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
2690 {
2691 /* This test was changes so that "make test" would work properlly */
2692 #ifdef DEBUG
2693 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);
2694 #endif
2695 assert((unsigned long long)(stat_p + host_index)->bytes);
2696 }
2697
2698 memcached_stat_free(NULL, stat_p);
2699
2700 return 0;
2701 }
2702 static test_return generate_buffer_data(memcached_st *memc)
2703 {
2704 size_t latch= 0;
2705
2706 latch= 1;
2707 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2708 generate_data(memc);
2709
2710 return 0;
2711 }
2712
2713 static test_return get_read_count(memcached_st *memc)
2714 {
2715 unsigned int x;
2716 memcached_return rc;
2717 memcached_st *memc_clone;
2718
2719 memc_clone= memcached_clone(NULL, memc);
2720 assert(memc_clone);
2721
2722 memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
2723
2724 {
2725 char *return_value;
2726 size_t return_value_length;
2727 uint32_t flags;
2728 uint32_t count;
2729
2730 for (x= count= 0; x < global_count; x++)
2731 {
2732 return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
2733 &return_value_length, &flags, &rc);
2734 if (rc == MEMCACHED_SUCCESS)
2735 {
2736 count++;
2737 if (return_value)
2738 free(return_value);
2739 }
2740 }
2741 fprintf(stderr, "\t%u -> %u", global_count, count);
2742 }
2743
2744 memcached_free(memc_clone);
2745
2746 return 0;
2747 }
2748
2749 static test_return get_read(memcached_st *memc)
2750 {
2751 unsigned int x;
2752 memcached_return rc;
2753
2754 {
2755 char *return_value;
2756 size_t return_value_length;
2757 uint32_t flags;
2758
2759 for (x= 0; x < global_count; x++)
2760 {
2761 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
2762 &return_value_length, &flags, &rc);
2763 /*
2764 assert(return_value);
2765 assert(rc == MEMCACHED_SUCCESS);
2766 */
2767 if (rc == MEMCACHED_SUCCESS && return_value)
2768 free(return_value);
2769 }
2770 }
2771
2772 return 0;
2773 }
2774
2775 static test_return mget_read(memcached_st *memc)
2776 {
2777 memcached_return rc;
2778
2779 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2780 assert(rc == MEMCACHED_SUCCESS);
2781 /* Turn this into a help function */
2782 {
2783 char return_key[MEMCACHED_MAX_KEY];
2784 size_t return_key_length;
2785 char *return_value;
2786 size_t return_value_length;
2787 uint32_t flags;
2788
2789 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2790 &return_value_length, &flags, &rc)))
2791 {
2792 assert(return_value);
2793 assert(rc == MEMCACHED_SUCCESS);
2794 free(return_value);
2795 }
2796 }
2797
2798 return 0;
2799 }
2800
2801 static test_return mget_read_result(memcached_st *memc)
2802 {
2803 memcached_return rc;
2804
2805 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2806 assert(rc == MEMCACHED_SUCCESS);
2807 /* Turn this into a help function */
2808 {
2809 memcached_result_st results_obj;
2810 memcached_result_st *results;
2811
2812 results= memcached_result_create(memc, &results_obj);
2813
2814 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
2815 {
2816 assert(results);
2817 assert(rc == MEMCACHED_SUCCESS);
2818 }
2819
2820 memcached_result_free(&results_obj);
2821 }
2822
2823 return 0;
2824 }
2825
2826 static test_return mget_read_function(memcached_st *memc)
2827 {
2828 memcached_return rc;
2829 unsigned int counter;
2830 memcached_execute_function callbacks[1];
2831
2832 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2833 assert(rc == MEMCACHED_SUCCESS);
2834
2835 callbacks[0]= &callback_counter;
2836 counter= 0;
2837 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
2838
2839 return 0;
2840 }
2841
2842 static test_return delete_generate(memcached_st *memc)
2843 {
2844 unsigned int x;
2845
2846 for (x= 0; x < global_count; x++)
2847 {
2848 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2849 }
2850
2851 return 0;
2852 }
2853
2854 static test_return delete_buffer_generate(memcached_st *memc)
2855 {
2856 size_t latch= 0;
2857 unsigned int x;
2858
2859 latch= 1;
2860 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2861
2862 for (x= 0; x < global_count; x++)
2863 {
2864 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2865 }
2866
2867 return 0;
2868 }
2869
2870 static test_return add_host_test1(memcached_st *memc)
2871 {
2872 unsigned int x;
2873 memcached_return rc;
2874 char servername[]= "0.example.com";
2875 memcached_server_st *servers;
2876
2877 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
2878 assert(servers);
2879 assert(1 == memcached_server_list_count(servers));
2880
2881 for (x= 2; x < 20; x++)
2882 {
2883 char buffer[SMALL_STRING_LEN];
2884
2885 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
2886 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
2887 &rc);
2888 assert(rc == MEMCACHED_SUCCESS);
2889 assert(x == memcached_server_list_count(servers));
2890 }
2891
2892 rc= memcached_server_push(memc, servers);
2893 assert(rc == MEMCACHED_SUCCESS);
2894 rc= memcached_server_push(memc, servers);
2895 assert(rc == MEMCACHED_SUCCESS);
2896
2897 memcached_server_list_free(servers);
2898
2899 return 0;
2900 }
2901
2902 static memcached_return pre_nonblock(memcached_st *memc)
2903 {
2904 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2905
2906 return MEMCACHED_SUCCESS;
2907 }
2908
2909 static memcached_return pre_nonblock_binary(memcached_st *memc)
2910 {
2911 memcached_return rc= MEMCACHED_FAILURE;
2912 memcached_st *memc_clone;
2913
2914 memc_clone= memcached_clone(NULL, memc);
2915 assert(memc_clone);
2916 // The memcached_version needs to be done on a clone, because the server
2917 // will not toggle protocol on an connection.
2918 memcached_version(memc_clone);
2919
2920 if (memc_clone->hosts[0].major_version >= 1 && memc_clone->hosts[0].minor_version > 2)
2921 {
2922 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2923 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
2924 assert(rc == MEMCACHED_SUCCESS);
2925 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
2926 }
2927
2928 memcached_free(memc_clone);
2929 return rc;
2930 }
2931
2932 static memcached_return pre_murmur(memcached_st *memc)
2933 {
2934 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
2935
2936 return MEMCACHED_SUCCESS;
2937 }
2938
2939 static memcached_return pre_jenkins(memcached_st *memc)
2940 {
2941 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
2942
2943 return MEMCACHED_SUCCESS;
2944 }
2945
2946
2947 static memcached_return pre_md5(memcached_st *memc)
2948 {
2949 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
2950
2951 return MEMCACHED_SUCCESS;
2952 }
2953
2954 static memcached_return pre_crc(memcached_st *memc)
2955 {
2956 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
2957
2958 return MEMCACHED_SUCCESS;
2959 }
2960
2961 static memcached_return pre_hsieh(memcached_st *memc)
2962 {
2963 #ifdef HAVE_HSIEH_HASH
2964 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
2965 return MEMCACHED_SUCCESS;
2966 #else
2967 (void) memc;
2968 return MEMCACHED_FAILURE;
2969 #endif
2970 }
2971
2972 static memcached_return pre_hash_fnv1_64(memcached_st *memc)
2973 {
2974 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
2975
2976 return MEMCACHED_SUCCESS;
2977 }
2978
2979 static memcached_return pre_hash_fnv1a_64(memcached_st *memc)
2980 {
2981 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
2982
2983 return MEMCACHED_SUCCESS;
2984 }
2985
2986 static memcached_return pre_hash_fnv1_32(memcached_st *memc)
2987 {
2988 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
2989
2990 return MEMCACHED_SUCCESS;
2991 }
2992
2993 static memcached_return pre_hash_fnv1a_32(memcached_st *memc)
2994 {
2995 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
2996
2997 return MEMCACHED_SUCCESS;
2998 }
2999
3000 static memcached_return pre_behavior_ketama(memcached_st *memc)
3001 {
3002 memcached_return rc;
3003 uint64_t value;
3004
3005 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
3006 assert(rc == MEMCACHED_SUCCESS);
3007
3008 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
3009 assert(value == 1);
3010
3011 return MEMCACHED_SUCCESS;
3012 }
3013
3014 static memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
3015 {
3016 memcached_return rc;
3017 uint64_t value;
3018
3019 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3020 assert(rc == MEMCACHED_SUCCESS);
3021
3022 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3023 assert(value == 1);
3024
3025 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3026 assert(rc == MEMCACHED_SUCCESS);
3027
3028 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3029 assert(value == MEMCACHED_HASH_MD5);
3030 return MEMCACHED_SUCCESS;
3031 }
3032
3033 static memcached_return pre_binary(memcached_st *memc)
3034 {
3035 memcached_return rc= MEMCACHED_FAILURE;
3036 memcached_st *memc_clone;
3037
3038 memc_clone= memcached_clone(NULL, memc);
3039 assert(memc_clone);
3040 // The memcached_version needs to be done on a clone, because the server
3041 // will not toggle protocol on an connection.
3042 memcached_version(memc_clone);
3043
3044 if (memc_clone->hosts[0].major_version >= 1 && memc_clone->hosts[0].minor_version > 2)
3045 {
3046 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3047 assert(rc == MEMCACHED_SUCCESS);
3048 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3049 }
3050
3051 memcached_free(memc_clone);
3052 return rc;
3053 }
3054
3055 static memcached_return pre_replication(memcached_st *memc)
3056 {
3057 memcached_return rc= MEMCACHED_FAILURE;
3058 if (pre_binary(memc) == MEMCACHED_SUCCESS)
3059 {
3060 /*
3061 * Make sure that we store the item on all servers
3062 * (master + replicas == number of servers)
3063 */
3064 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
3065 memc->number_of_hosts - 1);
3066 assert(rc == MEMCACHED_SUCCESS);
3067 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS) == memc->number_of_hosts - 1);
3068 }
3069
3070 return rc;
3071 }
3072
3073 static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
3074 {
3075 free(mem);
3076 }
3077
3078 static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
3079 {
3080 void *ret= malloc(size);
3081 if (ret != NULL)
3082 memset(ret, 0xff, size);
3083
3084 return ret;
3085 }
3086
3087 static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
3088 {
3089 return realloc(mem, size);
3090 }
3091
3092 static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size)
3093 {
3094 return calloc(nelem, size);
3095 }
3096
3097 static memcached_return set_prefix(memcached_st *memc)
3098 {
3099 memcached_return rc;
3100 const char *key= "mine";
3101 char *value;
3102
3103 /* Make sure be default none exists */
3104 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3105 assert(rc == MEMCACHED_FAILURE);
3106
3107 /* Test a clean set */
3108 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3109 assert(rc == MEMCACHED_SUCCESS);
3110
3111 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3112 assert(memcmp(value, key, 4) == 0);
3113 assert(rc == MEMCACHED_SUCCESS);
3114
3115 /* Test that we can turn it off */
3116 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3117 assert(rc == MEMCACHED_SUCCESS);
3118
3119 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3120 assert(rc == MEMCACHED_FAILURE);
3121
3122 /* Now setup for main test */
3123 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3124 assert(rc == MEMCACHED_SUCCESS);
3125
3126 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3127 assert(rc == MEMCACHED_SUCCESS);
3128 assert(memcmp(value, key, 4) == 0);
3129
3130 /* Set to Zero, and then Set to something too large */
3131 {
3132 char *long_key;
3133 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3134 assert(rc == MEMCACHED_SUCCESS);
3135
3136 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3137 assert(rc == MEMCACHED_FAILURE);
3138 assert(value == NULL);
3139
3140 /* Test a long key for failure */
3141 /* TODO, extend test to determine based on setting, what result should be */
3142 long_key= "Thisismorethentheallottednumberofcharacters";
3143 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3144 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3145 assert(rc == MEMCACHED_SUCCESS);
3146
3147 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3148 long_key= "This is more then the allotted number of characters";
3149 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3150 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3151
3152 /* Test for a bad prefix, but with a short key */
3153 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
3154 assert(rc == MEMCACHED_SUCCESS);
3155
3156 long_key= "dog cat";
3157 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3158 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3159 }
3160
3161 return MEMCACHED_SUCCESS;
3162 }
3163
3164 static memcached_return deprecated_set_memory_alloc(memcached_st *memc)
3165 {
3166 void *test_ptr= NULL;
3167 void *cb_ptr= NULL;
3168 {
3169 memcached_malloc_function malloc_cb=
3170 (memcached_malloc_function)my_malloc;
3171 cb_ptr= *(void **)&malloc_cb;
3172 memcached_return rc;
3173
3174 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
3175 assert(rc == MEMCACHED_SUCCESS);
3176 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
3177 assert(rc == MEMCACHED_SUCCESS);
3178 assert(test_ptr == cb_ptr);
3179 }
3180
3181 {
3182 memcached_realloc_function realloc_cb=
3183 (memcached_realloc_function)my_realloc;
3184 cb_ptr= *(void **)&realloc_cb;
3185 memcached_return rc;
3186
3187 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
3188 assert(rc == MEMCACHED_SUCCESS);
3189 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
3190 assert(rc == MEMCACHED_SUCCESS);
3191 assert(test_ptr == cb_ptr);
3192 }
3193
3194 {
3195 memcached_free_function free_cb=
3196 (memcached_free_function)my_free;
3197 cb_ptr= *(void **)&free_cb;
3198 memcached_return rc;
3199
3200 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
3201 assert(rc == MEMCACHED_SUCCESS);
3202 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
3203 assert(rc == MEMCACHED_SUCCESS);
3204 assert(test_ptr == cb_ptr);
3205 }
3206 return MEMCACHED_SUCCESS;
3207 }
3208
3209 static memcached_return set_memory_alloc(memcached_st *memc)
3210 {
3211 memcached_return rc;
3212 rc= memcached_set_memory_allocators(memc, NULL, my_free,
3213 my_realloc, my_calloc);
3214 assert(rc == MEMCACHED_FAILURE);
3215
3216 rc= memcached_set_memory_allocators(memc, my_malloc, my_free,
3217 my_realloc, my_calloc);
3218
3219 memcached_malloc_function mem_malloc;
3220 memcached_free_function mem_free;
3221 memcached_realloc_function mem_realloc;
3222 memcached_calloc_function mem_calloc;
3223 memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
3224 &mem_realloc, &mem_calloc);
3225
3226 assert(mem_malloc == my_malloc);
3227 assert(mem_realloc == my_realloc);
3228 assert(mem_calloc == my_calloc);
3229 assert(mem_free == my_free);
3230
3231 return MEMCACHED_SUCCESS;
3232 }
3233
3234 static memcached_return enable_consistent(memcached_st *memc)
3235 {
3236 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
3237 memcached_hash hash;
3238 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
3239 if (pre_hsieh(memc) != MEMCACHED_SUCCESS)
3240 return MEMCACHED_FAILURE;
3241
3242 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
3243 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
3244
3245 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
3246 assert(hash == MEMCACHED_HASH_HSIEH);
3247
3248
3249 return MEMCACHED_SUCCESS;
3250 }
3251
3252 static memcached_return enable_cas(memcached_st *memc)
3253 {
3254 unsigned int set= 1;
3255
3256 memcached_version(memc);
3257
3258 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3259 || memc->hosts[0].minor_version > 2)
3260 {
3261 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
3262
3263 return MEMCACHED_SUCCESS;
3264 }
3265
3266 return MEMCACHED_FAILURE;
3267 }
3268
3269 static memcached_return check_for_1_2_3(memcached_st *memc)
3270 {
3271 memcached_version(memc);
3272
3273 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3274 || memc->hosts[0].minor_version > 2)
3275 return MEMCACHED_SUCCESS;
3276
3277 return MEMCACHED_FAILURE;
3278 }
3279
3280 static memcached_return pre_unix_socket(memcached_st *memc)
3281 {
3282 memcached_return rc;
3283 struct stat buf;
3284
3285 memcached_server_list_free(memc->hosts);
3286 memc->hosts= NULL;
3287 memc->number_of_hosts= 0;
3288
3289 if (stat("/tmp/memcached.socket", &buf))
3290 return MEMCACHED_FAILURE;
3291
3292 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
3293
3294 return rc;
3295 }
3296
3297 static memcached_return pre_nodelay(memcached_st *memc)
3298 {
3299 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3300 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
3301
3302 return MEMCACHED_SUCCESS;
3303 }
3304
3305 static memcached_return pre_settimer(memcached_st *memc)
3306 {
3307 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
3308 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
3309
3310 return MEMCACHED_SUCCESS;
3311 }
3312
3313 static memcached_return poll_timeout(memcached_st *memc)
3314 {
3315 size_t timeout;
3316
3317 timeout= 100;
3318
3319 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
3320
3321 timeout= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
3322
3323 assert(timeout == 100);
3324
3325 return MEMCACHED_SUCCESS;
3326 }
3327
3328 static test_return noreply_test(memcached_st *memc)
3329 {
3330 memcached_return ret;
3331 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
3332 assert(ret == MEMCACHED_SUCCESS);
3333 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3334 assert(ret == MEMCACHED_SUCCESS);
3335 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
3336 assert(ret == MEMCACHED_SUCCESS);
3337 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
3338 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
3339 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
3340
3341 for (int count=0; count < 5; ++count)
3342 {
3343 for (int x=0; x < 100; ++x)
3344 {
3345 char key[10];
3346 size_t len= (size_t)sprintf(key, "%d", x);
3347 switch (count)
3348 {
3349 case 0:
3350 ret=memcached_add(memc, key, len, key, len, 0, 0);
3351 break;
3352 case 1:
3353 ret=memcached_replace(memc, key, len, key, len, 0, 0);
3354 break;
3355 case 2:
3356 ret=memcached_set(memc, key, len, key, len, 0, 0);
3357 break;
3358 case 3:
3359 ret=memcached_append(memc, key, len, key, len, 0, 0);
3360 break;
3361 case 4:
3362 ret=memcached_prepend(memc, key, len, key, len, 0, 0);
3363 break;
3364 default:
3365 assert(count);
3366 break;
3367 }
3368 assert(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
3369 }
3370
3371 /*
3372 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3373 ** API and is _ONLY_ done this way to verify that the library works the
3374 ** way it is supposed to do!!!!
3375 */
3376 int no_msg=0;
3377 for (uint32_t x=0; x < memc->number_of_hosts; ++x)
3378 no_msg+=(int)(memc->hosts[x].cursor_active);
3379
3380 assert(no_msg == 0);
3381 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3382
3383 /*
3384 ** Now validate that all items was set properly!
3385 */
3386 for (int x=0; x < 100; ++x)
3387 {
3388 char key[10];
3389 size_t len= (size_t)sprintf(key, "%d", x);
3390 size_t length;
3391 uint32_t flags;
3392 char* value=memcached_get(memc, key, strlen(key),
3393 &length, &flags, &ret);
3394 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3395 switch (count)
3396 {
3397 case 0: /* FALLTHROUGH */
3398 case 1: /* FALLTHROUGH */
3399 case 2:
3400 assert(strncmp(value, key, len) == 0);
3401 assert(len == length);
3402 break;
3403 case 3:
3404 assert(length == len * 2);
3405 break;
3406 case 4:
3407 assert(length == len * 3);
3408 break;
3409 default:
3410 assert(count);
3411 break;
3412 }
3413 free(value);
3414 }
3415 }
3416
3417 /* Try setting an illegal cas value (should not return an error to
3418 * the caller (because we don't expect a return message from the server)
3419 */
3420 char* keys[]= {"0"};
3421 size_t lengths[]= {1};
3422 size_t length;
3423 uint32_t flags;
3424 memcached_result_st results_obj;
3425 memcached_result_st *results;
3426 ret= memcached_mget(memc, keys, lengths, 1);
3427 assert(ret == MEMCACHED_SUCCESS);
3428
3429 results= memcached_result_create(memc, &results_obj);
3430 assert(results);
3431 results= memcached_fetch_result(memc, &results_obj, &ret);
3432 assert(results);
3433 assert(ret == MEMCACHED_SUCCESS);
3434 uint64_t cas= memcached_result_cas(results);
3435 memcached_result_free(&results_obj);
3436
3437 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3438 assert(ret == MEMCACHED_SUCCESS);
3439
3440 /*
3441 * The item will have a new cas value, so try to set it again with the old
3442 * value. This should fail!
3443 */
3444 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3445 assert(ret == MEMCACHED_SUCCESS);
3446 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3447 char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
3448 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3449 free(value);
3450
3451 return TEST_SUCCESS;
3452 }
3453
3454 static test_return analyzer_test(memcached_st *memc)
3455 {
3456 memcached_return rc;
3457 memcached_stat_st *memc_stat;
3458 memcached_analysis_st *report;
3459
3460 memc_stat= memcached_stat(memc, NULL, &rc);
3461 assert(rc == MEMCACHED_SUCCESS);
3462 assert(memc_stat);
3463
3464 report= memcached_analyze(memc, memc_stat, &rc);
3465 assert(rc == MEMCACHED_SUCCESS);
3466 assert(report);
3467
3468 free(report);
3469 memcached_stat_free(NULL, memc_stat);
3470
3471 return TEST_SUCCESS;
3472 }
3473
3474 /* Count the objects */
3475 static memcached_return callback_dump_counter(memcached_st *ptr __attribute__((unused)),
3476 const char *key __attribute__((unused)),
3477 size_t key_length __attribute__((unused)),
3478 void *context)
3479 {
3480 uint32_t *counter= (uint32_t *)context;
3481
3482 *counter= *counter + 1;
3483
3484 return MEMCACHED_SUCCESS;
3485 }
3486
3487 static test_return dump_test(memcached_st *memc)
3488 {
3489 memcached_return rc;
3490 uint32_t counter= 0;
3491 memcached_dump_func callbacks[1];
3492 test_return main_rc;
3493
3494 callbacks[0]= &callback_dump_counter;
3495
3496 /* No support for Binary protocol yet */
3497 if (memc->flags & MEM_BINARY_PROTOCOL)
3498 return TEST_SUCCESS;
3499
3500 main_rc= set_test3(memc);
3501
3502 assert (main_rc == TEST_SUCCESS);
3503
3504 rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
3505 assert(rc == MEMCACHED_SUCCESS);
3506
3507 /* We may have more then 32 if our previous flush has not completed */
3508 assert(counter >= 32);
3509
3510 return TEST_SUCCESS;
3511 }
3512
3513 #ifdef HAVE_LIBMEMCACHEDUTIL
3514 static void* connection_release(void *arg) {
3515 struct {
3516 memcached_pool_st* pool;
3517 memcached_st* mmc;
3518 } *resource= arg;
3519
3520 usleep(250);
3521 assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
3522 return arg;
3523 }
3524
3525 static test_return connection_pool_test(memcached_st *memc)
3526 {
3527 memcached_pool_st* pool= memcached_pool_create(memc, 5, 10);
3528 assert(pool != NULL);
3529 memcached_st* mmc[10];
3530 memcached_return rc;
3531
3532 for (int x= 0; x < 10; ++x) {
3533 mmc[x]= memcached_pool_pop(pool, false, &rc);
3534 assert(mmc[x] != NULL);
3535 assert(rc == MEMCACHED_SUCCESS);
3536 }
3537
3538 assert(memcached_pool_pop(pool, false, &rc) == NULL);
3539 assert(rc == MEMCACHED_SUCCESS);
3540
3541 pthread_t tid;
3542 struct {
3543 memcached_pool_st* pool;
3544 memcached_st* mmc;
3545 } item= { .pool = pool, .mmc = mmc[9] };
3546 pthread_create(&tid, NULL, connection_release, &item);
3547 mmc[9]= memcached_pool_pop(pool, true, &rc);
3548 assert(rc == MEMCACHED_SUCCESS);
3549 pthread_join(tid, NULL);
3550 assert(mmc[9] == item.mmc);
3551 const char *key= "key";
3552 size_t keylen= strlen(key);
3553
3554 // verify that I can do ops with all connections
3555 rc= memcached_set(mmc[0], key, keylen, "0", 1, 0, 0);
3556 assert(rc == MEMCACHED_SUCCESS);
3557
3558 for (unsigned int x= 0; x < 10; ++x) {
3559 uint64_t number_value;
3560 rc= memcached_increment(mmc[x], key, keylen, 1, &number_value);
3561 assert(rc == MEMCACHED_SUCCESS);
3562 assert(number_value == (x+1));
3563 }
3564
3565 // Release them..
3566 for (int x= 0; x < 10; ++x)
3567 assert(memcached_pool_push(pool, mmc[x]) == MEMCACHED_SUCCESS);
3568
3569 assert(memcached_pool_destroy(pool) == memc);
3570 return TEST_SUCCESS;
3571 }
3572 #endif
3573
3574 static test_return replication_set_test(memcached_st *memc)
3575 {
3576 memcached_return rc;
3577 memcached_st *clone= memcached_clone(NULL, memc);
3578 memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
3579
3580 rc= memcached_set(memc, "bubba", 5, "0", 1, 0, 0);
3581 assert(rc == MEMCACHED_SUCCESS);
3582
3583 /*
3584 ** "bubba" should now be stored on all of our servers. We don't have an
3585 ** easy to use API to address each individual server, so I'll just iterate
3586 ** through a bunch of "master keys" and I should most likely hit all of the
3587 ** servers...
3588 */
3589 for (int x= 'a'; x <= 'z'; ++x)
3590 {
3591 char key[2]= { [0]= (char)x };
3592 size_t len;
3593 uint32_t flags;
3594 char *val= memcached_get_by_key(clone, key, 1, "bubba", 5,
3595 &len, &flags, &rc);
3596 assert(rc == MEMCACHED_SUCCESS);
3597 assert(val != NULL);
3598 free(val);
3599 }
3600
3601 memcached_free(clone);
3602
3603 return TEST_SUCCESS;
3604 }
3605
3606 static test_return replication_get_test(memcached_st *memc)
3607 {
3608 memcached_return rc;
3609
3610 /*
3611 * Don't do the following in your code. I am abusing the internal details
3612 * within the library, and this is not a supported interface.
3613 * This is to verify correct behavior in the library
3614 */
3615 for (uint32_t host= 0; host < memc->number_of_hosts; ++host)
3616 {
3617 memcached_st *clone= memcached_clone(NULL, memc);
3618 clone->hosts[host].port= 0;
3619
3620 for (int x= 'a'; x <= 'z'; ++x)
3621 {
3622 char key[2]= { [0]= (char)x };
3623 size_t len;
3624 uint32_t flags;
3625 char *val= memcached_get_by_key(clone, key, 1, "bubba", 5,
3626 &len, &flags, &rc);
3627 assert(rc == MEMCACHED_SUCCESS);
3628 assert(val != NULL);
3629 free(val);
3630 }
3631
3632 memcached_free(clone);
3633 }
3634
3635 return TEST_SUCCESS;
3636 }
3637
3638 static test_return replication_mget_test(memcached_st *memc)
3639 {
3640 memcached_return rc;
3641 memcached_st *clone= memcached_clone(NULL, memc);
3642 memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
3643
3644 char *keys[]= { "bubba", "key1", "key2", "key3" };
3645 size_t len[]= { 5, 4, 4, 4 };
3646
3647 for (int x=0; x< 4; ++x)
3648 {
3649 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
3650 assert(rc == MEMCACHED_SUCCESS);
3651 }
3652
3653 /*
3654 * Don't do the following in your code. I am abusing the internal details
3655 * within the library, and this is not a supported interface.
3656 * This is to verify correct behavior in the library
3657 */
3658 memcached_result_st result_obj;
3659 for (uint32_t host= 0; host < clone->number_of_hosts; host++)
3660 {
3661 memcached_st *new_clone= memcached_clone(NULL, memc);
3662 new_clone->hosts[host].port= 0;
3663
3664 for (int x= 'a'; x <= 'z'; ++x)
3665 {
3666 char key[2]= { [0]= (char)x };
3667
3668 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
3669 assert(rc == MEMCACHED_SUCCESS);
3670
3671 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
3672 assert(results);
3673
3674 int hits= 0;
3675 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
3676 {
3677 hits++;
3678 }
3679 assert(hits == 4);
3680 memcached_result_free(&result_obj);
3681 }
3682
3683 memcached_free(new_clone);
3684 }
3685
3686 return TEST_SUCCESS;
3687 }
3688
3689 static test_return replication_delete_test(memcached_st *memc)
3690 {
3691 memcached_return rc;
3692 memcached_st *clone= memcached_clone(NULL, memc);
3693 /* Delete the items from all of the servers except 1 */
3694 uint64_t repl= memcached_behavior_get(memc,
3695 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
3696 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, --repl);
3697
3698 char *keys[]= { "bubba", "key1", "key2", "key3" };
3699 size_t len[]= { 5, 4, 4, 4 };
3700
3701 for (int x=0; x< 4; ++x)
3702 {
3703 rc= memcached_delete_by_key(memc, keys[0], len[0], keys[x], len[x], 0);
3704 assert(rc == MEMCACHED_SUCCESS);
3705 }
3706
3707 /*
3708 * Don't do the following in your code. I am abusing the internal details
3709 * within the library, and this is not a supported interface.
3710 * This is to verify correct behavior in the library
3711 */
3712 uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
3713 for (uint32_t x= 0; x < (repl + 1); ++x)
3714 {
3715 clone->hosts[hash].port= 0;
3716 if (++hash == clone->number_of_hosts)
3717 hash= 0;
3718 }
3719
3720 memcached_result_st result_obj;
3721 for (uint32_t host= 0; host < clone->number_of_hosts; ++host)
3722 {
3723 for (int x= 'a'; x <= 'z'; ++x)
3724 {
3725 char key[2]= { [0]= (char)x };
3726
3727 rc= memcached_mget_by_key(clone, key, 1, keys, len, 4);
3728 assert(rc == MEMCACHED_SUCCESS);
3729
3730 memcached_result_st *results= memcached_result_create(clone, &result_obj);
3731 assert(results);
3732
3733 int hits= 0;
3734 while ((results= memcached_fetch_result(clone, &result_obj, &rc)) != NULL)
3735 {
3736 ++hits;
3737 }
3738 assert(hits == 4);
3739 memcached_result_free(&result_obj);
3740 }
3741 }
3742 memcached_free(clone);
3743
3744 return TEST_SUCCESS;
3745 }
3746
3747 static void increment_request_id(uint16_t *id)
3748 {
3749 (*id)++;
3750 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
3751 *id= 0;
3752 }
3753
3754 static uint16_t *get_udp_request_ids(memcached_st *memc)
3755 {
3756 uint16_t *ids= malloc(sizeof(uint16_t) * memc->number_of_hosts);
3757 assert(ids != NULL);
3758 unsigned int x;
3759
3760 for (x= 0; x < memc->number_of_hosts; x++)
3761 ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) memc->hosts[x].write_buffer);
3762
3763 return ids;
3764 }
3765
3766 static test_return post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
3767 {
3768 unsigned int x;
3769 memcached_server_st *cur_server = memc->hosts;
3770 uint16_t *cur_req_ids = get_udp_request_ids(memc);
3771
3772 for (x= 0; x < memc->number_of_hosts; x++)
3773 {
3774 assert(cur_server[x].cursor_active == 0);
3775 assert(cur_req_ids[x] == expected_req_ids[x]);
3776 }
3777 free(expected_req_ids);
3778 free(cur_req_ids);
3779
3780 return TEST_SUCCESS;
3781 }
3782
3783 /*
3784 ** There is a little bit of a hack here, instead of removing
3785 ** the servers, I just set num host to 0 and them add then new udp servers
3786 **/
3787 static memcached_return init_udp(memcached_st *memc)
3788 {
3789 memcached_version(memc);
3790 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3791 if (memc->hosts[0].major_version != 1 || memc->hosts[0].minor_version != 2
3792 || memc->hosts[0].micro_version < 6)
3793 return MEMCACHED_FAILURE;
3794
3795 uint32_t num_hosts= memc->number_of_hosts;
3796 unsigned int x= 0;
3797 memcached_server_st servers[num_hosts];
3798 memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts);
3799 for (x= 0; x < num_hosts; x++)
3800 memcached_server_free(&memc->hosts[x]);
3801
3802 memc->number_of_hosts= 0;
3803 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
3804 for (x= 0; x < num_hosts; x++)
3805 {
3806 assert(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
3807 assert(memc->hosts[x].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3808 }
3809
3810 return MEMCACHED_SUCCESS;
3811 }
3812
3813 static memcached_return binary_init_udp(memcached_st *memc)
3814 {
3815 pre_binary(memc);
3816 return init_udp(memc);
3817 }
3818
3819 /* Make sure that I cant add a tcp server to a udp client */
3820 static test_return add_tcp_server_udp_client_test(memcached_st *memc)
3821 {
3822 memcached_server_st server;
3823 memcached_server_clone(&server, &memc->hosts[0]);
3824 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3825 assert(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3826 return TEST_SUCCESS;
3827 }
3828
3829 /* Make sure that I cant add a udp server to a tcp client */
3830 static test_return add_udp_server_tcp_client_test(memcached_st *memc)
3831 {
3832 memcached_server_st server;
3833 memcached_server_clone(&server, &memc->hosts[0]);
3834 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3835
3836 memcached_st tcp_client;
3837 memcached_create(&tcp_client);
3838 assert(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3839 return TEST_SUCCESS;
3840 }
3841
3842 static test_return set_udp_behavior_test(memcached_st *memc)
3843 {
3844
3845 memcached_quit(memc);
3846 memc->number_of_hosts= 0;
3847 run_distribution(memc);
3848 assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1) == MEMCACHED_SUCCESS);
3849 assert(memc->flags & MEM_USE_UDP);
3850 assert(memc->flags & MEM_NOREPLY);;
3851
3852 assert(memc->number_of_hosts == 0);
3853
3854 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0);
3855 assert(!(memc->flags & MEM_USE_UDP));
3856 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY,0);
3857 assert(!(memc->flags & MEM_NOREPLY));
3858 return TEST_SUCCESS;
3859 }
3860
3861 static test_return udp_set_test(memcached_st *memc)
3862 {
3863 unsigned int x= 0;
3864 unsigned int num_iters= 1025; //request id rolls over at 1024
3865 for (x= 0; x < num_iters;x++)
3866 {
3867 memcached_return rc;
3868 char *key= "foo";
3869 char *value= "when we sanitize";
3870 uint16_t *expected_ids= get_udp_request_ids(memc);
3871 unsigned int server_key= memcached_generate_hash(memc,key,strlen(key));
3872 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3873 rc= memcached_set(memc, key, strlen(key),
3874 value, strlen(value),
3875 (time_t)0, (uint32_t)0);
3876 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3877 /** NB, the check below assumes that if new write_ptr is less than
3878 * the original write_ptr that we have flushed. For large payloads, this
3879 * maybe an invalid assumption, but for the small payload we have it is OK
3880 */
3881 if (rc == MEMCACHED_SUCCESS ||
3882 memc->hosts[server_key].write_buffer_offset < init_offset)
3883 increment_request_id(&expected_ids[server_key]);
3884
3885 if (rc == MEMCACHED_SUCCESS)
3886 {
3887 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3888 }
3889 else
3890 {
3891 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
3892 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
3893 }
3894 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
3895 }
3896 return TEST_SUCCESS;
3897 }
3898
3899 static test_return udp_buffered_set_test(memcached_st *memc)
3900 {
3901 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3902 return udp_set_test(memc);
3903 }
3904
3905 static test_return udp_set_too_big_test(memcached_st *memc)
3906 {
3907 memcached_return rc;
3908 char *key= "bar";
3909 char value[MAX_UDP_DATAGRAM_LENGTH];
3910 uint16_t *expected_ids= get_udp_request_ids(memc);
3911 rc= memcached_set(memc, key, strlen(key),
3912 value, MAX_UDP_DATAGRAM_LENGTH,
3913 (time_t)0, (uint32_t)0);
3914 assert(rc == MEMCACHED_WRITE_FAILURE);
3915 return post_udp_op_check(memc,expected_ids);
3916 }
3917
3918 static test_return udp_delete_test(memcached_st *memc)
3919 {
3920 unsigned int x= 0;
3921 unsigned int num_iters= 1025; //request id rolls over at 1024
3922 for (x= 0; x < num_iters;x++)
3923 {
3924 memcached_return rc;
3925 char *key= "foo";
3926 uint16_t *expected_ids=get_udp_request_ids(memc);
3927 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
3928 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3929 rc= memcached_delete(memc, key, strlen(key), 0);
3930 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3931 if (rc == MEMCACHED_SUCCESS || memc->hosts[server_key].write_buffer_offset < init_offset)
3932 increment_request_id(&expected_ids[server_key]);
3933 if (rc == MEMCACHED_SUCCESS)
3934 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3935 else
3936 {
3937 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
3938 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
3939 }
3940 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
3941 }
3942 return TEST_SUCCESS;
3943 }
3944
3945 static test_return udp_buffered_delete_test(memcached_st *memc)
3946 {
3947 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3948 return udp_delete_test(memc);
3949 }
3950
3951 static test_return udp_verbosity_test(memcached_st *memc)
3952 {
3953 memcached_return rc;
3954 uint16_t *expected_ids= get_udp_request_ids(memc);
3955 unsigned int x;
3956 for (x= 0; x < memc->number_of_hosts;x++)
3957 increment_request_id(&expected_ids[x]);
3958
3959 rc= memcached_verbosity(memc,3);
3960 assert(rc == MEMCACHED_SUCCESS);
3961 return post_udp_op_check(memc,expected_ids);
3962 }
3963
3964 static test_return udp_quit_test(memcached_st *memc)
3965 {
3966 uint16_t *expected_ids= get_udp_request_ids(memc);
3967 memcached_quit(memc);
3968 return post_udp_op_check(memc, expected_ids);
3969 }
3970
3971 static test_return udp_flush_test(memcached_st *memc)
3972 {
3973 memcached_return rc;
3974 uint16_t *expected_ids= get_udp_request_ids(memc);
3975 unsigned int x;
3976 for (x= 0; x < memc->number_of_hosts;x++)
3977 increment_request_id(&expected_ids[x]);
3978
3979 rc= memcached_flush(memc,0);
3980 assert(rc == MEMCACHED_SUCCESS);
3981 return post_udp_op_check(memc,expected_ids);
3982 }
3983
3984 static test_return udp_incr_test(memcached_st *memc)
3985 {
3986 memcached_return rc;
3987 char *key= "incr";
3988 char *value= "1";
3989 rc= memcached_set(memc, key, strlen(key),
3990 value, strlen(value),
3991 (time_t)0, (uint32_t)0);
3992
3993 assert(rc == MEMCACHED_SUCCESS);
3994 uint16_t *expected_ids= get_udp_request_ids(memc);
3995 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
3996 increment_request_id(&expected_ids[server_key]);
3997 uint64_t newvalue;
3998 rc= memcached_increment(memc, key, strlen(key), 1, &newvalue);
3999 assert(rc == MEMCACHED_SUCCESS);
4000 return post_udp_op_check(memc, expected_ids);
4001 }
4002
4003 static test_return udp_decr_test(memcached_st *memc)
4004 {
4005 memcached_return rc;
4006 char *key= "decr";
4007 char *value= "1";
4008 rc= memcached_set(memc, key, strlen(key),
4009 value, strlen(value),
4010 (time_t)0, (uint32_t)0);
4011
4012 assert(rc == MEMCACHED_SUCCESS);
4013 uint16_t *expected_ids= get_udp_request_ids(memc);
4014 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
4015 increment_request_id(&expected_ids[server_key]);
4016 uint64_t newvalue;
4017 rc= memcached_decrement(memc, key, strlen(key), 1, &newvalue);
4018 assert(rc == MEMCACHED_SUCCESS);
4019 return post_udp_op_check(memc, expected_ids);
4020 }
4021
4022
4023 static test_return udp_stat_test(memcached_st *memc)
4024 {
4025 memcached_stat_st * rv= NULL;
4026 memcached_return rc;
4027 char args[]= "";
4028 uint16_t *expected_ids = get_udp_request_ids(memc);
4029 rv = memcached_stat(memc, args, &rc);
4030 free(rv);
4031 assert(rc == MEMCACHED_NOT_SUPPORTED);
4032 return post_udp_op_check(memc, expected_ids);
4033 }
4034
4035 static test_return udp_version_test(memcached_st *memc)
4036 {
4037 memcached_return rc;
4038 uint16_t *expected_ids = get_udp_request_ids(memc);
4039 rc = memcached_version(memc);
4040 assert(rc == MEMCACHED_NOT_SUPPORTED);
4041 return post_udp_op_check(memc, expected_ids);
4042 }
4043
4044 static test_return udp_get_test(memcached_st *memc)
4045 {
4046 memcached_return rc;
4047 char *key= "foo";
4048 size_t vlen;
4049 uint16_t *expected_ids = get_udp_request_ids(memc);
4050 char *val= memcached_get(memc, key, strlen(key), &vlen, (uint32_t)0, &rc);
4051 assert(rc == MEMCACHED_NOT_SUPPORTED);
4052 assert(val == NULL);
4053 return post_udp_op_check(memc, expected_ids);
4054 }
4055
4056 static test_return udp_mixed_io_test(memcached_st *memc)
4057 {
4058 test_st current_op;
4059 test_st mixed_io_ops [] ={
4060 {"udp_set_test", 0, udp_set_test},
4061 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4062 {"udp_delete_test", 0, udp_delete_test},
4063 {"udp_verbosity_test", 0, udp_verbosity_test},
4064 {"udp_quit_test", 0, udp_quit_test},
4065 {"udp_flush_test", 0, udp_flush_test},
4066 {"udp_incr_test", 0, udp_incr_test},
4067 {"udp_decr_test", 0, udp_decr_test},
4068 {"udp_version_test", 0, udp_version_test}
4069 };
4070 unsigned int x= 0;
4071 for (x= 0; x < 500; x++)
4072 {
4073 current_op= mixed_io_ops[random() % 9];
4074 assert(current_op.function(memc) == TEST_SUCCESS);
4075 }
4076 return TEST_SUCCESS;
4077 }
4078
4079 static test_return hsieh_avaibility_test (memcached_st *memc)
4080 {
4081 memcached_return expected_rc= MEMCACHED_FAILURE;
4082 #ifdef HAVE_HSIEH_HASH
4083 expected_rc= MEMCACHED_SUCCESS;
4084 #endif
4085 memcached_return rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
4086 (uint64_t)MEMCACHED_HASH_HSIEH);
4087 assert(rc == expected_rc);
4088 return TEST_SUCCESS;
4089 }
4090
4091 static char *list[]=
4092 {
4093 "apple",
4094 "beat",
4095 "carrot",
4096 "daikon",
4097 "eggplant",
4098 "flower",
4099 "green",
4100 "hide",
4101 "ick",
4102 "jack",
4103 "kick",
4104 "lime",
4105 "mushrooms",
4106 "nectarine",
4107 "orange",
4108 "peach",
4109 "quant",
4110 "ripen",
4111 "strawberry",
4112 "tang",
4113 "up",
4114 "volumne",
4115 "when",
4116 "yellow",
4117 "zip",
4118 NULL
4119 };
4120
4121 static test_return md5_run (memcached_st *memc __attribute__((unused)))
4122 {
4123 uint32_t x;
4124 char **ptr;
4125 uint32_t values[]= { 3195025439, 2556848621, 3724893440, 3332385401, 245758794, 2550894432,
4126 121710495, 3053817768, 1250994555, 1862072655, 2631955953, 2951528551,
4127 1451250070, 2820856945, 2060845566, 3646985608, 2138080750, 217675895,
4128 2230934345, 1234361223, 3968582726, 2455685270, 1293568479, 199067604,
4129 2042482093 };
4130
4131
4132 for (ptr= list, x= 0; *ptr; ptr++, x++)
4133 {
4134 uint32_t hash_val;
4135
4136 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MD5);
4137 assert(values[x] == hash_val);
4138 }
4139
4140 return TEST_SUCCESS;
4141 }
4142
4143 static test_return crc_run (memcached_st *memc __attribute__((unused)))
4144 {
4145 uint32_t x;
4146 char **ptr;
4147 uint32_t values[]= { 10542, 22009, 14526, 19510, 19432, 10199, 20634, 9369, 11511, 10362,
4148 7893, 31289, 11313, 9354, 7621, 30628, 15218, 25967, 2695, 9380,
4149 17300, 28156, 9192, 20484, 16925 };
4150
4151 for (ptr= list, x= 0; *ptr; ptr++, x++)
4152 {
4153 uint32_t hash_val;
4154
4155 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_CRC);
4156 assert(values[x] == hash_val);
4157 }
4158
4159 return TEST_SUCCESS;
4160 }
4161
4162 static test_return fnv1_64_run (memcached_st *memc __attribute__((unused)))
4163 {
4164 uint32_t x;
4165 char **ptr;
4166 uint32_t values[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4167 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4168 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4169 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4170 1801941682 };
4171
4172 for (ptr= list, x= 0; *ptr; ptr++, x++)
4173 {
4174 uint32_t hash_val;
4175
4176 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4177 assert(values[x] == hash_val);
4178 }
4179
4180 return TEST_SUCCESS;
4181 }
4182
4183 static test_return fnv1a_64_run (memcached_st *memc __attribute__((unused)))
4184 {
4185 uint32_t x;
4186 char **ptr;
4187 uint32_t values[]= { 1488911807, 2500855813, 1510099634, 1390325195, 3647689787, 3241528582,
4188 1669328060, 2604311949, 734810122, 1516407546, 560948863, 1767346780,
4189 561034892, 4156330026, 3716417003, 3475297030, 1518272172, 227211583,
4190 3938128828, 126112909, 3043416448, 3131561933, 1328739897, 2455664041,
4191 2272238452 };
4192
4193 for (ptr= list, x= 0; *ptr; ptr++, x++)
4194 {
4195 uint32_t hash_val;
4196
4197 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_64);
4198 assert(values[x] == hash_val);
4199 }
4200
4201 return TEST_SUCCESS;
4202 }
4203
4204 static test_return fnv1_32_run (memcached_st *memc __attribute__((unused)))
4205 {
4206 uint32_t x;
4207 char **ptr;
4208 uint32_t values[]= { 67176023, 1190179409, 2043204404, 3221866419, 2567703427, 3787535528, 4147287986,
4209 3500475733, 344481048, 3865235296, 2181839183, 119581266, 510234242, 4248244304,
4210 1362796839, 103389328, 1449620010, 182962511, 3554262370, 3206747549, 1551306158,
4211 4127558461, 1889140833, 2774173721, 1180552018 };
4212
4213
4214 for (ptr= list, x= 0; *ptr; ptr++, x++)
4215 {
4216 uint32_t hash_val;
4217
4218 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_32);
4219 assert(values[x] == hash_val);
4220 }
4221
4222 return TEST_SUCCESS;
4223 }
4224
4225 static test_return fnv1a_32_run (memcached_st *memc __attribute__((unused)))
4226 {
4227 uint32_t x;
4228 char **ptr;
4229 uint32_t values[]= { 280767167, 2421315013, 3072375666, 855001899, 459261019, 3521085446, 18738364,
4230 1625305005, 2162232970, 777243802, 3323728671, 132336572, 3654473228, 260679466,
4231 1169454059, 2698319462, 1062177260, 235516991, 2218399068, 405302637, 1128467232,
4232 3579622413, 2138539289, 96429129, 2877453236 };
4233
4234 for (ptr= list, x= 0; *ptr; ptr++, x++)
4235 {
4236 uint32_t hash_val;
4237
4238 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_32);
4239 assert(values[x] == hash_val);
4240 }
4241
4242 return TEST_SUCCESS;
4243 }
4244
4245 static test_return hsieh_run (memcached_st *memc __attribute__((unused)))
4246 {
4247 uint32_t x;
4248 char **ptr;
4249 #ifdef HAVE_HSIEH_HASH
4250 uint32_t values[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4251 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4252 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4253 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4254 3395453351 };
4255 #else
4256 uint32_t values[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
4257 #endif
4258
4259 for (ptr= list, x= 0; *ptr; ptr++, x++)
4260 {
4261 uint32_t hash_val;
4262
4263 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_HSIEH);
4264 assert(values[x] == hash_val);
4265 }
4266
4267 return TEST_SUCCESS;
4268 }
4269
4270 static test_return murmur_run (memcached_st *memc __attribute__((unused)))
4271 {
4272 uint32_t x;
4273 char **ptr;
4274 uint32_t values[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4275 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4276 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4277 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4278 1801941682 };
4279
4280 for (ptr= list, x= 0; *ptr; ptr++, x++)
4281 {
4282 uint32_t hash_val;
4283
4284 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4285 assert(values[x] == hash_val);
4286 }
4287
4288 return TEST_SUCCESS;
4289 }
4290
4291 static test_return jenkins_run (memcached_st *memc __attribute__((unused)))
4292 {
4293 uint32_t x;
4294 char **ptr;
4295 uint32_t values[]= { 1442444624, 4253821186, 1885058256, 2120131735, 3261968576, 3515188778,
4296 4232909173, 4288625128, 1812047395, 3689182164, 2502979932, 1214050606,
4297 2415988847, 1494268927, 1025545760, 3920481083, 4153263658, 3824871822,
4298 3072759809, 798622255, 3065432577, 1453328165, 2691550971, 3408888387,
4299 2629893356 };
4300
4301
4302 for (ptr= list, x= 0; *ptr; ptr++, x++)
4303 {
4304 uint32_t hash_val;
4305
4306 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_JENKINS);
4307 assert(values[x] == hash_val);
4308 }
4309
4310 return TEST_SUCCESS;
4311 }
4312
4313 test_st udp_setup_server_tests[] ={
4314 {"set_udp_behavior_test", 0, set_udp_behavior_test},
4315 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test},
4316 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test},
4317 {0, 0, 0}
4318 };
4319
4320 test_st upd_io_tests[] ={
4321 {"udp_set_test", 0, udp_set_test},
4322 {"udp_buffered_set_test", 0, udp_buffered_set_test},
4323 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4324 {"udp_delete_test", 0, udp_delete_test},
4325 {"udp_buffered_delete_test", 0, udp_buffered_delete_test},
4326 {"udp_verbosity_test", 0, udp_verbosity_test},
4327 {"udp_quit_test", 0, udp_quit_test},
4328 {"udp_flush_test", 0, udp_flush_test},
4329 {"udp_incr_test", 0, udp_incr_test},
4330 {"udp_decr_test", 0, udp_decr_test},
4331 {"udp_stat_test", 0, udp_stat_test},
4332 {"udp_version_test", 0, udp_version_test},
4333 {"udp_get_test", 0, udp_get_test},
4334 {"udp_mixed_io_test", 0, udp_mixed_io_test},
4335 {0, 0, 0}
4336 };
4337
4338 /* Clean the server before beginning testing */
4339 test_st tests[] ={
4340 {"flush", 0, flush_test },
4341 {"init", 0, init_test },
4342 {"allocation", 0, allocation_test },
4343 {"server_list_null_test", 0, server_list_null_test},
4344 {"server_unsort", 0, server_unsort_test},
4345 {"server_sort", 0, server_sort_test},
4346 {"server_sort2", 0, server_sort2_test},
4347 {"clone_test", 0, clone_test },
4348 {"connection_test", 0, connection_test},
4349 {"callback_test", 0, callback_test},
4350 {"behavior_test", 0, behavior_test},
4351 {"userdata_test", 0, userdata_test},
4352 {"error", 0, error_test },
4353 {"set", 0, set_test },
4354 {"set2", 0, set_test2 },
4355 {"set3", 0, set_test3 },
4356 {"dump", 1, dump_test},
4357 {"add", 1, add_test },
4358 {"replace", 1, replace_test },
4359 {"delete", 1, delete_test },
4360 {"get", 1, get_test },
4361 {"get2", 0, get_test2 },
4362 {"get3", 0, get_test3 },
4363 {"get4", 0, get_test4 },
4364 {"partial mget", 0, get_test5 },
4365 {"stats_servername", 0, stats_servername_test },
4366 {"increment", 0, increment_test },
4367 {"increment_with_initial", 1, increment_with_initial_test },
4368 {"decrement", 0, decrement_test },
4369 {"decrement_with_initial", 1, decrement_with_initial_test },
4370 {"quit", 0, quit_test },
4371 {"mget", 1, mget_test },
4372 {"mget_result", 1, mget_result_test },
4373 {"mget_result_alloc", 1, mget_result_alloc_test },
4374 {"mget_result_function", 1, mget_result_function },
4375 {"get_stats", 0, get_stats },
4376 {"add_host_test", 0, add_host_test },
4377 {"add_host_test_1", 0, add_host_test1 },
4378 {"get_stats_keys", 0, get_stats_keys },
4379 {"behavior_test", 0, get_stats_keys },
4380 {"callback_test", 0, get_stats_keys },
4381 {"version_string_test", 0, version_string_test},
4382 {"bad_key", 1, bad_key_test },
4383 {"memcached_server_cursor", 1, memcached_server_cursor_test },
4384 {"read_through", 1, read_through },
4385 {"delete_through", 1, delete_through },
4386 {"noreply", 1, noreply_test},
4387 {"analyzer", 1, analyzer_test},
4388 #ifdef HAVE_LIBMEMCACHEDUTIL
4389 {"connectionpool", 1, connection_pool_test },
4390 #endif
4391 {0, 0, 0}
4392 };
4393
4394 test_st async_tests[] ={
4395 {"add", 1, add_wrapper },
4396 {0, 0, 0}
4397 };
4398
4399 test_st string_tests[] ={
4400 {"string static with null", 0, string_static_null },
4401 {"string alloc with null", 0, string_alloc_null },
4402 {"string alloc with 1K", 0, string_alloc_with_size },
4403 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
4404 {"string append", 0, string_alloc_append },
4405 {"string append failure (too big)", 0, string_alloc_append_toobig },
4406 {0, 0, 0}
4407 };
4408
4409 test_st result_tests[] ={
4410 {"result static", 0, result_static},
4411 {"result alloc", 0, result_alloc},
4412 {0, 0, 0}
4413 };
4414
4415 test_st version_1_2_3[] ={
4416 {"append", 0, append_test },
4417 {"prepend", 0, prepend_test },
4418 {"cas", 0, cas_test },
4419 {"cas2", 0, cas2_test },
4420 {"append_binary", 0, append_binary_test },
4421 {0, 0, 0}
4422 };
4423
4424 test_st user_tests[] ={
4425 {"user_supplied_bug1", 0, user_supplied_bug1 },
4426 {"user_supplied_bug2", 0, user_supplied_bug2 },
4427 {"user_supplied_bug3", 0, user_supplied_bug3 },
4428 {"user_supplied_bug4", 0, user_supplied_bug4 },
4429 {"user_supplied_bug5", 1, user_supplied_bug5 },
4430 {"user_supplied_bug6", 1, user_supplied_bug6 },
4431 {"user_supplied_bug7", 1, user_supplied_bug7 },
4432 {"user_supplied_bug8", 1, user_supplied_bug8 },
4433 {"user_supplied_bug9", 1, user_supplied_bug9 },
4434 {"user_supplied_bug10", 1, user_supplied_bug10 },
4435 {"user_supplied_bug11", 1, user_supplied_bug11 },
4436 {"user_supplied_bug12", 1, user_supplied_bug12 },
4437 {"user_supplied_bug13", 1, user_supplied_bug13 },
4438 {"user_supplied_bug14", 1, user_supplied_bug14 },
4439 {"user_supplied_bug15", 1, user_supplied_bug15 },
4440 {"user_supplied_bug16", 1, user_supplied_bug16 },
4441 #ifndef __sun
4442 /*
4443 ** It seems to be something weird with the character sets..
4444 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4445 ** guess I need to find out how this is supposed to work.. Perhaps I need
4446 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4447 ** so just disable the code for now...).
4448 */
4449 {"user_supplied_bug17", 1, user_supplied_bug17 },
4450 #endif
4451 {"user_supplied_bug18", 1, user_supplied_bug18 },
4452 {"user_supplied_bug19", 1, user_supplied_bug19 },
4453 {"user_supplied_bug20", 1, user_supplied_bug20 },
4454 {0, 0, 0}
4455 };
4456
4457 test_st replication_tests[]= {
4458 {"set", 1, replication_set_test },
4459 {"get", 0, replication_get_test },
4460 {"mget", 0, replication_mget_test },
4461 {"delete", 0, replication_delete_test },
4462 {0, 0, 0}
4463 };
4464
4465 test_st generate_tests[] ={
4466 {"generate_pairs", 1, generate_pairs },
4467 {"generate_data", 1, generate_data },
4468 {"get_read", 0, get_read },
4469 {"delete_generate", 0, delete_generate },
4470 {"generate_buffer_data", 1, generate_buffer_data },
4471 {"delete_buffer", 0, delete_buffer_generate},
4472 {"generate_data", 1, generate_data },
4473 {"mget_read", 0, mget_read },
4474 {"mget_read_result", 0, mget_read_result },
4475 {"mget_read_function", 0, mget_read_function },
4476 {"cleanup", 1, cleanup_pairs },
4477 {"generate_large_pairs", 1, generate_large_pairs },
4478 {"generate_data", 1, generate_data },
4479 {"generate_buffer_data", 1, generate_buffer_data },
4480 {"cleanup", 1, cleanup_pairs },
4481 {0, 0, 0}
4482 };
4483
4484 test_st consistent_tests[] ={
4485 {"generate_pairs", 1, generate_pairs },
4486 {"generate_data", 1, generate_data },
4487 {"get_read", 0, get_read_count },
4488 {"cleanup", 1, cleanup_pairs },
4489 {0, 0, 0}
4490 };
4491
4492 test_st consistent_weighted_tests[] ={
4493 {"generate_pairs", 1, generate_pairs },
4494 {"generate_data", 1, generate_data_with_stats },
4495 {"get_read", 0, get_read_count },
4496 {"cleanup", 1, cleanup_pairs },
4497 {0, 0, 0}
4498 };
4499
4500 test_st hsieh_availability[] ={
4501 {"hsieh_avaibility_test",0,hsieh_avaibility_test},
4502 {0, 0, 0}
4503 };
4504
4505 test_st ketama_auto_eject_hosts[] ={
4506 {"auto_eject_hosts", 1, auto_eject_hosts },
4507 {0, 0, 0}
4508 };
4509
4510 test_st hash_tests[] ={
4511 {"md5", 0, md5_run },
4512 {"crc", 0, crc_run },
4513 {"fnv1_64", 0, fnv1_64_run },
4514 {"fnv1a_64", 0, fnv1a_64_run },
4515 {"fnv1_32", 0, fnv1_32_run },
4516 {"fnv1a_32", 0, fnv1a_32_run },
4517 {"hsieh", 0, hsieh_run },
4518 {"murmur", 0, murmur_run },
4519 {"jenkis", 0, jenkins_run },
4520 {0, 0, 0}
4521 };
4522
4523 collection_st collection[] ={
4524 {"hsieh_availability",0,0,hsieh_availability},
4525 {"udp_setup", init_udp, 0, udp_setup_server_tests},
4526 {"udp_io", init_udp, 0, upd_io_tests},
4527 {"udp_binary_io", binary_init_udp, 0, upd_io_tests},
4528 {"block", 0, 0, tests},
4529 {"binary", pre_binary, 0, tests},
4530 {"nonblock", pre_nonblock, 0, tests},
4531 {"nodelay", pre_nodelay, 0, tests},
4532 {"settimer", pre_settimer, 0, tests},
4533 {"md5", pre_md5, 0, tests},
4534 {"crc", pre_crc, 0, tests},
4535 {"hsieh", pre_hsieh, 0, tests},
4536 {"jenkins", pre_jenkins, 0, tests},
4537 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
4538 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
4539 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
4540 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
4541 {"ketama", pre_behavior_ketama, 0, tests},
4542 {"ketama_auto_eject_hosts", pre_behavior_ketama, 0, ketama_auto_eject_hosts},
4543 {"unix_socket", pre_unix_socket, 0, tests},
4544 {"unix_socket_nodelay", pre_nodelay, 0, tests},
4545 {"poll_timeout", poll_timeout, 0, tests},
4546 {"gets", enable_cas, 0, tests},
4547 {"consistent", enable_consistent, 0, tests},
4548 {"deprecated_memory_allocators", deprecated_set_memory_alloc, 0, tests},
4549 {"memory_allocators", set_memory_alloc, 0, tests},
4550 {"prefix", set_prefix, 0, tests},
4551 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
4552 {"string", 0, 0, string_tests},
4553 {"result", 0, 0, result_tests},
4554 {"async", pre_nonblock, 0, async_tests},
4555 {"async_binary", pre_nonblock_binary, 0, async_tests},
4556 {"user", 0, 0, user_tests},
4557 {"generate", 0, 0, generate_tests},
4558 {"generate_hsieh", pre_hsieh, 0, generate_tests},
4559 {"generate_ketama", pre_behavior_ketama, 0, generate_tests},
4560 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
4561 {"generate_md5", pre_md5, 0, generate_tests},
4562 {"generate_murmur", pre_murmur, 0, generate_tests},
4563 {"generate_jenkins", pre_jenkins, 0, generate_tests},
4564 {"generate_nonblock", pre_nonblock, 0, generate_tests},
4565 {"consistent_not", 0, 0, consistent_tests},
4566 {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests},
4567 {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
4568 {"test_hashes", 0, 0, hash_tests},
4569 {"replication", pre_replication, 0, replication_tests},
4570 {0, 0, 0, 0}
4571 };
4572
4573 #define SERVERS_TO_CREATE 5
4574
4575 /* Prototypes for functions we will pass to test framework */
4576 void *world_create(void);
4577 void world_destroy(void *p);
4578
4579 void *world_create(void)
4580 {
4581 server_startup_st *construct;
4582
4583 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
4584 memset(construct, 0, sizeof(server_startup_st));
4585 construct->count= SERVERS_TO_CREATE;
4586 construct->udp= 0;
4587 server_startup(construct);
4588
4589 return construct;
4590 }
4591
4592
4593 void world_destroy(void *p)
4594 {
4595 server_startup_st *construct= (server_startup_st *)p;
4596 memcached_server_st *servers= (memcached_server_st *)construct->servers;
4597 memcached_server_list_free(servers);
4598
4599 server_shutdown(construct);
4600 free(construct);
4601 }
4602
4603 void get_world(world_st *world)
4604 {
4605 world->collections= collection;
4606 world->create= world_create;
4607 world->destroy= world_destroy;
4608 }