4
1
mirror of https://github.com/QuasarApp/Heart.git synced 2025-05-09 16:09:49 +00:00

fix testst

This commit is contained in:
Andrei Yankovich 2021-02-01 15:10:57 +03:00
parent c517955a58
commit ffabb9b2e9
18 changed files with 312 additions and 58 deletions

@ -13,7 +13,7 @@ if(TARGET ${PROJECT_NAME})
endif()
if(NOT DEFINED HEART_BUILD_LVL)
set(HEART_BUILD_LVL 2)
set(HEART_BUILD_LVL 1)
endif()
include(QuasarAppLib/CMake/ccache.cmake)

@ -23,9 +23,11 @@ namespace PKG {
*/
enum class UserRequestType: unsigned char {
/// Request to registration a new user.
SignIn,
SignUp,
/// Request to login an exists user.
Login,
LogIn,
/// Request to logout an exists user. This request remove the generated accsses token from server.
LogOut,
/// Remove all data of this user from server
Remove
};

@ -19,16 +19,16 @@ SingleServer::SingleServer()
}
RegisteruserResult SingleServer::registerNewUser(PKG::UserMember user,
UserOperationResult SingleServer::registerNewUser(PKG::UserMember user,
const AbstractNodeInfo *info) {
if (!db()) {
return RegisteruserResult::InternalError;
return UserOperationResult::InternalError;
}
auto localObject = db()->getObject(user);
if (localObject) {
return RegisteruserResult::UserExits;
return UserOperationResult::UserExits;
}
user.setAuthenticationData(hashgenerator(user.authenticationData()));
@ -38,37 +38,38 @@ RegisteruserResult SingleServer::registerNewUser(PKG::UserMember user,
addUpdatePermission(requester, user.dbAddress(), Permission::Write);
if (!db()->insertObject(QSharedPointer<decltype(user)>::create(user))) {
return RegisteruserResult::InternalError;
return UserOperationResult::InternalError;
};
return loginUser(user, info);
}
RegisteruserResult SingleServer::loginUser(PKG::UserMember user,
// To-Do fix me bug #12 https://github.com/QuasarApp/Heart/issues/12
UserOperationResult SingleServer::loginUser(PKG::UserMember user,
const AbstractNodeInfo *info) {
if (!db()) {
return RegisteruserResult::InternalError;
return UserOperationResult::InternalError;
}
auto localObject = db()->getObject(user);
if (!localObject) {
return RegisteruserResult::UserNotExits;
return UserOperationResult::UserNotExits;
}
if (localObject->authenticationData() != hashgenerator(user.authenticationData())) {
return RegisteruserResult::UserInvalidPasswoed;
return UserOperationResult::UserInvalidPasswoed;
}
auto nodeinfo = dynamic_cast<const BaseNodeInfo*>(info);
if (!nodeinfo)
return RegisteruserResult::InternalError;
return UserOperationResult::InternalError;
AccessToken token((nodeinfo->token().toBytes()));
if (token.isValid()) {
return RegisteruserResult::UserAlreadyLogged;
return UserOperationResult::UserAlreadyLogged;
}
token = localObject->token();
@ -79,17 +80,53 @@ RegisteruserResult SingleServer::loginUser(PKG::UserMember user,
auto editableNodeInfo = static_cast<BaseNodeInfo*>(getInfoPtr(info->networkAddress()));
if (!editableNodeInfo)
return RegisteruserResult::InternalError;
return UserOperationResult::InternalError;
editableNodeInfo->setToken(token);
user.setToken(token);
user.setAuthenticationData("");
if (!sendData(&user, info->networkAddress())) {
return RegisteruserResult::InternalError;
return UserOperationResult::InternalError;
};
return RegisteruserResult::Success;
return UserOperationResult::Success;
}
UserOperationResult SingleServer::loginOutUser(PKG::UserMember user,
const AbstractNodeInfo *info) {
if (!db()) {
return UserOperationResult::InternalError;
}
auto localObject = db()->getObject(user);
if (!localObject) {
return UserOperationResult::UserNotExits;
}
auto nodeinfo = dynamic_cast<const BaseNodeInfo*>(info);
if (!nodeinfo)
return UserOperationResult::InternalError;
AccessToken token((nodeinfo->token().toBytes()));
if (!token.isValid()) {
return UserOperationResult::UserNotLogged;
}
if (user.token() != token) {
return UserOperationResult::UserNotLogged;
}
auto mutableNodeInfo = dynamic_cast<BaseNodeInfo*>(getInfoPtr(info->networkAddress()));
if (!mutableNodeInfo)
return UserOperationResult::InternalError;
mutableNodeInfo->setToken(AccessToken{});
return UserOperationResult::Success;
}
AccessToken SingleServer::generateToken(int length) {
@ -134,7 +171,7 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
const Package &pkg,
const AbstractNodeInfo *sender) {
RegisteruserResult result = RegisteruserResult::InternalError;
UserOperationResult result = UserOperationResult::InternalError;
auto request = obj.dynamicCast<Request>();
@ -142,10 +179,12 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
return false;
}
if (request->getRequestCmd() == static_cast<quint8>(PKG::UserRequestType::Login)) {
if (request->getRequestCmd() == static_cast<quint8>(PKG::UserRequestType::LogIn)) {
result = loginUser(*obj.staticCast<PKG::UserMember>().data(), sender);
} else if (request->getRequestCmd() == static_cast<quint8>(PKG::UserRequestType::SignIn)) {
} else if (request->getRequestCmd() == static_cast<quint8>(PKG::UserRequestType::SignUp)) {
result = registerNewUser(*obj, sender);
} else if (request->getRequestCmd() == static_cast<quint8>(PKG::UserRequestType::LogOut)) {
result = loginOutUser(*obj, sender);
} else if (request->getRequestCmd() == static_cast<quint8>(PKG::UserRequestType::Remove)) {
auto requesterId = getSender(sender, obj.data());
@ -161,7 +200,7 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
switch (result) {
case RegisteruserResult::InternalError: {
case UserOperationResult::InternalError: {
QuasarAppUtils::Params::log("Internal error ocured in the loginUser or registerNewUser method.",
QuasarAppUtils::Error);
badRequest(sender->networkAddress(), pkg.hdr,
@ -176,7 +215,7 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
return false;
}
case RegisteruserResult::UserExits: {
case UserOperationResult::UserExits: {
badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InternalError,
@ -187,7 +226,7 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
}
case RegisteruserResult::UserNotExits: {
case UserOperationResult::UserNotExits: {
badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserNotExits,
@ -198,7 +237,7 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
}
case RegisteruserResult::UserInvalidPasswoed: {
case UserOperationResult::UserInvalidPasswoed: {
badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserInvalidPasswoed,
@ -209,7 +248,7 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
}
case RegisteruserResult::UserAlreadyLogged: {
case UserOperationResult::UserAlreadyLogged: {
badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserInvalidPasswoed,
@ -220,7 +259,18 @@ bool SingleServer::workWithUserRequest(const QSharedPointer<PKG::UserMember>& ob
}
case RegisteruserResult::Success: {
case UserOperationResult::UserNotLogged: {
badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserInvalidPasswoed,
"User is not Logged"
},
REQUEST_LOGIN_ERROR);
return true;
}
case UserOperationResult::Success: {
return true;
}
}

@ -23,9 +23,9 @@ class UserMember;
#define REQUEST_LOGIN_ERROR -1
/**
* @brief The RegisteruserResult enum
* @brief The UserOperationResult enum
*/
enum class RegisteruserResult {
enum class UserOperationResult {
/// User not registered because database not inited or other error occurred.
InternalError,
/// User not registered because user already exists.
@ -36,6 +36,8 @@ enum class RegisteruserResult {
UserInvalidPasswoed,
/// User Already Logged.
UserAlreadyLogged,
/// User is not Loggined.
UserNotLogged,
/// User registered successful.
Success
};
@ -68,11 +70,11 @@ protected:
* @param user This is data of new user.
* @param info This is info about requested client.
* @return status of registration a new user. For more information about result statuses see the RegisteruserResult enum.
* @return status of registration a new user. For more information about result statuses see the UserOperationResult enum.
*
* @note This method send userData with new token to the client.
*/
virtual RegisteruserResult registerNewUser(PKG::UserMember user,
virtual UserOperationResult registerNewUser(PKG::UserMember user,
const AbstractNodeInfo* info);
/**
@ -80,11 +82,19 @@ protected:
* @param user This is user data with name and password.
* For login use PKG::User::getID and PKG::User::authenticationData like userName and password hash.
* @param info This is info about requested client.
* @return status of login a user. For more information about result statuses see the RegisteruserResult enum.
* @return status of login a user. For more information about result statuses see the UserOperationResult enum.
*
* @note This method send userData with new token to the client.
*/
virtual RegisteruserResult loginUser(PKG::UserMember user, const AbstractNodeInfo* info);
virtual UserOperationResult loginUser(PKG::UserMember user, const AbstractNodeInfo* info);
/**
* @brief loginOutUser This method remove the generated accsses token from server.
* @param user This is network member data (user data)
* @param info This is info about requested client.
* @return status of operation. For more information about result statuses see the UserOperationResult enum.
*/
virtual UserOperationResult loginOutUser(PKG::UserMember user, const AbstractNodeInfo* info);
/**
* @brief generateToken This method generate a new toke.
@ -97,7 +107,9 @@ protected:
QByteArray hashgenerator(const QByteArray &data) override;
private:
bool workWithUserRequest(const QSharedPointer<PKG::UserMember> &obj, const Package &pkg, const AbstractNodeInfo *sender);
bool workWithUserRequest(const QSharedPointer<PKG::UserMember> &obj,
const Package &pkg,
const AbstractNodeInfo *sender);
};

@ -7,6 +7,7 @@
#include "singleserverclient.h"
#include "quasarapp.h"
#include "authrequest.h"
namespace QH {
@ -14,8 +15,31 @@ SingleServerClient::SingleServerClient() {
}
ParserResult SingleServerClient::parsePackage(const Package &pkg, const AbstractNodeInfo *sender) {
ParserResult SingleServerClient::parsePackage(const Package &pkg,
const AbstractNodeInfo *sender) {
auto parentResult = DataBaseNode::parsePackage(pkg, sender);
if (parentResult != QH::ParserResult::NotProcessed) {
return parentResult;
}
if (H_16<QH::PKG::UserMember>() == pkg.hdr.command) {
QH::PKG::UserMember obj(pkg);
if (!(obj.isValid() && obj.token().isValid())) {
return ParserResult::Error;
}
setMember(obj);
if (getStatus() == ClientStatus::Loginning)
setStatus(ClientStatus::Logined);
return QH::ParserResult::Processed;
}
return QH::ParserResult::NotProcessed;
}
ClientStatus SingleServerClient::getStatus() const {
@ -23,11 +47,106 @@ ClientStatus SingleServerClient::getStatus() const {
}
bool SingleServerClient::login(const QString &userId, const QString &rawPassword) {
if (getStatus() < ClientStatus::Connected) {
QuasarAppUtils::Params::log("You try make login on the offline client."
" Please wait of ClientStatus::Connected status.",
QuasarAppUtils::Error);
return false;
}
if (getStatus() < ClientStatus::Loginning) {
setStatus(ClientStatus::Loginning);
}
if (!p_login(userId, hashgenerator(rawPassword.toLatin1()))) {
return false;
};
return true;
}
bool SingleServerClient::logout() {
if (getStatus() < ClientStatus::Logined) {
QuasarAppUtils::Params::log("You try logout on the not Loggined client."
" Please wait of ClientStatus::Logined status.",
QuasarAppUtils::Error);
return false;
}
QH::PKG::AuthRequest request;
const auto &user = getMember();
request.setId(user.getId());
request.setRequest(PKG::UserRequestType::LogOut);
if (!sendData(&request, serverAddress())) {
return false;
};
return true;
}
bool SingleServerClient::signup(const QString &userId, const QString &rawPassword) {
if (getStatus() < ClientStatus::Connected) {
QuasarAppUtils::Params::log("You try make signup on the offline client."
" Please wait of ClientStatus::Connected status.",
QuasarAppUtils::Error);
return false;
}
if (getStatus() < ClientStatus::Loginning) {
setStatus(ClientStatus::Loginning);
}
if (!p_signup(userId, hashgenerator(rawPassword.toLatin1()))) {
return false;
};
return true;
}
bool SingleServerClient::removeUser() {
if (getStatus() < ClientStatus::Logined) {
QuasarAppUtils::Params::log("You try make remove on the not Loggined client."
" Please wait of ClientStatus::Logined status.",
QuasarAppUtils::Error);
return false;
}
QH::PKG::AuthRequest request;
const auto &user = getMember();
request.setId(user.getId());
request.setRequest(PKG::UserRequestType::Remove);
if (!sendData(&request, serverAddress())) {
return false;
};
return true;
}
bool SingleServerClient::connectToServer() {
if (getStatus() >= ClientStatus::Connected) {
QuasarAppUtils::Params::log("You try make connect on the online client."
" This client alredy connected to server.",
QuasarAppUtils::Error);
return false;
}
if (getStatus() < ClientStatus::Connecting) {
setStatus(ClientStatus::Connecting);
}
addNode(serverAddress());
return true;
}
void SingleServerClient::disconnectFromServer() {
if (getStatus() == ClientStatus::Dissconnected) {
return;
}
removeNode(serverAddress());
setStatus(ClientStatus::Dissconnected);
}
void SingleServerClient::setStatus(const ClientStatus &status) {
@ -43,10 +162,40 @@ void SingleServerClient::handleError(unsigned char, const QString &error) {
}
bool SingleServerClient::p_login(const QString &userId, const QByteArray &hashPassword) {
QH::PKG::AuthRequest request;
request.setId(userId);
request.setRequest(QH::PKG::UserRequestType::LogIn);
if (hashPassword.isEmpty()) {
const auto &member = getMember();
if (!member.token().isValid()) {
return false;
}
request.setToken(member.token());
}
return sendData(&request, serverAddress());
}
bool SingleServerClient::p_signIn(const QString &userId, const QByteArray &hashPassword) {
bool SingleServerClient::p_signup(const QString &userId, const QByteArray &hashPassword) {
QH::PKG::AuthRequest request;
request.setId(userId);
request.setAuthenticationData(hashPassword);
request.setRequest(QH::PKG::UserRequestType::SignUp);
return sendData(&request, serverAddress());
}
const PKG::UserMember &SingleServerClient::getMember() const {
return _member;
}
HostAddress SingleServerClient::serverAddress() const {
return HostAddress{"localhost", DEFAULT_PORT};
}
void SingleServerClient::setMember(const PKG::UserMember &member) {
_member = member;
}
}

@ -99,7 +99,6 @@ public:
*/
void disconnectFromServer();
signals:
/**
* @brief statusChanged This sigmnal emited when the client change an own status.
@ -115,6 +114,20 @@ protected:
*/
void setStatus(const ClientStatus &status);
/**
* @brief getMember This method return the UserMember object of loffined user.
* @return UserMember object.
*/
const PKG::UserMember &getMember() const;
/**
* @brief serverAddress This method return the addres of server.
* Override this method for change server address.
* Default implementation return the localhost address with the 3090 port.
* @return host of the server.
*/
virtual HostAddress serverAddress() const;
protected slots:
/**
* @brief handleError This handle method invoked when the client received the BadRequest from server. Ovveride this method for add actions for this event.
@ -126,7 +139,8 @@ protected slots:
private:
bool p_login(const QString &userId, const QByteArray &hashPassword = {});
bool p_signIn(const QString &userId, const QByteArray &hashPassword);
bool p_signup(const QString &userId, const QByteArray &hashPassword);
void setMember(const PKG::UserMember &member);
ClientStatus _status;
PKG::UserMember _member;

@ -24,7 +24,7 @@
namespace QH {
using namespace PKG;
bool SqlDBWriter::exec(QSqlQuery *sq,const QString& sqlFile) {
bool SqlDBWriter::exec(QSqlQuery *sq, const QString& sqlFile) {
QFile f(sqlFile);
bool result = true;
if (f.open(QIODevice::ReadOnly)) {

@ -1 +1 @@
Subproject commit bce113979b640dcf680a8d31ad1c20757fad8082
Subproject commit 810376b593479606e4c8841e70676bb1f5790e2c

@ -12,4 +12,9 @@
namespace QH {
bool init() {
initResources();
return true;
}
}

@ -21,6 +21,8 @@
#include "networknode.h"
#endif
inline void initResources() { Q_INIT_RESOURCE(ProtockolResusces); }
/**
* @brief The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart library.
* Usage:
@ -164,6 +166,24 @@ protected:
*\image html Async.svg width=800px
*/
namespace QH {
/**
* @brief init This method initialize default resources of the Heart Library.
* @warning Do not Forget invoke this method before using library.
*
* Example :
* @code {cpp}
* #include <heart.h>
* int main() {
* if (!QH::init()) {
* return 1;
* }
* // some code
* return 0
* }
* @endcode
* @return true if all resources initialize successful.
*/
bool init();
/** @brief The PKG namesapce - this namespace contains all default packages of the Heart library.
* If you want create a pool request for Heart Library with own implemented packages

@ -41,8 +41,8 @@ AbstractNodeTest::AbstractNodeTest() {
}
AbstractNodeTest::~AbstractNodeTest() {
delete _nodeA;
delete _nodeB;
_nodeA->softDelete();
_nodeB->softDelete();
}
void AbstractNodeTest::test() {

@ -1,6 +1,2 @@
#include "itest.h"
iTest::iTest()
{
}

@ -8,7 +8,8 @@
class iTest
{
public:
iTest();
iTest() = default;
virtual ~iTest() = default;
/**
* @brief test run tests of object.

@ -14,7 +14,6 @@
#include <hostaddress.h>
#include <keystorage.h>
#include <ping.h>
#include <qsecretrsa2048.h>
class TestingBaseClient: public QH::DataBaseNode {
@ -52,10 +51,9 @@ BaseNodeTest::BaseNodeTest() {
}
BaseNodeTest::~BaseNodeTest() {
delete _client1;
delete _client2;
delete _server;
_client1->softDelete();
_client2->softDelete();
_server->softDelete();
}
void BaseNodeTest::test() {
@ -72,7 +70,7 @@ bool BaseNodeTest::powerTest() {
return false;
}
delete _nodeAPtr;
_nodeAPtr->softDelete();
return true;
}

@ -32,8 +32,12 @@ bool testCase(const T& t) {
}
#define RUN_TEST_CASE(TYPE) \
if (!testCase(Case0{})) \
return false;
{ \
TYPE *tst = new TYPE(); \
if (!testCase(*tst)) \
return false; \
tst->softDelete(); \
}
bool DataBaseNodeUnitTests::test() {
RUN_TEST_CASE(Case0)

@ -19,6 +19,7 @@ class DataBaseNodeUnitTests: public iTest {
// Test interface
public:
bool test() override;
};
#endif // DBTESTS_H

@ -46,9 +46,9 @@ NetworkNodeTest::NetworkNodeTest() {
}
NetworkNodeTest::~NetworkNodeTest() {
delete _nodeA;
delete _nodeB;
delete _nodeC;
_nodeA->softDelete();
_nodeB->softDelete();
_nodeC->softDelete();
}
@ -70,7 +70,7 @@ bool NetworkNodeTest::powerTest() {
return false;
};
delete _nodeAPtr;
_nodeAPtr->softDelete();
return true;
}
@ -82,7 +82,7 @@ bool NetworkNodeTest::dbTest() {
return false;
}
delete node;
node->softDelete();
return true;
}

@ -40,6 +40,8 @@ private slots:
testProtockol::testProtockol() {
QH::init();
#if HEART_BUILD_LVL >= 0
_tests.push_back(new AbstractNodeTest);
#endif