[luksbootkeyfile] Start adding tests

This commit is contained in:
Adriaan de Groot 2022-04-23 14:29:10 +02:00
parent 1752dd573b
commit 4466e360e1
3 changed files with 96 additions and 6 deletions

View File

@ -3,7 +3,7 @@
# SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org> # SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
# SPDX-License-Identifier: BSD-2-Clause # SPDX-License-Identifier: BSD-2-Clause
# #
calamares_add_plugin( luksbootkeyfile calamares_add_plugin(luksbootkeyfile
TYPE job TYPE job
EXPORT_MACRO PLUGINDLLEXPORT_PRO EXPORT_MACRO PLUGINDLLEXPORT_PRO
SOURCES SOURCES
@ -11,3 +11,10 @@ calamares_add_plugin( luksbootkeyfile
SHARED_LIB SHARED_LIB
NO_CONFIG NO_CONFIG
) )
calamares_add_test(
luksbootkeyfiletest
SOURCES
Tests.cpp
LuksBootKeyFileJob.cpp
)

View File

@ -157,21 +157,21 @@ partitionsFromGlobalStorage()
} }
/// Checks if the partition (represented by @p map) mounts to the given @p path /// 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 ) hasMountPoint( const QVariantMap& map, const QString& path )
{ {
const QString mountPoint = map.value( QStringLiteral( "mountPoint" ) ).toString(); const QString mountPoint = map.value( QStringLiteral( "mountPoint" ) ).toString();
return QDir::cleanPath( mountPoint ) == path; return QDir::cleanPath( mountPoint ) == path;
} }
static bool STATICTEST bool
isEncrypted( const QVariantMap& map ) isEncrypted( const QVariantMap& map )
{ {
return map.contains( QStringLiteral( "luksMapperName" ) ); return map.contains( QStringLiteral( "luksMapperName" ) );
} }
/// Checks for any partition satisfying @p pred /// Checks for any partition satisfying @p pred
static bool STATICTEST bool
anyPartition( bool ( *pred )( const QVariantMap& ) ) anyPartition( bool ( *pred )( const QVariantMap& ) )
{ {
const auto partitions = partitionsFromGlobalStorage(); const auto partitions = partitionsFromGlobalStorage();
@ -181,7 +181,7 @@ anyPartition( bool ( *pred )( const QVariantMap& ) )
!= partitions.cend(); != partitions.cend();
} }
static bool STATICTEST bool
hasUnencryptedSeparateBoot() hasUnencryptedSeparateBoot()
{ {
return anyPartition( return anyPartition(
@ -189,7 +189,7 @@ hasUnencryptedSeparateBoot()
{ return hasMountPoint( partition, QStringLiteral( "/boot" ) ) && !isEncrypted( partition ); } ); { return hasMountPoint( partition, QStringLiteral( "/boot" ) ) && !isEncrypted( partition ); } );
} }
static bool STATICTEST bool
hasEncryptedRoot() hasEncryptedRoot()
{ {
return anyPartition( []( const QVariantMap& partition ) return anyPartition( []( const QVariantMap& partition )

View File

@ -0,0 +1,83 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2022 Adriaan de Groot <groot@kde.org>
* 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 <QtTest/QtTest>
#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"