2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2019-10-03 13:32:48 +02:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-10-03 13:32:48 +02:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2019-10-03 13:32:48 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-07 20:29:42 +01:00
|
|
|
#include "MachineIdJob.h"
|
2019-10-03 13:32:48 +02:00
|
|
|
#include "Workers.h"
|
|
|
|
|
2020-02-07 20:29:42 +01:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2019-10-03 13:32:48 +02:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2020-02-17 11:51:56 +01:00
|
|
|
// Internals of Workers.cpp
|
|
|
|
extern int getUrandomPoolSize();
|
|
|
|
|
2019-10-03 13:32:48 +02:00
|
|
|
class MachineIdTests : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
MachineIdTests() {}
|
2020-09-22 22:14:41 +02:00
|
|
|
~MachineIdTests() override {}
|
2019-10-03 13:32:48 +02:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
2020-09-15 13:11:39 +02:00
|
|
|
void testConfigEntropyFiles();
|
2019-10-03 13:32:48 +02:00
|
|
|
|
|
|
|
void testCopyFile();
|
|
|
|
|
|
|
|
void testPoolSize();
|
2020-02-07 20:29:42 +01:00
|
|
|
|
|
|
|
void testJob();
|
2019-10-03 13:32:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineIdTests::initTestCase()
|
|
|
|
{
|
|
|
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:11:39 +02:00
|
|
|
void
|
|
|
|
MachineIdTests::testConfigEntropyFiles()
|
|
|
|
{
|
|
|
|
static QString urandom_entropy( "/var/lib/urandom/random-seed" );
|
|
|
|
// No config at all
|
|
|
|
{
|
|
|
|
QVariantMap m;
|
|
|
|
MachineIdJob j;
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList() );
|
|
|
|
}
|
|
|
|
// No entropy, deprecated setting
|
|
|
|
{
|
|
|
|
QVariantMap m;
|
|
|
|
MachineIdJob j;
|
|
|
|
m.insert( "entropy", false );
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList() );
|
|
|
|
}
|
|
|
|
// Entropy, deprecated setting
|
|
|
|
{
|
|
|
|
QVariantMap m;
|
|
|
|
MachineIdJob j;
|
|
|
|
m.insert( "entropy", true );
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList { urandom_entropy } );
|
|
|
|
}
|
|
|
|
// Duplicate entry, with deprecated setting
|
|
|
|
{
|
|
|
|
QVariantMap m;
|
|
|
|
MachineIdJob j;
|
|
|
|
m.insert( "entropy", true );
|
|
|
|
m.insert( "entropy-files", QStringList { urandom_entropy } );
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList { urandom_entropy } );
|
|
|
|
|
|
|
|
m.clear();
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList() );
|
|
|
|
|
|
|
|
// This would be weird
|
|
|
|
m.insert( "entropy", false );
|
|
|
|
m.insert( "entropy-files", QStringList { urandom_entropy } );
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList { urandom_entropy } );
|
|
|
|
}
|
|
|
|
// No deprecated setting
|
|
|
|
{
|
|
|
|
QString tmp_entropy( "/tmp/entropy" );
|
|
|
|
QVariantMap m;
|
|
|
|
MachineIdJob j;
|
|
|
|
m.insert( "entropy-files", QStringList { urandom_entropy, tmp_entropy } );
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QVERIFY( !j.entropyFileNames().isEmpty() );
|
|
|
|
QCOMPARE( j.entropyFileNames(), QStringList() << urandom_entropy << tmp_entropy );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-03 13:32:48 +02:00
|
|
|
void
|
|
|
|
MachineIdTests::testCopyFile()
|
|
|
|
{
|
2019-10-04 17:07:48 +02:00
|
|
|
QTemporaryDir tempRoot( QDir::tempPath() + QStringLiteral( "/test-root-XXXXXX" ) );
|
2019-10-03 13:32:48 +02:00
|
|
|
cDebug() << "Temporary files as" << QDir::tempPath();
|
2019-10-04 17:07:48 +02:00
|
|
|
cDebug() << "Temp dir file at " << tempRoot.path();
|
|
|
|
QVERIFY( !tempRoot.path().isEmpty() );
|
2019-10-03 13:32:48 +02:00
|
|
|
|
2019-10-04 17:07:48 +02:00
|
|
|
// This will pretend to be the host system
|
|
|
|
QTemporaryDir tempISOdir( QDir::tempPath() + QStringLiteral( "/test-live-XXXXXX" ) );
|
|
|
|
QVERIFY( QDir( tempRoot.path() ).mkpath( tempRoot.path() + tempISOdir.path() ) );
|
|
|
|
|
|
|
|
QFile source( tempRoot.filePath( "example" ) );
|
2019-10-03 13:32:48 +02:00
|
|
|
QVERIFY( !source.exists() );
|
|
|
|
source.open( QIODevice::WriteOnly );
|
|
|
|
source.write( "Derp" );
|
|
|
|
source.close();
|
|
|
|
QCOMPARE( source.size(), 4 );
|
|
|
|
QVERIFY( source.exists() );
|
|
|
|
|
|
|
|
// This should fail since "example" isn't standard in our test directory
|
2019-10-04 17:07:48 +02:00
|
|
|
auto r0 = MachineId::copyFile( tempRoot.path(), "example" );
|
2019-10-03 13:32:48 +02:00
|
|
|
QVERIFY( !r0 );
|
|
|
|
|
2019-10-04 17:07:48 +02:00
|
|
|
const QString sampleFile = QStringLiteral( "CMakeCache.txt" );
|
|
|
|
if ( QFile::exists( sampleFile ) )
|
2019-10-03 13:32:48 +02:00
|
|
|
{
|
2019-10-04 17:07:48 +02:00
|
|
|
auto r1 = MachineId::copyFile( tempRoot.path(), sampleFile );
|
|
|
|
// Also fail, because it's not an absolute path
|
|
|
|
QVERIFY( !r1 );
|
|
|
|
|
|
|
|
QVERIFY( QFile::copy( sampleFile, tempISOdir.path() + '/' + sampleFile ) );
|
|
|
|
auto r2 = MachineId::copyFile( tempRoot.path(), tempISOdir.path() + '/' + sampleFile );
|
|
|
|
QVERIFY( r2 );
|
2019-10-03 13:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineIdTests::testPoolSize()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_FREEBSD
|
|
|
|
// It hardly makes sense, but also the /proc entry is missing
|
2020-02-17 11:51:56 +01:00
|
|
|
QCOMPARE( getUrandomPoolSize(), 512 );
|
2019-10-03 13:32:48 +02:00
|
|
|
#else
|
|
|
|
// Based on a sample size of 1, Netrunner
|
2020-02-17 11:51:56 +01:00
|
|
|
QCOMPARE( getUrandomPoolSize(), 4096 );
|
2019-10-03 13:32:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:29:42 +01:00
|
|
|
void
|
|
|
|
MachineIdTests::testJob()
|
|
|
|
{
|
|
|
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
|
|
|
|
2020-02-21 19:21:58 +01:00
|
|
|
QTemporaryDir tempRoot( QDir::tempPath() + QStringLiteral( "/test-job-XXXXXX" ) );
|
2020-09-15 13:36:10 +02:00
|
|
|
// Only clean up if the tests succeed
|
2020-02-21 19:21:58 +01:00
|
|
|
tempRoot.setAutoRemove( false );
|
|
|
|
cDebug() << "Temporary files as" << QDir::tempPath();
|
|
|
|
|
2020-02-07 20:29:42 +01:00
|
|
|
// Ensure we have a system object, expect it to be a "bogus" one
|
|
|
|
CalamaresUtils::System* system = CalamaresUtils::System::instance();
|
|
|
|
QVERIFY( system );
|
|
|
|
QVERIFY( system->doChroot() );
|
|
|
|
|
|
|
|
// Ensure we have a system-wide GlobalStorage with /tmp as root
|
|
|
|
if ( !Calamares::JobQueue::instance() )
|
|
|
|
{
|
|
|
|
cDebug() << "Creating new JobQueue";
|
|
|
|
(void)new Calamares::JobQueue();
|
|
|
|
}
|
|
|
|
Calamares::GlobalStorage* gs
|
|
|
|
= Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr;
|
|
|
|
QVERIFY( gs );
|
2020-02-21 19:21:58 +01:00
|
|
|
gs->insert( "rootMountPoint", tempRoot.path() );
|
2020-02-07 20:29:42 +01:00
|
|
|
|
|
|
|
// Prepare part of the target filesystem
|
2020-02-14 13:07:29 +01:00
|
|
|
{
|
2020-08-22 01:19:58 +02:00
|
|
|
QVERIFY( system->createTargetDirs( "/etc" ) );
|
2020-02-14 13:07:29 +01:00
|
|
|
auto r = system->createTargetFile( "/etc/machine-id", "Hello" );
|
|
|
|
QVERIFY( !r.failed() );
|
|
|
|
QVERIFY( r );
|
|
|
|
QVERIFY( !r.path().isEmpty() );
|
|
|
|
}
|
2020-02-07 20:29:42 +01:00
|
|
|
|
|
|
|
MachineIdJob job( nullptr );
|
|
|
|
QVERIFY( !job.prettyName().isEmpty() );
|
|
|
|
|
|
|
|
QVariantMap config;
|
|
|
|
config.insert( "dbus", true );
|
|
|
|
job.setConfigurationMap( config );
|
|
|
|
|
|
|
|
{
|
|
|
|
auto r = job.exec();
|
|
|
|
QVERIFY( !r ); // It's supposed to fail, because no dbus-uuidgen executable exists
|
2020-02-21 19:21:58 +01:00
|
|
|
QVERIFY( QFile::exists( tempRoot.filePath( "var/lib/dbus" ) ) ); // but the target dir exists
|
2020-02-07 20:29:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
config.insert( "dbus-symlink", true );
|
|
|
|
job.setConfigurationMap( config );
|
|
|
|
{
|
|
|
|
auto r = job.exec();
|
|
|
|
QVERIFY( !r ); // It's supposed to fail, because no dbus-uuidgen executable exists
|
2020-02-21 19:21:58 +01:00
|
|
|
QVERIFY( QFile::exists( tempRoot.filePath( "var/lib/dbus" ) ) ); // but the target dir exists
|
2020-02-07 20:29:42 +01:00
|
|
|
|
|
|
|
// These all (would) fail, because the chroot isn't viable
|
|
|
|
#if 0
|
|
|
|
QVERIFY( QFile::exists( "/tmp/var/lib/dbus/machine-id" ) );
|
|
|
|
|
|
|
|
QFileInfo fi( "/tmp/var/lib/dbus/machine-id" );
|
|
|
|
QVERIFY( fi.exists() );
|
|
|
|
QVERIFY( fi.isSymLink() );
|
2020-08-22 01:19:58 +02:00
|
|
|
QCOMPARE( fi.size(), 5 );
|
2020-02-07 20:29:42 +01:00
|
|
|
#endif
|
|
|
|
}
|
2020-09-15 13:36:10 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
QString tmp_entropy2( "/pineapple.random" );
|
|
|
|
QString tmp_entropy( "/tmp/entropy" );
|
|
|
|
QVariantMap m;
|
|
|
|
MachineIdJob j;
|
|
|
|
m.insert( "entropy-files", QStringList { tmp_entropy2, tmp_entropy } );
|
|
|
|
m.insert( "entropy", true );
|
|
|
|
j.setConfigurationMap( m );
|
|
|
|
QCOMPARE( j.entropyFileNames().count(), 3 ); // Because of the standard entropy entry
|
|
|
|
|
|
|
|
// Check all three are created
|
|
|
|
auto r = j.exec();
|
|
|
|
QVERIFY( r );
|
|
|
|
for ( const auto& fileName : j.entropyFileNames() )
|
|
|
|
{
|
2020-09-15 16:49:45 +02:00
|
|
|
cDebug() << "Verifying existence of" << fileName;
|
|
|
|
QVERIFY( QFile::exists( tempRoot.filePath( fileName.mid( 1 ) ) ) );
|
2020-09-15 13:36:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:21:58 +01:00
|
|
|
tempRoot.setAutoRemove( true ); // All tests succeeded
|
2020-02-07 20:29:42 +01:00
|
|
|
}
|
2019-10-03 13:32:48 +02:00
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN( MachineIdTests )
|
|
|
|
|
|
|
|
#include "utils/moc-warnings.h"
|
2019-11-28 23:31:50 +01:00
|
|
|
|
|
|
|
#include "Tests.moc"
|