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

0001 /*
0002     SPDX-FileCopyrightText: 2009 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 <util/functions.h>
0010 #include <util/log.h>
0011 #include <utp/connection.h>
0012 #include <utp/remotewindow.h>
0013 
0014 using namespace utp;
0015 
0016 class RemoteWindowTest : public QObject, public utp::Retransmitter
0017 {
0018     Q_OBJECT
0019 public:
0020     QSet<bt::Uint16> retransmit_seq_nr;
0021     bool update_rtt_called;
0022     bool retransmit_ok;
0023 
0024     RemoteWindowTest(QObject *parent = nullptr)
0025         : QObject(parent)
0026         , update_rtt_called(false)
0027         , retransmit_ok(false)
0028     {
0029     }
0030 
0031     void updateRTT(const Header *hdr, bt::Uint32 packet_rtt, bt::Uint32 packet_size) override
0032     {
0033         Q_UNUSED(hdr);
0034         Q_UNUSED(packet_rtt);
0035         Q_UNUSED(packet_size);
0036         update_rtt_called = true;
0037     }
0038 
0039     void retransmit(PacketBuffer & /*packet*/, bt::Uint16 p_seq_nr) override
0040     {
0041         bt::Out(SYS_UTP | LOG_NOTICE) << "retransmit " << p_seq_nr << bt::endl;
0042         retransmit_ok = retransmit_seq_nr.contains(p_seq_nr);
0043     }
0044 
0045     void reset()
0046     {
0047         retransmit_seq_nr.clear();
0048         update_rtt_called = false;
0049         retransmit_ok = false;
0050     }
0051 
0052     bt::Uint32 currentTimeout() const override
0053     {
0054         return 1000;
0055     }
0056 
0057 private Q_SLOTS:
0058     void initTestCase()
0059     {
0060         bt::InitLog("remotewindowtest.log");
0061     }
0062 
0063     void cleanupTestCase()
0064     {
0065     }
0066 
0067     void init()
0068     {
0069         reset();
0070     }
0071 
0072     void testNormalUsage()
0073     {
0074         PacketBuffer pkt;
0075         pkt.fillDummyData(200);
0076 
0077         RemoteWindow wnd;
0078         wnd.addPacket(pkt, 1, bt::Now());
0079         QVERIFY(!wnd.allPacketsAcked());
0080         QVERIFY(wnd.numUnackedPackets() == 1);
0081 
0082         wnd.addPacket(pkt, 2, bt::Now());
0083         QVERIFY(wnd.numUnackedPackets() == 2);
0084 
0085         wnd.addPacket(pkt, 3, bt::Now());
0086         QVERIFY(wnd.numUnackedPackets() == 3);
0087 
0088         Header hdr;
0089         hdr.ack_nr = 1;
0090         hdr.wnd_size = 5000;
0091         wnd.packetReceived(&hdr, nullptr, this);
0092         QVERIFY(wnd.numUnackedPackets() == 2);
0093         QVERIFY(update_rtt_called);
0094 
0095         reset();
0096         hdr.ack_nr = 2;
0097         wnd.packetReceived(&hdr, nullptr, this);
0098         QVERIFY(wnd.numUnackedPackets() == 1);
0099         QVERIFY(update_rtt_called);
0100 
0101         reset();
0102         hdr.ack_nr = 3;
0103         wnd.packetReceived(&hdr, nullptr, this);
0104         QVERIFY(wnd.numUnackedPackets() == 0);
0105         QVERIFY(update_rtt_called);
0106     }
0107 
0108     void testMultipleAcks()
0109     {
0110         PacketBuffer pkt;
0111         pkt.fillDummyData(200);
0112 
0113         RemoteWindow wnd;
0114         wnd.addPacket(pkt, 1, bt::Now());
0115         QVERIFY(!wnd.allPacketsAcked());
0116         QVERIFY(wnd.numUnackedPackets() == 1);
0117 
0118         wnd.addPacket(pkt, 2, bt::Now());
0119         QVERIFY(wnd.numUnackedPackets() == 2);
0120 
0121         wnd.addPacket(pkt, 3, bt::Now());
0122         QVERIFY(wnd.numUnackedPackets() == 3);
0123 
0124         Header hdr;
0125         hdr.ack_nr = 3;
0126         hdr.wnd_size = 5000;
0127         wnd.packetReceived(&hdr, nullptr, this);
0128         QVERIFY(wnd.numUnackedPackets() == 0);
0129         QVERIFY(update_rtt_called);
0130     }
0131 
0132     void testSelectiveAck()
0133     {
0134         PacketBuffer pkt;
0135         pkt.fillDummyData(200);
0136 
0137         RemoteWindow wnd;
0138         wnd.addPacket(pkt, 1, bt::Now());
0139         QVERIFY(!wnd.allPacketsAcked());
0140         QVERIFY(wnd.numUnackedPackets() == 1);
0141 
0142         wnd.addPacket(pkt, 2, bt::Now());
0143         QVERIFY(wnd.numUnackedPackets() == 2);
0144 
0145         wnd.addPacket(pkt, 3, bt::Now());
0146         QVERIFY(wnd.numUnackedPackets() == 3);
0147 
0148         // Selectively ack 3
0149         bt::Uint8 sack_data[6];
0150         memset(sack_data, 0, 6);
0151         SelectiveAck sack;
0152         sack.length = 4;
0153         sack.extension = 0;
0154         sack.bitmask = sack_data + 2;
0155         Ack(&sack, 3);
0156 
0157         Header hdr;
0158         hdr.ack_nr = 0;
0159         hdr.wnd_size = 5000;
0160         wnd.packetReceived(&hdr, &sack, this);
0161         QVERIFY(wnd.numUnackedPackets() == 2);
0162         QVERIFY(update_rtt_called);
0163 
0164         reset();
0165 
0166         // Ack the rest
0167         hdr.ack_nr = 3;
0168         hdr.wnd_size = 5000;
0169         wnd.packetReceived(&hdr, nullptr, this);
0170         QVERIFY(wnd.numUnackedPackets() == 0);
0171         QVERIFY(update_rtt_called);
0172     }
0173 
0174     void testRetransmits()
0175     {
0176         reset();
0177         PacketBuffer pkt;
0178         pkt.fillDummyData(200);
0179 
0180         RemoteWindow wnd;
0181         for (bt::Uint32 i = 0; i < 4; i++) {
0182             wnd.addPacket(pkt, i + 1, bt::Now());
0183             QVERIFY(!wnd.allPacketsAcked());
0184             QVERIFY(wnd.numUnackedPackets() == i + 1);
0185         }
0186 
0187         // Selectively ack the last 3 packets
0188         bt::Uint8 sack_data[6];
0189         memset(sack_data, 0, 6);
0190         SelectiveAck sack;
0191         sack.length = 4;
0192         sack.extension = 0;
0193         sack.bitmask = sack_data + 2;
0194         Ack(&sack, 2);
0195         Ack(&sack, 3);
0196         Ack(&sack, 4);
0197         Header hdr;
0198         hdr.ack_nr = 0;
0199         hdr.wnd_size = 5000;
0200         retransmit_seq_nr.insert(1);
0201         wnd.packetReceived(&hdr, &sack, this);
0202         QVERIFY(wnd.numUnackedPackets() == 1);
0203         QVERIFY(update_rtt_called);
0204         QVERIFY(retransmit_ok);
0205     }
0206 
0207     void testMultipleRetransmits()
0208     {
0209         PacketBuffer pkt;
0210         pkt.fillDummyData(200);
0211 
0212         RemoteWindow wnd;
0213         for (bt::Uint32 i = 0; i < 4; i++) {
0214             wnd.addPacket(pkt, i + 1, bt::Now());
0215             QVERIFY(!wnd.allPacketsAcked());
0216             QVERIFY(wnd.numUnackedPackets() == i + 1);
0217         }
0218 
0219         // Selectively ack the last 3 packets
0220         bt::Uint8 sack_data[6];
0221         memset(sack_data, 0, 6);
0222         SelectiveAck sack;
0223         sack.length = 4;
0224         sack.extension = 0;
0225         sack.bitmask = sack_data + 2;
0226         Ack(&sack, 2);
0227         Ack(&sack, 3);
0228         Ack(&sack, 4);
0229         Header hdr;
0230         hdr.ack_nr = 0;
0231         hdr.wnd_size = 5000;
0232         retransmit_seq_nr.insert(1);
0233         wnd.packetReceived(&hdr, &sack, this);
0234         QVERIFY(wnd.numUnackedPackets() == 1);
0235         QVERIFY(update_rtt_called);
0236         QVERIFY(retransmit_ok);
0237     }
0238 
0239     void testMultipleRetransmits2()
0240     {
0241         PacketBuffer pkt;
0242         pkt.fillDummyData(200);
0243 
0244         RemoteWindow wnd;
0245         for (bt::Uint32 i = 0; i < 10; i++) {
0246             wnd.addPacket(pkt, i + 1, bt::Now());
0247             QVERIFY(!wnd.allPacketsAcked());
0248             QVERIFY(wnd.numUnackedPackets() == i + 1);
0249         }
0250 
0251         // Selectively ack the last 3 packets
0252         bt::Uint8 sack_data[6];
0253         memset(sack_data, 0, 6);
0254         SelectiveAck sack;
0255         sack.length = 4;
0256         sack.extension = 0;
0257         sack.bitmask = sack_data + 2;
0258         Ack(&sack, 8);
0259         Ack(&sack, 9);
0260         Ack(&sack, 10);
0261         Header hdr;
0262         hdr.ack_nr = 0;
0263         hdr.wnd_size = 5000;
0264         for (int i = 1; i < 8; i++)
0265             retransmit_seq_nr.insert(i);
0266         wnd.packetReceived(&hdr, &sack, this);
0267         QVERIFY(wnd.numUnackedPackets() == 7);
0268         QVERIFY(update_rtt_called);
0269         QVERIFY(retransmit_ok);
0270     }
0271 
0272     void testMultipleRetransmits3()
0273     {
0274         PacketBuffer pkt;
0275         pkt.fillDummyData(200);
0276 
0277         RemoteWindow wnd;
0278         for (bt::Uint32 i = 0; i < 10; i++) {
0279             wnd.addPacket(pkt, i + 1, bt::Now());
0280             QVERIFY(!wnd.allPacketsAcked());
0281             QVERIFY(wnd.numUnackedPackets() == i + 1);
0282         }
0283 
0284         // Selectively ack 3 random packets
0285         bt::Uint8 sack_data[6];
0286         memset(sack_data, 0, 6);
0287         SelectiveAck sack;
0288         sack.length = 4;
0289         sack.extension = 0;
0290         sack.bitmask = sack_data + 2;
0291         Ack(&sack, 3);
0292         Ack(&sack, 6);
0293         Ack(&sack, 10);
0294         Header hdr;
0295         hdr.ack_nr = 0;
0296         hdr.wnd_size = 5000;
0297         for (int i = 1; i <= 2; i++)
0298             retransmit_seq_nr.insert(i);
0299         wnd.packetReceived(&hdr, &sack, this);
0300         QVERIFY(wnd.numUnackedPackets() == 7);
0301         QVERIFY(update_rtt_called);
0302         QVERIFY(retransmit_ok);
0303     }
0304 
0305     void testWrapAround()
0306     {
0307         bt::Out(SYS_UTP | LOG_NOTICE) << "testWrapAround " << bt::endl;
0308         reset();
0309         PacketBuffer pkt;
0310         pkt.fillDummyData(200);
0311 
0312         RemoteWindow wnd;
0313         bt::Uint16 seq_nr = 65530;
0314         for (bt::Uint32 i = 0; i < 10; i++) {
0315             wnd.addPacket(pkt, seq_nr + i, bt::Now());
0316             QVERIFY(!wnd.allPacketsAcked());
0317             QVERIFY(wnd.numUnackedPackets() == i + 1);
0318         }
0319 
0320         // Selectively ack 3 random packets
0321         bt::Uint8 sack_data[6];
0322         memset(sack_data, 0, 6);
0323         SelectiveAck sack;
0324         sack.length = 4;
0325         sack.extension = 0;
0326         sack.bitmask = sack_data + 2;
0327         Ack(&sack, 3);
0328         Ack(&sack, 6);
0329         Ack(&sack, 10);
0330         Header hdr;
0331         hdr.ack_nr = seq_nr - 1;
0332         hdr.wnd_size = 5000;
0333         for (int i = 0; i < 2; i++)
0334             retransmit_seq_nr.insert(seq_nr + i);
0335         wnd.packetReceived(&hdr, &sack, this);
0336         QVERIFY(wnd.numUnackedPackets() == 7);
0337         QVERIFY(update_rtt_called);
0338         QVERIFY(retransmit_ok);
0339     }
0340 };
0341 
0342 QTEST_MAIN(RemoteWindowTest)
0343 
0344 #include "remotewindowtest.moc"