File indexing completed on 2025-01-05 04:37:31
0001 /* 0002 SPDX-FileCopyrightText: 2010 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/log.h> 0010 #include <utp/connection.h> 0011 #include <utp/utpserver.h> 0012 0013 using namespace utp; 0014 0015 class ConnectionTest : public QEventLoop, public Transmitter 0016 { 0017 public: 0018 ConnectionTest(QObject *parent = nullptr) 0019 : QEventLoop(parent) 0020 , remote("127.0.0.1", 50000) 0021 { 0022 } 0023 0024 bool sendTo(Connection::Ptr conn, const PacketBuffer &packet) override 0025 { 0026 sent_packets.append(packet); 0027 Q_UNUSED(conn) 0028 return true; 0029 } 0030 0031 void stateChanged(Connection::Ptr conn, bool readable, bool writeable) override 0032 { 0033 Q_UNUSED(conn) 0034 Q_UNUSED(readable) 0035 Q_UNUSED(writeable) 0036 } 0037 0038 void closed(Connection::Ptr conn) override 0039 { 0040 Q_UNUSED(conn) 0041 } 0042 0043 bt::Buffer::Ptr buildPacket(bt::Uint32 type, bt::Uint32 recv_conn_id, bt::Uint32 send_conn_id, bt::Uint16 seq_nr, bt::Uint16 ack_nr) 0044 { 0045 TimeValue tv; 0046 bt::Buffer::Ptr packet = pool->get(Header::size()); 0047 Header hdr; 0048 hdr.version = 1; 0049 hdr.type = type; 0050 hdr.extension = 0; 0051 hdr.connection_id = type == ST_SYN ? recv_conn_id : send_conn_id; 0052 hdr.timestamp_microseconds = tv.microseconds; 0053 hdr.timestamp_difference_microseconds = 0; 0054 hdr.wnd_size = 6666; 0055 hdr.seq_nr = seq_nr; 0056 hdr.ack_nr = ack_nr; 0057 hdr.write(packet->get()); 0058 return packet; 0059 } 0060 0061 private: 0062 void initTestCase() 0063 { 0064 bt::InitLog("connectiontest.log"); 0065 pool = bt::BufferPool::Ptr(new bt::BufferPool()); 0066 pool->setWeakPointer(pool.toWeakRef()); 0067 } 0068 0069 void cleanupTestCase() 0070 { 0071 pool.clear(); 0072 } 0073 0074 void init() 0075 { 0076 sent_packets.clear(); 0077 } 0078 0079 void testConnID() 0080 { 0081 bt::Uint32 conn_id = 666; 0082 Connection conn(conn_id, utp::Connection::INCOMING, remote, this); 0083 QVERIFY(conn.connectionStats().recv_connection_id == conn_id); 0084 QVERIFY(conn.connectionStats().send_connection_id == conn_id - 1); 0085 0086 Connection conn2(conn_id, utp::Connection::OUTGOING, remote, this); 0087 QVERIFY(conn2.connectionStats().recv_connection_id == conn_id); 0088 QVERIFY(conn2.connectionStats().send_connection_id == conn_id + 1); 0089 } 0090 0091 void testOutgoingConnectionSetup() 0092 { 0093 bt::Uint32 conn_id = 666; 0094 Connection conn(conn_id, utp::Connection::OUTGOING, remote, this); 0095 conn.startConnecting(); 0096 const Connection::Stats &s = conn.connectionStats(); 0097 QVERIFY(s.state == utp::CS_SYN_SENT); 0098 QVERIFY(s.seq_nr == 2); 0099 0100 bt::Buffer::Ptr pkt = buildPacket(ST_STATE, conn_id, conn_id + 1, 1, 1); 0101 PacketParser pp(pkt->get(), pkt->size()); 0102 QVERIFY(pp.parse()); 0103 conn.handlePacket(pp, pkt); 0104 QVERIFY(s.state == CS_CONNECTED); 0105 QVERIFY(sent_packets.count() == 1); 0106 } 0107 0108 void testIncomingConnectionSetup() 0109 { 0110 bt::Uint32 conn_id = 666; 0111 Connection conn(conn_id, utp::Connection::INCOMING, remote, this); 0112 const Connection::Stats &s = conn.connectionStats(); 0113 0114 bt::Buffer::Ptr pkt = buildPacket(ST_SYN, conn_id - 1, conn_id, 1, 1); 0115 PacketParser pp(pkt->get(), pkt->size()); 0116 conn.handlePacket(pp, pkt); 0117 QVERIFY(s.state == CS_CONNECTED); 0118 } 0119 0120 private: 0121 net::Address remote; 0122 QList<PacketBuffer> sent_packets; 0123 bt::BufferPool::Ptr pool; 0124 }; 0125 0126 QTEST_MAIN(ConnectionTest)