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 ConnectTest : public QEventLoop
0016 {
0017 public:
0018     void accepted()
0019     {
0020         accepted_conn = srv.acceptedConnection().toStrongRef();
0021         exit();
0022     }
0023 
0024     void endEventLoop()
0025     {
0026         exit();
0027     }
0028 
0029     void startConnect()
0030     {
0031         net::Address addr("127.0.0.1", port);
0032         srv.connectTo(addr);
0033         QTimer::singleShot(5000, this, &ConnectTest::endEventLoop); // use a 5 second timeout
0034     }
0035 
0036 private:
0037     void initTestCase()
0038     {
0039         bt::InitLog("connecttest.log");
0040 
0041         port = 50000;
0042         while (port < 60000) {
0043             if (!srv.changePort(port))
0044                 port++;
0045             else
0046                 break;
0047         }
0048 
0049         srv.setCreateSockets(false);
0050         srv.start();
0051         QVERIFY(port < 60000);
0052     }
0053 
0054     void cleanupTestCase()
0055     {
0056         srv.stop();
0057     }
0058 
0059     void testConnect()
0060     {
0061         connect(&srv, &utp::UTPServer::accepted, this, &ConnectTest::accepted, Qt::QueuedConnection);
0062         QTimer::singleShot(0, this, &ConnectTest::startConnect);
0063         exec();
0064         QVERIFY(accepted_conn != nullptr);
0065     }
0066 
0067 private:
0068     utp::UTPServer srv;
0069     int port;
0070     utp::Connection::Ptr accepted_conn;
0071 };
0072 
0073 QTEST_MAIN(ConnectTest)