diff --git a/Makefile b/Makefile index d9848f8..1014831 100644 --- a/Makefile +++ b/Makefile @@ -43,13 +43,14 @@ lib: $(TARGET) all: lib test: lib - $(MAKE) -C test all + $(MAKE) -C test $(TARGET): $(OBJECTS) $(AR) -rc $@ $^ clean: rm -vf $(TARGET) $(OBJECTS) $(DEPENDS) + rm -f *.d.* $(MAKE) -C test clean install: $(TARGET) diff --git a/test/Makefile b/test/Makefile index df63afa..eb7066c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,8 +9,9 @@ TST=$(subst .cc,,$(SRC)) all: $(TST) -%: %.cc ../libpEpAdapter.a +$(TST): framework.o clean: rm -f $(TST) rm -Rf *.dSYM + rm -f *.o diff --git a/test/framework.cc b/test/framework.cc new file mode 100644 index 0000000..de3a76e --- /dev/null +++ b/test/framework.cc @@ -0,0 +1,42 @@ +#include "framework.hh" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "Adapter.hh" + +namespace pEp { + namespace Test { + using namespace Adapter; + + void setup() + { + char path[MAXPATHLEN+1]; + const char *templ = "/tmp/test_pEp.XXXXXXXXXXXX"; + strcpy(path, templ); + char *tmpdir = mkdtemp(path); + assert(tmpdir); + chdir(tmpdir); + setenv("HOME", path, 1); + cerr << "test directory: " << path << endl; + + } + + void import_key_from_file(string filename) + { + ifstream f(filename, ifstream::in); + string key{istreambuf_iterator(f), istreambuf_iterator()}; + ::identity_list *il = NULL; + PEP_STATUS status = ::import_key(session(), key.c_str(), key.length(), &il); + assert(status == PEP_KEY_IMPORTED); + ::free_identity_list(il); + } + }; +}; diff --git a/test/framework.hh b/test/framework.hh new file mode 100644 index 0000000..48af82d --- /dev/null +++ b/test/framework.hh @@ -0,0 +1,14 @@ +#pragma once + +#include +#include "Adapter.hh" + +namespace pEp { + namespace Test { + using namespace std; + + void setup(); + void import_key_from_file(string filename); + }; +}; + diff --git a/test/test_adapter.cc b/test/test_adapter.cc index 45e5c96..a0c90f2 100644 --- a/test/test_adapter.cc +++ b/test/test_adapter.cc @@ -1,7 +1,7 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#include "Adapter.hh" +#include "framework.hh" #include #include #include @@ -26,14 +26,7 @@ PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handsha int main() { - char path[MAXPATHLEN+1]; - const char *templ = "/tmp/test_adapter.XXXXXXXXXXXX"; - strcpy(path, templ); - char *tmpdir = mkdtemp(path); - assert(tmpdir); - chdir(tmpdir); - setenv("HOME", path, 1); - cerr << "test directory: " << path << endl; + pEp::Test::setup(); // Create new identity pEpLog("updating or creating identity for me"); diff --git a/test/test_adapter_cxx.cc b/test/test_adapter_cxx.cc index fd33314..02a5c22 100644 --- a/test/test_adapter_cxx.cc +++ b/test/test_adapter_cxx.cc @@ -1,7 +1,7 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#include "Adapter.hh" +#include "framework.hh" #include #include #include @@ -32,6 +32,8 @@ public: } o; int main() { + pEp::Test::setup(); + // Create new identity pEpLog("updating or creating identity for me"); pEp_identity *me = new_identity("alice@peptest.ch", NULL, "23", "Who the F* is Alice"); diff --git a/test/test_ensure_passphrase.cc b/test/test_ensure_passphrase.cc index 9de9690..f0e48ee 100644 --- a/test/test_ensure_passphrase.cc +++ b/test/test_ensure_passphrase.cc @@ -7,7 +7,8 @@ #include #include -#include "Adapter.hh" +#include "framework.hh" + #include "passphrase_cache.hh" #include "status_to_string.hh" @@ -19,18 +20,10 @@ using namespace pEp; using namespace pEp::Adapter; using namespace std; -void import_key_from_file(string filename) +int main() { - ifstream f(filename, ifstream::in); - string key{istreambuf_iterator(f), istreambuf_iterator()}; - ::identity_list *il = NULL; - PEP_STATUS status = ::import_key(session(), key.c_str(), key.length(), &il); - assert(status == PEP_KEY_IMPORTED); - ::free_identity_list(il); -} + Test::setup(); -void test() -{ passphrase_cache.add("erwin"); passphrase_cache.add("cathy"); passphrase_cache.add("bob"); @@ -41,8 +34,8 @@ void test() const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp"; const char* erwin_fpr = "CBA968BC01FCEB89F04CCF155C5E9E3F0420A570"; - import_key_from_file(bob_filename); - import_key_from_file(erwin_filename); + Test::import_key_from_file(bob_filename); + Test::import_key_from_file(erwin_filename); pEp_identity* bob = ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog"); PEP_STATUS status = ::set_own_key(session(), bob, bob_fpr); @@ -59,20 +52,7 @@ void test() ::free_identity(erwin); session(Adapter::release); -} -int main() -{ - char path[MAXPATHLEN+1]; - const char *templ = "/tmp/test_ensure_passphrase.XXXXXXXXXXXX"; - strcpy(path, templ); - char *tmpdir = mkdtemp(path); - assert(tmpdir); - chdir(tmpdir); - setenv("HOME", path, 1); - cerr << "test directory: " << path << endl; - - test(); return 0; } diff --git a/test/test_message_cache.cc b/test/test_message_cache.cc index 2c0b8ef..f5592c1 100644 --- a/test/test_message_cache.cc +++ b/test/test_message_cache.cc @@ -3,21 +3,14 @@ #include #include #include "message_cache.hh" -#include "Adapter.hh" +#include "framework.hh" using namespace std; using namespace pEp; int main() { - char path[MAXPATHLEN+1]; - const char *templ = "/tmp/test_message_cache.XXXXXXXXXXXX"; - strcpy(path, templ); - char *tmpdir = mkdtemp(path); - assert(tmpdir); - chdir(tmpdir); - setenv("HOME", path, 1); - cerr << "test directory: " << path << endl; + Test::setup(); pEp_identity *alice = ::new_identity("alice@mail.com", nullptr, PEP_OWN_USERID, "Alice"); ::myself(pEp::Adapter::session(), alice); diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc index 4d51d75..96ad354 100644 --- a/test/test_passphrase_cache.cc +++ b/test/test_passphrase_cache.cc @@ -3,7 +3,7 @@ #include #include -#include "Adapter.hh" +#include "framework.hh" #include "passphrase_cache.hh" #include "status_to_string.hh" @@ -23,14 +23,7 @@ extern "C" { int main() { - char path[MAXPATHLEN+1]; - const char *templ = "/tmp/test_passphrase_cache.XXXXXXXXXXXX"; - strcpy(path, templ); - char *tmpdir = mkdtemp(path); - assert(tmpdir); - chdir(tmpdir); - setenv("HOME", path, 1); - std::cerr << "test directory: " << path << std::endl; + pEp::Test::setup(); const char *str = "23"; char *bytes = NULL;