fa9ce010e42fb7b6fb4ad8f1c44e6f38ee224bca
[m6w6/libmemcached] / tests / libmemcached-1.0 / mem_functions.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include "mem_config.h"
39 #include "libtest/test.hpp"
40
41 #if defined(HAVE_LIBUUID) && HAVE_LIBUUID
42 # include <uuid/uuid.h>
43 #endif
44
45 /*
46 Test cases
47 */
48
49 #include "libmemcached-1.0/memcached.h"
50 #include "libmemcached/is.h"
51 #include "libmemcached/server_instance.h"
52
53 #include "libhashkit-1.0/hashkit.h"
54
55 #include "libtest/memcached.hpp"
56
57 #include <cerrno>
58 #include <memory>
59 #include <pthread.h>
60 #include <semaphore.h>
61 #include <signal.h>
62 #include <sys/stat.h>
63 #include <sys/time.h>
64 #include <sys/types.h>
65 #include <unistd.h>
66
67 #include <iostream>
68
69 #include "libtest/server.h"
70
71 #include "bin/generator.h"
72
73 #define SMALL_STRING_LEN 1024
74
75 #include "libtest/test.hpp"
76
77 using namespace libtest;
78
79 #include "libmemcachedutil-1.0/util.h"
80
81 #include "tests/hash_results.h"
82
83 #include "tests/libmemcached-1.0/callback_counter.h"
84 #include "tests/libmemcached-1.0/fetch_all_results.h"
85 #include "tests/libmemcached-1.0/mem_functions.h"
86 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
87 #include "tests/print.h"
88 #include "tests/debug.h"
89 #include "tests/memc.hpp"
90
91 #define UUID_STRING_MAXLENGTH 36
92
93 #include "tests/keys.hpp"
94
95 #include "libmemcached/instance.hpp"
96
97
98 test_return_t mget_end(memcached_st *memc)
99 {
100 const char *keys[]= { "foo", "foo2" };
101 size_t lengths[]= { 3, 4 };
102 const char *values[]= { "fjord", "41" };
103
104 // Set foo and foo2
105 for (size_t x= 0; x < test_array_length(keys); x++)
106 {
107 test_compare(MEMCACHED_SUCCESS,
108 memcached_set(memc,
109 keys[x], lengths[x],
110 values[x], strlen(values[x]),
111 time_t(0), uint32_t(0)));
112 }
113
114 char *string;
115 size_t string_length;
116 uint32_t flags;
117
118 // retrieve both via mget
119 test_compare(MEMCACHED_SUCCESS,
120 memcached_mget(memc,
121 keys, lengths,
122 test_array_length(keys)));
123
124 char key[MEMCACHED_MAX_KEY];
125 size_t key_length;
126 memcached_return_t rc;
127
128 // this should get both
129 for (size_t x= 0; x < test_array_length(keys); x++)
130 {
131 string= memcached_fetch(memc, key, &key_length, &string_length,
132 &flags, &rc);
133 test_compare(MEMCACHED_SUCCESS, rc);
134 int val = 0;
135 if (key_length == 4)
136 {
137 val= 1;
138 }
139
140 test_compare(string_length, strlen(values[val]));
141 test_true(strncmp(values[val], string, string_length) == 0);
142 free(string);
143 }
144
145 // this should indicate end
146 string= memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
147 test_compare(MEMCACHED_END, rc);
148 test_null(string);
149
150 // now get just one
151 test_compare(MEMCACHED_SUCCESS,
152 memcached_mget(memc, keys, lengths, 1));
153
154 string= memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
155 test_compare(key_length, lengths[0]);
156 test_true(strncmp(keys[0], key, key_length) == 0);
157 test_compare(string_length, strlen(values[0]));
158 test_true(strncmp(values[0], string, string_length) == 0);
159 test_compare(MEMCACHED_SUCCESS, rc);
160 free(string);
161
162 // this should indicate end
163 string= memcached_fetch(memc, key, &key_length, &string_length, &flags, &rc);
164 test_compare(MEMCACHED_END, rc);
165 test_null(string);
166
167 return TEST_SUCCESS;
168 }
169
170 test_return_t mget_result_test(memcached_st *memc)
171 {
172 const char *keys[]= {"fudge", "son", "food"};
173 size_t key_length[]= {5, 3, 4};
174
175 memcached_result_st results_obj;
176 memcached_result_st *results= memcached_result_create(memc, &results_obj);
177 test_true(results);
178 test_true(&results_obj == results);
179
180 /* We need to empty the server before continueing test */
181 test_compare(MEMCACHED_SUCCESS,
182 memcached_flush(memc, 0));
183
184 test_compare(MEMCACHED_SUCCESS,
185 memcached_mget(memc, keys, key_length, 3));
186
187 memcached_return_t rc;
188 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
189 {
190 test_true(results);
191 }
192
193 while ((results= memcached_fetch_result(memc, &results_obj, &rc))) { test_true(false); /* We should never see a value returned */ };
194 test_false(results);
195 test_compare(MEMCACHED_NOTFOUND, rc);
196
197 for (uint32_t x= 0; x < 3; x++)
198 {
199 rc= memcached_set(memc, keys[x], key_length[x],
200 keys[x], key_length[x],
201 (time_t)50, (uint32_t)9);
202 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
203 }
204
205 test_compare(MEMCACHED_SUCCESS,
206 memcached_mget(memc, keys, key_length, 3));
207
208 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
209 {
210 test_true(results);
211 test_true(&results_obj == results);
212 test_compare(MEMCACHED_SUCCESS, rc);
213 test_memcmp(memcached_result_key_value(results),
214 memcached_result_value(results),
215 memcached_result_length(results));
216 test_compare(memcached_result_key_length(results), memcached_result_length(results));
217 }
218
219 memcached_result_free(&results_obj);
220
221 return TEST_SUCCESS;
222 }
223
224 test_return_t mget_result_alloc_test(memcached_st *memc)
225 {
226 const char *keys[]= {"fudge", "son", "food"};
227 size_t key_length[]= {5, 3, 4};
228
229 memcached_result_st *results;
230
231 /* We need to empty the server before continueing test */
232 test_compare(MEMCACHED_SUCCESS,
233 memcached_flush(memc, 0));
234
235 test_compare(MEMCACHED_SUCCESS,
236 memcached_mget(memc, keys, key_length, 3));
237
238 memcached_return_t rc;
239 while ((results= memcached_fetch_result(memc, NULL, &rc)))
240 {
241 test_true(results);
242 }
243 test_false(results);
244 test_compare(MEMCACHED_NOTFOUND, rc);
245
246 for (uint32_t x= 0; x < 3; x++)
247 {
248 rc= memcached_set(memc, keys[x], key_length[x],
249 keys[x], key_length[x],
250 (time_t)50, (uint32_t)9);
251 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
252 }
253
254 test_compare(MEMCACHED_SUCCESS,
255 memcached_mget(memc, keys, key_length, 3));
256
257 uint32_t x= 0;
258 while ((results= memcached_fetch_result(memc, NULL, &rc)))
259 {
260 test_true(results);
261 test_compare(MEMCACHED_SUCCESS, rc);
262 test_compare(memcached_result_key_length(results), memcached_result_length(results));
263 test_memcmp(memcached_result_key_value(results),
264 memcached_result_value(results),
265 memcached_result_length(results));
266 memcached_result_free(results);
267 x++;
268 }
269
270 return TEST_SUCCESS;
271 }
272
273 test_return_t mget_result_function(memcached_st *memc)
274 {
275 const char *keys[]= {"fudge", "son", "food"};
276 size_t key_length[]= {5, 3, 4};
277 size_t counter;
278 memcached_execute_fn callbacks[1];
279
280 for (uint32_t x= 0; x < 3; x++)
281 {
282 test_compare(return_value_based_on_buffering(memc),
283 memcached_set(memc, keys[x], key_length[x],
284 keys[x], key_length[x],
285 time_t(50), uint32_t(9)));
286 }
287 test_compare(MEMCACHED_SUCCESS, memcached_flush_buffers(memc));
288 memcached_quit(memc);
289
290 test_compare(MEMCACHED_SUCCESS,
291 memcached_mget(memc, keys, key_length, 3));
292
293 callbacks[0]= &callback_counter;
294 counter= 0;
295
296 test_compare(MEMCACHED_SUCCESS,
297 memcached_fetch_execute(memc, callbacks, (void *)&counter, 1));
298
299 test_compare(size_t(3), counter);
300
301 return TEST_SUCCESS;
302 }
303
304 test_return_t mget_test(memcached_st *memc)
305 {
306 const char *keys[]= {"fudge", "son", "food"};
307 size_t key_length[]= {5, 3, 4};
308
309 char return_key[MEMCACHED_MAX_KEY];
310 size_t return_key_length;
311 char *return_value;
312 size_t return_value_length;
313
314 test_compare(MEMCACHED_SUCCESS,
315 memcached_mget(memc, keys, key_length, 3));
316
317 uint32_t flags;
318 memcached_return_t rc;
319 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
320 &return_value_length, &flags, &rc)))
321 {
322 test_true(return_value);
323 }
324 test_false(return_value);
325 test_zero(return_value_length);
326 test_compare(MEMCACHED_NOTFOUND, rc);
327
328 for (uint32_t x= 0; x < 3; x++)
329 {
330 rc= memcached_set(memc, keys[x], key_length[x],
331 keys[x], key_length[x],
332 (time_t)50, (uint32_t)9);
333 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
334 }
335 test_compare(MEMCACHED_SUCCESS,
336 memcached_mget(memc, keys, key_length, 3));
337
338 uint32_t x= 0;
339 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
340 &return_value_length, &flags, &rc)))
341 {
342 test_true(return_value);
343 test_compare(MEMCACHED_SUCCESS, rc);
344 if (not memc->_namespace)
345 {
346 test_compare(return_key_length, return_value_length);
347 test_memcmp(return_value, return_key, return_value_length);
348 }
349 free(return_value);
350 x++;
351 }
352
353 return TEST_SUCCESS;
354 }
355
356 test_return_t mget_execute(memcached_st *original_memc)
357 {
358 test_skip(true, memcached_behavior_get(original_memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
359
360 memcached_st *memc= create_single_instance_memcached(original_memc, "--BINARY-PROTOCOL");
361 test_true(memc);
362
363 keys_st keys(20480);
364
365 /* First add all of the items.. */
366 char blob[1024] = {0};
367
368 for (size_t x= 0; x < keys.size(); ++x)
369 {
370 uint64_t query_id= memcached_query_id(memc);
371 memcached_return_t rc= memcached_add(memc,
372 keys.key_at(x), keys.length_at(x),
373 blob, sizeof(blob),
374 0, 0);
375 ASSERT_TRUE_(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED, "Returned %s", memcached_strerror(NULL, rc));
376 test_compare(query_id +1, memcached_query_id(memc));
377 }
378
379 /* Try to get all of them with a large multiget */
380 size_t counter= 0;
381 memcached_execute_fn callbacks[]= { &callback_counter };
382 test_compare(MEMCACHED_SUCCESS,
383 memcached_mget_execute(memc,
384 keys.keys_ptr(), keys.lengths_ptr(),
385 keys.size(), callbacks, &counter, 1));
386
387 {
388 uint64_t query_id= memcached_query_id(memc);
389 test_compare(MEMCACHED_SUCCESS,
390 memcached_fetch_execute(memc, callbacks, (void *)&counter, 1));
391 test_compare(query_id, memcached_query_id(memc));
392
393 /* Verify that we got all of the items */
394 test_compare(keys.size(), counter);
395 }
396
397 memcached_free(memc);
398
399 return TEST_SUCCESS;
400 }
401
402
403
404 test_return_t memcached_fetch_result_NOT_FOUND(memcached_st *memc)
405 {
406 memcached_return_t rc;
407
408 const char *key= "not_found";
409 size_t key_length= test_literal_param_size("not_found");
410
411 test_compare(MEMCACHED_SUCCESS,
412 memcached_mget(memc, &key, &key_length, 1));
413
414 memcached_result_st *result= memcached_fetch_result(memc, NULL, &rc);
415 test_null(result);
416 test_compare(MEMCACHED_NOTFOUND, rc);
417
418 memcached_result_free(result);
419
420 return TEST_SUCCESS;
421 }
422
423 /*
424 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
425 */
426 test_return_t user_supplied_bug12(memcached_st *memc)
427 {
428 memcached_return_t rc;
429 uint32_t flags;
430 size_t value_length;
431 char *value;
432 uint64_t number_value;
433
434 value= memcached_get(memc, "autoincrement", strlen("autoincrement"),
435 &value_length, &flags, &rc);
436 test_null(value);
437 test_compare(MEMCACHED_NOTFOUND, rc);
438
439 rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"),
440 1, &number_value);
441 test_null(value);
442 /* The binary protocol will set the key if it doesn't exist */
443 if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1)
444 {
445 test_compare(MEMCACHED_SUCCESS, rc);
446 }
447 else
448 {
449 test_compare(MEMCACHED_NOTFOUND, rc);
450 }
451
452 test_compare(MEMCACHED_SUCCESS,
453 memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0));
454
455 value= memcached_get(memc, "autoincrement", strlen("autoincrement"), &value_length, &flags, &rc);
456 test_true(value);
457 free(value);
458
459 test_compare(MEMCACHED_SUCCESS,
460 memcached_increment(memc, "autoincrement", strlen("autoincrement"), 1, &number_value));
461 test_compare(2UL, number_value);
462
463 return TEST_SUCCESS;
464 }
465
466 /*
467 Bug found where command total one more than MEMCACHED_MAX_BUFFER
468 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
469 */
470 test_return_t user_supplied_bug13(memcached_st *memc)
471 {
472 char key[] = "key34567890";
473
474 char commandFirst[]= "set key34567890 0 0 ";
475 char commandLast[] = " \r\n"; /* first line of command sent to server */
476 size_t commandLength;
477
478 commandLength = strlen(commandFirst) + strlen(commandLast) + 4; /* 4 is number of characters in size, probably 8196 */
479
480 size_t overflowSize = MEMCACHED_MAX_BUFFER - commandLength;
481
482 for (size_t testSize= overflowSize - 1; testSize < overflowSize + 1; testSize++)
483 {
484 char *overflow= new (std::nothrow) char[testSize];
485 test_true(overflow);
486
487 memset(overflow, 'x', testSize);
488 test_compare(MEMCACHED_SUCCESS,
489 memcached_set(memc, key, strlen(key),
490 overflow, testSize, 0, 0));
491 delete [] overflow;
492 }
493
494 return TEST_SUCCESS;
495 }
496
497
498 /*
499 Test values of many different sizes
500 Bug found where command total one more than MEMCACHED_MAX_BUFFER
501 set key34567890 0 0 8169 \r\n
502 is sent followed by buffer of size 8169, followed by 8169
503 */
504 test_return_t user_supplied_bug14(memcached_st *memc)
505 {
506 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true);
507
508 libtest::vchar_t value;
509 value.reserve(18000);
510 for (ptrdiff_t x= 0; x < 18000; x++)
511 {
512 value.push_back((char) (x % 127));
513 }
514
515 for (size_t current_length= 1; current_length < value.size(); current_length++)
516 {
517 memcached_return_t rc= memcached_set(memc, test_literal_param("foo"),
518 &value[0], current_length,
519 (time_t)0, (uint32_t)0);
520 ASSERT_TRUE_(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED, "Instead got %s", memcached_strerror(NULL, rc));
521
522 size_t string_length;
523 uint32_t flags;
524 char *string= memcached_get(memc, test_literal_param("foo"),
525 &string_length, &flags, &rc);
526
527 test_compare(MEMCACHED_SUCCESS, rc);
528 test_compare(string_length, current_length);
529 char buffer[1024];
530 snprintf(buffer, sizeof(buffer), "%u", uint32_t(string_length));
531 test_memcmp(string, &value[0], string_length);
532
533 free(string);
534 }
535
536 return TEST_SUCCESS;
537 }
538
539 /*
540 From Andrei on IRC
541 */
542
543 test_return_t user_supplied_bug19(memcached_st *)
544 {
545 memcached_return_t res;
546
547 memcached_st *memc= memcached(test_literal_param("--server=localhost:11311/?100 --server=localhost:11312/?100"));
548
549 const memcached_instance_st * server= memcached_server_by_key(memc, "a", 1, &res);
550 test_true(server);
551
552 memcached_free(memc);
553
554 return TEST_SUCCESS;
555 }
556
557 /* CAS test from Andei */
558 test_return_t user_supplied_bug20(memcached_st *memc)
559 {
560 const char *key= "abc";
561 size_t key_len= strlen("abc");
562
563 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, true));
564
565 test_compare(MEMCACHED_SUCCESS,
566 memcached_set(memc,
567 test_literal_param("abc"),
568 test_literal_param("foobar"),
569 (time_t)0, (uint32_t)0));
570
571 test_compare(MEMCACHED_SUCCESS,
572 memcached_mget(memc, &key, &key_len, 1));
573
574 memcached_result_st result_obj;
575 memcached_result_st *result= memcached_result_create(memc, &result_obj);
576 test_true(result);
577
578 memcached_result_create(memc, &result_obj);
579 memcached_return_t status;
580 result= memcached_fetch_result(memc, &result_obj, &status);
581
582 test_true(result);
583 test_compare(MEMCACHED_SUCCESS, status);
584
585 memcached_result_free(result);
586
587 return TEST_SUCCESS;
588 }
589
590 /* Large mget() of missing keys with binary proto
591 *
592 * If many binary quiet commands (such as getq's in an mget) fill the output
593 * buffer and the server chooses not to respond, memcached_flush hangs. See
594 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
595 */
596
597 /* sighandler_t function that always asserts false */
598 static __attribute__((noreturn)) void fail(int)
599 {
600 fatal_assert(0);
601 }
602
603
604 test_return_t _user_supplied_bug21(memcached_st* memc, size_t key_count)
605 {
606 #ifdef WIN32
607 (void)memc;
608 (void)key_count;
609 return TEST_SKIPPED;
610 #else
611 void (*oldalarm)(int);
612
613 memcached_st *memc_clone= memcached_clone(NULL, memc);
614 test_true(memc_clone);
615
616 /* only binproto uses getq for mget */
617 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
618
619 /* empty the cache to ensure misses (hence non-responses) */
620 test_compare(MEMCACHED_SUCCESS, memcached_flush(memc_clone, 0));
621
622 keys_st keys(key_count);
623
624 oldalarm= signal(SIGALRM, fail);
625 alarm(5);
626
627 test_compare_got(MEMCACHED_SUCCESS,
628 memcached_mget(memc_clone, keys.keys_ptr(), keys.lengths_ptr(), keys.size()),
629 memcached_last_error_message(memc_clone));
630
631 alarm(0);
632 signal(SIGALRM, oldalarm);
633
634 memcached_return_t rc;
635 uint32_t flags;
636 char return_key[MEMCACHED_MAX_KEY];
637 size_t return_key_length;
638 char *return_value;
639 size_t return_value_length;
640 while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
641 &return_value_length, &flags, &rc)))
642 {
643 test_false(return_value); // There are no keys to fetch, so the value should never be returned
644 }
645 test_compare(MEMCACHED_NOTFOUND, rc);
646 test_zero(return_value_length);
647 test_zero(return_key_length);
648 test_false(return_key[0]);
649 test_false(return_value);
650
651 memcached_free(memc_clone);
652
653 return TEST_SUCCESS;
654 #endif
655 }
656
657 test_return_t user_supplied_bug21(memcached_st *memc)
658 {
659 test_skip(TEST_SUCCESS, pre_binary(memc));
660
661 /* should work as of r580 */
662 test_compare(TEST_SUCCESS,
663 _user_supplied_bug21(memc, 10));
664
665 /* should fail as of r580 */
666 test_compare(TEST_SUCCESS,
667 _user_supplied_bug21(memc, 1000));
668
669 return TEST_SUCCESS;
670 }
671
672 test_return_t comparison_operator_memcached_st_and__memcached_return_t_TEST(memcached_st *)
673 {
674 test::Memc memc_;
675
676 memcached_st *memc= &memc_;
677
678 ASSERT_EQ(memc, MEMCACHED_SUCCESS);
679 test_compare(memc, MEMCACHED_SUCCESS);
680
681 ASSERT_NEQ(memc, MEMCACHED_FAILURE);
682
683 return TEST_SUCCESS;
684 }
685
686
687 static void my_free(const memcached_st *ptr, void *mem, void *context)
688 {
689 (void)context;
690 (void)ptr;
691 #ifdef HARD_MALLOC_TESTS
692 void *real_ptr= (mem == NULL) ? mem : (void*)((caddr_t)mem - 8);
693 free(real_ptr);
694 #else
695 free(mem);
696 #endif
697 }
698
699
700 static void *my_malloc(const memcached_st *ptr, const size_t size, void *context)
701 {
702 (void)context;
703 (void)ptr;
704 #ifdef HARD_MALLOC_TESTS
705 void *ret= malloc(size + 8);
706 if (ret != NULL)
707 {
708 ret= (void*)((caddr_t)ret + 8);
709 }
710 #else
711 void *ret= malloc(size);
712 #endif
713
714 if (ret != NULL)
715 {
716 memset(ret, 0xff, size);
717 }
718
719 return ret;
720 }
721
722
723 static void *my_realloc(const memcached_st *ptr, void *mem, const size_t size, void *)
724 {
725 #ifdef HARD_MALLOC_TESTS
726 void *real_ptr= (mem == NULL) ? NULL : (void*)((caddr_t)mem - 8);
727 void *nmem= realloc(real_ptr, size + 8);
728
729 void *ret= NULL;
730 if (nmem != NULL)
731 {
732 ret= (void*)((caddr_t)nmem + 8);
733 }
734
735 return ret;
736 #else
737 (void)ptr;
738 return realloc(mem, size);
739 #endif
740 }
741
742
743 static void *my_calloc(const memcached_st *ptr, size_t nelem, const size_t size, void *)
744 {
745 #ifdef HARD_MALLOC_TESTS
746 void *mem= my_malloc(ptr, nelem * size);
747 if (mem)
748 {
749 memset(mem, 0, nelem * size);
750 }
751
752 return mem;
753 #else
754 (void)ptr;
755 return calloc(nelem, size);
756 #endif
757 }
758
759 #ifdef MEMCACHED_ENABLE_DEPRECATED
760 test_return_t deprecated_set_memory_alloc(memcached_st *memc)
761 {
762 void *test_ptr= NULL;
763 void *cb_ptr= NULL;
764 {
765 memcached_malloc_fn malloc_cb= (memcached_malloc_fn)my_malloc;
766 cb_ptr= *(void **)&malloc_cb;
767 memcached_return_t rc;
768
769 test_compare(MEMCACHED_SUCCESS,
770 memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, cb_ptr));
771 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
772 test_compare(MEMCACHED_SUCCESS, rc);
773 test_true(test_ptr == cb_ptr);
774 }
775
776 {
777 memcached_realloc_fn realloc_cb=
778 (memcached_realloc_fn)my_realloc;
779 cb_ptr= *(void **)&realloc_cb;
780 memcached_return_t rc;
781
782 test_compare(MEMCACHED_SUCCESS,
783 memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, cb_ptr));
784 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
785 test_compare(MEMCACHED_SUCCESS, rc);
786 test_true(test_ptr == cb_ptr);
787 }
788
789 {
790 memcached_free_fn free_cb=
791 (memcached_free_fn)my_free;
792 cb_ptr= *(void **)&free_cb;
793 memcached_return_t rc;
794
795 test_compare(MEMCACHED_SUCCESS,
796 memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, cb_ptr));
797 test_ptr= memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
798 test_compare(MEMCACHED_SUCCESS, rc);
799 test_true(test_ptr == cb_ptr);
800 }
801
802 return TEST_SUCCESS;
803 }
804 #endif
805
806
807 test_return_t set_memory_alloc(memcached_st *memc)
808 {
809 test_compare(MEMCACHED_INVALID_ARGUMENTS,
810 memcached_set_memory_allocators(memc, NULL, my_free,
811 my_realloc, my_calloc, NULL));
812
813 test_compare(MEMCACHED_SUCCESS,
814 memcached_set_memory_allocators(memc, my_malloc, my_free,
815 my_realloc, my_calloc, NULL));
816
817 memcached_malloc_fn mem_malloc;
818 memcached_free_fn mem_free;
819 memcached_realloc_fn mem_realloc;
820 memcached_calloc_fn mem_calloc;
821 memcached_get_memory_allocators(memc, &mem_malloc, &mem_free,
822 &mem_realloc, &mem_calloc);
823
824 test_true(mem_malloc == my_malloc);
825 test_true(mem_realloc == my_realloc);
826 test_true(mem_calloc == my_calloc);
827 test_true(mem_free == my_free);
828
829 return TEST_SUCCESS;
830 }
831
832
833
834 /*
835 Test case adapted from John Gorman <johngorman2@gmail.com>
836
837 We are testing the error condition when we connect to a server via memcached_get()
838 but find that the server is not available.
839 */
840 test_return_t memcached_get_MEMCACHED_ERRNO(memcached_st *)
841 {
842 size_t len;
843 uint32_t flags;
844 memcached_return rc;
845
846 // Create a handle.
847 memcached_st *tl_memc_h= memcached(test_literal_param("--server=localhost:9898 --server=localhost:9899")); // This server should not exist
848
849 // See if memcached is reachable.
850 char *value= memcached_get(tl_memc_h,
851 test_literal_param(__func__),
852 &len, &flags, &rc);
853
854 test_false(value);
855 test_zero(len);
856 test_true(memcached_failed(rc));
857
858 memcached_free(tl_memc_h);
859
860 return TEST_SUCCESS;
861 }
862
863 /*
864 Test case adapted from John Gorman <johngorman2@gmail.com>
865
866 We are testing the error condition when we connect to a server via memcached_get_by_key()
867 but find that the server is not available.
868 */
869 test_return_t memcached_get_by_key_MEMCACHED_ERRNO(memcached_st *)
870 {
871 size_t len;
872 uint32_t flags;
873 memcached_return rc;
874
875 // Create a handle.
876 memcached_st *tl_memc_h= memcached_create(NULL);
877 memcached_server_st *servers= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
878 memcached_server_push(tl_memc_h, servers);
879 memcached_server_list_free(servers);
880
881 // See if memcached is reachable.
882 char *value= memcached_get_by_key(tl_memc_h,
883 test_literal_param(__func__), // Key
884 test_literal_param(__func__), // Value
885 &len, &flags, &rc);
886
887 test_false(value);
888 test_zero(len);
889 test_true(memcached_failed(rc));
890
891 memcached_free(tl_memc_h);
892
893 return TEST_SUCCESS;
894 }
895
896 /* Test memcached_server_get_last_disconnect
897 * For a working server set, shall be NULL
898 * For a set of non existing server, shall not be NULL
899 */
900 test_return_t test_get_last_disconnect(memcached_st *memc)
901 {
902 memcached_return_t rc;
903 const memcached_instance_st * disconnected_server;
904
905 /* With the working set of server */
906 const char *key= "marmotte";
907 const char *value= "milka";
908
909 memcached_reset_last_disconnected_server(memc);
910 test_false(memc->last_disconnected_server);
911 rc= memcached_set(memc, key, strlen(key),
912 value, strlen(value),
913 (time_t)0, (uint32_t)0);
914 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
915
916 disconnected_server = memcached_server_get_last_disconnect(memc);
917 test_false(disconnected_server);
918
919 /* With a non existing server */
920 memcached_st *mine;
921 memcached_server_st *servers;
922
923 const char *server_list= "localhost:9";
924
925 servers= memcached_servers_parse(server_list);
926 test_true(servers);
927 mine= memcached_create(NULL);
928 rc= memcached_server_push(mine, servers);
929 test_compare(MEMCACHED_SUCCESS, rc);
930 memcached_server_list_free(servers);
931 test_true(mine);
932
933 rc= memcached_set(mine, key, strlen(key),
934 value, strlen(value),
935 (time_t)0, (uint32_t)0);
936 test_true(memcached_failed(rc));
937
938 disconnected_server= memcached_server_get_last_disconnect(mine);
939 test_true_got(disconnected_server, memcached_strerror(mine, rc));
940 test_compare(in_port_t(9), memcached_server_port(disconnected_server));
941 test_false(strncmp(memcached_server_name(disconnected_server),"localhost",9));
942
943 memcached_quit(mine);
944 memcached_free(mine);
945
946 return TEST_SUCCESS;
947 }
948
949 test_return_t test_multiple_get_last_disconnect(memcached_st *)
950 {
951 const char *server_string= "--server=localhost:8888 --server=localhost:8889 --server=localhost:8890 --server=localhost:8891 --server=localhost:8892";
952 char buffer[BUFSIZ];
953
954 test_compare(MEMCACHED_SUCCESS,
955 libmemcached_check_configuration(server_string, strlen(server_string), buffer, sizeof(buffer)));
956
957 memcached_st *memc= memcached(server_string, strlen(server_string));
958 test_true(memc);
959
960 // We will just use the error strings as our keys
961 uint32_t counter= 100;
962 while (--counter)
963 {
964 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
965 {
966 const char *msg= memcached_strerror(memc, memcached_return_t(x));
967 memcached_return_t ret= memcached_set(memc, msg, strlen(msg), NULL, 0, (time_t)0, (uint32_t)0);
968 test_true_got((ret == MEMCACHED_CONNECTION_FAILURE or ret == MEMCACHED_SERVER_TEMPORARILY_DISABLED), memcached_last_error_message(memc));
969
970 const memcached_instance_st * disconnected_server= memcached_server_get_last_disconnect(memc);
971 test_true(disconnected_server);
972 test_strcmp("localhost", memcached_server_name(disconnected_server));
973 test_true(memcached_server_port(disconnected_server) >= 8888 and memcached_server_port(disconnected_server) <= 8892);
974
975 if (random() % 2)
976 {
977 memcached_reset_last_disconnected_server(memc);
978 }
979 }
980 }
981
982 memcached_free(memc);
983
984 return TEST_SUCCESS;
985 }
986
987 /*
988 * This test ensures that the failure counter isn't incremented during
989 * normal termination of the memcached instance.
990 */
991 test_return_t wrong_failure_counter_test(memcached_st *original_memc)
992 {
993 memcached_st* memc= create_single_instance_memcached(original_memc, NULL);
994
995 /* Ensure that we are connected to the server by setting a value */
996 memcached_return_t rc= memcached_set(memc,
997 test_literal_param(__func__), // Key
998 test_literal_param(__func__), // Value
999 time_t(0), uint32_t(0));
1000 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
1001
1002
1003 const memcached_instance_st * instance= memcached_server_instance_by_position(memc, 0);
1004
1005 /* The test is to see that the memcached_quit doesn't increase the
1006 * the server failure conter, so let's ensure that it is zero
1007 * before sending quit
1008 */
1009 ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
1010
1011 memcached_quit(memc);
1012
1013 /* Verify that it memcached_quit didn't increment the failure counter
1014 * Please note that this isn't bullet proof, because an error could
1015 * occur...
1016 */
1017 test_zero(instance->server_failure_counter);
1018
1019 memcached_free(memc);
1020
1021 return TEST_SUCCESS;
1022 }
1023
1024 /*
1025 * This tests ensures expected disconnections (for some behavior changes
1026 * for instance) do not wrongly increase failure counter
1027 */
1028 test_return_t wrong_failure_counter_two_test(memcached_st *memc)
1029 {
1030 /* Set value to force connection to the server */
1031 const char *key= "marmotte";
1032 const char *value= "milka";
1033
1034 test_compare_hint(MEMCACHED_SUCCESS,
1035 memcached_set(memc, key, strlen(key),
1036 value, strlen(value),
1037 (time_t)0, (uint32_t)0),
1038 memcached_last_error_message(memc));
1039
1040
1041 /* put failure limit to 1 */
1042 test_compare(MEMCACHED_SUCCESS,
1043 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, true));
1044
1045 /* Put a retry timeout to effectively activate failure_limit effect */
1046 test_compare(MEMCACHED_SUCCESS,
1047 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 1));
1048
1049 /* change behavior that triggers memcached_quit()*/
1050 test_compare(MEMCACHED_SUCCESS,
1051 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true));
1052
1053
1054 /* Check if we still are connected */
1055 uint32_t flags;
1056 size_t string_length;
1057 memcached_return rc;
1058 char *string= memcached_get(memc, key, strlen(key),
1059 &string_length, &flags, &rc);
1060
1061 test_compare_got(MEMCACHED_SUCCESS, rc, memcached_strerror(NULL, rc));
1062 test_true(string);
1063 free(string);
1064
1065 return TEST_SUCCESS;
1066 }
1067
1068
1069 #define regression_bug_655423_COUNT 6000
1070 test_return_t regression_bug_655423(memcached_st *memc)
1071 {
1072 memcached_st *clone= memcached_clone(NULL, memc);
1073 memc= NULL; // Just to make sure it is not used
1074 test_true(clone);
1075 char payload[100];
1076
1077 #ifdef __APPLE__
1078 return TEST_SKIPPED;
1079 #endif
1080
1081 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1));
1082 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1));
1083 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1));
1084 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH, 1));
1085
1086 memset(payload, int('x'), sizeof(payload));
1087
1088 keys_st keys(regression_bug_655423_COUNT);
1089
1090 for (size_t x= 0; x < keys.size(); x++)
1091 {
1092 test_compare(MEMCACHED_SUCCESS, memcached_set(clone,
1093 keys.key_at(x),
1094 keys.length_at(x),
1095 payload, sizeof(payload), 0, 0));
1096 }
1097
1098 for (size_t x= 0; x < keys.size(); x++)
1099 {
1100 size_t value_length;
1101 memcached_return_t rc;
1102 char *value= memcached_get(clone,
1103 keys.key_at(x),
1104 keys.length_at(x),
1105 &value_length, NULL, &rc);
1106
1107 if (rc == MEMCACHED_NOTFOUND)
1108 {
1109 test_false(value);
1110 test_zero(value_length);
1111 continue;
1112 }
1113
1114 test_compare(MEMCACHED_SUCCESS, rc);
1115 test_true(value);
1116 test_compare(100LLU, value_length);
1117 free(value);
1118 }
1119
1120 test_compare(MEMCACHED_SUCCESS,
1121 memcached_mget(clone,
1122 keys.keys_ptr(), keys.lengths_ptr(),
1123 keys.size()));
1124
1125 uint32_t count= 0;
1126 memcached_result_st *result= NULL;
1127 while ((result= memcached_fetch_result(clone, result, NULL)))
1128 {
1129 test_compare(size_t(100), memcached_result_length(result));
1130 count++;
1131 }
1132
1133 test_true(count > 100); // If we don't get back atleast this, something is up
1134
1135 memcached_free(clone);
1136
1137 return TEST_SUCCESS;
1138 }
1139
1140 /*
1141 * Test that ensures that buffered set to not trigger problems during io_flush
1142 */
1143 #define regression_bug_490520_COUNT 200480
1144 test_return_t regression_bug_490520(memcached_st *original_memc)
1145 {
1146 memcached_st* memc= create_single_instance_memcached(original_memc, NULL);
1147
1148 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK,1);
1149 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS,1);
1150 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 1000);
1151 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT,1);
1152 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 3600);
1153
1154 /* First add all of the items.. */
1155 char blob[3333] = {0};
1156 for (uint32_t x= 0; x < regression_bug_490520_COUNT; ++x)
1157 {
1158 char key[251];
1159 int key_length= snprintf(key, sizeof(key), "0200%u", x);
1160
1161 memcached_return rc= memcached_set(memc, key, key_length, blob, sizeof(blob), 0, 0);
1162 test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED, memcached_last_error_message(memc));
1163 }
1164
1165 memcached_free(memc);
1166
1167 return TEST_SUCCESS;
1168 }
1169
1170 test_return_t regression_bug_1251482(memcached_st*)
1171 {
1172 test::Memc memc("--server=localhost:5");
1173
1174 memcached_behavior_set(&memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 0);
1175
1176 for (size_t x= 0; x < 5; ++x)
1177 {
1178 size_t value_length;
1179 memcached_return_t rc;
1180 char *value= memcached_get(&memc,
1181 test_literal_param(__func__),
1182 &value_length, NULL, &rc);
1183
1184 test_false(value);
1185 test_compare(0LLU, value_length);
1186 if (x) {
1187 test_ne_compare(MEMCACHED_SUCCESS, rc);
1188 } else {
1189 test_compare(MEMCACHED_CONNECTION_FAILURE, rc);
1190 }
1191 }
1192
1193 return TEST_SUCCESS;
1194 }
1195
1196 test_return_t regression_1009493_TEST(memcached_st*)
1197 {
1198 memcached_st* memc= memcached_create(NULL);
1199 test_true(memc);
1200 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, true));
1201
1202 memcached_st* clone= memcached_clone(NULL, memc);
1203 test_true(clone);
1204
1205 test_compare(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED),
1206 memcached_behavior_get(clone, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED));
1207
1208 memcached_free(memc);
1209 memcached_free(clone);
1210
1211 return TEST_SUCCESS;
1212 }
1213
1214 test_return_t regression_994772_TEST(memcached_st* memc)
1215 {
1216 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1));
1217
1218 test_compare(MEMCACHED_SUCCESS,
1219 memcached_set(memc,
1220 test_literal_param(__func__), // Key
1221 test_literal_param(__func__), // Value
1222 time_t(0), uint32_t(0)));
1223
1224 const char *keys[] = { __func__ };
1225 size_t key_length[]= { strlen(__func__) };
1226 test_compare(MEMCACHED_SUCCESS,
1227 memcached_mget(memc, keys, key_length, 1));
1228
1229 memcached_return_t rc;
1230 memcached_result_st *results= memcached_fetch_result(memc, NULL, &rc);
1231 test_true(results);
1232 test_compare(MEMCACHED_SUCCESS, rc);
1233
1234 test_strcmp(__func__, memcached_result_value(results));
1235 uint64_t cas_value= memcached_result_cas(results);
1236 test_true(cas_value);
1237
1238 char* take_value= memcached_result_take_value(results);
1239 test_strcmp(__func__, take_value);
1240 free(take_value);
1241
1242 memcached_result_free(results);
1243
1244 // Bad cas value, sanity check
1245 test_true(cas_value != 9999);
1246 test_compare(MEMCACHED_END,
1247 memcached_cas(memc,
1248 test_literal_param(__func__), // Key
1249 test_literal_param(__FILE__), // Value
1250 time_t(0), uint32_t(0), 9999));
1251
1252 test_compare(MEMCACHED_SUCCESS, memcached_set(memc,
1253 "different", strlen("different"), // Key
1254 test_literal_param(__FILE__), // Value
1255 time_t(0), uint32_t(0)));
1256
1257 return TEST_SUCCESS;
1258 }