Merge in additional fix for manpages.
[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
40 #include <vector>
41 #include <iostream>
42 #include <string>
43 #include <errno.h>
44
45 #define BUILDING_LIBMEMCACHED
46 #include <libmemcached/memcached.h>
47
48 #include "tests/parser.h"
49 #include "tests/print.h"
50
51 enum scanner_type_t
52 {
53 NIL,
54 UNSIGNED,
55 SIGNED,
56 ARRAY
57 };
58
59
60 struct scanner_string_st {
61 const char *c_str;
62 size_t size;
63 };
64
65 static inline scanner_string_st scanner_string(const char *arg, size_t arg_size)
66 {
67 scanner_string_st local= { arg, arg_size };
68 return local;
69 }
70
71 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
72
73 static struct scanner_string_st scanner_string_null= { 0, 0};
74
75 struct scanner_variable_t {
76 enum scanner_type_t type;
77 struct scanner_string_st option;
78 struct scanner_string_st result;
79 test_return_t (*check_func)(memcached_st *memc, const scanner_string_st &hostname);
80 };
81
82 // Check and make sure the first host is what we expect it to be
83 static test_return_t __check_host(memcached_st *memc, const scanner_string_st &hostname)
84 {
85 memcached_server_instance_st instance=
86 memcached_server_instance_by_position(memc, 0);
87
88 test_true(instance);
89
90 const char *first_hostname = memcached_server_name(instance);
91 test_true(first_hostname);
92 test_strcmp(first_hostname, hostname.c_str);
93
94 return TEST_SUCCESS;
95 }
96
97 // Check and make sure the prefix_key is what we expect it to be
98 static test_return_t __check_prefix_key(memcached_st *memc, const scanner_string_st &hostname)
99 {
100 memcached_server_instance_st instance=
101 memcached_server_instance_by_position(memc, 0);
102
103 test_true(instance);
104
105 const char *first_hostname = memcached_server_name(instance);
106 test_true(first_hostname);
107 test_strcmp(first_hostname, hostname.c_str);
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 prefix_key_strings[]= {
207 { ARRAY, make_scanner_string("--NAMESPACE=foo"), make_scanner_string("foo"), __check_prefix_key },
208 { ARRAY, make_scanner_string("--NAMESPACE=\"foo\""), make_scanner_string("foo"), __check_prefix_key },
209 { ARRAY, make_scanner_string("--NAMESPACE=\"This_is_a_very_long_key\""), make_scanner_string("This_is_a_very_long_key"), __check_prefix_key },
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=FNV1A_64"), scanner_string_null, NULL },
226 { ARRAY, make_scanner_string("--HASH=FNV1_32"), scanner_string_null, NULL },
227 { ARRAY, make_scanner_string("--HASH=FNV1_64"), scanner_string_null, NULL },
228 { ARRAY, make_scanner_string("--HASH=JENKINS"), scanner_string_null, NULL },
229 { ARRAY, make_scanner_string("--HASH=MD5"), scanner_string_null, NULL },
230 { ARRAY, make_scanner_string("--HASH=MURMUR"), scanner_string_null, NULL },
231 { NIL, scanner_string_null, scanner_string_null, NULL}
232 };
233
234
235 static test_return_t _test_option(scanner_variable_t *scanner, bool test_true= true)
236 {
237 (void)test_true;
238
239 for (scanner_variable_t *ptr= scanner; ptr->type != NIL; ptr++)
240 {
241 memcached_st *memc;
242 memc= memcached(ptr->option.c_str, ptr->option.size);
243 if (test_true)
244 {
245 if (not memc)
246 {
247 char buffer[2048];
248 memcached_return_t rc= libmemcached_check_configuration(ptr->option.c_str, ptr->option.size, buffer, sizeof(buffer));
249 std::cerr << "About error for " << memcached_strerror(NULL, rc) << " : " << buffer << std::endl;
250 }
251
252 test_true(memc);
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 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
327
328 test_return_t memcached_create_with_options_with_filename(memcached_st*)
329 {
330 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
331 return TEST_SKIPPED;
332
333 memcached_st *memc_ptr;
334 memc_ptr= memcached(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""));
335 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
336 memcached_free(memc_ptr);
337
338 return TEST_SUCCESS;
339 }
340
341 test_return_t libmemcached_check_configuration_with_filename_test(memcached_st*)
342 {
343 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
344 return TEST_SKIPPED;
345
346 memcached_return_t rc;
347 char buffer[BUFSIZ];
348
349 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer));
350 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
351
352 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer));
353 test_false_with(rc == MEMCACHED_SUCCESS, buffer);
354
355 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer));
356 test_true_got(rc == MEMCACHED_ERRNO, buffer);
357
358 return TEST_SUCCESS;
359 }
360
361 test_return_t libmemcached_check_configuration_test(memcached_st*)
362 {
363 memcached_return_t rc;
364 char buffer[BUFSIZ];
365
366 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost"), buffer, sizeof(buffer));
367 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
368
369 rc= libmemcached_check_configuration(STRING_WITH_LEN("--dude=localhost"), buffer, sizeof(buffer));
370 test_false_with(rc == MEMCACHED_SUCCESS, buffer);
371 test_true(rc == MEMCACHED_PARSE_ERROR);
372
373 return TEST_SUCCESS;
374 }
375
376 test_return_t memcached_create_with_options_test(memcached_st*)
377 {
378 memcached_st *memc_ptr;
379 memc_ptr= memcached(STRING_WITH_LEN("--server=localhost"));
380 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
381 memcached_free(memc_ptr);
382
383 memc_ptr= memcached(STRING_WITH_LEN("--dude=localhost"));
384 test_false_with(memc_ptr, memcached_last_error_message(memc_ptr));
385
386 return TEST_SUCCESS;
387 }
388
389 test_return_t test_include_keyword(memcached_st*)
390 {
391 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
392 return TEST_SKIPPED;
393
394 char buffer[BUFSIZ];
395 memcached_return_t rc;
396 rc= libmemcached_check_configuration(STRING_WITH_LEN("INCLUDE \"support/example.cnf\""), buffer, sizeof(buffer));
397 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
398
399 return TEST_SUCCESS;
400 }
401
402 test_return_t test_end_keyword(memcached_st*)
403 {
404 char buffer[BUFSIZ];
405 memcached_return_t rc;
406 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost END bad keywords"), buffer, sizeof(buffer));
407 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
408
409 return TEST_SUCCESS;
410 }
411
412 test_return_t test_reset_keyword(memcached_st*)
413 {
414 char buffer[BUFSIZ];
415 memcached_return_t rc;
416 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost reset --server=bad.com"), buffer, sizeof(buffer));
417 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
418
419 return TEST_SUCCESS;
420 }
421
422 test_return_t test_error_keyword(memcached_st*)
423 {
424 char buffer[BUFSIZ];
425 memcached_return_t rc;
426 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost ERROR --server=bad.com"), buffer, sizeof(buffer));
427 test_true_got(rc != MEMCACHED_SUCCESS, buffer);
428
429 return TEST_SUCCESS;
430 }
431
432 #define RANDOM_STRINGS 100
433 test_return_t random_statement_build_test(memcached_st*)
434 {
435 std::vector<scanner_string_st *> option_list;
436
437 for (scanner_variable_t *ptr= test_server_strings; ptr->type != NIL; ptr++)
438 option_list.push_back(&ptr->option);
439
440 for (scanner_variable_t *ptr= test_number_options; ptr->type != NIL; ptr++)
441 option_list.push_back(&ptr->option);
442
443 for (scanner_variable_t *ptr= test_boolean_options; ptr->type != NIL; ptr++)
444 option_list.push_back(&ptr->option);
445
446 for (scanner_variable_t *ptr= prefix_key_strings; ptr->type != NIL; ptr++)
447 option_list.push_back(&ptr->option);
448
449 for (scanner_variable_t *ptr= distribution_strings; ptr->type != NIL; ptr++)
450 option_list.push_back(&ptr->option);
451
452 for (scanner_variable_t *ptr= hash_strings; ptr->type != NIL; ptr++)
453 option_list.push_back(&ptr->option);
454
455 for (uint32_t x= 0; x < RANDOM_STRINGS; x++)
456 {
457 std::string random_options;
458
459 uint32_t number_of= random() % option_list.size();
460 for (uint32_t options= 0; options < number_of; options++)
461 {
462 random_options+= option_list[random() % option_list.size()]->c_str;
463 random_options+= " ";
464 }
465
466 memcached_st *memc_ptr= memcached(random_options.c_str(), random_options.size() -1);
467 if (not memc_ptr)
468 {
469 switch (errno)
470 {
471 case EINVAL:
472 #if 0 // Testing framework is not smart enough for this just yet.
473 {
474 // We will try to find the specific error
475 char buffer[2048];
476 memcached_return_t rc= libmemcached_check_configuration(random_options.c_str(), random_options.size(), buffer, sizeof(buffer));
477 test_true_got(rc != MEMCACHED_SUCCESS, "memcached_create_with_options() failed whiled libmemcached_check_configuration() was successful");
478 std::cerr << "Error occured on " << random_options.c_str() << " : " << buffer << std::endl;
479 return TEST_FAILURE;
480 }
481 #endif
482 break;
483 case ENOMEM:
484 std::cerr << "Failed to allocate memory for memcached_create_with_options()" << std::endl;
485 memcached_free(memc_ptr);
486 return TEST_FAILURE;
487 default:
488 std::cerr << "Unknown error from memcached_create_with_options?!!" << std::endl;
489 memcached_free(memc_ptr);
490 return TEST_FAILURE;
491 }
492 }
493 memcached_free(memc_ptr);
494 }
495
496 return TEST_SUCCESS;
497 }