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