e12ad3f7d9a70a6ef4977b040370300f2f50332d
[m6w6/libmemcached] / tests / mem_functions.c
1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 /*
10 Sample test application.
11 */
12
13 #include "libmemcached/common.h"
14
15 #include <assert.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include "server.h"
26 #include "clients/generator.h"
27 #include "clients/execute.h"
28
29 #ifndef INT64_MAX
30 #define INT64_MAX LONG_MAX
31 #endif
32 #ifndef INT32_MAX
33 #define INT32_MAX INT_MAX
34 #endif
35
36
37 #include "test.h"
38
39 #ifdef HAVE_LIBMEMCACHEDUTIL
40 #include <pthread.h>
41 #include "libmemcached/memcached_util.h"
42 #endif
43
44 #include "hash_results.h"
45
46 #define GLOBAL_COUNT 10000
47 #define GLOBAL2_COUNT 100
48 #define SERVERS_TO_CREATE 5
49 static uint32_t global_count;
50
51 static pairs_st *global_pairs;
52 static const char *global_keys[GLOBAL_COUNT];
53 static size_t global_keys_length[GLOBAL_COUNT];
54
55 // Prototype
56 static test_return_t pre_binary(memcached_st *memc);
57
58
59 static test_return_t init_test(memcached_st *not_used __attribute__((unused)))
60 {
61 memcached_st memc;
62
63 (void)memcached_create(&memc);
64 memcached_free(&memc);
65
66 return TEST_SUCCESS;
67 }
68
69 static test_return_t server_list_null_test(memcached_st *ptr __attribute__((unused)))
70 {
71 memcached_server_st *server_list;
72 memcached_return_t rc;
73
74 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, NULL);
75 test_true(server_list == NULL);
76
77 server_list= memcached_server_list_append_with_weight(NULL, "localhost", 0, 0, NULL);
78 test_true(server_list == NULL);
79
80 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, &rc);
81 test_true(server_list == NULL);
82
83 return TEST_SUCCESS;
84 }
85
86 #define TEST_PORT_COUNT 7
87 in_port_t test_ports[TEST_PORT_COUNT];
88
89 static memcached_return_t server_display_function(const memcached_st *ptr __attribute__((unused)),
90 const memcached_server_st *server,
91 void *context)
92 {
93 /* Do Nothing */
94 size_t bigger= *((size_t *)(context));
95 assert(bigger <= memcached_server_port(server));
96 *((size_t *)(context))= memcached_server_port(server);
97
98 return MEMCACHED_SUCCESS;
99 }
100
101 static test_return_t server_sort_test(memcached_st *ptr __attribute__((unused)))
102 {
103 size_t bigger= 0; /* Prime the value for the test_true in server_display_function */
104
105 memcached_return_t rc;
106 memcached_server_fn callbacks[1];
107 memcached_st *local_memc;
108
109 local_memc= memcached_create(NULL);
110 test_true(local_memc);
111 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
112
113 for (size_t x= 0; x < TEST_PORT_COUNT; x++)
114 {
115 test_ports[x]= (in_port_t)random() % 64000;
116 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
117 test_true(memcached_server_count(local_memc) == x + 1);
118 #if 0 // Rewrite
119 test_true(memcached_server_list_count(memcached_server_list(local_memc)) == x+1);
120 #endif
121 test_true(rc == MEMCACHED_SUCCESS);
122 }
123
124 callbacks[0]= server_display_function;
125 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
126
127
128 memcached_free(local_memc);
129
130 return TEST_SUCCESS;
131 }
132
133 static test_return_t server_sort2_test(memcached_st *ptr __attribute__((unused)))
134 {
135 size_t bigger= 0; /* Prime the value for the test_true in server_display_function */
136 memcached_return_t rc;
137 memcached_server_fn callbacks[1];
138 memcached_st *local_memc;
139 memcached_server_instance_st instance;
140
141 local_memc= memcached_create(NULL);
142 test_true(local_memc);
143 rc= memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
144 test_true(rc == MEMCACHED_SUCCESS);
145
146 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
147 test_true(rc == MEMCACHED_SUCCESS);
148 instance= memcached_server_instance_by_position(local_memc, 0);
149 test_true(memcached_server_port(instance) == 43043);
150
151 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
152 test_true(rc == MEMCACHED_SUCCESS);
153
154 instance= memcached_server_instance_by_position(local_memc, 0);
155 test_true(memcached_server_port(instance) == 43042);
156
157 instance= memcached_server_instance_by_position(local_memc, 1);
158 test_true(memcached_server_port(instance) == 43043);
159
160 callbacks[0]= server_display_function;
161 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
162
163
164 memcached_free(local_memc);
165
166 return TEST_SUCCESS;
167 }
168
169 static memcached_return_t server_print_callback(const memcached_st *ptr __attribute__((unused)),
170 const memcached_server_st *server,
171 void *context __attribute__((unused)))
172 {
173 (void)server; // Just in case we aren't printing.
174
175 #if 0
176 fprintf(stderr, "%s(%d)", memcached_server_name(server), memcached_server_port(server));
177 #endif
178
179 return MEMCACHED_SUCCESS;
180 }
181
182 static test_return_t memcached_server_remove_test(memcached_st *ptr __attribute__((unused)))
183 {
184 memcached_return_t rc;
185 memcached_st local_memc;
186 memcached_st *memc;
187 memcached_server_st *servers;
188 memcached_server_fn callbacks[1];
189
190 const char *server_string= "localhost:4444, localhost:4445, localhost:4446, localhost:4447, localhost, 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";
191
192 memc= memcached_create(&local_memc);
193
194 servers= memcached_servers_parse(server_string);
195
196 rc= memcached_server_push(memc, servers);
197 memcached_server_list_free(servers);
198
199 callbacks[0]= server_print_callback;
200 memcached_server_cursor(memc, callbacks, NULL, 1);
201
202 memcached_free(memc);
203
204 return TEST_SUCCESS;
205 }
206
207 static memcached_return_t server_display_unsort_function(const memcached_st *ptr __attribute__((unused)),
208 const memcached_server_st *server,
209 void *context)
210 {
211 /* Do Nothing */
212 uint32_t x= *((uint32_t *)(context));
213
214 assert(test_ports[x] == server->port);
215 *((uint32_t *)(context))= ++x;
216
217 return MEMCACHED_SUCCESS;
218 }
219
220 static test_return_t server_unsort_test(memcached_st *ptr __attribute__((unused)))
221 {
222 size_t counter= 0; /* Prime the value for the test_true in server_display_function */
223 size_t bigger= 0; /* Prime the value for the test_true in server_display_function */
224 memcached_return_t rc;
225 memcached_server_fn callbacks[1];
226 memcached_st *local_memc;
227
228 local_memc= memcached_create(NULL);
229 test_true(local_memc);
230
231 for (size_t x= 0; x < TEST_PORT_COUNT; x++)
232 {
233 test_ports[x]= (in_port_t)(random() % 64000);
234 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
235 test_true(memcached_server_count(local_memc) == x+1);
236 #if 0 // Rewrite
237 test_true(memcached_server_list_count(memcached_server_list(local_memc)) == x+1);
238 #endif
239 test_true(rc == MEMCACHED_SUCCESS);
240 }
241
242 callbacks[0]= server_display_unsort_function;
243 memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1);
244
245 /* Now we sort old data! */
246 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
247 callbacks[0]= server_display_function;
248 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
249
250
251 memcached_free(local_memc);
252
253 return TEST_SUCCESS;
254 }
255
256 static test_return_t allocation_test(memcached_st *not_used __attribute__((unused)))
257 {
258 memcached_st *memc;
259 memc= memcached_create(NULL);
260 test_true(memc);
261 memcached_free(memc);
262
263 return TEST_SUCCESS;
264 }
265
266 static test_return_t clone_test(memcached_st *memc)
267 {
268 /* All null? */
269 {
270 memcached_st *memc_clone;
271 memc_clone= memcached_clone(NULL, NULL);
272 test_true(memc_clone);
273 memcached_free(memc_clone);
274 }
275
276 /* Can we init from null? */
277 {
278 memcached_st *memc_clone;
279 memc_clone= memcached_clone(NULL, memc);
280 test_true(memc_clone);
281
282 { // Test allocators
283 test_true(memc_clone->allocators.free == memc->allocators.free);
284 test_true(memc_clone->allocators.malloc == memc->allocators.malloc);
285 test_true(memc_clone->allocators.realloc == memc->allocators.realloc);
286 test_true(memc_clone->allocators.calloc == memc->allocators.calloc);
287 }
288
289 test_true(memc_clone->connect_timeout == memc->connect_timeout);
290 test_true(memc_clone->delete_trigger == memc->delete_trigger);
291 test_true(memc_clone->distribution == memc->distribution);
292 { // Test all of the flags
293 test_true(memc_clone->flags.no_block == memc->flags.no_block);
294 test_true(memc_clone->flags.tcp_nodelay == memc->flags.tcp_nodelay);
295 test_true(memc_clone->flags.reuse_memory == memc->flags.reuse_memory);
296 test_true(memc_clone->flags.use_cache_lookups == memc->flags.use_cache_lookups);
297 test_true(memc_clone->flags.support_cas == memc->flags.support_cas);
298 test_true(memc_clone->flags.buffer_requests == memc->flags.buffer_requests);
299 test_true(memc_clone->flags.use_sort_hosts == memc->flags.use_sort_hosts);
300 test_true(memc_clone->flags.verify_key == memc->flags.verify_key);
301 test_true(memc_clone->flags.ketama_weighted == memc->flags.ketama_weighted);
302 test_true(memc_clone->flags.binary_protocol == memc->flags.binary_protocol);
303 test_true(memc_clone->flags.hash_with_prefix_key == memc->flags.hash_with_prefix_key);
304 test_true(memc_clone->flags.no_reply == memc->flags.no_reply);
305 test_true(memc_clone->flags.use_udp == memc->flags.use_udp);
306 test_true(memc_clone->flags.auto_eject_hosts == memc->flags.auto_eject_hosts);
307 test_true(memc_clone->flags.randomize_replica_read == memc->flags.randomize_replica_read);
308 }
309 test_true(memc_clone->get_key_failure == memc->get_key_failure);
310 test_true(hashkit_compare(&memc_clone->hashkit, &memc->hashkit));
311 test_true(hashkit_compare(&memc_clone->distribution_hashkit, &memc->distribution_hashkit));
312 test_true(memc_clone->io_bytes_watermark == memc->io_bytes_watermark);
313 test_true(memc_clone->io_msg_watermark == memc->io_msg_watermark);
314 test_true(memc_clone->io_key_prefetch == memc->io_key_prefetch);
315 test_true(memc_clone->on_cleanup == memc->on_cleanup);
316 test_true(memc_clone->on_clone == memc->on_clone);
317 test_true(memc_clone->poll_timeout == memc->poll_timeout);
318 test_true(memc_clone->rcv_timeout == memc->rcv_timeout);
319 test_true(memc_clone->recv_size == memc->recv_size);
320 test_true(memc_clone->retry_timeout == memc->retry_timeout);
321 test_true(memc_clone->send_size == memc->send_size);
322 test_true(memc_clone->server_failure_limit == memc->server_failure_limit);
323 test_true(memc_clone->snd_timeout == memc->snd_timeout);
324 test_true(memc_clone->user_data == memc->user_data);
325
326 memcached_free(memc_clone);
327 }
328
329 /* Can we init from struct? */
330 {
331 memcached_st declared_clone;
332 memcached_st *memc_clone;
333 memset(&declared_clone, 0 , sizeof(memcached_st));
334 memc_clone= memcached_clone(&declared_clone, NULL);
335 test_true(memc_clone);
336 memcached_free(memc_clone);
337 }
338
339 /* Can we init from struct? */
340 {
341 memcached_st declared_clone;
342 memcached_st *memc_clone;
343 memset(&declared_clone, 0 , sizeof(memcached_st));
344 memc_clone= memcached_clone(&declared_clone, memc);
345 test_true(memc_clone);
346 memcached_free(memc_clone);
347 }
348
349 return TEST_SUCCESS;
350 }
351
352 static test_return_t userdata_test(memcached_st *memc)
353 {
354 void* foo= NULL;
355 test_true(memcached_set_user_data(memc, foo) == NULL);
356 test_true(memcached_get_user_data(memc) == foo);
357 test_true(memcached_set_user_data(memc, NULL) == foo);
358
359 return TEST_SUCCESS;
360 }
361
362 static test_return_t connection_test(memcached_st *memc)
363 {
364 memcached_return_t rc;
365
366 rc= memcached_server_add_with_weight(memc, "localhost", 0, 0);
367 test_true(rc == MEMCACHED_SUCCESS);
368
369 return TEST_SUCCESS;
370 }
371
372 static test_return_t error_test(memcached_st *memc)
373 {
374 memcached_return_t rc;
375 uint32_t values[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
376 982370485U, 1263635348U, 4242906218U, 3829656100U,
377 1891735253U, 334139633U, 2257084983U, 3088286104U,
378 13199785U, 2542027183U, 1097051614U, 199566778U,
379 2748246961U, 2465192557U, 1664094137U, 2405439045U,
380 1842224848U, 692413798U, 3479807801U, 919913813U,
381 4269430871U, 610793021U, 527273862U, 1437122909U,
382 2300930706U, 2943759320U, 674306647U, 2400528935U,
383 54481931U, 4186304426U, 1741088401U, 2979625118U,
384 4159057246U, 3425930182U, 2593724503U, 1868899624U,
385 1769812374U, 2302537950U, 1110330676U };
386
387 // You have updated the memcache_error messages but not updated docs/tests.
388 test_true(MEMCACHED_MAXIMUM_RETURN == 43);
389 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
390 {
391 uint32_t hash_val;
392 const char *msg= memcached_strerror(memc, rc);
393 hash_val= memcached_generate_hash_value(msg, strlen(msg),
394 MEMCACHED_HASH_JENKINS);
395 if (values[rc] != hash_val)
396 {
397 fprintf(stderr, "\n\nYou have updated memcached_return_t without updating the error_test\n");
398 fprintf(stderr, "%u, %s, (%u)\n\n", (uint32_t)rc, memcached_strerror(memc, rc), hash_val);
399 }
400 test_true(values[rc] == hash_val);
401 }
402
403 return TEST_SUCCESS;
404 }
405
406 static test_return_t set_test(memcached_st *memc)
407 {
408 memcached_return_t rc;
409 const char *key= "foo";
410 const char *value= "when we sanitize";
411
412 rc= memcached_set(memc, key, strlen(key),
413 value, strlen(value),
414 (time_t)0, (uint32_t)0);
415 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
416
417 return TEST_SUCCESS;
418 }
419
420 static test_return_t append_test(memcached_st *memc)
421 {
422 memcached_return_t rc;
423 const char *key= "fig";
424 const char *in_value= "we";
425 char *out_value= NULL;
426 size_t value_length;
427 uint32_t flags;
428
429 rc= memcached_flush(memc, 0);
430 test_true(rc == MEMCACHED_SUCCESS);
431
432 rc= memcached_set(memc, key, strlen(key),
433 in_value, strlen(in_value),
434 (time_t)0, (uint32_t)0);
435 test_true(rc == MEMCACHED_SUCCESS);
436
437 rc= memcached_append(memc, key, strlen(key),
438 " the", strlen(" the"),
439 (time_t)0, (uint32_t)0);
440 test_true(rc == MEMCACHED_SUCCESS);
441
442 rc= memcached_append(memc, key, strlen(key),
443 " people", strlen(" people"),
444 (time_t)0, (uint32_t)0);
445 test_true(rc == MEMCACHED_SUCCESS);
446
447 out_value= memcached_get(memc, key, strlen(key),
448 &value_length, &flags, &rc);
449 test_true(!memcmp(out_value, "we the people", strlen("we the people")));
450 test_true(strlen("we the people") == value_length);
451 test_true(rc == MEMCACHED_SUCCESS);
452 free(out_value);
453
454 return TEST_SUCCESS;
455 }
456
457 static test_return_t append_binary_test(memcached_st *memc)
458 {
459 memcached_return_t rc;
460 const char *key= "numbers";
461 uint32_t store_list[] = { 23, 56, 499, 98, 32847, 0 };
462 uint32_t *value;
463 size_t value_length;
464 uint32_t flags;
465 uint32_t x;
466
467 rc= memcached_flush(memc, 0);
468 test_true(rc == MEMCACHED_SUCCESS);
469
470 rc= memcached_set(memc,
471 key, strlen(key),
472 NULL, 0,
473 (time_t)0, (uint32_t)0);
474 test_true(rc == MEMCACHED_SUCCESS);
475
476 for (x= 0; store_list[x] ; x++)
477 {
478 rc= memcached_append(memc,
479 key, strlen(key),
480 (char *)&store_list[x], sizeof(uint32_t),
481 (time_t)0, (uint32_t)0);
482 test_true(rc == MEMCACHED_SUCCESS);
483 }
484
485 value= (uint32_t *)memcached_get(memc, key, strlen(key),
486 &value_length, &flags, &rc);
487 test_true((value_length == (sizeof(uint32_t) * x)));
488 test_true(rc == MEMCACHED_SUCCESS);
489
490 for (uint32_t counter= x, *ptr= value; counter; counter--)
491 {
492 test_true(*ptr == store_list[x - counter]);
493 ptr++;
494 }
495 free(value);
496
497 return TEST_SUCCESS;
498 }
499
500 static test_return_t cas2_test(memcached_st *memc)
501 {
502 memcached_return_t rc;
503 const char *keys[]= {"fudge", "son", "food"};
504 size_t key_length[]= {5, 3, 4};
505 const char *value= "we the people";
506 size_t value_length= strlen("we the people");
507 unsigned int x;
508 memcached_result_st results_obj;
509 memcached_result_st *results;
510 unsigned int set= 1;
511
512 rc= memcached_flush(memc, 0);
513 test_true(rc == MEMCACHED_SUCCESS);
514
515 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
516
517 for (x= 0; x < 3; x++)
518 {
519 rc= memcached_set(memc, keys[x], key_length[x],
520 keys[x], key_length[x],
521 (time_t)50, (uint32_t)9);
522 test_true(rc == MEMCACHED_SUCCESS);
523 }
524
525 rc= memcached_mget(memc, keys, key_length, 3);
526
527 results= memcached_result_create(memc, &results_obj);
528
529 results= memcached_fetch_result(memc, &results_obj, &rc);
530 test_true(results);
531 test_true(results->item_cas);
532 test_true(rc == MEMCACHED_SUCCESS);
533 test_true(memcached_result_cas(results));
534
535 test_true(!memcmp(value, "we the people", strlen("we the people")));
536 test_true(strlen("we the people") == value_length);
537 test_true(rc == MEMCACHED_SUCCESS);
538
539 memcached_result_free(&results_obj);
540
541 return TEST_SUCCESS;
542 }
543
544 static test_return_t cas_test(memcached_st *memc)
545 {
546 memcached_return_t rc;
547 const char *key= "fun";
548 size_t key_length= strlen(key);
549 const char *value= "we the people";
550 const char* keys[2] = { key, NULL };
551 size_t keylengths[2] = { strlen(key), 0 };
552 size_t value_length= strlen(value);
553 const char *value2= "change the value";
554 size_t value2_length= strlen(value2);
555
556 memcached_result_st results_obj;
557 memcached_result_st *results;
558 unsigned int set= 1;
559
560 rc= memcached_flush(memc, 0);
561 test_true(rc == MEMCACHED_SUCCESS);
562
563 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
564
565 rc= memcached_set(memc, key, strlen(key),
566 value, strlen(value),
567 (time_t)0, (uint32_t)0);
568 test_true(rc == MEMCACHED_SUCCESS);
569
570 rc= memcached_mget(memc, keys, keylengths, 1);
571
572 results= memcached_result_create(memc, &results_obj);
573
574 results= memcached_fetch_result(memc, &results_obj, &rc);
575 test_true(results);
576 test_true(rc == MEMCACHED_SUCCESS);
577 test_true(memcached_result_cas(results));
578 test_true(!memcmp(value, memcached_result_value(results), value_length));
579 test_true(strlen(memcached_result_value(results)) == value_length);
580 test_true(rc == MEMCACHED_SUCCESS);
581 uint64_t cas = memcached_result_cas(results);
582
583 #if 0
584 results= memcached_fetch_result(memc, &results_obj, &rc);
585 test_true(rc == MEMCACHED_END);
586 test_true(results == NULL);
587 #endif
588
589 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
590 test_true(rc == MEMCACHED_SUCCESS);
591
592 /*
593 * The item will have a new cas value, so try to set it again with the old
594 * value. This should fail!
595 */
596 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
597 test_true(rc == MEMCACHED_DATA_EXISTS);
598
599 memcached_result_free(&results_obj);
600
601 return TEST_SUCCESS;
602 }
603
604 static test_return_t prepend_test(memcached_st *memc)
605 {
606 memcached_return_t rc;
607 const char *key= "fig";
608 const char *value= "people";
609 char *out_value= NULL;
610 size_t value_length;
611 uint32_t flags;
612
613 rc= memcached_flush(memc, 0);
614 test_true(rc == MEMCACHED_SUCCESS);
615
616 rc= memcached_set(memc, key, strlen(key),
617 value, strlen(value),
618 (time_t)0, (uint32_t)0);
619 test_true(rc == MEMCACHED_SUCCESS);
620
621 rc= memcached_prepend(memc, key, strlen(key),
622 "the ", strlen("the "),
623 (time_t)0, (uint32_t)0);
624 test_true(rc == MEMCACHED_SUCCESS);
625
626 rc= memcached_prepend(memc, key, strlen(key),
627 "we ", strlen("we "),
628 (time_t)0, (uint32_t)0);
629 test_true(rc == MEMCACHED_SUCCESS);
630
631 out_value= memcached_get(memc, key, strlen(key),
632 &value_length, &flags, &rc);
633 test_true(!memcmp(out_value, "we the people", strlen("we the people")));
634 test_true(strlen("we the people") == value_length);
635 test_true(rc == MEMCACHED_SUCCESS);
636 free(out_value);
637
638 return TEST_SUCCESS;
639 }
640
641 /*
642 Set the value, then quit to make sure it is flushed.
643 Come back in and test that add fails.
644 */
645 static test_return_t add_test(memcached_st *memc)
646 {
647 memcached_return_t rc;
648 const char *key= "foo";
649 const char *value= "when we sanitize";
650 unsigned long long setting_value;
651
652 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
653
654 rc= memcached_set(memc, key, strlen(key),
655 value, strlen(value),
656 (time_t)0, (uint32_t)0);
657 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
658 memcached_quit(memc);
659 rc= memcached_add(memc, key, strlen(key),
660 value, strlen(value),
661 (time_t)0, (uint32_t)0);
662
663 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
664 if (setting_value)
665 {
666 test_true(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
667 }
668 else
669 {
670 test_true(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_DATA_EXISTS);
671 }
672
673 return TEST_SUCCESS;
674 }
675
676 /*
677 ** There was a problem of leaking filedescriptors in the initial release
678 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
679 ** systems it seems that the kernel is slow on reclaiming the resources
680 ** because the connects starts to time out (the test doesn't do much
681 ** anyway, so just loop 10 iterations)
682 */
683 static test_return_t add_wrapper(memcached_st *memc)
684 {
685 unsigned int x;
686 unsigned int max= 10000;
687 #ifdef __sun
688 max= 10;
689 #endif
690 #ifdef __APPLE__
691 max= 10;
692 #endif
693
694 for (x= 0; x < max; x++)
695 add_test(memc);
696
697 return TEST_SUCCESS;
698 }
699
700 static test_return_t replace_test(memcached_st *memc)
701 {
702 memcached_return_t rc;
703 const char *key= "foo";
704 const char *value= "when we sanitize";
705 const char *original= "first we insert some data";
706
707 rc= memcached_set(memc, key, strlen(key),
708 original, strlen(original),
709 (time_t)0, (uint32_t)0);
710 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
711
712 rc= memcached_replace(memc, key, strlen(key),
713 value, strlen(value),
714 (time_t)0, (uint32_t)0);
715 test_true(rc == MEMCACHED_SUCCESS);
716
717 return TEST_SUCCESS;
718 }
719
720 static test_return_t delete_test(memcached_st *memc)
721 {
722 memcached_return_t rc;
723 const char *key= "foo";
724 const char *value= "when we sanitize";
725
726 rc= memcached_set(memc, key, strlen(key),
727 value, strlen(value),
728 (time_t)0, (uint32_t)0);
729 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
730
731 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
732 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
733
734 return TEST_SUCCESS;
735 }
736
737 static test_return_t flush_test(memcached_st *memc)
738 {
739 memcached_return_t rc;
740
741 rc= memcached_flush(memc, 0);
742 test_true(rc == MEMCACHED_SUCCESS);
743
744 return TEST_SUCCESS;
745 }
746
747 static memcached_return_t server_function(const memcached_st *ptr __attribute__((unused)),
748 const memcached_server_st *server __attribute__((unused)),
749 void *context __attribute__((unused)))
750 {
751 /* Do Nothing */
752
753 return MEMCACHED_SUCCESS;
754 }
755
756 static test_return_t memcached_server_cursor_test(memcached_st *memc)
757 {
758 char context[8];
759 strcpy(context, "foo bad");
760 memcached_server_fn callbacks[1];
761
762 callbacks[0]= server_function;
763 memcached_server_cursor(memc, callbacks, context, 1);
764 return TEST_SUCCESS;
765 }
766
767 static test_return_t bad_key_test(memcached_st *memc)
768 {
769 memcached_return_t rc;
770 const char *key= "foo bad";
771 char *string;
772 size_t string_length;
773 uint32_t flags;
774 memcached_st *memc_clone;
775 unsigned int set= 1;
776 size_t max_keylen= 0xffff;
777
778 // Just skip if we are in binary mode.
779 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL))
780 return TEST_SKIPPED;
781
782 memc_clone= memcached_clone(NULL, memc);
783 test_true(memc_clone);
784
785 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
786 test_true(rc == MEMCACHED_SUCCESS);
787
788 /* All keys are valid in the binary protocol (except for length) */
789 if (memcached_behavior_get(memc_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0)
790 {
791 string= memcached_get(memc_clone, key, strlen(key),
792 &string_length, &flags, &rc);
793 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
794 test_true(string_length == 0);
795 test_true(!string);
796
797 set= 0;
798 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
799 test_true(rc == MEMCACHED_SUCCESS);
800 string= memcached_get(memc_clone, key, strlen(key),
801 &string_length, &flags, &rc);
802 test_true(rc == MEMCACHED_NOTFOUND);
803 test_true(string_length == 0);
804 test_true(!string);
805
806 /* Test multi key for bad keys */
807 const char *keys[] = { "GoodKey", "Bad Key", "NotMine" };
808 size_t key_lengths[] = { 7, 7, 7 };
809 set= 1;
810 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
811 test_true(rc == MEMCACHED_SUCCESS);
812
813 rc= memcached_mget(memc_clone, keys, key_lengths, 3);
814 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
815
816 rc= memcached_mget_by_key(memc_clone, "foo daddy", 9, keys, key_lengths, 1);
817 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
818
819 max_keylen= 250;
820
821 /* The following test should be moved to the end of this function when the
822 memcached server is updated to allow max size length of the keys in the
823 binary protocol
824 */
825 rc= memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
826 test_true(rc == MEMCACHED_SUCCESS);
827
828 char *longkey= malloc(max_keylen + 1);
829 if (longkey != NULL)
830 {
831 memset(longkey, 'a', max_keylen + 1);
832 string= memcached_get(memc_clone, longkey, max_keylen,
833 &string_length, &flags, &rc);
834 test_true(rc == MEMCACHED_NOTFOUND);
835 test_true(string_length == 0);
836 test_true(!string);
837
838 string= memcached_get(memc_clone, longkey, max_keylen + 1,
839 &string_length, &flags, &rc);
840 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
841 test_true(string_length == 0);
842 test_true(!string);
843
844 free(longkey);
845 }
846 }
847
848 /* Make sure zero length keys are marked as bad */
849 set= 1;
850 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
851 test_true(rc == MEMCACHED_SUCCESS);
852 string= memcached_get(memc_clone, key, 0,
853 &string_length, &flags, &rc);
854 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
855 test_true(string_length == 0);
856 test_true(!string);
857
858 memcached_free(memc_clone);
859
860 return TEST_SUCCESS;
861 }
862
863 #define READ_THROUGH_VALUE "set for me"
864 static memcached_return_t read_through_trigger(memcached_st *memc __attribute__((unused)),
865 char *key __attribute__((unused)),
866 size_t key_length __attribute__((unused)),
867 memcached_result_st *result)
868 {
869
870 return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
871 }
872
873 static test_return_t read_through(memcached_st *memc)
874 {
875 memcached_return_t rc;
876 const char *key= "foo";
877 char *string;
878 size_t string_length;
879 uint32_t flags;
880 memcached_trigger_key_fn cb= (memcached_trigger_key_fn)read_through_trigger;
881
882 string= memcached_get(memc, key, strlen(key),
883 &string_length, &flags, &rc);
884
885 test_true(rc == MEMCACHED_NOTFOUND);
886 test_false(string_length);
887 test_false(string);
888
889 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE,
890 *(void **)&cb);
891 test_true(rc == MEMCACHED_SUCCESS);
892
893 string= memcached_get(memc, key, strlen(key),
894 &string_length, &flags, &rc);
895
896 test_true(rc == MEMCACHED_SUCCESS);
897 test_true(string_length == strlen(READ_THROUGH_VALUE));
898 test_strcmp(READ_THROUGH_VALUE, string);
899 free(string);
900
901 string= memcached_get(memc, key, strlen(key),
902 &string_length, &flags, &rc);
903
904 test_true(rc == MEMCACHED_SUCCESS);
905 test_true(string_length == strlen(READ_THROUGH_VALUE));
906 test_true(!strcmp(READ_THROUGH_VALUE, string));
907 free(string);
908
909 return TEST_SUCCESS;
910 }
911
912 static memcached_return_t delete_trigger(memcached_st *ptr __attribute__((unused)),
913 const char *key,
914 size_t key_length __attribute__((unused)))
915 {
916 assert(key);
917
918 return MEMCACHED_SUCCESS;
919 }
920
921 static test_return_t delete_through(memcached_st *memc)
922 {
923 memcached_trigger_delete_key_fn callback;
924 memcached_return_t rc;
925
926 callback= (memcached_trigger_delete_key_fn)delete_trigger;
927
928 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, *(void**)&callback);
929 test_true(rc == MEMCACHED_SUCCESS);
930
931 return TEST_SUCCESS;
932 }
933
934 static test_return_t get_test(memcached_st *memc)
935 {
936 memcached_return_t rc;
937 const char *key= "foo";
938 char *string;
939 size_t string_length;
940 uint32_t flags;
941
942 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
943 test_true(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
944
945 string= memcached_get(memc, key, strlen(key),
946 &string_length, &flags, &rc);
947
948 test_true(rc == MEMCACHED_NOTFOUND);
949 test_false(string_length);
950 test_false(string);
951
952 return TEST_SUCCESS;
953 }
954
955 static test_return_t get_test2(memcached_st *memc)
956 {
957 memcached_return_t rc;
958 const char *key= "foo";
959 const char *value= "when we sanitize";
960 char *string;
961 size_t string_length;
962 uint32_t flags;
963
964 rc= memcached_set(memc, key, strlen(key),
965 value, strlen(value),
966 (time_t)0, (uint32_t)0);
967 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
968
969 string= memcached_get(memc, key, strlen(key),
970 &string_length, &flags, &rc);
971
972 test_true(string);
973 test_true(rc == MEMCACHED_SUCCESS);
974 test_true(string_length == strlen(value));
975 test_true(!memcmp(string, value, string_length));
976
977 free(string);
978
979 return TEST_SUCCESS;
980 }
981
982 static test_return_t set_test2(memcached_st *memc)
983 {
984 memcached_return_t rc;
985 const char *key= "foo";
986 const char *value= "train in the brain";
987 size_t value_length= strlen(value);
988 unsigned int x;
989
990 for (x= 0; x < 10; x++)
991 {
992 rc= memcached_set(memc, key, strlen(key),
993 value, value_length,
994 (time_t)0, (uint32_t)0);
995 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
996 }
997
998 return TEST_SUCCESS;
999 }
1000
1001 static test_return_t set_test3(memcached_st *memc)
1002 {
1003 memcached_return_t rc;
1004 char *value;
1005 size_t value_length= 8191;
1006 unsigned int x;
1007
1008 value = (char*)malloc(value_length);
1009 test_true(value);
1010
1011 for (x= 0; x < value_length; x++)
1012 value[x] = (char) (x % 127);
1013
1014 /* The dump test relies on there being at least 32 items in memcached */
1015 for (x= 0; x < 32; x++)
1016 {
1017 char key[16];
1018
1019 sprintf(key, "foo%u", x);
1020
1021 rc= memcached_set(memc, key, strlen(key),
1022 value, value_length,
1023 (time_t)0, (uint32_t)0);
1024 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1025 }
1026
1027 free(value);
1028
1029 return TEST_SUCCESS;
1030 }
1031
1032 static test_return_t get_test3(memcached_st *memc)
1033 {
1034 memcached_return_t rc;
1035 const char *key= "foo";
1036 char *value;
1037 size_t value_length= 8191;
1038 char *string;
1039 size_t string_length;
1040 uint32_t flags;
1041 uint32_t x;
1042
1043 value = (char*)malloc(value_length);
1044 test_true(value);
1045
1046 for (x= 0; x < value_length; x++)
1047 value[x] = (char) (x % 127);
1048
1049 rc= memcached_set(memc, key, strlen(key),
1050 value, value_length,
1051 (time_t)0, (uint32_t)0);
1052 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1053
1054 string= memcached_get(memc, key, strlen(key),
1055 &string_length, &flags, &rc);
1056
1057 test_true(rc == MEMCACHED_SUCCESS);
1058 test_true(string);
1059 test_true(string_length == value_length);
1060 test_true(!memcmp(string, value, string_length));
1061
1062 free(string);
1063 free(value);
1064
1065 return TEST_SUCCESS;
1066 }
1067
1068 static test_return_t get_test4(memcached_st *memc)
1069 {
1070 memcached_return_t rc;
1071 const char *key= "foo";
1072 char *value;
1073 size_t value_length= 8191;
1074 char *string;
1075 size_t string_length;
1076 uint32_t flags;
1077 uint32_t x;
1078
1079 value = (char*)malloc(value_length);
1080 test_true(value);
1081
1082 for (x= 0; x < value_length; x++)
1083 value[x] = (char) (x % 127);
1084
1085 rc= memcached_set(memc, key, strlen(key),
1086 value, value_length,
1087 (time_t)0, (uint32_t)0);
1088 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1089
1090 for (x= 0; x < 10; x++)
1091 {
1092 string= memcached_get(memc, key, strlen(key),
1093 &string_length, &flags, &rc);
1094
1095 test_true(rc == MEMCACHED_SUCCESS);
1096 test_true(string);
1097 test_true(string_length == value_length);
1098 test_true(!memcmp(string, value, string_length));
1099 free(string);
1100 }
1101
1102 free(value);
1103
1104 return TEST_SUCCESS;
1105 }
1106
1107 /*
1108 * This test verifies that memcached_read_one_response doesn't try to
1109 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1110 * responses before you execute a storage command.
1111 */
1112 static test_return_t get_test5(memcached_st *memc)
1113 {
1114 /*
1115 ** Request the same key twice, to ensure that we hash to the same server
1116 ** (so that we have multiple response values queued up) ;-)
1117 */
1118 const char *keys[]= { "key", "key" };
1119 size_t lengths[]= { 3, 3 };
1120 uint32_t flags;
1121 size_t rlen;
1122
1123 memcached_return_t rc= memcached_set(memc, keys[0], lengths[0],
1124 keys[0], lengths[0], 0, 0);
1125 test_true(rc == MEMCACHED_SUCCESS);
1126 rc= memcached_mget(memc, keys, lengths, 2);
1127
1128 memcached_result_st results_obj;
1129 memcached_result_st *results;
1130 results=memcached_result_create(memc, &results_obj);
1131 test_true(results);
1132 results=memcached_fetch_result(memc, &results_obj, &rc);
1133 test_true(results);
1134 memcached_result_free(&results_obj);
1135
1136 /* Don't read out the second result, but issue a set instead.. */
1137 rc= memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0);
1138 test_true(rc == MEMCACHED_SUCCESS);
1139
1140 char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
1141 &rlen, &flags, &rc);
1142 test_true(val == NULL);
1143 test_true(rc == MEMCACHED_NOTFOUND);
1144 val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
1145 test_true(val != NULL);
1146 test_true(rc == MEMCACHED_SUCCESS);
1147 free(val);
1148
1149 return TEST_SUCCESS;
1150 }
1151
1152 static test_return_t mget_end(memcached_st *memc)
1153 {
1154 const char *keys[]= { "foo", "foo2" };
1155 size_t lengths[]= { 3, 4 };
1156 const char *values[]= { "fjord", "41" };
1157
1158 memcached_return_t rc;
1159
1160 // Set foo and foo2
1161 for (int i= 0; i < 2; i++)
1162 {
1163 rc= memcached_set(memc, keys[i], lengths[i], values[i], strlen(values[i]),
1164 (time_t)0, (uint32_t)0);
1165 test_true(rc == MEMCACHED_SUCCESS);
1166 }
1167
1168 char *string;
1169 size_t string_length;
1170 uint32_t flags;
1171
1172 // retrieve both via mget
1173 rc= memcached_mget(memc, keys, lengths, 2);
1174 test_true(rc == MEMCACHED_SUCCESS);
1175
1176 char key[MEMCACHED_MAX_KEY];
1177 size_t key_length;
1178
1179 // this should get both
1180 for (int i = 0; i < 2; i++)
1181 {
1182 string= memcached_fetch(memc, key, &key_length, &string_length,
1183 &flags, &rc);
1184 test_true(rc == MEMCACHED_SUCCESS);
1185 int val = 0;
1186 if (key_length == 4)
1187 val= 1;
1188 test_true(string_length == strlen(values[val]));
1189 test_true(strncmp(values[val], string, string_length) == 0);
1190 free(string);
1191 }
1192
1193 // this should indicate end
1194 string= memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
1195 test_true(rc == MEMCACHED_END);
1196
1197 // now get just one
1198 rc= memcached_mget(memc, keys, lengths, 1);
1199 test_true(rc == MEMCACHED_SUCCESS);
1200
1201 string= memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
1202 test_true(key_length == lengths[0]);
1203 test_true(strncmp(keys[0], key, key_length) == 0);
1204 test_true(string_length == strlen(values[0]));
1205 test_true(strncmp(values[0], string, string_length) == 0);
1206 test_true(rc == MEMCACHED_SUCCESS);
1207 free(string);
1208
1209 // this should indicate end
1210 string= memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
1211 test_true(rc == MEMCACHED_END);
1212
1213 return TEST_SUCCESS;
1214 }
1215
1216 /* Do not copy the style of this code, I just access hosts to testthis function */
1217 static test_return_t stats_servername_test(memcached_st *memc)
1218 {
1219 memcached_return_t rc;
1220 memcached_stat_st memc_stat;
1221 memcached_server_instance_st instance=
1222 memcached_server_instance_by_position(memc, 0);
1223
1224 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1225 if (memcached_get_sasl_callbacks(memc) != NULL)
1226 return TEST_SKIPPED;
1227 #endif
1228 rc= memcached_stat_servername(&memc_stat, NULL,
1229 memcached_server_name(instance),
1230 memcached_server_port(instance));
1231
1232 return TEST_SUCCESS;
1233 }
1234
1235 static test_return_t increment_test(memcached_st *memc)
1236 {
1237 uint64_t new_number;
1238 memcached_return_t rc;
1239 const char *key= "number";
1240 const char *value= "0";
1241
1242 rc= memcached_set(memc, key, strlen(key),
1243 value, strlen(value),
1244 (time_t)0, (uint32_t)0);
1245 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1246
1247 rc= memcached_increment(memc, key, strlen(key),
1248 1, &new_number);
1249 test_true(rc == MEMCACHED_SUCCESS);
1250 test_true(new_number == 1);
1251
1252 rc= memcached_increment(memc, key, strlen(key),
1253 1, &new_number);
1254 test_true(rc == MEMCACHED_SUCCESS);
1255 test_true(new_number == 2);
1256
1257 return TEST_SUCCESS;
1258 }
1259
1260 static test_return_t increment_with_initial_test(memcached_st *memc)
1261 {
1262 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1263 {
1264 uint64_t new_number;
1265 memcached_return_t rc;
1266 const char *key= "number";
1267 uint64_t initial= 0;
1268
1269 rc= memcached_increment_with_initial(memc, key, strlen(key),
1270 1, initial, 0, &new_number);
1271 test_true(rc == MEMCACHED_SUCCESS);
1272 test_true(new_number == initial);
1273
1274 rc= memcached_increment_with_initial(memc, key, strlen(key),
1275 1, initial, 0, &new_number);
1276 test_true(rc == MEMCACHED_SUCCESS);
1277 test_true(new_number == (initial + 1));
1278 }
1279 return TEST_SUCCESS;
1280 }
1281
1282 static test_return_t decrement_test(memcached_st *memc)
1283 {
1284 uint64_t new_number;
1285 memcached_return_t rc;
1286 const char *key= "number";
1287 const char *value= "3";
1288
1289 rc= memcached_set(memc, key, strlen(key),
1290 value, strlen(value),
1291 (time_t)0, (uint32_t)0);
1292 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1293
1294 rc= memcached_decrement(memc, key, strlen(key),
1295 1, &new_number);
1296 test_true(rc == MEMCACHED_SUCCESS);
1297 test_true(new_number == 2);
1298
1299 rc= memcached_decrement(memc, key, strlen(key),
1300 1, &new_number);
1301 test_true(rc == MEMCACHED_SUCCESS);
1302 test_true(new_number == 1);
1303
1304 return TEST_SUCCESS;
1305 }
1306
1307 static test_return_t decrement_with_initial_test(memcached_st *memc)
1308 {
1309 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1310 {
1311 uint64_t new_number;
1312 memcached_return_t rc;
1313 const char *key= "number";
1314 uint64_t initial= 3;
1315
1316 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1317 1, initial, 0, &new_number);
1318 test_true(rc == MEMCACHED_SUCCESS);
1319 test_true(new_number == initial);
1320
1321 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1322 1, initial, 0, &new_number);
1323 test_true(rc == MEMCACHED_SUCCESS);
1324 test_true(new_number == (initial - 1));
1325 }
1326 return TEST_SUCCESS;
1327 }
1328
1329 static test_return_t increment_by_key_test(memcached_st *memc)
1330 {
1331 uint64_t new_number;
1332 memcached_return_t rc;
1333 const char *master_key= "foo";
1334 const char *key= "number";
1335 const char *value= "0";
1336
1337 rc= memcached_set_by_key(memc, master_key, strlen(master_key),
1338 key, strlen(key),
1339 value, strlen(value),
1340 (time_t)0, (uint32_t)0);
1341 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1342
1343 rc= memcached_increment_by_key(memc, master_key, strlen(master_key), key, strlen(key),
1344 1, &new_number);
1345 test_true(rc == MEMCACHED_SUCCESS);
1346 test_true(new_number == 1);
1347
1348 rc= memcached_increment_by_key(memc, master_key, strlen(master_key), key, strlen(key),
1349 1, &new_number);
1350 test_true(rc == MEMCACHED_SUCCESS);
1351 test_true(new_number == 2);
1352
1353 return TEST_SUCCESS;
1354 }
1355
1356 static test_return_t increment_with_initial_by_key_test(memcached_st *memc)
1357 {
1358 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1359 {
1360 uint64_t new_number;
1361 memcached_return_t rc;
1362 const char *master_key= "foo";
1363 const char *key= "number";
1364 uint64_t initial= 0;
1365
1366 rc= memcached_increment_with_initial_by_key(memc, master_key, strlen(master_key),
1367 key, strlen(key),
1368 1, initial, 0, &new_number);
1369 test_true(rc == MEMCACHED_SUCCESS);
1370 test_true(new_number == initial);
1371
1372 rc= memcached_increment_with_initial_by_key(memc, master_key, strlen(master_key),
1373 key, strlen(key),
1374 1, initial, 0, &new_number);
1375 test_true(rc == MEMCACHED_SUCCESS);
1376 test_true(new_number == (initial + 1));
1377 }
1378 return TEST_SUCCESS;
1379 }
1380
1381 static test_return_t decrement_by_key_test(memcached_st *memc)
1382 {
1383 uint64_t new_number;
1384 memcached_return_t rc;
1385 const char *master_key= "foo";
1386 const char *key= "number";
1387 const char *value= "3";
1388
1389 rc= memcached_set_by_key(memc, master_key, strlen(master_key),
1390 key, strlen(key),
1391 value, strlen(value),
1392 (time_t)0, (uint32_t)0);
1393 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1394
1395 rc= memcached_decrement_by_key(memc, master_key, strlen(master_key),
1396 key, strlen(key),
1397 1, &new_number);
1398 test_true(rc == MEMCACHED_SUCCESS);
1399 test_true(new_number == 2);
1400
1401 rc= memcached_decrement_by_key(memc, master_key, strlen(master_key),
1402 key, strlen(key),
1403 1, &new_number);
1404 test_true(rc == MEMCACHED_SUCCESS);
1405 test_true(new_number == 1);
1406
1407 return TEST_SUCCESS;
1408 }
1409
1410 static test_return_t decrement_with_initial_by_key_test(memcached_st *memc)
1411 {
1412 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1413 {
1414 uint64_t new_number;
1415 memcached_return_t rc;
1416 const char *master_key= "foo";
1417 const char *key= "number";
1418 uint64_t initial= 3;
1419
1420 rc= memcached_decrement_with_initial_by_key(memc, master_key, strlen(master_key),
1421 key, strlen(key),
1422 1, initial, 0, &new_number);
1423 test_true(rc == MEMCACHED_SUCCESS);
1424 test_true(new_number == initial);
1425
1426 rc= memcached_decrement_with_initial_by_key(memc, master_key, strlen(master_key),
1427 key, strlen(key),
1428 1, initial, 0, &new_number);
1429 test_true(rc == MEMCACHED_SUCCESS);
1430 test_true(new_number == (initial - 1));
1431 }
1432 return TEST_SUCCESS;
1433 }
1434
1435 static test_return_t quit_test(memcached_st *memc)
1436 {
1437 memcached_return_t rc;
1438 const char *key= "fudge";
1439 const char *value= "sanford and sun";
1440
1441 rc= memcached_set(memc, key, strlen(key),
1442 value, strlen(value),
1443 (time_t)10, (uint32_t)3);
1444 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1445 memcached_quit(memc);
1446
1447 rc= memcached_set(memc, key, strlen(key),
1448 value, strlen(value),
1449 (time_t)50, (uint32_t)9);
1450 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1451
1452 return TEST_SUCCESS;
1453 }
1454
1455 static test_return_t mget_result_test(memcached_st *memc)
1456 {
1457 memcached_return_t rc;
1458 const char *keys[]= {"fudge", "son", "food"};
1459 size_t key_length[]= {5, 3, 4};
1460 unsigned int x;
1461
1462 memcached_result_st results_obj;
1463 memcached_result_st *results;
1464
1465 results= memcached_result_create(memc, &results_obj);
1466 test_true(results);
1467 test_true(&results_obj == results);
1468
1469 /* We need to empty the server before continueing test */
1470 rc= memcached_flush(memc, 0);
1471 test_true(rc == MEMCACHED_SUCCESS);
1472
1473 rc= memcached_mget(memc, keys, key_length, 3);
1474 test_true(rc == MEMCACHED_SUCCESS);
1475
1476 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1477 {
1478 test_true(results);
1479 }
1480
1481 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1482 test_true(!results);
1483 test_true(rc == MEMCACHED_END);
1484
1485 for (x= 0; x < 3; x++)
1486 {
1487 rc= memcached_set(memc, keys[x], key_length[x],
1488 keys[x], key_length[x],
1489 (time_t)50, (uint32_t)9);
1490 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1491 }
1492
1493 rc= memcached_mget(memc, keys, key_length, 3);
1494 test_true(rc == MEMCACHED_SUCCESS);
1495
1496 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1497 {
1498 test_true(results);
1499 test_true(&results_obj == results);
1500 test_true(rc == MEMCACHED_SUCCESS);
1501 test_true(memcached_result_key_length(results) == memcached_result_length(results));
1502 test_true(!memcmp(memcached_result_key_value(results),
1503 memcached_result_value(results),
1504 memcached_result_length(results)));
1505 }
1506
1507 memcached_result_free(&results_obj);
1508
1509 return TEST_SUCCESS;
1510 }
1511
1512 static test_return_t mget_result_alloc_test(memcached_st *memc)
1513 {
1514 memcached_return_t rc;
1515 const char *keys[]= {"fudge", "son", "food"};
1516 size_t key_length[]= {5, 3, 4};
1517 unsigned int x;
1518
1519 memcached_result_st *results;
1520
1521 /* We need to empty the server before continueing test */
1522 rc= memcached_flush(memc, 0);
1523 test_true(rc == MEMCACHED_SUCCESS);
1524
1525 rc= memcached_mget(memc, keys, key_length, 3);
1526 test_true(rc == MEMCACHED_SUCCESS);
1527
1528 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
1529 {
1530 test_true(results);
1531 }
1532 test_true(!results);
1533 test_true(rc == MEMCACHED_END);
1534
1535 for (x= 0; x < 3; x++)
1536 {
1537 rc= memcached_set(memc, keys[x], key_length[x],
1538 keys[x], key_length[x],
1539 (time_t)50, (uint32_t)9);
1540 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1541 }
1542
1543 rc= memcached_mget(memc, keys, key_length, 3);
1544 test_true(rc == MEMCACHED_SUCCESS);
1545
1546 x= 0;
1547 while ((results= memcached_fetch_result(memc, NULL, &rc)))
1548 {
1549 test_true(results);
1550 test_true(rc == MEMCACHED_SUCCESS);
1551 test_true(memcached_result_key_length(results) == memcached_result_length(results));
1552 test_true(!memcmp(memcached_result_key_value(results),
1553 memcached_result_value(results),
1554 memcached_result_length(results)));
1555 memcached_result_free(results);
1556 x++;
1557 }
1558
1559 return TEST_SUCCESS;
1560 }
1561
1562 /* Count the results */
1563 static memcached_return_t callback_counter(const memcached_st *ptr __attribute__((unused)),
1564 memcached_result_st *result __attribute__((unused)),
1565 void *context)
1566 {
1567 size_t *counter= (size_t *)context;
1568
1569 *counter= *counter + 1;
1570
1571 return MEMCACHED_SUCCESS;
1572 }
1573
1574 static test_return_t mget_result_function(memcached_st *memc)
1575 {
1576 memcached_return_t rc;
1577 const char *keys[]= {"fudge", "son", "food"};
1578 size_t key_length[]= {5, 3, 4};
1579 unsigned int x;
1580 size_t counter;
1581 memcached_execute_fn callbacks[1];
1582
1583 /* We need to empty the server before continueing test */
1584 rc= memcached_flush(memc, 0);
1585 for (x= 0; x < 3; x++)
1586 {
1587 rc= memcached_set(memc, keys[x], key_length[x],
1588 keys[x], key_length[x],
1589 (time_t)50, (uint32_t)9);
1590 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1591 }
1592
1593 rc= memcached_mget(memc, keys, key_length, 3);
1594 test_true(rc == MEMCACHED_SUCCESS);
1595
1596 callbacks[0]= &callback_counter;
1597 counter= 0;
1598 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1599
1600 test_true(counter == 3);
1601
1602 return TEST_SUCCESS;
1603 }
1604
1605 static test_return_t mget_test(memcached_st *memc)
1606 {
1607 memcached_return_t rc;
1608 const char *keys[]= {"fudge", "son", "food"};
1609 size_t key_length[]= {5, 3, 4};
1610 unsigned int x;
1611 uint32_t flags;
1612
1613 char return_key[MEMCACHED_MAX_KEY];
1614 size_t return_key_length;
1615 char *return_value;
1616 size_t return_value_length;
1617
1618 /* We need to empty the server before continueing test */
1619 rc= memcached_flush(memc, 0);
1620 test_true(rc == MEMCACHED_SUCCESS);
1621
1622 rc= memcached_mget(memc, keys, key_length, 3);
1623 test_true(rc == MEMCACHED_SUCCESS);
1624
1625 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1626 &return_value_length, &flags, &rc)) != NULL)
1627 {
1628 test_true(return_value);
1629 }
1630 test_true(!return_value);
1631 test_true(return_value_length == 0);
1632 test_true(rc == MEMCACHED_END);
1633
1634 for (x= 0; x < 3; x++)
1635 {
1636 rc= memcached_set(memc, keys[x], key_length[x],
1637 keys[x], key_length[x],
1638 (time_t)50, (uint32_t)9);
1639 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1640 }
1641
1642 rc= memcached_mget(memc, keys, key_length, 3);
1643 test_true(rc == MEMCACHED_SUCCESS);
1644
1645 x= 0;
1646 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1647 &return_value_length, &flags, &rc)))
1648 {
1649 test_true(return_value);
1650 test_true(rc == MEMCACHED_SUCCESS);
1651 test_true(return_key_length == return_value_length);
1652 test_true(!memcmp(return_value, return_key, return_value_length));
1653 free(return_value);
1654 x++;
1655 }
1656
1657 return TEST_SUCCESS;
1658 }
1659
1660 static test_return_t mget_execute(memcached_st *memc)
1661 {
1662 bool binary= false;
1663
1664 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1665 binary= true;
1666
1667 /*
1668 * I only want to hit _one_ server so I know the number of requests I'm
1669 * sending in the pipeline.
1670 */
1671 uint32_t number_of_hosts= memc->number_of_hosts;
1672 memc->number_of_hosts= 1;
1673
1674 size_t max_keys= 20480;
1675
1676
1677 char **keys= calloc(max_keys, sizeof(char*));
1678 size_t *key_length=calloc(max_keys, sizeof(size_t));
1679
1680 /* First add all of the items.. */
1681 char blob[1024] = {0};
1682 memcached_return_t rc;
1683
1684 for (size_t x= 0; x < max_keys; ++x)
1685 {
1686 char k[251];
1687
1688 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
1689 keys[x]= strdup(k);
1690 test_true(keys[x] != NULL);
1691 rc= memcached_add(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0);
1692 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1693 }
1694
1695 /* Try to get all of them with a large multiget */
1696 size_t counter= 0;
1697 memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
1698 rc= memcached_mget_execute(memc, (const char**)keys, key_length,
1699 max_keys, callbacks, &counter, 1);
1700
1701 if (rc == MEMCACHED_SUCCESS)
1702 {
1703 test_true(binary);
1704 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1705 test_true(rc == MEMCACHED_END);
1706
1707 /* Verify that we got all of the items */
1708 test_true(counter == max_keys);
1709 }
1710 else if (rc == MEMCACHED_NOT_SUPPORTED)
1711 {
1712 test_true(counter == 0);
1713 }
1714 else
1715 {
1716 test_fail("note: this test functions differently when in binary mode");
1717 }
1718
1719 /* Release all allocated resources */
1720 for (size_t x= 0; x < max_keys; ++x)
1721 {
1722 free(keys[x]);
1723 }
1724 free(keys);
1725 free(key_length);
1726
1727 memc->number_of_hosts= number_of_hosts;
1728 return TEST_SUCCESS;
1729 }
1730
1731 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1732
1733 static test_return_t key_setup(memcached_st *memc)
1734 {
1735 (void)memc;
1736
1737 if (pre_binary(memc) != TEST_SUCCESS)
1738 return TEST_SKIPPED;
1739
1740 global_pairs= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT, 0);
1741
1742 return TEST_SUCCESS;
1743 }
1744
1745 static test_return_t key_teardown(memcached_st *memc)
1746 {
1747 (void)memc;
1748 pairs_free(global_pairs);
1749
1750 return TEST_SUCCESS;
1751 }
1752
1753 static test_return_t block_add_regression(memcached_st *memc)
1754 {
1755 /* First add all of the items.. */
1756 for (size_t x= 0; x < REGRESSION_BINARY_VS_BLOCK_COUNT; ++x)
1757 {
1758 memcached_return_t rc;
1759 char blob[1024] = {0};
1760
1761 rc= memcached_add_by_key(memc, "bob", 3, global_pairs[x].key, global_pairs[x].key_length, blob, sizeof(blob), 0, 0);
1762 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1763 }
1764
1765 return TEST_SUCCESS;
1766 }
1767
1768 static test_return_t binary_add_regression(memcached_st *memc)
1769 {
1770 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
1771 test_return_t rc= block_add_regression(memc);
1772 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
1773 return rc;
1774 }
1775
1776 static test_return_t get_stats_keys(memcached_st *memc)
1777 {
1778 char **stat_list;
1779 char **ptr;
1780 memcached_stat_st memc_stat;
1781 memcached_return_t rc;
1782
1783 stat_list= memcached_stat_get_keys(memc, &memc_stat, &rc);
1784 test_true(rc == MEMCACHED_SUCCESS);
1785 for (ptr= stat_list; *ptr; ptr++)
1786 test_true(*ptr);
1787
1788 free(stat_list);
1789
1790 return TEST_SUCCESS;
1791 }
1792
1793 static test_return_t version_string_test(memcached_st *memc __attribute__((unused)))
1794 {
1795 const char *version_string;
1796
1797 version_string= memcached_lib_version();
1798
1799 test_true(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
1800
1801 return TEST_SUCCESS;
1802 }
1803
1804 static test_return_t get_stats(memcached_st *memc)
1805 {
1806 char **stat_list;
1807 char **ptr;
1808 memcached_return_t rc;
1809 memcached_stat_st *memc_stat;
1810
1811 memc_stat= memcached_stat(memc, NULL, &rc);
1812 test_true(rc == MEMCACHED_SUCCESS);
1813
1814 test_true(rc == MEMCACHED_SUCCESS);
1815 test_true(memc_stat);
1816
1817 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
1818 {
1819 stat_list= memcached_stat_get_keys(memc, memc_stat+x, &rc);
1820 test_true(rc == MEMCACHED_SUCCESS);
1821 for (ptr= stat_list; *ptr; ptr++);
1822
1823 free(stat_list);
1824 }
1825
1826 memcached_stat_free(NULL, memc_stat);
1827
1828 return TEST_SUCCESS;
1829 }
1830
1831 static test_return_t add_host_test(memcached_st *memc)
1832 {
1833 unsigned int x;
1834 memcached_server_st *servers;
1835 memcached_return_t rc;
1836 char servername[]= "0.example.com";
1837
1838 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
1839 test_true(servers);
1840 test_true(1 == memcached_server_list_count(servers));
1841
1842 for (x= 2; x < 20; x++)
1843 {
1844 char buffer[SMALL_STRING_LEN];
1845
1846 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1847 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
1848 &rc);
1849 test_true(rc == MEMCACHED_SUCCESS);
1850 test_true(x == memcached_server_list_count(servers));
1851 }
1852
1853 rc= memcached_server_push(memc, servers);
1854 test_true(rc == MEMCACHED_SUCCESS);
1855 rc= memcached_server_push(memc, servers);
1856 test_true(rc == MEMCACHED_SUCCESS);
1857
1858 memcached_server_list_free(servers);
1859
1860 return TEST_SUCCESS;
1861 }
1862
1863 static memcached_return_t clone_test_callback(memcached_st *parent __attribute__((unused)), memcached_st *memc_clone __attribute__((unused)))
1864 {
1865 return MEMCACHED_SUCCESS;
1866 }
1867
1868 static memcached_return_t cleanup_test_callback(memcached_st *ptr __attribute__((unused)))
1869 {
1870 return MEMCACHED_SUCCESS;
1871 }
1872
1873 static test_return_t callback_test(memcached_st *memc)
1874 {
1875 /* Test User Data */
1876 {
1877 int x= 5;
1878 int *test_ptr;
1879 memcached_return_t rc;
1880
1881 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
1882 test_true(rc == MEMCACHED_SUCCESS);
1883 test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
1884 test_true(*test_ptr == x);
1885 }
1886
1887 /* Test Clone Callback */
1888 {
1889 memcached_clone_fn clone_cb= (memcached_clone_fn)clone_test_callback;
1890 void *clone_cb_ptr= *(void **)&clone_cb;
1891 void *temp_function= NULL;
1892 memcached_return_t rc;
1893
1894 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1895 clone_cb_ptr);
1896 test_true(rc == MEMCACHED_SUCCESS);
1897 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1898 test_true(temp_function == clone_cb_ptr);
1899 }
1900
1901 /* Test Cleanup Callback */
1902 {
1903 memcached_cleanup_fn cleanup_cb=
1904 (memcached_cleanup_fn)cleanup_test_callback;
1905 void *cleanup_cb_ptr= *(void **)&cleanup_cb;
1906 void *temp_function= NULL;
1907 memcached_return_t rc;
1908
1909 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1910 cleanup_cb_ptr);
1911 test_true(rc == MEMCACHED_SUCCESS);
1912 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1913 test_true(temp_function == cleanup_cb_ptr);
1914 }
1915
1916 return TEST_SUCCESS;
1917 }
1918
1919 /* We don't test the behavior itself, we test the switches */
1920 static test_return_t behavior_test(memcached_st *memc)
1921 {
1922 uint64_t value;
1923 uint32_t set= 1;
1924
1925 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1926 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1927 test_true(value == 1);
1928
1929 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1930 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1931 test_true(value == 1);
1932
1933 set= MEMCACHED_HASH_MD5;
1934 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1935 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1936 test_true(value == MEMCACHED_HASH_MD5);
1937
1938 set= 0;
1939
1940 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1941 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1942 test_true(value == 0);
1943
1944 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1945 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1946 test_true(value == 0);
1947
1948 set= MEMCACHED_HASH_DEFAULT;
1949 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1950 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1951 test_true(value == MEMCACHED_HASH_DEFAULT);
1952
1953 set= MEMCACHED_HASH_CRC;
1954 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1955 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1956 test_true(value == MEMCACHED_HASH_CRC);
1957
1958 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1959 test_true(value > 0);
1960
1961 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1962 test_true(value > 0);
1963
1964 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
1965 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, value + 1);
1966 test_true((value + 1) == memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
1967
1968 return TEST_SUCCESS;
1969 }
1970
1971 static test_return_t MEMCACHED_BEHAVIOR_CORK_test(memcached_st *memc)
1972 {
1973 memcached_return_t rc;
1974 bool set= true;
1975 bool value;
1976
1977 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CORK, set);
1978 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOT_SUPPORTED);
1979
1980 value= (bool)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_CORK);
1981
1982 if (rc == MEMCACHED_SUCCESS)
1983 {
1984 test_true((bool)value == set);
1985 }
1986 else
1987 {
1988 test_false((bool)value == set);
1989 }
1990
1991 return TEST_SUCCESS;
1992 }
1993
1994
1995 static test_return_t MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st *memc)
1996 {
1997 memcached_return_t rc;
1998 bool set= true;
1999 bool value;
2000
2001 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE, set);
2002 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOT_SUPPORTED);
2003
2004 value= (bool)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE);
2005
2006 if (rc == MEMCACHED_SUCCESS)
2007 {
2008 test_true((bool)value == set);
2009 }
2010 else
2011 {
2012 test_false((bool)value == set);
2013 }
2014
2015 return TEST_SUCCESS;
2016 }
2017
2018
2019 static test_return_t MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st *memc)
2020 {
2021 memcached_return_t rc;
2022 bool set= true;
2023 bool value;
2024
2025 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE, set);
2026 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOT_SUPPORTED);
2027
2028 value= (bool)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE);
2029
2030 if (rc == MEMCACHED_SUCCESS)
2031 {
2032 test_true((bool)value == set);
2033 }
2034 else
2035 {
2036 test_false((bool)value == set);
2037 }
2038
2039 return TEST_SUCCESS;
2040 }
2041
2042 static test_return_t fetch_all_results(memcached_st *memc)
2043 {
2044 memcached_return_t rc= MEMCACHED_SUCCESS;
2045 char return_key[MEMCACHED_MAX_KEY];
2046 size_t return_key_length;
2047 char *return_value;
2048 size_t return_value_length;
2049 uint32_t flags;
2050
2051 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2052 &return_value_length, &flags, &rc)))
2053 {
2054 test_true(return_value);
2055 test_true(rc == MEMCACHED_SUCCESS);
2056 free(return_value);
2057 }
2058
2059 return ((rc == MEMCACHED_END) || (rc == MEMCACHED_SUCCESS)) ? TEST_SUCCESS : TEST_FAILURE;
2060 }
2061
2062 /* Test case provided by Cal Haldenbrand */
2063 static test_return_t user_supplied_bug1(memcached_st *memc)
2064 {
2065 unsigned int setter= 1;
2066
2067 unsigned long long total= 0;
2068 uint32_t size= 0;
2069 char key[10];
2070 char randomstuff[6 * 1024];
2071 memcached_return_t rc;
2072
2073 memset(randomstuff, 0, 6 * 1024);
2074
2075 /* We just keep looking at the same values over and over */
2076 srandom(10);
2077
2078 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
2079 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2080
2081
2082 /* add key */
2083 for (uint32_t x= 0 ; total < 20 * 1024576 ; x++ )
2084 {
2085 unsigned int j= 0;
2086
2087 size= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2088 memset(randomstuff, 0, 6 * 1024);
2089 test_true(size < 6 * 1024); /* Being safe here */
2090
2091 for (j= 0 ; j < size ;j++)
2092 randomstuff[j] = (signed char) ((rand() % 26) + 97);
2093
2094 total += size;
2095 snprintf(key, sizeof(key), "%u", x);
2096 rc = memcached_set(memc, key, strlen(key),
2097 randomstuff, strlen(randomstuff), 10, 0);
2098 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
2099 /* If we fail, lets try again */
2100 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
2101 rc = memcached_set(memc, key, strlen(key),
2102 randomstuff, strlen(randomstuff), 10, 0);
2103 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
2104 }
2105
2106 return TEST_SUCCESS;
2107 }
2108
2109 /* Test case provided by Cal Haldenbrand */
2110 static test_return_t user_supplied_bug2(memcached_st *memc)
2111 {
2112 unsigned int setter;
2113 size_t total= 0;
2114
2115 setter= 1;
2116 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
2117 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2118 #ifdef NOT_YET
2119 setter = 20 * 1024576;
2120 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
2121 setter = 20 * 1024576;
2122 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
2123 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
2124 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
2125
2126 for (x= 0, errors= 0; total < 20 * 1024576 ; x++)
2127 #endif
2128
2129 for (uint32_t x= 0, errors= 0; total < 24576 ; x++)
2130 {
2131 memcached_return_t rc= MEMCACHED_SUCCESS;
2132 char buffer[SMALL_STRING_LEN];
2133 uint32_t flags= 0;
2134 size_t val_len= 0;
2135 char *getval;
2136
2137 memset(buffer, 0, SMALL_STRING_LEN);
2138
2139 snprintf(buffer, sizeof(buffer), "%u", x);
2140 getval= memcached_get(memc, buffer, strlen(buffer),
2141 &val_len, &flags, &rc);
2142 if (rc != MEMCACHED_SUCCESS)
2143 {
2144 if (rc == MEMCACHED_NOTFOUND)
2145 errors++;
2146 else
2147 {
2148 test_true(rc);
2149 }
2150
2151 continue;
2152 }
2153 total+= val_len;
2154 errors= 0;
2155 free(getval);
2156 }
2157
2158 return TEST_SUCCESS;
2159 }
2160
2161 /* Do a large mget() over all the keys we think exist */
2162 #define KEY_COUNT 3000 // * 1024576
2163 static test_return_t user_supplied_bug3(memcached_st *memc)
2164 {
2165 memcached_return_t rc;
2166 unsigned int setter;
2167 unsigned int x;
2168 char **keys;
2169 size_t key_lengths[KEY_COUNT];
2170
2171 setter= 1;
2172 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
2173 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2174 #ifdef NOT_YET
2175 setter = 20 * 1024576;
2176 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
2177 setter = 20 * 1024576;
2178 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
2179 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
2180 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
2181 #endif
2182
2183 keys= calloc(KEY_COUNT, sizeof(char *));
2184 test_true(keys);
2185 for (x= 0; x < KEY_COUNT; x++)
2186 {
2187 char buffer[30];
2188
2189 snprintf(buffer, 30, "%u", x);
2190 keys[x]= strdup(buffer);
2191 key_lengths[x]= strlen(keys[x]);
2192 }
2193
2194 rc= memcached_mget(memc, (const char **)keys, key_lengths, KEY_COUNT);
2195 test_true(rc == MEMCACHED_SUCCESS);
2196
2197 test_true(fetch_all_results(memc) == TEST_SUCCESS);
2198
2199 for (x= 0; x < KEY_COUNT; x++)
2200 free(keys[x]);
2201 free(keys);
2202
2203 return TEST_SUCCESS;
2204 }
2205
2206 /* Make sure we behave properly if server list has no values */
2207 static test_return_t user_supplied_bug4(memcached_st *memc)
2208 {
2209 memcached_return_t rc;
2210 const char *keys[]= {"fudge", "son", "food"};
2211 size_t key_length[]= {5, 3, 4};
2212 unsigned int x;
2213 uint32_t flags;
2214 char return_key[MEMCACHED_MAX_KEY];
2215 size_t return_key_length;
2216 char *return_value;
2217 size_t return_value_length;
2218
2219 /* Here we free everything before running a bunch of mget tests */
2220 memcached_servers_reset(memc);
2221
2222
2223 /* We need to empty the server before continueing test */
2224 rc= memcached_flush(memc, 0);
2225 test_true(rc == MEMCACHED_NO_SERVERS);
2226
2227 rc= memcached_mget(memc, keys, key_length, 3);
2228 test_true(rc == MEMCACHED_NO_SERVERS);
2229
2230 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2231 &return_value_length, &flags, &rc)) != NULL)
2232 {
2233 test_true(return_value);
2234 }
2235 test_true(!return_value);
2236 test_true(return_value_length == 0);
2237 test_true(rc == MEMCACHED_NO_SERVERS);
2238
2239 for (x= 0; x < 3; x++)
2240 {
2241 rc= memcached_set(memc, keys[x], key_length[x],
2242 keys[x], key_length[x],
2243 (time_t)50, (uint32_t)9);
2244 test_true(rc == MEMCACHED_NO_SERVERS);
2245 }
2246
2247 rc= memcached_mget(memc, keys, key_length, 3);
2248 test_true(rc == MEMCACHED_NO_SERVERS);
2249
2250 x= 0;
2251 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2252 &return_value_length, &flags, &rc)))
2253 {
2254 test_true(return_value);
2255 test_true(rc == MEMCACHED_SUCCESS);
2256 test_true(return_key_length == return_value_length);
2257 test_true(!memcmp(return_value, return_key, return_value_length));
2258 free(return_value);
2259 x++;
2260 }
2261
2262 return TEST_SUCCESS;
2263 }
2264
2265 #define VALUE_SIZE_BUG5 1048064
2266 static test_return_t user_supplied_bug5(memcached_st *memc)
2267 {
2268 memcached_return_t rc;
2269 const char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2270 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2271 char return_key[MEMCACHED_MAX_KEY];
2272 size_t return_key_length;
2273 char *value;
2274 size_t value_length;
2275 uint32_t flags;
2276 unsigned int count;
2277 unsigned int x;
2278 char insert_data[VALUE_SIZE_BUG5];
2279
2280 for (x= 0; x < VALUE_SIZE_BUG5; x++)
2281 insert_data[x]= (signed char)rand();
2282
2283 memcached_flush(memc, 0);
2284 value= memcached_get(memc, keys[0], key_length[0],
2285 &value_length, &flags, &rc);
2286 test_true(value == NULL);
2287 rc= memcached_mget(memc, keys, key_length, 4);
2288
2289 count= 0;
2290 while ((value= memcached_fetch(memc, return_key, &return_key_length,
2291 &value_length, &flags, &rc)))
2292 count++;
2293 test_true(count == 0);
2294
2295 for (x= 0; x < 4; x++)
2296 {
2297 rc= memcached_set(memc, keys[x], key_length[x],
2298 insert_data, VALUE_SIZE_BUG5,
2299 (time_t)0, (uint32_t)0);
2300 test_true(rc == MEMCACHED_SUCCESS);
2301 }
2302
2303 for (x= 0; x < 10; x++)
2304 {
2305 value= memcached_get(memc, keys[0], key_length[0],
2306 &value_length, &flags, &rc);
2307 test_true(value);
2308 free(value);
2309
2310 rc= memcached_mget(memc, keys, key_length, 4);
2311 count= 0;
2312 while ((value= memcached_fetch(memc, return_key, &return_key_length,
2313 &value_length, &flags, &rc)))
2314 {
2315 count++;
2316 free(value);
2317 }
2318 test_true(count == 4);
2319 }
2320
2321 return TEST_SUCCESS;
2322 }
2323
2324 static test_return_t user_supplied_bug6(memcached_st *memc)
2325 {
2326 memcached_return_t rc;
2327 const char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2328 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2329 char return_key[MEMCACHED_MAX_KEY];
2330 size_t return_key_length;
2331 char *value;
2332 size_t value_length;
2333 uint32_t flags;
2334 unsigned int count;
2335 unsigned int x;
2336 char insert_data[VALUE_SIZE_BUG5];
2337
2338 for (x= 0; x < VALUE_SIZE_BUG5; x++)
2339 insert_data[x]= (signed char)rand();
2340
2341 memcached_flush(memc, 0);
2342 value= memcached_get(memc, keys[0], key_length[0],
2343 &value_length, &flags, &rc);
2344 test_true(value == NULL);
2345 test_true(rc == MEMCACHED_NOTFOUND);
2346 rc= memcached_mget(memc, keys, key_length, 4);
2347 test_true(rc == MEMCACHED_SUCCESS);
2348
2349 count= 0;
2350 while ((value= memcached_fetch(memc, return_key, &return_key_length,
2351 &value_length, &flags, &rc)))
2352 count++;
2353 test_true(count == 0);
2354 test_true(rc == MEMCACHED_END);
2355
2356 for (x= 0; x < 4; x++)
2357 {
2358 rc= memcached_set(memc, keys[x], key_length[x],
2359 insert_data, VALUE_SIZE_BUG5,
2360 (time_t)0, (uint32_t)0);
2361 test_true(rc == MEMCACHED_SUCCESS);
2362 }
2363
2364 for (x= 0; x < 2; x++)
2365 {
2366 value= memcached_get(memc, keys[0], key_length[0],
2367 &value_length, &flags, &rc);
2368 test_true(value);
2369 free(value);
2370
2371 rc= memcached_mget(memc, keys, key_length, 4);
2372 test_true(rc == MEMCACHED_SUCCESS);
2373 count= 3;
2374 /* We test for purge of partial complete fetches */
2375 for (count= 3; count; count--)
2376 {
2377 value= memcached_fetch(memc, return_key, &return_key_length,
2378 &value_length, &flags, &rc);
2379 test_true(rc == MEMCACHED_SUCCESS);
2380 test_true(!(memcmp(value, insert_data, value_length)));
2381 test_true(value_length);
2382 free(value);
2383 }
2384 }
2385
2386 return TEST_SUCCESS;
2387 }
2388
2389 static test_return_t user_supplied_bug8(memcached_st *memc __attribute__((unused)))
2390 {
2391 memcached_return_t rc;
2392 memcached_st *mine;
2393 memcached_st *memc_clone;
2394
2395 memcached_server_st *servers;
2396 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";
2397
2398 servers= memcached_servers_parse(server_list);
2399 test_true(servers);
2400
2401 mine= memcached_create(NULL);
2402 rc= memcached_server_push(mine, servers);
2403 test_true(rc == MEMCACHED_SUCCESS);
2404 memcached_server_list_free(servers);
2405
2406 test_true(mine);
2407 memc_clone= memcached_clone(NULL, mine);
2408
2409 memcached_quit(mine);
2410 memcached_quit(memc_clone);
2411
2412
2413 memcached_free(mine);
2414 memcached_free(memc_clone);
2415
2416 return TEST_SUCCESS;
2417 }
2418
2419 /* Test flag store/retrieve */
2420 static test_return_t user_supplied_bug7(memcached_st *memc)
2421 {
2422 memcached_return_t rc;
2423 const char *keys= "036790384900";
2424 size_t key_length= strlen(keys);
2425 char return_key[MEMCACHED_MAX_KEY];
2426 size_t return_key_length;
2427 char *value;
2428 size_t value_length;
2429 uint32_t flags;
2430 unsigned int x;
2431 char insert_data[VALUE_SIZE_BUG5];
2432
2433 for (x= 0; x < VALUE_SIZE_BUG5; x++)
2434 insert_data[x]= (signed char)rand();
2435
2436 memcached_flush(memc, 0);
2437
2438 flags= 245;
2439 rc= memcached_set(memc, keys, key_length,
2440 insert_data, VALUE_SIZE_BUG5,
2441 (time_t)0, flags);
2442 test_true(rc == MEMCACHED_SUCCESS);
2443
2444 flags= 0;
2445 value= memcached_get(memc, keys, key_length,
2446 &value_length, &flags, &rc);
2447 test_true(flags == 245);
2448 test_true(value);
2449 free(value);
2450
2451 rc= memcached_mget(memc, &keys, &key_length, 1);
2452
2453 flags= 0;
2454 value= memcached_fetch(memc, return_key, &return_key_length,
2455 &value_length, &flags, &rc);
2456 test_true(flags == 245);
2457 test_true(value);
2458 free(value);
2459
2460
2461 return TEST_SUCCESS;
2462 }
2463
2464 static test_return_t user_supplied_bug9(memcached_st *memc)
2465 {
2466 memcached_return_t rc;
2467 const char *keys[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2468 size_t key_length[3];
2469 unsigned int x;
2470 uint32_t flags;
2471 unsigned count= 0;
2472
2473 char return_key[MEMCACHED_MAX_KEY];
2474 size_t return_key_length;
2475 char *return_value;
2476 size_t return_value_length;
2477
2478
2479 key_length[0]= strlen("UDATA:edevil@sapo.pt");
2480 key_length[1]= strlen("fudge&*@#");
2481 key_length[2]= strlen("for^#@&$not");
2482
2483
2484 for (x= 0; x < 3; x++)
2485 {
2486 rc= memcached_set(memc, keys[x], key_length[x],
2487 keys[x], key_length[x],
2488 (time_t)50, (uint32_t)9);
2489 test_true(rc == MEMCACHED_SUCCESS);
2490 }
2491
2492 rc= memcached_mget(memc, keys, key_length, 3);
2493 test_true(rc == MEMCACHED_SUCCESS);
2494
2495 /* We need to empty the server before continueing test */
2496 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2497 &return_value_length, &flags, &rc)) != NULL)
2498 {
2499 test_true(return_value);
2500 free(return_value);
2501 count++;
2502 }
2503 test_true(count == 3);
2504
2505 return TEST_SUCCESS;
2506 }
2507
2508 /* We are testing with aggressive timeout to get failures */
2509 static test_return_t user_supplied_bug10(memcached_st *memc)
2510 {
2511 const char *key= "foo";
2512 char *value;
2513 size_t value_length= 512;
2514 unsigned int x;
2515 size_t key_len= 3;
2516 memcached_return_t rc;
2517 unsigned int set= 1;
2518 memcached_st *mclone= memcached_clone(NULL, memc);
2519 int32_t timeout;
2520
2521 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
2522 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
2523 timeout= 2;
2524 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
2525 (uint64_t)timeout);
2526
2527 value = (char*)malloc(value_length * sizeof(char));
2528
2529 for (x= 0; x < value_length; x++)
2530 value[x]= (char) (x % 127);
2531
2532 for (x= 1; x <= 100000; ++x)
2533 {
2534 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
2535
2536 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE ||
2537 rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT);
2538
2539 if (rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_TIMEOUT)
2540 x--;
2541 }
2542
2543 free(value);
2544 memcached_free(mclone);
2545
2546 return TEST_SUCCESS;
2547 }
2548
2549 /*
2550 We are looking failures in the async protocol
2551 */
2552 static test_return_t user_supplied_bug11(memcached_st *memc)
2553 {
2554 const char *key= "foo";
2555 char *value;
2556 size_t value_length= 512;
2557 unsigned int x;
2558 size_t key_len= 3;
2559 memcached_return_t rc;
2560 unsigned int set= 1;
2561 int32_t timeout;
2562 memcached_st *mclone= memcached_clone(NULL, memc);
2563
2564 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
2565 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
2566 timeout= -1;
2567 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
2568 (size_t)timeout);
2569
2570 timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
2571
2572 test_true(timeout == -1);
2573
2574 value = (char*)malloc(value_length * sizeof(char));
2575
2576 for (x= 0; x < value_length; x++)
2577 value[x]= (char) (x % 127);
2578
2579 for (x= 1; x <= 100000; ++x)
2580 {
2581 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
2582 }
2583
2584 free(value);
2585 memcached_free(mclone);
2586
2587 return TEST_SUCCESS;
2588 }
2589
2590 /*
2591 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2592 */
2593 static test_return_t user_supplied_bug12(memcached_st *memc)
2594 {
2595 memcached_return_t rc;
2596 uint32_t flags;
2597 size_t value_length;
2598 char *value;
2599 uint64_t number_value;
2600
2601 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
2602 &value_length, &flags, &rc);
2603 test_true(value == NULL);
2604 test_true(rc == MEMCACHED_NOTFOUND);
2605
2606 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
2607 1, &number_value);
2608
2609 test_true(value == NULL);
2610 /* The binary protocol will set the key if it doesn't exist */
2611 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1)
2612 {
2613 test_true(rc == MEMCACHED_SUCCESS);
2614 }
2615 else
2616 {
2617 test_true(rc == MEMCACHED_NOTFOUND);
2618 }
2619
2620 rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2621
2622 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
2623 &value_length, &flags, &rc);
2624 test_true(value);
2625 test_true(rc == MEMCACHED_SUCCESS);
2626 free(value);
2627
2628 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
2629 1, &number_value);
2630 test_true(number_value == 2);
2631 test_true(rc == MEMCACHED_SUCCESS);
2632
2633 return TEST_SUCCESS;
2634 }
2635
2636 /*
2637 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2638 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2639 */
2640 static test_return_t user_supplied_bug13(memcached_st *memc)
2641 {
2642 char key[] = "key34567890";
2643 char *overflow;
2644 memcached_return_t rc;
2645 size_t overflowSize;
2646
2647 char commandFirst[]= "set key34567890 0 0 ";
2648 char commandLast[] = " \r\n"; /* first line of command sent to server */
2649 size_t commandLength;
2650 size_t testSize;
2651
2652 commandLength = strlen(commandFirst) + strlen(commandLast) + 4; /* 4 is number of characters in size, probably 8196 */
2653
2654 overflowSize = MEMCACHED_MAX_BUFFER - commandLength;
2655
2656 for (testSize= overflowSize - 1; testSize < overflowSize + 1; testSize++)
2657 {
2658 overflow= malloc(testSize);
2659 test_true(overflow != NULL);
2660
2661 memset(overflow, 'x', testSize);
2662 rc= memcached_set(memc, key, strlen(key),
2663 overflow, testSize, 0, 0);
2664 test_true(rc == MEMCACHED_SUCCESS);
2665 free(overflow);
2666 }
2667
2668 return TEST_SUCCESS;
2669 }
2670
2671
2672 /*
2673 Test values of many different sizes
2674 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2675 set key34567890 0 0 8169 \r\n
2676 is sent followed by buffer of size 8169, followed by 8169
2677 */
2678 static test_return_t user_supplied_bug14(memcached_st *memc)
2679 {
2680 size_t setter= 1;
2681 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2682 memcached_return_t rc;
2683 const char *key= "foo";
2684 char *value;
2685 size_t value_length= 18000;
2686 char *string;
2687 size_t string_length;
2688 uint32_t flags;
2689 unsigned int x;
2690 size_t current_length;
2691
2692 value = (char*)malloc(value_length);
2693 test_true(value);
2694
2695 for (x= 0; x < value_length; x++)
2696 value[x] = (char) (x % 127);
2697
2698 for (current_length= 0; current_length < value_length; current_length++)
2699 {
2700 rc= memcached_set(memc, key, strlen(key),
2701 value, current_length,
2702 (time_t)0, (uint32_t)0);
2703 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
2704
2705 string= memcached_get(memc, key, strlen(key),
2706 &string_length, &flags, &rc);
2707
2708 test_true(rc == MEMCACHED_SUCCESS);
2709 test_true(string_length == current_length);
2710 test_true(!memcmp(string, value, string_length));
2711
2712 free(string);
2713 }
2714
2715 free(value);
2716
2717 return TEST_SUCCESS;
2718 }
2719
2720 /*
2721 Look for zero length value problems
2722 */
2723 static test_return_t user_supplied_bug15(memcached_st *memc)
2724 {
2725 uint32_t x;
2726 memcached_return_t rc;
2727 const char *key= "mykey";
2728 char *value;
2729 size_t length;
2730 uint32_t flags;
2731
2732 for (x= 0; x < 2; x++)
2733 {
2734 rc= memcached_set(memc, key, strlen(key),
2735 NULL, 0,
2736 (time_t)0, (uint32_t)0);
2737
2738 test_true(rc == MEMCACHED_SUCCESS);
2739
2740 value= memcached_get(memc, key, strlen(key),
2741 &length, &flags, &rc);
2742
2743 test_true(rc == MEMCACHED_SUCCESS);
2744 test_true(value == NULL);
2745 test_true(length == 0);
2746 test_true(flags == 0);
2747
2748 value= memcached_get(memc, key, strlen(key),
2749 &length, &flags, &rc);
2750
2751 test_true(rc == MEMCACHED_SUCCESS);
2752 test_true(value == NULL);
2753 test_true(length == 0);
2754 test_true(flags == 0);
2755 }
2756
2757 return TEST_SUCCESS;
2758 }
2759
2760 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2761 static test_return_t user_supplied_bug16(memcached_st *memc)
2762 {
2763 memcached_return_t rc;
2764 const char *key= "mykey";
2765 char *value;
2766 size_t length;
2767 uint32_t flags;
2768
2769 rc= memcached_set(memc, key, strlen(key),
2770 NULL, 0,
2771 (time_t)0, UINT32_MAX);
2772
2773 test_true(rc == MEMCACHED_SUCCESS);
2774
2775 value= memcached_get(memc, key, strlen(key),
2776 &length, &flags, &rc);
2777
2778 test_true(rc == MEMCACHED_SUCCESS);
2779 test_true(value == NULL);
2780 test_true(length == 0);
2781 test_true(flags == UINT32_MAX);
2782
2783 return TEST_SUCCESS;
2784 }
2785
2786 #ifndef __sun
2787 /* Check the validity of chinese key*/
2788 static test_return_t user_supplied_bug17(memcached_st *memc)
2789 {
2790 memcached_return_t rc;
2791 const char *key= "豆瓣";
2792 const char *value="我们在炎热抑郁的夏天无法停止豆瓣";
2793 char *value2;
2794 size_t length;
2795 uint32_t flags;
2796
2797 rc= memcached_set(memc, key, strlen(key),
2798 value, strlen(value),
2799 (time_t)0, 0);
2800
2801 test_true(rc == MEMCACHED_SUCCESS);
2802
2803 value2= memcached_get(memc, key, strlen(key),
2804 &length, &flags, &rc);
2805
2806 test_true(length==strlen(value));
2807 test_true(rc == MEMCACHED_SUCCESS);
2808 test_true(memcmp(value, value2, length)==0);
2809 free(value2);
2810
2811 return TEST_SUCCESS;
2812 }
2813 #endif
2814
2815 /*
2816 From Andrei on IRC
2817 */
2818
2819 static test_return_t user_supplied_bug19(memcached_st *not_used)
2820 {
2821 memcached_st *memc;
2822 const memcached_server_st *server;
2823 memcached_return_t res;
2824
2825 (void)not_used;
2826
2827 memc= memcached_create(NULL);
2828 memcached_server_add_with_weight(memc, "localhost", 11311, 100);
2829 memcached_server_add_with_weight(memc, "localhost", 11312, 100);
2830
2831 server= memcached_server_by_key(memc, "a", 1, &res);
2832
2833 memcached_free(memc);
2834
2835 return TEST_SUCCESS;
2836 }
2837
2838 /* CAS test from Andei */
2839 static test_return_t user_supplied_bug20(memcached_st *memc)
2840 {
2841 memcached_return_t status;
2842 memcached_result_st *result, result_obj;
2843 const char *key = "abc";
2844 size_t key_len = strlen("abc");
2845 const char *value = "foobar";
2846 size_t value_len = strlen(value);
2847
2848 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
2849
2850 status = memcached_set(memc, key, key_len, value, value_len, (time_t)0, (uint32_t)0);
2851 test_true(status == MEMCACHED_SUCCESS);
2852
2853 status = memcached_mget(memc, &key, &key_len, 1);
2854 test_true(status == MEMCACHED_SUCCESS);
2855
2856 result= memcached_result_create(memc, &result_obj);
2857 test_true(result);
2858
2859 memcached_result_create(memc, &result_obj);
2860 result= memcached_fetch_result(memc, &result_obj, &status);
2861
2862 test_true(result);
2863 test_true(status == MEMCACHED_SUCCESS);
2864
2865 memcached_result_free(result);
2866
2867 return TEST_SUCCESS;
2868 }
2869
2870 #include "ketama_test_cases.h"
2871 static test_return_t user_supplied_bug18(memcached_st *trash)
2872 {
2873 memcached_return_t rc;
2874 uint64_t value;
2875 int x;
2876 memcached_server_st *server_pool;
2877 memcached_st *memc;
2878
2879 (void)trash;
2880
2881 memc= memcached_create(NULL);
2882 test_true(memc);
2883
2884 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2885 test_true(rc == MEMCACHED_SUCCESS);
2886
2887 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2888 test_true(value == 1);
2889
2890 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2891 test_true(rc == MEMCACHED_SUCCESS);
2892
2893 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2894 test_true(value == MEMCACHED_HASH_MD5);
2895
2896 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");
2897 memcached_server_push(memc, server_pool);
2898
2899 /* verify that the server list was parsed okay. */
2900 test_true(memcached_server_count(memc) == 8);
2901 test_true(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
2902 test_true(server_pool[0].port == 11211);
2903 test_true(server_pool[0].weight == 600);
2904 test_true(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
2905 test_true(server_pool[2].port == 11211);
2906 test_true(server_pool[2].weight == 200);
2907 test_true(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
2908 test_true(server_pool[7].port == 11211);
2909 test_true(server_pool[7].weight == 100);
2910
2911 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2912 * us test the boundary wraparound.
2913 */
2914 test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
2915
2916 /* verify the standard ketama set. */
2917 for (x= 0; x < 99; x++)
2918 {
2919 uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
2920
2921 memcached_server_instance_st instance=
2922 memcached_server_instance_by_position(memc, server_idx);
2923
2924 const char *hostname = memcached_server_name(instance);
2925 test_strcmp(hostname, ketama_test_cases[x].server);
2926 }
2927
2928 memcached_server_list_free(server_pool);
2929 memcached_free(memc);
2930
2931 return TEST_SUCCESS;
2932 }
2933
2934 /* Large mget() of missing keys with binary proto
2935 *
2936 * If many binary quiet commands (such as getq's in an mget) fill the output
2937 * buffer and the server chooses not to respond, memcached_flush hangs. See
2938 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2939 */
2940
2941 /* sighandler_t function that always asserts false */
2942 static void fail(int unused __attribute__((unused)))
2943 {
2944 assert(0);
2945 }
2946
2947
2948 static test_return_t _user_supplied_bug21(memcached_st* memc, size_t key_count)
2949 {
2950 memcached_return_t rc;
2951 unsigned int x;
2952 char **keys;
2953 size_t* key_lengths;
2954 void (*oldalarm)(int);
2955 memcached_st *memc_clone;
2956
2957 memc_clone= memcached_clone(NULL, memc);
2958 test_true(memc_clone);
2959
2960 /* only binproto uses getq for mget */
2961 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
2962
2963 /* empty the cache to ensure misses (hence non-responses) */
2964 rc= memcached_flush(memc_clone, 0);
2965 test_true(rc == MEMCACHED_SUCCESS);
2966
2967 key_lengths= calloc(key_count, sizeof(size_t));
2968 keys= calloc(key_count, sizeof(char *));
2969 test_true(keys);
2970 for (x= 0; x < key_count; x++)
2971 {
2972 char buffer[30];
2973
2974 snprintf(buffer, 30, "%u", x);
2975 keys[x]= strdup(buffer);
2976 key_lengths[x]= strlen(keys[x]);
2977 }
2978
2979 oldalarm= signal(SIGALRM, fail);
2980 alarm(5);
2981
2982 rc= memcached_mget(memc_clone, (const char **)keys, key_lengths, key_count);
2983 test_true(rc == MEMCACHED_SUCCESS);
2984
2985 alarm(0);
2986 signal(SIGALRM, oldalarm);
2987
2988 test_true(fetch_all_results(memc) == TEST_SUCCESS);
2989
2990 for (x= 0; x < key_count; x++)
2991 free(keys[x]);
2992 free(keys);
2993 free(key_lengths);
2994
2995 memcached_free(memc_clone);
2996
2997 return TEST_SUCCESS;
2998 }
2999
3000 static test_return_t user_supplied_bug21(memcached_st *memc)
3001 {
3002 test_return_t test_rc;
3003 test_rc= pre_binary(memc);
3004
3005 if (test_rc != TEST_SUCCESS)
3006 return test_rc;
3007
3008 test_return_t rc;
3009
3010 /* should work as of r580 */
3011 rc= _user_supplied_bug21(memc, 10);
3012 test_true(rc == TEST_SUCCESS);
3013
3014 /* should fail as of r580 */
3015 rc= _user_supplied_bug21(memc, 1000);
3016 test_true(rc == TEST_SUCCESS);
3017
3018 return TEST_SUCCESS;
3019 }
3020
3021 static test_return_t auto_eject_hosts(memcached_st *trash)
3022 {
3023 (void) trash;
3024 memcached_server_instance_st instance;
3025
3026 memcached_return_t rc;
3027 memcached_st *memc= memcached_create(NULL);
3028 test_true(memc);
3029
3030 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3031 test_true(rc == MEMCACHED_SUCCESS);
3032
3033 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3034 test_true(value == 1);
3035
3036 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3037 test_true(rc == MEMCACHED_SUCCESS);
3038
3039 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3040 test_true(value == MEMCACHED_HASH_MD5);
3041
3042 /* server should be removed when in delay */
3043 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS, 1);
3044 test_true(rc == MEMCACHED_SUCCESS);
3045
3046 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
3047 test_true(value == 1);
3048
3049 memcached_server_st *server_pool;
3050 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");
3051 memcached_server_push(memc, server_pool);
3052
3053 /* verify that the server list was parsed okay. */
3054 test_true(memcached_server_count(memc) == 8);
3055 test_true(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
3056 test_true(server_pool[0].port == 11211);
3057 test_true(server_pool[0].weight == 600);
3058 test_true(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
3059 test_true(server_pool[2].port == 11211);
3060 test_true(server_pool[2].weight == 200);
3061 test_true(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
3062 test_true(server_pool[7].port == 11211);
3063 test_true(server_pool[7].weight == 100);
3064
3065 instance= memcached_server_instance_by_position(memc, 2);
3066 ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) + 15;
3067 memc->next_distribution_rebuild= time(NULL) - 1;
3068
3069 /*
3070 This would not work if there were only two hosts.
3071 */
3072 for (size_t x= 0; x < 99; x++)
3073 {
3074 memcached_autoeject(memc);
3075 uint32_t server_idx= memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
3076 test_true(server_idx != 2);
3077 }
3078
3079 /* and re-added when it's back. */
3080 ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) - 1;
3081 memc->next_distribution_rebuild= time(NULL) - 1;
3082 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION,
3083 memc->distribution);
3084 for (size_t x= 0; x < 99; x++)
3085 {
3086 uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
3087 // We re-use instance from above.
3088 instance=
3089 memcached_server_instance_by_position(memc, server_idx);
3090 const char *hostname = memcached_server_name(instance);
3091 test_true(strcmp(hostname, ketama_test_cases[x].server) == 0);
3092 }
3093
3094 memcached_server_list_free(server_pool);
3095 memcached_free(memc);
3096
3097 return TEST_SUCCESS;
3098 }
3099
3100 static test_return_t output_ketama_weighted_keys(memcached_st *trash)
3101 {
3102 (void) trash;
3103
3104 memcached_return_t rc;
3105 memcached_st *memc= memcached_create(NULL);
3106 test_true(memc);
3107
3108
3109 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3110 test_true(rc == MEMCACHED_SUCCESS);
3111
3112 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3113 test_true(value == 1);
3114
3115 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3116 test_true(rc == MEMCACHED_SUCCESS);
3117
3118 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3119 test_true(value == MEMCACHED_HASH_MD5);
3120
3121
3122 test_true(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) == MEMCACHED_SUCCESS);
3123
3124 memcached_server_st *server_pool;
3125 server_pool = memcached_servers_parse("10.0.1.1:11211,10.0.1.2:11211,10.0.1.3:11211,10.0.1.4:11211,10.0.1.5:11211,10.0.1.6:11211,10.0.1.7:11211,10.0.1.8:11211,192.168.1.1:11211,192.168.100.1:11211");
3126 memcached_server_push(memc, server_pool);
3127
3128 // @todo this needs to be refactored to actually test something.
3129 #if 0
3130 FILE *fp;
3131 if ((fp = fopen("ketama_keys.txt", "w")))
3132 {
3133 // noop
3134 } else {
3135 printf("cannot write to file ketama_keys.txt");
3136 return TEST_FAILURE;
3137 }
3138
3139 for (int x= 0; x < 10000; x++)
3140 {
3141 char key[10];
3142 sprintf(key, "%d", x);
3143
3144 uint32_t server_idx = memcached_generate_hash(memc, key, strlen(key));
3145 char *hostname = memc->hosts[server_idx].hostname;
3146 in_port_t port = memc->hosts[server_idx].port;
3147 fprintf(fp, "key %s is on host /%s:%u\n", key, hostname, port);
3148 memcached_server_instance_st instance=
3149 memcached_server_instance_by_position(memc, host_index);
3150 }
3151 fclose(fp);
3152 #endif
3153 memcached_server_list_free(server_pool);
3154 memcached_free(memc);
3155
3156 return TEST_SUCCESS;
3157 }
3158
3159
3160 static test_return_t result_static(memcached_st *memc)
3161 {
3162 memcached_result_st result;
3163 memcached_result_st *result_ptr;
3164
3165 result_ptr= memcached_result_create(memc, &result);
3166 test_true(result.options.is_allocated == false);
3167 test_true(memcached_is_initialized(&result) == true);
3168 test_true(result_ptr);
3169 test_true(result_ptr == &result);
3170
3171 memcached_result_free(&result);
3172
3173 test_true(result.options.is_allocated == false);
3174 test_true(memcached_is_initialized(&result) == false);
3175
3176 return TEST_SUCCESS;
3177 }
3178
3179 static test_return_t result_alloc(memcached_st *memc)
3180 {
3181 memcached_result_st *result_ptr;
3182
3183 result_ptr= memcached_result_create(memc, NULL);
3184 test_true(result_ptr);
3185 test_true(result_ptr->options.is_allocated == true);
3186 test_true(memcached_is_initialized(result_ptr) == true);
3187 memcached_result_free(result_ptr);
3188
3189 return TEST_SUCCESS;
3190 }
3191
3192 static test_return_t string_static_null(memcached_st *memc)
3193 {
3194 memcached_string_st string;
3195 memcached_string_st *string_ptr;
3196
3197 string_ptr= memcached_string_create(memc, &string, 0);
3198 test_true(string.options.is_initialized == true);
3199 test_true(string_ptr);
3200
3201 /* The following two better be the same! */
3202 test_true(memcached_is_allocated(string_ptr) == false);
3203 test_true(memcached_is_allocated(&string) == false);
3204 test_true(&string == string_ptr);
3205
3206 test_true(string.options.is_initialized == true);
3207 test_true(memcached_is_initialized(&string) == true);
3208 memcached_string_free(&string);
3209 test_true(memcached_is_initialized(&string) == false);
3210
3211 return TEST_SUCCESS;
3212 }
3213
3214 static test_return_t string_alloc_null(memcached_st *memc)
3215 {
3216 memcached_string_st *string;
3217
3218 string= memcached_string_create(memc, NULL, 0);
3219 test_true(string);
3220 test_true(memcached_is_allocated(string) == true);
3221 test_true(memcached_is_initialized(string) == true);
3222 memcached_string_free(string);
3223
3224 return TEST_SUCCESS;
3225 }
3226
3227 static test_return_t string_alloc_with_size(memcached_st *memc)
3228 {
3229 memcached_string_st *string;
3230
3231 string= memcached_string_create(memc, NULL, 1024);
3232 test_true(string);
3233 test_true(memcached_is_allocated(string) == true);
3234 test_true(memcached_is_initialized(string) == true);
3235 memcached_string_free(string);
3236
3237 return TEST_SUCCESS;
3238 }
3239
3240 static test_return_t string_alloc_with_size_toobig(memcached_st *memc)
3241 {
3242 memcached_string_st *string;
3243
3244 string= memcached_string_create(memc, NULL, SIZE_MAX);
3245 test_true(string == NULL);
3246
3247 return TEST_SUCCESS;
3248 }
3249
3250 static test_return_t string_alloc_append(memcached_st *memc)
3251 {
3252 unsigned int x;
3253 char buffer[SMALL_STRING_LEN];
3254 memcached_string_st *string;
3255
3256 /* Ring the bell! */
3257 memset(buffer, 6, SMALL_STRING_LEN);
3258
3259 string= memcached_string_create(memc, NULL, 100);
3260 test_true(string);
3261 test_true(memcached_is_allocated(string) == true);
3262 test_true(memcached_is_initialized(string) == true);
3263
3264 for (x= 0; x < 1024; x++)
3265 {
3266 memcached_return_t rc;
3267 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
3268 test_true(rc == MEMCACHED_SUCCESS);
3269 }
3270 test_true(memcached_is_allocated(string) == true);
3271 memcached_string_free(string);
3272
3273 return TEST_SUCCESS;
3274 }
3275
3276 static test_return_t string_alloc_append_toobig(memcached_st *memc)
3277 {
3278 memcached_return_t rc;
3279 unsigned int x;
3280 char buffer[SMALL_STRING_LEN];
3281 memcached_string_st *string;
3282
3283 /* Ring the bell! */
3284 memset(buffer, 6, SMALL_STRING_LEN);
3285
3286 string= memcached_string_create(memc, NULL, 100);
3287 test_true(string);
3288 test_true(memcached_is_allocated(string) == true);
3289 test_true(memcached_is_initialized(string) == true);
3290
3291 for (x= 0; x < 1024; x++)
3292 {
3293 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
3294 test_true(rc == MEMCACHED_SUCCESS);
3295 }
3296 rc= memcached_string_append(string, buffer, SIZE_MAX);
3297 test_true(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
3298 test_true(memcached_is_allocated(string) == true);
3299 memcached_string_free(string);
3300
3301 return TEST_SUCCESS;
3302 }
3303
3304 static test_return_t cleanup_pairs(memcached_st *memc __attribute__((unused)))
3305 {
3306 pairs_free(global_pairs);
3307
3308 return TEST_SUCCESS;
3309 }
3310
3311 static test_return_t generate_pairs(memcached_st *memc __attribute__((unused)))
3312 {
3313 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
3314 global_count= GLOBAL_COUNT;
3315
3316 for (size_t x= 0; x < global_count; x++)
3317 {
3318 global_keys[x]= global_pairs[x].key;
3319 global_keys_length[x]= global_pairs[x].key_length;
3320 }
3321
3322 return TEST_SUCCESS;
3323 }
3324
3325 static test_return_t generate_large_pairs(memcached_st *memc __attribute__((unused)))
3326 {
3327 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
3328 global_count= GLOBAL2_COUNT;
3329
3330 for (size_t x= 0; x < global_count; x++)
3331 {
3332 global_keys[x]= global_pairs[x].key;
3333 global_keys_length[x]= global_pairs[x].key_length;
3334 }
3335
3336 return TEST_SUCCESS;
3337 }
3338
3339 static test_return_t generate_data(memcached_st *memc)
3340 {
3341 execute_set(memc, global_pairs, global_count);
3342
3343 return TEST_SUCCESS;
3344 }
3345
3346 static test_return_t generate_data_with_stats(memcached_st *memc)
3347 {
3348 memcached_stat_st *stat_p;
3349 memcached_return_t rc;
3350 uint32_t host_index= 0;
3351 execute_set(memc, global_pairs, global_count);
3352
3353 //TODO: hosts used size stats
3354 stat_p= memcached_stat(memc, NULL, &rc);
3355 test_true(stat_p);
3356
3357 for (host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
3358 {
3359 /* This test was changes so that "make test" would work properlly */
3360 #ifdef DEBUG
3361 memcached_server_instance_st instance=
3362 memcached_server_instance_by_position(memc, host_index);
3363
3364 printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes);
3365 #endif
3366 test_true((unsigned long long)(stat_p + host_index)->bytes);
3367 }
3368
3369 memcached_stat_free(NULL, stat_p);
3370
3371 return TEST_SUCCESS;
3372 }
3373 static test_return_t generate_buffer_data(memcached_st *memc)
3374 {
3375 size_t latch= 0;
3376
3377 latch= 1;
3378 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
3379 generate_data(memc);
3380
3381 return TEST_SUCCESS;
3382 }
3383
3384 static test_return_t get_read_count(memcached_st *memc)
3385 {
3386 memcached_return_t rc;
3387 memcached_st *memc_clone;
3388
3389 memc_clone= memcached_clone(NULL, memc);
3390 test_true(memc_clone);
3391
3392 memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
3393
3394 {
3395 char *return_value;
3396 size_t return_value_length;
3397 uint32_t flags;
3398 uint32_t count;
3399
3400 for (size_t x= count= 0; x < global_count; x++)
3401 {
3402 return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
3403 &return_value_length, &flags, &rc);
3404 if (rc == MEMCACHED_SUCCESS)
3405 {
3406 count++;
3407 if (return_value)
3408 free(return_value);
3409 }
3410 }
3411 }
3412
3413 memcached_free(memc_clone);
3414
3415 return TEST_SUCCESS;
3416 }
3417
3418 static test_return_t get_read(memcached_st *memc)
3419 {
3420 memcached_return_t rc;
3421
3422 {
3423 char *return_value;
3424 size_t return_value_length;
3425 uint32_t flags;
3426
3427 for (size_t x= 0; x < global_count; x++)
3428 {
3429 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
3430 &return_value_length, &flags, &rc);
3431 /*
3432 test_true(return_value);
3433 test_true(rc == MEMCACHED_SUCCESS);
3434 */
3435 if (rc == MEMCACHED_SUCCESS && return_value)
3436 free(return_value);
3437 }
3438 }
3439
3440 return TEST_SUCCESS;
3441 }
3442
3443 static test_return_t mget_read(memcached_st *memc)
3444 {
3445 memcached_return_t rc;
3446
3447 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
3448 test_true(rc == MEMCACHED_SUCCESS);
3449 test_true(fetch_all_results(memc) == TEST_SUCCESS);
3450
3451 return TEST_SUCCESS;
3452 }
3453
3454 static test_return_t mget_read_result(memcached_st *memc)
3455 {
3456 memcached_return_t rc;
3457
3458 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
3459 test_true(rc == MEMCACHED_SUCCESS);
3460 /* Turn this into a help function */
3461 {
3462 memcached_result_st results_obj;
3463 memcached_result_st *results;
3464
3465 results= memcached_result_create(memc, &results_obj);
3466
3467 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
3468 {
3469 test_true(results);
3470 test_true(rc == MEMCACHED_SUCCESS);
3471 }
3472
3473 memcached_result_free(&results_obj);
3474 }
3475
3476 return TEST_SUCCESS;
3477 }
3478
3479 static test_return_t mget_read_function(memcached_st *memc)
3480 {
3481 memcached_return_t rc;
3482 size_t counter;
3483 memcached_execute_fn callbacks[1];
3484
3485 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
3486 test_true(rc == MEMCACHED_SUCCESS);
3487
3488 callbacks[0]= &callback_counter;
3489 counter= 0;
3490 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
3491
3492 return TEST_SUCCESS;
3493 }
3494
3495 static test_return_t delete_generate(memcached_st *memc)
3496 {
3497 for (size_t x= 0; x < global_count; x++)
3498 {
3499 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
3500 }
3501
3502 return TEST_SUCCESS;
3503 }
3504
3505 static test_return_t delete_buffer_generate(memcached_st *memc)
3506 {
3507 uint64_t latch= 0;
3508
3509 latch= 1;
3510 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
3511
3512 for (size_t x= 0; x < global_count; x++)
3513 {
3514 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
3515 }
3516
3517 return TEST_SUCCESS;
3518 }
3519
3520 static test_return_t add_host_test1(memcached_st *memc)
3521 {
3522 memcached_return_t rc;
3523 char servername[]= "0.example.com";
3524 memcached_server_st *servers;
3525
3526 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
3527 test_true(servers);
3528 test_true(1 == memcached_server_list_count(servers));
3529
3530 for (size_t x= 2; x < 20; x++)
3531 {
3532 char buffer[SMALL_STRING_LEN];
3533
3534 snprintf(buffer, SMALL_STRING_LEN, "%zu.example.com", 400+x);
3535 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
3536 &rc);
3537 test_true(rc == MEMCACHED_SUCCESS);
3538 test_true(x == memcached_server_list_count(servers));
3539 }
3540
3541 rc= memcached_server_push(memc, servers);
3542 test_true(rc == MEMCACHED_SUCCESS);
3543 rc= memcached_server_push(memc, servers);
3544 test_true(rc == MEMCACHED_SUCCESS);
3545
3546 memcached_server_list_free(servers);
3547
3548 return TEST_SUCCESS;
3549 }
3550
3551 static test_return_t pre_nonblock(memcached_st *memc)
3552 {
3553 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3554
3555 return TEST_SUCCESS;
3556 }
3557
3558 static test_return_t pre_cork(memcached_st *memc)
3559 {
3560 memcached_return_t rc;
3561 bool set= true;
3562
3563 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CORK, set);
3564
3565 #ifdef __APPLE__
3566 return TEST_SKIPPED;
3567 #endif
3568
3569 if (rc == MEMCACHED_SUCCESS)
3570 return TEST_SUCCESS;
3571
3572 return TEST_SKIPPED;
3573 }
3574
3575 static test_return_t pre_cork_and_nonblock(memcached_st *memc)
3576 {
3577 test_return_t rc;
3578
3579 rc= pre_cork(memc);
3580
3581 #ifdef __APPLE__
3582 return TEST_SKIPPED;
3583 #endif
3584
3585 if (rc != TEST_SUCCESS)
3586 return rc;
3587
3588 return pre_nonblock(memc);
3589 }
3590
3591 static test_return_t pre_nonblock_binary(memcached_st *memc)
3592 {
3593 memcached_return_t rc= MEMCACHED_FAILURE;
3594 memcached_st *memc_clone;
3595
3596 memc_clone= memcached_clone(NULL, memc);
3597 test_true(memc_clone);
3598 // The memcached_version needs to be done on a clone, because the server
3599 // will not toggle protocol on an connection.
3600 memcached_version(memc_clone);
3601
3602 if (libmemcached_util_version_check(memc_clone, 1, 2, 0))
3603 {
3604 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3605 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3606 test_true(rc == MEMCACHED_SUCCESS);
3607 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3608 }
3609 else
3610 {
3611 return TEST_SKIPPED;
3612 }
3613
3614 memcached_free(memc_clone);
3615
3616 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3617 }
3618
3619 static test_return_t pre_murmur(memcached_st *memc)
3620 {
3621 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
3622
3623 return TEST_SUCCESS;
3624 }
3625
3626 static test_return_t pre_jenkins(memcached_st *memc)
3627 {
3628 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
3629
3630 return TEST_SUCCESS;
3631 }
3632
3633
3634 static test_return_t pre_md5(memcached_st *memc)
3635 {
3636 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
3637
3638 return TEST_SUCCESS;
3639 }
3640
3641 static test_return_t pre_crc(memcached_st *memc)
3642 {
3643 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
3644
3645 return TEST_SUCCESS;
3646 }
3647
3648 static test_return_t pre_hsieh(memcached_st *memc)
3649 {
3650 #ifdef HAVE_HSIEH_HASH
3651 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
3652 return TEST_SUCCESS;
3653 #else
3654 (void) memc;
3655 return TEST_SKIPPED;
3656 #endif
3657 }
3658
3659 static test_return_t pre_hash_fnv1_64(memcached_st *memc)
3660 {
3661 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
3662
3663 return TEST_SUCCESS;
3664 }
3665
3666 static test_return_t pre_hash_fnv1a_64(memcached_st *memc)
3667 {
3668 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
3669
3670 return TEST_SUCCESS;
3671 }
3672
3673 static test_return_t pre_hash_fnv1_32(memcached_st *memc)
3674 {
3675 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
3676
3677 return TEST_SUCCESS;
3678 }
3679
3680 static test_return_t pre_hash_fnv1a_32(memcached_st *memc)
3681 {
3682 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
3683
3684 return TEST_SUCCESS;
3685 }
3686
3687 static test_return_t pre_behavior_ketama(memcached_st *memc)
3688 {
3689 memcached_return_t rc;
3690 uint64_t value;
3691
3692 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
3693 test_true(rc == MEMCACHED_SUCCESS);
3694
3695 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
3696 test_true(value == 1);
3697
3698 return TEST_SUCCESS;
3699 }
3700
3701 static test_return_t pre_behavior_ketama_weighted(memcached_st *memc)
3702 {
3703 memcached_return_t rc;
3704 uint64_t value;
3705
3706 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3707 test_true(rc == MEMCACHED_SUCCESS);
3708
3709 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3710 test_true(value == 1);
3711
3712 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3713 test_true(rc == MEMCACHED_SUCCESS);
3714
3715 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3716 test_true(value == MEMCACHED_HASH_MD5);
3717
3718 return TEST_SUCCESS;
3719 }
3720
3721 /**
3722 @note This should be testing to see if the server really supports the binary protocol.
3723 */
3724 static test_return_t pre_binary(memcached_st *memc)
3725 {
3726 memcached_return_t rc= MEMCACHED_FAILURE;
3727 memcached_st *memc_clone;
3728
3729 memc_clone= memcached_clone(NULL, memc);
3730 test_true(memc_clone);
3731 // The memcached_version needs to be done on a clone, because the server
3732 // will not toggle protocol on an connection.
3733 memcached_version(memc_clone);
3734
3735 if (libmemcached_util_version_check(memc_clone, 1, 2, 0))
3736 {
3737 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3738 test_true(rc == MEMCACHED_SUCCESS);
3739 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3740 }
3741
3742 memcached_free(memc_clone);
3743
3744 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3745 }
3746
3747 static test_return_t pre_sasl(memcached_st *memc)
3748 {
3749 memcached_return_t rc= MEMCACHED_FAILURE;
3750
3751 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3752 const char *server= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3753 const char *user= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3754 const char *pass= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3755
3756 if (server != NULL && user != NULL && pass != NULL)
3757 {
3758 memcached_server_st *servers= memcached_servers_parse(server);
3759 test_true(servers != NULL);
3760 memcached_servers_reset(memc);
3761 test_true(memcached_server_push(memc, servers) == MEMCACHED_SUCCESS);
3762 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3763 rc= memcached_set_sasl_auth_data(memc, user, pass);
3764 test_true(rc == MEMCACHED_SUCCESS);
3765 }
3766 #else
3767 (void)memc;
3768 #endif
3769
3770 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3771 }
3772
3773 static test_return_t pre_replication(memcached_st *memc)
3774 {
3775 test_return_t test_rc;
3776 test_rc= pre_binary(memc);
3777
3778 if (test_rc != TEST_SUCCESS)
3779 return test_rc;
3780
3781 /*
3782 * Make sure that we store the item on all servers
3783 * (master + replicas == number of servers)
3784 */
3785 memcached_return_t rc;
3786 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
3787 memcached_server_count(memc) - 1);
3788 test_true(rc == MEMCACHED_SUCCESS);
3789 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS) == memcached_server_count(memc) - 1);
3790
3791 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3792 }
3793
3794
3795 static test_return_t pre_replication_noblock(memcached_st *memc)
3796 {
3797 test_return_t rc;
3798
3799 rc= pre_replication(memc);
3800 if (rc != TEST_SUCCESS)
3801 return rc;
3802
3803 rc= pre_nonblock(memc);
3804
3805 return rc;
3806 }
3807
3808
3809 static void my_free(const memcached_st *ptr __attribute__((unused)), void *mem, void *context)
3810 {
3811 (void) context;
3812 #ifdef HARD_MALLOC_TESTS
3813 void *real_ptr= (mem == NULL) ? mem : (void*)((caddr_t)mem - 8);
3814 free(real_ptr);
3815 #else
3816 free(mem);
3817 #endif
3818 }
3819
3820
3821 static void *my_malloc(const memcached_st *ptr __attribute__((unused)), const size_t size, void *context)
3822 {
3823 (void)context;
3824 #ifdef HARD_MALLOC_TESTS
3825 void *ret= malloc(size + 8);
3826 if (ret != NULL)
3827 {
3828 ret= (void*)((caddr_t)ret + 8);
3829 }
3830 #else
3831 void *ret= malloc(size);
3832 #endif
3833
3834 if (ret != NULL)
3835 {
3836 memset(ret, 0xff, size);
3837 }
3838
3839 return ret;
3840 }
3841
3842
3843 static void *my_realloc(const memcached_st *ptr __attribute__((unused)), void *mem, const size_t size, void *context)
3844 {
3845 (void)context;
3846 #ifdef HARD_MALLOC_TESTS
3847 void *real_ptr= (mem == NULL) ? NULL : (void*)((caddr_t)mem - 8);
3848 void *nmem= realloc(real_ptr, size + 8);
3849
3850 void *ret= NULL;
3851 if (nmem != NULL)
3852 {
3853 ret= (void*)((caddr_t)nmem + 8);
3854 }
3855
3856 return ret;
3857 #else
3858 return realloc(mem, size);
3859 #endif
3860 }
3861
3862
3863 static void *my_calloc(const memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size, void *context)
3864 {
3865 (void)context;
3866 #ifdef HARD_MALLOC_TESTS
3867 void *mem= my_malloc(ptr, nelem * size);
3868 if (mem)
3869 {
3870 memset(mem, 0, nelem * size);
3871 }
3872
3873 return mem;
3874 #else
3875 return calloc(nelem, size);
3876 #endif
3877 }
3878
3879
3880 static test_return_t set_prefix(memcached_st *memc)
3881 {
3882 memcached_return_t rc;
3883 const char *key= "mine";
3884 char *value;
3885
3886 /* Make sure be default none exists */
3887 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3888 test_true(rc == MEMCACHED_FAILURE);
3889
3890 /* Test a clean set */
3891 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3892 test_true(rc == MEMCACHED_SUCCESS);
3893
3894 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3895 test_true(memcmp(value, key, 4) == 0);
3896 test_true(rc == MEMCACHED_SUCCESS);
3897
3898 /* Test that we can turn it off */
3899 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3900 test_true(rc == MEMCACHED_SUCCESS);
3901
3902 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3903 test_true(rc == MEMCACHED_FAILURE);
3904
3905 /* Now setup for main test */
3906 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3907 test_true(rc == MEMCACHED_SUCCESS);
3908
3909 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3910 test_true(rc == MEMCACHED_SUCCESS);
3911 test_true(memcmp(value, key, 4) == 0);
3912
3913 /* Set to Zero, and then Set to something too large */
3914 {
3915 char long_key[255];
3916 memset(long_key, 0, 255);
3917
3918 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3919 test_true(rc == MEMCACHED_SUCCESS);
3920
3921 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3922 test_true(rc == MEMCACHED_FAILURE);
3923 test_true(value == NULL);
3924
3925 /* Test a long key for failure */
3926 /* TODO, extend test to determine based on setting, what result should be */
3927 strcpy(long_key, "Thisismorethentheallottednumberofcharacters");
3928 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3929 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3930 test_true(rc == MEMCACHED_SUCCESS);
3931
3932 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3933 strcpy(long_key, "This is more then the allotted number of characters");
3934 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3935 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3936
3937 /* Test for a bad prefix, but with a short key */
3938 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
3939 test_true(rc == MEMCACHED_SUCCESS);
3940
3941 strcpy(long_key, "dog cat");
3942 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3943 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3944 }
3945
3946 return TEST_SUCCESS;
3947 }
3948
3949
3950 #ifdef MEMCACHED_ENABLE_DEPRECATED
3951 static test_return_t deprecated_set_memory_alloc(memcached_st *memc)
3952 {
3953 void *test_ptr= NULL;
3954 void *cb_ptr= NULL;
3955 {
3956 memcached_malloc_fn malloc_cb=
3957 (memcached_malloc_fn)my_malloc;
3958 cb_ptr= *(void **)&malloc_cb;
3959 memcached_return_t rc;
3960
3961 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
3962 test_true(rc == MEMCACHED_SUCCESS);
3963 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
3964 test_true(rc == MEMCACHED_SUCCESS);
3965 test_true(test_ptr == cb_ptr);
3966 }
3967
3968 {
3969 memcached_realloc_fn realloc_cb=
3970 (memcached_realloc_fn)my_realloc;
3971 cb_ptr= *(void **)&realloc_cb;
3972 memcached_return_t rc;
3973
3974 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
3975 test_true(rc == MEMCACHED_SUCCESS);
3976 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
3977 test_true(rc == MEMCACHED_SUCCESS);
3978 test_true(test_ptr == cb_ptr);
3979 }
3980
3981 {
3982 memcached_free_fn free_cb=
3983 (memcached_free_fn)my_free;
3984 cb_ptr= *(void **)&free_cb;
3985 memcached_return_t rc;
3986
3987 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
3988 test_true(rc == MEMCACHED_SUCCESS);
3989 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
3990 test_true(rc == MEMCACHED_SUCCESS);
3991 test_true(test_ptr == cb_ptr);
3992 }
3993
3994 return TEST_SUCCESS;
3995 }
3996 #endif
3997
3998
3999 static test_return_t set_memory_alloc(memcached_st *memc)
4000 {
4001 memcached_return_t rc;
4002 rc= memcached_set_memory_allocators(memc, NULL, my_free,
4003 my_realloc, my_calloc, NULL);
4004 test_true(rc == MEMCACHED_FAILURE);
4005
4006 rc= memcached_set_memory_allocators(memc, my_malloc, my_free,
4007 my_realloc, my_calloc, NULL);
4008
4009 memcached_malloc_fn mem_malloc;
4010 memcached_free_fn mem_free;
4011 memcached_realloc_fn mem_realloc;
4012 memcached_calloc_fn mem_calloc;
4013 memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
4014 &mem_realloc, &mem_calloc);
4015
4016 test_true(mem_malloc == my_malloc);
4017 test_true(mem_realloc == my_realloc);
4018 test_true(mem_calloc == my_calloc);
4019 test_true(mem_free == my_free);
4020
4021 return TEST_SUCCESS;
4022 }
4023
4024 static test_return_t enable_consistent_crc(memcached_st *memc)
4025 {
4026 test_return_t rc;
4027 memcached_server_distribution_t value= MEMCACHED_DISTRIBUTION_CONSISTENT;
4028 memcached_hash_t hash;
4029 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
4030 if ((rc= pre_crc(memc)) != TEST_SUCCESS)
4031 return rc;
4032
4033 value= (memcached_server_distribution_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
4034 test_true(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
4035
4036 hash= (memcached_hash_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
4037
4038 if (hash != MEMCACHED_HASH_CRC)
4039 return TEST_SKIPPED;
4040
4041 return TEST_SUCCESS;
4042 }
4043
4044 static test_return_t enable_consistent_hsieh(memcached_st *memc)
4045 {
4046 test_return_t rc;
4047 memcached_server_distribution_t value= MEMCACHED_DISTRIBUTION_CONSISTENT;
4048 memcached_hash_t hash;
4049 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
4050 if ((rc= pre_hsieh(memc)) != TEST_SUCCESS)
4051 return rc;
4052
4053 value= (memcached_server_distribution_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
4054 test_true(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
4055
4056 hash= (memcached_hash_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
4057
4058 if (hash != MEMCACHED_HASH_HSIEH)
4059 return TEST_SKIPPED;
4060
4061
4062 return TEST_SUCCESS;
4063 }
4064
4065 static test_return_t enable_cas(memcached_st *memc)
4066 {
4067 unsigned int set= 1;
4068
4069 if (libmemcached_util_version_check(memc, 1, 2, 4))
4070 {
4071 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
4072
4073 return TEST_SUCCESS;
4074 }
4075
4076 return TEST_SKIPPED;
4077 }
4078
4079 static test_return_t check_for_1_2_3(memcached_st *memc)
4080 {
4081 memcached_version(memc);
4082
4083 memcached_server_instance_st instance=
4084 memcached_server_instance_by_position(memc, 0);
4085
4086 if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4))
4087 || instance->minor_version > 2)
4088 {
4089 return TEST_SUCCESS;
4090 }
4091
4092 return TEST_SKIPPED;
4093 }
4094
4095 static test_return_t pre_unix_socket(memcached_st *memc)
4096 {
4097 memcached_return_t rc;
4098 struct stat buf;
4099
4100 memcached_servers_reset(memc);
4101
4102 if (stat("/tmp/memcached.socket", &buf))
4103 return TEST_SKIPPED;
4104
4105 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
4106
4107 return ( rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_FAILURE );
4108 }
4109
4110 static test_return_t pre_nodelay(memcached_st *memc)
4111 {
4112 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
4113 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
4114
4115 return TEST_SUCCESS;
4116 }
4117
4118 static test_return_t pre_settimer(memcached_st *memc)
4119 {
4120 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
4121 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
4122
4123 return TEST_SUCCESS;
4124 }
4125
4126 static test_return_t poll_timeout(memcached_st *memc)
4127 {
4128 size_t timeout;
4129
4130 timeout= 100;
4131
4132 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
4133
4134 timeout= (size_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
4135
4136 test_true(timeout == 100);
4137
4138 return TEST_SUCCESS;
4139 }
4140
4141 static test_return_t noreply_test(memcached_st *memc)
4142 {
4143 memcached_return_t ret;
4144 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
4145 test_true(ret == MEMCACHED_SUCCESS);
4146 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
4147 test_true(ret == MEMCACHED_SUCCESS);
4148 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
4149 test_true(ret == MEMCACHED_SUCCESS);
4150 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
4151 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
4152 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
4153
4154 for (int count=0; count < 5; ++count)
4155 {
4156 for (size_t x= 0; x < 100; ++x)
4157 {
4158 char key[10];
4159 size_t len= (size_t)sprintf(key, "%zu", x);
4160 switch (count)
4161 {
4162 case 0:
4163 ret= memcached_add(memc, key, len, key, len, 0, 0);
4164 break;
4165 case 1:
4166 ret= memcached_replace(memc, key, len, key, len, 0, 0);
4167 break;
4168 case 2:
4169 ret= memcached_set(memc, key, len, key, len, 0, 0);
4170 break;
4171 case 3:
4172 ret= memcached_append(memc, key, len, key, len, 0, 0);
4173 break;
4174 case 4:
4175 ret= memcached_prepend(memc, key, len, key, len, 0, 0);
4176 break;
4177 default:
4178 test_true(count);
4179 break;
4180 }
4181 test_true(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
4182 }
4183
4184 /*
4185 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4186 ** API and is _ONLY_ done this way to verify that the library works the
4187 ** way it is supposed to do!!!!
4188 */
4189 int no_msg=0;
4190 for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
4191 {
4192 memcached_server_instance_st instance=
4193 memcached_server_instance_by_position(memc, x);
4194 no_msg+=(int)(instance->cursor_active);
4195 }
4196
4197 test_true(no_msg == 0);
4198 test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
4199
4200 /*
4201 ** Now validate that all items was set properly!
4202 */
4203 for (size_t x= 0; x < 100; ++x)
4204 {
4205 char key[10];
4206
4207 size_t len= (size_t)sprintf(key, "%zu", x);
4208 size_t length;
4209 uint32_t flags;
4210 char* value=memcached_get(memc, key, strlen(key),
4211 &length, &flags, &ret);
4212 test_true(ret == MEMCACHED_SUCCESS && value != NULL);
4213 switch (count)
4214 {
4215 case 0: /* FALLTHROUGH */
4216 case 1: /* FALLTHROUGH */
4217 case 2:
4218 test_true(strncmp(value, key, len) == 0);
4219 test_true(len == length);
4220 break;
4221 case 3:
4222 test_true(length == len * 2);
4223 break;
4224 case 4:
4225 test_true(length == len * 3);
4226 break;
4227 default:
4228 test_true(count);
4229 break;
4230 }
4231 free(value);
4232 }
4233 }
4234
4235 /* Try setting an illegal cas value (should not return an error to
4236 * the caller (because we don't expect a return message from the server)
4237 */
4238 const char* keys[]= {"0"};
4239 size_t lengths[]= {1};
4240 size_t length;
4241 uint32_t flags;
4242 memcached_result_st results_obj;
4243 memcached_result_st *results;
4244 ret= memcached_mget(memc, keys, lengths, 1);
4245 test_true(ret == MEMCACHED_SUCCESS);
4246
4247 results= memcached_result_create(memc, &results_obj);
4248 test_true(results);
4249 results= memcached_fetch_result(memc, &results_obj, &ret);
4250 test_true(results);
4251 test_true(ret == MEMCACHED_SUCCESS);
4252 uint64_t cas= memcached_result_cas(results);
4253 memcached_result_free(&results_obj);
4254
4255 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
4256 test_true(ret == MEMCACHED_SUCCESS);
4257
4258 /*
4259 * The item will have a new cas value, so try to set it again with the old
4260 * value. This should fail!
4261 */
4262 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
4263 test_true(ret == MEMCACHED_SUCCESS);
4264 test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
4265 char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
4266 test_true(ret == MEMCACHED_SUCCESS && value != NULL);
4267 free(value);
4268
4269 return TEST_SUCCESS;
4270 }
4271
4272 static test_return_t analyzer_test(memcached_st *memc)
4273 {
4274 memcached_return_t rc;
4275 memcached_stat_st *memc_stat;
4276 memcached_analysis_st *report;
4277
4278 memc_stat= memcached_stat(memc, NULL, &rc);
4279 test_true(rc == MEMCACHED_SUCCESS);
4280 test_true(memc_stat);
4281
4282 report= memcached_analyze(memc, memc_stat, &rc);
4283 test_true(rc == MEMCACHED_SUCCESS);
4284 test_true(report);
4285
4286 free(report);
4287 memcached_stat_free(NULL, memc_stat);
4288
4289 return TEST_SUCCESS;
4290 }
4291
4292 /* Count the objects */
4293 static memcached_return_t callback_dump_counter(const memcached_st *ptr __attribute__((unused)),
4294 const char *key __attribute__((unused)),
4295 size_t key_length __attribute__((unused)),
4296 void *context)
4297 {
4298 size_t *counter= (size_t *)context;
4299
4300 *counter= *counter + 1;
4301
4302 return MEMCACHED_SUCCESS;
4303 }
4304
4305 static test_return_t dump_test(memcached_st *memc)
4306 {
4307 memcached_return_t rc;
4308 size_t counter= 0;
4309 memcached_dump_fn callbacks[1];
4310 test_return_t main_rc;
4311
4312 callbacks[0]= &callback_dump_counter;
4313
4314 /* No support for Binary protocol yet */
4315 if (memc->flags.binary_protocol)
4316 return TEST_SUCCESS;
4317
4318 main_rc= set_test3(memc);
4319
4320 test_true (main_rc == TEST_SUCCESS);
4321
4322 rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
4323 test_true(rc == MEMCACHED_SUCCESS);
4324
4325 /* We may have more then 32 if our previous flush has not completed */
4326 test_true(counter >= 32);
4327
4328 return TEST_SUCCESS;
4329 }
4330
4331 #ifdef HAVE_LIBMEMCACHEDUTIL
4332 static void* connection_release(void *arg)
4333 {
4334 struct {
4335 memcached_pool_st* pool;
4336 memcached_st* mmc;
4337 } *resource= arg;
4338
4339 usleep(250);
4340 assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
4341 return arg;
4342 }
4343
4344 static test_return_t connection_pool_test(memcached_st *memc)
4345 {
4346 memcached_pool_st* pool= memcached_pool_create(memc, 5, 10);
4347 test_true(pool != NULL);
4348 memcached_st* mmc[10];
4349 memcached_return_t rc;
4350
4351 for (size_t x= 0; x < 10; ++x)
4352 {
4353 mmc[x]= memcached_pool_pop(pool, false, &rc);
4354 test_true(mmc[x] != NULL);
4355 test_true(rc == MEMCACHED_SUCCESS);
4356 }
4357
4358 test_true(memcached_pool_pop(pool, false, &rc) == NULL);
4359 test_true(rc == MEMCACHED_SUCCESS);
4360
4361 pthread_t tid;
4362 struct {
4363 memcached_pool_st* pool;
4364 memcached_st* mmc;
4365 } item= { .pool = pool, .mmc = mmc[9] };
4366 pthread_create(&tid, NULL, connection_release, &item);
4367 mmc[9]= memcached_pool_pop(pool, true, &rc);
4368 test_true(rc == MEMCACHED_SUCCESS);
4369 pthread_join(tid, NULL);
4370 test_true(mmc[9] == item.mmc);
4371 const char *key= "key";
4372 size_t keylen= strlen(key);
4373
4374 // verify that I can do ops with all connections
4375 rc= memcached_set(mmc[0], key, keylen, "0", 1, 0, 0);
4376 test_true(rc == MEMCACHED_SUCCESS);
4377
4378 for (size_t x= 0; x < 10; ++x)
4379 {
4380 uint64_t number_value;
4381 rc= memcached_increment(mmc[x], key, keylen, 1, &number_value);
4382 test_true(rc == MEMCACHED_SUCCESS);
4383 test_true(number_value == (x+1));
4384 }
4385
4386 // Release them..
4387 for (size_t x= 0; x < 10; ++x)
4388 {
4389 test_true(memcached_pool_push(pool, mmc[x]) == MEMCACHED_SUCCESS);
4390 }
4391
4392
4393 /* verify that I can set behaviors on the pool when I don't have all
4394 * of the connections in the pool. It should however be enabled
4395 * when I push the item into the pool
4396 */
4397 mmc[0]= memcached_pool_pop(pool, false, &rc);
4398 test_true(mmc[0] != NULL);
4399
4400 rc= memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999);
4401 test_true(rc == MEMCACHED_SUCCESS);
4402
4403 mmc[1]= memcached_pool_pop(pool, false, &rc);
4404 test_true(mmc[1] != NULL);
4405
4406 test_true(memcached_behavior_get(mmc[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK) == 9999);
4407 test_true(memcached_pool_push(pool, mmc[1]) == MEMCACHED_SUCCESS);
4408 test_true(memcached_pool_push(pool, mmc[0]) == MEMCACHED_SUCCESS);
4409
4410 mmc[0]= memcached_pool_pop(pool, false, &rc);
4411 test_true(memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK) == 9999);
4412 test_true(memcached_pool_push(pool, mmc[0]) == MEMCACHED_SUCCESS);
4413
4414
4415 test_true(memcached_pool_destroy(pool) == memc);
4416 return TEST_SUCCESS;
4417 }
4418
4419 static test_return_t util_version_test(memcached_st *memc)
4420 {
4421 bool if_successful;
4422
4423 if_successful= libmemcached_util_version_check(memc, 0, 0, 0);
4424 test_true(if_successful == true);
4425
4426 if_successful= libmemcached_util_version_check(memc, 9, 9, 9);
4427 test_true(if_successful == false);
4428
4429 memcached_server_instance_st instance=
4430 memcached_server_instance_by_position(memc, 0);
4431
4432 memcached_version(memc);
4433
4434 // We only use one binary when we test, so this should be just fine.
4435 if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version, instance->minor_version);
4436 test_true(if_successful == true);
4437
4438 if (instance->minor_version > 0)
4439 if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version, instance->minor_version -1);
4440 else if (instance->micro_version > 0)
4441 if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version - 1, instance->minor_version);
4442 else if (instance->major_version > 0)
4443 if_successful= libmemcached_util_version_check(memc, instance->major_version -1, instance->micro_version, instance->minor_version);
4444
4445 test_true(if_successful == true);
4446
4447 if (instance->minor_version > 0)
4448 if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version, instance->minor_version +1);
4449 else if (instance->micro_version > 0)
4450 if_successful= libmemcached_util_version_check(memc, instance->major_version, instance->micro_version +1, instance->minor_version);
4451 else if (instance->major_version > 0)
4452 if_successful= libmemcached_util_version_check(memc, instance->major_version +1, instance->micro_version, instance->minor_version);
4453
4454 test_true(if_successful == false);
4455
4456 return TEST_SUCCESS;
4457 }
4458
4459 static test_return_t ping_test(memcached_st *memc)
4460 {
4461 memcached_return_t rc;
4462 memcached_server_instance_st instance=
4463 memcached_server_instance_by_position(memc, 0);
4464
4465 // Test both the version that returns a code, and the one that does not.
4466 test_true(libmemcached_util_ping(memcached_server_name(instance),
4467 memcached_server_port(instance), NULL));
4468
4469 test_true(libmemcached_util_ping(memcached_server_name(instance),
4470 memcached_server_port(instance), &rc));
4471
4472 test_true(rc == MEMCACHED_SUCCESS);
4473
4474 return TEST_SUCCESS;
4475 }
4476 #endif
4477
4478 static test_return_t replication_set_test(memcached_st *memc)
4479 {
4480 memcached_return_t rc;
4481 memcached_st *memc_clone= memcached_clone(NULL, memc);
4482 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
4483
4484 rc= memcached_set(memc, "bubba", 5, "0", 1, 0, 0);
4485 test_true(rc == MEMCACHED_SUCCESS);
4486
4487 /*
4488 ** We are using the quiet commands to store the replicas, so we need
4489 ** to ensure that all of them are processed before we can continue.
4490 ** In the test we go directly from storing the object to trying to
4491 ** receive the object from all of the different servers, so we
4492 ** could end up in a race condition (the memcached server hasn't yet
4493 ** processed the quiet command from the replication set when it process
4494 ** the request from the other client (created by the clone)). As a
4495 ** workaround for that we call memcached_quit to send the quit command
4496 ** to the server and wait for the response ;-) If you use the test code
4497 ** as an example for your own code, please note that you shouldn't need
4498 ** to do this ;-)
4499 */
4500 memcached_quit(memc);
4501
4502 /*
4503 ** "bubba" should now be stored on all of our servers. We don't have an
4504 ** easy to use API to address each individual server, so I'll just iterate
4505 ** through a bunch of "master keys" and I should most likely hit all of the
4506 ** servers...
4507 */
4508 for (int x= 'a'; x <= 'z'; ++x)
4509 {
4510 char key[2]= { [0]= (char)x };
4511 size_t len;
4512 uint32_t flags;
4513 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
4514 &len, &flags, &rc);
4515 test_true(rc == MEMCACHED_SUCCESS);
4516 test_true(val != NULL);
4517 free(val);
4518 }
4519
4520 memcached_free(memc_clone);
4521
4522 return TEST_SUCCESS;
4523 }
4524
4525 static test_return_t replication_get_test(memcached_st *memc)
4526 {
4527 memcached_return_t rc;
4528
4529 /*
4530 * Don't do the following in your code. I am abusing the internal details
4531 * within the library, and this is not a supported interface.
4532 * This is to verify correct behavior in the library
4533 */
4534 for (uint32_t host= 0; host < memcached_server_count(memc); ++host)
4535 {
4536 memcached_st *memc_clone= memcached_clone(NULL, memc);
4537 memcached_server_instance_st instance=
4538 memcached_server_instance_by_position(memc_clone, host);
4539
4540 ((memcached_server_write_instance_st)instance)->port= 0;
4541
4542 for (int x= 'a'; x <= 'z'; ++x)
4543 {
4544 char key[2]= { [0]= (char)x };
4545 size_t len;
4546 uint32_t flags;
4547 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
4548 &len, &flags, &rc);
4549 test_true(rc == MEMCACHED_SUCCESS);
4550 test_true(val != NULL);
4551 free(val);
4552 }
4553
4554 memcached_free(memc_clone);
4555 }
4556
4557 return TEST_SUCCESS;
4558 }
4559
4560 static test_return_t replication_mget_test(memcached_st *memc)
4561 {
4562 memcached_return_t rc;
4563 memcached_st *memc_clone= memcached_clone(NULL, memc);
4564 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
4565
4566 const char *keys[]= { "bubba", "key1", "key2", "key3" };
4567 size_t len[]= { 5, 4, 4, 4 };
4568
4569 for (size_t x= 0; x< 4; ++x)
4570 {
4571 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
4572 test_true(rc == MEMCACHED_SUCCESS);
4573 }
4574
4575 /*
4576 ** We are using the quiet commands to store the replicas, so we need
4577 ** to ensure that all of them are processed before we can continue.
4578 ** In the test we go directly from storing the object to trying to
4579 ** receive the object from all of the different servers, so we
4580 ** could end up in a race condition (the memcached server hasn't yet
4581 ** processed the quiet command from the replication set when it process
4582 ** the request from the other client (created by the clone)). As a
4583 ** workaround for that we call memcached_quit to send the quit command
4584 ** to the server and wait for the response ;-) If you use the test code
4585 ** as an example for your own code, please note that you shouldn't need
4586 ** to do this ;-)
4587 */
4588 memcached_quit(memc);
4589
4590 /*
4591 * Don't do the following in your code. I am abusing the internal details
4592 * within the library, and this is not a supported interface.
4593 * This is to verify correct behavior in the library
4594 */
4595 memcached_result_st result_obj;
4596 for (uint32_t host= 0; host < memc_clone->number_of_hosts; host++)
4597 {
4598 memcached_st *new_clone= memcached_clone(NULL, memc);
4599 memcached_server_instance_st instance=
4600 memcached_server_instance_by_position(new_clone, host);
4601 ((memcached_server_write_instance_st)instance)->port= 0;
4602
4603 for (int x= 'a'; x <= 'z'; ++x)
4604 {
4605 char key[2]= { [0]= (char)x, [1]= 0 };
4606
4607 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
4608 test_true(rc == MEMCACHED_SUCCESS);
4609
4610 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
4611 test_true(results);
4612
4613 int hits= 0;
4614 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
4615 {
4616 hits++;
4617 }
4618 test_true(hits == 4);
4619 memcached_result_free(&result_obj);
4620 }
4621
4622 memcached_free(new_clone);
4623 }
4624
4625 memcached_free(memc_clone);
4626
4627 return TEST_SUCCESS;
4628 }
4629
4630 static test_return_t replication_randomize_mget_test(memcached_st *memc)
4631 {
4632 memcached_result_st result_obj;
4633 memcached_return_t rc;
4634 memcached_st *memc_clone= memcached_clone(NULL, memc);
4635 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
4636 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1);
4637
4638 const char *keys[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4639 size_t len[]= { 4, 4, 4, 4, 4, 4, 4 };
4640
4641 for (size_t x= 0; x< 7; ++x)
4642 {
4643 rc= memcached_set(memc, keys[x], len[x], "1", 1, 0, 0);
4644 test_true(rc == MEMCACHED_SUCCESS);
4645 }
4646
4647 memcached_quit(memc);
4648
4649 for (size_t x= 0; x< 7; ++x)
4650 {
4651 const char key[2]= { [0]= (const char)x };
4652
4653 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 7);
4654 test_true(rc == MEMCACHED_SUCCESS);
4655
4656 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
4657 test_true(results);
4658
4659 int hits= 0;
4660 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
4661 {
4662 ++hits;
4663 }
4664 test_true(hits == 7);
4665 memcached_result_free(&result_obj);
4666 }
4667 memcached_free(memc_clone);
4668 return TEST_SUCCESS;
4669 }
4670
4671 static test_return_t replication_delete_test(memcached_st *memc)
4672 {
4673 memcached_return_t rc;
4674 memcached_st *memc_clone= memcached_clone(NULL, memc);
4675 /* Delete the items from all of the servers except 1 */
4676 uint64_t repl= memcached_behavior_get(memc,
4677 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
4678 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, --repl);
4679
4680 const char *keys[]= { "bubba", "key1", "key2", "key3" };
4681 size_t len[]= { 5, 4, 4, 4 };
4682
4683 for (size_t x= 0; x< 4; ++x)
4684 {
4685 rc= memcached_delete_by_key(memc, keys[0], len[0], keys[x], len[x], 0);
4686 test_true(rc == MEMCACHED_SUCCESS);
4687 }
4688
4689 /*
4690 * Don't do the following in your code. I am abusing the internal details
4691 * within the library, and this is not a supported interface.
4692 * This is to verify correct behavior in the library
4693 */
4694 uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
4695 for (uint32_t x= 0; x < (repl + 1); ++x)
4696 {
4697 memcached_server_instance_st instance=
4698 memcached_server_instance_by_position(memc_clone, x);
4699
4700 ((memcached_server_write_instance_st)instance)->port= 0;
4701 if (++hash == memc_clone->number_of_hosts)
4702 hash= 0;
4703 }
4704
4705 memcached_result_st result_obj;
4706 for (uint32_t host= 0; host < memc_clone->number_of_hosts; ++host)
4707 {
4708 for (size_t x= 'a'; x <= 'z'; ++x)
4709 {
4710 const char key[2]= { [0]= (const char)x };
4711
4712 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 4);
4713 test_true(rc == MEMCACHED_SUCCESS);
4714
4715 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
4716 test_true(results);
4717
4718 int hits= 0;
4719 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
4720 {
4721 ++hits;
4722 }
4723 test_true(hits == 4);
4724 memcached_result_free(&result_obj);
4725 }
4726 }
4727 memcached_free(memc_clone);
4728
4729 return TEST_SUCCESS;
4730 }
4731
4732 #if 0
4733 static test_return_t hash_sanity_test (memcached_st *memc)
4734 {
4735 (void)memc;
4736
4737 assert(MEMCACHED_HASH_DEFAULT == MEMCACHED_HASH_DEFAULT);
4738 assert(MEMCACHED_HASH_MD5 == MEMCACHED_HASH_MD5);
4739 assert(MEMCACHED_HASH_CRC == MEMCACHED_HASH_CRC);
4740 assert(MEMCACHED_HASH_FNV1_64 == MEMCACHED_HASH_FNV1_64);
4741 assert(MEMCACHED_HASH_FNV1A_64 == MEMCACHED_HASH_FNV1A_64);
4742 assert(MEMCACHED_HASH_FNV1_32 == MEMCACHED_HASH_FNV1_32);
4743 assert(MEMCACHED_HASH_FNV1A_32 == MEMCACHED_HASH_FNV1A_32);
4744 #ifdef HAVE_HSIEH_HASH
4745 assert(MEMCACHED_HASH_HSIEH == MEMCACHED_HASH_HSIEH);
4746 #endif
4747 assert(MEMCACHED_HASH_MURMUR == MEMCACHED_HASH_MURMUR);
4748 assert(MEMCACHED_HASH_JENKINS == MEMCACHED_HASH_JENKINS);
4749 assert(MEMCACHED_HASH_MAX == MEMCACHED_HASH_MAX);
4750
4751 return TEST_SUCCESS;
4752 }
4753 #endif
4754
4755 static test_return_t hsieh_avaibility_test (memcached_st *memc)
4756 {
4757 memcached_return_t expected_rc= MEMCACHED_FAILURE;
4758 #ifdef HAVE_HSIEH_HASH
4759 expected_rc= MEMCACHED_SUCCESS;
4760 #endif
4761 memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
4762 (uint64_t)MEMCACHED_HASH_HSIEH);
4763 test_true(rc == expected_rc);
4764
4765 return TEST_SUCCESS;
4766 }
4767
4768 static test_return_t one_at_a_time_run (memcached_st *memc __attribute__((unused)))
4769 {
4770 uint32_t x;
4771 const char **ptr;
4772
4773 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4774 {
4775 uint32_t hash_val;
4776
4777 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_DEFAULT);
4778 test_true(one_at_a_time_values[x] == hash_val);
4779 }
4780
4781 return TEST_SUCCESS;
4782 }
4783
4784 static test_return_t md5_run (memcached_st *memc __attribute__((unused)))
4785 {
4786 uint32_t x;
4787 const char **ptr;
4788
4789 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4790 {
4791 uint32_t hash_val;
4792
4793 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MD5);
4794 test_true(md5_values[x] == hash_val);
4795 }
4796
4797 return TEST_SUCCESS;
4798 }
4799
4800 static test_return_t crc_run (memcached_st *memc __attribute__((unused)))
4801 {
4802 uint32_t x;
4803 const char **ptr;
4804
4805 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4806 {
4807 uint32_t hash_val;
4808
4809 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_CRC);
4810 test_true(crc_values[x] == hash_val);
4811 }
4812
4813 return TEST_SUCCESS;
4814 }
4815
4816 static test_return_t fnv1_64_run (memcached_st *memc __attribute__((unused)))
4817 {
4818 uint32_t x;
4819 const char **ptr;
4820
4821 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4822 {
4823 uint32_t hash_val;
4824
4825 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4826 test_true(fnv1_64_values[x] == hash_val);
4827 }
4828
4829 return TEST_SUCCESS;
4830 }
4831
4832 static test_return_t fnv1a_64_run (memcached_st *memc __attribute__((unused)))
4833 {
4834 uint32_t x;
4835 const char **ptr;
4836
4837 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4838 {
4839 uint32_t hash_val;
4840
4841 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_64);
4842 test_true(fnv1a_64_values[x] == hash_val);
4843 }
4844
4845 return TEST_SUCCESS;
4846 }
4847
4848 static test_return_t fnv1_32_run (memcached_st *memc __attribute__((unused)))
4849 {
4850 uint32_t x;
4851 const char **ptr;
4852
4853
4854 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4855 {
4856 uint32_t hash_val;
4857
4858 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_32);
4859 test_true(fnv1_32_values[x] == hash_val);
4860 }
4861
4862 return TEST_SUCCESS;
4863 }
4864
4865 static test_return_t fnv1a_32_run (memcached_st *memc __attribute__((unused)))
4866 {
4867 uint32_t x;
4868 const char **ptr;
4869
4870 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4871 {
4872 uint32_t hash_val;
4873
4874 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_32);
4875 test_true(fnv1a_32_values[x] == hash_val);
4876 }
4877
4878 return TEST_SUCCESS;
4879 }
4880
4881 static test_return_t hsieh_run (memcached_st *memc __attribute__((unused)))
4882 {
4883 uint32_t x;
4884 const char **ptr;
4885
4886 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4887 {
4888 uint32_t hash_val;
4889
4890 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_HSIEH);
4891 test_true(hsieh_values[x] == hash_val);
4892 }
4893
4894 return TEST_SUCCESS;
4895 }
4896
4897 static test_return_t murmur_run (memcached_st *memc __attribute__((unused)))
4898 {
4899 #ifdef WORDS_BIGENDIAN
4900 return TEST_SKIPPED;
4901 #else
4902 uint32_t x;
4903 const char **ptr;
4904
4905 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4906 {
4907 uint32_t hash_val;
4908
4909 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MURMUR);
4910 test_true(murmur_values[x] == hash_val);
4911 }
4912
4913 return TEST_SUCCESS;
4914 #endif
4915 }
4916
4917 static test_return_t jenkins_run (memcached_st *memc __attribute__((unused)))
4918 {
4919 uint32_t x;
4920 const char **ptr;
4921
4922
4923 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4924 {
4925 uint32_t hash_val;
4926
4927 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_JENKINS);
4928 test_true(jenkins_values[x] == hash_val);
4929 }
4930
4931 return TEST_SUCCESS;
4932 }
4933
4934 static uint32_t hash_md5_test_function(const char *string, size_t string_length, void *context)
4935 {
4936 (void)context;
4937 return libhashkit_md5(string, string_length);
4938 }
4939
4940 static uint32_t hash_crc_test_function(const char *string, size_t string_length, void *context)
4941 {
4942 (void)context;
4943 return libhashkit_crc32(string, string_length);
4944 }
4945
4946 static test_return_t memcached_get_hashkit_test (memcached_st *memc)
4947 {
4948 uint32_t x;
4949 const char **ptr;
4950 const hashkit_st *kit;
4951 hashkit_st new_kit;
4952 hashkit_return_t hash_rc;
4953
4954 uint32_t md5_hosts[]= {4U, 1U, 0U, 1U, 4U, 2U, 0U, 3U, 0U, 0U, 3U, 1U, 0U, 0U, 1U, 3U, 0U, 0U, 0U, 3U, 1U, 0U, 4U, 4U, 3U};
4955 uint32_t crc_hosts[]= {2U, 4U, 1U, 0U, 2U, 4U, 4U, 4U, 1U, 2U, 3U, 4U, 3U, 4U, 1U, 3U, 3U, 2U, 0U, 0U, 0U, 1U, 2U, 4U, 0U};
4956
4957 kit= memcached_get_hashkit(memc);
4958
4959 hashkit_clone(&new_kit, kit);
4960 hash_rc= hashkit_set_custom_function(&new_kit, hash_md5_test_function, NULL);
4961 test_true(hash_rc == HASHKIT_SUCCESS);
4962
4963 memcached_set_hashkit(memc, &new_kit);
4964
4965 /*
4966 Verify Setting the hash.
4967 */
4968 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4969 {
4970 uint32_t hash_val;
4971
4972 hash_val= hashkit_digest(kit, *ptr, strlen(*ptr));
4973 test_true(md5_values[x] == hash_val);
4974 }
4975
4976
4977 /*
4978 Now check memcached_st.
4979 */
4980 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4981 {
4982 uint32_t hash_val;
4983
4984 hash_val= memcached_generate_hash(memc, *ptr, strlen(*ptr));
4985 test_true(md5_hosts[x] == hash_val);
4986 }
4987
4988 hash_rc= hashkit_set_custom_function(&new_kit, hash_crc_test_function, NULL);
4989 test_true(hash_rc == HASHKIT_SUCCESS);
4990
4991 memcached_set_hashkit(memc, &new_kit);
4992
4993 /*
4994 Verify Setting the hash.
4995 */
4996 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4997 {
4998 uint32_t hash_val;
4999
5000 hash_val= hashkit_digest(kit, *ptr, strlen(*ptr));
5001 test_true(crc_values[x] == hash_val);
5002 }
5003
5004 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
5005 {
5006 uint32_t hash_val;
5007
5008 hash_val= memcached_generate_hash(memc, *ptr, strlen(*ptr));
5009 test_true(crc_hosts[x] == hash_val);
5010 }
5011
5012 return TEST_SUCCESS;
5013 }
5014
5015 /*
5016 Test case adapted from John Gorman <johngorman2@gmail.com>
5017
5018 We are testing the error condition when we connect to a server via memcached_get()
5019 but find that the server is not available.
5020 */
5021 static test_return_t memcached_get_MEMCACHED_SOME_ERRORS(memcached_st *memc)
5022 {
5023 (void)memc;
5024 memcached_st *tl_memc_h;
5025 memcached_server_st *servers;
5026
5027 const char *key= "MemcachedLives";
5028 size_t len;
5029 uint32_t flags;
5030 memcached_return rc;
5031 char *value;
5032
5033 // Create a handle.
5034 tl_memc_h= memcached_create(NULL);
5035 servers= memcached_servers_parse("localhost:9898"); // This server should not exist
5036 memcached_server_push(tl_memc_h, servers);
5037 memcached_server_list_free(servers);
5038
5039 // See if memcached is reachable.
5040 value= memcached_get(tl_memc_h, key, strlen(key), &len, &flags, &rc);
5041
5042 if (value)
5043 {
5044 free(value);
5045 test_true(value); // Pointer won't be zero so this is fine.
5046 }
5047
5048 test_true(len == 0);
5049 test_true(rc == MEMCACHED_SOME_ERRORS);
5050
5051 return TEST_SUCCESS;
5052 }
5053
5054 /*
5055 We connect to a server which exists, but search for a key that does not exist.
5056 */
5057 static test_return_t memcached_get_MEMCACHED_NOTFOUND(memcached_st *memc)
5058 {
5059 const char *key= "MemcachedKeyNotEXIST";
5060 size_t len;
5061 uint32_t flags;
5062 memcached_return rc;
5063 char *value;
5064
5065 // See if memcached is reachable.
5066 value= memcached_get(memc, key, strlen(key), &len, &flags, &rc);
5067
5068 if (value)
5069 {
5070 free(value);
5071 test_true(value); // Pointer won't be zero so this is fine.
5072 }
5073
5074 test_true(len == 0);
5075 test_true(rc == MEMCACHED_NOTFOUND);
5076
5077 return TEST_SUCCESS;
5078 }
5079
5080 /*
5081 Test case adapted from John Gorman <johngorman2@gmail.com>
5082
5083 We are testing the error condition when we connect to a server via memcached_get_by_key()
5084 but find that the server is not available.
5085 */
5086 static test_return_t memcached_get_by_key_MEMCACHED_SOME_ERRORS(memcached_st *memc)
5087 {
5088 (void)memc;
5089 memcached_st *tl_memc_h;
5090 memcached_server_st *servers;
5091
5092 const char *key= "MemcachedLives";
5093 size_t len;
5094 uint32_t flags;
5095 memcached_return rc;
5096 char *value;
5097
5098 // Create a handle.
5099 tl_memc_h= memcached_create(NULL);
5100 servers= memcached_servers_parse("localhost:9898"); // This server should not exist
5101 memcached_server_push(tl_memc_h, servers);
5102 memcached_server_list_free(servers);
5103
5104 // See if memcached is reachable.
5105 value= memcached_get_by_key(tl_memc_h, key, strlen(key), key, strlen(key), &len, &flags, &rc);
5106
5107 if (value)
5108 {
5109 free(value);
5110 test_true(value); // Pointer won't be zero so this is fine.
5111 }
5112
5113 test_true(len == 0);
5114 test_true(rc == MEMCACHED_SOME_ERRORS);
5115
5116 return TEST_SUCCESS;
5117 }
5118
5119 /*
5120 We connect to a server which exists, but search for a key that does not exist.
5121 */
5122 static test_return_t memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st *memc)
5123 {
5124 const char *key= "MemcachedKeyNotEXIST";
5125 size_t len;
5126 uint32_t flags;
5127 memcached_return rc;
5128 char *value;
5129
5130 // See if memcached is reachable.
5131 value= memcached_get_by_key(memc, key, strlen(key), key, strlen(key), &len, &flags, &rc);
5132
5133 if (value)
5134 {
5135 free(value);
5136 test_true(value); // Pointer won't be zero so this is fine.
5137 }
5138
5139 test_true(len == 0);
5140 test_true(rc == MEMCACHED_NOTFOUND);
5141
5142 return TEST_SUCCESS;
5143 }
5144
5145
5146 static test_return_t ketama_compatibility_libmemcached(memcached_st *trash)
5147 {
5148 memcached_return_t rc;
5149 uint64_t value;
5150 int x;
5151 memcached_server_st *server_pool;
5152 memcached_st *memc;
5153
5154 (void)trash;
5155
5156 memc= memcached_create(NULL);
5157 test_true(memc);
5158
5159 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
5160 test_true(rc == MEMCACHED_SUCCESS);
5161
5162 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
5163 test_true(value == 1);
5164
5165 test_true(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) == MEMCACHED_SUCCESS);
5166 test_true(memcached_behavior_get_distribution(memc) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
5167
5168
5169 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");
5170 memcached_server_push(memc, server_pool);
5171
5172 /* verify that the server list was parsed okay. */
5173 test_true(memcached_server_count(memc) == 8);
5174 test_strcmp(server_pool[0].hostname, "10.0.1.1");
5175 test_true(server_pool[0].port == 11211);
5176 test_true(server_pool[0].weight == 600);
5177 test_strcmp(server_pool[2].hostname, "10.0.1.3");
5178 test_true(server_pool[2].port == 11211);
5179 test_true(server_pool[2].weight == 200);
5180 test_strcmp(server_pool[7].hostname, "10.0.1.8");
5181 test_true(server_pool[7].port == 11211);
5182 test_true(server_pool[7].weight == 100);
5183
5184 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5185 * us test the boundary wraparound.
5186 */
5187 test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
5188
5189 /* verify the standard ketama set. */
5190 for (x= 0; x < 99; x++)
5191 {
5192 uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
5193 memcached_server_instance_st instance=
5194 memcached_server_instance_by_position(memc, server_idx);
5195 const char *hostname = memcached_server_name(instance);
5196
5197 test_strcmp(hostname, ketama_test_cases[x].server);
5198 }
5199
5200 memcached_server_list_free(server_pool);
5201 memcached_free(memc);
5202
5203 return TEST_SUCCESS;
5204 }
5205
5206 static test_return_t ketama_compatibility_spymemcached(memcached_st *trash)
5207 {
5208 memcached_return_t rc;
5209 uint64_t value;
5210 int x;
5211 memcached_server_st *server_pool;
5212 memcached_st *memc;
5213
5214 (void)trash;
5215
5216 memc= memcached_create(NULL);
5217 test_true(memc);
5218
5219 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
5220 test_true(rc == MEMCACHED_SUCCESS);
5221
5222 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
5223 test_true(value == 1);
5224
5225 test_true(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) == MEMCACHED_SUCCESS);
5226 test_true(memcached_behavior_get_distribution(memc) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY);
5227
5228 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");
5229 memcached_server_push(memc, server_pool);
5230
5231 /* verify that the server list was parsed okay. */
5232 test_true(memcached_server_count(memc) == 8);
5233 test_strcmp(server_pool[0].hostname, "10.0.1.1");
5234 test_true(server_pool[0].port == 11211);
5235 test_true(server_pool[0].weight == 600);
5236 test_strcmp(server_pool[2].hostname, "10.0.1.3");
5237 test_true(server_pool[2].port == 11211);
5238 test_true(server_pool[2].weight == 200);
5239 test_strcmp(server_pool[7].hostname, "10.0.1.8");
5240 test_true(server_pool[7].port == 11211);
5241 test_true(server_pool[7].weight == 100);
5242
5243 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5244 * us test the boundary wraparound.
5245 */
5246 test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
5247
5248 /* verify the standard ketama set. */
5249 for (x= 0; x < 99; x++)
5250 {
5251 uint32_t server_idx= memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key));
5252
5253 memcached_server_instance_st instance=
5254 memcached_server_instance_by_position(memc, server_idx);
5255
5256 const char *hostname= memcached_server_name(instance);
5257
5258 test_strcmp(hostname, ketama_test_cases_spy[x].server);
5259 }
5260
5261 memcached_server_list_free(server_pool);
5262 memcached_free(memc);
5263
5264 return TEST_SUCCESS;
5265 }
5266
5267 static test_return_t regression_bug_434484(memcached_st *memc)
5268 {
5269 test_return_t test_rc;
5270 test_rc= pre_binary(memc);
5271
5272 if (test_rc != TEST_SUCCESS)
5273 return test_rc;
5274
5275 memcached_return_t ret;
5276 const char *key= "regression_bug_434484";
5277 size_t keylen= strlen(key);
5278
5279 ret= memcached_append(memc, key, keylen, key, keylen, 0, 0);
5280 test_true(ret == MEMCACHED_NOTSTORED);
5281
5282 size_t size= 2048 * 1024;
5283 void *data= calloc(1, size);
5284 test_true(data != NULL);
5285 ret= memcached_set(memc, key, keylen, data, size, 0, 0);
5286 test_true(ret == MEMCACHED_E2BIG);
5287 free(data);
5288
5289 return TEST_SUCCESS;
5290 }
5291
5292 static test_return_t regression_bug_434843(memcached_st *memc)
5293 {
5294 test_return_t test_rc;
5295 test_rc= pre_binary(memc);
5296
5297 if (test_rc != TEST_SUCCESS)
5298 return test_rc;
5299
5300 memcached_return_t rc;
5301 size_t counter= 0;
5302 memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
5303
5304 /*
5305 * I only want to hit only _one_ server so I know the number of requests I'm
5306 * sending in the pipleine to the server. Let's try to do a multiget of
5307 * 1024 (that should satisfy most users don't you think?). Future versions
5308 * will include a mget_execute function call if you need a higher number.
5309 */
5310 uint32_t number_of_hosts= memcached_server_count(memc);
5311 memc->number_of_hosts= 1;
5312 const size_t max_keys= 1024;
5313 char **keys= calloc(max_keys, sizeof(char*));
5314 size_t *key_length=calloc(max_keys, sizeof(size_t));
5315
5316 for (size_t x= 0; x < max_keys; ++x)
5317 {
5318 char k[251];
5319
5320 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
5321 keys[x]= strdup(k);
5322 test_true(keys[x] != NULL);
5323 }
5324
5325 /*
5326 * Run two times.. the first time we should have 100% cache miss,
5327 * and the second time we should have 100% cache hits
5328 */
5329 for (size_t y= 0; y < 2; y++)
5330 {
5331 rc= memcached_mget(memc, (const char**)keys, key_length, max_keys);
5332 test_true(rc == MEMCACHED_SUCCESS);
5333 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5334
5335 if (y == 0)
5336 {
5337 /* The first iteration should give me a 100% cache miss. verify that*/
5338 char blob[1024]= { 0 };
5339
5340 test_true(counter == 0);
5341
5342 for (size_t x= 0; x < max_keys; ++x)
5343 {
5344 rc= memcached_add(memc, keys[x], key_length[x],
5345 blob, sizeof(blob), 0, 0);
5346 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5347 }
5348 }
5349 else
5350 {
5351 /* Verify that we received all of the key/value pairs */
5352 test_true(counter == max_keys);
5353 }
5354 }
5355
5356 /* Release allocated resources */
5357 for (size_t x= 0; x < max_keys; ++x)
5358 {
5359 free(keys[x]);
5360 }
5361 free(keys);
5362 free(key_length);
5363
5364 memc->number_of_hosts= number_of_hosts;
5365
5366 return TEST_SUCCESS;
5367 }
5368
5369 static test_return_t regression_bug_434843_buffered(memcached_st *memc)
5370 {
5371 memcached_return_t rc;
5372 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
5373 test_true(rc == MEMCACHED_SUCCESS);
5374
5375 return regression_bug_434843(memc);
5376 }
5377
5378 static test_return_t regression_bug_421108(memcached_st *memc)
5379 {
5380 memcached_return_t rc;
5381 memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc);
5382 test_true(rc == MEMCACHED_SUCCESS);
5383
5384 char *bytes= memcached_stat_get_value(memc, memc_stat, "bytes", &rc);
5385 test_true(rc == MEMCACHED_SUCCESS);
5386 test_true(bytes != NULL);
5387 char *bytes_read= memcached_stat_get_value(memc, memc_stat,
5388 "bytes_read", &rc);
5389 test_true(rc == MEMCACHED_SUCCESS);
5390 test_true(bytes_read != NULL);
5391
5392 char *bytes_written= memcached_stat_get_value(memc, memc_stat,
5393 "bytes_written", &rc);
5394 test_true(rc == MEMCACHED_SUCCESS);
5395 test_true(bytes_written != NULL);
5396
5397 test_true(strcmp(bytes, bytes_read) != 0);
5398 test_true(strcmp(bytes, bytes_written) != 0);
5399
5400 /* Release allocated resources */
5401 free(bytes);
5402 free(bytes_read);
5403 free(bytes_written);
5404 memcached_stat_free(NULL, memc_stat);
5405
5406 return TEST_SUCCESS;
5407 }
5408
5409 /*
5410 * The test case isn't obvious so I should probably document why
5411 * it works the way it does. Bug 442914 was caused by a bug
5412 * in the logic in memcached_purge (it did not handle the case
5413 * where the number of bytes sent was equal to the watermark).
5414 * In this test case, create messages so that we hit that case
5415 * and then disable noreply mode and issue a new command to
5416 * verify that it isn't stuck. If we change the format for the
5417 * delete command or the watermarks, we need to update this
5418 * test....
5419 */
5420 static test_return_t regression_bug_442914(memcached_st *memc)
5421 {
5422 memcached_return_t rc;
5423 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
5424 test_true(rc == MEMCACHED_SUCCESS);
5425 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
5426
5427 uint32_t number_of_hosts= memcached_server_count(memc);
5428 memc->number_of_hosts= 1;
5429
5430 char k[250];
5431 size_t len;
5432
5433 for (uint32_t x= 0; x < 250; ++x)
5434 {
5435 len= (size_t)snprintf(k, sizeof(k), "%0250u", x);
5436 rc= memcached_delete(memc, k, len, 0);
5437 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5438 }
5439
5440 (void)snprintf(k, sizeof(k), "%037u", 251U);
5441 len= strlen(k);
5442
5443 rc= memcached_delete(memc, k, len, 0);
5444 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5445
5446 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0);
5447 test_true(rc == MEMCACHED_SUCCESS);
5448 rc= memcached_delete(memc, k, len, 0);
5449 test_true(rc == MEMCACHED_NOTFOUND);
5450
5451 memc->number_of_hosts= number_of_hosts;
5452
5453 return TEST_SUCCESS;
5454 }
5455
5456 static test_return_t regression_bug_447342(memcached_st *memc)
5457 {
5458 memcached_server_instance_st instance_one;
5459 memcached_server_instance_st instance_two;
5460
5461 if (memcached_server_count(memc) < 3 || pre_replication(memc) != MEMCACHED_SUCCESS)
5462 return TEST_SKIPPED;
5463
5464 memcached_return_t rc;
5465
5466 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 2);
5467 test_true(rc == MEMCACHED_SUCCESS);
5468
5469 const size_t max_keys= 100;
5470 char **keys= calloc(max_keys, sizeof(char*));
5471 size_t *key_length= calloc(max_keys, sizeof(size_t));
5472
5473 for (size_t x= 0; x < max_keys; ++x)
5474 {
5475 char k[251];
5476
5477 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
5478 keys[x]= strdup(k);
5479 test_true(keys[x] != NULL);
5480 rc= memcached_set(memc, k, key_length[x], k, key_length[x], 0, 0);
5481 test_true(rc == MEMCACHED_SUCCESS);
5482 }
5483
5484 /*
5485 ** We are using the quiet commands to store the replicas, so we need
5486 ** to ensure that all of them are processed before we can continue.
5487 ** In the test we go directly from storing the object to trying to
5488 ** receive the object from all of the different servers, so we
5489 ** could end up in a race condition (the memcached server hasn't yet
5490 ** processed the quiet command from the replication set when it process
5491 ** the request from the other client (created by the clone)). As a
5492 ** workaround for that we call memcached_quit to send the quit command
5493 ** to the server and wait for the response ;-) If you use the test code
5494 ** as an example for your own code, please note that you shouldn't need
5495 ** to do this ;-)
5496 */
5497 memcached_quit(memc);
5498
5499 /* Verify that all messages are stored, and we didn't stuff too much
5500 * into the servers
5501 */
5502 rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
5503 test_true(rc == MEMCACHED_SUCCESS);
5504
5505 size_t counter= 0;
5506 memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
5507 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5508 /* Verify that we received all of the key/value pairs */
5509 test_true(counter == max_keys);
5510
5511 memcached_quit(memc);
5512 /*
5513 * Don't do the following in your code. I am abusing the internal details
5514 * within the library, and this is not a supported interface.
5515 * This is to verify correct behavior in the library. Fake that two servers
5516 * are dead..
5517 */
5518 instance_one= memcached_server_instance_by_position(memc, 0);
5519 instance_two= memcached_server_instance_by_position(memc, 2);
5520 in_port_t port0= instance_one->port;
5521 in_port_t port2= instance_two->port;
5522
5523 ((memcached_server_write_instance_st)instance_one)->port= 0;
5524 ((memcached_server_write_instance_st)instance_two)->port= 0;
5525
5526 rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
5527 test_true(rc == MEMCACHED_SUCCESS);
5528
5529 counter= 0;
5530 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5531 test_true(counter == (unsigned int)max_keys);
5532
5533 /* restore the memc handle */
5534 ((memcached_server_write_instance_st)instance_one)->port= port0;
5535 ((memcached_server_write_instance_st)instance_two)->port= port2;
5536
5537 memcached_quit(memc);
5538
5539 /* Remove half of the objects */
5540 for (size_t x= 0; x < max_keys; ++x)
5541 {
5542 if (x & 1)
5543 {
5544 rc= memcached_delete(memc, keys[x], key_length[x], 0);
5545 test_true(rc == MEMCACHED_SUCCESS);
5546 }
5547 }
5548
5549 memcached_quit(memc);
5550 ((memcached_server_write_instance_st)instance_one)->port= 0;
5551 ((memcached_server_write_instance_st)instance_two)->port= 0;
5552
5553 /* now retry the command, this time we should have cache misses */
5554 rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
5555 test_true(rc == MEMCACHED_SUCCESS);
5556
5557 counter= 0;
5558 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5559 test_true(counter == (unsigned int)(max_keys >> 1));
5560
5561 /* Release allocated resources */
5562 for (size_t x= 0; x < max_keys; ++x)
5563 {
5564 free(keys[x]);
5565 }
5566 free(keys);
5567 free(key_length);
5568
5569 /* restore the memc handle */
5570 ((memcached_server_write_instance_st)instance_one)->port= port0;
5571 ((memcached_server_write_instance_st)instance_two)->port= port2;
5572
5573 return TEST_SUCCESS;
5574 }
5575
5576 static test_return_t regression_bug_463297(memcached_st *memc)
5577 {
5578 memcached_st *memc_clone= memcached_clone(NULL, memc);
5579 test_true(memc_clone != NULL);
5580 test_true(memcached_version(memc_clone) == MEMCACHED_SUCCESS);
5581
5582
5583 if (libmemcached_util_version_check(memc_clone, 1, 1, 2))
5584 {
5585 /* Binary protocol doesn't support deferred delete */
5586 memcached_st *bin_clone= memcached_clone(NULL, memc);
5587 test_true(bin_clone != NULL);
5588 test_true(memcached_behavior_set(bin_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1) == MEMCACHED_SUCCESS);
5589 test_true(memcached_delete(bin_clone, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS);
5590 memcached_free(bin_clone);
5591
5592 memcached_quit(memc_clone);
5593
5594 /* If we know the server version, deferred delete should fail
5595 * with invalid arguments */
5596 test_true(memcached_delete(memc_clone, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS);
5597
5598 /* If we don't know the server version, we should get a protocol error */
5599 memcached_return_t rc= memcached_delete(memc, "foo", 3, 1);
5600
5601 /* but there is a bug in some of the memcached servers (1.4) that treats
5602 * the counter as noreply so it doesn't send the proper error message
5603 */
5604 test_true(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
5605
5606 /* And buffered mode should be disabled and we should get protocol error */
5607 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1) == MEMCACHED_SUCCESS);
5608 rc= memcached_delete(memc, "foo", 3, 1);
5609 test_true(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
5610
5611 /* Same goes for noreply... */
5612 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1) == MEMCACHED_SUCCESS);
5613 rc= memcached_delete(memc, "foo", 3, 1);
5614 test_true(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
5615
5616 /* but a normal request should go through (and be buffered) */
5617 test_true((rc= memcached_delete(memc, "foo", 3, 0)) == MEMCACHED_BUFFERED);
5618 test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
5619
5620 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0) == MEMCACHED_SUCCESS);
5621 /* unbuffered noreply should be success */
5622 test_true(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_SUCCESS);
5623 /* unbuffered with reply should be not found... */
5624 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0) == MEMCACHED_SUCCESS);
5625 test_true(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_NOTFOUND);
5626 }
5627
5628 memcached_free(memc_clone);
5629 return TEST_SUCCESS;
5630 }
5631
5632
5633 /* Test memcached_server_get_last_disconnect
5634 * For a working server set, shall be NULL
5635 * For a set of non existing server, shall not be NULL
5636 */
5637 static test_return_t test_get_last_disconnect(memcached_st *memc)
5638 {
5639 memcached_return_t rc;
5640 memcached_server_instance_st disconnected_server;
5641
5642 /* With the working set of server */
5643 const char *key= "marmotte";
5644 const char *value= "milka";
5645
5646 memcached_reset_last_disconnected_server(memc);
5647 rc= memcached_set(memc, key, strlen(key),
5648 value, strlen(value),
5649 (time_t)0, (uint32_t)0);
5650 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5651
5652 disconnected_server = memcached_server_get_last_disconnect(memc);
5653 test_true(disconnected_server == NULL);
5654
5655 /* With a non existing server */
5656 memcached_st *mine;
5657 memcached_server_st *servers;
5658
5659 const char *server_list= "localhost:9";
5660
5661 servers= memcached_servers_parse(server_list);
5662 test_true(servers);
5663 mine= memcached_create(NULL);
5664 rc= memcached_server_push(mine, servers);
5665 test_true(rc == MEMCACHED_SUCCESS);
5666 memcached_server_list_free(servers);
5667 test_true(mine);
5668
5669 rc= memcached_set(mine, key, strlen(key),
5670 value, strlen(value),
5671 (time_t)0, (uint32_t)0);
5672 test_true(rc != MEMCACHED_SUCCESS);
5673
5674 disconnected_server= memcached_server_get_last_disconnect(mine);
5675 if (disconnected_server == NULL)
5676 {
5677 fprintf(stderr, "RC %s\n", memcached_strerror(mine, rc));
5678 abort();
5679 }
5680 test_true(disconnected_server != NULL);
5681 test_true(memcached_server_port(disconnected_server)== 9);
5682 test_true(strncmp(memcached_server_name(disconnected_server),"localhost",9) == 0);
5683
5684 memcached_quit(mine);
5685 memcached_free(mine);
5686
5687 return TEST_SUCCESS;
5688 }
5689
5690 static test_return_t test_verbosity(memcached_st *memc)
5691 {
5692 memcached_verbosity(memc, 3);
5693
5694 return TEST_SUCCESS;
5695 }
5696
5697 static test_return_t test_server_failure(memcached_st *memc)
5698 {
5699 memcached_st *local_memc;
5700 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, 0);
5701
5702 local_memc= memcached_create(NULL);
5703
5704 memcached_server_add(local_memc, memcached_server_name(instance), memcached_server_port(instance));
5705 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 2);
5706
5707 uint32_t server_count= memcached_server_count(local_memc);
5708
5709 test_true(server_count == 1);
5710
5711 // Disable the server
5712 instance= memcached_server_instance_by_position(local_memc, 0);
5713 ((memcached_server_write_instance_st)instance)->server_failure_counter= 2;
5714
5715 memcached_return_t rc;
5716 rc= memcached_set(local_memc, "foo", strlen("foo"),
5717 NULL, 0,
5718 (time_t)0, (uint32_t)0);
5719 test_true(rc == MEMCACHED_SERVER_MARKED_DEAD);
5720
5721 ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
5722 rc= memcached_set(local_memc, "foo", strlen("foo"),
5723 NULL, 0,
5724 (time_t)0, (uint32_t)0);
5725 test_true(rc == MEMCACHED_SUCCESS);
5726
5727
5728 memcached_free(local_memc);
5729
5730 return TEST_SUCCESS;
5731 }
5732
5733 static test_return_t test_cull_servers(memcached_st *memc)
5734 {
5735 uint32_t count = memcached_server_count(memc);
5736
5737 // Do not do this in your code, it is not supported.
5738 memc->servers[1].state.is_dead= true;
5739 memc->state.is_time_for_rebuild= true;
5740
5741 uint32_t new_count= memcached_server_count(memc);
5742 test_true(count == new_count);
5743
5744 #if 0
5745 test_true(count == new_count + 1 );
5746 #endif
5747
5748 return TEST_SUCCESS;
5749 }
5750
5751 /*
5752 * This test ensures that the failure counter isn't incremented during
5753 * normal termination of the memcached instance.
5754 */
5755 static test_return_t wrong_failure_counter_test(memcached_st *memc)
5756 {
5757 memcached_return_t rc;
5758 memcached_server_instance_st instance;
5759
5760 /* Set value to force connection to the server */
5761 const char *key= "marmotte";
5762 const char *value= "milka";
5763
5764 /*
5765 * Please note that I'm abusing the internal structures in libmemcached
5766 * in a non-portable way and you shouldn't be doing this. I'm only
5767 * doing this in order to verify that the library works the way it should
5768 */
5769 uint32_t number_of_hosts= memcached_server_count(memc);
5770 memc->number_of_hosts= 1;
5771
5772 /* Ensure that we are connected to the server by setting a value */
5773 rc= memcached_set(memc, key, strlen(key),
5774 value, strlen(value),
5775 (time_t)0, (uint32_t)0);
5776 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5777
5778
5779 instance= memcached_server_instance_by_position(memc, 0);
5780 /* The test is to see that the memcached_quit doesn't increase the
5781 * the server failure conter, so let's ensure that it is zero
5782 * before sending quit
5783 */
5784 ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
5785
5786 memcached_quit(memc);
5787
5788 /* Verify that it memcached_quit didn't increment the failure counter
5789 * Please note that this isn't bullet proof, because an error could
5790 * occur...
5791 */
5792 test_true(instance->server_failure_counter == 0);
5793
5794 /* restore the instance */
5795 memc->number_of_hosts= number_of_hosts;
5796
5797 return TEST_SUCCESS;
5798 }
5799
5800
5801
5802
5803 /*
5804 * Test that ensures mget_execute does not end into recursive calls that finally fails
5805 */
5806 static test_return_t regression_bug_490486(memcached_st *memc)
5807 {
5808 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
5809 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
5810 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 1000);
5811 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 1);
5812 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 3600);
5813
5814 #ifdef __APPLE__
5815 return TEST_SKIPPED; // My MAC can't handle this test
5816 #endif
5817
5818 /*
5819 * I only want to hit _one_ server so I know the number of requests I'm
5820 * sending in the pipeline.
5821 */
5822 uint32_t number_of_hosts= memc->number_of_hosts;
5823 memc->number_of_hosts= 1;
5824 size_t max_keys= 20480;
5825
5826
5827 char **keys= calloc(max_keys, sizeof(char*));
5828 size_t *key_length=calloc(max_keys, sizeof(size_t));
5829
5830 /* First add all of the items.. */
5831 bool slept= false;
5832 char blob[1024]= { 0 };
5833 memcached_return rc;
5834 for (size_t x= 0; x < max_keys; ++x)
5835 {
5836 char k[251];
5837 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
5838 keys[x]= strdup(k);
5839 assert(keys[x] != NULL);
5840 rc= memcached_set(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0);
5841 #ifdef __APPLE__
5842 if (rc == MEMCACHED_SERVER_MARKED_DEAD)
5843 {
5844 break; // We are out of business
5845 }
5846 #endif
5847 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT); // MEMCACHED_TIMEOUT <-- only observed on OSX
5848
5849 if (rc == MEMCACHED_TIMEOUT && slept == false)
5850 {
5851 x++;
5852 sleep(1);// We will try to sleep
5853 slept= true;
5854 }
5855 else if (rc == MEMCACHED_TIMEOUT && slept == true)
5856 {
5857 // We failed to send everything.
5858 break;
5859 }
5860 }
5861
5862 if (rc != MEMCACHED_SERVER_MARKED_DEAD)
5863 {
5864
5865 /* Try to get all of them with a large multiget */
5866 size_t counter= 0;
5867 memcached_execute_function callbacks[1]= { [0]= &callback_counter };
5868 rc= memcached_mget_execute(memc, (const char**)keys, key_length,
5869 (size_t)max_keys, callbacks, &counter, 1);
5870
5871 assert(rc == MEMCACHED_SUCCESS);
5872 char* the_value= NULL;
5873 char the_key[MEMCACHED_MAX_KEY];
5874 size_t the_key_length;
5875 size_t the_value_length;
5876 uint32_t the_flags;
5877
5878 do {
5879 the_value= memcached_fetch(memc, the_key, &the_key_length, &the_value_length, &the_flags, &rc);
5880
5881 if ((the_value!= NULL) && (rc == MEMCACHED_SUCCESS))
5882 {
5883 ++counter;
5884 free(the_value);
5885 }
5886
5887 } while ( (the_value!= NULL) && (rc == MEMCACHED_SUCCESS));
5888
5889
5890 assert(rc == MEMCACHED_END);
5891
5892 /* Verify that we got all of the items */
5893 assert(counter == max_keys);
5894 }
5895
5896 /* Release all allocated resources */
5897 for (size_t x= 0; x < max_keys; ++x)
5898 {
5899 free(keys[x]);
5900 }
5901 free(keys);
5902 free(key_length);
5903
5904 memc->number_of_hosts= number_of_hosts;
5905
5906 return TEST_SUCCESS;
5907 }
5908
5909 static test_return_t regression_bug_583031(memcached_st *unused)
5910 {
5911 (void)unused;
5912
5913 memcached_st *memc= memcached_create(NULL);
5914 assert(memc);
5915 memcached_server_add(memc, "10.2.3.4", 11211);
5916
5917 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 1000);
5918 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 1000);
5919 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
5920 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
5921 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 1000);
5922 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 3);
5923
5924 memcached_return_t rc;
5925 size_t length;
5926 uint32_t flags;
5927
5928 (void)memcached_get(memc, "dsf", 3, &length, &flags, &rc);
5929
5930 test_true(rc == MEMCACHED_TIMEOUT);
5931
5932 memcached_free(memc);
5933
5934 return TEST_SUCCESS;
5935 }
5936
5937 static void memcached_die(memcached_st* mc, memcached_return error, const char* what, uint32_t it)
5938 {
5939 fprintf(stderr, "Iteration #%u: ", it);
5940
5941 if(error == MEMCACHED_ERRNO)
5942 {
5943 fprintf(stderr, "system error %d from %s: %s\n",
5944 errno, what, strerror(errno));
5945 }
5946 else
5947 {
5948 fprintf(stderr, "error %d from %s: %s\n", error, what,
5949 memcached_strerror(mc, error));
5950 }
5951 }
5952
5953 #define TEST_CONSTANT_CREATION 200
5954
5955 static test_return_t regression_bug_(memcached_st *memc)
5956 {
5957 const char *remote_server;
5958 (void)memc;
5959
5960 if (! (remote_server= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5961 {
5962 return TEST_SKIPPED;
5963 }
5964
5965 for (uint32_t x= 0; x < TEST_CONSTANT_CREATION; x++)
5966 {
5967 memcached_st* mc= memcached_create(NULL);
5968 memcached_return rc;
5969
5970 rc= memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
5971 if (rc != MEMCACHED_SUCCESS)
5972 {
5973 memcached_die(mc, rc, "memcached_behavior_set", x);
5974 }
5975
5976 rc= memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS, 1);
5977 if (rc != MEMCACHED_SUCCESS)
5978 {
5979 memcached_die(mc, rc, "memcached_behavior_set", x);
5980 }
5981
5982 rc= memcached_server_add(mc, remote_server, 0);
5983 if (rc != MEMCACHED_SUCCESS)
5984 {
5985 memcached_die(mc, rc, "memcached_server_add", x);
5986 }
5987
5988 const char *set_key= "akey";
5989 const size_t set_key_len= strlen(set_key);
5990 const char *set_value= "a value";
5991 const size_t set_value_len= strlen(set_value);
5992
5993 if (rc == MEMCACHED_SUCCESS)
5994 {
5995 if (x > 0)
5996 {
5997 size_t get_value_len;
5998 char *get_value;
5999 uint32_t get_value_flags;
6000
6001 get_value= memcached_get(mc, set_key, set_key_len, &get_value_len,
6002 &get_value_flags, &rc);
6003 if (rc != MEMCACHED_SUCCESS)
6004 {
6005 memcached_die(mc, rc, "memcached_get", x);
6006 }
6007 else
6008 {
6009
6010 if (x != 0 &&
6011 (get_value_len != set_value_len
6012 || 0!=strncmp(get_value, set_value, get_value_len)))
6013 {
6014 fprintf(stderr, "Values don't match?\n");
6015 rc= MEMCACHED_FAILURE;
6016 }
6017 free(get_value);
6018 }
6019 }
6020
6021 rc= memcached_set(mc,
6022 set_key, set_key_len,
6023 set_value, set_value_len,
6024 0, /* time */
6025 0 /* flags */
6026 );
6027 if (rc != MEMCACHED_SUCCESS)
6028 {
6029 memcached_die(mc, rc, "memcached_set", x);
6030 }
6031 }
6032
6033 memcached_quit(mc);
6034 memcached_free(mc);
6035
6036 if (rc != MEMCACHED_SUCCESS)
6037 {
6038 break;
6039 }
6040 }
6041
6042 return TEST_SUCCESS;
6043 }
6044
6045 /*
6046 * Test that the sasl authentication works. We cannot use the default
6047 * pool of servers, because that would require that all servers we want
6048 * to test supports SASL authentication, and that they use the default
6049 * creds.
6050 */
6051 static test_return_t sasl_auth_test(memcached_st *memc)
6052 {
6053 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6054 memcached_return_t rc;
6055
6056 rc= memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6057 test_true(rc == MEMCACHED_SUCCESS);
6058 test_true((rc= memcached_delete(memc, "foo", 3, 0)) == MEMCACHED_SUCCESS);
6059 test_true((rc= memcached_destroy_sasl_auth_data(memc)) == MEMCACHED_SUCCESS);
6060 test_true((rc= memcached_destroy_sasl_auth_data(memc)) == MEMCACHED_FAILURE);
6061 test_true((rc= memcached_destroy_sasl_auth_data(NULL)) == MEMCACHED_FAILURE);
6062 memcached_quit(memc);
6063
6064 rc= memcached_set_sasl_auth_data(memc,
6065 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6066 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6067 test_true(rc == MEMCACHED_SUCCESS);
6068
6069 rc= memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6070 test_true(rc == MEMCACHED_AUTH_FAILURE);
6071 test_true(memcached_destroy_sasl_auth_data(memc) == MEMCACHED_SUCCESS);
6072
6073 memcached_quit(memc);
6074 return TEST_SUCCESS;
6075 #else
6076 (void)memc;
6077 return TEST_FAILURE;
6078 #endif
6079 }
6080
6081 /* Clean the server before beginning testing */
6082 test_st tests[] ={
6083 {"flush", 0, (test_callback_fn)flush_test },
6084 {"init", 0, (test_callback_fn)init_test },
6085 {"allocation", 0, (test_callback_fn)allocation_test },
6086 {"server_list_null_test", 0, (test_callback_fn)server_list_null_test},
6087 {"server_unsort", 0, (test_callback_fn)server_unsort_test},
6088 {"server_sort", 0, (test_callback_fn)server_sort_test},
6089 {"server_sort2", 0, (test_callback_fn)server_sort2_test},
6090 {"memcached_server_remove", 0, (test_callback_fn)memcached_server_remove_test},
6091 {"clone_test", 0, (test_callback_fn)clone_test },
6092 {"connection_test", 0, (test_callback_fn)connection_test},
6093 {"callback_test", 0, (test_callback_fn)callback_test},
6094 {"userdata_test", 0, (test_callback_fn)userdata_test},
6095 {"error", 0, (test_callback_fn)error_test },
6096 {"set", 0, (test_callback_fn)set_test },
6097 {"set2", 0, (test_callback_fn)set_test2 },
6098 {"set3", 0, (test_callback_fn)set_test3 },
6099 {"dump", 1, (test_callback_fn)dump_test},
6100 {"add", 1, (test_callback_fn)add_test },
6101 {"replace", 1, (test_callback_fn)replace_test },
6102 {"delete", 1, (test_callback_fn)delete_test },
6103 {"get", 1, (test_callback_fn)get_test },
6104 {"get2", 0, (test_callback_fn)get_test2 },
6105 {"get3", 0, (test_callback_fn)get_test3 },
6106 {"get4", 0, (test_callback_fn)get_test4 },
6107 {"partial mget", 0, (test_callback_fn)get_test5 },
6108 {"stats_servername", 0, (test_callback_fn)stats_servername_test },
6109 {"increment", 0, (test_callback_fn)increment_test },
6110 {"increment_with_initial", 1, (test_callback_fn)increment_with_initial_test },
6111 {"decrement", 0, (test_callback_fn)decrement_test },
6112 {"decrement_with_initial", 1, (test_callback_fn)decrement_with_initial_test },
6113 {"increment_by_key", 0, (test_callback_fn)increment_by_key_test },
6114 {"increment_with_initial_by_key", 1, (test_callback_fn)increment_with_initial_by_key_test },
6115 {"decrement_by_key", 0, (test_callback_fn)decrement_by_key_test },
6116 {"decrement_with_initial_by_key", 1, (test_callback_fn)decrement_with_initial_by_key_test },
6117 {"quit", 0, (test_callback_fn)quit_test },
6118 {"mget", 1, (test_callback_fn)mget_test },
6119 {"mget_result", 1, (test_callback_fn)mget_result_test },
6120 {"mget_result_alloc", 1, (test_callback_fn)mget_result_alloc_test },
6121 {"mget_result_function", 1, (test_callback_fn)mget_result_function },
6122 {"mget_execute", 1, (test_callback_fn)mget_execute },
6123 {"mget_end", 0, (test_callback_fn)mget_end },
6124 {"get_stats", 0, (test_callback_fn)get_stats },
6125 {"add_host_test", 0, (test_callback_fn)add_host_test },
6126 {"add_host_test_1", 0, (test_callback_fn)add_host_test1 },
6127 {"get_stats_keys", 0, (test_callback_fn)get_stats_keys },
6128 {"version_string_test", 0, (test_callback_fn)version_string_test},
6129 {"bad_key", 1, (test_callback_fn)bad_key_test },
6130 {"memcached_server_cursor", 1, (test_callback_fn)memcached_server_cursor_test },
6131 {"read_through", 1, (test_callback_fn)read_through },
6132 {"delete_through", 1, (test_callback_fn)delete_through },
6133 {"noreply", 1, (test_callback_fn)noreply_test},
6134 {"analyzer", 1, (test_callback_fn)analyzer_test},
6135 #ifdef HAVE_LIBMEMCACHEDUTIL
6136 {"connectionpool", 1, (test_callback_fn)connection_pool_test },
6137 {"ping", 1, (test_callback_fn)ping_test },
6138 {"util_version", 1, (test_callback_fn)util_version_test },
6139 #endif
6140 {"test_get_last_disconnect", 1, (test_callback_fn)test_get_last_disconnect},
6141 {"verbosity", 1, (test_callback_fn)test_verbosity},
6142 {"test_server_failure", 1, (test_callback_fn)test_server_failure},
6143 {"cull_servers", 1, (test_callback_fn)test_cull_servers},
6144 {0, 0, 0}
6145 };
6146
6147 test_st behavior_tests[] ={
6148 {"behavior_test", 0, (test_callback_fn)behavior_test},
6149 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn)MEMCACHED_BEHAVIOR_CORK_test},
6150 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test},
6151 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test},
6152 {0, 0, 0}
6153 };
6154
6155 test_st regression_binary_vs_block[] ={
6156 {"block add", 1, (test_callback_fn)block_add_regression},
6157 {"binary add", 1, (test_callback_fn)binary_add_regression},
6158 {0, 0, 0}
6159 };
6160
6161 test_st async_tests[] ={
6162 {"add", 1, (test_callback_fn)add_wrapper },
6163 {0, 0, 0}
6164 };
6165
6166 test_st string_tests[] ={
6167 {"string static with null", 0, (test_callback_fn)string_static_null },
6168 {"string alloc with null", 0, (test_callback_fn)string_alloc_null },
6169 {"string alloc with 1K", 0, (test_callback_fn)string_alloc_with_size },
6170 {"string alloc with malloc failure", 0, (test_callback_fn)string_alloc_with_size_toobig },
6171 {"string append", 0, (test_callback_fn)string_alloc_append },
6172 {"string append failure (too big)", 0, (test_callback_fn)string_alloc_append_toobig },
6173 {0, 0, (test_callback_fn)0}
6174 };
6175
6176 test_st result_tests[] ={
6177 {"result static", 0, (test_callback_fn)result_static},
6178 {"result alloc", 0, (test_callback_fn)result_alloc},
6179 {0, 0, (test_callback_fn)0}
6180 };
6181
6182 test_st version_1_2_3[] ={
6183 {"append", 0, (test_callback_fn)append_test },
6184 {"prepend", 0, (test_callback_fn)prepend_test },
6185 {"cas", 0, (test_callback_fn)cas_test },
6186 {"cas2", 0, (test_callback_fn)cas2_test },
6187 {"append_binary", 0, (test_callback_fn)append_binary_test },
6188 {0, 0, (test_callback_fn)0}
6189 };
6190
6191 test_st user_tests[] ={
6192 {"user_supplied_bug1", 0, (test_callback_fn)user_supplied_bug1 },
6193 {"user_supplied_bug2", 0, (test_callback_fn)user_supplied_bug2 },
6194 {"user_supplied_bug3", 0, (test_callback_fn)user_supplied_bug3 },
6195 {"user_supplied_bug4", 0, (test_callback_fn)user_supplied_bug4 },
6196 {"user_supplied_bug5", 1, (test_callback_fn)user_supplied_bug5 },
6197 {"user_supplied_bug6", 1, (test_callback_fn)user_supplied_bug6 },
6198 {"user_supplied_bug7", 1, (test_callback_fn)user_supplied_bug7 },
6199 {"user_supplied_bug8", 1, (test_callback_fn)user_supplied_bug8 },
6200 {"user_supplied_bug9", 1, (test_callback_fn)user_supplied_bug9 },
6201 {"user_supplied_bug10", 1, (test_callback_fn)user_supplied_bug10 },
6202 {"user_supplied_bug11", 1, (test_callback_fn)user_supplied_bug11 },
6203 {"user_supplied_bug12", 1, (test_callback_fn)user_supplied_bug12 },
6204 {"user_supplied_bug13", 1, (test_callback_fn)user_supplied_bug13 },
6205 {"user_supplied_bug14", 1, (test_callback_fn)user_supplied_bug14 },
6206 {"user_supplied_bug15", 1, (test_callback_fn)user_supplied_bug15 },
6207 {"user_supplied_bug16", 1, (test_callback_fn)user_supplied_bug16 },
6208 #ifndef __sun
6209 /*
6210 ** It seems to be something weird with the character sets..
6211 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6212 ** guess I need to find out how this is supposed to work.. Perhaps I need
6213 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6214 ** so just disable the code for now...).
6215 */
6216 {"user_supplied_bug17", 1, (test_callback_fn)user_supplied_bug17 },
6217 #endif
6218 {"user_supplied_bug18", 1, (test_callback_fn)user_supplied_bug18 },
6219 {"user_supplied_bug19", 1, (test_callback_fn)user_supplied_bug19 },
6220 {"user_supplied_bug20", 1, (test_callback_fn)user_supplied_bug20 },
6221 {"user_supplied_bug21", 1, (test_callback_fn)user_supplied_bug21 },
6222 {"wrong_failure_counter_test", 1, (test_callback_fn)wrong_failure_counter_test},
6223 {0, 0, (test_callback_fn)0}
6224 };
6225
6226 test_st replication_tests[]= {
6227 {"set", 1, (test_callback_fn)replication_set_test },
6228 {"get", 0, (test_callback_fn)replication_get_test },
6229 {"mget", 0, (test_callback_fn)replication_mget_test },
6230 {"delete", 0, (test_callback_fn)replication_delete_test },
6231 {"rand_mget", 0, (test_callback_fn)replication_randomize_mget_test },
6232 {0, 0, (test_callback_fn)0}
6233 };
6234
6235 /*
6236 * The following test suite is used to verify that we don't introduce
6237 * regression bugs. If you want more information about the bug / test,
6238 * you should look in the bug report at
6239 * http://bugs.launchpad.net/libmemcached
6240 */
6241 test_st regression_tests[]= {
6242 {"lp:434484", 1, (test_callback_fn)regression_bug_434484 },
6243 {"lp:434843", 1, (test_callback_fn)regression_bug_434843 },
6244 {"lp:434843-buffered", 1, (test_callback_fn)regression_bug_434843_buffered },
6245 {"lp:421108", 1, (test_callback_fn)regression_bug_421108 },
6246 {"lp:442914", 1, (test_callback_fn)regression_bug_442914 },
6247 {"lp:447342", 1, (test_callback_fn)regression_bug_447342 },
6248 {"lp:463297", 1, (test_callback_fn)regression_bug_463297 },
6249 {"lp:490486", 1, (test_callback_fn)regression_bug_490486 },
6250 {"lp:583031", 1, (test_callback_fn)regression_bug_583031 },
6251 {"lp:?", 1, (test_callback_fn)regression_bug_ },
6252 {0, 0, (test_callback_fn)0}
6253 };
6254
6255 test_st sasl_auth_tests[]= {
6256 {"sasl_auth", 1, (test_callback_fn)sasl_auth_test },
6257 {0, 0, (test_callback_fn)0}
6258 };
6259
6260 test_st ketama_compatibility[]= {
6261 {"libmemcached", 1, (test_callback_fn)ketama_compatibility_libmemcached },
6262 {"spymemcached", 1, (test_callback_fn)ketama_compatibility_spymemcached },
6263 {0, 0, (test_callback_fn)0}
6264 };
6265
6266 test_st generate_tests[] ={
6267 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
6268 {"generate_data", 1, (test_callback_fn)generate_data },
6269 {"get_read", 0, (test_callback_fn)get_read },
6270 {"delete_generate", 0, (test_callback_fn)delete_generate },
6271 {"generate_buffer_data", 1, (test_callback_fn)generate_buffer_data },
6272 {"delete_buffer", 0, (test_callback_fn)delete_buffer_generate},
6273 {"generate_data", 1, (test_callback_fn)generate_data },
6274 {"mget_read", 0, (test_callback_fn)mget_read },
6275 {"mget_read_result", 0, (test_callback_fn)mget_read_result },
6276 {"mget_read_function", 0, (test_callback_fn)mget_read_function },
6277 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6278 {"generate_large_pairs", 1, (test_callback_fn)generate_large_pairs },
6279 {"generate_data", 1, (test_callback_fn)generate_data },
6280 {"generate_buffer_data", 1, (test_callback_fn)generate_buffer_data },
6281 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6282 {0, 0, (test_callback_fn)0}
6283 };
6284
6285 test_st consistent_tests[] ={
6286 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
6287 {"generate_data", 1, (test_callback_fn)generate_data },
6288 {"get_read", 0, (test_callback_fn)get_read_count },
6289 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6290 {0, 0, (test_callback_fn)0}
6291 };
6292
6293 test_st consistent_weighted_tests[] ={
6294 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
6295 {"generate_data", 1, (test_callback_fn)generate_data_with_stats },
6296 {"get_read", 0, (test_callback_fn)get_read_count },
6297 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6298 {0, 0, (test_callback_fn)0}
6299 };
6300
6301 test_st hsieh_availability[] ={
6302 {"hsieh_avaibility_test", 0, (test_callback_fn)hsieh_avaibility_test},
6303 {0, 0, (test_callback_fn)0}
6304 };
6305
6306 #if 0
6307 test_st hash_sanity[] ={
6308 {"hash sanity", 0, (test_callback_fn)hash_sanity_test},
6309 {0, 0, (test_callback_fn)0}
6310 };
6311 #endif
6312
6313 test_st ketama_auto_eject_hosts[] ={
6314 {"auto_eject_hosts", 1, (test_callback_fn)auto_eject_hosts },
6315 {"output_ketama_weighted_keys", 1, (test_callback_fn)output_ketama_weighted_keys },
6316 {0, 0, (test_callback_fn)0}
6317 };
6318
6319 test_st hash_tests[] ={
6320 {"one_at_a_time_run", 0, (test_callback_fn)one_at_a_time_run },
6321 {"md5", 0, (test_callback_fn)md5_run },
6322 {"crc", 0, (test_callback_fn)crc_run },
6323 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
6324 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
6325 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
6326 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
6327 {"hsieh", 0, (test_callback_fn)hsieh_run },
6328 {"murmur", 0, (test_callback_fn)murmur_run },
6329 {"jenkis", 0, (test_callback_fn)jenkins_run },
6330 {"memcached_get_hashkit", 0, (test_callback_fn)memcached_get_hashkit_test },
6331 {0, 0, (test_callback_fn)0}
6332 };
6333
6334 test_st error_conditions[] ={
6335 {"memcached_get_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn)memcached_get_MEMCACHED_SOME_ERRORS },
6336 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn)memcached_get_MEMCACHED_NOTFOUND },
6337 {"memcached_get_by_key_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn)memcached_get_by_key_MEMCACHED_SOME_ERRORS },
6338 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn)memcached_get_by_key_MEMCACHED_NOTFOUND },
6339 {0, 0, (test_callback_fn)0}
6340 };
6341
6342 collection_st collection[] ={
6343 #if 0
6344 {"hash_sanity", 0, 0, hash_sanity},
6345 #endif
6346 {"hsieh_availability", 0, 0, hsieh_availability},
6347 {"block", 0, 0, tests},
6348 {"binary", (test_callback_fn)pre_binary, 0, tests},
6349 {"nonblock", (test_callback_fn)pre_nonblock, 0, tests},
6350 {"nodelay", (test_callback_fn)pre_nodelay, 0, tests},
6351 {"settimer", (test_callback_fn)pre_settimer, 0, tests},
6352 {"md5", (test_callback_fn)pre_md5, 0, tests},
6353 {"crc", (test_callback_fn)pre_crc, 0, tests},
6354 {"hsieh", (test_callback_fn)pre_hsieh, 0, tests},
6355 {"jenkins", (test_callback_fn)pre_jenkins, 0, tests},
6356 {"fnv1_64", (test_callback_fn)pre_hash_fnv1_64, 0, tests},
6357 {"fnv1a_64", (test_callback_fn)pre_hash_fnv1a_64, 0, tests},
6358 {"fnv1_32", (test_callback_fn)pre_hash_fnv1_32, 0, tests},
6359 {"fnv1a_32", (test_callback_fn)pre_hash_fnv1a_32, 0, tests},
6360 {"ketama", (test_callback_fn)pre_behavior_ketama, 0, tests},
6361 {"ketama_auto_eject_hosts", (test_callback_fn)pre_behavior_ketama, 0, ketama_auto_eject_hosts},
6362 {"unix_socket", (test_callback_fn)pre_unix_socket, 0, tests},
6363 {"unix_socket_nodelay", (test_callback_fn)pre_nodelay, 0, tests},
6364 {"poll_timeout", (test_callback_fn)poll_timeout, 0, tests},
6365 {"gets", (test_callback_fn)enable_cas, 0, tests},
6366 {"consistent_crc", (test_callback_fn)enable_consistent_crc, 0, tests},
6367 {"consistent_hsieh", (test_callback_fn)enable_consistent_hsieh, 0, tests},
6368 #ifdef MEMCACHED_ENABLE_DEPRECATED
6369 {"deprecated_memory_allocators", (test_callback_fn)deprecated_set_memory_alloc, 0, tests},
6370 #endif
6371 {"memory_allocators", (test_callback_fn)set_memory_alloc, 0, tests},
6372 {"prefix", (test_callback_fn)set_prefix, 0, tests},
6373 {"sasl_auth", (test_callback_fn)pre_sasl, 0, sasl_auth_tests },
6374 {"sasl", (test_callback_fn)pre_sasl, 0, tests },
6375 {"version_1_2_3", (test_callback_fn)check_for_1_2_3, 0, version_1_2_3},
6376 {"string", 0, 0, string_tests},
6377 {"result", 0, 0, result_tests},
6378 {"async", (test_callback_fn)pre_nonblock, 0, async_tests},
6379 {"async_binary", (test_callback_fn)pre_nonblock_binary, 0, async_tests},
6380 {"user", 0, 0, user_tests},
6381 {"generate", 0, 0, generate_tests},
6382 {"generate_hsieh", (test_callback_fn)pre_hsieh, 0, generate_tests},
6383 {"generate_ketama", (test_callback_fn)pre_behavior_ketama, 0, generate_tests},
6384 {"generate_hsieh_consistent", (test_callback_fn)enable_consistent_hsieh, 0, generate_tests},
6385 {"generate_md5", (test_callback_fn)pre_md5, 0, generate_tests},
6386 {"generate_murmur", (test_callback_fn)pre_murmur, 0, generate_tests},
6387 {"generate_jenkins", (test_callback_fn)pre_jenkins, 0, generate_tests},
6388 {"generate_nonblock", (test_callback_fn)pre_nonblock, 0, generate_tests},
6389 // Too slow
6390 {"generate_corked", (test_callback_fn)pre_cork, 0, generate_tests},
6391 {"generate_corked_and_nonblock", (test_callback_fn)pre_cork_and_nonblock, 0, generate_tests},
6392 {"consistent_not", 0, 0, consistent_tests},
6393 {"consistent_ketama", (test_callback_fn)pre_behavior_ketama, 0, consistent_tests},
6394 {"consistent_ketama_weighted", (test_callback_fn)pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
6395 {"ketama_compat", 0, 0, ketama_compatibility},
6396 {"test_hashes", 0, 0, hash_tests},
6397 {"replication", (test_callback_fn)pre_replication, 0, replication_tests},
6398 {"replication_noblock", (test_callback_fn)pre_replication_noblock, 0, replication_tests},
6399 {"regression", 0, 0, regression_tests},
6400 {"behaviors", 0, 0, behavior_tests},
6401 {"regression_binary_vs_block", (test_callback_fn)key_setup, (test_callback_fn)key_teardown, regression_binary_vs_block},
6402 {"error_conditions", 0, 0, error_conditions},
6403 {0, 0, 0, 0}
6404 };
6405
6406 #define SERVERS_TO_CREATE 5
6407
6408 #include "libmemcached_world.h"
6409
6410 void get_world(world_st *world)
6411 {
6412 world->collections= collection;
6413
6414 world->create= (test_callback_create_fn)world_create;
6415 world->destroy= (test_callback_fn)world_destroy;
6416
6417 world->test.startup= (test_callback_fn)world_test_startup;
6418 world->test.flush= (test_callback_fn)world_flush;
6419 world->test.pre_run= (test_callback_fn)world_pre_run;
6420 world->test.post_run= (test_callback_fn)world_post_run;
6421 world->test.on_error= (test_callback_error_fn)world_on_error;
6422
6423 world->collection.startup= (test_callback_fn)world_container_startup;
6424 world->collection.shutdown= (test_callback_fn)world_container_shutdown;
6425
6426 world->runner= &defualt_libmemcached_runner;
6427 }