make it testable Release_2.1.0-RC9
authorVolker Birk <vb@pep-project.org>
Sun, 05 Jul 2020 02:27:49 +0200
changeset 278e4758952329d
parent 277 e729ecd07115
child 279 b824f9f85814
make it testable
passphrase_cache.cc
passphrase_cache.hh
test/test_passphrase_cache.cc
     1.1 --- a/passphrase_cache.cc	Sun Jul 05 01:45:46 2020 +0200
     1.2 +++ b/passphrase_cache.cc	Sun Jul 05 02:27:49 2020 +0200
     1.3 @@ -10,13 +10,14 @@
     1.4      { }
     1.5  
     1.6      PassphraseCache::PassphraseCache(size_t max_size, duration timeout) :
     1.7 -            _max_size{max_size}, _timeout{timeout}, first_time(true)
     1.8 +            _max_size{max_size}, _timeout{timeout}, _which(_cache.end()),
     1.9 +            first_time(true)
    1.10      { }
    1.11  
    1.12      PassphraseCache::PassphraseCache(const PassphraseCache& second) :
    1.13              _cache{second._cache}, _max_size{second._max_size},
    1.14              _timeout{second._timeout}, _stored{second._stored},
    1.15 -            first_time(true)
    1.16 +            _which(_cache.end()), first_time(true)
    1.17      {
    1.18          cleanup();
    1.19      }
    1.20 @@ -94,28 +95,28 @@
    1.21          _cache.splice(_cache.end(), _cache, entry);
    1.22      }
    1.23  
    1.24 -    const char *PassphraseCache::latest_passphrase()
    1.25 +    const char *PassphraseCache::latest_passphrase(PassphraseCache& c)
    1.26      {
    1.27 -        if (first_time) {
    1.28 -            cleanup();
    1.29 -            _which = _cache.end();
    1.30 -            first_time = false;
    1.31 -            if (!_stored.empty())
    1.32 -                return _stored.c_str();
    1.33 +        if (c.first_time) {
    1.34 +            c.cleanup();
    1.35 +            c._which = c._cache.end();
    1.36 +            c.first_time = false;
    1.37 +            if (!c._stored.empty())
    1.38 +                return c._stored.c_str();
    1.39          }
    1.40  
    1.41 -        if (_cache.empty()) {
    1.42 -            first_time = true;
    1.43 +        if (c._cache.empty()) {
    1.44 +            c.first_time = true;
    1.45              throw Empty();
    1.46          }
    1.47  
    1.48 -        if (_which == _cache.begin()) {
    1.49 -            first_time = true;
    1.50 +        if (c._which == c._cache.begin()) {
    1.51 +            c.first_time = true;
    1.52              throw Exhausted();
    1.53          }
    1.54  
    1.55 -        --_which;
    1.56 -        return _which->passphrase.c_str();
    1.57 +        --c._which;
    1.58 +        return c._which->passphrase.c_str();
    1.59      }
    1.60  
    1.61  	PEP_STATUS PassphraseCache::config_next_passphrase(bool reset)
    1.62 @@ -134,7 +135,7 @@
    1.63          }
    1.64  
    1.65          try {
    1.66 -            ::config_passphrase(Adapter::session(), _copy.latest_passphrase());
    1.67 +            ::config_passphrase(Adapter::session(), latest_passphrase(_copy));
    1.68              return PEP_STATUS_OK;
    1.69          }
    1.70          catch (pEp::PassphraseCache::Empty&) {
     2.1 --- a/passphrase_cache.hh	Sun Jul 05 01:45:46 2020 +0200
     2.2 +++ b/passphrase_cache.hh	Sun Jul 05 02:27:49 2020 +0200
     2.3 @@ -70,12 +70,13 @@
     2.4          template<typename... A> PEP_STATUS api(PEP_STATUS f(PEP_SESSION, A...),
     2.5                  PEP_SESSION session, A... a);
     2.6  
     2.7 +        static const char *latest_passphrase(PassphraseCache& _cache);
     2.8 +        using passphrase_callee = std::function<bool(std::string)>;
     2.9 +        bool for_each_passphrase(const passphrase_callee& callee);
    2.10 +
    2.11      protected:
    2.12          void cleanup();
    2.13          void refresh(cache::iterator entry);
    2.14 -        const char *latest_passphrase();
    2.15 -        using passphrase_callee = std::function<bool(std::string)>;
    2.16 -        bool for_each_passphrase(const passphrase_callee& callee);
    2.17      };
    2.18  
    2.19      extern PassphraseCache passphrase_cache;
     3.1 --- a/test/test_passphrase_cache.cc	Sun Jul 05 01:45:46 2020 +0200
     3.2 +++ b/test/test_passphrase_cache.cc	Sun Jul 05 02:27:49 2020 +0200
     3.3 @@ -56,14 +56,16 @@
     3.4      pEp::PassphraseCache _cache = cache;
     3.5      try {
     3.6          while (1) {
     3.7 -            std::cout << "'" << _cache.latest_passphrase() << "'\n";
     3.8 +            std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n";
     3.9          }
    3.10      }
    3.11      catch (std::underflow_error&) { }
    3.12  
    3.13 +    pEp::passphrase_cache.add("hello");
    3.14 +    pEp::passphrase_cache.add("world");
    3.15      std::cout << "two times PEP_STATUS_OK (0), one time PEP_WRONG_PASSPHRASE (2561)\n";
    3.16      do {
    3.17 -        status = pEp::PassphraseCache::messageToSend(cache, session);
    3.18 +        status = pEp::PassphraseCache::config_next_passphrase();
    3.19          std::cout << pEp::status_to_string(status) << " (" << status << ")\n";
    3.20      } while (status == PEP_STATUS_OK);
    3.21