1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Libmemcached Client and Server
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
44 #define BUILDING_LIBMEMCACHED
45 #include <libmemcached/memcached.h>
47 #include "tests/parser.h"
48 #include "tests/print.h"
59 struct scanner_string_st
{
64 static inline scanner_string_st
scanner_string(const char *arg
, size_t arg_size
)
66 scanner_string_st local
= { arg
, arg_size
};
70 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
72 static struct scanner_string_st scanner_string_null
= { 0, 0};
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
);
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
)
84 memcached_server_instance_st instance
=
85 memcached_server_instance_by_position(memc
, 0);
89 const char *first_hostname
= memcached_server_name(instance
);
90 test_true(first_hostname
);
91 test_strcmp(first_hostname
, hostname
.c_str
);
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
)
99 memcached_server_instance_st instance
=
100 memcached_server_instance_by_position(memc
, 0);
104 const char *first_hostname
= memcached_server_name(instance
);
105 test_true(first_hostname
);
106 test_strcmp(first_hostname
, hostname
.c_str
);
111 static test_return_t
__check_IO_MSG_WATERMARK(memcached_st
*memc
, const scanner_string_st
&value
)
113 uint64_t value_number
;
115 value_number
= atoll(value
.c_str
);
117 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == value_number
);
121 static test_return_t
__check_AUTO_EJECT_HOSTS(memcached_st
*memc
, const scanner_string_st
&value
)
124 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
));
128 static test_return_t
__check_NOREPLY(memcached_st
*memc
, const scanner_string_st
&value
)
131 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
));
135 static test_return_t
__check_VERIFY_KEY(memcached_st
*memc
, const scanner_string_st
&value
)
138 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
));
142 static test_return_t
__check_distribution_RANDOM(memcached_st
*memc
, const scanner_string_st
&value
)
145 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
) == MEMCACHED_DISTRIBUTION_RANDOM
);
149 scanner_variable_t test_server_strings
[]= {
150 { ARRAY
, make_scanner_string("--server=localhost"), make_scanner_string("localhost"), __check_host
},
151 { ARRAY
, make_scanner_string("--server=10.0.2.1"), make_scanner_string("10.0.2.1"), __check_host
},
152 { ARRAY
, make_scanner_string("--server=example.com"), make_scanner_string("example.com"), __check_host
},
153 { ARRAY
, make_scanner_string("--server=localhost:30"), make_scanner_string("localhost"), __check_host
},
154 { ARRAY
, make_scanner_string("--server=10.0.2.1:20"), make_scanner_string("10.0.2.1"), __check_host
},
155 { ARRAY
, make_scanner_string("--server=example.com:1024"), make_scanner_string("example.com"), __check_host
},
156 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
159 scanner_variable_t test_servers_strings
[]= {
160 { ARRAY
, make_scanner_string("--servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225"), scanner_string_null
, NULL
},
161 { ARRAY
, make_scanner_string("--servers=a.example.com:81,localhost:82,b.example.com"), scanner_string_null
, NULL
},
162 { ARRAY
, make_scanner_string("--servers=localhost,localhost:80"), scanner_string_null
, NULL
},
163 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
167 scanner_variable_t bad_test_strings
[]= {
168 { ARRAY
, make_scanner_string("-servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:11225"), scanner_string_null
, NULL
},
169 { ARRAY
, make_scanner_string("-- servers=a.example.com:81,localhost:82,b.example.com"), scanner_string_null
, NULL
},
170 { ARRAY
, make_scanner_string("--servers=localhost+80"), scanner_string_null
, NULL
},
171 { ARRAY
, make_scanner_string("--servers=localhost.com."), scanner_string_null
, NULL
},
172 { ARRAY
, make_scanner_string("--server=localhost.com."), scanner_string_null
, NULL
},
173 { ARRAY
, make_scanner_string("--server=localhost.com.:80"), scanner_string_null
, NULL
},
174 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
177 scanner_variable_t test_number_options
[]= {
178 { ARRAY
, make_scanner_string("--CONNECT_TIMEOUT=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("--IO_BYTES_WATERMARK=456"), scanner_string_null
, NULL
},
181 { ARRAY
, make_scanner_string("--IO_KEY_PREFETCH=456"), scanner_string_null
, NULL
},
182 { ARRAY
, make_scanner_string("--NUMBER_OF_REPLICAS=456"), scanner_string_null
, NULL
},
183 { ARRAY
, make_scanner_string("--POLL_TIMEOUT=456"), scanner_string_null
, NULL
},
184 { ARRAY
, make_scanner_string("--RCV_TIMEOUT=456"), scanner_string_null
, NULL
},
185 { ARRAY
, make_scanner_string("--RETRY_TIMEOUT=456"), scanner_string_null
, NULL
},
186 { ARRAY
, make_scanner_string("--SERVER_FAILURE_LIMIT=456"), scanner_string_null
, NULL
},
187 { ARRAY
, make_scanner_string("--SND_TIMEOUT=456"), scanner_string_null
, NULL
},
188 { ARRAY
, make_scanner_string("--SOCKET_RECV_SIZE=456"), scanner_string_null
, NULL
},
189 { ARRAY
, make_scanner_string("--SOCKET_SEND_SIZE=456"), scanner_string_null
, NULL
},
190 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
193 scanner_variable_t test_boolean_options
[]= {
194 { ARRAY
, make_scanner_string("--AUTO_EJECT_HOSTS"), scanner_string_null
, __check_AUTO_EJECT_HOSTS
},
195 { ARRAY
, make_scanner_string("--BINARY_PROTOCOL"), scanner_string_null
, NULL
},
196 { ARRAY
, make_scanner_string("--BUFFER_REQUESTS"), scanner_string_null
, NULL
},
197 { ARRAY
, make_scanner_string("--HASH_WITH_PREFIX_KEY"), scanner_string_null
, NULL
},
198 { ARRAY
, make_scanner_string("--KETAMA_WEIGHTED"), scanner_string_null
, NULL
},
199 { ARRAY
, make_scanner_string("--NOREPLY"), scanner_string_null
, __check_NOREPLY
},
200 { ARRAY
, make_scanner_string("--RANDOMIZE_REPLICA_READ"), scanner_string_null
, NULL
},
201 { ARRAY
, make_scanner_string("--SORT_HOSTS"), scanner_string_null
, NULL
},
202 { ARRAY
, make_scanner_string("--SUPPORT_CAS"), scanner_string_null
, NULL
},
203 { ARRAY
, make_scanner_string("--TCP_NODELAY"), scanner_string_null
, NULL
},
204 { ARRAY
, make_scanner_string("--TCP_KEEPALIVE"), scanner_string_null
, NULL
},
205 { ARRAY
, make_scanner_string("--TCP_KEEPIDLE"), scanner_string_null
, NULL
},
206 { ARRAY
, make_scanner_string("--USE_UDP"), scanner_string_null
, NULL
},
207 { ARRAY
, make_scanner_string("--VERIFY_KEY"), scanner_string_null
, __check_VERIFY_KEY
},
208 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
211 scanner_variable_t prefix_key_strings
[]= {
212 { ARRAY
, make_scanner_string("--PREFIX_KEY=foo"), make_scanner_string("foo"), __check_prefix_key
},
213 { ARRAY
, make_scanner_string("--PREFIX-KEY=\"foo\""), make_scanner_string("foo"), __check_prefix_key
},
214 { ARRAY
, make_scanner_string("--PREFIX-KEY=\"This is a very long key\""), make_scanner_string("This is a very long key"), __check_prefix_key
},
215 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
218 scanner_variable_t distribution_strings
[]= {
219 { ARRAY
, make_scanner_string("--DISTRIBUTION=consistent"), scanner_string_null
, NULL
},
220 { ARRAY
, make_scanner_string("--DISTRIBUTION=consistent,CRC"), scanner_string_null
, NULL
},
221 { ARRAY
, make_scanner_string("--DISTRIBUTION=consistent,MD5"), scanner_string_null
, NULL
},
222 { ARRAY
, make_scanner_string("--DISTRIBUTION=random"), scanner_string_null
, __check_distribution_RANDOM
},
223 { ARRAY
, make_scanner_string("--DISTRIBUTION=modula"), scanner_string_null
, NULL
},
224 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
227 scanner_variable_t hash_strings
[]= {
228 { ARRAY
, make_scanner_string("--HASH=CRC"), scanner_string_null
, NULL
},
229 { ARRAY
, make_scanner_string("--HASH=FNV1A_32"), scanner_string_null
, NULL
},
230 { ARRAY
, make_scanner_string("--HASH=FNV1A_64"), scanner_string_null
, NULL
},
231 { ARRAY
, make_scanner_string("--HASH=FNV1_32"), scanner_string_null
, NULL
},
232 { ARRAY
, make_scanner_string("--HASH=FNV1_64"), scanner_string_null
, NULL
},
233 { ARRAY
, make_scanner_string("--HASH=JENKINS"), scanner_string_null
, NULL
},
234 { ARRAY
, make_scanner_string("--HASH=MD5"), scanner_string_null
, NULL
},
235 { ARRAY
, make_scanner_string("--HASH=MURMUR"), scanner_string_null
, NULL
},
236 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
240 static test_return_t
_test_option(scanner_variable_t
*scanner
, bool test_true
= true)
244 memc
= memcached_create(NULL
);
246 for (scanner_variable_t
*ptr
= scanner
; ptr
->type
!= NIL
; ptr
++)
248 memcached_return_t rc
;
249 rc
= memcached_parse_configuration(memc
, ptr
->option
.c_str
, ptr
->option
.size
);
252 if (rc
!= MEMCACHED_SUCCESS
)
253 memcached_error_print(memc
);
255 test_true(rc
== MEMCACHED_SUCCESS
);
259 (*ptr
->check_func
)(memc
, ptr
->result
);
264 test_false_with(rc
== MEMCACHED_SUCCESS
, ptr
->option
.c_str
);
266 memcached_reset(memc
);
268 memcached_free(memc
);
273 test_return_t
server_test(memcached_st
*junk
)
276 return _test_option(test_server_strings
);
279 test_return_t
servers_test(memcached_st
*junk
)
284 if ((rc
= _test_option(test_server_strings
)) != TEST_SUCCESS
)
290 memcached_server_fn callbacks
[1];
291 callbacks
[0]= server_print_callback
;
292 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
295 if ((rc
= _test_option(bad_test_strings
, false)) != TEST_SUCCESS
)
303 test_return_t
parser_number_options_test(memcached_st
*junk
)
306 return _test_option(test_number_options
);
309 test_return_t
parser_boolean_options_test(memcached_st
*junk
)
312 return _test_option(test_boolean_options
);
315 test_return_t
behavior_parser_test(memcached_st
*junk
)
321 test_return_t
parser_hash_test(memcached_st
*junk
)
324 return _test_option(hash_strings
);
327 test_return_t
parser_distribution_test(memcached_st
*junk
)
330 return _test_option(distribution_strings
);
333 test_return_t
parser_key_prefix_test(memcached_st
*junk
)
336 return _test_option(distribution_strings
);
339 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
341 test_return_t
memcached_parse_configure_file_test(memcached_st
*junk
)
345 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
349 memcached_st
*memc_ptr
= memcached_create(&memc
);
353 memcached_return_t rc
= memcached_parse_configure_file(memc_ptr
, memcached_string_with_size(SUPPORT_EXAMPLE_CNF
));
354 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc_ptr
) ? memcached_last_error_message(memc_ptr
) : memcached_strerror(NULL
, rc
));
355 memcached_free(memc_ptr
);
360 test_return_t
memcached_create_with_options_with_filename(memcached_st
*junk
)
363 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
366 memcached_st
*memc_ptr
;
367 memc_ptr
= memcached_create_with_options(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""));
368 test_true_got(memc_ptr
, memcached_last_error_message(memc_ptr
));
369 memcached_free(memc_ptr
);
374 test_return_t
libmemcached_check_configuration_with_filename_test(memcached_st
*junk
)
378 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
381 memcached_return_t rc
;
384 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer
, sizeof(buffer
));
385 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
387 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer
, sizeof(buffer
));
388 test_false_with(rc
== MEMCACHED_SUCCESS
, buffer
);
390 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer
, sizeof(buffer
));
391 test_true_got(rc
== MEMCACHED_ERRNO
, buffer
);
396 test_return_t
libmemcached_check_configuration_test(memcached_st
*junk
)
400 memcached_return_t rc
;
403 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost"), buffer
, sizeof(buffer
));
404 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
406 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--dude=localhost"), buffer
, sizeof(buffer
));
407 test_false_with(rc
== MEMCACHED_SUCCESS
, buffer
);
408 test_true(rc
== MEMCACHED_PARSE_ERROR
);
413 test_return_t
memcached_create_with_options_test(memcached_st
*junk
)
417 memcached_st
*memc_ptr
;
418 memc_ptr
= memcached_create_with_options(STRING_WITH_LEN("--server=localhost"));
419 test_true_got(memc_ptr
, memcached_last_error_message(memc_ptr
));
420 memcached_free(memc_ptr
);
422 memc_ptr
= memcached_create_with_options(STRING_WITH_LEN("--dude=localhost"));
423 test_false_with(memc_ptr
, memcached_last_error_message(memc_ptr
));
428 test_return_t
test_include_keyword(memcached_st
*junk
)
430 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
435 memcached_return_t rc
;
436 rc
= libmemcached_check_configuration(STRING_WITH_LEN("INCLUDE \"support/example.cnf\""), buffer
, sizeof(buffer
));
437 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
442 test_return_t
test_end_keyword(memcached_st
*junk
)
446 memcached_return_t rc
;
447 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost END bad keywords"), buffer
, sizeof(buffer
));
448 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
453 test_return_t
test_reset_keyword(memcached_st
*junk
)
457 memcached_return_t rc
;
458 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost reset --server=bad.com"), buffer
, sizeof(buffer
));
459 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
464 test_return_t
test_error_keyword(memcached_st
*junk
)
468 memcached_return_t rc
;
469 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost ERROR --server=bad.com"), buffer
, sizeof(buffer
));
470 test_true_got(rc
!= MEMCACHED_SUCCESS
, buffer
);
475 #define RANDOM_STRINGS 50
476 test_return_t
random_statement_build_test(memcached_st
*junk
)
479 std::vector
<scanner_string_st
*> option_list
;
481 for (scanner_variable_t
*ptr
= test_server_strings
; ptr
->type
!= NIL
; ptr
++)
482 option_list
.push_back(&ptr
->option
);
485 for (scanner_variable_t
*ptr
= test_servers_strings
; ptr
->type
!= NIL
; ptr
++)
486 option_list
.push_back(&ptr
->option
);
489 for (scanner_variable_t
*ptr
= test_number_options
; ptr
->type
!= NIL
; ptr
++)
490 option_list
.push_back(&ptr
->option
);
492 for (scanner_variable_t
*ptr
= test_boolean_options
; ptr
->type
!= NIL
; ptr
++)
493 option_list
.push_back(&ptr
->option
);
495 for (scanner_variable_t
*ptr
= prefix_key_strings
; ptr
->type
!= NIL
; ptr
++)
496 option_list
.push_back(&ptr
->option
);
498 for (scanner_variable_t
*ptr
= distribution_strings
; ptr
->type
!= NIL
; ptr
++)
499 option_list
.push_back(&ptr
->option
);
501 for (scanner_variable_t
*ptr
= hash_strings
; ptr
->type
!= NIL
; ptr
++)
502 option_list
.push_back(&ptr
->option
);
504 for (uint32_t x
= 0; x
< RANDOM_STRINGS
; x
++)
506 std::string random_options
;
508 uint32_t number_of
= random() % option_list
.size();
509 for (uint32_t options
= 0; options
< number_of
; options
++)
511 random_options
+= option_list
[random() % option_list
.size()]->c_str
;
512 random_options
+= " ";
515 memcached_return_t rc
;
516 memcached_st
*memc_ptr
= memcached_create(NULL
);
517 rc
= memcached_parse_configuration(memc_ptr
, random_options
.c_str(), random_options
.size() -1);
518 if (rc
== MEMCACHED_PARSE_ERROR
)
520 std::cerr
<< std::endl
<< "Failed to parse(" << memcached_strerror(NULL
, rc
) << "): " << random_options
<< std::endl
;
521 memcached_error_print(memc_ptr
);
523 memcached_free(memc_ptr
);