Browse Source

add passphrase_cache test

synchronous
Volker Birk 5 years ago
parent
commit
20e47d4759
  1. 11
      passphrase_cache.cc
  2. 2
      passphrase_cache.hh
  3. 4
      test/Makefile
  4. 26
      test/test_passphrase_cache.cc

11
passphrase_cache.cc

@ -4,11 +4,10 @@ namespace pEp {
void PassphraseCache::add(std::string passphrase) void PassphraseCache::add(std::string passphrase)
{ {
std::lock_guard<std::mutex> lock(_mtx); std::lock_guard<std::mutex> lock(_mtx);
cleanup();
_cache.push_back({passphrase, clock::now()}); _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<std::mutex> lock(_mtx); std::lock_guard<std::mutex> lock(_mtx);
cleanup(); cleanup();
@ -23,12 +22,8 @@ namespace pEp {
void PassphraseCache::cleanup() void PassphraseCache::cleanup()
{ {
for (auto entry = _cache.begin(); entry != _cache.end(); ) { while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout)
if (entry->tp < clock::now() - _timeout) _cache.pop_front();
_cache.erase(entry);
else
break;
}
} }
}; };

2
passphrase_cache.hh

@ -35,7 +35,7 @@ namespace pEp {
// matching passphrase or no passphrases are left // matching passphrase or no passphrases are left
// 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(passphrase_callee& callee); bool for_each_passphrase(const passphrase_callee& callee);
protected: protected:
void cleanup(); void cleanup();

4
test/Makefile

@ -6,7 +6,7 @@ CXXFLAGS+=-I../
.PHONY=all, test_adapter, test_adapter_cxx, test_library .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 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_library: test_library.cc ../libpEpAdapter.a
test_passphrase_cache: test_passphrase_cache.cc ../libpEpAdapter.a
clean: clean:
rm -vf test_adapter rm -vf test_adapter
rm -rvf test_adapter.dSYM rm -rvf test_adapter.dSYM

26
test/test_passphrase_cache.cc

@ -0,0 +1,26 @@
#include <iostream>
#include <unistd.h>
#include <assert.h>
#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;
}
Loading…
Cancel
Save