Browse Source

always try empty passphrase first

synchronous
Volker Birk 5 years ago
parent
commit
a94ba173e9
  1. 3
      passphrase_cache.cc
  2. 1
      passphrase_cache.hh
  3. 8
      test/test_passphrase_cache.cc

3
passphrase_cache.cc

@ -14,6 +14,9 @@ namespace pEp {
std::lock_guard<std::mutex> lock(_mtx); std::lock_guard<std::mutex> lock(_mtx);
cleanup(); cleanup();
if (callee(""))
return true;
for (auto entry=_cache.begin(); entry!=_cache.end(); ++entry) { for (auto entry=_cache.begin(); entry!=_cache.end(); ++entry) {
if (callee(entry->passphrase)) { if (callee(entry->passphrase)) {
refresh(entry); refresh(entry);

1
passphrase_cache.hh

@ -35,6 +35,7 @@ namespace pEp {
// for each passphrase call the callee until it returns true for a // for each passphrase call the callee until it returns true for a
// matching passphrase or no passphrases are left // matching passphrase or no passphrases are left
// always tests empty passphrase first
// returns true if a passphrase was matching, false otherwise // returns true if a passphrase was matching, false otherwise
using passphrase_callee = std::function<bool(std::string)>; using passphrase_callee = std::function<bool(std::string)>;
bool for_each_passphrase(const passphrase_callee& callee); bool for_each_passphrase(const passphrase_callee& callee);

8
test/test_passphrase_cache.cc

@ -12,18 +12,18 @@ int main()
cache.add("world"); cache.add("world");
std::cout << "expected: two passphrases\n"; std::cout << "expected: two passphrases\n";
cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return false;}); cache.for_each_passphrase([&](std::string passphrase){std::cout << "'" << passphrase << "'\n"; return false;});
std::cout << "expected: one passphrase\n"; std::cout << "expected: one passphrase\n";
cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return true;}); cache.for_each_passphrase([&](std::string passphrase){std::cout << "'" << passphrase << "'\n"; return passphrase != "";});
std::cout << "expected: two passphrases but reverse order\n"; std::cout << "expected: two passphrases but reverse order\n";
cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return false;}); cache.for_each_passphrase([&](std::string passphrase){std::cout << "'" << passphrase << "'\n"; return false;});
sleep(2); sleep(2);
std::cout << "expected: no passphrase\n"; std::cout << "expected: no passphrase\n";
cache.for_each_passphrase([&](std::string passphrase){std::cout << passphrase << "\n"; return false;}); cache.for_each_passphrase([&](std::string passphrase){std::cout << "'" << passphrase << "'\n"; return false;});
return 0; return 0;
} }

Loading…
Cancel
Save