Updating for 0.30
[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 /* This test was changes so that "make test" would work properlly */
2676 #ifdef DEBUG
2677 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);
2678 #endif
2679 assert((unsigned long long)(stat_p + host_index)->bytes);
2680 }
2681
2682 memcached_stat_free(NULL, stat_p);
2683
2684 return 0;
2685 }
2686 static test_return generate_buffer_data(memcached_st *memc)
2687 {
2688 int latch= 0;
2689
2690 latch= 1;
2691 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2692 generate_data(memc);
2693
2694 return 0;
2695 }
2696
2697 static test_return get_read_count(memcached_st *memc)
2698 {
2699 unsigned int x;
2700 memcached_return rc;
2701 memcached_st *clone;
2702
2703 clone= memcached_clone(NULL, memc);
2704 assert(clone);
2705
2706 memcached_server_add_with_weight(clone, "localhost", 6666, 0);
2707
2708 {
2709 char *return_value;
2710 size_t return_value_length;
2711 uint32_t flags;
2712 uint32_t count;
2713
2714 for (x= count= 0; x < global_count; x++)
2715 {
2716 return_value= memcached_get(clone, global_keys[x], global_keys_length[x],
2717 &return_value_length, &flags, &rc);
2718 if (rc == MEMCACHED_SUCCESS)
2719 {
2720 count++;
2721 if (return_value)
2722 free(return_value);
2723 }
2724 }
2725 fprintf(stderr, "\t%u -> %u", global_count, count);
2726 }
2727
2728 memcached_free(clone);
2729
2730 return 0;
2731 }
2732
2733 static test_return get_read(memcached_st *memc)
2734 {
2735 unsigned int x;
2736 memcached_return rc;
2737
2738 {
2739 char *return_value;
2740 size_t return_value_length;
2741 uint32_t flags;
2742
2743 for (x= 0; x < global_count; x++)
2744 {
2745 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
2746 &return_value_length, &flags, &rc);
2747 /*
2748 assert(return_value);
2749 assert(rc == MEMCACHED_SUCCESS);
2750 */
2751 if (rc == MEMCACHED_SUCCESS && return_value)
2752 free(return_value);
2753 }
2754 }
2755
2756 return 0;
2757 }
2758
2759 static test_return mget_read(memcached_st *memc)
2760 {
2761 memcached_return rc;
2762
2763 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2764 assert(rc == MEMCACHED_SUCCESS);
2765 /* Turn this into a help function */
2766 {
2767 char return_key[MEMCACHED_MAX_KEY];
2768 size_t return_key_length;
2769 char *return_value;
2770 size_t return_value_length;
2771 uint32_t flags;
2772
2773 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2774 &return_value_length, &flags, &rc)))
2775 {
2776 assert(return_value);
2777 assert(rc == MEMCACHED_SUCCESS);
2778 free(return_value);
2779 }
2780 }
2781
2782 return 0;
2783 }
2784
2785 static test_return mget_read_result(memcached_st *memc)
2786 {
2787 memcached_return rc;
2788
2789 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2790 assert(rc == MEMCACHED_SUCCESS);
2791 /* Turn this into a help function */
2792 {
2793 memcached_result_st results_obj;
2794 memcached_result_st *results;
2795
2796 results= memcached_result_create(memc, &results_obj);
2797
2798 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
2799 {
2800 assert(results);
2801 assert(rc == MEMCACHED_SUCCESS);
2802 }
2803
2804 memcached_result_free(&results_obj);
2805 }
2806
2807 return 0;
2808 }
2809
2810 static test_return mget_read_function(memcached_st *memc)
2811 {
2812 memcached_return rc;
2813 unsigned int counter;
2814 memcached_execute_function callbacks[1];
2815
2816 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2817 assert(rc == MEMCACHED_SUCCESS);
2818
2819 callbacks[0]= &callback_counter;
2820 counter= 0;
2821 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
2822
2823 return 0;
2824 }
2825
2826 static test_return delete_generate(memcached_st *memc)
2827 {
2828 unsigned int x;
2829
2830 for (x= 0; x < global_count; x++)
2831 {
2832 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2833 }
2834
2835 return 0;
2836 }
2837
2838 static test_return delete_buffer_generate(memcached_st *memc)
2839 {
2840 int latch= 0;
2841 unsigned int x;
2842
2843 latch= 1;
2844 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2845
2846 for (x= 0; x < global_count; x++)
2847 {
2848 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2849 }
2850
2851 return 0;
2852 }
2853
2854 static test_return add_host_test1(memcached_st *memc)
2855 {
2856 unsigned int x;
2857 memcached_return rc;
2858 char servername[]= "0.example.com";
2859 memcached_server_st *servers;
2860
2861 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
2862 assert(servers);
2863 assert(1 == memcached_server_list_count(servers));
2864
2865 for (x= 2; x < 20; x++)
2866 {
2867 char buffer[SMALL_STRING_LEN];
2868
2869 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
2870 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
2871 &rc);
2872 assert(rc == MEMCACHED_SUCCESS);
2873 assert(x == memcached_server_list_count(servers));
2874 }
2875
2876 rc= memcached_server_push(memc, servers);
2877 assert(rc == MEMCACHED_SUCCESS);
2878 rc= memcached_server_push(memc, servers);
2879 assert(rc == MEMCACHED_SUCCESS);
2880
2881 memcached_server_list_free(servers);
2882
2883 return 0;
2884 }
2885
2886 static memcached_return pre_nonblock(memcached_st *memc)
2887 {
2888 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2889
2890 return MEMCACHED_SUCCESS;
2891 }
2892
2893 static memcached_return pre_nonblock_binary(memcached_st *memc)
2894 {
2895 memcached_return rc= MEMCACHED_FAILURE;
2896 memcached_st *clone;
2897
2898 clone= memcached_clone(NULL, memc);
2899 assert(clone);
2900 // The memcached_version needs to be done on a clone, because the server
2901 // will not toggle protocol on an connection.
2902 memcached_version(clone);
2903
2904 if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2)
2905 {
2906 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2907 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
2908 assert(rc == MEMCACHED_SUCCESS);
2909 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
2910 }
2911
2912 memcached_free(clone);
2913 return rc;
2914 }
2915
2916 static memcached_return pre_murmur(memcached_st *memc)
2917 {
2918 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
2919
2920 return MEMCACHED_SUCCESS;
2921 }
2922
2923 static memcached_return pre_jenkins(memcached_st *memc)
2924 {
2925 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
2926
2927 return MEMCACHED_SUCCESS;
2928 }
2929
2930
2931 static memcached_return pre_md5(memcached_st *memc)
2932 {
2933 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
2934
2935 return MEMCACHED_SUCCESS;
2936 }
2937
2938 static memcached_return pre_crc(memcached_st *memc)
2939 {
2940 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
2941
2942 return MEMCACHED_SUCCESS;
2943 }
2944
2945 static memcached_return pre_hsieh(memcached_st *memc)
2946 {
2947 #ifdef HAVE_HSIEH_HASH
2948 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
2949 return MEMCACHED_SUCCESS;
2950 #else
2951 (void) memc;
2952 return MEMCACHED_FAILURE;
2953 #endif
2954 }
2955
2956 static memcached_return pre_hash_fnv1_64(memcached_st *memc)
2957 {
2958 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
2959
2960 return MEMCACHED_SUCCESS;
2961 }
2962
2963 static memcached_return pre_hash_fnv1a_64(memcached_st *memc)
2964 {
2965 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
2966
2967 return MEMCACHED_SUCCESS;
2968 }
2969
2970 static memcached_return pre_hash_fnv1_32(memcached_st *memc)
2971 {
2972 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
2973
2974 return MEMCACHED_SUCCESS;
2975 }
2976
2977 static memcached_return pre_hash_fnv1a_32(memcached_st *memc)
2978 {
2979 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
2980
2981 return MEMCACHED_SUCCESS;
2982 }
2983
2984 static memcached_return pre_behavior_ketama(memcached_st *memc)
2985 {
2986 memcached_return rc;
2987 uint64_t value;
2988
2989 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
2990 assert(rc == MEMCACHED_SUCCESS);
2991
2992 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
2993 assert(value == 1);
2994
2995 return MEMCACHED_SUCCESS;
2996 }
2997
2998 static memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
2999 {
3000 memcached_return rc;
3001 uint64_t value;
3002
3003 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3004 assert(rc == MEMCACHED_SUCCESS);
3005
3006 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3007 assert(value == 1);
3008
3009 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3010 assert(rc == MEMCACHED_SUCCESS);
3011
3012 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3013 assert(value == MEMCACHED_HASH_MD5);
3014 return MEMCACHED_SUCCESS;
3015 }
3016
3017 static memcached_return pre_binary(memcached_st *memc)
3018 {
3019 memcached_return rc= MEMCACHED_FAILURE;
3020 memcached_st *clone;
3021
3022 clone= memcached_clone(NULL, memc);
3023 assert(clone);
3024 // The memcached_version needs to be done on a clone, because the server
3025 // will not toggle protocol on an connection.
3026 memcached_version(clone);
3027
3028 if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2)
3029 {
3030 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3031 assert(rc == MEMCACHED_SUCCESS);
3032 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3033 }
3034
3035 memcached_free(clone);
3036 return rc;
3037 }
3038
3039 static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
3040 {
3041 free(mem);
3042 }
3043
3044 static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
3045 {
3046 return calloc(1, size);
3047 }
3048
3049 static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
3050 {
3051 return realloc(mem, size);
3052 }
3053
3054 static memcached_return set_prefix(memcached_st *memc)
3055 {
3056 memcached_return rc;
3057 const char *key= "mine";
3058 char *value;
3059
3060 /* Make sure be default none exists */
3061 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3062 assert(rc == MEMCACHED_FAILURE);
3063
3064 /* Test a clean set */
3065 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3066 assert(rc == MEMCACHED_SUCCESS);
3067
3068 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3069 assert(memcmp(value, key, 4) == 0);
3070 assert(rc == MEMCACHED_SUCCESS);
3071
3072 /* Test that we can turn it off */
3073 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3074 assert(rc == MEMCACHED_SUCCESS);
3075
3076 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3077 assert(rc == MEMCACHED_FAILURE);
3078
3079 /* Now setup for main test */
3080 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3081 assert(rc == MEMCACHED_SUCCESS);
3082
3083 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3084 assert(rc == MEMCACHED_SUCCESS);
3085 assert(memcmp(value, key, 4) == 0);
3086
3087 /* Set to Zero, and then Set to something too large */
3088 {
3089 char *long_key;
3090 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3091 assert(rc == MEMCACHED_SUCCESS);
3092
3093 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3094 assert(rc == MEMCACHED_FAILURE);
3095 assert(value == NULL);
3096
3097 /* Test a long key for failure */
3098 /* TODO, extend test to determine based on setting, what result should be */
3099 long_key= "Thisismorethentheallottednumberofcharacters";
3100 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3101 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3102 assert(rc == MEMCACHED_SUCCESS);
3103
3104 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3105 long_key= "This is more then the allotted number of characters";
3106 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3107 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3108
3109 /* Test for a bad prefix, but with a short key */
3110 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
3111 assert(rc == MEMCACHED_SUCCESS);
3112
3113 long_key= "dog cat";
3114 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3115 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3116 }
3117
3118 return MEMCACHED_SUCCESS;
3119 }
3120
3121 static memcached_return set_memory_alloc(memcached_st *memc)
3122 {
3123 void *test_ptr= NULL;
3124 void *cb_ptr= NULL;
3125 {
3126 memcached_malloc_function malloc_cb=
3127 (memcached_malloc_function)my_malloc;
3128 cb_ptr= *(void **)&malloc_cb;
3129 memcached_return rc;
3130
3131 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
3132 assert(rc == MEMCACHED_SUCCESS);
3133 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
3134 assert(rc == MEMCACHED_SUCCESS);
3135 assert(test_ptr == cb_ptr);
3136 }
3137
3138 {
3139 memcached_realloc_function realloc_cb=
3140 (memcached_realloc_function)my_realloc;
3141 cb_ptr= *(void **)&realloc_cb;
3142 memcached_return rc;
3143
3144 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
3145 assert(rc == MEMCACHED_SUCCESS);
3146 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
3147 assert(rc == MEMCACHED_SUCCESS);
3148 assert(test_ptr == cb_ptr);
3149 }
3150
3151 {
3152 memcached_free_function free_cb=
3153 (memcached_free_function)my_free;
3154 cb_ptr= *(void **)&free_cb;
3155 memcached_return rc;
3156
3157 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
3158 assert(rc == MEMCACHED_SUCCESS);
3159 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
3160 assert(rc == MEMCACHED_SUCCESS);
3161 assert(test_ptr == cb_ptr);
3162 }
3163
3164 return MEMCACHED_SUCCESS;
3165 }
3166
3167 static memcached_return enable_consistent(memcached_st *memc)
3168 {
3169 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
3170 memcached_hash hash;
3171 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
3172 if (pre_hsieh(memc) != MEMCACHED_SUCCESS)
3173 return MEMCACHED_FAILURE;
3174
3175 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
3176 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
3177
3178 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
3179 assert(hash == MEMCACHED_HASH_HSIEH);
3180
3181
3182 return MEMCACHED_SUCCESS;
3183 }
3184
3185 static memcached_return enable_cas(memcached_st *memc)
3186 {
3187 unsigned int set= 1;
3188
3189 memcached_version(memc);
3190
3191 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3192 || memc->hosts[0].minor_version > 2)
3193 {
3194 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
3195
3196 return MEMCACHED_SUCCESS;
3197 }
3198
3199 return MEMCACHED_FAILURE;
3200 }
3201
3202 static memcached_return check_for_1_2_3(memcached_st *memc)
3203 {
3204 memcached_version(memc);
3205
3206 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3207 || memc->hosts[0].minor_version > 2)
3208 return MEMCACHED_SUCCESS;
3209
3210 return MEMCACHED_FAILURE;
3211 }
3212
3213 static memcached_return pre_unix_socket(memcached_st *memc)
3214 {
3215 memcached_return rc;
3216 struct stat buf;
3217
3218 memcached_server_list_free(memc->hosts);
3219 memc->hosts= NULL;
3220 memc->number_of_hosts= 0;
3221
3222 if (stat("/tmp/memcached.socket", &buf))
3223 return MEMCACHED_FAILURE;
3224
3225 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
3226
3227 return rc;
3228 }
3229
3230 static memcached_return pre_nodelay(memcached_st *memc)
3231 {
3232 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3233 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
3234
3235 return MEMCACHED_SUCCESS;
3236 }
3237
3238 static memcached_return pre_settimer(memcached_st *memc)
3239 {
3240 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
3241 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
3242
3243 return MEMCACHED_SUCCESS;
3244 }
3245
3246 static memcached_return poll_timeout(memcached_st *memc)
3247 {
3248 int32_t timeout;
3249
3250 timeout= 100;
3251
3252 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
3253
3254 timeout= (int32_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
3255
3256 assert(timeout == 100);
3257
3258 return MEMCACHED_SUCCESS;
3259 }
3260
3261 static test_return noreply_test(memcached_st *memc)
3262 {
3263 memcached_return ret;
3264 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
3265 assert(ret == MEMCACHED_SUCCESS);
3266 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3267 assert(ret == MEMCACHED_SUCCESS);
3268 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
3269 assert(ret == MEMCACHED_SUCCESS);
3270 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
3271 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
3272 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
3273
3274 for (int count=0; count < 5; ++count)
3275 {
3276 for (int x=0; x < 100; ++x)
3277 {
3278 char key[10];
3279 size_t len= sprintf(key, "%d", x);
3280 switch (count)
3281 {
3282 case 0:
3283 ret=memcached_add(memc, key, len, key, len, 0, 0);
3284 break;
3285 case 1:
3286 ret=memcached_replace(memc, key, len, key, len, 0, 0);
3287 break;
3288 case 2:
3289 ret=memcached_set(memc, key, len, key, len, 0, 0);
3290 break;
3291 case 3:
3292 ret=memcached_append(memc, key, len, key, len, 0, 0);
3293 break;
3294 case 4:
3295 ret=memcached_prepend(memc, key, len, key, len, 0, 0);
3296 break;
3297 }
3298 assert(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
3299 }
3300
3301 /*
3302 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3303 ** API and is _ONLY_ done this way to verify that the library works the
3304 ** way it is supposed to do!!!!
3305 */
3306 int no_msg=0;
3307 for (uint32_t x=0; x < memc->number_of_hosts; ++x)
3308 no_msg+=memc->hosts[x].cursor_active;
3309
3310 assert(no_msg == 0);
3311 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3312
3313 /*
3314 ** Now validate that all items was set properly!
3315 */
3316 for (int x=0; x < 100; ++x)
3317 {
3318 char key[10];
3319 size_t len= sprintf(key, "%d", x);
3320 size_t length;
3321 uint32_t flags;
3322 char* value=memcached_get(memc, key, strlen(key),
3323 &length, &flags, &ret);
3324 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3325 switch (count)
3326 {
3327 case 0: /* FALLTHROUGH */
3328 case 1: /* FALLTHROUGH */
3329 case 2:
3330 assert(strncmp(value, key, len) == 0);
3331 assert(len == length);
3332 break;
3333 case 3:
3334 assert(length == len * 2);
3335 break;
3336 case 4:
3337 assert(length == len * 3);
3338 break;
3339 }
3340 free(value);
3341 }
3342 }
3343
3344 /* Try setting an illegal cas value (should not return an error to
3345 * the caller (because we don't expect a return message from the server)
3346 */
3347 char* keys[]= {"0"};
3348 size_t lengths[]= {1};
3349 size_t length;
3350 uint32_t flags;
3351 memcached_result_st results_obj;
3352 memcached_result_st *results;
3353 ret= memcached_mget(memc, keys, lengths, 1);
3354 assert(ret == MEMCACHED_SUCCESS);
3355
3356 results= memcached_result_create(memc, &results_obj);
3357 assert(results);
3358 results= memcached_fetch_result(memc, &results_obj, &ret);
3359 assert(results);
3360 assert(ret == MEMCACHED_SUCCESS);
3361 uint64_t cas= memcached_result_cas(results);
3362 memcached_result_free(&results_obj);
3363
3364 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3365 assert(ret == MEMCACHED_SUCCESS);
3366
3367 /*
3368 * The item will have a new cas value, so try to set it again with the old
3369 * value. This should fail!
3370 */
3371 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3372 assert(ret == MEMCACHED_SUCCESS);
3373 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3374 char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
3375 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3376 free(value);
3377
3378 return TEST_SUCCESS;
3379 }
3380
3381 static test_return analyzer_test(memcached_st *memc)
3382 {
3383 memcached_return rc;
3384 memcached_stat_st *stat;
3385 memcached_analysis_st *report;
3386
3387 stat= memcached_stat(memc, NULL, &rc);
3388 assert(rc == MEMCACHED_SUCCESS);
3389 assert(stat);
3390
3391 report= memcached_analyze(memc, stat, &rc);
3392 assert(rc == MEMCACHED_SUCCESS);
3393 assert(report);
3394
3395 free(report);
3396 memcached_stat_free(NULL, stat);
3397
3398 return TEST_SUCCESS;
3399 }
3400
3401 /* Count the objects */
3402 static memcached_return callback_dump_counter(memcached_st *ptr __attribute__((unused)),
3403 const char *key __attribute__((unused)),
3404 size_t key_length __attribute__((unused)),
3405 void *context)
3406 {
3407 uint32_t *counter= (uint32_t *)context;
3408
3409 *counter= *counter + 1;
3410
3411 return MEMCACHED_SUCCESS;
3412 }
3413
3414 static test_return dump_test(memcached_st *memc)
3415 {
3416 memcached_return rc;
3417 uint32_t counter= 0;
3418 memcached_dump_func callbacks[1];
3419 test_return main_rc;
3420
3421 callbacks[0]= &callback_dump_counter;
3422
3423 /* No support for Binary protocol yet */
3424 if (memc->flags & MEM_BINARY_PROTOCOL)
3425 return TEST_SUCCESS;
3426
3427 main_rc= set_test3(memc);
3428
3429 assert (main_rc == TEST_SUCCESS);
3430
3431 rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
3432 assert(rc == MEMCACHED_SUCCESS);
3433
3434 /* We may have more then 32 if our previous flush has not completed */
3435 assert(counter >= 32);
3436
3437 return TEST_SUCCESS;
3438 }
3439
3440 #ifdef HAVE_LIBMEMCACHEDUTIL
3441 static void* connection_release(void *arg) {
3442 struct {
3443 memcached_pool_st* pool;
3444 memcached_st* mmc;
3445 } *resource= arg;
3446
3447 usleep(250);
3448 assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
3449 return arg;
3450 }
3451
3452 static test_return connection_pool_test(memcached_st *memc)
3453 {
3454 memcached_pool_st* pool= memcached_pool_create(memc, 5, 10);
3455 assert(pool != NULL);
3456 memcached_st* mmc[10];
3457 memcached_return rc;
3458
3459 for (int x= 0; x < 10; ++x) {
3460 mmc[x]= memcached_pool_pop(pool, false, &rc);
3461 assert(mmc[x] != NULL);
3462 assert(rc == MEMCACHED_SUCCESS);
3463 }
3464
3465 assert(memcached_pool_pop(pool, false, &rc) == NULL);
3466 assert(rc == MEMCACHED_SUCCESS);
3467
3468 pthread_t tid;
3469 struct {
3470 memcached_pool_st* pool;
3471 memcached_st* mmc;
3472 } item= { .pool = pool, .mmc = mmc[9] };
3473 pthread_create(&tid, NULL, connection_release, &item);
3474 mmc[9]= memcached_pool_pop(pool, true, &rc);
3475 assert(rc == MEMCACHED_SUCCESS);
3476 pthread_join(tid, NULL);
3477 assert(mmc[9] == item.mmc);
3478 const char *key= "key";
3479 size_t keylen= strlen(key);
3480
3481 // verify that I can do ops with all connections
3482 rc= memcached_set(mmc[0], key, keylen, "0", 1, 0, 0);
3483 assert(rc == MEMCACHED_SUCCESS);
3484
3485 for (unsigned int x= 0; x < 10; ++x) {
3486 uint64_t number_value;
3487 rc= memcached_increment(mmc[x], key, keylen, 1, &number_value);
3488 assert(rc == MEMCACHED_SUCCESS);
3489 assert(number_value == (x+1));
3490 }
3491
3492 // Release them..
3493 for (int x= 0; x < 10; ++x)
3494 assert(memcached_pool_push(pool, mmc[x]) == MEMCACHED_SUCCESS);
3495
3496 assert(memcached_pool_destroy(pool) == memc);
3497 return TEST_SUCCESS;
3498 }
3499 #endif
3500
3501 static void increment_request_id(uint16_t *id)
3502 {
3503 (*id)++;
3504 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
3505 *id= 0;
3506 }
3507
3508 static uint16_t *get_udp_request_ids(memcached_st *memc)
3509 {
3510 uint16_t *ids= malloc(sizeof(uint16_t) * memc->number_of_hosts);
3511 assert(ids != NULL);
3512 unsigned int x;
3513 for (x= 0; x < memc->number_of_hosts; x++)
3514 ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) memc->hosts[x].write_buffer);
3515
3516 return ids;
3517 }
3518
3519 static test_return post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
3520 {
3521 unsigned int x;
3522 memcached_server_st *cur_server = memc->hosts;
3523 uint16_t *cur_req_ids = get_udp_request_ids(memc);
3524 for (x= 0; x < memc->number_of_hosts; x++)
3525 {
3526 assert(cur_server[x].cursor_active == 0);
3527 assert(cur_req_ids[x] == expected_req_ids[x]);
3528 }
3529 free(expected_req_ids);
3530 free(cur_req_ids);
3531 return TEST_SUCCESS;
3532 }
3533
3534 /*
3535 ** There is a little bit of a hack here, instead of removing
3536 ** the servers, I just set num host to 0 and them add then new udp servers
3537 **/
3538 static memcached_return init_udp(memcached_st *memc)
3539 {
3540 memcached_version(memc);
3541 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3542 if (memc->hosts[0].major_version != 1 || memc->hosts[0].minor_version != 2
3543 || memc->hosts[0].micro_version < 6)
3544 return MEMCACHED_FAILURE;
3545
3546 uint32_t num_hosts= memc->number_of_hosts;
3547 unsigned int x= 0;
3548 memcached_server_st servers[num_hosts];
3549 memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts);
3550 for (x= 0; x < num_hosts; x++)
3551 memcached_server_free(&memc->hosts[x]);
3552 memc->number_of_hosts= 0;
3553 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
3554 for (x= 0; x < num_hosts; x++)
3555 {
3556 assert(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
3557 assert(memc->hosts[x].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3558 }
3559 return MEMCACHED_SUCCESS;
3560 }
3561
3562 static memcached_return binary_init_udp(memcached_st *memc)
3563 {
3564 pre_binary(memc);
3565 return init_udp(memc);
3566 }
3567
3568 /* Make sure that I cant add a tcp server to a udp client */
3569 static test_return add_tcp_server_udp_client_test(memcached_st *memc)
3570 {
3571 memcached_server_st server;
3572 memcached_server_clone(&server, &memc->hosts[0]);
3573 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3574 assert(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3575 return TEST_SUCCESS;
3576 }
3577
3578 /* Make sure that I cant add a udp server to a tcp client */
3579 static test_return add_udp_server_tcp_client_test(memcached_st *memc)
3580 {
3581 memcached_server_st server;
3582 memcached_server_clone(&server, &memc->hosts[0]);
3583 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3584
3585 memcached_st tcp_client;
3586 memcached_create(&tcp_client);
3587 assert(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3588 return TEST_SUCCESS;
3589 }
3590
3591 static test_return set_udp_behavior_test(memcached_st *memc)
3592 {
3593
3594 memcached_quit(memc);
3595 memc->number_of_hosts= 0;
3596 run_distribution(memc);
3597 assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1) == MEMCACHED_SUCCESS);
3598 assert(memc->flags & MEM_USE_UDP);
3599 assert(memc->flags & MEM_NOREPLY);;
3600
3601 assert(memc->number_of_hosts == 0);
3602
3603 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0);
3604 assert(!(memc->flags & MEM_USE_UDP));
3605 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY,0);
3606 assert(!(memc->flags & MEM_NOREPLY));
3607 return TEST_SUCCESS;
3608 }
3609
3610 static test_return udp_set_test(memcached_st *memc)
3611 {
3612 unsigned int x= 0;
3613 unsigned int num_iters= 1025; //request id rolls over at 1024
3614 for (x= 0; x < num_iters;x++)
3615 {
3616 memcached_return rc;
3617 char *key= "foo";
3618 char *value= "when we sanitize";
3619 uint16_t *expected_ids= get_udp_request_ids(memc);
3620 unsigned int server_key= memcached_generate_hash(memc,key,strlen(key));
3621 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3622 rc= memcached_set(memc, key, strlen(key),
3623 value, strlen(value),
3624 (time_t)0, (uint32_t)0);
3625 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3626 /** NB, the check below assumes that if new write_ptr is less than
3627 * the original write_ptr that we have flushed. For large payloads, this
3628 * maybe an invalid assumption, but for the small payload we have it is OK
3629 */
3630 if (rc == MEMCACHED_SUCCESS ||
3631 memc->hosts[server_key].write_buffer_offset < init_offset)
3632 increment_request_id(&expected_ids[server_key]);
3633
3634 if (rc == MEMCACHED_SUCCESS)
3635 {
3636 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3637 }
3638 else
3639 {
3640 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
3641 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
3642 }
3643 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
3644 }
3645 return TEST_SUCCESS;
3646 }
3647
3648 static test_return udp_buffered_set_test(memcached_st *memc)
3649 {
3650 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3651 return udp_set_test(memc);
3652 }
3653
3654 static test_return udp_set_too_big_test(memcached_st *memc)
3655 {
3656 memcached_return rc;
3657 char *key= "bar";
3658 char value[MAX_UDP_DATAGRAM_LENGTH];
3659 uint16_t *expected_ids= get_udp_request_ids(memc);
3660 rc= memcached_set(memc, key, strlen(key),
3661 value, MAX_UDP_DATAGRAM_LENGTH,
3662 (time_t)0, (uint32_t)0);
3663 assert(rc == MEMCACHED_WRITE_FAILURE);
3664 return post_udp_op_check(memc,expected_ids);
3665 }
3666
3667 static test_return udp_delete_test(memcached_st *memc)
3668 {
3669 unsigned int x= 0;
3670 unsigned int num_iters= 1025; //request id rolls over at 1024
3671 for (x= 0; x < num_iters;x++)
3672 {
3673 memcached_return rc;
3674 char *key= "foo";
3675 uint16_t *expected_ids=get_udp_request_ids(memc);
3676 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
3677 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3678 rc= memcached_delete(memc, key, strlen(key), 0);
3679 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3680 if (rc == MEMCACHED_SUCCESS || memc->hosts[server_key].write_buffer_offset < init_offset)
3681 increment_request_id(&expected_ids[server_key]);
3682 if (rc == MEMCACHED_SUCCESS)
3683 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3684 else
3685 {
3686 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
3687 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
3688 }
3689 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
3690 }
3691 return TEST_SUCCESS;
3692 }
3693
3694 static test_return udp_buffered_delete_test(memcached_st *memc)
3695 {
3696 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3697 return udp_delete_test(memc);
3698 }
3699
3700 static test_return udp_verbosity_test(memcached_st *memc)
3701 {
3702 memcached_return rc;
3703 uint16_t *expected_ids= get_udp_request_ids(memc);
3704 unsigned int x;
3705 for (x= 0; x < memc->number_of_hosts;x++)
3706 increment_request_id(&expected_ids[x]);
3707
3708 rc= memcached_verbosity(memc,3);
3709 assert(rc == MEMCACHED_SUCCESS);
3710 return post_udp_op_check(memc,expected_ids);
3711 }
3712
3713 static test_return udp_quit_test(memcached_st *memc)
3714 {
3715 uint16_t *expected_ids= get_udp_request_ids(memc);
3716 memcached_quit(memc);
3717 return post_udp_op_check(memc, expected_ids);
3718 }
3719
3720 static test_return udp_flush_test(memcached_st *memc)
3721 {
3722 memcached_return rc;
3723 uint16_t *expected_ids= get_udp_request_ids(memc);
3724 unsigned int x;
3725 for (x= 0; x < memc->number_of_hosts;x++)
3726 increment_request_id(&expected_ids[x]);
3727
3728 rc= memcached_flush(memc,0);
3729 assert(rc == MEMCACHED_SUCCESS);
3730 return post_udp_op_check(memc,expected_ids);
3731 }
3732
3733 static test_return udp_incr_test(memcached_st *memc)
3734 {
3735 memcached_return rc;
3736 char *key= "incr";
3737 char *value= "1";
3738 rc= memcached_set(memc, key, strlen(key),
3739 value, strlen(value),
3740 (time_t)0, (uint32_t)0);
3741
3742 assert(rc == MEMCACHED_SUCCESS);
3743 uint16_t *expected_ids= get_udp_request_ids(memc);
3744 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
3745 increment_request_id(&expected_ids[server_key]);
3746 uint64_t newvalue;
3747 rc= memcached_increment(memc, key, strlen(key), 1, &newvalue);
3748 assert(rc == MEMCACHED_SUCCESS);
3749 return post_udp_op_check(memc, expected_ids);
3750 }
3751
3752 static test_return udp_decr_test(memcached_st *memc)
3753 {
3754 memcached_return rc;
3755 char *key= "decr";
3756 char *value= "1";
3757 rc= memcached_set(memc, key, strlen(key),
3758 value, strlen(value),
3759 (time_t)0, (uint32_t)0);
3760
3761 assert(rc == MEMCACHED_SUCCESS);
3762 uint16_t *expected_ids= get_udp_request_ids(memc);
3763 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
3764 increment_request_id(&expected_ids[server_key]);
3765 uint64_t newvalue;
3766 rc= memcached_decrement(memc, key, strlen(key), 1, &newvalue);
3767 assert(rc == MEMCACHED_SUCCESS);
3768 return post_udp_op_check(memc, expected_ids);
3769 }
3770
3771
3772 static test_return udp_stat_test(memcached_st *memc)
3773 {
3774 memcached_stat_st * rv= NULL;
3775 memcached_return rc;
3776 char args[]= "";
3777 uint16_t *expected_ids = get_udp_request_ids(memc);
3778 rv = memcached_stat(memc, args, &rc);
3779 free(rv);
3780 assert(rc == MEMCACHED_NOT_SUPPORTED);
3781 return post_udp_op_check(memc, expected_ids);
3782 }
3783
3784 static test_return udp_version_test(memcached_st *memc)
3785 {
3786 memcached_return rc;
3787 uint16_t *expected_ids = get_udp_request_ids(memc);
3788 rc = memcached_version(memc);
3789 assert(rc == MEMCACHED_NOT_SUPPORTED);
3790 return post_udp_op_check(memc, expected_ids);
3791 }
3792
3793 static test_return udp_get_test(memcached_st *memc)
3794 {
3795 memcached_return rc;
3796 char *key= "foo";
3797 size_t vlen;
3798 uint16_t *expected_ids = get_udp_request_ids(memc);
3799 char *val= memcached_get(memc, key, strlen(key), &vlen, (uint32_t)0, &rc);
3800 assert(rc == MEMCACHED_NOT_SUPPORTED);
3801 assert(val == NULL);
3802 return post_udp_op_check(memc, expected_ids);
3803 }
3804
3805 static test_return udp_mixed_io_test(memcached_st *memc)
3806 {
3807 test_st current_op;
3808 test_st mixed_io_ops [] ={
3809 {"udp_set_test", 0, udp_set_test},
3810 {"udp_set_too_big_test", 0, udp_set_too_big_test},
3811 {"udp_delete_test", 0, udp_delete_test},
3812 {"udp_verbosity_test", 0, udp_verbosity_test},
3813 {"udp_quit_test", 0, udp_quit_test},
3814 {"udp_flush_test", 0, udp_flush_test},
3815 {"udp_incr_test", 0, udp_incr_test},
3816 {"udp_decr_test", 0, udp_decr_test},
3817 {"udp_version_test", 0, udp_version_test}
3818 };
3819 unsigned int x= 0;
3820 for (x= 0; x < 500; x++)
3821 {
3822 current_op= mixed_io_ops[random() % 9];
3823 assert(current_op.function(memc) == TEST_SUCCESS);
3824 }
3825 return TEST_SUCCESS;
3826 }
3827
3828 static test_return hsieh_avaibility_test (memcached_st *memc)
3829 {
3830 memcached_return expected_rc= MEMCACHED_FAILURE;
3831 #ifdef HAVE_HSIEH_HASH
3832 expected_rc= MEMCACHED_SUCCESS;
3833 #endif
3834 memcached_return rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
3835 (uint64_t)MEMCACHED_HASH_HSIEH);
3836 assert(rc == expected_rc);
3837 return TEST_SUCCESS;
3838 }
3839
3840 static char *list[]=
3841 {
3842 "apple",
3843 "beat",
3844 "carrot",
3845 "daikon",
3846 "eggplant",
3847 "flower",
3848 "green",
3849 "hide",
3850 "ick",
3851 "jack",
3852 "kick",
3853 "lime",
3854 "mushrooms",
3855 "nectarine",
3856 "orange",
3857 "peach",
3858 "quant",
3859 "ripen",
3860 "strawberry",
3861 "tang",
3862 "up",
3863 "volumne",
3864 "when",
3865 "yellow",
3866 "zip",
3867 NULL
3868 };
3869
3870 static test_return md5_run (memcached_st *memc __attribute__((unused)))
3871 {
3872 uint32_t x;
3873 char **ptr;
3874 uint32_t values[]= { 3195025439, 2556848621, 3724893440, 3332385401, 245758794, 2550894432,
3875 121710495, 3053817768, 1250994555, 1862072655, 2631955953, 2951528551,
3876 1451250070, 2820856945, 2060845566, 3646985608, 2138080750, 217675895,
3877 2230934345, 1234361223, 3968582726, 2455685270, 1293568479, 199067604,
3878 2042482093 };
3879
3880
3881 for (ptr= list, x= 0; *ptr; ptr++, x++)
3882 {
3883 uint32_t hash_val;
3884
3885 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MD5);
3886 assert(values[x] == hash_val);
3887 }
3888
3889 return TEST_SUCCESS;
3890 }
3891
3892 static test_return crc_run (memcached_st *memc __attribute__((unused)))
3893 {
3894 uint32_t x;
3895 char **ptr;
3896 uint32_t values[]= { 10542, 22009, 14526, 19510, 19432, 10199, 20634, 9369, 11511, 10362,
3897 7893, 31289, 11313, 9354, 7621, 30628, 15218, 25967, 2695, 9380,
3898 17300, 28156, 9192, 20484, 16925 };
3899
3900 for (ptr= list, x= 0; *ptr; ptr++, x++)
3901 {
3902 uint32_t hash_val;
3903
3904 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_CRC);
3905 assert(values[x] == hash_val);
3906 }
3907
3908 return TEST_SUCCESS;
3909 }
3910
3911 static test_return fnv1_64_run (memcached_st *memc __attribute__((unused)))
3912 {
3913 uint32_t x;
3914 char **ptr;
3915 uint32_t values[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
3916 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
3917 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
3918 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
3919 1801941682 };
3920
3921 for (ptr= list, x= 0; *ptr; ptr++, x++)
3922 {
3923 uint32_t hash_val;
3924
3925 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
3926 assert(values[x] == hash_val);
3927 }
3928
3929 return TEST_SUCCESS;
3930 }
3931
3932 static test_return fnv1a_64_run (memcached_st *memc __attribute__((unused)))
3933 {
3934 uint32_t x;
3935 char **ptr;
3936 uint32_t values[]= { 1488911807, 2500855813, 1510099634, 1390325195, 3647689787, 3241528582,
3937 1669328060, 2604311949, 734810122, 1516407546, 560948863, 1767346780,
3938 561034892, 4156330026, 3716417003, 3475297030, 1518272172, 227211583,
3939 3938128828, 126112909, 3043416448, 3131561933, 1328739897, 2455664041,
3940 2272238452 };
3941
3942 for (ptr= list, x= 0; *ptr; ptr++, x++)
3943 {
3944 uint32_t hash_val;
3945
3946 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_64);
3947 assert(values[x] == hash_val);
3948 }
3949
3950 return TEST_SUCCESS;
3951 }
3952
3953 static test_return fnv1_32_run (memcached_st *memc __attribute__((unused)))
3954 {
3955 uint32_t x;
3956 char **ptr;
3957 uint32_t values[]= { 67176023, 1190179409, 2043204404, 3221866419, 2567703427, 3787535528, 4147287986,
3958 3500475733, 344481048, 3865235296, 2181839183, 119581266, 510234242, 4248244304,
3959 1362796839, 103389328, 1449620010, 182962511, 3554262370, 3206747549, 1551306158,
3960 4127558461, 1889140833, 2774173721, 1180552018 };
3961
3962
3963 for (ptr= list, x= 0; *ptr; ptr++, x++)
3964 {
3965 uint32_t hash_val;
3966
3967 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_32);
3968 assert(values[x] == hash_val);
3969 }
3970
3971 return TEST_SUCCESS;
3972 }
3973
3974 static test_return fnv1a_32_run (memcached_st *memc __attribute__((unused)))
3975 {
3976 uint32_t x;
3977 char **ptr;
3978 uint32_t values[]= { 280767167, 2421315013, 3072375666, 855001899, 459261019, 3521085446, 18738364,
3979 1625305005, 2162232970, 777243802, 3323728671, 132336572, 3654473228, 260679466,
3980 1169454059, 2698319462, 1062177260, 235516991, 2218399068, 405302637, 1128467232,
3981 3579622413, 2138539289, 96429129, 2877453236 };
3982
3983 for (ptr= list, x= 0; *ptr; ptr++, x++)
3984 {
3985 uint32_t hash_val;
3986
3987 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_32);
3988 assert(values[x] == hash_val);
3989 }
3990
3991 return TEST_SUCCESS;
3992 }
3993
3994 static test_return hsieh_run (memcached_st *memc __attribute__((unused)))
3995 {
3996 uint32_t x;
3997 char **ptr;
3998 #ifdef HAVE_HSIEH_HASH
3999 uint32_t values[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4000 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4001 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4002 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4003 3395453351 };
4004 #else
4005 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 };
4006 #endif
4007
4008 for (ptr= list, x= 0; *ptr; ptr++, x++)
4009 {
4010 uint32_t hash_val;
4011
4012 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_HSIEH);
4013 assert(values[x] == hash_val);
4014 }
4015
4016 return TEST_SUCCESS;
4017 }
4018
4019 static test_return murmur_run (memcached_st *memc __attribute__((unused)))
4020 {
4021 uint32_t x;
4022 char **ptr;
4023 uint32_t values[]= { 473199127, 4148981457, 3971873300, 3257986707, 1722477987, 2991193800,
4024 4147007314, 3633179701, 1805162104, 3503289120, 3395702895, 3325073042,
4025 2345265314, 3340346032, 2722964135, 1173398992, 2815549194, 2562818319,
4026 224996066, 2680194749, 3035305390, 246890365, 2395624193, 4145193337,
4027 1801941682 };
4028
4029 for (ptr= list, x= 0; *ptr; ptr++, x++)
4030 {
4031 uint32_t hash_val;
4032
4033 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4034 assert(values[x] == hash_val);
4035 }
4036
4037 return TEST_SUCCESS;
4038 }
4039
4040 static test_return jenkins_run (memcached_st *memc __attribute__((unused)))
4041 {
4042 uint32_t x;
4043 char **ptr;
4044 uint32_t values[]= { 1442444624, 4253821186, 1885058256, 2120131735, 3261968576, 3515188778,
4045 4232909173, 4288625128, 1812047395, 3689182164, 2502979932, 1214050606,
4046 2415988847, 1494268927, 1025545760, 3920481083, 4153263658, 3824871822,
4047 3072759809, 798622255, 3065432577, 1453328165, 2691550971, 3408888387,
4048 2629893356 };
4049
4050
4051 for (ptr= list, x= 0; *ptr; ptr++, x++)
4052 {
4053 uint32_t hash_val;
4054
4055 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_JENKINS);
4056 assert(values[x] == hash_val);
4057 }
4058
4059 return TEST_SUCCESS;
4060 }
4061
4062 test_st udp_setup_server_tests[] ={
4063 {"set_udp_behavior_test", 0, set_udp_behavior_test},
4064 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test},
4065 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test},
4066 {0, 0, 0}
4067 };
4068
4069 test_st upd_io_tests[] ={
4070 {"udp_set_test", 0, udp_set_test},
4071 {"udp_buffered_set_test", 0, udp_buffered_set_test},
4072 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4073 {"udp_delete_test", 0, udp_delete_test},
4074 {"udp_buffered_delete_test", 0, udp_buffered_delete_test},
4075 {"udp_verbosity_test", 0, udp_verbosity_test},
4076 {"udp_quit_test", 0, udp_quit_test},
4077 {"udp_flush_test", 0, udp_flush_test},
4078 {"udp_incr_test", 0, udp_incr_test},
4079 {"udp_decr_test", 0, udp_decr_test},
4080 {"udp_stat_test", 0, udp_stat_test},
4081 {"udp_version_test", 0, udp_version_test},
4082 {"udp_get_test", 0, udp_get_test},
4083 {"udp_mixed_io_test", 0, udp_mixed_io_test},
4084 {0, 0, 0}
4085 };
4086
4087 /* Clean the server before beginning testing */
4088 test_st tests[] ={
4089 {"flush", 0, flush_test },
4090 {"init", 0, init_test },
4091 {"allocation", 0, allocation_test },
4092 {"server_list_null_test", 0, server_list_null_test},
4093 {"server_unsort", 0, server_unsort_test},
4094 {"server_sort", 0, server_sort_test},
4095 {"server_sort2", 0, server_sort2_test},
4096 {"clone_test", 0, clone_test },
4097 {"connection_test", 0, connection_test},
4098 {"callback_test", 0, callback_test},
4099 {"behavior_test", 0, behavior_test},
4100 {"error", 0, error_test },
4101 {"set", 0, set_test },
4102 {"set2", 0, set_test2 },
4103 {"set3", 0, set_test3 },
4104 {"dump", 1, dump_test},
4105 {"add", 1, add_test },
4106 {"replace", 1, replace_test },
4107 {"delete", 1, delete_test },
4108 {"get", 1, get_test },
4109 {"get2", 0, get_test2 },
4110 {"get3", 0, get_test3 },
4111 {"get4", 0, get_test4 },
4112 {"partial mget", 0, get_test5 },
4113 {"stats_servername", 0, stats_servername_test },
4114 {"increment", 0, increment_test },
4115 {"increment_with_initial", 1, increment_with_initial_test },
4116 {"decrement", 0, decrement_test },
4117 {"decrement_with_initial", 1, decrement_with_initial_test },
4118 {"quit", 0, quit_test },
4119 {"mget", 1, mget_test },
4120 {"mget_result", 1, mget_result_test },
4121 {"mget_result_alloc", 1, mget_result_alloc_test },
4122 {"mget_result_function", 1, mget_result_function },
4123 {"get_stats", 0, get_stats },
4124 {"add_host_test", 0, add_host_test },
4125 {"add_host_test_1", 0, add_host_test1 },
4126 {"get_stats_keys", 0, get_stats_keys },
4127 {"behavior_test", 0, get_stats_keys },
4128 {"callback_test", 0, get_stats_keys },
4129 {"version_string_test", 0, version_string_test},
4130 {"bad_key", 1, bad_key_test },
4131 {"memcached_server_cursor", 1, memcached_server_cursor_test },
4132 {"read_through", 1, read_through },
4133 {"delete_through", 1, delete_through },
4134 {"noreply", 1, noreply_test},
4135 {"analyzer", 1, analyzer_test},
4136 #ifdef HAVE_LIBMEMCACHEDUTIL
4137 {"connectionpool", 1, connection_pool_test },
4138 #endif
4139 {0, 0, 0}
4140 };
4141
4142 test_st async_tests[] ={
4143 {"add", 1, add_wrapper },
4144 {0, 0, 0}
4145 };
4146
4147 test_st string_tests[] ={
4148 {"string static with null", 0, string_static_null },
4149 {"string alloc with null", 0, string_alloc_null },
4150 {"string alloc with 1K", 0, string_alloc_with_size },
4151 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
4152 {"string append", 0, string_alloc_append },
4153 {"string append failure (too big)", 0, string_alloc_append_toobig },
4154 {0, 0, 0}
4155 };
4156
4157 test_st result_tests[] ={
4158 {"result static", 0, result_static},
4159 {"result alloc", 0, result_alloc},
4160 {0, 0, 0}
4161 };
4162
4163 test_st version_1_2_3[] ={
4164 {"append", 0, append_test },
4165 {"prepend", 0, prepend_test },
4166 {"cas", 0, cas_test },
4167 {"cas2", 0, cas2_test },
4168 {"append_binary", 0, append_binary_test },
4169 {0, 0, 0}
4170 };
4171
4172 test_st user_tests[] ={
4173 {"user_supplied_bug1", 0, user_supplied_bug1 },
4174 {"user_supplied_bug2", 0, user_supplied_bug2 },
4175 {"user_supplied_bug3", 0, user_supplied_bug3 },
4176 {"user_supplied_bug4", 0, user_supplied_bug4 },
4177 {"user_supplied_bug5", 1, user_supplied_bug5 },
4178 {"user_supplied_bug6", 1, user_supplied_bug6 },
4179 {"user_supplied_bug7", 1, user_supplied_bug7 },
4180 {"user_supplied_bug8", 1, user_supplied_bug8 },
4181 {"user_supplied_bug9", 1, user_supplied_bug9 },
4182 {"user_supplied_bug10", 1, user_supplied_bug10 },
4183 {"user_supplied_bug11", 1, user_supplied_bug11 },
4184 {"user_supplied_bug12", 1, user_supplied_bug12 },
4185 {"user_supplied_bug13", 1, user_supplied_bug13 },
4186 {"user_supplied_bug14", 1, user_supplied_bug14 },
4187 {"user_supplied_bug15", 1, user_supplied_bug15 },
4188 {"user_supplied_bug16", 1, user_supplied_bug16 },
4189 #ifndef __sun
4190 /*
4191 ** It seems to be something weird with the character sets..
4192 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4193 ** guess I need to find out how this is supposed to work.. Perhaps I need
4194 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4195 ** so just disable the code for now...).
4196 */
4197 {"user_supplied_bug17", 1, user_supplied_bug17 },
4198 #endif
4199 {"user_supplied_bug18", 1, user_supplied_bug18 },
4200 {"user_supplied_bug19", 1, user_supplied_bug19 },
4201 {"user_supplied_bug20", 1, user_supplied_bug20 },
4202 {0, 0, 0}
4203 };
4204
4205 test_st generate_tests[] ={
4206 {"generate_pairs", 1, generate_pairs },
4207 {"generate_data", 1, generate_data },
4208 {"get_read", 0, get_read },
4209 {"delete_generate", 0, delete_generate },
4210 {"generate_buffer_data", 1, generate_buffer_data },
4211 {"delete_buffer", 0, delete_buffer_generate},
4212 {"generate_data", 1, generate_data },
4213 {"mget_read", 0, mget_read },
4214 {"mget_read_result", 0, mget_read_result },
4215 {"mget_read_function", 0, mget_read_function },
4216 {"cleanup", 1, cleanup_pairs },
4217 {"generate_large_pairs", 1, generate_large_pairs },
4218 {"generate_data", 1, generate_data },
4219 {"generate_buffer_data", 1, generate_buffer_data },
4220 {"cleanup", 1, cleanup_pairs },
4221 {0, 0, 0}
4222 };
4223
4224 test_st consistent_tests[] ={
4225 {"generate_pairs", 1, generate_pairs },
4226 {"generate_data", 1, generate_data },
4227 {"get_read", 0, get_read_count },
4228 {"cleanup", 1, cleanup_pairs },
4229 {0, 0, 0}
4230 };
4231
4232 test_st consistent_weighted_tests[] ={
4233 {"generate_pairs", 1, generate_pairs },
4234 {"generate_data", 1, generate_data_with_stats },
4235 {"get_read", 0, get_read_count },
4236 {"cleanup", 1, cleanup_pairs },
4237 {0, 0, 0}
4238 };
4239
4240 test_st hsieh_availability[] ={
4241 {"hsieh_avaibility_test",0,hsieh_avaibility_test},
4242 {0, 0, 0}
4243 };
4244
4245 test_st ketama_auto_eject_hosts[] ={
4246 {"auto_eject_hosts", 1, auto_eject_hosts },
4247 {0, 0, 0}
4248 };
4249
4250 test_st hash_tests[] ={
4251 {"md5", 0, md5_run },
4252 {"crc", 0, crc_run },
4253 {"fnv1_64", 0, fnv1_64_run },
4254 {"fnv1a_64", 0, fnv1a_64_run },
4255 {"fnv1_32", 0, fnv1_32_run },
4256 {"fnv1a_32", 0, fnv1a_32_run },
4257 {"hsieh", 0, hsieh_run },
4258 {"murmur", 0, murmur_run },
4259 {"jenkis", 0, jenkins_run },
4260 {0, 0, 0}
4261 };
4262
4263 collection_st collection[] ={
4264 {"hsieh_availability",0,0,hsieh_availability},
4265 {"udp_setup", init_udp, 0, udp_setup_server_tests},
4266 {"udp_io", init_udp, 0, upd_io_tests},
4267 {"udp_binary_io", binary_init_udp, 0, upd_io_tests},
4268 {"block", 0, 0, tests},
4269 {"binary", pre_binary, 0, tests},
4270 {"nonblock", pre_nonblock, 0, tests},
4271 {"nodelay", pre_nodelay, 0, tests},
4272 {"settimer", pre_settimer, 0, tests},
4273 {"md5", pre_md5, 0, tests},
4274 {"crc", pre_crc, 0, tests},
4275 {"hsieh", pre_hsieh, 0, tests},
4276 {"jenkins", pre_jenkins, 0, tests},
4277 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
4278 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
4279 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
4280 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
4281 {"ketama", pre_behavior_ketama, 0, tests},
4282 {"ketama_auto_eject_hosts", pre_behavior_ketama, 0, ketama_auto_eject_hosts},
4283 {"unix_socket", pre_unix_socket, 0, tests},
4284 {"unix_socket_nodelay", pre_nodelay, 0, tests},
4285 {"poll_timeout", poll_timeout, 0, tests},
4286 {"gets", enable_cas, 0, tests},
4287 {"consistent", enable_consistent, 0, tests},
4288 {"memory_allocators", set_memory_alloc, 0, tests},
4289 {"prefix", set_prefix, 0, tests},
4290 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
4291 {"string", 0, 0, string_tests},
4292 {"result", 0, 0, result_tests},
4293 {"async", pre_nonblock, 0, async_tests},
4294 {"async_binary", pre_nonblock_binary, 0, async_tests},
4295 {"user", 0, 0, user_tests},
4296 {"generate", 0, 0, generate_tests},
4297 {"generate_hsieh", pre_hsieh, 0, generate_tests},
4298 {"generate_ketama", pre_behavior_ketama, 0, generate_tests},
4299 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
4300 {"generate_md5", pre_md5, 0, generate_tests},
4301 {"generate_murmur", pre_murmur, 0, generate_tests},
4302 {"generate_jenkins", pre_jenkins, 0, generate_tests},
4303 {"generate_nonblock", pre_nonblock, 0, generate_tests},
4304 {"consistent_not", 0, 0, consistent_tests},
4305 {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests},
4306 {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
4307 {"test_hashes", 0, 0, hash_tests},
4308 {0, 0, 0, 0}
4309 };
4310
4311 #define SERVERS_TO_CREATE 5
4312
4313 /* Prototypes for functions we will pass to test framework */
4314 void *world_create(void);
4315 void world_destroy(void *p);
4316
4317 void *world_create(void)
4318 {
4319 server_startup_st *construct;
4320
4321 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
4322 memset(construct, 0, sizeof(server_startup_st));
4323 construct->count= SERVERS_TO_CREATE;
4324 construct->udp= 0;
4325 server_startup(construct);
4326
4327 return construct;
4328 }
4329
4330
4331 void world_destroy(void *p)
4332 {
4333 server_startup_st *construct= (server_startup_st *)p;
4334 memcached_server_st *servers= (memcached_server_st *)construct->servers;
4335 memcached_server_list_free(servers);
4336
4337 server_shutdown(construct);
4338 free(construct);
4339 }
4340
4341 void get_world(world_st *world)
4342 {
4343 world->collections= collection;
4344 world->create= world_create;
4345 world->destroy= world_destroy;
4346 }