Fixed binary mget so that it will always issue a NOOP after every
[awesomized/libmemcached] / tests / function.c
1 /*
2 Sample test application.
3 */
4
5 #include "libmemcached/common.h"
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <time.h>
16 #include "server.h"
17 #include "clients/generator.h"
18 #include "clients/execute.h"
19
20 #ifndef INT64_MAX
21 #define INT64_MAX LONG_MAX
22 #endif
23 #ifndef INT32_MAX
24 #define INT32_MAX INT_MAX
25 #endif
26
27
28 #include "test.h"
29
30 #ifdef HAVE_LIBMEMCACHEDUTIL
31 #include <pthread.h>
32 #include "libmemcached/memcached_util.h"
33 #endif
34
35 #define GLOBAL_COUNT 10000
36 #define GLOBAL2_COUNT 100
37 #define SERVERS_TO_CREATE 5
38 static uint32_t global_count;
39
40 static pairs_st *global_pairs;
41 static const char *global_keys[GLOBAL_COUNT];
42 static size_t global_keys_length[GLOBAL_COUNT];
43
44 static test_return init_test(memcached_st *not_used __attribute__((unused)))
45 {
46 memcached_st memc;
47
48 (void)memcached_create(&memc);
49 memcached_free(&memc);
50
51 return 0;
52 }
53
54 static test_return server_list_null_test(memcached_st *ptr __attribute__((unused)))
55 {
56 memcached_server_st *server_list;
57 memcached_return rc;
58
59 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, NULL);
60 assert(server_list == NULL);
61
62 server_list= memcached_server_list_append_with_weight(NULL, "localhost", 0, 0, NULL);
63 assert(server_list == NULL);
64
65 server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, &rc);
66 assert(server_list == NULL);
67
68 return 0;
69 }
70
71 #define TEST_PORT_COUNT 7
72 uint32_t test_ports[TEST_PORT_COUNT];
73
74 static memcached_return server_display_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
75 {
76 /* Do Nothing */
77 uint32_t bigger= *((uint32_t *)(context));
78 assert(bigger <= server->port);
79 *((uint32_t *)(context))= server->port;
80
81 return MEMCACHED_SUCCESS;
82 }
83
84 static test_return server_sort_test(memcached_st *ptr __attribute__((unused)))
85 {
86 uint32_t x;
87 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
88 memcached_return rc;
89 memcached_server_function callbacks[1];
90 memcached_st *local_memc;
91
92 local_memc= memcached_create(NULL);
93 assert(local_memc);
94 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
95
96 for (x= 0; x < TEST_PORT_COUNT; x++)
97 {
98 test_ports[x]= (uint32_t)random() % 64000;
99 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
100 assert(local_memc->number_of_hosts == x + 1);
101 assert(local_memc->hosts[0].count == x+1);
102 assert(rc == MEMCACHED_SUCCESS);
103 }
104
105 callbacks[0]= server_display_function;
106 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
107
108
109 memcached_free(local_memc);
110
111 return 0;
112 }
113
114 static test_return server_sort2_test(memcached_st *ptr __attribute__((unused)))
115 {
116 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
117 memcached_return rc;
118 memcached_server_function callbacks[1];
119 memcached_st *local_memc;
120
121 local_memc= memcached_create(NULL);
122 assert(local_memc);
123 rc= memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
124 assert(rc == MEMCACHED_SUCCESS);
125
126 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
127 assert(rc == MEMCACHED_SUCCESS);
128 assert(local_memc->hosts[0].port == 43043);
129
130 rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
131 assert(rc == MEMCACHED_SUCCESS);
132 assert(local_memc->hosts[0].port == 43042);
133 assert(local_memc->hosts[1].port == 43043);
134
135 callbacks[0]= server_display_function;
136 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
137
138
139 memcached_free(local_memc);
140
141 return 0;
142 }
143
144 static memcached_return server_display_unsort_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
145 {
146 /* Do Nothing */
147 uint32_t x= *((uint32_t *)(context));
148
149 assert(test_ports[x] == server->port);
150 *((uint32_t *)(context))= ++x;
151
152 return MEMCACHED_SUCCESS;
153 }
154
155 static test_return server_unsort_test(memcached_st *ptr __attribute__((unused)))
156 {
157 uint32_t x;
158 uint32_t counter= 0; /* Prime the value for the assert in server_display_function */
159 uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */
160 memcached_return rc;
161 memcached_server_function callbacks[1];
162 memcached_st *local_memc;
163
164 local_memc= memcached_create(NULL);
165 assert(local_memc);
166
167 for (x= 0; x < TEST_PORT_COUNT; x++)
168 {
169 test_ports[x]= (uint32_t)(random() % 64000);
170 rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
171 assert(local_memc->number_of_hosts == x+1);
172 assert(local_memc->hosts[0].count == x+1);
173 assert(rc == MEMCACHED_SUCCESS);
174 }
175
176 callbacks[0]= server_display_unsort_function;
177 memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1);
178
179 /* Now we sort old data! */
180 memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
181 callbacks[0]= server_display_function;
182 memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
183
184
185 memcached_free(local_memc);
186
187 return 0;
188 }
189
190 static test_return allocation_test(memcached_st *not_used __attribute__((unused)))
191 {
192 memcached_st *memc;
193 memc= memcached_create(NULL);
194 assert(memc);
195 memcached_free(memc);
196
197 return 0;
198 }
199
200 static test_return clone_test(memcached_st *memc)
201 {
202 /* All null? */
203 {
204 memcached_st *memc_clone;
205 memc_clone= memcached_clone(NULL, NULL);
206 assert(memc_clone);
207 memcached_free(memc_clone);
208 }
209
210 /* Can we init from null? */
211 {
212 memcached_st *memc_clone;
213 memc_clone= memcached_clone(NULL, memc);
214 assert(memc_clone);
215
216 assert(memc_clone->call_free == memc->call_free);
217 assert(memc_clone->call_malloc == memc->call_malloc);
218 assert(memc_clone->call_realloc == memc->call_realloc);
219 assert(memc_clone->call_calloc == memc->call_calloc);
220 assert(memc_clone->connect_timeout == memc->connect_timeout);
221 assert(memc_clone->delete_trigger == memc->delete_trigger);
222 assert(memc_clone->distribution == memc->distribution);
223 assert(memc_clone->flags == memc->flags);
224 assert(memc_clone->get_key_failure == memc->get_key_failure);
225 assert(memc_clone->hash == memc->hash);
226 assert(memc_clone->hash_continuum == memc->hash_continuum);
227 assert(memc_clone->io_bytes_watermark == memc->io_bytes_watermark);
228 assert(memc_clone->io_msg_watermark == memc->io_msg_watermark);
229 assert(memc_clone->io_key_prefetch == memc->io_key_prefetch);
230 assert(memc_clone->on_cleanup == memc->on_cleanup);
231 assert(memc_clone->on_clone == memc->on_clone);
232 assert(memc_clone->poll_timeout == memc->poll_timeout);
233 assert(memc_clone->rcv_timeout == memc->rcv_timeout);
234 assert(memc_clone->recv_size == memc->recv_size);
235 assert(memc_clone->retry_timeout == memc->retry_timeout);
236 assert(memc_clone->send_size == memc->send_size);
237 assert(memc_clone->server_failure_limit == memc->server_failure_limit);
238 assert(memc_clone->snd_timeout == memc->snd_timeout);
239 assert(memc_clone->user_data == memc->user_data);
240
241 memcached_free(memc_clone);
242 }
243
244 /* Can we init from struct? */
245 {
246 memcached_st declared_clone;
247 memcached_st *memc_clone;
248 memset(&declared_clone, 0 , sizeof(memcached_st));
249 memc_clone= memcached_clone(&declared_clone, NULL);
250 assert(memc_clone);
251 memcached_free(memc_clone);
252 }
253
254 /* Can we init from struct? */
255 {
256 memcached_st declared_clone;
257 memcached_st *memc_clone;
258 memset(&declared_clone, 0 , sizeof(memcached_st));
259 memc_clone= memcached_clone(&declared_clone, memc);
260 assert(memc_clone);
261 memcached_free(memc_clone);
262 }
263
264 return 0;
265 }
266
267 static test_return userdata_test(memcached_st *memc)
268 {
269 void* foo= NULL;
270 assert(memcached_set_user_data(memc, foo) == NULL);
271 assert(memcached_get_user_data(memc) == foo);
272 assert(memcached_set_user_data(memc, NULL) == foo);
273
274 return TEST_SUCCESS;
275 }
276
277 static test_return connection_test(memcached_st *memc)
278 {
279 memcached_return rc;
280
281 rc= memcached_server_add_with_weight(memc, "localhost", 0, 0);
282 assert(rc == MEMCACHED_SUCCESS);
283
284 return 0;
285 }
286
287 static test_return error_test(memcached_st *memc)
288 {
289 memcached_return rc;
290 uint32_t values[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U, 982370485U, 1263635348U, 4242906218U, 3829656100U, 1891735253U,
291 334139633U, 2257084983U, 3088286104U, 13199785U, 2542027183U, 1097051614U, 199566778U, 2748246961U, 2465192557U,
292 1664094137U, 2405439045U, 1842224848U, 692413798U, 3479807801U, 919913813U, 4269430871U, 610793021U, 527273862U,
293 1437122909U, 2300930706U, 2943759320U, 674306647U, 2400528935U, 54481931U, 4186304426U, 1741088401U, 2979625118U,
294 4159057246U };
295
296 assert(MEMCACHED_MAXIMUM_RETURN == 37); // You have updated the memcache_error messages but not updated docs/tests.
297 for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
298 {
299 uint32_t hash_val;
300 hash_val= memcached_generate_hash_value(memcached_strerror(memc, rc), strlen(memcached_strerror(memc, rc)), MEMCACHED_HASH_JENKINS);
301 assert(values[rc] == hash_val);
302 }
303
304 return 0;
305 }
306
307 static test_return set_test(memcached_st *memc)
308 {
309 memcached_return rc;
310 const char *key= "foo";
311 const char *value= "when we sanitize";
312
313 rc= memcached_set(memc, key, strlen(key),
314 value, strlen(value),
315 (time_t)0, (uint32_t)0);
316 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
317
318 return 0;
319 }
320
321 static test_return append_test(memcached_st *memc)
322 {
323 memcached_return rc;
324 const char *key= "fig";
325 const char *in_value= "we";
326 char *out_value= NULL;
327 size_t value_length;
328 uint32_t flags;
329
330 rc= memcached_flush(memc, 0);
331 assert(rc == MEMCACHED_SUCCESS);
332
333 rc= memcached_set(memc, key, strlen(key),
334 in_value, strlen(in_value),
335 (time_t)0, (uint32_t)0);
336 assert(rc == MEMCACHED_SUCCESS);
337
338 rc= memcached_append(memc, key, strlen(key),
339 " the", strlen(" the"),
340 (time_t)0, (uint32_t)0);
341 assert(rc == MEMCACHED_SUCCESS);
342
343 rc= memcached_append(memc, key, strlen(key),
344 " people", strlen(" people"),
345 (time_t)0, (uint32_t)0);
346 assert(rc == MEMCACHED_SUCCESS);
347
348 out_value= memcached_get(memc, key, strlen(key),
349 &value_length, &flags, &rc);
350 assert(!memcmp(out_value, "we the people", strlen("we the people")));
351 assert(strlen("we the people") == value_length);
352 assert(rc == MEMCACHED_SUCCESS);
353 free(out_value);
354
355 return 0;
356 }
357
358 static test_return append_binary_test(memcached_st *memc)
359 {
360 memcached_return rc;
361 const char *key= "numbers";
362 unsigned int *store_ptr;
363 unsigned int store_list[] = { 23, 56, 499, 98, 32847, 0 };
364 char *value;
365 size_t value_length;
366 uint32_t flags;
367 unsigned int x;
368
369 rc= memcached_flush(memc, 0);
370 assert(rc == MEMCACHED_SUCCESS);
371
372 rc= memcached_set(memc,
373 key, strlen(key),
374 NULL, 0,
375 (time_t)0, (uint32_t)0);
376 assert(rc == MEMCACHED_SUCCESS);
377
378 for (x= 0; store_list[x] ; x++)
379 {
380 rc= memcached_append(memc,
381 key, strlen(key),
382 (char *)&store_list[x], sizeof(unsigned int),
383 (time_t)0, (uint32_t)0);
384 assert(rc == MEMCACHED_SUCCESS);
385 }
386
387 value= memcached_get(memc, key, strlen(key),
388 &value_length, &flags, &rc);
389 assert((value_length == (sizeof(unsigned int) * x)));
390 assert(rc == MEMCACHED_SUCCESS);
391
392 store_ptr= (unsigned int *)value;
393 x= 0;
394 while ((size_t)store_ptr < (size_t)(value + value_length))
395 {
396 assert(*store_ptr == store_list[x++]);
397 store_ptr++;
398 }
399 free(value);
400
401 return 0;
402 }
403
404 static test_return cas2_test(memcached_st *memc)
405 {
406 memcached_return rc;
407 const char *keys[]= {"fudge", "son", "food"};
408 size_t key_length[]= {5, 3, 4};
409 const char *value= "we the people";
410 size_t value_length= strlen("we the people");
411 unsigned int x;
412 memcached_result_st results_obj;
413 memcached_result_st *results;
414 unsigned int set= 1;
415
416 rc= memcached_flush(memc, 0);
417 assert(rc == MEMCACHED_SUCCESS);
418
419 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
420
421 for (x= 0; x < 3; x++)
422 {
423 rc= memcached_set(memc, keys[x], key_length[x],
424 keys[x], key_length[x],
425 (time_t)50, (uint32_t)9);
426 assert(rc == MEMCACHED_SUCCESS);
427 }
428
429 rc= memcached_mget(memc, keys, key_length, 3);
430
431 results= memcached_result_create(memc, &results_obj);
432
433 results= memcached_fetch_result(memc, &results_obj, &rc);
434 assert(results);
435 assert(results->cas);
436 assert(rc == MEMCACHED_SUCCESS);
437 assert(memcached_result_cas(results));
438
439 assert(!memcmp(value, "we the people", strlen("we the people")));
440 assert(strlen("we the people") == value_length);
441 assert(rc == MEMCACHED_SUCCESS);
442
443 memcached_result_free(&results_obj);
444
445 return 0;
446 }
447
448 static test_return cas_test(memcached_st *memc)
449 {
450 memcached_return rc;
451 const char *key= "fun";
452 size_t key_length= strlen(key);
453 const char *value= "we the people";
454 const char* keys[2] = { key, NULL };
455 size_t keylengths[2] = { strlen(key), 0 };
456 size_t value_length= strlen(value);
457 const char *value2= "change the value";
458 size_t value2_length= strlen(value2);
459
460 memcached_result_st results_obj;
461 memcached_result_st *results;
462 unsigned int set= 1;
463
464 rc= memcached_flush(memc, 0);
465 assert(rc == MEMCACHED_SUCCESS);
466
467 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
468
469 rc= memcached_set(memc, key, strlen(key),
470 value, strlen(value),
471 (time_t)0, (uint32_t)0);
472 assert(rc == MEMCACHED_SUCCESS);
473
474 rc= memcached_mget(memc, keys, keylengths, 1);
475
476 results= memcached_result_create(memc, &results_obj);
477
478 results= memcached_fetch_result(memc, &results_obj, &rc);
479 assert(results);
480 assert(rc == MEMCACHED_SUCCESS);
481 assert(memcached_result_cas(results));
482 assert(!memcmp(value, memcached_result_value(results), value_length));
483 assert(strlen(memcached_result_value(results)) == value_length);
484 assert(rc == MEMCACHED_SUCCESS);
485 uint64_t cas = memcached_result_cas(results);
486
487 #if 0
488 results= memcached_fetch_result(memc, &results_obj, &rc);
489 assert(rc == MEMCACHED_END);
490 assert(results == NULL);
491 #endif
492
493 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
494 assert(rc == MEMCACHED_SUCCESS);
495
496 /*
497 * The item will have a new cas value, so try to set it again with the old
498 * value. This should fail!
499 */
500 rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
501 assert(rc == MEMCACHED_DATA_EXISTS);
502
503 memcached_result_free(&results_obj);
504
505 return 0;
506 }
507
508 static test_return prepend_test(memcached_st *memc)
509 {
510 memcached_return rc;
511 const char *key= "fig";
512 const char *value= "people";
513 char *out_value= NULL;
514 size_t value_length;
515 uint32_t flags;
516
517 rc= memcached_flush(memc, 0);
518 assert(rc == MEMCACHED_SUCCESS);
519
520 rc= memcached_set(memc, key, strlen(key),
521 value, strlen(value),
522 (time_t)0, (uint32_t)0);
523 assert(rc == MEMCACHED_SUCCESS);
524
525 rc= memcached_prepend(memc, key, strlen(key),
526 "the ", strlen("the "),
527 (time_t)0, (uint32_t)0);
528 assert(rc == MEMCACHED_SUCCESS);
529
530 rc= memcached_prepend(memc, key, strlen(key),
531 "we ", strlen("we "),
532 (time_t)0, (uint32_t)0);
533 assert(rc == MEMCACHED_SUCCESS);
534
535 out_value= memcached_get(memc, key, strlen(key),
536 &value_length, &flags, &rc);
537 assert(!memcmp(out_value, "we the people", strlen("we the people")));
538 assert(strlen("we the people") == value_length);
539 assert(rc == MEMCACHED_SUCCESS);
540 free(out_value);
541
542 return 0;
543 }
544
545 /*
546 Set the value, then quit to make sure it is flushed.
547 Come back in and test that add fails.
548 */
549 static test_return add_test(memcached_st *memc)
550 {
551 memcached_return rc;
552 const char *key= "foo";
553 const char *value= "when we sanitize";
554 unsigned long long setting_value;
555
556 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
557
558 rc= memcached_set(memc, key, strlen(key),
559 value, strlen(value),
560 (time_t)0, (uint32_t)0);
561 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
562 memcached_quit(memc);
563 rc= memcached_add(memc, key, strlen(key),
564 value, strlen(value),
565 (time_t)0, (uint32_t)0);
566
567 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
568 if (setting_value)
569 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
570 else
571 assert(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_DATA_EXISTS);
572
573 return 0;
574 }
575
576 /*
577 ** There was a problem of leaking filedescriptors in the initial release
578 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
579 ** systems it seems that the kernel is slow on reclaiming the resources
580 ** because the connects starts to time out (the test doesn't do much
581 ** anyway, so just loop 10 iterations)
582 */
583 static test_return add_wrapper(memcached_st *memc)
584 {
585 unsigned int x;
586 unsigned int max= 10000;
587 #ifdef __sun
588 max= 10;
589 #endif
590
591 for (x= 0; x < max; x++)
592 add_test(memc);
593
594 return 0;
595 }
596
597 static test_return replace_test(memcached_st *memc)
598 {
599 memcached_return rc;
600 const char *key= "foo";
601 const char *value= "when we sanitize";
602 const char *original= "first we insert some data";
603
604 rc= memcached_set(memc, key, strlen(key),
605 original, strlen(original),
606 (time_t)0, (uint32_t)0);
607 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
608
609 rc= memcached_replace(memc, key, strlen(key),
610 value, strlen(value),
611 (time_t)0, (uint32_t)0);
612 assert(rc == MEMCACHED_SUCCESS);
613
614 return 0;
615 }
616
617 static test_return delete_test(memcached_st *memc)
618 {
619 memcached_return rc;
620 const char *key= "foo";
621 const char *value= "when we sanitize";
622
623 rc= memcached_set(memc, key, strlen(key),
624 value, strlen(value),
625 (time_t)0, (uint32_t)0);
626 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
627
628 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
629 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
630
631 return 0;
632 }
633
634 static test_return flush_test(memcached_st *memc)
635 {
636 memcached_return rc;
637
638 rc= memcached_flush(memc, 0);
639 assert(rc == MEMCACHED_SUCCESS);
640
641 return 0;
642 }
643
644 static memcached_return server_function(memcached_st *ptr __attribute__((unused)),
645 memcached_server_st *server __attribute__((unused)),
646 void *context __attribute__((unused)))
647 {
648 /* Do Nothing */
649
650 return MEMCACHED_SUCCESS;
651 }
652
653 static test_return memcached_server_cursor_test(memcached_st *memc)
654 {
655 char context[8];
656 strcpy(context, "foo bad");
657 memcached_server_function callbacks[1];
658
659 callbacks[0]= server_function;
660 memcached_server_cursor(memc, callbacks, context, 1);
661 return 0;
662 }
663
664 static test_return bad_key_test(memcached_st *memc)
665 {
666 memcached_return rc;
667 const char *key= "foo bad";
668 char *string;
669 size_t string_length;
670 uint32_t flags;
671 memcached_st *memc_clone;
672 unsigned int set= 1;
673 size_t max_keylen= 0xffff;
674
675 memc_clone= memcached_clone(NULL, memc);
676 assert(memc_clone);
677
678 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
679 assert(rc == MEMCACHED_SUCCESS);
680
681 /* All keys are valid in the binary protocol (except for length) */
682 if (memcached_behavior_get(memc_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0)
683 {
684 string= memcached_get(memc_clone, key, strlen(key),
685 &string_length, &flags, &rc);
686 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
687 assert(string_length == 0);
688 assert(!string);
689
690 set= 0;
691 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
692 assert(rc == MEMCACHED_SUCCESS);
693 string= memcached_get(memc_clone, key, strlen(key),
694 &string_length, &flags, &rc);
695 assert(rc == MEMCACHED_NOTFOUND);
696 assert(string_length == 0);
697 assert(!string);
698
699 /* Test multi key for bad keys */
700 const char *keys[] = { "GoodKey", "Bad Key", "NotMine" };
701 size_t key_lengths[] = { 7, 7, 7 };
702 set= 1;
703 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
704 assert(rc == MEMCACHED_SUCCESS);
705
706 rc= memcached_mget(memc_clone, keys, key_lengths, 3);
707 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
708
709 rc= memcached_mget_by_key(memc_clone, "foo daddy", 9, keys, key_lengths, 1);
710 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
711
712 max_keylen= 250;
713
714 /* The following test should be moved to the end of this function when the
715 memcached server is updated to allow max size length of the keys in the
716 binary protocol
717 */
718 rc= memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
719 assert(rc == MEMCACHED_SUCCESS);
720
721 char *longkey= malloc(max_keylen + 1);
722 if (longkey != NULL)
723 {
724 memset(longkey, 'a', max_keylen + 1);
725 string= memcached_get(memc_clone, longkey, max_keylen,
726 &string_length, &flags, &rc);
727 assert(rc == MEMCACHED_NOTFOUND);
728 assert(string_length == 0);
729 assert(!string);
730
731 string= memcached_get(memc_clone, longkey, max_keylen + 1,
732 &string_length, &flags, &rc);
733 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
734 assert(string_length == 0);
735 assert(!string);
736
737 free(longkey);
738 }
739 }
740
741 /* Make sure zero length keys are marked as bad */
742 set= 1;
743 rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
744 assert(rc == MEMCACHED_SUCCESS);
745 string= memcached_get(memc_clone, key, 0,
746 &string_length, &flags, &rc);
747 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
748 assert(string_length == 0);
749 assert(!string);
750
751 memcached_free(memc_clone);
752
753 return 0;
754 }
755
756 #define READ_THROUGH_VALUE "set for me"
757 static memcached_return read_through_trigger(memcached_st *memc __attribute__((unused)),
758 char *key __attribute__((unused)),
759 size_t key_length __attribute__((unused)),
760 memcached_result_st *result)
761 {
762
763 return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
764 }
765
766 static test_return read_through(memcached_st *memc)
767 {
768 memcached_return rc;
769 const char *key= "foo";
770 char *string;
771 size_t string_length;
772 uint32_t flags;
773 memcached_trigger_key cb= (memcached_trigger_key)read_through_trigger;
774
775 string= memcached_get(memc, key, strlen(key),
776 &string_length, &flags, &rc);
777
778 assert(rc == MEMCACHED_NOTFOUND);
779 assert(string_length == 0);
780 assert(!string);
781
782 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE,
783 *(void **)&cb);
784 assert(rc == MEMCACHED_SUCCESS);
785
786 string= memcached_get(memc, key, strlen(key),
787 &string_length, &flags, &rc);
788
789 assert(rc == MEMCACHED_SUCCESS);
790 assert(string_length == strlen(READ_THROUGH_VALUE));
791 assert(!strcmp(READ_THROUGH_VALUE, string));
792 free(string);
793
794 string= memcached_get(memc, key, strlen(key),
795 &string_length, &flags, &rc);
796
797 assert(rc == MEMCACHED_SUCCESS);
798 assert(string_length == strlen(READ_THROUGH_VALUE));
799 assert(!strcmp(READ_THROUGH_VALUE, string));
800 free(string);
801
802 return 0;
803 }
804
805 static memcached_return delete_trigger(memcached_st *ptr __attribute__((unused)),
806 const char *key,
807 size_t key_length __attribute__((unused)))
808 {
809 assert(key);
810
811 return MEMCACHED_SUCCESS;
812 }
813
814 static test_return delete_through(memcached_st *memc)
815 {
816 memcached_trigger_delete_key callback;
817 memcached_return rc;
818
819 callback= (memcached_trigger_delete_key)delete_trigger;
820
821 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, *(void**)&callback);
822 assert(rc == MEMCACHED_SUCCESS);
823
824 return 0;
825 }
826
827 static test_return get_test(memcached_st *memc)
828 {
829 memcached_return rc;
830 const char *key= "foo";
831 char *string;
832 size_t string_length;
833 uint32_t flags;
834
835 rc= memcached_delete(memc, key, strlen(key), (time_t)0);
836 assert(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
837
838 string= memcached_get(memc, key, strlen(key),
839 &string_length, &flags, &rc);
840
841 assert(rc == MEMCACHED_NOTFOUND);
842 assert(string_length == 0);
843 assert(!string);
844
845 return 0;
846 }
847
848 static test_return get_test2(memcached_st *memc)
849 {
850 memcached_return rc;
851 const char *key= "foo";
852 const char *value= "when we sanitize";
853 char *string;
854 size_t string_length;
855 uint32_t flags;
856
857 rc= memcached_set(memc, key, strlen(key),
858 value, strlen(value),
859 (time_t)0, (uint32_t)0);
860 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
861
862 string= memcached_get(memc, key, strlen(key),
863 &string_length, &flags, &rc);
864
865 assert(string);
866 assert(rc == MEMCACHED_SUCCESS);
867 assert(string_length == strlen(value));
868 assert(!memcmp(string, value, string_length));
869
870 free(string);
871
872 return 0;
873 }
874
875 static test_return set_test2(memcached_st *memc)
876 {
877 memcached_return rc;
878 const char *key= "foo";
879 const char *value= "train in the brain";
880 size_t value_length= strlen(value);
881 unsigned int x;
882
883 for (x= 0; x < 10; x++)
884 {
885 rc= memcached_set(memc, key, strlen(key),
886 value, value_length,
887 (time_t)0, (uint32_t)0);
888 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
889 }
890
891 return 0;
892 }
893
894 static test_return set_test3(memcached_st *memc)
895 {
896 memcached_return rc;
897 char *value;
898 size_t value_length= 8191;
899 unsigned int x;
900
901 value = (char*)malloc(value_length);
902 assert(value);
903
904 for (x= 0; x < value_length; x++)
905 value[x] = (char) (x % 127);
906
907 /* The dump test relies on there being at least 32 items in memcached */
908 for (x= 0; x < 32; x++)
909 {
910 char key[16];
911
912 sprintf(key, "foo%u", x);
913
914 rc= memcached_set(memc, key, strlen(key),
915 value, value_length,
916 (time_t)0, (uint32_t)0);
917 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
918 }
919
920 free(value);
921
922 return 0;
923 }
924
925 static test_return get_test3(memcached_st *memc)
926 {
927 memcached_return rc;
928 const 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 string= memcached_get(memc, key, strlen(key),
948 &string_length, &flags, &rc);
949
950 assert(rc == MEMCACHED_SUCCESS);
951 assert(string);
952 assert(string_length == value_length);
953 assert(!memcmp(string, value, string_length));
954
955 free(string);
956 free(value);
957
958 return 0;
959 }
960
961 static test_return get_test4(memcached_st *memc)
962 {
963 memcached_return rc;
964 const char *key= "foo";
965 char *value;
966 size_t value_length= 8191;
967 char *string;
968 size_t string_length;
969 uint32_t flags;
970 uint32_t x;
971
972 value = (char*)malloc(value_length);
973 assert(value);
974
975 for (x= 0; x < value_length; x++)
976 value[x] = (char) (x % 127);
977
978 rc= memcached_set(memc, key, strlen(key),
979 value, value_length,
980 (time_t)0, (uint32_t)0);
981 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
982
983 for (x= 0; x < 10; x++)
984 {
985 string= memcached_get(memc, key, strlen(key),
986 &string_length, &flags, &rc);
987
988 assert(rc == MEMCACHED_SUCCESS);
989 assert(string);
990 assert(string_length == value_length);
991 assert(!memcmp(string, value, string_length));
992 free(string);
993 }
994
995 free(value);
996
997 return 0;
998 }
999
1000 /*
1001 * This test verifies that memcached_read_one_response doesn't try to
1002 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1003 * responses before you execute a storage command.
1004 */
1005 static test_return get_test5(memcached_st *memc)
1006 {
1007 /*
1008 ** Request the same key twice, to ensure that we hash to the same server
1009 ** (so that we have multiple response values queued up) ;-)
1010 */
1011 const char *keys[]= { "key", "key" };
1012 size_t lengths[]= { 3, 3 };
1013 uint32_t flags;
1014 size_t rlen;
1015
1016 memcached_return rc= memcached_set(memc, keys[0], lengths[0],
1017 keys[0], lengths[0], 0, 0);
1018 assert(rc == MEMCACHED_SUCCESS);
1019 rc= memcached_mget(memc, keys, lengths, 2);
1020
1021 memcached_result_st results_obj;
1022 memcached_result_st *results;
1023 results=memcached_result_create(memc, &results_obj);
1024 assert(results);
1025 results=memcached_fetch_result(memc, &results_obj, &rc);
1026 assert(results);
1027 memcached_result_free(&results_obj);
1028
1029 /* Don't read out the second result, but issue a set instead.. */
1030 rc= memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0);
1031 assert(rc == MEMCACHED_SUCCESS);
1032
1033 char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
1034 &rlen, &flags, &rc);
1035 assert(val == NULL);
1036 assert(rc == MEMCACHED_NOTFOUND);
1037 val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
1038 assert(val != NULL);
1039 assert(rc == MEMCACHED_SUCCESS);
1040 free(val);
1041
1042 return TEST_SUCCESS;
1043 }
1044
1045 static test_return mget_end(memcached_st *memc)
1046 {
1047 const char *keys[] = { "foo", "foo2" };
1048 size_t lengths[] = { 3, 4 };
1049 const char *values[] = { "fjord", "41" };
1050
1051 memcached_return rc;
1052
1053 // Set foo and foo2
1054 for(int i = 0; i < 2; i++)
1055 {
1056 rc= memcached_set(memc, keys[i], lengths[i], values[i], strlen(values[i]),
1057 (time_t)0, (uint32_t)0);
1058 assert(rc == MEMCACHED_SUCCESS);
1059 }
1060
1061 char *string;
1062 size_t string_length;
1063 uint32_t flags;
1064
1065 // retrieve both via mget
1066 rc= memcached_mget(memc, keys, lengths, 2);
1067 assert(rc == MEMCACHED_SUCCESS);
1068
1069 char key[MEMCACHED_MAX_KEY];
1070 size_t key_length;
1071
1072 // this should get both
1073 for(int i = 0; i < 2; i++) {
1074 string = memcached_fetch(memc, key, &key_length, &string_length,
1075 &flags, &rc);
1076 assert(rc == MEMCACHED_SUCCESS);
1077 int val = 0;
1078 if(key_length == 4)
1079 val = 1;
1080 assert(string_length == strlen(values[val]));
1081 assert(strncmp(values[val], string, string_length) == 0);
1082 free(string);
1083 }
1084
1085 // this should indicate end
1086 string = memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
1087 assert(rc == MEMCACHED_END);
1088
1089 // now get just one
1090 rc= memcached_mget(memc, keys, lengths, 1);
1091 assert(rc == MEMCACHED_SUCCESS);
1092
1093 string = memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
1094 assert(key_length == lengths[0]);
1095 assert(strncmp(keys[0], key, key_length) == 0);
1096 assert(string_length == strlen(values[0]));
1097 assert(strncmp(values[0], string, string_length) == 0);
1098 assert(rc == MEMCACHED_SUCCESS);
1099 free(string);
1100
1101 // this should indicate end
1102 string = memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
1103 assert(rc != MEMCACHED_SUCCESS);
1104 assert(rc == MEMCACHED_END);
1105
1106 return TEST_SUCCESS;
1107 }
1108
1109 /* Do not copy the style of this code, I just access hosts to testthis function */
1110 static test_return stats_servername_test(memcached_st *memc)
1111 {
1112 memcached_return rc;
1113 memcached_stat_st memc_stat;
1114 rc= memcached_stat_servername(&memc_stat, NULL,
1115 memc->hosts[0].hostname,
1116 memc->hosts[0].port);
1117
1118 return 0;
1119 }
1120
1121 static test_return increment_test(memcached_st *memc)
1122 {
1123 uint64_t new_number;
1124 memcached_return rc;
1125 const char *key= "number";
1126 const char *value= "0";
1127
1128 rc= memcached_set(memc, key, strlen(key),
1129 value, strlen(value),
1130 (time_t)0, (uint32_t)0);
1131 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1132
1133 rc= memcached_increment(memc, key, strlen(key),
1134 1, &new_number);
1135 assert(rc == MEMCACHED_SUCCESS);
1136 assert(new_number == 1);
1137
1138 rc= memcached_increment(memc, key, strlen(key),
1139 1, &new_number);
1140 assert(rc == MEMCACHED_SUCCESS);
1141 assert(new_number == 2);
1142
1143 return 0;
1144 }
1145
1146 static test_return increment_with_initial_test(memcached_st *memc)
1147 {
1148 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1149 {
1150 uint64_t new_number;
1151 memcached_return rc;
1152 const char *key= "number";
1153 uint64_t initial= 0;
1154
1155 rc= memcached_increment_with_initial(memc, key, strlen(key),
1156 1, initial, 0, &new_number);
1157 assert(rc == MEMCACHED_SUCCESS);
1158 assert(new_number == initial);
1159
1160 rc= memcached_increment_with_initial(memc, key, strlen(key),
1161 1, initial, 0, &new_number);
1162 assert(rc == MEMCACHED_SUCCESS);
1163 assert(new_number == (initial + 1));
1164 }
1165 return 0;
1166 }
1167
1168 static test_return decrement_test(memcached_st *memc)
1169 {
1170 uint64_t new_number;
1171 memcached_return rc;
1172 const char *key= "number";
1173 const char *value= "3";
1174
1175 rc= memcached_set(memc, key, strlen(key),
1176 value, strlen(value),
1177 (time_t)0, (uint32_t)0);
1178 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1179
1180 rc= memcached_decrement(memc, key, strlen(key),
1181 1, &new_number);
1182 assert(rc == MEMCACHED_SUCCESS);
1183 assert(new_number == 2);
1184
1185 rc= memcached_decrement(memc, key, strlen(key),
1186 1, &new_number);
1187 assert(rc == MEMCACHED_SUCCESS);
1188 assert(new_number == 1);
1189
1190 return 0;
1191 }
1192
1193 static test_return decrement_with_initial_test(memcached_st *memc)
1194 {
1195 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
1196 {
1197 uint64_t new_number;
1198 memcached_return rc;
1199 const char *key= "number";
1200 uint64_t initial= 3;
1201
1202 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1203 1, initial, 0, &new_number);
1204 assert(rc == MEMCACHED_SUCCESS);
1205 assert(new_number == initial);
1206
1207 rc= memcached_decrement_with_initial(memc, key, strlen(key),
1208 1, initial, 0, &new_number);
1209 assert(rc == MEMCACHED_SUCCESS);
1210 assert(new_number == (initial - 1));
1211 }
1212 return 0;
1213 }
1214
1215 static test_return quit_test(memcached_st *memc)
1216 {
1217 memcached_return rc;
1218 const char *key= "fudge";
1219 const char *value= "sanford and sun";
1220
1221 rc= memcached_set(memc, key, strlen(key),
1222 value, strlen(value),
1223 (time_t)10, (uint32_t)3);
1224 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1225 memcached_quit(memc);
1226
1227 rc= memcached_set(memc, key, strlen(key),
1228 value, strlen(value),
1229 (time_t)50, (uint32_t)9);
1230 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1231
1232 return 0;
1233 }
1234
1235 static test_return mget_result_test(memcached_st *memc)
1236 {
1237 memcached_return rc;
1238 const char *keys[]= {"fudge", "son", "food"};
1239 size_t key_length[]= {5, 3, 4};
1240 unsigned int x;
1241
1242 memcached_result_st results_obj;
1243 memcached_result_st *results;
1244
1245 results= memcached_result_create(memc, &results_obj);
1246 assert(results);
1247 assert(&results_obj == results);
1248
1249 /* We need to empty the server before continueing test */
1250 rc= memcached_flush(memc, 0);
1251 assert(rc == MEMCACHED_SUCCESS);
1252
1253 rc= memcached_mget(memc, keys, key_length, 3);
1254 assert(rc == MEMCACHED_SUCCESS);
1255
1256 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1257 {
1258 assert(results);
1259 }
1260
1261 while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
1262 assert(!results);
1263 assert(rc == MEMCACHED_END);
1264
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 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
1277 {
1278 assert(results);
1279 assert(&results_obj == results);
1280 assert(rc == MEMCACHED_SUCCESS);
1281 assert(memcached_result_key_length(results) == memcached_result_length(results));
1282 assert(!memcmp(memcached_result_key_value(results),
1283 memcached_result_value(results),
1284 memcached_result_length(results)));
1285 }
1286
1287 memcached_result_free(&results_obj);
1288
1289 return 0;
1290 }
1291
1292 static test_return mget_result_alloc_test(memcached_st *memc)
1293 {
1294 memcached_return rc;
1295 const char *keys[]= {"fudge", "son", "food"};
1296 size_t key_length[]= {5, 3, 4};
1297 unsigned int x;
1298
1299 memcached_result_st *results;
1300
1301 /* We need to empty the server before continueing test */
1302 rc= memcached_flush(memc, 0);
1303 assert(rc == MEMCACHED_SUCCESS);
1304
1305 rc= memcached_mget(memc, keys, key_length, 3);
1306 assert(rc == MEMCACHED_SUCCESS);
1307
1308 while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
1309 {
1310 assert(results);
1311 }
1312 assert(!results);
1313 assert(rc == MEMCACHED_END);
1314
1315 for (x= 0; x < 3; x++)
1316 {
1317 rc= memcached_set(memc, keys[x], key_length[x],
1318 keys[x], key_length[x],
1319 (time_t)50, (uint32_t)9);
1320 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1321 }
1322
1323 rc= memcached_mget(memc, keys, key_length, 3);
1324 assert(rc == MEMCACHED_SUCCESS);
1325
1326 x= 0;
1327 while ((results= memcached_fetch_result(memc, NULL, &rc)))
1328 {
1329 assert(results);
1330 assert(rc == MEMCACHED_SUCCESS);
1331 assert(memcached_result_key_length(results) == memcached_result_length(results));
1332 assert(!memcmp(memcached_result_key_value(results),
1333 memcached_result_value(results),
1334 memcached_result_length(results)));
1335 memcached_result_free(results);
1336 x++;
1337 }
1338
1339 return 0;
1340 }
1341
1342 /* Count the results */
1343 static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
1344 memcached_result_st *result __attribute__((unused)),
1345 void *context)
1346 {
1347 unsigned int *counter= (unsigned int *)context;
1348
1349 *counter= *counter + 1;
1350
1351 return MEMCACHED_SUCCESS;
1352 }
1353
1354 static test_return mget_result_function(memcached_st *memc)
1355 {
1356 memcached_return rc;
1357 const char *keys[]= {"fudge", "son", "food"};
1358 size_t key_length[]= {5, 3, 4};
1359 unsigned int x;
1360 unsigned int counter;
1361 memcached_execute_function callbacks[1];
1362
1363 /* We need to empty the server before continueing test */
1364 rc= memcached_flush(memc, 0);
1365 for (x= 0; x < 3; x++)
1366 {
1367 rc= memcached_set(memc, keys[x], key_length[x],
1368 keys[x], key_length[x],
1369 (time_t)50, (uint32_t)9);
1370 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1371 }
1372
1373 rc= memcached_mget(memc, keys, key_length, 3);
1374 assert(rc == MEMCACHED_SUCCESS);
1375
1376 callbacks[0]= &callback_counter;
1377 counter= 0;
1378 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
1379
1380 assert(counter == 3);
1381
1382 return 0;
1383 }
1384
1385 static test_return mget_test(memcached_st *memc)
1386 {
1387 memcached_return rc;
1388 const char *keys[]= {"fudge", "son", "food"};
1389 size_t key_length[]= {5, 3, 4};
1390 unsigned int x;
1391 uint32_t flags;
1392
1393 char return_key[MEMCACHED_MAX_KEY];
1394 size_t return_key_length;
1395 char *return_value;
1396 size_t return_value_length;
1397
1398 /* We need to empty the server before continueing test */
1399 rc= memcached_flush(memc, 0);
1400 assert(rc == MEMCACHED_SUCCESS);
1401
1402 rc= memcached_mget(memc, keys, key_length, 3);
1403 assert(rc == MEMCACHED_SUCCESS);
1404
1405 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1406 &return_value_length, &flags, &rc)) != NULL)
1407 {
1408 assert(return_value);
1409 }
1410 assert(!return_value);
1411 assert(return_value_length == 0);
1412 assert(rc == MEMCACHED_END);
1413
1414 for (x= 0; x < 3; x++)
1415 {
1416 rc= memcached_set(memc, keys[x], key_length[x],
1417 keys[x], key_length[x],
1418 (time_t)50, (uint32_t)9);
1419 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1420 }
1421
1422 rc= memcached_mget(memc, keys, key_length, 3);
1423 assert(rc == MEMCACHED_SUCCESS);
1424
1425 x= 0;
1426 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1427 &return_value_length, &flags, &rc)))
1428 {
1429 assert(return_value);
1430 assert(rc == MEMCACHED_SUCCESS);
1431 assert(return_key_length == return_value_length);
1432 assert(!memcmp(return_value, return_key, return_value_length));
1433 free(return_value);
1434 x++;
1435 }
1436
1437 return 0;
1438 }
1439
1440 static test_return get_stats_keys(memcached_st *memc)
1441 {
1442 char **list;
1443 char **ptr;
1444 memcached_stat_st memc_stat;
1445 memcached_return rc;
1446
1447 list= memcached_stat_get_keys(memc, &memc_stat, &rc);
1448 assert(rc == MEMCACHED_SUCCESS);
1449 for (ptr= list; *ptr; ptr++)
1450 assert(*ptr);
1451 fflush(stdout);
1452
1453 free(list);
1454
1455 return 0;
1456 }
1457
1458 static test_return version_string_test(memcached_st *memc __attribute__((unused)))
1459 {
1460 const char *version_string;
1461
1462 version_string= memcached_lib_version();
1463
1464 assert(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
1465
1466 return 0;
1467 }
1468
1469 static test_return get_stats(memcached_st *memc)
1470 {
1471 unsigned int x;
1472 char **list;
1473 char **ptr;
1474 memcached_return rc;
1475 memcached_stat_st *memc_stat;
1476
1477 memc_stat= memcached_stat(memc, NULL, &rc);
1478 assert(rc == MEMCACHED_SUCCESS);
1479
1480 assert(rc == MEMCACHED_SUCCESS);
1481 assert(memc_stat);
1482
1483 for (x= 0; x < memcached_server_count(memc); x++)
1484 {
1485 list= memcached_stat_get_keys(memc, memc_stat+x, &rc);
1486 assert(rc == MEMCACHED_SUCCESS);
1487 for (ptr= list; *ptr; ptr++);
1488
1489 free(list);
1490 }
1491
1492 memcached_stat_free(NULL, memc_stat);
1493
1494 return 0;
1495 }
1496
1497 static test_return add_host_test(memcached_st *memc)
1498 {
1499 unsigned int x;
1500 memcached_server_st *servers;
1501 memcached_return rc;
1502 char servername[]= "0.example.com";
1503
1504 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
1505 assert(servers);
1506 assert(1 == memcached_server_list_count(servers));
1507
1508 for (x= 2; x < 20; x++)
1509 {
1510 char buffer[SMALL_STRING_LEN];
1511
1512 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
1513 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
1514 &rc);
1515 assert(rc == MEMCACHED_SUCCESS);
1516 assert(x == memcached_server_list_count(servers));
1517 }
1518
1519 rc= memcached_server_push(memc, servers);
1520 assert(rc == MEMCACHED_SUCCESS);
1521 rc= memcached_server_push(memc, servers);
1522 assert(rc == MEMCACHED_SUCCESS);
1523
1524 memcached_server_list_free(servers);
1525
1526 return 0;
1527 }
1528
1529 static memcached_return clone_test_callback(memcached_st *parent __attribute__((unused)), memcached_st *memc_clone __attribute__((unused)))
1530 {
1531 return MEMCACHED_SUCCESS;
1532 }
1533
1534 static memcached_return cleanup_test_callback(memcached_st *ptr __attribute__((unused)))
1535 {
1536 return MEMCACHED_SUCCESS;
1537 }
1538
1539 static test_return callback_test(memcached_st *memc)
1540 {
1541 /* Test User Data */
1542 {
1543 int x= 5;
1544 int *test_ptr;
1545 memcached_return rc;
1546
1547 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
1548 assert(rc == MEMCACHED_SUCCESS);
1549 test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
1550 assert(*test_ptr == x);
1551 }
1552
1553 /* Test Clone Callback */
1554 {
1555 memcached_clone_func clone_cb= (memcached_clone_func)clone_test_callback;
1556 void *clone_cb_ptr= *(void **)&clone_cb;
1557 void *temp_function= NULL;
1558 memcached_return rc;
1559
1560 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1561 clone_cb_ptr);
1562 assert(rc == MEMCACHED_SUCCESS);
1563 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1564 assert(temp_function == clone_cb_ptr);
1565 }
1566
1567 /* Test Cleanup Callback */
1568 {
1569 memcached_cleanup_func cleanup_cb=
1570 (memcached_cleanup_func)cleanup_test_callback;
1571 void *cleanup_cb_ptr= *(void **)&cleanup_cb;
1572 void *temp_function= NULL;
1573 memcached_return rc;
1574
1575 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION,
1576 cleanup_cb_ptr);
1577 assert(rc == MEMCACHED_SUCCESS);
1578 temp_function= memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
1579 assert(temp_function == cleanup_cb_ptr);
1580 }
1581
1582 return 0;
1583 }
1584
1585 /* We don't test the behavior itself, we test the switches */
1586 static test_return behavior_test(memcached_st *memc)
1587 {
1588 uint64_t value;
1589 uint32_t set= 1;
1590
1591 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1592 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1593 assert(value == 1);
1594
1595 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1596 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1597 assert(value == 1);
1598
1599 set= MEMCACHED_HASH_MD5;
1600 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1601 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1602 assert(value == MEMCACHED_HASH_MD5);
1603
1604 set= 0;
1605
1606 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
1607 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
1608 assert(value == 0);
1609
1610 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
1611 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
1612 assert(value == 0);
1613
1614 set= MEMCACHED_HASH_DEFAULT;
1615 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1616 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1617 assert(value == MEMCACHED_HASH_DEFAULT);
1618
1619 set= MEMCACHED_HASH_CRC;
1620 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
1621 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
1622 assert(value == MEMCACHED_HASH_CRC);
1623
1624 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1625 assert(value > 0);
1626
1627 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1628 assert(value > 0);
1629
1630 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
1631 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, value + 1);
1632 assert((value + 1) == memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
1633 return 0;
1634 }
1635
1636 /* Test case provided by Cal Haldenbrand */
1637 static test_return user_supplied_bug1(memcached_st *memc)
1638 {
1639 unsigned int setter= 1;
1640 unsigned int x;
1641
1642 unsigned long long total= 0;
1643 uint32_t size= 0;
1644 char key[10];
1645 char randomstuff[6 * 1024];
1646 memcached_return rc;
1647
1648 memset(randomstuff, 0, 6 * 1024);
1649
1650 /* We just keep looking at the same values over and over */
1651 srandom(10);
1652
1653 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1654 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1655
1656
1657 /* add key */
1658 for (x= 0 ; total < 20 * 1024576 ; x++ )
1659 {
1660 unsigned int j= 0;
1661
1662 size= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
1663 memset(randomstuff, 0, 6 * 1024);
1664 assert(size < 6 * 1024); /* Being safe here */
1665
1666 for (j= 0 ; j < size ;j++)
1667 randomstuff[j] = (signed char) ((rand() % 26) + 97);
1668
1669 total += size;
1670 sprintf(key, "%d", x);
1671 rc = memcached_set(memc, key, strlen(key),
1672 randomstuff, strlen(randomstuff), 10, 0);
1673 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1674 /* If we fail, lets try again */
1675 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
1676 rc = memcached_set(memc, key, strlen(key),
1677 randomstuff, strlen(randomstuff), 10, 0);
1678 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
1679 }
1680
1681 return 0;
1682 }
1683
1684 /* Test case provided by Cal Haldenbrand */
1685 static test_return user_supplied_bug2(memcached_st *memc)
1686 {
1687 int errors;
1688 unsigned int setter;
1689 unsigned int x;
1690 unsigned long long total;
1691
1692 setter= 1;
1693 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1694 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1695 #ifdef NOT_YET
1696 setter = 20 * 1024576;
1697 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1698 setter = 20 * 1024576;
1699 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1700 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1701 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1702
1703 for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
1704 #endif
1705
1706 for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
1707 {
1708 memcached_return rc= MEMCACHED_SUCCESS;
1709 char buffer[SMALL_STRING_LEN];
1710 uint32_t flags= 0;
1711 size_t val_len= 0;
1712 char *getval;
1713
1714 memset(buffer, 0, SMALL_STRING_LEN);
1715
1716 snprintf(buffer, SMALL_STRING_LEN, "%u", x);
1717 getval= memcached_get(memc, buffer, strlen(buffer),
1718 &val_len, &flags, &rc);
1719 if (rc != MEMCACHED_SUCCESS)
1720 {
1721 if (rc == MEMCACHED_NOTFOUND)
1722 errors++;
1723 else
1724 {
1725 assert(rc);
1726 }
1727
1728 continue;
1729 }
1730 total+= val_len;
1731 errors= 0;
1732 free(getval);
1733 }
1734
1735 return 0;
1736 }
1737
1738 /* Do a large mget() over all the keys we think exist */
1739 #define KEY_COUNT 3000 // * 1024576
1740 static test_return user_supplied_bug3(memcached_st *memc)
1741 {
1742 memcached_return rc;
1743 unsigned int setter;
1744 unsigned int x;
1745 char **keys;
1746 size_t key_lengths[KEY_COUNT];
1747
1748 setter= 1;
1749 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
1750 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
1751 #ifdef NOT_YET
1752 setter = 20 * 1024576;
1753 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
1754 setter = 20 * 1024576;
1755 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
1756 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
1757 getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
1758 #endif
1759
1760 keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
1761 assert(keys);
1762 memset(keys, 0, (sizeof(char *) * KEY_COUNT));
1763 for (x= 0; x < KEY_COUNT; x++)
1764 {
1765 char buffer[30];
1766
1767 snprintf(buffer, 30, "%u", x);
1768 keys[x]= strdup(buffer);
1769 key_lengths[x]= strlen(keys[x]);
1770 }
1771
1772 rc= memcached_mget(memc, (const char **)keys, key_lengths, KEY_COUNT);
1773 assert(rc == MEMCACHED_SUCCESS);
1774
1775 /* Turn this into a help function */
1776 {
1777 char return_key[MEMCACHED_MAX_KEY];
1778 size_t return_key_length;
1779 char *return_value;
1780 size_t return_value_length;
1781 uint32_t flags;
1782
1783 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1784 &return_value_length, &flags, &rc)))
1785 {
1786 assert(return_value);
1787 assert(rc == MEMCACHED_SUCCESS);
1788 free(return_value);
1789 }
1790 }
1791
1792 for (x= 0; x < KEY_COUNT; x++)
1793 free(keys[x]);
1794 free(keys);
1795
1796 return 0;
1797 }
1798
1799 /* Make sure we behave properly if server list has no values */
1800 static test_return user_supplied_bug4(memcached_st *memc)
1801 {
1802 memcached_return rc;
1803 const char *keys[]= {"fudge", "son", "food"};
1804 size_t key_length[]= {5, 3, 4};
1805 unsigned int x;
1806 uint32_t flags;
1807 char return_key[MEMCACHED_MAX_KEY];
1808 size_t return_key_length;
1809 char *return_value;
1810 size_t return_value_length;
1811
1812 /* Here we free everything before running a bunch of mget tests */
1813 {
1814 memcached_server_list_free(memc->hosts);
1815 memc->hosts= NULL;
1816 memc->number_of_hosts= 0;
1817 }
1818
1819
1820 /* We need to empty the server before continueing test */
1821 rc= memcached_flush(memc, 0);
1822 assert(rc == MEMCACHED_NO_SERVERS);
1823
1824 rc= memcached_mget(memc, keys, key_length, 3);
1825 assert(rc == MEMCACHED_NO_SERVERS);
1826
1827 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1828 &return_value_length, &flags, &rc)) != NULL)
1829 {
1830 assert(return_value);
1831 }
1832 assert(!return_value);
1833 assert(return_value_length == 0);
1834 assert(rc == MEMCACHED_NO_SERVERS);
1835
1836 for (x= 0; x < 3; x++)
1837 {
1838 rc= memcached_set(memc, keys[x], key_length[x],
1839 keys[x], key_length[x],
1840 (time_t)50, (uint32_t)9);
1841 assert(rc == MEMCACHED_NO_SERVERS);
1842 }
1843
1844 rc= memcached_mget(memc, keys, key_length, 3);
1845 assert(rc == MEMCACHED_NO_SERVERS);
1846
1847 x= 0;
1848 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
1849 &return_value_length, &flags, &rc)))
1850 {
1851 assert(return_value);
1852 assert(rc == MEMCACHED_SUCCESS);
1853 assert(return_key_length == return_value_length);
1854 assert(!memcmp(return_value, return_key, return_value_length));
1855 free(return_value);
1856 x++;
1857 }
1858
1859 return 0;
1860 }
1861
1862 #define VALUE_SIZE_BUG5 1048064
1863 static test_return user_supplied_bug5(memcached_st *memc)
1864 {
1865 memcached_return rc;
1866 const char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1867 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1868 char return_key[MEMCACHED_MAX_KEY];
1869 size_t return_key_length;
1870 char *value;
1871 size_t value_length;
1872 uint32_t flags;
1873 unsigned int count;
1874 unsigned int x;
1875 char insert_data[VALUE_SIZE_BUG5];
1876
1877 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1878 insert_data[x]= (signed char)rand();
1879
1880 memcached_flush(memc, 0);
1881 value= memcached_get(memc, keys[0], key_length[0],
1882 &value_length, &flags, &rc);
1883 assert(value == NULL);
1884 rc= memcached_mget(memc, keys, key_length, 4);
1885
1886 count= 0;
1887 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1888 &value_length, &flags, &rc)))
1889 count++;
1890 assert(count == 0);
1891
1892 for (x= 0; x < 4; x++)
1893 {
1894 rc= memcached_set(memc, keys[x], key_length[x],
1895 insert_data, VALUE_SIZE_BUG5,
1896 (time_t)0, (uint32_t)0);
1897 assert(rc == MEMCACHED_SUCCESS);
1898 }
1899
1900 for (x= 0; x < 10; x++)
1901 {
1902 value= memcached_get(memc, keys[0], key_length[0],
1903 &value_length, &flags, &rc);
1904 assert(value);
1905 free(value);
1906
1907 rc= memcached_mget(memc, keys, key_length, 4);
1908 count= 0;
1909 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1910 &value_length, &flags, &rc)))
1911 {
1912 count++;
1913 free(value);
1914 }
1915 assert(count == 4);
1916 }
1917
1918 return 0;
1919 }
1920
1921 static test_return user_supplied_bug6(memcached_st *memc)
1922 {
1923 memcached_return rc;
1924 const char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
1925 size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
1926 char return_key[MEMCACHED_MAX_KEY];
1927 size_t return_key_length;
1928 char *value;
1929 size_t value_length;
1930 uint32_t flags;
1931 unsigned int count;
1932 unsigned int x;
1933 char insert_data[VALUE_SIZE_BUG5];
1934
1935 for (x= 0; x < VALUE_SIZE_BUG5; x++)
1936 insert_data[x]= (signed char)rand();
1937
1938 memcached_flush(memc, 0);
1939 value= memcached_get(memc, keys[0], key_length[0],
1940 &value_length, &flags, &rc);
1941 assert(value == NULL);
1942 assert(rc == MEMCACHED_NOTFOUND);
1943 rc= memcached_mget(memc, keys, key_length, 4);
1944 assert(rc == MEMCACHED_SUCCESS);
1945
1946 count= 0;
1947 while ((value= memcached_fetch(memc, return_key, &return_key_length,
1948 &value_length, &flags, &rc)))
1949 count++;
1950 assert(count == 0);
1951 assert(rc == MEMCACHED_END);
1952
1953 for (x= 0; x < 4; x++)
1954 {
1955 rc= memcached_set(memc, keys[x], key_length[x],
1956 insert_data, VALUE_SIZE_BUG5,
1957 (time_t)0, (uint32_t)0);
1958 assert(rc == MEMCACHED_SUCCESS);
1959 }
1960
1961 for (x= 0; x < 2; x++)
1962 {
1963 value= memcached_get(memc, keys[0], key_length[0],
1964 &value_length, &flags, &rc);
1965 assert(value);
1966 free(value);
1967
1968 rc= memcached_mget(memc, keys, key_length, 4);
1969 assert(rc == MEMCACHED_SUCCESS);
1970 count= 3;
1971 /* We test for purge of partial complete fetches */
1972 for (count= 3; count; count--)
1973 {
1974 value= memcached_fetch(memc, return_key, &return_key_length,
1975 &value_length, &flags, &rc);
1976 assert(rc == MEMCACHED_SUCCESS);
1977 assert(!(memcmp(value, insert_data, value_length)));
1978 assert(value_length);
1979 free(value);
1980 }
1981 }
1982
1983 return 0;
1984 }
1985
1986 static test_return user_supplied_bug8(memcached_st *memc __attribute__((unused)))
1987 {
1988 memcached_return rc;
1989 memcached_st *mine;
1990 memcached_st *memc_clone;
1991
1992 memcached_server_st *servers;
1993 const 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";
1994
1995 servers= memcached_servers_parse(server_list);
1996 assert(servers);
1997
1998 mine= memcached_create(NULL);
1999 rc= memcached_server_push(mine, servers);
2000 assert(rc == MEMCACHED_SUCCESS);
2001 memcached_server_list_free(servers);
2002
2003 assert(mine);
2004 memc_clone= memcached_clone(NULL, mine);
2005
2006 memcached_quit(mine);
2007 memcached_quit(memc_clone);
2008
2009
2010 memcached_free(mine);
2011 memcached_free(memc_clone);
2012
2013 return 0;
2014 }
2015
2016 /* Test flag store/retrieve */
2017 static test_return user_supplied_bug7(memcached_st *memc)
2018 {
2019 memcached_return rc;
2020 const char *keys= "036790384900";
2021 size_t key_length= strlen(keys);
2022 char return_key[MEMCACHED_MAX_KEY];
2023 size_t return_key_length;
2024 char *value;
2025 size_t value_length;
2026 uint32_t flags;
2027 unsigned int x;
2028 char insert_data[VALUE_SIZE_BUG5];
2029
2030 for (x= 0; x < VALUE_SIZE_BUG5; x++)
2031 insert_data[x]= (signed char)rand();
2032
2033 memcached_flush(memc, 0);
2034
2035 flags= 245;
2036 rc= memcached_set(memc, keys, key_length,
2037 insert_data, VALUE_SIZE_BUG5,
2038 (time_t)0, flags);
2039 assert(rc == MEMCACHED_SUCCESS);
2040
2041 flags= 0;
2042 value= memcached_get(memc, keys, key_length,
2043 &value_length, &flags, &rc);
2044 assert(flags == 245);
2045 assert(value);
2046 free(value);
2047
2048 rc= memcached_mget(memc, &keys, &key_length, 1);
2049
2050 flags= 0;
2051 value= memcached_fetch(memc, return_key, &return_key_length,
2052 &value_length, &flags, &rc);
2053 assert(flags == 245);
2054 assert(value);
2055 free(value);
2056
2057
2058 return 0;
2059 }
2060
2061 static test_return user_supplied_bug9(memcached_st *memc)
2062 {
2063 memcached_return rc;
2064 const char *keys[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2065 size_t key_length[3];
2066 unsigned int x;
2067 uint32_t flags;
2068 unsigned count= 0;
2069
2070 char return_key[MEMCACHED_MAX_KEY];
2071 size_t return_key_length;
2072 char *return_value;
2073 size_t return_value_length;
2074
2075
2076 key_length[0]= strlen("UDATA:edevil@sapo.pt");
2077 key_length[1]= strlen("fudge&*@#");
2078 key_length[2]= strlen("for^#@&$not");
2079
2080
2081 for (x= 0; x < 3; x++)
2082 {
2083 rc= memcached_set(memc, keys[x], key_length[x],
2084 keys[x], key_length[x],
2085 (time_t)50, (uint32_t)9);
2086 assert(rc == MEMCACHED_SUCCESS);
2087 }
2088
2089 rc= memcached_mget(memc, keys, key_length, 3);
2090 assert(rc == MEMCACHED_SUCCESS);
2091
2092 /* We need to empty the server before continueing test */
2093 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2094 &return_value_length, &flags, &rc)) != NULL)
2095 {
2096 assert(return_value);
2097 free(return_value);
2098 count++;
2099 }
2100 assert(count == 3);
2101
2102 return 0;
2103 }
2104
2105 /* We are testing with aggressive timeout to get failures */
2106 static test_return user_supplied_bug10(memcached_st *memc)
2107 {
2108 const char *key= "foo";
2109 char *value;
2110 size_t value_length= 512;
2111 unsigned int x;
2112 size_t key_len= 3;
2113 memcached_return rc;
2114 unsigned int set= 1;
2115 memcached_st *mclone= memcached_clone(NULL, memc);
2116 int32_t timeout;
2117
2118 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
2119 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
2120 timeout= 2;
2121 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
2122 (uint64_t)timeout);
2123
2124 value = (char*)malloc(value_length * sizeof(char));
2125
2126 for (x= 0; x < value_length; x++)
2127 value[x]= (char) (x % 127);
2128
2129 for (x= 1; x <= 100000; ++x)
2130 {
2131 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
2132
2133 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE ||
2134 rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT);
2135
2136 if (rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_TIMEOUT)
2137 x--;
2138 }
2139
2140 free(value);
2141 memcached_free(mclone);
2142
2143 return 0;
2144 }
2145
2146 /*
2147 We are looking failures in the async protocol
2148 */
2149 static test_return user_supplied_bug11(memcached_st *memc)
2150 {
2151 const char *key= "foo";
2152 char *value;
2153 size_t value_length= 512;
2154 unsigned int x;
2155 size_t key_len= 3;
2156 memcached_return rc;
2157 unsigned int set= 1;
2158 int32_t timeout;
2159 memcached_st *mclone= memcached_clone(NULL, memc);
2160
2161 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
2162 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
2163 timeout= -1;
2164 memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
2165 (size_t)timeout);
2166
2167 timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
2168
2169 assert(timeout == -1);
2170
2171 value = (char*)malloc(value_length * sizeof(char));
2172
2173 for (x= 0; x < value_length; x++)
2174 value[x]= (char) (x % 127);
2175
2176 for (x= 1; x <= 100000; ++x)
2177 {
2178 rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
2179 }
2180
2181 free(value);
2182 memcached_free(mclone);
2183
2184 return 0;
2185 }
2186
2187 /*
2188 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2189 */
2190 static test_return user_supplied_bug12(memcached_st *memc)
2191 {
2192 memcached_return rc;
2193 uint32_t flags;
2194 size_t value_length;
2195 char *value;
2196 uint64_t number_value;
2197
2198 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
2199 &value_length, &flags, &rc);
2200 assert(value == NULL);
2201 assert(rc == MEMCACHED_NOTFOUND);
2202
2203 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
2204 1, &number_value);
2205
2206 assert(value == NULL);
2207 /* The binary protocol will set the key if it doesn't exist */
2208 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1)
2209 assert(rc == MEMCACHED_SUCCESS);
2210 else
2211 assert(rc == MEMCACHED_NOTFOUND);
2212
2213 rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2214
2215 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
2216 &value_length, &flags, &rc);
2217 assert(value);
2218 assert(rc == MEMCACHED_SUCCESS);
2219 free(value);
2220
2221 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
2222 1, &number_value);
2223 assert(number_value == 2);
2224 assert(rc == MEMCACHED_SUCCESS);
2225
2226 return 0;
2227 }
2228
2229 /*
2230 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2231 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2232 */
2233 static test_return user_supplied_bug13(memcached_st *memc)
2234 {
2235 char key[] = "key34567890";
2236 char *overflow;
2237 memcached_return rc;
2238 size_t overflowSize;
2239
2240 char commandFirst[]= "set key34567890 0 0 ";
2241 char commandLast[] = " \r\n"; /* first line of command sent to server */
2242 size_t commandLength;
2243 size_t testSize;
2244
2245 commandLength = strlen(commandFirst) + strlen(commandLast) + 4; /* 4 is number of characters in size, probably 8196 */
2246
2247 overflowSize = MEMCACHED_MAX_BUFFER - commandLength;
2248
2249 for (testSize= overflowSize - 1; testSize < overflowSize + 1; testSize++)
2250 {
2251 overflow= malloc(testSize);
2252 assert(overflow != NULL);
2253
2254 memset(overflow, 'x', testSize);
2255 rc= memcached_set(memc, key, strlen(key),
2256 overflow, testSize, 0, 0);
2257 assert(rc == MEMCACHED_SUCCESS);
2258 free(overflow);
2259 }
2260
2261 return 0;
2262 }
2263
2264
2265 /*
2266 Test values of many different sizes
2267 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2268 set key34567890 0 0 8169 \r\n
2269 is sent followed by buffer of size 8169, followed by 8169
2270 */
2271 static test_return user_supplied_bug14(memcached_st *memc)
2272 {
2273 size_t setter= 1;
2274 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
2275 memcached_return rc;
2276 const char *key= "foo";
2277 char *value;
2278 size_t value_length= 18000;
2279 char *string;
2280 size_t string_length;
2281 uint32_t flags;
2282 unsigned int x;
2283 size_t current_length;
2284
2285 value = (char*)malloc(value_length);
2286 assert(value);
2287
2288 for (x= 0; x < value_length; x++)
2289 value[x] = (char) (x % 127);
2290
2291 for (current_length= 0; current_length < value_length; current_length++)
2292 {
2293 rc= memcached_set(memc, key, strlen(key),
2294 value, current_length,
2295 (time_t)0, (uint32_t)0);
2296 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
2297
2298 string= memcached_get(memc, key, strlen(key),
2299 &string_length, &flags, &rc);
2300
2301 assert(rc == MEMCACHED_SUCCESS);
2302 assert(string_length == current_length);
2303 assert(!memcmp(string, value, string_length));
2304
2305 free(string);
2306 }
2307
2308 free(value);
2309
2310 return 0;
2311 }
2312
2313 /*
2314 Look for zero length value problems
2315 */
2316 static test_return user_supplied_bug15(memcached_st *memc)
2317 {
2318 uint32_t x;
2319 memcached_return rc;
2320 const char *key= "mykey";
2321 char *value;
2322 size_t length;
2323 uint32_t flags;
2324
2325 for (x= 0; x < 2; x++)
2326 {
2327 rc= memcached_set(memc, key, strlen(key),
2328 NULL, 0,
2329 (time_t)0, (uint32_t)0);
2330
2331 assert(rc == MEMCACHED_SUCCESS);
2332
2333 value= memcached_get(memc, key, strlen(key),
2334 &length, &flags, &rc);
2335
2336 assert(rc == MEMCACHED_SUCCESS);
2337 assert(value == NULL);
2338 assert(length == 0);
2339 assert(flags == 0);
2340
2341 value= memcached_get(memc, key, strlen(key),
2342 &length, &flags, &rc);
2343
2344 assert(rc == MEMCACHED_SUCCESS);
2345 assert(value == NULL);
2346 assert(length == 0);
2347 assert(flags == 0);
2348 }
2349
2350 return 0;
2351 }
2352
2353 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2354 static test_return user_supplied_bug16(memcached_st *memc)
2355 {
2356 memcached_return rc;
2357 const char *key= "mykey";
2358 char *value;
2359 size_t length;
2360 uint32_t flags;
2361
2362 rc= memcached_set(memc, key, strlen(key),
2363 NULL, 0,
2364 (time_t)0, UINT32_MAX);
2365
2366 assert(rc == MEMCACHED_SUCCESS);
2367
2368 value= memcached_get(memc, key, strlen(key),
2369 &length, &flags, &rc);
2370
2371 assert(rc == MEMCACHED_SUCCESS);
2372 assert(value == NULL);
2373 assert(length == 0);
2374 assert(flags == UINT32_MAX);
2375
2376 return 0;
2377 }
2378
2379 #ifndef __sun
2380 /* Check the validity of chinese key*/
2381 static test_return user_supplied_bug17(memcached_st *memc)
2382 {
2383 memcached_return rc;
2384 const char *key= "豆瓣";
2385 const char *value="我们在炎热抑郁的夏天无法停止豆瓣";
2386 char *value2;
2387 size_t length;
2388 uint32_t flags;
2389
2390 rc= memcached_set(memc, key, strlen(key),
2391 value, strlen(value),
2392 (time_t)0, 0);
2393
2394 assert(rc == MEMCACHED_SUCCESS);
2395
2396 value2= memcached_get(memc, key, strlen(key),
2397 &length, &flags, &rc);
2398
2399 assert(length==strlen(value));
2400 assert(rc == MEMCACHED_SUCCESS);
2401 assert(memcmp(value, value2, length)==0);
2402 free(value2);
2403
2404 return 0;
2405 }
2406 #endif
2407
2408 /*
2409 From Andrei on IRC
2410 */
2411
2412 static test_return user_supplied_bug19(memcached_st *memc)
2413 {
2414 memcached_st *m;
2415 memcached_server_st *s;
2416 memcached_return res;
2417
2418 (void)memc;
2419
2420 m= memcached_create(NULL);
2421 memcached_server_add_with_weight(m, "localhost", 11311, 100);
2422 memcached_server_add_with_weight(m, "localhost", 11312, 100);
2423
2424 s= memcached_server_by_key(m, "a", 1, &res);
2425 memcached_server_free(s);
2426
2427 memcached_free(m);
2428
2429 return 0;
2430 }
2431
2432 /* CAS test from Andei */
2433 static test_return user_supplied_bug20(memcached_st *memc)
2434 {
2435 memcached_return status;
2436 memcached_result_st *result, result_obj;
2437 const char *key = "abc";
2438 size_t key_len = strlen("abc");
2439 const char *value = "foobar";
2440 size_t value_len = strlen(value);
2441
2442 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
2443
2444 status = memcached_set(memc, key, key_len, value, value_len, (time_t)0, (uint32_t)0);
2445 assert(status == MEMCACHED_SUCCESS);
2446
2447 status = memcached_mget(memc, &key, &key_len, 1);
2448 assert(status == MEMCACHED_SUCCESS);
2449
2450 result= memcached_result_create(memc, &result_obj);
2451 assert(result);
2452
2453 memcached_result_create(memc, &result_obj);
2454 result= memcached_fetch_result(memc, &result_obj, &status);
2455
2456 assert(result);
2457 assert(status == MEMCACHED_SUCCESS);
2458
2459 memcached_result_free(result);
2460
2461 return 0;
2462 }
2463
2464 #include "ketama_test_cases.h"
2465 static test_return user_supplied_bug18(memcached_st *trash)
2466 {
2467 memcached_return rc;
2468 uint64_t value;
2469 int x;
2470 memcached_server_st *server_pool;
2471 memcached_st *memc;
2472
2473 (void)trash;
2474
2475 memc= memcached_create(NULL);
2476 assert(memc);
2477
2478 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2479 assert(rc == MEMCACHED_SUCCESS);
2480
2481 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2482 assert(value == 1);
2483
2484 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2485 assert(rc == MEMCACHED_SUCCESS);
2486
2487 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2488 assert(value == MEMCACHED_HASH_MD5);
2489
2490 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");
2491 memcached_server_push(memc, server_pool);
2492
2493 /* verify that the server list was parsed okay. */
2494 assert(memc->number_of_hosts == 8);
2495 assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
2496 assert(server_pool[0].port == 11211);
2497 assert(server_pool[0].weight == 600);
2498 assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
2499 assert(server_pool[2].port == 11211);
2500 assert(server_pool[2].weight == 200);
2501 assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
2502 assert(server_pool[7].port == 11211);
2503 assert(server_pool[7].weight == 100);
2504
2505 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2506 * us test the boundary wraparound.
2507 */
2508 assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
2509
2510 /* verify the standard ketama set. */
2511 for (x= 0; x < 99; x++)
2512 {
2513 uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key));
2514 char *hostname = memc->hosts[server_idx].hostname;
2515 assert(strcmp(hostname, test_cases[x].server) == 0);
2516 }
2517
2518 memcached_server_list_free(server_pool);
2519 memcached_free(memc);
2520
2521 return 0;
2522 }
2523
2524 static test_return auto_eject_hosts(memcached_st *trash)
2525 {
2526 (void) trash;
2527
2528 memcached_return rc;
2529 memcached_st *memc= memcached_create(NULL);
2530 assert(memc);
2531
2532 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
2533 assert(rc == MEMCACHED_SUCCESS);
2534
2535 uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
2536 assert(value == 1);
2537
2538 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
2539 assert(rc == MEMCACHED_SUCCESS);
2540
2541 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
2542 assert(value == MEMCACHED_HASH_MD5);
2543
2544 /* server should be removed when in delay */
2545 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS, 1);
2546 assert(rc == MEMCACHED_SUCCESS);
2547
2548 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
2549 assert(value == 1);
2550
2551 memcached_server_st *server_pool;
2552 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");
2553 memcached_server_push(memc, server_pool);
2554
2555 /* verify that the server list was parsed okay. */
2556 assert(memc->number_of_hosts == 8);
2557 assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0);
2558 assert(server_pool[0].port == 11211);
2559 assert(server_pool[0].weight == 600);
2560 assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0);
2561 assert(server_pool[2].port == 11211);
2562 assert(server_pool[2].weight == 200);
2563 assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0);
2564 assert(server_pool[7].port == 11211);
2565 assert(server_pool[7].weight == 100);
2566
2567 memc->hosts[2].next_retry = time(NULL) + 15;
2568 memc->next_distribution_rebuild= time(NULL) - 1;
2569
2570 for (int x= 0; x < 99; x++)
2571 {
2572 uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key));
2573 assert(server_idx != 2);
2574 }
2575
2576 /* and re-added when it's back. */
2577 memc->hosts[2].next_retry = time(NULL) - 1;
2578 memc->next_distribution_rebuild= time(NULL) - 1;
2579 run_distribution(memc);
2580 for (int x= 0; x < 99; x++)
2581 {
2582 uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key));
2583 char *hostname = memc->hosts[server_idx].hostname;
2584 assert(strcmp(hostname, test_cases[x].server) == 0);
2585 }
2586
2587 memcached_server_list_free(server_pool);
2588 memcached_free(memc);
2589
2590 return TEST_SUCCESS;
2591 }
2592
2593 static test_return result_static(memcached_st *memc)
2594 {
2595 memcached_result_st result;
2596 memcached_result_st *result_ptr;
2597
2598 result_ptr= memcached_result_create(memc, &result);
2599 assert(result.is_allocated == false);
2600 assert(result_ptr);
2601 memcached_result_free(&result);
2602
2603 return 0;
2604 }
2605
2606 static test_return result_alloc(memcached_st *memc)
2607 {
2608 memcached_result_st *result;
2609
2610 result= memcached_result_create(memc, NULL);
2611 assert(result);
2612 memcached_result_free(result);
2613
2614 return 0;
2615 }
2616
2617 static test_return string_static_null(memcached_st *memc)
2618 {
2619 memcached_string_st string;
2620 memcached_string_st *string_ptr;
2621
2622 string_ptr= memcached_string_create(memc, &string, 0);
2623 assert(string.is_allocated == false);
2624 assert(string_ptr);
2625 memcached_string_free(&string);
2626
2627 return 0;
2628 }
2629
2630 static test_return string_alloc_null(memcached_st *memc)
2631 {
2632 memcached_string_st *string;
2633
2634 string= memcached_string_create(memc, NULL, 0);
2635 assert(string);
2636 memcached_string_free(string);
2637
2638 return 0;
2639 }
2640
2641 static test_return string_alloc_with_size(memcached_st *memc)
2642 {
2643 memcached_string_st *string;
2644
2645 string= memcached_string_create(memc, NULL, 1024);
2646 assert(string);
2647 memcached_string_free(string);
2648
2649 return 0;
2650 }
2651
2652 static test_return string_alloc_with_size_toobig(memcached_st *memc)
2653 {
2654 memcached_string_st *string;
2655
2656 string= memcached_string_create(memc, NULL, SIZE_MAX);
2657 assert(string == NULL);
2658
2659 return 0;
2660 }
2661
2662 static test_return string_alloc_append(memcached_st *memc)
2663 {
2664 unsigned int x;
2665 char buffer[SMALL_STRING_LEN];
2666 memcached_string_st *string;
2667
2668 /* Ring the bell! */
2669 memset(buffer, 6, SMALL_STRING_LEN);
2670
2671 string= memcached_string_create(memc, NULL, 100);
2672 assert(string);
2673
2674 for (x= 0; x < 1024; x++)
2675 {
2676 memcached_return rc;
2677 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2678 assert(rc == MEMCACHED_SUCCESS);
2679 }
2680 memcached_string_free(string);
2681
2682 return 0;
2683 }
2684
2685 static test_return string_alloc_append_toobig(memcached_st *memc)
2686 {
2687 memcached_return rc;
2688 unsigned int x;
2689 char buffer[SMALL_STRING_LEN];
2690 memcached_string_st *string;
2691
2692 /* Ring the bell! */
2693 memset(buffer, 6, SMALL_STRING_LEN);
2694
2695 string= memcached_string_create(memc, NULL, 100);
2696 assert(string);
2697
2698 for (x= 0; x < 1024; x++)
2699 {
2700 rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
2701 assert(rc == MEMCACHED_SUCCESS);
2702 }
2703 rc= memcached_string_append(string, buffer, SIZE_MAX);
2704 assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
2705 memcached_string_free(string);
2706
2707 return 0;
2708 }
2709
2710 static test_return cleanup_pairs(memcached_st *memc __attribute__((unused)))
2711 {
2712 pairs_free(global_pairs);
2713
2714 return 0;
2715 }
2716
2717 static test_return generate_pairs(memcached_st *memc __attribute__((unused)))
2718 {
2719 unsigned long long x;
2720 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
2721 global_count= GLOBAL_COUNT;
2722
2723 for (x= 0; x < global_count; x++)
2724 {
2725 global_keys[x]= global_pairs[x].key;
2726 global_keys_length[x]= global_pairs[x].key_length;
2727 }
2728
2729 return 0;
2730 }
2731
2732 static test_return generate_large_pairs(memcached_st *memc __attribute__((unused)))
2733 {
2734 unsigned long long x;
2735 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
2736 global_count= GLOBAL2_COUNT;
2737
2738 for (x= 0; x < global_count; x++)
2739 {
2740 global_keys[x]= global_pairs[x].key;
2741 global_keys_length[x]= global_pairs[x].key_length;
2742 }
2743
2744 return 0;
2745 }
2746
2747 static test_return generate_data(memcached_st *memc)
2748 {
2749 execute_set(memc, global_pairs, global_count);
2750
2751 return 0;
2752 }
2753
2754 static test_return generate_data_with_stats(memcached_st *memc)
2755 {
2756 memcached_stat_st *stat_p;
2757 memcached_return rc;
2758 uint32_t host_index= 0;
2759 execute_set(memc, global_pairs, global_count);
2760
2761 //TODO: hosts used size stats
2762 stat_p= memcached_stat(memc, NULL, &rc);
2763 assert(stat_p);
2764
2765 for (host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
2766 {
2767 /* This test was changes so that "make test" would work properlly */
2768 #ifdef DEBUG
2769 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);
2770 #endif
2771 assert((unsigned long long)(stat_p + host_index)->bytes);
2772 }
2773
2774 memcached_stat_free(NULL, stat_p);
2775
2776 return 0;
2777 }
2778 static test_return generate_buffer_data(memcached_st *memc)
2779 {
2780 size_t latch= 0;
2781
2782 latch= 1;
2783 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2784 generate_data(memc);
2785
2786 return 0;
2787 }
2788
2789 static test_return get_read_count(memcached_st *memc)
2790 {
2791 unsigned int x;
2792 memcached_return rc;
2793 memcached_st *memc_clone;
2794
2795 memc_clone= memcached_clone(NULL, memc);
2796 assert(memc_clone);
2797
2798 memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
2799
2800 {
2801 char *return_value;
2802 size_t return_value_length;
2803 uint32_t flags;
2804 uint32_t count;
2805
2806 for (x= count= 0; x < global_count; x++)
2807 {
2808 return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
2809 &return_value_length, &flags, &rc);
2810 if (rc == MEMCACHED_SUCCESS)
2811 {
2812 count++;
2813 if (return_value)
2814 free(return_value);
2815 }
2816 }
2817 fprintf(stderr, "\t%u -> %u", global_count, count);
2818 }
2819
2820 memcached_free(memc_clone);
2821
2822 return 0;
2823 }
2824
2825 static test_return get_read(memcached_st *memc)
2826 {
2827 unsigned int x;
2828 memcached_return rc;
2829
2830 {
2831 char *return_value;
2832 size_t return_value_length;
2833 uint32_t flags;
2834
2835 for (x= 0; x < global_count; x++)
2836 {
2837 return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
2838 &return_value_length, &flags, &rc);
2839 /*
2840 assert(return_value);
2841 assert(rc == MEMCACHED_SUCCESS);
2842 */
2843 if (rc == MEMCACHED_SUCCESS && return_value)
2844 free(return_value);
2845 }
2846 }
2847
2848 return 0;
2849 }
2850
2851 static test_return mget_read(memcached_st *memc)
2852 {
2853 memcached_return rc;
2854
2855 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2856 assert(rc == MEMCACHED_SUCCESS);
2857 /* Turn this into a help function */
2858 {
2859 char return_key[MEMCACHED_MAX_KEY];
2860 size_t return_key_length;
2861 char *return_value;
2862 size_t return_value_length;
2863 uint32_t flags;
2864
2865 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
2866 &return_value_length, &flags, &rc)))
2867 {
2868 assert(return_value);
2869 assert(rc == MEMCACHED_SUCCESS);
2870 free(return_value);
2871 }
2872 }
2873
2874 return 0;
2875 }
2876
2877 static test_return mget_read_result(memcached_st *memc)
2878 {
2879 memcached_return rc;
2880
2881 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2882 assert(rc == MEMCACHED_SUCCESS);
2883 /* Turn this into a help function */
2884 {
2885 memcached_result_st results_obj;
2886 memcached_result_st *results;
2887
2888 results= memcached_result_create(memc, &results_obj);
2889
2890 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
2891 {
2892 assert(results);
2893 assert(rc == MEMCACHED_SUCCESS);
2894 }
2895
2896 memcached_result_free(&results_obj);
2897 }
2898
2899 return 0;
2900 }
2901
2902 static test_return mget_read_function(memcached_st *memc)
2903 {
2904 memcached_return rc;
2905 unsigned int counter;
2906 memcached_execute_function callbacks[1];
2907
2908 rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
2909 assert(rc == MEMCACHED_SUCCESS);
2910
2911 callbacks[0]= &callback_counter;
2912 counter= 0;
2913 rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
2914
2915 return 0;
2916 }
2917
2918 static test_return delete_generate(memcached_st *memc)
2919 {
2920 unsigned int x;
2921
2922 for (x= 0; x < global_count; x++)
2923 {
2924 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2925 }
2926
2927 return 0;
2928 }
2929
2930 static test_return delete_buffer_generate(memcached_st *memc)
2931 {
2932 size_t latch= 0;
2933 unsigned int x;
2934
2935 latch= 1;
2936 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
2937
2938 for (x= 0; x < global_count; x++)
2939 {
2940 (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
2941 }
2942
2943 return 0;
2944 }
2945
2946 static test_return add_host_test1(memcached_st *memc)
2947 {
2948 unsigned int x;
2949 memcached_return rc;
2950 char servername[]= "0.example.com";
2951 memcached_server_st *servers;
2952
2953 servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
2954 assert(servers);
2955 assert(1 == memcached_server_list_count(servers));
2956
2957 for (x= 2; x < 20; x++)
2958 {
2959 char buffer[SMALL_STRING_LEN];
2960
2961 snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
2962 servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
2963 &rc);
2964 assert(rc == MEMCACHED_SUCCESS);
2965 assert(x == memcached_server_list_count(servers));
2966 }
2967
2968 rc= memcached_server_push(memc, servers);
2969 assert(rc == MEMCACHED_SUCCESS);
2970 rc= memcached_server_push(memc, servers);
2971 assert(rc == MEMCACHED_SUCCESS);
2972
2973 memcached_server_list_free(servers);
2974
2975 return 0;
2976 }
2977
2978 static memcached_return pre_nonblock(memcached_st *memc)
2979 {
2980 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2981
2982 return MEMCACHED_SUCCESS;
2983 }
2984
2985 static memcached_return pre_nonblock_binary(memcached_st *memc)
2986 {
2987 memcached_return rc= MEMCACHED_FAILURE;
2988 memcached_st *memc_clone;
2989
2990 memc_clone= memcached_clone(NULL, memc);
2991 assert(memc_clone);
2992 // The memcached_version needs to be done on a clone, because the server
2993 // will not toggle protocol on an connection.
2994 memcached_version(memc_clone);
2995
2996 if (memc_clone->hosts[0].major_version >= 1 && memc_clone->hosts[0].minor_version > 2)
2997 {
2998 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
2999 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3000 assert(rc == MEMCACHED_SUCCESS);
3001 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3002 }
3003
3004 memcached_free(memc_clone);
3005 return rc;
3006 }
3007
3008 static memcached_return pre_murmur(memcached_st *memc)
3009 {
3010 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
3011
3012 return MEMCACHED_SUCCESS;
3013 }
3014
3015 static memcached_return pre_jenkins(memcached_st *memc)
3016 {
3017 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
3018
3019 return MEMCACHED_SUCCESS;
3020 }
3021
3022
3023 static memcached_return pre_md5(memcached_st *memc)
3024 {
3025 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
3026
3027 return MEMCACHED_SUCCESS;
3028 }
3029
3030 static memcached_return pre_crc(memcached_st *memc)
3031 {
3032 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
3033
3034 return MEMCACHED_SUCCESS;
3035 }
3036
3037 static memcached_return pre_hsieh(memcached_st *memc)
3038 {
3039 #ifdef HAVE_HSIEH_HASH
3040 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
3041 return MEMCACHED_SUCCESS;
3042 #else
3043 (void) memc;
3044 return MEMCACHED_FAILURE;
3045 #endif
3046 }
3047
3048 static memcached_return pre_hash_fnv1_64(memcached_st *memc)
3049 {
3050 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
3051
3052 return MEMCACHED_SUCCESS;
3053 }
3054
3055 static memcached_return pre_hash_fnv1a_64(memcached_st *memc)
3056 {
3057 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
3058
3059 return MEMCACHED_SUCCESS;
3060 }
3061
3062 static memcached_return pre_hash_fnv1_32(memcached_st *memc)
3063 {
3064 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
3065
3066 return MEMCACHED_SUCCESS;
3067 }
3068
3069 static memcached_return pre_hash_fnv1a_32(memcached_st *memc)
3070 {
3071 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
3072
3073 return MEMCACHED_SUCCESS;
3074 }
3075
3076 static memcached_return pre_behavior_ketama(memcached_st *memc)
3077 {
3078 memcached_return rc;
3079 uint64_t value;
3080
3081 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
3082 assert(rc == MEMCACHED_SUCCESS);
3083
3084 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
3085 assert(value == 1);
3086
3087 return MEMCACHED_SUCCESS;
3088 }
3089
3090 static memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
3091 {
3092 memcached_return rc;
3093 uint64_t value;
3094
3095 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
3096 assert(rc == MEMCACHED_SUCCESS);
3097
3098 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
3099 assert(value == 1);
3100
3101 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
3102 assert(rc == MEMCACHED_SUCCESS);
3103
3104 value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
3105 assert(value == MEMCACHED_HASH_MD5);
3106 return MEMCACHED_SUCCESS;
3107 }
3108
3109 static memcached_return pre_binary(memcached_st *memc)
3110 {
3111 memcached_return rc= MEMCACHED_FAILURE;
3112 memcached_st *memc_clone;
3113
3114 memc_clone= memcached_clone(NULL, memc);
3115 assert(memc_clone);
3116 // The memcached_version needs to be done on a clone, because the server
3117 // will not toggle protocol on an connection.
3118 memcached_version(memc_clone);
3119
3120 if (memc_clone->hosts[0].major_version >= 1 && memc_clone->hosts[0].minor_version > 2)
3121 {
3122 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
3123 assert(rc == MEMCACHED_SUCCESS);
3124 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
3125 }
3126
3127 memcached_free(memc_clone);
3128 return rc;
3129 }
3130
3131 static memcached_return pre_replication(memcached_st *memc)
3132 {
3133 memcached_return rc= MEMCACHED_FAILURE;
3134 if (pre_binary(memc) == MEMCACHED_SUCCESS)
3135 {
3136 /*
3137 * Make sure that we store the item on all servers
3138 * (master + replicas == number of servers)
3139 */
3140 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
3141 memc->number_of_hosts - 1);
3142 assert(rc == MEMCACHED_SUCCESS);
3143 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS) == memc->number_of_hosts - 1);
3144 }
3145
3146 return rc;
3147 }
3148
3149 static memcached_return pre_replication_noblock(memcached_st *memc)
3150 {
3151 memcached_return rc= MEMCACHED_FAILURE;
3152 if (pre_replication(memc) == MEMCACHED_SUCCESS &&
3153 pre_nonblock(memc) == MEMCACHED_SUCCESS)
3154 rc= MEMCACHED_SUCCESS;
3155
3156 return rc;
3157 }
3158
3159 static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
3160 {
3161 free(mem);
3162 }
3163
3164 static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
3165 {
3166 void *ret= malloc(size);
3167 if (ret != NULL)
3168 memset(ret, 0xff, size);
3169
3170 return ret;
3171 }
3172
3173 static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
3174 {
3175 return realloc(mem, size);
3176 }
3177
3178 static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size)
3179 {
3180 return calloc(nelem, size);
3181 }
3182
3183 static memcached_return set_prefix(memcached_st *memc)
3184 {
3185 memcached_return rc;
3186 const char *key= "mine";
3187 char *value;
3188
3189 /* Make sure be default none exists */
3190 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3191 assert(rc == MEMCACHED_FAILURE);
3192
3193 /* Test a clean set */
3194 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3195 assert(rc == MEMCACHED_SUCCESS);
3196
3197 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3198 assert(memcmp(value, key, 4) == 0);
3199 assert(rc == MEMCACHED_SUCCESS);
3200
3201 /* Test that we can turn it off */
3202 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3203 assert(rc == MEMCACHED_SUCCESS);
3204
3205 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3206 assert(rc == MEMCACHED_FAILURE);
3207
3208 /* Now setup for main test */
3209 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
3210 assert(rc == MEMCACHED_SUCCESS);
3211
3212 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3213 assert(rc == MEMCACHED_SUCCESS);
3214 assert(memcmp(value, key, 4) == 0);
3215
3216 /* Set to Zero, and then Set to something too large */
3217 {
3218 char long_key[255];
3219 memset(long_key, 0, 255);
3220
3221 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
3222 assert(rc == MEMCACHED_SUCCESS);
3223
3224 value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
3225 assert(rc == MEMCACHED_FAILURE);
3226 assert(value == NULL);
3227
3228 /* Test a long key for failure */
3229 /* TODO, extend test to determine based on setting, what result should be */
3230 strcpy(long_key, "Thisismorethentheallottednumberofcharacters");
3231 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3232 //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3233 assert(rc == MEMCACHED_SUCCESS);
3234
3235 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3236 strcpy(long_key, "This is more then the allotted number of characters");
3237 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3238 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3239
3240 /* Test for a bad prefix, but with a short key */
3241 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
3242 assert(rc == MEMCACHED_SUCCESS);
3243
3244 strcpy(long_key, "dog cat");
3245 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
3246 assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
3247 }
3248
3249 return MEMCACHED_SUCCESS;
3250 }
3251
3252 #ifdef MEMCACHED_ENABLE_DEPRECATED
3253 static memcached_return deprecated_set_memory_alloc(memcached_st *memc)
3254 {
3255 void *test_ptr= NULL;
3256 void *cb_ptr= NULL;
3257 {
3258 memcached_malloc_function malloc_cb=
3259 (memcached_malloc_function)my_malloc;
3260 cb_ptr= *(void **)&malloc_cb;
3261 memcached_return rc;
3262
3263 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr);
3264 assert(rc == MEMCACHED_SUCCESS);
3265 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
3266 assert(rc == MEMCACHED_SUCCESS);
3267 assert(test_ptr == cb_ptr);
3268 }
3269
3270 {
3271 memcached_realloc_function realloc_cb=
3272 (memcached_realloc_function)my_realloc;
3273 cb_ptr= *(void **)&realloc_cb;
3274 memcached_return rc;
3275
3276 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr);
3277 assert(rc == MEMCACHED_SUCCESS);
3278 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
3279 assert(rc == MEMCACHED_SUCCESS);
3280 assert(test_ptr == cb_ptr);
3281 }
3282
3283 {
3284 memcached_free_function free_cb=
3285 (memcached_free_function)my_free;
3286 cb_ptr= *(void **)&free_cb;
3287 memcached_return rc;
3288
3289 rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr);
3290 assert(rc == MEMCACHED_SUCCESS);
3291 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
3292 assert(rc == MEMCACHED_SUCCESS);
3293 assert(test_ptr == cb_ptr);
3294 }
3295 return MEMCACHED_SUCCESS;
3296 }
3297 #endif
3298
3299 static memcached_return set_memory_alloc(memcached_st *memc)
3300 {
3301 memcached_return rc;
3302 rc= memcached_set_memory_allocators(memc, NULL, my_free,
3303 my_realloc, my_calloc);
3304 assert(rc == MEMCACHED_FAILURE);
3305
3306 rc= memcached_set_memory_allocators(memc, my_malloc, my_free,
3307 my_realloc, my_calloc);
3308
3309 memcached_malloc_function mem_malloc;
3310 memcached_free_function mem_free;
3311 memcached_realloc_function mem_realloc;
3312 memcached_calloc_function mem_calloc;
3313 memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
3314 &mem_realloc, &mem_calloc);
3315
3316 assert(mem_malloc == my_malloc);
3317 assert(mem_realloc == my_realloc);
3318 assert(mem_calloc == my_calloc);
3319 assert(mem_free == my_free);
3320
3321 return MEMCACHED_SUCCESS;
3322 }
3323
3324 static memcached_return enable_consistent(memcached_st *memc)
3325 {
3326 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
3327 memcached_hash hash;
3328 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
3329 if (pre_hsieh(memc) != MEMCACHED_SUCCESS)
3330 return MEMCACHED_FAILURE;
3331
3332 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
3333 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
3334
3335 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
3336 assert(hash == MEMCACHED_HASH_HSIEH);
3337
3338
3339 return MEMCACHED_SUCCESS;
3340 }
3341
3342 static memcached_return enable_cas(memcached_st *memc)
3343 {
3344 unsigned int set= 1;
3345
3346 memcached_version(memc);
3347
3348 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3349 || memc->hosts[0].minor_version > 2)
3350 {
3351 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, set);
3352
3353 return MEMCACHED_SUCCESS;
3354 }
3355
3356 return MEMCACHED_FAILURE;
3357 }
3358
3359 static memcached_return check_for_1_2_3(memcached_st *memc)
3360 {
3361 memcached_version(memc);
3362
3363 if ((memc->hosts[0].major_version >= 1 && (memc->hosts[0].minor_version == 2 && memc->hosts[0].micro_version >= 4))
3364 || memc->hosts[0].minor_version > 2)
3365 return MEMCACHED_SUCCESS;
3366
3367 return MEMCACHED_FAILURE;
3368 }
3369
3370 static memcached_return pre_unix_socket(memcached_st *memc)
3371 {
3372 memcached_return rc;
3373 struct stat buf;
3374
3375 memcached_server_list_free(memc->hosts);
3376 memc->hosts= NULL;
3377 memc->number_of_hosts= 0;
3378
3379 if (stat("/tmp/memcached.socket", &buf))
3380 return MEMCACHED_FAILURE;
3381
3382 rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0);
3383
3384 return rc;
3385 }
3386
3387 static memcached_return pre_nodelay(memcached_st *memc)
3388 {
3389 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
3390 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 0);
3391
3392 return MEMCACHED_SUCCESS;
3393 }
3394
3395 static memcached_return pre_settimer(memcached_st *memc)
3396 {
3397 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SND_TIMEOUT, 1000);
3398 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 1000);
3399
3400 return MEMCACHED_SUCCESS;
3401 }
3402
3403 static memcached_return poll_timeout(memcached_st *memc)
3404 {
3405 size_t timeout;
3406
3407 timeout= 100;
3408
3409 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
3410
3411 timeout= (size_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
3412
3413 assert(timeout == 100);
3414
3415 return MEMCACHED_SUCCESS;
3416 }
3417
3418 static test_return noreply_test(memcached_st *memc)
3419 {
3420 memcached_return ret;
3421 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
3422 assert(ret == MEMCACHED_SUCCESS);
3423 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
3424 assert(ret == MEMCACHED_SUCCESS);
3425 ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
3426 assert(ret == MEMCACHED_SUCCESS);
3427 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1);
3428 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1);
3429 assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS) == 1);
3430
3431 for (int count=0; count < 5; ++count)
3432 {
3433 for (int x=0; x < 100; ++x)
3434 {
3435 char key[10];
3436 size_t len= (size_t)sprintf(key, "%d", x);
3437 switch (count)
3438 {
3439 case 0:
3440 ret=memcached_add(memc, key, len, key, len, 0, 0);
3441 break;
3442 case 1:
3443 ret=memcached_replace(memc, key, len, key, len, 0, 0);
3444 break;
3445 case 2:
3446 ret=memcached_set(memc, key, len, key, len, 0, 0);
3447 break;
3448 case 3:
3449 ret=memcached_append(memc, key, len, key, len, 0, 0);
3450 break;
3451 case 4:
3452 ret=memcached_prepend(memc, key, len, key, len, 0, 0);
3453 break;
3454 default:
3455 assert(count);
3456 break;
3457 }
3458 assert(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED);
3459 }
3460
3461 /*
3462 ** NOTE: Don't ever do this in your code! this is not a supported use of the
3463 ** API and is _ONLY_ done this way to verify that the library works the
3464 ** way it is supposed to do!!!!
3465 */
3466 int no_msg=0;
3467 for (uint32_t x=0; x < memc->number_of_hosts; ++x)
3468 no_msg+=(int)(memc->hosts[x].cursor_active);
3469
3470 assert(no_msg == 0);
3471 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3472
3473 /*
3474 ** Now validate that all items was set properly!
3475 */
3476 for (int x=0; x < 100; ++x)
3477 {
3478 char key[10];
3479 size_t len= (size_t)sprintf(key, "%d", x);
3480 size_t length;
3481 uint32_t flags;
3482 char* value=memcached_get(memc, key, strlen(key),
3483 &length, &flags, &ret);
3484 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3485 switch (count)
3486 {
3487 case 0: /* FALLTHROUGH */
3488 case 1: /* FALLTHROUGH */
3489 case 2:
3490 assert(strncmp(value, key, len) == 0);
3491 assert(len == length);
3492 break;
3493 case 3:
3494 assert(length == len * 2);
3495 break;
3496 case 4:
3497 assert(length == len * 3);
3498 break;
3499 default:
3500 assert(count);
3501 break;
3502 }
3503 free(value);
3504 }
3505 }
3506
3507 /* Try setting an illegal cas value (should not return an error to
3508 * the caller (because we don't expect a return message from the server)
3509 */
3510 const char* keys[]= {"0"};
3511 size_t lengths[]= {1};
3512 size_t length;
3513 uint32_t flags;
3514 memcached_result_st results_obj;
3515 memcached_result_st *results;
3516 ret= memcached_mget(memc, keys, lengths, 1);
3517 assert(ret == MEMCACHED_SUCCESS);
3518
3519 results= memcached_result_create(memc, &results_obj);
3520 assert(results);
3521 results= memcached_fetch_result(memc, &results_obj, &ret);
3522 assert(results);
3523 assert(ret == MEMCACHED_SUCCESS);
3524 uint64_t cas= memcached_result_cas(results);
3525 memcached_result_free(&results_obj);
3526
3527 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3528 assert(ret == MEMCACHED_SUCCESS);
3529
3530 /*
3531 * The item will have a new cas value, so try to set it again with the old
3532 * value. This should fail!
3533 */
3534 ret= memcached_cas(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0, cas);
3535 assert(ret == MEMCACHED_SUCCESS);
3536 assert(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
3537 char* value=memcached_get(memc, keys[0], lengths[0], &length, &flags, &ret);
3538 assert(ret == MEMCACHED_SUCCESS && value != NULL);
3539 free(value);
3540
3541 return TEST_SUCCESS;
3542 }
3543
3544 static test_return analyzer_test(memcached_st *memc)
3545 {
3546 memcached_return rc;
3547 memcached_stat_st *memc_stat;
3548 memcached_analysis_st *report;
3549
3550 memc_stat= memcached_stat(memc, NULL, &rc);
3551 assert(rc == MEMCACHED_SUCCESS);
3552 assert(memc_stat);
3553
3554 report= memcached_analyze(memc, memc_stat, &rc);
3555 assert(rc == MEMCACHED_SUCCESS);
3556 assert(report);
3557
3558 free(report);
3559 memcached_stat_free(NULL, memc_stat);
3560
3561 return TEST_SUCCESS;
3562 }
3563
3564 /* Count the objects */
3565 static memcached_return callback_dump_counter(memcached_st *ptr __attribute__((unused)),
3566 const char *key __attribute__((unused)),
3567 size_t key_length __attribute__((unused)),
3568 void *context)
3569 {
3570 uint32_t *counter= (uint32_t *)context;
3571
3572 *counter= *counter + 1;
3573
3574 return MEMCACHED_SUCCESS;
3575 }
3576
3577 static test_return dump_test(memcached_st *memc)
3578 {
3579 memcached_return rc;
3580 uint32_t counter= 0;
3581 memcached_dump_func callbacks[1];
3582 test_return main_rc;
3583
3584 callbacks[0]= &callback_dump_counter;
3585
3586 /* No support for Binary protocol yet */
3587 if (memc->flags & MEM_BINARY_PROTOCOL)
3588 return TEST_SUCCESS;
3589
3590 main_rc= set_test3(memc);
3591
3592 assert (main_rc == TEST_SUCCESS);
3593
3594 rc= memcached_dump(memc, callbacks, (void *)&counter, 1);
3595 assert(rc == MEMCACHED_SUCCESS);
3596
3597 /* We may have more then 32 if our previous flush has not completed */
3598 assert(counter >= 32);
3599
3600 return TEST_SUCCESS;
3601 }
3602
3603 #ifdef HAVE_LIBMEMCACHEDUTIL
3604 static void* connection_release(void *arg) {
3605 struct {
3606 memcached_pool_st* pool;
3607 memcached_st* mmc;
3608 } *resource= arg;
3609
3610 usleep(250);
3611 assert(memcached_pool_push(resource->pool, resource->mmc) == MEMCACHED_SUCCESS);
3612 return arg;
3613 }
3614
3615 static test_return connection_pool_test(memcached_st *memc)
3616 {
3617 memcached_pool_st* pool= memcached_pool_create(memc, 5, 10);
3618 assert(pool != NULL);
3619 memcached_st* mmc[10];
3620 memcached_return rc;
3621
3622 for (int x= 0; x < 10; ++x) {
3623 mmc[x]= memcached_pool_pop(pool, false, &rc);
3624 assert(mmc[x] != NULL);
3625 assert(rc == MEMCACHED_SUCCESS);
3626 }
3627
3628 assert(memcached_pool_pop(pool, false, &rc) == NULL);
3629 assert(rc == MEMCACHED_SUCCESS);
3630
3631 pthread_t tid;
3632 struct {
3633 memcached_pool_st* pool;
3634 memcached_st* mmc;
3635 } item= { .pool = pool, .mmc = mmc[9] };
3636 pthread_create(&tid, NULL, connection_release, &item);
3637 mmc[9]= memcached_pool_pop(pool, true, &rc);
3638 assert(rc == MEMCACHED_SUCCESS);
3639 pthread_join(tid, NULL);
3640 assert(mmc[9] == item.mmc);
3641 const char *key= "key";
3642 size_t keylen= strlen(key);
3643
3644 // verify that I can do ops with all connections
3645 rc= memcached_set(mmc[0], key, keylen, "0", 1, 0, 0);
3646 assert(rc == MEMCACHED_SUCCESS);
3647
3648 for (unsigned int x= 0; x < 10; ++x) {
3649 uint64_t number_value;
3650 rc= memcached_increment(mmc[x], key, keylen, 1, &number_value);
3651 assert(rc == MEMCACHED_SUCCESS);
3652 assert(number_value == (x+1));
3653 }
3654
3655 // Release them..
3656 for (int x= 0; x < 10; ++x)
3657 assert(memcached_pool_push(pool, mmc[x]) == MEMCACHED_SUCCESS);
3658
3659 assert(memcached_pool_destroy(pool) == memc);
3660 return TEST_SUCCESS;
3661 }
3662 #endif
3663
3664 static test_return replication_set_test(memcached_st *memc)
3665 {
3666 memcached_return rc;
3667 memcached_st *memc_clone= memcached_clone(NULL, memc);
3668 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
3669
3670 rc= memcached_set(memc, "bubba", 5, "0", 1, 0, 0);
3671 assert(rc == MEMCACHED_SUCCESS);
3672
3673 /*
3674 ** We are using the quiet commands to store the replicas, so we need
3675 ** to ensure that all of them are processed before we can continue.
3676 ** In the test we go directly from storing the object to trying to
3677 ** receive the object from all of the different servers, so we
3678 ** could end up in a race condition (the memcached server hasn't yet
3679 ** processed the quiet command from the replication set when it process
3680 ** the request from the other client (created by the clone)). As a
3681 ** workaround for that we call memcached_quit to send the quit command
3682 ** to the server and wait for the response ;-) If you use the test code
3683 ** as an example for your own code, please note that you shouldn't need
3684 ** to do this ;-)
3685 */
3686 memcached_quit(memc);
3687
3688 /*
3689 ** "bubba" should now be stored on all of our servers. We don't have an
3690 ** easy to use API to address each individual server, so I'll just iterate
3691 ** through a bunch of "master keys" and I should most likely hit all of the
3692 ** servers...
3693 */
3694 for (int x= 'a'; x <= 'z'; ++x)
3695 {
3696 char key[2]= { [0]= (char)x };
3697 size_t len;
3698 uint32_t flags;
3699 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
3700 &len, &flags, &rc);
3701 assert(rc == MEMCACHED_SUCCESS);
3702 assert(val != NULL);
3703 free(val);
3704 }
3705
3706 memcached_free(memc_clone);
3707
3708 return TEST_SUCCESS;
3709 }
3710
3711 static test_return replication_get_test(memcached_st *memc)
3712 {
3713 memcached_return rc;
3714
3715 /*
3716 * Don't do the following in your code. I am abusing the internal details
3717 * within the library, and this is not a supported interface.
3718 * This is to verify correct behavior in the library
3719 */
3720 for (uint32_t host= 0; host < memc->number_of_hosts; ++host)
3721 {
3722 memcached_st *memc_clone= memcached_clone(NULL, memc);
3723 memc_clone->hosts[host].port= 0;
3724
3725 for (int x= 'a'; x <= 'z'; ++x)
3726 {
3727 char key[2]= { [0]= (char)x };
3728 size_t len;
3729 uint32_t flags;
3730 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
3731 &len, &flags, &rc);
3732 assert(rc == MEMCACHED_SUCCESS);
3733 assert(val != NULL);
3734 free(val);
3735 }
3736
3737 memcached_free(memc_clone);
3738 }
3739
3740 return TEST_SUCCESS;
3741 }
3742
3743 static test_return replication_mget_test(memcached_st *memc)
3744 {
3745 memcached_return rc;
3746 memcached_st *memc_clone= memcached_clone(NULL, memc);
3747 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
3748
3749 const char *keys[]= { "bubba", "key1", "key2", "key3" };
3750 size_t len[]= { 5, 4, 4, 4 };
3751
3752 for (int x=0; x< 4; ++x)
3753 {
3754 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
3755 assert(rc == MEMCACHED_SUCCESS);
3756 }
3757
3758 /*
3759 ** We are using the quiet commands to store the replicas, so we need
3760 ** to ensure that all of them are processed before we can continue.
3761 ** In the test we go directly from storing the object to trying to
3762 ** receive the object from all of the different servers, so we
3763 ** could end up in a race condition (the memcached server hasn't yet
3764 ** processed the quiet command from the replication set when it process
3765 ** the request from the other client (created by the clone)). As a
3766 ** workaround for that we call memcached_quit to send the quit command
3767 ** to the server and wait for the response ;-) If you use the test code
3768 ** as an example for your own code, please note that you shouldn't need
3769 ** to do this ;-)
3770 */
3771 memcached_quit(memc);
3772
3773 /*
3774 * Don't do the following in your code. I am abusing the internal details
3775 * within the library, and this is not a supported interface.
3776 * This is to verify correct behavior in the library
3777 */
3778 memcached_result_st result_obj;
3779 for (uint32_t host= 0; host < memc_clone->number_of_hosts; host++)
3780 {
3781 memcached_st *new_clone= memcached_clone(NULL, memc);
3782 new_clone->hosts[host].port= 0;
3783
3784 for (int x= 'a'; x <= 'z'; ++x)
3785 {
3786 const char key[2]= { [0]= (const char)x };
3787
3788 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
3789 assert(rc == MEMCACHED_SUCCESS);
3790
3791 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
3792 assert(results);
3793
3794 int hits= 0;
3795 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
3796 {
3797 hits++;
3798 }
3799 assert(hits == 4);
3800 memcached_result_free(&result_obj);
3801 }
3802
3803 memcached_free(new_clone);
3804 }
3805
3806 memcached_free(memc_clone);
3807
3808 return TEST_SUCCESS;
3809 }
3810
3811 static test_return replication_delete_test(memcached_st *memc)
3812 {
3813 memcached_return rc;
3814 memcached_st *memc_clone= memcached_clone(NULL, memc);
3815 /* Delete the items from all of the servers except 1 */
3816 uint64_t repl= memcached_behavior_get(memc,
3817 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
3818 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, --repl);
3819
3820 const char *keys[]= { "bubba", "key1", "key2", "key3" };
3821 size_t len[]= { 5, 4, 4, 4 };
3822
3823 for (int x=0; x< 4; ++x)
3824 {
3825 rc= memcached_delete_by_key(memc, keys[0], len[0], keys[x], len[x], 0);
3826 assert(rc == MEMCACHED_SUCCESS);
3827 }
3828
3829 /*
3830 * Don't do the following in your code. I am abusing the internal details
3831 * within the library, and this is not a supported interface.
3832 * This is to verify correct behavior in the library
3833 */
3834 uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
3835 for (uint32_t x= 0; x < (repl + 1); ++x)
3836 {
3837 memc_clone->hosts[hash].port= 0;
3838 if (++hash == memc_clone->number_of_hosts)
3839 hash= 0;
3840 }
3841
3842 memcached_result_st result_obj;
3843 for (uint32_t host= 0; host < memc_clone->number_of_hosts; ++host)
3844 {
3845 for (int x= 'a'; x <= 'z'; ++x)
3846 {
3847 const char key[2]= { [0]= (const char)x };
3848
3849 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 4);
3850 assert(rc == MEMCACHED_SUCCESS);
3851
3852 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
3853 assert(results);
3854
3855 int hits= 0;
3856 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
3857 {
3858 ++hits;
3859 }
3860 assert(hits == 4);
3861 memcached_result_free(&result_obj);
3862 }
3863 }
3864 memcached_free(memc_clone);
3865
3866 return TEST_SUCCESS;
3867 }
3868
3869 static void increment_request_id(uint16_t *id)
3870 {
3871 (*id)++;
3872 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
3873 *id= 0;
3874 }
3875
3876 static uint16_t *get_udp_request_ids(memcached_st *memc)
3877 {
3878 uint16_t *ids= malloc(sizeof(uint16_t) * memc->number_of_hosts);
3879 assert(ids != NULL);
3880 unsigned int x;
3881
3882 for (x= 0; x < memc->number_of_hosts; x++)
3883 ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) memc->hosts[x].write_buffer);
3884
3885 return ids;
3886 }
3887
3888 static test_return post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
3889 {
3890 unsigned int x;
3891 memcached_server_st *cur_server = memc->hosts;
3892 uint16_t *cur_req_ids = get_udp_request_ids(memc);
3893
3894 for (x= 0; x < memc->number_of_hosts; x++)
3895 {
3896 assert(cur_server[x].cursor_active == 0);
3897 assert(cur_req_ids[x] == expected_req_ids[x]);
3898 }
3899 free(expected_req_ids);
3900 free(cur_req_ids);
3901
3902 return TEST_SUCCESS;
3903 }
3904
3905 /*
3906 ** There is a little bit of a hack here, instead of removing
3907 ** the servers, I just set num host to 0 and them add then new udp servers
3908 **/
3909 static memcached_return init_udp(memcached_st *memc)
3910 {
3911 memcached_version(memc);
3912 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
3913 if (memc->hosts[0].major_version != 1 || memc->hosts[0].minor_version != 2
3914 || memc->hosts[0].micro_version < 6)
3915 return MEMCACHED_FAILURE;
3916
3917 uint32_t num_hosts= memc->number_of_hosts;
3918 unsigned int x= 0;
3919 memcached_server_st servers[num_hosts];
3920 memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts);
3921 for (x= 0; x < num_hosts; x++)
3922 memcached_server_free(&memc->hosts[x]);
3923
3924 memc->number_of_hosts= 0;
3925 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
3926 for (x= 0; x < num_hosts; x++)
3927 {
3928 assert(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
3929 assert(memc->hosts[x].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
3930 }
3931
3932 return MEMCACHED_SUCCESS;
3933 }
3934
3935 static memcached_return binary_init_udp(memcached_st *memc)
3936 {
3937 pre_binary(memc);
3938 return init_udp(memc);
3939 }
3940
3941 /* Make sure that I cant add a tcp server to a udp client */
3942 static test_return add_tcp_server_udp_client_test(memcached_st *memc)
3943 {
3944 memcached_server_st server;
3945 memcached_server_clone(&server, &memc->hosts[0]);
3946 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3947 assert(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3948 return TEST_SUCCESS;
3949 }
3950
3951 /* Make sure that I cant add a udp server to a tcp client */
3952 static test_return add_udp_server_tcp_client_test(memcached_st *memc)
3953 {
3954 memcached_server_st server;
3955 memcached_server_clone(&server, &memc->hosts[0]);
3956 assert(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
3957
3958 memcached_st tcp_client;
3959 memcached_create(&tcp_client);
3960 assert(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
3961 return TEST_SUCCESS;
3962 }
3963
3964 static test_return set_udp_behavior_test(memcached_st *memc)
3965 {
3966
3967 memcached_quit(memc);
3968 memc->number_of_hosts= 0;
3969 run_distribution(memc);
3970 assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1) == MEMCACHED_SUCCESS);
3971 assert(memc->flags & MEM_USE_UDP);
3972 assert(memc->flags & MEM_NOREPLY);;
3973
3974 assert(memc->number_of_hosts == 0);
3975
3976 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0);
3977 assert(!(memc->flags & MEM_USE_UDP));
3978 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY,0);
3979 assert(!(memc->flags & MEM_NOREPLY));
3980 return TEST_SUCCESS;
3981 }
3982
3983 static test_return udp_set_test(memcached_st *memc)
3984 {
3985 unsigned int x= 0;
3986 unsigned int num_iters= 1025; //request id rolls over at 1024
3987 for (x= 0; x < num_iters;x++)
3988 {
3989 memcached_return rc;
3990 const char *key= "foo";
3991 const char *value= "when we sanitize";
3992 uint16_t *expected_ids= get_udp_request_ids(memc);
3993 unsigned int server_key= memcached_generate_hash(memc,key,strlen(key));
3994 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
3995 rc= memcached_set(memc, key, strlen(key),
3996 value, strlen(value),
3997 (time_t)0, (uint32_t)0);
3998 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
3999 /** NB, the check below assumes that if new write_ptr is less than
4000 * the original write_ptr that we have flushed. For large payloads, this
4001 * maybe an invalid assumption, but for the small payload we have it is OK
4002 */
4003 if (rc == MEMCACHED_SUCCESS ||
4004 memc->hosts[server_key].write_buffer_offset < init_offset)
4005 increment_request_id(&expected_ids[server_key]);
4006
4007 if (rc == MEMCACHED_SUCCESS)
4008 {
4009 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
4010 }
4011 else
4012 {
4013 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
4014 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
4015 }
4016 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
4017 }
4018 return TEST_SUCCESS;
4019 }
4020
4021 static test_return udp_buffered_set_test(memcached_st *memc)
4022 {
4023 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
4024 return udp_set_test(memc);
4025 }
4026
4027 static test_return udp_set_too_big_test(memcached_st *memc)
4028 {
4029 memcached_return rc;
4030 const char *key= "bar";
4031 char value[MAX_UDP_DATAGRAM_LENGTH];
4032 uint16_t *expected_ids= get_udp_request_ids(memc);
4033 rc= memcached_set(memc, key, strlen(key),
4034 value, MAX_UDP_DATAGRAM_LENGTH,
4035 (time_t)0, (uint32_t)0);
4036 assert(rc == MEMCACHED_WRITE_FAILURE);
4037 return post_udp_op_check(memc,expected_ids);
4038 }
4039
4040 static test_return udp_delete_test(memcached_st *memc)
4041 {
4042 unsigned int x= 0;
4043 unsigned int num_iters= 1025; //request id rolls over at 1024
4044 for (x= 0; x < num_iters;x++)
4045 {
4046 memcached_return rc;
4047 const char *key= "foo";
4048 uint16_t *expected_ids=get_udp_request_ids(memc);
4049 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
4050 size_t init_offset= memc->hosts[server_key].write_buffer_offset;
4051 rc= memcached_delete(memc, key, strlen(key), 0);
4052 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
4053 if (rc == MEMCACHED_SUCCESS || memc->hosts[server_key].write_buffer_offset < init_offset)
4054 increment_request_id(&expected_ids[server_key]);
4055 if (rc == MEMCACHED_SUCCESS)
4056 assert(memc->hosts[server_key].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
4057 else
4058 {
4059 assert(memc->hosts[server_key].write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
4060 assert(memc->hosts[server_key].write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
4061 }
4062 assert(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
4063 }
4064 return TEST_SUCCESS;
4065 }
4066
4067 static test_return udp_buffered_delete_test(memcached_st *memc)
4068 {
4069 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
4070 return udp_delete_test(memc);
4071 }
4072
4073 static test_return udp_verbosity_test(memcached_st *memc)
4074 {
4075 memcached_return rc;
4076 uint16_t *expected_ids= get_udp_request_ids(memc);
4077 unsigned int x;
4078 for (x= 0; x < memc->number_of_hosts;x++)
4079 increment_request_id(&expected_ids[x]);
4080
4081 rc= memcached_verbosity(memc,3);
4082 assert(rc == MEMCACHED_SUCCESS);
4083 return post_udp_op_check(memc,expected_ids);
4084 }
4085
4086 static test_return udp_quit_test(memcached_st *memc)
4087 {
4088 uint16_t *expected_ids= get_udp_request_ids(memc);
4089 memcached_quit(memc);
4090 return post_udp_op_check(memc, expected_ids);
4091 }
4092
4093 static test_return udp_flush_test(memcached_st *memc)
4094 {
4095 memcached_return rc;
4096 uint16_t *expected_ids= get_udp_request_ids(memc);
4097 unsigned int x;
4098 for (x= 0; x < memc->number_of_hosts;x++)
4099 increment_request_id(&expected_ids[x]);
4100
4101 rc= memcached_flush(memc,0);
4102 assert(rc == MEMCACHED_SUCCESS);
4103 return post_udp_op_check(memc,expected_ids);
4104 }
4105
4106 static test_return udp_incr_test(memcached_st *memc)
4107 {
4108 memcached_return rc;
4109 const char *key= "incr";
4110 const char *value= "1";
4111 rc= memcached_set(memc, key, strlen(key),
4112 value, strlen(value),
4113 (time_t)0, (uint32_t)0);
4114
4115 assert(rc == MEMCACHED_SUCCESS);
4116 uint16_t *expected_ids= get_udp_request_ids(memc);
4117 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
4118 increment_request_id(&expected_ids[server_key]);
4119 uint64_t newvalue;
4120 rc= memcached_increment(memc, key, strlen(key), 1, &newvalue);
4121 assert(rc == MEMCACHED_SUCCESS);
4122 return post_udp_op_check(memc, expected_ids);
4123 }
4124
4125 static test_return udp_decr_test(memcached_st *memc)
4126 {
4127 memcached_return rc;
4128 const char *key= "decr";
4129 const char *value= "1";
4130 rc= memcached_set(memc, key, strlen(key),
4131 value, strlen(value),
4132 (time_t)0, (uint32_t)0);
4133
4134 assert(rc == MEMCACHED_SUCCESS);
4135 uint16_t *expected_ids= get_udp_request_ids(memc);
4136 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
4137 increment_request_id(&expected_ids[server_key]);
4138 uint64_t newvalue;
4139 rc= memcached_decrement(memc, key, strlen(key), 1, &newvalue);
4140 assert(rc == MEMCACHED_SUCCESS);
4141 return post_udp_op_check(memc, expected_ids);
4142 }
4143
4144
4145 static test_return udp_stat_test(memcached_st *memc)
4146 {
4147 memcached_stat_st * rv= NULL;
4148 memcached_return rc;
4149 char args[]= "";
4150 uint16_t *expected_ids = get_udp_request_ids(memc);
4151 rv = memcached_stat(memc, args, &rc);
4152 free(rv);
4153 assert(rc == MEMCACHED_NOT_SUPPORTED);
4154 return post_udp_op_check(memc, expected_ids);
4155 }
4156
4157 static test_return udp_version_test(memcached_st *memc)
4158 {
4159 memcached_return rc;
4160 uint16_t *expected_ids = get_udp_request_ids(memc);
4161 rc = memcached_version(memc);
4162 assert(rc == MEMCACHED_NOT_SUPPORTED);
4163 return post_udp_op_check(memc, expected_ids);
4164 }
4165
4166 static test_return udp_get_test(memcached_st *memc)
4167 {
4168 memcached_return rc;
4169 const char *key= "foo";
4170 size_t vlen;
4171 uint16_t *expected_ids = get_udp_request_ids(memc);
4172 char *val= memcached_get(memc, key, strlen(key), &vlen, (uint32_t)0, &rc);
4173 assert(rc == MEMCACHED_NOT_SUPPORTED);
4174 assert(val == NULL);
4175 return post_udp_op_check(memc, expected_ids);
4176 }
4177
4178 static test_return udp_mixed_io_test(memcached_st *memc)
4179 {
4180 test_st current_op;
4181 test_st mixed_io_ops [] ={
4182 {"udp_set_test", 0, udp_set_test},
4183 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4184 {"udp_delete_test", 0, udp_delete_test},
4185 {"udp_verbosity_test", 0, udp_verbosity_test},
4186 {"udp_quit_test", 0, udp_quit_test},
4187 {"udp_flush_test", 0, udp_flush_test},
4188 {"udp_incr_test", 0, udp_incr_test},
4189 {"udp_decr_test", 0, udp_decr_test},
4190 {"udp_version_test", 0, udp_version_test}
4191 };
4192 unsigned int x= 0;
4193 for (x= 0; x < 500; x++)
4194 {
4195 current_op= mixed_io_ops[random() % 9];
4196 assert(current_op.function(memc) == TEST_SUCCESS);
4197 }
4198 return TEST_SUCCESS;
4199 }
4200
4201 static test_return hsieh_avaibility_test (memcached_st *memc)
4202 {
4203 memcached_return expected_rc= MEMCACHED_FAILURE;
4204 #ifdef HAVE_HSIEH_HASH
4205 expected_rc= MEMCACHED_SUCCESS;
4206 #endif
4207 memcached_return rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH,
4208 (uint64_t)MEMCACHED_HASH_HSIEH);
4209 assert(rc == expected_rc);
4210 return TEST_SUCCESS;
4211 }
4212
4213 static const char *list[]=
4214 {
4215 "apple",
4216 "beat",
4217 "carrot",
4218 "daikon",
4219 "eggplant",
4220 "flower",
4221 "green",
4222 "hide",
4223 "ick",
4224 "jack",
4225 "kick",
4226 "lime",
4227 "mushrooms",
4228 "nectarine",
4229 "orange",
4230 "peach",
4231 "quant",
4232 "ripen",
4233 "strawberry",
4234 "tang",
4235 "up",
4236 "volumne",
4237 "when",
4238 "yellow",
4239 "zip",
4240 NULL
4241 };
4242
4243 static test_return md5_run (memcached_st *memc __attribute__((unused)))
4244 {
4245 uint32_t x;
4246 const char **ptr;
4247 uint32_t values[]= { 3195025439U, 2556848621U, 3724893440U, 3332385401U,
4248 245758794U, 2550894432U, 121710495U, 3053817768U,
4249 1250994555U, 1862072655U, 2631955953U, 2951528551U,
4250 1451250070U, 2820856945U, 2060845566U, 3646985608U,
4251 2138080750U, 217675895U, 2230934345U, 1234361223U,
4252 3968582726U, 2455685270U, 1293568479U, 199067604U,
4253 2042482093U };
4254
4255
4256 for (ptr= list, x= 0; *ptr; ptr++, x++)
4257 {
4258 uint32_t hash_val;
4259
4260 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_MD5);
4261 assert(values[x] == hash_val);
4262 }
4263
4264 return TEST_SUCCESS;
4265 }
4266
4267 static test_return crc_run (memcached_st *memc __attribute__((unused)))
4268 {
4269 uint32_t x;
4270 const char **ptr;
4271 uint32_t values[]= { 10542U, 22009U, 14526U, 19510U, 19432U, 10199U, 20634U,
4272 9369U, 11511U, 10362U, 7893U, 31289U, 11313U, 9354U,
4273 7621U, 30628U, 15218U, 25967U, 2695U, 9380U,
4274 17300U, 28156U, 9192U, 20484U, 16925U };
4275
4276 for (ptr= list, x= 0; *ptr; ptr++, x++)
4277 {
4278 uint32_t hash_val;
4279
4280 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_CRC);
4281 assert(values[x] == hash_val);
4282 }
4283
4284 return TEST_SUCCESS;
4285 }
4286
4287 static test_return fnv1_64_run (memcached_st *memc __attribute__((unused)))
4288 {
4289 uint32_t x;
4290 const char **ptr;
4291 uint32_t values[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4292 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4293 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4294 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4295 2815549194U, 2562818319U, 224996066U, 2680194749U,
4296 3035305390U, 246890365U, 2395624193U, 4145193337U,
4297 1801941682U };
4298
4299 for (ptr= list, x= 0; *ptr; ptr++, x++)
4300 {
4301 uint32_t hash_val;
4302
4303 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4304 assert(values[x] == hash_val);
4305 }
4306
4307 return TEST_SUCCESS;
4308 }
4309
4310 static test_return fnv1a_64_run (memcached_st *memc __attribute__((unused)))
4311 {
4312 uint32_t x;
4313 const char **ptr;
4314 uint32_t values[]= { 1488911807U, 2500855813U, 1510099634U, 1390325195U,
4315 3647689787U, 3241528582U, 1669328060U, 2604311949U,
4316 734810122U, 1516407546U, 560948863U, 1767346780U,
4317 561034892U, 4156330026U, 3716417003U, 3475297030U,
4318 1518272172U, 227211583U, 3938128828U, 126112909U,
4319 3043416448U, 3131561933U, 1328739897U, 2455664041U,
4320 2272238452U };
4321
4322 for (ptr= list, x= 0; *ptr; ptr++, x++)
4323 {
4324 uint32_t hash_val;
4325
4326 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_64);
4327 assert(values[x] == hash_val);
4328 }
4329
4330 return TEST_SUCCESS;
4331 }
4332
4333 static test_return fnv1_32_run (memcached_st *memc __attribute__((unused)))
4334 {
4335 uint32_t x;
4336 const char **ptr;
4337 uint32_t values[]= { 67176023U, 1190179409U, 2043204404U, 3221866419U,
4338 2567703427U, 3787535528U, 4147287986U, 3500475733U,
4339 344481048U, 3865235296U, 2181839183U, 119581266U,
4340 510234242U, 4248244304U, 1362796839U, 103389328U,
4341 1449620010U, 182962511U, 3554262370U, 3206747549U,
4342 1551306158U, 4127558461U, 1889140833U, 2774173721U,
4343 1180552018U };
4344
4345
4346 for (ptr= list, x= 0; *ptr; ptr++, x++)
4347 {
4348 uint32_t hash_val;
4349
4350 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_32);
4351 assert(values[x] == hash_val);
4352 }
4353
4354 return TEST_SUCCESS;
4355 }
4356
4357 static test_return fnv1a_32_run (memcached_st *memc __attribute__((unused)))
4358 {
4359 uint32_t x;
4360 const char **ptr;
4361 uint32_t values[]= { 280767167U, 2421315013U, 3072375666U, 855001899U,
4362 459261019U, 3521085446U, 18738364U, 1625305005U,
4363 2162232970U, 777243802U, 3323728671U, 132336572U,
4364 3654473228U, 260679466U, 1169454059U, 2698319462U,
4365 1062177260U, 235516991U, 2218399068U, 405302637U,
4366 1128467232U, 3579622413U, 2138539289U, 96429129U,
4367 2877453236U };
4368
4369 for (ptr= list, x= 0; *ptr; ptr++, x++)
4370 {
4371 uint32_t hash_val;
4372
4373 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1A_32);
4374 assert(values[x] == hash_val);
4375 }
4376
4377 return TEST_SUCCESS;
4378 }
4379
4380 static test_return hsieh_run (memcached_st *memc __attribute__((unused)))
4381 {
4382 uint32_t x;
4383 const char **ptr;
4384 #ifdef HAVE_HSIEH_HASH
4385 uint32_t values[]= { 3738850110, 3636226060, 3821074029, 3489929160, 3485772682, 80540287,
4386 1805464076, 1895033657, 409795758, 979934958, 3634096985, 1284445480,
4387 2265380744, 707972988, 353823508, 1549198350, 1327930172, 9304163,
4388 4220749037, 2493964934, 2777873870, 2057831732, 1510213931, 2027828987,
4389 3395453351 };
4390 #else
4391 uint32_t values[]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
4392 #endif
4393
4394 for (ptr= list, x= 0; *ptr; ptr++, x++)
4395 {
4396 uint32_t hash_val;
4397
4398 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_HSIEH);
4399 assert(values[x] == hash_val);
4400 }
4401
4402 return TEST_SUCCESS;
4403 }
4404
4405 static test_return murmur_run (memcached_st *memc __attribute__((unused)))
4406 {
4407 uint32_t x;
4408 const char **ptr;
4409 uint32_t values[]= { 473199127U, 4148981457U, 3971873300U, 3257986707U,
4410 1722477987U, 2991193800U, 4147007314U, 3633179701U,
4411 1805162104U, 3503289120U, 3395702895U, 3325073042U,
4412 2345265314U, 3340346032U, 2722964135U, 1173398992U,
4413 2815549194U, 2562818319U, 224996066U, 2680194749U,
4414 3035305390U, 246890365U, 2395624193U, 4145193337U,
4415 1801941682U };
4416
4417 for (ptr= list, x= 0; *ptr; ptr++, x++)
4418 {
4419 uint32_t hash_val;
4420
4421 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_FNV1_64);
4422 assert(values[x] == hash_val);
4423 }
4424
4425 return TEST_SUCCESS;
4426 }
4427
4428 static test_return jenkins_run (memcached_st *memc __attribute__((unused)))
4429 {
4430 uint32_t x;
4431 const char **ptr;
4432 uint32_t values[]= { 1442444624U, 4253821186U, 1885058256U, 2120131735U,
4433 3261968576U, 3515188778U, 4232909173U, 4288625128U,
4434 1812047395U, 3689182164U, 2502979932U, 1214050606U,
4435 2415988847U, 1494268927U, 1025545760U, 3920481083U,
4436 4153263658U, 3824871822U, 3072759809U, 798622255U,
4437 3065432577U, 1453328165U, 2691550971U, 3408888387U,
4438 2629893356U };
4439
4440
4441 for (ptr= list, x= 0; *ptr; ptr++, x++)
4442 {
4443 uint32_t hash_val;
4444
4445 hash_val= memcached_generate_hash_value(*ptr, strlen(*ptr), MEMCACHED_HASH_JENKINS);
4446 assert(values[x] == hash_val);
4447 }
4448
4449 return TEST_SUCCESS;
4450 }
4451
4452 test_st udp_setup_server_tests[] ={
4453 {"set_udp_behavior_test", 0, set_udp_behavior_test},
4454 {"add_tcp_server_udp_client_test", 0, add_tcp_server_udp_client_test},
4455 {"add_udp_server_tcp_client_test", 0, add_udp_server_tcp_client_test},
4456 {0, 0, 0}
4457 };
4458
4459 test_st upd_io_tests[] ={
4460 {"udp_set_test", 0, udp_set_test},
4461 {"udp_buffered_set_test", 0, udp_buffered_set_test},
4462 {"udp_set_too_big_test", 0, udp_set_too_big_test},
4463 {"udp_delete_test", 0, udp_delete_test},
4464 {"udp_buffered_delete_test", 0, udp_buffered_delete_test},
4465 {"udp_verbosity_test", 0, udp_verbosity_test},
4466 {"udp_quit_test", 0, udp_quit_test},
4467 {"udp_flush_test", 0, udp_flush_test},
4468 {"udp_incr_test", 0, udp_incr_test},
4469 {"udp_decr_test", 0, udp_decr_test},
4470 {"udp_stat_test", 0, udp_stat_test},
4471 {"udp_version_test", 0, udp_version_test},
4472 {"udp_get_test", 0, udp_get_test},
4473 {"udp_mixed_io_test", 0, udp_mixed_io_test},
4474 {0, 0, 0}
4475 };
4476
4477 /* Clean the server before beginning testing */
4478 test_st tests[] ={
4479 {"flush", 0, flush_test },
4480 {"init", 0, init_test },
4481 {"allocation", 0, allocation_test },
4482 {"server_list_null_test", 0, server_list_null_test},
4483 {"server_unsort", 0, server_unsort_test},
4484 {"server_sort", 0, server_sort_test},
4485 {"server_sort2", 0, server_sort2_test},
4486 {"clone_test", 0, clone_test },
4487 {"connection_test", 0, connection_test},
4488 {"callback_test", 0, callback_test},
4489 {"behavior_test", 0, behavior_test},
4490 {"userdata_test", 0, userdata_test},
4491 {"error", 0, error_test },
4492 {"set", 0, set_test },
4493 {"set2", 0, set_test2 },
4494 {"set3", 0, set_test3 },
4495 {"dump", 1, dump_test},
4496 {"add", 1, add_test },
4497 {"replace", 1, replace_test },
4498 {"delete", 1, delete_test },
4499 {"get", 1, get_test },
4500 {"get2", 0, get_test2 },
4501 {"get3", 0, get_test3 },
4502 {"get4", 0, get_test4 },
4503 {"partial mget", 0, get_test5 },
4504 {"stats_servername", 0, stats_servername_test },
4505 {"increment", 0, increment_test },
4506 {"increment_with_initial", 1, increment_with_initial_test },
4507 {"decrement", 0, decrement_test },
4508 {"decrement_with_initial", 1, decrement_with_initial_test },
4509 {"quit", 0, quit_test },
4510 {"mget", 1, mget_test },
4511 {"mget_result", 1, mget_result_test },
4512 {"mget_result_alloc", 1, mget_result_alloc_test },
4513 {"mget_result_function", 1, mget_result_function },
4514 {"mget_end", 0, mget_end },
4515 {"get_stats", 0, get_stats },
4516 {"add_host_test", 0, add_host_test },
4517 {"add_host_test_1", 0, add_host_test1 },
4518 {"get_stats_keys", 0, get_stats_keys },
4519 {"behavior_test", 0, get_stats_keys },
4520 {"callback_test", 0, get_stats_keys },
4521 {"version_string_test", 0, version_string_test},
4522 {"bad_key", 1, bad_key_test },
4523 {"memcached_server_cursor", 1, memcached_server_cursor_test },
4524 {"read_through", 1, read_through },
4525 {"delete_through", 1, delete_through },
4526 {"noreply", 1, noreply_test},
4527 {"analyzer", 1, analyzer_test},
4528 #ifdef HAVE_LIBMEMCACHEDUTIL
4529 {"connectionpool", 1, connection_pool_test },
4530 #endif
4531 {0, 0, 0}
4532 };
4533
4534 test_st async_tests[] ={
4535 {"add", 1, add_wrapper },
4536 {0, 0, 0}
4537 };
4538
4539 test_st string_tests[] ={
4540 {"string static with null", 0, string_static_null },
4541 {"string alloc with null", 0, string_alloc_null },
4542 {"string alloc with 1K", 0, string_alloc_with_size },
4543 {"string alloc with malloc failure", 0, string_alloc_with_size_toobig },
4544 {"string append", 0, string_alloc_append },
4545 {"string append failure (too big)", 0, string_alloc_append_toobig },
4546 {0, 0, 0}
4547 };
4548
4549 test_st result_tests[] ={
4550 {"result static", 0, result_static},
4551 {"result alloc", 0, result_alloc},
4552 {0, 0, 0}
4553 };
4554
4555 test_st version_1_2_3[] ={
4556 {"append", 0, append_test },
4557 {"prepend", 0, prepend_test },
4558 {"cas", 0, cas_test },
4559 {"cas2", 0, cas2_test },
4560 {"append_binary", 0, append_binary_test },
4561 {0, 0, 0}
4562 };
4563
4564 test_st user_tests[] ={
4565 {"user_supplied_bug1", 0, user_supplied_bug1 },
4566 {"user_supplied_bug2", 0, user_supplied_bug2 },
4567 {"user_supplied_bug3", 0, user_supplied_bug3 },
4568 {"user_supplied_bug4", 0, user_supplied_bug4 },
4569 {"user_supplied_bug5", 1, user_supplied_bug5 },
4570 {"user_supplied_bug6", 1, user_supplied_bug6 },
4571 {"user_supplied_bug7", 1, user_supplied_bug7 },
4572 {"user_supplied_bug8", 1, user_supplied_bug8 },
4573 {"user_supplied_bug9", 1, user_supplied_bug9 },
4574 {"user_supplied_bug10", 1, user_supplied_bug10 },
4575 {"user_supplied_bug11", 1, user_supplied_bug11 },
4576 {"user_supplied_bug12", 1, user_supplied_bug12 },
4577 {"user_supplied_bug13", 1, user_supplied_bug13 },
4578 {"user_supplied_bug14", 1, user_supplied_bug14 },
4579 {"user_supplied_bug15", 1, user_supplied_bug15 },
4580 {"user_supplied_bug16", 1, user_supplied_bug16 },
4581 #ifndef __sun
4582 /*
4583 ** It seems to be something weird with the character sets..
4584 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
4585 ** guess I need to find out how this is supposed to work.. Perhaps I need
4586 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
4587 ** so just disable the code for now...).
4588 */
4589 {"user_supplied_bug17", 1, user_supplied_bug17 },
4590 #endif
4591 {"user_supplied_bug18", 1, user_supplied_bug18 },
4592 {"user_supplied_bug19", 1, user_supplied_bug19 },
4593 {"user_supplied_bug20", 1, user_supplied_bug20 },
4594 {0, 0, 0}
4595 };
4596
4597 test_st replication_tests[]= {
4598 {"set", 1, replication_set_test },
4599 {"get", 0, replication_get_test },
4600 {"mget", 0, replication_mget_test },
4601 {"delete", 0, replication_delete_test },
4602 {0, 0, 0}
4603 };
4604
4605 test_st generate_tests[] ={
4606 {"generate_pairs", 1, generate_pairs },
4607 {"generate_data", 1, generate_data },
4608 {"get_read", 0, get_read },
4609 {"delete_generate", 0, delete_generate },
4610 {"generate_buffer_data", 1, generate_buffer_data },
4611 {"delete_buffer", 0, delete_buffer_generate},
4612 {"generate_data", 1, generate_data },
4613 {"mget_read", 0, mget_read },
4614 {"mget_read_result", 0, mget_read_result },
4615 {"mget_read_function", 0, mget_read_function },
4616 {"cleanup", 1, cleanup_pairs },
4617 {"generate_large_pairs", 1, generate_large_pairs },
4618 {"generate_data", 1, generate_data },
4619 {"generate_buffer_data", 1, generate_buffer_data },
4620 {"cleanup", 1, cleanup_pairs },
4621 {0, 0, 0}
4622 };
4623
4624 test_st consistent_tests[] ={
4625 {"generate_pairs", 1, generate_pairs },
4626 {"generate_data", 1, generate_data },
4627 {"get_read", 0, get_read_count },
4628 {"cleanup", 1, cleanup_pairs },
4629 {0, 0, 0}
4630 };
4631
4632 test_st consistent_weighted_tests[] ={
4633 {"generate_pairs", 1, generate_pairs },
4634 {"generate_data", 1, generate_data_with_stats },
4635 {"get_read", 0, get_read_count },
4636 {"cleanup", 1, cleanup_pairs },
4637 {0, 0, 0}
4638 };
4639
4640 test_st hsieh_availability[] ={
4641 {"hsieh_avaibility_test",0,hsieh_avaibility_test},
4642 {0, 0, 0}
4643 };
4644
4645 test_st ketama_auto_eject_hosts[] ={
4646 {"auto_eject_hosts", 1, auto_eject_hosts },
4647 {0, 0, 0}
4648 };
4649
4650 test_st hash_tests[] ={
4651 {"md5", 0, md5_run },
4652 {"crc", 0, crc_run },
4653 {"fnv1_64", 0, fnv1_64_run },
4654 {"fnv1a_64", 0, fnv1a_64_run },
4655 {"fnv1_32", 0, fnv1_32_run },
4656 {"fnv1a_32", 0, fnv1a_32_run },
4657 {"hsieh", 0, hsieh_run },
4658 {"murmur", 0, murmur_run },
4659 {"jenkis", 0, jenkins_run },
4660 {0, 0, 0}
4661 };
4662
4663 collection_st collection[] ={
4664 {"hsieh_availability",0,0,hsieh_availability},
4665 {"udp_setup", init_udp, 0, udp_setup_server_tests},
4666 {"udp_io", init_udp, 0, upd_io_tests},
4667 {"udp_binary_io", binary_init_udp, 0, upd_io_tests},
4668 {"block", 0, 0, tests},
4669 {"binary", pre_binary, 0, tests},
4670 {"nonblock", pre_nonblock, 0, tests},
4671 {"nodelay", pre_nodelay, 0, tests},
4672 {"settimer", pre_settimer, 0, tests},
4673 {"md5", pre_md5, 0, tests},
4674 {"crc", pre_crc, 0, tests},
4675 {"hsieh", pre_hsieh, 0, tests},
4676 {"jenkins", pre_jenkins, 0, tests},
4677 {"fnv1_64", pre_hash_fnv1_64, 0, tests},
4678 {"fnv1a_64", pre_hash_fnv1a_64, 0, tests},
4679 {"fnv1_32", pre_hash_fnv1_32, 0, tests},
4680 {"fnv1a_32", pre_hash_fnv1a_32, 0, tests},
4681 {"ketama", pre_behavior_ketama, 0, tests},
4682 {"ketama_auto_eject_hosts", pre_behavior_ketama, 0, ketama_auto_eject_hosts},
4683 {"unix_socket", pre_unix_socket, 0, tests},
4684 {"unix_socket_nodelay", pre_nodelay, 0, tests},
4685 {"poll_timeout", poll_timeout, 0, tests},
4686 {"gets", enable_cas, 0, tests},
4687 {"consistent", enable_consistent, 0, tests},
4688 #ifdef MEMCACHED_ENABLE_DEPRECATED
4689 {"deprecated_memory_allocators", deprecated_set_memory_alloc, 0, tests},
4690 #endif
4691 {"memory_allocators", set_memory_alloc, 0, tests},
4692 {"prefix", set_prefix, 0, tests},
4693 {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},
4694 {"string", 0, 0, string_tests},
4695 {"result", 0, 0, result_tests},
4696 {"async", pre_nonblock, 0, async_tests},
4697 {"async_binary", pre_nonblock_binary, 0, async_tests},
4698 {"user", 0, 0, user_tests},
4699 {"generate", 0, 0, generate_tests},
4700 {"generate_hsieh", pre_hsieh, 0, generate_tests},
4701 {"generate_ketama", pre_behavior_ketama, 0, generate_tests},
4702 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
4703 {"generate_md5", pre_md5, 0, generate_tests},
4704 {"generate_murmur", pre_murmur, 0, generate_tests},
4705 {"generate_jenkins", pre_jenkins, 0, generate_tests},
4706 {"generate_nonblock", pre_nonblock, 0, generate_tests},
4707 {"consistent_not", 0, 0, consistent_tests},
4708 {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests},
4709 {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests},
4710 {"test_hashes", 0, 0, hash_tests},
4711 {"replication", pre_replication, 0, replication_tests},
4712 {"replication_noblock", pre_replication_noblock, 0, replication_tests},
4713 {0, 0, 0, 0}
4714 };
4715
4716 #define SERVERS_TO_CREATE 5
4717
4718 /* Prototypes for functions we will pass to test framework */
4719 void *world_create(void);
4720 void world_destroy(void *p);
4721
4722 void *world_create(void)
4723 {
4724 server_startup_st *construct;
4725
4726 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
4727 memset(construct, 0, sizeof(server_startup_st));
4728 construct->count= SERVERS_TO_CREATE;
4729 construct->udp= 0;
4730 server_startup(construct);
4731
4732 return construct;
4733 }
4734
4735
4736 void world_destroy(void *p)
4737 {
4738 server_startup_st *construct= (server_startup_st *)p;
4739 memcached_server_st *servers= (memcached_server_st *)construct->servers;
4740 memcached_server_list_free(servers);
4741
4742 server_shutdown(construct);
4743 free(construct);
4744 }
4745
4746 void get_world(world_st *world)
4747 {
4748 world->collections= collection;
4749 world->create= world_create;
4750 world->destroy= world_destroy;
4751 }