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