From: Brian Aker Date: Sun, 21 Apr 2013 08:19:20 +0000 (-0400) Subject: Clean up cppcheck and add SKIP_UNLESS X-Git-Tag: 1.0.18~26^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=08c302d13c70ea37ecba7a4a1c3862c1c0d3b284;p=awesomized%2Flibmemcached Clean up cppcheck and add SKIP_UNLESS --- diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 659d3407..b19ba716 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -760,7 +760,7 @@ static memcached_return_t _memcached_connect(memcached_instance_st* server, cons case MEMCACHED_CONNECTION_TCP: rc= network_connect(server); -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) if (LIBMEMCACHED_WITH_SASL_SUPPORT) { if (server->fd != INVALID_SOCKET and server->root->sasl.callbacks) diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index 121e8f7c..75bfbee6 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -591,7 +591,7 @@ memcached_return_t memcached_server_add_parsed(memcached_st *ptr, in_port_t port, uint32_t weight) { - char buffer[MEMCACHED_NI_MAXHOST]; + char buffer[MEMCACHED_NI_MAXHOST]= { 0 }; memcpy(buffer, hostname, hostname_length); buffer[hostname_length]= 0; diff --git a/libtest/has.cc b/libtest/has.cc index fd25550e..b957bc6a 100644 --- a/libtest/has.cc +++ b/libtest/has.cc @@ -43,6 +43,18 @@ namespace libtest { +bool has_libmemcached_sasl(void) +{ +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT + if (LIBMEMCACHED_WITH_SASL_SUPPORT) + { + return true; + } +#endif + + return false; +} + bool has_libmemcached(void) { #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED @@ -85,7 +97,7 @@ bool has_postgres_support(void) bool has_gearmand() { -#if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY +#if defined(GEARMAND_BINARY) && defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY if (HAVE_GEARMAND_BINARY) { std::stringstream arg_buffer; @@ -111,7 +123,7 @@ bool has_gearmand() bool has_drizzled() { -#if defined(HAVE_DRIZZLED_BINARY) && HAVE_DRIZZLED_BINARY +#if defined(DRIZZLED_BINARY) && defined(HAVE_DRIZZLED_BINARY) && HAVE_DRIZZLED_BINARY if (HAVE_DRIZZLED_BINARY) { if (access(DRIZZLED_BINARY, X_OK) == 0) @@ -126,7 +138,7 @@ bool has_drizzled() bool has_mysqld() { -#if defined(HAVE_MYSQLD_BUILD) && HAVE_MYSQLD_BUILD +#if defined(MYSQLD_BINARY) && defined(HAVE_MYSQLD_BUILD) && HAVE_MYSQLD_BUILD if (HAVE_MYSQLD_BUILD) { if (access(MYSQLD_BINARY, X_OK) == 0) @@ -141,11 +153,11 @@ bool has_mysqld() static char memcached_binary_path[FILENAME_MAX]; -static void initialize_curl_startup() +static void initialize_memcached_binary_path() { memcached_binary_path[0]= 0; -#if defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY +#if defined(MEMCACHED_BINARY) && defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY if (HAVE_MEMCACHED_BINARY) { std::stringstream arg_buffer; @@ -170,7 +182,7 @@ static pthread_once_t memcached_binary_once= PTHREAD_ONCE_INIT; static void initialize_memcached_binary(void) { int ret; - if ((ret= pthread_once(&memcached_binary_once, initialize_curl_startup)) != 0) + if ((ret= pthread_once(&memcached_binary_once, initialize_memcached_binary_path)) != 0) { FATAL(strerror(ret)); } @@ -181,6 +193,7 @@ bool has_memcached() initialize_memcached_binary(); if (memcached_binary_path[0]) + if (memcached_binary_path[0] and (strlen(memcached_binary_path) > 0)) { return true; } @@ -200,29 +213,61 @@ const char* memcached_binary() return NULL; } -bool has_memcached_sasl() +static char memcached_sasl_binary_path[FILENAME_MAX]; + +static void initialize_has_memcached_sasl() { -#if defined(HAVE_MEMCACHED_SASL_BINARY) && HAVE_MEMCACHED_SASL_BINARY - if (HAVE_MEMCACHED_SASL_BINARY) + memcached_sasl_binary_path[0]= 0; + +#if defined(MEMCACHED_BINARY) && defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY + if (HAVE_MEMCACHED_BINARY) { - if (access(MEMCACHED_SASL_BINARY, X_OK) == 0) + std::stringstream arg_buffer; + + char *getenv_ptr; + if (bool((getenv_ptr= getenv("PWD"))) and strcmp(MEMCACHED_BINARY, "memcached/memcached") == 0) { - return true; + arg_buffer << getenv_ptr; + arg_buffer << "/"; + } + arg_buffer << MEMCACHED_BINARY; + + if (access(arg_buffer.str().c_str(), X_OK) == 0) + { + strncpy(memcached_sasl_binary_path, arg_buffer.str().c_str(), FILENAME_MAX); } } #endif +} + +bool has_memcached_sasl() +{ + initialize_has_memcached_sasl(); + + if (memcached_sasl_binary_path[0] and (strlen(memcached_sasl_binary_path) > 0)) + { + return true; + } return false; } const char *gearmand_binary() { +#if defined(GEARMAND_BINARY) return GEARMAND_BINARY; +#else + return NULL; +#endif } const char *drizzled_binary() { +#if defined(DRIZZLED_BINARY) return DRIZZLED_BINARY; +#else + return NULL; +#endif } } // namespace libtest diff --git a/libtest/has.hpp b/libtest/has.hpp index f38306d5..59ee88f9 100644 --- a/libtest/has.hpp +++ b/libtest/has.hpp @@ -38,6 +38,9 @@ namespace libtest { +LIBTEST_API +bool has_libmemcached_sasl(void); + LIBTEST_API bool has_libmemcached(); diff --git a/libtest/http.cc b/libtest/http.cc index 621c714c..c6caa09f 100644 --- a/libtest/http.cc +++ b/libtest/http.cc @@ -97,14 +97,14 @@ static void init(CURL *curl, const std::string& url) (void)http_get_result_callback; (void)curl; (void)url; +#if defined(HAVE_LIBCURL) && HAVE_LIBCURL if (HAVE_LIBCURL) { -#if defined(HAVE_LIBCURL) && HAVE_LIBCURL assert(curl); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT); -#endif } +#endif } HTTP::HTTP(const std::string& url_arg) : @@ -118,9 +118,9 @@ bool GET::execute() { (void)init; +#if defined(HAVE_LIBCURL) && HAVE_LIBCURL if (HAVE_LIBCURL) { -#if defined(HAVE_LIBCURL) && HAVE_LIBCURL CURL *curl= curl_easy_init(); init(curl, url()); @@ -134,17 +134,17 @@ bool GET::execute() curl_easy_cleanup(curl); return bool(retref == CURLE_OK); -#endif } +#endif return false; } bool POST::execute() { +#if defined(HAVE_LIBCURL) && HAVE_LIBCURL if (HAVE_LIBCURL) { -#if defined(HAVE_LIBCURL) && HAVE_LIBCURL CURL *curl= curl_easy_init();; init(curl, url()); @@ -158,17 +158,17 @@ bool POST::execute() curl_easy_cleanup(curl); return bool(retref == CURLE_OK); -#endif } +#endif return false; } bool TRACE::execute() { +#if defined(HAVE_LIBCURL) && HAVE_LIBCURL if (HAVE_LIBCURL) { -#if defined(HAVE_LIBCURL) && HAVE_LIBCURL CURL *curl= curl_easy_init();; init(curl, url()); @@ -183,17 +183,17 @@ bool TRACE::execute() curl_easy_cleanup(curl); return retref == CURLE_OK; -#endif } +#endif return false; } bool HEAD::execute() { +#if defined(HAVE_LIBCURL) && HAVE_LIBCURL if (HAVE_LIBCURL) { -#if defined(HAVE_LIBCURL) && HAVE_LIBCURL CURL *curl= curl_easy_init();; init(curl, url()); @@ -207,8 +207,8 @@ bool HEAD::execute() curl_easy_cleanup(curl); return retref == CURLE_OK; -#endif } +#endif return false; } diff --git a/libtest/lite.h b/libtest/lite.h index 342be97d..36f873e8 100644 --- a/libtest/lite.h +++ b/libtest/lite.h @@ -140,6 +140,34 @@ do \ } \ } while (0) +#define SKIP_UNLESS(__expression) \ +do \ +{ \ + if (! (__expression)) { \ + if (YATL_FULL) { \ + SKIP(#__expression); \ + } \ + fprintf(stdout, "\n%s:%d: %s SKIP '(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \ + exit(EXIT_SKIP); \ + } \ +} while (0) + +#define SKIP_UNLESS_(__expression, ...) \ +do \ +{ \ + if (! (__expression)) { \ + size_t ask= snprintf(0, 0, __VA_ARGS__); \ + ask++; \ + char *buffer= (char*)alloca(sizeof(char) * ask); \ + snprintf(buffer, ask, __VA_ARGS__); \ + if (YATL_FULL) { \ + SKIP(#__expression, buffer); \ + } \ + fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \ + exit(EXIT_SKIP); \ + } \ +} while (0) + #define ASSERT_TRUE(__expression) \ do \ { \ diff --git a/libtest/unittest.cc b/libtest/unittest.cc index 1663e59a..59351762 100644 --- a/libtest/unittest.cc +++ b/libtest/unittest.cc @@ -142,6 +142,25 @@ static test_return_t test_throw_skip_macro_TEST(void *) return TEST_FAILURE; } +static test_return_t test_throw_skip_unless_macro_TEST(void *) +{ + try { + SKIP_UNLESS(false); + } + catch (const libtest::__skipped&) + { + return TEST_SUCCESS; + } + catch (...) + { + FAIL("SLIP_UNLESS() failed to throw libtest::_skipped"); + } + + FAIL("SLIP_UNLESS() failed to throw"); + + return TEST_FAILURE; +} + static test_return_t test_throw_skip_TEST(void *) { try { @@ -1043,6 +1062,7 @@ test_st tests_log[] ={ {"SUCCESS", false, test_throw_success_TEST }, {"libtest::__skipped", false, test_throw_skip_TEST }, {"SKIP_IF", false, test_throw_skip_macro_TEST }, + {"SKIP_UNLESS", false, test_throw_skip_unless_macro_TEST }, {"FAIL", false, test_throw_fail_TEST }, {"ASSERT_FALSE_", false, ASSERT_FALSE__TEST }, {"ASSERT_FALSE", false, ASSERT_FALSE_TEST }, diff --git a/tests/libmemcached_world.h b/tests/libmemcached_world.h index 3dda2d8a..6bed25a9 100644 --- a/tests/libmemcached_world.h +++ b/tests/libmemcached_world.h @@ -43,26 +43,12 @@ static void *world_create(libtest::server_startup_st& servers, test_return_t& error) { - if (libtest::has_memcached() == false) - { - error= TEST_SKIPPED; - return NULL; - } + SKIP_UNLESS(libtest::has_libmemcached()); if (servers.sasl()) { - if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { - error= TEST_SKIPPED; - return NULL; - } + SKIP_UNLESS(libtest::has_libmemcached_sasl()); - if (HAVE_MEMCACHED_SASL_BINARY == 0) - { - error= TEST_SKIPPED; - return NULL; - } - // Assume we are running under valgrind, and bail if (getenv("TESTS_ENVIRONMENT")) {