From 004d7eb1e658759b4d30932f414ed2de9a134519 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 16 Sep 2020 14:25:19 +0200 Subject: [PATCH] testing: sasl --- support/CMakeLists.txt | 4 +-- testing/lib/MemcachedCluster.cpp | 25 ++++++++++++--- testing/lib/MemcachedCluster.hpp | 4 +++ testing/lib/Retry.cpp | 2 +- testing/lib/Server.cpp | 11 +++++-- testing/lib/Server.hpp | 1 + testing/main.cpp | 21 ++++++++---- .../tests/memcached/regression/lp434843.cpp | 1 - testing/tests/memcached/sasl.cpp | 32 +++++++++++++++++++ 9 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 testing/tests/memcached/sasl.cpp diff --git a/support/CMakeLists.txt b/support/CMakeLists.txt index beefd5da..119e78bb 100644 --- a/support/CMakeLists.txt +++ b/support/CMakeLists.txt @@ -14,8 +14,8 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libmemcached.pc ) if(BUILD_TESTING) - set(LIBMEMCACHED_WITH_SASL_PWDB "MEMCACHED_SASL_PWDB=${CMAKE_CURRENT_BINARY_DIR}/memcached.pwdb" PARENT_SCOPE) - set(LIBMEMCACHED_WITH_SASL_CONF "SASL_CONF_PATH=${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE) + set(LIBMEMCACHED_WITH_SASL_PWDB "${CMAKE_CURRENT_BINARY_DIR}/memcached.pwdb" PARENT_SCOPE) + set(LIBMEMCACHED_WITH_SASL_CONF "${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE) cmake_host_system_information(RESULT HOSTNAME QUERY HOSTNAME) configure_file(memcached.pwdb.in memcached.pwdb @ONLY) diff --git a/testing/lib/MemcachedCluster.cpp b/testing/lib/MemcachedCluster.cpp index 910aca3c..c03bacce 100644 --- a/testing/lib/MemcachedCluster.cpp +++ b/testing/lib/MemcachedCluster.cpp @@ -6,6 +6,11 @@ const memcached_st MemcachedCluster::empty_memc{}; void MemcachedCluster::init() { REQUIRE(cluster.start()); + Retry cluster_is_listening([this]() { + return cluster.isListening(); + }); + REQUIRE(cluster_is_listening()); + REQUIRE(memcached_create(&memc)); for (const auto &server : cluster.getServers()) { auto target = server.getSocketOrPort(); @@ -16,10 +21,6 @@ void MemcachedCluster::init() { } } - Retry cluster_is_listening([this]() { - return cluster.isListening(); - }); - REQUIRE(cluster_is_listening()); } MemcachedCluster::~MemcachedCluster() { @@ -78,6 +79,22 @@ MemcachedCluster MemcachedCluster::socket() { }}}; } +#if LIBMEMCACHED_WITH_SASL_SUPPORT +MemcachedCluster MemcachedCluster::sasl() { + auto mc = MemcachedCluster{Cluster{Server{ + MEMCACHED_BINARY, + { + Server::arg_pair_t{"-p", random_socket_or_port_string}, + Server::arg_t{"-S"} + } + }}}; + mc.enableBinaryProto(); + REQUIRE(MEMCACHED_SUCCESS == memcached_set_sasl_auth_data(&mc.memc, + "memcached", "memcached")); + return mc; +} +#endif + void MemcachedCluster::enableBinaryProto(bool enable) { REQUIRE(MEMCACHED_SUCCESS == memcached_behavior_set(&memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, enable)); diff --git a/testing/lib/MemcachedCluster.hpp b/testing/lib/MemcachedCluster.hpp index ee2dfdd2..39859b7d 100644 --- a/testing/lib/MemcachedCluster.hpp +++ b/testing/lib/MemcachedCluster.hpp @@ -31,6 +31,10 @@ public: static MemcachedCluster network(); static MemcachedCluster socket(); +#if LIBMEMCACHED_WITH_SASL_SUPPORT + static MemcachedCluster sasl(); +#endif + private: static const memcached_st empty_memc; diff --git a/testing/lib/Retry.cpp b/testing/lib/Retry.cpp index 3630089f..71fde0e0 100644 --- a/testing/lib/Retry.cpp +++ b/testing/lib/Retry.cpp @@ -15,7 +15,7 @@ bool Retry::operator()() { return true; } this_thread::sleep_for(dur); - dur *= 2; + dur *= 1.2; } return false; diff --git a/testing/lib/Server.cpp b/testing/lib/Server.cpp index 2ab4d5b7..17bafe74 100644 --- a/testing/lib/Server.cpp +++ b/testing/lib/Server.cpp @@ -42,8 +42,8 @@ optional Server::handleArg(vector &arr, const string &arg, const if (arg == "-p" || arg == "--port") { auto port = next_arg(arg); pushArg(arr, port); - pushArg(arr, "-U"); - pushArg(arr, port); +// pushArg(arr, "-U"); +// pushArg(arr, port); socket_or_port = stoi(port); return port; } else if (arg == "-s" || arg == "--unix-socket") { @@ -51,6 +51,8 @@ optional Server::handleArg(vector &arr, const string &arg, const pushArg(arr, sock); socket_or_port = sock; return sock; + } else if (arg == "-S" || arg == "--enable-sasl") { + sasl = true; } return {}; } @@ -122,6 +124,11 @@ bool Server::isListening() { } } + if (sasl) { + memcached_behavior_set(*memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); + memcached_set_sasl_auth_data(*memc, "memcached", "memcached"); + } + Malloced stat(memcached_stat(*memc, nullptr, nullptr)); if (!*stat || !stat->pid || stat->pid != pid) { return false; diff --git a/testing/lib/Server.hpp b/testing/lib/Server.hpp index fb1b8af1..ac05eb13 100644 --- a/testing/lib/Server.hpp +++ b/testing/lib/Server.hpp @@ -67,6 +67,7 @@ public: private: string binary; argv_t args; + bool sasl = false; pid_t pid = 0; int pipe = -1; int status = 0; diff --git a/testing/main.cpp b/testing/main.cpp index b24f727b..2908745b 100644 --- a/testing/main.cpp +++ b/testing/main.cpp @@ -1,15 +1,24 @@ #define CATCH_CONFIG_RUNNER #include "lib/catch.hpp" +#include "mem_config.h" #include +#if HAVE_SETENV +# define SET_ENV(n, k, v) setenv(k, v, 0); +#else // !HAVE_SETENV +# define SET_ENV(n, k, v) static char n ## _env[] = k "=" v; putenv(n ## _env) +#endif + int main(int argc, char *argv[]) { + #if HAVE_ASAN -# if HAVE_SETENV - setenv("ASAN_OPTIONS", "halt_on_error=0", 0); -# else - char env[] = "ASAN_OPTIONS=halt_on_error=0"; - putenv(env); -# endif + SET_ENV(asan, "ASAN_OPTIONS", "halt_on_error=0") #endif + +#if LIBMEMCACHED_WITH_SASL_SUPPORT + SET_ENV(sasl_pwdb, "MEMCACHED_SASL_PWDB", LIBMEMCACHED_WITH_SASL_PWDB); + SET_ENV(sasl_conf, "SASL_CONF_PATH", LIBMEMCACHED_WITH_SASL_CONF); +#endif + return Catch::Session().run(argc, argv); } diff --git a/testing/tests/memcached/regression/lp434843.cpp b/testing/tests/memcached/regression/lp434843.cpp index 9a372a8c..1c1e95c1 100644 --- a/testing/tests/memcached/regression/lp434843.cpp +++ b/testing/tests/memcached/regression/lp434843.cpp @@ -31,7 +31,6 @@ TEST_CASE("memcached_regression_lp434843") { str[i] = random_ascii_string(36); chr[i] = str[i].data(); len[i] = str[i].length(); - cerr << str[i] << endl; } REQUIRE_SUCCESS(memcached_mget(memc, chr.data(), len.data(), NUM_KEYS)); diff --git a/testing/tests/memcached/sasl.cpp b/testing/tests/memcached/sasl.cpp new file mode 100644 index 00000000..cb5dbc9d --- /dev/null +++ b/testing/tests/memcached/sasl.cpp @@ -0,0 +1,32 @@ +#include "testing/lib/common.hpp" +#include "testing/lib/Shell.hpp" +#include "testing/lib/MemcachedCluster.hpp" + +TEST_CASE("memcached_sasl") { +#if !LIBMEMCACHED_WITH_SASL_SUPPORT + WARN("ENABLE_SASL=OFF"); +#else + Shell sh; + string mc{MEMCACHED_BINARY}, err; + + if (!sh.run(mc + " -S --version", err)) { + WARN(mc << ": " << err); + } else { + auto test = MemcachedCluster::sasl(); + auto memc = &test.memc; + + REQUIRE_SUCCESS(memcached_set(memc, S(__func__), S(__func__), 0, 0)); + REQUIRE_SUCCESS(memcached_delete(memc, S(__func__), 0)); + REQUIRE_SUCCESS(memcached_destroy_sasl_auth_data(memc)); + REQUIRE_SUCCESS(memcached_destroy_sasl_auth_data(memc)); + REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS, memcached_destroy_sasl_auth_data(nullptr)); + + memcached_quit(memc); + + REQUIRE_RC(MEMCACHED_AUTH_FAILURE, memcached_set(memc, S(__func__), S(__func__), 0, 0)); + + REQUIRE_SUCCESS(memcached_set_sasl_auth_data(memc, "username", "password")); + REQUIRE_RC(MEMCACHED_AUTH_FAILURE, memcached_set(memc, S(__func__), S(__func__), 0, 0)); + } +#endif +} -- 2.30.2