diff --git a/.clang-format b/.clang-format index cfbf3de..04d7370 100644 --- a/.clang-format +++ b/.clang-format @@ -1,11 +1,13 @@ BasedOnStyle: LLVM Language: Cpp +Standard: c++14 DerivePointerAlignment: true SortIncludes: false ReflowComments: false PointerAlignment: Left AlignAfterOpenBracket: AlwaysBreak AlignOperands: AlignAfterOperator +BreakConstructorInitializers: AfterColon AlignTrailingComments: true AllowAllArgumentsOnNextLine: false AllowAllParametersOfDeclarationOnNextLine: false @@ -35,4 +37,5 @@ NamespaceIndentation: All SpaceAfterTemplateKeyword: false AccessModifierOffset: -4 AllowShortBlocksOnASingleLine: Always -IndentPPDirectives: BeforeHash \ No newline at end of file +IndentPPDirectives: BeforeHash +Cpp11BracedListStyle: false \ No newline at end of file diff --git a/test/framework.cc b/test/framework.cc index 0dc557d..486adc3 100644 --- a/test/framework.cc +++ b/test/framework.cc @@ -28,7 +28,7 @@ namespace pEp { namespace Test { using namespace Adapter; - void setup(vector& args) + void setup(vector &args) { #ifdef WIN32 string dir = getenv("TEMP"); @@ -45,17 +45,15 @@ namespace pEp { cout << "usage: " << args[0] << " [--dir HOME]" << endl; #endif exit(0); - } - else if (args[1] == "--dir" && args.size() == 3) { + } else if (args[1] == "--dir" && args.size() == 3) { dir = args[2]; - } - else { + } else { cerr << "illegal parameter" << endl; exit(1); } } - char _path[MAXPATHLEN+1]; + char _path[MAXPATHLEN + 1]; const char *templ = dir.c_str(); strcpy(_path, templ); mkdtemp(_path); @@ -71,8 +69,8 @@ namespace pEp { void setup(int argc, char **argv) { - vector args{(size_t) argc}; - for (int i=0; i args{ (size_t)argc }; + for (int i = 0; i < argc; ++i) args[i] = argv[i]; setup(args); @@ -81,7 +79,7 @@ namespace pEp { void import_key_from_file(string filename) { ifstream f(filename, ifstream::in); - string key{istreambuf_iterator(f), istreambuf_iterator()}; + 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); @@ -95,14 +93,18 @@ namespace pEp { 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 *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); return make_message(msg); } @@ -143,8 +145,7 @@ namespace pEp { text += _text; pEp_free(_text); return text; - } - else if (string("application/pEp.distribution") == a->mime_type) { + } else if (string("application/pEp.distribution") == a->mime_type) { char *_text; status = PER_to_XER_Distribution_msg(a->value, a->size, &_text); throw_status(status); @@ -171,11 +172,10 @@ namespace pEp { return msg; } - + void Transport::send(Message msg) { mkdir(outbox_path.c_str(), 0770); - } - }; -}; + }; // namespace Test +}; // namespace pEp diff --git a/test/framework.hh b/test/framework.hh index 05c5711..9dfeb59 100644 --- a/test/framework.hh +++ b/test/framework.hh @@ -14,15 +14,15 @@ namespace pEp { using namespace std; // manually set up test - void setup(vector& args); + void setup(vector &args); // 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); 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 // deleter @@ -57,7 +57,7 @@ namespace pEp { extern Transport transport; extern string path; - }; -}; + }; // namespace Test +}; // namespace pEp #endif // LIBPEPADAPTER_FRAMEWORK_HH diff --git a/test/test_adapter_cxx.cc b/test/test_adapter_cxx.cc index 7dcdc27..997acc2 100644 --- a/test/test_adapter_cxx.cc +++ b/test/test_adapter_cxx.cc @@ -10,50 +10,60 @@ using namespace pEp::Adapter; -PEP_STATUS messageToSend(struct _message *msg) { - pEpLog("called"); - return PEP_STATUS_OK; +PEP_STATUS messageToSend(struct _message *msg) +{ + pEpLog("called"); + return PEP_STATUS_OK; } -PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) { - pEpLog("called"); - return PEP_STATUS_OK; +PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) +{ + pEpLog("called"); + return PEP_STATUS_OK; } class JNISync { public: - void onSyncStartup() { - pEpLog("called"); - } + void onSyncStartup() + { + pEpLog("called"); + } - void onSyncShutdown() { - pEpLog("called"); - } + void onSyncShutdown() + { + pEpLog("called"); + } } o; -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ pEp::Test::setup(argc, argv); - // 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"); - assert(me); - PEP_STATUS status = myself(session(), me); - free_identity(me); - pEp::throw_status(status); - - // start and stop sync repeatedly - useconds_t sleepuSec = 1000 * 100; - unsigned long long int nrIters = 1000 * 1000 * 1000; - for (int i = 0; i < nrIters; i++) { - pEpLog("RUN NR: "); - pEpLog(i); - pEpLog("SYNC START"); - pEpLog("starting the adapter including sync"); - startup(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown); - pEpLog("SYNC STOP"); - usleep(sleepuSec); - shutdown(); - } - return 0; + // 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"); + assert(me); + PEP_STATUS status = myself(session(), me); + free_identity(me); + pEp::throw_status(status); + + // start and stop sync repeatedly + useconds_t sleepuSec = 1000 * 100; + unsigned long long int nrIters = 1000 * 1000 * 1000; + for (int i = 0; i < nrIters; i++) { + pEpLog("RUN NR: "); + pEpLog(i); + pEpLog("SYNC START"); + pEpLog("starting the adapter including sync"); + startup( + messageToSend, + notifyHandshake, + &o, + &JNISync::onSyncStartup, + &JNISync::onSyncShutdown); + pEpLog("SYNC STOP"); + usleep(sleepuSec); + shutdown(); + } + return 0; } diff --git a/test/test_ensure_passphrase.cc b/test/test_ensure_passphrase.cc index ef7146d..12ef456 100644 --- a/test/test_ensure_passphrase.cc +++ b/test/test_ensure_passphrase.cc @@ -20,7 +20,7 @@ using namespace pEp; using namespace pEp::Adapter; using namespace std; -int main(int argc, char **argv) +int main(int argc, char** argv) { Test::setup(argc, argv); @@ -28,7 +28,8 @@ int main(int argc, char **argv) passphrase_cache.add("cathy"); 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* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp"; @@ -55,4 +56,3 @@ int main(int argc, char **argv) return 0; } - diff --git a/test/test_leave_device_group.cc b/test/test_leave_device_group.cc index 368ead9..98bf827 100644 --- a/test/test_leave_device_group.cc +++ b/test/test_leave_device_group.cc @@ -12,23 +12,19 @@ using namespace pEp; using namespace pEp::Adapter; using namespace std; -vector expected_msg = { - "synchronizeGroupKeys", - "groupKeysUpdate", - "initUnledGroupKeyReset", - "beacon", - "beacon" - }; - -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) +vector expected_msg = { "synchronizeGroupKeys", + "groupKeysUpdate", + "initUnledGroupKeyReset", + "beacon", + "beacon" }; + +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(); @@ -37,11 +33,11 @@ PEP_STATUS test_messageToSend(::message *_msg) cerr << "expecting: " << *actual << endl; cerr << text; 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(); @@ -50,10 +46,10 @@ PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_ cerr << "expecting: " << *actual << endl; cerr << "notifyHandshake: " << signal << endl; 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); session(); @@ -63,7 +59,8 @@ int main(int argc, char **argv) passphrase_cache.add("erwin"); 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* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp"; @@ -72,14 +69,16 @@ int main(int argc, char **argv) Test::import_key_from_file(bob_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( + ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog")); PEP_STATUS status = ::set_own_key(session(), bob.get(), bob_fpr); assert(status == PEP_STATUS_OK); status = ::enable_identity_for_sync(session(), bob.get()); 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( + ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin")); status = ::set_own_key(session(), erwin.get(), erwin_fpr); assert(status == PEP_STATUS_OK); @@ -118,4 +117,3 @@ int main(int argc, char **argv) return 0; } - diff --git a/test/test_library.cc b/test/test_library.cc index 5179cc2..2c43c30 100644 --- a/test/test_library.cc +++ b/test/test_library.cc @@ -9,108 +9,96 @@ using namespace pEp; // Producer's data: -class P -{ +class P { public: - P(int i) - : data{new char[64]} - { - snprintf(data, 63, "%i", i); - } - - P(const P&) = delete; - void operator=(const P&) = delete; - - ~P() - { - std::cerr << "~P(): data=" << (data ? data : "(NULL)" ) << '\n'; - delete[] data; - } - - char* data; + P(int i) : data{ new char[64] } + { + snprintf(data, 63, "%i", i); + } + + P(const P&) = delete; + void operator=(const P&) = delete; + + ~P() + { + std::cerr << "~P(): data=" << (data ? data : "(NULL)") << '\n'; + delete[] data; + } + + char* data; }; // Consumer's data: -class C -{ +class C { public: - C(int _i) : i(_i) {} - int i; + C(int _i) : i(_i) {} + int i; }; -pc_container pc; +pc_container pc; void consumer_thread() { - bool keep_running = true; - int sum = 0; - while(keep_running) - { - for(auto& q : pc) - { - switch(q.state()) - { - case PC_State::Created: - { - const int value = atoi( q.pdata->data ); - if(value<0) - { - std::cerr << "Q\n"; - keep_running = false; - }else{ - std::cerr << "C"; - q.cdata = new C( value ); - sum += q.cdata->i; - } - break; - } - case PC_State::Deleted: - { - std::cerr << "D"; - sum -= q.cdata->i; - delete q.cdata; - break; - } - case PC_State::Changed: - { - std::cerr << "X"; - sum -= q.cdata->i; - delete q.cdata; - q.cdata = new C( atoi( q.pdata->data ) ); - sum += q.cdata->i; - break; - } - default: throw "Illegal state"; - } - } - } - - std::cout << "Consumer sum: " << sum << ".\n"; + bool keep_running = true; + int sum = 0; + while (keep_running) { + for (auto& q : pc) { + switch (q.state()) { + case PC_State::Created: { + const int value = atoi(q.pdata->data); + if (value < 0) { + std::cerr << "Q\n"; + keep_running = false; + } else { + std::cerr << "C"; + q.cdata = new C(value); + sum += q.cdata->i; + } + break; + } + case PC_State::Deleted: { + std::cerr << "D"; + sum -= q.cdata->i; + delete q.cdata; + break; + } + case PC_State::Changed: { + std::cerr << "X"; + sum -= q.cdata->i; + delete q.cdata; + q.cdata = new C(atoi(q.pdata->data)); + sum += q.cdata->i; + break; + } + default: + throw "Illegal state"; + } + } + } + + std::cout << "Consumer sum: " << sum << ".\n"; } int main() { - for(int i=0; i<10; ++i) - { - pc.insert( new P(i) ); - } - - std::thread t{ &consumer_thread }; - - for(int i=10; i<100; ++i) - { - pc.insert( new P(i) ); - } - - while( !pc.empty() ) - { - auto q = pc.begin(); - delete q->pdata; - pc.erase( q ); - } - - pc.insert( new P(-1) ); - - t.join(); + for (int i = 0; i < 10; ++i) { + pc.insert(new P(i)); + } + + std::thread t{ &consumer_thread }; + + for (int i = 10; i < 100; ++i) { + pc.insert(new P(i)); + } + + while (!pc.empty()) { + auto q = pc.begin(); + delete q->pdata; + pc.erase(q); + } + + pc.insert(new P(-1)); + + t.join(); } diff --git a/test/test_message_cache.cc b/test/test_message_cache.cc index b3d493f..bfab699 100644 --- a/test/test_message_cache.cc +++ b/test/test_message_cache.cc @@ -25,7 +25,11 @@ int main(int argc, char **argv) ::message *src = nullptr; 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); status = ::myself(pEp::Adapter::session(), src->from); @@ -38,7 +42,13 @@ int main(int argc, char **argv) src->dir = PEP_dir_outgoing; ::message *dst = nullptr; - status = MessageCache::cache_encrypt_message(pEp::Adapter::session(), src, nullptr, &dst, PEP_enc_PEP, 0); + status = MessageCache::cache_encrypt_message( + pEp::Adapter::session(), + src, + nullptr, + &dst, + PEP_enc_PEP, + 0); assert(status != PEP_ILLEGAL_VALUE); assert(src->longmsg == nullptr); @@ -69,7 +79,13 @@ int main(int argc, char **argv) PEP_decrypt_flags_t flags = 0; stringlist_t *keylist = nullptr; - status = MessageCache::cache_decrypt_message(pEp::Adapter::session(), src, &dst, &keylist, &rating, &flags); + status = MessageCache::cache_decrypt_message( + pEp::Adapter::session(), + src, + &dst, + &keylist, + &rating, + &flags); assert(status != PEP_ILLEGAL_VALUE); assert(src->longmsg == nullptr); @@ -95,4 +111,3 @@ int main(int argc, char **argv) pEp::Adapter::session(pEp::Adapter::release); return 0; } - diff --git a/test/test_passphrase_cache.cc b/test/test_passphrase_cache.cc index a57160d..f5246ae 100644 --- a/test/test_passphrase_cache.cc +++ b/test/test_passphrase_cache.cc @@ -8,17 +8,17 @@ #include "status_to_string.hh" extern "C" { - PEP_STATUS api_test1(PEP_SESSION session, const char *str, char *bytes, int n, ::stringlist_t *sl) - { - std::cout << "called api_test1\n"; - return PEP_WRONG_PASSPHRASE; - } +PEP_STATUS api_test1(PEP_SESSION session, const char *str, char *bytes, int n, ::stringlist_t *sl) +{ + std::cout << "called api_test1\n"; + return PEP_WRONG_PASSPHRASE; +} - PEP_STATUS api_test2(PEP_SESSION session, int n, const char *str, char *bytes, ::stringlist_t *sl) - { - std::cout << "called api_test2\n"; - return PEP_STATUS_OK; - } +PEP_STATUS api_test2(PEP_SESSION session, int n, const char *str, char *bytes, ::stringlist_t *sl) +{ + std::cout << "called api_test2\n"; + return PEP_STATUS_OK; +} }; int main(int argc, char **argv) @@ -30,21 +30,36 @@ int main(int argc, char **argv) int n = 42; ::stringlist_t *sl = NULL; - pEp::PassphraseCache cache{2, std::chrono::seconds(1)}; + pEp::PassphraseCache cache{ 2, std::chrono::seconds(1) }; cache.add("say"); 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;}); + 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 passphrase != "";}); + cache.for_each_passphrase([&](std::string passphrase) { + std::cout << "'" << passphrase << "'\n"; + return passphrase != ""; + }); std::cout << "expected: two passphrases but reverse order\n"; - 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); + 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); assert(status == PEP_WRONG_PASSPHRASE); status = cache.api(api_test2, pEp::Adapter::session(), n, str, bytes, sl); assert(status == PEP_STATUS_OK); @@ -57,8 +72,8 @@ int main(int argc, char **argv) while (1) { std::cout << "'" << _cache.latest_passphrase(_cache) << "'\n"; } + } catch (std::underflow_error &) { } - catch (std::underflow_error&) { } pEp::passphrase_cache.add("hello"); pEp::passphrase_cache.add("world"); @@ -71,7 +86,10 @@ int main(int argc, char **argv) sleep(2); 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); assert(status == PEP_WRONG_PASSPHRASE); @@ -81,4 +99,3 @@ int main(int argc, char **argv) pEp::Adapter::session(pEp::Adapter::release); return 0; } - diff --git a/test/test_semaphore.cc b/test/test_semaphore.cc index 3b64c3c..658f2c3 100644 --- a/test/test_semaphore.cc +++ b/test/test_semaphore.cc @@ -10,26 +10,25 @@ int main() { Semaphore semaphore; - thread thread1([&](){ - cout << "1: before stop\n"; - semaphore.stop(); - cout << "1: now on stop\n"; - semaphore.try_wait(); - cout << "1: and on go again\n"; - semaphore.try_wait(); - cout << "1: keep going\n"; - }); + thread thread1([&]() { + cout << "1: before stop\n"; + semaphore.stop(); + cout << "1: now on stop\n"; + semaphore.try_wait(); + cout << "1: and on go again\n"; + semaphore.try_wait(); + cout << "1: keep going\n"; + }); sleep(1); - thread thread2([&](){ - cout << "2: setting go\n"; - semaphore.go(); - }); + thread thread2([&]() { + cout << "2: setting go\n"; + semaphore.go(); + }); thread1.join(); thread2.join(); return 0; } -