File indexing completed on 2024-05-05 04:58:59

0001 /**
0002  * SPDX-FileCopyrightText: 2023 Albert Vaca Cintora <albertvaka@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include <QCoreApplication>
0008 #include <QObject>
0009 #include <QSignalSpy>
0010 #include <QSocketNotifier>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 #include "core/backends/lan/mdns_wrapper.h"
0015 #include "kdeconnect-version.h"
0016 
0017 class TestMdns : public QObject
0018 {
0019     Q_OBJECT
0020 public:
0021     TestMdns()
0022     {
0023     }
0024 
0025 private Q_SLOTS:
0026     void testAnounceAndDiscover()
0027     {
0028         QString instanceName = QStringLiteral("myinstance");
0029         uint16_t instancePort = 1716;
0030         QString serviceType = QStringLiteral("_test._udp.local");
0031         QString txtKey = QStringLiteral("keyerino");
0032         QString txtValue = QStringLiteral("valuerino");
0033 
0034         MdnsWrapper::Announcer announcer(instanceName, serviceType, instancePort);
0035         announcer.putTxtRecord(txtKey, txtValue);
0036 
0037         MdnsWrapper::Discoverer discoverer;
0038 
0039         QSignalSpy spy(&discoverer, &MdnsWrapper::Discoverer::serviceFound);
0040 
0041         connect(&discoverer,
0042                 &MdnsWrapper::Discoverer::serviceFound,
0043                 this,
0044                 [instanceName, instancePort, txtKey, txtValue](const MdnsWrapper::Discoverer::MdnsService &service) {
0045                     QCOMPARE(instanceName, service.name);
0046                     QCOMPARE(instancePort, service.port);
0047                     QVERIFY(service.txtRecords.size() == 1);
0048                     QVERIFY(service.txtRecords.contains(txtKey));
0049                     QCOMPARE(txtValue, service.txtRecords.value(txtKey));
0050                 });
0051 
0052         announcer.startAnnouncing();
0053         discoverer.startDiscovering(serviceType);
0054 
0055         QVERIFY(spy.wait(2000));
0056         QVERIFY(spy.count() > 0);
0057 
0058         discoverer.stopDiscovering();
0059         announcer.stopAnnouncing();
0060     }
0061 };
0062 
0063 QTEST_MAIN(TestMdns);
0064 
0065 #include "mdnstest.moc"