Remove how use instance (keep API intact)
[m6w6/libmemcached] / tests / libmemcached-1.0 / parser.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Client and Server
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * 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 <config.h>
39 #include <libtest/test.hpp>
40
41 using namespace libtest;
42
43 #include <vector>
44 #include <string>
45 #include <cerrno>
46
47 #include <libmemcached/memcached.h>
48 #include <libmemcached/util.h>
49
50 #include <tests/libmemcached-1.0/parser.h>
51 #include <tests/print.h>
52 #include "libmemcached/instance.h"
53
54 enum scanner_type_t
55 {
56 NIL,
57 UNSIGNED,
58 SIGNED,
59 ARRAY
60 };
61
62
63 struct scanner_string_st {
64 const char *c_str;
65 size_t size;
66 };
67
68 static inline scanner_string_st scanner_string(const char *arg, size_t arg_size)
69 {
70 scanner_string_st local= { arg, arg_size };
71 return local;
72 }
73
74 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
75
76 static struct scanner_string_st scanner_string_null= { 0, 0};
77
78 struct scanner_variable_t {
79 enum scanner_type_t type;
80 struct scanner_string_st option;
81 struct scanner_string_st result;
82 test_return_t (*check_func)(memcached_st *memc, const scanner_string_st &hostname);
83 };
84
85 // Check and make sure the first host is what we expect it to be
86 static test_return_t __check_host(memcached_st *memc, const scanner_string_st &hostname)
87 {
88 memcached_server_instance_st instance=
89 memcached_server_instance_by_position(memc, 0);
90
91 test_true(instance);
92
93 const char *first_hostname = memcached_server_name(instance);
94 test_true(first_hostname);
95 test_strcmp(first_hostname, hostname.c_str);
96
97 return TEST_SUCCESS;
98 }
99
100 // Check and make sure the prefix_key is what we expect it to be
101 static test_return_t __check_namespace(memcached_st *, const scanner_string_st &)
102 {
103 #if 0
104 const char *_namespace = memcached_get_namespace(memc);
105 test_true(_namespace);
106 test_strcmp(_namespace, arg.c_str);
107 #endif
108
109 return TEST_SUCCESS;
110 }
111
112 static test_return_t __check_IO_MSG_WATERMARK(memcached_st *memc, const scanner_string_st &value)
113 {
114 uint64_t value_number;
115
116 value_number= atoll(value.c_str);
117
118 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK) == value_number);
119 return TEST_SUCCESS;
120 }
121
122 static test_return_t __check_REMOVE_FAILED_SERVERS(memcached_st *memc, const scanner_string_st &)
123 {
124 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS));
125 return TEST_SUCCESS;
126 }
127
128 static test_return_t __check_NOREPLY(memcached_st *memc, const scanner_string_st &)
129 {
130 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY));
131 return TEST_SUCCESS;
132 }
133
134 static test_return_t __check_VERIFY_KEY(memcached_st *memc, const scanner_string_st &)
135 {
136 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY));
137 return TEST_SUCCESS;
138 }
139
140 static test_return_t __check_distribution_RANDOM(memcached_st *memc, const scanner_string_st &)
141 {
142 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION) == MEMCACHED_DISTRIBUTION_RANDOM);
143 return TEST_SUCCESS;
144 }
145
146 scanner_variable_t test_server_strings[]= {
147 { ARRAY, make_scanner_string("--server=localhost"), make_scanner_string("localhost"), __check_host },
148 { ARRAY, make_scanner_string("--server=10.0.2.1"), make_scanner_string("10.0.2.1"), __check_host },
149 { ARRAY, make_scanner_string("--server=example.com"), make_scanner_string("example.com"), __check_host },
150 { ARRAY, make_scanner_string("--server=localhost:30"), make_scanner_string("localhost"), __check_host },
151 { ARRAY, make_scanner_string("--server=10.0.2.1:20"), make_scanner_string("10.0.2.1"), __check_host },
152 { ARRAY, make_scanner_string("--server=example.com:1024"), make_scanner_string("example.com"), __check_host },
153 { NIL, scanner_string_null, scanner_string_null, NULL }
154 };
155
156 scanner_variable_t test_server_strings_with_weights[]= {
157 { ARRAY, make_scanner_string("--server=10.0.2.1:30/?40"), make_scanner_string("10.0.2.1"), __check_host },
158 { ARRAY, make_scanner_string("--server=example.com:1024/?30"), make_scanner_string("example.com"), __check_host },
159 { ARRAY, make_scanner_string("--server=10.0.2.1/?20"), make_scanner_string("10.0.2.1"), __check_host },
160 { ARRAY, make_scanner_string("--server=example.com/?10"), make_scanner_string("example.com"), __check_host },
161 { NIL, scanner_string_null, scanner_string_null, NULL }
162 };
163
164 scanner_variable_t bad_test_strings[]= {
165 { ARRAY, make_scanner_string("-servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225"), scanner_string_null, NULL },
166 { ARRAY, make_scanner_string("-- servers=a.example.com:81,localhost:82,b.example.com"), scanner_string_null, NULL },
167 { ARRAY, make_scanner_string("--servers=localhost:+80"), scanner_string_null, NULL},
168 { ARRAY, make_scanner_string("--servers=localhost.com."), scanner_string_null, NULL},
169 { ARRAY, make_scanner_string("--server=localhost.com."), scanner_string_null, NULL},
170 { ARRAY, make_scanner_string("--server=localhost.com.:80"), scanner_string_null, NULL},
171 { NIL, scanner_string_null, scanner_string_null, NULL}
172 };
173
174 scanner_variable_t test_number_options[]= {
175 { ARRAY, make_scanner_string("--CONNECT-TIMEOUT=456"), scanner_string_null, NULL },
176 { ARRAY, make_scanner_string("--IO-BYTES-WATERMARK=456"), scanner_string_null, NULL },
177 { ARRAY, make_scanner_string("--IO-KEY-PREFETCH=456"), scanner_string_null, NULL },
178 { ARRAY, make_scanner_string("--IO-MSG-WATERMARK=456"), make_scanner_string("456"), __check_IO_MSG_WATERMARK },
179 { ARRAY, make_scanner_string("--NUMBER-OF-REPLICAS=456"), scanner_string_null, NULL },
180 { ARRAY, make_scanner_string("--POLL-TIMEOUT=456"), scanner_string_null, NULL },
181 { ARRAY, make_scanner_string("--RCV-TIMEOUT=456"), scanner_string_null, NULL },
182 { ARRAY, make_scanner_string("--REMOVE-FAILED-SERVERS=3"), scanner_string_null, __check_REMOVE_FAILED_SERVERS },
183 { ARRAY, make_scanner_string("--RETRY-TIMEOUT=456"), scanner_string_null, NULL },
184 { ARRAY, make_scanner_string("--SND-TIMEOUT=456"), scanner_string_null, NULL },
185 { ARRAY, make_scanner_string("--SOCKET-RECV-SIZE=456"), scanner_string_null, NULL },
186 { ARRAY, make_scanner_string("--SOCKET-SEND-SIZE=456"), scanner_string_null, NULL },
187 { NIL, scanner_string_null, scanner_string_null, NULL}
188 };
189
190 scanner_variable_t test_boolean_options[]= {
191 { ARRAY, make_scanner_string("--BINARY-PROTOCOL"), scanner_string_null, NULL },
192 { ARRAY, make_scanner_string("--BUFFER-REQUESTS"), scanner_string_null, NULL },
193 { ARRAY, make_scanner_string("--HASH-WITH-NAMESPACE"), scanner_string_null, NULL },
194 { ARRAY, make_scanner_string("--NOREPLY"), scanner_string_null, __check_NOREPLY },
195 { ARRAY, make_scanner_string("--RANDOMIZE-REPLICA-READ"), scanner_string_null, NULL },
196 { ARRAY, make_scanner_string("--SORT-HOSTS"), scanner_string_null, NULL },
197 { ARRAY, make_scanner_string("--SUPPORT-CAS"), scanner_string_null, NULL },
198 { ARRAY, make_scanner_string("--TCP-NODELAY"), scanner_string_null, NULL },
199 { ARRAY, make_scanner_string("--TCP-KEEPALIVE"), scanner_string_null, NULL },
200 { ARRAY, make_scanner_string("--TCP-KEEPIDLE"), scanner_string_null, NULL },
201 { ARRAY, make_scanner_string("--USE-UDP"), scanner_string_null, NULL },
202 { ARRAY, make_scanner_string("--VERIFY-KEY"), scanner_string_null, __check_VERIFY_KEY },
203 { NIL, scanner_string_null, scanner_string_null, NULL}
204 };
205
206 scanner_variable_t namespace_strings[]= {
207 { ARRAY, make_scanner_string("--NAMESPACE=foo"), make_scanner_string("foo"), __check_namespace },
208 { ARRAY, make_scanner_string("--NAMESPACE=\"foo\""), make_scanner_string("foo"), __check_namespace },
209 { ARRAY, make_scanner_string("--NAMESPACE=\"This_is_a_very_long_key\""), make_scanner_string("This_is_a_very_long_key"), __check_namespace },
210 { NIL, scanner_string_null, scanner_string_null, NULL}
211 };
212
213 scanner_variable_t distribution_strings[]= {
214 { ARRAY, make_scanner_string("--DISTRIBUTION=consistent"), scanner_string_null, NULL },
215 { ARRAY, make_scanner_string("--DISTRIBUTION=consistent,CRC"), scanner_string_null, NULL },
216 { ARRAY, make_scanner_string("--DISTRIBUTION=consistent,MD5"), scanner_string_null, NULL },
217 { ARRAY, make_scanner_string("--DISTRIBUTION=random"), scanner_string_null, __check_distribution_RANDOM },
218 { ARRAY, make_scanner_string("--DISTRIBUTION=modula"), scanner_string_null, NULL },
219 { NIL, scanner_string_null, scanner_string_null, NULL}
220 };
221
222 scanner_variable_t hash_strings[]= {
223 { ARRAY, make_scanner_string("--HASH=CRC"), scanner_string_null, NULL },
224 { ARRAY, make_scanner_string("--HASH=FNV1A_32"), scanner_string_null, NULL },
225 { ARRAY, make_scanner_string("--HASH=FNV1_32"), scanner_string_null, NULL },
226 #if 0
227 { ARRAY, make_scanner_string("--HASH=JENKINS"), scanner_string_null, NULL },
228 #endif
229 { ARRAY, make_scanner_string("--HASH=MD5"), scanner_string_null, NULL },
230 { NIL, scanner_string_null, scanner_string_null, NULL}
231 };
232
233
234 static test_return_t _test_option(scanner_variable_t *scanner, bool test_true_opt= true)
235 {
236 for (scanner_variable_t *ptr= scanner; ptr->type != NIL; ptr++)
237 {
238 memcached_st *memc= memcached(ptr->option.c_str, ptr->option.size);
239
240 // The case that it should have parsed, but it didn't. We will inspect for
241 // an error with libmemcached_check_configuration()
242 if (memc == NULL and test_true_opt)
243 {
244 char buffer[2048];
245 bool success= libmemcached_check_configuration(ptr->option.c_str, ptr->option.size, buffer, sizeof(buffer));
246
247 std::string temp(buffer);
248 temp+= " with option string:";
249 temp+= ptr->option.c_str;
250 test_true_got(success, temp.c_str());
251 Error << "Failed for " << temp;
252
253 return TEST_FAILURE; // The line above should fail since memc should be null
254 }
255
256 if (test_true_opt)
257 {
258 if (ptr->check_func)
259 {
260 test_return_t test_rc= (*ptr->check_func)(memc, ptr->result);
261 if (test_rc != TEST_SUCCESS)
262 {
263 memcached_free(memc);
264 return test_rc;
265 }
266 }
267
268 memcached_free(memc);
269 }
270 else
271 {
272 test_false_with(memc, ptr->option.c_str);
273 }
274 }
275
276 return TEST_SUCCESS;
277 }
278
279 test_return_t server_test(memcached_st *)
280 {
281 return _test_option(test_server_strings);
282 }
283
284 test_return_t server_with_weight_test(memcached_st *)
285 {
286 return _test_option(test_server_strings_with_weights);
287 }
288
289 test_return_t servers_bad_test(memcached_st *)
290 {
291 test_return_t rc;
292 if ((rc= _test_option(bad_test_strings, false)) != TEST_SUCCESS)
293 {
294 return rc;
295 }
296
297 return TEST_SUCCESS;
298 }
299
300 test_return_t parser_number_options_test(memcached_st*)
301 {
302 return _test_option(test_number_options);
303 }
304
305 test_return_t parser_boolean_options_test(memcached_st*)
306 {
307 return _test_option(test_boolean_options);
308 }
309
310 test_return_t behavior_parser_test(memcached_st*)
311 {
312 return TEST_SUCCESS;
313 }
314
315 test_return_t parser_hash_test(memcached_st*)
316 {
317 return _test_option(hash_strings);
318 }
319
320 test_return_t parser_distribution_test(memcached_st*)
321 {
322 return _test_option(distribution_strings);
323 }
324
325 test_return_t parser_key_prefix_test(memcached_st*)
326 {
327 return _test_option(distribution_strings);
328 }
329
330 test_return_t test_namespace_keyword(memcached_st*)
331 {
332 return _test_option(namespace_strings);
333 }
334
335 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
336
337 test_return_t memcached_create_with_options_with_filename(memcached_st*)
338 {
339 test_skip(0, access(SUPPORT_EXAMPLE_CNF, R_OK));
340
341 memcached_st *memc_ptr= memcached(test_literal_param("--CONFIGURE-FILE=\"support/example.cnf\""));
342 test_true_got(memc_ptr, "memcached() failed");
343 memcached_free(memc_ptr);
344
345 return TEST_SUCCESS;
346 }
347
348 test_return_t libmemcached_check_configuration_with_filename_test(memcached_st*)
349 {
350 test_skip(0, access(SUPPORT_EXAMPLE_CNF, R_OK));
351
352 char buffer[BUFSIZ];
353
354 test_compare_hint(MEMCACHED_SUCCESS,
355 libmemcached_check_configuration(test_literal_param("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer)),
356 buffer);
357
358 test_compare_hint(MEMCACHED_SUCCESS,
359 libmemcached_check_configuration(test_literal_param("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer)),
360 buffer);
361
362 test_compare_hint(MEMCACHED_ERRNO,
363 libmemcached_check_configuration(test_literal_param("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer)),
364 buffer) ;
365
366 return TEST_SUCCESS;
367 }
368
369 test_return_t libmemcached_check_configuration_test(memcached_st*)
370 {
371 char buffer[BUFSIZ];
372 test_compare(MEMCACHED_SUCCESS,
373 libmemcached_check_configuration(test_literal_param("--server=localhost"), buffer, sizeof(buffer)));
374
375 test_compare_hint(MEMCACHED_PARSE_ERROR,
376 libmemcached_check_configuration(test_literal_param("--dude=localhost"), buffer, sizeof(buffer)),
377 buffer);
378
379 return TEST_SUCCESS;
380 }
381
382 test_return_t memcached_create_with_options_test(memcached_st*)
383 {
384 {
385 memcached_st *memc_ptr;
386 memc_ptr= memcached(test_literal_param("--server=localhost"));
387 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
388 memcached_free(memc_ptr);
389 }
390
391 {
392 memcached_st *memc_ptr= memcached(test_literal_param("--dude=localhost"));
393 test_false_with(memc_ptr, memcached_last_error_message(memc_ptr));
394 }
395
396 return TEST_SUCCESS;
397 }
398
399 test_return_t test_include_keyword(memcached_st*)
400 {
401 test_skip(0, access(SUPPORT_EXAMPLE_CNF, R_OK));
402
403 char buffer[BUFSIZ];
404 test_compare(MEMCACHED_SUCCESS,
405 libmemcached_check_configuration(test_literal_param("INCLUDE \"support/example.cnf\""), buffer, sizeof(buffer)));
406
407 return TEST_SUCCESS;
408 }
409
410 test_return_t test_end_keyword(memcached_st*)
411 {
412 char buffer[BUFSIZ];
413 test_compare(MEMCACHED_SUCCESS,
414 libmemcached_check_configuration(test_literal_param("--server=localhost END bad keywords"), buffer, sizeof(buffer)));
415
416 return TEST_SUCCESS;
417 }
418
419 test_return_t test_reset_keyword(memcached_st*)
420 {
421 char buffer[BUFSIZ];
422 test_compare(MEMCACHED_SUCCESS,
423 libmemcached_check_configuration(test_literal_param("--server=localhost reset --server=bad.com"), buffer, sizeof(buffer)));
424
425 return TEST_SUCCESS;
426 }
427
428 test_return_t test_error_keyword(memcached_st*)
429 {
430 char buffer[BUFSIZ];
431 memcached_return_t rc;
432 rc= libmemcached_check_configuration(test_literal_param("--server=localhost ERROR --server=bad.com"), buffer, sizeof(buffer));
433 test_true_got(rc != MEMCACHED_SUCCESS, buffer);
434
435 return TEST_SUCCESS;
436 }
437
438 #define RANDOM_STRINGS 1000
439 test_return_t random_statement_build_test(memcached_st*)
440 {
441 std::vector<scanner_string_st *> option_list;
442
443 for (scanner_variable_t *ptr= test_server_strings; ptr->type != NIL; ptr++)
444 option_list.push_back(&ptr->option);
445
446 for (scanner_variable_t *ptr= test_number_options; ptr->type != NIL; ptr++)
447 option_list.push_back(&ptr->option);
448
449 for (scanner_variable_t *ptr= test_boolean_options; ptr->type != NIL; ptr++)
450 option_list.push_back(&ptr->option);
451
452 for (scanner_variable_t *ptr= namespace_strings; ptr->type != NIL; ptr++)
453 option_list.push_back(&ptr->option);
454
455 for (scanner_variable_t *ptr= distribution_strings; ptr->type != NIL; ptr++)
456 option_list.push_back(&ptr->option);
457
458 for (scanner_variable_t *ptr= hash_strings; ptr->type != NIL; ptr++)
459 option_list.push_back(&ptr->option);
460
461 std::vector<bool> used_list;
462 used_list.resize(option_list.size());
463
464 struct used_options_st {
465 bool has_hash;
466 bool has_namespace;
467 bool has_distribution;
468 bool has_buffer_requests;
469 bool has_udp;
470 bool has_binary;
471 bool has_verify_key;
472
473 used_options_st() :
474 has_hash(false),
475 has_namespace(false),
476 has_distribution(false),
477 has_buffer_requests(false),
478 has_udp(false),
479 has_binary(false),
480 has_verify_key(false)
481 {
482 }
483 } used_options;
484
485 for (uint32_t x= 0; x < RANDOM_STRINGS; x++)
486 {
487 std::string random_options;
488
489 uint32_t number_of= random() % option_list.size();
490 for (uint32_t options= 0; options < number_of; options++)
491 {
492 size_t option_list_position= random() % option_list.size();
493
494 if (used_list[option_list_position])
495 {
496 continue;
497 }
498 used_list[option_list_position]= true;
499
500 std::string random_string= option_list[option_list_position]->c_str;
501
502 if (random_string.compare(0, test_literal_compare_param("--HASH")) == 0)
503 {
504 if (used_options.has_hash)
505 {
506 continue;
507 }
508
509 if (used_options.has_distribution)
510 {
511 continue;
512 }
513 used_options.has_hash= true;
514 }
515
516 if (random_string.compare(0, test_literal_compare_param("--NAMESPACE")) == 0)
517 {
518 if (used_options.has_namespace)
519 {
520 continue;
521 }
522 used_options.has_namespace= true;
523 }
524
525 if (random_string.compare(0, test_literal_compare_param("--USE-UDP")) == 0)
526 {
527 if (used_options.has_udp)
528 {
529 continue;
530 }
531 used_options.has_udp= true;
532
533 if (used_options.has_buffer_requests)
534 {
535 continue;
536 }
537 }
538
539 if (random_string.compare(0, test_literal_compare_param("--BUFFER-REQUESTS")) == 0)
540 {
541 if (used_options.has_buffer_requests)
542 {
543 continue;
544 }
545 used_options.has_buffer_requests= true;
546
547 if (used_options.has_udp)
548 {
549 continue;
550 }
551 }
552
553 if (random_string.compare(0, test_literal_compare_param("--BINARY-PROTOCOL")) == 0)
554 {
555 if (used_options.has_binary)
556 {
557 continue;
558 }
559 used_options.has_binary= true;
560
561 if (used_options.has_verify_key)
562 {
563 continue;
564 }
565 }
566
567 if (random_string.compare(0, test_literal_compare_param("--VERIFY-KEY")) == 0)
568 {
569 if (used_options.has_verify_key)
570 {
571 continue;
572 }
573 used_options.has_verify_key= true;
574
575 if (used_options.has_binary)
576 {
577 continue;
578 }
579 }
580
581 if (random_string.compare(0, test_literal_compare_param("--DISTRIBUTION")) == 0)
582 {
583 if (used_options.has_distribution)
584 {
585 continue;
586 }
587
588 if (used_options.has_hash)
589 {
590 continue;
591 }
592 used_options.has_distribution= true;
593 }
594
595 random_options+= random_string;
596 random_options+= " ";
597 }
598
599 if (random_options.size() <= 1)
600 {
601 continue;
602 }
603
604 random_options.resize(random_options.size() -1);
605
606 char buffer[BUFSIZ];
607 memcached_return_t rc= libmemcached_check_configuration(random_options.c_str(), random_options.size(), buffer, sizeof(buffer));
608 if (memcached_failed(rc))
609 {
610 Error << "libmemcached_check_configuration(" << random_options << ") : " << buffer;
611 return TEST_FAILURE;
612 }
613 }
614
615 return TEST_SUCCESS;
616 }
617
618 static memcached_return_t dump_server_information(const memcached_st *,
619 memcached_server_instance_st instance,
620 void *)
621 {
622 if (strcmp(memcached_server_name(instance), "localhost"))
623 {
624 fatal_assert(not memcached_server_name(instance));
625 return MEMCACHED_FAILURE;
626 }
627
628 if (memcached_server_port(instance) < 8888 or memcached_server_port(instance) > 8892)
629 {
630 fatal_assert(not memcached_server_port(instance));
631 return MEMCACHED_FAILURE;
632 }
633
634 if (instance->weight > 5 or instance->weight < 2)
635 {
636 fatal_assert(not instance->weight);
637 return MEMCACHED_FAILURE;
638 }
639
640 return MEMCACHED_SUCCESS;
641 }
642
643 test_return_t test_hostname_port_weight(memcached_st *)
644 {
645 const char *server_string= "--server=localhost:8888/?2 --server=localhost:8889/?3 --server=localhost:8890/?4 --server=localhost:8891/?5 --server=localhost:8892/?3";
646 char buffer[BUFSIZ];
647
648 test_compare_got(MEMCACHED_SUCCESS,
649 libmemcached_check_configuration(server_string, strlen(server_string), buffer, sizeof(buffer)), buffer);
650
651 memcached_st *memc= memcached(server_string, strlen(server_string));
652 test_true(memc);
653
654 memcached_server_fn callbacks[]= { dump_server_information };
655 test_true(memcached_success(memcached_server_cursor(memc, callbacks, NULL, 1)));
656
657 memcached_free(memc);
658
659 return TEST_SUCCESS;
660 }
661
662 struct socket_weight_t {
663 const char *socket;
664 size_t weight;
665 };
666
667 static memcached_return_t dump_socket_information(const memcached_st *,
668 memcached_server_instance_st instance,
669 void *context)
670 {
671 socket_weight_t *check= (socket_weight_t *)context;
672
673 if (strcmp(memcached_server_name(instance), check->socket))
674 {
675 Error << memcached_server_name(instance) << " != " << check->socket;
676 return MEMCACHED_FAILURE;
677 }
678
679 if (instance->weight == check->weight)
680 {
681 return MEMCACHED_SUCCESS;
682 }
683
684 return MEMCACHED_FAILURE;
685 }
686
687 test_return_t test_parse_socket(memcached_st *)
688 {
689 char buffer[BUFSIZ];
690
691 memcached_server_fn callbacks[]= { dump_socket_information };
692 {
693 test_compare_got(MEMCACHED_SUCCESS,
694 libmemcached_check_configuration(test_literal_param("--socket=\"/tmp/foo\""), buffer, sizeof(buffer)),
695 buffer);
696
697 memcached_st *memc= memcached(test_literal_param("--socket=\"/tmp/foo\""));
698 test_true(memc);
699 socket_weight_t check= { "/tmp/foo", 1 };
700 test_compare(MEMCACHED_SUCCESS,
701 memcached_server_cursor(memc, callbacks, &check, 1));
702 memcached_free(memc);
703 }
704
705 {
706 test_compare_got(MEMCACHED_SUCCESS,
707 libmemcached_check_configuration(test_literal_param("--socket=\"/tmp/foo\"/?23"), buffer, sizeof(buffer)),
708 buffer);
709
710 memcached_st *memc= memcached(test_literal_param("--socket=\"/tmp/foo\"/?23"));
711 test_true(memc);
712 socket_weight_t check= { "/tmp/foo", 23 };
713 test_compare(MEMCACHED_SUCCESS,
714 memcached_server_cursor(memc, callbacks, &check, 1));
715 memcached_free(memc);
716 }
717
718 return TEST_SUCCESS;
719 }
720
721 /*
722 By setting the timeout value to zero, we force poll() to return immediatly.
723 */
724 test_return_t regression_bug_71231153_connect(memcached_st *)
725 {
726 if (libmemcached_util_ping("10.0.2.252", 0, NULL)) // If for whatever reason someone has a host at this address, skip
727 return TEST_SKIPPED;
728
729 { // Test the connect-timeout, on a bad host we should get MEMCACHED_CONNECTION_FAILURE
730 memcached_st *memc= memcached(test_literal_param("--SERVER=10.0.2.252 --CONNECT-TIMEOUT=0"));
731 test_true(memc);
732 test_zero(memc->connect_timeout);
733 test_compare(MEMCACHED_DEFAULT_TIMEOUT, memc->poll_timeout);
734
735 memcached_return_t rc;
736 size_t value_len;
737 char *value= memcached_get(memc, test_literal_param("test"), &value_len, NULL, &rc);
738 test_false(value);
739 test_zero(value_len);
740 test_compare_got(MEMCACHED_TIMEOUT, rc, memcached_last_error_message(memc));
741
742 memcached_free(memc);
743 }
744
745 return TEST_SUCCESS;
746 }
747
748 test_return_t regression_bug_71231153_poll(memcached_st *)
749 {
750 if (libmemcached_util_ping("10.0.2.252", 0, NULL)) // If for whatever reason someone has a host at this address, skip
751 return TEST_SKIPPED;
752
753 { // Test the poll timeout, on a bad host we should get MEMCACHED_CONNECTION_FAILURE
754 memcached_st *memc= memcached(test_literal_param("--SERVER=10.0.2.252 --POLL-TIMEOUT=0"));
755 test_true(memc);
756 test_compare(MEMCACHED_DEFAULT_CONNECT_TIMEOUT, memc->connect_timeout);
757 test_zero(memc->poll_timeout);
758
759 memcached_return_t rc;
760 size_t value_len;
761 char *value= memcached_get(memc, test_literal_param("test"), &value_len, NULL, &rc);
762 test_false(value);
763 test_zero(value_len);
764 #ifdef __APPLE__
765 test_compare_got(MEMCACHED_CONNECTION_FAILURE, rc, memcached_last_error_message(memc));
766 #else
767 test_compare_got(MEMCACHED_TIMEOUT, rc, memcached_last_error_message(memc));
768 #endif
769
770 memcached_free(memc);
771 }
772
773 return TEST_SUCCESS;
774 }