2019-10-03 13:32:48 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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() {}
|
|
|
|
virtual ~MachineIdTests() {}
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
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" ) );
|
|
|
|
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-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"
|