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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include <QObject>
0007 #include <QtTest>
0008 #include <torrent/globals.h>
0009 #include <util/log.h>
0010 #include <utp/utpserver.h>
0011 #include <utp/utpsocket.h>
0012 
0013 using namespace utp;
0014 
0015 class SendTest : public QEventLoop
0016 {
0017 public:
0018     void accepted()
0019     {
0020         incoming = new UTPSocket(bt::Globals::instance().getUTPServer().acceptedConnection());
0021         exit();
0022     }
0023 
0024     void endEventLoop()
0025     {
0026         exit();
0027     }
0028 
0029 private:
0030     void initTestCase()
0031     {
0032         bt::InitLog("sendtest.log");
0033 
0034         incoming = nullptr;
0035         port = 50000;
0036         while (port < 60000) {
0037             if (!bt::Globals::instance().initUTPServer(port))
0038                 port++;
0039             else
0040                 break;
0041         }
0042 
0043         bt::Globals::instance().getUTPServer().setCreateSockets(false);
0044     }
0045 
0046     void cleanupTestCase()
0047     {
0048         delete incoming;
0049         delete outgoing;
0050         bt::Globals::instance().shutdownUTPServer();
0051     }
0052 
0053     void testConnect()
0054     {
0055         bt::Out(SYS_UTP | LOG_DEBUG) << "testConnect" << bt::endl;
0056 
0057         net::Address addr("127.0.0.1", port);
0058         utp::UTPServer &srv = bt::Globals::instance().getUTPServer();
0059         connect(&srv, &utp::UTPServer::accepted, this, &SendTest::accepted, Qt::QueuedConnection);
0060         outgoing = new utp::UTPSocket();
0061         outgoing->setBlocking(false);
0062         outgoing->connectTo(addr);
0063 
0064         QTimer::singleShot(5000, this, &SendTest::endEventLoop); // use a 5 second timeout
0065         exec();
0066         QVERIFY(incoming != nullptr);
0067 
0068         // Wait until connection is complete
0069         int times = 0;
0070         while (!outgoing->connectSuccesFull() && times < 5) {
0071             QTest::qSleep(1000);
0072             times++;
0073         }
0074     }
0075 
0076     void testSend()
0077     {
0078         bt::Out(SYS_UTP | LOG_DEBUG) << "testSend" << bt::endl;
0079         outgoing->setBlocking(true);
0080         char test[] = "TEST";
0081 
0082         int ret = outgoing->send((const bt::Uint8 *)test, strlen(test));
0083         QVERIFY(ret == (int)strlen(test));
0084 
0085         char tmp[20];
0086         memset(tmp, 0, 20);
0087         ret = incoming->recv((bt::Uint8 *)tmp, 20);
0088         QVERIFY(ret == 4);
0089         QVERIFY(memcmp(tmp, test, ret) == 0);
0090     }
0091 
0092     void testSend2()
0093     {
0094         bt::Out(SYS_UTP | LOG_DEBUG) << "testSend2" << bt::endl;
0095         bt::Uint8 *sdata = new bt::Uint8[1000];
0096         outgoing->send(sdata, 1000);
0097 
0098         bt::Uint8 *rdata = new bt::Uint8[1000];
0099         int ret = incoming->recv(rdata, 1000);
0100         QVERIFY(ret == 1000);
0101         QVERIFY(memcmp(sdata, rdata, ret) == 0);
0102 
0103         delete[] rdata;
0104         delete[] sdata;
0105     }
0106 
0107     void testSend3()
0108     {
0109         bt::Out(SYS_UTP | LOG_DEBUG) << "testSend3" << bt::endl;
0110         char test[] = "TEST";
0111 
0112         outgoing->send((const bt::Uint8 *)test, strlen(test));
0113         incoming->send((const bt::Uint8 *)test, strlen(test));
0114 
0115         char tmp[20];
0116         memset(tmp, 0, 20);
0117         int ret = incoming->recv((bt::Uint8 *)tmp, 20);
0118         QVERIFY(ret == 4);
0119         QVERIFY(memcmp(tmp, test, ret) == 0);
0120 
0121         memset(tmp, 0, 20);
0122         ret = outgoing->recv((bt::Uint8 *)tmp, 20);
0123         QVERIFY(ret == 4);
0124         QVERIFY(memcmp(tmp, test, ret) == 0);
0125     }
0126 
0127     void testSend4()
0128     {
0129         bt::Out(SYS_UTP | LOG_DEBUG) << "testSend4" << bt::endl;
0130         char test[] = "TEST";
0131 
0132         outgoing->send((const bt::Uint8 *)test, strlen(test));
0133         outgoing->send((const bt::Uint8 *)test, strlen(test));
0134 
0135         char tmp[20];
0136         memset(tmp, 0, 20);
0137         int ret = incoming->recv((bt::Uint8 *)tmp, 20);
0138         QVERIFY(ret == 4 || ret == 8);
0139         QVERIFY(memcmp(tmp, test, 4) == 0);
0140         if (ret != 8) {
0141             memset(tmp, 0, 20);
0142             ret = incoming->recv((bt::Uint8 *)tmp, 20);
0143             QVERIFY(ret == 4);
0144             QVERIFY(memcmp(tmp, test, ret) == 0);
0145         } else {
0146             QVERIFY(memcmp(tmp + 4, test, 4) == 0);
0147         }
0148     }
0149 
0150 private:
0151     int port;
0152     utp::UTPSocket *incoming;
0153     utp::UTPSocket *outgoing;
0154 };
0155 
0156 QTEST_MAIN(SendTest)