Skip to content

Commit

Permalink
✅ Fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierLDff committed Apr 12, 2021
1 parent 9d7b108 commit a335b3b
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions tests/ServerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@
#include <MySocket.hpp>

#include <gtest/gtest.h>
#include <QtTest/QTest>
#include <QtTest/QSignalSpy>
#include <QtCore/QTimer>
#include <QtCore/QObject>
#include <QtCore/QDebug>

class ServerTests : public ::testing::Test
{
public:
MyServer server;
MySocket client;

QTimer timer;

void echoTest(quint16 port)
{
QSignalSpy connectedSpy(&client, &MySocket::isConnectedChanged);
Expand All @@ -55,6 +61,62 @@ class ServerTests : public ::testing::Test
ASSERT_EQ(s, QString("My String"));
}
}

int connectedCount = 0;
bool clientSendError = false;
bool serverSendError = false;

void runFuzzClientDisconnection(quint16 port)
{
server.sendError = serverSendError;

QSignalSpy newClientSpy(&server, &MyServer::objectInserted);
client.setWatchdogPeriod(10);
// Send Echo counter every seconds
QObject::connect(&timer, &QTimer::timeout,
[this]()
{
if(client.isConnected())
{
if(clientSendError)
{
qDebug() << "Send Error";
Q_EMIT client.sendErrorString();
}
else
{
Q_EMIT client.sendString("Hello World");
}
}
});
QObject::connect(&client, &MySocket::isConnectedChanged,
[this](bool value)
{
if(value)
{
++connectedCount;
qDebug() << "Client Connected on port " << client.localPort();
}
});
server.onInserted([this](net::tcp::Socket* s)
{ qDebug() << "New Client connected " << s->peerAddress() << ":" << s->peerPort(); });

// server.start(port) can be called to listen from every interfaces
server.start("127.0.0.1", port);

client.setWatchdogPeriod(1);
client.start("127.0.0.1", port);

timer.start(10);

QTest::qWait(10000);
timer.stop();
// Give time to finish last connection
QTest::qWait(100);
qInfo() << "Connected Count : " << connectedCount;
ASSERT_TRUE(connectedCount > 100);
ASSERT_EQ(newClientSpy.count(), connectedCount);
}
};

TEST_F(ServerTests, echoTestMonoThread)
Expand Down Expand Up @@ -84,3 +146,75 @@ TEST_F(ServerTests, echoTestWorkerThreadClientServer)
client.setUseWorkerThread(true);
echoTest(30003);
}

TEST_F(ServerTests, fuzzDisconnectionClientServer)
{
clientSendError = true;
serverSendError = false;
server.setUseWorkerThread(false);
client.setUseWorkerThread(false);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionClientServerWorkerThreadClient)
{
clientSendError = true;
serverSendError = false;
server.setUseWorkerThread(true);
client.setUseWorkerThread(false);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionClientServerWorkerThreadServer)
{
clientSendError = true;
serverSendError = false;
server.setUseWorkerThread(false);
client.setUseWorkerThread(true);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionClientServerWorkerThreadClientServer)
{
clientSendError = true;
serverSendError = false;
server.setUseWorkerThread(true);
client.setUseWorkerThread(true);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionServerClient)
{
clientSendError = false;
serverSendError = true;
server.setUseWorkerThread(false);
client.setUseWorkerThread(false);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionServerClientWorkerThreadClient)
{
clientSendError = false;
serverSendError = true;
server.setUseWorkerThread(true);
client.setUseWorkerThread(false);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionServerClientWorkerThreadServer)
{
clientSendError = false;
serverSendError = true;
server.setUseWorkerThread(false);
client.setUseWorkerThread(true);
runFuzzClientDisconnection(30004);
}

TEST_F(ServerTests, fuzzDisconnectionServerClientWorkerThreadClientServer)
{
clientSendError = false;
serverSendError = true;
server.setUseWorkerThread(true);
client.setUseWorkerThread(true);
runFuzzClientDisconnection(30004);
}

0 comments on commit a335b3b

Please sign in to comment.