Browse Source

LIB-13: YES, diff breaking cahnge, reformat tests. clang-format now or never!

master
heck 3 months ago
parent
commit
9b2defe5fb
10 changed files with 242 additions and 212 deletions
  1. +4
    -1
      .clang-format
  2. +17
    -17
      test/framework.cc
  3. +5
    -5
      test/framework.hh
  4. +45
    -35
      test/test_adapter_cxx.cc
  5. +3
    -3
      test/test_ensure_passphrase.cc
  6. +23
    -25
      test/test_leave_device_group.cc
  7. +77
    -89
      test/test_library.cc
  8. +19
    -4
      test/test_message_cache.cc
  9. +36
    -19
      test/test_passphrase_cache.cc
  10. +13
    -14
      test/test_semaphore.cc

+ 4
- 1
.clang-format View File

@ -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
IndentPPDirectives: BeforeHash
Cpp11BracedListStyle: false

+ 17
- 17
test/framework.cc View File

@ -28,7 +28,7 @@ namespace pEp {
namespace Test {
using namespace Adapter;
void setup(vector<string>& args)
void setup(vector<string> &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<string> args{(size_t) argc};
for (int i=0; i<argc; ++i)
vector<string> 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<char>(f), istreambuf_iterator<char>()};
string key{ istreambuf_iterator<char>(f), istreambuf_iterator<char>() };
::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

+ 5
- 5
test/framework.hh View File

@ -14,15 +14,15 @@ namespace pEp {
using namespace std;
// manually set up test
void setup(vector<string>& args);
void setup(vector<string> &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

+ 45
- 35
test/test_adapter_cxx.cc View File

@ -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<JNISync>(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<JNISync>(
messageToSend,
notifyHandshake,
&o,
&JNISync::onSyncStartup,
&JNISync::onSyncShutdown);
pEpLog("SYNC STOP");
usleep(sleepuSec);
shutdown();
}
return 0;
}

+ 3
- 3
test/test_ensure_passphrase.cc View File

@ -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;
}

+ 23
- 25
test/test_leave_device_group.cc View File

@ -12,23 +12,19 @@ using namespace pEp;
using namespace pEp::Adapter;
using namespace std;
vector<string> 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<string> 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;
}

+ 77
- 89
test/test_library.cc View File

@ -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<P,C> pc;
pc_container<P, C> 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();
}

+ 19
- 4
test/test_message_cache.cc View File

@ -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;
}

+ 36
- 19
test/test_passphrase_cache.cc View File

@ -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;
}

+ 13
- 14
test/test_semaphore.cc View File

@ -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;
}

Loading…
Cancel
Save