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