easyssl/tests/units/cryptotest.h

63 lines
1.9 KiB
C
Raw Normal View History

2023-03-26 21:13:39 +02:00
//#
//# Copyright (C) 2020-2023 QuasarApp.
2023-03-26 21:25:15 +02:00
//# Distributed under the GPLv3 software license, see the accompanying
2023-03-26 21:13:39 +02:00
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef CRYPTO_TEST_H
#define CRYPTO_TEST_H
#include "test.h"
#include "testutils.h"
#include <QtTest>
#include <easyssl/icrypto.h>
template <class TestClass>
class CryptoTest: public Test, protected TestUtils
{
public:
void test() override {
2023-07-13 23:41:51 +02:00
// test short messges
testImpl("Test");
//test long messages
2023-07-18 22:01:06 +02:00
#ifdef Q_OS_UNIX
2023-07-14 16:33:48 +02:00
const int Mb = 1024 * 1024; //1 mb
2023-07-18 22:01:06 +02:00
#else
const int Mb = 1024 * 100; //100 kb
#endif
2023-07-13 23:41:51 +02:00
testImpl(QByteArray(Mb, 'c'));
} ;
void testImpl(const QByteArray& message) const {
2023-03-26 21:13:39 +02:00
// create a publick and private keys array.
QByteArray pub, priv;
TestClass crypto;
QVERIFY2(crypto.makeKeys(pub, priv), "Failed to generate keys pair.");
QVERIFY2(pub.size(), "Publick key should be generated successfull");
QVERIFY2(priv.size(), "Private key should be generated successfull");
if (crypto.supportedFeatures() & EasySSL::ICrypto::Features::Signing) {
2023-07-13 23:41:51 +02:00
auto siganture = crypto.signMessage(message, priv);
2023-03-26 21:13:39 +02:00
QVERIFY2(siganture.size(), "Siganture of the message should not be empty");
2023-07-13 23:41:51 +02:00
QVERIFY2(crypto.checkSign(message, siganture, pub), "failed to check message");
2023-03-26 21:13:39 +02:00
}
if (crypto.supportedFeatures() & EasySSL::ICrypto::Features::Encription) {
2023-07-13 23:41:51 +02:00
auto encriptedMsg = crypto.encrypt(message, pub);
2023-03-26 21:13:39 +02:00
QVERIFY2(encriptedMsg.size(), "Encripted message should not be empty");
auto decryptedMsg = crypto.decrypt(encriptedMsg, priv);
2023-07-13 23:41:51 +02:00
QVERIFY2(decryptedMsg == message, "Failed to check message after decryption");
2023-03-26 21:13:39 +02:00
}
2023-07-13 23:41:51 +02:00
}
2023-03-26 21:13:39 +02:00
};
#endif // CRYPTO_TEST_H