[machineid] Apply new STATICTEST specifier, hide implementation details

This commit is contained in:
Adriaan de Groot 2020-02-17 11:51:56 +01:00
parent 92260e7d0b
commit b42520b0ef
3 changed files with 33 additions and 32 deletions

View File

@ -28,6 +28,9 @@
#include <QFile>
#include <QtTest/QtTest>
// Internals of Workers.cpp
extern int getUrandomPoolSize();
class MachineIdTests : public QObject
{
Q_OBJECT
@ -93,10 +96,10 @@ MachineIdTests::testPoolSize()
{
#ifdef Q_OS_FREEBSD
// It hardly makes sense, but also the /proc entry is missing
QCOMPARE( MachineId::getUrandomPoolSize(), 512 );
QCOMPARE( getUrandomPoolSize(), 512 );
#else
// Based on a sample size of 1, Netrunner
QCOMPARE( MachineId::getUrandomPoolSize(), 4096 );
QCOMPARE( getUrandomPoolSize(), 4096 );
#endif
}

View File

@ -27,6 +27,34 @@
#include <QFile>
/// @brief Returns a recommended size for the entropy pool (in bytes)
STATICTEST int
getUrandomPoolSize()
{
QFile f( "/proc/sys/kernel/random/poolsize" );
constexpr const int minimumPoolSize = 512;
int poolSize = minimumPoolSize;
if ( f.exists() && f.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QByteArray v = f.read( 16 );
if ( v.length() > 2 )
{
if ( v.endsWith( '\n' ) )
{
v.chop( 1 );
}
bool ok = false;
poolSize = v.toInt( &ok );
if ( !ok )
{
poolSize = minimumPoolSize;
}
}
}
return ( poolSize >= minimumPoolSize ) ? poolSize : minimumPoolSize;
}
namespace MachineId
{
@ -59,33 +87,6 @@ copyFile( const QString& rootMountPoint, const QString& fileName )
return Calamares::JobResult::ok();
}
int
getUrandomPoolSize()
{
QFile f( "/proc/sys/kernel/random/poolsize" );
constexpr const int minimumPoolSize = 512;
int poolSize = minimumPoolSize;
if ( f.exists() && f.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QByteArray v = f.read( 16 );
if ( v.length() > 2 )
{
if ( v.endsWith( '\n' ) )
{
v.chop( 1 );
}
bool ok = false;
poolSize = v.toInt( &ok );
if ( !ok )
{
poolSize = minimumPoolSize;
}
}
}
return ( poolSize >= minimumPoolSize ) ? poolSize : minimumPoolSize;
}
Calamares::JobResult
createNewEntropy( int poolSize, const QString& rootMountPoint, const QString& fileName )
{

View File

@ -48,9 +48,6 @@ enum class EntropyGeneration
CopyFromHost
};
/// @brief Returns a recommended size for the entropy pool (in bytes)
int getUrandomPoolSize();
/// @brief Creates a new entropy file @p fileName in the target system at @p rootMountPoint
Calamares::JobResult createNewEntropy( int poolSize, const QString& rootMountPoint, const QString& fileName );