From 58c279d58e2a44562f729e93e301dfedf42f530b Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Tue, 16 Aug 2011 17:03:08 -0700 Subject: [PATCH] Merge in updates for sasl. --- clients/memcat.cc | 13 +- clients/memcp.cc | 13 +- clients/memdump.cc | 15 +- clients/memflush.cc | 14 +- clients/memping.cc | 2 - clients/memrm.cc | 39 ++-- clients/utilities.cc | 103 ---------- configure.ac | 4 +- libmemcached/include.am | 4 + libmemcached/sasl.cc | 42 +++- libmemcached/util/pid.cc | 8 +- libmemcached/util/ping.cc | 6 - libtest/common.h | 1 + libtest/include.am | 3 + libtest/killpid.cc | 42 +++- libtest/killpid.h | 9 +- libtest/memcached.cc | 120 ++++++++---- libtest/server.cc | 335 -------------------------------- libtest/server.h | 76 -------- libtest/server_container.cc | 378 ++++++++++++++++++++++++++++++++++++ libtest/server_container.h | 111 +++++++++++ libtest/test.hpp | 1 + libtest/unittest.cc | 22 +++ libtest/version.h.in | 3 - tests/include.am | 6 +- tests/libmemcached_world.h | 8 + 26 files changed, 753 insertions(+), 625 deletions(-) create mode 100644 libtest/server_container.cc create mode 100644 libtest/server_container.h 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/include.am b/libmemcached/include.am index aa15f8e3..274fd25c 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -144,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/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/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..ceb2d878 100644 --- a/libtest/memcached.cc +++ b/libtest/memcached.cc @@ -100,25 +100,11 @@ public: memcached_return_t rc; 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..b2d7df0d 100644 --- a/libtest/unittest.cc +++ b/libtest/unittest.cc @@ -229,6 +229,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 +261,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} }; 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 a05bd2b8..ed345c92 100644 --- a/tests/include.am +++ b/tests/include.am @@ -370,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++) { -- 2.30.2