Snake/SnakeServer/serverProtocolTests/tst_testsnakeserver.cpp

209 lines
3.9 KiB
C++
Raw Normal View History

2019-02-13 13:03:40 +03:00
#include <QtTest>
#include <cp.h>
#include <sp.h>
#include <thread>
#include <quasarapp.h>
#include <QCoreApplication>
#include <streamers.h>
#include <QCryptographicHash>
#include "randomobjectfactory.h"
2019-02-13 13:03:40 +03:00
// add necessary includes here
class testSankeServer : public QObject
{
Q_OBJECT
private:
void testPingServerProtockol();
2019-02-16 17:08:00 +03:00
2019-02-13 13:03:40 +03:00
void testPingClientProtockol();
void testLogin();
void testUserData();
void testGetItem();
void testApplyData();
void testNetworkClasses();
2019-02-13 13:03:40 +03:00
public:
testSankeServer();
~testSankeServer();
private slots:
void initTestCase();
void cleanupTestCase();
void testServerProtockol();
void testClientProtockol();
};
testSankeServer::testSankeServer()
{
}
testSankeServer::~testSankeServer()
{
}
void testSankeServer::initTestCase()
{
}
void testSankeServer::cleanupTestCase()
{
}
void testSankeServer::testPingServerProtockol()
{
QuasarAppUtils::Params::setEnable("verbose", true);
int argc =0;
char * argv[] = {nullptr};
QCoreApplication app(argc, argv);
auto serv = new ServerProtocol::Server(this);
QVERIFY(serv->run(DEFAULT_SERVER));
auto client = new ServerProtocol::Client(this);
bool isWork = false;
QObject::connect(client, &ServerProtocol::Client::sigIncommingData,
[&isWork, &app] (const QVariantMap& map) {
isWork = map["res"].toString() == "Pong";
app.exit(0);
});
ServerProtocol::Package pkg;
pkg.hdr.command = ServerProtocol::ping;
QVERIFY(client->sendPackage(pkg));
QTimer::singleShot(1000, [&app](){
app.exit(0);
});
app.exec();
QVERIFY(isWork);
delete serv;
delete client;
}
void testSankeServer::testPingClientProtockol() {
QuasarAppUtils::Params::setEnable("verbose", true);
int argc = 0;
char * argv[] = {nullptr};
QCoreApplication app(argc, argv);
auto serv = new ClientProtocol::Server(this);
QVERIFY(serv->run(LOCAL_SNAKE_SERVER, DEFAULT_SNAKE_PORT));
auto client = new ClientProtocol::Client(this);
bool isWork = false;
QObject::connect(client, &ClientProtocol::Client::sigIncommingData,
[&isWork, &app] (const QVariantMap& map) {
isWork = map["res"].toString() == "Pong";
app.exit(0);
});
ClientProtocol::Package pkg;
2019-02-15 23:36:59 +03:00
pkg.hdr.command = ClientProtocol::Ping;
2019-02-13 13:03:40 +03:00
QVERIFY(client->sendPackage(pkg));
QTimer::singleShot(1000, [&app](){
app.exit(0);
});
app.exec();
QVERIFY(isWork);
delete serv;
delete client;
2019-02-16 17:08:00 +03:00
}
void testSankeServer::testLogin() {
ClientProtocol::Client cle;
auto pass = QCryptographicHash::hash("testpass", QCryptographicHash::Sha256);
QVERIFY(cle.login("Test@gmail.com", pass));
2019-02-16 17:08:00 +03:00
}
2019-02-13 13:03:40 +03:00
void testSankeServer::testUserData() {
ClientProtocol::Client cle;
QVERIFY(!cle.updateData());
auto token = QCryptographicHash::hash("testtoken", QCryptographicHash::Sha256);
cle._token = token;
2019-02-16 17:08:00 +03:00
QVERIFY(cle.updateData());
2019-02-16 17:08:00 +03:00
}
void testSankeServer::testGetItem() {
ClientProtocol::Client cle;
QVERIFY(!cle.updateData());
auto token = QCryptographicHash::hash("testtoken", QCryptographicHash::Sha256);
cle._token = token;
QVERIFY(cle.getItem(0));
}
void testSankeServer::testApplyData()
{
ClientProtocol::Client cle;
QVERIFY(!cle.updateData());
auto token = QCryptographicHash::hash("testtoken", QCryptographicHash::Sha256);
cle._token = token;
QVERIFY(!cle.savaData(QVariantMap()));
QVariantMap data = RandomObjectFactory::build(ClientProtocol::NetworkClasses::Game);
QVERIFY(cle.savaData(data));
2019-02-13 13:03:40 +03:00
}
void testSankeServer::testServerProtockol() {
testPingServerProtockol();
}
void testSankeServer::testClientProtockol() {
testPingClientProtockol();
2019-02-16 17:08:00 +03:00
testLogin();
testGetItem();
testUserData();
testApplyData();
2019-02-13 13:03:40 +03:00
}
QTEST_APPLESS_MAIN(testSankeServer)
#include "tst_testsnakeserver.moc"