Browse Source

make it testable

synchronous Release_2.1.0-RC9
Volker Birk 5 years ago
parent
commit
89367957ef
  1. 33
      passphrase_cache.cc
  2. 7
      passphrase_cache.hh
  3. 6
      test/test_passphrase_cache.cc

33
passphrase_cache.cc

@ -10,13 +10,14 @@ namespace pEp {
{ } { }
PassphraseCache::PassphraseCache(size_t max_size, duration timeout) : 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) : PassphraseCache::PassphraseCache(const PassphraseCache& second) :
_cache{second._cache}, _max_size{second._max_size}, _cache{second._cache}, _max_size{second._max_size},
_timeout{second._timeout}, _stored{second._stored}, _timeout{second._timeout}, _stored{second._stored},
first_time(true) _which(_cache.end()), first_time(true)
{ {
cleanup(); cleanup();
} }
@ -94,28 +95,28 @@ namespace pEp {
_cache.splice(_cache.end(), _cache, entry); _cache.splice(_cache.end(), _cache, entry);
} }
const char *PassphraseCache::latest_passphrase() const char *PassphraseCache::latest_passphrase(PassphraseCache& c)
{ {
if (first_time) { if (c.first_time) {
cleanup(); c.cleanup();
_which = _cache.end(); c._which = c._cache.end();
first_time = false; c.first_time = false;
if (!_stored.empty()) if (!c._stored.empty())
return _stored.c_str(); return c._stored.c_str();
} }
if (_cache.empty()) { if (c._cache.empty()) {
first_time = true; c.first_time = true;
throw Empty(); throw Empty();
} }
if (_which == _cache.begin()) { if (c._which == c._cache.begin()) {
first_time = true; c.first_time = true;
throw Exhausted(); throw Exhausted();
} }
--_which; --c._which;
return _which->passphrase.c_str(); return c._which->passphrase.c_str();
} }
PEP_STATUS PassphraseCache::config_next_passphrase(bool reset) PEP_STATUS PassphraseCache::config_next_passphrase(bool reset)
@ -134,7 +135,7 @@ namespace pEp {
} }
try { try {
::config_passphrase(Adapter::session(), _copy.latest_passphrase()); ::config_passphrase(Adapter::session(), latest_passphrase(_copy));
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
catch (pEp::PassphraseCache::Empty&) { catch (pEp::PassphraseCache::Empty&) {

7
passphrase_cache.hh

@ -70,12 +70,13 @@ namespace pEp {
template<typename... A> PEP_STATUS api(PEP_STATUS f(PEP_SESSION, A...), template<typename... A> PEP_STATUS api(PEP_STATUS f(PEP_SESSION, A...),
PEP_SESSION session, A... a); PEP_SESSION session, A... a);
static const char *latest_passphrase(PassphraseCache& _cache);
using passphrase_callee = std::function<bool(std::string)>;
bool for_each_passphrase(const passphrase_callee& callee);
protected: protected:
void cleanup(); void cleanup();
void refresh(cache::iterator entry); void refresh(cache::iterator entry);
const char *latest_passphrase();
using passphrase_callee = std::function<bool(std::string)>;
bool for_each_passphrase(const passphrase_callee& callee);
}; };
extern PassphraseCache passphrase_cache; extern PassphraseCache passphrase_cache;

6
test/test_passphrase_cache.cc

@ -56,14 +56,16 @@ int main()
pEp::PassphraseCache _cache = cache; pEp::PassphraseCache _cache = cache;
try { try {
while (1) { while (1) {
std::cout << "'" << _cache.latest_passphrase() << "'\n"; std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n";
} }
} }
catch (std::underflow_error&) { } 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"; std::cout << "two times PEP_STATUS_OK (0), one time PEP_WRONG_PASSPHRASE (2561)\n";
do { do {
status = pEp::PassphraseCache::messageToSend(cache, session); status = pEp::PassphraseCache::config_next_passphrase();
std::cout << pEp::status_to_string(status) << " (" << status << ")\n"; std::cout << pEp::status_to_string(status) << " (" << status << ")\n";
} while (status == PEP_STATUS_OK); } while (status == PEP_STATUS_OK);

Loading…
Cancel
Save