Merge in change such that we now use "config_" as the prepend on the
[awesomized/libmemcached] / tests / parser.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Gearmand client and server library.
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
44 #define BUILDING_LIBMEMCACHED
45 #include <libmemcached/memcached.h>
46
47 #include "tests/parser.h"
48 #include "tests/print.h"
49
50 enum scanner_type_t
51 {
52 NIL,
53 UNSIGNED,
54 SIGNED,
55 ARRAY
56 };
57
58
59 struct scanner_string_st {
60 const char *c_str;
61 size_t size;
62 };
63
64 static inline scanner_string_st scanner_string(const char *arg, size_t arg_size)
65 {
66 scanner_string_st local= { arg, arg_size };
67 return local;
68 }
69
70 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
71
72 static struct scanner_string_st scanner_string_null= { 0, 0};
73
74 struct scanner_variable_t {
75 enum scanner_type_t type;
76 struct scanner_string_st option;
77 struct scanner_string_st result;
78 test_return_t (*check_func)(memcached_st *memc, const scanner_string_st &hostname);
79 };
80
81 // Check and make sure the first host is what we expect it to be
82 static test_return_t __check_host(memcached_st *memc, const scanner_string_st &hostname)
83 {
84 memcached_server_instance_st instance=
85 memcached_server_instance_by_position(memc, 0);
86
87 test_true(instance);
88
89 const char *first_hostname = memcached_server_name(instance);
90 test_true(first_hostname);
91 test_strcmp(first_hostname, hostname.c_str);
92
93 return TEST_SUCCESS;
94 }
95
96 // Check and make sure the prefix_key is what we expect it to be
97 static test_return_t __check_prefix_key(memcached_st *memc, const scanner_string_st &hostname)
98 {
99 memcached_server_instance_st instance=
100 memcached_server_instance_by_position(memc, 0);
101
102 test_true(instance);
103
104 const char *first_hostname = memcached_server_name(instance);
105 test_true(first_hostname);
106 test_strcmp(first_hostname, hostname.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_AUTO_EJECT_HOSTS(memcached_st *memc, const scanner_string_st &value)
122 {
123 (void)value;
124 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS));
125 return TEST_SUCCESS;
126 }
127
128 static test_return_t __check_CACHE_LOOKUPS(memcached_st *memc, const scanner_string_st &value)
129 {
130 (void)value;
131 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS));
132 return TEST_SUCCESS;
133 }
134
135 static test_return_t __check_NOREPLY(memcached_st *memc, const scanner_string_st &value)
136 {
137 (void)value;
138 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY));
139 return TEST_SUCCESS;
140 }
141
142 static test_return_t __check_VERIFY_KEY(memcached_st *memc, const scanner_string_st &value)
143 {
144 (void)value;
145 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY));
146 return TEST_SUCCESS;
147 }
148
149 static test_return_t __check_distribution_RANDOM(memcached_st *memc, const scanner_string_st &value)
150 {
151 (void)value;
152 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION) == MEMCACHED_DISTRIBUTION_RANDOM);
153 return TEST_SUCCESS;
154 }
155
156 scanner_variable_t test_server_strings[]= {
157 { ARRAY, make_scanner_string("--server=localhost"), make_scanner_string("localhost"), __check_host },
158 { ARRAY, make_scanner_string("--server=10.0.2.1"), make_scanner_string("10.0.2.1"), __check_host },
159 { ARRAY, make_scanner_string("--server=example.com"), make_scanner_string("example.com"), __check_host },
160 { ARRAY, make_scanner_string("--server=localhost:30"), make_scanner_string("localhost"), __check_host },
161 { ARRAY, make_scanner_string("--server=10.0.2.1:20"), make_scanner_string("10.0.2.1"), __check_host },
162 { ARRAY, make_scanner_string("--server=example.com:1024"), make_scanner_string("example.com"), __check_host },
163 { NIL, scanner_string_null, scanner_string_null, NULL }
164 };
165
166 scanner_variable_t test_servers_strings[]= {
167 { ARRAY, make_scanner_string("--servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225"), scanner_string_null, NULL },
168 { ARRAY, make_scanner_string("--servers=a.example.com:81,localhost:82,b.example.com"), scanner_string_null, NULL },
169 { ARRAY, make_scanner_string("--servers=localhost,localhost:80"), scanner_string_null, NULL },
170 { NIL, scanner_string_null, scanner_string_null, NULL}
171 };
172
173
174 scanner_variable_t bad_test_strings[]= {
175 { ARRAY, make_scanner_string("-servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225"), scanner_string_null, NULL },
176 { ARRAY, make_scanner_string("-- servers=a.example.com:81,localhost:82,b.example.com"), scanner_string_null, NULL },
177 { ARRAY, make_scanner_string("--servers=localhost+80"), scanner_string_null, NULL},
178 { ARRAY, make_scanner_string("--servers=localhost.com."), scanner_string_null, NULL},
179 { ARRAY, make_scanner_string("--server=localhost.com."), scanner_string_null, NULL},
180 { ARRAY, make_scanner_string("--server=localhost.com.:80"), scanner_string_null, NULL},
181 { NIL, scanner_string_null, scanner_string_null, NULL}
182 };
183
184 scanner_variable_t test_number_options[]= {
185 { ARRAY, make_scanner_string("--CONNECT_TIMEOUT=456"), scanner_string_null, NULL },
186 { ARRAY, make_scanner_string("--IO_MSG_WATERMARK=456"), make_scanner_string("456"), __check_IO_MSG_WATERMARK },
187 { ARRAY, make_scanner_string("--IO_BYTES_WATERMARK=456"), scanner_string_null, NULL },
188 { ARRAY, make_scanner_string("--IO_KEY_PREFETCH=456"), scanner_string_null, NULL },
189 { ARRAY, make_scanner_string("--NUMBER_OF_REPLICAS=456"), scanner_string_null, NULL },
190 { ARRAY, make_scanner_string("--POLL_TIMEOUT=456"), scanner_string_null, NULL },
191 { ARRAY, make_scanner_string("--RCV_TIMEOUT=456"), scanner_string_null, NULL },
192 { ARRAY, make_scanner_string("--RETRY_TIMEOUT=456"), scanner_string_null, NULL },
193 { ARRAY, make_scanner_string("--SERVER_FAILURE_LIMIT=456"), scanner_string_null, NULL },
194 { ARRAY, make_scanner_string("--SND_TIMEOUT=456"), scanner_string_null, NULL },
195 { ARRAY, make_scanner_string("--SOCKET_RECV_SIZE=456"), scanner_string_null, NULL },
196 { ARRAY, make_scanner_string("--SOCKET_SEND_SIZE=456"), scanner_string_null, NULL },
197 { NIL, scanner_string_null, scanner_string_null, NULL}
198 };
199
200 scanner_variable_t test_boolean_options[]= {
201 { ARRAY, make_scanner_string("--AUTO_EJECT_HOSTS"), scanner_string_null, __check_AUTO_EJECT_HOSTS },
202 { ARRAY, make_scanner_string("--BINARY_PROTOCOL"), scanner_string_null, NULL },
203 { ARRAY, make_scanner_string("--BUFFER_REQUESTS"), scanner_string_null, NULL },
204 { ARRAY, make_scanner_string("--CACHE_LOOKUPS"), scanner_string_null, __check_CACHE_LOOKUPS },
205 #if 0 // Not all platforms support
206 { ARRAY, make_scanner_string("--CORK"), scanner_string_null, NULL },
207 #endif
208 { ARRAY, make_scanner_string("--HASH_WITH_PREFIX_KEY"), scanner_string_null, NULL },
209 { ARRAY, make_scanner_string("--KETAMA"), scanner_string_null, NULL },
210 { ARRAY, make_scanner_string("--KETAMA_WEIGHTED"), scanner_string_null, NULL },
211 { ARRAY, make_scanner_string("--NOREPLY"), scanner_string_null, __check_NOREPLY },
212 { ARRAY, make_scanner_string("--RANDOMIZE_REPLICA_READ"), scanner_string_null, NULL },
213 { ARRAY, make_scanner_string("--SORT_HOSTS"), scanner_string_null, NULL },
214 { ARRAY, make_scanner_string("--SUPPORT_CAS"), scanner_string_null, NULL },
215 { ARRAY, make_scanner_string("--TCP_NODELAY"), scanner_string_null, NULL },
216 { ARRAY, make_scanner_string("--TCP_KEEPALIVE"), scanner_string_null, NULL },
217 { ARRAY, make_scanner_string("--TCP_KEEPIDLE"), scanner_string_null, NULL },
218 { ARRAY, make_scanner_string("--USE_UDP"), scanner_string_null, NULL },
219 { ARRAY, make_scanner_string("--VERIFY_KEY"), scanner_string_null, __check_VERIFY_KEY },
220 { NIL, scanner_string_null, scanner_string_null, NULL}
221 };
222
223 scanner_variable_t prefix_key_strings[]= {
224 { ARRAY, make_scanner_string("--PREFIX_KEY=foo"), make_scanner_string("foo"), __check_prefix_key },
225 { ARRAY, make_scanner_string("--PREFIX-KEY=\"foo\""), make_scanner_string("foo"), __check_prefix_key },
226 { ARRAY, make_scanner_string("--PREFIX-KEY=\"This is a very long key\""), make_scanner_string("This is a very long key"), __check_prefix_key },
227 { NIL, scanner_string_null, scanner_string_null, NULL}
228 };
229
230 scanner_variable_t distribution_strings[]= {
231 { ARRAY, make_scanner_string("--DISTRIBUTION=consistent"), scanner_string_null, NULL },
232 { ARRAY, make_scanner_string("--DISTRIBUTION=random"), scanner_string_null, __check_distribution_RANDOM },
233 { ARRAY, make_scanner_string("--DISTRIBUTION=modula"), scanner_string_null, NULL },
234 { NIL, scanner_string_null, scanner_string_null, NULL}
235 };
236
237 scanner_variable_t hash_strings[]= {
238 { ARRAY, make_scanner_string("--HASH=CRC"), scanner_string_null, NULL },
239 { ARRAY, make_scanner_string("--HASH=FNV1A_32"), scanner_string_null, NULL },
240 { ARRAY, make_scanner_string("--HASH=FNV1A_64"), scanner_string_null, NULL },
241 { ARRAY, make_scanner_string("--HASH=FNV1_32"), scanner_string_null, NULL },
242 { ARRAY, make_scanner_string("--HASH=FNV1_64"), scanner_string_null, NULL },
243 { ARRAY, make_scanner_string("--HASH=JENKINS"), scanner_string_null, NULL },
244 { ARRAY, make_scanner_string("--HASH=MD5"), scanner_string_null, NULL },
245 { ARRAY, make_scanner_string("--HASH=MURMUR"), scanner_string_null, NULL },
246 { NIL, scanner_string_null, scanner_string_null, NULL}
247 };
248
249
250 static test_return_t _test_option(scanner_variable_t *scanner, bool test_true= true)
251 {
252 (void)test_true;
253 memcached_st *memc;
254 memc= memcached_create(NULL);
255
256 for (scanner_variable_t *ptr= scanner; ptr->type != NIL; ptr++)
257 {
258 memcached_return_t rc;
259 rc= memcached_parse_configuration(memc, ptr->option.c_str, ptr->option.size);
260 if (test_true)
261 {
262 if (rc != MEMCACHED_SUCCESS)
263 memcached_error_print(memc);
264
265 test_true(rc == MEMCACHED_SUCCESS);
266
267 if (ptr->check_func)
268 {
269 (*ptr->check_func)(memc, ptr->result);
270 }
271 }
272 else
273 {
274 test_false_with(rc == MEMCACHED_SUCCESS, ptr->option.c_str);
275 }
276 memcached_reset(memc);
277 }
278 memcached_free(memc);
279
280 return TEST_SUCCESS;
281 }
282
283 test_return_t server_test(memcached_st *junk)
284 {
285 (void)junk;
286 return _test_option(test_server_strings);
287 }
288
289 test_return_t servers_test(memcached_st *junk)
290 {
291 (void)junk;
292
293 test_return_t rc;
294 if ((rc= _test_option(test_server_strings)) != TEST_SUCCESS)
295 {
296 return rc;
297 }
298
299 #if 0
300 memcached_server_fn callbacks[1];
301 callbacks[0]= server_print_callback;
302 memcached_server_cursor(memc, callbacks, NULL, 1);
303 #endif
304
305 if ((rc= _test_option(bad_test_strings, false)) != TEST_SUCCESS)
306 {
307 return rc;
308 }
309
310 return TEST_SUCCESS;
311 }
312
313 test_return_t parser_number_options_test(memcached_st *junk)
314 {
315 (void)junk;
316 return _test_option(test_number_options);
317 }
318
319 test_return_t parser_boolean_options_test(memcached_st *junk)
320 {
321 (void)junk;
322 return _test_option(test_boolean_options);
323 }
324
325 test_return_t behavior_parser_test(memcached_st *junk)
326 {
327 (void)junk;
328 return TEST_SUCCESS;
329 }
330
331 test_return_t parser_hash_test(memcached_st *junk)
332 {
333 (void)junk;
334 return _test_option(hash_strings);
335 }
336
337 test_return_t parser_distribution_test(memcached_st *junk)
338 {
339 (void)junk;
340 return _test_option(distribution_strings);
341 }
342
343 test_return_t parser_key_prefix_test(memcached_st *junk)
344 {
345 (void)junk;
346 return _test_option(distribution_strings);
347 }
348
349 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
350
351 test_return_t memcached_parse_configure_file_test(memcached_st *junk)
352 {
353 (void)junk;
354
355 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
356 return TEST_SKIPPED;
357
358 memcached_st memc;
359 memcached_st *memc_ptr= memcached_create(&memc);
360
361 test_true(memc_ptr);
362
363 memcached_return_t rc= memcached_parse_configure_file(memc_ptr, memcached_string_with_size(SUPPORT_EXAMPLE_CNF));
364 test_true_got(rc == MEMCACHED_SUCCESS, memcached_last_error_message(memc_ptr) ? memcached_last_error_message(memc_ptr) : memcached_strerror(NULL, rc));
365 memcached_free(memc_ptr);
366
367 return TEST_SUCCESS;
368 }
369
370 test_return_t memcached_create_with_options_with_filename(memcached_st *junk)
371 {
372 (void)junk;
373 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
374 return TEST_SKIPPED;
375
376 memcached_st *memc_ptr;
377 memc_ptr= memcached_create_with_options(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""));
378 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
379 memcached_free(memc_ptr);
380
381 return TEST_SUCCESS;
382 }
383
384 test_return_t libmemcached_check_configuration_with_filename_test(memcached_st *junk)
385 {
386 (void)junk;
387
388 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
389 return TEST_SKIPPED;
390
391 memcached_return_t rc;
392 char buffer[BUFSIZ];
393
394 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer));
395 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
396
397 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer));
398 test_false_with(rc == MEMCACHED_SUCCESS, buffer);
399
400 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer));
401 test_true_got(rc == MEMCACHED_ERRNO, buffer);
402
403 return TEST_SUCCESS;
404 }
405
406 test_return_t libmemcached_check_configuration_test(memcached_st *junk)
407 {
408 (void)junk;
409
410 memcached_return_t rc;
411 char buffer[BUFSIZ];
412
413 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost"), buffer, sizeof(buffer));
414 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
415
416 rc= libmemcached_check_configuration(STRING_WITH_LEN("--dude=localhost"), buffer, sizeof(buffer));
417 test_false_with(rc == MEMCACHED_SUCCESS, buffer);
418 test_true(rc == MEMCACHED_PARSE_ERROR);
419
420 return TEST_SUCCESS;
421 }
422
423 test_return_t memcached_create_with_options_test(memcached_st *junk)
424 {
425 (void)junk;
426
427 memcached_st *memc_ptr;
428 memc_ptr= memcached_create_with_options(STRING_WITH_LEN("--server=localhost"));
429 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
430 memcached_free(memc_ptr);
431
432 memc_ptr= memcached_create_with_options(STRING_WITH_LEN("--dude=localhost"));
433 test_false_with(memc_ptr, memcached_last_error_message(memc_ptr));
434
435 return TEST_SUCCESS;
436 }
437
438 test_return_t test_include_keyword(memcached_st *junk)
439 {
440 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
441 return TEST_SKIPPED;
442
443 (void)junk;
444 char buffer[BUFSIZ];
445 memcached_return_t rc;
446 rc= libmemcached_check_configuration(STRING_WITH_LEN("INCLUDE \"support/example.cnf\""), buffer, sizeof(buffer));
447 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
448
449 return TEST_SUCCESS;
450 }
451
452 test_return_t test_end_keyword(memcached_st *junk)
453 {
454 (void)junk;
455 char buffer[BUFSIZ];
456 memcached_return_t rc;
457 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost END bad keywords"), buffer, sizeof(buffer));
458 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
459
460 return TEST_SUCCESS;
461 }
462
463 test_return_t test_reset_keyword(memcached_st *junk)
464 {
465 (void)junk;
466 char buffer[BUFSIZ];
467 memcached_return_t rc;
468 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost reset --server=bad.com"), buffer, sizeof(buffer));
469 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
470
471 return TEST_SUCCESS;
472 }
473
474 test_return_t test_error_keyword(memcached_st *junk)
475 {
476 (void)junk;
477 char buffer[BUFSIZ];
478 memcached_return_t rc;
479 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost ERROR --server=bad.com"), buffer, sizeof(buffer));
480 test_true_got(rc != MEMCACHED_SUCCESS, buffer);
481
482 return TEST_SUCCESS;
483 }
484
485 #define RANDOM_STRINGS 50
486 test_return_t random_statement_build_test(memcached_st *junk)
487 {
488 (void)junk;
489 std::vector<scanner_string_st *> option_list;
490
491 for (scanner_variable_t *ptr= test_server_strings; ptr->type != NIL; ptr++)
492 option_list.push_back(&ptr->option);
493
494 #if 0
495 for (scanner_variable_t *ptr= test_servers_strings; ptr->type != NIL; ptr++)
496 option_list.push_back(&ptr->option);
497 #endif
498
499 for (scanner_variable_t *ptr= test_number_options; ptr->type != NIL; ptr++)
500 option_list.push_back(&ptr->option);
501
502 for (scanner_variable_t *ptr= test_boolean_options; ptr->type != NIL; ptr++)
503 option_list.push_back(&ptr->option);
504
505 for (scanner_variable_t *ptr= prefix_key_strings; ptr->type != NIL; ptr++)
506 option_list.push_back(&ptr->option);
507
508 for (scanner_variable_t *ptr= distribution_strings; ptr->type != NIL; ptr++)
509 option_list.push_back(&ptr->option);
510
511 for (scanner_variable_t *ptr= hash_strings; ptr->type != NIL; ptr++)
512 option_list.push_back(&ptr->option);
513
514 for (uint32_t x= 0; x < RANDOM_STRINGS; x++)
515 {
516 std::string random_options;
517
518 uint32_t number_of= random() % option_list.size();
519 for (uint32_t options= 0; options < number_of; options++)
520 {
521 random_options+= option_list[random() % option_list.size()]->c_str;
522 random_options+= " ";
523 }
524 random_options.resize(random_options.size() -1);
525
526 memcached_return_t rc;
527 memcached_st *memc_ptr= memcached_create(NULL);
528 rc= memcached_parse_configuration(memc_ptr, random_options.c_str(), random_options.size());
529 if (rc == MEMCACHED_PARSE_ERROR)
530 {
531 std::cerr << std::endl << "Failed to parse(" << memcached_strerror(NULL, rc) << "): " << random_options << std::endl;
532 memcached_error_print(memc_ptr);
533 }
534 memcached_free(memc_ptr);
535 }
536
537 return TEST_SUCCESS;
538 }