Merged in new generate tests.
[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 unsigned int 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 unsigned int 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 uint8_t result_static(memcached_st *memc)
874 {
875 memcached_result_st result;
876 memcached_result_st *result_ptr;
877
878 result_ptr= memcached_result_create(memc, &result);
879 assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
880 assert(result_ptr);
881 memcached_result_free(&result);
882
883 return 0;
884 }
885
886 uint8_t result_alloc(memcached_st *memc)
887 {
888 memcached_result_st *result;
889
890 result= memcached_result_create(memc, NULL);
891 assert(result);
892 memcached_result_free(result);
893
894 return 0;
895 }
896
897 uint8_t string_static_null(memcached_st *memc)
898 {
899 memcached_string_st string;
900 memcached_string_st *string_ptr;
901
902 string_ptr= memcached_string_create(memc, &string, 0);
903 assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
904 assert(string_ptr);
905 memcached_string_free(&string);
906
907 return 0;
908 }
909
910 uint8_t string_alloc_null(memcached_st *memc)
911 {
912 memcached_string_st *string;
913
914 string= memcached_string_create(memc, NULL, 0);
915 assert(string);
916 memcached_string_free(string);
917
918 return 0;
919 }
920
921 uint8_t string_alloc_with_size(memcached_st *memc)
922 {
923 memcached_string_st *string;
924
925 string= memcached_string_create(memc, NULL, 1024);
926 assert(string);
927 memcached_string_free(string);
928
929 return 0;
930 }
931
932 uint8_t string_alloc_with_size_toobig(memcached_st *memc)
933 {
934 memcached_string_st *string;
935
936 string= memcached_string_create(memc, NULL, INT64_MAX);
937 assert(string == NULL);
938
939 return 0;
940 }
941
942 uint8_t string_alloc_append(memcached_st *memc)
943 {
944 unsigned int x;
945 char buffer[SMALL_STRING_LEN];
946 memcached_string_st *string;
947
948 /* Ring the bell! */
949 memset(buffer, 6, SMALL_STRING_LEN);
950
951 string= memcached_string_create(memc, NULL, 100);
952 assert(string);
953
954 for (x= 0; x < 1024; x++)
955 {
956 memcached_return rc;
957 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
958 assert(rc == MEMCACHED_SUCCESS);
959 }
960 memcached_string_free(string);
961
962 return 0;
963 }
964
965 uint8_t string_alloc_append_toobig(memcached_st *memc)
966 {
967 memcached_return rc;
968 unsigned int x;
969 char buffer[SMALL_STRING_LEN];
970 memcached_string_st *string;
971
972 /* Ring the bell! */
973 memset(buffer, 6, SMALL_STRING_LEN);
974
975 string= memcached_string_create(memc, NULL, 100);
976 assert(string);
977
978 for (x= 0; x < 1024; x++)
979 {
980 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
981 assert(rc == MEMCACHED_SUCCESS);
982 }
983 rc= memcached_string_append(string, buffer, INT64_MAX);
984 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
985 memcached_string_free(string);
986
987 return 0;
988 }
989
990 uint8_t generate_data(memcached_st *memc)
991 {
992 unsigned long long x;
993 global_pairs= pairs_generate(GLOBAL_COUNT);
994 execute_set(memc, global_pairs, GLOBAL_COUNT);
995
996 for (x= 0; x < GLOBAL_COUNT; x++)
997 {
998 global_keys[x]= global_pairs[x].key;
999 global_keys_length[x]= global_pairs[x].key_length;
1000 }
1001
1002 return 0;
1003 }
1004
1005 uint8_t get_read(memcached_st *memc)
1006 {
1007 unsigned int x;
1008 memcached_return rc;
1009
1010 {
1011 char *return_value;
1012 size_t return_value_length;
1013 uint16_t flags;
1014
1015 for (x= 0; x < GLOBAL_COUNT; x++)
1016 {
1017 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
1018 &return_value_length, &flags, &rc);
1019 /*
1020 assert(return_value);
1021 assert(rc == MEMCACHED_SUCCESS);
1022 */
1023 if (rc == MEMCACHED_SUCCESS && return_value)
1024 free(return_value);
1025 }
1026 }
1027
1028 return 0;
1029 }
1030
1031 uint8_t mget_read(memcached_st *memc)
1032 {
1033 memcached_return rc;
1034
1035 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1036 assert(rc == MEMCACHED_SUCCESS);
1037 /* Turn this into a help function */
1038 {
1039 char return_key[MEMCACHED_MAX_KEY];
1040 size_t return_key_length;
1041 char *return_value;
1042 size_t return_value_length;
1043 uint16_t flags;
1044
1045 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1046 &return_value_length, &flags, &rc)))
1047 {
1048 assert(return_value);
1049 assert(rc == MEMCACHED_SUCCESS);
1050 free(return_value);
1051 }
1052 }
1053
1054 return 0;
1055 }
1056
1057 uint8_t mget_read_result(memcached_st *memc)
1058 {
1059 memcached_return rc;
1060
1061 rc= memcached_mget(memc, global_keys, global_keys_length, GLOBAL_COUNT);
1062 assert(rc == MEMCACHED_SUCCESS);
1063 /* Turn this into a help function */
1064 {
1065 memcached_result_st results_obj;
1066 memcached_result_st *results;
1067
1068 results= memcached_result_create(memc, &results_obj);
1069
1070 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1071 {
1072 assert(results);
1073 assert(rc == MEMCACHED_SUCCESS);
1074 }
1075
1076 memcached_result_free(&results_obj);
1077 }
1078
1079 return 0;
1080 }
1081
1082 uint8_t free_data(memcached_st *memc)
1083 {
1084 pairs_free(global_pairs);
1085
1086 return 0;
1087 }
1088
1089 uint8_t add_host_test1(memcached_st *memc)
1090 {
1091 unsigned int x;
1092 memcached_return rc;
1093 char servername[]= "0.example.com";
1094 memcached_server_st *servers;
1095
1096 servers= memcached_server_list_append(NULL, servername, 400, &rc);
1097 assert(servers);
1098 assert(1 == memcached_server_list_count(servers));
1099
1100 for (x= 2; x < 20; x++)
1101 {
1102 char buffer[SMALL_STRING_LEN];
1103
1104 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1105 servers= memcached_server_list_append(servers, buffer, 401,
1106 &rc);
1107 assert(rc == MEMCACHED_SUCCESS);
1108 assert(x == memcached_server_list_count(servers));
1109 }
1110
1111 rc= memcached_server_push(memc, servers);
1112 assert(rc == MEMCACHED_SUCCESS);
1113 rc= memcached_server_push(memc, servers);
1114 assert(rc == MEMCACHED_SUCCESS);
1115
1116 memcached_server_list_free(servers);
1117
1118 return 0;
1119 }
1120
1121 memcached_return pre_nonblock(memcached_st *memc)
1122 {
1123 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1124
1125 return MEMCACHED_SUCCESS;
1126 }
1127
1128 memcached_return pre_md5(memcached_st *memc)
1129 {
1130 memcached_hash value= MEMCACHED_HASH_MD5;
1131 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1132
1133 return MEMCACHED_SUCCESS;
1134 }
1135
1136 memcached_return pre_crc(memcached_st *memc)
1137 {
1138 memcached_hash value= MEMCACHED_HASH_CRC;
1139 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1140
1141 return MEMCACHED_SUCCESS;
1142 }
1143
1144 memcached_return pre_hash_fnv1_64(memcached_st *memc)
1145 {
1146 memcached_hash value= MEMCACHED_HASH_FNV1_64;
1147 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1148
1149 return MEMCACHED_SUCCESS;
1150 }
1151
1152 memcached_return pre_hash_fnv1a_64(memcached_st *memc)
1153 {
1154 memcached_hash value= MEMCACHED_HASH_FNV1A_64;
1155 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1156
1157 return MEMCACHED_SUCCESS;
1158 }
1159
1160 memcached_return pre_hash_fnv1_32(memcached_st *memc)
1161 {
1162 memcached_hash value= MEMCACHED_HASH_FNV1_32;
1163 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1164
1165 return MEMCACHED_SUCCESS;
1166 }
1167
1168 memcached_return pre_hash_fnv1a_32(memcached_st *memc)
1169 {
1170 memcached_hash value= MEMCACHED_HASH_FNV1A_32;
1171 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1172
1173 return MEMCACHED_SUCCESS;
1174 }
1175
1176 memcached_return pre_hash_ketama(memcached_st *memc)
1177 {
1178 memcached_hash value= MEMCACHED_HASH_KETAMA;
1179 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
1180
1181 return MEMCACHED_SUCCESS;
1182 }
1183
1184 memcached_return pre_unix_socket(memcached_st *memc)
1185 {
1186 memcached_return rc;
1187 struct stat buf;
1188
1189 memcached_server_list_free(memc->hosts);
1190 memc->hosts= NULL;
1191 memc->number_of_hosts= 0;
1192
1193 if (stat("/tmp/memcached.socket", &buf))
1194 return MEMCACHED_FAILURE;
1195
1196 rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket");
1197
1198 return rc;
1199 }
1200
1201 memcached_return pre_nodelay(memcached_st *memc)
1202 {
1203 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
1204 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
1205
1206 return MEMCACHED_SUCCESS;
1207 }
1208
1209
1210 /* Clean the server before beginning testing */
1211 test_st tests[] ={
1212 {"flush", 0, flush_test },
1213 {"init", 0, init_test },
1214 {"allocation", 0, allocation_test },
1215 {"clone_test", 0, clone_test },
1216 {"error", 0, error_test },
1217 {"set", 0, set_test },
1218 {"set2", 0, set_test2 },
1219 {"set3", 0, set_test3 },
1220 {"add", 0, add_test },
1221 {"replace", 0, replace_test },
1222 {"delete", 1, delete_test },
1223 {"get", 1, get_test },
1224 {"get2", 0, get_test2 },
1225 {"get3", 0, get_test3 },
1226 {"get4", 0, get_test4 },
1227 {"stats_servername", 0, stats_servername_test },
1228 {"increment", 0, increment_test },
1229 {"decrement", 0, decrement_test },
1230 {"quit", 0, quit_test },
1231 {"mget", 1, mget_test },
1232 {"mget_result", 1, mget_result_test },
1233 {"mget_result_alloc", 1, mget_result_alloc_test },
1234 {"get_stats", 0, get_stats },
1235 {"add_host_test", 0, add_host_test },
1236 {"get_stats_keys", 0, get_stats_keys },
1237 {"behavior_test", 0, get_stats_keys },
1238 {0, 0, 0}
1239 };
1240
1241 test_st string_tests[] ={
1242 {"string static with null", 0, string_static_null },
1243 {"string alloc with null", 0, string_alloc_null },
1244 {"string alloc with 1K", 0, string_alloc_with_size },
1245 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
1246 {"string append", 0, string_alloc_append },
1247 {"string append failure (too big)", 0, string_alloc_append_toobig },
1248 {0, 0, 0}
1249 };
1250
1251 test_st result_tests[] ={
1252 {"result static", 0, result_static},
1253 {"result alloc", 0, result_alloc},
1254 {0, 0, 0}
1255 };
1256
1257 test_st user_tests[] ={
1258 {"user_supplied_bug1", 0, user_supplied_bug1 },
1259 {"user_supplied_bug2", 0, user_supplied_bug2 },
1260 {"user_supplied_bug3", 0, user_supplied_bug3 },
1261 {0, 0, 0}
1262 };
1263
1264 test_st generate_tests[] ={
1265 {"generate_data", 0, generate_data },
1266 {"get_read", 0, get_read },
1267 {"mget_read", 0, mget_read },
1268 {"mget_read_result", 0, mget_read_result },
1269 {0, 0, 0}
1270 };
1271
1272
1273 collection_st collection[] ={
1274 {"block", 0, 0, tests},
1275 {"nonblock", pre_nonblock, 0, tests},
1276 {"nodelay", pre_nodelay, 0, tests},
1277 {"md5", pre_md5, 0, tests},
1278 {"crc", pre_crc, 0, tests},
1279 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
1280 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
1281 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
1282 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
1283 {"ketama", pre_hash_ketama, 0, tests},
1284 {"unix_socket", pre_unix_socket, 0, tests},
1285 {"unix_socket_nodelay", pre_nodelay, 0, tests},
1286 {"string", 0, 0, string_tests},
1287 {"result", 0, 0, result_tests},
1288 {"user", 0, 0, user_tests},
1289 {"generate", 0, 0, generate_tests},
1290 {0, 0, 0, 0}
1291 };
1292
1293 collection_st *gets_collections(void)
1294 {
1295 return collection;
1296 }