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 
0007 #include <QObject>
0008 #include <QtTest>
0009 #include <boost/concept_check.hpp>
0010 #include <torrent/globals.h>
0011 #include <util/log.h>
0012 #include <utp/utpserver.h>
0013 #include <utp/utpsocket.h>
0014 
0015 using namespace utp;
0016 
0017 class SocketTest : public QEventLoop
0018 {
0019 public:
0020     void accepted()
0021     {
0022         incoming = new UTPSocket(bt::Globals::instance().getUTPServer().acceptedConnection());
0023         exit();
0024     }
0025 
0026     void endEventLoop()
0027     {
0028         exit();
0029     }
0030 
0031 private:
0032     void initTestCase()
0033     {
0034         bt::InitLog("sockettest.log");
0035         incoming = outgoing = nullptr;
0036 
0037         port = 50000;
0038         while (port < 60000) {
0039             if (bt::Globals::instance().initUTPServer(port))
0040                 break;
0041             else
0042                 port++;
0043         }
0044 
0045         bt::Globals::instance().getUTPServer().setCreateSockets(false);
0046     }
0047 
0048     void cleanupTestCase()
0049     {
0050         bt::Globals::instance().shutdownUTPServer();
0051         delete incoming;
0052         delete outgoing;
0053     }
0054 
0055     void testConnect()
0056     {
0057         UTPServer &srv = bt::Globals::instance().getUTPServer();
0058         net::Address addr("127.0.0.1", port);
0059         connect(&srv, &UTPServer::accepted, this, &SocketTest::accepted, Qt::QueuedConnection);
0060         outgoing = new UTPSocket();
0061         outgoing->connectTo(addr);
0062         QTimer::singleShot(5000, this, &SocketTest::endEventLoop); // use a 5 second timeout
0063         exec();
0064         QVERIFY(incoming != nullptr);
0065         QVERIFY(incoming->connectSuccesFull());
0066         QVERIFY(outgoing->connectSuccesFull());
0067     }
0068 
0069     void testSend()
0070     {
0071         outgoing->setBlocking(true);
0072         incoming->setBlocking(true);
0073         char test[] = "TEST";
0074 
0075         UTPSocket *a = incoming;
0076         UTPSocket *b = outgoing;
0077         for (int i = 0; i < 10; i++) {
0078             int ret = a->send((const bt::Uint8 *)test, strlen(test));
0079             QVERIFY(ret == (int)strlen(test));
0080 
0081             char tmp[20];
0082             memset(tmp, 0, 20);
0083             ret = b->recv((bt::Uint8 *)tmp, 20);
0084             QVERIFY(ret == 4);
0085             QVERIFY(memcmp(tmp, test, ret) == 0);
0086             std::swap(a, b);
0087         }
0088     }
0089 
0090     void testClose()
0091     {
0092         outgoing->setBlocking(true);
0093         incoming->close();
0094         bt::Uint8 tmp[20];
0095         int ret = outgoing->recv(tmp, 20);
0096         QVERIFY(ret == 0);
0097     }
0098 
0099     void testConnectionTimeout()
0100     {
0101         UTPSocket sock;
0102         net::Address addr("127.0.0.1", port + 1);
0103         sock.setBlocking(true);
0104         QVERIFY(sock.connectTo(addr) == false);
0105     }
0106 
0107     void testInvalidAddress()
0108     {
0109         UTPSocket sock;
0110         net::Address addr("127.0.0.1", 0);
0111         sock.setBlocking(true);
0112         QVERIFY(sock.connectTo(addr) == false);
0113     }
0114 
0115 private:
0116     int port;
0117     utp::UTPSocket *incoming;
0118     utp::UTPSocket *outgoing;
0119 };
0120 
0121 QTEST_MAIN(SocketTest)