2 Sample test application.
10 #include <sys/types.h>
14 #include "../lib/common.h"
15 #include "../src/generator.h"
16 #include "../src/execute.h"
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
];
25 uint8_t init_test(memcached_st
*not_used
)
29 (void)memcached_create(&memc
);
30 memcached_free(&memc
);
35 uint8_t allocation_test(memcached_st
*not_used
)
38 memc
= memcached_create(NULL
);
45 uint8_t clone_test(memcached_st
*memc
)
50 clone
= memcached_clone(NULL
, NULL
);
52 memcached_free(clone
);
55 /* Can we init from null? */
58 clone
= memcached_clone(NULL
, memc
);
60 memcached_free(clone
);
63 /* Can we init from struct? */
65 memcached_st declared_clone
;
67 clone
= memcached_clone(&declared_clone
, NULL
);
69 memcached_free(clone
);
72 /* Can we init from struct? */
74 memcached_st declared_clone
;
76 clone
= memcached_clone(&declared_clone
, memc
);
78 memcached_free(clone
);
84 uint8_t connection_test(memcached_st
*memc
)
88 rc
= memcached_server_add(memc
, "localhost", 0);
89 assert(rc
== MEMCACHED_SUCCESS
);
94 uint8_t error_test(memcached_st
*memc
)
98 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
100 printf("Error %d -> %s\n", rc
, memcached_strerror(memc
, rc
));
106 uint8_t set_test(memcached_st
*memc
)
110 char *value
= "when we sanitize";
112 rc
= memcached_set(memc
, key
, strlen(key
),
113 value
, strlen(value
),
114 (time_t)0, (uint16_t)0);
115 assert(rc
== MEMCACHED_SUCCESS
);
120 uint8_t add_test(memcached_st
*memc
)
124 char *value
= "when we sanitize";
126 rc
= memcached_add(memc
, key
, strlen(key
),
127 value
, strlen(value
),
128 (time_t)0, (uint16_t)0);
129 assert(rc
== MEMCACHED_NOTSTORED
);
134 uint8_t replace_test(memcached_st
*memc
)
138 char *value
= "when we sanitize";
140 rc
= memcached_replace(memc
, key
, strlen(key
),
141 value
, strlen(value
),
142 (time_t)0, (uint16_t)0);
143 assert(rc
== MEMCACHED_SUCCESS
);
148 uint8_t delete_test(memcached_st
*memc
)
152 char *value
= "when we sanitize";
154 rc
= memcached_set(memc
, key
, strlen(key
),
155 value
, strlen(value
),
156 (time_t)0, (uint16_t)0);
157 assert(rc
== MEMCACHED_SUCCESS
);
159 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
160 assert(rc
== MEMCACHED_SUCCESS
);
165 uint8_t flush_test(memcached_st
*memc
)
169 rc
= memcached_flush(memc
, 0);
170 assert(rc
== MEMCACHED_SUCCESS
);
175 uint8_t get_test(memcached_st
*memc
)
180 size_t string_length
;
183 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
184 assert(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
);
186 string
= memcached_get(memc
, key
, strlen(key
),
187 &string_length
, &flags
, &rc
);
189 assert(rc
== MEMCACHED_NOTFOUND
);
190 assert(string_length
== 0);
196 uint8_t get_test2(memcached_st
*memc
)
200 char *value
= "when we sanitize";
202 size_t string_length
;
205 rc
= memcached_set(memc
, key
, strlen(key
),
206 value
, strlen(value
),
207 (time_t)0, (uint16_t)0);
208 assert(rc
== MEMCACHED_SUCCESS
);
210 string
= memcached_get(memc
, key
, strlen(key
),
211 &string_length
, &flags
, &rc
);
214 assert(rc
== MEMCACHED_SUCCESS
);
215 assert(string_length
== strlen(value
));
216 assert(!memcmp(string
, value
, string_length
));
223 uint8_t set_test2(memcached_st
*memc
)
227 char *value
= "train in the brain";
228 size_t value_length
= strlen(value
);
231 for (x
= 0; x
< 10; x
++)
233 rc
= memcached_set(memc
, key
, strlen(key
),
235 (time_t)0, (uint16_t)0);
236 assert(rc
== MEMCACHED_SUCCESS
);
242 uint8_t set_test3(memcached_st
*memc
)
247 size_t value_length
= 8191;
250 value
= (char*)malloc(value_length
);
253 for (x
= 0; x
< value_length
; x
++)
254 value
[x
] = (char) (x
% 127);
256 for (x
= 0; x
< 1; x
++)
258 rc
= memcached_set(memc
, key
, strlen(key
),
260 (time_t)0, (uint16_t)0);
261 assert(rc
== MEMCACHED_SUCCESS
);
269 uint8_t get_test3(memcached_st
*memc
)
274 size_t value_length
= 8191;
276 size_t string_length
;
280 value
= (char*)malloc(value_length
);
283 for (x
= 0; x
< value_length
; x
++)
284 value
[x
] = (char) (x
% 127);
286 rc
= memcached_set(memc
, key
, strlen(key
),
288 (time_t)0, (uint16_t)0);
289 assert(rc
== MEMCACHED_SUCCESS
);
291 string
= memcached_get(memc
, key
, strlen(key
),
292 &string_length
, &flags
, &rc
);
294 assert(rc
== MEMCACHED_SUCCESS
);
296 assert(string_length
== value_length
);
297 assert(!memcmp(string
, value
, string_length
));
305 uint8_t get_test4(memcached_st
*memc
)
310 size_t value_length
= 8191;
312 size_t string_length
;
316 value
= (char*)malloc(value_length
);
319 for (x
= 0; x
< value_length
; x
++)
320 value
[x
] = (char) (x
% 127);
322 rc
= memcached_set(memc
, key
, strlen(key
),
324 (time_t)0, (uint16_t)0);
325 assert(rc
== MEMCACHED_SUCCESS
);
327 for (x
= 0; x
< 10; x
++)
329 string
= memcached_get(memc
, key
, strlen(key
),
330 &string_length
, &flags
, &rc
);
332 assert(rc
== MEMCACHED_SUCCESS
);
334 assert(string_length
== value_length
);
335 assert(!memcmp(string
, value
, string_length
));
344 uint8_t stats_servername_test(memcached_st
*memc
)
347 memcached_stat_st stat
;
348 rc
= memcached_stat_servername(&stat
, NULL
,
350 MEMCACHED_DEFAULT_PORT
);
355 uint8_t increment_test(memcached_st
*memc
)
362 rc
= memcached_set(memc
, key
, strlen(key
),
363 value
, strlen(value
),
364 (time_t)0, (uint16_t)0);
365 assert(rc
== MEMCACHED_SUCCESS
);
367 rc
= memcached_increment(memc
, key
, strlen(key
),
369 assert(rc
== MEMCACHED_SUCCESS
);
370 assert(new_number
== 1);
372 rc
= memcached_increment(memc
, key
, strlen(key
),
374 assert(rc
== MEMCACHED_SUCCESS
);
375 assert(new_number
== 2);
380 uint8_t decrement_test(memcached_st
*memc
)
387 rc
= memcached_set(memc
, key
, strlen(key
),
388 value
, strlen(value
),
389 (time_t)0, (uint16_t)0);
390 assert(rc
== MEMCACHED_SUCCESS
);
392 rc
= memcached_decrement(memc
, key
, strlen(key
),
394 assert(rc
== MEMCACHED_SUCCESS
);
395 assert(new_number
== 2);
397 rc
= memcached_decrement(memc
, key
, strlen(key
),
399 assert(rc
== MEMCACHED_SUCCESS
);
400 assert(new_number
== 1);
405 uint8_t quit_test(memcached_st
*memc
)
409 char *value
= "sanford and sun";
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
);
417 rc
= memcached_set(memc
, key
, strlen(key
),
418 value
, strlen(value
),
419 (time_t)50, (uint16_t)9);
420 assert(rc
== MEMCACHED_SUCCESS
);
425 uint8_t mget_result_test(memcached_st
*memc
)
428 char *keys
[]= {"fudge", "son", "food"};
429 size_t key_length
[]= {5, 3, 4};
432 memcached_result_st results_obj
;
433 memcached_result_st
*results
;
435 results
= memcached_result_create(memc
, &results_obj
);
437 assert(&results_obj
== results
);
439 /* We need to empty the server before continueing test */
440 rc
= memcached_flush(memc
, 0);
441 assert(rc
== MEMCACHED_SUCCESS
);
443 rc
= memcached_mget(memc
, keys
, key_length
, 3);
444 assert(rc
== MEMCACHED_SUCCESS
);
446 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
450 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
452 assert(rc
== MEMCACHED_NOTFOUND
);
454 for (x
= 0; x
< 3; x
++)
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
);
462 rc
= memcached_mget(memc
, keys
, key_length
, 3);
463 assert(rc
== MEMCACHED_SUCCESS
);
465 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
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
)));
476 memcached_result_free(&results_obj
);
481 uint8_t mget_result_alloc_test(memcached_st
*memc
)
484 char *keys
[]= {"fudge", "son", "food"};
485 size_t key_length
[]= {5, 3, 4};
488 memcached_result_st
*results
;
490 /* We need to empty the server before continueing test */
491 rc
= memcached_flush(memc
, 0);
492 assert(rc
== MEMCACHED_SUCCESS
);
494 rc
= memcached_mget(memc
, keys
, key_length
, 3);
495 assert(rc
== MEMCACHED_SUCCESS
);
497 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
502 assert(rc
== MEMCACHED_NOTFOUND
);
504 for (x
= 0; x
< 3; x
++)
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
);
512 rc
= memcached_mget(memc
, keys
, key_length
, 3);
513 assert(rc
== MEMCACHED_SUCCESS
);
516 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
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
);
531 uint8_t mget_test(memcached_st
*memc
)
534 char *keys
[]= {"fudge", "son", "food"};
535 size_t key_length
[]= {5, 3, 4};
539 char return_key
[MEMCACHED_MAX_KEY
];
540 size_t return_key_length
;
542 size_t return_value_length
;
544 /* We need to empty the server before continueing test */
545 rc
= memcached_flush(memc
, 0);
546 assert(rc
== MEMCACHED_SUCCESS
);
548 rc
= memcached_mget(memc
, keys
, key_length
, 3);
549 assert(rc
== MEMCACHED_SUCCESS
);
551 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
552 &return_value_length
, &flags
, &rc
)) != NULL
)
554 assert(return_value
);
556 assert(!return_value
);
557 assert(return_value_length
== 0);
558 assert(rc
== MEMCACHED_NOTFOUND
);
560 for (x
= 0; x
< 3; x
++)
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
);
568 rc
= memcached_mget(memc
, keys
, key_length
, 3);
569 assert(rc
== MEMCACHED_SUCCESS
);
572 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
573 &return_value_length
, &flags
, &rc
)))
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
));
586 uint8_t get_stats_keys(memcached_st
*memc
)
590 memcached_stat_st stat
;
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
);
604 uint8_t get_stats(memcached_st
*memc
)
610 memcached_stat_st
*stat
;
612 stat
= memcached_stat(memc
, NULL
, &rc
);
613 assert(rc
== MEMCACHED_SUCCESS
);
615 assert(rc
== MEMCACHED_SUCCESS
);
618 for (x
= 0; x
< memcached_server_count(memc
); x
++)
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
);
628 memcached_stat_free(NULL
, stat
);
633 uint8_t add_host_test(memcached_st
*memc
)
636 memcached_server_st
*servers
;
638 char servername
[]= "0.example.com";
640 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
642 assert(1 == memcached_server_list_count(servers
));
644 for (x
= 2; x
< 20; x
++)
646 char buffer
[SMALL_STRING_LEN
];
648 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
649 servers
= memcached_server_list_append(servers
, buffer
, 401,
651 assert(rc
== MEMCACHED_SUCCESS
);
652 assert(x
== memcached_server_list_count(servers
));
655 rc
= memcached_server_push(memc
, servers
);
656 assert(rc
== MEMCACHED_SUCCESS
);
657 rc
= memcached_server_push(memc
, servers
);
658 assert(rc
== MEMCACHED_SUCCESS
);
660 memcached_server_list_free(servers
);
665 /* We don't test the behavior itself, we test the switches */
666 uint8_t behavior_test(memcached_st
*memc
)
668 unsigned long long value
;
671 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
672 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
675 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
676 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
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
);
686 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &set
);
687 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
690 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &set
);
691 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
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
);
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
);
704 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
707 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
713 /* Test case provided by Cal Haldenbrand */
714 uint8_t user_supplied_bug1(memcached_st
*memc
)
716 unsigned int setter
= 1;
719 unsigned long long total
= 0;
722 char randomstuff
[6 * 1024];
725 memset(randomstuff
, 0, 6 * 1024);
727 /* We just keep looking at the same values over and over */
730 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
731 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
735 for (x
= 0 ; total
< 20 * 1024576 ; x
++ )
739 size
= (rand() % ( 5 * 1024 ) ) + 400;
740 memset(randomstuff
, 0, 6 * 1024);
741 assert(size
< 6 * 1024); /* Being safe here */
743 for (j
= 0 ; j
< size
;j
++)
744 randomstuff
[j
] = (char) (rand() % 26) + 97;
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
);
760 /* Test case provided by Cal Haldenbrand */
761 uint8_t user_supplied_bug2(memcached_st
*memc
)
766 unsigned long long total
;
769 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
770 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
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
);
779 for (x
= 0, errors
= 0, total
= 0 ; total
< 20 * 1024576 ; x
++)
782 for (x
= 0, errors
= 0, total
= 0 ; total
< 24576 ; x
++)
784 memcached_return rc
= MEMCACHED_SUCCESS
;
785 char buffer
[SMALL_STRING_LEN
];
790 memset(buffer
, 0, SMALL_STRING_LEN
);
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
)
797 if (rc
== MEMCACHED_NOTFOUND
)
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
)
820 size_t key_lengths
[KEY_COUNT
];
823 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &setter
);
824 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &setter
);
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
);
834 keys
= (char **)malloc(sizeof(char *) * KEY_COUNT
);
836 memset(keys
, 0, (sizeof(char *) * KEY_COUNT
));
837 for (x
= 0; x
< KEY_COUNT
; x
++)
841 snprintf(buffer
, 30, "%u", x
);
842 keys
[x
]= strdup(buffer
);
843 key_lengths
[x
]= strlen(keys
[x
]);
846 rc
= memcached_mget(memc
, keys
, key_lengths
, KEY_COUNT
);
847 assert(rc
== MEMCACHED_SUCCESS
);
849 /* Turn this into a help function */
851 char return_key
[MEMCACHED_MAX_KEY
];
852 size_t return_key_length
;
854 size_t return_value_length
;
857 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
858 &return_value_length
, &flags
, &rc
)))
860 assert(return_value
);
861 assert(rc
== MEMCACHED_SUCCESS
);
866 for (x
= 0; x
< KEY_COUNT
; x
++)
873 /* Make sure we behave properly if server list has no values */
874 uint8_t user_supplied_bug4(memcached_st
*memc
)
877 char *keys
[]= {"fudge", "son", "food"};
878 size_t key_length
[]= {5, 3, 4};
882 /* Here we free everything before running a bunch of mget tests */
884 memcached_server_list_free(memc
->hosts
);
886 memc
->number_of_hosts
= 0;
889 char return_key
[MEMCACHED_MAX_KEY
];
890 size_t return_key_length
;
892 size_t return_value_length
;
894 /* We need to empty the server before continueing test */
895 rc
= memcached_flush(memc
, 0);
896 assert(rc
== MEMCACHED_NO_SERVERS
);
898 rc
= memcached_mget(memc
, keys
, key_length
, 3);
899 assert(rc
== MEMCACHED_NO_SERVERS
);
901 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
902 &return_value_length
, &flags
, &rc
)) != NULL
)
904 assert(return_value
);
906 assert(!return_value
);
907 assert(return_value_length
== 0);
908 assert(rc
== MEMCACHED_NO_SERVERS
);
910 for (x
= 0; x
< 3; x
++)
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
);
918 rc
= memcached_mget(memc
, keys
, key_length
, 3);
919 assert(rc
== MEMCACHED_NO_SERVERS
);
922 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
923 &return_value_length
, &flags
, &rc
)))
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
));
936 #define VALUE_SIZE_BUG5 1048064
937 uint8_t user_supplied_bug5(memcached_st
*memc
)
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
;
949 char insert_data
[VALUE_SIZE_BUG5
];
951 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
952 insert_data
[x
]= rand();
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);
961 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
962 &value_length
, &flags
, &rc
)))
966 for (x
= 0; x
< 4; x
++)
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
);
974 for (x
= 0; x
< 10; x
++)
976 value
= memcached_get(memc
, keys
[0], key_length
[0],
977 &value_length
, &flags
, &rc
);
981 rc
= memcached_mget(memc
, keys
, key_length
, 4);
983 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
984 &value_length
, &flags
, &rc
)))
995 uint8_t result_static(memcached_st
*memc
)
997 memcached_result_st result
;
998 memcached_result_st
*result_ptr
;
1000 result_ptr
= memcached_result_create(memc
, &result
);
1001 assert(result
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1003 memcached_result_free(&result
);
1008 uint8_t result_alloc(memcached_st
*memc
)
1010 memcached_result_st
*result
;
1012 result
= memcached_result_create(memc
, NULL
);
1014 memcached_result_free(result
);
1019 uint8_t string_static_null(memcached_st
*memc
)
1021 memcached_string_st string
;
1022 memcached_string_st
*string_ptr
;
1024 string_ptr
= memcached_string_create(memc
, &string
, 0);
1025 assert(string
.is_allocated
== MEMCACHED_NOT_ALLOCATED
);
1027 memcached_string_free(&string
);
1032 uint8_t string_alloc_null(memcached_st
*memc
)
1034 memcached_string_st
*string
;
1036 string
= memcached_string_create(memc
, NULL
, 0);
1038 memcached_string_free(string
);
1043 uint8_t string_alloc_with_size(memcached_st
*memc
)
1045 memcached_string_st
*string
;
1047 string
= memcached_string_create(memc
, NULL
, 1024);
1049 memcached_string_free(string
);
1054 uint8_t string_alloc_with_size_toobig(memcached_st
*memc
)
1056 memcached_string_st
*string
;
1058 string
= memcached_string_create(memc
, NULL
, INT64_MAX
);
1059 assert(string
== NULL
);
1064 uint8_t string_alloc_append(memcached_st
*memc
)
1067 char buffer
[SMALL_STRING_LEN
];
1068 memcached_string_st
*string
;
1070 /* Ring the bell! */
1071 memset(buffer
, 6, SMALL_STRING_LEN
);
1073 string
= memcached_string_create(memc
, NULL
, 100);
1076 for (x
= 0; x
< 1024; x
++)
1078 memcached_return rc
;
1079 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1080 assert(rc
== MEMCACHED_SUCCESS
);
1082 memcached_string_free(string
);
1087 uint8_t string_alloc_append_toobig(memcached_st
*memc
)
1089 memcached_return rc
;
1091 char buffer
[SMALL_STRING_LEN
];
1092 memcached_string_st
*string
;
1094 /* Ring the bell! */
1095 memset(buffer
, 6, SMALL_STRING_LEN
);
1097 string
= memcached_string_create(memc
, NULL
, 100);
1100 for (x
= 0; x
< 1024; x
++)
1102 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
1103 assert(rc
== MEMCACHED_SUCCESS
);
1105 rc
= memcached_string_append(string
, buffer
, INT64_MAX
);
1106 assert(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
1107 memcached_string_free(string
);
1112 uint8_t generate_data(memcached_st
*memc
)
1114 unsigned long long x
;
1115 global_pairs
= pairs_generate(GLOBAL_COUNT
);
1116 execute_set(memc
, global_pairs
, GLOBAL_COUNT
);
1118 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1120 global_keys
[x
]= global_pairs
[x
].key
;
1121 global_keys_length
[x
]= global_pairs
[x
].key_length
;
1127 uint8_t get_read(memcached_st
*memc
)
1130 memcached_return rc
;
1134 size_t return_value_length
;
1137 for (x
= 0; x
< GLOBAL_COUNT
; x
++)
1139 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
1140 &return_value_length
, &flags
, &rc
);
1142 assert(return_value);
1143 assert(rc == MEMCACHED_SUCCESS);
1145 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
1153 uint8_t mget_read(memcached_st
*memc
)
1155 memcached_return rc
;
1157 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1158 assert(rc
== MEMCACHED_SUCCESS
);
1159 /* Turn this into a help function */
1161 char return_key
[MEMCACHED_MAX_KEY
];
1162 size_t return_key_length
;
1164 size_t return_value_length
;
1167 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1168 &return_value_length
, &flags
, &rc
)))
1170 assert(return_value
);
1171 assert(rc
== MEMCACHED_SUCCESS
);
1179 uint8_t mget_read_result(memcached_st
*memc
)
1181 memcached_return rc
;
1183 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, GLOBAL_COUNT
);
1184 assert(rc
== MEMCACHED_SUCCESS
);
1185 /* Turn this into a help function */
1187 memcached_result_st results_obj
;
1188 memcached_result_st
*results
;
1190 results
= memcached_result_create(memc
, &results_obj
);
1192 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1195 assert(rc
== MEMCACHED_SUCCESS
);
1198 memcached_result_free(&results_obj
);
1204 uint8_t free_data(memcached_st
*memc
)
1206 pairs_free(global_pairs
);
1211 uint8_t add_host_test1(memcached_st
*memc
)
1214 memcached_return rc
;
1215 char servername
[]= "0.example.com";
1216 memcached_server_st
*servers
;
1218 servers
= memcached_server_list_append(NULL
, servername
, 400, &rc
);
1220 assert(1 == memcached_server_list_count(servers
));
1222 for (x
= 2; x
< 20; x
++)
1224 char buffer
[SMALL_STRING_LEN
];
1226 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1227 servers
= memcached_server_list_append(servers
, buffer
, 401,
1229 assert(rc
== MEMCACHED_SUCCESS
);
1230 assert(x
== memcached_server_list_count(servers
));
1233 rc
= memcached_server_push(memc
, servers
);
1234 assert(rc
== MEMCACHED_SUCCESS
);
1235 rc
= memcached_server_push(memc
, servers
);
1236 assert(rc
== MEMCACHED_SUCCESS
);
1238 memcached_server_list_free(servers
);
1243 memcached_return
pre_nonblock(memcached_st
*memc
)
1245 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1247 return MEMCACHED_SUCCESS
;
1250 memcached_return
pre_md5(memcached_st
*memc
)
1252 memcached_hash value
= MEMCACHED_HASH_MD5
;
1253 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1255 return MEMCACHED_SUCCESS
;
1258 memcached_return
pre_crc(memcached_st
*memc
)
1260 memcached_hash value
= MEMCACHED_HASH_CRC
;
1261 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1263 return MEMCACHED_SUCCESS
;
1266 memcached_return
pre_hash_fnv1_64(memcached_st
*memc
)
1268 memcached_hash value
= MEMCACHED_HASH_FNV1_64
;
1269 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1271 return MEMCACHED_SUCCESS
;
1274 memcached_return
pre_hash_fnv1a_64(memcached_st
*memc
)
1276 memcached_hash value
= MEMCACHED_HASH_FNV1A_64
;
1277 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1279 return MEMCACHED_SUCCESS
;
1282 memcached_return
pre_hash_fnv1_32(memcached_st
*memc
)
1284 memcached_hash value
= MEMCACHED_HASH_FNV1_32
;
1285 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1287 return MEMCACHED_SUCCESS
;
1290 memcached_return
pre_hash_fnv1a_32(memcached_st
*memc
)
1292 memcached_hash value
= MEMCACHED_HASH_FNV1A_32
;
1293 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1295 return MEMCACHED_SUCCESS
;
1298 memcached_return
pre_hash_ketama(memcached_st
*memc
)
1300 memcached_hash value
= MEMCACHED_HASH_KETAMA
;
1301 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, &value
);
1303 return MEMCACHED_SUCCESS
;
1306 memcached_return
pre_unix_socket(memcached_st
*memc
)
1308 memcached_return rc
;
1311 memcached_server_list_free(memc
->hosts
);
1313 memc
->number_of_hosts
= 0;
1315 if (stat("/tmp/memcached.socket", &buf
))
1316 return MEMCACHED_FAILURE
;
1318 rc
= memcached_server_add_unix_socket(memc
, "/tmp/memcached.socket");
1323 memcached_return
pre_nodelay(memcached_st
*memc
)
1325 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
1326 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, NULL
);
1328 return MEMCACHED_SUCCESS
;
1332 /* Clean the server before beginning testing */
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
},
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
},
1373 test_st result_tests
[] ={
1374 {"result static", 0, result_static
},
1375 {"result alloc", 0, result_alloc
},
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
},
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
},
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
},
1417 collection_st
*gets_collections(void)