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