Merge in local trunk
[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 // !NEVER use common.h, always use memcached.h in your own apps
47 #include <libmemcached/common.h>
48
49 #include "tests/parser.h"
50 #include "tests/print.h"
51
52 enum scanner_type_t
53 {
54 NIL,
55 UNSIGNED,
56 SIGNED,
57 ARRAY
58 };
59
60
61 struct scanner_string_st {
62 const char *c_str;
63 size_t size;
64 };
65
66 static inline scanner_string_st scanner_string(const char *arg, size_t arg_size)
67 {
68 scanner_string_st local= { arg, arg_size };
69 return local;
70 }
71
72 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
73
74 static struct scanner_string_st scanner_string_null= { 0, 0};
75
76 struct scanner_variable_t {
77 enum scanner_type_t type;
78 struct scanner_string_st option;
79 struct scanner_string_st result;
80 test_return_t (*check_func)(memcached_st *memc, const scanner_string_st &hostname);
81 };
82
83 // Check and make sure the first host is what we expect it to be
84 static test_return_t __check_host(memcached_st *memc, const scanner_string_st &hostname)
85 {
86 memcached_server_instance_st instance=
87 memcached_server_instance_by_position(memc, 0);
88
89 test_true(instance);
90
91 const char *first_hostname = memcached_server_name(instance);
92 test_true(first_hostname);
93 test_strcmp(first_hostname, hostname.c_str);
94
95 return TEST_SUCCESS;
96 }
97
98 // Check and make sure the prefix_key is what we expect it to be
99 static test_return_t __check_prefix_key(memcached_st *memc, const scanner_string_st &hostname)
100 {
101 memcached_server_instance_st instance=
102 memcached_server_instance_by_position(memc, 0);
103
104 test_true(instance);
105
106 const char *first_hostname = memcached_server_name(instance);
107 test_true(first_hostname);
108 test_strcmp(first_hostname, hostname.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 prefix_key_strings[]= {
208 { ARRAY, make_scanner_string("--NAMESPACE=foo"), make_scanner_string("foo"), __check_prefix_key },
209 { ARRAY, make_scanner_string("--NAMESPACE=\"foo\""), make_scanner_string("foo"), __check_prefix_key },
210 { ARRAY, make_scanner_string("--NAMESPACE=\"This_is_a_very_long_key\""), make_scanner_string("This_is_a_very_long_key"), __check_prefix_key },
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= true)
237 {
238 (void)test_true;
239
240 for (scanner_variable_t *ptr= scanner; ptr->type != NIL; ptr++)
241 {
242 memcached_st *memc;
243 memc= memcached(ptr->option.c_str, ptr->option.size);
244 if (test_true)
245 {
246 if (not memc)
247 {
248 char buffer[2048];
249 memcached_return_t rc= libmemcached_check_configuration(ptr->option.c_str, ptr->option.size, buffer, sizeof(buffer));
250 std::cerr << "About error for " << memcached_strerror(NULL, rc) << " : " << buffer << std::endl;
251 }
252
253 test_true(memc);
254
255 if (ptr->check_func)
256 {
257 test_return_t test_rc= (*ptr->check_func)(memc, ptr->result);
258 if (test_rc != TEST_SUCCESS)
259 {
260 memcached_free(memc);
261 return test_rc;
262 }
263 }
264
265 memcached_free(memc);
266 }
267 else
268 {
269 test_false_with(memc, ptr->option.c_str);
270 }
271 }
272
273 return TEST_SUCCESS;
274 }
275
276 test_return_t server_test(memcached_st *)
277 {
278 return _test_option(test_server_strings);
279 }
280
281 test_return_t server_with_weight_test(memcached_st *)
282 {
283 return _test_option(test_server_strings_with_weights);
284 }
285
286 test_return_t servers_bad_test(memcached_st *)
287 {
288 test_return_t rc;
289 if ((rc= _test_option(bad_test_strings, false)) != TEST_SUCCESS)
290 {
291 return rc;
292 }
293
294 return TEST_SUCCESS;
295 }
296
297 test_return_t parser_number_options_test(memcached_st*)
298 {
299 return _test_option(test_number_options);
300 }
301
302 test_return_t parser_boolean_options_test(memcached_st*)
303 {
304 return _test_option(test_boolean_options);
305 }
306
307 test_return_t behavior_parser_test(memcached_st*)
308 {
309 return TEST_SUCCESS;
310 }
311
312 test_return_t parser_hash_test(memcached_st*)
313 {
314 return _test_option(hash_strings);
315 }
316
317 test_return_t parser_distribution_test(memcached_st*)
318 {
319 return _test_option(distribution_strings);
320 }
321
322 test_return_t parser_key_prefix_test(memcached_st*)
323 {
324 return _test_option(distribution_strings);
325 }
326
327 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
328
329 test_return_t memcached_create_with_options_with_filename(memcached_st*)
330 {
331 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
332 return TEST_SKIPPED;
333
334 memcached_st *memc_ptr;
335 memc_ptr= memcached(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""));
336 test_true_got(memc_ptr, "memcached() failed");
337 test_strcmp(SUPPORT_EXAMPLE_CNF, memcached_array_string(memc_ptr->configure.filename));
338 memcached_free(memc_ptr);
339
340 return TEST_SUCCESS;
341 }
342
343 test_return_t libmemcached_check_configuration_with_filename_test(memcached_st*)
344 {
345 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
346 return TEST_SKIPPED;
347
348 memcached_return_t rc;
349 char buffer[BUFSIZ];
350
351 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer));
352 test_true_got(rc == MEMCACHED_SUCCESS, (rc == MEMCACHED_ERRNO) ? strerror(errno) : memcached_strerror(NULL, rc));
353
354 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer));
355 test_false_with(rc == MEMCACHED_SUCCESS, memcached_strerror(NULL, rc));
356
357 rc= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer));
358 test_true_got(rc == MEMCACHED_ERRNO, memcached_strerror(NULL, rc));
359
360 return TEST_SUCCESS;
361 }
362
363 test_return_t libmemcached_check_configuration_test(memcached_st*)
364 {
365 memcached_return_t rc;
366 char buffer[BUFSIZ];
367
368 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost"), buffer, sizeof(buffer));
369 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
370
371 rc= libmemcached_check_configuration(STRING_WITH_LEN("--dude=localhost"), buffer, sizeof(buffer));
372 test_false_with(rc == MEMCACHED_SUCCESS, buffer);
373 test_true(rc == MEMCACHED_PARSE_ERROR);
374
375 return TEST_SUCCESS;
376 }
377
378 test_return_t memcached_create_with_options_test(memcached_st*)
379 {
380 memcached_st *memc_ptr;
381 memc_ptr= memcached(STRING_WITH_LEN("--server=localhost"));
382 test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
383 memcached_free(memc_ptr);
384
385 memc_ptr= memcached(STRING_WITH_LEN("--dude=localhost"));
386 test_false_with(memc_ptr, memcached_last_error_message(memc_ptr));
387
388 return TEST_SUCCESS;
389 }
390
391 test_return_t test_include_keyword(memcached_st*)
392 {
393 if (access(SUPPORT_EXAMPLE_CNF, R_OK))
394 return TEST_SKIPPED;
395
396 char buffer[BUFSIZ];
397 memcached_return_t rc;
398 rc= libmemcached_check_configuration(STRING_WITH_LEN("INCLUDE \"support/example.cnf\""), buffer, sizeof(buffer));
399 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
400
401 return TEST_SUCCESS;
402 }
403
404 test_return_t test_end_keyword(memcached_st*)
405 {
406 char buffer[BUFSIZ];
407 memcached_return_t rc;
408 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost END bad keywords"), buffer, sizeof(buffer));
409 test_true_got(rc == MEMCACHED_SUCCESS, buffer);
410
411 return TEST_SUCCESS;
412 }
413
414 test_return_t test_reset_keyword(memcached_st*)
415 {
416 char buffer[BUFSIZ];
417 memcached_return_t rc;
418 rc= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost reset --server=bad.com"), buffer, sizeof(buffer));
419 test_true_got(rc == MEMCACHED_SUCCESS, 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(STRING_WITH_LEN("--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= prefix_key_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 }