diff --git a/src/modules/luksbootkeyfile/CMakeLists.txt b/src/modules/luksbootkeyfile/CMakeLists.txt index 6e98af468..dff682521 100644 --- a/src/modules/luksbootkeyfile/CMakeLists.txt +++ b/src/modules/luksbootkeyfile/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-FileCopyrightText: 2020 Adriaan de Groot # SPDX-License-Identifier: BSD-2-Clause # -calamares_add_plugin( luksbootkeyfile +calamares_add_plugin(luksbootkeyfile TYPE job EXPORT_MACRO PLUGINDLLEXPORT_PRO SOURCES @@ -11,3 +11,10 @@ calamares_add_plugin( luksbootkeyfile SHARED_LIB NO_CONFIG ) + +calamares_add_test( + luksbootkeyfiletest + SOURCES + Tests.cpp + LuksBootKeyFileJob.cpp +) diff --git a/src/modules/luksbootkeyfile/LuksBootKeyFileJob.cpp b/src/modules/luksbootkeyfile/LuksBootKeyFileJob.cpp index ba772aee1..ecee92467 100644 --- a/src/modules/luksbootkeyfile/LuksBootKeyFileJob.cpp +++ b/src/modules/luksbootkeyfile/LuksBootKeyFileJob.cpp @@ -157,21 +157,21 @@ partitionsFromGlobalStorage() } /// Checks if the partition (represented by @p map) mounts to the given @p path -static bool +STATICTEST bool hasMountPoint( const QVariantMap& map, const QString& path ) { const QString mountPoint = map.value( QStringLiteral( "mountPoint" ) ).toString(); return QDir::cleanPath( mountPoint ) == path; } -static bool +STATICTEST bool isEncrypted( const QVariantMap& map ) { return map.contains( QStringLiteral( "luksMapperName" ) ); } /// Checks for any partition satisfying @p pred -static bool +STATICTEST bool anyPartition( bool ( *pred )( const QVariantMap& ) ) { const auto partitions = partitionsFromGlobalStorage(); @@ -181,7 +181,7 @@ anyPartition( bool ( *pred )( const QVariantMap& ) ) != partitions.cend(); } -static bool +STATICTEST bool hasUnencryptedSeparateBoot() { return anyPartition( @@ -189,7 +189,7 @@ hasUnencryptedSeparateBoot() { return hasMountPoint( partition, QStringLiteral( "/boot" ) ) && !isEncrypted( partition ); } ); } -static bool +STATICTEST bool hasEncryptedRoot() { return anyPartition( []( const QVariantMap& partition ) diff --git a/src/modules/luksbootkeyfile/Tests.cpp b/src/modules/luksbootkeyfile/Tests.cpp new file mode 100644 index 000000000..bb295919b --- /dev/null +++ b/src/modules/luksbootkeyfile/Tests.cpp @@ -0,0 +1,83 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2022 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#include "JobQueue.h" +#include "utils/Logger.h" + +#include + +#undef STATICTEST +#define STATICTEST extern + +// Implementation details +STATICTEST bool hasMountPoint( const QVariantMap& map, const QString& path ); + +STATICTEST bool isEncrypted( const QVariantMap& map ); + +STATICTEST bool anyPartition( bool ( *pred )( const QVariantMap& ) ); + +STATICTEST bool hasUnencryptedSeparateBoot(); + +STATICTEST bool hasEncryptedRoot(); + +class LuksBootKeyFileTests : public QObject +{ + Q_OBJECT +public: + LuksBootKeyFileTests() {} + ~LuksBootKeyFileTests() override {} + +private Q_SLOTS: + void initTestCase(); + + void testMountPoint(); +}; + +void +LuksBootKeyFileTests::initTestCase() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); + cDebug() << "LuksBootKeyFile test started."; + + if ( !Calamares::JobQueue::instance() ) + { + (void)new Calamares::JobQueue(); + } +} + +void +LuksBootKeyFileTests::testMountPoint() +{ + QVariantMap m; // As if this is a partition data + const QString key = QStringLiteral( "mountPoint" ); + const QString boot = QStringLiteral( "/boot" ); + const QString root = QStringLiteral( "/" ); + + QVERIFY( !hasMountPoint( m, QString() ) ); + QVERIFY( !hasMountPoint( m, boot ) ); + + m.insert( key, boot ); + QVERIFY( hasMountPoint( m, boot ) ); + QVERIFY( !hasMountPoint( m, QString() ) ); + QVERIFY( !hasMountPoint( m, root ) ); + + m.insert( key, root ); + QVERIFY( !hasMountPoint( m, boot ) ); + QVERIFY( !hasMountPoint( m, QString() ) ); + QVERIFY( hasMountPoint( m, root ) ); + + m.remove( key ); + QVERIFY( !hasMountPoint( m, root ) ); +} + +QTEST_GUILESS_MAIN( LuksBootKeyFileTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc"