mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-10-01 01:26:01 -04:00
Merge pull request #1957 from michalkaptur/improved_tools_coverage
improved unit test coverage in Tools
This commit is contained in:
commit
558cb3d0b5
@ -24,6 +24,7 @@
|
||||
#include <QImageReader>
|
||||
#include <QLocale>
|
||||
#include <QStringList>
|
||||
#include <cctype>
|
||||
|
||||
#include <QElapsedTimer>
|
||||
|
||||
@ -56,8 +57,9 @@
|
||||
namespace Tools
|
||||
{
|
||||
|
||||
QString humanReadableFileSize(qint64 bytes)
|
||||
QString humanReadableFileSize(qint64 bytes, quint32 precision)
|
||||
{
|
||||
constexpr auto kibibyte = 1024;
|
||||
double size = bytes;
|
||||
|
||||
QStringList units = QStringList() << "B"
|
||||
@ -67,12 +69,12 @@ namespace Tools
|
||||
int i = 0;
|
||||
int maxI = units.size() - 1;
|
||||
|
||||
while ((size >= 1024) && (i < maxI)) {
|
||||
size /= 1024;
|
||||
while ((size >= kibibyte) && (i < maxI)) {
|
||||
size /= kibibyte;
|
||||
i++;
|
||||
}
|
||||
|
||||
return QString("%1 %2").arg(QLocale().toString(size, 'f', 2), units.at(i));
|
||||
return QString("%1 %2").arg(QLocale().toString(size, 'f', precision), units.at(i));
|
||||
}
|
||||
|
||||
bool hasChild(const QObject* parent, const QObject* child)
|
||||
@ -147,8 +149,8 @@ namespace Tools
|
||||
|
||||
bool isHex(const QByteArray& ba)
|
||||
{
|
||||
for (char c : ba) {
|
||||
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
|
||||
for (const unsigned char c : ba) {
|
||||
if (!std::isxdigit(c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -158,8 +160,8 @@ namespace Tools
|
||||
|
||||
bool isBase64(const QByteArray& ba)
|
||||
{
|
||||
QRegExp regexp(
|
||||
"^(?:[a-z0-9+/]{4})*(?:[a-z0-9+/]{3}=|[a-z0-9+/]{2}==)?$", Qt::CaseInsensitive, QRegExp::RegExp2);
|
||||
constexpr auto pattern = R"(^(?:[a-z0-9+]{4})*(?:[a-z0-9+]{3}=|[a-z0-9+]{2}==)?$)";
|
||||
QRegExp regexp(pattern, Qt::CaseInsensitive, QRegExp::RegExp2);
|
||||
|
||||
QString base64 = QString::fromLatin1(ba.constData(), ba.size());
|
||||
|
||||
@ -318,14 +320,15 @@ namespace Tools
|
||||
}
|
||||
|
||||
// Set discretionary access control list
|
||||
bSuccess = ERROR_SUCCESS == SetSecurityInfo(GetCurrentProcess(), // object handle
|
||||
SE_KERNEL_OBJECT, // type of object
|
||||
DACL_SECURITY_INFORMATION, // change only the objects DACL
|
||||
nullptr,
|
||||
nullptr, // do not change owner or group
|
||||
pACL, // DACL specified
|
||||
nullptr // do not change SACL
|
||||
);
|
||||
bSuccess = ERROR_SUCCESS
|
||||
== SetSecurityInfo(GetCurrentProcess(), // object handle
|
||||
SE_KERNEL_OBJECT, // type of object
|
||||
DACL_SECURITY_INFORMATION, // change only the objects DACL
|
||||
nullptr,
|
||||
nullptr, // do not change owner or group
|
||||
pACL, // DACL specified
|
||||
nullptr // do not change SACL
|
||||
);
|
||||
|
||||
Cleanup:
|
||||
|
||||
|
@ -32,7 +32,7 @@ class QIODevice;
|
||||
namespace Tools
|
||||
{
|
||||
|
||||
QString humanReadableFileSize(qint64 bytes);
|
||||
QString humanReadableFileSize(qint64 bytes, quint32 precision = 2);
|
||||
bool hasChild(const QObject* parent, const QObject* child);
|
||||
bool readFromDevice(QIODevice* device, QByteArray& data, int size = 16384);
|
||||
bool readAllFromDevice(QIODevice* device, QByteArray& data);
|
||||
|
@ -190,6 +190,9 @@ add_unit_test(NAME testykchallengeresponsekey
|
||||
add_unit_test(NAME testdatabase SOURCES TestDatabase.cpp
|
||||
LIBS ${TEST_LIBRARIES})
|
||||
|
||||
add_unit_test(NAME testtools SOURCES TestTools.cpp
|
||||
LIBS ${TEST_LIBRARIES})
|
||||
|
||||
if(WITH_GUI_TESTS)
|
||||
add_subdirectory(gui)
|
||||
endif(WITH_GUI_TESTS)
|
||||
|
@ -24,7 +24,6 @@
|
||||
#include "config-keepassx-tests.h"
|
||||
|
||||
#include "core/Metadata.h"
|
||||
#include "core/Tools.h"
|
||||
#include "crypto/Crypto.h"
|
||||
#include "crypto/CryptoHash.h"
|
||||
#include "crypto/kdf/AesKdf.h"
|
||||
|
@ -23,7 +23,6 @@
|
||||
#include "core/Database.h"
|
||||
#include "core/Group.h"
|
||||
#include "core/Metadata.h"
|
||||
#include "core/Tools.h"
|
||||
#include "crypto/Crypto.h"
|
||||
|
||||
QTEST_GUILESS_MAIN(TestModified)
|
||||
|
65
tests/TestTools.cpp
Normal file
65
tests/TestTools.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* This program 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 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TestTools.h"
|
||||
|
||||
#include <QLocale>
|
||||
#include <QTest>
|
||||
|
||||
QTEST_GUILESS_MAIN(TestTools)
|
||||
|
||||
namespace
|
||||
{
|
||||
QString createDecimal(QString wholes, QString fractions, QString unit)
|
||||
{
|
||||
return wholes + QLocale().decimalPoint() + fractions + " " + unit;
|
||||
}
|
||||
}
|
||||
|
||||
void TestTools::testHumanReadableFileSize()
|
||||
{
|
||||
constexpr auto kibibyte = 1024u;
|
||||
using namespace Tools;
|
||||
|
||||
QCOMPARE(createDecimal("1", "00", "B"), humanReadableFileSize(1));
|
||||
QCOMPARE(createDecimal("1", "00", "KiB"), humanReadableFileSize(kibibyte));
|
||||
QCOMPARE(createDecimal("1", "00", "MiB"), humanReadableFileSize(kibibyte * kibibyte));
|
||||
QCOMPARE(createDecimal("1", "00", "GiB"), humanReadableFileSize(kibibyte * kibibyte * kibibyte));
|
||||
|
||||
QCOMPARE(QString("100 B"), humanReadableFileSize(100, 0));
|
||||
QCOMPARE(createDecimal("1", "10", "KiB"), humanReadableFileSize(kibibyte + 100));
|
||||
QCOMPARE(createDecimal("1", "001", "KiB"), humanReadableFileSize(kibibyte + 1, 3));
|
||||
QCOMPARE(createDecimal("15", "00", "KiB"), humanReadableFileSize(kibibyte * 15));
|
||||
}
|
||||
|
||||
void TestTools::testIsHex()
|
||||
{
|
||||
QVERIFY(Tools::isHex("0123456789abcdefABCDEF"));
|
||||
QVERIFY(not Tools::isHex(QByteArray("0xnothex")));
|
||||
}
|
||||
|
||||
void TestTools::testIsBase64()
|
||||
{
|
||||
QVERIFY(Tools::isBase64(QByteArray("1234")));
|
||||
QVERIFY(Tools::isBase64(QByteArray("123=")));
|
||||
QVERIFY(Tools::isBase64(QByteArray("12==")));
|
||||
QVERIFY(Tools::isBase64(QByteArray("abcd9876MN==")));
|
||||
QVERIFY(Tools::isBase64(QByteArray("abcd9876DEFGhijkMNO=")));
|
||||
QVERIFY(not Tools::isBase64(QByteArray("abcd123==")));
|
||||
QVERIFY(not Tools::isBase64(QByteArray("abc_")));
|
||||
QVERIFY(not Tools::isBase64(QByteArray("123")));
|
||||
}
|
32
tests/TestTools.h
Normal file
32
tests/TestTools.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
||||
*
|
||||
* This program 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 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSX_TESTTOOLS_H
|
||||
#define KEEPASSX_TESTTOOLS_H
|
||||
|
||||
#include "core/Tools.h"
|
||||
|
||||
class TestTools : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void testHumanReadableFileSize();
|
||||
void testIsHex();
|
||||
void testIsBase64();
|
||||
};
|
||||
|
||||
#endif // KEEPASSX_TESTTOOLS_H
|
Loading…
Reference in New Issue
Block a user