diff --git a/CMakeLists.txt b/CMakeLists.txt index ab9106c..bd1d4ab 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ # cmake_minimum_required(VERSION 3.10) -project(Patronum) +project(Patronum LANGUAGES CXX) if(TARGET ${PROJECT_NAME}) message("The ${PROJECT_NAME} arledy included in main Project") return() @@ -15,6 +15,7 @@ endif() include(Patronum/QuasarAppLib/CMake/ccache.cmake) # Add sub directories add_subdirectory(Patronum) +add_subdirectory(Tests) include(Patronum/QuasarAppLib/CMake/QuasarAppCITargets.cmake) diff --git a/Patronum/src/PService.h b/Patronum/src/PService.h index bbe2a08..e1f8417 100644 --- a/Patronum/src/PService.h +++ b/Patronum/src/PService.h @@ -24,7 +24,7 @@ public: * @param argv - test of arguments * @param name - name of your service */ - Service(int argc, char **argv, const QString &name) + Service(int argc, const char *argv[], const QString &name) : QtService(argc, argv, name) { d_ptr = new ServicePrivate(name, this); @@ -37,7 +37,6 @@ protected: * Default inplementation send message abount error. */ void handleReceive(const QList &data) { - Q_UNUSED(data) auto list = supportedFeatures(); @@ -49,7 +48,12 @@ protected: QVariantMap result; - result["Error"] = "Wrong command!"; + QString commandList; + for (const auto&i : data ) { + commandList += i.toString() + " "; + } + + result["Error"] = "Wrong command! The commands : " + commandList + " is notsupported"; result["Available commands"] = stringList; sendResuylt(result); @@ -74,6 +78,15 @@ protected: return d_ptr->sendCmdResult(result); } + /** + * @brief sendResuylt this method send text responce to controller + * @param result - message + * @return true if data sendet is seccusseful + */ + bool sendResuylt(const QString &result) { + return d_ptr->sendCmdResult({{"Result:", result}}); + } + /** * @brief createApplication default implementation create a Application object and parse argumnts. * @param argc argumnts count diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..13275f4 --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,33 @@ +# +# Copyright (C) 2018-2020 QuasarApp. +# 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. +# + +cmake_minimum_required(VERSION 3.10) + +set(CURRENT_PROJECT ${PROJECT_NAME}Test) + +include(../Patronum/QuasarAppLib/CMake/ProjectOut.cmake) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Qt5 COMPONENTS Core Test REQUIRED) + +file(GLOB SOURCE_CPP + "*.cpp" +) + +add_executable(${CURRENT_PROJECT} ${SOURCE_CPP}) +target_link_libraries(${CURRENT_PROJECT} PRIVATE Qt5::Test Patronum) +target_include_directories(${CURRENT_PROJECT} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + +include(../Patronum/QuasarAppLib/CMake/QuasarAppCITargets.cmake) + +addTests("NetworkProtokol" ${CURRENT_PROJECT}) +initTests() diff --git a/Tests/defaultcontroller.cpp b/Tests/defaultcontroller.cpp new file mode 100644 index 0000000..a08ea36 --- /dev/null +++ b/Tests/defaultcontroller.cpp @@ -0,0 +1,7 @@ +#include "defaultcontroller.h" + +DefaultController::DefaultController(): + Patronum::Controller("TestPatronum") +{ + +} diff --git a/Tests/defaultcontroller.h b/Tests/defaultcontroller.h new file mode 100644 index 0000000..5be0e88 --- /dev/null +++ b/Tests/defaultcontroller.h @@ -0,0 +1,11 @@ +#ifndef DEFAULTCONTROLLER_H +#define DEFAULTCONTROLLER_H +#include + +class DefaultController : public Patronum::Controller +{ +public: + DefaultController(); +}; + +#endif // DEFAULTCONTROLLER_H diff --git a/Tests/defaultservice.cpp b/Tests/defaultservice.cpp new file mode 100644 index 0000000..34c646f --- /dev/null +++ b/Tests/defaultservice.cpp @@ -0,0 +1,36 @@ +#include "defaultservice.h" + +const char* arg[] = { + "/", + "-e" +}; + +DefaultService::DefaultService(): + Patronum::Service(0, arg, "TestPatronum") { + +} + +void DefaultService::start() { + QuasarAppUtils::Params::log("Server started!", QuasarAppUtils::Info); +} + +void DefaultService::handleReceive(const QList &data) { + + QList notSupportedList; + for (const auto& i : data) { + if (i.cmd() == "ping") { + sendResuylt("pomg"); + } else { + notSupportedList += i; + } + } + + Patronum::Service::handleReceive(notSupportedList); +} + +QList DefaultService::supportedFeatures() { + QList res; + res += Patronum::Feature("ping", {}, "test ping", "ping"); + + return res; +} diff --git a/Tests/defaultservice.h b/Tests/defaultservice.h new file mode 100644 index 0000000..c5ca539 --- /dev/null +++ b/Tests/defaultservice.h @@ -0,0 +1,21 @@ +#ifndef DEFAULTSERVICE_H +#define DEFAULTSERVICE_H +#include + + +class DefaultService : public Patronum::Service +{ +public: + DefaultService(); + + // QtServiceBase interface +protected: + void start(); + + // IService interface +public: + void handleReceive(const QList &data); + QList supportedFeatures(); +}; + +#endif // DEFAULTSERVICE_H diff --git a/Tests/testutils.cpp b/Tests/testutils.cpp new file mode 100644 index 0000000..fe633cc --- /dev/null +++ b/Tests/testutils.cpp @@ -0,0 +1,111 @@ +#include "testutils.h" + +#include +#include +#include +#include +#include + +bool funcPrivate(std::function requestFunc, + NP::BaseNode* node, + SP* responce = nullptr, + QHostAddress *responceSender = nullptr) { + + bool received = false; + QMetaObject::Connection m_connection; + m_connection = QObject::connect(node, &NP::BaseNode::incomingData, + [ &received, responce, responceSender] + (SP pkg, + const QHostAddress& sender) { + + received = true; + + if (responce) { + *responce = pkg; + } + + if (responceSender) { + *responceSender = sender; + } + + }); + + if (!requestFunc()) { + return false; + } + + if (!TestUtils::wait(received, 10000)) + return false; + + QObject::disconnect(m_connection); + + + return true; +} + + +bool funcPrivateConnect(std::function requestFunc, + NP::Client* node) { + + bool connected = false; + QMetaObject::Connection m_connection; + m_connection = QObject::connect(node, &NP::Client::statusChanged, + [ &connected](int new_status) { + + connected = NP::Client::Status::Online == static_cast(new_status); + + }); + + if (!requestFunc()) { + return false; + } + + TestUtils::wait(connected, 10900); + QObject::disconnect(m_connection); + + return connected; +} + +TestUtils::TestUtils() +{ + +} + +bool TestUtils::wait(const bool &forWait, int msec) { + auto curmsec = QDateTime::currentMSecsSinceEpoch() + msec; + while (curmsec > QDateTime::currentMSecsSinceEpoch() && !forWait) { + QCoreApplication::processEvents(); + } + QCoreApplication::processEvents(); + return forWait; +} + +bool TestUtils::loginFunc( + NP::Client *cli, + const QString& login, + const QByteArray& pass, + bool sendResult, + bool loginResult) { + + auto wraper = [cli, login, pass](){return cli->login(login, pass);}; + bool result = funcPrivate(wraper, cli); + + if (!result) { + return !sendResult; + } + + return loginResult == (cli->status() == NP::Client::Logined); +} + +bool TestUtils::connectFunc( + NP::Client *cli, + const QString& address, + unsigned short port) { + + auto wraper = [&cli, address, port](){ + cli->setHost(QHostAddress(address), port); + return cli->connectClient(); + }; + + return funcPrivateConnect(wraper, cli); +} diff --git a/Tests/testutils.h b/Tests/testutils.h new file mode 100644 index 0000000..34fa88b --- /dev/null +++ b/Tests/testutils.h @@ -0,0 +1,32 @@ +#ifndef TESTUTILS_H +#define TESTUTILS_H + +#include + +namespace NP { + class Client; +} +class TestUtils +{ +public: + TestUtils(); + static bool wait(const bool &forWait, int msec); + static bool loginFunc(NP::Client *cli, + const QString &login, + const QByteArray &pass, + bool sendResult, + bool loginResult); + + static bool connectFunc(NP::Client *cli, + const QString &address, + unsigned short port); +// static bool getState(ServerProtocol::Client &cli, QVariantMap &state); +// static bool unBanFunc(ServerProtocol::Client &cli, const QHostAddress &address); +// static bool banFunc(ServerProtocol::Client &cli, const QHostAddress &address); +// static bool reconnectFunc(ClientProtocol::Client &cli); +// static bool registerFunc(ClientProtocol::Client &cli, const QString &login, + // const QByteArray &pass, bool sendResult, bool loginResult); + +}; + +#endif // TESTUTILS_H diff --git a/Tests/tst_testsnakeserver.cpp b/Tests/tst_testsnakeserver.cpp new file mode 100644 index 0000000..f713849 --- /dev/null +++ b/Tests/tst_testsnakeserver.cpp @@ -0,0 +1,54 @@ +#include + +#include +#include +#include "testutils.h" + +#include + +#define TEST_LOCAL_HOST "127.0.0.1" +#define TEST_PORT 27777 + +class testProtockol : public QObject +{ + Q_OBJECT + +private: + + +public: + testProtockol(); + + void connectTest(Patronum::Service *service, Patronum::Controller *terminal); + + ~testProtockol(); + +private slots: + void initTestCase(); + void testPakageData(); + void testBaseNode(); + void testUser(); + + +}; + +testProtockol::testProtockol() { + QuasarAppUtils::Params::setArg("verbose", 3); + +} + +void testProtockol::connectTest(NP::Client *cli, NP::BaseNode *serv) { + QVERIFY(serv->run(TEST_LOCAL_HOST, TEST_PORT)); + QVERIFY(TestUtils::connectFunc(cli, TEST_LOCAL_HOST, TEST_PORT)); +} + +testProtockol::~testProtockol() { + +} + +void testProtockol::initTestCase() { +} + +QTEST_APPLESS_MAIN(testProtockol) + +#include "tst_testsnakeserver.moc"