Adding comments to memcached_mget() that you need to call fetch methods
[awesomized/libmemcached] / tests / function.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <memcached.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <time.h>
14 #include "../lib/common.h"
15 #include "../src/generator.h"
16 #include "../src/execute.h"
17
18 #include "test.h"
19
20 #define GLOBAL_COUNT 100000
21 static pairs_st *global_pairs;
22 static char *global_keys[GLOBAL_COUNT];
23 static size_t global_keys_length[GLOBAL_COUNT];
24
25 uint8_t init_test(memcached_st *not_used)
26 {
27 memcached_st memc;
28
29 (void)memcached_create(&memc);
30 memcached_free(&memc);
31
32 return 0;
33 }
34
35 uint8_t allocation_test(memcached_st *not_used)
36 {
37 memcached_st *memc;
38 memc= memcached_create(NULL);
39 assert(memc);
40 memcached_free(memc);
41
42 return 0;
43 }
44
45 uint8_t clone_test(memcached_st *memc)
46 {
47 /* All null? */
48 {
49 memcached_st *clone;
50 clone= memcached_clone(NULL, NULL);
51 assert(clone);
52 memcached_free(clone);
53 }
54
55 /* Can we init from null? */
56 {
57 memcached_st *clone;
58 clone= memcached_clone(NULL, memc);
59 assert(clone);
60 memcached_free(clone);
61 }
62
63 /* Can we init from struct? */
64 {
65 memcached_st declared_clone;
66 memcached_st *clone;
67 clone= memcached_clone(&declared_clone, NULL);
68 assert(clone);
69 memcached_free(clone);
70 }
71
72 /* Can we init from struct? */
73 {
74 memcached_st declared_clone;
75 memcached_st *clone;
76 clone= memcached_clone(&declared_clone, memc);
77 assert(clone);
78 memcached_free(clone);
79 }
80
81 return 0;
82 }
83
84 uint8_t connection_test(memcached_st *memc)
85 {
86 memcached_return rc;
87
88 rc= memcached_server_add(memc, "localhost", 0);
89 assert(rc == MEMCACHED_SUCCESS);
90
91 return 0;
92 }
93
94 uint8_t error_test(memcached_st *memc)
95 {
96 memcached_return rc;
97
98 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
99 {
100 printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
101 }
102
103 return 0;
104 }
105
106 uint8_t set_test(memcached_st *memc)
107 {
108 memcached_return rc;
109 char *key= "foo";
110 char *value= "when we sanitize";
111
112 rc= memcached_set(memc, key, strlen(key),
113 value, strlen(value),
114 (time_t)0, (uint16_t)0);
115 assert(rc == MEMCACHED_SUCCESS);
116
117 return 0;
118 }
119
120 uint8_t add_test(memcached_st *memc)
121 {
122 memcached_return rc;
123 char *key= "foo";
124 char *value= "when we sanitize";
125
126 rc= memcached_add(memc, key, strlen(key),
127 value, strlen(value),
128 (time_t)0, (uint16_t)0);
129 assert(rc == MEMCACHED_NOTSTORED);
130
131 return 0;
132 }
133
134 uint8_t replace_test(memcached_st *memc)
135 {
136 memcached_return rc;
137 char *key= "foo";
138 char *value= "when we sanitize";
139
140 rc= memcached_replace(memc, key, strlen(key),
141 value, strlen(value),
142 (time_t)0, (uint16_t)0);
143 assert(rc == MEMCACHED_SUCCESS);
144
145 return 0;
146 }
147
148 uint8_t delete_test(memcached_st *memc)
149 {
150 memcached_return rc;
151 char *key= "foo";
152 char *value= "when we sanitize";
153
154 rc= memcached_set(memc, key, strlen(key),
155 value, strlen(value),
156 (time_t)0, (uint16_t)0);
157 assert(rc == MEMCACHED_SUCCESS);
158
159 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
160 assert(rc == MEMCACHED_SUCCESS);
161
162 return 0;
163 }
164
165 uint8_t flush_test(memcached_st *memc)
166 {
167 memcached_return rc;
168
169 rc= memcached_flush(memc, 0);
170 assert(rc == MEMCACHED_SUCCESS);
171
172 return 0;
173 }
174
175 uint8_t get_test(memcached_st *memc)
176 {
177 memcached_return rc;
178 char *key= "foo";
179 char *string;
180 size_t string_length;
181 uint16_t flags;
182
183 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
184 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND);
185
186 string= memcached_get(memc, key, strlen(key),
187 &string_length, &flags, &rc);
188
189 assert(rc == MEMCACHED_NOTFOUND);
190 assert(string_length == 0);
191 assert(!string);
192
193 return 0;
194 }
195
196 uint8_t get_test2(memcached_st *memc)
197 {
198 memcached_return rc;
199 char *key= "foo";
200 char *value= "when we sanitize";
201 char *string;
202 size_t string_length;
203 uint16_t flags;
204
205 rc= memcached_set(memc, key, strlen(key),
206 value, strlen(value),
207 (time_t)0, (uint16_t)0);
208 assert(rc == MEMCACHED_SUCCESS);
209
210 string= memcached_get(memc, key, strlen(key),
211 &string_length, &flags, &rc);
212
213 assert(string);
214 assert(rc == MEMCACHED_SUCCESS);
215 assert(string_length == strlen(value));
216 assert(!memcmp(string, value, string_length));
217
218 free(string);
219
220 return 0;
221 }
222
223 uint8_t set_test2(memcached_st *memc)
224 {
225 memcached_return rc;
226 char *key= "foo";
227 char *value= "train in the brain";
228 size_t value_length= strlen(value);
229 unsigned int x;
230
231 for (x= 0; x < 10; x++)
232 {
233 rc= memcached_set(memc, key, strlen(key),
234 value, value_length,
235 (time_t)0, (uint16_t)0);
236 assert(rc == MEMCACHED_SUCCESS);
237 }
238
239 return 0;
240 }
241
242 uint8_t set_test3(memcached_st *memc)
243 {
244 memcached_return rc;
245 char *key= "foo";
246 char *value;
247 size_t value_length= 8191;
248 unsigned int x;
249
250 value = (char*)malloc(value_length);
251 assert(value);
252
253 for (x= 0; x < value_length; x++)
254 value[x] = (char) (x % 127);
255
256 for (x= 0; x < 1; x++)
257 {
258 rc= memcached_set(memc, key, strlen(key),
259 value, value_length,
260 (time_t)0, (uint16_t)0);
261 assert(rc == MEMCACHED_SUCCESS);
262 }
263
264 free(value);
265
266 return 0;
267 }
268
269 uint8_t get_test3(memcached_st *memc)
270 {
271 memcached_return rc;
272 char *key= "foo";
273 char *value;
274 size_t value_length= 8191;
275 char *string;
276 size_t string_length;
277 uint16_t flags;
278 int x;
279
280 value = (char*)malloc(value_length);
281 assert(value);
282
283 for (x= 0; x < value_length; x++)
284 value[x] = (char) (x % 127);
285
286 rc= memcached_set(memc, key, strlen(key),
287 value, value_length,
288 (time_t)0, (uint16_t)0);
289 assert(rc == MEMCACHED_SUCCESS);
290
291 string= memcached_get(memc, key, strlen(key),
292 &string_length, &flags, &rc);
293
294 assert(rc == MEMCACHED_SUCCESS);
295 assert(string);
296 assert(string_length == value_length);
297 assert(!memcmp(string, value, string_length));
298
299 free(string);
300 free(value);
301
302 return 0;
303 }
304
305 uint8_t get_test4(memcached_st *memc)
306 {
307 memcached_return rc;
308 char *key= "foo";
309 char *value;
310 size_t value_length= 8191;
311 char *string;
312 size_t string_length;
313 uint16_t flags;
314 int x;
315
316 value = (char*)malloc(value_length);
317 assert(value);
318
319 for (x= 0; x < value_length; x++)
320 value[x] = (char) (x % 127);
321
322 rc= memcached_set(memc, key, strlen(key),
323 value, value_length,
324 (time_t)0, (uint16_t)0);
325 assert(rc == MEMCACHED_SUCCESS);
326
327 for (x= 0; x < 10; x++)
328 {
329 string= memcached_get(memc, key, strlen(key),
330 &string_length, &flags, &rc);
331
332 assert(rc == MEMCACHED_SUCCESS);
333 assert(string);
334 assert(string_length == value_length);
335 assert(!memcmp(string, value, string_length));
336 free(string);
337 }
338
339 free(value);
340
341 return 0;
342 }
343
344 uint8_t stats_servername_test(memcached_st *memc)
345 {
346 memcached_return rc;
347 memcached_stat_st stat;
348 rc= memcached_stat_servername(&stat, NULL,
349 "localhost",
350 MEMCACHED_DEFAULT_PORT);
351
352 return 0;
353 }
354
355 uint8_t increment_test(memcached_st *memc)
356 {
357 uint64_t new_number;
358 memcached_return rc;
359 char *key= "number";
360 char *value= "0";
361
362 rc= memcached_set(memc, key, strlen(key),
363 value, strlen(value),
364 (time_t)0, (uint16_t)0);
365 assert(rc == MEMCACHED_SUCCESS);
366
367 rc= memcached_increment(memc, key, strlen(key),
368 1, &new_number);
369 assert(rc == MEMCACHED_SUCCESS);
370 assert(new_number == 1);
371
372 rc= memcached_increment(memc, key, strlen(key),
373 1, &new_number);
374 assert(rc == MEMCACHED_SUCCESS);
375 assert(new_number == 2);
376
377 return 0;
378 }
379
380 uint8_t decrement_test(memcached_st *memc)
381 {
382 uint64_t new_number;
383 memcached_return rc;
384 char *key= "number";
385 char *value= "3";
386
387 rc= memcached_set(memc, key, strlen(key),
388 value, strlen(value),
389 (time_t)0, (uint16_t)0);
390 assert(rc == MEMCACHED_SUCCESS);
391
392 rc= memcached_decrement(memc, key, strlen(key),
393 1, &new_number);
394 assert(rc == MEMCACHED_SUCCESS);
395 assert(new_number == 2);
396
397 rc= memcached_decrement(memc, key, strlen(key),
398 1, &new_number);
399 assert(rc == MEMCACHED_SUCCESS);
400 assert(new_number == 1);
401
402 return 0;
403 }
404
405 uint8_t quit_test(memcached_st *memc)
406 {
407 memcached_return rc;
408 char *key= "fudge";
409 char *value= "sanford and sun";
410
411 rc= memcached_set(memc, key, strlen(key),
412 value, strlen(value),
413 (time_t)10, (uint16_t)3);
414 assert(rc == MEMCACHED_SUCCESS);
415 memcached_quit(memc);
416
417 rc= memcached_set(memc, key, strlen(key),
418 value, strlen(value),
419 (time_t)50, (uint16_t)9);
420 assert(rc == MEMCACHED_SUCCESS);
421
422 return 0;
423 }
424
425 uint8_t mget_result_test(memcached_st *memc)
426 {
427 memcached_return rc;
428 char *keys[]= {"fudge", "son", "food"};
429 size_t key_length[]= {5, 3, 4};
430 unsigned int x;
431
432 memcached_result_st results_obj;
433 memcached_result_st *results;
434
435 results= memcached_result_create(memc, &results_obj);
436 assert(results);
437 assert(&results_obj == results);
438
439 /* We need to empty the server before continueing test */
440 rc= memcached_flush(memc, 0);
441 assert(rc == MEMCACHED_SUCCESS);
442
443 rc= memcached_mget(memc, keys, key_length, 3);
444 assert(rc == MEMCACHED_SUCCESS);
445
446 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
447 {
448 assert(results);
449 }
450 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
451 assert(!results);
452 assert(rc == MEMCACHED_NOTFOUND);
453
454 for (x= 0; x < 3; x++)
455 {
456 rc= memcached_set(memc, keys[x], key_length[x],
457 keys[x], key_length[x],
458 (time_t)50, (uint16_t)9);
459 assert(rc == MEMCACHED_SUCCESS);
460 }
461
462 rc= memcached_mget(memc, keys, key_length, 3);
463 assert(rc == MEMCACHED_SUCCESS);
464
465 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
466 {
467 assert(results);
468 assert(&results_obj == results);
469 assert(rc == MEMCACHED_SUCCESS);
470 assert(memcached_result_key_length(results) == memcached_result_length(results));
471 assert(!memcmp(memcached_result_key_value(results),
472 memcached_result_value(results),
473 memcached_result_length(results)));
474 }
475
476 memcached_result_free(&results_obj);
477
478 return 0;
479 }
480
481 uint8_t mget_result_alloc_test(memcached_st *memc)
482 {
483 memcached_return rc;
484 char *keys[]= {"fudge", "son", "food"};
485 size_t key_length[]= {5, 3, 4};
486 unsigned int x;
487
488 memcached_result_st *results;
489
490 /* We need to empty the server before continueing test */
491 rc= memcached_flush(memc, 0);
492 assert(rc == MEMCACHED_SUCCESS);
493
494 rc= memcached_mget(memc, keys, key_length, 3);
495 assert(rc == MEMCACHED_SUCCESS);
496
497 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
498 {
499 assert(results);
500 }
501 assert(!results);
502 assert(rc == MEMCACHED_NOTFOUND);
503
504 for (x= 0; x < 3; x++)
505 {
506 rc= memcached_set(memc, keys[x], key_length[x],
507 keys[x], key_length[x],
508 (time_t)50, (uint16_t)9);
509 assert(rc == MEMCACHED_SUCCESS);
510 }
511
512 rc= memcached_mget(memc, keys, key_length, 3);
513 assert(rc == MEMCACHED_SUCCESS);
514
515 x= 0;
516 while ((results= memcached_fetch_result(memc, NULL, &rc)))
517 {
518 assert(results);
519 assert(rc == MEMCACHED_SUCCESS);
520 assert(memcached_result_key_length(results) == memcached_result_length(results));
521 assert(!memcmp(memcached_result_key_value(results),
522 memcached_result_value(results),
523 memcached_result_length(results)));
524 memcached_result_free(results);
525 x++;
526 }
527
528 return 0;
529 }
530
531 uint8_t mget_test(memcached_st *memc)
532 {
533 memcached_return rc;
534 char *keys[]= {"fudge", "son", "food"};
535 size_t key_length[]= {5, 3, 4};
536 unsigned int x;
537 uint16_t flags;
538
539 char return_key[MEMCACHED_MAX_KEY];
540 size_t return_key_length;
541 char *return_value;
542 size_t return_value_length;
543
544 /* We need to empty the server before continueing test */
545 rc= memcached_flush(memc, 0);
546 assert(rc == MEMCACHED_SUCCESS);
547
548 rc= memcached_mget(memc, keys, key_length, 3);
549 assert(rc == MEMCACHED_SUCCESS);
550
551 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
552 &return_value_length, &flags, &rc)) != NULL)
553 {
554 assert(return_value);
555 }
556 assert(!return_value);
557 assert(return_value_length == 0);
558 assert(rc == MEMCACHED_NOTFOUND);
559
560 for (x= 0; x < 3; x++)
561 {
562 rc= memcached_set(memc, keys[x], key_length[x],
563 keys[x], key_length[x],
564 (time_t)50, (uint16_t)9);
565 assert(rc == MEMCACHED_SUCCESS);
566 }
567
568 rc= memcached_mget(memc, keys, key_length, 3);
569 assert(rc == MEMCACHED_SUCCESS);
570
571 x= 0;
572 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
573 &return_value_length, &flags, &rc)))
574 {
575 assert(return_value);
576 assert(rc == MEMCACHED_SUCCESS);
577 assert(return_key_length == return_value_length);
578 assert(!memcmp(return_value, return_key, return_value_length));
579 free(return_value);
580 x++;
581 }
582
583 return 0;
584 }
585
586 uint8_t get_stats_keys(memcached_st *memc)
587 {
588 char **list;
589 char **ptr;
590 memcached_stat_st stat;
591 memcached_return rc;
592
593 list= memcached_stat_get_keys(memc, &stat, &rc);
594 assert(rc == MEMCACHED_SUCCESS);
595 for (ptr= list; *ptr; ptr++)
596 printf("Found key %s\n", *ptr);
597 fflush(stdout);
598
599 free(list);
600
601 return 0;
602 }
603
604 uint8_t get_stats(memcached_st *memc)
605 {
606 unsigned int x;
607 char **list;
608 char **ptr;
609 memcached_return rc;
610 memcached_stat_st *stat;
611
612 stat= memcached_stat(memc, NULL, &rc);
613 assert(rc == MEMCACHED_SUCCESS);
614
615 assert(rc == MEMCACHED_SUCCESS);
616 assert(stat);
617
618 for (x= 0; x < memcached_server_count(memc); x++)
619 {
620 list= memcached_stat_get_keys(memc, &stat[x], &rc);
621 assert(rc == MEMCACHED_SUCCESS);
622 for (ptr= list; *ptr; ptr++)
623 printf("Found key %s\n", *ptr);
624
625 free(list);
626 }
627
628 memcached_stat_free(NULL, stat);
629
630 return 0;
631 }
632
633 uint8_t add_host_test(memcached_st *memc)
634 {
635 unsigned int x;
636 memcached_server_st *servers;
637 memcached_return rc;
638 char servername[]= "0.example.com";
639
640 servers= memcached_server_list_append(NULL, servername, 400, &rc);
641 assert(servers);
642 assert(1 == memcached_server_list_count(servers));
643
644 for (x= 2; x < 20; x++)
645 {
646 char buffer[SMALL_STRING_LEN];
647
648 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
649 servers= memcached_server_list_append(servers, buffer, 401,
650 &rc);
651 assert(rc == MEMCACHED_SUCCESS);
652 assert(x == memcached_server_list_count(servers));
653 }
654
655 rc= memcached_server_push(memc, servers);
656 assert(rc == MEMCACHED_SUCCESS);
657 rc= memcached_server_push(memc, servers);
658 assert(rc == MEMCACHED_SUCCESS);
659
660 memcached_server_list_free(servers);
661
662 return 0;
663 }
664
665 /* We don't test the behavior itself, we test the switches */
666 uint8_t behavior_test(memcached_st *memc)
667 {
668 unsigned long long value;
669 unsigned int set= 1;
670
671 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
672 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
673 assert(value == 1);
674
675 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
676 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
677 assert(value == 1);
678
679 set= MEMCACHED_HASH_MD5;
680 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
681 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
682 assert(value == MEMCACHED_HASH_MD5);
683
684 set= 0;
685
686 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set);
687 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
688 assert(value == 0);
689
690 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set);
691 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
692 assert(value == 0);
693
694 set= MEMCACHED_HASH_DEFAULT;
695 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
696 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
697 assert(value == MEMCACHED_HASH_DEFAULT);
698
699 set= MEMCACHED_HASH_CRC;
700 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set);
701 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
702 assert(value == MEMCACHED_HASH_CRC);
703
704 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
705 assert(value > 0);
706
707 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
708 assert(value > 0);
709
710 return 0;
711 }
712
713 /* Test case provided by Cal Haldenbrand */
714 uint8_t user_supplied_bug1(memcached_st *memc)
715 {
716 unsigned int setter= 1;
717 unsigned int x;
718
719 unsigned long long total= 0;
720 int size= 0;
721 char key[10];
722 char randomstuff[6 * 1024];
723 memcached_return rc;
724
725 memset(randomstuff, 0, 6 * 1024);
726
727 /* We just keep looking at the same values over and over */
728 srandom(10);
729
730 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
731 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
732
733
734 /* add key */
735 for (x= 0 ; total < 20 * 1024576 ; x++ )
736 {
737 unsigned int j= 0;
738
739 size= (rand() % ( 5 * 1024 ) ) + 400;
740 memset(randomstuff, 0, 6 * 1024);
741 assert(size < 6 * 1024); /* Being safe here */
742
743 for (j= 0 ; j < size ;j++)
744 randomstuff[j] = (char) (rand() % 26) + 97;
745
746 total += size;
747 sprintf(key, "%d", x);
748 rc = memcached_set(memc, key, strlen(key),
749 randomstuff, strlen(randomstuff), 10, 0);
750 /* If we fail, lets try again */
751 if (rc != MEMCACHED_SUCCESS)
752 rc = memcached_set(memc, key, strlen(key),
753 randomstuff, strlen(randomstuff), 10, 0);
754 assert(rc == MEMCACHED_SUCCESS);
755 }
756
757 return 0;
758 }
759
760 /* Test case provided by Cal Haldenbrand */
761 uint8_t user_supplied_bug2(memcached_st *memc)
762 {
763 int errors;
764 unsigned int setter;
765 unsigned int x;
766 unsigned long long total;
767
768 setter= 1;
769 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
770 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
771 #ifdef NOT_YET
772 setter = 20 * 1024576;
773 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
774 setter = 20 * 1024576;
775 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
776 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
777 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
778
779 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
780 #endif
781
782 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
783 {
784 memcached_return rc= MEMCACHED_SUCCESS;
785 char buffer[SMALL_STRING_LEN];
786 uint16_t flags= 0;
787 size_t val_len= 0;
788 char *getval;
789
790 memset(buffer, 0, SMALL_STRING_LEN);
791
792 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
793 getval= memcached_get(memc, buffer, strlen(buffer),
794 &val_len, &flags, &rc);
795 if (rc != MEMCACHED_SUCCESS)
796 {
797 if (rc == MEMCACHED_NOTFOUND)
798 errors++;
799 else
800 assert(0);
801
802 continue;
803 }
804 total+= val_len;
805 errors= 0;
806 free(getval);
807 }
808
809 return 0;
810 }
811
812 /* Do a large mget() over all the keys we think exist */
813 #define KEY_COUNT 3000 // * 1024576
814 uint8_t user_supplied_bug3(memcached_st *memc)
815 {
816 memcached_return rc;
817 unsigned int setter;
818 unsigned int x;
819 char **keys;
820 size_t key_lengths[KEY_COUNT];
821
822 setter= 1;
823 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter);
824 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter);
825 #ifdef NOT_YET
826 setter = 20 * 1024576;
827 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, &setter);
828 setter = 20 * 1024576;
829 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, &setter);
830 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
831 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
832 #endif
833
834 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
835 assert(keys);
836 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
837 for (x= 0; x < KEY_COUNT; x++)
838 {
839 char buffer[30];
840
841 snprintf(buffer, 30, "%u", x);
842 keys[x]= strdup(buffer);
843 key_lengths[x]= strlen(keys[x]);
844 }
845
846 rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
847 assert(rc == MEMCACHED_SUCCESS);
848
849 /* Turn this into a help function */
850 {
851 char return_key[MEMCACHED_MAX_KEY];
852 size_t return_key_length;
853 char *return_value;
854 size_t return_value_length;
855 uint16_t flags;
856
857 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
858 &return_value_length, &flags, &rc)))
859 {
860 assert(return_value);
861 assert(rc == MEMCACHED_SUCCESS);
862 free(return_value);
863 }
864 }
865
866 for (x= 0; x < KEY_COUNT; x++)
867 free(keys[x]);
868 free(keys);
869
870 return 0;
871 }
872
873 /* Make sure we behave properly if server list has no values */
874 uint8_t user_supplied_bug4(memcached_st *memc)
875 {
876 memcached_return rc;
877 char *keys[]= {"fudge", "son", "food"};
878 size_t key_length[]= {5, 3, 4};
879 unsigned int x;
880 uint16_t flags;
881
882 /* Here we free everything before running a bunch of mget tests */
883 {
884 memcached_server_list_free(memc->hosts);
885 memc->hosts= NULL;
886 memc->number_of_hosts= 0;
887 }
888
889 char return_key[MEMCACHED_MAX_KEY];
890 size_t return_key_length;
891 char *return_value;
892 size_t return_value_length;
893
894 /* We need to empty the server before continueing test */
895 rc= memcached_flush(memc, 0);
896 assert(rc == MEMCACHED_NO_SERVERS);
897
898 rc= memcached_mget(memc, keys, key_length, 3);
899 assert(rc == MEMCACHED_NO_SERVERS);
900
901 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
902 &return_value_length, &flags, &rc)) != NULL)
903 {
904 assert(return_value);
905 }
906 assert(!return_value);
907 assert(return_value_length == 0);
908 assert(rc == MEMCACHED_NO_SERVERS);
909
910 for (x= 0; x < 3; x++)
911 {
912 rc= memcached_set(memc, keys[x], key_length[x],
913 keys[x], key_length[x],
914 (time_t)50, (uint16_t)9);
915 assert(rc == MEMCACHED_NO_SERVERS);
916 }
917
918 rc= memcached_mget(memc, keys, key_length, 3);
919 assert(rc == MEMCACHED_NO_SERVERS);
920
921 x= 0;
922 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
923 &return_value_length, &flags, &rc)))
924 {
925 assert(return_value);
926 assert(rc == MEMCACHED_SUCCESS);
927 assert(return_key_length == return_value_length);
928 assert(!memcmp(return_value, return_key, return_value_length));
929 free(return_value);
930 x++;
931 }
932
933 return 0;
934 }
935
936 #define VALUE_SIZE_BUG5 1048064
937 uint8_t user_supplied_bug5(memcached_st *memc)
938 {
939 memcached_return rc;
940 char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
941 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
942 char return_key[MEMCACHED_MAX_KEY];
943 size_t return_key_length;
944 char *value;
945 size_t value_length;
946 uint16_t flags;
947 unsigned int count;
948 unsigned int x;
949 char insert_data[VALUE_SIZE_BUG5];
950
951 for (x= 0; x < VALUE_SIZE_BUG5; x++)
952 insert_data[x]= rand();
953
954 memcached_flush(memc, 0);
955 value= memcached_get(memc, keys[0], key_length[0],
956 &value_length, &flags, &rc);
957 assert(value == NULL);
958 rc= memcached_mget(memc, keys, key_length, 4);
959
960 count= 0;
961 while ((value= memcached_fetch(memc, return_key, &return_key_length,
962 &value_length, &flags, &rc)))
963 count++;
964 assert(count == 0);
965
966 for (x= 0; x < 4; x++)
967 {
968 rc= memcached_set(memc, keys[x], key_length[x],
969 insert_data, VALUE_SIZE_BUG5,
970 (time_t)0, (uint16_t)0);
971 assert(rc == MEMCACHED_SUCCESS);
972 }
973
974 for (x= 0; x < 10; x++)
975 {
976 value= memcached_get(memc, keys[0], key_length[0],
977 &value_length, &flags, &rc);
978 assert(value);
979 free(value);
980
981 rc= memcached_mget(memc, keys, key_length, 4);
982 count= 0;
983 while ((value= memcached_fetch(memc, return_key, &return_key_length,
984 &value_length, &flags, &rc)))
985 {
986 count++;
987 free(value);
988 }
989 assert(count == 4);
990 }
991
992 return 0;
993 }
994
995 uint8_t result_static(memcached_st *memc)
996 {
997 memcached_result_st result;
998 memcached_result_st *result_ptr;
999
1000 result_ptr= memcached_result_create(memc, &result);
1001 assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
1002 assert(result_ptr);
1003 memcached_result_free(&result);
1004
1005 return 0;
1006 }
1007
1008 uint8_t result_alloc(memcached_st *memc)
1009 {
1010 memcached_result_st *result;
1011
1012 result= memcached_result_create(memc, NULL);
1013 assert(result);
1014 memcached_result_free(result);
1015
1016 return 0;
1017 }
1018
1019 uint8_t string_static_null(memcached_st *memc)
1020 {
1021 memcached_string_st string;
1022 memcached_string_st *string_ptr;
1023
1024 string_ptr= memcached_string_create(memc, &string, 0);
1025 assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
1026 assert(string_ptr);
1027 memcached_string_free(&string);
1028
1029 return 0;
1030 }
1031
1032 uint8_t string_alloc_null(memcached_st *memc)
1033 {
1034 memcached_string_st *string;
1035
1036 string= memcached_string_create(memc, NULL, 0);
1037 assert(string);
1038 memcached_string_free(string);
1039
1040 return 0;
1041 }
1042
1043 uint8_t string_alloc_with_size(memcached_st *memc)
1044 {
1045 memcached_string_st *string;
1046
1047 string= memcached_string_create(memc, NULL, 1024);
1048 assert(string);
1049 memcached_string_free(string);
1050
1051 return 0;
1052 }
1053
1054 uint8_t string_alloc_with_size_toobig(memcached_st *memc)
1055 {
1056 memcached_string_st *string;
1057
1058 string= memcached_string_create(memc, NULL, INT64_MAX);
1059 assert(string == NULL);
1060
1061 return 0;
1062 }
1063
1064 uint8_t string_alloc_append(memcached_st *memc)
1065 {
1066 unsigned int x;
1067 char buffer[SMALL_STRING_LEN];
1068 memcached_string_st *string;
1069
1070 /* Ring the bell! */
1071 memset(buffer, 6, SMALL_STRING_LEN);
1072
1073 string= memcached_string_create(memc, NULL, 100);
1074 assert(string);
1075
1076 for (x= 0; x < 1024; x++)
1077 {
1078 memcached_return rc;
1079 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1080 assert(rc == MEMCACHED_SUCCESS);
1081 }
1082 memcached_string_free(string);
1083
1084 return 0;
1085 }
1086
1087 uint8_t string_alloc_append_toobig(memcached_st *memc)
1088 {
1089 memcached_return rc;
1090 unsigned int x;
1091 char buffer[SMALL_STRING_LEN];
1092 memcached_string_st *string;
1093
1094 /* Ring the bell! */
1095 memset(buffer, 6, SMALL_STRING_LEN);
1096
1097 string= memcached_string_create(memc, NULL, 100);
1098 assert(string);
1099
1100 for (x= 0; x < 1024; x++)
1101 {
1102 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
1103 assert(rc == MEMCACHED_SUCCESS);
1104 }
1105 rc= memcached_string_append(string, buffer, INT64_MAX);
1106 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
1107 memcached_string_free(string);
1108
1109 return 0;
1110 }
1111
1112 uint8_t generate_data(memcached_st *memc)
1113 {
1114 unsigned long long x;
1115 global_pairs= pairs_generate(GLOBAL_COUNT);
1116 execute_set(memc, global_pairs, GLOBAL_COUNT);
1117
1118 for (x= 0; x < GLOBAL_COUNT; x++)
1119 {
1120 global_keys[x]= global_pairs[x].key;
1121 global_keys_length[x]= global_pairs[x].key_length;
1122 }
1123
1124 return 0;
1125 }
1126
1127 uint8_t get_read(memcached_st *memc)
1128 {
1129 unsigned int x;
1130 memcached_return rc;
1131
1132 {
1133 char *return_value;
1134 size_t return_value_length;
1135 uint16_t flags;
1136
1137 for (x= 0; x < GLOBAL_COUNT; x++)
1138 {
1139 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
1140 &return_value_length, &flags, &rc);
1141 /*
1142 assert(return_value);
1143 assert(rc == MEMCACHED_SUCCESS);
1144 */
1145 if (rc == MEMCACHED_SUCCESS && return_value)
1146 free(return_value);
1147 }
1148 }
1149
1150 return 0;
1151 }
1152
1153 uint8_t mget_read(memcached_st *memc)
1154 {
1155 memcached_return rc;
1156
1157 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1158 assert(rc == MEMCACHED_SUCCESS);
1159 /* Turn this into a help function */
1160 {
1161 char return_key[MEMCACHED_MAX_KEY];
1162 size_t return_key_length;
1163 char *return_value;
1164 size_t return_value_length;
1165 uint16_t flags;
1166
1167 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1168 &return_value_length, &flags, &rc)))
1169 {
1170 assert(return_value);
1171 assert(rc == MEMCACHED_SUCCESS);
1172 free(return_value);
1173 }
1174 }
1175
1176 return 0;
1177 }
1178
1179 uint8_t mget_read_result(memcached_st *memc)
1180 {
1181 memcached_return rc;
1182
1183 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1184 assert(rc == MEMCACHED_SUCCESS);
1185 /* Turn this into a help function */
1186 {
1187 memcached_result_st results_obj;
1188 memcached_result_st *results;
1189
1190 results= memcached_result_create(memc, &results_obj);
1191
1192 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1193 {
1194 assert(results);
1195 assert(rc == MEMCACHED_SUCCESS);
1196 }
1197
1198 memcached_result_free(&results_obj);
1199 }
1200
1201 return 0;
1202 }
1203
1204 uint8_t free_data(memcached_st *memc)
1205 {
1206 pairs_free(global_pairs);
1207
1208 return 0;
1209 }
1210
1211 uint8_t add_host_test1(memcached_st *memc)
1212 {
1213 unsigned int x;
1214 memcached_return rc;
1215 char servername[]= "0.example.com";
1216 memcached_server_st *servers;
1217
1218 servers= memcached_server_list_append(NULL, servername, 400, &rc);
1219 assert(servers);
1220 assert(1 == memcached_server_list_count(servers));
1221
1222 for (x= 2; x < 20; x++)
1223 {
1224 char buffer[SMALL_STRING_LEN];
1225
1226 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1227 servers= memcached_server_list_append(servers, buffer, 401,
1228 &rc);
1229 assert(rc == MEMCACHED_SUCCESS);
1230 assert(x == memcached_server_list_count(servers));
1231 }
1232
1233 rc= memcached_server_push(memc, servers);
1234 assert(rc == MEMCACHED_SUCCESS);
1235 rc= memcached_server_push(memc, servers);
1236 assert(rc == MEMCACHED_SUCCESS);
1237
1238 memcached_server_list_free(servers);
1239
1240 return 0;
1241 }
1242
1243 memcached_return pre_nonblock(memcached_st *memc)
1244 {
1245 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1246
1247 return MEMCACHED_SUCCESS;
1248 }
1249
1250 memcached_return pre_md5(memcached_st *memc)
1251 {
1252 memcached_hash value= MEMCACHED_HASH_MD5;
1253 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1254
1255 return MEMCACHED_SUCCESS;
1256 }
1257
1258 memcached_return pre_crc(memcached_st *memc)
1259 {
1260 memcached_hash value= MEMCACHED_HASH_CRC;
1261 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1262
1263 return MEMCACHED_SUCCESS;
1264 }
1265
1266 memcached_return pre_hash_fnv1_64(memcached_st *memc)
1267 {
1268 memcached_hash value= MEMCACHED_HASH_FNV1_64;
1269 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1270
1271 return MEMCACHED_SUCCESS;
1272 }
1273
1274 memcached_return pre_hash_fnv1a_64(memcached_st *memc)
1275 {
1276 memcached_hash value= MEMCACHED_HASH_FNV1A_64;
1277 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1278
1279 return MEMCACHED_SUCCESS;
1280 }
1281
1282 memcached_return pre_hash_fnv1_32(memcached_st *memc)
1283 {
1284 memcached_hash value= MEMCACHED_HASH_FNV1_32;
1285 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1286
1287 return MEMCACHED_SUCCESS;
1288 }
1289
1290 memcached_return pre_hash_fnv1a_32(memcached_st *memc)
1291 {
1292 memcached_hash value= MEMCACHED_HASH_FNV1A_32;
1293 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1294
1295 return MEMCACHED_SUCCESS;
1296 }
1297
1298 memcached_return pre_hash_ketama(memcached_st *memc)
1299 {
1300 memcached_hash value= MEMCACHED_HASH_KETAMA;
1301 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1302
1303 return MEMCACHED_SUCCESS;
1304 }
1305
1306 memcached_return pre_unix_socket(memcached_st *memc)
1307 {
1308 memcached_return rc;
1309 struct stat buf;
1310
1311 memcached_server_list_free(memc->hosts);
1312 memc->hosts= NULL;
1313 memc->number_of_hosts= 0;
1314
1315 if (stat("/tmp/memcached.socket", &buf))
1316 return MEMCACHED_FAILURE;
1317
1318 rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket");
1319
1320 return rc;
1321 }
1322
1323 memcached_return pre_nodelay(memcached_st *memc)
1324 {
1325 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1326 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
1327
1328 return MEMCACHED_SUCCESS;
1329 }
1330
1331
1332 /* Clean the server before beginning testing */
1333 test_st tests[] ={
1334 {"flush", 0, flush_test },
1335 {"init", 0, init_test },
1336 {"allocation", 0, allocation_test },
1337 {"clone_test", 0, clone_test },
1338 {"error", 0, error_test },
1339 {"set", 0, set_test },
1340 {"set2", 0, set_test2 },
1341 {"set3", 0, set_test3 },
1342 {"add", 0, add_test },
1343 {"replace", 0, replace_test },
1344 {"delete", 1, delete_test },
1345 {"get", 1, get_test },
1346 {"get2", 0, get_test2 },
1347 {"get3", 0, get_test3 },
1348 {"get4", 0, get_test4 },
1349 {"stats_servername", 0, stats_servername_test },
1350 {"increment", 0, increment_test },
1351 {"decrement", 0, decrement_test },
1352 {"quit", 0, quit_test },
1353 {"mget", 1, mget_test },
1354 {"mget_result", 1, mget_result_test },
1355 {"mget_result_alloc", 1, mget_result_alloc_test },
1356 {"get_stats", 0, get_stats },
1357 {"add_host_test", 0, add_host_test },
1358 {"get_stats_keys", 0, get_stats_keys },
1359 {"behavior_test", 0, get_stats_keys },
1360 {0, 0, 0}
1361 };
1362
1363 test_st string_tests[] ={
1364 {"string static with null", 0, string_static_null },
1365 {"string alloc with null", 0, string_alloc_null },
1366 {"string alloc with 1K", 0, string_alloc_with_size },
1367 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
1368 {"string append", 0, string_alloc_append },
1369 {"string append failure (too big)", 0, string_alloc_append_toobig },
1370 {0, 0, 0}
1371 };
1372
1373 test_st result_tests[] ={
1374 {"result static", 0, result_static},
1375 {"result alloc", 0, result_alloc},
1376 {0, 0, 0}
1377 };
1378
1379 test_st user_tests[] ={
1380 {"user_supplied_bug1", 0, user_supplied_bug1 },
1381 {"user_supplied_bug2", 0, user_supplied_bug2 },
1382 {"user_supplied_bug3", 0, user_supplied_bug3 },
1383 {"user_supplied_bug4", 0, user_supplied_bug4 },
1384 {"user_supplied_bug5", 1, user_supplied_bug5 },
1385 {0, 0, 0}
1386 };
1387
1388 test_st generate_tests[] ={
1389 {"generate_data", 0, generate_data },
1390 {"get_read", 0, get_read },
1391 {"mget_read", 0, mget_read },
1392 {"mget_read_result", 0, mget_read_result },
1393 {0, 0, 0}
1394 };
1395
1396
1397 collection_st collection[] ={
1398 {"block", 0, 0, tests},
1399 {"nonblock", pre_nonblock, 0, tests},
1400 {"nodelay", pre_nodelay, 0, tests},
1401 {"md5", pre_md5, 0, tests},
1402 {"crc", pre_crc, 0, tests},
1403 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
1404 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
1405 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
1406 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
1407 {"ketama", pre_hash_ketama, 0, tests},
1408 {"unix_socket", pre_unix_socket, 0, tests},
1409 {"unix_socket_nodelay", pre_nodelay, 0, tests},
1410 {"string", 0, 0, string_tests},
1411 {"result", 0, 0, result_tests},
1412 {"user", 0, 0, user_tests},
1413 {"generate", 0, 0, generate_tests},
1414 {0, 0, 0, 0}
1415 };
1416
1417 collection_st *gets_collections(void)
1418 {
1419 return collection;
1420 }