diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e47a3e0 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 9c55d18..c45fb54 100644 --- a/Makefile +++ b/Makefile @@ -1,34 +1,48 @@ -include ../../Makefile.conf +include Makefile.conf -TARGET=src/libPityTest.a -LDFLAGS:=-L../../src/ $(LDFLAGS) -L../framework/ -LDLIBS=-lstdc++ -lpEpAdapter -lpEpEngine -lpthread -ldl -CXXFLAGS:=-std=c++11 -g -I./src $(CXXFLAGS) -TEST_EXTRA_OBJS=../framework/test_utils.o +LDFLAGS:=$(LDFLAGS) +LDLIBS=-lstdc++ -lpthread -ldl -lpEpEngine -lpEpAdapter +CXXFLAGS:=-I./src $(CXXFLAGS) # Src SRC=$(wildcard src/*.cc) OBJ=$(subst .cc,.o,$(SRC)) +HEADERS=$(wildcard src/*.hh src/*.hxx) # Tests TEST_SRC=$(wildcard test/*.cc) TEST_OBJ=$(subst .cc,,$(TEST_SRC)) +DEPENDS=$(subst .cc,.d,$(SRC_CXX)) +CXXFLAGS+= -MMD -MP -.PHONY: all clean rmtestdata -.DEFAULT_GOAL := all +ifneq ($(MAKECMDGOALS),clean) + -include $(DEPENDS) +endif +.PHONY: all install uninstall clean + +#.DEFAULT_GOAL := all all: $(TARGET) test -$(TARGET): $(OBJ) $(TEST_EXTRA_OBJS) +$(TARGET): $(OBJ) $(AR) -rc $@ $^ - test : $(TEST_OBJ) -$(TEST_OBJ): $(OBJ) $(TEST_EXTRA_OBJS) +$(TEST_OBJ): $(OBJ) + + +install: $(TARGET) + mkdir -p $(PREFIX)/include/pEp/pitytest11 + mkdir -p $(PREFIX)/lib + cp -v $(HEADERS) $(PREFIX)/include/pEp/pitytest11/ + cp -v $(TARGET) $(PREFIX)/lib/ +uninstall: + cd $(PREFIX)/include/pEp/pitytest11 && rm -vf $(HEADERS) + cd $(PREFIX)/lib && rm -vf $(TARGET) clean: rm -f $(TARGET) @@ -36,4 +50,6 @@ clean: rm -f $(TEST_OBJ) rm -rf src/*.dSYM rm -rf test/*.dSYM + rm -f src/*.d + rm -f test/*.d diff --git a/Makefile.conf b/Makefile.conf new file mode 100644 index 0000000..6d3a5a3 --- /dev/null +++ b/Makefile.conf @@ -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 + diff --git a/local.conf.example b/local.conf.example new file mode 100644 index 0000000..964a1d3 --- /dev/null +++ b/local.conf.example @@ -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 diff --git a/src/AbstractPityUnit.cc b/src/AbstractPityUnit.cc index b504384..1f18fcd 100644 --- a/src/AbstractPityUnit.cc +++ b/src/AbstractPityUnit.cc @@ -2,7 +2,7 @@ // see LICENSE.txt #include "AbstractPityUnit.hh" -#include "../../../src/std_utils.hh" +#include #include #include #include diff --git a/src/AbstractPityUnit.hh b/src/AbstractPityUnit.hh index 4c6b944..9406b4a 100644 --- a/src/AbstractPityUnit.hh +++ b/src/AbstractPityUnit.hh @@ -4,8 +4,8 @@ #ifndef PITYTEST_ABSTRACTPITYUNIT_HH #define PITYTEST_ABSTRACTPITYUNIT_HH -#include "../../../src/pEpLog.hh" -#include "../../../src/std_utils.hh" +#include +#include #include "fs_mutex.hh" #include "PityTree.hh" #include "PityTransport.hh" diff --git a/src/PityModel.hh b/src/PityModel.hh index aa38dfc..c553bc6 100644 --- a/src/PityModel.hh +++ b/src/PityModel.hh @@ -4,7 +4,7 @@ #ifndef PITYTEST_PITYMODEL_HH #define PITYTEST_PITYMODEL_HH -#include "../../../src/pEpLog.hh" +#include #include "PityNode.hh" #include #include diff --git a/src/PityNode.hh b/src/PityNode.hh index a18edc8..6941eec 100644 --- a/src/PityNode.hh +++ b/src/PityNode.hh @@ -4,8 +4,8 @@ #ifndef PITYTEST_PITYNODE_HH #define PITYTEST_PITYNODE_HH -#include "../../../src/pEpLog.hh" -#include "../../framework/test_utils.hh" +#include +#include "test_utils.hh" namespace pEp { namespace PityTest11 { diff --git a/src/PityPerspective.hh b/src/PityPerspective.hh index ce32ba4..3ca0429 100644 --- a/src/PityPerspective.hh +++ b/src/PityPerspective.hh @@ -4,8 +4,8 @@ #ifndef PITYTEST_PITYPERSPECTIVE_HH #define PITYTEST_PITYPERSPECTIVE_HH -#include "../../../src/pEpLog.hh" -#include "../../framework/test_utils.hh" +#include +#include "test_utils.hh" #include "PityModel.hh" #include diff --git a/src/PitySwarm.hh b/src/PitySwarm.hh index b26ae1e..4384cb4 100644 --- a/src/PitySwarm.hh +++ b/src/PitySwarm.hh @@ -7,7 +7,7 @@ #include "PityModel.hh" #include "PityUnit.hh" #include "PityPerspective.hh" -#include "../../../src/pEpLog.hh" +#include #include #include #include diff --git a/src/PityTest.hh b/src/PityTest.hh index 846ec44..3a35536 100644 --- a/src/PityTest.hh +++ b/src/PityTest.hh @@ -8,12 +8,13 @@ #include "PityModel.hh" #include "PitySwarm.hh" #include "PityPerspective.hh" +#include "AbstractPityUnit.hh" #ifndef PITYASSERT #define PITYASSERT(condition, msg) \ do { \ if (!(condition)) { \ - throw PityAssertException(msg); \ + throw pEp::PityTest11::PityAssertException(msg); \ } \ } while (0); #endif @@ -23,7 +24,7 @@ try { \ (func); \ PITYASSERT(false, msg); \ - } catch (const PityAssertException& pae) { \ + } catch (const pEp::PityTest11::PityAssertException& pae) { \ throw(pae); \ } catch (const std::exception& e) { \ } catch (...) { \ diff --git a/src/PityTransport.cc b/src/PityTransport.cc index d3fcc36..474657a 100644 --- a/src/PityTransport.cc +++ b/src/PityTransport.cc @@ -1,6 +1,6 @@ #include "PityTransport.hh" #include "PityUnit.hh" -#include "../../../src/std_utils.hh" +#include #include "iostream" #include #include diff --git a/src/PityTransport.hh b/src/PityTransport.hh index a5c8caa..0875482 100644 --- a/src/PityTransport.hh +++ b/src/PityTransport.hh @@ -4,7 +4,7 @@ #ifndef PITYTEST_PITYTRANSPORT_HH #define PITYTEST_PITYTRANSPORT_HH -#include "../../../src/pEpLog.hh" +#include #include #include #include diff --git a/src/PityUnit.hh b/src/PityUnit.hh index 2b91434..1a6ee17 100644 --- a/src/PityUnit.hh +++ b/src/PityUnit.hh @@ -4,8 +4,8 @@ #ifndef PITYTEST_PITYUNIT_HH #define PITYTEST_PITYUNIT_HH -#include "../../../src/pEpLog.hh" -#include "../../../src/std_utils.hh" +#include +#include #include "AbstractPityUnit.hh" #include "fs_mutex.hh" #include "PityTransport.hh" diff --git a/src/PityUnit.hxx b/src/PityUnit.hxx index 1ef51b2..81d9049 100644 --- a/src/PityUnit.hxx +++ b/src/PityUnit.hxx @@ -4,7 +4,7 @@ #ifndef PITYTEST_PITYUNIT_HXX #define PITYTEST_PITYUNIT_HXX -#include "../../../src/std_utils.hh" +#include #include "PityUnit.hh" #include diff --git a/src/fs_mutex.cc b/src/fs_mutex.cc index 42e8cb5..cd79f01 100644 --- a/src/fs_mutex.cc +++ b/src/fs_mutex.cc @@ -1,5 +1,5 @@ #include "fs_mutex.hh" -#include "../../../src/std_utils.hh" +#include #include diff --git a/src/test_utils.cc b/src/test_utils.cc new file mode 100644 index 0000000..85afad9 --- /dev/null +++ b/src/test_utils.cc @@ -0,0 +1,270 @@ +#include "test_utils.hh" + +#include +#include +#include +#include +#include +#include +#include +#include + +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 &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 \ No newline at end of file diff --git a/src/test_utils.hh b/src/test_utils.hh new file mode 100644 index 0000000..a40aa9f --- /dev/null +++ b/src/test_utils.hh @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// ------------------------------------------------------------------------------------------------ + +#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__ - " +// 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; + // [ EncryptedMessage, MimeText, couldEncrypt ] + using EncryptResult = std::tuple; + + // 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 &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 \ No newline at end of file diff --git a/test/test_execmodes.cc b/test/test_execmodes.cc index f6daa60..648158e 100644 --- a/test/test_execmodes.cc +++ b/test/test_execmodes.cc @@ -1,5 +1,5 @@ #include "../src/PityTest.hh" -#include "../../../src/utils.hh" +#include #include using namespace std; diff --git a/test/test_pitytree.cc b/test/test_pitytree.cc index 7288530..d43bf47 100644 --- a/test/test_pitytree.cc +++ b/test/test_pitytree.cc @@ -1,7 +1,7 @@ #include "../src/PityTree.hh" #include "../src/PityTest.hh" -#include "../../../src/std_utils.hh" -#include "../../../src/utils.hh" +#include +#include #include #include using namespace pEp; @@ -106,16 +106,6 @@ BNode *BNode::clone() // ----------------------------------------------------------------------------------------------- -void not_throwing() -{ - throw std::runtime_error{ "Fsd" }; -} - -void throwing() -{ - throw std::runtime_error{ "Fsd" }; -} - int main(int argc, char *argv[]) { // Create lone node diff --git a/test/test_swarm.cc b/test/test_swarm.cc index 0823825..848f378 100644 --- a/test/test_swarm.cc +++ b/test/test_swarm.cc @@ -1,7 +1,7 @@ #include "../src/PityTest.hh" -#include "../../../src/utils.hh" +//#include "../../../src/utils.hh" using namespace pEp; -using namespace pEp::Adapter; +//using namespace pEp::Adapter; using namespace pEp::PityTest11; using TextCTX = PityModel;