
21 changed files with 523 additions and 43 deletions
@ -0,0 +1,25 @@ |
|||
*.o |
|||
*.a |
|||
*.d |
|||
*.swp |
|||
.DS_Store |
|||
ws |
|||
.gnupg |
|||
.pEp* |
|||
lib |
|||
local.conf |
|||
build/ |
|||
# Default ignored files |
|||
?idea/ |
|||
# Windows |
|||
build-windows/Debug/ |
|||
build-windows/Release/ |
|||
build-windows/libpEpAdapter.vcxproj.user |
|||
*.dSYM |
|||
*.db |
|||
/test/test_assert |
|||
/test/test_execmodes |
|||
/test/test_linear |
|||
/test/test_pitytree |
|||
/test/test_processdirs |
|||
/test/test_swarm |
@ -0,0 +1,34 @@ |
|||
# Copyright 2018, pEp Foundation
|
|||
# This file is part of libpEpAdapter
|
|||
# This file may be used under the terms of the GNU General Public License version 3
|
|||
# see LICENSE.txt
|
|||
|
|||
HERE:=$(dir $(lastword $(MAKEFILE_LIST))) |
|||
|
|||
TARGET=src/libPityTest11.a |
|||
|
|||
# Build config
|
|||
# Defaults
|
|||
DEBUG=1 |
|||
PREFIX?=$(HOME) |
|||
CXXFLAGS+=-std=c++11 -fPIC |
|||
|
|||
# Overrides
|
|||
-include $(HERE)local.conf |
|||
|
|||
# Constants
|
|||
CXXFLAGS+=-I$(PREFIX)/include |
|||
LDFLAGS+=-L$(PREFIX)/lib |
|||
|
|||
ifneq (,$(findstring g++,$(CXX))) |
|||
CXXFLAGS+=-fdiagnostics-color=always |
|||
else ifneq (,$(findstring clang,$(CXX))) |
|||
CXXFLAGS+=-fcolor-diagnostics |
|||
endif |
|||
|
|||
ifeq ($(DEBUG),1) |
|||
CXXFLAGS+=-g -O0 |
|||
else |
|||
CXXFLAGS+=-DNDEBUG=1 -O3 |
|||
endif |
|||
|
@ -0,0 +1,12 @@ |
|||
# This is an Example build config file (local.conf) |
|||
# you might not need this file, but if the defaults dont work for you |
|||
# You can override them here. |
|||
# Tweak the values to your needs and rename it to local.conf |
|||
|
|||
######### C++ Compiler ######### |
|||
# Should work with clang and g++ |
|||
# CXX=g++ |
|||
# DEBUG=1 # DEBUG Build (Default) |
|||
# DEBUG=0 # RELEASE Build |
|||
|
|||
# PREFIX=$(HOME)/local |
@ -0,0 +1,270 @@ |
|||
#include "test_utils.hh" |
|||
|
|||
#include <pEp/pEpEngine.h> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/keymanagement.h> |
|||
#include <pEp/identity_list.h> |
|||
#include <pEp/Adapter.hh> |
|||
#include <pEp/status_to_string.hh> |
|||
#include <pEp/mime.h> |
|||
#include <tuple> |
|||
|
|||
namespace pEp { |
|||
namespace Test { |
|||
namespace Utils { |
|||
|
|||
//Ident
|
|||
pEpIdent wrap(::pEp_identity *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdent(ident, [](::pEp_identity *) {}); |
|||
return ret; |
|||
} |
|||
|
|||
pEpIdent appropriate(::pEp_identity *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdent(ident, ::free_identity); |
|||
return ret; |
|||
} |
|||
|
|||
pEpIdent dup(const ::pEp_identity *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdent(::identity_dup(ident), ::free_identity); |
|||
return ret; |
|||
} |
|||
|
|||
pEpIdent kill(::pEp_identity *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdent(::identity_dup(ident), ::free_identity); |
|||
::free_identity(ident); |
|||
return ret; |
|||
} |
|||
|
|||
//IdentityList
|
|||
pEpIdentList wrap(::identity_list *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdentList(ident, [](::identity_list *) {}); |
|||
return ret; |
|||
} |
|||
|
|||
pEpIdentList appropriate(::identity_list *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdentList(ident, ::free_identity_list); |
|||
return ret; |
|||
} |
|||
|
|||
pEpIdentList dup(const ::identity_list *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdentList(::identity_list_dup(ident), ::free_identity_list); |
|||
return ret; |
|||
} |
|||
|
|||
pEpIdentList kill(::identity_list *const ident) |
|||
{ |
|||
assert(ident); |
|||
auto ret = pEpIdentList(::identity_list_dup(ident), ::free_identity_list); |
|||
::free_identity_list(ident); |
|||
return ret; |
|||
} |
|||
|
|||
//Message
|
|||
pEpMessage wrap(::message *const msg) |
|||
{ |
|||
assert(msg); |
|||
auto ret = pEpMessage(msg, [](::message *) {}); |
|||
return ret; |
|||
} |
|||
|
|||
pEpMessage appropriate(::message *const msg) |
|||
{ |
|||
assert(msg); |
|||
auto ret = pEpMessage(msg, ::free_message); |
|||
return ret; |
|||
} |
|||
|
|||
pEpMessage dup(const ::message *const msg) |
|||
{ |
|||
assert(msg); |
|||
auto ret = pEpMessage(::message_dup(msg), ::free_message); |
|||
return ret; |
|||
} |
|||
|
|||
pEpMessage kill(::message *const msg) |
|||
{ |
|||
assert(msg); |
|||
auto ret = pEpMessage(::message_dup(msg), ::free_message); |
|||
::free_message(msg); |
|||
return ret; |
|||
} |
|||
|
|||
// helpers
|
|||
pEpIdent createOwnIdent(const std::string &address) |
|||
{ |
|||
std::string name; |
|||
::pEp_identity *ident = nullptr; |
|||
ident = ::new_identity( |
|||
strdup(address.c_str()), |
|||
"", |
|||
PEP_OWN_USERID, |
|||
("myself " + address).c_str()); |
|||
ident->me = true; |
|||
|
|||
return appropriate(ident); |
|||
} |
|||
|
|||
pEpIdent createCptIdent(const std::string &address) |
|||
{ |
|||
std::string name; |
|||
::pEp_identity *ident = nullptr; |
|||
ident = ::new_identity( |
|||
strdup(address.c_str()), |
|||
"", |
|||
"", |
|||
("partner " + address).c_str()); |
|||
ident->me = false; |
|||
|
|||
return appropriate(ident); |
|||
} |
|||
|
|||
pEpIdent createRawIdent(const std::string &address) |
|||
{ |
|||
std::string name; |
|||
::pEp_identity *ident = nullptr; |
|||
ident = ::new_identity(strdup(address.c_str()), "", "", ""); |
|||
ident->me = false; |
|||
|
|||
return appropriate(ident); |
|||
} |
|||
|
|||
pEpIdentList createIdentityList(const std::vector<std::string> &addresses) |
|||
{ |
|||
::identity_list *list; |
|||
list = ::new_identity_list(nullptr); |
|||
for (std::string addr : addresses) { |
|||
::identity_list_add(list, ::identity_dup(createCptIdent(addr).get())); |
|||
} |
|||
return appropriate(list); |
|||
} |
|||
|
|||
pEpMessage createMessage(pEpIdent from, pEpIdent to, const std::string &longmsg) |
|||
{ |
|||
// create and fill in msg
|
|||
::message *msg = ::new_message(PEP_dir_outgoing); |
|||
msg->from = ::identity_dup(from.get()); |
|||
msg->to = ::new_identity_list(::identity_dup(to.get())); |
|||
msg->longmsg = strdup(longmsg.c_str()); |
|||
|
|||
pEpMessage ret = appropriate(msg); |
|||
return ret; |
|||
} |
|||
|
|||
pEpMessage createMessage(pEpIdent from, const std::string &to_addr, const std::string &longmsg) |
|||
{ |
|||
pEpIdent to_ident = createCptIdent(to_addr); |
|||
return createMessage(from, to_ident, longmsg); |
|||
} |
|||
|
|||
std::string mimeEncode(const pEpMessage msg) |
|||
{ |
|||
char *mimetext; |
|||
PEP_STATUS status = ::mime_encode_message(msg.get(), false, &mimetext, false); |
|||
throw_status(status); |
|||
std::string text{ mimetext }; |
|||
free(mimetext); |
|||
return text; |
|||
} |
|||
|
|||
pEpMessage mimeDecode(const std::string &mime_text) |
|||
{ |
|||
::message *msg; |
|||
bool has_possible_pEp_msg; |
|||
::PEP_STATUS status = ::mime_decode_message( |
|||
mime_text.c_str(), |
|||
mime_text.length(), |
|||
&msg, |
|||
&has_possible_pEp_msg); |
|||
throw_status(status); |
|||
return pEpMessage(msg, ::free_message); |
|||
} |
|||
|
|||
EncryptResult encryptMessage(const pEpMessage msg) |
|||
{ |
|||
pEpMessage msg_out; |
|||
bool could_encrypt = false; |
|||
::message *msgenc = nullptr; |
|||
PEP_STATUS status = ::encrypt_message( |
|||
Adapter::session(), |
|||
msg.get(), |
|||
nullptr, |
|||
&msgenc, |
|||
PEP_enc_PEP, |
|||
0); |
|||
throw_status(status); |
|||
::message *msg_out_p = nullptr; |
|||
if (msgenc != nullptr) { |
|||
could_encrypt = true; |
|||
msg_out = appropriate(msgenc); |
|||
} else { |
|||
could_encrypt = false; |
|||
msg_out = msg; |
|||
} |
|||
return EncryptResult(msg_out, "", could_encrypt); |
|||
} |
|||
|
|||
|
|||
DecryptResult decryptMessage(const pEpMessage msg, ::PEP_decrypt_flags_t *flags) |
|||
{ |
|||
pEpMessage msg_out; |
|||
bool was_encrypted = false; |
|||
|
|||
::message *dec{ nullptr }; |
|||
::stringlist_t *kl = ::new_stringlist(""); |
|||
::PEP_rating rating; |
|||
PEP_STATUS status = ::decrypt_message( |
|||
Adapter::session(), |
|||
msg.get(), |
|||
&dec, |
|||
&kl, |
|||
&rating, |
|||
flags); |
|||
throw_status(status); |
|||
if (dec != nullptr) { |
|||
was_encrypted = true; |
|||
msg_out = appropriate(dec); |
|||
} else { |
|||
was_encrypted = false; |
|||
msg_out = msg; |
|||
} |
|||
return DecryptResult(msg_out, rating, kl, flags, was_encrypted); |
|||
} |
|||
|
|||
DecryptResult decryptMessage(const pEpMessage msg) |
|||
{ |
|||
::PEP_decrypt_flags_t dummy{ 0 }; |
|||
return decryptMessage(msg, &dummy); |
|||
} |
|||
|
|||
EncryptResult encryptAndEncode(const pEpMessage msg) |
|||
{ |
|||
EncryptResult ret = encryptMessage(msg); |
|||
std::string mime_text = mimeEncode(std::get<0>(ret)); |
|||
std::get<1>(ret) = mime_text; |
|||
return ret; |
|||
} |
|||
|
|||
DecryptResult decryptAndDecode(const std::string &mime_data) |
|||
{ |
|||
DecryptResult ret; |
|||
pEpMessage rx_msg = mimeDecode(mime_data); |
|||
ret = decryptMessage(rx_msg); |
|||
return ret; |
|||
} |
|||
} // namespace Utils
|
|||
} // namespace Test
|
|||
} // namespace pEp
|
@ -0,0 +1,132 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef LIBPEPADAPTER_TEST_UTILS_HH |
|||
#define LIBPEPADAPTER_TEST_UTILS_HH |
|||
|
|||
#include <pEp/pEpLog.hh> |
|||
#include <string> |
|||
#include <exception> |
|||
#include <chrono> |
|||
#include <thread> |
|||
#include <cstring> |
|||
#include <tuple> |
|||
#include <pEp/pEpEngine.h> |
|||
#include <pEp/identity_list.h> |
|||
#include <pEp/message.h> |
|||
#include <pEp/message_api.h> |
|||
|
|||
// ------------------------------------------------------------------------------------------------
|
|||
|
|||
#ifndef ASSERT_EXCEPT |
|||
#define ASSERT_EXCEPT(func) \ |
|||
do { \ |
|||
try { \ |
|||
(func); \ |
|||
assert(false); \ |
|||
} catch (const exception &e) { \ |
|||
pEp::Adapter::pEpLog::log(nested_exception_to_string(e)); \ |
|||
} \ |
|||
} while (0) |
|||
#endif |
|||
|
|||
// ------------------------------------------------------------------------------------------------
|
|||
// Logging macros for testing
|
|||
// ------------------------------------------------------------------------------------------------
|
|||
// Use the macros if you need the message to be prefixed with "thread - __FILE__::__FUNTION__"
|
|||
// OTHERWISE, just use the logging functions from pEp::Adapter::pEpLog
|
|||
|
|||
// TESTLOG - logformat "thread - __FILE__::__FUNTION__ - <message>"
|
|||
// To be used in a non-class/object context
|
|||
#ifndef TESTLOG |
|||
#define TESTLOG(msg) \ |
|||
do { \ |
|||
std::stringstream msg_; \ |
|||
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \ |
|||
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \ |
|||
msg_ << " - " << msg; \ |
|||
pEp::Adapter::pEpLog::log(msg_.str()); \ |
|||
} while (0) |
|||
#endif // TESTLOG
|
|||
|
|||
// TESTLOGH1 - logformat "Thread - __FILE__::__FUNTION__ - <=============== message ==============>"
|
|||
#ifndef TESTLOGH1 |
|||
#define TESTLOGH1(msg) \ |
|||
do { \ |
|||
std::stringstream msg_; \ |
|||
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \ |
|||
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \ |
|||
msg_ << " - " << pEp::Adapter::pEpLog::decorateH1(msg); \ |
|||
pEp::Adapter::pEpLog::log(msg_.str()); \ |
|||
} while (0) |
|||
#endif // TESTLOGH1
|
|||
|
|||
// TESTLOGH2 - logformat "Thread - __FILE__::__FUNTION__ - <--------------- message -------------->"
|
|||
#ifndef TESTLOGH2 |
|||
#define TESTLOGH2(msg) \ |
|||
do { \ |
|||
std::stringstream msg_; \ |
|||
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \ |
|||
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \ |
|||
msg_ << " - " << pEp::Adapter::pEpLog::decorateH2(msg); \ |
|||
pEp::Adapter::pEpLog::log(msg_.str()); \ |
|||
} while (0) |
|||
#endif // TESTLOGH2
|
|||
|
|||
// ------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace pEp { |
|||
namespace Test { |
|||
namespace Utils { |
|||
using pEpIdent = std::shared_ptr<::pEp_identity>; |
|||
using pEpIdentList = std::shared_ptr<::identity_list>; |
|||
using pEpMessage = std::shared_ptr<::message>; |
|||
// [ DecryptedMessage, Rating, KeyList, Flags, WasEncrypted ]
|
|||
using DecryptResult = std:: |
|||
tuple<pEpMessage, ::PEP_rating, ::stringlist_t *, ::PEP_decrypt_flags_t *, bool>; |
|||
// [ EncryptedMessage, MimeText, couldEncrypt ]
|
|||
using EncryptResult = std::tuple<pEpMessage, std::string, bool>; |
|||
|
|||
// Datatypes
|
|||
//Ident
|
|||
pEpIdent wrap(::pEp_identity *const ident); |
|||
pEpIdent appropriate(::pEp_identity *const ident); |
|||
pEpIdent dup(const ::pEp_identity *const ident); |
|||
pEpIdent kill(::pEp_identity *const ident); |
|||
|
|||
//IdentityList
|
|||
pEpIdentList wrap(::identity_list *const ident); |
|||
pEpIdentList appropriate(::identity_list *const ident); |
|||
pEpIdentList dup(const ::identity_list *const ident); |
|||
pEpIdentList kill(::identity_list *const ident); |
|||
|
|||
//Message
|
|||
pEpMessage wrap(::message *const msg); |
|||
pEpMessage appropriate(::message *const msg); |
|||
pEpMessage dup(const ::message *const msg); |
|||
pEpMessage kill(::message *const msg); |
|||
|
|||
// helpers
|
|||
pEpIdent createOwnIdent(const std::string &address); |
|||
pEpIdent createCptIdent(const std::string &address); |
|||
pEpIdent createRawIdent(const std::string &address); |
|||
pEpIdentList createIdentityList(const std::vector<std::string> &addresses); |
|||
pEpMessage createMessage(pEpIdent from, pEpIdent to, const std::string &longmsg); |
|||
pEpMessage createMessage(pEpIdent from, const std::string &to_addr, const std::string &longmsg); |
|||
|
|||
|
|||
std::string mimeEncode(const pEpMessage msg); |
|||
pEpMessage mimeDecode(const std::string &mime_text); |
|||
|
|||
EncryptResult encryptMessage(const pEpMessage msg); |
|||
DecryptResult decryptMessage(const pEpMessage msg, ::PEP_decrypt_flags_t *flags); |
|||
DecryptResult decryptMessage(const pEpMessage msg); |
|||
|
|||
EncryptResult encryptAndEncode(const pEpMessage msg); |
|||
DecryptResult decryptAndDecode(const std::string &mime_data); |
|||
|
|||
} // namespace Utils
|
|||
} // namespace Test
|
|||
} // namespace pEp
|
|||
|
|||
#endif // LIBPEPADAPTER_TEST_UTILS_HH
|
Loading…
Reference in new issue