From 20e47d47598cf6bba875d5a3964ea8556330970e Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Fri, 26 Jun 2020 17:15:07 +0200 Subject: [PATCH] add passphrase_cache test --- passphrase_cache.cc | 11 +++-------- passphrase_cache.hh | 2 +- test/Makefile | 4 +++- test/test_passphrase_cache.cc | 26 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 test/test_passphrase_cache.cc diff --git a/passphrase_cache.cc b/passphrase_cache.cc index e91a3ca..57c0f35 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -4,11 +4,10 @@ namespace pEp { void PassphraseCache::add(std::string passphrase) { std::lock_guard lock(_mtx); - cleanup(); _cache.push_back({passphrase, clock::now()}); } - bool PassphraseCache::for_each_passphrase(passphrase_callee& callee) + bool PassphraseCache::for_each_passphrase(const passphrase_callee& callee) { std::lock_guard lock(_mtx); cleanup(); @@ -23,12 +22,8 @@ namespace pEp { void PassphraseCache::cleanup() { - for (auto entry = _cache.begin(); entry != _cache.end(); ) { - if (entry->tp < clock::now() - _timeout) - _cache.erase(entry); - else - break; - } + while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout) + _cache.pop_front(); } }; diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 9ab32b6..d74e3be 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -35,7 +35,7 @@ namespace pEp { // matching passphrase or no passphrases are left // returns true if a passphrase was matching, false otherwise using passphrase_callee = std::function; - bool for_each_passphrase(passphrase_callee& callee); + bool for_each_passphrase(const passphrase_callee& callee); protected: void cleanup(); diff --git a/test/Makefile b/test/Makefile index 7f8f4d2..574f0be 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,7 +6,7 @@ CXXFLAGS+=-I../ .PHONY=all, test_adapter, test_adapter_cxx, test_library -all: test_adapter test_adapter_cxx test_library +all: test_adapter test_adapter_cxx test_library test_passphrase_cache test_adapter: test_adapter.cc ../libpEpAdapter.a @@ -14,6 +14,8 @@ test_adapter_cxx: test_adapter_cxx.cc ../libpEpAdapter.a test_library: test_library.cc ../libpEpAdapter.a +test_passphrase_cache: test_passphrase_cache.cc ../libpEpAdapter.a + clean: rm -vf test_adapter rm -rvf test_adapter.dSYM diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc new file mode 100644 index 0000000..77c5f43 --- /dev/null +++ b/test/test_passphrase_cache.cc @@ -0,0 +1,26 @@ +#include +#include +#include + +#include "passphrase_cache.hh" + +int main() +{ + pEp::PassphraseCache cache{std::chrono::seconds(1)}; + cache.add("hello"); + cache.add("world"); + + std::cout << "expected: two passphrases\n"; + cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return false;}); + + std::cout << "expected: one passphrase\n"; + cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return true;}); + + sleep(2); + + std::cout << "expected: no passphrase\n"; + cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return false;}); + + return 0; +} +