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