Browse Source

Tests: test_tofu - refactor out the datatypes and utils

LIB-11
heck 1 year ago
parent
commit
420ec93e9e
3 changed files with 260 additions and 185 deletions
  1. +206
    -0
      test/framework/utils.cc
  2. +42
    -3
      test/framework/utils.hh
  3. +12
    -182
      test/test_tofu.cc

+ 206
- 0
test/framework/utils.cc View File

@ -1,2 +1,208 @@
#include "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>
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;
}
//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 createIdentity(const std::string &address, bool myself)
{
std::string name;
std::string id;
::pEp_identity *partner = nullptr;
if (myself) {
partner = ::new_identity(
strdup(address.c_str()),
"",
PEP_OWN_USERID,
("myself " + address).c_str());
partner->me = true;
} else {
partner = ::new_identity(
strdup(address.c_str()),
"",
"23",
("partner " + address).c_str());
partner->me = false;
}
return appropriate(partner);
}
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 = createIdentity(to_addr, false);
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);
}
pEpMessage encryptMessage(const pEpMessage msg)
{
pEpMessage ret;
::message *msgenc = nullptr;
PEP_STATUS status = ::encrypt_message(
Adapter::session(),
msg.get(),
nullptr,
&msgenc,
PEP_enc_PEP,
0);
throw_status(status);
::message *msg_out = nullptr;
if (msgenc != nullptr) {
ret = appropriate(msgenc);
} else {
ret = msg;
}
// unit.log("encryptAndSend: ENCRYPT - " + status_to_string(status));
// unit.log("encryptAndSend" + Utils::to_string(msg_out, false));
return ret;
}
pEpMessage decryptMessage(const pEpMessage msg)
{
pEpMessage ret;
::message *dec{ nullptr };
::stringlist_t *kl = ::new_stringlist("");
PEP_rating rating;
unsigned int flags{ 0 };
PEP_STATUS status = ::decrypt_message(
Adapter::session(),
msg.get(),
&dec,
&kl,
&rating,
&flags);
throw_status(status);
if (dec != nullptr) {
ret = appropriate(dec);
} else {
ret = msg;
// unit.log("NOT DECRYPTED");
}
// unit.log("decryptMIME" + status_to_string(status));
// unit.log("decryptMIME" + Utils::to_string(ret.get(), false));
return ret;
}
std::string encryptAndEncode(const pEpMessage msg)
{
std::string ret;
pEpMessage msg_enc = encryptMessage(msg);
ret = mimeEncode(msg_enc);
return ret;
}
pEpMessage decryptAndDecode(const std::string &mime_data)
{
pEpMessage ret;
pEpMessage rx_msg = mimeDecode(mime_data);
ret = decryptMessage(rx_msg);
return ret;
}
} // namespace Utils
} // namespace Test
} // namespace pEp

+ 42
- 3
test/framework/utils.hh View File

@ -11,6 +11,9 @@
#include <thread>
#include <unistd.h>
#include <pEp/pEpEngine.h>
#include <pEp/message.h>
// ------------------------------------------------------------------------------------------------
#ifndef ASSERT_EXCEPT
@ -37,7 +40,7 @@
#define TESTLOG(msg) \
do { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
msg_ << " - " << msg; \
pEp::Adapter::pEpLog::log(msg_.str()); \
@ -49,7 +52,7 @@
#define TESTLOGH1(msg) \
do { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
msg_ << " - " << pEp::Adapter::pEpLog::decorateH1(msg); \
pEp::Adapter::pEpLog::log(msg_.str()); \
@ -61,7 +64,7 @@
#define TESTLOGH2(msg) \
do { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
msg_ << " - " << pEp::Adapter::pEpLog::decorateH2(msg); \
pEp::Adapter::pEpLog::log(msg_.str()); \
@ -70,5 +73,41 @@
// ------------------------------------------------------------------------------------------------
namespace pEp {
namespace Test {
namespace Utils {
using pEpIdent = std::shared_ptr<::pEp_identity>;
using pEpMessage = std::shared_ptr<::message>;
// 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);
//Message
pEpMessage wrap(::message *const msg);
pEpMessage appropriate(::message *const msg);
pEpMessage dup(const ::message *const msg);
pEpMessage kill(::message *const msg);
// helpers
pEpIdent createIdentity(const std::string &address, bool myself);
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);
pEpMessage encryptMessage(const pEpMessage msg);
pEpMessage decryptMessage(const pEpMessage msg);
std::string encryptAndEncode(const pEpMessage msg);
pEpMessage decryptAndDecode(const std::string &mime_data);
} // namespace Utils
} // namespace Test
} // namespace pEp
#endif // LIBPEPADAPTER_TEST_UTILS_HH

+ 12
- 182
test/test_tofu.cc View File

@ -4,6 +4,7 @@
#include "pitytest11/src/PityPerspective.hh"
#include "../src/utils.hh"
#include "framework/framework.hh"
#include "framework/utils.hh"
#include <pEp/pEpEngine.h>
#include <pEp/message_api.h>
@ -15,180 +16,9 @@
using namespace pEp;
using namespace pEp::Adapter;
using namespace pEp::Test::Utils;
using namespace pEp::PityTest11;
using pEpIdent = std::shared_ptr<::pEp_identity>;
using pEpMessage = std::shared_ptr<::message>;
//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;
}
//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;
}
pEpIdent createIdentity(const std::string &address, bool myself)
{
std::string name;
std::string id;
::pEp_identity *partner = nullptr;
if (myself) {
partner = ::new_identity(
strdup(address.c_str()),
"",
PEP_OWN_USERID,
("myself " + address).c_str());
partner->me = true;
} else {
partner = ::new_identity(strdup(address.c_str()), "", "23", ("partner " + address).c_str());
partner->me = false;
}
return appropriate(partner);
}
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 = createIdentity(to_addr, false);
return createMessage(from, to_ident, longmsg);
}
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);
}
std::string mimeEncode(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;
}
void encryptAndSend(pEpIdent from, std::string to_addr, std::string longmsg, PityUnit<PityPerspective> &unit)
{
pEpMessage msg = createMessage(from, to_addr, longmsg);
::message *msgenc = nullptr;
// ::pEp_identity *receiver_ident = msg.get()->to->ident;
// {
// PEP_STATUS status = ::update_identity(Adapter::session(), receiver_ident);
// unit.log("encryptAndSend: UPDATE IDENT - " + status_to_string(status));
// }
// msg.get()->to = ::new_identity_list(receiver_ident);
PEP_STATUS status = ::encrypt_message(Adapter::session(), msg.get(), nullptr, &msgenc, PEP_enc_PEP, 0);
throw_status(status);
::message *msg_out = nullptr;
if(msgenc != nullptr ){
msg_out = msgenc;
} else {
msg_out = msg.get();
}
unit.log("encryptAndSend: ENCRYPT - " + status_to_string(status));
unit.log("encryptAndSend" + Utils::to_string(msg_out, false));
std::string mime_text = mimeEncode(wrap(msg_out));
unit.transport()->sendMsg(to_addr, mime_text);
}
pEpMessage receiveAndDecrypt(PityUnit<PityPerspective> &unit)
{
pEpMessage ret;
std::string rx_data = unit.transport()->receiveMsg();
pEpMessage rx_msg = mimeDecode(rx_data);
::message *dec{ nullptr };
::stringlist_t *kl = ::new_stringlist("");
PEP_rating rating;
unsigned int flags{ 0 };
PEP_STATUS status = ::decrypt_message(Adapter::session(), rx_msg.get(), &dec, &kl, &rating, &flags);
throw_status(status);
if (dec != nullptr) {
ret = appropriate(dec);
} else {
unit.log("NOT DECRYPTED");
ret = rx_msg;
}
unit.log("receiveAndDecrypt" + status_to_string(status));
unit.log("receiveAndDecrypt" + Utils::to_string(ret.get(), false));
return ret;
}
void tofu(PityUnit<PityPerspective> &unit, PityPerspective *ctx, bool init)
{
@ -203,28 +33,28 @@ void tofu(PityUnit<PityPerspective> &unit, PityPerspective *ctx, bool init)
pEpIdent my_ident = createIdentity(ctx->name, true);
::PEP_STATUS status = ::myself(Adapter::session(), my_ident.get());
pEp::throw_status(status);
// unit.log(status_to_string(status));
// unit.log(Utils::to_string(my_ident.get()));
// ACT
// Utils::readKey();
if (init) {
unit.log("Initiating TOFU...");
encryptAndSend(my_ident, ctx->partner, "INIT TOFU", unit);
pEpMessage msg = createMessage(my_ident, ctx->partner, "INIT TOFU");
std::string mime_data = encryptAndEncode(msg);
unit.transport()->sendMsg(ctx->partner, mime_data);
}
while (true) {
pEpMessage msg_rx = receiveAndDecrypt(unit);
std::string mime_data_rx = unit.transport()->receiveMsg();
pEpMessage msg_rx = decryptAndDecode(mime_data_rx);
unit.log(">");
//
pEpIdent rx_from = wrap(msg_rx->from);
std::string rx_longmessage = msg_rx->longmsg;
//
encryptAndSend(
pEpMessage msg = createMessage(
my_ident,
rx_from->address,
"REPLY[ " + std::string(rx_from->address) + " ] " + rx_longmessage,
unit);
"REPLY[ " + std::string(rx_from->address) + " ] " + rx_longmessage);
std::string mime_data_tx = encryptAndEncode(msg);
unit.transport()->sendMsg(rx_from->address, mime_data_tx);
unit.log("<");
Utils::sleep_millis(1000);
}


Loading…
Cancel
Save