Browse Source

documenting test framework

pull/1/head
Volker Birk 5 years ago
parent
commit
0426b8b8bb
  1. 14
      Makefile
  2. 12
      Makefile.conf
  3. 6
      test/Makefile
  4. 86
      test/framework.cc
  5. 33
      test/framework.hh

14
Makefile

@ -7,19 +7,7 @@ include Makefile.conf
TARGET=libpEpAdapter.a TARGET=libpEpAdapter.a
ifneq ($(wildcard local.conf),) .PHONY: install, uninstall, clean
$(info ================================================)
$(info Overrides in `local.conf` are used.)
$(info ================================================)
endif
ifdef BUILD_CONFIG
$(info ================================================)
$(info Overrides in `$(BUILD_CONFIG)` are used.)
$(info ================================================)
endif
.PHONY: all, lib, test, install, uninstall, clean
SOURCE=$(wildcard *.cc) SOURCE=$(wildcard *.cc)
HEADERS=$(wildcard *.hh *.hxx) HEADERS=$(wildcard *.hh *.hxx)

12
Makefile.conf

@ -7,7 +7,7 @@ HERE:=$(dir $(lastword $(MAKEFILE_LIST)))
PREFIX?=$(HOME) PREFIX?=$(HOME)
CXXFLAGS+=-std=c++11 -fPIC -O0 CXXFLAGS+=-std=c++11 -fPIC
# Build target # Build target
BUILD_FOR:=$(shell uname) BUILD_FOR:=$(shell uname)
@ -19,13 +19,11 @@ else ifneq (,$(findstring clang,$(CXX)))
endif endif
# Debug or Release build # Debug or Release build
DEBUG=1
ifeq ($(DEBUG),1) ifndef NDEBUG
$(info Debug build (set DEBUG=0 for release build)) CXXFLAGS+=-g -O0
CXXFLAGS+=-g
else else
$(info Release Build (set DEBUG=1 for debug build)) CXXFLAGS+=-DNDEBUG -O2
CXXFLAGS+=-DNDEBUG=1
endif endif
######### Engine ######### ######### Engine #########

6
test/Makefile

@ -11,8 +11,14 @@ all: $(TST)
$(TST): framework.o $(TST): framework.o
.PHONY: clean rmtestdata
clean: clean:
rm -f $(TST) rm -f $(TST)
rm -Rf *.dSYM rm -Rf *.dSYM
rm -f *.o rm -f *.o
rm -Rf /tmp/test_pEp.* rm -Rf /tmp/test_pEp.*
rmtestdata:
rm -Rf /tmp/test_pEp.*

86
test/framework.cc

@ -4,29 +4,44 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <utility> #include <utility>
#include <exception>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include <pEp/mime.h>
pEp::Test::Transport pEp::Test::transport;
std::string pEp::Test::path;
namespace pEp { namespace pEp {
namespace Test { namespace Test {
using namespace Adapter; using namespace Adapter;
void setup(vector<string>& a) void setup(vector<string>& args)
{ {
#ifdef WIN32
string dir = getenv("TEMP");
dir += "\\test_pEp.XXXXXXXXXXXX";
#else
string dir = "/tmp/test_pEp.XXXXXXXXXXXX"; string dir = "/tmp/test_pEp.XXXXXXXXXXXX";
#endif
if (a.size() > 1) { if (args.size() > 1) {
if (a[1] == "--help") { if (args[1] == "--help") {
cout << "usage: " << a[0] << " [--dir HOME]" << endl; #ifdef WIN32
cout << "usage: " << args[0] << " [--dir LOCALAPPDATA]" << endl;
#else
cout << "usage: " << args[0] << " [--dir HOME]" << endl;
#endif
exit(0); exit(0);
} }
else if (a[1] == "--dir" && a.size() == 3) { else if (args[1] == "--dir" && args.size() == 3) {
dir = a[2]; dir = args[2];
} }
else { else {
cerr << "illegal parameter" << endl; cerr << "illegal parameter" << endl;
@ -34,22 +49,27 @@ namespace pEp {
} }
} }
char path[MAXPATHLEN+1]; char _path[MAXPATHLEN+1];
const char *templ = dir.c_str(); const char *templ = dir.c_str();
strcpy(path, templ); strcpy(_path, templ);
mkdtemp(path); mkdtemp(_path);
chdir(path); chdir(_path);
setenv("HOME", path, 1); #ifdef WIN32
setenv("LOCALAPPDATA", _path, 1);
#else
setenv("HOME", _path, 1);
#endif
path = _path;
cerr << "test directory: " << path << endl; cerr << "test directory: " << path << endl;
} }
void setup(int argc, char **argv) void setup(int argc, char **argv)
{ {
vector<string> a{(size_t) argc}; vector<string> args{(size_t) argc};
for (int i=0; i<argc; ++i) for (int i=0; i<argc; ++i)
a[i] = argv[i]; args[i] = argv[i];
setup(a); setup(args);
} }
void import_key_from_file(string filename) void import_key_from_file(string filename)
@ -61,5 +81,43 @@ namespace pEp {
assert(status == PEP_KEY_IMPORTED); assert(status == PEP_KEY_IMPORTED);
::free_identity_list(il); ::free_identity_list(il);
} }
Message make_message(::message *msg)
{
return shared_ptr<::message>(msg, ::free_message);
}
Message make_message(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);
throw_status(status);
return make_message(msg);
}
string make_string(Message msg)
{
char *mimetext;
PEP_STATUS status = ::mime_encode_message(msg.get(), false, &mimetext, false);
throw_status(status);
string text = mimetext;
free(mimetext);
return text;
}
Message Transport::recv()
{
mkdir(inbox_path.c_str(), 0770);
auto msg = make_message(nullptr);
return msg;
}
void Transport::send(Message msg)
{
mkdir(outbox_path.c_str(), 0770);
}
}; };
}; };

33
test/framework.hh

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "Adapter.hh" #include "Adapter.hh"
@ -9,9 +10,39 @@ namespace pEp {
namespace Test { namespace Test {
using namespace std; using namespace std;
void setup(vector<string>& a); // manually set up test
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); void import_key_from_file(string filename);
using Message = shared_ptr<::message>;
// use this instead of constructor to auto assign ::free_message as
// deleter
Message make_message(::message *msg);
// MIME parser
Message make_message(string text);
// MIME composer
string make_string(Message msg);
struct Transport {
string inbox_path = "inbox";
string outbox_path = "outbox";
// reads next message from inbox
Message recv();
// appends message to outbox
void send(Message msg);
};
extern Transport transport;
extern string path;
}; };
}; };

Loading…
Cancel
Save