2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-02-17 18:10:46 +01:00
|
|
|
*
|
2020-07-29 13:31:39 +02:00
|
|
|
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2020-02-17 18:10:46 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2020-02-17 18:10:46 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SetHostNameJob.h"
|
|
|
|
|
|
|
|
// Implementation details
|
|
|
|
extern bool setFileHostname( const QString& );
|
|
|
|
extern bool writeFileEtcHosts( const QString& );
|
2020-02-18 10:16:19 +01:00
|
|
|
extern bool setSystemdHostname( const QString& );
|
2020-02-17 18:10:46 +01:00
|
|
|
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
#include "utils/Yaml.h"
|
|
|
|
|
|
|
|
#include <QTemporaryDir>
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2022-03-21 14:15:15 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-02-17 18:10:46 +01:00
|
|
|
class UsersTests : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
UsersTests();
|
2020-09-22 22:14:41 +02:00
|
|
|
~UsersTests() override {}
|
2020-02-17 18:10:46 +01:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
|
|
|
|
void testEtcHostname();
|
|
|
|
void testEtcHosts();
|
2020-02-18 10:16:19 +01:00
|
|
|
void testHostnamed();
|
2020-02-17 18:10:46 +01:00
|
|
|
|
2020-02-17 18:21:44 +01:00
|
|
|
void cleanup();
|
|
|
|
|
2020-02-17 18:10:46 +01:00
|
|
|
private:
|
|
|
|
QTemporaryDir m_dir;
|
2022-03-21 14:08:17 +01:00
|
|
|
QString m_originalHostName;
|
2020-02-17 18:10:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
UsersTests::UsersTests()
|
|
|
|
: m_dir( QStringLiteral( "/tmp/calamares-usertest" ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:21:44 +01:00
|
|
|
void
|
|
|
|
UsersTests::initTestCase()
|
2020-02-17 18:10:46 +01:00
|
|
|
{
|
|
|
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
|
|
|
cDebug() << "Users test started.";
|
|
|
|
cDebug() << "Test dir" << m_dir.path();
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
gs->insert( "rootMountPoint", m_dir.path() );
|
2022-03-21 14:08:17 +01:00
|
|
|
|
|
|
|
if ( m_originalHostName.isEmpty() )
|
|
|
|
{
|
|
|
|
QFile hostname( QStringLiteral( "/etc/hostname" ) );
|
|
|
|
if ( hostname.exists() && hostname.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
|
|
|
{
|
|
|
|
m_originalHostName = hostname.readAll().trimmed();
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 18:10:46 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:21:44 +01:00
|
|
|
void
|
|
|
|
UsersTests::testEtcHostname()
|
2020-02-17 18:10:46 +01:00
|
|
|
{
|
|
|
|
cDebug() << "Test dir" << m_dir.path();
|
|
|
|
|
|
|
|
QVERIFY( QFile::exists( m_dir.path() ) );
|
|
|
|
QVERIFY( !QFile::exists( m_dir.filePath( "etc" ) ) );
|
|
|
|
|
2022-04-11 14:55:05 +02:00
|
|
|
const QString testHostname = QStringLiteral( "tubophone.calamares.io" );
|
2020-02-17 18:10:46 +01:00
|
|
|
// Doesn't create intermediate directories
|
2022-04-11 14:55:05 +02:00
|
|
|
QVERIFY( !setFileHostname( testHostname ) );
|
2020-02-17 18:10:46 +01:00
|
|
|
|
|
|
|
QVERIFY( CalamaresUtils::System::instance()->createTargetDirs( "/etc" ) );
|
|
|
|
QVERIFY( QFile::exists( m_dir.filePath( "etc" ) ) );
|
|
|
|
|
|
|
|
// Does write the file
|
2022-04-11 14:55:05 +02:00
|
|
|
QVERIFY( setFileHostname( testHostname ) );
|
2020-02-17 18:10:46 +01:00
|
|
|
QVERIFY( QFile::exists( m_dir.filePath( "etc/hostname" ) ) );
|
|
|
|
|
|
|
|
// 22 for the test string, above, and 1 for the newline
|
2022-04-11 14:55:05 +02:00
|
|
|
QCOMPARE( QFileInfo( m_dir.filePath( "etc/hostname" ) ).size(), testHostname.length() + 1 );
|
2020-02-17 18:10:46 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:21:44 +01:00
|
|
|
void
|
|
|
|
UsersTests::testEtcHosts()
|
2020-02-17 18:10:46 +01:00
|
|
|
{
|
|
|
|
// Assume previous tests did their work
|
|
|
|
QVERIFY( QFile::exists( m_dir.path() ) );
|
|
|
|
QVERIFY( QFile::exists( m_dir.filePath( "etc" ) ) );
|
|
|
|
|
2022-04-11 14:51:52 +02:00
|
|
|
const QString testHostname = QStringLiteral( "tubophone.calamares.io" );
|
|
|
|
QVERIFY( writeFileEtcHosts( testHostname ) );
|
2020-02-17 18:10:46 +01:00
|
|
|
QVERIFY( QFile::exists( m_dir.filePath( "etc/hosts" ) ) );
|
|
|
|
// The skeleton contains %1 which has the hostname substituted in, so we lose two,
|
2022-04-11 14:51:52 +02:00
|
|
|
// and the rest of the blabla is 145 (the "standard" part) and 34 (the "for this host" part)
|
|
|
|
QCOMPARE( QFileInfo( m_dir.filePath( "etc/hosts" ) ).size(), 145 + 34 + testHostname.length() - 2 );
|
2020-02-17 18:10:46 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 10:16:19 +01:00
|
|
|
void
|
|
|
|
UsersTests::testHostnamed()
|
|
|
|
{
|
|
|
|
// Since the service might not be running (e.g. non-systemd systems,
|
|
|
|
// FreeBSD, docker, ..) we're not going to fail a test here.
|
2022-03-21 14:15:15 +01:00
|
|
|
// There's also the permissions problem to think of. But if we're
|
|
|
|
// root, assume it will succeed.
|
|
|
|
if ( geteuid() != 0 )
|
|
|
|
{
|
2022-06-28 23:50:56 +02:00
|
|
|
if ( !setSystemdHostname( QStringLiteral( "tubophone.calamares.io" ) ) )
|
|
|
|
{
|
|
|
|
QEXPECT_FAIL( "", "Hostname changes are access-controlled", Continue );
|
|
|
|
}
|
2022-03-21 14:15:15 +01:00
|
|
|
}
|
2022-03-21 14:08:17 +01:00
|
|
|
QVERIFY( setSystemdHostname( QStringLiteral( "tubophone.calamares.io" ) ) );
|
|
|
|
if ( !m_originalHostName.isEmpty() )
|
|
|
|
{
|
2022-03-21 14:12:18 +01:00
|
|
|
// If the previous test succeeded (to change the hostname to something bogus)
|
|
|
|
// then this one should, also; or, if the previous one failed, then this
|
|
|
|
// changes to whatever-the-hostname-is, and systemd dbus seems to call that
|
|
|
|
// a success, as well (since nothing changes). So no failure-expectation here.
|
2022-05-29 17:00:37 +02:00
|
|
|
setSystemdHostname( m_originalHostName );
|
2022-03-21 14:08:17 +01:00
|
|
|
}
|
2020-02-18 10:16:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-17 18:21:44 +01:00
|
|
|
void
|
|
|
|
UsersTests::cleanup()
|
|
|
|
{
|
|
|
|
if ( QTest::currentTestFailed() )
|
|
|
|
{
|
|
|
|
m_dir.setAutoRemove( false );
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 18:10:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN( UsersTests )
|
|
|
|
|
|
|
|
#include "utils/moc-warnings.h"
|
|
|
|
|
2020-07-29 13:23:41 +02:00
|
|
|
#include "TestSetHostNameJob.moc"
|