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