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