From 094c213baa2b07e55b22bd59af3aba4424b80715 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 18 Feb 2020 10:16:19 +0100 Subject: [PATCH] [users] Also test hostname-setting via hostnamed SEE #1140 --- src/modules/users/SetHostNameJob.cpp | 12 +++++++++++- src/modules/users/Tests.cpp | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/modules/users/SetHostNameJob.cpp b/src/modules/users/SetHostNameJob.cpp index b43968f99..98343027b 100644 --- a/src/modules/users/SetHostNameJob.cpp +++ b/src/modules/users/SetHostNameJob.cpp @@ -83,20 +83,27 @@ ff02::2 ip6-allrouters .failed() ); } -STATICTEST void +STATICTEST bool setSystemdHostname( const QString& hostname ) { QDBusInterface hostnamed( "org.freedesktop.hostname1", "/org/freedesktop/hostname1", "org.freedesktop.hostname1", QDBusConnection::systemBus() ); + if ( !hostnamed.isValid() ) + { + cWarning() << "Interface" << hostnamed.interface() << "is not valid."; + return false; + } + bool success = true; // Static, writes /etc/hostname { QDBusReply< uint > r = hostnamed.call( "SetStaticHostname", hostname, false ); if ( !r.isValid() ) { cWarning() << "Could not set hostname through org.freedesktop.hostname1.SetStaticHostname." << r.error(); + success = false; } } // Dynamic, updates kernel @@ -105,8 +112,11 @@ setSystemdHostname( const QString& hostname ) if ( !r.isValid() ) { cWarning() << "Could not set hostname through org.freedesktop.hostname1.SetHostname." << r.error(); + success = false; } } + + return success; } diff --git a/src/modules/users/Tests.cpp b/src/modules/users/Tests.cpp index 3c1d2d25d..196fd9d68 100644 --- a/src/modules/users/Tests.cpp +++ b/src/modules/users/Tests.cpp @@ -21,7 +21,7 @@ // Implementation details extern bool setFileHostname( const QString& ); extern bool writeFileEtcHosts( const QString& ); -extern void setSystemdHostname( const QString& ); +extern bool setSystemdHostname( const QString& ); #include "GlobalStorage.h" #include "JobQueue.h" @@ -44,6 +44,7 @@ private Q_SLOTS: void testEtcHostname(); void testEtcHosts(); + void testHostnamed(); void cleanup(); @@ -116,6 +117,17 @@ UsersTests::testEtcHosts() QCOMPARE( QFileInfo( m_dir.filePath( "etc/hosts" ) ).size(), 150 + 22 - 2 ); } +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. + // There's also the permissions problem to think of. + QEXPECT_FAIL( "", "Hostname changes are access-controlled", Continue ); + QVERIFY( setSystemdHostname( "tubophone.calamares.io" ) ); +} + + void UsersTests::cleanup() {