...
 
Commits (3)
BasedOnStyle: LLVM BasedOnStyle: LLVM
Language: Cpp Language: Cpp
Standard: c++14
DerivePointerAlignment: true DerivePointerAlignment: true
SortIncludes: false SortIncludes: false
ReflowComments: false ReflowComments: false
PointerAlignment: Left PointerAlignment: Left
AlignAfterOpenBracket: AlwaysBreak AlignAfterOpenBracket: AlwaysBreak
AlignOperands: AlignAfterOperator AlignOperands: AlignAfterOperator
BreakConstructorInitializers: AfterColon
AlignTrailingComments: true AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false AllowAllParametersOfDeclarationOnNextLine: false
...@@ -35,4 +37,5 @@ NamespaceIndentation: All ...@@ -35,4 +37,5 @@ NamespaceIndentation: All
SpaceAfterTemplateKeyword: false SpaceAfterTemplateKeyword: false
AccessModifierOffset: -4 AccessModifierOffset: -4
AllowShortBlocksOnASingleLine: Always AllowShortBlocksOnASingleLine: Always
IndentPPDirectives: BeforeHash IndentPPDirectives: BeforeHash
\ No newline at end of file Cpp11BracedListStyle: false
\ No newline at end of file
...@@ -2,8 +2,7 @@ include ../Makefile.conf ...@@ -2,8 +2,7 @@ include ../Makefile.conf
LDFLAGS=-L../src $(ENGINE_LIB) LDFLAGS=-L../src $(ENGINE_LIB)
LDLIBS=-lstdc++ -lpEpEngine -lpEpAdapter LDLIBS=-lstdc++ -lpEpEngine -lpEpAdapter
CXXFLAGS+=-I../src $(ENGINE_INC) -std=c++11 -DENGINE_TEST=$(ENGINE_TEST) CXXFLAGS:=-I../src -DENGINE_TEST=$(ENGINE_TEST) $(CXXFLAGS)
SRC=$(wildcard test_*.cc) SRC=$(wildcard test_*.cc)
TST=$(subst .cc,,$(SRC)) TST=$(subst .cc,,$(SRC))
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include <pEp/sync_codec.h> #include <pEp/sync_codec.h>
#include <pEp/distribution_codec.h> #include <pEp/distribution_codec.h>
#include <Adapter.hh>
pEp::Test::Transport pEp::Test::transport; pEp::Test::Transport pEp::Test::transport;
std::string pEp::Test::path; std::string pEp::Test::path;
extern std::thread pEp::Adapter::_sync_thread; extern std::thread pEp::Adapter::_sync_thread;
...@@ -28,7 +30,7 @@ namespace pEp { ...@@ -28,7 +30,7 @@ namespace pEp {
namespace Test { namespace Test {
using namespace Adapter; using namespace Adapter;
void setup(vector<string>& args) void setup(vector<string> &args)
{ {
#ifdef WIN32 #ifdef WIN32
string dir = getenv("TEMP"); string dir = getenv("TEMP");
...@@ -45,17 +47,15 @@ namespace pEp { ...@@ -45,17 +47,15 @@ namespace pEp {
cout << "usage: " << args[0] << " [--dir HOME]" << endl; cout << "usage: " << args[0] << " [--dir HOME]" << endl;
#endif #endif
exit(0); exit(0);
} } else if (args[1] == "--dir" && args.size() == 3) {
else if (args[1] == "--dir" && args.size() == 3) {
dir = args[2]; dir = args[2];
} } else {
else {
cerr << "illegal parameter" << endl; cerr << "illegal parameter" << endl;
exit(1); exit(1);
} }
} }
char _path[MAXPATHLEN+1]; char _path[MAXPATHLEN + 1];
const char *templ = dir.c_str(); const char *templ = dir.c_str();
strcpy(_path, templ); strcpy(_path, templ);
mkdtemp(_path); mkdtemp(_path);
...@@ -71,8 +71,8 @@ namespace pEp { ...@@ -71,8 +71,8 @@ namespace pEp {
void setup(int argc, char **argv) void setup(int argc, char **argv)
{ {
vector<string> args{(size_t) argc}; vector<string> args{ (size_t)argc };
for (int i=0; i<argc; ++i) for (int i = 0; i < argc; ++i)
args[i] = argv[i]; args[i] = argv[i];
setup(args); setup(args);
...@@ -81,9 +81,9 @@ namespace pEp { ...@@ -81,9 +81,9 @@ namespace pEp {
void import_key_from_file(string filename) void import_key_from_file(string filename)
{ {
ifstream f(filename, ifstream::in); ifstream f(filename, ifstream::in);
string key{istreambuf_iterator<char>(f), istreambuf_iterator<char>()}; string key{ istreambuf_iterator<char>(f), istreambuf_iterator<char>() };
::identity_list *il = NULL; ::identity_list *il = NULL;
PEP_STATUS status = ::import_key(session(), key.c_str(), key.length(), &il); ::PEP_STATUS status = ::import_key(session(), key.c_str(), key.length(), &il);
assert(status == PEP_KEY_IMPORTED); assert(status == PEP_KEY_IMPORTED);
::free_identity_list(il); ::free_identity_list(il);
} }
...@@ -95,14 +95,18 @@ namespace pEp { ...@@ -95,14 +95,18 @@ namespace pEp {
Identity make_identity(::pEp_identity *ident) Identity make_identity(::pEp_identity *ident)
{ {
return shared_ptr<::pEp_identity>(ident , ::free_identity); return shared_ptr<::pEp_identity>(ident, ::free_identity);
} }
Message mime_parse(string text) Message mime_parse(string text)
{ {
::message *msg; ::message *msg;
bool has_possible_pEp_msg; bool has_possible_pEp_msg;
PEP_STATUS status = ::mime_decode_message(text.c_str(), text.length(), &msg, &has_possible_pEp_msg); ::PEP_STATUS status = ::mime_decode_message(
text.c_str(),
text.length(),
&msg,
&has_possible_pEp_msg);
throw_status(status); throw_status(status);
return make_message(msg); return make_message(msg);
} }
...@@ -123,9 +127,9 @@ namespace pEp { ...@@ -123,9 +127,9 @@ namespace pEp {
::message *_dst; ::message *_dst;
stringlist_t *keylist; stringlist_t *keylist;
PEP_rating rating; ::PEP_rating rating;
PEP_decrypt_flags_t flags = 0; ::PEP_decrypt_flags_t flags = 0;
PEP_STATUS status = ::decrypt_message(session(), msg.get(), &_dst, &keylist, &rating, &flags); ::PEP_STATUS status = ::decrypt_message(session(), msg.get(), &_dst, &keylist, &rating, &flags);
throw_status(status); throw_status(status);
Message dst; Message dst;
...@@ -138,18 +142,17 @@ namespace pEp { ...@@ -138,18 +142,17 @@ namespace pEp {
for (auto a = dst.get()->attachments; a && a->value; a = a->next) { for (auto a = dst.get()->attachments; a && a->value; a = a->next) {
if (string("application/pEp.sync") == a->mime_type) { if (string("application/pEp.sync") == a->mime_type) {
char *_text; char *_text;
status = PER_to_XER_Sync_msg(a->value, a->size, &_text); status = ::PER_to_XER_Sync_msg(a->value, a->size, &_text);
throw_status(status); throw_status(status);
text += _text; text += _text;
pEp_free(_text); ::pEp_free(_text);
return text; return text;
} } else if (string("application/pEp.distribution") == a->mime_type) {
else if (string("application/pEp.distribution") == a->mime_type) {
char *_text; char *_text;
status = PER_to_XER_Distribution_msg(a->value, a->size, &_text); status = ::PER_to_XER_Distribution_msg(a->value, a->size, &_text);
throw_status(status); throw_status(status);
text += _text; text += _text;
pEp_free(_text); ::pEp_free(_text);
return text; return text;
} }
} }
...@@ -171,11 +174,10 @@ namespace pEp { ...@@ -171,11 +174,10 @@ namespace pEp {
return msg; return msg;
} }
void Transport::send(Message msg) void Transport::send(Message msg)
{ {
mkdir(outbox_path.c_str(), 0770); mkdir(outbox_path.c_str(), 0770);
} }
}; }; // namespace Test
}; }; // namespace pEp
...@@ -7,22 +7,22 @@ ...@@ -7,22 +7,22 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "Adapter.hh" #include <pEp/message.h>
namespace pEp { namespace pEp {
namespace Test { namespace Test {
using namespace std; using namespace std;
// manually set up test // manually set up test
void setup(vector<string>& args); void setup(vector<string> &args);
// call this in main() for auto set up // call this in main() for auto set up
void setup(int argc=1, char **argv=nullptr); void setup(int argc = 1, char **argv = nullptr);
void import_key_from_file(string filename); void import_key_from_file(string filename);
using Message = shared_ptr<::message>; using Message = shared_ptr<::message>;
using Identity= shared_ptr<::pEp_identity>; using Identity = shared_ptr<::pEp_identity>;
// use this instead of constructor to auto assign ::free_message as // use this instead of constructor to auto assign ::free_message as
// deleter // deleter
...@@ -57,7 +57,7 @@ namespace pEp { ...@@ -57,7 +57,7 @@ namespace pEp {
extern Transport transport; extern Transport transport;
extern string path; extern string path;
}; }; // namespace Test
}; }; // namespace pEp
#endif // LIBPEPADAPTER_FRAMEWORK_HH #endif // LIBPEPADAPTER_FRAMEWORK_HH
...@@ -6,11 +6,15 @@ ...@@ -6,11 +6,15 @@
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <sys/param.h> #include <sys/param.h>
#include <pEpLog.hh>
#include <Adapter.hh>
#include <pEp/sync_api.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include "pEpLog.hh" #include <pEp/message_api.h>
using namespace std; using namespace std;
using namespace pEp::Adapter; using namespace pEp;
PEP_STATUS messageToSend(struct _message *msg) PEP_STATUS messageToSend(struct _message *msg)
{ {
...@@ -18,7 +22,7 @@ PEP_STATUS messageToSend(struct _message *msg) ...@@ -18,7 +22,7 @@ PEP_STATUS messageToSend(struct _message *msg)
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal)
{ {
pEpLog("called()"); pEpLog("called()");
return PEP_STATUS_OK; return PEP_STATUS_OK;
...@@ -26,14 +30,14 @@ PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handsha ...@@ -26,14 +30,14 @@ PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handsha
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
pEp::Test::setup(argc, argv); Test::setup(argc, argv);
// Create new identity // Create new identity
pEpLog("updating or creating identity for me"); pEpLog("updating or creating identity for me");
pEp_identity *me = new_identity("alice@peptest.ch", NULL, "23", "Who the F* is Alice"); ::pEp_identity *me = new_identity("alice@peptest.ch", NULL, "23", "Who the F* is Alice");
assert(me); assert(me);
PEP_STATUS status = myself(session(), me); ::PEP_STATUS status = ::myself(Adapter::session(), me);
free_identity(me); ::free_identity(me);
pEp::throw_status(status); pEp::throw_status(status);
// start and stop sync repeatedly // start and stop sync repeatedly
...@@ -44,10 +48,10 @@ int main(int argc, char **argv) ...@@ -44,10 +48,10 @@ int main(int argc, char **argv)
pEpLog(i); pEpLog(i);
pEpLog("SYNC START"); pEpLog("SYNC START");
pEpLog("starting the adapter including sync"); pEpLog("starting the adapter including sync");
startup(messageToSend, notifyHandshake); Adapter::startup(messageToSend, notifyHandshake);
pEpLog("SYNC STOP"); pEpLog("SYNC STOP");
usleep(sleepuSec); usleep(sleepuSec);
shutdown(); Adapter::shutdown();
} }
return 0; return 0;
} }
...@@ -5,55 +5,69 @@ ...@@ -5,55 +5,69 @@
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include "pEpLog.hh" #include <pEpLog.hh>
#include <pEp/sync_api.h>
#include <Adapter.hh>
using namespace pEp::Adapter; using namespace pEp;
PEP_STATUS messageToSend(struct _message *msg) { PEP_STATUS messageToSend(struct _message *msg)
pEpLog("called"); {
return PEP_STATUS_OK; pEpLog("called");
return PEP_STATUS_OK;
} }
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) { PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, ::sync_handshake_signal signal)
pEpLog("called"); {
return PEP_STATUS_OK; pEpLog("called");
return PEP_STATUS_OK;
} }
class JNISync { class JNISync {
public: public:
void onSyncStartup() { void onSyncStartup()
pEpLog("called"); {
} pEpLog("called");
}
void onSyncShutdown() { void onSyncShutdown()
pEpLog("called"); {
} pEpLog("called");
}
} o; } o;
int main(int argc, char **argv) { int main(int argc, char **argv)
{
pEp::Test::setup(argc, argv); pEp::Test::setup(argc, argv);
// Create new identity // Create new identity
pEpLog("updating or creating identity for me"); pEpLog("updating or creating identity for me");
pEp_identity *me = new_identity("alice@peptest.ch", NULL, "23", "Who the F* is Alice"); ::pEp_identity *me = ::new_identity("alice@peptest.ch", NULL, "23", "Who the F* is Alice");
assert(me); assert(me);
PEP_STATUS status = myself(session(), me); ::PEP_STATUS status = ::myself(Adapter::session(), me);
free_identity(me); ::free_identity(me);
pEp::throw_status(status); throw_status(status);
// start and stop sync repeatedly // start and stop sync repeatedly
useconds_t sleepuSec = 1000 * 100; useconds_t sleepuSec = 1000 * 100;
unsigned long long int nrIters = 1000 * 1000 * 1000; unsigned long long int nrIters = 1000 * 1000 * 1000;
for (int i = 0; i < nrIters; i++) { for (int i = 0; i < nrIters; i++) {
pEpLog("RUN NR: "); pEpLog("RUN NR: ");
pEpLog(i); pEpLog(i);
pEpLog("SYNC START"); pEpLog("SYNC START");
pEpLog("starting the adapter including sync"); pEpLog("starting the adapter including sync");
startup<JNISync>(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown); Adapter::startup<JNISync>(
pEpLog("SYNC STOP"); messageToSend,
usleep(sleepuSec); notifyHandshake,
shutdown(); &o,
} &JNISync::onSyncStartup,
return 0; &JNISync::onSyncShutdown);
pEpLog("SYNC STOP");
usleep(sleepuSec);
Adapter::shutdown();
}
return 0;
} }
...@@ -9,18 +9,18 @@ ...@@ -9,18 +9,18 @@
#include "framework.hh" #include "framework.hh"
#include "passphrase_cache.hh" #include <passphrase_cache.hh>
#include "status_to_string.hh" #include <status_to_string.hh>
#include <Adapter.hh>
#include <pEp/message_api.h> #include <pEp/message_api.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include <pEp/key_reset.h> #include <pEp/key_reset.h>
using namespace pEp; using namespace pEp;
using namespace pEp::Adapter;
using namespace std; using namespace std;
int main(int argc, char **argv) int main(int argc, char** argv)
{ {
Test::setup(argc, argv); Test::setup(argc, argv);
...@@ -28,7 +28,8 @@ int main(int argc, char **argv) ...@@ -28,7 +28,8 @@ int main(int argc, char **argv)
passphrase_cache.add("cathy"); passphrase_cache.add("cathy");
passphrase_cache.add("bob"); passphrase_cache.add("bob");
const char* bob_filename = ENGINE_TEST "/test_keys/bob-primary-with-password-bob-subkey-without.pgp"; const char* bob_filename = ENGINE_TEST
"/test_keys/bob-primary-with-password-bob-subkey-without.pgp";
const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736"; const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736";
const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp"; const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp";
...@@ -37,15 +38,15 @@ int main(int argc, char **argv) ...@@ -37,15 +38,15 @@ int main(int argc, char **argv)
Test::import_key_from_file(bob_filename); Test::import_key_from_file(bob_filename);
Test::import_key_from_file(erwin_filename); Test::import_key_from_file(erwin_filename);
pEp_identity* bob = ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog"); ::pEp_identity* bob = ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog");
PEP_STATUS status = ::set_own_key(session(), bob, bob_fpr); ::PEP_STATUS status = ::set_own_key(Adapter::session(), bob, bob_fpr);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
pEp_identity* erwin = ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin"); ::pEp_identity* erwin = ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin");
status = ::set_own_key(session(), erwin, erwin_fpr); status = ::set_own_key(Adapter::session(), erwin, erwin_fpr);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
status = ::key_reset_all_own_keys(session()); status = ::key_reset_all_own_keys(Adapter::session());
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
::free_identity(bob); ::free_identity(bob);
...@@ -55,4 +56,3 @@ int main(int argc, char **argv) ...@@ -55,4 +56,3 @@ int main(int argc, char **argv)
return 0; return 0;
} }
...@@ -3,32 +3,27 @@ ...@@ -3,32 +3,27 @@
#include <unistd.h> #include <unistd.h>
#include "framework.hh" #include "framework.hh"
#include "passphrase_cache.hh" #include <passphrase_cache.hh>
#include "callback_dispatcher.hh" #include <callback_dispatcher.hh>
#include <pEp/sync_api.h> #include <pEp/sync_api.h>
using namespace pEp;
using namespace pEp::Adapter;
using namespace std; using namespace std;
using namespace pEp;
vector<string> expected_msg = { "synchronizeGroupKeys",
"groupKeysUpdate",
"initUnledGroupKeyReset",
"beacon",
"beacon" };
vector<string> expected_msg = { vector<::sync_handshake_signal> expected_notification = { SYNC_NOTIFY_IN_GROUP,
"synchronizeGroupKeys", SYNC_NOTIFY_START,
"groupKeysUpdate", SYNC_NOTIFY_SOLE,
"initUnledGroupKeyReset", SYNC_NOTIFY_START,
"beacon", SYNC_NOTIFY_STOP };
"beacon"
}; ::PEP_STATUS test_messageToSend(::message* _msg)
vector<::sync_handshake_signal> expected_notification = {
SYNC_NOTIFY_IN_GROUP,
SYNC_NOTIFY_START,
SYNC_NOTIFY_SOLE,
SYNC_NOTIFY_START,
SYNC_NOTIFY_STOP
};
PEP_STATUS test_messageToSend(::message *_msg)
{ {
static auto actual = expected_msg.begin(); static auto actual = expected_msg.begin();
...@@ -37,11 +32,11 @@ PEP_STATUS test_messageToSend(::message *_msg) ...@@ -37,11 +32,11 @@ PEP_STATUS test_messageToSend(::message *_msg)
cerr << "expecting: " << *actual << endl; cerr << "expecting: " << *actual << endl;
cerr << text; cerr << text;
assert(text.find(*actual++) != string::npos); assert(text.find(*actual++) != string::npos);
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_handshake_signal signal) ::PEP_STATUS test_notifyHandshake(::pEp_identity* _me, ::pEp_identity* _partner, sync_handshake_signal signal)
{ {
static auto actual = expected_notification.begin(); static auto actual = expected_notification.begin();
...@@ -50,20 +45,21 @@ PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_ ...@@ -50,20 +45,21 @@ PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_
cerr << "expecting: " << *actual << endl; cerr << "expecting: " << *actual << endl;
cerr << "notifyHandshake: " << signal << endl; cerr << "notifyHandshake: " << signal << endl;
assert(signal == *actual++); assert(signal == *actual++);
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
int main(int argc, char **argv) int main(int argc, char** argv)
{ {
Test::setup(argc, argv); Test::setup(argc, argv);
session(); Adapter::session();
// set up two own identites for sync // set up two own identites for sync
passphrase_cache.add("erwin"); passphrase_cache.add("erwin");
passphrase_cache.add("bob"); passphrase_cache.add("bob");
const char* bob_filename = ENGINE_TEST "/test_keys/bob-primary-with-password-bob-subkey-without.pgp"; const char* bob_filename = ENGINE_TEST
"/test_keys/bob-primary-with-password-bob-subkey-without.pgp";
const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736"; const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736";
const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp"; const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp";
...@@ -72,24 +68,26 @@ int main(int argc, char **argv) ...@@ -72,24 +68,26 @@ int main(int argc, char **argv)
Test::import_key_from_file(bob_filename); Test::import_key_from_file(bob_filename);
Test::import_key_from_file(erwin_filename); Test::import_key_from_file(erwin_filename);
Test::Identity bob = Test::make_identity(::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog")); Test::Identity bob = Test::make_identity(
PEP_STATUS status = ::set_own_key(session(), bob.get(), bob_fpr); ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog"));
PEP_STATUS status = ::set_own_key(Adapter::session(), bob.get(), bob_fpr);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
status = ::enable_identity_for_sync(session(), bob.get()); status = ::enable_identity_for_sync(Adapter::session(), bob.get());
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
Test::Identity erwin = Test::make_identity(::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin")); Test::Identity erwin = Test::make_identity(
status = ::set_own_key(session(), erwin.get(), erwin_fpr); ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin"));
status = ::set_own_key(Adapter::session(), erwin.get(), erwin_fpr);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
status = ::enable_identity_for_sync(session(), erwin.get()); status = ::enable_identity_for_sync(Adapter::session(), erwin.get());
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
// simulate a device group by setting the identities to in sync // simulate a device group by setting the identities to in sync
status = set_identity_flags(session(), bob.get(), PEP_idf_devicegroup); status = ::set_identity_flags(Adapter::session(), bob.get(), PEP_idf_devicegroup);
status = set_identity_flags(session(), erwin.get(), PEP_idf_devicegroup); status = ::set_identity_flags(Adapter::session(), erwin.get(), PEP_idf_devicegroup);
// register at callback_dispatcher and start sync // register at callback_dispatcher and start sync
...@@ -98,24 +96,24 @@ int main(int argc, char **argv) ...@@ -98,24 +96,24 @@ int main(int argc, char **argv)
// leave device group // leave device group
status = ::leave_device_group(session());
status = ::leave_device_group(Adapter::session());
// wait for sync shutdown and release first session // wait for sync shutdown and release first session
Test::join_sync_thread(); Test::join_sync_thread();
assert(!is_sync_running()); assert(!Adapter::is_sync_running());
// switch off and on again // switch off and on again
CallbackDispatcher::start_sync(); CallbackDispatcher::start_sync();
sleep(2); sleep(2);
assert(is_sync_running()); assert(Adapter::is_sync_running());
CallbackDispatcher::stop_sync(); CallbackDispatcher::stop_sync();
Test::join_sync_thread(); Test::join_sync_thread();
assert(!is_sync_running()); assert(!Adapter::is_sync_running());
session(Adapter::release); Adapter::session(Adapter::release);
return 0; return 0;
} }
...@@ -4,113 +4,99 @@ ...@@ -4,113 +4,99 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
using namespace pEp; using namespace pEp;
// Producer's data: // Producer's data:
class P class P {
{
public: public:
P(int i) P(int i) : data{ new char[64] }
: data{new char[64]} {
{ snprintf(data, 63, "%i", i);
snprintf(data, 63, "%i", i); }
}
P(const P&) = delete;
P(const P&) = delete; void operator=(const P&) = delete;
void operator=(const P&) = delete;
~P()
~P() {
{ std::cerr << "~P(): data=" << (data ? data : "(NULL)") << '\n';
std::cerr << "~P(): data=" << (data ? data : "(NULL)" ) << '\n'; delete[] data;
delete[] data; }
}
char* data;
char* data;
}; };
// Consumer's data: // Consumer's data:
class C class C {
{
public: public:
C(int _i) : i(_i) {} C(int _i) : i(_i) {}
int i; int i;
}; };
pc_container<P,C> pc; pc_container<P, C> pc;
void consumer_thread() void consumer_thread()
{ {
bool keep_running = true; bool keep_running = true;
int sum = 0; int sum = 0;
while(keep_running) while (keep_running) {
{ for (auto& q : pc) {
for(auto& q : pc) switch (q.state()) {
{ case PC_State::Created: {
switch(q.state()) const int value = atoi(q.pdata->data);
{ if (value < 0) {
case PC_State::Created: std::cerr << "Q\n";
{ keep_running = false;
const int value = atoi( q.pdata->data ); } else {
if(value<0) std::cerr << "C";
{ q.cdata = new C(value);
std::cerr << "Q\n"; sum += q.cdata->i;
keep_running = false; }
}else{ break;
std::cerr << "C"; }
q.cdata = new C( value ); case PC_State::Deleted: {
sum += q.cdata->i; std::cerr << "D";
} sum -= q.cdata->i;
break; delete q.cdata;
} break;
case PC_State::Deleted: }
{ case PC_State::Changed: {
std::cerr << "D"; std::cerr << "X";
sum -= q.cdata->i; sum -= q.cdata->i;
delete q.cdata; delete q.cdata;
break; q.cdata = new C(atoi(q.pdata->data));
} sum += q.cdata->i;
case PC_State::Changed: break;
{ }
std::cerr << "X"; default:
sum -= q.cdata->i; throw "Illegal state";
delete q.cdata; }
q.cdata = new C( atoi( q.pdata->data ) ); }
sum += q.cdata->i; }
break;
} std::cout << "Consumer sum: " << sum << ".\n";
default: throw "Illegal state";
}
}
}
std::cout << "Consumer sum: " << sum << ".\n";
} }
int main() int main()
{ {
for(int i=0; i<10; ++i) for (int i = 0; i < 10; ++i) {
{ pc.insert(new P(i));
pc.insert( new P(i) ); }
}
std::thread t{ &consumer_thread };
std::thread t{ &consumer_thread };
for (int i = 10; i < 100; ++i) {
for(int i=10; i<100; ++i) pc.insert(new P(i));
{ }
pc.insert( new P(i) );
} while (!pc.empty()) {
auto q = pc.begin();
while( !pc.empty() ) delete q->pdata;
{ pc.erase(q);
auto q = pc.begin(); }
delete q->pdata;
pc.erase( q ); pc.insert(new P(-1));
}
t.join();
pc.insert( new P(-1) );
t.join();
} }
#include "framework.hh"
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <sys/param.h> #include <sys/param.h>
#include <unistd.h> #include <unistd.h>
#include "message_cache.hh" #include <message_cache.hh>
#include "framework.hh" #include <Adapter.hh>
using namespace std; using namespace std;
using namespace pEp; using namespace pEp;
...@@ -12,8 +13,8 @@ int main(int argc, char **argv) ...@@ -12,8 +13,8 @@ int main(int argc, char **argv)
{ {
Test::setup(argc, argv); Test::setup(argc, argv);
pEp_identity *alice = ::new_identity("alice@mail.com", nullptr, PEP_OWN_USERID, "Alice"); ::pEp_identity *alice = ::new_identity("alice@mail.com", nullptr, PEP_OWN_USERID, "Alice");
::myself(pEp::Adapter::session(), alice); ::myself(Adapter::session(), alice);
char *mime = strdup("From: Alice <alice@mail.com>\n" char *mime = strdup("From: Alice <alice@mail.com>\n"
"To: Bob <bob@mail.com>\n" "To: Bob <bob@mail.com>\n"
...@@ -25,20 +26,30 @@ int main(int argc, char **argv) ...@@ -25,20 +26,30 @@ int main(int argc, char **argv)
::message *src = nullptr; ::message *src = nullptr;
bool has_possible_pEp_msg; bool has_possible_pEp_msg;
PEP_STATUS status = MessageCache::cache_mime_decode_message(mime, strlen(mime), &src, &has_possible_pEp_msg); ::PEP_STATUS status = MessageCache::cache_mime_decode_message(
mime,
strlen(mime),
&src,
&has_possible_pEp_msg);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
status = ::myself(pEp::Adapter::session(), src->from); status = ::myself(Adapter::session(), src->from);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
::update_identity(pEp::Adapter::session(), src->to->ident); ::update_identity(Adapter::session(), src->to->ident);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
pEp_identity *bob = identity_dup(src->to->ident); ::pEp_identity *bob = identity_dup(src->to->ident);
src->dir = PEP_dir_outgoing; src->dir = ::PEP_dir_outgoing;
::message *dst = nullptr; ::message *dst = nullptr;
status = MessageCache::cache_encrypt_message(pEp::Adapter::session(), src, nullptr, &dst, PEP_enc_PEP, 0); status = MessageCache::cache_encrypt_message(
Adapter::session(),
src,
nullptr,
&dst,
PEP_enc_PEP,
0);
assert(status != PEP_ILLEGAL_VALUE); assert(status != PEP_ILLEGAL_VALUE);
assert(src->longmsg == nullptr); assert(src->longmsg == nullptr);
...@@ -65,11 +76,17 @@ int main(int argc, char **argv) ...@@ -65,11 +76,17 @@ int main(int argc, char **argv)
assert(src->longmsg == nullptr); assert(src->longmsg == nullptr);
assert(src->attachments == nullptr); assert(src->attachments == nullptr);
PEP_rating rating; ::PEP_rating rating;
PEP_decrypt_flags_t flags = 0; ::PEP_decrypt_flags_t flags = 0;
stringlist_t *keylist = nullptr; ::stringlist_t *keylist = nullptr;
status = MessageCache::cache_decrypt_message(pEp::Adapter::session(), src, &dst, &keylist, &rating, &flags); status = MessageCache::cache_decrypt_message(
Adapter::session(),
src,
&dst,
&keylist,
&rating,
&flags);
assert(status != PEP_ILLEGAL_VALUE); assert(status != PEP_ILLEGAL_VALUE);
assert(src->longmsg == nullptr); assert(src->longmsg == nullptr);
...@@ -92,7 +109,6 @@ int main(int argc, char **argv) ...@@ -92,7 +109,6 @@ int main(int argc, char **argv)
::free_identity(bob); ::free_identity(bob);
::free_identity(alice); ::free_identity(alice);
pEp::Adapter::session(pEp::Adapter::release); Adapter::session(Adapter::release);
return 0; return 0;
} }
#include "framework.hh"
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <sys/param.h> #include <sys/param.h>
#include <passphrase_cache.hh>
#include <status_to_string.hh>
#include <Adapter.hh>
#include "framework.hh" using namespace pEp;
#include "passphrase_cache.hh"
#include "status_to_string.hh"
extern "C" { extern "C" {
PEP_STATUS api_test1(PEP_SESSION session, const char *str, char *bytes, int n, ::stringlist_t *sl) ::PEP_STATUS api_test1(::PEP_SESSION session, const char *str, char *bytes, int n, ::stringlist_t *sl)
{ {
std::cout << "called api_test1\n"; std::cout << "called api_test1\n";
return PEP_WRONG_PASSPHRASE; return PEP_WRONG_PASSPHRASE;
} }
PEP_STATUS api_test2(PEP_SESSION session, int n, const char *str, char *bytes, ::stringlist_t *sl) ::PEP_STATUS api_test2(::PEP_SESSION session, int n, const char *str, char *bytes, ::stringlist_t *sl)
{ {
std::cout << "called api_test2\n"; std::cout << "called api_test2\n";
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
}; };
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
pEp::Test::setup(argc, argv); Test::setup(argc, argv);
const char *str = "23"; const char *str = "23";
char *bytes = NULL; char *bytes = NULL;
int n = 42; int n = 42;
::stringlist_t *sl = NULL; ::stringlist_t *sl = NULL;
pEp::PassphraseCache cache{2, std::chrono::seconds(1)}; PassphraseCache cache{ 2, std::chrono::seconds(1) };
cache.add("say"); cache.add("say");
cache.add("hello"); cache.add("hello");
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 passphrase != "";}); 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;
});
PEP_STATUS status = cache.api(api_test1, pEp::Adapter::session(), "23", bytes, n, (::stringlist_t *) NULL); ::PEP_STATUS status = cache.api(api_test1, Adapter::session(), "23", bytes, n, (::stringlist_t *)NULL);
assert(status == PEP_WRONG_PASSPHRASE); assert(status == PEP_WRONG_PASSPHRASE);
status = cache.api(api_test2, pEp::Adapter::session(), n, str, bytes, sl); status = cache.api(api_test2, Adapter::session(), n, str, bytes, sl);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
cache.add("hello"); cache.add("hello");
cache.add("world"); cache.add("world");
std::cout << "expected: two passphrases in reverse order\n"; std::cout << "expected: two passphrases in reverse order\n";
pEp::PassphraseCache _cache = cache; PassphraseCache _cache = cache;
try { try {
while (1) { while (1) {
std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n"; std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n";
} }
} catch (std::underflow_error &) {
} }
catch (std::underflow_error&) { }
pEp::passphrase_cache.add("hello"); passphrase_cache.add("hello");
pEp::passphrase_cache.add("world"); passphrase_cache.add("world");
std::cout << "two times PEP_STATUS_OK (0), one time PEP_WRONG_PASSPHRASE (2561)\n"; std::cout << "two times PEP_STATUS_OK (0), one time PEP_WRONG_PASSPHRASE (2561)\n";
do { do {
status = pEp::PassphraseCache::config_next_passphrase(); status = PassphraseCache::config_next_passphrase();
std::cout << pEp::status_to_string(status) << " (" << status << ")\n"; std::cout << status_to_string(status) << " (" << status << ")\n";
} while (status == PEP_STATUS_OK); } while (status == PEP_STATUS_OK);
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;
});
status = cache.api(api_test1, pEp::Adapter::session(), str, bytes, n, sl); status = cache.api(api_test1, Adapter::session(), str, bytes, n, sl);
assert(status == PEP_WRONG_PASSPHRASE); assert(status == PEP_WRONG_PASSPHRASE);
status = cache.api(api_test2, pEp::Adapter::session(), 23, str, bytes, sl); status = cache.api(api_test2, Adapter::session(), 23, str, bytes, sl);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
pEp::Adapter::session(pEp::Adapter::release); Adapter::session(Adapter::release);
return 0; return 0;
} }
...@@ -10,26 +10,25 @@ int main() ...@@ -10,26 +10,25 @@ int main()
{ {
Semaphore semaphore; Semaphore semaphore;
thread thread1([&](){ thread thread1([&]() {
cout << "1: before stop\n"; cout << "1: before stop\n";
semaphore.stop(); semaphore.stop();
cout << "1: now on stop\n"; cout << "1: now on stop\n";
semaphore.try_wait(); semaphore.try_wait();
cout << "1: and on go again\n"; cout << "1: and on go again\n";
semaphore.try_wait(); semaphore.try_wait();
cout << "1: keep going\n"; cout << "1: keep going\n";
}); });
sleep(1); sleep(1);
thread thread2([&](){ thread thread2([&]() {
cout << "2: setting go\n"; cout << "2: setting go\n";
semaphore.go(); semaphore.go();
}); });
thread1.join(); thread1.join();
thread2.join(); thread2.join();
return 0; return 0;
} }