Deprecate MEMCACHED_NO_KEY_PROVIDED, and fixed key validation tests for the binary...
[awesomized/libmemcached] / tests / function.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <time.h>
13 #include "server.h"
14 #include "../clients/generator.h"
15 #include "../clients/execute.h"
16
17 #ifndef INT64_MAX
18 #define INT64_MAX LONG_MAX
19 #endif
20 #ifndef INT32_MAX
21 #define INT32_MAX INT_MAX
22 #endif
23
24
25 #include "test.h"
26
27 #define GLOBAL_COUNT 10000
28 #define GLOBAL2_COUNT 100
29 #define SERVERS_TO_CREATE 5
30 static uint32_t global_count;
31
32 static pairs_st *global_pairs;
33 static char *global_keys[GLOBAL_COUNT];
34 static size_t global_keys_length[GLOBAL_COUNT];
35
36 static test_return init_test(memcached_st *not_used __attribute__((unused)))
37 {
38 memcached_st memc;
39
40 (void)memcached_create(&memc);
41 memcached_free(&memc);
42
43 return 0;
44 }
45
46 static test_return server_list_null_test(memcached_st *ptr __attribute__((unused)))
47 {
48 memcached_server_st *server_list;
49 memcached_return rc;
50
51 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, NULL);
52 assert(server_list == NULL);
53
54 server_list= memcached_server_list_append_with_weight(NULL, "localhost", 0, 0, NULL);
55 assert(server_list == NULL);
56
57 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, &rc);
58 assert(server_list == NULL);
59
60 return 0;
61 }
62
63 #define TEST_PORT_COUNT 7
64 uint32_t test_ports[TEST_PORT_COUNT];
65
66 static memcached_return server_display_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
67 {
68 /* Do Nothing */
69 uint32_t bigger= *((uint32_t *)(context));
70 assert(bigger <= server->port);
71 *((uint32_t *)(context))= server->port;
72
73 return MEMCACHED_SUCCESS;
74 }
75
76 static test_return server_sort_test(memcached_st *ptr __attribute__((unused)))
77 {
78 uint32_t x;
79 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
80 memcached_return rc;
81 memcached_server_function callbacks[1];
82 memcached_st *local_memc;
83
84 local_memc= memcached_create(NULL);
85 assert(local_memc);
86 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
87
88 for (x= 0; x < TEST_PORT_COUNT; x++)
89 {
90 test_ports[x]= random() % 64000;
91 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
92 assert(local_memc->number_of_hosts == x + 1);
93 assert(local_memc->hosts[0].count == x+1);
94 assert(rc == MEMCACHED_SUCCESS);
95 }
96
97 callbacks[0]= server_display_function;
98 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
99
100
101 memcached_free(local_memc);
102
103 return 0;
104 }
105
106 static test_return server_sort2_test(memcached_st *ptr __attribute__((unused)))
107 {
108 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
109 memcached_return rc;
110 memcached_server_function callbacks[1];
111 memcached_st *local_memc;
112
113 local_memc= memcached_create(NULL);
114 assert(local_memc);
115 rc= memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
116 assert(rc == MEMCACHED_SUCCESS);
117
118 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
119 assert(rc == MEMCACHED_SUCCESS);
120 assert(local_memc->hosts[0].port == 43043);
121
122 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
123 assert(rc == MEMCACHED_SUCCESS);
124 assert(local_memc->hosts[0].port == 43042);
125 assert(local_memc->hosts[1].port == 43043);
126
127 callbacks[0]= server_display_function;
128 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
129
130
131 memcached_free(local_memc);
132
133 return 0;
134 }
135
136 static memcached_return server_display_unsort_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
137 {
138 /* Do Nothing */
139 uint32_t x= *((uint32_t *)(context));
140
141 assert(test_ports[x] == server->port);
142 *((uint32_t *)(context))= ++x;
143
144 return MEMCACHED_SUCCESS;
145 }
146
147 static test_return server_unsort_test(memcached_st *ptr __attribute__((unused)))
148 {
149 uint32_t x;
150 uint32_t counter= 0; /* Prime the value for the assert in server_display_function */
151 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
152 memcached_return rc;
153 memcached_server_function callbacks[1];
154 memcached_st *local_memc;
155
156 local_memc= memcached_create(NULL);
157 assert(local_memc);
158
159 for (x= 0; x < TEST_PORT_COUNT; x++)
160 {
161 test_ports[x]= random() % 64000;
162 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
163 assert(local_memc->number_of_hosts == x+1);
164 assert(local_memc->hosts[0].count == x+1);
165 assert(rc == MEMCACHED_SUCCESS);
166 }
167
168 callbacks[0]= server_display_unsort_function;
169 memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1);
170
171 /* Now we sort old data! */
172 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
173 callbacks[0]= server_display_function;
174 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
175
176
177 memcached_free(local_memc);
178
179 return 0;
180 }
181
182 static test_return allocation_test(memcached_st *not_used __attribute__((unused)))
183 {
184 memcached_st *memc;
185 memc= memcached_create(NULL);
186 assert(memc);
187 memcached_free(memc);
188
189 return 0;
190 }
191
192 static test_return clone_test(memcached_st *memc)
193 {
194 /* All null? */
195 {
196 memcached_st *clone;
197 clone= memcached_clone(NULL, NULL);
198 assert(clone);
199 memcached_free(clone);
200 }
201
202 /* Can we init from null? */
203 {
204 memcached_st *clone;
205 clone= memcached_clone(NULL, memc);
206 assert(clone);
207 memcached_free(clone);
208 }
209
210 /* Can we init from struct? */
211 {
212 memcached_st declared_clone;
213 memcached_st *clone;
214 memset(&declared_clone, 0 , sizeof(memcached_st));
215 clone= memcached_clone(&declared_clone, NULL);
216 assert(clone);
217 memcached_free(clone);
218 }
219
220 /* Can we init from struct? */
221 {
222 memcached_st declared_clone;
223 memcached_st *clone;
224 memset(&declared_clone, 0 , sizeof(memcached_st));
225 clone= memcached_clone(&declared_clone, memc);
226 assert(clone);
227 memcached_free(clone);
228 }
229
230 return 0;
231 }
232
233 static test_return connection_test(memcached_st *memc)
234 {
235 memcached_return rc;
236
237 rc= memcached_server_add_with_weight(memc, "localhost", 0, 0);
238 assert(rc == MEMCACHED_SUCCESS);
239
240 return 0;
241 }
242
243 static test_return error_test(memcached_st *memc)
244 {
245 memcached_return rc;
246
247 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
248 {
249 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
250 }
251
252 return 0;
253 }
254
255 static test_return set_test(memcached_st *memc)
256 {
257 memcached_return rc;
258 char *key= "foo";
259 char *value= "when we sanitize";
260
261 rc= memcached_set(memc, key, strlen(key),
262 value, strlen(value),
263 (time_t)0, (uint32_t)0);
264 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
265
266 return 0;
267 }
268
269 static test_return append_test(memcached_st *memc)
270 {
271 memcached_return rc;
272 char *key= "fig";
273 char *value= "we";
274 size_t value_length;
275 uint32_t flags;
276
277 rc= memcached_flush(memc, 0);
278 assert(rc == MEMCACHED_SUCCESS);
279
280 rc= memcached_set(memc, key, strlen(key),
281 value, strlen(value),
282 (time_t)0, (uint32_t)0);
283 assert(rc == MEMCACHED_SUCCESS);
284
285 rc= memcached_append(memc, key, strlen(key),
286 " the", strlen(" the"),
287 (time_t)0, (uint32_t)0);
288 assert(rc == MEMCACHED_SUCCESS);
289
290 rc= memcached_append(memc, key, strlen(key),
291 " people", strlen(" people"),
292 (time_t)0, (uint32_t)0);
293 assert(rc == MEMCACHED_SUCCESS);
294
295 value= memcached_get(memc, key, strlen(key),
296 &value_length, &flags, &rc);
297 assert(!memcmp(value, "we the people", strlen("we the people")));
298 assert(strlen("we the people") == value_length);
299 assert(rc == MEMCACHED_SUCCESS);
300 free(value);
301
302 return 0;
303 }
304
305 static test_return append_binary_test(memcached_st *memc)
306 {
307 memcached_return rc;
308 char *key= "numbers";
309 unsigned int *store_ptr;
310 unsigned int store_list[] = { 23, 56, 499, 98, 32847, 0 };
311 char *value;
312 size_t value_length;
313 uint32_t flags;
314 unsigned int x;
315
316 rc= memcached_flush(memc, 0);
317 assert(rc == MEMCACHED_SUCCESS);
318
319 rc= memcached_set(memc,
320 key, strlen(key),
321 NULL, 0,
322 (time_t)0, (uint32_t)0);
323 assert(rc == MEMCACHED_SUCCESS);
324
325 for (x= 0; store_list[x] ; x++)
326 {
327 rc= memcached_append(memc,
328 key, strlen(key),
329 (char *)&store_list[x], sizeof(unsigned int),
330 (time_t)0, (uint32_t)0);
331 assert(rc == MEMCACHED_SUCCESS);
332 }
333
334 value= memcached_get(memc, key, strlen(key),
335 &value_length, &flags, &rc);
336 assert((value_length == (sizeof(unsigned int) * x)));
337 assert(rc == MEMCACHED_SUCCESS);
338
339 store_ptr= (unsigned int *)value;
340 x= 0;
341 while ((size_t)store_ptr < (size_t)(value + value_length))
342 {
343 assert(*store_ptr == store_list[x++]);
344 store_ptr++;
345 }
346 free(value);
347
348 return 0;
349 }
350
351 static test_return cas2_test(memcached_st *memc)
352 {
353 memcached_return rc;
354 char *keys[]= {"fudge", "son", "food"};
355 size_t key_length[]= {5, 3, 4};
356 char *value= "we the people";
357 size_t value_length= strlen("we the people");
358 unsigned int x;
359 memcached_result_st results_obj;
360 memcached_result_st *results;
361 unsigned int set= 1;
362
363 rc= memcached_flush(memc, 0);
364 assert(rc == MEMCACHED_SUCCESS);
365
366 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
367
368 for (x= 0; x < 3; x++)
369 {
370 rc= memcached_set(memc, keys[x], key_length[x],
371 keys[x], key_length[x],
372 (time_t)50, (uint32_t)9);
373 assert(rc == MEMCACHED_SUCCESS);
374 }
375
376 rc= memcached_mget(memc, keys, key_length, 3);
377
378 results= memcached_result_create(memc, &results_obj);
379
380 results= memcached_fetch_result(memc, &results_obj, &rc);
381 assert(results);
382 assert(results->cas);
383 assert(rc == MEMCACHED_SUCCESS);
384 WATCHPOINT_ASSERT(memcached_result_cas(results));
385
386 assert(!memcmp(value, "we the people", strlen("we the people")));
387 assert(strlen("we the people") == value_length);
388 assert(rc == MEMCACHED_SUCCESS);
389
390 memcached_result_free(&results_obj);
391
392 return 0;
393 }
394
395 static test_return cas_test(memcached_st *memc)
396 {
397 memcached_return rc;
398 const char *key= "fun";
399 size_t key_length= strlen(key);
400 const char *value= "we the people";
401 size_t value_length= strlen(value);
402 const char *value2= "change the value";
403 size_t value2_length= strlen(value2);
404
405 memcached_result_st results_obj;
406 memcached_result_st *results;
407 unsigned int set= 1;
408
409 rc= memcached_flush(memc, 0);
410 assert(rc == MEMCACHED_SUCCESS);
411
412 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
413
414 rc= memcached_set(memc, key, strlen(key),
415 value, strlen(value),
416 (time_t)0, (uint32_t)0);
417 assert(rc == MEMCACHED_SUCCESS);
418
419 rc= memcached_mget(memc, &key, &key_length, 1);
420
421 results= memcached_result_create(memc, &results_obj);
422
423 results= memcached_fetch_result(memc, &results_obj, &rc);
424 assert(results);
425 assert(rc == MEMCACHED_SUCCESS);
426 WATCHPOINT_ASSERT(memcached_result_cas(results));
427 assert(!memcmp(value, memcached_result_value(results), value_length));
428 assert(strlen(memcached_result_value(results)) == value_length);
429 assert(rc == MEMCACHED_SUCCESS);
430 uint64_t cas = memcached_result_cas(results);
431
432 #if 0
433 results= memcached_fetch_result(memc, &results_obj, &rc);
434 assert(rc == MEMCACHED_END);
435 assert(results == NULL);
436 #endif
437
438 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
439 assert(rc == MEMCACHED_SUCCESS);
440
441 /*
442 * The item will have a new cas value, so try to set it again with the old
443 * value. This should fail!
444 */
445 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
446 assert(rc == MEMCACHED_DATA_EXISTS);
447
448 memcached_result_free(&results_obj);
449
450 return 0;
451 }
452
453 static test_return prepend_test(memcached_st *memc)
454 {
455 memcached_return rc;
456 char *key= "fig";
457 char *value= "people";
458 size_t value_length;
459 uint32_t flags;
460
461 rc= memcached_flush(memc, 0);
462 assert(rc == MEMCACHED_SUCCESS);
463
464 rc= memcached_set(memc, key, strlen(key),
465 value, strlen(value),
466 (time_t)0, (uint32_t)0);
467 assert(rc == MEMCACHED_SUCCESS);
468
469 rc= memcached_prepend(memc, key, strlen(key),
470 "the ", strlen("the "),
471 (time_t)0, (uint32_t)0);
472 assert(rc == MEMCACHED_SUCCESS);
473
474 rc= memcached_prepend(memc, key, strlen(key),
475 "we ", strlen("we "),
476 (time_t)0, (uint32_t)0);
477 assert(rc == MEMCACHED_SUCCESS);
478
479 value= memcached_get(memc, key, strlen(key),
480 &value_length, &flags, &rc);
481 assert(!memcmp(value, "we the people", strlen("we the people")));
482 assert(strlen("we the people") == value_length);
483 assert(rc == MEMCACHED_SUCCESS);
484 free(value);
485
486 return 0;
487 }
488
489 /*
490 Set the value, then quit to make sure it is flushed.
491 Come back in and test that add fails.
492 */
493 static test_return add_test(memcached_st *memc)
494 {
495 memcached_return rc;
496 char *key= "foo";
497 char *value= "when we sanitize";
498 unsigned long long setting_value;
499
500 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
501
502 rc= memcached_set(memc, key, strlen(key),
503 value, strlen(value),
504 (time_t)0, (uint32_t)0);
505 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
506 memcached_quit(memc);
507 rc= memcached_add(memc, key, strlen(key),
508 value, strlen(value),
509 (time_t)0, (uint32_t)0);
510
511 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
512 if (setting_value)
513 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
514 else
515 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_DATA_EXISTS);
516
517 return 0;
518 }
519
520 static test_return add_wrapper(memcached_st *memc)
521 {
522 unsigned int x;
523
524 for (x= 0; x < 10000; x++)
525 add_test(memc);
526
527 return 0;
528 }
529
530 static test_return replace_test(memcached_st *memc)
531 {
532 memcached_return rc;
533 char *key= "foo";
534 char *value= "when we sanitize";
535 char *original= "first we insert some data";
536
537 rc= memcached_set(memc, key, strlen(key),
538 original, strlen(original),
539 (time_t)0, (uint32_t)0);
540 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
541
542 rc= memcached_replace(memc, key, strlen(key),
543 value, strlen(value),
544 (time_t)0, (uint32_t)0);
545 assert(rc == MEMCACHED_SUCCESS);
546
547 return 0;
548 }
549
550 static test_return delete_test(memcached_st *memc)
551 {
552 memcached_return rc;
553 char *key= "foo";
554 char *value= "when we sanitize";
555
556 rc= memcached_set(memc, key, strlen(key),
557 value, strlen(value),
558 (time_t)0, (uint32_t)0);
559 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
560
561 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
562 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
563
564 return 0;
565 }
566
567 static test_return flush_test(memcached_st *memc)
568 {
569 memcached_return rc;
570
571 rc= memcached_flush(memc, 0);
572 assert(rc == MEMCACHED_SUCCESS);
573
574 return 0;
575 }
576
577 static memcached_return server_function(memcached_st *ptr __attribute__((unused)),
578 memcached_server_st *server __attribute__((unused)),
579 void *context __attribute__((unused)))
580 {
581 /* Do Nothing */
582
583 return MEMCACHED_SUCCESS;
584 }
585
586 static test_return memcached_server_cursor_test(memcached_st *memc)
587 {
588 char *context= "foo bad";
589 memcached_server_function callbacks[1];
590
591 callbacks[0]= server_function;
592 memcached_server_cursor(memc, callbacks, context, 1);
593
594 return 0;
595 }
596
597 static test_return bad_key_test(memcached_st *memc)
598 {
599 memcached_return rc;
600 char *key= "foo bad";
601 char *string;
602 size_t string_length;
603 uint32_t flags;
604 memcached_st *clone;
605 unsigned int set= 1;
606 size_t max_keylen= 0xffff;
607
608 clone= memcached_clone(NULL, memc);
609 assert(clone);
610
611 rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
612 assert(rc == MEMCACHED_SUCCESS);
613
614 /* All keys are valid in the binary protocol (except for length) */
615 if (memcached_behavior_get(clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0)
616 {
617 string= memcached_get(clone, key, strlen(key),
618 &string_length, &flags, &rc);
619 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
620 assert(string_length == 0);
621 assert(!string);
622
623 set= 0;
624 rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
625 assert(rc == MEMCACHED_SUCCESS);
626 string= memcached_get(clone, key, strlen(key),
627 &string_length, &flags, &rc);
628 assert(rc == MEMCACHED_NOTFOUND);
629 assert(string_length == 0);
630 assert(!string);
631
632 /* Test multi key for bad keys */
633 char *keys[] = { "GoodKey", "Bad Key", "NotMine" };
634 size_t key_lengths[] = { 7, 7, 7 };
635 set= 1;
636 rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
637 assert(rc == MEMCACHED_SUCCESS);
638
639 rc= memcached_mget(clone, keys, key_lengths, 3);
640 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
641
642 rc= memcached_mget_by_key(clone, "foo daddy", 9, keys, key_lengths, 1);
643 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
644
645 max_keylen= 250;
646
647 /* The following test should be moved to the end of this function when the
648 memcached server is updated to allow max size length of the keys in the
649 binary protocol
650 */
651 rc= memcached_callback_set(clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
652 assert(rc == MEMCACHED_SUCCESS);
653
654 char *longkey= malloc(max_keylen + 1);
655 if (longkey != NULL)
656 {
657 memset(longkey, 'a', max_keylen + 1);
658 string= memcached_get(clone, longkey, max_keylen,
659 &string_length, &flags, &rc);
660 assert(rc == MEMCACHED_NOTFOUND);
661 assert(string_length == 0);
662 assert(!string);
663
664 string= memcached_get(clone, longkey, max_keylen + 1,
665 &string_length, &flags, &rc);
666 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
667 assert(string_length == 0);
668 assert(!string);
669
670 free(longkey);
671 }
672 }
673
674 /* Make sure zero length keys are marked as bad */
675 set= 1;
676 rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
677 assert(rc == MEMCACHED_SUCCESS);
678 string= memcached_get(clone, key, 0,
679 &string_length, &flags, &rc);
680 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
681 assert(string_length == 0);
682 assert(!string);
683
684 memcached_free(clone);
685
686 return 0;
687 }
688
689 #define READ_THROUGH_VALUE "set for me"
690 static memcached_return read_through_trigger(memcached_st *memc __attribute__((unused)),
691 char *key __attribute__((unused)),
692 size_t key_length __attribute__((unused)),
693 memcached_result_st *result)
694 {
695
696 return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
697 }
698
699 static test_return read_through(memcached_st *memc)
700 {
701 memcached_return rc;
702 char *key= "foo";
703 char *string;
704 size_t string_length;
705 uint32_t flags;
706
707 string= memcached_get(memc, key, strlen(key),
708 &string_length, &flags, &rc);
709
710 assert(rc == MEMCACHED_NOTFOUND);
711 assert(string_length == 0);
712 assert(!string);
713
714 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE, (void *)read_through_trigger);
715 assert(rc == MEMCACHED_SUCCESS);
716
717 string= memcached_get(memc, key, strlen(key),
718 &string_length, &flags, &rc);
719
720 assert(rc == MEMCACHED_SUCCESS);
721 assert(string_length == strlen(READ_THROUGH_VALUE));
722 assert(!strcmp(READ_THROUGH_VALUE, string));
723 free(string);
724
725 string= memcached_get(memc, key, strlen(key),
726 &string_length, &flags, &rc);
727
728 assert(rc == MEMCACHED_SUCCESS);
729 assert(string_length == strlen(READ_THROUGH_VALUE));
730 assert(!strcmp(READ_THROUGH_VALUE, string));
731 free(string);
732
733 return 0;
734 }
735
736 static memcached_return delete_trigger(memcached_st *ptr __attribute__((unused)),
737 const char *key,
738 size_t key_length __attribute__((unused)))
739 {
740 assert(key);
741
742 return MEMCACHED_SUCCESS;
743 }
744
745 static test_return delete_through(memcached_st *memc)
746 {
747 memcached_trigger_delete_key callback;
748 memcached_return rc;
749
750 callback= delete_trigger;
751
752 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, callback);
753 assert(rc == MEMCACHED_SUCCESS);
754
755 return 0;
756 }
757
758 static test_return get_test(memcached_st *memc)
759 {
760 memcached_return rc;
761 char *key= "foo";
762 char *string;
763 size_t string_length;
764 uint32_t flags;
765
766 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
767 assert(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
768
769 string= memcached_get(memc, key, strlen(key),
770 &string_length, &flags, &rc);
771
772 assert(rc == MEMCACHED_NOTFOUND);
773 assert(string_length == 0);
774 assert(!string);
775
776 return 0;
777 }
778
779 static test_return get_test2(memcached_st *memc)
780 {
781 memcached_return rc;
782 char *key= "foo";
783 char *value= "when we sanitize";
784 char *string;
785 size_t string_length;
786 uint32_t flags;
787
788 rc= memcached_set(memc, key, strlen(key),
789 value, strlen(value),
790 (time_t)0, (uint32_t)0);
791 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
792
793 string= memcached_get(memc, key, strlen(key),
794 &string_length, &flags, &rc);
795
796 assert(string);
797 assert(rc == MEMCACHED_SUCCESS);
798 assert(string_length == strlen(value));
799 assert(!memcmp(string, value, string_length));
800
801 free(string);
802
803 return 0;
804 }
805
806 static test_return set_test2(memcached_st *memc)
807 {
808 memcached_return rc;
809 char *key= "foo";
810 char *value= "train in the brain";
811 size_t value_length= strlen(value);
812 unsigned int x;
813
814 for (x= 0; x < 10; x++)
815 {
816 rc= memcached_set(memc, key, strlen(key),
817 value, value_length,
818 (time_t)0, (uint32_t)0);
819 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
820 }
821
822 return 0;
823 }
824
825 static test_return set_test3(memcached_st *memc)
826 {
827 memcached_return rc;
828 char *key= "foo";
829 char *value;
830 size_t value_length= 8191;
831 unsigned int x;
832
833 value = (char*)malloc(value_length);
834 assert(value);
835
836 for (x= 0; x < value_length; x++)
837 value[x] = (char) (x % 127);
838
839 for (x= 0; x < 1; x++)
840 {
841 rc= memcached_set(memc, key, strlen(key),
842 value, value_length,
843 (time_t)0, (uint32_t)0);
844 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
845 }
846
847 free(value);
848
849 return 0;
850 }
851
852 static test_return get_test3(memcached_st *memc)
853 {
854 memcached_return rc;
855 char *key= "foo";
856 char *value;
857 size_t value_length= 8191;
858 char *string;
859 size_t string_length;
860 uint32_t flags;
861 uint32_t x;
862
863 value = (char*)malloc(value_length);
864 assert(value);
865
866 for (x= 0; x < value_length; x++)
867 value[x] = (char) (x % 127);
868
869 rc= memcached_set(memc, key, strlen(key),
870 value, value_length,
871 (time_t)0, (uint32_t)0);
872 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
873
874 string= memcached_get(memc, key, strlen(key),
875 &string_length, &flags, &rc);
876
877 assert(rc == MEMCACHED_SUCCESS);
878 assert(string);
879 assert(string_length == value_length);
880 assert(!memcmp(string, value, string_length));
881
882 free(string);
883 free(value);
884
885 return 0;
886 }
887
888 static test_return get_test4(memcached_st *memc)
889 {
890 memcached_return rc;
891 char *key= "foo";
892 char *value;
893 size_t value_length= 8191;
894 char *string;
895 size_t string_length;
896 uint32_t flags;
897 uint32_t x;
898
899 value = (char*)malloc(value_length);
900 assert(value);
901
902 for (x= 0; x < value_length; x++)
903 value[x] = (char) (x % 127);
904
905 rc= memcached_set(memc, key, strlen(key),
906 value, value_length,
907 (time_t)0, (uint32_t)0);
908 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
909
910 for (x= 0; x < 10; x++)
911 {
912 string= memcached_get(memc, key, strlen(key),
913 &string_length, &flags, &rc);
914
915 assert(rc == MEMCACHED_SUCCESS);
916 assert(string);
917 assert(string_length == value_length);
918 assert(!memcmp(string, value, string_length));
919 free(string);
920 }
921
922 free(value);
923
924 return 0;
925 }
926
927 /* Do not copy the style of this code, I just access hosts to testthis function */
928 static test_return stats_servername_test(memcached_st *memc)
929 {
930 memcached_return rc;
931 memcached_stat_st stat;
932 rc= memcached_stat_servername(&stat, NULL,
933 memc->hosts[0].hostname,
934 memc->hosts[0].port);
935
936 return 0;
937 }
938
939 static test_return increment_test(memcached_st *memc)
940 {
941 uint64_t new_number;
942 memcached_return rc;
943 char *key= "number";
944 char *value= "0";
945
946 rc= memcached_set(memc, key, strlen(key),
947 value, strlen(value),
948 (time_t)0, (uint32_t)0);
949 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
950
951 rc= memcached_increment(memc, key, strlen(key),
952 1, &new_number);
953 assert(rc == MEMCACHED_SUCCESS);
954 assert(new_number == 1);
955
956 rc= memcached_increment(memc, key, strlen(key),
957 1, &new_number);
958 assert(rc == MEMCACHED_SUCCESS);
959 assert(new_number == 2);
960
961 return 0;
962 }
963
964 static test_return decrement_test(memcached_st *memc)
965 {
966 uint64_t new_number;
967 memcached_return rc;
968 char *key= "number";
969 char *value= "3";
970
971 rc= memcached_set(memc, key, strlen(key),
972 value, strlen(value),
973 (time_t)0, (uint32_t)0);
974 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
975
976 rc= memcached_decrement(memc, key, strlen(key),
977 1, &new_number);
978 assert(rc == MEMCACHED_SUCCESS);
979 assert(new_number == 2);
980
981 rc= memcached_decrement(memc, key, strlen(key),
982 1, &new_number);
983 assert(rc == MEMCACHED_SUCCESS);
984 assert(new_number == 1);
985
986 return 0;
987 }
988
989 static test_return quit_test(memcached_st *memc)
990 {
991 memcached_return rc;
992 char *key= "fudge";
993 char *value= "sanford and sun";
994
995 rc= memcached_set(memc, key, strlen(key),
996 value, strlen(value),
997 (time_t)10, (uint32_t)3);
998 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
999 memcached_quit(memc);
1000
1001 rc= memcached_set(memc, key, strlen(key),
1002 value, strlen(value),
1003 (time_t)50, (uint32_t)9);
1004 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1005
1006 return 0;
1007 }
1008
1009 static test_return mget_result_test(memcached_st *memc)
1010 {
1011 memcached_return rc;
1012 char *keys[]= {"fudge", "son", "food"};
1013 size_t key_length[]= {5, 3, 4};
1014 unsigned int x;
1015
1016 memcached_result_st results_obj;
1017 memcached_result_st *results;
1018
1019 results= memcached_result_create(memc, &results_obj);
1020 assert(results);
1021 assert(&results_obj == results);
1022
1023 /* We need to empty the server before continueing test */
1024 rc= memcached_flush(memc, 0);
1025 assert(rc == MEMCACHED_SUCCESS);
1026
1027 rc= memcached_mget(memc, keys, key_length, 3);
1028 assert(rc == MEMCACHED_SUCCESS);
1029
1030 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1031 {
1032 assert(results);
1033 }
1034
1035 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1036 assert(!results);
1037 assert(rc == MEMCACHED_END);
1038
1039 for (x= 0; x < 3; x++)
1040 {
1041 rc= memcached_set(memc, keys[x], key_length[x],
1042 keys[x], key_length[x],
1043 (time_t)50, (uint32_t)9);
1044 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1045 }
1046
1047 rc= memcached_mget(memc, keys, key_length, 3);
1048 assert(rc == MEMCACHED_SUCCESS);
1049
1050 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1051 {
1052 assert(results);
1053 assert(&results_obj == results);
1054 assert(rc == MEMCACHED_SUCCESS);
1055 assert(memcached_result_key_length(results) == memcached_result_length(results));
1056 assert(!memcmp(memcached_result_key_value(results),
1057 memcached_result_value(results),
1058 memcached_result_length(results)));
1059 }
1060
1061 memcached_result_free(&results_obj);
1062
1063 return 0;
1064 }
1065
1066 static test_return mget_result_alloc_test(memcached_st *memc)
1067 {
1068 memcached_return rc;
1069 char *keys[]= {"fudge", "son", "food"};
1070 size_t key_length[]= {5, 3, 4};
1071 unsigned int x;
1072
1073 memcached_result_st *results;
1074
1075 /* We need to empty the server before continueing test */
1076 rc= memcached_flush(memc, 0);
1077 assert(rc == MEMCACHED_SUCCESS);
1078
1079 rc= memcached_mget(memc, keys, key_length, 3);
1080 assert(rc == MEMCACHED_SUCCESS);
1081
1082 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
1083 {
1084 assert(results);
1085 }
1086 assert(!results);
1087 assert(rc == MEMCACHED_END);
1088
1089 for (x= 0; x < 3; x++)
1090 {
1091 rc= memcached_set(memc, keys[x], key_length[x],
1092 keys[x], key_length[x],
1093 (time_t)50, (uint32_t)9);
1094 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1095 }
1096
1097 rc= memcached_mget(memc, keys, key_length, 3);
1098 assert(rc == MEMCACHED_SUCCESS);
1099
1100 x= 0;
1101 while ((results= memcached_fetch_result(memc, NULL, &rc)))
1102 {
1103 assert(results);
1104 assert(rc == MEMCACHED_SUCCESS);
1105 assert(memcached_result_key_length(results) == memcached_result_length(results));
1106 assert(!memcmp(memcached_result_key_value(results),
1107 memcached_result_value(results),
1108 memcached_result_length(results)));
1109 memcached_result_free(results);
1110 x++;
1111 }
1112
1113 return 0;
1114 }
1115
1116 /* Count the results */
1117 static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
1118 memcached_result_st *result __attribute__((unused)),
1119 void *context)
1120 {
1121 unsigned int *counter= (unsigned int *)context;
1122
1123 *counter= *counter + 1;
1124
1125 return MEMCACHED_SUCCESS;
1126 }
1127
1128 static test_return mget_result_function(memcached_st *memc)
1129 {
1130 memcached_return rc;
1131 char *keys[]= {"fudge", "son", "food"};
1132 size_t key_length[]= {5, 3, 4};
1133 unsigned int x;
1134 unsigned int counter;
1135 memcached_execute_function callbacks[1];
1136
1137 /* We need to empty the server before continueing test */
1138 rc= memcached_flush(memc, 0);
1139 for (x= 0; x < 3; x++)
1140 {
1141 rc= memcached_set(memc, keys[x], key_length[x],
1142 keys[x], key_length[x],
1143 (time_t)50, (uint32_t)9);
1144 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1145 }
1146
1147 rc= memcached_mget(memc, keys, key_length, 3);
1148 assert(rc == MEMCACHED_SUCCESS);
1149
1150 callbacks[0]= &callback_counter;
1151 counter= 0;
1152 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1153
1154 assert(counter == 3);
1155
1156 return 0;
1157 }
1158
1159 static test_return mget_test(memcached_st *memc)
1160 {
1161 memcached_return rc;
1162 char *keys[]= {"fudge", "son", "food"};
1163 size_t key_length[]= {5, 3, 4};
1164 unsigned int x;
1165 uint32_t flags;
1166
1167 char return_key[MEMCACHED_MAX_KEY];
1168 size_t return_key_length;
1169 char *return_value;
1170 size_t return_value_length;
1171
1172 /* We need to empty the server before continueing test */
1173 rc= memcached_flush(memc, 0);
1174 assert(rc == MEMCACHED_SUCCESS);
1175
1176 rc= memcached_mget(memc, keys, key_length, 3);
1177 assert(rc == MEMCACHED_SUCCESS);
1178
1179 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1180 &return_value_length, &flags, &rc)) != NULL)
1181 {
1182 assert(return_value);
1183 }
1184 assert(!return_value);
1185 assert(return_value_length == 0);
1186 assert(rc == MEMCACHED_END);
1187
1188 for (x= 0; x < 3; x++)
1189 {
1190 rc= memcached_set(memc, keys[x], key_length[x],
1191 keys[x], key_length[x],
1192 (time_t)50, (uint32_t)9);
1193 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1194 }
1195
1196 rc= memcached_mget(memc, keys, key_length, 3);
1197 assert(rc == MEMCACHED_SUCCESS);
1198
1199 x= 0;
1200 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1201 &return_value_length, &flags, &rc)))
1202 {
1203 assert(return_value);
1204 assert(rc == MEMCACHED_SUCCESS);
1205 assert(return_key_length == return_value_length);
1206 assert(!memcmp(return_value, return_key, return_value_length));
1207 free(return_value);
1208 x++;
1209 }
1210
1211 return 0;
1212 }
1213
1214 static test_return get_stats_keys(memcached_st *memc)
1215 {
1216 char **list;
1217 char **ptr;
1218 memcached_stat_st stat;
1219 memcached_return rc;
1220
1221 list= memcached_stat_get_keys(memc, &stat, &rc);
1222 assert(rc == MEMCACHED_SUCCESS);
1223 for (ptr= list; *ptr; ptr++)
1224 assert(*ptr);
1225 fflush(stdout);
1226
1227 free(list);
1228
1229 return 0;
1230 }
1231
1232 static test_return version_string_test(memcached_st *memc __attribute__((unused)))
1233 {
1234 const char *version_string;
1235
1236 version_string= memcached_lib_version();
1237
1238 assert(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
1239
1240 return 0;
1241 }
1242
1243 static test_return get_stats(memcached_st *memc)
1244 {
1245 unsigned int x;
1246 char **list;
1247 char **ptr;
1248 memcached_return rc;
1249 memcached_stat_st *stat;
1250
1251 stat= memcached_stat(memc, NULL, &rc);
1252 assert(rc == MEMCACHED_SUCCESS);
1253
1254 assert(rc == MEMCACHED_SUCCESS);
1255 assert(stat);
1256
1257 for (x= 0; x < memcached_server_count(memc); x++)
1258 {
1259 list= memcached_stat_get_keys(memc, stat+x, &rc);
1260 assert(rc == MEMCACHED_SUCCESS);
1261 for (ptr= list; *ptr; ptr++);
1262
1263 free(list);
1264 }
1265
1266 memcached_stat_free(NULL, stat);
1267
1268 return 0;
1269 }
1270
1271 static test_return add_host_test(memcached_st *memc)
1272 {
1273 unsigned int x;
1274 memcached_server_st *servers;
1275 memcached_return rc;
1276 char servername[]= "0.example.com";
1277
1278 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
1279 assert(servers);
1280 assert(1 == memcached_server_list_count(servers));
1281
1282 for (x= 2; x < 20; x++)
1283 {
1284 char buffer[SMALL_STRING_LEN];
1285
1286 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1287 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
1288 &rc);
1289 assert(rc == MEMCACHED_SUCCESS);
1290 assert(x == memcached_server_list_count(servers));
1291 }
1292
1293 rc= memcached_server_push(memc, servers);
1294 assert(rc == MEMCACHED_SUCCESS);
1295 rc= memcached_server_push(memc, servers);
1296 assert(rc == MEMCACHED_SUCCESS);
1297
1298 memcached_server_list_free(servers);
1299
1300 return 0;
1301 }
1302
1303 static memcached_return clone_test_callback(memcached_st *parent __attribute__((unused)), memcached_st *clone __attribute__((unused)))
1304 {
1305 return MEMCACHED_SUCCESS;
1306 }
1307
1308 static memcached_return cleanup_test_callback(memcached_st *ptr __attribute__((unused)))
1309 {
1310 return MEMCACHED_SUCCESS;
1311 }
1312
1313 static test_return callback_test(memcached_st *memc)
1314 {
1315 /* Test User Data */
1316 {
1317 int x= 5;
1318 int *test_ptr;
1319 memcached_return rc;
1320
1321 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
1322 assert(rc == MEMCACHED_SUCCESS);
1323 test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
1324 assert(*test_ptr == x);
1325 }
1326
1327 /* Test Clone Callback */
1328 {
1329 memcached_clone_func temp_function;
1330 memcached_return rc;
1331
1332 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, clone_test_callback);
1333 assert(rc == MEMCACHED_SUCCESS);
1334 temp_function= (memcached_clone_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1335 assert(temp_function == clone_test_callback);
1336 }
1337
1338 /* Test Cleanup Callback */
1339 {
1340 memcached_cleanup_func temp_function;
1341 memcached_return rc;
1342
1343 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, cleanup_test_callback);
1344 assert(rc == MEMCACHED_SUCCESS);
1345 temp_function= (memcached_cleanup_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1346 assert(temp_function == cleanup_test_callback);
1347 }
1348
1349 return 0;
1350 }
1351
1352 /* We don't test the behavior itself, we test the switches */
1353 static test_return behavior_test(memcached_st *memc)
1354 {
1355 uint64_t value;
1356 uint32_t set= 1;
1357
1358 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1359 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1360 assert(value == 1);
1361
1362 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1363 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1364 assert(value == 1);
1365
1366 set= MEMCACHED_HASH_MD5;
1367 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1368 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1369 assert(value == MEMCACHED_HASH_MD5);
1370
1371 set= 0;
1372
1373 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1374 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1375 assert(value == 0);
1376
1377 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1378 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1379 assert(value == 0);
1380
1381 set= MEMCACHED_HASH_DEFAULT;
1382 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1383 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1384 assert(value == MEMCACHED_HASH_DEFAULT);
1385
1386 set= MEMCACHED_HASH_CRC;
1387 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1388 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1389 assert(value == MEMCACHED_HASH_CRC);
1390
1391 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1392 assert(value > 0);
1393
1394 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1395 assert(value > 0);
1396
1397 return 0;
1398 }
1399
1400 /* Test case provided by Cal Haldenbrand */
1401 static test_return user_supplied_bug1(memcached_st *memc)
1402 {
1403 unsigned int setter= 1;
1404 unsigned int x;
1405
1406 unsigned long long total= 0;
1407 uint32_t size= 0;
1408 char key[10];
1409 char randomstuff[6 * 1024];
1410 memcached_return rc;
1411
1412 memset(randomstuff, 0, 6 * 1024);
1413
1414 /* We just keep looking at the same values over and over */
1415 srandom(10);
1416
1417 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1418 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1419
1420
1421 /* add key */
1422 for (x= 0 ; total < 20 * 1024576 ; x++ )
1423 {
1424 unsigned int j= 0;
1425
1426 size= (rand() % ( 5 * 1024 ) ) + 400;
1427 memset(randomstuff, 0, 6 * 1024);
1428 assert(size < 6 * 1024); /* Being safe here */
1429
1430 for (j= 0 ; j < size ;j++)
1431 randomstuff[j] = (char) (rand() % 26) + 97;
1432
1433 total += size;
1434 sprintf(key, "%d", x);
1435 rc = memcached_set(memc, key, strlen(key),
1436 randomstuff, strlen(randomstuff), 10, 0);
1437 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1438 /* If we fail, lets try again */
1439 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
1440 rc = memcached_set(memc, key, strlen(key),
1441 randomstuff, strlen(randomstuff), 10, 0);
1442 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1443 }
1444
1445 return 0;
1446 }
1447
1448 /* Test case provided by Cal Haldenbrand */
1449 static test_return user_supplied_bug2(memcached_st *memc)
1450 {
1451 int errors;
1452 unsigned int setter;
1453 unsigned int x;
1454 unsigned long long total;
1455
1456 setter= 1;
1457 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1458 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1459 #ifdef NOT_YET
1460 setter = 20 * 1024576;
1461 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1462 setter = 20 * 1024576;
1463 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1464 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1465 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1466
1467 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
1468 #endif
1469
1470 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
1471 {
1472 memcached_return rc= MEMCACHED_SUCCESS;
1473 char buffer[SMALL_STRING_LEN];
1474 uint32_t flags= 0;
1475 size_t val_len= 0;
1476 char *getval;
1477
1478 memset(buffer, 0, SMALL_STRING_LEN);
1479
1480 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
1481 getval= memcached_get(memc, buffer, strlen(buffer),
1482 &val_len, &flags, &rc);
1483 if (rc != MEMCACHED_SUCCESS)
1484 {
1485 if (rc == MEMCACHED_NOTFOUND)
1486 errors++;
1487 else
1488 {
1489 WATCHPOINT_ERROR(rc);
1490 assert(0);
1491 }
1492
1493 continue;
1494 }
1495 total+= val_len;
1496 errors= 0;
1497 free(getval);
1498 }
1499
1500 return 0;
1501 }
1502
1503 /* Do a large mget() over all the keys we think exist */
1504 #define KEY_COUNT 3000 // * 1024576
1505 static test_return user_supplied_bug3(memcached_st *memc)
1506 {
1507 memcached_return rc;
1508 unsigned int setter;
1509 unsigned int x;
1510 char **keys;
1511 size_t key_lengths[KEY_COUNT];
1512
1513 setter= 1;
1514 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1515 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1516 #ifdef NOT_YET
1517 setter = 20 * 1024576;
1518 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1519 setter = 20 * 1024576;
1520 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1521 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1522 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1523 #endif
1524
1525 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
1526 assert(keys);
1527 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
1528 for (x= 0; x < KEY_COUNT; x++)
1529 {
1530 char buffer[30];
1531
1532 snprintf(buffer, 30, "%u", x);
1533 keys[x]= strdup(buffer);
1534 key_lengths[x]= strlen(keys[x]);
1535 }
1536
1537 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
1538 assert(rc == MEMCACHED_SUCCESS);
1539
1540 /* Turn this into a help function */
1541 {
1542 char return_key[MEMCACHED_MAX_KEY];
1543 size_t return_key_length;
1544 char *return_value;
1545 size_t return_value_length;
1546 uint32_t flags;
1547
1548 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1549 &return_value_length, &flags, &rc)))
1550 {
1551 assert(return_value);
1552 assert(rc == MEMCACHED_SUCCESS);
1553 free(return_value);
1554 }
1555 }
1556
1557 for (x= 0; x < KEY_COUNT; x++)
1558 free(keys[x]);
1559 free(keys);
1560
1561 return 0;
1562 }
1563
1564 /* Make sure we behave properly if server list has no values */
1565 static test_return user_supplied_bug4(memcached_st *memc)
1566 {
1567 memcached_return rc;
1568 char *keys[]= {"fudge", "son", "food"};
1569 size_t key_length[]= {5, 3, 4};
1570 unsigned int x;
1571 uint32_t flags;
1572 char return_key[MEMCACHED_MAX_KEY];
1573 size_t return_key_length;
1574 char *return_value;
1575 size_t return_value_length;
1576
1577 /* Here we free everything before running a bunch of mget tests */
1578 {
1579 memcached_server_list_free(memc->hosts);
1580 memc->hosts= NULL;
1581 memc->number_of_hosts= 0;
1582 }
1583
1584
1585 /* We need to empty the server before continueing test */
1586 rc= memcached_flush(memc, 0);
1587 assert(rc == MEMCACHED_NO_SERVERS);
1588
1589 rc= memcached_mget(memc, keys, key_length, 3);
1590 assert(rc == MEMCACHED_NO_SERVERS);
1591
1592 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1593 &return_value_length, &flags, &rc)) != NULL)
1594 {
1595 assert(return_value);
1596 }
1597 assert(!return_value);
1598 assert(return_value_length == 0);
1599 assert(rc == MEMCACHED_NO_SERVERS);
1600
1601 for (x= 0; x < 3; x++)
1602 {
1603 rc= memcached_set(memc, keys[x], key_length[x],
1604 keys[x], key_length[x],
1605 (time_t)50, (uint32_t)9);
1606 assert(rc == MEMCACHED_NO_SERVERS);
1607 }
1608
1609 rc= memcached_mget(memc, keys, key_length, 3);
1610 assert(rc == MEMCACHED_NO_SERVERS);
1611
1612 x= 0;
1613 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1614 &return_value_length, &flags, &rc)))
1615 {
1616 assert(return_value);
1617 assert(rc == MEMCACHED_SUCCESS);
1618 assert(return_key_length == return_value_length);
1619 assert(!memcmp(return_value, return_key, return_value_length));
1620 free(return_value);
1621 x++;
1622 }
1623
1624 return 0;
1625 }
1626
1627 #define VALUE_SIZE_BUG5 1048064
1628 static test_return user_supplied_bug5(memcached_st *memc)
1629 {
1630 memcached_return rc;
1631 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1632 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1633 char return_key[MEMCACHED_MAX_KEY];
1634 size_t return_key_length;
1635 char *value;
1636 size_t value_length;
1637 uint32_t flags;
1638 unsigned int count;
1639 unsigned int x;
1640 char insert_data[VALUE_SIZE_BUG5];
1641
1642 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1643 insert_data[x]= rand();
1644
1645 memcached_flush(memc, 0);
1646 value= memcached_get(memc, keys[0], key_length[0],
1647 &value_length, &flags, &rc);
1648 assert(value == NULL);
1649 rc= memcached_mget(memc, keys, key_length, 4);
1650
1651 count= 0;
1652 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1653 &value_length, &flags, &rc)))
1654 count++;
1655 assert(count == 0);
1656
1657 for (x= 0; x < 4; x++)
1658 {
1659 rc= memcached_set(memc, keys[x], key_length[x],
1660 insert_data, VALUE_SIZE_BUG5,
1661 (time_t)0, (uint32_t)0);
1662 assert(rc == MEMCACHED_SUCCESS);
1663 }
1664
1665 for (x= 0; x < 10; x++)
1666 {
1667 value= memcached_get(memc, keys[0], key_length[0],
1668 &value_length, &flags, &rc);
1669 assert(value);
1670 free(value);
1671
1672 rc= memcached_mget(memc, keys, key_length, 4);
1673 count= 0;
1674 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1675 &value_length, &flags, &rc)))
1676 {
1677 count++;
1678 free(value);
1679 }
1680 assert(count == 4);
1681 }
1682
1683 return 0;
1684 }
1685
1686 static test_return user_supplied_bug6(memcached_st *memc)
1687 {
1688 memcached_return rc;
1689 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1690 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1691 char return_key[MEMCACHED_MAX_KEY];
1692 size_t return_key_length;
1693 char *value;
1694 size_t value_length;
1695 uint32_t flags;
1696 unsigned int count;
1697 unsigned int x;
1698 char insert_data[VALUE_SIZE_BUG5];
1699
1700 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1701 insert_data[x]= rand();
1702
1703 memcached_flush(memc, 0);
1704 value= memcached_get(memc, keys[0], key_length[0],
1705 &value_length, &flags, &rc);
1706 assert(value == NULL);
1707 assert(rc == MEMCACHED_NOTFOUND);
1708 rc= memcached_mget(memc, keys, key_length, 4);
1709 assert(rc == MEMCACHED_SUCCESS);
1710
1711 count= 0;
1712 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1713 &value_length, &flags, &rc)))
1714 count++;
1715 assert(count == 0);
1716 assert(rc == MEMCACHED_END);
1717
1718 for (x= 0; x < 4; x++)
1719 {
1720 rc= memcached_set(memc, keys[x], key_length[x],
1721 insert_data, VALUE_SIZE_BUG5,
1722 (time_t)0, (uint32_t)0);
1723 assert(rc == MEMCACHED_SUCCESS);
1724 }
1725
1726 for (x= 0; x < 2; x++)
1727 {
1728 value= memcached_get(memc, keys[0], key_length[0],
1729 &value_length, &flags, &rc);
1730 assert(value);
1731 free(value);
1732
1733 rc= memcached_mget(memc, keys, key_length, 4);
1734 assert(rc == MEMCACHED_SUCCESS);
1735 count= 3;
1736 /* We test for purge of partial complete fetches */
1737 for (count= 3; count; count--)
1738 {
1739 value= memcached_fetch(memc, return_key, &return_key_length,
1740 &value_length, &flags, &rc);
1741 assert(rc == MEMCACHED_SUCCESS);
1742 assert(!(memcmp(value, insert_data, value_length)));
1743 assert(value_length);
1744 free(value);
1745 }
1746 }
1747
1748 return 0;
1749 }
1750
1751 static test_return user_supplied_bug8(memcached_st *memc __attribute__((unused)))
1752 {
1753 memcached_return rc;
1754 memcached_st *mine;
1755 memcached_st *clone;
1756
1757 memcached_server_st *servers;
1758 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";
1759
1760 servers= memcached_servers_parse(server_list);
1761 assert(servers);
1762
1763 mine= memcached_create(NULL);
1764 rc= memcached_server_push(mine, servers);
1765 assert(rc == MEMCACHED_SUCCESS);
1766 memcached_server_list_free(servers);
1767
1768 assert(mine);
1769 clone= memcached_clone(NULL, mine);
1770
1771 memcached_quit(mine);
1772 memcached_quit(clone);
1773
1774
1775 memcached_free(mine);
1776 memcached_free(clone);
1777
1778 return 0;
1779 }
1780
1781 /* Test flag store/retrieve */
1782 static test_return user_supplied_bug7(memcached_st *memc)
1783 {
1784 memcached_return rc;
1785 char *keys= "036790384900";
1786 size_t key_length= strlen("036790384900");
1787 char return_key[MEMCACHED_MAX_KEY];
1788 size_t return_key_length;
1789 char *value;
1790 size_t value_length;
1791 uint32_t flags;
1792 unsigned int x;
1793 char insert_data[VALUE_SIZE_BUG5];
1794
1795 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1796 insert_data[x]= rand();
1797
1798 memcached_flush(memc, 0);
1799
1800 flags= 245;
1801 rc= memcached_set(memc, keys, key_length,
1802 insert_data, VALUE_SIZE_BUG5,
1803 (time_t)0, flags);
1804 assert(rc == MEMCACHED_SUCCESS);
1805
1806 flags= 0;
1807 value= memcached_get(memc, keys, key_length,
1808 &value_length, &flags, &rc);
1809 assert(flags == 245);
1810 assert(value);
1811 free(value);
1812
1813 rc= memcached_mget(memc, &keys, &key_length, 1);
1814
1815 flags= 0;
1816 value= memcached_fetch(memc, return_key, &return_key_length,
1817 &value_length, &flags, &rc);
1818 assert(flags == 245);
1819 assert(value);
1820 free(value);
1821
1822
1823 return 0;
1824 }
1825
1826 static test_return user_supplied_bug9(memcached_st *memc)
1827 {
1828 memcached_return rc;
1829 char *keys[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
1830 size_t key_length[3];
1831 unsigned int x;
1832 uint32_t flags;
1833 unsigned count= 0;
1834
1835 char return_key[MEMCACHED_MAX_KEY];
1836 size_t return_key_length;
1837 char *return_value;
1838 size_t return_value_length;
1839
1840
1841 key_length[0]= strlen("UDATA:edevil@sapo.pt");
1842 key_length[1]= strlen("fudge&*@#");
1843 key_length[2]= strlen("for^#@&$not");
1844
1845
1846 for (x= 0; x < 3; x++)
1847 {
1848 rc= memcached_set(memc, keys[x], key_length[x],
1849 keys[x], key_length[x],
1850 (time_t)50, (uint32_t)9);
1851 assert(rc == MEMCACHED_SUCCESS);
1852 }
1853
1854 rc= memcached_mget(memc, keys, key_length, 3);
1855 assert(rc == MEMCACHED_SUCCESS);
1856
1857 /* We need to empty the server before continueing test */
1858 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1859 &return_value_length, &flags, &rc)) != NULL)
1860 {
1861 assert(return_value);
1862 free(return_value);
1863 count++;
1864 }
1865 assert(count == 3);
1866
1867 return 0;
1868 }
1869
1870 /* We are testing with aggressive timeout to get failures */
1871 static test_return user_supplied_bug10(memcached_st *memc)
1872 {
1873 char *key= "foo";
1874 char *value;
1875 size_t value_length= 512;
1876 unsigned int x;
1877 int key_len= 3;
1878 memcached_return rc;
1879 unsigned int set= 1;
1880 memcached_st *mclone= memcached_clone(NULL, memc);
1881 int32_t timeout;
1882
1883 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1884 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1885 timeout= 2;
1886 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
1887
1888 value = (char*)malloc(value_length * sizeof(char));
1889
1890 for (x= 0; x < value_length; x++)
1891 value[x]= (char) (x % 127);
1892
1893 for (x= 1; x <= 100000; ++x)
1894 {
1895 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
1896
1897 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_BUFFERED);
1898
1899 if (rc == MEMCACHED_WRITE_FAILURE)
1900 x--;
1901 }
1902
1903 free(value);
1904 memcached_free(mclone);
1905
1906 return 0;
1907 }
1908
1909 /*
1910 We are looking failures in the async protocol
1911 */
1912 static test_return user_supplied_bug11(memcached_st *memc)
1913 {
1914 char *key= "foo";
1915 char *value;
1916 size_t value_length= 512;
1917 unsigned int x;
1918 int key_len= 3;
1919 memcached_return rc;
1920 unsigned int set= 1;
1921 int32_t timeout;
1922 memcached_st *mclone= memcached_clone(NULL, memc);
1923
1924 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1925 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1926 timeout= -1;
1927 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
1928
1929 timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
1930
1931 assert(timeout == -1);
1932
1933 value = (char*)malloc(value_length * sizeof(char));
1934
1935 for (x= 0; x < value_length; x++)
1936 value[x]= (char) (x % 127);
1937
1938 for (x= 1; x <= 100000; ++x)
1939 {
1940 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
1941 }
1942
1943 free(value);
1944 memcached_free(mclone);
1945
1946 return 0;
1947 }
1948
1949 /*
1950 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
1951 */
1952 static test_return user_supplied_bug12(memcached_st *memc)
1953 {
1954 memcached_return rc;
1955 uint32_t flags;
1956 size_t value_length;
1957 char *value;
1958 uint64_t number_value;
1959
1960 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
1961 &value_length, &flags, &rc);
1962 assert(value == NULL);
1963 assert(rc == MEMCACHED_NOTFOUND);
1964
1965 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
1966 1, &number_value);
1967
1968 assert(value == NULL);
1969 /* The binary protocol will set the key if it doesn't exist */
1970 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1)
1971 assert(rc == MEMCACHED_SUCCESS);
1972 else
1973 assert(rc == MEMCACHED_NOTFOUND);
1974
1975 rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
1976
1977 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
1978 &value_length, &flags, &rc);
1979 assert(value);
1980 assert(rc == MEMCACHED_SUCCESS);
1981 free(value);
1982
1983 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
1984 1, &number_value);
1985 assert(number_value == 2);
1986 assert(rc == MEMCACHED_SUCCESS);
1987
1988 return 0;
1989 }
1990
1991 /*
1992 Bug found where command total one more than MEMCACHED_MAX_BUFFER
1993 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
1994 */
1995 static test_return user_supplied_bug13(memcached_st *memc)
1996 {
1997 char key[] = "key34567890";
1998 char *overflow;
1999 memcached_return rc;
2000 size_t overflowSize;
2001
2002 char commandFirst[]= "set key34567890 0 0 ";
2003 char commandLast[] = " \r\n"; /* first line of command sent to server */
2004 size_t commandLength;
2005 size_t testSize;
2006
2007 commandLength = strlen(commandFirst) + strlen(commandLast) + 4; /* 4 is number of characters in size, probably 8196 */
2008
2009 overflowSize = MEMCACHED_MAX_BUFFER - commandLength;
2010
2011 for (testSize= overflowSize - 1; testSize < overflowSize + 1; testSize++)
2012 {
2013 overflow= malloc(testSize);
2014 assert(overflow != NULL);
2015
2016 memset(overflow, 'x', testSize);
2017 rc= memcached_set(memc, key, strlen(key),
2018 overflow, testSize, 0, 0);
2019 assert(rc == MEMCACHED_SUCCESS);
2020 free(overflow);
2021 }
2022
2023 return 0;
2024 }
2025
2026
2027 /*
2028 Test values of many different sizes
2029 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2030 set key34567890 0 0 8169 \r\n
2031 is sent followed by buffer of size 8169, followed by 8169
2032 */
2033 static test_return user_supplied_bug14(memcached_st *memc)
2034 {
2035 int setter= 1;
2036 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2037 memcached_return rc;
2038 char *key= "foo";
2039 char *value;
2040 size_t value_length= 18000;
2041 char *string;
2042 size_t string_length;
2043 uint32_t flags;
2044 unsigned int x;
2045 size_t current_length;
2046
2047 value = (char*)malloc(value_length);
2048 assert(value);
2049
2050 for (x= 0; x < value_length; x++)
2051 value[x] = (char) (x % 127);
2052
2053 for (current_length= 0; current_length < value_length; current_length++)
2054 {
2055 rc= memcached_set(memc, key, strlen(key),
2056 value, current_length,
2057 (time_t)0, (uint32_t)0);
2058 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
2059
2060 string= memcached_get(memc, key, strlen(key),
2061 &string_length, &flags, &rc);
2062
2063 assert(rc == MEMCACHED_SUCCESS);
2064 assert(string_length == current_length);
2065 assert(!memcmp(string, value, string_length));
2066
2067 free(string);
2068 }
2069
2070 free(value);
2071
2072 return 0;
2073 }
2074
2075 /*
2076 Look for zero length value problems
2077 */
2078 static test_return user_supplied_bug15(memcached_st *memc)
2079 {
2080 uint32_t x;
2081 memcached_return rc;
2082 char *key= "mykey";
2083 char *value;
2084 size_t length;
2085 uint32_t flags;
2086
2087 for (x= 0; x < 2; x++)
2088 {
2089 rc= memcached_set(memc, key, strlen(key),
2090 NULL, 0,
2091 (time_t)0, (uint32_t)0);
2092
2093 assert(rc == MEMCACHED_SUCCESS);
2094
2095 value= memcached_get(memc, key, strlen(key),
2096 &length, &flags, &rc);
2097
2098 assert(rc == MEMCACHED_SUCCESS);
2099 assert(value == NULL);
2100 assert(length == 0);
2101 assert(flags == 0);
2102
2103 value= memcached_get(memc, key, strlen(key),
2104 &length, &flags, &rc);
2105
2106 assert(rc == MEMCACHED_SUCCESS);
2107 assert(value == NULL);
2108 assert(length == 0);
2109 assert(flags == 0);
2110 }
2111
2112 return 0;
2113 }
2114
2115 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2116 static test_return user_supplied_bug16(memcached_st *memc)
2117 {
2118 memcached_return rc;
2119 char *key= "mykey";
2120 char *value;
2121 size_t length;
2122 uint32_t flags;
2123
2124 rc= memcached_set(memc, key, strlen(key),
2125 NULL, 0,
2126 (time_t)0, UINT32_MAX);
2127
2128 assert(rc == MEMCACHED_SUCCESS);
2129
2130 value= memcached_get(memc, key, strlen(key),
2131 &length, &flags, &rc);
2132
2133 assert(rc == MEMCACHED_SUCCESS);
2134 assert(value == NULL);
2135 assert(length == 0);
2136 assert(flags == UINT32_MAX);
2137
2138 return 0;
2139 }
2140
2141 /* Check the validity of chinese key*/
2142 static test_return user_supplied_bug17(memcached_st *memc)
2143 {
2144 memcached_return rc;
2145 char *key= "豆瓣";
2146 char *value="我们在炎热抑郁的夏天无法停止豆瓣";
2147 char *value2;
2148 size_t length;
2149 uint32_t flags;
2150
2151 rc= memcached_set(memc, key, strlen(key),
2152 value, strlen(value),
2153 (time_t)0, 0);
2154
2155 assert(rc == MEMCACHED_SUCCESS);
2156
2157 value2= memcached_get(memc, key, strlen(key),
2158 &length, &flags, &rc);
2159
2160 assert(length==strlen(value));
2161 assert(rc == MEMCACHED_SUCCESS);
2162 assert(memcmp(value, value2, length)==0);
2163 free(value2);
2164
2165 return 0;
2166 }
2167
2168 /*
2169 From Andrei on IRC
2170 */
2171
2172 test_return user_supplied_bug19(memcached_st *memc)
2173 {
2174 memcached_st *m;
2175 memcached_server_st *s;
2176 memcached_return res;
2177
2178 (void)memc;
2179
2180 m= memcached_create(NULL);
2181 memcached_server_add_with_weight(m, "localhost", 11311, 100);
2182 memcached_server_add_with_weight(m, "localhost", 11312, 100);
2183
2184 s= memcached_server_by_key(m, "a", 1, &res);
2185 memcached_server_free(s);
2186
2187 memcached_free(m);
2188
2189 return 0;
2190 }
2191
2192 /* CAS test from Andei */
2193 test_return user_supplied_bug20(memcached_st *memc)
2194 {
2195 memcached_return status;
2196 memcached_result_st *result, result_obj;
2197 char *key = "abc";
2198 size_t key_len = strlen("abc");
2199 char *value = "foobar";
2200 size_t value_len = strlen(value);
2201
2202 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
2203
2204 status = memcached_set(memc, key, key_len, value, value_len, (time_t)0, (uint32_t)0);
2205 assert(status == MEMCACHED_SUCCESS);
2206
2207 status = memcached_mget(memc, &key, &key_len, 1);
2208 assert(status == MEMCACHED_SUCCESS);
2209
2210 result= memcached_result_create(memc, &result_obj);
2211 assert(result);
2212
2213 memcached_result_create(memc, &result_obj);
2214 result= memcached_fetch_result(memc, &result_obj, &status);
2215
2216 assert(result);
2217 assert(status == MEMCACHED_SUCCESS);
2218
2219 memcached_result_free(result);
2220
2221 return 0;
2222 }
2223
2224 #include "ketama_test_cases.h"
2225 test_return user_supplied_bug18(memcached_st *trash)
2226 {
2227 memcached_return rc;
2228 int value;
2229 int i;
2230 memcached_server_st *server_pool;
2231 memcached_st *memc;
2232
2233 (void)trash;
2234
2235 memc= memcached_create(NULL);
2236 assert(memc);
2237
2238 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2239 assert(rc == MEMCACHED_SUCCESS);
2240
2241 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2242 assert(value == 1);
2243
2244 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2245 assert(rc == MEMCACHED_SUCCESS);
2246
2247 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2248 assert(value == MEMCACHED_HASH_MD5);
2249
2250 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");
2251 memcached_server_push(memc, server_pool);
2252
2253 /* verify that the server list was parsed okay. */
2254 assert(memc->number_of_hosts == 8);
2255 assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
2256 assert(server_pool[0].port == 11211);
2257 assert(server_pool[0].weight == 600);
2258 assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
2259 assert(server_pool[2].port == 11211);
2260 assert(server_pool[2].weight == 200);
2261 assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
2262 assert(server_pool[7].port == 11211);
2263 assert(server_pool[7].weight == 100);
2264
2265 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2266 * us test the boundary wraparound.
2267 */
2268 assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
2269
2270 /* verify the standard ketama set. */
2271 for (i= 0; i < 99; i++)
2272 {
2273 uint32_t server_idx = memcached_generate_hash(memc, test_cases[i].key, strlen(test_cases[i].key));
2274 char *hostname = memc->hosts[server_idx].hostname;
2275 assert(strcmp(hostname, test_cases[i].server) == 0);
2276 }
2277
2278 memcached_server_list_free(server_pool);
2279 memcached_free(memc);
2280
2281 return 0;
2282 }
2283
2284 static test_return result_static(memcached_st *memc)
2285 {
2286 memcached_result_st result;
2287 memcached_result_st *result_ptr;
2288
2289 result_ptr= memcached_result_create(memc, &result);
2290 assert(result.is_allocated == false);
2291 assert(result_ptr);
2292 memcached_result_free(&result);
2293
2294 return 0;
2295 }
2296
2297 static test_return result_alloc(memcached_st *memc)
2298 {
2299 memcached_result_st *result;
2300
2301 result= memcached_result_create(memc, NULL);
2302 assert(result);
2303 memcached_result_free(result);
2304
2305 return 0;
2306 }
2307
2308 static test_return string_static_null(memcached_st *memc)
2309 {
2310 memcached_string_st string;
2311 memcached_string_st *string_ptr;
2312
2313 string_ptr= memcached_string_create(memc, &string, 0);
2314 assert(string.is_allocated == false);
2315 assert(string_ptr);
2316 memcached_string_free(&string);
2317
2318 return 0;
2319 }
2320
2321 static test_return string_alloc_null(memcached_st *memc)
2322 {
2323 memcached_string_st *string;
2324
2325 string= memcached_string_create(memc, NULL, 0);
2326 assert(string);
2327 memcached_string_free(string);
2328
2329 return 0;
2330 }
2331
2332 static test_return string_alloc_with_size(memcached_st *memc)
2333 {
2334 memcached_string_st *string;
2335
2336 string= memcached_string_create(memc, NULL, 1024);
2337 assert(string);
2338 memcached_string_free(string);
2339
2340 return 0;
2341 }
2342
2343 static test_return string_alloc_with_size_toobig(memcached_st *memc)
2344 {
2345 memcached_string_st *string;
2346
2347 string= memcached_string_create(memc, NULL, INT64_MAX);
2348 assert(string == NULL);
2349
2350 return 0;
2351 }
2352
2353 static test_return string_alloc_append(memcached_st *memc)
2354 {
2355 unsigned int x;
2356 char buffer[SMALL_STRING_LEN];
2357 memcached_string_st *string;
2358
2359 /* Ring the bell! */
2360 memset(buffer, 6, SMALL_STRING_LEN);
2361
2362 string= memcached_string_create(memc, NULL, 100);
2363 assert(string);
2364
2365 for (x= 0; x < 1024; x++)
2366 {
2367 memcached_return rc;
2368 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2369 assert(rc == MEMCACHED_SUCCESS);
2370 }
2371 memcached_string_free(string);
2372
2373 return 0;
2374 }
2375
2376 static test_return string_alloc_append_toobig(memcached_st *memc)
2377 {
2378 memcached_return rc;
2379 unsigned int x;
2380 char buffer[SMALL_STRING_LEN];
2381 memcached_string_st *string;
2382
2383 /* Ring the bell! */
2384 memset(buffer, 6, SMALL_STRING_LEN);
2385
2386 string= memcached_string_create(memc, NULL, 100);
2387 assert(string);
2388
2389 for (x= 0; x < 1024; x++)
2390 {
2391 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2392 assert(rc == MEMCACHED_SUCCESS);
2393 }
2394 rc= memcached_string_append(string, buffer, INT64_MAX);
2395 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
2396 memcached_string_free(string);
2397
2398 return 0;
2399 }
2400
2401 static test_return cleanup_pairs(memcached_st *memc __attribute__((unused)))
2402 {
2403 pairs_free(global_pairs);
2404
2405 return 0;
2406 }
2407
2408 static test_return generate_pairs(memcached_st *memc __attribute__((unused)))
2409 {
2410 unsigned long long x;
2411 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
2412 global_count= GLOBAL_COUNT;
2413
2414 for (x= 0; x < global_count; x++)
2415 {
2416 global_keys[x]= global_pairs[x].key;
2417 global_keys_length[x]= global_pairs[x].key_length;
2418 }
2419
2420 return 0;
2421 }
2422
2423 static test_return generate_large_pairs(memcached_st *memc __attribute__((unused)))
2424 {
2425 unsigned long long x;
2426 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
2427 global_count= GLOBAL2_COUNT;
2428
2429 for (x= 0; x < global_count; x++)
2430 {
2431 global_keys[x]= global_pairs[x].key;
2432 global_keys_length[x]= global_pairs[x].key_length;
2433 }
2434
2435 return 0;
2436 }
2437
2438 static test_return generate_data(memcached_st *memc)
2439 {
2440 execute_set(memc, global_pairs, global_count);
2441
2442 return 0;
2443 }
2444
2445 static test_return generate_data_with_stats(memcached_st *memc)
2446 {
2447 memcached_stat_st *stat_p;
2448 memcached_return rc;
2449 uint32_t host_index= 0;
2450 execute_set(memc, global_pairs, global_count);
2451
2452 //TODO: hosts used size stats
2453 stat_p= memcached_stat(memc, NULL, &rc);
2454 assert(stat_p);
2455
2456 for (host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
2457 {
2458 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);
2459 }
2460
2461 memcached_stat_free(NULL, stat_p);
2462
2463 return 0;
2464 }
2465 static test_return generate_buffer_data(memcached_st *memc)
2466 {
2467 int latch= 0;
2468
2469 latch= 1;
2470 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2471 generate_data(memc);
2472
2473 return 0;
2474 }
2475
2476 static test_return get_read_count(memcached_st *memc)
2477 {
2478 unsigned int x;
2479 memcached_return rc;
2480 memcached_st *clone;
2481
2482 clone= memcached_clone(NULL, memc);
2483 assert(clone);
2484
2485 memcached_server_add_with_weight(clone, "localhost", 6666, 0);
2486
2487 {
2488 char *return_value;
2489 size_t return_value_length;
2490 uint32_t flags;
2491 uint32_t count;
2492
2493 for (x= count= 0; x < global_count; x++)
2494 {
2495 return_value= memcached_get(clone, global_keys[x], global_keys_length[x],
2496 &return_value_length, &flags, &rc);
2497 if (rc == MEMCACHED_SUCCESS)
2498 {
2499 count++;
2500 if (return_value)
2501 free(return_value);
2502 }
2503 }
2504 fprintf(stderr, "\t%u -> %u", global_count, count);
2505 }
2506
2507 memcached_free(clone);
2508
2509 return 0;
2510 }
2511
2512 static test_return get_read(memcached_st *memc)
2513 {
2514 unsigned int x;
2515 memcached_return rc;
2516
2517 {
2518 char *return_value;
2519 size_t return_value_length;
2520 uint32_t flags;
2521
2522 for (x= 0; x < global_count; x++)
2523 {
2524 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
2525 &return_value_length, &flags, &rc);
2526 /*
2527 assert(return_value);
2528 assert(rc == MEMCACHED_SUCCESS);
2529 */
2530 if (rc == MEMCACHED_SUCCESS && return_value)
2531 free(return_value);
2532 }
2533 }
2534
2535 return 0;
2536 }
2537
2538 static test_return mget_read(memcached_st *memc)
2539 {
2540 memcached_return rc;
2541
2542 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2543 assert(rc == MEMCACHED_SUCCESS);
2544 /* Turn this into a help function */
2545 {
2546 char return_key[MEMCACHED_MAX_KEY];
2547 size_t return_key_length;
2548 char *return_value;
2549 size_t return_value_length;
2550 uint32_t flags;
2551
2552 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2553 &return_value_length, &flags, &rc)))
2554 {
2555 assert(return_value);
2556 assert(rc == MEMCACHED_SUCCESS);
2557 free(return_value);
2558 }
2559 }
2560
2561 return 0;
2562 }
2563
2564 static test_return mget_read_result(memcached_st *memc)
2565 {
2566 memcached_return rc;
2567
2568 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2569 assert(rc == MEMCACHED_SUCCESS);
2570 /* Turn this into a help function */
2571 {
2572 memcached_result_st results_obj;
2573 memcached_result_st *results;
2574
2575 results= memcached_result_create(memc, &results_obj);
2576
2577 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
2578 {
2579 assert(results);
2580 assert(rc == MEMCACHED_SUCCESS);
2581 }
2582
2583 memcached_result_free(&results_obj);
2584 }
2585
2586 return 0;
2587 }
2588
2589 static test_return mget_read_function(memcached_st *memc)
2590 {
2591 memcached_return rc;
2592 unsigned int counter;
2593 unsigned int (*callbacks[1])(memcached_st *, memcached_result_st *, void *);
2594
2595 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2596 assert(rc == MEMCACHED_SUCCESS);
2597
2598 callbacks[0]= &callback_counter;
2599 counter= 0;
2600 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
2601
2602 return 0;
2603 }
2604
2605 static test_return delete_generate(memcached_st *memc)
2606 {
2607 unsigned int x;
2608
2609 for (x= 0; x < global_count; x++)
2610 {
2611 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2612 }
2613
2614 return 0;
2615 }
2616
2617 static test_return delete_buffer_generate(memcached_st *memc)
2618 {
2619 int latch= 0;
2620 unsigned int x;
2621
2622 latch= 1;
2623 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2624
2625 for (x= 0; x < global_count; x++)
2626 {
2627 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2628 }
2629
2630 return 0;
2631 }
2632
2633 static test_return free_data(memcached_st *memc __attribute__((unused)))
2634 {
2635 pairs_free(global_pairs);
2636
2637 return 0;
2638 }
2639
2640 static test_return add_host_test1(memcached_st *memc)
2641 {
2642 unsigned int x;
2643 memcached_return rc;
2644 char servername[]= "0.example.com";
2645 memcached_server_st *servers;
2646
2647 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
2648 assert(servers);
2649 assert(1 == memcached_server_list_count(servers));
2650
2651 for (x= 2; x < 20; x++)
2652 {
2653 char buffer[SMALL_STRING_LEN];
2654
2655 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
2656 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
2657 &rc);
2658 assert(rc == MEMCACHED_SUCCESS);
2659 assert(x == memcached_server_list_count(servers));
2660 }
2661
2662 rc= memcached_server_push(memc, servers);
2663 assert(rc == MEMCACHED_SUCCESS);
2664 rc= memcached_server_push(memc, servers);
2665 assert(rc == MEMCACHED_SUCCESS);
2666
2667 memcached_server_list_free(servers);
2668
2669 return 0;
2670 }
2671
2672 static memcached_return pre_nonblock(memcached_st *memc)
2673 {
2674 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2675
2676 return MEMCACHED_SUCCESS;
2677 }
2678
2679 static memcached_return pre_nonblock_binary(memcached_st *memc)
2680 {
2681 memcached_return rc= MEMCACHED_FAILURE;
2682 memcached_st *clone;
2683
2684 clone= memcached_clone(NULL, memc);
2685 assert(clone);
2686 // The memcached_version needs to be done on a clone, because the server
2687 // will not toggle protocol on an connection.
2688 memcached_version(clone);
2689
2690 if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2)
2691 {
2692 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2693 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
2694 assert(rc == MEMCACHED_SUCCESS);
2695 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
2696 }
2697
2698 memcached_free(clone);
2699 return rc;
2700 }
2701
2702 static memcached_return pre_murmur(memcached_st *memc)
2703 {
2704 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
2705
2706 return MEMCACHED_SUCCESS;
2707 }
2708
2709 static memcached_return pre_jenkins(memcached_st *memc)
2710 {
2711 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
2712
2713 return MEMCACHED_SUCCESS;
2714 }
2715
2716
2717 static memcached_return pre_md5(memcached_st *memc)
2718 {
2719 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
2720
2721 return MEMCACHED_SUCCESS;
2722 }
2723
2724 static memcached_return pre_crc(memcached_st *memc)
2725 {
2726 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
2727
2728 return MEMCACHED_SUCCESS;
2729 }
2730
2731 static memcached_return pre_hsieh(memcached_st *memc)
2732 {
2733 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
2734
2735 return MEMCACHED_SUCCESS;
2736 }
2737
2738 static memcached_return pre_hash_fnv1_64(memcached_st *memc)
2739 {
2740 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
2741
2742 return MEMCACHED_SUCCESS;
2743 }
2744
2745 static memcached_return pre_hash_fnv1a_64(memcached_st *memc)
2746 {
2747 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
2748
2749 return MEMCACHED_SUCCESS;
2750 }
2751
2752 static memcached_return pre_hash_fnv1_32(memcached_st *memc)
2753 {
2754 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
2755
2756 return MEMCACHED_SUCCESS;
2757 }
2758
2759 static memcached_return pre_hash_fnv1a_32(memcached_st *memc)
2760 {
2761 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
2762
2763 return MEMCACHED_SUCCESS;
2764 }
2765
2766 static memcached_return pre_behavior_ketama(memcached_st *memc)
2767 {
2768 memcached_return rc;
2769 uint64_t value;
2770
2771 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
2772 assert(rc == MEMCACHED_SUCCESS);
2773
2774 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
2775 assert(value == 1);
2776
2777 return MEMCACHED_SUCCESS;
2778 }
2779
2780 static memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
2781 {
2782 memcached_return rc;
2783 uint64_t value;
2784
2785 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2786 assert(rc == MEMCACHED_SUCCESS);
2787
2788 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2789 assert(value == 1);
2790
2791 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2792 assert(rc == MEMCACHED_SUCCESS);
2793
2794 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2795 assert(value == MEMCACHED_HASH_MD5);
2796 return MEMCACHED_SUCCESS;
2797 }
2798
2799 static memcached_return pre_binary(memcached_st *memc)
2800 {
2801 memcached_return rc= MEMCACHED_FAILURE;
2802 memcached_st *clone;
2803
2804 clone= memcached_clone(NULL, memc);
2805 assert(clone);
2806 // The memcached_version needs to be done on a clone, because the server
2807 // will not toggle protocol on an connection.
2808 memcached_version(clone);
2809
2810 if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2)
2811 {
2812 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
2813 assert(rc == MEMCACHED_SUCCESS);
2814 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
2815 }
2816
2817 memcached_free(clone);
2818 return rc;
2819 }
2820
2821 static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
2822 {
2823 free(mem);
2824 }
2825
2826 static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
2827 {
2828 return malloc(size);
2829 }
2830
2831 static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
2832 {
2833 return realloc(mem, size);
2834 }
2835
2836 static memcached_return set_prefix(memcached_st *memc)
2837 {
2838 memcached_return rc;
2839 const char *key= "mine";
2840 char *value;
2841
2842 /* Make sure be default none exists */
2843 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2844 assert(rc == MEMCACHED_FAILURE);
2845
2846 /* Test a clean set */
2847 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
2848 assert(rc == MEMCACHED_SUCCESS);
2849
2850 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2851 assert(memcmp(value, key, 4) == 0);
2852 assert(rc == MEMCACHED_SUCCESS);
2853
2854 /* Test that we can turn it off */
2855 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
2856 assert(rc == MEMCACHED_SUCCESS);
2857
2858 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2859 assert(rc == MEMCACHED_FAILURE);
2860
2861 /* Now setup for main test */
2862 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
2863 assert(rc == MEMCACHED_SUCCESS);
2864
2865 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2866 assert(rc == MEMCACHED_SUCCESS);
2867 assert(memcmp(value, key, 4) == 0);
2868
2869 /* Set to Zero, and then Set to something too large */
2870 {
2871 char *long_key;
2872 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
2873 assert(rc == MEMCACHED_SUCCESS);
2874
2875 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
2876 assert(rc == MEMCACHED_FAILURE);
2877 assert(value == NULL);
2878
2879 /* Test a long key for failure */
2880 /* TODO, extend test to determine based on setting, what result should be */
2881 long_key= "Thisismorethentheallottednumberofcharacters";
2882 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
2883 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
2884 assert(rc == MEMCACHED_SUCCESS);
2885
2886 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
2887 long_key= "This is more then the allotted number of characters";
2888 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
2889 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
2890
2891 /* Test for a bad prefix, but with a short key */
2892 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
2893 assert(rc == MEMCACHED_SUCCESS);
2894
2895 long_key= "dog cat";
2896 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
2897 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
2898 }
2899
2900 return MEMCACHED_SUCCESS;
2901 }
2902
2903 static memcached_return set_memory_alloc(memcached_st *memc)
2904 {
2905 {
2906 memcached_malloc_function test_ptr;
2907 memcached_return rc;
2908
2909 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &my_malloc);
2910 assert(rc == MEMCACHED_SUCCESS);
2911 test_ptr= (memcached_malloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
2912 assert(rc == MEMCACHED_SUCCESS);
2913 assert(test_ptr == my_malloc);
2914 }
2915
2916 {
2917 memcached_realloc_function test_ptr;
2918 memcached_return rc;
2919
2920 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &my_realloc);
2921 assert(rc == MEMCACHED_SUCCESS);
2922 test_ptr= (memcached_realloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
2923 assert(rc == MEMCACHED_SUCCESS);
2924 assert(test_ptr == my_realloc);
2925 }
2926
2927 {
2928 memcached_free_function test_ptr;
2929 memcached_return rc;
2930
2931 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, my_free);
2932 assert(rc == MEMCACHED_SUCCESS);
2933 test_ptr= (memcached_free_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
2934 assert(rc == MEMCACHED_SUCCESS);
2935 assert(test_ptr == my_free);
2936 }
2937
2938 return MEMCACHED_SUCCESS;
2939 }
2940
2941 static memcached_return enable_consistent(memcached_st *memc)
2942 {
2943 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
2944 memcached_hash hash;
2945 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
2946 pre_hsieh(memc);
2947
2948 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
2949 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
2950
2951 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
2952 assert(hash == MEMCACHED_HASH_HSIEH);
2953
2954
2955 return MEMCACHED_SUCCESS;
2956 }
2957
2958 static memcached_return enable_cas(memcached_st *memc)
2959 {
2960 unsigned int set= 1;
2961
2962 memcached_version(memc);
2963
2964 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
2965 || memc->hosts[0].minor_version > 2)
2966 {
2967 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
2968
2969 return MEMCACHED_SUCCESS;
2970 }
2971
2972 return MEMCACHED_FAILURE;
2973 }
2974
2975 static memcached_return check_for_1_2_3(memcached_st *memc)
2976 {
2977 memcached_version(memc);
2978
2979 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
2980 || memc->hosts[0].minor_version > 2)
2981 return MEMCACHED_SUCCESS;
2982
2983 return MEMCACHED_FAILURE;
2984 }
2985
2986 static memcached_return pre_unix_socket(memcached_st *memc)
2987 {
2988 memcached_return rc;
2989 struct stat buf;
2990
2991 memcached_server_list_free(memc->hosts);
2992 memc->hosts= NULL;
2993 memc->number_of_hosts= 0;
2994
2995 if (stat("/tmp/memcached.socket", &buf))
2996 return MEMCACHED_FAILURE;
2997
2998 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
2999
3000 return rc;
3001 }
3002
3003 static memcached_return pre_nodelay(memcached_st *memc)
3004 {
3005 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3006 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
3007
3008 return MEMCACHED_SUCCESS;
3009 }
3010
3011 static memcached_return pre_settimer(memcached_st *memc)
3012 {
3013 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
3014 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
3015
3016 return MEMCACHED_SUCCESS;
3017 }
3018
3019 static memcached_return poll_timeout(memcached_st *memc)
3020 {
3021 int32_t timeout;
3022
3023 timeout= 100;
3024
3025 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
3026
3027 timeout= (int32_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
3028
3029 assert(timeout == 100);
3030
3031 return MEMCACHED_SUCCESS;
3032 }
3033
3034
3035 /* Clean the server before beginning testing */
3036 test_st tests[] ={
3037 {"flush", 0, flush_test },
3038 {"init", 0, init_test },
3039 {"allocation", 0, allocation_test },
3040 {"server_list_null_test", 0, server_list_null_test},
3041 {"server_unsort", 0, server_unsort_test},
3042 {"server_sort", 0, server_sort_test},
3043 {"server_sort2", 0, server_sort2_test},
3044 {"clone_test", 0, clone_test },
3045 {"error", 0, error_test },
3046 {"set", 0, set_test },
3047 {"set2", 0, set_test2 },
3048 {"set3", 0, set_test3 },
3049 {"add", 1, add_test },
3050 {"replace", 1, replace_test },
3051 {"delete", 1, delete_test },
3052 {"get", 1, get_test },
3053 {"get2", 0, get_test2 },
3054 {"get3", 0, get_test3 },
3055 {"get4", 0, get_test4 },
3056 {"stats_servername", 0, stats_servername_test },
3057 {"increment", 0, increment_test },
3058 {"decrement", 0, decrement_test },
3059 {"quit", 0, quit_test },
3060 {"mget", 1, mget_test },
3061 {"mget_result", 1, mget_result_test },
3062 {"mget_result_alloc", 1, mget_result_alloc_test },
3063 {"mget_result_function", 1, mget_result_function },
3064 {"get_stats", 0, get_stats },
3065 {"add_host_test", 0, add_host_test },
3066 {"add_host_test_1", 0, add_host_test1 },
3067 {"get_stats_keys", 0, get_stats_keys },
3068 {"behavior_test", 0, get_stats_keys },
3069 {"callback_test", 0, get_stats_keys },
3070 {"version_string_test", 0, version_string_test},
3071 {"bad_key", 1, bad_key_test },
3072 {"memcached_server_cursor", 1, memcached_server_cursor_test },
3073 {"read_through", 1, read_through },
3074 {"delete_through", 1, delete_through },
3075 {0, 0, 0}
3076 };
3077
3078 test_st async_tests[] ={
3079 {"add", 1, add_wrapper },
3080 {0, 0, 0}
3081 };
3082
3083 test_st string_tests[] ={
3084 {"string static with null", 0, string_static_null },
3085 {"string alloc with null", 0, string_alloc_null },
3086 {"string alloc with 1K", 0, string_alloc_with_size },
3087 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
3088 {"string append", 0, string_alloc_append },
3089 {"string append failure (too big)", 0, string_alloc_append_toobig },
3090 {0, 0, 0}
3091 };
3092
3093 test_st result_tests[] ={
3094 {"result static", 0, result_static},
3095 {"result alloc", 0, result_alloc},
3096 {0, 0, 0}
3097 };
3098
3099 test_st version_1_2_3[] ={
3100 {"append", 0, append_test },
3101 {"prepend", 0, prepend_test },
3102 {"cas", 0, cas_test },
3103 {"cas2", 0, cas2_test },
3104 {"append_binary", 0, append_binary_test },
3105 {0, 0, 0}
3106 };
3107
3108 test_st user_tests[] ={
3109 {"user_supplied_bug1", 0, user_supplied_bug1 },
3110 {"user_supplied_bug2", 0, user_supplied_bug2 },
3111 {"user_supplied_bug3", 0, user_supplied_bug3 },
3112 {"user_supplied_bug4", 0, user_supplied_bug4 },
3113 {"user_supplied_bug5", 1, user_supplied_bug5 },
3114 {"user_supplied_bug6", 1, user_supplied_bug6 },
3115 {"user_supplied_bug7", 1, user_supplied_bug7 },
3116 {"user_supplied_bug8", 1, user_supplied_bug8 },
3117 {"user_supplied_bug9", 1, user_supplied_bug9 },
3118 {"user_supplied_bug10", 1, user_supplied_bug10 },
3119 {"user_supplied_bug11", 1, user_supplied_bug11 },
3120 {"user_supplied_bug12", 1, user_supplied_bug12 },
3121 {"user_supplied_bug13", 1, user_supplied_bug13 },
3122 {"user_supplied_bug14", 1, user_supplied_bug14 },
3123 {"user_supplied_bug15", 1, user_supplied_bug15 },
3124 {"user_supplied_bug16", 1, user_supplied_bug16 },
3125 #ifndef __sun
3126 /*
3127 ** It seems to be something weird with the character sets..
3128 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
3129 ** guess I need to find out how this is supposed to work.. Perhaps I need
3130 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
3131 ** so just disable the code for now...).
3132 */
3133 {"user_supplied_bug17", 1, user_supplied_bug17 },
3134 #endif
3135 {"user_supplied_bug18", 1, user_supplied_bug18 },
3136 {"user_supplied_bug19", 1, user_supplied_bug19 },
3137 {"user_supplied_bug20", 1, user_supplied_bug20 },
3138 {0, 0, 0}
3139 };
3140
3141 test_st generate_tests[] ={
3142 {"generate_pairs", 1, generate_pairs },
3143 {"generate_data", 1, generate_data },
3144 {"get_read", 0, get_read },
3145 {"delete_generate", 0, delete_generate },
3146 {"generate_buffer_data", 1, generate_buffer_data },
3147 {"delete_buffer", 0, delete_buffer_generate},
3148 {"generate_data", 1, generate_data },
3149 {"mget_read", 0, mget_read },
3150 {"mget_read_result", 0, mget_read_result },
3151 {"mget_read_function", 0, mget_read_function },
3152 {"cleanup", 1, cleanup_pairs },
3153 {"generate_large_pairs", 1, generate_large_pairs },
3154 {"generate_data", 1, generate_data },
3155 {"generate_buffer_data", 1, generate_buffer_data },
3156 {"cleanup", 1, cleanup_pairs },
3157 {0, 0, 0}
3158 };
3159
3160 test_st consistent_tests[] ={
3161 {"generate_pairs", 1, generate_pairs },
3162 {"generate_data", 1, generate_data },
3163 {"get_read", 0, get_read_count },
3164 {"cleanup", 1, cleanup_pairs },
3165 {0, 0, 0}
3166 };
3167
3168 test_st consistent_weighted_tests[] ={
3169 {"generate_pairs", 1, generate_pairs },
3170 {"generate_data", 1, generate_data_with_stats },
3171 {"get_read", 0, get_read_count },
3172 {"cleanup", 1, cleanup_pairs },
3173 {0, 0, 0}
3174 };
3175
3176 collection_st collection[] ={
3177 {"block", 0, 0, tests},
3178 {"binary", pre_binary, 0, tests},
3179 {"nonblock", pre_nonblock, 0, tests},
3180 {"nodelay", pre_nodelay, 0, tests},
3181 {"settimer", pre_settimer, 0, tests},
3182 {"md5", pre_md5, 0, tests},
3183 {"crc", pre_crc, 0, tests},
3184 {"hsieh", pre_hsieh, 0, tests},
3185 {"jenkins", pre_jenkins, 0, tests},
3186 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
3187 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
3188 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
3189 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
3190 {"ketama", pre_behavior_ketama, 0, tests},
3191 {"unix_socket", pre_unix_socket, 0, tests},
3192 {"unix_socket_nodelay", pre_nodelay, 0, tests},
3193 {"poll_timeout", poll_timeout, 0, tests},
3194 {"gets", enable_cas, 0, tests},
3195 {"consistent", enable_consistent, 0, tests},
3196 {"memory_allocators", set_memory_alloc, 0, tests},
3197 {"prefix", set_prefix, 0, tests},
3198 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
3199 {"string", 0, 0, string_tests},
3200 {"result", 0, 0, result_tests},
3201 {"async", pre_nonblock, 0, async_tests},
3202 {"async_binary", pre_nonblock_binary, 0, async_tests},
3203 {"user", 0, 0, user_tests},
3204 {"generate", 0, 0, generate_tests},
3205 {"generate_hsieh", pre_hsieh, 0, generate_tests},
3206 {"generate_ketama", pre_behavior_ketama, 0, generate_tests},
3207 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
3208 {"generate_md5", pre_md5, 0, generate_tests},
3209 {"generate_murmur", pre_murmur, 0, generate_tests},
3210 {"generate_jenkins", pre_jenkins, 0, generate_tests},
3211 {"generate_nonblock", pre_nonblock, 0, generate_tests},
3212 {"consistent_not", 0, 0, consistent_tests},
3213 {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests},
3214 {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
3215 {0, 0, 0, 0}
3216 };
3217
3218 #define SERVERS_TO_CREATE 5
3219
3220 /* Prototypes for functions we will pass to test framework */
3221 void *world_create(void);
3222 void world_destroy(void *p);
3223
3224 void *world_create(void)
3225 {
3226 server_startup_st *construct;
3227
3228 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
3229 memset(construct, 0, sizeof(server_startup_st));
3230 construct->count= SERVERS_TO_CREATE;
3231 construct->udp= 0;
3232 server_startup(construct);
3233
3234 return construct;
3235 }
3236
3237
3238 void world_destroy(void *p)
3239 {
3240 server_startup_st *construct= (server_startup_st *)p;
3241 memcached_server_st *servers= (memcached_server_st *)construct->servers;
3242 memcached_server_list_free(servers);
3243
3244 server_shutdown(construct);
3245 free(construct);
3246 }
3247
3248 void get_world(world_st *world)
3249 {
3250 world->collections= collection;
3251 world->create= world_create;
3252 world->destroy= world_destroy;
3253 }