Added libmemcached_ping() to memcached utility library.
[awesomized/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 memcached_server_instance_st instance;
3596
3597 memc_clone= memcached_clone(NULL, memc);
3598 test_true(memc_clone);
3599 // The memcached_version needs to be done on a clone, because the server
3600 // will not toggle protocol on an connection.
3601 memcached_version(memc_clone);
3602
3603 instance= memcached_server_instance_by_position(memc_clone, 0);
3604
3605 if (instance->major_version >= 1 && instance->minor_version > 2)
3606 {
3607 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3608 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3609 test_true(rc == MEMCACHED_SUCCESS);
3610 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3611 }
3612 else
3613 {
3614 return TEST_SKIPPED;
3615 }
3616
3617 memcached_free(memc_clone);
3618
3619 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3620 }
3621
3622 static test_return_t pre_murmur(memcached_st *memc)
3623 {
3624 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
3625
3626 return TEST_SUCCESS;
3627 }
3628
3629 static test_return_t pre_jenkins(memcached_st *memc)
3630 {
3631 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
3632
3633 return TEST_SUCCESS;
3634 }
3635
3636
3637 static test_return_t pre_md5(memcached_st *memc)
3638 {
3639 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
3640
3641 return TEST_SUCCESS;
3642 }
3643
3644 static test_return_t pre_crc(memcached_st *memc)
3645 {
3646 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
3647
3648 return TEST_SUCCESS;
3649 }
3650
3651 static test_return_t pre_hsieh(memcached_st *memc)
3652 {
3653 #ifdef HAVE_HSIEH_HASH
3654 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
3655 return TEST_SUCCESS;
3656 #else
3657 (void) memc;
3658 return TEST_SKIPPED;
3659 #endif
3660 }
3661
3662 static test_return_t pre_hash_fnv1_64(memcached_st *memc)
3663 {
3664 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
3665
3666 return TEST_SUCCESS;
3667 }
3668
3669 static test_return_t pre_hash_fnv1a_64(memcached_st *memc)
3670 {
3671 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
3672
3673 return TEST_SUCCESS;
3674 }
3675
3676 static test_return_t pre_hash_fnv1_32(memcached_st *memc)
3677 {
3678 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
3679
3680 return TEST_SUCCESS;
3681 }
3682
3683 static test_return_t pre_hash_fnv1a_32(memcached_st *memc)
3684 {
3685 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
3686
3687 return TEST_SUCCESS;
3688 }
3689
3690 static test_return_t pre_behavior_ketama(memcached_st *memc)
3691 {
3692 memcached_return_t rc;
3693 uint64_t value;
3694
3695 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
3696 test_true(rc == MEMCACHED_SUCCESS);
3697
3698 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
3699 test_true(value == 1);
3700
3701 return TEST_SUCCESS;
3702 }
3703
3704 static test_return_t pre_behavior_ketama_weighted(memcached_st *memc)
3705 {
3706 memcached_return_t rc;
3707 uint64_t value;
3708
3709 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3710 test_true(rc == MEMCACHED_SUCCESS);
3711
3712 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3713 test_true(value == 1);
3714
3715 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3716 test_true(rc == MEMCACHED_SUCCESS);
3717
3718 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3719 test_true(value == MEMCACHED_HASH_MD5);
3720
3721 return TEST_SUCCESS;
3722 }
3723
3724 /**
3725 @note This should be testing to see if the server really supports the binary protocol.
3726 */
3727 static test_return_t pre_binary(memcached_st *memc)
3728 {
3729 memcached_return_t rc= MEMCACHED_FAILURE;
3730 memcached_st *memc_clone;
3731 memcached_server_instance_st instance;
3732
3733 memc_clone= memcached_clone(NULL, memc);
3734 test_true(memc_clone);
3735 // The memcached_version needs to be done on a clone, because the server
3736 // will not toggle protocol on an connection.
3737 memcached_version(memc_clone);
3738
3739 instance= memcached_server_instance_by_position(memc_clone, 0);
3740
3741 if (instance->major_version >= 1 && instance->minor_version > 2)
3742 {
3743 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3744 test_true(rc == MEMCACHED_SUCCESS);
3745 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3746 }
3747
3748 memcached_free(memc_clone);
3749
3750 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3751 }
3752
3753 static test_return_t pre_sasl(memcached_st *memc)
3754 {
3755 memcached_return_t rc= MEMCACHED_FAILURE;
3756
3757 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3758 const char *server= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3759 const char *user= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3760 const char *pass= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3761
3762 if (server != NULL && user != NULL && pass != NULL)
3763 {
3764 memcached_server_st *servers= memcached_servers_parse(server);
3765 test_true(servers != NULL);
3766 memcached_servers_reset(memc);
3767 test_true(memcached_server_push(memc, servers) == MEMCACHED_SUCCESS);
3768 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3769 rc= memcached_set_sasl_auth_data(memc, user, pass);
3770 test_true(rc == MEMCACHED_SUCCESS);
3771 }
3772 #else
3773 (void)memc;
3774 #endif
3775
3776 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3777 }
3778
3779 static test_return_t pre_replication(memcached_st *memc)
3780 {
3781 test_return_t test_rc;
3782 test_rc= pre_binary(memc);
3783
3784 if (test_rc != TEST_SUCCESS)
3785 return test_rc;
3786
3787 /*
3788 * Make sure that we store the item on all servers
3789 * (master + replicas == number of servers)
3790 */
3791 memcached_return_t rc;
3792 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
3793 memcached_server_count(memc) - 1);
3794 test_true(rc == MEMCACHED_SUCCESS);
3795 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS) == memcached_server_count(memc) - 1);
3796
3797 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
3798 }
3799
3800
3801 static test_return_t pre_replication_noblock(memcached_st *memc)
3802 {
3803 test_return_t rc;
3804
3805 rc= pre_replication(memc);
3806 if (rc != TEST_SUCCESS)
3807 return rc;
3808
3809 rc= pre_nonblock(memc);
3810
3811 return rc;
3812 }
3813
3814
3815 static void my_free(const memcached_st *ptr __attribute__((unused)), void *mem, void *context)
3816 {
3817 (void) context;
3818 #ifdef HARD_MALLOC_TESTS
3819 void *real_ptr= (mem == NULL) ? mem : (void*)((caddr_t)mem - 8);
3820 free(real_ptr);
3821 #else
3822 free(mem);
3823 #endif
3824 }
3825
3826
3827 static void *my_malloc(const memcached_st *ptr __attribute__((unused)), const size_t size, void *context)
3828 {
3829 (void)context;
3830 #ifdef HARD_MALLOC_TESTS
3831 void *ret= malloc(size + 8);
3832 if (ret != NULL)
3833 {
3834 ret= (void*)((caddr_t)ret + 8);
3835 }
3836 #else
3837 void *ret= malloc(size);
3838 #endif
3839
3840 if (ret != NULL)
3841 {
3842 memset(ret, 0xff, size);
3843 }
3844
3845 return ret;
3846 }
3847
3848
3849 static void *my_realloc(const memcached_st *ptr __attribute__((unused)), void *mem, const size_t size, void *context)
3850 {
3851 (void)context;
3852 #ifdef HARD_MALLOC_TESTS
3853 void *real_ptr= (mem == NULL) ? NULL : (void*)((caddr_t)mem - 8);
3854 void *nmem= realloc(real_ptr, size + 8);
3855
3856 void *ret= NULL;
3857 if (nmem != NULL)
3858 {
3859 ret= (void*)((caddr_t)nmem + 8);
3860 }
3861
3862 return ret;
3863 #else
3864 return realloc(mem, size);
3865 #endif
3866 }
3867
3868
3869 static void *my_calloc(const memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size, void *context)
3870 {
3871 (void)context;
3872 #ifdef HARD_MALLOC_TESTS
3873 void *mem= my_malloc(ptr, nelem * size);
3874 if (mem)
3875 {
3876 memset(mem, 0, nelem * size);
3877 }
3878
3879 return mem;
3880 #else
3881 return calloc(nelem, size);
3882 #endif
3883 }
3884
3885
3886 static test_return_t set_prefix(memcached_st *memc)
3887 {
3888 memcached_return_t rc;
3889 const char *key= "mine";
3890 char *value;
3891
3892 /* Make sure be default none exists */
3893 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3894 test_true(rc == MEMCACHED_FAILURE);
3895
3896 /* Test a clean set */
3897 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3898 test_true(rc == MEMCACHED_SUCCESS);
3899
3900 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3901 test_true(memcmp(value, key, 4) == 0);
3902 test_true(rc == MEMCACHED_SUCCESS);
3903
3904 /* Test that we can turn it off */
3905 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3906 test_true(rc == MEMCACHED_SUCCESS);
3907
3908 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3909 test_true(rc == MEMCACHED_FAILURE);
3910
3911 /* Now setup for main test */
3912 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3913 test_true(rc == MEMCACHED_SUCCESS);
3914
3915 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3916 test_true(rc == MEMCACHED_SUCCESS);
3917 test_true(memcmp(value, key, 4) == 0);
3918
3919 /* Set to Zero, and then Set to something too large */
3920 {
3921 char long_key[255];
3922 memset(long_key, 0, 255);
3923
3924 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3925 test_true(rc == MEMCACHED_SUCCESS);
3926
3927 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3928 test_true(rc == MEMCACHED_FAILURE);
3929 test_true(value == NULL);
3930
3931 /* Test a long key for failure */
3932 /* TODO, extend test to determine based on setting, what result should be */
3933 strcpy(long_key, "Thisismorethentheallottednumberofcharacters");
3934 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3935 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3936 test_true(rc == MEMCACHED_SUCCESS);
3937
3938 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3939 strcpy(long_key, "This is more then the allotted number of characters");
3940 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3941 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3942
3943 /* Test for a bad prefix, but with a short key */
3944 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
3945 test_true(rc == MEMCACHED_SUCCESS);
3946
3947 strcpy(long_key, "dog cat");
3948 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3949 test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3950 }
3951
3952 return TEST_SUCCESS;
3953 }
3954
3955
3956 #ifdef MEMCACHED_ENABLE_DEPRECATED
3957 static test_return_t deprecated_set_memory_alloc(memcached_st *memc)
3958 {
3959 void *test_ptr= NULL;
3960 void *cb_ptr= NULL;
3961 {
3962 memcached_malloc_fn malloc_cb=
3963 (memcached_malloc_fn)my_malloc;
3964 cb_ptr= *(void **)&malloc_cb;
3965 memcached_return_t rc;
3966
3967 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
3968 test_true(rc == MEMCACHED_SUCCESS);
3969 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
3970 test_true(rc == MEMCACHED_SUCCESS);
3971 test_true(test_ptr == cb_ptr);
3972 }
3973
3974 {
3975 memcached_realloc_fn realloc_cb=
3976 (memcached_realloc_fn)my_realloc;
3977 cb_ptr= *(void **)&realloc_cb;
3978 memcached_return_t rc;
3979
3980 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
3981 test_true(rc == MEMCACHED_SUCCESS);
3982 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
3983 test_true(rc == MEMCACHED_SUCCESS);
3984 test_true(test_ptr == cb_ptr);
3985 }
3986
3987 {
3988 memcached_free_fn free_cb=
3989 (memcached_free_fn)my_free;
3990 cb_ptr= *(void **)&free_cb;
3991 memcached_return_t rc;
3992
3993 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
3994 test_true(rc == MEMCACHED_SUCCESS);
3995 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
3996 test_true(rc == MEMCACHED_SUCCESS);
3997 test_true(test_ptr == cb_ptr);
3998 }
3999
4000 return TEST_SUCCESS;
4001 }
4002 #endif
4003
4004
4005 static test_return_t set_memory_alloc(memcached_st *memc)
4006 {
4007 memcached_return_t rc;
4008 rc= memcached_set_memory_allocators(memc, NULL, my_free,
4009 my_realloc, my_calloc, NULL);
4010 test_true(rc == MEMCACHED_FAILURE);
4011
4012 rc= memcached_set_memory_allocators(memc, my_malloc, my_free,
4013 my_realloc, my_calloc, NULL);
4014
4015 memcached_malloc_fn mem_malloc;
4016 memcached_free_fn mem_free;
4017 memcached_realloc_fn mem_realloc;
4018 memcached_calloc_fn mem_calloc;
4019 memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
4020 &mem_realloc, &mem_calloc);
4021
4022 test_true(mem_malloc == my_malloc);
4023 test_true(mem_realloc == my_realloc);
4024 test_true(mem_calloc == my_calloc);
4025 test_true(mem_free == my_free);
4026
4027 return TEST_SUCCESS;
4028 }
4029
4030 static test_return_t enable_consistent_crc(memcached_st *memc)
4031 {
4032 test_return_t rc;
4033 memcached_server_distribution_t value= MEMCACHED_DISTRIBUTION_CONSISTENT;
4034 memcached_hash_t hash;
4035 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
4036 if ((rc= pre_crc(memc)) != TEST_SUCCESS)
4037 return rc;
4038
4039 value= (memcached_server_distribution_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
4040 test_true(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
4041
4042 hash= (memcached_hash_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
4043
4044 if (hash != MEMCACHED_HASH_CRC)
4045 return TEST_SKIPPED;
4046
4047 return TEST_SUCCESS;
4048 }
4049
4050 static test_return_t enable_consistent_hsieh(memcached_st *memc)
4051 {
4052 test_return_t rc;
4053 memcached_server_distribution_t value= MEMCACHED_DISTRIBUTION_CONSISTENT;
4054 memcached_hash_t hash;
4055 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
4056 if ((rc= pre_hsieh(memc)) != TEST_SUCCESS)
4057 return rc;
4058
4059 value= (memcached_server_distribution_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
4060 test_true(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
4061
4062 hash= (memcached_hash_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
4063
4064 if (hash != MEMCACHED_HASH_HSIEH)
4065 return TEST_SKIPPED;
4066
4067
4068 return TEST_SUCCESS;
4069 }
4070
4071 static test_return_t enable_cas(memcached_st *memc)
4072 {
4073 unsigned int set= 1;
4074
4075 memcached_server_instance_st instance=
4076 memcached_server_instance_by_position(memc, 0);
4077
4078 memcached_version(memc);
4079
4080 if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4))
4081 || instance->minor_version > 2)
4082 {
4083 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
4084
4085 return TEST_SUCCESS;
4086 }
4087
4088 return TEST_SKIPPED;
4089 }
4090
4091 static test_return_t check_for_1_2_3(memcached_st *memc)
4092 {
4093 memcached_version(memc);
4094
4095 memcached_server_instance_st instance=
4096 memcached_server_instance_by_position(memc, 0);
4097
4098 if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4))
4099 || instance->minor_version > 2)
4100 return TEST_SUCCESS;
4101
4102 return TEST_SKIPPED;
4103 }
4104
4105 static test_return_t pre_unix_socket(memcached_st *memc)
4106 {
4107 memcached_return_t rc;
4108 struct stat buf;
4109
4110 memcached_servers_reset(memc);
4111
4112 if (stat("/tmp/memcached.socket", &buf))
4113 return TEST_SKIPPED;
4114
4115 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
4116
4117 return ( rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_FAILURE );
4118 }
4119
4120 static test_return_t pre_nodelay(memcached_st *memc)
4121 {
4122 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
4123 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
4124
4125 return TEST_SUCCESS;
4126 }
4127
4128 static test_return_t pre_settimer(memcached_st *memc)
4129 {
4130 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
4131 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
4132
4133 return TEST_SUCCESS;
4134 }
4135
4136 static test_return_t poll_timeout(memcached_st *memc)
4137 {
4138 size_t timeout;
4139
4140 timeout= 100;
4141
4142 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
4143
4144 timeout= (size_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
4145
4146 test_true(timeout == 100);
4147
4148 return TEST_SUCCESS;
4149 }
4150
4151 static test_return_t noreply_test(memcached_st *memc)
4152 {
4153 memcached_return_t ret;
4154 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
4155 test_true(ret == MEMCACHED_SUCCESS);
4156 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
4157 test_true(ret == MEMCACHED_SUCCESS);
4158 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
4159 test_true(ret == MEMCACHED_SUCCESS);
4160 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
4161 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
4162 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
4163
4164 for (int count=0; count < 5; ++count)
4165 {
4166 for (size_t x= 0; x < 100; ++x)
4167 {
4168 char key[10];
4169 size_t len= (size_t)sprintf(key, "%zu", x);
4170 switch (count)
4171 {
4172 case 0:
4173 ret= memcached_add(memc, key, len, key, len, 0, 0);
4174 break;
4175 case 1:
4176 ret= memcached_replace(memc, key, len, key, len, 0, 0);
4177 break;
4178 case 2:
4179 ret= memcached_set(memc, key, len, key, len, 0, 0);
4180 break;
4181 case 3:
4182 ret= memcached_append(memc, key, len, key, len, 0, 0);
4183 break;
4184 case 4:
4185 ret= memcached_prepend(memc, key, len, key, len, 0, 0);
4186 break;
4187 default:
4188 test_true(count);
4189 break;
4190 }
4191 test_true(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
4192 }
4193
4194 /*
4195 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4196 ** API and is _ONLY_ done this way to verify that the library works the
4197 ** way it is supposed to do!!!!
4198 */
4199 int no_msg=0;
4200 for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
4201 {
4202 memcached_server_instance_st instance=
4203 memcached_server_instance_by_position(memc, x);
4204 no_msg+=(int)(instance->cursor_active);
4205 }
4206
4207 test_true(no_msg == 0);
4208 test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
4209
4210 /*
4211 ** Now validate that all items was set properly!
4212 */
4213 for (size_t x= 0; x < 100; ++x)
4214 {
4215 char key[10];
4216
4217 size_t len= (size_t)sprintf(key, "%zu", x);
4218 size_t length;
4219 uint32_t flags;
4220 char* value=memcached_get(memc, key, strlen(key),
4221 &length, &flags, &ret);
4222 test_true(ret == MEMCACHED_SUCCESS && value != NULL);
4223 switch (count)
4224 {
4225 case 0: /* FALLTHROUGH */
4226 case 1: /* FALLTHROUGH */
4227 case 2:
4228 test_true(strncmp(value, key, len) == 0);
4229 test_true(len == length);
4230 break;
4231 case 3:
4232 test_true(length == len * 2);
4233 break;
4234 case 4:
4235 test_true(length == len * 3);
4236 break;
4237 default:
4238 test_true(count);
4239 break;
4240 }
4241 free(value);
4242 }
4243 }
4244
4245 /* Try setting an illegal cas value (should not return an error to
4246 * the caller (because we don't expect a return message from the server)
4247 */
4248 const char* keys[]= {"0"};
4249 size_t lengths[]= {1};
4250 size_t length;
4251 uint32_t flags;
4252 memcached_result_st results_obj;
4253 memcached_result_st *results;
4254 ret= memcached_mget(memc, keys, lengths, 1);
4255 test_true(ret == MEMCACHED_SUCCESS);
4256
4257 results= memcached_result_create(memc, &results_obj);
4258 test_true(results);
4259 results= memcached_fetch_result(memc, &results_obj, &ret);
4260 test_true(results);
4261 test_true(ret == MEMCACHED_SUCCESS);
4262 uint64_t cas= memcached_result_cas(results);
4263 memcached_result_free(&results_obj);
4264
4265 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
4266 test_true(ret == MEMCACHED_SUCCESS);
4267
4268 /*
4269 * The item will have a new cas value, so try to set it again with the old
4270 * value. This should fail!
4271 */
4272 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
4273 test_true(ret == MEMCACHED_SUCCESS);
4274 test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
4275 char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
4276 test_true(ret == MEMCACHED_SUCCESS && value != NULL);
4277 free(value);
4278
4279 return TEST_SUCCESS;
4280 }
4281
4282 static test_return_t analyzer_test(memcached_st *memc)
4283 {
4284 memcached_return_t rc;
4285 memcached_stat_st *memc_stat;
4286 memcached_analysis_st *report;
4287
4288 memc_stat= memcached_stat(memc, NULL, &rc);
4289 test_true(rc == MEMCACHED_SUCCESS);
4290 test_true(memc_stat);
4291
4292 report= memcached_analyze(memc, memc_stat, &rc);
4293 test_true(rc == MEMCACHED_SUCCESS);
4294 test_true(report);
4295
4296 free(report);
4297 memcached_stat_free(NULL, memc_stat);
4298
4299 return TEST_SUCCESS;
4300 }
4301
4302 /* Count the objects */
4303 static memcached_return_t callback_dump_counter(const memcached_st *ptr __attribute__((unused)),
4304 const char *key __attribute__((unused)),
4305 size_t key_length __attribute__((unused)),
4306 void *context)
4307 {
4308 size_t *counter= (size_t *)context;
4309
4310 *counter= *counter + 1;
4311
4312 return MEMCACHED_SUCCESS;
4313 }
4314
4315 static test_return_t dump_test(memcached_st *memc)
4316 {
4317 memcached_return_t rc;
4318 size_t counter= 0;
4319 memcached_dump_fn callbacks[1];
4320 test_return_t main_rc;
4321
4322 callbacks[0]= &callback_dump_counter;
4323
4324 /* No support for Binary protocol yet */
4325 if (memc->flags.binary_protocol)
4326 return TEST_SUCCESS;
4327
4328 main_rc= set_test3(memc);
4329
4330 test_true (main_rc == TEST_SUCCESS);
4331
4332 rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
4333 test_true(rc == MEMCACHED_SUCCESS);
4334
4335 /* We may have more then 32 if our previous flush has not completed */
4336 test_true(counter >= 32);
4337
4338 return TEST_SUCCESS;
4339 }
4340
4341 #ifdef HAVE_LIBMEMCACHEDUTIL
4342 static void* connection_release(void *arg)
4343 {
4344 struct {
4345 memcached_pool_st* pool;
4346 memcached_st* mmc;
4347 } *resource= arg;
4348
4349 usleep(250);
4350 assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
4351 return arg;
4352 }
4353
4354 static test_return_t connection_pool_test(memcached_st *memc)
4355 {
4356 memcached_pool_st* pool= memcached_pool_create(memc, 5, 10);
4357 test_true(pool != NULL);
4358 memcached_st* mmc[10];
4359 memcached_return_t rc;
4360
4361 for (size_t x= 0; x < 10; ++x)
4362 {
4363 mmc[x]= memcached_pool_pop(pool, false, &rc);
4364 test_true(mmc[x] != NULL);
4365 test_true(rc == MEMCACHED_SUCCESS);
4366 }
4367
4368 test_true(memcached_pool_pop(pool, false, &rc) == NULL);
4369 test_true(rc == MEMCACHED_SUCCESS);
4370
4371 pthread_t tid;
4372 struct {
4373 memcached_pool_st* pool;
4374 memcached_st* mmc;
4375 } item= { .pool = pool, .mmc = mmc[9] };
4376 pthread_create(&tid, NULL, connection_release, &item);
4377 mmc[9]= memcached_pool_pop(pool, true, &rc);
4378 test_true(rc == MEMCACHED_SUCCESS);
4379 pthread_join(tid, NULL);
4380 test_true(mmc[9] == item.mmc);
4381 const char *key= "key";
4382 size_t keylen= strlen(key);
4383
4384 // verify that I can do ops with all connections
4385 rc= memcached_set(mmc[0], key, keylen, "0", 1, 0, 0);
4386 test_true(rc == MEMCACHED_SUCCESS);
4387
4388 for (size_t x= 0; x < 10; ++x)
4389 {
4390 uint64_t number_value;
4391 rc= memcached_increment(mmc[x], key, keylen, 1, &number_value);
4392 test_true(rc == MEMCACHED_SUCCESS);
4393 test_true(number_value == (x+1));
4394 }
4395
4396 // Release them..
4397 for (size_t x= 0; x < 10; ++x)
4398 {
4399 test_true(memcached_pool_push(pool, mmc[x]) == MEMCACHED_SUCCESS);
4400 }
4401
4402
4403 /* verify that I can set behaviors on the pool when I don't have all
4404 * of the connections in the pool. It should however be enabled
4405 * when I push the item into the pool
4406 */
4407 mmc[0]= memcached_pool_pop(pool, false, &rc);
4408 test_true(mmc[0] != NULL);
4409
4410 rc= memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999);
4411 test_true(rc == MEMCACHED_SUCCESS);
4412
4413 mmc[1]= memcached_pool_pop(pool, false, &rc);
4414 test_true(mmc[1] != NULL);
4415
4416 test_true(memcached_behavior_get(mmc[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK) == 9999);
4417 test_true(memcached_pool_push(pool, mmc[1]) == MEMCACHED_SUCCESS);
4418 test_true(memcached_pool_push(pool, mmc[0]) == MEMCACHED_SUCCESS);
4419
4420 mmc[0]= memcached_pool_pop(pool, false, &rc);
4421 test_true(memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK) == 9999);
4422 test_true(memcached_pool_push(pool, mmc[0]) == MEMCACHED_SUCCESS);
4423
4424
4425 test_true(memcached_pool_destroy(pool) == memc);
4426 return TEST_SUCCESS;
4427 }
4428
4429 static test_return_t ping_test(memcached_st *memc)
4430 {
4431 memcached_return_t rc;
4432 memcached_server_instance_st instance=
4433 memcached_server_instance_by_position(memc, 0);
4434
4435 // Test both the version that returns a code, and the one that does not.
4436 test_true(libmemcached_ping(memcached_server_name(instance),
4437 memcached_server_port(instance), NULL));
4438
4439 test_true(libmemcached_ping(memcached_server_name(instance),
4440 memcached_server_port(instance), &rc));
4441
4442 test_true(rc == MEMCACHED_SUCCESS);
4443
4444 return TEST_SUCCESS;
4445 }
4446 #endif
4447
4448 static test_return_t replication_set_test(memcached_st *memc)
4449 {
4450 memcached_return_t rc;
4451 memcached_st *memc_clone= memcached_clone(NULL, memc);
4452 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
4453
4454 rc= memcached_set(memc, "bubba", 5, "0", 1, 0, 0);
4455 test_true(rc == MEMCACHED_SUCCESS);
4456
4457 /*
4458 ** We are using the quiet commands to store the replicas, so we need
4459 ** to ensure that all of them are processed before we can continue.
4460 ** In the test we go directly from storing the object to trying to
4461 ** receive the object from all of the different servers, so we
4462 ** could end up in a race condition (the memcached server hasn't yet
4463 ** processed the quiet command from the replication set when it process
4464 ** the request from the other client (created by the clone)). As a
4465 ** workaround for that we call memcached_quit to send the quit command
4466 ** to the server and wait for the response ;-) If you use the test code
4467 ** as an example for your own code, please note that you shouldn't need
4468 ** to do this ;-)
4469 */
4470 memcached_quit(memc);
4471
4472 /*
4473 ** "bubba" should now be stored on all of our servers. We don't have an
4474 ** easy to use API to address each individual server, so I'll just iterate
4475 ** through a bunch of "master keys" and I should most likely hit all of the
4476 ** servers...
4477 */
4478 for (int x= 'a'; x <= 'z'; ++x)
4479 {
4480 char key[2]= { [0]= (char)x };
4481 size_t len;
4482 uint32_t flags;
4483 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
4484 &len, &flags, &rc);
4485 test_true(rc == MEMCACHED_SUCCESS);
4486 test_true(val != NULL);
4487 free(val);
4488 }
4489
4490 memcached_free(memc_clone);
4491
4492 return TEST_SUCCESS;
4493 }
4494
4495 static test_return_t replication_get_test(memcached_st *memc)
4496 {
4497 memcached_return_t rc;
4498
4499 /*
4500 * Don't do the following in your code. I am abusing the internal details
4501 * within the library, and this is not a supported interface.
4502 * This is to verify correct behavior in the library
4503 */
4504 for (uint32_t host= 0; host < memcached_server_count(memc); ++host)
4505 {
4506 memcached_st *memc_clone= memcached_clone(NULL, memc);
4507 memcached_server_instance_st instance=
4508 memcached_server_instance_by_position(memc_clone, host);
4509
4510 ((memcached_server_write_instance_st)instance)->port= 0;
4511
4512 for (int x= 'a'; x <= 'z'; ++x)
4513 {
4514 char key[2]= { [0]= (char)x };
4515 size_t len;
4516 uint32_t flags;
4517 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
4518 &len, &flags, &rc);
4519 test_true(rc == MEMCACHED_SUCCESS);
4520 test_true(val != NULL);
4521 free(val);
4522 }
4523
4524 memcached_free(memc_clone);
4525 }
4526
4527 return TEST_SUCCESS;
4528 }
4529
4530 static test_return_t replication_mget_test(memcached_st *memc)
4531 {
4532 memcached_return_t rc;
4533 memcached_st *memc_clone= memcached_clone(NULL, memc);
4534 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
4535
4536 const char *keys[]= { "bubba", "key1", "key2", "key3" };
4537 size_t len[]= { 5, 4, 4, 4 };
4538
4539 for (size_t x= 0; x< 4; ++x)
4540 {
4541 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
4542 test_true(rc == MEMCACHED_SUCCESS);
4543 }
4544
4545 /*
4546 ** We are using the quiet commands to store the replicas, so we need
4547 ** to ensure that all of them are processed before we can continue.
4548 ** In the test we go directly from storing the object to trying to
4549 ** receive the object from all of the different servers, so we
4550 ** could end up in a race condition (the memcached server hasn't yet
4551 ** processed the quiet command from the replication set when it process
4552 ** the request from the other client (created by the clone)). As a
4553 ** workaround for that we call memcached_quit to send the quit command
4554 ** to the server and wait for the response ;-) If you use the test code
4555 ** as an example for your own code, please note that you shouldn't need
4556 ** to do this ;-)
4557 */
4558 memcached_quit(memc);
4559
4560 /*
4561 * Don't do the following in your code. I am abusing the internal details
4562 * within the library, and this is not a supported interface.
4563 * This is to verify correct behavior in the library
4564 */
4565 memcached_result_st result_obj;
4566 for (uint32_t host= 0; host < memc_clone->number_of_hosts; host++)
4567 {
4568 memcached_st *new_clone= memcached_clone(NULL, memc);
4569 memcached_server_instance_st instance=
4570 memcached_server_instance_by_position(new_clone, host);
4571 ((memcached_server_write_instance_st)instance)->port= 0;
4572
4573 for (int x= 'a'; x <= 'z'; ++x)
4574 {
4575 char key[2]= { [0]= (char)x, [1]= 0 };
4576
4577 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
4578 test_true(rc == MEMCACHED_SUCCESS);
4579
4580 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
4581 test_true(results);
4582
4583 int hits= 0;
4584 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
4585 {
4586 hits++;
4587 }
4588 test_true(hits == 4);
4589 memcached_result_free(&result_obj);
4590 }
4591
4592 memcached_free(new_clone);
4593 }
4594
4595 memcached_free(memc_clone);
4596
4597 return TEST_SUCCESS;
4598 }
4599
4600 static test_return_t replication_randomize_mget_test(memcached_st *memc)
4601 {
4602 memcached_result_st result_obj;
4603 memcached_return_t rc;
4604 memcached_st *memc_clone= memcached_clone(NULL, memc);
4605 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
4606 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1);
4607
4608 const char *keys[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4609 size_t len[]= { 4, 4, 4, 4, 4, 4, 4 };
4610
4611 for (size_t x= 0; x< 7; ++x)
4612 {
4613 rc= memcached_set(memc, keys[x], len[x], "1", 1, 0, 0);
4614 test_true(rc == MEMCACHED_SUCCESS);
4615 }
4616
4617 memcached_quit(memc);
4618
4619 for (size_t x= 0; x< 7; ++x)
4620 {
4621 const char key[2]= { [0]= (const char)x };
4622
4623 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 7);
4624 test_true(rc == MEMCACHED_SUCCESS);
4625
4626 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
4627 test_true(results);
4628
4629 int hits= 0;
4630 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
4631 {
4632 ++hits;
4633 }
4634 test_true(hits == 7);
4635 memcached_result_free(&result_obj);
4636 }
4637 memcached_free(memc_clone);
4638 return TEST_SUCCESS;
4639 }
4640
4641 static test_return_t replication_delete_test(memcached_st *memc)
4642 {
4643 memcached_return_t rc;
4644 memcached_st *memc_clone= memcached_clone(NULL, memc);
4645 /* Delete the items from all of the servers except 1 */
4646 uint64_t repl= memcached_behavior_get(memc,
4647 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
4648 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, --repl);
4649
4650 const char *keys[]= { "bubba", "key1", "key2", "key3" };
4651 size_t len[]= { 5, 4, 4, 4 };
4652
4653 for (size_t x= 0; x< 4; ++x)
4654 {
4655 rc= memcached_delete_by_key(memc, keys[0], len[0], keys[x], len[x], 0);
4656 test_true(rc == MEMCACHED_SUCCESS);
4657 }
4658
4659 /*
4660 * Don't do the following in your code. I am abusing the internal details
4661 * within the library, and this is not a supported interface.
4662 * This is to verify correct behavior in the library
4663 */
4664 uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
4665 for (uint32_t x= 0; x < (repl + 1); ++x)
4666 {
4667 memcached_server_instance_st instance=
4668 memcached_server_instance_by_position(memc_clone, x);
4669
4670 ((memcached_server_write_instance_st)instance)->port= 0;
4671 if (++hash == memc_clone->number_of_hosts)
4672 hash= 0;
4673 }
4674
4675 memcached_result_st result_obj;
4676 for (uint32_t host= 0; host < memc_clone->number_of_hosts; ++host)
4677 {
4678 for (size_t x= 'a'; x <= 'z'; ++x)
4679 {
4680 const char key[2]= { [0]= (const char)x };
4681
4682 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 4);
4683 test_true(rc == MEMCACHED_SUCCESS);
4684
4685 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
4686 test_true(results);
4687
4688 int hits= 0;
4689 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
4690 {
4691 ++hits;
4692 }
4693 test_true(hits == 4);
4694 memcached_result_free(&result_obj);
4695 }
4696 }
4697 memcached_free(memc_clone);
4698
4699 return TEST_SUCCESS;
4700 }
4701
4702 #if 0
4703 static test_return_t hash_sanity_test (memcached_st *memc)
4704 {
4705 (void)memc;
4706
4707 assert(MEMCACHED_HASH_DEFAULT == MEMCACHED_HASH_DEFAULT);
4708 assert(MEMCACHED_HASH_MD5 == MEMCACHED_HASH_MD5);
4709 assert(MEMCACHED_HASH_CRC == MEMCACHED_HASH_CRC);
4710 assert(MEMCACHED_HASH_FNV1_64 == MEMCACHED_HASH_FNV1_64);
4711 assert(MEMCACHED_HASH_FNV1A_64 == MEMCACHED_HASH_FNV1A_64);
4712 assert(MEMCACHED_HASH_FNV1_32 == MEMCACHED_HASH_FNV1_32);
4713 assert(MEMCACHED_HASH_FNV1A_32 == MEMCACHED_HASH_FNV1A_32);
4714 #ifdef HAVE_HSIEH_HASH
4715 assert(MEMCACHED_HASH_HSIEH == MEMCACHED_HASH_HSIEH);
4716 #endif
4717 assert(MEMCACHED_HASH_MURMUR == MEMCACHED_HASH_MURMUR);
4718 assert(MEMCACHED_HASH_JENKINS == MEMCACHED_HASH_JENKINS);
4719 assert(MEMCACHED_HASH_MAX == MEMCACHED_HASH_MAX);
4720
4721 return TEST_SUCCESS;
4722 }
4723 #endif
4724
4725 static test_return_t hsieh_avaibility_test (memcached_st *memc)
4726 {
4727 memcached_return_t expected_rc= MEMCACHED_FAILURE;
4728 #ifdef HAVE_HSIEH_HASH
4729 expected_rc= MEMCACHED_SUCCESS;
4730 #endif
4731 memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
4732 (uint64_t)MEMCACHED_HASH_HSIEH);
4733 test_true(rc == expected_rc);
4734
4735 return TEST_SUCCESS;
4736 }
4737
4738 static test_return_t one_at_a_time_run (memcached_st *memc __attribute__((unused)))
4739 {
4740 uint32_t x;
4741 const char **ptr;
4742
4743 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4744 {
4745 uint32_t hash_val;
4746
4747 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_DEFAULT);
4748 test_true(one_at_a_time_values[x] == hash_val);
4749 }
4750
4751 return TEST_SUCCESS;
4752 }
4753
4754 static test_return_t md5_run (memcached_st *memc __attribute__((unused)))
4755 {
4756 uint32_t x;
4757 const char **ptr;
4758
4759 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4760 {
4761 uint32_t hash_val;
4762
4763 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MD5);
4764 test_true(md5_values[x] == hash_val);
4765 }
4766
4767 return TEST_SUCCESS;
4768 }
4769
4770 static test_return_t crc_run (memcached_st *memc __attribute__((unused)))
4771 {
4772 uint32_t x;
4773 const char **ptr;
4774
4775 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4776 {
4777 uint32_t hash_val;
4778
4779 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_CRC);
4780 test_true(crc_values[x] == hash_val);
4781 }
4782
4783 return TEST_SUCCESS;
4784 }
4785
4786 static test_return_t fnv1_64_run (memcached_st *memc __attribute__((unused)))
4787 {
4788 uint32_t x;
4789 const char **ptr;
4790
4791 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4792 {
4793 uint32_t hash_val;
4794
4795 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4796 test_true(fnv1_64_values[x] == hash_val);
4797 }
4798
4799 return TEST_SUCCESS;
4800 }
4801
4802 static test_return_t fnv1a_64_run (memcached_st *memc __attribute__((unused)))
4803 {
4804 uint32_t x;
4805 const char **ptr;
4806
4807 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4808 {
4809 uint32_t hash_val;
4810
4811 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_64);
4812 test_true(fnv1a_64_values[x] == hash_val);
4813 }
4814
4815 return TEST_SUCCESS;
4816 }
4817
4818 static test_return_t fnv1_32_run (memcached_st *memc __attribute__((unused)))
4819 {
4820 uint32_t x;
4821 const char **ptr;
4822
4823
4824 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4825 {
4826 uint32_t hash_val;
4827
4828 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_32);
4829 test_true(fnv1_32_values[x] == hash_val);
4830 }
4831
4832 return TEST_SUCCESS;
4833 }
4834
4835 static test_return_t fnv1a_32_run (memcached_st *memc __attribute__((unused)))
4836 {
4837 uint32_t x;
4838 const char **ptr;
4839
4840 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4841 {
4842 uint32_t hash_val;
4843
4844 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_32);
4845 test_true(fnv1a_32_values[x] == hash_val);
4846 }
4847
4848 return TEST_SUCCESS;
4849 }
4850
4851 static test_return_t hsieh_run (memcached_st *memc __attribute__((unused)))
4852 {
4853 uint32_t x;
4854 const char **ptr;
4855
4856 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4857 {
4858 uint32_t hash_val;
4859
4860 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_HSIEH);
4861 test_true(hsieh_values[x] == hash_val);
4862 }
4863
4864 return TEST_SUCCESS;
4865 }
4866
4867 static test_return_t murmur_run (memcached_st *memc __attribute__((unused)))
4868 {
4869 #ifdef WORDS_BIGENDIAN
4870 return TEST_SKIPPED;
4871 #else
4872 uint32_t x;
4873 const char **ptr;
4874
4875 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4876 {
4877 uint32_t hash_val;
4878
4879 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MURMUR);
4880 test_true(murmur_values[x] == hash_val);
4881 }
4882
4883 return TEST_SUCCESS;
4884 #endif
4885 }
4886
4887 static test_return_t jenkins_run (memcached_st *memc __attribute__((unused)))
4888 {
4889 uint32_t x;
4890 const char **ptr;
4891
4892
4893 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4894 {
4895 uint32_t hash_val;
4896
4897 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_JENKINS);
4898 test_true(jenkins_values[x] == hash_val);
4899 }
4900
4901 return TEST_SUCCESS;
4902 }
4903
4904 static uint32_t hash_md5_test_function(const char *string, size_t string_length, void *context)
4905 {
4906 (void)context;
4907 return libhashkit_md5(string, string_length);
4908 }
4909
4910 static uint32_t hash_crc_test_function(const char *string, size_t string_length, void *context)
4911 {
4912 (void)context;
4913 return libhashkit_crc32(string, string_length);
4914 }
4915
4916 static test_return_t memcached_get_hashkit_test (memcached_st *memc)
4917 {
4918 uint32_t x;
4919 const char **ptr;
4920 const hashkit_st *kit;
4921 hashkit_st new_kit;
4922 hashkit_return_t hash_rc;
4923
4924 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};
4925 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};
4926
4927 kit= memcached_get_hashkit(memc);
4928
4929 hashkit_clone(&new_kit, kit);
4930 hash_rc= hashkit_set_custom_function(&new_kit, hash_md5_test_function, NULL);
4931 test_true(hash_rc == HASHKIT_SUCCESS);
4932
4933 memcached_set_hashkit(memc, &new_kit);
4934
4935 /*
4936 Verify Setting the hash.
4937 */
4938 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4939 {
4940 uint32_t hash_val;
4941
4942 hash_val= hashkit_digest(kit, *ptr, strlen(*ptr));
4943 test_true(md5_values[x] == hash_val);
4944 }
4945
4946
4947 /*
4948 Now check memcached_st.
4949 */
4950 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4951 {
4952 uint32_t hash_val;
4953
4954 hash_val= memcached_generate_hash(memc, *ptr, strlen(*ptr));
4955 test_true(md5_hosts[x] == hash_val);
4956 }
4957
4958 hash_rc= hashkit_set_custom_function(&new_kit, hash_crc_test_function, NULL);
4959 test_true(hash_rc == HASHKIT_SUCCESS);
4960
4961 memcached_set_hashkit(memc, &new_kit);
4962
4963 /*
4964 Verify Setting the hash.
4965 */
4966 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4967 {
4968 uint32_t hash_val;
4969
4970 hash_val= hashkit_digest(kit, *ptr, strlen(*ptr));
4971 test_true(crc_values[x] == hash_val);
4972 }
4973
4974 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
4975 {
4976 uint32_t hash_val;
4977
4978 hash_val= memcached_generate_hash(memc, *ptr, strlen(*ptr));
4979 test_true(crc_hosts[x] == hash_val);
4980 }
4981
4982 return TEST_SUCCESS;
4983 }
4984
4985 /*
4986 Test case adapted from John Gorman <johngorman2@gmail.com>
4987
4988 We are testing the error condition when we connect to a server via memcached_get()
4989 but find that the server is not available.
4990 */
4991 static test_return_t memcached_get_MEMCACHED_SOME_ERRORS(memcached_st *memc)
4992 {
4993 (void)memc;
4994 memcached_st *tl_memc_h;
4995 memcached_server_st *servers;
4996
4997 const char *key= "MemcachedLives";
4998 size_t len;
4999 uint32_t flags;
5000 memcached_return rc;
5001 char *value;
5002
5003 // Create a handle.
5004 tl_memc_h= memcached_create(NULL);
5005 servers= memcached_servers_parse("localhost:9898"); // This server should not exist
5006 memcached_server_push(tl_memc_h, servers);
5007 memcached_server_list_free(servers);
5008
5009 // See if memcached is reachable.
5010 value= memcached_get(tl_memc_h, key, strlen(key), &len, &flags, &rc);
5011
5012 if (value)
5013 {
5014 free(value);
5015 test_true(value); // Pointer won't be zero so this is fine.
5016 }
5017
5018 test_true(len == 0);
5019 test_true(rc == MEMCACHED_SOME_ERRORS);
5020
5021 return TEST_SUCCESS;
5022 }
5023
5024 /*
5025 We connect to a server which exists, but search for a key that does not exist.
5026 */
5027 static test_return_t memcached_get_MEMCACHED_NOTFOUND(memcached_st *memc)
5028 {
5029 const char *key= "MemcachedKeyNotEXIST";
5030 size_t len;
5031 uint32_t flags;
5032 memcached_return rc;
5033 char *value;
5034
5035 // See if memcached is reachable.
5036 value= memcached_get(memc, key, strlen(key), &len, &flags, &rc);
5037
5038 if (value)
5039 {
5040 free(value);
5041 test_true(value); // Pointer won't be zero so this is fine.
5042 }
5043
5044 test_true(len == 0);
5045 test_true(rc == MEMCACHED_NOTFOUND);
5046
5047 return TEST_SUCCESS;
5048 }
5049
5050 /*
5051 Test case adapted from John Gorman <johngorman2@gmail.com>
5052
5053 We are testing the error condition when we connect to a server via memcached_get_by_key()
5054 but find that the server is not available.
5055 */
5056 static test_return_t memcached_get_by_key_MEMCACHED_SOME_ERRORS(memcached_st *memc)
5057 {
5058 (void)memc;
5059 memcached_st *tl_memc_h;
5060 memcached_server_st *servers;
5061
5062 const char *key= "MemcachedLives";
5063 size_t len;
5064 uint32_t flags;
5065 memcached_return rc;
5066 char *value;
5067
5068 // Create a handle.
5069 tl_memc_h= memcached_create(NULL);
5070 servers= memcached_servers_parse("localhost:9898"); // This server should not exist
5071 memcached_server_push(tl_memc_h, servers);
5072 memcached_server_list_free(servers);
5073
5074 // See if memcached is reachable.
5075 value= memcached_get_by_key(tl_memc_h, key, strlen(key), key, strlen(key), &len, &flags, &rc);
5076
5077 if (value)
5078 {
5079 free(value);
5080 test_true(value); // Pointer won't be zero so this is fine.
5081 }
5082
5083 test_true(len == 0);
5084 test_true(rc == MEMCACHED_SOME_ERRORS);
5085
5086 return TEST_SUCCESS;
5087 }
5088
5089 /*
5090 We connect to a server which exists, but search for a key that does not exist.
5091 */
5092 static test_return_t memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st *memc)
5093 {
5094 const char *key= "MemcachedKeyNotEXIST";
5095 size_t len;
5096 uint32_t flags;
5097 memcached_return rc;
5098 char *value;
5099
5100 // See if memcached is reachable.
5101 value= memcached_get_by_key(memc, key, strlen(key), key, strlen(key), &len, &flags, &rc);
5102
5103 if (value)
5104 {
5105 free(value);
5106 test_true(value); // Pointer won't be zero so this is fine.
5107 }
5108
5109 test_true(len == 0);
5110 test_true(rc == MEMCACHED_NOTFOUND);
5111
5112 return TEST_SUCCESS;
5113 }
5114
5115
5116 static test_return_t ketama_compatibility_libmemcached(memcached_st *trash)
5117 {
5118 memcached_return_t rc;
5119 uint64_t value;
5120 int x;
5121 memcached_server_st *server_pool;
5122 memcached_st *memc;
5123
5124 (void)trash;
5125
5126 memc= memcached_create(NULL);
5127 test_true(memc);
5128
5129 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
5130 test_true(rc == MEMCACHED_SUCCESS);
5131
5132 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
5133 test_true(value == 1);
5134
5135 test_true(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) == MEMCACHED_SUCCESS);
5136 test_true(memcached_behavior_get_distribution(memc) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
5137
5138
5139 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");
5140 memcached_server_push(memc, server_pool);
5141
5142 /* verify that the server list was parsed okay. */
5143 test_true(memcached_server_count(memc) == 8);
5144 test_strcmp(server_pool[0].hostname, "10.0.1.1");
5145 test_true(server_pool[0].port == 11211);
5146 test_true(server_pool[0].weight == 600);
5147 test_strcmp(server_pool[2].hostname, "10.0.1.3");
5148 test_true(server_pool[2].port == 11211);
5149 test_true(server_pool[2].weight == 200);
5150 test_strcmp(server_pool[7].hostname, "10.0.1.8");
5151 test_true(server_pool[7].port == 11211);
5152 test_true(server_pool[7].weight == 100);
5153
5154 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5155 * us test the boundary wraparound.
5156 */
5157 test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
5158
5159 /* verify the standard ketama set. */
5160 for (x= 0; x < 99; x++)
5161 {
5162 uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
5163 memcached_server_instance_st instance=
5164 memcached_server_instance_by_position(memc, server_idx);
5165 const char *hostname = memcached_server_name(instance);
5166
5167 test_strcmp(hostname, ketama_test_cases[x].server);
5168 }
5169
5170 memcached_server_list_free(server_pool);
5171 memcached_free(memc);
5172
5173 return TEST_SUCCESS;
5174 }
5175
5176 static test_return_t ketama_compatibility_spymemcached(memcached_st *trash)
5177 {
5178 memcached_return_t rc;
5179 uint64_t value;
5180 int x;
5181 memcached_server_st *server_pool;
5182 memcached_st *memc;
5183
5184 (void)trash;
5185
5186 memc= memcached_create(NULL);
5187 test_true(memc);
5188
5189 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
5190 test_true(rc == MEMCACHED_SUCCESS);
5191
5192 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
5193 test_true(value == 1);
5194
5195 test_true(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) == MEMCACHED_SUCCESS);
5196 test_true(memcached_behavior_get_distribution(memc) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY);
5197
5198 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");
5199 memcached_server_push(memc, server_pool);
5200
5201 /* verify that the server list was parsed okay. */
5202 test_true(memcached_server_count(memc) == 8);
5203 test_strcmp(server_pool[0].hostname, "10.0.1.1");
5204 test_true(server_pool[0].port == 11211);
5205 test_true(server_pool[0].weight == 600);
5206 test_strcmp(server_pool[2].hostname, "10.0.1.3");
5207 test_true(server_pool[2].port == 11211);
5208 test_true(server_pool[2].weight == 200);
5209 test_strcmp(server_pool[7].hostname, "10.0.1.8");
5210 test_true(server_pool[7].port == 11211);
5211 test_true(server_pool[7].weight == 100);
5212
5213 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5214 * us test the boundary wraparound.
5215 */
5216 test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
5217
5218 /* verify the standard ketama set. */
5219 for (x= 0; x < 99; x++)
5220 {
5221 uint32_t server_idx= memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key));
5222
5223 memcached_server_instance_st instance=
5224 memcached_server_instance_by_position(memc, server_idx);
5225
5226 const char *hostname= memcached_server_name(instance);
5227
5228 test_strcmp(hostname, ketama_test_cases_spy[x].server);
5229 }
5230
5231 memcached_server_list_free(server_pool);
5232 memcached_free(memc);
5233
5234 return TEST_SUCCESS;
5235 }
5236
5237 static test_return_t regression_bug_434484(memcached_st *memc)
5238 {
5239 test_return_t test_rc;
5240 test_rc= pre_binary(memc);
5241
5242 if (test_rc != TEST_SUCCESS)
5243 return test_rc;
5244
5245 memcached_return_t ret;
5246 const char *key= "regression_bug_434484";
5247 size_t keylen= strlen(key);
5248
5249 ret= memcached_append(memc, key, keylen, key, keylen, 0, 0);
5250 test_true(ret == MEMCACHED_NOTSTORED);
5251
5252 size_t size= 2048 * 1024;
5253 void *data= calloc(1, size);
5254 test_true(data != NULL);
5255 ret= memcached_set(memc, key, keylen, data, size, 0, 0);
5256 test_true(ret == MEMCACHED_E2BIG);
5257 free(data);
5258
5259 return TEST_SUCCESS;
5260 }
5261
5262 static test_return_t regression_bug_434843(memcached_st *memc)
5263 {
5264 test_return_t test_rc;
5265 test_rc= pre_binary(memc);
5266
5267 if (test_rc != TEST_SUCCESS)
5268 return test_rc;
5269
5270 memcached_return_t rc;
5271 size_t counter= 0;
5272 memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
5273
5274 /*
5275 * I only want to hit only _one_ server so I know the number of requests I'm
5276 * sending in the pipleine to the server. Let's try to do a multiget of
5277 * 1024 (that should satisfy most users don't you think?). Future versions
5278 * will include a mget_execute function call if you need a higher number.
5279 */
5280 uint32_t number_of_hosts= memcached_server_count(memc);
5281 memc->number_of_hosts= 1;
5282 const size_t max_keys= 1024;
5283 char **keys= calloc(max_keys, sizeof(char*));
5284 size_t *key_length=calloc(max_keys, sizeof(size_t));
5285
5286 for (size_t x= 0; x < max_keys; ++x)
5287 {
5288 char k[251];
5289
5290 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
5291 keys[x]= strdup(k);
5292 test_true(keys[x] != NULL);
5293 }
5294
5295 /*
5296 * Run two times.. the first time we should have 100% cache miss,
5297 * and the second time we should have 100% cache hits
5298 */
5299 for (size_t y= 0; y < 2; y++)
5300 {
5301 rc= memcached_mget(memc, (const char**)keys, key_length, max_keys);
5302 test_true(rc == MEMCACHED_SUCCESS);
5303 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5304
5305 if (y == 0)
5306 {
5307 /* The first iteration should give me a 100% cache miss. verify that*/
5308 char blob[1024]= { 0 };
5309
5310 test_true(counter == 0);
5311
5312 for (size_t x= 0; x < max_keys; ++x)
5313 {
5314 rc= memcached_add(memc, keys[x], key_length[x],
5315 blob, sizeof(blob), 0, 0);
5316 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5317 }
5318 }
5319 else
5320 {
5321 /* Verify that we received all of the key/value pairs */
5322 test_true(counter == max_keys);
5323 }
5324 }
5325
5326 /* Release allocated resources */
5327 for (size_t x= 0; x < max_keys; ++x)
5328 {
5329 free(keys[x]);
5330 }
5331 free(keys);
5332 free(key_length);
5333
5334 memc->number_of_hosts= number_of_hosts;
5335
5336 return TEST_SUCCESS;
5337 }
5338
5339 static test_return_t regression_bug_434843_buffered(memcached_st *memc)
5340 {
5341 memcached_return_t rc;
5342 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
5343 test_true(rc == MEMCACHED_SUCCESS);
5344
5345 return regression_bug_434843(memc);
5346 }
5347
5348 static test_return_t regression_bug_421108(memcached_st *memc)
5349 {
5350 memcached_return_t rc;
5351 memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc);
5352 test_true(rc == MEMCACHED_SUCCESS);
5353
5354 char *bytes= memcached_stat_get_value(memc, memc_stat, "bytes", &rc);
5355 test_true(rc == MEMCACHED_SUCCESS);
5356 test_true(bytes != NULL);
5357 char *bytes_read= memcached_stat_get_value(memc, memc_stat,
5358 "bytes_read", &rc);
5359 test_true(rc == MEMCACHED_SUCCESS);
5360 test_true(bytes_read != NULL);
5361
5362 char *bytes_written= memcached_stat_get_value(memc, memc_stat,
5363 "bytes_written", &rc);
5364 test_true(rc == MEMCACHED_SUCCESS);
5365 test_true(bytes_written != NULL);
5366
5367 test_true(strcmp(bytes, bytes_read) != 0);
5368 test_true(strcmp(bytes, bytes_written) != 0);
5369
5370 /* Release allocated resources */
5371 free(bytes);
5372 free(bytes_read);
5373 free(bytes_written);
5374 memcached_stat_free(NULL, memc_stat);
5375
5376 return TEST_SUCCESS;
5377 }
5378
5379 /*
5380 * The test case isn't obvious so I should probably document why
5381 * it works the way it does. Bug 442914 was caused by a bug
5382 * in the logic in memcached_purge (it did not handle the case
5383 * where the number of bytes sent was equal to the watermark).
5384 * In this test case, create messages so that we hit that case
5385 * and then disable noreply mode and issue a new command to
5386 * verify that it isn't stuck. If we change the format for the
5387 * delete command or the watermarks, we need to update this
5388 * test....
5389 */
5390 static test_return_t regression_bug_442914(memcached_st *memc)
5391 {
5392 memcached_return_t rc;
5393 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
5394 test_true(rc == MEMCACHED_SUCCESS);
5395 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
5396
5397 uint32_t number_of_hosts= memcached_server_count(memc);
5398 memc->number_of_hosts= 1;
5399
5400 char k[250];
5401 size_t len;
5402
5403 for (uint32_t x= 0; x < 250; ++x)
5404 {
5405 len= (size_t)snprintf(k, sizeof(k), "%0250u", x);
5406 rc= memcached_delete(memc, k, len, 0);
5407 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5408 }
5409
5410 (void)snprintf(k, sizeof(k), "%037u", 251U);
5411 len= strlen(k);
5412
5413 rc= memcached_delete(memc, k, len, 0);
5414 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5415
5416 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0);
5417 test_true(rc == MEMCACHED_SUCCESS);
5418 rc= memcached_delete(memc, k, len, 0);
5419 test_true(rc == MEMCACHED_NOTFOUND);
5420
5421 memc->number_of_hosts= number_of_hosts;
5422
5423 return TEST_SUCCESS;
5424 }
5425
5426 static test_return_t regression_bug_447342(memcached_st *memc)
5427 {
5428 memcached_server_instance_st instance_one;
5429 memcached_server_instance_st instance_two;
5430
5431 if (memcached_server_count(memc) < 3 || pre_replication(memc) != MEMCACHED_SUCCESS)
5432 return TEST_SKIPPED;
5433
5434 memcached_return_t rc;
5435
5436 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 2);
5437 test_true(rc == MEMCACHED_SUCCESS);
5438
5439 const size_t max_keys= 100;
5440 char **keys= calloc(max_keys, sizeof(char*));
5441 size_t *key_length= calloc(max_keys, sizeof(size_t));
5442
5443 for (size_t x= 0; x < max_keys; ++x)
5444 {
5445 char k[251];
5446
5447 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
5448 keys[x]= strdup(k);
5449 test_true(keys[x] != NULL);
5450 rc= memcached_set(memc, k, key_length[x], k, key_length[x], 0, 0);
5451 test_true(rc == MEMCACHED_SUCCESS);
5452 }
5453
5454 /*
5455 ** We are using the quiet commands to store the replicas, so we need
5456 ** to ensure that all of them are processed before we can continue.
5457 ** In the test we go directly from storing the object to trying to
5458 ** receive the object from all of the different servers, so we
5459 ** could end up in a race condition (the memcached server hasn't yet
5460 ** processed the quiet command from the replication set when it process
5461 ** the request from the other client (created by the clone)). As a
5462 ** workaround for that we call memcached_quit to send the quit command
5463 ** to the server and wait for the response ;-) If you use the test code
5464 ** as an example for your own code, please note that you shouldn't need
5465 ** to do this ;-)
5466 */
5467 memcached_quit(memc);
5468
5469 /* Verify that all messages are stored, and we didn't stuff too much
5470 * into the servers
5471 */
5472 rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
5473 test_true(rc == MEMCACHED_SUCCESS);
5474
5475 size_t counter= 0;
5476 memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
5477 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5478 /* Verify that we received all of the key/value pairs */
5479 test_true(counter == max_keys);
5480
5481 memcached_quit(memc);
5482 /*
5483 * Don't do the following in your code. I am abusing the internal details
5484 * within the library, and this is not a supported interface.
5485 * This is to verify correct behavior in the library. Fake that two servers
5486 * are dead..
5487 */
5488 instance_one= memcached_server_instance_by_position(memc, 0);
5489 instance_two= memcached_server_instance_by_position(memc, 2);
5490 in_port_t port0= instance_one->port;
5491 in_port_t port2= instance_two->port;
5492
5493 ((memcached_server_write_instance_st)instance_one)->port= 0;
5494 ((memcached_server_write_instance_st)instance_two)->port= 0;
5495
5496 rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
5497 test_true(rc == MEMCACHED_SUCCESS);
5498
5499 counter= 0;
5500 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5501 test_true(counter == (unsigned int)max_keys);
5502
5503 /* restore the memc handle */
5504 ((memcached_server_write_instance_st)instance_one)->port= port0;
5505 ((memcached_server_write_instance_st)instance_two)->port= port2;
5506
5507 memcached_quit(memc);
5508
5509 /* Remove half of the objects */
5510 for (size_t x= 0; x < max_keys; ++x)
5511 {
5512 if (x & 1)
5513 {
5514 rc= memcached_delete(memc, keys[x], key_length[x], 0);
5515 test_true(rc == MEMCACHED_SUCCESS);
5516 }
5517 }
5518
5519 memcached_quit(memc);
5520 ((memcached_server_write_instance_st)instance_one)->port= 0;
5521 ((memcached_server_write_instance_st)instance_two)->port= 0;
5522
5523 /* now retry the command, this time we should have cache misses */
5524 rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
5525 test_true(rc == MEMCACHED_SUCCESS);
5526
5527 counter= 0;
5528 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
5529 test_true(counter == (unsigned int)(max_keys >> 1));
5530
5531 /* Release allocated resources */
5532 for (size_t x= 0; x < max_keys; ++x)
5533 {
5534 free(keys[x]);
5535 }
5536 free(keys);
5537 free(key_length);
5538
5539 /* restore the memc handle */
5540 ((memcached_server_write_instance_st)instance_one)->port= port0;
5541 ((memcached_server_write_instance_st)instance_two)->port= port2;
5542
5543 return TEST_SUCCESS;
5544 }
5545
5546 static test_return_t regression_bug_463297(memcached_st *memc)
5547 {
5548 memcached_st *memc_clone= memcached_clone(NULL, memc);
5549 test_true(memc_clone != NULL);
5550 test_true(memcached_version(memc_clone) == MEMCACHED_SUCCESS);
5551
5552 memcached_server_instance_st instance=
5553 memcached_server_instance_by_position(memc_clone, 0);
5554
5555 if (instance->major_version > 1 ||
5556 (instance->major_version == 1 &&
5557 instance->minor_version > 2))
5558 {
5559 /* Binary protocol doesn't support deferred delete */
5560 memcached_st *bin_clone= memcached_clone(NULL, memc);
5561 test_true(bin_clone != NULL);
5562 test_true(memcached_behavior_set(bin_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1) == MEMCACHED_SUCCESS);
5563 test_true(memcached_delete(bin_clone, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS);
5564 memcached_free(bin_clone);
5565
5566 memcached_quit(memc_clone);
5567
5568 /* If we know the server version, deferred delete should fail
5569 * with invalid arguments */
5570 test_true(memcached_delete(memc_clone, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS);
5571
5572 /* If we don't know the server version, we should get a protocol error */
5573 memcached_return_t rc= memcached_delete(memc, "foo", 3, 1);
5574
5575 /* but there is a bug in some of the memcached servers (1.4) that treats
5576 * the counter as noreply so it doesn't send the proper error message
5577 */
5578 test_true(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
5579
5580 /* And buffered mode should be disabled and we should get protocol error */
5581 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1) == MEMCACHED_SUCCESS);
5582 rc= memcached_delete(memc, "foo", 3, 1);
5583 test_true(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
5584
5585 /* Same goes for noreply... */
5586 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1) == MEMCACHED_SUCCESS);
5587 rc= memcached_delete(memc, "foo", 3, 1);
5588 test_true(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
5589
5590 /* but a normal request should go through (and be buffered) */
5591 test_true((rc= memcached_delete(memc, "foo", 3, 0)) == MEMCACHED_BUFFERED);
5592 test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
5593
5594 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0) == MEMCACHED_SUCCESS);
5595 /* unbuffered noreply should be success */
5596 test_true(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_SUCCESS);
5597 /* unbuffered with reply should be not found... */
5598 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0) == MEMCACHED_SUCCESS);
5599 test_true(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_NOTFOUND);
5600 }
5601
5602 memcached_free(memc_clone);
5603 return TEST_SUCCESS;
5604 }
5605
5606
5607 /* Test memcached_server_get_last_disconnect
5608 * For a working server set, shall be NULL
5609 * For a set of non existing server, shall not be NULL
5610 */
5611 static test_return_t test_get_last_disconnect(memcached_st *memc)
5612 {
5613 memcached_return_t rc;
5614 memcached_server_instance_st disconnected_server;
5615
5616 /* With the working set of server */
5617 const char *key= "marmotte";
5618 const char *value= "milka";
5619
5620 memcached_reset_last_disconnected_server(memc);
5621 rc= memcached_set(memc, key, strlen(key),
5622 value, strlen(value),
5623 (time_t)0, (uint32_t)0);
5624 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5625
5626 disconnected_server = memcached_server_get_last_disconnect(memc);
5627 test_true(disconnected_server == NULL);
5628
5629 /* With a non existing server */
5630 memcached_st *mine;
5631 memcached_server_st *servers;
5632
5633 const char *server_list= "localhost:9";
5634
5635 servers= memcached_servers_parse(server_list);
5636 test_true(servers);
5637 mine= memcached_create(NULL);
5638 rc= memcached_server_push(mine, servers);
5639 test_true(rc == MEMCACHED_SUCCESS);
5640 memcached_server_list_free(servers);
5641 test_true(mine);
5642
5643 rc= memcached_set(mine, key, strlen(key),
5644 value, strlen(value),
5645 (time_t)0, (uint32_t)0);
5646 test_true(rc != MEMCACHED_SUCCESS);
5647
5648 disconnected_server= memcached_server_get_last_disconnect(mine);
5649 if (disconnected_server == NULL)
5650 {
5651 fprintf(stderr, "RC %s\n", memcached_strerror(mine, rc));
5652 abort();
5653 }
5654 test_true(disconnected_server != NULL);
5655 test_true(memcached_server_port(disconnected_server)== 9);
5656 test_true(strncmp(memcached_server_name(disconnected_server),"localhost",9) == 0);
5657
5658 memcached_quit(mine);
5659 memcached_free(mine);
5660
5661 return TEST_SUCCESS;
5662 }
5663
5664 static test_return_t test_verbosity(memcached_st *memc)
5665 {
5666 memcached_verbosity(memc, 3);
5667
5668 return TEST_SUCCESS;
5669 }
5670
5671 static test_return_t test_server_failure(memcached_st *memc)
5672 {
5673 memcached_st *local_memc;
5674 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, 0);
5675
5676 local_memc= memcached_create(NULL);
5677
5678 memcached_server_add(local_memc, memcached_server_name(instance), memcached_server_port(instance));
5679 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 2);
5680
5681 uint32_t server_count= memcached_server_count(local_memc);
5682
5683 test_true(server_count == 1);
5684
5685 // Disable the server
5686 instance= memcached_server_instance_by_position(local_memc, 0);
5687 ((memcached_server_write_instance_st)instance)->server_failure_counter= 2;
5688
5689 memcached_return_t rc;
5690 rc= memcached_set(local_memc, "foo", strlen("foo"),
5691 NULL, 0,
5692 (time_t)0, (uint32_t)0);
5693 test_true(rc == MEMCACHED_SERVER_MARKED_DEAD);
5694
5695 ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
5696 rc= memcached_set(local_memc, "foo", strlen("foo"),
5697 NULL, 0,
5698 (time_t)0, (uint32_t)0);
5699 test_true(rc == MEMCACHED_SUCCESS);
5700
5701
5702 memcached_free(local_memc);
5703
5704 return TEST_SUCCESS;
5705 }
5706
5707 static test_return_t test_cull_servers(memcached_st *memc)
5708 {
5709 uint32_t count = memcached_server_count(memc);
5710
5711 // Do not do this in your code, it is not supported.
5712 memc->servers[1].state.is_dead= true;
5713 memc->state.is_time_for_rebuild= true;
5714
5715 uint32_t new_count= memcached_server_count(memc);
5716 test_true(count == new_count);
5717
5718 #if 0
5719 test_true(count == new_count + 1 );
5720 #endif
5721
5722 return TEST_SUCCESS;
5723 }
5724
5725 /*
5726 * This test ensures that the failure counter isn't incremented during
5727 * normal termination of the memcached instance.
5728 */
5729 static test_return_t wrong_failure_counter_test(memcached_st *memc)
5730 {
5731 memcached_return_t rc;
5732 memcached_server_instance_st instance;
5733
5734 /* Set value to force connection to the server */
5735 const char *key= "marmotte";
5736 const char *value= "milka";
5737
5738 /*
5739 * Please note that I'm abusing the internal structures in libmemcached
5740 * in a non-portable way and you shouldn't be doing this. I'm only
5741 * doing this in order to verify that the library works the way it should
5742 */
5743 uint32_t number_of_hosts= memcached_server_count(memc);
5744 memc->number_of_hosts= 1;
5745
5746 /* Ensure that we are connected to the server by setting a value */
5747 rc= memcached_set(memc, key, strlen(key),
5748 value, strlen(value),
5749 (time_t)0, (uint32_t)0);
5750 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
5751
5752
5753 instance= memcached_server_instance_by_position(memc, 0);
5754 /* The test is to see that the memcached_quit doesn't increase the
5755 * the server failure conter, so let's ensure that it is zero
5756 * before sending quit
5757 */
5758 ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
5759
5760 memcached_quit(memc);
5761
5762 /* Verify that it memcached_quit didn't increment the failure counter
5763 * Please note that this isn't bullet proof, because an error could
5764 * occur...
5765 */
5766 test_true(instance->server_failure_counter == 0);
5767
5768 /* restore the instance */
5769 memc->number_of_hosts= number_of_hosts;
5770
5771 return TEST_SUCCESS;
5772 }
5773
5774
5775
5776
5777 /*
5778 * Test that ensures mget_execute does not end into recursive calls that finally fails
5779 */
5780 static test_return_t regression_bug_490486(memcached_st *memc)
5781 {
5782 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
5783 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
5784 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 1000);
5785 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 1);
5786 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 3600);
5787
5788 #ifdef __APPLE__
5789 return TEST_SKIPPED; // My MAC can't handle this test
5790 #endif
5791
5792 /*
5793 * I only want to hit _one_ server so I know the number of requests I'm
5794 * sending in the pipeline.
5795 */
5796 uint32_t number_of_hosts= memc->number_of_hosts;
5797 memc->number_of_hosts= 1;
5798 size_t max_keys= 20480;
5799
5800
5801 char **keys= calloc(max_keys, sizeof(char*));
5802 size_t *key_length=calloc(max_keys, sizeof(size_t));
5803
5804 /* First add all of the items.. */
5805 bool slept= false;
5806 char blob[1024]= { 0 };
5807 memcached_return rc;
5808 for (size_t x= 0; x < max_keys; ++x)
5809 {
5810 char k[251];
5811 key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
5812 keys[x]= strdup(k);
5813 assert(keys[x] != NULL);
5814 rc= memcached_set(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0);
5815 #ifdef __APPLE__
5816 if (rc == MEMCACHED_SERVER_MARKED_DEAD)
5817 {
5818 break; // We are out of business
5819 }
5820 #endif
5821 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT); // MEMCACHED_TIMEOUT <-- only observed on OSX
5822
5823 if (rc == MEMCACHED_TIMEOUT && slept == false)
5824 {
5825 x++;
5826 sleep(1);// We will try to sleep
5827 slept= true;
5828 }
5829 else if (rc == MEMCACHED_TIMEOUT && slept == true)
5830 {
5831 // We failed to send everything.
5832 break;
5833 }
5834 }
5835
5836 if (rc != MEMCACHED_SERVER_MARKED_DEAD)
5837 {
5838
5839 /* Try to get all of them with a large multiget */
5840 size_t counter= 0;
5841 memcached_execute_function callbacks[1]= { [0]= &callback_counter };
5842 rc= memcached_mget_execute(memc, (const char**)keys, key_length,
5843 (size_t)max_keys, callbacks, &counter, 1);
5844
5845 assert(rc == MEMCACHED_SUCCESS);
5846 char* the_value= NULL;
5847 char the_key[MEMCACHED_MAX_KEY];
5848 size_t the_key_length;
5849 size_t the_value_length;
5850 uint32_t the_flags;
5851
5852 do {
5853 the_value= memcached_fetch(memc, the_key, &the_key_length, &the_value_length, &the_flags, &rc);
5854
5855 if ((the_value!= NULL) && (rc == MEMCACHED_SUCCESS))
5856 {
5857 ++counter;
5858 free(the_value);
5859 }
5860
5861 } while ( (the_value!= NULL) && (rc == MEMCACHED_SUCCESS));
5862
5863
5864 assert(rc == MEMCACHED_END);
5865
5866 /* Verify that we got all of the items */
5867 assert(counter == max_keys);
5868 }
5869
5870 /* Release all allocated resources */
5871 for (size_t x= 0; x < max_keys; ++x)
5872 {
5873 free(keys[x]);
5874 }
5875 free(keys);
5876 free(key_length);
5877
5878 memc->number_of_hosts= number_of_hosts;
5879
5880 return TEST_SUCCESS;
5881 }
5882
5883 static test_return_t regression_bug_583031(memcached_st *unused)
5884 {
5885 (void)unused;
5886
5887 memcached_st *memc= memcached_create(NULL);
5888 assert(memc);
5889 memcached_server_add(memc, "10.2.3.4", 11211);
5890
5891 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 1000);
5892 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 1000);
5893 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
5894 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
5895 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 1000);
5896 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 3);
5897
5898 memcached_return_t rc;
5899 size_t length;
5900 uint32_t flags;
5901
5902 (void)memcached_get(memc, "dsf", 3, &length, &flags, &rc);
5903
5904 test_true(rc == MEMCACHED_TIMEOUT);
5905
5906 memcached_free(memc);
5907
5908 return TEST_SUCCESS;
5909 }
5910
5911 static void memcached_die(memcached_st* mc, memcached_return error, const char* what, uint32_t it)
5912 {
5913 fprintf(stderr, "Iteration #%u: ", it);
5914
5915 if(error == MEMCACHED_ERRNO)
5916 {
5917 fprintf(stderr, "system error %d from %s: %s\n",
5918 errno, what, strerror(errno));
5919 }
5920 else
5921 {
5922 fprintf(stderr, "error %d from %s: %s\n", error, what,
5923 memcached_strerror(mc, error));
5924 }
5925 }
5926
5927 #define TEST_CONSTANT_CREATION 200
5928
5929 static test_return_t regression_bug_(memcached_st *memc)
5930 {
5931 const char *remote_server;
5932 (void)memc;
5933
5934 if (! (remote_server= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5935 {
5936 return TEST_SKIPPED;
5937 }
5938
5939 for (uint32_t x= 0; x < TEST_CONSTANT_CREATION; x++)
5940 {
5941 memcached_st* mc= memcached_create(NULL);
5942 memcached_return rc;
5943
5944 rc= memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
5945 if (rc != MEMCACHED_SUCCESS)
5946 {
5947 memcached_die(mc, rc, "memcached_behavior_set", x);
5948 }
5949
5950 rc= memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS, 1);
5951 if (rc != MEMCACHED_SUCCESS)
5952 {
5953 memcached_die(mc, rc, "memcached_behavior_set", x);
5954 }
5955
5956 rc= memcached_server_add(mc, remote_server, 0);
5957 if (rc != MEMCACHED_SUCCESS)
5958 {
5959 memcached_die(mc, rc, "memcached_server_add", x);
5960 }
5961
5962 const char *set_key= "akey";
5963 const size_t set_key_len= strlen(set_key);
5964 const char *set_value= "a value";
5965 const size_t set_value_len= strlen(set_value);
5966
5967 if (rc == MEMCACHED_SUCCESS)
5968 {
5969 if (x > 0)
5970 {
5971 size_t get_value_len;
5972 char *get_value;
5973 uint32_t get_value_flags;
5974
5975 get_value= memcached_get(mc, set_key, set_key_len, &get_value_len,
5976 &get_value_flags, &rc);
5977 if (rc != MEMCACHED_SUCCESS)
5978 {
5979 memcached_die(mc, rc, "memcached_get", x);
5980 }
5981 else
5982 {
5983
5984 if (x != 0 &&
5985 (get_value_len != set_value_len
5986 || 0!=strncmp(get_value, set_value, get_value_len)))
5987 {
5988 fprintf(stderr, "Values don't match?\n");
5989 rc= MEMCACHED_FAILURE;
5990 }
5991 free(get_value);
5992 }
5993 }
5994
5995 rc= memcached_set(mc,
5996 set_key, set_key_len,
5997 set_value, set_value_len,
5998 0, /* time */
5999 0 /* flags */
6000 );
6001 if (rc != MEMCACHED_SUCCESS)
6002 {
6003 memcached_die(mc, rc, "memcached_set", x);
6004 }
6005 }
6006
6007 memcached_quit(mc);
6008 memcached_free(mc);
6009
6010 if (rc != MEMCACHED_SUCCESS)
6011 {
6012 break;
6013 }
6014 }
6015
6016 return TEST_SUCCESS;
6017 }
6018
6019 /*
6020 * Test that the sasl authentication works. We cannot use the default
6021 * pool of servers, because that would require that all servers we want
6022 * to test supports SASL authentication, and that they use the default
6023 * creds.
6024 */
6025 static test_return_t sasl_auth_test(memcached_st *memc)
6026 {
6027 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6028 memcached_return_t rc;
6029
6030 rc= memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6031 test_true(rc == MEMCACHED_SUCCESS);
6032 test_true((rc= memcached_delete(memc, "foo", 3, 0)) == MEMCACHED_SUCCESS);
6033 test_true((rc= memcached_destroy_sasl_auth_data(memc)) == MEMCACHED_SUCCESS);
6034 test_true((rc= memcached_destroy_sasl_auth_data(memc)) == MEMCACHED_FAILURE);
6035 test_true((rc= memcached_destroy_sasl_auth_data(NULL)) == MEMCACHED_FAILURE);
6036 memcached_quit(memc);
6037
6038 rc= memcached_set_sasl_auth_data(memc,
6039 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6040 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6041 test_true(rc == MEMCACHED_SUCCESS);
6042
6043 rc= memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6044 test_true(rc == MEMCACHED_AUTH_FAILURE);
6045 test_true(memcached_destroy_sasl_auth_data(memc) == MEMCACHED_SUCCESS);
6046
6047 memcached_quit(memc);
6048 return TEST_SUCCESS;
6049 #else
6050 (void)memc;
6051 return TEST_FAILURE;
6052 #endif
6053 }
6054
6055 /* Clean the server before beginning testing */
6056 test_st tests[] ={
6057 {"flush", 0, (test_callback_fn)flush_test },
6058 {"init", 0, (test_callback_fn)init_test },
6059 {"allocation", 0, (test_callback_fn)allocation_test },
6060 {"server_list_null_test", 0, (test_callback_fn)server_list_null_test},
6061 {"server_unsort", 0, (test_callback_fn)server_unsort_test},
6062 {"server_sort", 0, (test_callback_fn)server_sort_test},
6063 {"server_sort2", 0, (test_callback_fn)server_sort2_test},
6064 {"memcached_server_remove", 0, (test_callback_fn)memcached_server_remove_test},
6065 {"clone_test", 0, (test_callback_fn)clone_test },
6066 {"connection_test", 0, (test_callback_fn)connection_test},
6067 {"callback_test", 0, (test_callback_fn)callback_test},
6068 {"userdata_test", 0, (test_callback_fn)userdata_test},
6069 {"error", 0, (test_callback_fn)error_test },
6070 {"set", 0, (test_callback_fn)set_test },
6071 {"set2", 0, (test_callback_fn)set_test2 },
6072 {"set3", 0, (test_callback_fn)set_test3 },
6073 {"dump", 1, (test_callback_fn)dump_test},
6074 {"add", 1, (test_callback_fn)add_test },
6075 {"replace", 1, (test_callback_fn)replace_test },
6076 {"delete", 1, (test_callback_fn)delete_test },
6077 {"get", 1, (test_callback_fn)get_test },
6078 {"get2", 0, (test_callback_fn)get_test2 },
6079 {"get3", 0, (test_callback_fn)get_test3 },
6080 {"get4", 0, (test_callback_fn)get_test4 },
6081 {"partial mget", 0, (test_callback_fn)get_test5 },
6082 {"stats_servername", 0, (test_callback_fn)stats_servername_test },
6083 {"increment", 0, (test_callback_fn)increment_test },
6084 {"increment_with_initial", 1, (test_callback_fn)increment_with_initial_test },
6085 {"decrement", 0, (test_callback_fn)decrement_test },
6086 {"decrement_with_initial", 1, (test_callback_fn)decrement_with_initial_test },
6087 {"increment_by_key", 0, (test_callback_fn)increment_by_key_test },
6088 {"increment_with_initial_by_key", 1, (test_callback_fn)increment_with_initial_by_key_test },
6089 {"decrement_by_key", 0, (test_callback_fn)decrement_by_key_test },
6090 {"decrement_with_initial_by_key", 1, (test_callback_fn)decrement_with_initial_by_key_test },
6091 {"quit", 0, (test_callback_fn)quit_test },
6092 {"mget", 1, (test_callback_fn)mget_test },
6093 {"mget_result", 1, (test_callback_fn)mget_result_test },
6094 {"mget_result_alloc", 1, (test_callback_fn)mget_result_alloc_test },
6095 {"mget_result_function", 1, (test_callback_fn)mget_result_function },
6096 {"mget_execute", 1, (test_callback_fn)mget_execute },
6097 {"mget_end", 0, (test_callback_fn)mget_end },
6098 {"get_stats", 0, (test_callback_fn)get_stats },
6099 {"add_host_test", 0, (test_callback_fn)add_host_test },
6100 {"add_host_test_1", 0, (test_callback_fn)add_host_test1 },
6101 {"get_stats_keys", 0, (test_callback_fn)get_stats_keys },
6102 {"version_string_test", 0, (test_callback_fn)version_string_test},
6103 {"bad_key", 1, (test_callback_fn)bad_key_test },
6104 {"memcached_server_cursor", 1, (test_callback_fn)memcached_server_cursor_test },
6105 {"read_through", 1, (test_callback_fn)read_through },
6106 {"delete_through", 1, (test_callback_fn)delete_through },
6107 {"noreply", 1, (test_callback_fn)noreply_test},
6108 {"analyzer", 1, (test_callback_fn)analyzer_test},
6109 #ifdef HAVE_LIBMEMCACHEDUTIL
6110 {"connectionpool", 1, (test_callback_fn)connection_pool_test },
6111 {"ping", 1, (test_callback_fn)ping_test },
6112 #endif
6113 {"test_get_last_disconnect", 1, (test_callback_fn)test_get_last_disconnect},
6114 {"verbosity", 1, (test_callback_fn)test_verbosity},
6115 {"test_server_failure", 1, (test_callback_fn)test_server_failure},
6116 {"cull_servers", 1, (test_callback_fn)test_cull_servers},
6117 {0, 0, 0}
6118 };
6119
6120 test_st behavior_tests[] ={
6121 {"behavior_test", 0, (test_callback_fn)behavior_test},
6122 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn)MEMCACHED_BEHAVIOR_CORK_test},
6123 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test},
6124 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test},
6125 {0, 0, 0}
6126 };
6127
6128 test_st regression_binary_vs_block[] ={
6129 {"block add", 1, (test_callback_fn)block_add_regression},
6130 {"binary add", 1, (test_callback_fn)binary_add_regression},
6131 {0, 0, 0}
6132 };
6133
6134 test_st async_tests[] ={
6135 {"add", 1, (test_callback_fn)add_wrapper },
6136 {0, 0, 0}
6137 };
6138
6139 test_st string_tests[] ={
6140 {"string static with null", 0, (test_callback_fn)string_static_null },
6141 {"string alloc with null", 0, (test_callback_fn)string_alloc_null },
6142 {"string alloc with 1K", 0, (test_callback_fn)string_alloc_with_size },
6143 {"string alloc with malloc failure", 0, (test_callback_fn)string_alloc_with_size_toobig },
6144 {"string append", 0, (test_callback_fn)string_alloc_append },
6145 {"string append failure (too big)", 0, (test_callback_fn)string_alloc_append_toobig },
6146 {0, 0, (test_callback_fn)0}
6147 };
6148
6149 test_st result_tests[] ={
6150 {"result static", 0, (test_callback_fn)result_static},
6151 {"result alloc", 0, (test_callback_fn)result_alloc},
6152 {0, 0, (test_callback_fn)0}
6153 };
6154
6155 test_st version_1_2_3[] ={
6156 {"append", 0, (test_callback_fn)append_test },
6157 {"prepend", 0, (test_callback_fn)prepend_test },
6158 {"cas", 0, (test_callback_fn)cas_test },
6159 {"cas2", 0, (test_callback_fn)cas2_test },
6160 {"append_binary", 0, (test_callback_fn)append_binary_test },
6161 {0, 0, (test_callback_fn)0}
6162 };
6163
6164 test_st user_tests[] ={
6165 {"user_supplied_bug1", 0, (test_callback_fn)user_supplied_bug1 },
6166 {"user_supplied_bug2", 0, (test_callback_fn)user_supplied_bug2 },
6167 {"user_supplied_bug3", 0, (test_callback_fn)user_supplied_bug3 },
6168 {"user_supplied_bug4", 0, (test_callback_fn)user_supplied_bug4 },
6169 {"user_supplied_bug5", 1, (test_callback_fn)user_supplied_bug5 },
6170 {"user_supplied_bug6", 1, (test_callback_fn)user_supplied_bug6 },
6171 {"user_supplied_bug7", 1, (test_callback_fn)user_supplied_bug7 },
6172 {"user_supplied_bug8", 1, (test_callback_fn)user_supplied_bug8 },
6173 {"user_supplied_bug9", 1, (test_callback_fn)user_supplied_bug9 },
6174 {"user_supplied_bug10", 1, (test_callback_fn)user_supplied_bug10 },
6175 {"user_supplied_bug11", 1, (test_callback_fn)user_supplied_bug11 },
6176 {"user_supplied_bug12", 1, (test_callback_fn)user_supplied_bug12 },
6177 {"user_supplied_bug13", 1, (test_callback_fn)user_supplied_bug13 },
6178 {"user_supplied_bug14", 1, (test_callback_fn)user_supplied_bug14 },
6179 {"user_supplied_bug15", 1, (test_callback_fn)user_supplied_bug15 },
6180 {"user_supplied_bug16", 1, (test_callback_fn)user_supplied_bug16 },
6181 #ifndef __sun
6182 /*
6183 ** It seems to be something weird with the character sets..
6184 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6185 ** guess I need to find out how this is supposed to work.. Perhaps I need
6186 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6187 ** so just disable the code for now...).
6188 */
6189 {"user_supplied_bug17", 1, (test_callback_fn)user_supplied_bug17 },
6190 #endif
6191 {"user_supplied_bug18", 1, (test_callback_fn)user_supplied_bug18 },
6192 {"user_supplied_bug19", 1, (test_callback_fn)user_supplied_bug19 },
6193 {"user_supplied_bug20", 1, (test_callback_fn)user_supplied_bug20 },
6194 {"user_supplied_bug21", 1, (test_callback_fn)user_supplied_bug21 },
6195 {"wrong_failure_counter_test", 1, (test_callback_fn)wrong_failure_counter_test},
6196 {0, 0, (test_callback_fn)0}
6197 };
6198
6199 test_st replication_tests[]= {
6200 {"set", 1, (test_callback_fn)replication_set_test },
6201 {"get", 0, (test_callback_fn)replication_get_test },
6202 {"mget", 0, (test_callback_fn)replication_mget_test },
6203 {"delete", 0, (test_callback_fn)replication_delete_test },
6204 {"rand_mget", 0, (test_callback_fn)replication_randomize_mget_test },
6205 {0, 0, (test_callback_fn)0}
6206 };
6207
6208 /*
6209 * The following test suite is used to verify that we don't introduce
6210 * regression bugs. If you want more information about the bug / test,
6211 * you should look in the bug report at
6212 * http://bugs.launchpad.net/libmemcached
6213 */
6214 test_st regression_tests[]= {
6215 {"lp:434484", 1, (test_callback_fn)regression_bug_434484 },
6216 {"lp:434843", 1, (test_callback_fn)regression_bug_434843 },
6217 {"lp:434843-buffered", 1, (test_callback_fn)regression_bug_434843_buffered },
6218 {"lp:421108", 1, (test_callback_fn)regression_bug_421108 },
6219 {"lp:442914", 1, (test_callback_fn)regression_bug_442914 },
6220 {"lp:447342", 1, (test_callback_fn)regression_bug_447342 },
6221 {"lp:463297", 1, (test_callback_fn)regression_bug_463297 },
6222 {"lp:490486", 1, (test_callback_fn)regression_bug_490486 },
6223 {"lp:583031", 1, (test_callback_fn)regression_bug_583031 },
6224 {"lp:?", 1, (test_callback_fn)regression_bug_ },
6225 {0, 0, (test_callback_fn)0}
6226 };
6227
6228 test_st sasl_auth_tests[]= {
6229 {"sasl_auth", 1, (test_callback_fn)sasl_auth_test },
6230 {0, 0, (test_callback_fn)0}
6231 };
6232
6233 test_st ketama_compatibility[]= {
6234 {"libmemcached", 1, (test_callback_fn)ketama_compatibility_libmemcached },
6235 {"spymemcached", 1, (test_callback_fn)ketama_compatibility_spymemcached },
6236 {0, 0, (test_callback_fn)0}
6237 };
6238
6239 test_st generate_tests[] ={
6240 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
6241 {"generate_data", 1, (test_callback_fn)generate_data },
6242 {"get_read", 0, (test_callback_fn)get_read },
6243 {"delete_generate", 0, (test_callback_fn)delete_generate },
6244 {"generate_buffer_data", 1, (test_callback_fn)generate_buffer_data },
6245 {"delete_buffer", 0, (test_callback_fn)delete_buffer_generate},
6246 {"generate_data", 1, (test_callback_fn)generate_data },
6247 {"mget_read", 0, (test_callback_fn)mget_read },
6248 {"mget_read_result", 0, (test_callback_fn)mget_read_result },
6249 {"mget_read_function", 0, (test_callback_fn)mget_read_function },
6250 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6251 {"generate_large_pairs", 1, (test_callback_fn)generate_large_pairs },
6252 {"generate_data", 1, (test_callback_fn)generate_data },
6253 {"generate_buffer_data", 1, (test_callback_fn)generate_buffer_data },
6254 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6255 {0, 0, (test_callback_fn)0}
6256 };
6257
6258 test_st consistent_tests[] ={
6259 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
6260 {"generate_data", 1, (test_callback_fn)generate_data },
6261 {"get_read", 0, (test_callback_fn)get_read_count },
6262 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6263 {0, 0, (test_callback_fn)0}
6264 };
6265
6266 test_st consistent_weighted_tests[] ={
6267 {"generate_pairs", 1, (test_callback_fn)generate_pairs },
6268 {"generate_data", 1, (test_callback_fn)generate_data_with_stats },
6269 {"get_read", 0, (test_callback_fn)get_read_count },
6270 {"cleanup", 1, (test_callback_fn)cleanup_pairs },
6271 {0, 0, (test_callback_fn)0}
6272 };
6273
6274 test_st hsieh_availability[] ={
6275 {"hsieh_avaibility_test", 0, (test_callback_fn)hsieh_avaibility_test},
6276 {0, 0, (test_callback_fn)0}
6277 };
6278
6279 #if 0
6280 test_st hash_sanity[] ={
6281 {"hash sanity", 0, (test_callback_fn)hash_sanity_test},
6282 {0, 0, (test_callback_fn)0}
6283 };
6284 #endif
6285
6286 test_st ketama_auto_eject_hosts[] ={
6287 {"auto_eject_hosts", 1, (test_callback_fn)auto_eject_hosts },
6288 {"output_ketama_weighted_keys", 1, (test_callback_fn)output_ketama_weighted_keys },
6289 {0, 0, (test_callback_fn)0}
6290 };
6291
6292 test_st hash_tests[] ={
6293 {"one_at_a_time_run", 0, (test_callback_fn)one_at_a_time_run },
6294 {"md5", 0, (test_callback_fn)md5_run },
6295 {"crc", 0, (test_callback_fn)crc_run },
6296 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
6297 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
6298 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
6299 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
6300 {"hsieh", 0, (test_callback_fn)hsieh_run },
6301 {"murmur", 0, (test_callback_fn)murmur_run },
6302 {"jenkis", 0, (test_callback_fn)jenkins_run },
6303 {"memcached_get_hashkit", 0, (test_callback_fn)memcached_get_hashkit_test },
6304 {0, 0, (test_callback_fn)0}
6305 };
6306
6307 test_st error_conditions[] ={
6308 {"memcached_get_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn)memcached_get_MEMCACHED_SOME_ERRORS },
6309 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn)memcached_get_MEMCACHED_NOTFOUND },
6310 {"memcached_get_by_key_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn)memcached_get_by_key_MEMCACHED_SOME_ERRORS },
6311 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn)memcached_get_by_key_MEMCACHED_NOTFOUND },
6312 {0, 0, (test_callback_fn)0}
6313 };
6314
6315 collection_st collection[] ={
6316 #if 0
6317 {"hash_sanity", 0, 0, hash_sanity},
6318 #endif
6319 {"hsieh_availability", 0, 0, hsieh_availability},
6320 {"block", 0, 0, tests},
6321 {"binary", (test_callback_fn)pre_binary, 0, tests},
6322 {"nonblock", (test_callback_fn)pre_nonblock, 0, tests},
6323 {"nodelay", (test_callback_fn)pre_nodelay, 0, tests},
6324 {"settimer", (test_callback_fn)pre_settimer, 0, tests},
6325 {"md5", (test_callback_fn)pre_md5, 0, tests},
6326 {"crc", (test_callback_fn)pre_crc, 0, tests},
6327 {"hsieh", (test_callback_fn)pre_hsieh, 0, tests},
6328 {"jenkins", (test_callback_fn)pre_jenkins, 0, tests},
6329 {"fnv1_64", (test_callback_fn)pre_hash_fnv1_64, 0, tests},
6330 {"fnv1a_64", (test_callback_fn)pre_hash_fnv1a_64, 0, tests},
6331 {"fnv1_32", (test_callback_fn)pre_hash_fnv1_32, 0, tests},
6332 {"fnv1a_32", (test_callback_fn)pre_hash_fnv1a_32, 0, tests},
6333 {"ketama", (test_callback_fn)pre_behavior_ketama, 0, tests},
6334 {"ketama_auto_eject_hosts", (test_callback_fn)pre_behavior_ketama, 0, ketama_auto_eject_hosts},
6335 {"unix_socket", (test_callback_fn)pre_unix_socket, 0, tests},
6336 {"unix_socket_nodelay", (test_callback_fn)pre_nodelay, 0, tests},
6337 {"poll_timeout", (test_callback_fn)poll_timeout, 0, tests},
6338 {"gets", (test_callback_fn)enable_cas, 0, tests},
6339 {"consistent_crc", (test_callback_fn)enable_consistent_crc, 0, tests},
6340 {"consistent_hsieh", (test_callback_fn)enable_consistent_hsieh, 0, tests},
6341 #ifdef MEMCACHED_ENABLE_DEPRECATED
6342 {"deprecated_memory_allocators", (test_callback_fn)deprecated_set_memory_alloc, 0, tests},
6343 #endif
6344 {"memory_allocators", (test_callback_fn)set_memory_alloc, 0, tests},
6345 {"prefix", (test_callback_fn)set_prefix, 0, tests},
6346 {"sasl_auth", (test_callback_fn)pre_sasl, 0, sasl_auth_tests },
6347 {"sasl", (test_callback_fn)pre_sasl, 0, tests },
6348 {"version_1_2_3", (test_callback_fn)check_for_1_2_3, 0, version_1_2_3},
6349 {"string", 0, 0, string_tests},
6350 {"result", 0, 0, result_tests},
6351 {"async", (test_callback_fn)pre_nonblock, 0, async_tests},
6352 {"async_binary", (test_callback_fn)pre_nonblock_binary, 0, async_tests},
6353 {"user", 0, 0, user_tests},
6354 {"generate", 0, 0, generate_tests},
6355 {"generate_hsieh", (test_callback_fn)pre_hsieh, 0, generate_tests},
6356 {"generate_ketama", (test_callback_fn)pre_behavior_ketama, 0, generate_tests},
6357 {"generate_hsieh_consistent", (test_callback_fn)enable_consistent_hsieh, 0, generate_tests},
6358 {"generate_md5", (test_callback_fn)pre_md5, 0, generate_tests},
6359 {"generate_murmur", (test_callback_fn)pre_murmur, 0, generate_tests},
6360 {"generate_jenkins", (test_callback_fn)pre_jenkins, 0, generate_tests},
6361 {"generate_nonblock", (test_callback_fn)pre_nonblock, 0, generate_tests},
6362 // Too slow
6363 {"generate_corked", (test_callback_fn)pre_cork, 0, generate_tests},
6364 {"generate_corked_and_nonblock", (test_callback_fn)pre_cork_and_nonblock, 0, generate_tests},
6365 {"consistent_not", 0, 0, consistent_tests},
6366 {"consistent_ketama", (test_callback_fn)pre_behavior_ketama, 0, consistent_tests},
6367 {"consistent_ketama_weighted", (test_callback_fn)pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
6368 {"ketama_compat", 0, 0, ketama_compatibility},
6369 {"test_hashes", 0, 0, hash_tests},
6370 {"replication", (test_callback_fn)pre_replication, 0, replication_tests},
6371 {"replication_noblock", (test_callback_fn)pre_replication_noblock, 0, replication_tests},
6372 {"regression", 0, 0, regression_tests},
6373 {"behaviors", 0, 0, behavior_tests},
6374 {"regression_binary_vs_block", (test_callback_fn)key_setup, (test_callback_fn)key_teardown, regression_binary_vs_block},
6375 {"error_conditions", 0, 0, error_conditions},
6376 {0, 0, 0, 0}
6377 };
6378
6379 #define SERVERS_TO_CREATE 5
6380
6381 #include "libmemcached_world.h"
6382
6383 void get_world(world_st *world)
6384 {
6385 world->collections= collection;
6386
6387 world->create= (test_callback_create_fn)world_create;
6388 world->destroy= (test_callback_fn)world_destroy;
6389
6390 world->test.startup= (test_callback_fn)world_test_startup;
6391 world->test.flush= (test_callback_fn)world_flush;
6392 world->test.pre_run= (test_callback_fn)world_pre_run;
6393 world->test.post_run= (test_callback_fn)world_post_run;
6394 world->test.on_error= (test_callback_error_fn)world_on_error;
6395
6396 world->collection.startup= (test_callback_fn)world_container_startup;
6397 world->collection.shutdown= (test_callback_fn)world_container_shutdown;
6398
6399 world->runner= &defualt_libmemcached_runner;
6400 }