Merge of build
[m6w6/libmemcached] / tests / 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 <iostream>
45 #include <string>
46 #include <cerrno>
47 #include <cassert>
48
49 #include <libmemcached/memcached.h>
50 #include <libmemcached/util.h>
51
52 #include <tests/parser.h>
53 #include <tests/print.h>
54
55 enum scanner_type_t
56 {
57 NIL,
58 UNSIGNED,
59 SIGNED,
60 ARRAY
61 };
62
63
64 struct scanner_string_st {
65 const char *c_str;
66 size_t size;
67 };
68
69 static inline scanner_string_st scanner_string(const char *arg, size_t arg_size)
70 {
71 scanner_string_st local= { arg, arg_size };
72 return local;
73 }
74
75 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
76
77 static struct scanner_string_st scanner_string_null= { 0, 0};
78
79 struct scanner_variable_t {
80 enum scanner_type_t type;
81 struct scanner_string_st option;
82 struct scanner_string_st result;
83 test_return_t (*check_func)(memcached_st *memc, const scanner_string_st &hostname);
84 };
85
86 // Check and make sure the first host is what we expect it to be
87 static test_return_t __check_host(memcached_st *memc, const scanner_string_st &hostname)
88 {
89 memcached_server_instance_st instance=
90 memcached_server_instance_by_position(memc, 0);
91
92 test_true(instance);
93
94 const char *first_hostname = memcached_server_name(instance);
95 test_true(first_hostname);
96 test_strcmp(first_hostname, hostname.c_str);
97
98 return TEST_SUCCESS;
99 }
100
101 // Check and make sure the prefix_key is what we expect it to be
102 static test_return_t __check_namespace(memcached_st *memc, const scanner_string_st &arg)
103 {
104 const char *_namespace = memcached_get_namespace(memc);
105 test_true(_namespace);
106 test_strcmp(_namespace, arg.c_str);
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 { ARRAY, make_scanner_string("--HASH=JENKINS"), scanner_string_null, NULL },
226 { ARRAY, make_scanner_string("--HASH=MD5"), scanner_string_null, NULL },
227 { NIL, scanner_string_null, scanner_string_null, NULL}
228 };
229
230
231 static test_return_t _test_option(scanner_variable_t *scanner, bool test_true_opt= true)
232 {
233 for (scanner_variable_t *ptr= scanner; ptr->type != NIL; ptr++)
234 {
235 memcached_st *memc= memcached(ptr->option.c_str, ptr->option.size);
236
237 // The case that it should have parsed, but it didn't. We will inspect
238 // for an error with libmemcached_check_configuration()
239 if (not memc and test_true_opt)
240 {
241 char buffer[2048];
242 bool success= libmemcached_check_configuration(ptr->option.c_str, ptr->option.size, buffer, sizeof(buffer));
243
244 std::string temp(buffer);
245 temp+= " with option string:";
246 temp+= ptr->option.c_str;
247 test_true_got(success, temp.c_str());
248
249 return TEST_FAILURE; // The line above should fail since memc should be null
250 }
251
252 if (test_true_opt)
253 {
254 if (ptr->check_func)
255 {
256 test_return_t test_rc= (*ptr->check_func)(memc, ptr->result);
257 if (test_rc != TEST_SUCCESS)
258 {
259 memcached_free(memc);
260 return test_rc;
261 }
262 }
263
264 memcached_free(memc);
265 }
266 else
267 {
268 test_false_with(memc, ptr->option.c_str);
269 }
270 }
271
272 return TEST_SUCCESS;
273 }
274
275 test_return_t server_test(memcached_st *)
276 {
277 return _test_option(test_server_strings);
278 }
279
280 test_return_t server_with_weight_test(memcached_st *)
281 {
282 return _test_option(test_server_strings_with_weights);
283 }
284
285 test_return_t servers_bad_test(memcached_st *)
286 {
287 test_return_t rc;
288 if ((rc= _test_option(bad_test_strings, false)) != TEST_SUCCESS)
289 {
290 return rc;
291 }
292
293 return TEST_SUCCESS;
294 }
295
296 test_return_t parser_number_options_test(memcached_st*)
297 {
298 return _test_option(test_number_options);
299 }
300
301 test_return_t parser_boolean_options_test(memcached_st*)
302 {
303 return _test_option(test_boolean_options);
304 }
305
306 test_return_t behavior_parser_test(memcached_st*)
307 {
308 return TEST_SUCCESS;
309 }
310
311 test_return_t parser_hash_test(memcached_st*)
312 {
313 return _test_option(hash_strings);
314 }
315
316 test_return_t parser_distribution_test(memcached_st*)
317 {
318 return _test_option(distribution_strings);
319 }
320
321 test_return_t parser_key_prefix_test(memcached_st*)
322 {
323 return _test_option(distribution_strings);
324 }
325
326 test_return_t test_namespace_keyword(memcached_st*)
327 {
328 return _test_option(namespace_strings);
329 }
330
331 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
332
333 test_return_t memcached_create_with_options_with_filename(memcached_st*)
334 {
335 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
336 return TEST_SKIPPED;
337
338 memcached_st *memc_ptr;
339 memc_ptr= memcached(test_literal_param("--CONFIGURE-FILE=\"support/example.cnf\""));
340 test_true_got(memc_ptr, "memcached() failed");
341 memcached_free(memc_ptr);
342
343 return TEST_SUCCESS;
344 }
345
346 test_return_t libmemcached_check_configuration_with_filename_test(memcached_st*)
347 {
348 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
349 return TEST_SKIPPED;
350
351 memcached_return_t rc;
352 char buffer[BUFSIZ];
353
354 rc= libmemcached_check_configuration(test_literal_param("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer));
355 test_true_got(rc == MEMCACHED_SUCCESS, (rc == MEMCACHED_ERRNO) ? strerror(errno) : memcached_strerror(NULL, rc));
356
357 rc= libmemcached_check_configuration(test_literal_param("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer));
358 test_false_with(rc == MEMCACHED_SUCCESS, memcached_strerror(NULL, rc));
359
360 rc= libmemcached_check_configuration(test_literal_param("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer));
361 test_true_got(rc == MEMCACHED_ERRNO, memcached_strerror(NULL, rc));
362
363 return TEST_SUCCESS;
364 }
365
366 test_return_t libmemcached_check_configuration_test(memcached_st*)
367 {
368 memcached_return_t rc;
369 char buffer[BUFSIZ];
370
371 test_compare(MEMCACHED_SUCCESS,
372 libmemcached_check_configuration(test_literal_param("--server=localhost"), buffer, sizeof(buffer)));
373
374 rc= libmemcached_check_configuration(test_literal_param("--dude=localhost"), buffer, sizeof(buffer));
375 test_false_with(rc == MEMCACHED_SUCCESS, buffer);
376 test_true(rc == MEMCACHED_PARSE_ERROR);
377
378 return TEST_SUCCESS;
379 }
380
381 test_return_t memcached_create_with_options_test(memcached_st*)
382 {
383 memcached_st *memc_ptr;
384 memc_ptr= memcached(test_literal_param("--server=localhost"));
385 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
386 memcached_free(memc_ptr);
387
388 memc_ptr= memcached(test_literal_param("--dude=localhost"));
389 test_false_with(memc_ptr, memcached_last_error_message(memc_ptr));
390
391 return TEST_SUCCESS;
392 }
393
394 test_return_t test_include_keyword(memcached_st*)
395 {
396 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
397 return TEST_SKIPPED;
398
399 char buffer[BUFSIZ];
400 test_compare(MEMCACHED_SUCCESS,
401 libmemcached_check_configuration(test_literal_param("INCLUDE \"support/example.cnf\""), buffer, sizeof(buffer)));
402
403 return TEST_SUCCESS;
404 }
405
406 test_return_t test_end_keyword(memcached_st*)
407 {
408 char buffer[BUFSIZ];
409 test_compare(MEMCACHED_SUCCESS,
410 libmemcached_check_configuration(test_literal_param("--server=localhost END bad keywords"), buffer, sizeof(buffer)));
411
412 return TEST_SUCCESS;
413 }
414
415 test_return_t test_reset_keyword(memcached_st*)
416 {
417 char buffer[BUFSIZ];
418 test_compare(MEMCACHED_SUCCESS,
419 libmemcached_check_configuration(test_literal_param("--server=localhost reset --server=bad.com"), buffer, sizeof(buffer)));
420
421 return TEST_SUCCESS;
422 }
423
424 test_return_t test_error_keyword(memcached_st*)
425 {
426 char buffer[BUFSIZ];
427 memcached_return_t rc;
428 rc= libmemcached_check_configuration(test_literal_param("--server=localhost ERROR --server=bad.com"), buffer, sizeof(buffer));
429 test_true_got(rc != MEMCACHED_SUCCESS, buffer);
430
431 return TEST_SUCCESS;
432 }
433
434 #define RANDOM_STRINGS 100
435 test_return_t random_statement_build_test(memcached_st*)
436 {
437 std::vector<scanner_string_st *> option_list;
438
439 for (scanner_variable_t *ptr= test_server_strings; ptr->type != NIL; ptr++)
440 option_list.push_back(&ptr->option);
441
442 for (scanner_variable_t *ptr= test_number_options; ptr->type != NIL; ptr++)
443 option_list.push_back(&ptr->option);
444
445 for (scanner_variable_t *ptr= test_boolean_options; ptr->type != NIL; ptr++)
446 option_list.push_back(&ptr->option);
447
448 for (scanner_variable_t *ptr= namespace_strings; ptr->type != NIL; ptr++)
449 option_list.push_back(&ptr->option);
450
451 for (scanner_variable_t *ptr= distribution_strings; ptr->type != NIL; ptr++)
452 option_list.push_back(&ptr->option);
453
454 for (scanner_variable_t *ptr= hash_strings; ptr->type != NIL; ptr++)
455 option_list.push_back(&ptr->option);
456
457 for (uint32_t x= 0; x < RANDOM_STRINGS; x++)
458 {
459 std::string random_options;
460
461 uint32_t number_of= random() % option_list.size();
462 for (uint32_t options= 0; options < number_of; options++)
463 {
464 random_options+= option_list[random() % option_list.size()]->c_str;
465 random_options+= " ";
466 }
467
468 memcached_st *memc_ptr= memcached(random_options.c_str(), random_options.size() -1);
469 if (not memc_ptr)
470 {
471 switch (errno)
472 {
473 case EINVAL:
474 #if 0 // Testing framework is not smart enough for this just yet.
475 {
476 // We will try to find the specific error
477 char buffer[2048];
478 memcached_return_t rc= libmemcached_check_configuration(random_options.c_str(), random_options.size(), buffer, sizeof(buffer));
479 test_true_got(rc != MEMCACHED_SUCCESS, "memcached_create_with_options() failed whiled libmemcached_check_configuration() was successful");
480 std::cerr << "Error occured on " << random_options.c_str() << " : " << buffer << std::endl;
481 return TEST_FAILURE;
482 }
483 #endif
484 break;
485 case ENOMEM:
486 std::cerr << "Failed to allocate memory for memcached_create_with_options()" << std::endl;
487 memcached_free(memc_ptr);
488 return TEST_FAILURE;
489 default:
490 std::cerr << "Unknown error from memcached_create_with_options?!!" << std::endl;
491 memcached_free(memc_ptr);
492 return TEST_FAILURE;
493 }
494 }
495 memcached_free(memc_ptr);
496 }
497
498 return TEST_SUCCESS;
499 }
500
501 static memcached_return_t dump_server_information(const memcached_st *,
502 const memcached_server_st *instance,
503 void *)
504 {
505 if (strcmp(memcached_server_name(instance), "localhost"))
506 {
507 assert(not memcached_server_name(instance));
508 return MEMCACHED_FAILURE;
509 }
510
511 if (memcached_server_port(instance) < 8888 or memcached_server_port(instance) > 8892)
512 {
513 assert(not memcached_server_port(instance));
514 return MEMCACHED_FAILURE;
515 }
516
517 if (instance->weight > 5 or instance->weight < 2)
518 {
519 assert(not instance->weight);
520 return MEMCACHED_FAILURE;
521 }
522
523 return MEMCACHED_SUCCESS;
524 }
525
526
527 test_return_t test_hostname_port_weight(memcached_st *)
528 {
529 const char *server_string= "--server=localhost:8888/?2 --server=localhost:8889/?3 --server=localhost:8890/?4 --server=localhost:8891/?5 --server=localhost:8892/?3";
530 char buffer[BUFSIZ];
531
532 test_compare_got(MEMCACHED_SUCCESS,
533 libmemcached_check_configuration(server_string, strlen(server_string), buffer, sizeof(buffer)), buffer);
534
535 memcached_st *memc= memcached(server_string, strlen(server_string));
536 test_true(memc);
537
538 memcached_server_fn callbacks[]= { dump_server_information };
539 test_true(memcached_success(memcached_server_cursor(memc, callbacks, NULL, 1)));
540
541 memcached_free(memc);
542
543 return TEST_SUCCESS;
544 }
545
546 struct socket_weight_t {
547 const char *socket;
548 size_t weight;
549 };
550
551 static memcached_return_t dump_socket_information(const memcached_st *,
552 const memcached_server_st *instance,
553 void *context)
554 {
555 socket_weight_t *check= (socket_weight_t *)context;
556
557 if (strcmp(memcached_server_name(instance), check->socket))
558 {
559 std::cerr << std::endl << __FILE__ << ":" << __LINE__ << " " << memcached_server_name(instance) << " != " << check->socket << std::endl;
560 return MEMCACHED_FAILURE;
561 }
562
563 if (instance->weight == check->weight)
564 {
565 return MEMCACHED_SUCCESS;
566 }
567
568 return MEMCACHED_FAILURE;
569 }
570
571 test_return_t test_parse_socket(memcached_st *)
572 {
573 char buffer[BUFSIZ];
574
575 memcached_server_fn callbacks[]= { dump_socket_information };
576 {
577 test_compare_got(MEMCACHED_SUCCESS,
578 libmemcached_check_configuration(test_literal_param("--socket=\"/tmp/foo\""), buffer, sizeof(buffer)),
579 buffer);
580
581 memcached_st *memc= memcached(test_literal_param("--socket=\"/tmp/foo\""));
582 test_true(memc);
583 socket_weight_t check= { "/tmp/foo", 1 };
584 test_compare(MEMCACHED_SUCCESS,
585 memcached_server_cursor(memc, callbacks, &check, 1));
586 memcached_free(memc);
587 }
588
589 {
590 test_compare_got(MEMCACHED_SUCCESS,
591 libmemcached_check_configuration(test_literal_param("--socket=\"/tmp/foo\"/?23"), buffer, sizeof(buffer)),
592 buffer);
593
594 memcached_st *memc= memcached(test_literal_param("--socket=\"/tmp/foo\"/?23"));
595 test_true(memc);
596 socket_weight_t check= { "/tmp/foo", 23 };
597 test_compare(MEMCACHED_SUCCESS,
598 memcached_server_cursor(memc, callbacks, &check, 1));
599 memcached_free(memc);
600 }
601
602 return TEST_SUCCESS;
603 }
604
605 /*
606 By setting the timeout value to zero, we force poll() to return immediatly.
607 */
608 test_return_t regression_bug_71231153_connect(memcached_st *)
609 {
610 if (libmemcached_util_ping("10.0.2.252", 0, NULL)) // If for whatever reason someone has a host at this address, skip
611 return TEST_SKIPPED;
612
613 { // Test the connect-timeout, on a bad host we should get MEMCACHED_CONNECTION_FAILURE
614 memcached_st *memc= memcached(test_literal_param("--SERVER=10.0.2.252 --CONNECT-TIMEOUT=0"));
615 test_true(memc);
616 test_zero(memc->connect_timeout);
617 test_compare(MEMCACHED_DEFAULT_TIMEOUT, memc->poll_timeout);
618
619 memcached_return_t rc;
620 size_t value_len;
621 char *value= memcached_get(memc, test_literal_param("test"), &value_len, NULL, &rc);
622 test_false(value);
623 test_zero(value_len);
624 test_compare_got(MEMCACHED_TIMEOUT, rc, memcached_last_error_message(memc));
625
626 memcached_free(memc);
627 }
628
629 return TEST_SUCCESS;
630 }
631
632 test_return_t regression_bug_71231153_poll(memcached_st *)
633 {
634 if (libmemcached_util_ping("10.0.2.252", 0, NULL)) // If for whatever reason someone has a host at this address, skip
635 return TEST_SKIPPED;
636
637 { // Test the poll timeout, on a bad host we should get MEMCACHED_CONNECTION_FAILURE
638 memcached_st *memc= memcached(test_literal_param("--SERVER=10.0.2.252 --POLL-TIMEOUT=0"));
639 test_true(memc);
640 test_compare(MEMCACHED_DEFAULT_CONNECT_TIMEOUT, memc->connect_timeout);
641 test_zero(memc->poll_timeout);
642
643 memcached_return_t rc;
644 size_t value_len;
645 char *value= memcached_get(memc, test_literal_param("test"), &value_len, NULL, &rc);
646 test_false(value);
647 test_zero(value_len);
648 #ifdef __APPLE__
649 test_compare_got(MEMCACHED_CONNECTION_FAILURE, rc, memcached_last_error_message(memc));
650 #else
651 test_compare_got(MEMCACHED_TIMEOUT, rc, memcached_last_error_message(memc));
652 #endif
653
654 memcached_free(memc);
655 }
656
657 return TEST_SUCCESS;
658 }