Heart/HeartTests/AbstractSpace/abstractnodetest.cpp

158 lines
3.3 KiB
C++
Raw Normal View History

2020-09-15 21:16:35 +03:00
#include "abstractnodetest.h"
#include "testutils.h"
#include <abstractnode.h>
#include <keystorage.h>
#include <ping.h>
#include <qsecretrsa2048.h>
2020-09-15 22:07:49 +03:00
class TestingClient: public QH::AbstractNode {
2020-09-15 21:16:35 +03:00
// AbstractNode interface
public:
2020-09-15 22:07:49 +03:00
const QH::Ping& getPing() const {
2020-09-15 21:16:35 +03:00
return _ping;
}
protected:
2020-09-15 22:07:49 +03:00
void incomingData(QH::AbstractData *pkg, const QH::HostAddress &sender) {
2020-09-15 21:16:35 +03:00
Q_UNUSED(sender);
2020-09-15 22:07:49 +03:00
auto ping = dynamic_cast<QH::Ping*>(pkg);
2020-09-15 21:16:35 +03:00
if (ping)
_ping.copyFrom(ping);
}
private:
2020-09-15 22:07:49 +03:00
QH::Ping _ping;
2020-09-15 21:16:35 +03:00
};
AbstractNodeTest::AbstractNodeTest() {
2020-09-15 22:07:49 +03:00
_nodeA = new QH::AbstractNode();
2020-09-15 21:16:35 +03:00
_nodeB = new TestingClient();
}
AbstractNodeTest::~AbstractNodeTest() {
delete _nodeA;
delete _nodeB;
}
void AbstractNodeTest::test() {
QVERIFY(connectTest());
QVERIFY(sendDataTest());
}
bool AbstractNodeTest::connectTest() {
if (!_nodeA->run(TEST_LOCAL_HOST, TEST_PORT)) {
return false;
}
return connectFunc(_nodeB, TEST_LOCAL_HOST, TEST_PORT);
}
bool AbstractNodeTest::sendDataTest() {
auto request = [this](){
2020-09-15 22:07:49 +03:00
return _nodeB->ping(QH::HostAddress(TEST_LOCAL_HOST, TEST_PORT));
2020-09-15 21:16:35 +03:00
};
auto client = dynamic_cast<TestingClient*>(_nodeB);
auto check = [client](){
return client->getPing().ansver();
};
return funcPrivateConnect(request, check);
}
template<class Crypto>
bool validationCrypto() {
// create crypto oject
2020-09-15 22:07:49 +03:00
auto crypto = new QH::KeyStorage(new Crypto());
2020-09-15 21:16:35 +03:00
// get test pair keys
auto keys = crypto->getNextPair("TEST_KEY");
// must be failed becouse crypto object still not inited.
if (keys.isValid()) {
delete crypto;
return false;
}
if (!crypto->initDefaultStorageLocation()) {
delete crypto;
return false;
}
// get test pair keys
keys = crypto->getNextPair("TEST_KEY");
// chekck keys
if (!keys.isValid()) {
delete crypto;
return false;
}
// remove crypto object, after remove crypto object most be save all generated keys
delete crypto;
// second initialisin of crypto object
2020-09-15 22:07:49 +03:00
crypto = new QH::KeyStorage(new Crypto());
2020-09-15 21:16:35 +03:00
if (!crypto->initDefaultStorageLocation()) {
delete crypto;
return false;
}
// check get generated key pair
if (keys != crypto->getNextPair("TEST_KEY", 0)) {
delete crypto;
return false;
}
QByteArray msg = "test_message";
// check sign data
if (!crypto->sign(&msg, keys.privKey())) {
delete crypto;
return false;
}
if (!crypto->check(msg, keys.publicKey())) {
delete crypto;
return false;
}
// check genesis generation of keys
auto ThisIsKey = crypto->getNextPair("key", "this is key");
auto ThisIsKey2 = crypto->getNextPair("key2", "this is key");
if (ThisIsKey != ThisIsKey2) {
delete crypto;
return false;
}
delete crypto;
2020-09-15 22:07:49 +03:00
crypto = new QH::KeyStorage(new Crypto());
2020-09-15 21:16:35 +03:00
if (!crypto->initDefaultStorageLocation()) {
delete crypto;
return false;
}
auto lastKeys = crypto->getNextPair("key2", RAND_KEY, 0);
return lastKeys == ThisIsKey2;
}
bool AbstractNodeTest::testICtypto() {
// check
2020-09-15 22:07:49 +03:00
if (!validationCrypto<QH::QSecretRSA2048>()) {
2020-09-15 21:16:35 +03:00
return false;
}
return true;
}