merging in ENGINE-781 Release_2.1.0-RC23
authorVolker Birk <vb@pep-project.org>
Wed, 05 Aug 2020 21:42:47 +0200
changeset 341acced2f7f306
parent 334 b51c168a86c0
parent 340 907edf4c6966
child 342 98084432b9f7
merging in ENGINE-781
     1.1 --- a/Adapter.cc	Tue Aug 04 09:47:06 2020 +0200
     1.2 +++ b/Adapter.cc	Wed Aug 05 21:42:47 2020 +0200
     1.3 @@ -7,6 +7,7 @@
     1.4  #include <assert.h>
     1.5  #include "status_to_string.hh"
     1.6  #include "pEpLog.hh"
     1.7 +#include "passphrase_cache.hh"
     1.8  
     1.9  using namespace std;
    1.10  
    1.11 @@ -82,6 +83,11 @@
    1.12              return 0;
    1.13          }
    1.14  
    1.15 +        PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr)
    1.16 +        {
    1.17 +            return passphrase_cache.ensure_passphrase(session, fpr);
    1.18 +        }
    1.19 +
    1.20          // threshold: max waiting time in seconds
    1.21          SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold)
    1.22          {
    1.23 @@ -120,7 +126,7 @@
    1.24  
    1.25                  case init:
    1.26                      if (!_session)
    1.27 -                        status = ::init(&_session, _messageToSend, _inject_sync_event);
    1.28 +                        status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase);
    1.29                      break;
    1.30  
    1.31                  default:
     2.1 --- a/Adapter.hh	Tue Aug 04 09:47:06 2020 +0200
     2.2 +++ b/Adapter.hh	Wed Aug 05 21:42:47 2020 +0200
     2.3 @@ -24,6 +24,7 @@
     2.4  
     2.5      namespace Adapter {
     2.6          int _inject_sync_event(SYNC_EVENT ev, void *management);
     2.7 +        PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr);
     2.8  
     2.9          template<class T = void>
    2.10          void startup(
     3.1 --- a/Makefile	Tue Aug 04 09:47:06 2020 +0200
     3.2 +++ b/Makefile	Wed Aug 05 21:42:47 2020 +0200
     3.3 @@ -28,8 +28,6 @@
     3.4  
     3.5  all: $(TARGET)
     3.6  
     3.7 --include Makefile.protocols
     3.8 -
     3.9  %.d: %.cc
    3.10  	@set -e; rm -f $@; \
    3.11  	$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< > $@.$$$$; \
     4.1 --- a/Makefile.conf	Tue Aug 04 09:47:06 2020 +0200
     4.2 +++ b/Makefile.conf	Wed Aug 05 21:42:47 2020 +0200
     4.3 @@ -6,8 +6,8 @@
     4.4  HERE:=$(dir $(lastword $(MAKEFILE_LIST)))
     4.5  
     4.6  
     4.7 -PREFIX=$(HOME)
     4.8 -CXXFLAGS=-std=c++11 -fPIC -O0 $(ENGINE_INC)
     4.9 +PREFIX?=$(HOME)
    4.10 +CXXFLAGS+=-std=c++11 -fPIC -O0
    4.11  
    4.12  # Build target
    4.13  BUILD_FOR:=$(shell uname)
    4.14 @@ -29,8 +29,8 @@
    4.15  endif
    4.16  
    4.17  ######### Engine #########
    4.18 -ENGINE_LIB_PATH=$(HOME)/lib
    4.19 -ENGINE_INC_PATH=$(HOME)/include
    4.20 +ENGINE_LIB_PATH=$(PREFIX)/lib
    4.21 +ENGINE_INC_PATH=$(PREFIX)/include
    4.22  
    4.23  ######### Overrides #########
    4.24  -include $(HERE)/local.conf
    4.25 @@ -47,3 +47,9 @@
    4.26  ifdef BUILD_CONFIG
    4.27      include $(BUILD_CONFIG)
    4.28  endif
    4.29 +
    4.30 +ifndef ENGINE_TEST
    4.31 +	ENGINE_TEST = \"$(HOME)/dev/pEpEngine/test\"
    4.32 +endif
    4.33 +
    4.34 +CXXFLAGS += $(ENGINE_INC)
     5.1 --- a/passphrase_cache.cc	Tue Aug 04 09:47:06 2020 +0200
     5.2 +++ b/passphrase_cache.cc	Wed Aug 05 21:42:47 2020 +0200
     5.3 @@ -153,5 +153,21 @@
     5.4              return PEP_WRONG_PASSPHRASE;
     5.5          }
     5.6  	}
     5.7 +
     5.8 +    PEP_STATUS PassphraseCache::ensure_passphrase(PEP_SESSION session, std::string fpr)
     5.9 +    {
    5.10 +        PEP_STATUS status;
    5.11 +
    5.12 +        for_each_passphrase([&](std::string passphrase) {
    5.13 +            status = ::config_passphrase(session, passphrase.c_str());
    5.14 +            if (status)
    5.15 +                return true;
    5.16 +
    5.17 +            status = ::probe_encrypt(session, fpr.c_str());
    5.18 +            return status == PEP_STATUS_OK;
    5.19 +        });
    5.20 +
    5.21 +        return status;
    5.22 +    }
    5.23  };
    5.24  
     6.1 --- a/passphrase_cache.hh	Tue Aug 04 09:47:06 2020 +0200
     6.2 +++ b/passphrase_cache.hh	Wed Aug 05 21:42:47 2020 +0200
     6.3 @@ -73,6 +73,7 @@
     6.4          static const char *latest_passphrase(PassphraseCache& _cache);
     6.5          using passphrase_callee = std::function<bool(std::string)>;
     6.6          bool for_each_passphrase(const passphrase_callee& callee);
     6.7 +        PEP_STATUS ensure_passphrase(PEP_SESSION session, std::string fpr);
     6.8  
     6.9      protected:
    6.10          void cleanup();
     7.1 --- a/passphrase_cache.hxx	Tue Aug 04 09:47:06 2020 +0200
     7.2 +++ b/passphrase_cache.hxx	Wed Aug 05 21:42:47 2020 +0200
     7.3 @@ -1,4 +1,3 @@
     7.4 -#pragma once
     7.5  #include "passphrase_cache.hh"
     7.6  
     7.7  namespace pEp {
     8.1 --- a/test/Makefile	Tue Aug 04 09:47:06 2020 +0200
     8.2 +++ b/test/Makefile	Wed Aug 05 21:42:47 2020 +0200
     8.3 @@ -2,28 +2,15 @@
     8.4  
     8.5  LDFLAGS=-L../ $(ENGINE_LIB)
     8.6  LDLIBS=-lstdc++ -lpEpEngine -lpEpAdapter
     8.7 -CXXFLAGS+=-I../
     8.8 +CXXFLAGS+=-I../ $(ENGINE_INC) -std=c++11 -DENGINE_TEST=$(ENGINE_TEST)
     8.9  
    8.10 -.PHONY=all, test_adapter, test_adapter_cxx, test_library
    8.11 +SRC=$(wildcard test_*.cc)
    8.12 +TST=$(subst .cc,,$(SRC))
    8.13  
    8.14 -all: test_adapter test_adapter_cxx test_library test_passphrase_cache test_semaphore test_message_cache
    8.15 +all: $(TST)
    8.16  
    8.17 -test_adapter: test_adapter.cc ../libpEpAdapter.a
    8.18 -
    8.19 -test_adapter_cxx: test_adapter_cxx.cc ../libpEpAdapter.a
    8.20 -
    8.21 -test_library: test_library.cc ../libpEpAdapter.a
    8.22 -
    8.23 -test_passphrase_cache: test_passphrase_cache.cc ../libpEpAdapter.a
    8.24 -
    8.25 -test_semaphore: test_semaphore.cc ../libpEpAdapter.a
    8.26 -
    8.27 -test_message_cache: test_message_cache.cc ../libpEpAdapter.a
    8.28 +%: %.cc ../libpEpAdapter.a
    8.29  
    8.30  clean:	
    8.31 -	rm -vf test_adapter
    8.32 -	rm -rvf test_adapter.dSYM
    8.33 -	rm -vf test_adapter_cxx
    8.34 -	rm -rvf test_adapter_cxx.dSYM
    8.35 -	rm -vf test_library
    8.36 -	rm -rvf test_library.dSYM
    8.37 +	rm -f $(TST)
    8.38 +	rm -Rf *.dSYM
     9.1 --- a/test/test_adapter.cc	Tue Aug 04 09:47:06 2020 +0200
     9.2 +++ b/test/test_adapter.cc	Wed Aug 05 21:42:47 2020 +0200
     9.3 @@ -5,6 +5,7 @@
     9.4  #include <iostream>
     9.5  #include <assert.h>
     9.6  #include <unistd.h>
     9.7 +#include <sys/param.h>
     9.8  #include <pEp/keymanagement.h>
     9.9  #include "pEpLog.hh"
    9.10  
    9.11 @@ -25,6 +26,15 @@
    9.12  
    9.13  int main()
    9.14  {
    9.15 +    char path[MAXPATHLEN+1];
    9.16 +    const char *templ = "/tmp/test_adapter.XXXXXXXXXXXX";
    9.17 +    strcpy(path, templ);
    9.18 +    char *tmpdir = mkdtemp(path);
    9.19 +    assert(tmpdir);
    9.20 +    chdir(tmpdir);
    9.21 +    setenv("HOME", path, 1);
    9.22 +    cerr << "test directory: " << path << endl;
    9.23 +
    9.24      // Create new identity
    9.25      pEpLog("updating or creating identity for me");
    9.26      pEp_identity *me = new_identity("alice@peptest.ch", NULL, "23", "Who the F* is Alice");
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/test/test_ensure_passphrase.cc	Wed Aug 05 21:42:47 2020 +0200
    10.3 @@ -0,0 +1,78 @@
    10.4 +#include <iostream>
    10.5 +#include <fstream>
    10.6 +#include <sstream>
    10.7 +#include <unistd.h>
    10.8 +#include <assert.h>
    10.9 +#include <stdlib.h>
   10.10 +#include <string.h>
   10.11 +#include <sys/param.h>
   10.12 +
   10.13 +#include "Adapter.hh"
   10.14 +#include "passphrase_cache.hh"
   10.15 +#include "status_to_string.hh"
   10.16 +
   10.17 +#include <pEp/message_api.h>
   10.18 +#include <pEp/keymanagement.h>
   10.19 +#include <pEp/key_reset.h>
   10.20 +
   10.21 +using namespace pEp;
   10.22 +using namespace pEp::Adapter;
   10.23 +using namespace std;
   10.24 +
   10.25 +void import_key_from_file(string filename)
   10.26 +{
   10.27 +    ifstream f(filename, ifstream::in);
   10.28 +    string key{istreambuf_iterator<char>(f), istreambuf_iterator<char>()};
   10.29 +    ::identity_list *il = NULL;
   10.30 +    PEP_STATUS status = ::import_key(session(), key.c_str(), key.length(), &il);
   10.31 +    assert(status == PEP_KEY_IMPORTED);
   10.32 +    ::free_identity_list(il);
   10.33 +}
   10.34 +
   10.35 +void test()
   10.36 +{
   10.37 +    passphrase_cache.add("erwin");
   10.38 +    passphrase_cache.add("cathy");
   10.39 +    passphrase_cache.add("bob");
   10.40 +
   10.41 +    const char* bob_filename = ENGINE_TEST "/test_keys/bob-primary-with-password-bob-subkey-without.pgp";
   10.42 +    const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736";
   10.43 +
   10.44 +    const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp";
   10.45 +    const char* erwin_fpr = "CBA968BC01FCEB89F04CCF155C5E9E3F0420A570";
   10.46 +
   10.47 +    import_key_from_file(bob_filename);
   10.48 +    import_key_from_file(erwin_filename);
   10.49 +
   10.50 +    pEp_identity* bob = ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog");
   10.51 +    PEP_STATUS status = ::set_own_key(session(), bob, bob_fpr);
   10.52 +    assert(status == PEP_STATUS_OK);
   10.53 +
   10.54 +    pEp_identity* erwin = ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin");
   10.55 +    status = ::set_own_key(session(), erwin, erwin_fpr);
   10.56 +    assert(status == PEP_STATUS_OK);
   10.57 +
   10.58 +    status = ::key_reset_all_own_keys(session());
   10.59 +    assert(status == PEP_STATUS_OK);
   10.60 +
   10.61 +    ::free_identity(bob);
   10.62 +    ::free_identity(erwin);
   10.63 +
   10.64 +    session(Adapter::release);
   10.65 +}
   10.66 +
   10.67 +int main()
   10.68 +{
   10.69 +    char path[MAXPATHLEN+1];
   10.70 +    const char *templ = "/tmp/test_ensure_passphrase.XXXXXXXXXXXX";
   10.71 +    strcpy(path, templ);
   10.72 +    char *tmpdir = mkdtemp(path);
   10.73 +    assert(tmpdir);
   10.74 +    chdir(tmpdir);
   10.75 +    setenv("HOME", path, 1);
   10.76 +    cerr << "test directory: " << path << endl;
   10.77 +
   10.78 +    test();
   10.79 +    return 0;
   10.80 +}
   10.81 +
    11.1 --- a/test/test_library.cc	Tue Aug 04 09:47:06 2020 +0200
    11.2 +++ b/test/test_library.cc	Wed Aug 05 21:42:47 2020 +0200
    11.3 @@ -1,6 +1,3 @@
    11.4 -// small unittest program for libpEpAdapter
    11.5 -// TODO: use Gtest or the like for more modular unit tests!
    11.6 -
    11.7  #include "pc_container.hh"
    11.8  #include <cstdio>
    11.9  #include <cstdlib>
    12.1 --- a/test/test_message_cache.cc	Tue Aug 04 09:47:06 2020 +0200
    12.2 +++ b/test/test_message_cache.cc	Wed Aug 05 21:42:47 2020 +0200
    12.3 @@ -1,18 +1,26 @@
    12.4  #include <iostream>
    12.5  #include <cassert>
    12.6 +#include <sys/param.h>
    12.7 +#include <unistd.h>
    12.8  #include "message_cache.hh"
    12.9 +#include "Adapter.hh"
   12.10  
   12.11  using namespace std;
   12.12  using namespace pEp;
   12.13  
   12.14  int main()
   12.15  {
   12.16 -    PEP_SESSION session;
   12.17 -    PEP_STATUS status = ::init(&session, nullptr, nullptr);
   12.18 -    assert(status == PEP_STATUS_OK);
   12.19 +    char path[MAXPATHLEN+1];
   12.20 +    const char *templ = "/tmp/test_message_cache.XXXXXXXXXXXX";
   12.21 +    strcpy(path, templ);
   12.22 +    char *tmpdir = mkdtemp(path);
   12.23 +    assert(tmpdir);
   12.24 +    chdir(tmpdir);
   12.25 +    setenv("HOME", path, 1);
   12.26 +    cerr << "test directory: " << path << endl;
   12.27  
   12.28      pEp_identity *alice = ::new_identity("alice@mail.com", nullptr, PEP_OWN_USERID, "Alice");
   12.29 -    ::myself(session, alice);
   12.30 +    ::myself(pEp::Adapter::session(), alice);
   12.31  
   12.32      char *mime = strdup("From: Alice <alice@mail.com>\n"
   12.33                          "To: Bob <bob@mail.com>\n"
   12.34 @@ -24,20 +32,20 @@
   12.35  
   12.36      ::message *src = nullptr;
   12.37      bool has_possible_pEp_msg;
   12.38 -    status = MessageCache::cache_mime_decode_message(mime, strlen(mime), &src, &has_possible_pEp_msg);
   12.39 +    PEP_STATUS status = MessageCache::cache_mime_decode_message(mime, strlen(mime), &src, &has_possible_pEp_msg);
   12.40      assert(status == PEP_STATUS_OK);
   12.41  
   12.42 -    status = ::myself(session, src->from);
   12.43 +    status = ::myself(pEp::Adapter::session(), src->from);
   12.44      assert(status == PEP_STATUS_OK);
   12.45  
   12.46 -    ::update_identity(session, src->to->ident);
   12.47 +    ::update_identity(pEp::Adapter::session(), src->to->ident);
   12.48      assert(status == PEP_STATUS_OK);
   12.49  
   12.50      pEp_identity *bob = identity_dup(src->to->ident);
   12.51  
   12.52      src->dir = PEP_dir_outgoing;
   12.53      ::message *dst = nullptr;
   12.54 -    status = MessageCache::cache_encrypt_message(session, src, nullptr, &dst, PEP_enc_PEP, 0);
   12.55 +    status = MessageCache::cache_encrypt_message(pEp::Adapter::session(), src, nullptr, &dst, PEP_enc_PEP, 0);
   12.56      assert(status != PEP_ILLEGAL_VALUE);
   12.57  
   12.58      assert(src->longmsg == nullptr);
   12.59 @@ -68,7 +76,7 @@
   12.60      PEP_decrypt_flags_t flags = 0;
   12.61      stringlist_t *keylist = nullptr;
   12.62  
   12.63 -    status = MessageCache::cache_decrypt_message(session, src, &dst, &keylist, &rating, &flags);
   12.64 +    status = MessageCache::cache_decrypt_message(pEp::Adapter::session(), src, &dst, &keylist, &rating, &flags);
   12.65      assert(status != PEP_ILLEGAL_VALUE);
   12.66  
   12.67      assert(src->longmsg == nullptr);
   12.68 @@ -90,7 +98,8 @@
   12.69      ::free_message(dst);
   12.70      ::free_identity(bob);
   12.71      ::free_identity(alice);
   12.72 -    ::release(session);
   12.73 +
   12.74 +    pEp::Adapter::session(pEp::Adapter::release);
   12.75      return 0;
   12.76  }
   12.77  
    13.1 --- a/test/test_passphrase_cache.cc	Tue Aug 04 09:47:06 2020 +0200
    13.2 +++ b/test/test_passphrase_cache.cc	Wed Aug 05 21:42:47 2020 +0200
    13.3 @@ -1,7 +1,9 @@
    13.4  #include <iostream>
    13.5  #include <unistd.h>
    13.6  #include <assert.h>
    13.7 +#include <sys/param.h>
    13.8  
    13.9 +#include "Adapter.hh"
   13.10  #include "passphrase_cache.hh"
   13.11  #include "status_to_string.hh"
   13.12  
   13.13 @@ -21,10 +23,14 @@
   13.14  
   13.15  int main()
   13.16  {
   13.17 -    PEP_SESSION session;
   13.18 -    PEP_STATUS status = ::init(&session, NULL, NULL);
   13.19 -    assert(status == PEP_STATUS_OK);
   13.20 -    assert(session);
   13.21 +    char path[MAXPATHLEN+1];
   13.22 +    const char *templ = "/tmp/test_passphrase_cache.XXXXXXXXXXXX";
   13.23 +    strcpy(path, templ);
   13.24 +    char *tmpdir = mkdtemp(path);
   13.25 +    assert(tmpdir);
   13.26 +    chdir(tmpdir);
   13.27 +    setenv("HOME", path, 1);
   13.28 +    std::cerr << "test directory: " << path << std::endl;
   13.29  
   13.30      const char *str = "23";
   13.31      char *bytes = NULL;
   13.32 @@ -45,9 +51,9 @@
   13.33      std::cout << "expected: two passphrases but reverse order\n";
   13.34      cache.for_each_passphrase([&](std::string passphrase){std::cout << "'" << passphrase << "'\n"; return false;});
   13.35  
   13.36 -    status = cache.api(api_test1, session, "23", bytes, n, (::stringlist_t *) NULL);
   13.37 +    PEP_STATUS status = cache.api(api_test1, pEp::Adapter::session(), "23", bytes, n, (::stringlist_t *) NULL);
   13.38      assert(status == PEP_WRONG_PASSPHRASE);
   13.39 -    status = cache.api(api_test2, session, n, str, bytes, sl);
   13.40 +    status = cache.api(api_test2, pEp::Adapter::session(), n, str, bytes, sl);
   13.41      assert(status == PEP_STATUS_OK);
   13.42  
   13.43      cache.add("hello");
   13.44 @@ -74,12 +80,12 @@
   13.45      std::cout << "expected: no passphrase\n";
   13.46      cache.for_each_passphrase([&](std::string passphrase){std::cout << "'" << passphrase << "'\n"; return false;});
   13.47  
   13.48 -    status = cache.api(api_test1, session, str, bytes, n, sl);
   13.49 +    status = cache.api(api_test1, pEp::Adapter::session(), str, bytes, n, sl);
   13.50      assert(status == PEP_WRONG_PASSPHRASE);
   13.51 -    status = cache.api(api_test2, session, 23, str, bytes, sl);
   13.52 +    status = cache.api(api_test2, pEp::Adapter::session(), 23, str, bytes, sl);
   13.53      assert(status == PEP_STATUS_OK);
   13.54  
   13.55 -    ::release(session);
   13.56 +    pEp::Adapter::session(pEp::Adapter::release);
   13.57      return 0;
   13.58  }
   13.59