From: Brian Aker Date: Wed, 17 Aug 2011 01:16:16 +0000 (-0700) Subject: Merge in tests for SASL X-Git-Tag: 1.0.2~22^2~8 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=27ee6d2aea6210eaca004475600aba78b7170883;hp=a9c18d99fb245723f9d742af72eff9efa50e0334;p=m6w6%2Flibmemcached Merge in tests for SASL --- diff --git a/clients/memcat.cc b/clients/memcat.cc index fdbcb67e..66ee3253 100644 --- a/clients/memcat.cc +++ b/clients/memcat.cc @@ -81,10 +81,15 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false) + if (opt_username) { - memcached_free(memc); - return EXIT_FAILURE; + memcached_return_t ret; + if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) + { + std::cerr << memcached_last_error_message(memc) << std::endl; + memcached_free(memc); + return EXIT_FAILURE; + } } while (optind < argc) @@ -170,8 +175,6 @@ int main(int argc, char *argv[]) if (opt_hash) free(opt_hash); - shutdown_sasl(); - return return_code; } diff --git a/clients/memcp.cc b/clients/memcp.cc index 2a73390b..7b745476 100644 --- a/clients/memcp.cc +++ b/clients/memcp.cc @@ -123,11 +123,15 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (initialize_sasl(memc, opt_username, opt_passwd) == false) + if (opt_username) { - std::cerr << "Failed to initialize SASL support." << std::endl; - memcached_free(memc); - return EXIT_FAILURE; + memcached_return_t ret; + if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) + { + std::cerr << memcached_last_error_message(memc) << std::endl; + memcached_free(memc); + return EXIT_FAILURE; + } } while (optind < argc) @@ -216,7 +220,6 @@ int main(int argc, char *argv[]) free(opt_servers); if (opt_hash) free(opt_hash); - shutdown_sasl(); return return_code; } diff --git a/clients/memdump.cc b/clients/memdump.cc index 6f3c3e53..2b7b96fa 100644 --- a/clients/memdump.cc +++ b/clients/memdump.cc @@ -97,12 +97,15 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false) + if (opt_username) { - std::cerr << "Failed to initialize SASL support." << std::endl; - - memcached_free(memc); - return EXIT_FAILURE; + memcached_return_t ret; + if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) + { + std::cerr << memcached_last_error_message(memc) << std::endl; + memcached_free(memc); + return EXIT_FAILURE; + } } rc= memcached_dump(memc, callbacks, NULL, 1); @@ -122,8 +125,6 @@ int main(int argc, char *argv[]) if (opt_hash) free(opt_hash); - shutdown_sasl(); - return EXIT_SUCCESS; } diff --git a/clients/memflush.cc b/clients/memflush.cc index b19c2860..1ef1bfde 100644 --- a/clients/memflush.cc +++ b/clients/memflush.cc @@ -69,11 +69,15 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - - if (!initialize_sasl(memc, opt_username, opt_passwd)) + if (opt_username) { - memcached_free(memc); - return EXIT_FAILURE; + memcached_return_t ret; + if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) + { + std::cerr << memcached_last_error_message(memc) << std::endl; + memcached_free(memc); + return EXIT_FAILURE; + } } rc = memcached_flush(memc, opt_expire); @@ -90,8 +94,6 @@ int main(int argc, char *argv[]) free(opt_servers); - shutdown_sasl(); - return EXIT_SUCCESS; } diff --git a/clients/memping.cc b/clients/memping.cc index ee245cda..e2253471 100644 --- a/clients/memping.cc +++ b/clients/memping.cc @@ -73,8 +73,6 @@ int main(int argc, char *argv[]) free(opt_servers); - shutdown_sasl(); - return exit_code; } diff --git a/clients/memrm.cc b/clients/memrm.cc index 0551e326..73a29de9 100644 --- a/clients/memrm.cc +++ b/clients/memrm.cc @@ -53,8 +53,8 @@ int main(int argc, char *argv[]) opt_servers= strdup(temp); else { - fprintf(stderr, "No Servers provided\n"); - exit(1); + std::cerr << "No Servers provided" << std::endl; + return EXIT_FAILURE; } } @@ -74,28 +74,35 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - - if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false) + if (opt_username) { - std::cerr << "Failed to initialize SASL support." << std::endl; - - memcached_free(memc); - return EXIT_FAILURE; + memcached_return_t ret; + if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) + { + std::cerr << memcached_last_error_message(memc) << std::endl; + memcached_free(memc); + return EXIT_FAILURE; + } } while (optind < argc) { if (opt_verbose) - printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire); - rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); + { + std::cout << "key: " << argv[optind] << std::endl; + std::cout << "expires: " << opt_expire << std::endl; + } + rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { - fprintf(stderr, "memrm: %s: memcache error %s", - argv[optind], memcached_strerror(memc, rc)); + std::cerr << PROGRAM_NAME << ": " << argv[optind] << ": error " << memcached_strerror(memc, rc) << std::endl; + if (memcached_last_error_errno(memc)) - fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc))); - fprintf(stderr, "\n"); + { + std::cerr << " system error " << strerror(memcached_last_error_errno(memc)); + } + std::cerr << std::endl; return_code= -1; } @@ -111,8 +118,6 @@ int main(int argc, char *argv[]) if (opt_hash) free(opt_hash); - shutdown_sasl(); - return return_code; } diff --git a/clients/utilities.cc b/clients/utilities.cc index 8be59abb..97c30783 100644 --- a/clients/utilities.cc +++ b/clients/utilities.cc @@ -127,109 +127,6 @@ void process_hash_option(memcached_st *memc, char *opt_hash) } } - -static char *username; -static char *passwd; - -#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT - -static int get_username(void *context, int id, const char **result, unsigned int *len) -{ - (void)context; - if (!result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME)) - return SASL_BADPARAM; - - *result= username; - if (len) - { - *len= (username == NULL) ? 0 : (unsigned int)strlen(username); - } - - return SASL_OK; -} - -static int get_password(sasl_conn_t *conn, void *context, int id, - sasl_secret_t **psecret) -{ - (void)context; - static sasl_secret_t* ptr; - - if (!conn || ! psecret || id != SASL_CB_PASS) - return SASL_BADPARAM; - - if (passwd == NULL) - { - *psecret= NULL; - return SASL_OK; - } - - size_t len= strlen(passwd); - ptr= (sasl_secret_t *)malloc(sizeof(sasl_secret_t) + len +1); - if (not ptr) - return SASL_NOMEM; - - ptr->len= len; - memcpy(ptr->data, passwd, len); - ptr->data[len]= 0; - - *psecret= ptr; - return SASL_OK; -} - -typedef int (*local_sasl_fn)(void); - -/* callbacks we support */ -static sasl_callback_t sasl_callbacks[] = { - { SASL_CB_USER, (local_sasl_fn)get_username, NULL }, - { SASL_CB_AUTHNAME, (local_sasl_fn)get_username, NULL }, - { SASL_CB_PASS, (local_sasl_fn)get_password, NULL }, - { SASL_CB_LIST_END, NULL, NULL } -}; - -#endif - -bool initialize_sasl(memcached_st *memc, char *user, char *password) -{ - if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { - return false; - } - - if (memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)true) == 0) - { - return false; - } - - if (user != NULL && password != NULL) - { - username= user; - passwd= password; - -#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT - if (sasl_client_init(NULL) != SASL_OK) - { - fprintf(stderr, "Failed to initialize sasl library!\n"); - return false; - } - memcached_set_sasl_callbacks(memc, sasl_callbacks); -#else - (void)memc; -#endif - } - - return true; -} - -void shutdown_sasl(void) -{ - if (username or passwd) - { -#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT - sasl_done(); -#endif - } -} - void initialize_sockets(void) { /* Define the function for all platforms to avoid #ifdefs in each program */ diff --git a/configure.ac b/configure.ac index 5fc2c6c7..a7590b05 100644 --- a/configure.ac +++ b/configure.ac @@ -58,11 +58,11 @@ m4_include([m4/memcached_sasl.m4]) AM_CONDITIONAL(BUILDING_LIBMEMCACHED, true) AM_CONDITIONAL(HAVE_LIBMEMCACHED, false) -AC_SUBST(_WITH_LIBMEMCACHED_SUPPORT, ["_WITH_LIBMEMCACHED_SUPPORT 1"]) +AC_DEFINE([HAVE_LIBMEMCACHED], [1], [Enables libmemcached Support]) AM_CONDITIONAL(BUILDING_GEARMAN, false) AM_CONDITIONAL(HAVE_LIBGEARMAN, false) -AC_SUBST(_WITH_LIBGEARMAN_SUPPORT, ["_WITH_LIBGEARMAN_SUPPORT 0"]) +AC_DEFINE([HAVE_LIBGEARMAN], [0], [Enables libgearman Support]) AC_SEARCH_LIBS(getopt_long, gnugetopt) AC_SEARCH_LIBS(gethostbyname, nsl) diff --git a/libmemcached/allocators.cc b/libmemcached/allocators.cc index ec491ddb..cc41f2c3 100644 --- a/libmemcached/allocators.cc +++ b/libmemcached/allocators.cc @@ -40,7 +40,9 @@ void _libmemcached_free(const memcached_st*, void *mem, void*) { if (mem) + { free(mem); + } } void *_libmemcached_malloc(const memcached_st *, size_t size, void *) @@ -59,7 +61,9 @@ void *_libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size, { void *ret = _libmemcached_malloc(self, nelem * size, context); if (not ret) + { memset(ret, 0, nelem * size); + } return ret; } @@ -80,6 +84,11 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *self, memcached_calloc_fn mem_calloc, void *context) { + if (self == NULL) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + /* All should be set, or none should be set */ if (mem_malloc == NULL && mem_free == NULL && mem_realloc == NULL && mem_calloc == NULL) { @@ -87,7 +96,7 @@ memcached_return_t memcached_set_memory_allocators(memcached_st *self, } else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL) { - return MEMCACHED_FAILURE; + return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("NULL parameter provided for one or more allocators")); } else { @@ -112,6 +121,11 @@ void memcached_get_memory_allocators(const memcached_st *self, memcached_realloc_fn *mem_realloc, memcached_calloc_fn *mem_calloc) { + if (self == NULL) + { + return; + } + *mem_malloc= self->allocators.malloc; *mem_free= self->allocators.free; *mem_realloc= self->allocators.realloc; diff --git a/libmemcached/common.h b/libmemcached/common.h index 62a2b1c6..5af6916c 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -116,6 +116,7 @@ memcached_return_t memcached_server_execute(memcached_st *ptr, #ifdef __cplusplus #include #include +#include #endif #include @@ -164,14 +165,6 @@ memcached_return_t memcached_key_test(const memcached_st& memc, LIBMEMCACHED_LOCAL memcached_return_t memcached_purge(memcached_server_write_instance_st ptr); -LIBMEMCACHED_LOCAL - memcached_server_st *__server_create_with(const memcached_st *memc, - memcached_server_write_instance_st host, - const char *hostname, - in_port_t port, - uint32_t weight, - memcached_connection_t type); - static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary) { diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 3c12339c..fa35d84e 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -153,6 +153,7 @@ static memcached_return_t set_hostinfo(memcached_server_st *server) hints.ai_protocol= IPPROTO_TCP; } + server->address_info= NULL; int errcode; switch(errcode= getaddrinfo(server->hostname, str_port, &hints, &server->address_info)) { diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index 1bcaaa7a..4a9a1033 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -41,11 +41,6 @@ #include /* Protoypes (static) */ -static memcached_return_t server_add(memcached_st *ptr, const char *hostname, - in_port_t port, - uint32_t weight, - memcached_connection_t type); - static memcached_return_t update_continuum(memcached_st *ptr); static int compare_servers(const void *p1, const void *p2) @@ -342,6 +337,51 @@ static memcached_return_t update_continuum(memcached_st *ptr) return MEMCACHED_SUCCESS; } +static memcached_return_t server_add(memcached_st *ptr, + const memcached_string_t& hostname, + in_port_t port, + uint32_t weight, + memcached_connection_t type) +{ + assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st"); + if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP) + or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) ) + { + return memcached_set_error(*ptr, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT); + } + + memcached_server_st *new_host_list= static_cast(libmemcached_realloc(ptr, memcached_server_list(ptr), + sizeof(memcached_server_st) * (ptr->number_of_hosts + 1))); + + if (not new_host_list) + { + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } + + memcached_server_list_set(ptr, new_host_list); + + /* TODO: Check return type */ + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr)); + + if (not __server_create_with(ptr, instance, hostname, port, weight, type)) + { + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); + } + + if (weight > 1) + { + ptr->ketama.weighted= true; + } + + ptr->number_of_hosts++; + + // @note we place the count in the bottom of the server list + instance= memcached_server_instance_fetch(ptr, 0); + memcached_servers_set_count(instance, memcached_server_count(ptr)); + + return run_distribution(ptr); +} + memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list) { @@ -377,8 +417,10 @@ memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_serv instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr)); WATCHPOINT_ASSERT(instance); - if (not __server_create_with(ptr, instance, list[x].hostname, - list[x].port, list[x].weight, list[x].type)) + memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) }; + if (__server_create_with(ptr, instance, + hostname, + list[x].port, list[x].weight, list[x].type) == NULL) { return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } @@ -411,10 +453,18 @@ memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *pt const char *filename, uint32_t weight) { - if (! filename) + if (ptr == NULL) + { return MEMCACHED_FAILURE; + } + + memcached_string_t _filename= { memcached_string_make_from_cstr(filename) }; + if (memcached_is_valid_servername(_filename) == false) + { + memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided")); + } - return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET); + return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET); } memcached_return_t memcached_server_add_udp(memcached_st *ptr, @@ -429,13 +479,28 @@ memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr, in_port_t port, uint32_t weight) { + if (ptr == NULL) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + if (not port) + { port= MEMCACHED_DEFAULT_PORT; + } if (not hostname) + { hostname= "localhost"; + } - return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP); + memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) }; + if (memcached_is_valid_servername(_hostname) == false) + { + memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided")); + } + + return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_UDP); } memcached_return_t memcached_server_add(memcached_st *ptr, @@ -450,55 +515,31 @@ memcached_return_t memcached_server_add_with_weight(memcached_st *ptr, in_port_t port, uint32_t weight) { - if (not port) - port= MEMCACHED_DEFAULT_PORT; - - if (not hostname) - hostname= "localhost"; - - return server_add(ptr, hostname, port, weight, hostname[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP); -} - -static memcached_return_t server_add(memcached_st *ptr, const char *hostname, - in_port_t port, - uint32_t weight, - memcached_connection_t type) -{ - - if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP) - or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) ) + if (ptr == NULL) { - return MEMCACHED_INVALID_HOST_PROTOCOL; + return MEMCACHED_INVALID_ARGUMENTS; } - memcached_server_st *new_host_list= static_cast(libmemcached_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_st) * (ptr->number_of_hosts + 1))); - - if (not new_host_list) - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - - memcached_server_list_set(ptr, new_host_list); - - /* TODO: Check return type */ - memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr)); - - if (not __server_create_with(ptr, instance, hostname, port, weight, type)) + if (port == 0) { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); + port= MEMCACHED_DEFAULT_PORT; } - if (weight > 1) + size_t hostname_length= hostname ? strlen(hostname) : 0; + if (hostname_length == 0) { - ptr->ketama.weighted= true; + hostname= "localhost"; + hostname_length= sizeof("localhost") -1; } - ptr->number_of_hosts++; + memcached_string_t _hostname= { hostname, hostname_length }; - // @note we place the count in the bottom of the server list - instance= memcached_server_instance_fetch(ptr, 0); - memcached_servers_set_count(instance, memcached_server_count(ptr)); + if (memcached_is_valid_servername(_hostname) == false) + { + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided")); + } - return run_distribution(ptr); + return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP); } memcached_return_t memcached_server_add_parsed(memcached_st *ptr, @@ -512,7 +553,9 @@ memcached_return_t memcached_server_add_parsed(memcached_st *ptr, memcpy(buffer, hostname, hostname_length); buffer[hostname_length]= 0; - return server_add(ptr, buffer, + memcached_string_t _hostname= { buffer, hostname_length }; + + return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_TCP); diff --git a/libmemcached/include.am b/libmemcached/include.am index 425cec0b..274fd25c 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -31,6 +31,7 @@ noinst_HEADERS+= \ libmemcached/protocol/binary_handler.h \ libmemcached/protocol/common.h \ libmemcached/response.h \ + libmemcached/server.hpp \ libmemcached/server_instance.h \ libmemcached/string.hpp \ libmemcached/virtual_bucket.h @@ -143,6 +144,10 @@ libmemcached_libmemcached_la_LDFLAGS+= ${AM_LDFLAGS} -version-info ${MEMCACHED_L libmemcached_libmemcached_la_LIBADD+= $(LTLIBSASL) $(LTLIBSASL2) libmemcached_libmemcached_la_SOURCES += libmemcached/sasl.cc +if HAVE_SASL +libmemcached_libmemcached_la_CXXFLAGS+= ${PTHREAD_CFLAGS} +libmemcached_libmemcached_la_LIBADD+= ${PTHREAD_LIBS} +endif if HAVE_DTRACE BUILT_SOURCES+= libmemcached/dtrace_probes.h diff --git a/libmemcached/sasl.cc b/libmemcached/sasl.cc index 4f18ca2a..f1de41ea 100644 --- a/libmemcached/sasl.cc +++ b/libmemcached/sasl.cc @@ -41,6 +41,7 @@ #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT #include +#include void memcached_set_sasl_callbacks(memcached_st *ptr, const sasl_callback_t *callbacks) @@ -97,6 +98,23 @@ static memcached_return_t resolve_names(memcached_server_st& server, char *laddr return MEMCACHED_SUCCESS; } +static void sasl_shutdown_function() +{ + sasl_done(); +} + +static int sasl_startup_state= SASL_OK; +static pthread_once_t sasl_startup_once= PTHREAD_ONCE_INIT; +static void sasl_startup_function(void) +{ + sasl_startup_state= sasl_client_init(NULL); + + if (sasl_startup_state == SASL_OK) + { + (void)atexit(sasl_shutdown_function); + } +} + memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *server) { if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) @@ -158,18 +176,27 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s return rc; } - int ret; - if ((ret= sasl_client_init(NULL)) != SASL_OK) + int pthread_error; + if ((pthread_error= pthread_once(&sasl_startup_once, sasl_startup_function)) != 0) { - const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + return memcached_set_errno(*server, pthread_error, MEMCACHED_AT); + } + + if (sasl_startup_state != SASL_OK) + { + const char *sasl_error_msg= sasl_errstring(sasl_startup_state, NULL, NULL); return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } sasl_conn_t *conn; + int ret; if ((ret= sasl_client_new("memcached", server->hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn) ) != SASL_OK) { const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + + sasl_dispose(&conn); + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } @@ -181,6 +208,9 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s if (ret != SASL_OK and ret != SASL_CONTINUE) { const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + + sasl_dispose(&conn); + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } @@ -276,6 +306,12 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, return MEMCACHED_INVALID_ARGUMENTS; } + memcached_return_t ret; + if (memcached_failed(ret= memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) + { + return memcached_set_error(*ptr, ret, MEMCACHED_AT, memcached_literal_param("Unable change to binary protocol which is required for SASL.")); + } + memcached_destroy_sasl_auth_data(ptr); sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t)); diff --git a/libmemcached/server.cc b/libmemcached/server.cc index 5576caaa..073972dd 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -41,7 +41,8 @@ #include static inline void _server_init(memcached_server_st *self, memcached_st *root, - const char *hostname, in_port_t port, + const memcached_string_t& hostname, + in_port_t port, uint32_t weight, memcached_connection_t type) { self->options.is_shutting_down= false; @@ -80,14 +81,8 @@ static inline void _server_init(memcached_server_st *self, memcached_st *root, self->root= root; self->limit_maxbytes= 0; - if (hostname) - { - strncpy(self->hostname, hostname, NI_MAXHOST - 1); - } - else - { - self->hostname[0]= 0; - } + memcpy(self->hostname, hostname.c_str, hostname.size); + self->hostname[hostname.size]= 0; } static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc) @@ -97,7 +92,9 @@ static memcached_server_st *_server_create(memcached_server_st *self, const memc self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st)); if (not self) + { return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ + } self->options.is_allocated= true; } @@ -111,11 +108,19 @@ static memcached_server_st *_server_create(memcached_server_st *self, const memc return self; } -memcached_server_st *__server_create_with(const memcached_st *memc, +memcached_server_st *__server_create_with(memcached_st *memc, memcached_server_write_instance_st self, - const char *hostname, in_port_t port, - uint32_t weight, memcached_connection_t type) + const memcached_string_t& hostname, + const in_port_t port, + uint32_t weight, + const memcached_connection_t type) { + if (memcached_is_valid_servername(hostname) == false) + { + memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided")); + return NULL; + } + self= _server_create(self, memc); if (not self) @@ -143,6 +148,7 @@ void __server_free(memcached_server_st *self) { freeaddrinfo(self->address_info); self->address_info= NULL; + self->address_info_next= NULL; } memcached_error_free(*self); @@ -175,14 +181,18 @@ void memcached_server_free(memcached_server_st *self) If we do not have a valid object to clone from, we toss an error. */ memcached_server_st *memcached_server_clone(memcached_server_st *destination, - const memcached_server_st *source) + memcached_server_st *source) { /* We just do a normal create if source is missing */ if (not source) + { return NULL; + } + memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) }; destination= __server_create_with(source->root, destination, - source->hostname, source->port, source->weight, + hostname, + source->port, source->weight, source->type); if (not destination) { diff --git a/libmemcached/server.h b/libmemcached/server.h index fec1d48c..dc112153 100644 --- a/libmemcached/server.h +++ b/libmemcached/server.h @@ -108,7 +108,7 @@ void memcached_server_free(memcached_server_st *ptr); LIBMEMCACHED_LOCAL memcached_server_st *memcached_server_clone(memcached_server_st *destination, - const memcached_server_st *source); + memcached_server_st *source); LIBMEMCACHED_API memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *ptr); diff --git a/libmemcached/server.hpp b/libmemcached/server.hpp new file mode 100644 index 00000000..05b51f4f --- /dev/null +++ b/libmemcached/server.hpp @@ -0,0 +1,53 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + + +#pragma once + +#include + +static inline bool memcached_is_valid_servername(const memcached_string_t& arg) +{ + return arg.size > 0 or arg.size < NI_MAXHOST; +} + +LIBMEMCACHED_LOCAL + memcached_server_st *__server_create_with(memcached_st *memc, + memcached_server_write_instance_st host, + const memcached_string_t& hostname, + const in_port_t port, + uint32_t weight, + const memcached_connection_t type); diff --git a/libmemcached/server_list.cc b/libmemcached/server_list.cc index a599e0b6..a4a617c0 100644 --- a/libmemcached/server_list.cc +++ b/libmemcached/server_list.cc @@ -66,6 +66,7 @@ memcached_server_list_append_with_weight(memcached_server_list_st ptr, port= MEMCACHED_DEFAULT_PORT; } + /* Increment count for hosts */ count= 1; if (ptr != NULL) @@ -80,8 +81,9 @@ memcached_server_list_append_with_weight(memcached_server_list_st ptr, return NULL; } + memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) }; /* @todo Check return type */ - if (not __server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET)) + if (not __server_create_with(NULL, &new_host_list[count-1], _hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET)) { *error= memcached_set_errno(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); return NULL; diff --git a/libmemcached/util/pid.cc b/libmemcached/util/pid.cc index de009fbb..0ac3d4f3 100644 --- a/libmemcached/util/pid.cc +++ b/libmemcached/util/pid.cc @@ -118,12 +118,6 @@ pid_t libmemcached_util_getpid2(const char *hostname, in_port_t port, const char return -1; } - if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) - { - memcached_free(memc_ptr); - return false; - } - if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password))) { memcached_free(memc_ptr); @@ -148,7 +142,9 @@ pid_t libmemcached_util_getpid2(const char *hostname, in_port_t port, const char memcached_server_instance_st instance= memcached_server_instance_by_position(memc_ptr, 0); +#if 0 assert_msg(instance and instance->error_messages, " "); +#endif if (instance and instance->error_messages) { rc= memcached_server_error_return(instance); diff --git a/libmemcached/util/ping.cc b/libmemcached/util/ping.cc index 907f6dc0..66b1df77 100644 --- a/libmemcached/util/ping.cc +++ b/libmemcached/util/ping.cc @@ -101,12 +101,6 @@ bool libmemcached_util_ping2(const char *hostname, in_port_t port, const char *u return false; } - if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) - { - memcached_free(memc_ptr); - return false; - } - if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password))) { memcached_free(memc_ptr); diff --git a/libtest/common.h b/libtest/common.h index 8f2c0108..df271c8a 100644 --- a/libtest/common.h +++ b/libtest/common.h @@ -63,5 +63,6 @@ static inline bool is_pid_valid(const pid_t pid) #include #include +#include #include diff --git a/libtest/error.h b/libtest/error.h index 047d66b8..e24c1791 100644 --- a/libtest/error.h +++ b/libtest/error.h @@ -23,7 +23,7 @@ #pragma once enum test_return_t { - TEST_SUCCESS= 0, /* Backwards compatibility */ + TEST_SUCCESS, TEST_FAILURE, TEST_MEMORY_ALLOCATION_FAILURE, TEST_SKIPPED, diff --git a/libtest/include.am b/libtest/include.am index 3ee7d3f9..d7394fd0 100644 --- a/libtest/include.am +++ b/libtest/include.am @@ -70,6 +70,7 @@ noinst_HEADERS+= \ libtest/memcached.h \ libtest/runner.h \ libtest/server.h \ + libtest/server_container.h \ libtest/signal.h \ libtest/stats.h \ libtest/stream.h \ @@ -88,6 +89,7 @@ libtest_libtest_la_SOURCES= \ libtest/libtool.cc \ libtest/runner.cc \ libtest/server.cc \ + libtest/server_container.cc \ libtest/signal.cc \ libtest/test.cc @@ -183,6 +185,7 @@ libtest_unittest_DEPENDENCIES+= libtest/libtest.la libtest_tmp_dir libtest_unittest_LDADD+= libtest/libtest.la libtest_unittest_SOURCES= libtest/unittest.cc check_PROGRAMS+= libtest/unittest +noinst_PROGRAMS+= libtest/unittest test-unittest: libtest/unittest @libtest/unittest diff --git a/libtest/killpid.cc b/libtest/killpid.cc index 8a1543f4..79f6a3f8 100644 --- a/libtest/killpid.cc +++ b/libtest/killpid.cc @@ -85,15 +85,43 @@ bool kill_pid(pid_t pid_arg) return true; } - -pid_t kill_file(const std::string &filename) +bool check_pid(const std::string &filename) { - pid_t ret= -1; + if (filename.empty()) + { + return false; + } + FILE *fp; + if ((fp= fopen(filename.c_str(), "r"))) + { + char pid_buffer[1024]; + + char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp); + fclose(fp); + if (ptr) + { + pid_t pid= (pid_t)atoi(pid_buffer); + if (pid > 0) + { + return (::kill(pid, 0) == 0); + } + } + } + + return false; +} + + +bool kill_file(const std::string &filename) +{ if (filename.empty()) - return ret; + { + return true; + } + FILE *fp; if ((fp= fopen(filename.c_str(), "r"))) { char pid_buffer[1024]; @@ -106,13 +134,15 @@ pid_t kill_file(const std::string &filename) pid_t pid= (pid_t)atoi(pid_buffer); if (pid != 0) { - kill_pid(pid); + bool ret= kill_pid(pid); unlink(filename.c_str()); // If this happens we may be dealing with a dead server that left its pid file. + + return ret; } } } - return ret; + return false; } #define STRINGIFY(x) #x diff --git a/libtest/killpid.h b/libtest/killpid.h index becfd0ed..e35f8d99 100644 --- a/libtest/killpid.h +++ b/libtest/killpid.h @@ -25,6 +25,13 @@ bool kill_pid(pid_t pid_arg); -pid_t kill_file(const std::string &filename); +bool kill_file(const std::string &filename); + +bool check_pid(const std::string &filename); pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message); + +static inline bool check_pid(pid_t pid_arg) +{ + return (pid_arg > 1); +} diff --git a/libtest/memcached.cc b/libtest/memcached.cc index 3aba2543..3e8e3fcd 100644 --- a/libtest/memcached.cc +++ b/libtest/memcached.cc @@ -97,28 +97,14 @@ public: } pid_t local_pid; - memcached_return_t rc; + memcached_return_t rc= MEMCACHED_SUCCESS; if (has_socket()) { - if (username().empty()) - { - local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc); - } - else - { - local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc); - } + local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc); } else { - if (username().empty()) - { - local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc); - } - else - { - local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc); - } + local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc); } if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid)))) @@ -148,37 +134,16 @@ public: if (has_socket()) { - if (username().empty()) - { ret= libmemcached_util_ping(socket().c_str(), 0, &rc); - } - else - { - ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc); - } } else { - if (username().empty()) - { - ret= libmemcached_util_ping(hostname().c_str(), port(), &rc); - } - else - { - ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc); - } + ret= libmemcached_util_ping(hostname().c_str(), port(), &rc); } if (memcached_failed(rc) or not ret) { - if (username().empty()) - { - Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc); - } - else - { - Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc); - } + Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc); } return ret; @@ -260,6 +225,73 @@ public: return MEMCACHED_SASL_BINARY; } + pid_t get_pid(bool error_is_ok) + { + // Memcached is slow to start, so we need to do this + if (not pid_file().empty()) + { + Wait wait(pid_file(), 0); + + if (error_is_ok and not wait.successful()) + { + Error << "Pidfile was not found:" << pid_file(); + return -1; + } + } + + pid_t local_pid; + memcached_return_t rc; + if (has_socket()) + { + local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc); + } + else + { + local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc); + } + + if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid)))) + { + Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this; + } + + return local_pid; + } + + bool ping() + { + // Memcached is slow to start, so we need to do this + if (not pid_file().empty()) + { + Wait wait(pid_file(), 0); + + if (not wait.successful()) + { + Error << "Pidfile was not found:" << pid_file(); + return -1; + } + } + + memcached_return_t rc; + bool ret; + + if (has_socket()) + { + ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc); + } + else + { + ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc); + } + + if (memcached_failed(rc) or not ret) + { + Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc); + } + + return ret; + } + }; @@ -308,11 +340,21 @@ libtest::Server *build_memcached_socket(const std::string& socket_file, const in libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password) { + if (username.empty()) + { + return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached"); + } + return new MemcachedSaSL(hostname, try_port, false, username, password); } libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string &password) { + if (username.empty()) + { + return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached"); + } + return new MemcachedSaSL(socket_file, try_port, true, username, password); } diff --git a/libtest/server.cc b/libtest/server.cc index 603b3f45..5f0d59ec 100644 --- a/libtest/server.cc +++ b/libtest/server.cc @@ -113,13 +113,6 @@ Server::~Server() } } -std::string server_startup_st::option_string() const -{ - std::string temp= server_list; - rtrim(temp); - return temp; -} - // If the server exists, kill it bool Server::cycle() { @@ -445,332 +438,4 @@ bool Server::kill(pid_t pid_arg) return false; } -void server_startup_st::push_server(Server *arg) -{ - servers.push_back(arg); - - char port_str[NI_MAXSERV]; - snprintf(port_str, sizeof(port_str), "%u", int(arg->port())); - - std::string server_config_string; - if (arg->has_socket()) - { - server_config_string+= "--socket="; - server_config_string+= '"'; - server_config_string+= arg->socket(); - server_config_string+= '"'; - server_config_string+= " "; - } - else - { - server_config_string+= "--server="; - server_config_string+= arg->hostname(); - server_config_string+= ":"; - server_config_string+= port_str; - server_config_string+= " "; - } - - server_list+= server_config_string; - -} - -Server* server_startup_st::pop_server() -{ - Server *tmp= servers.back(); - servers.pop_back(); - return tmp; -} - -void server_startup_st::shutdown(bool remove) -{ - if (remove) - { - for (std::vector::iterator iter= servers.begin(); iter != servers.end(); iter++) - { - delete *iter; - } - servers.clear(); - } - else - { - for (std::vector::iterator iter= servers.begin(); iter != servers.end(); iter++) - { - if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid())) - { - Error << "Unable to kill:" << *(*iter); - } - } - } -} - -server_startup_st::~server_startup_st() -{ - shutdown(true); -} - -bool server_startup_st::is_debug() const -{ - return bool(getenv("LIBTEST_MANUAL_GDB")); -} - -bool server_startup_st::is_valgrind() const -{ - return bool(getenv("LIBTEST_MANUAL_VALGRIND")); -} - -bool server_startup_st::is_helgrind() const -{ - return bool(getenv("LIBTEST_MANUAL_HELGRIND")); -} - - -bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[]) -{ - Outn(); - (void)try_port; - - // Look to see if we are being provided ports to use - { - char variable_buffer[1024]; - snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count()); - - char *var; - if ((var= getenv(variable_buffer))) - { - in_port_t tmp= in_port_t(atoi(var)); - - if (tmp > 0) - try_port= tmp; - } - } - - libtest::Server *server= NULL; - if (0) - { } - else if (server_type.compare("gearmand") == 0) - { - if (GEARMAND_BINARY) - { - if (HAVE_LIBGEARMAN) - { - server= build_gearmand("localhost", try_port); - } - else - { - Error << "Libgearman was not found"; - } - } - else - { - Error << "No gearmand binary is available"; - } - } - else if (server_type.compare("blobslap_worker") == 0) - { - if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER) - { - if (HAVE_LIBGEARMAN) - { - server= build_blobslap_worker(try_port); - } - else - { - Error << "Libgearman was not found"; - } - } - else - { - Error << "No gearmand binary is available"; - } - } - else if (server_type.compare("memcached-sasl") == 0) - { - if (MEMCACHED_SASL_BINARY) - { - if (HAVE_LIBMEMCACHED) - { - server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password()); - } - else - { - Error << "Libmemcached was not found"; - } - } - else - { - Error << "No memcached binary that was compiled with sasl is available"; - } - } - else if (server_type.compare("memcached") == 0) - { - if (MEMCACHED_BINARY) - { - if (HAVE_LIBMEMCACHED) - { - server= build_memcached("localhost", try_port); - } - else - { - Error << "Libmemcached was not found"; - } - } - else - { - Error << "No memcached binary is available"; - } - } - else - { - Error << "Failed to start " << server_type << ", no support was found to be compiled in for it."; - } - - if (server == NULL) - { - Error << "Failure occured while creating server: " << server_type; - return false; - } - - /* - We will now cycle the server we have created. - */ - if (not server->cycle()) - { - Error << "Could not start up server " << *server; - delete server; - return false; - } - - server->build(argc, argv); - - if (construct.is_debug()) - { - Out << "Pausing for startup, hit return when ready."; - std::string gdb_command= server->base_command(); - std::string options; - Out << "run " << server->args(options); - getchar(); - } - else if (not server->start()) - { - Error << "Failed to start " << *server; - delete server; - return false; - } - else - { - Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running(); - } - - construct.push_server(server); - - if (default_port() == 0) - { - assert(server->has_port()); - set_default_port(server->port()); - } - - Outn(); - - return true; -} - -bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]) -{ - (void)try_port; - Outn(); - - Server *server= NULL; - if (0) - { } - else if (server_type.compare("gearmand") == 0) - { - Error << "Socket files are not supported for gearmand yet"; - } - else if (server_type.compare("memcached-sasl") == 0) - { - if (MEMCACHED_SASL_BINARY) - { - if (HAVE_LIBMEMCACHED) - { - server= build_memcached_sasl_socket("localhost", try_port, username(), password()); - } - else - { - Error << "Libmemcached was not found"; - } - } - else - { - Error << "No memcached binary is available"; - } - } - else if (server_type.compare("memcached") == 0) - { - if (MEMCACHED_BINARY) - { - if (HAVE_LIBMEMCACHED) - { - server= build_memcached_socket("localhost", try_port); - } - else - { - Error << "Libmemcached was not found"; - } - } - else - { - Error << "No memcached binary is available"; - } - } - else - { - Error << "Failed to start " << server_type << ", no support was found to be compiled in for it."; - } - - if (server == NULL) - { - Error << "Failure occured while creating server: " << server_type; - return false; - } - - /* - We will now cycle the server we have created. - */ - if (not server->cycle()) - { - Error << "Could not start up server " << *server; - delete server; - return false; - } - - server->build(argc, argv); - - if (is_debug()) - { - Out << "Pausing for startup, hit return when ready."; - std::string gdb_command= server->base_command(); - std::string options; - Out << "run " << server->args(options); - getchar(); - } - else if (not server->start()) - { - Error << "Failed to start " << *server; - delete server; - return false; - } - else - { - Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running(); - } - - push_server(server); - - set_default_socket(server->socket().c_str()); - - Outn(); - - return true; -} - } // namespace libtest diff --git a/libtest/server.h b/libtest/server.h index b4a48446..123d1c32 100644 --- a/libtest/server.h +++ b/libtest/server.h @@ -185,82 +185,6 @@ private: std::ostream& operator<<(std::ostream& output, const libtest::Server &arg); -class server_startup_st -{ -private: - std::string server_list; - bool _socket; - bool _sasl; - std::string _username; - std::string _password; - -public: - - uint8_t udp; - std::vector servers; - - server_startup_st() : - _socket(false), - _sasl(false), - udp(0) - { } - - bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]); - - std::string option_string() const; - - size_t count() const - { - return servers.size(); - } - - const std::string& password() const - { - return _password; - } - - const std::string& username() const - { - return _username; - } - - - bool is_debug() const; - bool is_helgrind() const; - bool is_valgrind() const; - - bool socket() - { - return _socket; - } - - bool sasl() - { - return _sasl; - } - - void set_socket() - { - _socket= true; - } - - void set_sasl(const std::string& username_arg, const std::string& password_arg) - { - _sasl= true; - _username= username_arg; - _password= password_arg; - } - - - void shutdown(bool remove= false); - void push_server(Server *); - Server *pop_server(); - - ~server_startup_st(); -}; - -bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]); - } // namespace libtest diff --git a/libtest/server_container.cc b/libtest/server_container.cc new file mode 100644 index 00000000..f747dc22 --- /dev/null +++ b/libtest/server_container.cc @@ -0,0 +1,378 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +// trim from end +static inline std::string &rtrim(std::string &s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); + return s; +} + +namespace libtest { + +void server_startup_st::push_server(Server *arg) +{ + servers.push_back(arg); + + char port_str[NI_MAXSERV]; + snprintf(port_str, sizeof(port_str), "%u", int(arg->port())); + + std::string server_config_string; + if (arg->has_socket()) + { + server_config_string+= "--socket="; + server_config_string+= '"'; + server_config_string+= arg->socket(); + server_config_string+= '"'; + server_config_string+= " "; + } + else + { + server_config_string+= "--server="; + server_config_string+= arg->hostname(); + server_config_string+= ":"; + server_config_string+= port_str; + server_config_string+= " "; + } + + server_list+= server_config_string; + +} + +Server* server_startup_st::pop_server() +{ + Server *tmp= servers.back(); + servers.pop_back(); + return tmp; +} + +void server_startup_st::shutdown(bool remove) +{ + if (remove) + { + for (std::vector::iterator iter= servers.begin(); iter != servers.end(); iter++) + { + delete *iter; + } + servers.clear(); + } + else + { + for (std::vector::iterator iter= servers.begin(); iter != servers.end(); iter++) + { + if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid())) + { + Error << "Unable to kill:" << *(*iter); + } + } + } +} + +server_startup_st::~server_startup_st() +{ + shutdown(true); +} + +bool server_startup_st::is_debug() const +{ + return bool(getenv("LIBTEST_MANUAL_GDB")); +} + +bool server_startup_st::is_valgrind() const +{ + return bool(getenv("LIBTEST_MANUAL_VALGRIND")); +} + +bool server_startup_st::is_helgrind() const +{ + return bool(getenv("LIBTEST_MANUAL_HELGRIND")); +} + + +bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[]) +{ + Outn(); + (void)try_port; + + // Look to see if we are being provided ports to use + { + char variable_buffer[1024]; + snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count()); + + char *var; + if ((var= getenv(variable_buffer))) + { + in_port_t tmp= in_port_t(atoi(var)); + + if (tmp > 0) + try_port= tmp; + } + } + + libtest::Server *server= NULL; + if (0) + { } + else if (server_type.compare("gearmand") == 0) + { + if (GEARMAND_BINARY) + { + if (HAVE_LIBGEARMAN) + { + server= build_gearmand("localhost", try_port); + } + else + { + Error << "Libgearman was not found"; + } + } + else + { + Error << "No gearmand binary is available"; + } + } + else if (server_type.compare("blobslap_worker") == 0) + { + if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER) + { + if (HAVE_LIBGEARMAN) + { + server= build_blobslap_worker(try_port); + } + else + { + Error << "Libgearman was not found"; + } + } + else + { + Error << "No gearmand binary is available"; + } + } + else if (server_type.compare("memcached-sasl") == 0) + { + if (MEMCACHED_SASL_BINARY) + { + if (HAVE_LIBMEMCACHED) + { + server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password()); + } + else + { + Error << "Libmemcached was not found"; + } + } + else + { + Error << "No memcached binary that was compiled with sasl is available"; + } + } + else if (server_type.compare("memcached") == 0) + { + if (MEMCACHED_BINARY) + { + if (HAVE_LIBMEMCACHED) + { + server= build_memcached("localhost", try_port); + } + else + { + Error << "Libmemcached was not found"; + } + } + else + { + Error << "No memcached binary is available"; + } + } + else + { + Error << "Failed to start " << server_type << ", no support was found to be compiled in for it."; + } + + if (server == NULL) + { + Error << "Failure occured while creating server: " << server_type; + return false; + } + + /* + We will now cycle the server we have created. + */ + if (not server->cycle()) + { + Error << "Could not start up server " << *server; + delete server; + return false; + } + + server->build(argc, argv); + + if (construct.is_debug()) + { + Out << "Pausing for startup, hit return when ready."; + std::string gdb_command= server->base_command(); + std::string options; + Out << "run " << server->args(options); + getchar(); + } + else if (not server->start()) + { + Error << "Failed to start " << *server; + delete server; + return false; + } + else + { + Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running(); + } + + construct.push_server(server); + + if (default_port() == 0) + { + assert(server->has_port()); + set_default_port(server->port()); + } + + Outn(); + + return true; +} + +bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]) +{ + (void)try_port; + Outn(); + + Server *server= NULL; + if (0) + { } + else if (server_type.compare("gearmand") == 0) + { + Error << "Socket files are not supported for gearmand yet"; + } + else if (server_type.compare("memcached-sasl") == 0) + { + if (MEMCACHED_SASL_BINARY) + { + if (HAVE_LIBMEMCACHED) + { + server= build_memcached_sasl_socket("localhost", try_port, username(), password()); + } + else + { + Error << "Libmemcached was not found"; + } + } + else + { + Error << "No memcached binary is available"; + } + } + else if (server_type.compare("memcached") == 0) + { + if (MEMCACHED_BINARY) + { + if (HAVE_LIBMEMCACHED) + { + server= build_memcached_socket("localhost", try_port); + } + else + { + Error << "Libmemcached was not found"; + } + } + else + { + Error << "No memcached binary is available"; + } + } + else + { + Error << "Failed to start " << server_type << ", no support was found to be compiled in for it."; + } + + if (server == NULL) + { + Error << "Failure occured while creating server: " << server_type; + return false; + } + + /* + We will now cycle the server we have created. + */ + if (not server->cycle()) + { + Error << "Could not start up server " << *server; + delete server; + return false; + } + + server->build(argc, argv); + + if (is_debug()) + { + Out << "Pausing for startup, hit return when ready."; + std::string gdb_command= server->base_command(); + std::string options; + Out << "run " << server->args(options); + getchar(); + } + else if (not server->start()) + { + Error << "Failed to start " << *server; + delete server; + return false; + } + else + { + Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running(); + } + + push_server(server); + + set_default_socket(server->socket().c_str()); + + Outn(); + + return true; +} + +std::string server_startup_st::option_string() const +{ + std::string temp= server_list; + rtrim(temp); + return temp; +} + + +} // namespace libtest diff --git a/libtest/server_container.h b/libtest/server_container.h new file mode 100644 index 00000000..9c9922ed --- /dev/null +++ b/libtest/server_container.h @@ -0,0 +1,111 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace libtest { + +class server_startup_st +{ +private: + std::string server_list; + bool _socket; + bool _sasl; + std::string _username; + std::string _password; + +public: + + uint8_t udp; + std::vector servers; + + server_startup_st() : + _socket(false), + _sasl(false), + udp(0) + { } + + bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]); + + std::string option_string() const; + + size_t count() const + { + return servers.size(); + } + + const std::string& password() const + { + return _password; + } + + const std::string& username() const + { + return _username; + } + + + bool is_debug() const; + bool is_helgrind() const; + bool is_valgrind() const; + + bool socket() + { + return _socket; + } + + bool sasl() + { + return _sasl; + } + + void set_socket() + { + _socket= true; + } + + void set_sasl(const std::string& username_arg, const std::string& password_arg) + { + _sasl= true; + _username= username_arg; + _password= password_arg; + } + + + void shutdown(bool remove= false); + void push_server(Server *); + Server *pop_server(); + + ~server_startup_st(); +}; + +bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]); + +} // namespace libtest diff --git a/libtest/test.hpp b/libtest/test.hpp index 1933ef4b..a10aa30d 100644 --- a/libtest/test.hpp +++ b/libtest/test.hpp @@ -34,6 +34,7 @@ #include #include +#include #include #include #include diff --git a/libtest/unittest.cc b/libtest/unittest.cc index d2c965ba..93192a25 100644 --- a/libtest/unittest.cc +++ b/libtest/unittest.cc @@ -24,6 +24,14 @@ #include +#if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT +#include +#endif + +#if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT +#include +#endif + #include #include @@ -53,6 +61,13 @@ static test_return_t GDB_COMMAND_test(void *) return TEST_SUCCESS; } +static test_return_t test_success_equals_one_test(void *) +{ + test_skip(HAVE_LIBMEMCACHED, true); + test_zero(MEMCACHED_SUCCESS); + return TEST_SUCCESS; +} + static test_return_t test_success_test(void *) { return TEST_SUCCESS; @@ -181,6 +196,28 @@ static test_return_t var_log_rm_test(void *) return TEST_SUCCESS; } +static test_return_t _compare_test_return_t_test(void *) +{ + test_compare(TEST_SUCCESS, TEST_SUCCESS); + + return TEST_SUCCESS; +} + +static test_return_t _compare_memcached_return_t_test(void *) +{ + test_skip(HAVE_LIBMEMCACHED, true); + test_compare(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS); + + return TEST_SUCCESS; +} + +static test_return_t _compare_gearman_return_t_test(void *) +{ + test_skip(HAVE_LIBGEARMAN, true); + + return TEST_SUCCESS; +} + static test_return_t gearmand_cycle_test(void *object) { server_startup_st *servers= (server_startup_st*)object; @@ -229,6 +266,27 @@ static test_return_t memcached_socket_cycle_test(void *object) return TEST_SKIPPED; } +static test_return_t memcached_sasl_test(void *object) +{ + server_startup_st *servers= (server_startup_st*)object; + test_true(servers); + + if (getenv("TESTS_ENVIRONMENT")) + { + return TEST_SKIPPED; + } + + if (MEMCACHED_SASL_BINARY and HAVE_LIBMEMCACHED) + { + const char *argv[1]= { "cycle_memcached_sasl" }; + test_true(server_startup(*servers, "memcached-sasl", 9996, 1, argv)); + + return TEST_SUCCESS; + } + + return TEST_SKIPPED; +} + test_st gearmand_tests[] ={ #if 0 {"pause", 0, pause_test }, @@ -240,6 +298,7 @@ test_st gearmand_tests[] ={ test_st memcached_tests[] ={ {"memcached startup-shutdown", 0, memcached_cycle_test }, {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test }, + {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test }, {0, 0, 0} }; @@ -252,8 +311,9 @@ test_st environment_tests[] ={ }; test_st tests_log[] ={ - {"TEST_SUCCESS", 0, test_success_test }, - {"TEST_FAILURE", 0, test_failure_test }, + {"TEST_SUCCESS", false, test_success_test }, + {"TEST_FAILURE", false, test_failure_test }, + {"TEST_SUCCESS == 0", false, test_success_equals_one_test }, {0, 0, 0} }; @@ -277,11 +337,19 @@ test_st directories_tests[] ={ {0, 0, 0} }; +test_st comparison_tests[] ={ + {"_compare(test_return_t)", 0, _compare_test_return_t_test }, + {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test }, + {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test }, + {0, 0, 0} +}; + collection_st collection[] ={ {"environment", 0, 0, environment_tests}, {"return values", 0, 0, tests_log}, {"local", 0, 0, local_log}, {"directories", 0, 0, directories_tests}, + {"comparison", 0, 0, comparison_tests}, {"gearmand", 0, 0, gearmand_tests}, {"memcached", 0, 0, memcached_tests}, {0, 0, 0, 0} diff --git a/libtest/version.h.in b/libtest/version.h.in index a670a2e1..16c4656b 100644 --- a/libtest/version.h.in +++ b/libtest/version.h.in @@ -26,9 +26,6 @@ extern "C" { #endif -#define LIBTEST@_WITH_LIBMEMCACHED_SUPPORT@ -#define LIBTEST@_WITH_LIBGEARMAN_SUPPORT@ - #define LIBTEST_VERSION_STRING "@VERSION@" #define LIBTEST_VERSION_HEX @HEX_VERSION@ diff --git a/tests/include.am b/tests/include.am index b5f4fb0d..ed345c92 100644 --- a/tests/include.am +++ b/tests/include.am @@ -36,6 +36,7 @@ noinst_HEADERS+= \ tests/pool.h \ tests/print.h \ tests/replication.h \ + tests/server_add.h \ tests/string.h \ tests/virtual_buckets.h @@ -77,6 +78,7 @@ tests_testapp_SOURCES= \ tests/pool.cc \ tests/print.cc \ tests/replication.cc \ + tests/server_add.cc \ tests/virtual_buckets.cc tests_testapp_SOURCES+= clients/generator.cc clients/execute.cc tests_testapp_DEPENDENCIES= \ @@ -368,15 +370,15 @@ valgrind-udp: tests/testudp valgrind-plus: tests/testplus @$(VALGRIND_COMMAND) tests/testplus +valgrind-sasl: tests/sasl + @$(VALGRIND_COMMAND) tests/sasl + valgrind-hash: tests/testhashkit @$(VALGRIND_COMMAND) tests/testhashkit valgrind-hashplus: tests/hash_plus @$(VALGRIND_COMMAND) tests/hash_plus -.PHONY += valgrind -valgrind: valgrind-cycle valgrind-mem valgrind-udp valgrind-plus valgrind-hash valgrind-hashplus - helgrind-cycle: tests/cycle @$(HELGRIND_COMMAND) tests/cycle diff --git a/tests/libmemcached_world.h b/tests/libmemcached_world.h index 17561838..e2f0c27d 100644 --- a/tests/libmemcached_world.h +++ b/tests/libmemcached_world.h @@ -37,6 +37,14 @@ static void *world_create(server_startup_st& servers, test_return_t& error) return NULL; } + // Assume we are running under valgrind, and bail + if (servers.sasl() and getenv("TESTS_ENVIRONMENT")) + { + error= TEST_SKIPPED; + return NULL; + } + + in_port_t max_port; for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++) { diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index 6845074b..ab850f50 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -68,16 +68,18 @@ #define SMALL_STRING_LEN 1024 #include + +#include "tests/basic.h" +#include "tests/debug.h" #include "tests/deprecated.h" -#include "tests/parser.h" +#include "tests/error_conditions.h" #include "tests/ketama.h" -#include "tests/pool.h" #include "tests/namespace.h" -#include "tests/replication.h" -#include "tests/debug.h" -#include "tests/basic.h" -#include "tests/error_conditions.h" +#include "tests/parser.h" +#include "tests/pool.h" #include "tests/print.h" +#include "tests/replication.h" +#include "tests/server_add.h" #include "tests/virtual_buckets.h" using namespace libtest; @@ -3875,7 +3877,7 @@ static test_return_t deprecated_set_memory_alloc(memcached_st *memc) static test_return_t set_memory_alloc(memcached_st *memc) { - test_compare(MEMCACHED_FAILURE, + test_compare(MEMCACHED_INVALID_ARGUMENTS, memcached_set_memory_allocators(memc, NULL, my_free, my_realloc, my_calloc, NULL)); @@ -6131,6 +6133,12 @@ test_st virtual_bucket_tests[] ={ {0, 0, (test_callback_fn*)0} }; +test_st memcached_server_add_tests[] ={ + {"memcached_server_add(\"\")", false, (test_callback_fn*)memcached_server_add_empty_test }, + {"memcached_server_add(NULL)", false, (test_callback_fn*)memcached_server_add_null_test }, + {0, 0, (test_callback_fn*)0} +}; + test_st namespace_tests[] ={ {"basic tests", true, (test_callback_fn*)selection_of_namespace_tests }, {"increment", true, (test_callback_fn*)memcached_increment_namespace }, @@ -6145,6 +6153,7 @@ collection_st collection[] ={ {"basic", 0, 0, basic_tests}, {"hsieh_availability", 0, 0, hsieh_availability}, {"murmur_availability", 0, 0, murmur_availability}, + {"memcached_server_add", 0, 0, memcached_server_add_tests}, {"block", 0, 0, tests}, {"binary", (test_callback_fn*)pre_binary, 0, tests}, {"nonblock", (test_callback_fn*)pre_nonblock, 0, tests}, diff --git a/tests/server_add.cc b/tests/server_add.cc new file mode 100644 index 00000000..b343bf9c --- /dev/null +++ b/tests/server_add.cc @@ -0,0 +1,67 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached Client and Server + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +using namespace libtest; + +#include + +#include + +test_return_t memcached_server_add_null_test(memcached_st*) +{ + memcached_st *memc= memcached_create(NULL); + + test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, NULL, 0)); + + memcached_free(memc); + + return TEST_SUCCESS; +} + +test_return_t memcached_server_add_empty_test(memcached_st*) +{ + memcached_st *memc= memcached_create(NULL); + + test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, "", 0)); + + memcached_free(memc); + + return TEST_SUCCESS; +} diff --git a/tests/server_add.h b/tests/server_add.h new file mode 100644 index 00000000..53ced524 --- /dev/null +++ b/tests/server_add.h @@ -0,0 +1,41 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached Client and Server + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#pragma once + +test_return_t memcached_server_add_null_test(memcached_st*); +test_return_t memcached_server_add_empty_test(memcached_st*);