2018-10-05 15:37:25 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
|
2018-10-05 15:37:25 +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-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
*
|
2018-10-05 15:37:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Tests.h"
|
|
|
|
|
2019-07-02 22:12:04 +02:00
|
|
|
#include "CalamaresUtilsSystem.h"
|
2020-01-29 16:14:17 +01:00
|
|
|
#include "Entropy.h"
|
2019-07-02 22:12:04 +02:00
|
|
|
#include "Logger.h"
|
2019-07-02 22:46:49 +02:00
|
|
|
#include "UMask.h"
|
2019-07-02 22:12:04 +02:00
|
|
|
#include "Yaml.h"
|
2018-10-05 15:37:25 +02:00
|
|
|
|
2020-02-07 11:51:13 +01:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
|
2019-06-07 13:56:16 +02:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
|
2018-10-05 15:37:25 +02:00
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2019-07-02 22:46:49 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-05 15:37:25 +02:00
|
|
|
QTEST_GUILESS_MAIN( LibCalamaresTests )
|
|
|
|
|
2019-07-02 22:12:04 +02:00
|
|
|
LibCalamaresTests::LibCalamaresTests() {}
|
2018-10-05 15:37:25 +02:00
|
|
|
|
2019-07-02 22:12:04 +02:00
|
|
|
LibCalamaresTests::~LibCalamaresTests() {}
|
2018-10-05 15:37:25 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::initTestCase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testDebugLevels()
|
|
|
|
{
|
|
|
|
Logger::setupLogLevel( Logger::LOG_DISABLE );
|
|
|
|
|
2019-07-02 22:12:04 +02:00
|
|
|
QCOMPARE( Logger::logLevel(), static_cast< unsigned int >( Logger::LOG_DISABLE ) );
|
2018-10-05 15:37:25 +02:00
|
|
|
|
2019-07-02 22:12:04 +02:00
|
|
|
for ( unsigned int level = 0; level <= Logger::LOGVERBOSE; ++level )
|
2018-10-05 15:37:25 +02:00
|
|
|
{
|
|
|
|
Logger::setupLogLevel( level );
|
|
|
|
QCOMPARE( Logger::logLevel(), level );
|
|
|
|
QVERIFY( Logger::logLevelEnabled( level ) );
|
|
|
|
|
|
|
|
for ( unsigned int xlevel = 0; xlevel <= Logger::LOGVERBOSE; ++xlevel )
|
|
|
|
{
|
|
|
|
QCOMPARE( Logger::logLevelEnabled( xlevel ), xlevel <= level );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:25:13 +01:00
|
|
|
void
|
|
|
|
LibCalamaresTests::testLoadSaveYaml()
|
|
|
|
{
|
|
|
|
QFile f( "settings.conf" );
|
2019-02-12 14:15:05 +01:00
|
|
|
// Find the nearest settings.conf to read
|
|
|
|
for ( unsigned int up = 0; !f.exists() && ( up < 4 ); ++up )
|
2019-07-02 22:12:04 +02:00
|
|
|
{
|
2019-02-12 14:15:05 +01:00
|
|
|
f.setFileName( QString( "../" ) + f.fileName() );
|
2019-07-02 22:12:04 +02:00
|
|
|
}
|
2019-02-12 14:15:05 +01:00
|
|
|
cDebug() << QDir().absolutePath() << f.fileName() << f.exists();
|
2019-01-28 14:25:13 +01:00
|
|
|
QVERIFY( f.exists() );
|
|
|
|
|
2019-02-12 14:15:05 +01:00
|
|
|
auto map = CalamaresUtils::loadYaml( f.fileName() );
|
2019-01-28 14:25:13 +01:00
|
|
|
CalamaresUtils::saveYaml( "out.yaml", map );
|
2019-01-29 13:31:29 +01:00
|
|
|
|
|
|
|
auto other_map = CalamaresUtils::loadYaml( "out.yaml" );
|
2019-07-02 22:12:04 +02:00
|
|
|
CalamaresUtils::saveYaml( " out2.yaml", other_map );
|
2019-01-29 13:31:29 +01:00
|
|
|
QCOMPARE( map, other_map );
|
|
|
|
|
|
|
|
QFile::remove( "out.yaml" );
|
|
|
|
QFile::remove( "out2.yaml" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static QStringList
|
|
|
|
findConf( const QDir& d )
|
|
|
|
{
|
|
|
|
QStringList mine;
|
|
|
|
if ( d.exists() )
|
|
|
|
{
|
|
|
|
QString path = d.absolutePath();
|
|
|
|
path.append( d.separator() );
|
|
|
|
for ( const auto& confname : d.entryList( { "*.conf" } ) )
|
|
|
|
mine.append( path + confname );
|
|
|
|
for ( const auto& subdirname : d.entryList( QDir::AllDirs | QDir::NoDotAndDotDot ) )
|
|
|
|
{
|
|
|
|
QDir subdir( d );
|
|
|
|
subdir.cd( subdirname );
|
|
|
|
mine.append( findConf( subdir ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mine;
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:19:11 +02:00
|
|
|
void LibCalamaresTests::recursiveCompareMap(const QVariantMap& a, const QVariantMap& b, int depth )
|
|
|
|
{
|
|
|
|
cDebug() << "Comparing depth" << depth << a.count() << b.count();
|
|
|
|
QCOMPARE( a.keys(), b.keys() );
|
|
|
|
for ( const auto& k : a.keys() )
|
|
|
|
{
|
|
|
|
cDebug() << Logger::SubEntry << k;
|
|
|
|
const auto& av = a[k];
|
|
|
|
const auto& bv = b[k];
|
|
|
|
|
|
|
|
if ( av.typeName() != bv.typeName() )
|
|
|
|
{
|
|
|
|
cDebug() << Logger::SubEntry << "a type" << av.typeName() << av;
|
|
|
|
cDebug() << Logger::SubEntry << "b type" << bv.typeName() << bv;
|
|
|
|
}
|
|
|
|
QCOMPARE( av.typeName(), bv.typeName() );
|
|
|
|
if ( av.canConvert<QVariantMap>() )
|
|
|
|
{
|
|
|
|
recursiveCompareMap( av.toMap(), bv.toMap(), depth+1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QCOMPARE( av, bv );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 13:31:29 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testLoadSaveYamlExtended()
|
|
|
|
{
|
2020-04-08 14:19:11 +02:00
|
|
|
bool loaded_ok;
|
2019-01-29 13:31:29 +01:00
|
|
|
for ( const auto& confname : findConf( QDir( "../src" ) ) )
|
|
|
|
{
|
2020-04-08 14:19:11 +02:00
|
|
|
loaded_ok = true;
|
2019-01-29 13:31:29 +01:00
|
|
|
cDebug() << "Testing" << confname;
|
2020-04-08 14:19:11 +02:00
|
|
|
auto map = CalamaresUtils::loadYaml( confname, &loaded_ok );
|
|
|
|
QVERIFY( loaded_ok );
|
2019-01-29 13:31:29 +01:00
|
|
|
QVERIFY( CalamaresUtils::saveYaml( "out.yaml", map ) );
|
2020-04-08 14:19:11 +02:00
|
|
|
auto othermap = CalamaresUtils::loadYaml( "out.yaml", &loaded_ok );
|
|
|
|
QVERIFY( loaded_ok );
|
|
|
|
QCOMPARE( map.keys(), othermap.keys() );
|
|
|
|
recursiveCompareMap( map, othermap, 0 );
|
2019-01-29 13:31:29 +01:00
|
|
|
QCOMPARE( map, othermap );
|
|
|
|
}
|
|
|
|
QFile::remove( "out.yaml" );
|
2019-01-28 14:25:13 +01:00
|
|
|
}
|
2019-06-07 13:56:16 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testCommands()
|
|
|
|
{
|
|
|
|
using CalamaresUtils::System;
|
2019-07-02 22:12:04 +02:00
|
|
|
auto r = System::runCommand( System::RunLocation::RunInHost, { "/bin/ls", "/tmp" } );
|
2019-06-07 13:56:16 +02:00
|
|
|
|
|
|
|
QVERIFY( r.getExitCode() == 0 );
|
|
|
|
|
|
|
|
QTemporaryFile tf( "/tmp/calamares-test-XXXXXX" );
|
|
|
|
QVERIFY( tf.open() );
|
|
|
|
QVERIFY( !tf.fileName().isEmpty() );
|
|
|
|
|
2019-06-07 16:00:37 +02:00
|
|
|
QFileInfo tfn( tf.fileName() );
|
|
|
|
QVERIFY( !r.getOutput().contains( tfn.fileName() ) );
|
|
|
|
|
|
|
|
// Run ls again, now that the file exists
|
2019-07-02 22:12:04 +02:00
|
|
|
r = System::runCommand( System::RunLocation::RunInHost, { "/bin/ls", "/tmp" } );
|
2019-06-07 16:00:37 +02:00
|
|
|
QVERIFY( r.getOutput().contains( tfn.fileName() ) );
|
|
|
|
|
|
|
|
// .. and without a working directory set, assume builddir != /tmp
|
2019-07-02 22:12:04 +02:00
|
|
|
r = System::runCommand( System::RunLocation::RunInHost, { "/bin/ls" } );
|
2019-06-07 16:00:37 +02:00
|
|
|
QVERIFY( !r.getOutput().contains( tfn.fileName() ) );
|
|
|
|
|
2019-07-02 22:12:04 +02:00
|
|
|
r = System::runCommand( System::RunLocation::RunInHost, { "/bin/ls" }, "/tmp" );
|
2019-06-07 16:00:37 +02:00
|
|
|
QVERIFY( r.getOutput().contains( tfn.fileName() ) );
|
2019-06-07 13:56:16 +02:00
|
|
|
}
|
2019-07-02 22:46:49 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testUmask()
|
|
|
|
{
|
|
|
|
struct stat mystat;
|
|
|
|
|
|
|
|
QTemporaryFile ft;
|
|
|
|
QVERIFY( ft.open() );
|
|
|
|
|
2019-08-30 12:35:53 +02:00
|
|
|
// m gets the previous value of the mask (depends on the environment the
|
|
|
|
// test is run in, might be 002, might be 077), ..
|
2019-07-02 22:46:49 +02:00
|
|
|
mode_t m = CalamaresUtils::setUMask( 022 );
|
2020-02-21 17:53:43 +01:00
|
|
|
QCOMPARE( CalamaresUtils::setUMask( m ), mode_t( 022 ) ); // But now most recently set was 022
|
2019-07-02 22:46:49 +02:00
|
|
|
|
2019-08-09 16:36:38 +02:00
|
|
|
for ( mode_t i = 0; i <= 0777 /* octal! */; ++i )
|
2019-07-02 22:46:49 +02:00
|
|
|
{
|
|
|
|
QByteArray name = ( ft.fileName() + QChar( '.' ) + QString::number( i, 8 ) ).toLatin1();
|
|
|
|
CalamaresUtils::UMask um( i );
|
|
|
|
int fd = creat( name, 0777 );
|
|
|
|
QVERIFY( fd >= 0 );
|
|
|
|
close( fd );
|
|
|
|
QFileInfo fi( name );
|
|
|
|
QVERIFY( fi.exists() );
|
|
|
|
QCOMPARE( stat( name, &mystat ), 0 );
|
|
|
|
QCOMPARE( mystat.st_mode & 0777, 0777 & ~i );
|
|
|
|
QCOMPARE( unlink( name ), 0 );
|
|
|
|
}
|
|
|
|
QCOMPARE( CalamaresUtils::setUMask( 022 ), m );
|
2020-02-21 17:53:43 +01:00
|
|
|
QCOMPARE( CalamaresUtils::setUMask( m ), mode_t( 022 ) );
|
2019-07-02 22:46:49 +02:00
|
|
|
}
|
2020-01-29 16:14:17 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testEntropy()
|
|
|
|
{
|
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
auto r0 = CalamaresUtils::getEntropy( 0, data );
|
|
|
|
QCOMPARE( CalamaresUtils::EntropySource::None, r0 );
|
2020-01-29 16:39:25 +01:00
|
|
|
QCOMPARE( data.size(), 0 );
|
2020-01-29 16:14:17 +01:00
|
|
|
|
|
|
|
auto r1 = CalamaresUtils::getEntropy( 16, data );
|
|
|
|
QVERIFY( r1 != CalamaresUtils::EntropySource::None );
|
2020-01-29 16:39:25 +01:00
|
|
|
QCOMPARE( data.size(), 16 );
|
2020-01-29 16:14:17 +01:00
|
|
|
// This can randomly fail (but not often)
|
|
|
|
QVERIFY( data.at( data.size() - 1 ) != char( 0xcb ) );
|
|
|
|
|
|
|
|
auto r2 = CalamaresUtils::getEntropy( 8, data );
|
|
|
|
QVERIFY( r2 != CalamaresUtils::EntropySource::None );
|
2020-01-29 16:39:25 +01:00
|
|
|
QCOMPARE( data.size(), 8 );
|
2020-01-29 16:14:17 +01:00
|
|
|
QCOMPARE( r1, r2 );
|
|
|
|
// This can randomly fail (but not often)
|
|
|
|
QVERIFY( data.at( data.size() - 1 ) != char( 0xcb ) );
|
|
|
|
}
|
2020-01-29 16:39:25 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testPrintableEntropy()
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
|
|
|
|
auto r0 = CalamaresUtils::getPrintableEntropy( 0, s );
|
|
|
|
QCOMPARE( CalamaresUtils::EntropySource::None, r0 );
|
|
|
|
QCOMPARE( s.length(), 0 );
|
|
|
|
|
|
|
|
auto r1 = CalamaresUtils::getPrintableEntropy( 16, s );
|
|
|
|
QVERIFY( r1 != CalamaresUtils::EntropySource::None );
|
|
|
|
QCOMPARE( s.length(), 16 );
|
|
|
|
for ( QChar c : s )
|
|
|
|
{
|
|
|
|
QVERIFY( c.isPrint() );
|
2020-02-21 17:53:43 +01:00
|
|
|
QCOMPARE( c.row(), uchar( 0 ) );
|
2020-01-29 16:41:17 +01:00
|
|
|
QVERIFY( c.cell() > 32 ); // ASCII SPACE
|
|
|
|
QVERIFY( c.cell() < 127 );
|
2020-01-29 16:39:25 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 12:22:02 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
LibCalamaresTests::testOddSizedPrintable()
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
for ( int l = 0; l <= 37; ++l )
|
|
|
|
{
|
|
|
|
auto r = CalamaresUtils::getPrintableEntropy( l, s );
|
|
|
|
if ( l == 0 )
|
|
|
|
{
|
|
|
|
QCOMPARE( r, CalamaresUtils::EntropySource::None );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QVERIFY( r != CalamaresUtils::EntropySource::None );
|
|
|
|
}
|
|
|
|
QCOMPARE( s.length(), l );
|
|
|
|
|
|
|
|
for ( QChar c : s )
|
|
|
|
{
|
|
|
|
QVERIFY( c.isPrint() );
|
2020-02-21 17:53:43 +01:00
|
|
|
QCOMPARE( c.row(), uchar( 0 ) );
|
2020-02-12 12:22:02 +01:00
|
|
|
QVERIFY( c.cell() > 32 ); // ASCII SPACE
|
|
|
|
QVERIFY( c.cell() < 127 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|