diff --git a/autotests/fakeUdisks2.cpp b/autotests/fakeUdisks2.cpp new file mode 100644 index 0000000..b123465 --- /dev/null +++ b/autotests/fakeUdisks2.cpp @@ -0,0 +1,37 @@ +/* + Copyright 2017 Konstantinos Tsanaktsidis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "fakeUdisks2.h" + +FakeUdisks2BlockDevice::FakeUdisks2BlockDevice(QObject *parent, const QString &device, quint64 device_number) + : QDBusAbstractAdaptor(parent), m_device(device), m_device_number(device_number) +{ + +} + +const QString &FakeUdisks2BlockDevice::device() +{ + return m_device; +} + +quint64 FakeUdisks2BlockDevice::device_number() +{ + return m_device_number; +} diff --git a/autotests/fakeUdisks2.h b/autotests/fakeUdisks2.h new file mode 100644 index 0000000..00bec2a --- /dev/null +++ b/autotests/fakeUdisks2.h @@ -0,0 +1,43 @@ +/* + Copyright 2017 Konstantinos Tsanaktsidis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#ifndef SOLID_FAKE_UDISKS2 +#define SOLID_FAKE_UDISKS2 + +#include +#include +#include + +class FakeUdisks2BlockDevice : public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.freedesktop.UDisks2.Block") + Q_PROPERTY(QString Device READ device) + Q_PROPERTY(quint64 DeviceNumber READ device_number) +public: + FakeUdisks2BlockDevice(QObject *parent, const QString &device, quint64 device_number); + + const QString &device(); + QString m_device; + quint64 device_number(); + quint64 m_device_number; +}; + +#endif // SOLID_FAKE_UDISKS2 diff --git a/autotests/solidudisks2test.cpp b/autotests/solidudisks2test.cpp new file mode 100644 index 0000000..e15b1dc --- /dev/null +++ b/autotests/solidudisks2test.cpp @@ -0,0 +1,118 @@ +/* + Copyright 2017 Konstantinos Tsanaktsidis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "qtest_dbus.h" +#include "fakeUdisks2.h" + +#include +#include +#include +#include +#include +#include + +#if HAVE_SYSMACROS_MAJOR_MINOR +#include +#elif HAVE_SYSTYPES_MAJOR_MINOR +#include +#elif HAVE_SYSMKDEV_MAJOR_MINOR +#include +#else +#error "Could not find a major/minor/makedev implementation" +#endif + +static Solid::Predicate majorMinorPredicate(quint32 majorNum, quint32 minorNum) +{ + Solid::Predicate hasMajorMinor( + Solid::DeviceInterface::Type::Block, + QStringLiteral("major"), + QVariant(majorNum), + Solid::Predicate::ComparisonOperator::Equals + ); + hasMajorMinor &= Solid::Predicate( + Solid::DeviceInterface::Type::Block, + QStringLiteral("minor"), + QVariant(minorNum), + Solid::Predicate::ComparisonOperator::Equals + ); + return hasMajorMinor; +} + + +class SolidUdisks2Test : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + void testDeviceNumbersAreDecomposed(); + void testLargeDeviceNumbersAreDecomposed(); + +private: + FakeUdisks2BlockDevice *m_deviceSda1; + FakeUdisks2BlockDevice *m_deviceNvme0n1p2; + QObject *m_emptyDbus; +}; + +void SolidUdisks2Test::initTestCase() +{ + m_deviceSda1 = new FakeUdisks2BlockDevice(this, "sda1", makedev(8, 1)); + m_deviceNvme0n1p2 = new FakeUdisks2BlockDevice(this, "nvme0n1p2", makedev(259, 2)); + m_emptyDbus = new QObject(this); + QDBusConnection::systemBus().registerService(QStringLiteral("org.freedesktop.UDisks2")); + QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/UDisks2/block_devices/sda1"), m_deviceSda1, QDBusConnection::ExportAllContents); + QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/UDisks2/block_devices/nvme0n1p2"), m_deviceNvme0n1p2, QDBusConnection::ExportAllContents); + QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/UDisks2/drives"), m_emptyDbus, QDBusConnection::ExportAllContents); +} + +void SolidUdisks2Test::testDeviceNumbersAreDecomposed() +{ + auto hasMajorMinor = majorMinorPredicate(8, 1); + QList solidSda1BlockDeviceList = Solid::Device::listFromQuery(hasMajorMinor); + QCOMPARE(solidSda1BlockDeviceList.count(), 1); + + auto solidSda1BlockDevice = dynamic_cast( + solidSda1BlockDeviceList.first().asDeviceInterface(Solid::DeviceInterface::Type::Block) + ); + QVERIFY(solidSda1BlockDevice); + + QCOMPARE(solidSda1BlockDevice->deviceMajor(), 8); + QCOMPARE(solidSda1BlockDevice->deviceMinor(), 1); +} + +void SolidUdisks2Test::testLargeDeviceNumbersAreDecomposed() +{ + // On Linux, nvme devices have a major number > 255, so take up more than 8 bits of dev_t + // This test checks that the correct glibc routines are being used to handle this + auto hasMajorMinor = majorMinorPredicate(259, 2); + QList solidNvme0n1p2BlockDeviceList = Solid::Device::listFromQuery(hasMajorMinor); + QCOMPARE(solidNvme0n1p2BlockDeviceList.count(), 1); + + auto solidNvme0n1p2BlockDevice = dynamic_cast( + solidNvme0n1p2BlockDeviceList.first().asDeviceInterface(Solid::DeviceInterface::Type::Block) + ); + QVERIFY(solidNvme0n1p2BlockDevice); + + QCOMPARE(solidNvme0n1p2BlockDevice->deviceMajor(), 259); + QCOMPARE(solidNvme0n1p2BlockDevice->deviceMinor(), 2); +} + +QTEST_GUILESS_MAIN_SYSTEM_DBUS(SolidUdisks2Test) + +#include "solidudisks2test.moc"