[OpenDocString] kdeconnect-kde (cpp)
testsocketlinereader.cpp
void TestSocketLineReader::init()
{
    m_packets.clear();
    m_server = new Server(this);

    QVERIFY2(m_server->listen(QHostAddress::LocalHost, 8694), "Failed to create local tcp server");

    m_timer.setInterval(4000); // For second is more enough to send some data via local socket
    m_timer.setSingleShot(true);
    connect(&m_timer, &QTimer::timeout, &m_loop, &QEventLoop::quit);

    m_conn = new QSslSocket(this);
    m_conn->connectToHost(QHostAddress::LocalHost, 8694);
    connect(m_conn, &QAbstractSocket::connected, &m_loop, &QEventLoop::quit);
    m_timer.start();
    m_loop.exec();

    QVERIFY2(m_conn->isOpen(), "Could not connect to local tcp server");
}
This creates a server and a connection to the local tcp server, and connects to it. It sets the timer interval and connects to the local socket using the SSL protocol. It also connects to the local tcp server using the SSL protocol, and starts the event loop.
void TestSocketLineReader::socketLineReader()
{
    QList dataToSend;
    dataToSend << "foobar\n"
               << "barfoo\n"
               << "foobar?\n"
               << "\n"
               << "barfoo!\n"
               << "panda\n";
    for (const QByteArray &line : qAsConst(dataToSend)) {
        m_conn->write(line);
    }
    m_conn->flush();

    int maxAttemps = 5;
    while (!m_server->hasPendingConnections() && maxAttemps > 0) {
        --maxAttemps;
        QTest::qSleep(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();

    /* remove the empty line before compare */
    dataToSend.removeOne("\n");

    QCOMPARE(m_packets.count(), 5); // We expect 5 Packets
    for (int x = 0; x < 5; ++x) {
        QCOMPARE(m_packets[x], dataToSend[x]);
    }
}
This tests that a socket gets opened and connects to the client, and receives 5 packets from the dataToSend list. It sends the data to the client, and waits for a connection to the client.
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.
void TestSocketLineReader::newPacket()
{
    int maxLoops = 5;
    while (m_reader->hasPacketsAvailable() && maxLoops > 0) {
        --maxLoops;
        const QByteArray packet = m_reader->readLine();
        if (!packet.isEmpty()) {
            m_packets.append(packet);
        }

        if (m_packets.count() == 5) {
            m_loop.exit();
        }
    }
}
This method creates a new packet from the socket line reader. It loops over the available packets and collects the packets. If the packet list is empty, it loops over and exits the loop.