void TestSocketLineReader::badData()
{
const QList dataToSend = {"data1\n", "data"}; // does not end in a \n
for (const QByteArray &line : qAsConst(dataToSend)) {
m_conn->write(line);
}
m_conn->flush();
QSignalSpy spy(m_server, &QTcpServer::newConnection);
QVERIFY(m_server->hasPendingConnections() || spy.wait(1000));
QSslSocket *sock = m_server->nextPendingConnection();
QVERIFY2(sock != nullptr, "Could not open a connection to the client");
m_reader = new SocketLineReader(sock, this);
connect(m_reader, &SocketLineReader::readyRead, this, &TestSocketLineReader::newPacket);
m_timer.start();
m_loop.exec();
QCOMPARE(m_packets.count(), 1);
QCOMPARE(m_packets[0], dataToSend[0]);
}
This tests that the data buffer is not empty. It sends the data1\n to the socket m_conn, and opens a new connection to the client, and opens a new socket for reading data.
If the server has pending connections or the client is not connected, the method creates a new socket line reader and connects it to the client, and waits for the connection to be established. It also starts the timer and compares the received packets with the data sent by the client.
If the server is not connected, the data is sent to the dataToSend array, the data count is 1. Finally, it sends a packet to the client, which can be used to read data.
If the server has pending connections or the client is not connected, the method creates a new socket line reader, and connects it to the client, which will read the data from the client, and starts the timer.