File indexing completed on 2025-01-05 04:37:32
0001 /* 0002 SPDX-FileCopyrightText: 2011 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QtTest> 0008 #include <util/circularbuffer.h> 0009 #include <util/log.h> 0010 #include <utp/packetbuffer.h> 0011 #include <utp/utpprotocol.h> 0012 0013 using namespace bt; 0014 0015 class PacketBufferTest : public QObject 0016 { 0017 Q_OBJECT 0018 public: 0019 void initTestCase() 0020 { 0021 bt::InitLog("packetbuffertest.log"); 0022 } 0023 0024 void cleanupTestCase() 0025 { 0026 } 0027 0028 private Q_SLOTS: 0029 void testPacketBuffer() 0030 { 0031 bt::Uint8 tmp[200]; 0032 memset(tmp, 0xFF, 200); 0033 0034 bt::CircularBuffer cbuf; 0035 cbuf.write(tmp, 200); 0036 0037 utp::PacketBuffer pbuf; 0038 QVERIFY(pbuf.fillData(cbuf, 200) == 200); 0039 QVERIFY(pbuf.payloadSize() == 200); 0040 QVERIFY(pbuf.bufferSize() == 200); 0041 QVERIFY(memcmp(pbuf.data(), tmp, 200) == 0); 0042 0043 utp::Header hdr; 0044 hdr.ack_nr = 600; 0045 hdr.connection_id = 1777; 0046 hdr.extension = 0; 0047 hdr.seq_nr = 1000; 0048 hdr.timestamp_difference_microseconds = 0; 0049 hdr.timestamp_microseconds = 0; 0050 hdr.version = 0; 0051 hdr.wnd_size = 6000; 0052 0053 pbuf.setHeader(hdr, 0); 0054 QVERIFY(pbuf.bufferSize() == 200 + utp::Header::size()); 0055 QVERIFY(memcmp(pbuf.data() + utp::Header::size(), tmp, 200) == 0); 0056 QVERIFY(pbuf.payloadSize() == 200); 0057 0058 utp::Header hdr2; 0059 hdr2.read(pbuf.data()); 0060 QVERIFY(memcmp(&hdr, &hdr2, sizeof(utp::Header)) == 0); 0061 0062 pbuf.setHeader(hdr, 4); 0063 QVERIFY(pbuf.bufferSize() == 200 + utp::Header::size() + 4); 0064 QVERIFY(memcmp(pbuf.data() + utp::Header::size() + 4, tmp, 200) == 0); 0065 QVERIFY(pbuf.payloadSize() == 200); 0066 0067 utp::Header hdr3; 0068 hdr3.read(pbuf.data()); 0069 QVERIFY(memcmp(&hdr, &hdr3, sizeof(utp::Header)) == 0); 0070 } 0071 0072 void testData() 0073 { 0074 bt::Uint8 tmp[200]; 0075 memset(tmp, 0xFF, 200); 0076 0077 utp::PacketBuffer pbuf; 0078 pbuf.fillData(tmp, 200); 0079 QVERIFY(pbuf.bufferSize() == 200); 0080 QVERIFY(pbuf.payloadSize() == 200); 0081 QVERIFY(memcmp(pbuf.data(), tmp, 200) == 0); 0082 } 0083 }; 0084 0085 QTEST_MAIN(PacketBufferTest) 0086 0087 #include "packetbuffertest.moc"