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