Merge Monty
[awesomized/libmemcached] / tests / function.c
1 /*
2 Sample test application.
3 */
4
5 #include "libmemcached/common.h"
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <time.h>
16 #include "server.h"
17 #include "clients/generator.h"
18 #include "clients/execute.h"
19
20 #ifndef INT64_MAX
21 #define INT64_MAX LONG_MAX
22 #endif
23 #ifndef INT32_MAX
24 #define INT32_MAX INT_MAX
25 #endif
26
27
28 #include "test.h"
29
30 #ifdef HAVE_LIBMEMCACHEDUTIL
31 #include <pthread.h>
32 #include "libmemcached/memcached_util.h"
33 #endif
34
35 #define GLOBAL_COUNT 10000
36 #define GLOBAL2_COUNT 100
37 #define SERVERS_TO_CREATE 5
38 static uint32_t global_count;
39
40 static pairs_st *global_pairs;
41 static char *global_keys[GLOBAL_COUNT];
42 static size_t global_keys_length[GLOBAL_COUNT];
43
44 static test_return init_test(memcached_st *not_used __attribute__((unused)))
45 {
46 memcached_st memc;
47
48 (void)memcached_create(&memc);
49 memcached_free(&memc);
50
51 return 0;
52 }
53
54 static test_return server_list_null_test(memcached_st *ptr __attribute__((unused)))
55 {
56 memcached_server_st *server_list;
57 memcached_return rc;
58
59 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, NULL);
60 assert(server_list == NULL);
61
62 server_list= memcached_server_list_append_with_weight(NULL, "localhost", 0, 0, NULL);
63 assert(server_list == NULL);
64
65 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, &rc);
66 assert(server_list == NULL);
67
68 return 0;
69 }
70
71 #define TEST_PORT_COUNT 7
72 uint32_t test_ports[TEST_PORT_COUNT];
73
74 static memcached_return server_display_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
75 {
76 /* Do Nothing */
77 uint32_t bigger= *((uint32_t *)(context));
78 assert(bigger <= server->port);
79 *((uint32_t *)(context))= server->port;
80
81 return MEMCACHED_SUCCESS;
82 }
83
84 static test_return server_sort_test(memcached_st *ptr __attribute__((unused)))
85 {
86 uint32_t x;
87 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
88 memcached_return rc;
89 memcached_server_function callbacks[1];
90 memcached_st *local_memc;
91
92 local_memc= memcached_create(NULL);
93 assert(local_memc);
94 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
95
96 for (x= 0; x < TEST_PORT_COUNT; x++)
97 {
98 test_ports[x]= (uint32_t)random() % 64000;
99 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
100 assert(local_memc->number_of_hosts == x + 1);
101 assert(local_memc->hosts[0].count == x+1);
102 assert(rc == MEMCACHED_SUCCESS);
103 }
104
105 callbacks[0]= server_display_function;
106 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
107
108
109 memcached_free(local_memc);
110
111 return 0;
112 }
113
114 static test_return server_sort2_test(memcached_st *ptr __attribute__((unused)))
115 {
116 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
117 memcached_return rc;
118 memcached_server_function callbacks[1];
119 memcached_st *local_memc;
120
121 local_memc= memcached_create(NULL);
122 assert(local_memc);
123 rc= memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
124 assert(rc == MEMCACHED_SUCCESS);
125
126 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
127 assert(rc == MEMCACHED_SUCCESS);
128 assert(local_memc->hosts[0].port == 43043);
129
130 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
131 assert(rc == MEMCACHED_SUCCESS);
132 assert(local_memc->hosts[0].port == 43042);
133 assert(local_memc->hosts[1].port == 43043);
134
135 callbacks[0]= server_display_function;
136 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
137
138
139 memcached_free(local_memc);
140
141 return 0;
142 }
143
144 static memcached_return server_display_unsort_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
145 {
146 /* Do Nothing */
147 uint32_t x= *((uint32_t *)(context));
148
149 assert(test_ports[x] == server->port);
150 *((uint32_t *)(context))= ++x;
151
152 return MEMCACHED_SUCCESS;
153 }
154
155 static test_return server_unsort_test(memcached_st *ptr __attribute__((unused)))
156 {
157 uint32_t x;
158 uint32_t counter= 0; /* Prime the value for the assert in server_display_function */
159 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
160 memcached_return rc;
161 memcached_server_function callbacks[1];
162 memcached_st *local_memc;
163
164 local_memc= memcached_create(NULL);
165 assert(local_memc);
166
167 for (x= 0; x < TEST_PORT_COUNT; x++)
168 {
169 test_ports[x]= (uint32_t)(random() % 64000);
170 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
171 assert(local_memc->number_of_hosts == x+1);
172 assert(local_memc->hosts[0].count == x+1);
173 assert(rc == MEMCACHED_SUCCESS);
174 }
175
176 callbacks[0]= server_display_unsort_function;
177 memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1);
178
179 /* Now we sort old data! */
180 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
181 callbacks[0]= server_display_function;
182 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
183
184
185 memcached_free(local_memc);
186
187 return 0;
188 }
189
190 static test_return allocation_test(memcached_st *not_used __attribute__((unused)))
191 {
192 memcached_st *memc;
193 memc= memcached_create(NULL);
194 assert(memc);
195 memcached_free(memc);
196
197 return 0;
198 }
199
200 static test_return clone_test(memcached_st *memc)
201 {
202 /* All null? */
203 {
204 memcached_st *memc_clone;
205 memc_clone= memcached_clone(NULL, NULL);
206 assert(memc_clone);
207 memcached_free(memc_clone);
208 }
209
210 /* Can we init from null? */
211 {
212 memcached_st *memc_clone;
213 memc_clone= memcached_clone(NULL, memc);
214 assert(memc_clone);
215
216 assert(memc_clone->call_free == memc->call_free);
217 assert(memc_clone->call_malloc == memc->call_malloc);
218 assert(memc_clone->call_realloc == memc->call_realloc);
219 assert(memc_clone->call_calloc == memc->call_calloc);
220 assert(memc_clone->connect_timeout == memc->connect_timeout);
221 assert(memc_clone->delete_trigger == memc->delete_trigger);
222 assert(memc_clone->distribution == memc->distribution);
223 assert(memc_clone->flags == memc->flags);
224 assert(memc_clone->get_key_failure == memc->get_key_failure);
225 assert(memc_clone->hash == memc->hash);
226 assert(memc_clone->hash_continuum == memc->hash_continuum);
227 assert(memc_clone->io_bytes_watermark == memc->io_bytes_watermark);
228 assert(memc_clone->io_msg_watermark == memc->io_msg_watermark);
229 assert(memc_clone->io_key_prefetch == memc->io_key_prefetch);
230 assert(memc_clone->on_cleanup == memc->on_cleanup);
231 assert(memc_clone->on_clone == memc->on_clone);
232 assert(memc_clone->poll_timeout == memc->poll_timeout);
233 assert(memc_clone->rcv_timeout == memc->rcv_timeout);
234 assert(memc_clone->recv_size == memc->recv_size);
235 assert(memc_clone->retry_timeout == memc->retry_timeout);
236 assert(memc_clone->send_size == memc->send_size);
237 assert(memc_clone->server_failure_limit == memc->server_failure_limit);
238 assert(memc_clone->snd_timeout == memc->snd_timeout);
239 assert(memc_clone->user_data == memc->user_data);
240
241 memcached_free(memc_clone);
242 }
243
244 /* Can we init from struct? */
245 {
246 memcached_st declared_clone;
247 memcached_st *memc_clone;
248 memset(&declared_clone, 0 , sizeof(memcached_st));
249 memc_clone= memcached_clone(&declared_clone, NULL);
250 assert(memc_clone);
251 memcached_free(memc_clone);
252 }
253
254 /* Can we init from struct? */
255 {
256 memcached_st declared_clone;
257 memcached_st *memc_clone;
258 memset(&declared_clone, 0 , sizeof(memcached_st));
259 memc_clone= memcached_clone(&declared_clone, memc);
260 assert(memc_clone);
261 memcached_free(memc_clone);
262 }
263
264 return 0;
265 }
266
267 static test_return userdata_test(memcached_st *memc)
268 {
269 void* foo= NULL;
270 assert(memcached_set_user_data(memc, foo) == NULL);
271 assert(memcached_get_user_data(memc) == foo);
272 assert(memcached_set_user_data(memc, NULL) == foo);
273
274 return TEST_SUCCESS;
275 }
276
277 static test_return connection_test(memcached_st *memc)
278 {
279 memcached_return rc;
280
281 rc= memcached_server_add_with_weight(memc, "localhost", 0, 0);
282 assert(rc == MEMCACHED_SUCCESS);
283
284 return 0;
285 }
286
287 static test_return error_test(memcached_st *memc)
288 {
289 memcached_return rc;
290
291 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
292 {
293 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
294 }
295
296 return 0;
297 }
298
299 static test_return set_test(memcached_st *memc)
300 {
301 memcached_return rc;
302 char *key= "foo";
303 char *value= "when we sanitize";
304
305 rc= memcached_set(memc, key, strlen(key),
306 value, strlen(value),
307 (time_t)0, (uint32_t)0);
308 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
309
310 return 0;
311 }
312
313 static test_return append_test(memcached_st *memc)
314 {
315 memcached_return rc;
316 char *key= "fig";
317 char *value= "we";
318 size_t value_length;
319 uint32_t flags;
320
321 rc= memcached_flush(memc, 0);
322 assert(rc == MEMCACHED_SUCCESS);
323
324 rc= memcached_set(memc, key, strlen(key),
325 value, strlen(value),
326 (time_t)0, (uint32_t)0);
327 assert(rc == MEMCACHED_SUCCESS);
328
329 rc= memcached_append(memc, key, strlen(key),
330 " the", strlen(" the"),
331 (time_t)0, (uint32_t)0);
332 assert(rc == MEMCACHED_SUCCESS);
333
334 rc= memcached_append(memc, key, strlen(key),
335 " people", strlen(" people"),
336 (time_t)0, (uint32_t)0);
337 assert(rc == MEMCACHED_SUCCESS);
338
339 value= memcached_get(memc, key, strlen(key),
340 &value_length, &flags, &rc);
341 assert(!memcmp(value, "we the people", strlen("we the people")));
342 assert(strlen("we the people") == value_length);
343 assert(rc == MEMCACHED_SUCCESS);
344 free(value);
345
346 return 0;
347 }
348
349 static test_return append_binary_test(memcached_st *memc)
350 {
351 memcached_return rc;
352 char *key= "numbers";
353 unsigned int *store_ptr;
354 unsigned int store_list[] = { 23, 56, 499, 98, 32847, 0 };
355 char *value;
356 size_t value_length;
357 uint32_t flags;
358 unsigned int x;
359
360 rc= memcached_flush(memc, 0);
361 assert(rc == MEMCACHED_SUCCESS);
362
363 rc= memcached_set(memc,
364 key, strlen(key),
365 NULL, 0,
366 (time_t)0, (uint32_t)0);
367 assert(rc == MEMCACHED_SUCCESS);
368
369 for (x= 0; store_list[x] ; x++)
370 {
371 rc= memcached_append(memc,
372 key, strlen(key),
373 (char *)&store_list[x], sizeof(unsigned int),
374 (time_t)0, (uint32_t)0);
375 assert(rc == MEMCACHED_SUCCESS);
376 }
377
378 value= memcached_get(memc, key, strlen(key),
379 &value_length, &flags, &rc);
380 assert((value_length == (sizeof(unsigned int) * x)));
381 assert(rc == MEMCACHED_SUCCESS);
382
383 store_ptr= (unsigned int *)value;
384 x= 0;
385 while ((size_t)store_ptr < (size_t)(value + value_length))
386 {
387 assert(*store_ptr == store_list[x++]);
388 store_ptr++;
389 }
390 free(value);
391
392 return 0;
393 }
394
395 static test_return cas2_test(memcached_st *memc)
396 {
397 memcached_return rc;
398 char *keys[]= {"fudge", "son", "food"};
399 size_t key_length[]= {5, 3, 4};
400 char *value= "we the people";
401 size_t value_length= strlen("we the people");
402 unsigned int x;
403 memcached_result_st results_obj;
404 memcached_result_st *results;
405 unsigned int set= 1;
406
407 rc= memcached_flush(memc, 0);
408 assert(rc == MEMCACHED_SUCCESS);
409
410 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
411
412 for (x= 0; x < 3; x++)
413 {
414 rc= memcached_set(memc, keys[x], key_length[x],
415 keys[x], key_length[x],
416 (time_t)50, (uint32_t)9);
417 assert(rc == MEMCACHED_SUCCESS);
418 }
419
420 rc= memcached_mget(memc, keys, key_length, 3);
421
422 results= memcached_result_create(memc, &results_obj);
423
424 results= memcached_fetch_result(memc, &results_obj, &rc);
425 assert(results);
426 assert(results->cas);
427 assert(rc == MEMCACHED_SUCCESS);
428 assert(memcached_result_cas(results));
429
430 assert(!memcmp(value, "we the people", strlen("we the people")));
431 assert(strlen("we the people") == value_length);
432 assert(rc == MEMCACHED_SUCCESS);
433
434 memcached_result_free(&results_obj);
435
436 return 0;
437 }
438
439 static test_return cas_test(memcached_st *memc)
440 {
441 memcached_return rc;
442 const char *key= "fun";
443 size_t key_length= strlen(key);
444 const char *value= "we the people";
445 char* keys[2] = { (char*)key, NULL };
446 size_t keylengths[2] = { strlen(key), 0 };
447 size_t value_length= strlen(value);
448 const char *value2= "change the value";
449 size_t value2_length= strlen(value2);
450
451 memcached_result_st results_obj;
452 memcached_result_st *results;
453 unsigned int set= 1;
454
455 rc= memcached_flush(memc, 0);
456 assert(rc == MEMCACHED_SUCCESS);
457
458 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
459
460 rc= memcached_set(memc, key, strlen(key),
461 value, strlen(value),
462 (time_t)0, (uint32_t)0);
463 assert(rc == MEMCACHED_SUCCESS);
464
465 rc= memcached_mget(memc, keys, keylengths, 1);
466
467 results= memcached_result_create(memc, &results_obj);
468
469 results= memcached_fetch_result(memc, &results_obj, &rc);
470 assert(results);
471 assert(rc == MEMCACHED_SUCCESS);
472 assert(memcached_result_cas(results));
473 assert(!memcmp(value, memcached_result_value(results), value_length));
474 assert(strlen(memcached_result_value(results)) == value_length);
475 assert(rc == MEMCACHED_SUCCESS);
476 uint64_t cas = memcached_result_cas(results);
477
478 #if 0
479 results= memcached_fetch_result(memc, &results_obj, &rc);
480 assert(rc == MEMCACHED_END);
481 assert(results == NULL);
482 #endif
483
484 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
485 assert(rc == MEMCACHED_SUCCESS);
486
487 /*
488 * The item will have a new cas value, so try to set it again with the old
489 * value. This should fail!
490 */
491 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
492 assert(rc == MEMCACHED_DATA_EXISTS);
493
494 memcached_result_free(&results_obj);
495
496 return 0;
497 }
498
499 static test_return prepend_test(memcached_st *memc)
500 {
501 memcached_return rc;
502 char *key= "fig";
503 char *value= "people";
504 size_t value_length;
505 uint32_t flags;
506
507 rc= memcached_flush(memc, 0);
508 assert(rc == MEMCACHED_SUCCESS);
509
510 rc= memcached_set(memc, key, strlen(key),
511 value, strlen(value),
512 (time_t)0, (uint32_t)0);
513 assert(rc == MEMCACHED_SUCCESS);
514
515 rc= memcached_prepend(memc, key, strlen(key),
516 "the ", strlen("the "),
517 (time_t)0, (uint32_t)0);
518 assert(rc == MEMCACHED_SUCCESS);
519
520 rc= memcached_prepend(memc, key, strlen(key),
521 "we ", strlen("we "),
522 (time_t)0, (uint32_t)0);
523 assert(rc == MEMCACHED_SUCCESS);
524
525 value= memcached_get(memc, key, strlen(key),
526 &value_length, &flags, &rc);
527 assert(!memcmp(value, "we the people", strlen("we the people")));
528 assert(strlen("we the people") == value_length);
529 assert(rc == MEMCACHED_SUCCESS);
530 free(value);
531
532 return 0;
533 }
534
535 /*
536 Set the value, then quit to make sure it is flushed.
537 Come back in and test that add fails.
538 */
539 static test_return add_test(memcached_st *memc)
540 {
541 memcached_return rc;
542 char *key= "foo";
543 char *value= "when we sanitize";
544 unsigned long long setting_value;
545
546 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
547
548 rc= memcached_set(memc, key, strlen(key),
549 value, strlen(value),
550 (time_t)0, (uint32_t)0);
551 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
552 memcached_quit(memc);
553 rc= memcached_add(memc, key, strlen(key),
554 value, strlen(value),
555 (time_t)0, (uint32_t)0);
556
557 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
558 if (setting_value)
559 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
560 else
561 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_DATA_EXISTS);
562
563 return 0;
564 }
565
566 /*
567 ** There was a problem of leaking filedescriptors in the initial release
568 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
569 ** systems it seems that the kernel is slow on reclaiming the resources
570 ** because the connects starts to time out (the test doesn't do much
571 ** anyway, so just loop 10 iterations)
572 */
573 static test_return add_wrapper(memcached_st *memc)
574 {
575 unsigned int x;
576 unsigned int max= 10000;
577 #ifdef __sun
578 max= 10;
579 #endif
580
581 for (x= 0; x < max; x++)
582 add_test(memc);
583
584 return 0;
585 }
586
587 static test_return replace_test(memcached_st *memc)
588 {
589 memcached_return rc;
590 char *key= "foo";
591 char *value= "when we sanitize";
592 char *original= "first we insert some data";
593
594 rc= memcached_set(memc, key, strlen(key),
595 original, strlen(original),
596 (time_t)0, (uint32_t)0);
597 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
598
599 rc= memcached_replace(memc, key, strlen(key),
600 value, strlen(value),
601 (time_t)0, (uint32_t)0);
602 assert(rc == MEMCACHED_SUCCESS);
603
604 return 0;
605 }
606
607 static test_return delete_test(memcached_st *memc)
608 {
609 memcached_return rc;
610 char *key= "foo";
611 char *value= "when we sanitize";
612
613 rc= memcached_set(memc, key, strlen(key),
614 value, strlen(value),
615 (time_t)0, (uint32_t)0);
616 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
617
618 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
619 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
620
621 return 0;
622 }
623
624 static test_return flush_test(memcached_st *memc)
625 {
626 memcached_return rc;
627
628 rc= memcached_flush(memc, 0);
629 assert(rc == MEMCACHED_SUCCESS);
630
631 return 0;
632 }
633
634 static memcached_return server_function(memcached_st *ptr __attribute__((unused)),
635 memcached_server_st *server __attribute__((unused)),
636 void *context __attribute__((unused)))
637 {
638 /* Do Nothing */
639
640 return MEMCACHED_SUCCESS;
641 }
642
643 static test_return memcached_server_cursor_test(memcached_st *memc)
644 {
645 char *context= "foo bad";
646 memcached_server_function callbacks[1];
647
648 callbacks[0]= server_function;
649 memcached_server_cursor(memc, callbacks, context, 1);
650
651 return 0;
652 }
653
654 static test_return bad_key_test(memcached_st *memc)
655 {
656 memcached_return rc;
657 char *key= "foo bad";
658 char *string;
659 size_t string_length;
660 uint32_t flags;
661 memcached_st *memc_clone;
662 unsigned int set= 1;
663 size_t max_keylen= 0xffff;
664
665 memc_clone= memcached_clone(NULL, memc);
666 assert(memc_clone);
667
668 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
669 assert(rc == MEMCACHED_SUCCESS);
670
671 /* All keys are valid in the binary protocol (except for length) */
672 if (memcached_behavior_get(memc_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0)
673 {
674 string= memcached_get(memc_clone, key, strlen(key),
675 &string_length, &flags, &rc);
676 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
677 assert(string_length == 0);
678 assert(!string);
679
680 set= 0;
681 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
682 assert(rc == MEMCACHED_SUCCESS);
683 string= memcached_get(memc_clone, key, strlen(key),
684 &string_length, &flags, &rc);
685 assert(rc == MEMCACHED_NOTFOUND);
686 assert(string_length == 0);
687 assert(!string);
688
689 /* Test multi key for bad keys */
690 char *keys[] = { "GoodKey", "Bad Key", "NotMine" };
691 size_t key_lengths[] = { 7, 7, 7 };
692 set= 1;
693 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
694 assert(rc == MEMCACHED_SUCCESS);
695
696 rc= memcached_mget(memc_clone, keys, key_lengths, 3);
697 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
698
699 rc= memcached_mget_by_key(memc_clone, "foo daddy", 9, keys, key_lengths, 1);
700 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
701
702 max_keylen= 250;
703
704 /* The following test should be moved to the end of this function when the
705 memcached server is updated to allow max size length of the keys in the
706 binary protocol
707 */
708 rc= memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
709 assert(rc == MEMCACHED_SUCCESS);
710
711 char *longkey= malloc(max_keylen + 1);
712 if (longkey != NULL)
713 {
714 memset(longkey, 'a', max_keylen + 1);
715 string= memcached_get(memc_clone, longkey, max_keylen,
716 &string_length, &flags, &rc);
717 assert(rc == MEMCACHED_NOTFOUND);
718 assert(string_length == 0);
719 assert(!string);
720
721 string= memcached_get(memc_clone, longkey, max_keylen + 1,
722 &string_length, &flags, &rc);
723 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
724 assert(string_length == 0);
725 assert(!string);
726
727 free(longkey);
728 }
729 }
730
731 /* Make sure zero length keys are marked as bad */
732 set= 1;
733 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
734 assert(rc == MEMCACHED_SUCCESS);
735 string= memcached_get(memc_clone, key, 0,
736 &string_length, &flags, &rc);
737 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
738 assert(string_length == 0);
739 assert(!string);
740
741 memcached_free(memc_clone);
742
743 return 0;
744 }
745
746 #define READ_THROUGH_VALUE "set for me"
747 static memcached_return read_through_trigger(memcached_st *memc __attribute__((unused)),
748 char *key __attribute__((unused)),
749 size_t key_length __attribute__((unused)),
750 memcached_result_st *result)
751 {
752
753 return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
754 }
755
756 static test_return read_through(memcached_st *memc)
757 {
758 memcached_return rc;
759 char *key= "foo";
760 char *string;
761 size_t string_length;
762 uint32_t flags;
763 memcached_trigger_key cb= (memcached_trigger_key)read_through_trigger;
764
765 string= memcached_get(memc, key, strlen(key),
766 &string_length, &flags, &rc);
767
768 assert(rc == MEMCACHED_NOTFOUND);
769 assert(string_length == 0);
770 assert(!string);
771
772 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE,
773 *(void **)&cb);
774 assert(rc == MEMCACHED_SUCCESS);
775
776 string= memcached_get(memc, key, strlen(key),
777 &string_length, &flags, &rc);
778
779 assert(rc == MEMCACHED_SUCCESS);
780 assert(string_length == strlen(READ_THROUGH_VALUE));
781 assert(!strcmp(READ_THROUGH_VALUE, string));
782 free(string);
783
784 string= memcached_get(memc, key, strlen(key),
785 &string_length, &flags, &rc);
786
787 assert(rc == MEMCACHED_SUCCESS);
788 assert(string_length == strlen(READ_THROUGH_VALUE));
789 assert(!strcmp(READ_THROUGH_VALUE, string));
790 free(string);
791
792 return 0;
793 }
794
795 static memcached_return delete_trigger(memcached_st *ptr __attribute__((unused)),
796 const char *key,
797 size_t key_length __attribute__((unused)))
798 {
799 assert(key);
800
801 return MEMCACHED_SUCCESS;
802 }
803
804 static test_return delete_through(memcached_st *memc)
805 {
806 memcached_trigger_delete_key callback;
807 memcached_return rc;
808
809 callback= (memcached_trigger_delete_key)delete_trigger;
810
811 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, *(void**)&callback);
812 assert(rc == MEMCACHED_SUCCESS);
813
814 return 0;
815 }
816
817 static test_return get_test(memcached_st *memc)
818 {
819 memcached_return rc;
820 char *key= "foo";
821 char *string;
822 size_t string_length;
823 uint32_t flags;
824
825 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
826 assert(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
827
828 string= memcached_get(memc, key, strlen(key),
829 &string_length, &flags, &rc);
830
831 assert(rc == MEMCACHED_NOTFOUND);
832 assert(string_length == 0);
833 assert(!string);
834
835 return 0;
836 }
837
838 static test_return get_test2(memcached_st *memc)
839 {
840 memcached_return rc;
841 char *key= "foo";
842 char *value= "when we sanitize";
843 char *string;
844 size_t string_length;
845 uint32_t flags;
846
847 rc= memcached_set(memc, key, strlen(key),
848 value, strlen(value),
849 (time_t)0, (uint32_t)0);
850 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
851
852 string= memcached_get(memc, key, strlen(key),
853 &string_length, &flags, &rc);
854
855 assert(string);
856 assert(rc == MEMCACHED_SUCCESS);
857 assert(string_length == strlen(value));
858 assert(!memcmp(string, value, string_length));
859
860 free(string);
861
862 return 0;
863 }
864
865 static test_return set_test2(memcached_st *memc)
866 {
867 memcached_return rc;
868 char *key= "foo";
869 char *value= "train in the brain";
870 size_t value_length= strlen(value);
871 unsigned int x;
872
873 for (x= 0; x < 10; x++)
874 {
875 rc= memcached_set(memc, key, strlen(key),
876 value, value_length,
877 (time_t)0, (uint32_t)0);
878 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
879 }
880
881 return 0;
882 }
883
884 static test_return set_test3(memcached_st *memc)
885 {
886 memcached_return rc;
887 char *value;
888 size_t value_length= 8191;
889 unsigned int x;
890
891 value = (char*)malloc(value_length);
892 assert(value);
893
894 for (x= 0; x < value_length; x++)
895 value[x] = (char) (x % 127);
896
897 /* The dump test relies on there being at least 32 items in memcached */
898 for (x= 0; x < 32; x++)
899 {
900 char key[16];
901
902 sprintf(key, "foo%u", x);
903
904 rc= memcached_set(memc, key, strlen(key),
905 value, value_length,
906 (time_t)0, (uint32_t)0);
907 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
908 }
909
910 free(value);
911
912 return 0;
913 }
914
915 static test_return get_test3(memcached_st *memc)
916 {
917 memcached_return rc;
918 char *key= "foo";
919 char *value;
920 size_t value_length= 8191;
921 char *string;
922 size_t string_length;
923 uint32_t flags;
924 uint32_t x;
925
926 value = (char*)malloc(value_length);
927 assert(value);
928
929 for (x= 0; x < value_length; x++)
930 value[x] = (char) (x % 127);
931
932 rc= memcached_set(memc, key, strlen(key),
933 value, value_length,
934 (time_t)0, (uint32_t)0);
935 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
936
937 string= memcached_get(memc, key, strlen(key),
938 &string_length, &flags, &rc);
939
940 assert(rc == MEMCACHED_SUCCESS);
941 assert(string);
942 assert(string_length == value_length);
943 assert(!memcmp(string, value, string_length));
944
945 free(string);
946 free(value);
947
948 return 0;
949 }
950
951 static test_return get_test4(memcached_st *memc)
952 {
953 memcached_return rc;
954 char *key= "foo";
955 char *value;
956 size_t value_length= 8191;
957 char *string;
958 size_t string_length;
959 uint32_t flags;
960 uint32_t x;
961
962 value = (char*)malloc(value_length);
963 assert(value);
964
965 for (x= 0; x < value_length; x++)
966 value[x] = (char) (x % 127);
967
968 rc= memcached_set(memc, key, strlen(key),
969 value, value_length,
970 (time_t)0, (uint32_t)0);
971 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
972
973 for (x= 0; x < 10; x++)
974 {
975 string= memcached_get(memc, key, strlen(key),
976 &string_length, &flags, &rc);
977
978 assert(rc == MEMCACHED_SUCCESS);
979 assert(string);
980 assert(string_length == value_length);
981 assert(!memcmp(string, value, string_length));
982 free(string);
983 }
984
985 free(value);
986
987 return 0;
988 }
989
990 /*
991 * This test verifies that memcached_read_one_response doesn't try to
992 * dereference a NIL-pointer if you issue a multi-get and don't read out all
993 * responses before you execute a storage command.
994 */
995 static test_return get_test5(memcached_st *memc)
996 {
997 /*
998 ** Request the same key twice, to ensure that we hash to the same server
999 ** (so that we have multiple response values queued up) ;-)
1000 */
1001 char *keys[]= { "key", "key" };
1002 size_t lengths[]= { 3, 3 };
1003 uint32_t flags;
1004 size_t rlen;
1005
1006 memcached_return rc= memcached_set(memc, keys[0], lengths[0],
1007 keys[0], lengths[0], 0, 0);
1008 assert(rc == MEMCACHED_SUCCESS);
1009 rc= memcached_mget(memc, keys, lengths, 2);
1010
1011 memcached_result_st results_obj;
1012 memcached_result_st *results;
1013 results=memcached_result_create(memc, &results_obj);
1014 assert(results);
1015 results=memcached_fetch_result(memc, &results_obj, &rc);
1016 assert(results);
1017 memcached_result_free(&results_obj);
1018
1019 /* Don't read out the second result, but issue a set instead.. */
1020 rc= memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0);
1021 assert(rc == MEMCACHED_SUCCESS);
1022
1023 char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
1024 &rlen, &flags, &rc);
1025 assert(val == NULL);
1026 assert(rc == MEMCACHED_NOTFOUND);
1027 val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
1028 assert(val != NULL);
1029 assert(rc == MEMCACHED_SUCCESS);
1030 free(val);
1031
1032 return TEST_SUCCESS;
1033 }
1034
1035 /* Do not copy the style of this code, I just access hosts to testthis function */
1036 static test_return stats_servername_test(memcached_st *memc)
1037 {
1038 memcached_return rc;
1039 memcached_stat_st memc_stat;
1040 rc= memcached_stat_servername(&memc_stat, NULL,
1041 memc->hosts[0].hostname,
1042 memc->hosts[0].port);
1043
1044 return 0;
1045 }
1046
1047 static test_return increment_test(memcached_st *memc)
1048 {
1049 uint64_t new_number;
1050 memcached_return rc;
1051 char *key= "number";
1052 char *value= "0";
1053
1054 rc= memcached_set(memc, key, strlen(key),
1055 value, strlen(value),
1056 (time_t)0, (uint32_t)0);
1057 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1058
1059 rc= memcached_increment(memc, key, strlen(key),
1060 1, &new_number);
1061 assert(rc == MEMCACHED_SUCCESS);
1062 assert(new_number == 1);
1063
1064 rc= memcached_increment(memc, key, strlen(key),
1065 1, &new_number);
1066 assert(rc == MEMCACHED_SUCCESS);
1067 assert(new_number == 2);
1068
1069 return 0;
1070 }
1071
1072 static test_return increment_with_initial_test(memcached_st *memc)
1073 {
1074 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1075 {
1076 uint64_t new_number;
1077 memcached_return rc;
1078 char *key= "number";
1079 uint64_t initial= 0;
1080
1081 rc= memcached_increment_with_initial(memc, key, strlen(key),
1082 1, initial, 0, &new_number);
1083 assert(rc == MEMCACHED_SUCCESS);
1084 assert(new_number == initial);
1085
1086 rc= memcached_increment_with_initial(memc, key, strlen(key),
1087 1, initial, 0, &new_number);
1088 assert(rc == MEMCACHED_SUCCESS);
1089 assert(new_number == (initial + 1));
1090 }
1091 return 0;
1092 }
1093
1094 static test_return decrement_test(memcached_st *memc)
1095 {
1096 uint64_t new_number;
1097 memcached_return rc;
1098 char *key= "number";
1099 char *value= "3";
1100
1101 rc= memcached_set(memc, key, strlen(key),
1102 value, strlen(value),
1103 (time_t)0, (uint32_t)0);
1104 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1105
1106 rc= memcached_decrement(memc, key, strlen(key),
1107 1, &new_number);
1108 assert(rc == MEMCACHED_SUCCESS);
1109 assert(new_number == 2);
1110
1111 rc= memcached_decrement(memc, key, strlen(key),
1112 1, &new_number);
1113 assert(rc == MEMCACHED_SUCCESS);
1114 assert(new_number == 1);
1115
1116 return 0;
1117 }
1118
1119 static test_return decrement_with_initial_test(memcached_st *memc)
1120 {
1121 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1122 {
1123 uint64_t new_number;
1124 memcached_return rc;
1125 char *key= "number";
1126 uint64_t initial= 3;
1127
1128 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1129 1, initial, 0, &new_number);
1130 assert(rc == MEMCACHED_SUCCESS);
1131 assert(new_number == initial);
1132
1133 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1134 1, initial, 0, &new_number);
1135 assert(rc == MEMCACHED_SUCCESS);
1136 assert(new_number == (initial - 1));
1137 }
1138 return 0;
1139 }
1140
1141 static test_return quit_test(memcached_st *memc)
1142 {
1143 memcached_return rc;
1144 char *key= "fudge";
1145 char *value= "sanford and sun";
1146
1147 rc= memcached_set(memc, key, strlen(key),
1148 value, strlen(value),
1149 (time_t)10, (uint32_t)3);
1150 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1151 memcached_quit(memc);
1152
1153 rc= memcached_set(memc, key, strlen(key),
1154 value, strlen(value),
1155 (time_t)50, (uint32_t)9);
1156 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1157
1158 return 0;
1159 }
1160
1161 static test_return mget_result_test(memcached_st *memc)
1162 {
1163 memcached_return rc;
1164 char *keys[]= {"fudge", "son", "food"};
1165 size_t key_length[]= {5, 3, 4};
1166 unsigned int x;
1167
1168 memcached_result_st results_obj;
1169 memcached_result_st *results;
1170
1171 results= memcached_result_create(memc, &results_obj);
1172 assert(results);
1173 assert(&results_obj == results);
1174
1175 /* We need to empty the server before continueing test */
1176 rc= memcached_flush(memc, 0);
1177 assert(rc == MEMCACHED_SUCCESS);
1178
1179 rc= memcached_mget(memc, keys, key_length, 3);
1180 assert(rc == MEMCACHED_SUCCESS);
1181
1182 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1183 {
1184 assert(results);
1185 }
1186
1187 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1188 assert(!results);
1189 assert(rc == MEMCACHED_END);
1190
1191 for (x= 0; x < 3; x++)
1192 {
1193 rc= memcached_set(memc, keys[x], key_length[x],
1194 keys[x], key_length[x],
1195 (time_t)50, (uint32_t)9);
1196 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1197 }
1198
1199 rc= memcached_mget(memc, keys, key_length, 3);
1200 assert(rc == MEMCACHED_SUCCESS);
1201
1202 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1203 {
1204 assert(results);
1205 assert(&results_obj == results);
1206 assert(rc == MEMCACHED_SUCCESS);
1207 assert(memcached_result_key_length(results) == memcached_result_length(results));
1208 assert(!memcmp(memcached_result_key_value(results),
1209 memcached_result_value(results),
1210 memcached_result_length(results)));
1211 }
1212
1213 memcached_result_free(&results_obj);
1214
1215 return 0;
1216 }
1217
1218 static test_return mget_result_alloc_test(memcached_st *memc)
1219 {
1220 memcached_return rc;
1221 char *keys[]= {"fudge", "son", "food"};
1222 size_t key_length[]= {5, 3, 4};
1223 unsigned int x;
1224
1225 memcached_result_st *results;
1226
1227 /* We need to empty the server before continueing test */
1228 rc= memcached_flush(memc, 0);
1229 assert(rc == MEMCACHED_SUCCESS);
1230
1231 rc= memcached_mget(memc, keys, key_length, 3);
1232 assert(rc == MEMCACHED_SUCCESS);
1233
1234 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
1235 {
1236 assert(results);
1237 }
1238 assert(!results);
1239 assert(rc == MEMCACHED_END);
1240
1241 for (x= 0; x < 3; x++)
1242 {
1243 rc= memcached_set(memc, keys[x], key_length[x],
1244 keys[x], key_length[x],
1245 (time_t)50, (uint32_t)9);
1246 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1247 }
1248
1249 rc= memcached_mget(memc, keys, key_length, 3);
1250 assert(rc == MEMCACHED_SUCCESS);
1251
1252 x= 0;
1253 while ((results= memcached_fetch_result(memc, NULL, &rc)))
1254 {
1255 assert(results);
1256 assert(rc == MEMCACHED_SUCCESS);
1257 assert(memcached_result_key_length(results) == memcached_result_length(results));
1258 assert(!memcmp(memcached_result_key_value(results),
1259 memcached_result_value(results),
1260 memcached_result_length(results)));
1261 memcached_result_free(results);
1262 x++;
1263 }
1264
1265 return 0;
1266 }
1267
1268 /* Count the results */
1269 static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
1270 memcached_result_st *result __attribute__((unused)),
1271 void *context)
1272 {
1273 unsigned int *counter= (unsigned int *)context;
1274
1275 *counter= *counter + 1;
1276
1277 return MEMCACHED_SUCCESS;
1278 }
1279
1280 static test_return mget_result_function(memcached_st *memc)
1281 {
1282 memcached_return rc;
1283 char *keys[]= {"fudge", "son", "food"};
1284 size_t key_length[]= {5, 3, 4};
1285 unsigned int x;
1286 unsigned int counter;
1287 memcached_execute_function callbacks[1];
1288
1289 /* We need to empty the server before continueing test */
1290 rc= memcached_flush(memc, 0);
1291 for (x= 0; x < 3; x++)
1292 {
1293 rc= memcached_set(memc, keys[x], key_length[x],
1294 keys[x], key_length[x],
1295 (time_t)50, (uint32_t)9);
1296 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1297 }
1298
1299 rc= memcached_mget(memc, keys, key_length, 3);
1300 assert(rc == MEMCACHED_SUCCESS);
1301
1302 callbacks[0]= &callback_counter;
1303 counter= 0;
1304 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1305
1306 assert(counter == 3);
1307
1308 return 0;
1309 }
1310
1311 static test_return mget_test(memcached_st *memc)
1312 {
1313 memcached_return rc;
1314 char *keys[]= {"fudge", "son", "food"};
1315 size_t key_length[]= {5, 3, 4};
1316 unsigned int x;
1317 uint32_t flags;
1318
1319 char return_key[MEMCACHED_MAX_KEY];
1320 size_t return_key_length;
1321 char *return_value;
1322 size_t return_value_length;
1323
1324 /* We need to empty the server before continueing test */
1325 rc= memcached_flush(memc, 0);
1326 assert(rc == MEMCACHED_SUCCESS);
1327
1328 rc= memcached_mget(memc, keys, key_length, 3);
1329 assert(rc == MEMCACHED_SUCCESS);
1330
1331 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1332 &return_value_length, &flags, &rc)) != NULL)
1333 {
1334 assert(return_value);
1335 }
1336 assert(!return_value);
1337 assert(return_value_length == 0);
1338 assert(rc == MEMCACHED_END);
1339
1340 for (x= 0; x < 3; x++)
1341 {
1342 rc= memcached_set(memc, keys[x], key_length[x],
1343 keys[x], key_length[x],
1344 (time_t)50, (uint32_t)9);
1345 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1346 }
1347
1348 rc= memcached_mget(memc, keys, key_length, 3);
1349 assert(rc == MEMCACHED_SUCCESS);
1350
1351 x= 0;
1352 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1353 &return_value_length, &flags, &rc)))
1354 {
1355 assert(return_value);
1356 assert(rc == MEMCACHED_SUCCESS);
1357 assert(return_key_length == return_value_length);
1358 assert(!memcmp(return_value, return_key, return_value_length));
1359 free(return_value);
1360 x++;
1361 }
1362
1363 return 0;
1364 }
1365
1366 static test_return get_stats_keys(memcached_st *memc)
1367 {
1368 char **list;
1369 char **ptr;
1370 memcached_stat_st memc_stat;
1371 memcached_return rc;
1372
1373 list= memcached_stat_get_keys(memc, &memc_stat, &rc);
1374 assert(rc == MEMCACHED_SUCCESS);
1375 for (ptr= list; *ptr; ptr++)
1376 assert(*ptr);
1377 fflush(stdout);
1378
1379 free(list);
1380
1381 return 0;
1382 }
1383
1384 static test_return version_string_test(memcached_st *memc __attribute__((unused)))
1385 {
1386 const char *version_string;
1387
1388 version_string= memcached_lib_version();
1389
1390 assert(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
1391
1392 return 0;
1393 }
1394
1395 static test_return get_stats(memcached_st *memc)
1396 {
1397 unsigned int x;
1398 char **list;
1399 char **ptr;
1400 memcached_return rc;
1401 memcached_stat_st *memc_stat;
1402
1403 memc_stat= memcached_stat(memc, NULL, &rc);
1404 assert(rc == MEMCACHED_SUCCESS);
1405
1406 assert(rc == MEMCACHED_SUCCESS);
1407 assert(memc_stat);
1408
1409 for (x= 0; x < memcached_server_count(memc); x++)
1410 {
1411 list= memcached_stat_get_keys(memc, memc_stat+x, &rc);
1412 assert(rc == MEMCACHED_SUCCESS);
1413 for (ptr= list; *ptr; ptr++);
1414
1415 free(list);
1416 }
1417
1418 memcached_stat_free(NULL, memc_stat);
1419
1420 return 0;
1421 }
1422
1423 static test_return add_host_test(memcached_st *memc)
1424 {
1425 unsigned int x;
1426 memcached_server_st *servers;
1427 memcached_return rc;
1428 char servername[]= "0.example.com";
1429
1430 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
1431 assert(servers);
1432 assert(1 == memcached_server_list_count(servers));
1433
1434 for (x= 2; x < 20; x++)
1435 {
1436 char buffer[SMALL_STRING_LEN];
1437
1438 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1439 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
1440 &rc);
1441 assert(rc == MEMCACHED_SUCCESS);
1442 assert(x == memcached_server_list_count(servers));
1443 }
1444
1445 rc= memcached_server_push(memc, servers);
1446 assert(rc == MEMCACHED_SUCCESS);
1447 rc= memcached_server_push(memc, servers);
1448 assert(rc == MEMCACHED_SUCCESS);
1449
1450 memcached_server_list_free(servers);
1451
1452 return 0;
1453 }
1454
1455 static memcached_return clone_test_callback(memcached_st *parent __attribute__((unused)), memcached_st *memc_clone __attribute__((unused)))
1456 {
1457 return MEMCACHED_SUCCESS;
1458 }
1459
1460 static memcached_return cleanup_test_callback(memcached_st *ptr __attribute__((unused)))
1461 {
1462 return MEMCACHED_SUCCESS;
1463 }
1464
1465 static test_return callback_test(memcached_st *memc)
1466 {
1467 /* Test User Data */
1468 {
1469 int x= 5;
1470 int *test_ptr;
1471 memcached_return rc;
1472
1473 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
1474 assert(rc == MEMCACHED_SUCCESS);
1475 test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
1476 assert(*test_ptr == x);
1477 }
1478
1479 /* Test Clone Callback */
1480 {
1481 memcached_clone_func clone_cb= (memcached_clone_func)clone_test_callback;
1482 void *clone_cb_ptr= *(void **)&clone_cb;
1483 void *temp_function= NULL;
1484 memcached_return rc;
1485
1486 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1487 clone_cb_ptr);
1488 assert(rc == MEMCACHED_SUCCESS);
1489 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1490 assert(temp_function == clone_cb_ptr);
1491 }
1492
1493 /* Test Cleanup Callback */
1494 {
1495 memcached_cleanup_func cleanup_cb=
1496 (memcached_cleanup_func)cleanup_test_callback;
1497 void *cleanup_cb_ptr= *(void **)&cleanup_cb;
1498 void *temp_function= NULL;
1499 memcached_return rc;
1500
1501 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1502 cleanup_cb_ptr);
1503 assert(rc == MEMCACHED_SUCCESS);
1504 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1505 assert(temp_function == cleanup_cb_ptr);
1506 }
1507
1508 return 0;
1509 }
1510
1511 /* We don't test the behavior itself, we test the switches */
1512 static test_return behavior_test(memcached_st *memc)
1513 {
1514 uint64_t value;
1515 uint32_t set= 1;
1516
1517 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1518 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1519 assert(value == 1);
1520
1521 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1522 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1523 assert(value == 1);
1524
1525 set= MEMCACHED_HASH_MD5;
1526 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1527 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1528 assert(value == MEMCACHED_HASH_MD5);
1529
1530 set= 0;
1531
1532 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1533 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1534 assert(value == 0);
1535
1536 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1537 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1538 assert(value == 0);
1539
1540 set= MEMCACHED_HASH_DEFAULT;
1541 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1542 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1543 assert(value == MEMCACHED_HASH_DEFAULT);
1544
1545 set= MEMCACHED_HASH_CRC;
1546 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1547 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1548 assert(value == MEMCACHED_HASH_CRC);
1549
1550 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1551 assert(value > 0);
1552
1553 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1554 assert(value > 0);
1555
1556 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
1557 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, value + 1);
1558 assert((value + 1) == memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
1559 return 0;
1560 }
1561
1562 /* Test case provided by Cal Haldenbrand */
1563 static test_return user_supplied_bug1(memcached_st *memc)
1564 {
1565 unsigned int setter= 1;
1566 unsigned int x;
1567
1568 unsigned long long total= 0;
1569 uint32_t size= 0;
1570 char key[10];
1571 char randomstuff[6 * 1024];
1572 memcached_return rc;
1573
1574 memset(randomstuff, 0, 6 * 1024);
1575
1576 /* We just keep looking at the same values over and over */
1577 srandom(10);
1578
1579 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1580 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1581
1582
1583 /* add key */
1584 for (x= 0 ; total < 20 * 1024576 ; x++ )
1585 {
1586 unsigned int j= 0;
1587
1588 size= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1589 memset(randomstuff, 0, 6 * 1024);
1590 assert(size < 6 * 1024); /* Being safe here */
1591
1592 for (j= 0 ; j < size ;j++)
1593 randomstuff[j] = (signed char) ((rand() % 26) + 97);
1594
1595 total += size;
1596 sprintf(key, "%d", x);
1597 rc = memcached_set(memc, key, strlen(key),
1598 randomstuff, strlen(randomstuff), 10, 0);
1599 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1600 /* If we fail, lets try again */
1601 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
1602 rc = memcached_set(memc, key, strlen(key),
1603 randomstuff, strlen(randomstuff), 10, 0);
1604 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1605 }
1606
1607 return 0;
1608 }
1609
1610 /* Test case provided by Cal Haldenbrand */
1611 static test_return user_supplied_bug2(memcached_st *memc)
1612 {
1613 int errors;
1614 unsigned int setter;
1615 unsigned int x;
1616 unsigned long long total;
1617
1618 setter= 1;
1619 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1620 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1621 #ifdef NOT_YET
1622 setter = 20 * 1024576;
1623 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1624 setter = 20 * 1024576;
1625 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1626 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1627 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1628
1629 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
1630 #endif
1631
1632 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
1633 {
1634 memcached_return rc= MEMCACHED_SUCCESS;
1635 char buffer[SMALL_STRING_LEN];
1636 uint32_t flags= 0;
1637 size_t val_len= 0;
1638 char *getval;
1639
1640 memset(buffer, 0, SMALL_STRING_LEN);
1641
1642 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
1643 getval= memcached_get(memc, buffer, strlen(buffer),
1644 &val_len, &flags, &rc);
1645 if (rc != MEMCACHED_SUCCESS)
1646 {
1647 if (rc == MEMCACHED_NOTFOUND)
1648 errors++;
1649 else
1650 {
1651 assert(rc);
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 memcached_return pre_replication_noblock(memcached_st *memc)
3074 {
3075 memcached_return rc= MEMCACHED_FAILURE;
3076 if (pre_replication(memc) == MEMCACHED_SUCCESS &&
3077 pre_nonblock(memc) == MEMCACHED_SUCCESS)
3078 rc= MEMCACHED_SUCCESS;
3079
3080 return rc;
3081 }
3082
3083 static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
3084 {
3085 free(mem);
3086 }
3087
3088 static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
3089 {
3090 void *ret= malloc(size);
3091 if (ret != NULL)
3092 memset(ret, 0xff, size);
3093
3094 return ret;
3095 }
3096
3097 static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
3098 {
3099 return realloc(mem, size);
3100 }
3101
3102 static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size)
3103 {
3104 return calloc(nelem, size);
3105 }
3106
3107 static memcached_return set_prefix(memcached_st *memc)
3108 {
3109 memcached_return rc;
3110 const char *key= "mine";
3111 char *value;
3112
3113 /* Make sure be default none exists */
3114 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3115 assert(rc == MEMCACHED_FAILURE);
3116
3117 /* Test a clean set */
3118 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3119 assert(rc == MEMCACHED_SUCCESS);
3120
3121 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3122 assert(memcmp(value, key, 4) == 0);
3123 assert(rc == MEMCACHED_SUCCESS);
3124
3125 /* Test that we can turn it off */
3126 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3127 assert(rc == MEMCACHED_SUCCESS);
3128
3129 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3130 assert(rc == MEMCACHED_FAILURE);
3131
3132 /* Now setup for main test */
3133 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3134 assert(rc == MEMCACHED_SUCCESS);
3135
3136 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3137 assert(rc == MEMCACHED_SUCCESS);
3138 assert(memcmp(value, key, 4) == 0);
3139
3140 /* Set to Zero, and then Set to something too large */
3141 {
3142 char *long_key;
3143 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3144 assert(rc == MEMCACHED_SUCCESS);
3145
3146 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3147 assert(rc == MEMCACHED_FAILURE);
3148 assert(value == NULL);
3149
3150 /* Test a long key for failure */
3151 /* TODO, extend test to determine based on setting, what result should be */
3152 long_key= "Thisismorethentheallottednumberofcharacters";
3153 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3154 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3155 assert(rc == MEMCACHED_SUCCESS);
3156
3157 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3158 long_key= "This is more then the allotted number of characters";
3159 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3160 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3161
3162 /* Test for a bad prefix, but with a short key */
3163 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
3164 assert(rc == MEMCACHED_SUCCESS);
3165
3166 long_key= "dog cat";
3167 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3168 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3169 }
3170
3171 return MEMCACHED_SUCCESS;
3172 }
3173
3174 #ifdef MEMCACHED_ENABLE_DEPRECATED
3175 static memcached_return deprecated_set_memory_alloc(memcached_st *memc)
3176 {
3177 void *test_ptr= NULL;
3178 void *cb_ptr= NULL;
3179 {
3180 memcached_malloc_function malloc_cb=
3181 (memcached_malloc_function)my_malloc;
3182 cb_ptr= *(void **)&malloc_cb;
3183 memcached_return rc;
3184
3185 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
3186 assert(rc == MEMCACHED_SUCCESS);
3187 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
3188 assert(rc == MEMCACHED_SUCCESS);
3189 assert(test_ptr == cb_ptr);
3190 }
3191
3192 {
3193 memcached_realloc_function realloc_cb=
3194 (memcached_realloc_function)my_realloc;
3195 cb_ptr= *(void **)&realloc_cb;
3196 memcached_return rc;
3197
3198 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
3199 assert(rc == MEMCACHED_SUCCESS);
3200 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
3201 assert(rc == MEMCACHED_SUCCESS);
3202 assert(test_ptr == cb_ptr);
3203 }
3204
3205 {
3206 memcached_free_function free_cb=
3207 (memcached_free_function)my_free;
3208 cb_ptr= *(void **)&free_cb;
3209 memcached_return rc;
3210
3211 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
3212 assert(rc == MEMCACHED_SUCCESS);
3213 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
3214 assert(rc == MEMCACHED_SUCCESS);
3215 assert(test_ptr == cb_ptr);
3216 }
3217 return MEMCACHED_SUCCESS;
3218 }
3219 #endif
3220
3221 static memcached_return set_memory_alloc(memcached_st *memc)
3222 {
3223 memcached_return rc;
3224 rc= memcached_set_memory_allocators(memc, NULL, my_free,
3225 my_realloc, my_calloc);
3226 assert(rc == MEMCACHED_FAILURE);
3227
3228 rc= memcached_set_memory_allocators(memc, my_malloc, my_free,
3229 my_realloc, my_calloc);
3230
3231 memcached_malloc_function mem_malloc;
3232 memcached_free_function mem_free;
3233 memcached_realloc_function mem_realloc;
3234 memcached_calloc_function mem_calloc;
3235 memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
3236 &mem_realloc, &mem_calloc);
3237
3238 assert(mem_malloc == my_malloc);
3239 assert(mem_realloc == my_realloc);
3240 assert(mem_calloc == my_calloc);
3241 assert(mem_free == my_free);
3242
3243 return MEMCACHED_SUCCESS;
3244 }
3245
3246 static memcached_return enable_consistent(memcached_st *memc)
3247 {
3248 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
3249 memcached_hash hash;
3250 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
3251 if (pre_hsieh(memc) != MEMCACHED_SUCCESS)
3252 return MEMCACHED_FAILURE;
3253
3254 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
3255 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
3256
3257 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
3258 assert(hash == MEMCACHED_HASH_HSIEH);
3259
3260
3261 return MEMCACHED_SUCCESS;
3262 }
3263
3264 static memcached_return enable_cas(memcached_st *memc)
3265 {
3266 unsigned int set= 1;
3267
3268 memcached_version(memc);
3269
3270 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3271 || memc->hosts[0].minor_version > 2)
3272 {
3273 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
3274
3275 return MEMCACHED_SUCCESS;
3276 }
3277
3278 return MEMCACHED_FAILURE;
3279 }
3280
3281 static memcached_return check_for_1_2_3(memcached_st *memc)
3282 {
3283 memcached_version(memc);
3284
3285 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3286 || memc->hosts[0].minor_version > 2)
3287 return MEMCACHED_SUCCESS;
3288
3289 return MEMCACHED_FAILURE;
3290 }
3291
3292 static memcached_return pre_unix_socket(memcached_st *memc)
3293 {
3294 memcached_return rc;
3295 struct stat buf;
3296
3297 memcached_server_list_free(memc->hosts);
3298 memc->hosts= NULL;
3299 memc->number_of_hosts= 0;
3300
3301 if (stat("/tmp/memcached.socket", &buf))
3302 return MEMCACHED_FAILURE;
3303
3304 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
3305
3306 return rc;
3307 }
3308
3309 static memcached_return pre_nodelay(memcached_st *memc)
3310 {
3311 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3312 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
3313
3314 return MEMCACHED_SUCCESS;
3315 }
3316
3317 static memcached_return pre_settimer(memcached_st *memc)
3318 {
3319 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
3320 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
3321
3322 return MEMCACHED_SUCCESS;
3323 }
3324
3325 static memcached_return poll_timeout(memcached_st *memc)
3326 {
3327 size_t timeout;
3328
3329 timeout= 100;
3330
3331 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
3332
3333 timeout= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
3334
3335 assert(timeout == 100);
3336
3337 return MEMCACHED_SUCCESS;
3338 }
3339
3340 static test_return noreply_test(memcached_st *memc)
3341 {
3342 memcached_return ret;
3343 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
3344 assert(ret == MEMCACHED_SUCCESS);
3345 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3346 assert(ret == MEMCACHED_SUCCESS);
3347 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
3348 assert(ret == MEMCACHED_SUCCESS);
3349 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
3350 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
3351 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
3352
3353 for (int count=0; count < 5; ++count)
3354 {
3355 for (int x=0; x < 100; ++x)
3356 {
3357 char key[10];
3358 size_t len= (size_t)sprintf(key, "%d", x);
3359 switch (count)
3360 {
3361 case 0:
3362 ret=memcached_add(memc, key, len, key, len, 0, 0);
3363 break;
3364 case 1:
3365 ret=memcached_replace(memc, key, len, key, len, 0, 0);
3366 break;
3367 case 2:
3368 ret=memcached_set(memc, key, len, key, len, 0, 0);
3369 break;
3370 case 3:
3371 ret=memcached_append(memc, key, len, key, len, 0, 0);
3372 break;
3373 case 4:
3374 ret=memcached_prepend(memc, key, len, key, len, 0, 0);
3375 break;
3376 default:
3377 assert(count);
3378 break;
3379 }
3380 assert(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
3381 }
3382
3383 /*
3384 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3385 ** API and is _ONLY_ done this way to verify that the library works the
3386 ** way it is supposed to do!!!!
3387 */
3388 int no_msg=0;
3389 for (uint32_t x=0; x < memc->number_of_hosts; ++x)
3390 no_msg+=(int)(memc->hosts[x].cursor_active);
3391
3392 assert(no_msg == 0);
3393 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3394
3395 /*
3396 ** Now validate that all items was set properly!
3397 */
3398 for (int x=0; x < 100; ++x)
3399 {
3400 char key[10];
3401 size_t len= (size_t)sprintf(key, "%d", x);
3402 size_t length;
3403 uint32_t flags;
3404 char* value=memcached_get(memc, key, strlen(key),
3405 &length, &flags, &ret);
3406 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3407 switch (count)
3408 {
3409 case 0: /* FALLTHROUGH */
3410 case 1: /* FALLTHROUGH */
3411 case 2:
3412 assert(strncmp(value, key, len) == 0);
3413 assert(len == length);
3414 break;
3415 case 3:
3416 assert(length == len * 2);
3417 break;
3418 case 4:
3419 assert(length == len * 3);
3420 break;
3421 default:
3422 assert(count);
3423 break;
3424 }
3425 free(value);
3426 }
3427 }
3428
3429 /* Try setting an illegal cas value (should not return an error to
3430 * the caller (because we don't expect a return message from the server)
3431 */
3432 char* keys[]= {"0"};
3433 size_t lengths[]= {1};
3434 size_t length;
3435 uint32_t flags;
3436 memcached_result_st results_obj;
3437 memcached_result_st *results;
3438 ret= memcached_mget(memc, keys, lengths, 1);
3439 assert(ret == MEMCACHED_SUCCESS);
3440
3441 results= memcached_result_create(memc, &results_obj);
3442 assert(results);
3443 results= memcached_fetch_result(memc, &results_obj, &ret);
3444 assert(results);
3445 assert(ret == MEMCACHED_SUCCESS);
3446 uint64_t cas= memcached_result_cas(results);
3447 memcached_result_free(&results_obj);
3448
3449 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3450 assert(ret == MEMCACHED_SUCCESS);
3451
3452 /*
3453 * The item will have a new cas value, so try to set it again with the old
3454 * value. This should fail!
3455 */
3456 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3457 assert(ret == MEMCACHED_SUCCESS);
3458 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3459 char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
3460 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3461 free(value);
3462
3463 return TEST_SUCCESS;
3464 }
3465
3466 static test_return analyzer_test(memcached_st *memc)
3467 {
3468 memcached_return rc;
3469 memcached_stat_st *memc_stat;
3470 memcached_analysis_st *report;
3471
3472 memc_stat= memcached_stat(memc, NULL, &rc);
3473 assert(rc == MEMCACHED_SUCCESS);
3474 assert(memc_stat);
3475
3476 report= memcached_analyze(memc, memc_stat, &rc);
3477 assert(rc == MEMCACHED_SUCCESS);
3478 assert(report);
3479
3480 free(report);
3481 memcached_stat_free(NULL, memc_stat);
3482
3483 return TEST_SUCCESS;
3484 }
3485
3486 /* Count the objects */
3487 static memcached_return callback_dump_counter(memcached_st *ptr __attribute__((unused)),
3488 const char *key __attribute__((unused)),
3489 size_t key_length __attribute__((unused)),
3490 void *context)
3491 {
3492 uint32_t *counter= (uint32_t *)context;
3493
3494 *counter= *counter + 1;
3495
3496 return MEMCACHED_SUCCESS;
3497 }
3498
3499 static test_return dump_test(memcached_st *memc)
3500 {
3501 memcached_return rc;
3502 uint32_t counter= 0;
3503 memcached_dump_func callbacks[1];
3504 test_return main_rc;
3505
3506 callbacks[0]= &callback_dump_counter;
3507
3508 /* No support for Binary protocol yet */
3509 if (memc->flags & MEM_BINARY_PROTOCOL)
3510 return TEST_SUCCESS;
3511
3512 main_rc= set_test3(memc);
3513
3514 assert (main_rc == TEST_SUCCESS);
3515
3516 rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
3517 assert(rc == MEMCACHED_SUCCESS);
3518
3519 /* We may have more then 32 if our previous flush has not completed */
3520 assert(counter >= 32);
3521
3522 return TEST_SUCCESS;
3523 }
3524
3525 #ifdef HAVE_LIBMEMCACHEDUTIL
3526 static void* connection_release(void *arg) {
3527 struct {
3528 memcached_pool_st* pool;
3529 memcached_st* mmc;
3530 } *resource= arg;
3531
3532 usleep(250);
3533 assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
3534 return arg;
3535 }
3536
3537 static test_return connection_pool_test(memcached_st *memc)
3538 {
3539 memcached_pool_st* pool= memcached_pool_create(memc, 5, 10);
3540 assert(pool != NULL);
3541 memcached_st* mmc[10];
3542 memcached_return rc;
3543
3544 for (int x= 0; x < 10; ++x) {
3545 mmc[x]= memcached_pool_pop(pool, false, &rc);
3546 assert(mmc[x] != NULL);
3547 assert(rc == MEMCACHED_SUCCESS);
3548 }
3549
3550 assert(memcached_pool_pop(pool, false, &rc) == NULL);
3551 assert(rc == MEMCACHED_SUCCESS);
3552
3553 pthread_t tid;
3554 struct {
3555 memcached_pool_st* pool;
3556 memcached_st* mmc;
3557 } item= { .pool = pool, .mmc = mmc[9] };
3558 pthread_create(&tid, NULL, connection_release, &item);
3559 mmc[9]= memcached_pool_pop(pool, true, &rc);
3560 assert(rc == MEMCACHED_SUCCESS);
3561 pthread_join(tid, NULL);
3562 assert(mmc[9] == item.mmc);
3563 const char *key= "key";
3564 size_t keylen= strlen(key);
3565
3566 // verify that I can do ops with all connections
3567 rc= memcached_set(mmc[0], key, keylen, "0", 1, 0, 0);
3568 assert(rc == MEMCACHED_SUCCESS);
3569
3570 for (unsigned int x= 0; x < 10; ++x) {
3571 uint64_t number_value;
3572 rc= memcached_increment(mmc[x], key, keylen, 1, &number_value);
3573 assert(rc == MEMCACHED_SUCCESS);
3574 assert(number_value == (x+1));
3575 }
3576
3577 // Release them..
3578 for (int x= 0; x < 10; ++x)
3579 assert(memcached_pool_push(pool, mmc[x]) == MEMCACHED_SUCCESS);
3580
3581 assert(memcached_pool_destroy(pool) == memc);
3582 return TEST_SUCCESS;
3583 }
3584 #endif
3585
3586 static test_return replication_set_test(memcached_st *memc)
3587 {
3588 memcached_return rc;
3589 memcached_st *memc_clone= memcached_clone(NULL, memc);
3590 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
3591
3592 rc= memcached_set(memc, "bubba", 5, "0", 1, 0, 0);
3593 assert(rc == MEMCACHED_SUCCESS);
3594
3595 /*
3596 ** We are using the quiet commands to store the replicas, so we need
3597 ** to ensure that all of them are processed before we can continue.
3598 ** In the test we go directly from storing the object to trying to
3599 ** receive the object from all of the different servers, so we
3600 ** could end up in a race condition (the memcached server hasn't yet
3601 ** processed the quiet command from the replication set when it process
3602 ** the request from the other client (created by the clone)). As a
3603 ** workaround for that we call memcached_quit to send the quit command
3604 ** to the server and wait for the response ;-) If you use the test code
3605 ** as an example for your own code, please note that you shouldn't need
3606 ** to do this ;-)
3607 */
3608 memcached_quit(memc);
3609
3610 /*
3611 ** "bubba" should now be stored on all of our servers. We don't have an
3612 ** easy to use API to address each individual server, so I'll just iterate
3613 ** through a bunch of "master keys" and I should most likely hit all of the
3614 ** servers...
3615 */
3616 for (int x= 'a'; x <= 'z'; ++x)
3617 {
3618 char key[2]= { [0]= (char)x };
3619 size_t len;
3620 uint32_t flags;
3621 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
3622 &len, &flags, &rc);
3623 assert(rc == MEMCACHED_SUCCESS);
3624 assert(val != NULL);
3625 free(val);
3626 }
3627
3628 memcached_free(memc_clone);
3629
3630 return TEST_SUCCESS;
3631 }
3632
3633 static test_return replication_get_test(memcached_st *memc)
3634 {
3635 memcached_return rc;
3636
3637 /*
3638 * Don't do the following in your code. I am abusing the internal details
3639 * within the library, and this is not a supported interface.
3640 * This is to verify correct behavior in the library
3641 */
3642 for (uint32_t host= 0; host < memc->number_of_hosts; ++host)
3643 {
3644 memcached_st *memc_clone= memcached_clone(NULL, memc);
3645 memc_clone->hosts[host].port= 0;
3646
3647 for (int x= 'a'; x <= 'z'; ++x)
3648 {
3649 char key[2]= { [0]= (char)x };
3650 size_t len;
3651 uint32_t flags;
3652 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
3653 &len, &flags, &rc);
3654 assert(rc == MEMCACHED_SUCCESS);
3655 assert(val != NULL);
3656 free(val);
3657 }
3658
3659 memcached_free(memc_clone);
3660 }
3661
3662 return TEST_SUCCESS;
3663 }
3664
3665 static test_return replication_mget_test(memcached_st *memc)
3666 {
3667 memcached_return rc;
3668 memcached_st *memc_clone= memcached_clone(NULL, memc);
3669 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
3670
3671 char *keys[]= { "bubba", "key1", "key2", "key3" };
3672 size_t len[]= { 5, 4, 4, 4 };
3673
3674 for (int x=0; x< 4; ++x)
3675 {
3676 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
3677 assert(rc == MEMCACHED_SUCCESS);
3678 }
3679
3680 /*
3681 ** We are using the quiet commands to store the replicas, so we need
3682 ** to ensure that all of them are processed before we can continue.
3683 ** In the test we go directly from storing the object to trying to
3684 ** receive the object from all of the different servers, so we
3685 ** could end up in a race condition (the memcached server hasn't yet
3686 ** processed the quiet command from the replication set when it process
3687 ** the request from the other client (created by the clone)). As a
3688 ** workaround for that we call memcached_quit to send the quit command
3689 ** to the server and wait for the response ;-) If you use the test code
3690 ** as an example for your own code, please note that you shouldn't need
3691 ** to do this ;-)
3692 */
3693 memcached_quit(memc);
3694
3695 /*
3696 * Don't do the following in your code. I am abusing the internal details
3697 * within the library, and this is not a supported interface.
3698 * This is to verify correct behavior in the library
3699 */
3700 memcached_result_st result_obj;
3701 for (uint32_t host= 0; host < memc_clone->number_of_hosts; host++)
3702 {
3703 memcached_st *new_clone= memcached_clone(NULL, memc);
3704 new_clone->hosts[host].port= 0;
3705
3706 for (int x= 'a'; x <= 'z'; ++x)
3707 {
3708 char key[2]= { [0]= (char)x };
3709
3710 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
3711 assert(rc == MEMCACHED_SUCCESS);
3712
3713 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
3714 assert(results);
3715
3716 int hits= 0;
3717 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
3718 {
3719 hits++;
3720 }
3721 assert(hits == 4);
3722 memcached_result_free(&result_obj);
3723 }
3724
3725 memcached_free(new_clone);
3726 }
3727
3728 return TEST_SUCCESS;
3729 }
3730
3731 static test_return replication_delete_test(memcached_st *memc)
3732 {
3733 memcached_return rc;
3734 memcached_st *memc_clone= memcached_clone(NULL, memc);
3735 /* Delete the items from all of the servers except 1 */
3736 uint64_t repl= memcached_behavior_get(memc,
3737 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
3738 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, --repl);
3739
3740 char *keys[]= { "bubba", "key1", "key2", "key3" };
3741 size_t len[]= { 5, 4, 4, 4 };
3742
3743 for (int x=0; x< 4; ++x)
3744 {
3745 rc= memcached_delete_by_key(memc, keys[0], len[0], keys[x], len[x], 0);
3746 assert(rc == MEMCACHED_SUCCESS);
3747 }
3748
3749 /*
3750 * Don't do the following in your code. I am abusing the internal details
3751 * within the library, and this is not a supported interface.
3752 * This is to verify correct behavior in the library
3753 */
3754 uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
3755 for (uint32_t x= 0; x < (repl + 1); ++x)
3756 {
3757 memc_clone->hosts[hash].port= 0;
3758 if (++hash == memc_clone->number_of_hosts)
3759 hash= 0;
3760 }
3761
3762 memcached_result_st result_obj;
3763 for (uint32_t host= 0; host < memc_clone->number_of_hosts; ++host)
3764 {
3765 for (int x= 'a'; x <= 'z'; ++x)
3766 {
3767 char key[2]= { [0]= (char)x };
3768
3769 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 4);
3770 assert(rc == MEMCACHED_SUCCESS);
3771
3772 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
3773 assert(results);
3774
3775 int hits= 0;
3776 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
3777 {
3778 ++hits;
3779 }
3780 assert(hits == 4);
3781 memcached_result_free(&result_obj);
3782 }
3783 }
3784 memcached_free(memc_clone);
3785
3786 return TEST_SUCCESS;
3787 }
3788
3789 static void increment_request_id(uint16_t *id)
3790 {
3791 (*id)++;
3792 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
3793 *id= 0;
3794 }
3795
3796 static uint16_t *get_udp_request_ids(memcached_st *memc)
3797 {
3798 uint16_t *ids= malloc(sizeof(uint16_t) * memc->number_of_hosts);
3799 assert(ids != NULL);
3800 unsigned int x;
3801
3802 for (x= 0; x < memc->number_of_hosts; x++)
3803 ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) memc->hosts[x].write_buffer);
3804
3805 return ids;
3806 }
3807
3808 static test_return post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
3809 {
3810 unsigned int x;
3811 memcached_server_st *cur_server = memc->hosts;
3812 uint16_t *cur_req_ids = get_udp_request_ids(memc);
3813
3814 for (x= 0; x < memc->number_of_hosts; x++)
3815 {
3816 assert(cur_server[x].cursor_active == 0);
3817 assert(cur_req_ids[x] == expected_req_ids[x]);
3818 }
3819 free(expected_req_ids);
3820 free(cur_req_ids);
3821
3822 return TEST_SUCCESS;
3823 }
3824
3825 /*
3826 ** There is a little bit of a hack here, instead of removing
3827 ** the servers, I just set num host to 0 and them add then new udp servers
3828 **/
3829 static memcached_return init_udp(memcached_st *memc)
3830 {
3831 memcached_version(memc);
3832 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3833 if (memc->hosts[0].major_version != 1 || memc->hosts[0].minor_version != 2
3834 || memc->hosts[0].micro_version < 6)
3835 return MEMCACHED_FAILURE;
3836
3837 uint32_t num_hosts= memc->number_of_hosts;
3838 unsigned int x= 0;
3839 memcached_server_st servers[num_hosts];
3840 memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts);
3841 for (x= 0; x < num_hosts; x++)
3842 memcached_server_free(&memc->hosts[x]);
3843
3844 memc->number_of_hosts= 0;
3845 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
3846 for (x= 0; x < num_hosts; x++)
3847 {
3848 assert(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
3849 assert(memc->hosts[x].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3850 }
3851
3852 return MEMCACHED_SUCCESS;
3853 }
3854
3855 static memcached_return binary_init_udp(memcached_st *memc)
3856 {
3857 pre_binary(memc);
3858 return init_udp(memc);
3859 }
3860
3861 /* Make sure that I cant add a tcp server to a udp client */
3862 static test_return add_tcp_server_udp_client_test(memcached_st *memc)
3863 {
3864 memcached_server_st server;
3865 memcached_server_clone(&server, &memc->hosts[0]);
3866 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3867 assert(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3868 return TEST_SUCCESS;
3869 }
3870
3871 /* Make sure that I cant add a udp server to a tcp client */
3872 static test_return add_udp_server_tcp_client_test(memcached_st *memc)
3873 {
3874 memcached_server_st server;
3875 memcached_server_clone(&server, &memc->hosts[0]);
3876 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3877
3878 memcached_st tcp_client;
3879 memcached_create(&tcp_client);
3880 assert(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3881 return TEST_SUCCESS;
3882 }
3883
3884 static test_return set_udp_behavior_test(memcached_st *memc)
3885 {
3886
3887 memcached_quit(memc);
3888 memc->number_of_hosts= 0;
3889 run_distribution(memc);
3890 assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1) == MEMCACHED_SUCCESS);
3891 assert(memc->flags & MEM_USE_UDP);
3892 assert(memc->flags & MEM_NOREPLY);;
3893
3894 assert(memc->number_of_hosts == 0);
3895
3896 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0);
3897 assert(!(memc->flags & MEM_USE_UDP));
3898 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY,0);
3899 assert(!(memc->flags & MEM_NOREPLY));
3900 return TEST_SUCCESS;
3901 }
3902
3903 static test_return udp_set_test(memcached_st *memc)
3904 {
3905 unsigned int x= 0;
3906 unsigned int num_iters= 1025; //request id rolls over at 1024
3907 for (x= 0; x < num_iters;x++)
3908 {
3909 memcached_return rc;
3910 char *key= "foo";
3911 char *value= "when we sanitize";
3912 uint16_t *expected_ids= get_udp_request_ids(memc);
3913 unsigned int server_key= memcached_generate_hash(memc,key,strlen(key));
3914 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3915 rc= memcached_set(memc, key, strlen(key),
3916 value, strlen(value),
3917 (time_t)0, (uint32_t)0);
3918 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3919 /** NB, the check below assumes that if new write_ptr is less than
3920 * the original write_ptr that we have flushed. For large payloads, this
3921 * maybe an invalid assumption, but for the small payload we have it is OK
3922 */
3923 if (rc == MEMCACHED_SUCCESS ||
3924 memc->hosts[server_key].write_buffer_offset < init_offset)
3925 increment_request_id(&expected_ids[server_key]);
3926
3927 if (rc == MEMCACHED_SUCCESS)
3928 {
3929 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3930 }
3931 else
3932 {
3933 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
3934 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
3935 }
3936 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
3937 }
3938 return TEST_SUCCESS;
3939 }
3940
3941 static test_return udp_buffered_set_test(memcached_st *memc)
3942 {
3943 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3944 return udp_set_test(memc);
3945 }
3946
3947 static test_return udp_set_too_big_test(memcached_st *memc)
3948 {
3949 memcached_return rc;
3950 char *key= "bar";
3951 char value[MAX_UDP_DATAGRAM_LENGTH];
3952 uint16_t *expected_ids= get_udp_request_ids(memc);
3953 rc= memcached_set(memc, key, strlen(key),
3954 value, MAX_UDP_DATAGRAM_LENGTH,
3955 (time_t)0, (uint32_t)0);
3956 assert(rc == MEMCACHED_WRITE_FAILURE);
3957 return post_udp_op_check(memc,expected_ids);
3958 }
3959
3960 static test_return udp_delete_test(memcached_st *memc)
3961 {
3962 unsigned int x= 0;
3963 unsigned int num_iters= 1025; //request id rolls over at 1024
3964 for (x= 0; x < num_iters;x++)
3965 {
3966 memcached_return rc;
3967 char *key= "foo";
3968 uint16_t *expected_ids=get_udp_request_ids(memc);
3969 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
3970 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3971 rc= memcached_delete(memc, key, strlen(key), 0);
3972 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3973 if (rc == MEMCACHED_SUCCESS || memc->hosts[server_key].write_buffer_offset < init_offset)
3974 increment_request_id(&expected_ids[server_key]);
3975 if (rc == MEMCACHED_SUCCESS)
3976 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3977 else
3978 {
3979 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
3980 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
3981 }
3982 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
3983 }
3984 return TEST_SUCCESS;
3985 }
3986
3987 static test_return udp_buffered_delete_test(memcached_st *memc)
3988 {
3989 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3990 return udp_delete_test(memc);
3991 }
3992
3993 static test_return udp_verbosity_test(memcached_st *memc)
3994 {
3995 memcached_return rc;
3996 uint16_t *expected_ids= get_udp_request_ids(memc);
3997 unsigned int x;
3998 for (x= 0; x < memc->number_of_hosts;x++)
3999 increment_request_id(&expected_ids[x]);
4000
4001 rc= memcached_verbosity(memc,3);
4002 assert(rc == MEMCACHED_SUCCESS);
4003 return post_udp_op_check(memc,expected_ids);
4004 }
4005
4006 static test_return udp_quit_test(memcached_st *memc)
4007 {
4008 uint16_t *expected_ids= get_udp_request_ids(memc);
4009 memcached_quit(memc);
4010 return post_udp_op_check(memc, expected_ids);
4011 }
4012
4013 static test_return udp_flush_test(memcached_st *memc)
4014 {
4015 memcached_return rc;
4016 uint16_t *expected_ids= get_udp_request_ids(memc);
4017 unsigned int x;
4018 for (x= 0; x < memc->number_of_hosts;x++)
4019 increment_request_id(&expected_ids[x]);
4020
4021 rc= memcached_flush(memc,0);
4022 assert(rc == MEMCACHED_SUCCESS);
4023 return post_udp_op_check(memc,expected_ids);
4024 }
4025
4026 static test_return udp_incr_test(memcached_st *memc)
4027 {
4028 memcached_return rc;
4029 char *key= "incr";
4030 char *value= "1";
4031 rc= memcached_set(memc, key, strlen(key),
4032 value, strlen(value),
4033 (time_t)0, (uint32_t)0);
4034
4035 assert(rc == MEMCACHED_SUCCESS);
4036 uint16_t *expected_ids= get_udp_request_ids(memc);
4037 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
4038 increment_request_id(&expected_ids[server_key]);
4039 uint64_t newvalue;
4040 rc= memcached_increment(memc, key, strlen(key), 1, &newvalue);
4041 assert(rc == MEMCACHED_SUCCESS);
4042 return post_udp_op_check(memc, expected_ids);
4043 }
4044
4045 static test_return udp_decr_test(memcached_st *memc)
4046 {
4047 memcached_return rc;
4048 char *key= "decr";
4049 char *value= "1";
4050 rc= memcached_set(memc, key, strlen(key),
4051 value, strlen(value),
4052 (time_t)0, (uint32_t)0);
4053
4054 assert(rc == MEMCACHED_SUCCESS);
4055 uint16_t *expected_ids= get_udp_request_ids(memc);
4056 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
4057 increment_request_id(&expected_ids[server_key]);
4058 uint64_t newvalue;
4059 rc= memcached_decrement(memc, key, strlen(key), 1, &newvalue);
4060 assert(rc == MEMCACHED_SUCCESS);
4061 return post_udp_op_check(memc, expected_ids);
4062 }
4063
4064
4065 static test_return udp_stat_test(memcached_st *memc)
4066 {
4067 memcached_stat_st * rv= NULL;
4068 memcached_return rc;
4069 char args[]= "";
4070 uint16_t *expected_ids = get_udp_request_ids(memc);
4071 rv = memcached_stat(memc, args, &rc);
4072 free(rv);
4073 assert(rc == MEMCACHED_NOT_SUPPORTED);
4074 return post_udp_op_check(memc, expected_ids);
4075 }
4076
4077 static test_return udp_version_test(memcached_st *memc)
4078 {
4079 memcached_return rc;
4080 uint16_t *expected_ids = get_udp_request_ids(memc);
4081 rc = memcached_version(memc);
4082 assert(rc == MEMCACHED_NOT_SUPPORTED);
4083 return post_udp_op_check(memc, expected_ids);
4084 }
4085
4086 static test_return udp_get_test(memcached_st *memc)
4087 {
4088 memcached_return rc;
4089 char *key= "foo";
4090 size_t vlen;
4091 uint16_t *expected_ids = get_udp_request_ids(memc);
4092 char *val= memcached_get(memc, key, strlen(key), &vlen, (uint32_t)0, &rc);
4093 assert(rc == MEMCACHED_NOT_SUPPORTED);
4094 assert(val == NULL);
4095 return post_udp_op_check(memc, expected_ids);
4096 }
4097
4098 static test_return udp_mixed_io_test(memcached_st *memc)
4099 {
4100 test_st current_op;
4101 test_st mixed_io_ops [] ={
4102 {"udp_set_test", 0, udp_set_test},
4103 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4104 {"udp_delete_test", 0, udp_delete_test},
4105 {"udp_verbosity_test", 0, udp_verbosity_test},
4106 {"udp_quit_test", 0, udp_quit_test},
4107 {"udp_flush_test", 0, udp_flush_test},
4108 {"udp_incr_test", 0, udp_incr_test},
4109 {"udp_decr_test", 0, udp_decr_test},
4110 {"udp_version_test", 0, udp_version_test}
4111 };
4112 unsigned int x= 0;
4113 for (x= 0; x < 500; x++)
4114 {
4115 current_op= mixed_io_ops[random() % 9];
4116 assert(current_op.function(memc) == TEST_SUCCESS);
4117 }
4118 return TEST_SUCCESS;
4119 }
4120
4121 static test_return hsieh_avaibility_test (memcached_st *memc)
4122 {
4123 memcached_return expected_rc= MEMCACHED_FAILURE;
4124 #ifdef HAVE_HSIEH_HASH
4125 expected_rc= MEMCACHED_SUCCESS;
4126 #endif
4127 memcached_return rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
4128 (uint64_t)MEMCACHED_HASH_HSIEH);
4129 assert(rc == expected_rc);
4130 return TEST_SUCCESS;
4131 }
4132
4133 static char *list[]=
4134 {
4135 "apple",
4136 "beat",
4137 "carrot",
4138 "daikon",
4139 "eggplant",
4140 "flower",
4141 "green",
4142 "hide",
4143 "ick",
4144 "jack",
4145 "kick",
4146 "lime",
4147 "mushrooms",
4148 "nectarine",
4149 "orange",
4150 "peach",
4151 "quant",
4152 "ripen",
4153 "strawberry",
4154 "tang",
4155 "up",
4156 "volumne",
4157 "when",
4158 "yellow",
4159 "zip",
4160 NULL
4161 };
4162
4163 static test_return md5_run (memcached_st *memc __attribute__((unused)))
4164 {
4165 uint32_t x;
4166 char **ptr;
4167 uint32_t values[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4168 245758794U, 2550894432U, 121710495U, 3053817768U,
4169 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4170 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4171 2138080750U, 217675895U, 2230934345U, 1234361223U,
4172 3968582726U, 2455685270U, 1293568479U, 199067604U,
4173 2042482093U };
4174
4175
4176 for (ptr= list, x= 0; *ptr; ptr++, x++)
4177 {
4178 uint32_t hash_val;
4179
4180 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MD5);
4181 assert(values[x] == hash_val);
4182 }
4183
4184 return TEST_SUCCESS;
4185 }
4186
4187 static test_return crc_run (memcached_st *memc __attribute__((unused)))
4188 {
4189 uint32_t x;
4190 char **ptr;
4191 uint32_t values[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4192 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4193 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4194 17300U, 28156U, 9192U, 20484U, 16925U };
4195
4196 for (ptr= list, x= 0; *ptr; ptr++, x++)
4197 {
4198 uint32_t hash_val;
4199
4200 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_CRC);
4201 assert(values[x] == hash_val);
4202 }
4203
4204 return TEST_SUCCESS;
4205 }
4206
4207 static test_return fnv1_64_run (memcached_st *memc __attribute__((unused)))
4208 {
4209 uint32_t x;
4210 char **ptr;
4211 uint32_t values[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4212 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4213 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4214 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4215 2815549194U, 2562818319U, 224996066U, 2680194749U,
4216 3035305390U, 246890365U, 2395624193U, 4145193337U,
4217 1801941682U };
4218
4219 for (ptr= list, x= 0; *ptr; ptr++, x++)
4220 {
4221 uint32_t hash_val;
4222
4223 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4224 assert(values[x] == hash_val);
4225 }
4226
4227 return TEST_SUCCESS;
4228 }
4229
4230 static test_return fnv1a_64_run (memcached_st *memc __attribute__((unused)))
4231 {
4232 uint32_t x;
4233 char **ptr;
4234 uint32_t values[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4235 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4236 734810122U, 1516407546U, 560948863U, 1767346780U,
4237 561034892U, 4156330026U, 3716417003U, 3475297030U,
4238 1518272172U, 227211583U, 3938128828U, 126112909U,
4239 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4240 2272238452U };
4241
4242 for (ptr= list, x= 0; *ptr; ptr++, x++)
4243 {
4244 uint32_t hash_val;
4245
4246 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_64);
4247 assert(values[x] == hash_val);
4248 }
4249
4250 return TEST_SUCCESS;
4251 }
4252
4253 static test_return fnv1_32_run (memcached_st *memc __attribute__((unused)))
4254 {
4255 uint32_t x;
4256 char **ptr;
4257 uint32_t values[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4258 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4259 344481048U, 3865235296U, 2181839183U, 119581266U,
4260 510234242U, 4248244304U, 1362796839U, 103389328U,
4261 1449620010U, 182962511U, 3554262370U, 3206747549U,
4262 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4263 1180552018U };
4264
4265
4266 for (ptr= list, x= 0; *ptr; ptr++, x++)
4267 {
4268 uint32_t hash_val;
4269
4270 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_32);
4271 assert(values[x] == hash_val);
4272 }
4273
4274 return TEST_SUCCESS;
4275 }
4276
4277 static test_return fnv1a_32_run (memcached_st *memc __attribute__((unused)))
4278 {
4279 uint32_t x;
4280 char **ptr;
4281 uint32_t values[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4282 459261019U, 3521085446U, 18738364U, 1625305005U,
4283 2162232970U, 777243802U, 3323728671U, 132336572U,
4284 3654473228U, 260679466U, 1169454059U, 2698319462U,
4285 1062177260U, 235516991U, 2218399068U, 405302637U,
4286 1128467232U, 3579622413U, 2138539289U, 96429129U,
4287 2877453236U };
4288
4289 for (ptr= list, x= 0; *ptr; ptr++, x++)
4290 {
4291 uint32_t hash_val;
4292
4293 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_32);
4294 assert(values[x] == hash_val);
4295 }
4296
4297 return TEST_SUCCESS;
4298 }
4299
4300 static test_return hsieh_run (memcached_st *memc __attribute__((unused)))
4301 {
4302 uint32_t x;
4303 char **ptr;
4304 #ifdef HAVE_HSIEH_HASH
4305 uint32_t values[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4306 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4307 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4308 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4309 3395453351 };
4310 #else
4311 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 };
4312 #endif
4313
4314 for (ptr= list, x= 0; *ptr; ptr++, x++)
4315 {
4316 uint32_t hash_val;
4317
4318 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_HSIEH);
4319 assert(values[x] == hash_val);
4320 }
4321
4322 return TEST_SUCCESS;
4323 }
4324
4325 static test_return murmur_run (memcached_st *memc __attribute__((unused)))
4326 {
4327 uint32_t x;
4328 char **ptr;
4329 uint32_t values[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4330 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4331 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4332 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4333 2815549194U, 2562818319U, 224996066U, 2680194749U,
4334 3035305390U, 246890365U, 2395624193U, 4145193337U,
4335 1801941682U };
4336
4337 for (ptr= list, x= 0; *ptr; ptr++, x++)
4338 {
4339 uint32_t hash_val;
4340
4341 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4342 assert(values[x] == hash_val);
4343 }
4344
4345 return TEST_SUCCESS;
4346 }
4347
4348 static test_return jenkins_run (memcached_st *memc __attribute__((unused)))
4349 {
4350 uint32_t x;
4351 char **ptr;
4352 uint32_t values[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4353 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4354 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4355 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4356 4153263658U, 3824871822U, 3072759809U, 798622255U,
4357 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4358 2629893356U };
4359
4360
4361 for (ptr= list, x= 0; *ptr; ptr++, x++)
4362 {
4363 uint32_t hash_val;
4364
4365 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_JENKINS);
4366 assert(values[x] == hash_val);
4367 }
4368
4369 return TEST_SUCCESS;
4370 }
4371
4372 test_st udp_setup_server_tests[] ={
4373 {"set_udp_behavior_test", 0, set_udp_behavior_test},
4374 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test},
4375 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test},
4376 {0, 0, 0}
4377 };
4378
4379 test_st upd_io_tests[] ={
4380 {"udp_set_test", 0, udp_set_test},
4381 {"udp_buffered_set_test", 0, udp_buffered_set_test},
4382 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4383 {"udp_delete_test", 0, udp_delete_test},
4384 {"udp_buffered_delete_test", 0, udp_buffered_delete_test},
4385 {"udp_verbosity_test", 0, udp_verbosity_test},
4386 {"udp_quit_test", 0, udp_quit_test},
4387 {"udp_flush_test", 0, udp_flush_test},
4388 {"udp_incr_test", 0, udp_incr_test},
4389 {"udp_decr_test", 0, udp_decr_test},
4390 {"udp_stat_test", 0, udp_stat_test},
4391 {"udp_version_test", 0, udp_version_test},
4392 {"udp_get_test", 0, udp_get_test},
4393 {"udp_mixed_io_test", 0, udp_mixed_io_test},
4394 {0, 0, 0}
4395 };
4396
4397 /* Clean the server before beginning testing */
4398 test_st tests[] ={
4399 {"flush", 0, flush_test },
4400 {"init", 0, init_test },
4401 {"allocation", 0, allocation_test },
4402 {"server_list_null_test", 0, server_list_null_test},
4403 {"server_unsort", 0, server_unsort_test},
4404 {"server_sort", 0, server_sort_test},
4405 {"server_sort2", 0, server_sort2_test},
4406 {"clone_test", 0, clone_test },
4407 {"connection_test", 0, connection_test},
4408 {"callback_test", 0, callback_test},
4409 {"behavior_test", 0, behavior_test},
4410 {"userdata_test", 0, userdata_test},
4411 {"error", 0, error_test },
4412 {"set", 0, set_test },
4413 {"set2", 0, set_test2 },
4414 {"set3", 0, set_test3 },
4415 {"dump", 1, dump_test},
4416 {"add", 1, add_test },
4417 {"replace", 1, replace_test },
4418 {"delete", 1, delete_test },
4419 {"get", 1, get_test },
4420 {"get2", 0, get_test2 },
4421 {"get3", 0, get_test3 },
4422 {"get4", 0, get_test4 },
4423 {"partial mget", 0, get_test5 },
4424 {"stats_servername", 0, stats_servername_test },
4425 {"increment", 0, increment_test },
4426 {"increment_with_initial", 1, increment_with_initial_test },
4427 {"decrement", 0, decrement_test },
4428 {"decrement_with_initial", 1, decrement_with_initial_test },
4429 {"quit", 0, quit_test },
4430 {"mget", 1, mget_test },
4431 {"mget_result", 1, mget_result_test },
4432 {"mget_result_alloc", 1, mget_result_alloc_test },
4433 {"mget_result_function", 1, mget_result_function },
4434 {"get_stats", 0, get_stats },
4435 {"add_host_test", 0, add_host_test },
4436 {"add_host_test_1", 0, add_host_test1 },
4437 {"get_stats_keys", 0, get_stats_keys },
4438 {"behavior_test", 0, get_stats_keys },
4439 {"callback_test", 0, get_stats_keys },
4440 {"version_string_test", 0, version_string_test},
4441 {"bad_key", 1, bad_key_test },
4442 {"memcached_server_cursor", 1, memcached_server_cursor_test },
4443 {"read_through", 1, read_through },
4444 {"delete_through", 1, delete_through },
4445 {"noreply", 1, noreply_test},
4446 {"analyzer", 1, analyzer_test},
4447 #ifdef HAVE_LIBMEMCACHEDUTIL
4448 {"connectionpool", 1, connection_pool_test },
4449 #endif
4450 {0, 0, 0}
4451 };
4452
4453 test_st async_tests[] ={
4454 {"add", 1, add_wrapper },
4455 {0, 0, 0}
4456 };
4457
4458 test_st string_tests[] ={
4459 {"string static with null", 0, string_static_null },
4460 {"string alloc with null", 0, string_alloc_null },
4461 {"string alloc with 1K", 0, string_alloc_with_size },
4462 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
4463 {"string append", 0, string_alloc_append },
4464 {"string append failure (too big)", 0, string_alloc_append_toobig },
4465 {0, 0, 0}
4466 };
4467
4468 test_st result_tests[] ={
4469 {"result static", 0, result_static},
4470 {"result alloc", 0, result_alloc},
4471 {0, 0, 0}
4472 };
4473
4474 test_st version_1_2_3[] ={
4475 {"append", 0, append_test },
4476 {"prepend", 0, prepend_test },
4477 {"cas", 0, cas_test },
4478 {"cas2", 0, cas2_test },
4479 {"append_binary", 0, append_binary_test },
4480 {0, 0, 0}
4481 };
4482
4483 test_st user_tests[] ={
4484 {"user_supplied_bug1", 0, user_supplied_bug1 },
4485 {"user_supplied_bug2", 0, user_supplied_bug2 },
4486 {"user_supplied_bug3", 0, user_supplied_bug3 },
4487 {"user_supplied_bug4", 0, user_supplied_bug4 },
4488 {"user_supplied_bug5", 1, user_supplied_bug5 },
4489 {"user_supplied_bug6", 1, user_supplied_bug6 },
4490 {"user_supplied_bug7", 1, user_supplied_bug7 },
4491 {"user_supplied_bug8", 1, user_supplied_bug8 },
4492 {"user_supplied_bug9", 1, user_supplied_bug9 },
4493 {"user_supplied_bug10", 1, user_supplied_bug10 },
4494 {"user_supplied_bug11", 1, user_supplied_bug11 },
4495 {"user_supplied_bug12", 1, user_supplied_bug12 },
4496 {"user_supplied_bug13", 1, user_supplied_bug13 },
4497 {"user_supplied_bug14", 1, user_supplied_bug14 },
4498 {"user_supplied_bug15", 1, user_supplied_bug15 },
4499 {"user_supplied_bug16", 1, user_supplied_bug16 },
4500 #ifndef __sun
4501 /*
4502 ** It seems to be something weird with the character sets..
4503 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4504 ** guess I need to find out how this is supposed to work.. Perhaps I need
4505 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4506 ** so just disable the code for now...).
4507 */
4508 {"user_supplied_bug17", 1, user_supplied_bug17 },
4509 #endif
4510 {"user_supplied_bug18", 1, user_supplied_bug18 },
4511 {"user_supplied_bug19", 1, user_supplied_bug19 },
4512 {"user_supplied_bug20", 1, user_supplied_bug20 },
4513 {0, 0, 0}
4514 };
4515
4516 test_st replication_tests[]= {
4517 {"set", 1, replication_set_test },
4518 {"get", 0, replication_get_test },
4519 {"mget", 0, replication_mget_test },
4520 {"delete", 0, replication_delete_test },
4521 {0, 0, 0}
4522 };
4523
4524 test_st generate_tests[] ={
4525 {"generate_pairs", 1, generate_pairs },
4526 {"generate_data", 1, generate_data },
4527 {"get_read", 0, get_read },
4528 {"delete_generate", 0, delete_generate },
4529 {"generate_buffer_data", 1, generate_buffer_data },
4530 {"delete_buffer", 0, delete_buffer_generate},
4531 {"generate_data", 1, generate_data },
4532 {"mget_read", 0, mget_read },
4533 {"mget_read_result", 0, mget_read_result },
4534 {"mget_read_function", 0, mget_read_function },
4535 {"cleanup", 1, cleanup_pairs },
4536 {"generate_large_pairs", 1, generate_large_pairs },
4537 {"generate_data", 1, generate_data },
4538 {"generate_buffer_data", 1, generate_buffer_data },
4539 {"cleanup", 1, cleanup_pairs },
4540 {0, 0, 0}
4541 };
4542
4543 test_st consistent_tests[] ={
4544 {"generate_pairs", 1, generate_pairs },
4545 {"generate_data", 1, generate_data },
4546 {"get_read", 0, get_read_count },
4547 {"cleanup", 1, cleanup_pairs },
4548 {0, 0, 0}
4549 };
4550
4551 test_st consistent_weighted_tests[] ={
4552 {"generate_pairs", 1, generate_pairs },
4553 {"generate_data", 1, generate_data_with_stats },
4554 {"get_read", 0, get_read_count },
4555 {"cleanup", 1, cleanup_pairs },
4556 {0, 0, 0}
4557 };
4558
4559 test_st hsieh_availability[] ={
4560 {"hsieh_avaibility_test",0,hsieh_avaibility_test},
4561 {0, 0, 0}
4562 };
4563
4564 test_st ketama_auto_eject_hosts[] ={
4565 {"auto_eject_hosts", 1, auto_eject_hosts },
4566 {0, 0, 0}
4567 };
4568
4569 test_st hash_tests[] ={
4570 {"md5", 0, md5_run },
4571 {"crc", 0, crc_run },
4572 {"fnv1_64", 0, fnv1_64_run },
4573 {"fnv1a_64", 0, fnv1a_64_run },
4574 {"fnv1_32", 0, fnv1_32_run },
4575 {"fnv1a_32", 0, fnv1a_32_run },
4576 {"hsieh", 0, hsieh_run },
4577 {"murmur", 0, murmur_run },
4578 {"jenkis", 0, jenkins_run },
4579 {0, 0, 0}
4580 };
4581
4582 collection_st collection[] ={
4583 {"hsieh_availability",0,0,hsieh_availability},
4584 {"udp_setup", init_udp, 0, udp_setup_server_tests},
4585 {"udp_io", init_udp, 0, upd_io_tests},
4586 {"udp_binary_io", binary_init_udp, 0, upd_io_tests},
4587 {"block", 0, 0, tests},
4588 {"binary", pre_binary, 0, tests},
4589 {"nonblock", pre_nonblock, 0, tests},
4590 {"nodelay", pre_nodelay, 0, tests},
4591 {"settimer", pre_settimer, 0, tests},
4592 {"md5", pre_md5, 0, tests},
4593 {"crc", pre_crc, 0, tests},
4594 {"hsieh", pre_hsieh, 0, tests},
4595 {"jenkins", pre_jenkins, 0, tests},
4596 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
4597 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
4598 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
4599 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
4600 {"ketama", pre_behavior_ketama, 0, tests},
4601 {"ketama_auto_eject_hosts", pre_behavior_ketama, 0, ketama_auto_eject_hosts},
4602 {"unix_socket", pre_unix_socket, 0, tests},
4603 {"unix_socket_nodelay", pre_nodelay, 0, tests},
4604 {"poll_timeout", poll_timeout, 0, tests},
4605 {"gets", enable_cas, 0, tests},
4606 {"consistent", enable_consistent, 0, tests},
4607 #ifdef MEMCACHED_ENABLE_DEPRECATED
4608 {"deprecated_memory_allocators", deprecated_set_memory_alloc, 0, tests},
4609 #endif
4610 {"memory_allocators", set_memory_alloc, 0, tests},
4611 {"prefix", set_prefix, 0, tests},
4612 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
4613 {"string", 0, 0, string_tests},
4614 {"result", 0, 0, result_tests},
4615 {"async", pre_nonblock, 0, async_tests},
4616 {"async_binary", pre_nonblock_binary, 0, async_tests},
4617 {"user", 0, 0, user_tests},
4618 {"generate", 0, 0, generate_tests},
4619 {"generate_hsieh", pre_hsieh, 0, generate_tests},
4620 {"generate_ketama", pre_behavior_ketama, 0, generate_tests},
4621 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
4622 {"generate_md5", pre_md5, 0, generate_tests},
4623 {"generate_murmur", pre_murmur, 0, generate_tests},
4624 {"generate_jenkins", pre_jenkins, 0, generate_tests},
4625 {"generate_nonblock", pre_nonblock, 0, generate_tests},
4626 {"consistent_not", 0, 0, consistent_tests},
4627 {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests},
4628 {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
4629 {"test_hashes", 0, 0, hash_tests},
4630 {"replication", pre_replication, 0, replication_tests},
4631 {"replication_noblock", pre_replication_noblock, 0, replication_tests},
4632 {0, 0, 0, 0}
4633 };
4634
4635 #define SERVERS_TO_CREATE 5
4636
4637 /* Prototypes for functions we will pass to test framework */
4638 void *world_create(void);
4639 void world_destroy(void *p);
4640
4641 void *world_create(void)
4642 {
4643 server_startup_st *construct;
4644
4645 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
4646 memset(construct, 0, sizeof(server_startup_st));
4647 construct->count= SERVERS_TO_CREATE;
4648 construct->udp= 0;
4649 server_startup(construct);
4650
4651 return construct;
4652 }
4653
4654
4655 void world_destroy(void *p)
4656 {
4657 server_startup_st *construct= (server_startup_st *)p;
4658 memcached_server_st *servers= (memcached_server_st *)construct->servers;
4659 memcached_server_list_free(servers);
4660
4661 server_shutdown(construct);
4662 free(construct);
4663 }
4664
4665 void get_world(world_st *world)
4666 {
4667 world->collections= collection;
4668 world->create= world_create;
4669 world->destroy= world_destroy;
4670 }