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.
46 #define BUILDING_LIBMEMCACHED
47 // !NEVER use common.h, always use memcached.h in your own apps
48 #include <libmemcached/common.h>
50 #include "tests/parser.h"
51 #include "tests/print.h"
62 struct scanner_string_st
{
67 static inline scanner_string_st
scanner_string(const char *arg
, size_t arg_size
)
69 scanner_string_st local
= { arg
, arg_size
};
73 #define make_scanner_string(X) scanner_string((X), static_cast<size_t>(sizeof(X) - 1))
75 static struct scanner_string_st scanner_string_null
= { 0, 0};
77 struct scanner_variable_t
{
78 enum scanner_type_t type
;
79 struct scanner_string_st option
;
80 struct scanner_string_st result
;
81 test_return_t (*check_func
)(memcached_st
*memc
, const scanner_string_st
&hostname
);
84 // Check and make sure the first host is what we expect it to be
85 static test_return_t
__check_host(memcached_st
*memc
, const scanner_string_st
&hostname
)
87 memcached_server_instance_st instance
=
88 memcached_server_instance_by_position(memc
, 0);
92 const char *first_hostname
= memcached_server_name(instance
);
93 test_true(first_hostname
);
94 test_strcmp(first_hostname
, hostname
.c_str
);
99 // Check and make sure the prefix_key is what we expect it to be
100 static test_return_t
__check_prefix_key(memcached_st
*memc
, const scanner_string_st
&hostname
)
102 memcached_server_instance_st instance
=
103 memcached_server_instance_by_position(memc
, 0);
107 const char *first_hostname
= memcached_server_name(instance
);
108 test_true(first_hostname
);
109 test_strcmp(first_hostname
, hostname
.c_str
);
114 static test_return_t
__check_IO_MSG_WATERMARK(memcached_st
*memc
, const scanner_string_st
&value
)
116 uint64_t value_number
;
118 value_number
= atoll(value
.c_str
);
120 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == value_number
);
124 static test_return_t
__check_REMOVE_FAILED_SERVERS(memcached_st
*memc
, const scanner_string_st
&)
126 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS
));
130 static test_return_t
__check_NOREPLY(memcached_st
*memc
, const scanner_string_st
&)
132 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
));
136 static test_return_t
__check_VERIFY_KEY(memcached_st
*memc
, const scanner_string_st
&)
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
&)
144 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
) == MEMCACHED_DISTRIBUTION_RANDOM
);
148 scanner_variable_t test_server_strings
[]= {
149 { ARRAY
, make_scanner_string("--server=localhost"), make_scanner_string("localhost"), __check_host
},
150 { ARRAY
, make_scanner_string("--server=10.0.2.1"), make_scanner_string("10.0.2.1"), __check_host
},
151 { ARRAY
, make_scanner_string("--server=example.com"), make_scanner_string("example.com"), __check_host
},
152 { ARRAY
, make_scanner_string("--server=localhost:30"), make_scanner_string("localhost"), __check_host
},
153 { ARRAY
, make_scanner_string("--server=10.0.2.1:20"), make_scanner_string("10.0.2.1"), __check_host
},
154 { ARRAY
, make_scanner_string("--server=example.com:1024"), make_scanner_string("example.com"), __check_host
},
155 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
158 scanner_variable_t test_server_strings_with_weights
[]= {
159 { ARRAY
, make_scanner_string("--server=10.0.2.1:30/?40"), make_scanner_string("10.0.2.1"), __check_host
},
160 { ARRAY
, make_scanner_string("--server=example.com:1024/?30"), make_scanner_string("example.com"), __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/?10"), make_scanner_string("example.com"), __check_host
},
163 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
166 scanner_variable_t bad_test_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:+80"), scanner_string_null
, NULL
},
170 { ARRAY
, make_scanner_string("--servers=localhost.com."), scanner_string_null
, NULL
},
171 { ARRAY
, make_scanner_string("--server=localhost.com."), scanner_string_null
, NULL
},
172 { ARRAY
, make_scanner_string("--server=localhost.com.:80"), scanner_string_null
, NULL
},
173 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
176 scanner_variable_t test_number_options
[]= {
177 { ARRAY
, make_scanner_string("--CONNECT-TIMEOUT=456"), scanner_string_null
, NULL
},
178 { ARRAY
, make_scanner_string("--IO-BYTES-WATERMARK=456"), scanner_string_null
, NULL
},
179 { ARRAY
, make_scanner_string("--IO-KEY-PREFETCH=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("--NUMBER-OF-REPLICAS=456"), scanner_string_null
, NULL
},
182 { ARRAY
, make_scanner_string("--POLL-TIMEOUT=456"), scanner_string_null
, NULL
},
183 { ARRAY
, make_scanner_string("--RCV-TIMEOUT=456"), scanner_string_null
, NULL
},
184 { ARRAY
, make_scanner_string("--REMOVE-FAILED-SERVERS=3"), scanner_string_null
, __check_REMOVE_FAILED_SERVERS
},
185 { ARRAY
, make_scanner_string("--RETRY-TIMEOUT=456"), scanner_string_null
, NULL
},
186 { ARRAY
, make_scanner_string("--SND-TIMEOUT=456"), scanner_string_null
, NULL
},
187 { ARRAY
, make_scanner_string("--SOCKET-RECV-SIZE=456"), scanner_string_null
, NULL
},
188 { ARRAY
, make_scanner_string("--SOCKET-SEND-SIZE=456"), scanner_string_null
, NULL
},
189 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
192 scanner_variable_t test_boolean_options
[]= {
193 { ARRAY
, make_scanner_string("--BINARY-PROTOCOL"), scanner_string_null
, NULL
},
194 { ARRAY
, make_scanner_string("--BUFFER-REQUESTS"), scanner_string_null
, NULL
},
195 { ARRAY
, make_scanner_string("--HASH-WITH-NAMESPACE"), scanner_string_null
, NULL
},
196 { ARRAY
, make_scanner_string("--NOREPLY"), scanner_string_null
, __check_NOREPLY
},
197 { ARRAY
, make_scanner_string("--RANDOMIZE-REPLICA-READ"), scanner_string_null
, NULL
},
198 { ARRAY
, make_scanner_string("--SORT-HOSTS"), scanner_string_null
, NULL
},
199 { ARRAY
, make_scanner_string("--SUPPORT-CAS"), scanner_string_null
, NULL
},
200 { ARRAY
, make_scanner_string("--TCP-NODELAY"), scanner_string_null
, NULL
},
201 { ARRAY
, make_scanner_string("--TCP-KEEPALIVE"), scanner_string_null
, NULL
},
202 { ARRAY
, make_scanner_string("--TCP-KEEPIDLE"), scanner_string_null
, NULL
},
203 { ARRAY
, make_scanner_string("--USE-UDP"), scanner_string_null
, NULL
},
204 { ARRAY
, make_scanner_string("--VERIFY-KEY"), scanner_string_null
, __check_VERIFY_KEY
},
205 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
208 scanner_variable_t prefix_key_strings
[]= {
209 { ARRAY
, make_scanner_string("--NAMESPACE=foo"), make_scanner_string("foo"), __check_prefix_key
},
210 { ARRAY
, make_scanner_string("--NAMESPACE=\"foo\""), make_scanner_string("foo"), __check_prefix_key
},
211 { ARRAY
, make_scanner_string("--NAMESPACE=\"This_is_a_very_long_key\""), make_scanner_string("This_is_a_very_long_key"), __check_prefix_key
},
212 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
215 scanner_variable_t distribution_strings
[]= {
216 { ARRAY
, make_scanner_string("--DISTRIBUTION=consistent"), scanner_string_null
, NULL
},
217 { ARRAY
, make_scanner_string("--DISTRIBUTION=consistent,CRC"), scanner_string_null
, NULL
},
218 { ARRAY
, make_scanner_string("--DISTRIBUTION=consistent,MD5"), scanner_string_null
, NULL
},
219 { ARRAY
, make_scanner_string("--DISTRIBUTION=random"), scanner_string_null
, __check_distribution_RANDOM
},
220 { ARRAY
, make_scanner_string("--DISTRIBUTION=modula"), scanner_string_null
, NULL
},
221 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
224 scanner_variable_t hash_strings
[]= {
225 { ARRAY
, make_scanner_string("--HASH=CRC"), scanner_string_null
, NULL
},
226 { ARRAY
, make_scanner_string("--HASH=FNV1A_32"), scanner_string_null
, NULL
},
227 { ARRAY
, make_scanner_string("--HASH=FNV1A_64"), scanner_string_null
, NULL
},
228 { ARRAY
, make_scanner_string("--HASH=FNV1_32"), scanner_string_null
, NULL
},
229 { ARRAY
, make_scanner_string("--HASH=FNV1_64"), scanner_string_null
, NULL
},
230 { ARRAY
, make_scanner_string("--HASH=JENKINS"), scanner_string_null
, NULL
},
231 { ARRAY
, make_scanner_string("--HASH=MD5"), scanner_string_null
, NULL
},
232 { ARRAY
, make_scanner_string("--HASH=MURMUR"), scanner_string_null
, NULL
},
233 { NIL
, scanner_string_null
, scanner_string_null
, NULL
}
237 static test_return_t
_test_option(scanner_variable_t
*scanner
, bool test_true
= true)
241 for (scanner_variable_t
*ptr
= scanner
; ptr
->type
!= NIL
; ptr
++)
244 memc
= memcached(ptr
->option
.c_str
, ptr
->option
.size
);
250 memcached_return_t rc
= libmemcached_check_configuration(ptr
->option
.c_str
, ptr
->option
.size
, buffer
, sizeof(buffer
));
251 std::cerr
<< "About error for " << memcached_strerror(NULL
, rc
) << " : " << buffer
<< std::endl
;
258 test_return_t test_rc
= (*ptr
->check_func
)(memc
, ptr
->result
);
259 if (test_rc
!= TEST_SUCCESS
)
261 memcached_free(memc
);
266 memcached_free(memc
);
270 test_false_with(memc
, ptr
->option
.c_str
);
277 test_return_t
server_test(memcached_st
*)
279 return _test_option(test_server_strings
);
282 test_return_t
server_with_weight_test(memcached_st
*)
284 return _test_option(test_server_strings_with_weights
);
287 test_return_t
servers_bad_test(memcached_st
*)
290 if ((rc
= _test_option(bad_test_strings
, false)) != TEST_SUCCESS
)
298 test_return_t
parser_number_options_test(memcached_st
*)
300 return _test_option(test_number_options
);
303 test_return_t
parser_boolean_options_test(memcached_st
*)
305 return _test_option(test_boolean_options
);
308 test_return_t
behavior_parser_test(memcached_st
*)
313 test_return_t
parser_hash_test(memcached_st
*)
315 return _test_option(hash_strings
);
318 test_return_t
parser_distribution_test(memcached_st
*)
320 return _test_option(distribution_strings
);
323 test_return_t
parser_key_prefix_test(memcached_st
*)
325 return _test_option(distribution_strings
);
328 #define SUPPORT_EXAMPLE_CNF "support/example.cnf"
330 test_return_t
memcached_create_with_options_with_filename(memcached_st
*)
332 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
335 memcached_st
*memc_ptr
;
336 memc_ptr
= memcached(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""));
337 test_true_got(memc_ptr
, "memcached() failed");
338 test_strcmp(SUPPORT_EXAMPLE_CNF
, memcached_array_string(memc_ptr
->configure
.filename
));
339 memcached_free(memc_ptr
);
344 test_return_t
libmemcached_check_configuration_with_filename_test(memcached_st
*)
346 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
349 memcached_return_t rc
;
352 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer
, sizeof(buffer
));
353 test_true_got(rc
== MEMCACHED_SUCCESS
, (rc
== MEMCACHED_ERRNO
) ? strerror(errno
) : memcached_strerror(NULL
, rc
));
355 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer
, sizeof(buffer
));
356 test_false_with(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
358 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer
, sizeof(buffer
));
359 test_true_got(rc
== MEMCACHED_ERRNO
, memcached_strerror(NULL
, rc
));
364 test_return_t
libmemcached_check_configuration_test(memcached_st
*)
366 memcached_return_t rc
;
369 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost"), buffer
, sizeof(buffer
));
370 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
372 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--dude=localhost"), buffer
, sizeof(buffer
));
373 test_false_with(rc
== MEMCACHED_SUCCESS
, buffer
);
374 test_true(rc
== MEMCACHED_PARSE_ERROR
);
379 test_return_t
memcached_create_with_options_test(memcached_st
*)
381 memcached_st
*memc_ptr
;
382 memc_ptr
= memcached(STRING_WITH_LEN("--server=localhost"));
383 test_true_got(memc_ptr
, memcached_last_error_message(memc_ptr
));
384 memcached_free(memc_ptr
);
386 memc_ptr
= memcached(STRING_WITH_LEN("--dude=localhost"));
387 test_false_with(memc_ptr
, memcached_last_error_message(memc_ptr
));
392 test_return_t
test_include_keyword(memcached_st
*)
394 if (access(SUPPORT_EXAMPLE_CNF
, R_OK
))
398 memcached_return_t rc
;
399 rc
= libmemcached_check_configuration(STRING_WITH_LEN("INCLUDE \"support/example.cnf\""), buffer
, sizeof(buffer
));
400 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
405 test_return_t
test_end_keyword(memcached_st
*)
408 memcached_return_t rc
;
409 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost END bad keywords"), buffer
, sizeof(buffer
));
410 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
415 test_return_t
test_reset_keyword(memcached_st
*)
418 memcached_return_t rc
;
419 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost reset --server=bad.com"), buffer
, sizeof(buffer
));
420 test_true_got(rc
== MEMCACHED_SUCCESS
, buffer
);
425 test_return_t
test_error_keyword(memcached_st
*)
428 memcached_return_t rc
;
429 rc
= libmemcached_check_configuration(STRING_WITH_LEN("--server=localhost ERROR --server=bad.com"), buffer
, sizeof(buffer
));
430 test_true_got(rc
!= MEMCACHED_SUCCESS
, buffer
);
435 #define RANDOM_STRINGS 100
436 test_return_t
random_statement_build_test(memcached_st
*)
438 std::vector
<scanner_string_st
*> option_list
;
440 for (scanner_variable_t
*ptr
= test_server_strings
; ptr
->type
!= NIL
; ptr
++)
441 option_list
.push_back(&ptr
->option
);
443 for (scanner_variable_t
*ptr
= test_number_options
; ptr
->type
!= NIL
; ptr
++)
444 option_list
.push_back(&ptr
->option
);
446 for (scanner_variable_t
*ptr
= test_boolean_options
; ptr
->type
!= NIL
; ptr
++)
447 option_list
.push_back(&ptr
->option
);
449 for (scanner_variable_t
*ptr
= prefix_key_strings
; ptr
->type
!= NIL
; ptr
++)
450 option_list
.push_back(&ptr
->option
);
452 for (scanner_variable_t
*ptr
= distribution_strings
; ptr
->type
!= NIL
; ptr
++)
453 option_list
.push_back(&ptr
->option
);
455 for (scanner_variable_t
*ptr
= hash_strings
; ptr
->type
!= NIL
; ptr
++)
456 option_list
.push_back(&ptr
->option
);
458 for (uint32_t x
= 0; x
< RANDOM_STRINGS
; x
++)
460 std::string random_options
;
462 uint32_t number_of
= random() % option_list
.size();
463 for (uint32_t options
= 0; options
< number_of
; options
++)
465 random_options
+= option_list
[random() % option_list
.size()]->c_str
;
466 random_options
+= " ";
469 memcached_st
*memc_ptr
= memcached(random_options
.c_str(), random_options
.size() -1);
475 #if 0 // Testing framework is not smart enough for this just yet.
477 // We will try to find the specific error
479 memcached_return_t rc
= libmemcached_check_configuration(random_options
.c_str(), random_options
.size(), buffer
, sizeof(buffer
));
480 test_true_got(rc
!= MEMCACHED_SUCCESS
, "memcached_create_with_options() failed whiled libmemcached_check_configuration() was successful");
481 std::cerr
<< "Error occured on " << random_options
.c_str() << " : " << buffer
<< std::endl
;
487 std::cerr
<< "Failed to allocate memory for memcached_create_with_options()" << std::endl
;
488 memcached_free(memc_ptr
);
491 std::cerr
<< "Unknown error from memcached_create_with_options?!!" << std::endl
;
492 memcached_free(memc_ptr
);
496 memcached_free(memc_ptr
);
502 static memcached_return_t
dump_server_information(const memcached_st
*,
503 const memcached_server_st
*instance
,
506 if (strcmp(memcached_server_name(instance
), "localhost"))
508 assert(not memcached_server_name(instance
));
509 return MEMCACHED_FAILURE
;
512 if (memcached_server_port(instance
) < 8888 or memcached_server_port(instance
) > 8892)
514 assert(not memcached_server_port(instance
));
515 return MEMCACHED_FAILURE
;
518 if (instance
->weight
> 5 or instance
->weight
< 2)
520 assert(not instance
->weight
);
521 return MEMCACHED_FAILURE
;
524 return MEMCACHED_SUCCESS
;
528 test_return_t
test_hostname_port_weight(memcached_st
*)
530 const char *server_string
= "--server=localhost:8888/?2 --server=localhost:8889/?3 --server=localhost:8890/?4 --server=localhost:8891/?5 --server=localhost:8892/?3";
533 memcached_return_t rc
;
534 test_compare_got(MEMCACHED_SUCCESS
,
535 rc
= libmemcached_check_configuration(server_string
, strlen(server_string
), buffer
, sizeof(buffer
)),
536 memcached_strerror(NULL
, rc
));
538 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
541 memcached_server_fn callbacks
[]= { dump_server_information
};
542 test_true(memcached_success(memcached_server_cursor(memc
, callbacks
, NULL
, 1)));
544 memcached_free(memc
);