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