From de3189fc0def62f03bfac9b2bd73ea8945c94742 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sun, 28 Jun 2020 14:46:20 +0200 Subject: [PATCH] C++ has no coroutines (yet), and the ones of boost are so ugly --- passphrase_cache.cc | 21 +++++++++++++++++++++ passphrase_cache.hh | 11 +++++++++++ test/test_passphrase_cache.cc | 11 +++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/passphrase_cache.cc b/passphrase_cache.cc index 4c5d7cc..fec2d42 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -43,5 +43,26 @@ namespace pEp { entry->tp = clock::now(); _cache.splice(_cache.end(), _cache, entry); } + + std::string PassphraseCache::latest_passphrase() + { + std::lock_guard lock(_mtx); + cleanup(); + _which = _cache.end(); + return ""; + } + + std::string PassphraseCache::next_passphrase() + { + std::lock_guard lock(_mtx); + + if (_cache.empty() || _which == _cache.begin()) { + return ""; + } + else { + --_which; + return _which->passphrase; + } + } }; diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 4a82a3c..70761aa 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -28,6 +28,8 @@ namespace pEp { int _max_size; duration _timeout; + cache::iterator _which; + public: PassphraseCache(int max_size=20, duration timeout = std::chrono::minutes(10)) : _max_size(max_size), @@ -49,6 +51,15 @@ namespace pEp { using passphrase_callee = std::function; bool for_each_passphrase(const passphrase_callee& callee); + // get all passphrases in cache from latest to oldest + // always returns "" + + std::string latest_passphrase(); + + // get next passphrase; returns "" when no passphrases are left + + std::string next_passphrase(); + // convenience functions // i.e. // status = cache.api(::encrypt_message, session, src, extra, dst, enc_format, flags) diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc index 05ce33a..673d063 100644 --- a/test/test_passphrase_cache.cc +++ b/test/test_passphrase_cache.cc @@ -49,6 +49,15 @@ int main() status = cache.api(api_test2, session, n, str, bytes, sl); assert(status == PEP_STATUS_OK); + std::cout << "expected: two passphrases in order\n"; + + pEp::PassphraseCache _cache = cache; + _cache.latest_passphrase(); + for (std::string passphrase = _cache.next_passphrase(); passphrase != "" ; + passphrase = _cache.next_passphrase()) { + std::cout << passphrase << "\n"; + } + sleep(2); std::cout << "expected: no passphrase\n"; @@ -59,8 +68,6 @@ int main() status = cache.api(api_test2, session, 23, str, bytes, sl); assert(status == PEP_STATUS_OK); - pEp::PassphraseCache _cache = cache; - ::release(session); return 0; }