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