Snake/SnakeServer/serverProtocolTests/tst_testsnakeserver.cpp

360 lines
7.7 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 <QCryptographicHash>
2019-04-17 15:16:11 +03:00
#include <sqldbwriter.h>
2019-03-05 00:48:32 +03:00
#include "factorynetobjects.h"
2019-02-13 13:03:40 +03:00
// add necessary includes here
class testSankeServer : public QObject
{
Q_OBJECT
private:
void testPingServerProtockol();
2019-03-14 19:21:37 +03:00
void testStateServerProtockol();
void testBanServerProtockol();
void testUnBanServerProtockol();
void testRestartServerProtockol();
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();
2019-04-12 17:25:17 +03:00
2019-02-13 13:03:40 +03:00
public:
testSankeServer();
void testSql();
2019-02-13 13:03:40 +03:00
~testSankeServer();
private slots:
void initTestCase();
void cleanupTestCase();
void testServerProtockol();
void testClientProtockol();
2019-04-12 17:25:17 +03:00
2019-02-13 13:03:40 +03:00
};
testSankeServer::testSankeServer()
{
}
testSankeServer::~testSankeServer()
{
}
void testSankeServer::initTestCase()
{
2019-04-29 14:39:48 +03:00
ClientProtocol::initClientProtockol();
2019-02-13 13:03:40 +03:00
}
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);
});
2019-03-10 17:30:34 +03:00
QVERIFY(client->ping());
2019-02-13 13:03:40 +03:00
QTimer::singleShot(1000, [&app](){
app.exit(0);
});
app.exec();
QVERIFY(isWork);
delete serv;
delete client;
}
2019-03-14 19:21:37 +03:00
void testSankeServer::testStateServerProtockol()
{
ServerProtocol::Client cle;
QVERIFY(cle.getState());
}
void testSankeServer::testBanServerProtockol()
{
ServerProtocol::Client cle;
QVERIFY(!cle.ban(QHostAddress()));
QVERIFY(cle.ban(QHostAddress("192.192.192.192")));
}
void testSankeServer::testUnBanServerProtockol()
{
ServerProtocol::Client cle;
QVERIFY(!cle.unBan(QHostAddress()));
QVERIFY(cle.unBan(QHostAddress("192.192.192.192")));
}
void testSankeServer::testRestartServerProtockol()
{
ServerProtocol::Client cle;
QVERIFY(!cle.restart("lolo", 0));
QVERIFY(!cle.restart("192.168.1.999", 0));
QVERIFY(!cle.restart("192.168.1.99", 0));
QVERIFY(!cle.restart("192.168.1.9", 0));
QVERIFY(!cle.restart("-1.168.1.999", 77));
QVERIFY(!cle.restart("192.168.-1.99", 7777));
QVERIFY(cle.restart("192.168.1.9", 3456));
}
2019-02-13 13:03:40 +03:00
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,
2019-04-29 14:39:48 +03:00
[&isWork, &app] (const ClientProtocol::Command cmd,
const QByteArray&) {
2019-02-13 13:03:40 +03:00
2019-04-29 14:39:48 +03:00
isWork = cmd == ClientProtocol::Command::Ping;
2019-02-13 13:03:40 +03:00
app.exit(0);
});
2019-03-05 11:11:46 +03:00
QVERIFY(client->ping());
2019-02-13 13:03:40 +03:00
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-03-02 16:47:26 +03:00
cle._online = true;
2019-02-16 17:08:00 +03:00
QVERIFY(cle.updateData());
2019-02-16 17:08:00 +03:00
}
void testSankeServer::testGetItem() {
2019-03-02 16:47:26 +03:00
ClientProtocol::Client cle;
QVERIFY(!cle.updateData());
auto token = QCryptographicHash::hash("testtoken", QCryptographicHash::Sha256);
cle._token = token;
2019-03-02 16:47:26 +03:00
cle._online = true;
2019-03-02 16:47:26 +03:00
QVERIFY(cle.getItem(1));
}
2019-03-02 16:47:26 +03:00
void testSankeServer::testApplyData() {
ClientProtocol::Client cle;
auto token = QCryptographicHash::hash("testtoken", QCryptographicHash::Sha256);
cle._token = token;
2019-03-02 16:47:26 +03:00
cle._online = true;
2019-03-05 00:48:32 +03:00
QVERIFY(!cle.savaData(QList<int>()));
2019-03-05 00:48:32 +03:00
QList<int> listData = {1};
2019-02-13 13:03:40 +03:00
2019-03-05 00:48:32 +03:00
QVERIFY(cle.savaData(listData));
2019-04-20 15:22:00 +03:00
2019-02-13 13:03:40 +03:00
}
2019-04-12 17:25:17 +03:00
void testSankeServer::testSql() {
2019-04-29 14:39:48 +03:00
// SqlDBWriter db;
// QFile::remove("./test.db");
2019-04-12 17:25:17 +03:00
2019-04-29 14:39:48 +03:00
// bool init = db.initDb("test.db", "./");
2019-04-29 14:39:48 +03:00
// if (!init) {
// QFile::remove("./test.db");
// }
2019-04-29 14:39:48 +03:00
// QVERIFY(init);
2019-04-29 14:39:48 +03:00
// QVariantMap tempItem;
2019-04-20 15:22:00 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(ClientProtocol::FactoryNetObjects::build(
// ClientProtocol::NetworkClasses::Snake, tempItem));
2019-04-29 14:39:48 +03:00
// QVERIFY(ClientProtocol::FactoryNetObjects::fillRandomData(tempItem));
2019-04-20 16:02:27 +03:00
2019-04-29 14:39:48 +03:00
// // TEST ITEM
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVariantMap resItem;
2019-04-15 17:12:22 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(db.saveItem(tempItem) < 0);
// tempItem["id"] = 0;
// int id = db.saveItem(tempItem);
2019-04-15 17:12:22 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(id == 0);
// QVERIFY(db.getItem(id, resItem));
// QVERIFY(tempItem.size() == resItem.size());
2019-04-15 17:12:22 +03:00
2019-04-29 14:39:48 +03:00
// for (auto &key :tempItem.keys()) {
// QVERIFY(tempItem.value(key).toString() == resItem.value(key).toString());
// }
2019-04-15 17:12:22 +03:00
2019-04-29 14:39:48 +03:00
// auto list = tempItem["skillet"].toList();
// list[1] = 100;
// tempItem["skillet"] = list;
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(db.saveItem(tempItem) == 0);
// QVERIFY(db.getItem(id, resItem));
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(resItem["skillet"].toList()[1] == 100);
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(tempItem.size() == resItem.size());
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// for (auto &key :tempItem.keys()) {
// QVERIFY(tempItem.value(key).toString() == resItem.value(key).toString());
// }
// QSet<int> items;
// QVERIFY(!db.getAllItemsOfPalyer(0, items));
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// // TEST PLAYER
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVariantMap tempPlayer;
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(ClientProtocol::FactoryNetObjects::build(
// ClientProtocol::NetworkClasses::Player, tempPlayer));
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(ClientProtocol::FactoryNetObjects::fillRandomData(tempPlayer));
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVariantMap resPlayer;
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(db.saveItem(tempPlayer) < 0);
// tempPlayer["id"] = 0;
// QVERIFY(db.saveItem(tempPlayer) < 0);
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// tempPlayer["items"] = QVariantList() << 0;
// tempPlayer["currentSnake"] = 0;
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// id = db.savePlayer(tempPlayer);
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// QVERIFY(id == 0);
// QVERIFY(db.getPlayer(id, resPlayer));
// QVERIFY(tempPlayer.size() == resPlayer.size());
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
// for (auto &key :tempPlayer.keys()) {
// QVERIFY(tempPlayer.value(key).toString() == resPlayer.value(key).toString());
// }
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
//// list = tempPlayer["skillet"].toList();
//// list[1] = 100;
//// tempItem["skillet"] = list;
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
//// QVERIFY(db.saveItem(tempItem) == 0);
//// QVERIFY(db.getItem(id, resItem));
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
//// QVERIFY(resItem["skillet"].toList()[1] == 100);
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
//// QVERIFY(tempItem.size() == resItem.size());
2019-04-22 13:03:30 +03:00
2019-04-29 14:39:48 +03:00
//// for (auto &key :tempItem.keys()) {
//// QVERIFY(tempItem.value(key).toString() == resItem.value(key).toString());
//// }
2019-04-22 13:03:30 +03:00
2019-04-12 17:25:17 +03:00
}
2019-02-13 13:03:40 +03:00
void testSankeServer::testServerProtockol() {
testPingServerProtockol();
2019-03-14 19:21:37 +03:00
auto serv = new ServerProtocol::Server(this);
QVERIFY(serv->run(DEFAULT_SERVER));
testStateServerProtockol();
testBanServerProtockol();
testUnBanServerProtockol();
testRestartServerProtockol();
2019-02-13 13:03:40 +03:00
}
void testSankeServer::testClientProtockol() {
testPingClientProtockol();
2019-02-16 17:08:00 +03:00
2019-03-02 16:47:26 +03:00
auto serv = new ClientProtocol::Server(this);
QVERIFY(serv->run(LOCAL_SNAKE_SERVER, DEFAULT_SNAKE_PORT));
testLogin();
testGetItem();
testUserData();
testApplyData();
testSql();
2019-02-13 13:03:40 +03:00
}
QTEST_APPLESS_MAIN(testSankeServer)
#include "tst_testsnakeserver.moc"