File indexing completed on 2024-05-12 17:08:31

0001 /*
0002  * SPDX-FileCopyrightText: 2018-2019 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include <QObject>
0008 #include <QSignalSpy>
0009 #include <QTest>
0010 
0011 #include "fakedevice.h"
0012 #include "fakemanager.h"
0013 #include "fakeserver.h"
0014 
0015 #include "device.h"
0016 #include "manager.h"
0017 
0018 #include <memory>
0019 
0020 class DeviceTest : public QObject
0021 {
0022     Q_OBJECT
0023 public:
0024     DeviceTest()
0025         : QObject()
0026     {
0027         FakeServer::enableFakeEnv();
0028         qRegisterMetaType<QSharedPointer<Bolt::Device>>();
0029     }
0030 
0031 private Q_SLOTS:
0032     void testAuthorize()
0033     {
0034         std::unique_ptr<FakeServer> fakeServer;
0035         try {
0036             fakeServer = std::make_unique<FakeServer>();
0037         } catch (const FakeServerException &e) {
0038             qWarning("Fake server exception: %s", e.what());
0039             QFAIL("Caught server exception");
0040         }
0041 
0042         auto fakeManager = fakeServer->manager();
0043         FakeDevice *fakeDevice = nullptr;
0044         try {
0045             fakeDevice = fakeManager->addDevice(std::make_unique<FakeDevice>(QStringLiteral("Device1")));
0046         } catch (const FakeDeviceException &e) {
0047             qWarning("Fake device exception: %s", e.what());
0048             QFAIL("Caught device exception");
0049         }
0050         fakeDevice->setAuthFlags(QStringLiteral("none"));
0051 
0052         Bolt::Manager manager;
0053         QVERIFY(manager.isAvailable());
0054 
0055         auto device = manager.device(fakeDevice->uid());
0056         QVERIFY(device);
0057         QCOMPARE(device->authFlags(), Bolt::Auth::None);
0058         device->authorize(Bolt::Auth::NoKey | Bolt::Auth::Boot);
0059 
0060         QTRY_COMPARE(fakeDevice->authFlags(), QStringLiteral("nokey | boot"));
0061     }
0062 };
0063 
0064 #include "devicetest.moc"
0065 
0066 QTEST_GUILESS_MAIN(DeviceTest)