File indexing completed on 2025-01-05 04:37:21

0001 /*
0002     SPDX-FileCopyrightText: 2012 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QtTest>
0009 #include <interfaces/peerinterface.h>
0010 #include <peer/packetreader.h>
0011 #include <util/log.h>
0012 
0013 class PacketReaderTest : public QObject, public bt::PeerInterface
0014 {
0015     Q_OBJECT
0016 public:
0017     PacketReaderTest(QObject *parent = nullptr)
0018         : QObject(parent)
0019         , bt::PeerInterface(bt::PeerID(), 100)
0020     {
0021     }
0022 
0023     void chunkAllowed(bt::Uint32 chunk) override
0024     {
0025         Q_UNUSED(chunk);
0026     }
0027 
0028     void handlePacket(const bt::Uint8 *packet, bt::Uint32 size) override
0029     {
0030         received_packet.reset(new bt::Uint8[size]);
0031         memcpy(received_packet.data(), packet, size);
0032         received_packet_size = size;
0033     }
0034 
0035     bool check(const bt::Uint8 *packet, bt::Uint32 size)
0036     {
0037         return received_packet_size == size && memcmp(packet, received_packet.data(), size) == 0;
0038     }
0039 
0040     bt::Uint32 averageDownloadSpeed() const override
0041     {
0042         return 0;
0043     }
0044 
0045     void kill() override
0046     {
0047     }
0048 
0049     void reset()
0050     {
0051         received_packet_size = 0;
0052         received_packet.reset();
0053     }
0054 
0055 private Q_SLOTS:
0056     void initTestCase()
0057     {
0058         bt::InitLog("packetreadertest.log");
0059     }
0060 
0061     void cleanupTestCase()
0062     {
0063     }
0064 
0065     void testOnePacket()
0066     {
0067         reset();
0068 
0069         bt::Uint8 data[] = {0, 0, 0, 10, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE};
0070         bt::PacketReader pr(1024);
0071 
0072         pr.onDataReady(data, 14);
0073         QVERIFY(pr.ok());
0074         pr.update(*this);
0075         QVERIFY(check(data + 4, 10));
0076     }
0077 
0078     void testMultiplePackets()
0079     {
0080         reset();
0081 
0082         bt::Uint8 data[] = {0, 0, 0, 10, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE};
0083         bt::Uint8 data2[] = {0, 0, 0, 10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
0084         bt::PacketReader pr(1024);
0085 
0086         pr.onDataReady(data, 14);
0087         QVERIFY(pr.ok());
0088         pr.update(*this);
0089         QVERIFY(check(data + 4, 10));
0090 
0091         pr.onDataReady(data2, 14);
0092         QVERIFY(pr.ok());
0093         pr.update(*this);
0094         QVERIFY(check(data2 + 4, 10));
0095     }
0096 
0097     void testChunked()
0098     {
0099         reset();
0100 
0101         bt::Uint8 data[] = {0, 0, 0, 10, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE};
0102         bt::PacketReader pr(1024);
0103 
0104         pr.onDataReady(data, 7);
0105         QVERIFY(pr.ok());
0106         pr.update(*this);
0107         QVERIFY(received_packet_size == 0);
0108 
0109         pr.onDataReady(data + 7, 7);
0110         QVERIFY(pr.ok());
0111         pr.update(*this);
0112         QVERIFY(check(data + 4, 10));
0113     }
0114 
0115     void testIncompleteLength()
0116     {
0117         reset();
0118 
0119         bt::Uint8 data[] = {0, 0, 0, 10, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE};
0120         bt::PacketReader pr(1024);
0121 
0122         pr.onDataReady(data, 3);
0123         QVERIFY(pr.ok());
0124         pr.update(*this);
0125         QVERIFY(received_packet_size == 0);
0126 
0127         pr.onDataReady(data + 3, 11);
0128         QVERIFY(pr.ok());
0129         pr.update(*this);
0130         QVERIFY(check(data + 4, 10));
0131     }
0132 
0133     void testLengthToLarge()
0134     {
0135         reset();
0136 
0137         bt::Uint8 data[] = {0, 0, 0, 10, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE};
0138         bt::PacketReader pr(7);
0139 
0140         pr.onDataReady(data, 14);
0141         QVERIFY(!pr.ok());
0142 
0143         pr.update(*this);
0144         QVERIFY(received_packet_size == 0);
0145     }
0146 
0147     void testUnicodeLiteral()
0148     {
0149         QString a = QString("%1Torrent").arg(QChar(0x00B5));
0150         QVERIFY(a == QStringLiteral("µTorrent"));
0151     }
0152 
0153 private:
0154     QScopedArrayPointer<bt::Uint8> received_packet;
0155     bt::Uint32 received_packet_size;
0156 };
0157 
0158 QTEST_MAIN(PacketReaderTest)
0159 
0160 #include "packetreadertest.moc"