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