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