4
1
mirror of https://github.com/QuasarApp/Heart.git synced 2025-05-03 04:59:39 +00:00
Heart/HeartTests/units/ecdsaauthtest.cpp

101 lines
2.4 KiB
C++
Raw Normal View History

2022-02-10 20:06:39 +03:00
/*
2022-12-31 11:25:59 +03:00
* Copyright (C) 2022-2023 QuasarApp.
2022-02-10 20:06:39 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "ecdsaauthtest.h"
#include <QtTest>
2022-08-06 22:02:02 +03:00
#include <hcryptoFeatures/authecdsa.h>
#include <thread>
2022-02-22 17:02:14 +03:00
#ifdef USE_HEART_SSL
2022-02-10 20:06:39 +03:00
/*
* test class
*/
class ECDSA: public QH::AuthECDSA {
public:
ECDSA(const QByteArray &publicKey, const QByteArray &privKey) {
setPublicKey(publicKey);
_priv = privKey;
}
// AsyncKeysAuth interface
protected:
QByteArray getPrivateKey() const override {
return _priv;
};
private:
QByteArray _priv;
};
ECDSAAuthTest::ECDSAAuthTest() {
}
ECDSAAuthTest::~ECDSAAuthTest() {
}
void ECDSAAuthTest::test() {
2022-02-13 19:04:06 +03:00
// create a publick and private keys array.
2022-02-10 20:06:39 +03:00
QByteArray pub, priv;
2022-02-14 10:42:58 +03:00
QString userID;
2022-02-10 20:06:39 +03:00
2022-02-13 19:04:06 +03:00
// make public and private keys.
2022-02-10 20:06:39 +03:00
QVERIFY(QH::AuthECDSA::makeKeys(pub, priv));
2022-02-14 10:42:58 +03:00
// make user id
QString userIDOfPubKey = QCryptographicHash::hash(pub,
QCryptographicHash::Sha256).
2022-02-17 18:49:09 +03:00
toBase64(QByteArray::Base64UrlEncoding);
2022-02-14 10:42:58 +03:00
2022-02-13 19:04:06 +03:00
// check createed keys. should be larget then 0.
2022-02-10 20:06:39 +03:00
QVERIFY(pub.length() && priv.length());
2022-02-13 19:04:06 +03:00
// create test auth object using ecdsa algorithm
2022-02-10 20:06:39 +03:00
ECDSA edsa(pub, priv);
2022-02-13 19:04:06 +03:00
// The terst object should be invalid because it is not prepared.
2022-02-10 20:06:39 +03:00
QVERIFY(!edsa.isValid());
2022-02-13 19:04:06 +03:00
// the authetication should be failed bacause ecdsa class is invalid.
2022-02-14 10:42:58 +03:00
QVERIFY(!edsa.auth(600, &userID));
QVERIFY(userID.isEmpty());
2022-02-10 20:06:39 +03:00
2022-02-13 19:04:06 +03:00
// prepare an authentication object.
2022-02-10 20:06:39 +03:00
QVERIFY(edsa.prepare());
2022-02-13 19:04:06 +03:00
// the prepared object should be valid.
2022-02-10 20:06:39 +03:00
QVERIFY(edsa.isValid());
2022-02-13 19:04:06 +03:00
// authentication should be finished successful because auth object contains prepared valid signature.
2022-02-14 10:42:58 +03:00
QVERIFY(edsa.auth(600, &userID));
QVERIFY(userID == userIDOfPubKey);
// forget user id before new auth
userID.clear();
2022-02-13 19:04:06 +03:00
// authentication should be failed because the time range is depricated.
2022-02-14 10:42:58 +03:00
QVERIFY(!edsa.auth(0, &userID));
QVERIFY(userID.isEmpty());
// change subsribe time and try login.
edsa.setUnixTime(time(0) + 1);
std::this_thread::sleep_for(std::chrono::seconds(1));
// should be failed because signature is different of the time.
QVERIFY(!edsa.auth(600, &userID));
QVERIFY(userID.isEmpty());
2022-02-10 20:06:39 +03:00
2022-02-13 19:04:06 +03:00
2022-02-10 20:06:39 +03:00
}
2022-02-22 17:02:14 +03:00
#endif