From 892e9798f4dd9400e3d97c7da81d8ed01af4cc5d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 29 Jul 2020 13:31:39 +0200 Subject: [PATCH] [users] Sanitize tests - move the testing of config-object methods to its own tests - simplify file structure for the password job tests --- src/modules/users/CMakeLists.txt | 8 +- src/modules/users/TestCreateUserJob.cpp | 58 ------------ src/modules/users/TestPasswordJob.cpp | 23 ++++- src/modules/users/TestPasswordJob.h | 36 -------- src/modules/users/TestSetHostNameJob.cpp | 3 +- src/modules/users/Tests.cpp | 113 +++++++++++++++++++++++ 6 files changed, 141 insertions(+), 100 deletions(-) delete mode 100644 src/modules/users/TestPasswordJob.h create mode 100644 src/modules/users/Tests.cpp diff --git a/src/modules/users/CMakeLists.txt b/src/modules/users/CMakeLists.txt index a486e93c3..5fafcd4f3 100644 --- a/src/modules/users/CMakeLists.txt +++ b/src/modules/users/CMakeLists.txt @@ -55,7 +55,6 @@ calamares_add_test( SOURCES TestCreateUserJob.cpp CreateUserJob.cpp - Config.cpp ) calamares_add_test( @@ -66,3 +65,10 @@ calamares_add_test( LIBRARIES Qt5::DBus ) + +calamares_add_test( + userstest + SOURCES + Tests.cpp + Config.cpp +) diff --git a/src/modules/users/TestCreateUserJob.cpp b/src/modules/users/TestCreateUserJob.cpp index f8c28a235..610f84eef 100644 --- a/src/modules/users/TestCreateUserJob.cpp +++ b/src/modules/users/TestCreateUserJob.cpp @@ -17,7 +17,6 @@ * along with Calamares. If not, see . */ -#include "Config.h" #include "CreateUserJob.h" #include "utils/Logger.h" @@ -27,7 +26,6 @@ // Implementation details extern QStringList groupsInTargetSystem( const QDir& targetRoot ); // CreateUserJob -extern void setConfigurationDefaultGroups( const QVariantMap& map, QStringList& defaultGroups ); class CreateUserTests : public QObject { @@ -40,7 +38,6 @@ private Q_SLOTS: void initTestCase(); void testReadGroup(); - void testDefaultGroups(); }; CreateUserTests::CreateUserTests() {} @@ -75,61 +72,6 @@ CreateUserTests::testReadGroup() } } -void -CreateUserTests::testDefaultGroups() -{ - { - QStringList groups; - QVariantMap hweelGroup; - QVERIFY( groups.isEmpty() ); - hweelGroup.insert( "defaultGroups", QStringList { "hweel" } ); - setConfigurationDefaultGroups( hweelGroup, groups ); - QCOMPARE( groups.count(), 1 ); - QVERIFY( groups.contains( "hweel" ) ); - } - - { - QStringList desired { "wheel", "root", "operator" }; - QStringList groups; - QVariantMap threeGroup; - QVERIFY( groups.isEmpty() ); - threeGroup.insert( "defaultGroups", desired ); - setConfigurationDefaultGroups( threeGroup, groups ); - QCOMPARE( groups.count(), 3 ); - QVERIFY( !groups.contains( "hweel" ) ); - QCOMPARE( groups, desired ); - } - - { - QStringList groups; - QVariantMap explicitEmpty; - QVERIFY( groups.isEmpty() ); - explicitEmpty.insert( "defaultGroups", QStringList() ); - setConfigurationDefaultGroups( explicitEmpty, groups ); - QCOMPARE( groups.count(), 0 ); - } - - { - QStringList groups; - QVariantMap missing; - QVERIFY( groups.isEmpty() ); - setConfigurationDefaultGroups( missing, groups ); - QCOMPARE( groups.count(), 6 ); // because of fallback! - QVERIFY( groups.contains( "lp" ) ); - } - - { - QStringList groups; - QVariantMap typeMismatch; - QVERIFY( groups.isEmpty() ); - typeMismatch.insert( "defaultGroups", 1 ); - setConfigurationDefaultGroups( typeMismatch, groups ); - QCOMPARE( groups.count(), 6 ); // because of fallback! - QVERIFY( groups.contains( "lp" ) ); - } -} - - QTEST_GUILESS_MAIN( CreateUserTests ) #include "utils/moc-warnings.h" diff --git a/src/modules/users/TestPasswordJob.cpp b/src/modules/users/TestPasswordJob.cpp index 7f38c109d..8677f88c2 100644 --- a/src/modules/users/TestPasswordJob.cpp +++ b/src/modules/users/TestPasswordJob.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * - * Copyright 2017, Adriaan de Groot + * SPDX-FileCopyrightText: 2017 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,11 +19,19 @@ #include "SetPasswordJob.h" -#include "TestPasswordJob.h" - #include -QTEST_GUILESS_MAIN( PasswordTests ) +class PasswordTests : public QObject +{ + Q_OBJECT +public: + PasswordTests(); + ~PasswordTests() override; + +private Q_SLOTS: + void initTestCase(); + void testSalt(); +}; PasswordTests::PasswordTests() {} @@ -48,3 +57,9 @@ PasswordTests::testSalt() QVERIFY( s.endsWith( '$' ) ); qDebug() << "Obtained salt" << s; } + +QTEST_GUILESS_MAIN( PasswordTests ) + +#include "utils/moc-warnings.h" + +#include "TestPasswordJob.moc" diff --git a/src/modules/users/TestPasswordJob.h b/src/modules/users/TestPasswordJob.h deleted file mode 100644 index 3b4b5d201..000000000 --- a/src/modules/users/TestPasswordJob.h +++ /dev/null @@ -1,36 +0,0 @@ -/* === This file is part of Calamares - === - * - * Copyright 2017, Adriaan de Groot - * - * 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 . - */ - -#ifndef PASSWORDTESTS_H -#define PASSWORDTESTS_H - -#include - -class PasswordTests : public QObject -{ - Q_OBJECT -public: - PasswordTests(); - ~PasswordTests() override; - -private Q_SLOTS: - void initTestCase(); - void testSalt(); -}; - -#endif diff --git a/src/modules/users/TestSetHostNameJob.cpp b/src/modules/users/TestSetHostNameJob.cpp index 9e08d4807..0e887a5f1 100644 --- a/src/modules/users/TestSetHostNameJob.cpp +++ b/src/modules/users/TestSetHostNameJob.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * - * Copyright 2020, Adriaan de Groot + * SPDX-FileCopyrightText: 2020 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/modules/users/Tests.cpp b/src/modules/users/Tests.cpp new file mode 100644 index 000000000..a4ee45fe2 --- /dev/null +++ b/src/modules/users/Tests.cpp @@ -0,0 +1,113 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2020 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * 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 . + */ + +#include "Config.h" + +#include "utils/Logger.h" + +#include + +// Implementation details +extern void setConfigurationDefaultGroups( const QVariantMap& map, QStringList& defaultGroups ); + +/** @brief Test Config object methods and internals + * + */ +class UserTests : public QObject +{ + Q_OBJECT +public: + UserTests(); + virtual ~UserTests() {} + +private Q_SLOTS: + void initTestCase(); + + void testDefaultGroups(); +}; + +UserTests::UserTests() {} + +void +UserTests::initTestCase() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); + cDebug() << "Users test started."; +} + +void +UserTests::testDefaultGroups() +{ + { + QStringList groups; + QVariantMap hweelGroup; + QVERIFY( groups.isEmpty() ); + hweelGroup.insert( "defaultGroups", QStringList { "hweel" } ); + setConfigurationDefaultGroups( hweelGroup, groups ); + QCOMPARE( groups.count(), 1 ); + QVERIFY( groups.contains( "hweel" ) ); + } + + { + QStringList desired { "wheel", "root", "operator" }; + QStringList groups; + QVariantMap threeGroup; + QVERIFY( groups.isEmpty() ); + threeGroup.insert( "defaultGroups", desired ); + setConfigurationDefaultGroups( threeGroup, groups ); + QCOMPARE( groups.count(), 3 ); + QVERIFY( !groups.contains( "hweel" ) ); + QCOMPARE( groups, desired ); + } + + { + QStringList groups; + QVariantMap explicitEmpty; + QVERIFY( groups.isEmpty() ); + explicitEmpty.insert( "defaultGroups", QStringList() ); + setConfigurationDefaultGroups( explicitEmpty, groups ); + QCOMPARE( groups.count(), 0 ); + } + + { + QStringList groups; + QVariantMap missing; + QVERIFY( groups.isEmpty() ); + setConfigurationDefaultGroups( missing, groups ); + QCOMPARE( groups.count(), 6 ); // because of fallback! + QVERIFY( groups.contains( "lp" ) ); + } + + { + QStringList groups; + QVariantMap typeMismatch; + QVERIFY( groups.isEmpty() ); + typeMismatch.insert( "defaultGroups", 1 ); + setConfigurationDefaultGroups( typeMismatch, groups ); + QCOMPARE( groups.count(), 6 ); // because of fallback! + QVERIFY( groups.contains( "lp" ) ); + } +} + + +QTEST_GUILESS_MAIN( UserTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc"