File indexing completed on 2024-04-21 03:51:52

0001 /*
0002  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "agentmanagertest.h"
0008 #include "autotests.h"
0009 #include "initmanagerjob.h"
0010 #include "manager.h"
0011 #include "pendingcall.h"
0012 #include "services.h"
0013 
0014 #include <QTest>
0015 
0016 namespace BluezQt
0017 {
0018 extern void bluezqt_initFakeBluezTestRun();
0019 }
0020 
0021 using namespace BluezQt;
0022 
0023 // TestAgent
0024 TestAgent::TestAgent(QObject *parent)
0025     : Agent(parent)
0026     , m_pinRequested(false)
0027     , m_passkeyRequested(false)
0028     , m_authorizationRequested(false)
0029     , m_cancelCalled(false)
0030     , m_releaseCalled(false)
0031 {
0032 }
0033 
0034 QDBusObjectPath TestAgent::objectPath() const
0035 {
0036     return QDBusObjectPath(QStringLiteral("/testagent"));
0037 }
0038 
0039 void TestAgent::requestPinCode(DevicePtr device, const BluezQt::Request<QString> &request)
0040 {
0041     m_device = device;
0042     m_pinRequested = true;
0043 
0044     request.accept(QString());
0045 }
0046 
0047 void TestAgent::displayPinCode(DevicePtr device, const QString &pinCode)
0048 {
0049     m_device = device;
0050     m_displayedPinCode = pinCode;
0051 }
0052 
0053 void TestAgent::requestPasskey(DevicePtr device, const BluezQt::Request<quint32> &request)
0054 {
0055     m_device = device;
0056     m_passkeyRequested = true;
0057 
0058     request.accept(0);
0059 }
0060 
0061 void TestAgent::displayPasskey(DevicePtr device, const QString &passkey, const QString &entered)
0062 {
0063     m_device = device;
0064     m_displayedPasskey = passkey;
0065     m_enteredPasskey = entered;
0066 }
0067 
0068 void TestAgent::requestConfirmation(DevicePtr device, const QString &passkey, const BluezQt::Request<> &request)
0069 {
0070     m_device = device;
0071     m_requestedPasskey = passkey;
0072 
0073     request.accept();
0074 }
0075 
0076 void TestAgent::requestAuthorization(DevicePtr device, const BluezQt::Request<> &request)
0077 {
0078     m_device = device;
0079     m_authorizationRequested = true;
0080 
0081     request.accept();
0082 }
0083 
0084 void TestAgent::authorizeService(DevicePtr device, const QString &uuid, const BluezQt::Request<> &request)
0085 {
0086     m_device = device;
0087     m_authorizedUuid = uuid;
0088 
0089     request.accept();
0090 }
0091 
0092 void TestAgent::cancel()
0093 {
0094     m_cancelCalled = true;
0095 }
0096 
0097 void TestAgent::release()
0098 {
0099     m_releaseCalled = true;
0100 }
0101 
0102 // AgentManagerTest
0103 void AgentManagerTest::initTestCase()
0104 {
0105     bluezqt_initFakeBluezTestRun();
0106 
0107     FakeBluez::start();
0108     FakeBluez::runTest(QStringLiteral("bluez-standard"));
0109 
0110     // Create adapter
0111     QVariantMap adapterProps;
0112     adapterProps[QStringLiteral("Path")] = QVariant::fromValue(QDBusObjectPath(QStringLiteral("/org/bluez/hci0")));
0113     adapterProps[QStringLiteral("Address")] = QStringLiteral("1C:E5:C3:BC:94:7E");
0114     adapterProps[QStringLiteral("Name")] = QStringLiteral("TestAdapter");
0115     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-adapter"), adapterProps);
0116 
0117     // Create device
0118     m_device = QDBusObjectPath(QStringLiteral("/org/bluez/hci0/dev_40_79_6A_0C_39_75"));
0119     QVariantMap deviceProps;
0120     deviceProps[QStringLiteral("Path")] = QVariant::fromValue(m_device);
0121     deviceProps[QStringLiteral("Adapter")] = adapterProps.value(QStringLiteral("Path"));
0122     deviceProps[QStringLiteral("Address")] = QStringLiteral("40:79:6A:0C:39:75");
0123     deviceProps[QStringLiteral("Name")] = QStringLiteral("TestDevice");
0124     FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("create-device"), deviceProps);
0125 
0126     Manager *manager = new Manager(this);
0127     InitManagerJob *job = manager->init();
0128     job->exec();
0129 
0130     QVERIFY(!job->error());
0131     QCOMPARE(manager->adapters().count(), 1);
0132     QCOMPARE(manager->devices().count(), 1);
0133 
0134     m_agent = new TestAgent(this);
0135     manager->registerAgent(m_agent)->waitForFinished();
0136 }
0137 
0138 void AgentManagerTest::cleanupTestCase()
0139 {
0140     FakeBluez::stop();
0141 }
0142 
0143 void AgentManagerTest::requestPinCodeTest()
0144 {
0145     QCOMPARE(m_agent->m_pinRequested, false);
0146 
0147     QVariantMap props;
0148     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0149     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-pincode"), props);
0150 
0151     QTRY_COMPARE(m_agent->m_pinRequested, true);
0152     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0153 }
0154 
0155 void AgentManagerTest::displayPinCodeTest()
0156 {
0157     QCOMPARE(m_agent->m_displayedPinCode, QString());
0158 
0159     QVariantMap props;
0160     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0161     props.insert(QStringLiteral("PinCode"), QStringLiteral("123456"));
0162     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("display-pincode"), props);
0163 
0164     QTRY_COMPARE(m_agent->m_displayedPinCode, QStringLiteral("123456"));
0165     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0166 }
0167 
0168 void AgentManagerTest::requestPasskeyTest()
0169 {
0170     QCOMPARE(m_agent->m_passkeyRequested, false);
0171 
0172     QVariantMap props;
0173     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0174     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-passkey"), props);
0175 
0176     QTRY_COMPARE(m_agent->m_passkeyRequested, true);
0177     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0178 }
0179 
0180 void AgentManagerTest::displayPasskeyTest()
0181 {
0182     QCOMPARE(m_agent->m_displayedPasskey, QString());
0183     QCOMPARE(m_agent->m_enteredPasskey, QString());
0184 
0185     QVariantMap props;
0186     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0187     props.insert(QStringLiteral("Passkey"), QVariant::fromValue(quint32(654321)));
0188     props.insert(QStringLiteral("EnteredPasskey"), QVariant::fromValue(quint16(43)));
0189     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("display-passkey"), props);
0190 
0191     QTRY_COMPARE(m_agent->m_displayedPasskey, QStringLiteral("654321"));
0192     QTRY_COMPARE(m_agent->m_enteredPasskey, QStringLiteral("43"));
0193     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0194 }
0195 
0196 void AgentManagerTest::requestConfirmationTest()
0197 {
0198     QCOMPARE(m_agent->m_requestedPasskey, QString());
0199 
0200     QVariantMap props;
0201     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0202     props.insert(QStringLiteral("Passkey"), QVariant::fromValue(quint32(12)));
0203     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-confirmation"), props);
0204 
0205     QTRY_COMPARE(m_agent->m_requestedPasskey, QStringLiteral("000012"));
0206     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0207 }
0208 
0209 void AgentManagerTest::requestAuthorizationTest()
0210 {
0211     QCOMPARE(m_agent->m_authorizationRequested, false);
0212 
0213     QVariantMap props;
0214     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0215     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("request-authorization"), props);
0216 
0217     QTRY_COMPARE(m_agent->m_authorizationRequested, true);
0218     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0219 }
0220 
0221 void AgentManagerTest::authorizeServiceTest()
0222 {
0223     QCOMPARE(m_agent->m_authorizedUuid, QString());
0224 
0225     QVariantMap props;
0226     props.insert(QStringLiteral("Device"), QVariant::fromValue(m_device));
0227     props.insert(QStringLiteral("UUID"), Services::ObexFileTransfer);
0228     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("authorize-service"), props);
0229 
0230     QTRY_COMPARE(m_agent->m_authorizedUuid, Services::ObexFileTransfer);
0231     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0232 }
0233 
0234 void AgentManagerTest::cancelTest()
0235 {
0236     QCOMPARE(m_agent->m_cancelCalled, false);
0237 
0238     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("cancel"));
0239 
0240     QTRY_COMPARE(m_agent->m_cancelCalled, true);
0241     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0242 }
0243 
0244 void AgentManagerTest::releaseTest()
0245 {
0246     QCOMPARE(m_agent->m_releaseCalled, false);
0247 
0248     FakeBluez::runAction(QStringLiteral("agentmanager"), QStringLiteral("release"));
0249 
0250     QTRY_COMPARE(m_agent->m_releaseCalled, true);
0251     QTRY_COMPARE(m_agent->m_device->name(), QStringLiteral("TestDevice"));
0252 }
0253 
0254 QTEST_MAIN(AgentManagerTest)
0255 
0256 #include "moc_agentmanagertest.cpp"