From fe04ae3ac1b84968b559f8ef32c6071c735c40a7 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 9 May 2022 14:23:53 +0200 Subject: [PATCH] [users] Introduce new structure for users-settings --- src/modules/users/Config.cpp | 19 ++++++--- src/modules/users/Tests.cpp | 55 ++++++++++++++++++++++++++ src/modules/users/tests/7an-shell.conf | 8 ++++ src/modules/users/tests/7ao-shell.conf | 7 ++++ src/modules/users/tests/7bn-shell.conf | 8 ++++ src/modules/users/tests/7bo-shell.conf | 7 ++++ src/modules/users/tests/7cn-shell.conf | 8 ++++ src/modules/users/tests/7co-shell.conf | 7 ++++ src/modules/users/tests/7dn-shell.conf | 8 ++++ src/modules/users/tests/7do-shell.conf | 7 ++++ src/modules/users/tests/7en-shell.conf | 8 ++++ src/modules/users/tests/7eo-shell.conf | 7 ++++ src/modules/users/tests/7fb-shell.conf | 10 +++++ src/modules/users/tests/7fn-shell.conf | 10 +++++ src/modules/users/tests/7fo-shell.conf | 10 +++++ src/modules/users/users.schema.yaml | 5 +++ 16 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 src/modules/users/tests/7an-shell.conf create mode 100644 src/modules/users/tests/7ao-shell.conf create mode 100644 src/modules/users/tests/7bn-shell.conf create mode 100644 src/modules/users/tests/7bo-shell.conf create mode 100644 src/modules/users/tests/7cn-shell.conf create mode 100644 src/modules/users/tests/7co-shell.conf create mode 100644 src/modules/users/tests/7dn-shell.conf create mode 100644 src/modules/users/tests/7do-shell.conf create mode 100644 src/modules/users/tests/7en-shell.conf create mode 100644 src/modules/users/tests/7eo-shell.conf create mode 100644 src/modules/users/tests/7fb-shell.conf create mode 100644 src/modules/users/tests/7fn-shell.conf create mode 100644 src/modules/users/tests/7fo-shell.conf diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index eedfd274d..f4e6680a9 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -884,13 +884,22 @@ copyLegacy( const QVariantMap& source, const QString& sourceKey, QVariantMap& ta void Config::setConfigurationMap( const QVariantMap& configurationMap ) { - QString shell( QLatin1String( "/bin/bash" ) ); // as if it's not set at all - if ( configurationMap.contains( "userShell" ) ) + // Handle *user* key and subkeys and legacy settings { - shell = CalamaresUtils::getString( configurationMap, "userShell" ); + bool ok = false; // Ignored + QVariantMap userSettings = CalamaresUtils::getSubMap( configurationMap, "user", ok ); + + // TODO:3.3: Remove calls to copyLegacy + copyLegacy( configurationMap, "userShell", userSettings, "shell" ); + + QString shell( QLatin1String( "/bin/bash" ) ); // as if it's not set at all + if ( userSettings.contains( "shell" ) ) + { + shell = CalamaresUtils::getString( userSettings, "shell" ); + } + // Now it might be explicitly set to empty, which is ok + setUserShell( shell ); } - // Now it might be explicitly set to empty, which is ok - setUserShell( shell ); setAutoLoginGroup( either< QString, const QString& >( CalamaresUtils::getString, configurationMap, "autologinGroup", "autoLoginGroup", QString() ) ); diff --git a/src/modules/users/Tests.cpp b/src/modules/users/Tests.cpp index 4e5a14a7e..b6badbe91 100644 --- a/src/modules/users/Tests.cpp +++ b/src/modules/users/Tests.cpp @@ -53,6 +53,9 @@ private Q_SLOTS: void testAutoLogin_data(); void testAutoLogin(); + + void testUserYAML_data(); + void testUserYAML(); }; UserTests::UserTests() {} @@ -455,6 +458,58 @@ UserTests::testAutoLogin() QCOMPARE( c.autoLoginGroup(), autoLoginGroupName ); } +void +UserTests::testUserYAML_data() +{ + QTest::addColumn< QString >( "filename" ); + QTest::addColumn< QString >( "shell" ); + + QTest::newRow( "old, unset " ) << "tests/7ao-shell.conf" + << "/bin/bash"; + QTest::newRow( "old, empty " ) << "tests/7bo-shell.conf" + << ""; + QTest::newRow( "old, relative" ) << "tests/7co-shell.conf" + << "/bin/ls"; // Setting is ignored + QTest::newRow( "old, invalid " ) << "tests/7do-shell.conf" + << ""; + QTest::newRow( "old, absolute" ) << "tests/7eo-shell.conf" + << "/usr/bin/dash"; + + QTest::newRow( "new, unset " ) << "tests/7an-shell.conf" + << "/bin/bash"; + QTest::newRow( "new, empty " ) << "tests/7bn-shell.conf" + << ""; + QTest::newRow( "new, relative" ) << "tests/7cn-shell.conf" + << "/bin/ls"; // Setting is ignored + QTest::newRow( "new, invalid " ) << "tests/7dn-shell.conf" + << ""; + QTest::newRow( "new, absolute" ) << "tests/7en-shell.conf" + << "/usr/bin/dash"; +} + +void +UserTests::testUserYAML() +{ + Config c; + c.setUserShell( QStringLiteral( "/bin/ls" ) ); + + QFETCH( QString, filename ); + QFETCH( QString, shell ); + + // BUILD_AS_TEST is the source-directory path + QFile fi( QString( "%1/%2" ).arg( BUILD_AS_TEST, filename ) ); + QVERIFY( fi.exists() ); + + bool ok = false; + const auto map = CalamaresUtils::loadYaml( fi, &ok ); + QVERIFY( ok ); + QVERIFY( map.count() > 0 ); + + QCOMPARE( c.userShell(), QStringLiteral( "/bin/ls" ) ); + c.setConfigurationMap( map ); + QCOMPARE( c.userShell(), shell ); +} + QTEST_GUILESS_MAIN( UserTests ) diff --git a/src/modules/users/tests/7an-shell.conf b/src/modules/users/tests/7an-shell.conf new file mode 100644 index 000000000..772ea5236 --- /dev/null +++ b/src/modules/users/tests/7an-shell.conf @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Unset (bogus needed to keep it valid YAML) +user: + # shell: /usr/bin/dash + bogus: true diff --git a/src/modules/users/tests/7ao-shell.conf b/src/modules/users/tests/7ao-shell.conf new file mode 100644 index 000000000..e2b49dc79 --- /dev/null +++ b/src/modules/users/tests/7ao-shell.conf @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Unset (bogus needed to keep it valid YAML) +# userShell: /usr/bin/dash +bogus: true diff --git a/src/modules/users/tests/7bn-shell.conf b/src/modules/users/tests/7bn-shell.conf new file mode 100644 index 000000000..7e568eefd --- /dev/null +++ b/src/modules/users/tests/7bn-shell.conf @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly empty +user: + shell: "" + diff --git a/src/modules/users/tests/7bo-shell.conf b/src/modules/users/tests/7bo-shell.conf new file mode 100644 index 000000000..1ecaf192b --- /dev/null +++ b/src/modules/users/tests/7bo-shell.conf @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly empty +userShell: "" + diff --git a/src/modules/users/tests/7cn-shell.conf b/src/modules/users/tests/7cn-shell.conf new file mode 100644 index 000000000..a13e44933 --- /dev/null +++ b/src/modules/users/tests/7cn-shell.conf @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Non-absolute path is ignored +user: + shell: dash + diff --git a/src/modules/users/tests/7co-shell.conf b/src/modules/users/tests/7co-shell.conf new file mode 100644 index 000000000..b3e2d58d4 --- /dev/null +++ b/src/modules/users/tests/7co-shell.conf @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Non-absolute path is ignored +userShell: dash + diff --git a/src/modules/users/tests/7dn-shell.conf b/src/modules/users/tests/7dn-shell.conf new file mode 100644 index 000000000..ddca7f40c --- /dev/null +++ b/src/modules/users/tests/7dn-shell.conf @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Invalid setting (should be string), won't pass validation +user: + shell: [1] + diff --git a/src/modules/users/tests/7do-shell.conf b/src/modules/users/tests/7do-shell.conf new file mode 100644 index 000000000..18871495c --- /dev/null +++ b/src/modules/users/tests/7do-shell.conf @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Invalid setting (should be string), won't pass validation +userShell: [1] + diff --git a/src/modules/users/tests/7en-shell.conf b/src/modules/users/tests/7en-shell.conf new file mode 100644 index 000000000..d0a28a7e4 --- /dev/null +++ b/src/modules/users/tests/7en-shell.conf @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly set with full path +user: + shell: /usr/bin/dash + diff --git a/src/modules/users/tests/7eo-shell.conf b/src/modules/users/tests/7eo-shell.conf new file mode 100644 index 000000000..e8fbbf756 --- /dev/null +++ b/src/modules/users/tests/7eo-shell.conf @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly set with full path +userShell: /usr/bin/dash + diff --git a/src/modules/users/tests/7fb-shell.conf b/src/modules/users/tests/7fb-shell.conf new file mode 100644 index 000000000..cd660e897 --- /dev/null +++ b/src/modules/users/tests/7fb-shell.conf @@ -0,0 +1,10 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly set with full path +user: + shell: /usr/bin/new + bogus: true + +userShell: /usr/bin/old diff --git a/src/modules/users/tests/7fn-shell.conf b/src/modules/users/tests/7fn-shell.conf new file mode 100644 index 000000000..13dca6da4 --- /dev/null +++ b/src/modules/users/tests/7fn-shell.conf @@ -0,0 +1,10 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly set with full path +user: + shell: /usr/bin/new + bogus: true + +# userShell: /usr/bin/old diff --git a/src/modules/users/tests/7fo-shell.conf b/src/modules/users/tests/7fo-shell.conf new file mode 100644 index 000000000..c15db233d --- /dev/null +++ b/src/modules/users/tests/7fo-shell.conf @@ -0,0 +1,10 @@ +# SPDX-FileCopyrightText: no +# SPDX-License-Identifier: CC0-1.0 +# +--- +# Explicitly set with full path +user: + # shell: /usr/bin/new + bogus: true + +userShell: /usr/bin/old diff --git a/src/modules/users/users.schema.yaml b/src/modules/users/users.schema.yaml index fe45d5fb2..552ed50ed 100644 --- a/src/modules/users/users.schema.yaml +++ b/src/modules/users/users.schema.yaml @@ -8,6 +8,11 @@ type: object properties: # User shell, should be path to /bin/sh or so userShell: { type: string } + user: + additionalProperties: false + type: object + properties: + shell: { type: string } # Overrides userShell # Group settings defaultGroups: type: array