Browse Source

Create project structure after extraction from libpEpAdapter repo

master
heck 4 years ago
parent
commit
33864efbb1
  1. 25
      .gitignore
  2. 38
      Makefile
  3. 34
      Makefile.conf
  4. 12
      local.conf.example
  5. 2
      src/AbstractPityUnit.cc
  6. 4
      src/AbstractPityUnit.hh
  7. 2
      src/PityModel.hh
  8. 4
      src/PityNode.hh
  9. 4
      src/PityPerspective.hh
  10. 2
      src/PitySwarm.hh
  11. 5
      src/PityTest.hh
  12. 2
      src/PityTransport.cc
  13. 2
      src/PityTransport.hh
  14. 4
      src/PityUnit.hh
  15. 2
      src/PityUnit.hxx
  16. 2
      src/fs_mutex.cc
  17. 270
      src/test_utils.cc
  18. 132
      src/test_utils.hh
  19. 2
      test/test_execmodes.cc
  20. 14
      test/test_pitytree.cc
  21. 4
      test/test_swarm.cc

25
.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

38
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

34
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

12
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

2
src/AbstractPityUnit.cc

@ -2,7 +2,7 @@
// see LICENSE.txt
#include "AbstractPityUnit.hh"
#include "../../../src/std_utils.hh"
#include <pEp/std_utils.hh>
#include <iostream>
#include <unistd.h>
#include <cstdlib>

4
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 <pEp/pEpLog.hh>
#include <pEp/std_utils.hh>
#include "fs_mutex.hh"
#include "PityTree.hh"
#include "PityTransport.hh"

2
src/PityModel.hh

@ -4,7 +4,7 @@
#ifndef PITYTEST_PITYMODEL_HH
#define PITYTEST_PITYMODEL_HH
#include "../../../src/pEpLog.hh"
#include <pEp/pEpLog.hh>
#include "PityNode.hh"
#include <vector>
#include <memory>

4
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 <pEp/pEpLog.hh>
#include "test_utils.hh"
namespace pEp {
namespace PityTest11 {

4
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 <pEp/pEpLog.hh>
#include "test_utils.hh"
#include "PityModel.hh"
#include <map>

2
src/PitySwarm.hh

@ -7,7 +7,7 @@
#include "PityModel.hh"
#include "PityUnit.hh"
#include "PityPerspective.hh"
#include "../../../src/pEpLog.hh"
#include <pEp/pEpLog.hh>
#include <vector>
#include <memory>
#include <functional>

5
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 (...) { \

2
src/PityTransport.cc

@ -1,6 +1,6 @@
#include "PityTransport.hh"
#include "PityUnit.hh"
#include "../../../src/std_utils.hh"
#include <pEp/std_utils.hh>
#include "iostream"
#include <random>
#include <fstream>

2
src/PityTransport.hh

@ -4,7 +4,7 @@
#ifndef PITYTEST_PITYTRANSPORT_HH
#define PITYTEST_PITYTRANSPORT_HH
#include "../../../src/pEpLog.hh"
#include <pEp/pEpLog.hh>
#include <vector>
#include <memory>
#include <unordered_map>

4
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 <pEp/pEpLog.hh>
#include <pEp/std_utils.hh>
#include "AbstractPityUnit.hh"
#include "fs_mutex.hh"
#include "PityTransport.hh"

2
src/PityUnit.hxx

@ -4,7 +4,7 @@
#ifndef PITYTEST_PITYUNIT_HXX
#define PITYTEST_PITYUNIT_HXX
#include "../../../src/std_utils.hh"
#include <pEp/std_utils.hh>
#include "PityUnit.hh"
#include <iostream>

2
src/fs_mutex.cc

@ -1,5 +1,5 @@
#include "fs_mutex.hh"
#include "../../../src/std_utils.hh"
#include <pEp/std_utils.hh>
#include<fstream>

270
src/test_utils.cc

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

132
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 <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

2
test/test_execmodes.cc

@ -1,5 +1,5 @@
#include "../src/PityTest.hh"
#include "../../../src/utils.hh"
#include <pEp/utils.hh>
#include <iostream>
using namespace std;

14
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 <pEp/std_utils.hh>
#include <pEp/utils.hh>
#include <iostream>
#include <exception>
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

4
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;

Loading…
Cancel
Save