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