diff --git a/passphrase_cache.cc b/passphrase_cache.cc index fd436fd..1be70b7 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -10,13 +10,14 @@ namespace pEp { { } PassphraseCache::PassphraseCache(size_t max_size, duration timeout) : - _max_size{max_size}, _timeout{timeout}, first_time(true) + _max_size{max_size}, _timeout{timeout}, _which(_cache.end()), + first_time(true) { } PassphraseCache::PassphraseCache(const PassphraseCache& second) : _cache{second._cache}, _max_size{second._max_size}, _timeout{second._timeout}, _stored{second._stored}, - first_time(true) + _which(_cache.end()), first_time(true) { cleanup(); } @@ -94,28 +95,28 @@ namespace pEp { _cache.splice(_cache.end(), _cache, entry); } - const char *PassphraseCache::latest_passphrase() + const char *PassphraseCache::latest_passphrase(PassphraseCache& c) { - if (first_time) { - cleanup(); - _which = _cache.end(); - first_time = false; - if (!_stored.empty()) - return _stored.c_str(); + if (c.first_time) { + c.cleanup(); + c._which = c._cache.end(); + c.first_time = false; + if (!c._stored.empty()) + return c._stored.c_str(); } - if (_cache.empty()) { - first_time = true; + if (c._cache.empty()) { + c.first_time = true; throw Empty(); } - if (_which == _cache.begin()) { - first_time = true; + if (c._which == c._cache.begin()) { + c.first_time = true; throw Exhausted(); } - --_which; - return _which->passphrase.c_str(); + --c._which; + return c._which->passphrase.c_str(); } PEP_STATUS PassphraseCache::config_next_passphrase(bool reset) @@ -134,7 +135,7 @@ namespace pEp { } try { - ::config_passphrase(Adapter::session(), _copy.latest_passphrase()); + ::config_passphrase(Adapter::session(), latest_passphrase(_copy)); return PEP_STATUS_OK; } catch (pEp::PassphraseCache::Empty&) { diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 345c394..205d1ea 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -70,12 +70,13 @@ namespace pEp { template PEP_STATUS api(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a); + static const char *latest_passphrase(PassphraseCache& _cache); + using passphrase_callee = std::function; + bool for_each_passphrase(const passphrase_callee& callee); + protected: void cleanup(); void refresh(cache::iterator entry); - const char *latest_passphrase(); - using passphrase_callee = std::function; - bool for_each_passphrase(const passphrase_callee& callee); }; extern PassphraseCache passphrase_cache; diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc index e6626f7..a94aa62 100644 --- a/test/test_passphrase_cache.cc +++ b/test/test_passphrase_cache.cc @@ -56,14 +56,16 @@ int main() pEp::PassphraseCache _cache = cache; try { while (1) { - std::cout << "'" << _cache.latest_passphrase() << "'\n"; + std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n"; } } catch (std::underflow_error&) { } + pEp::passphrase_cache.add("hello"); + pEp::passphrase_cache.add("world"); std::cout << "two times PEP_STATUS_OK (0), one time PEP_WRONG_PASSPHRASE (2561)\n"; do { - status = pEp::PassphraseCache::messageToSend(cache, session); + status = pEp::PassphraseCache::config_next_passphrase(); std::cout << pEp::status_to_string(status) << " (" << status << ")\n"; } while (status == PEP_STATUS_OK);