2013-08-09 14:56:43 +04:00
|
|
|
/*
|
|
|
|
This software is in the public domain, furnished "as is", without technical
|
|
|
|
support, and with no warranty, express or implied, as to its usefulness for
|
|
|
|
any purpose.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtTest>
|
2016-10-12 15:48:00 +03:00
|
|
|
#include <QTemporaryDir>
|
2013-08-09 14:56:43 +04:00
|
|
|
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2020-12-14 17:37:05 +03:00
|
|
|
#include "config.h"
|
2013-08-09 14:56:43 +04:00
|
|
|
|
2014-11-10 00:36:49 +03:00
|
|
|
using namespace OCC::Utility;
|
2013-08-09 14:56:43 +04:00
|
|
|
|
2018-02-20 16:29:45 +03:00
|
|
|
namespace OCC {
|
|
|
|
OCSYNC_EXPORT extern bool fsCasePreserving_override;
|
|
|
|
}
|
|
|
|
|
2013-08-09 14:56:43 +04:00
|
|
|
class TestUtility : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
2020-05-27 16:11:25 +03:00
|
|
|
void initTestCase()
|
|
|
|
{
|
|
|
|
QStandardPaths::setTestModeEnabled(true);
|
|
|
|
}
|
|
|
|
|
2013-08-09 14:56:43 +04:00
|
|
|
void testFormatFingerprint()
|
|
|
|
{
|
|
|
|
QVERIFY2(formatFingerprint("68ac906495480a3404beee4874ed853a037a7a8f")
|
2014-01-22 02:49:16 +04:00
|
|
|
== "68:ac:90:64:95:48:0a:34:04:be:ee:48:74:ed:85:3a:03:7a:7a:8f",
|
2013-08-09 14:56:43 +04:00
|
|
|
"Utility::formatFingerprint() is broken");
|
|
|
|
}
|
|
|
|
void testOctetsToString()
|
|
|
|
{
|
2015-01-15 15:54:39 +03:00
|
|
|
QLocale::setDefault(QLocale("en"));
|
2013-08-29 14:06:41 +04:00
|
|
|
QCOMPARE(octetsToString(999) , QString("999 B"));
|
2015-09-10 18:31:44 +03:00
|
|
|
QCOMPARE(octetsToString(1024) , QString("1 KB"));
|
2023-12-11 20:29:33 +03:00
|
|
|
QCOMPARE(octetsToString(1110) , QString("1 KB"));
|
2016-01-21 11:50:32 +03:00
|
|
|
QCOMPARE(octetsToString(1364) , QString("1 KB"));
|
2013-08-29 14:06:41 +04:00
|
|
|
|
2016-01-21 11:50:32 +03:00
|
|
|
QCOMPARE(octetsToString(9110) , QString("9 KB"));
|
|
|
|
QCOMPARE(octetsToString(9910) , QString("10 KB"));
|
2023-12-11 20:29:33 +03:00
|
|
|
QCOMPARE(octetsToString(9999) , QString("10 KB"));
|
2015-09-10 18:31:44 +03:00
|
|
|
QCOMPARE(octetsToString(10240) , QString("10 KB"));
|
2013-08-29 14:06:41 +04:00
|
|
|
|
2015-09-10 18:31:44 +03:00
|
|
|
QCOMPARE(octetsToString(123456) , QString("121 KB"));
|
2017-04-04 10:54:06 +03:00
|
|
|
QCOMPARE(octetsToString(1234567) , QString("1.2 MB"));
|
2015-08-05 17:22:09 +03:00
|
|
|
QCOMPARE(octetsToString(12345678) , QString("12 MB"));
|
2015-09-10 18:31:44 +03:00
|
|
|
QCOMPARE(octetsToString(123456789) , QString("118 MB"));
|
|
|
|
QCOMPARE(octetsToString(1000LL*1000*1000 * 5) , QString("4.7 GB"));
|
2013-08-29 14:06:41 +04:00
|
|
|
|
2013-08-29 16:48:40 +04:00
|
|
|
QCOMPARE(octetsToString(1), QString("1 B"));
|
|
|
|
QCOMPARE(octetsToString(2), QString("2 B"));
|
2015-09-10 18:31:44 +03:00
|
|
|
QCOMPARE(octetsToString(1024), QString("1 KB"));
|
|
|
|
QCOMPARE(octetsToString(1024*1024), QString("1 MB"));
|
|
|
|
QCOMPARE(octetsToString(1024LL*1024*1024), QString("1 GB"));
|
2023-12-11 20:29:33 +03:00
|
|
|
QCOMPARE(octetsToString(1024LL*1024*1024*1024), QString("1 TB"));
|
|
|
|
QCOMPARE(octetsToString(1024LL*1024*1024*1024 * 5), QString("5 TB"));
|
2013-08-09 14:56:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testLaunchOnStartup()
|
|
|
|
{
|
2021-09-03 22:37:46 +03:00
|
|
|
QString postfix = QString::number(OCC::Utility::rand());
|
2013-08-09 14:56:43 +04:00
|
|
|
|
2013-08-29 16:50:45 +04:00
|
|
|
const QString appName = QString::fromLatin1("testLaunchOnStartup.%1").arg(postfix);
|
2013-08-29 16:50:18 +04:00
|
|
|
const QString guiName = "LaunchOnStartup GUI Name";
|
|
|
|
|
|
|
|
QVERIFY(hasLaunchOnStartup(appName) == false);
|
|
|
|
setLaunchOnStartup(appName, guiName, true);
|
|
|
|
QVERIFY(hasLaunchOnStartup(appName) == true);
|
|
|
|
setLaunchOnStartup(appName, guiName, false);
|
|
|
|
QVERIFY(hasLaunchOnStartup(appName) == false);
|
2013-08-09 14:56:43 +04:00
|
|
|
}
|
|
|
|
|
2015-06-29 15:53:37 +03:00
|
|
|
void testDurationToDescriptiveString()
|
|
|
|
{
|
|
|
|
QLocale::setDefault(QLocale("C"));
|
|
|
|
//NOTE: in order for the plural to work we would need to load the english translation
|
|
|
|
|
|
|
|
quint64 sec = 1000;
|
|
|
|
quint64 hour = 3600 * sec;
|
|
|
|
|
2016-11-08 13:13:19 +03:00
|
|
|
QDateTime current = QDateTime::currentDateTimeUtc();
|
2015-06-29 15:53:37 +03:00
|
|
|
|
2016-01-21 11:53:58 +03:00
|
|
|
QCOMPARE(durationToDescriptiveString2(0), QString("0 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(5), QString("0 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(1000), QString("1 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(1005), QString("1 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(56123), QString("56 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(90*sec), QString("1 minute(s) 30 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(3*hour), QString("3 hour(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(3*hour + 20*sec), QString("3 hour(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(3*hour + 70*sec), QString("3 hour(s) 1 minute(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(3*hour + 100*sec), QString("3 hour(s) 2 minute(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
|
2015-06-29 15:53:37 +03:00
|
|
|
QString("4 year(s) 5 month(s)") );
|
2016-01-21 11:53:58 +03:00
|
|
|
QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
|
2015-06-29 15:53:37 +03:00
|
|
|
QString("2 day(s) 23 hour(s)") );
|
|
|
|
|
2016-01-21 11:53:58 +03:00
|
|
|
QCOMPARE(durationToDescriptiveString1(0), QString("0 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(5), QString("0 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(1000), QString("1 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(1005), QString("1 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(56123), QString("56 second(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(90*sec), QString("2 minute(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(3*hour), QString("3 hour(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(3*hour + 20*sec), QString("3 hour(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(3*hour + 70*sec), QString("3 hour(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(3*hour + 100*sec), QString("3 hour(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
|
|
|
|
QString("4 year(s)") );
|
|
|
|
QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
|
|
|
|
QString("3 day(s)") );
|
2015-06-29 15:53:37 +03:00
|
|
|
|
|
|
|
}
|
2015-07-16 19:16:16 +03:00
|
|
|
|
|
|
|
void testVersionOfInstalledBinary()
|
|
|
|
{
|
2020-12-14 16:47:45 +03:00
|
|
|
if (isLinux()) {
|
2020-12-14 17:37:05 +03:00
|
|
|
// pass the cmd client from our build dir
|
|
|
|
// this is a bit inaccurate as it does not test the "real thing"
|
|
|
|
// but cmd and gui have the same --version handler by now
|
|
|
|
// and cmd works without X in CI
|
|
|
|
QString ver = versionOfInstalledBinary(QStringLiteral(OWNCLOUD_BIN_PATH "/" APPLICATION_EXECUTABLE "cmd"));
|
2020-12-14 16:47:45 +03:00
|
|
|
qDebug() << "Version of installed Nextcloud: " << ver;
|
|
|
|
QVERIFY(!ver.isEmpty());
|
|
|
|
|
2021-10-29 17:56:29 +03:00
|
|
|
const QRegularExpression rx(QRegularExpression::anchoredPattern(APPLICATION_SHORTNAME R"( version \d+\.\d+\.\d+.*)"));
|
|
|
|
QVERIFY(rx.match(ver).hasMatch());
|
2020-12-14 16:47:45 +03:00
|
|
|
} else {
|
|
|
|
QVERIFY(versionOfInstalledBinary().isEmpty());
|
|
|
|
}
|
2015-07-16 19:16:16 +03:00
|
|
|
}
|
2016-02-15 19:59:01 +03:00
|
|
|
|
|
|
|
void testTimeAgo()
|
|
|
|
{
|
2016-05-12 10:25:15 +03:00
|
|
|
// Both times in same timezone
|
2016-02-15 19:59:01 +03:00
|
|
|
QDateTime d1 = QDateTime::fromString("2015-01-24T09:20:30+01:00", Qt::ISODate);
|
|
|
|
QDateTime d2 = QDateTime::fromString("2015-01-23T09:20:30+01:00", Qt::ISODate);
|
|
|
|
QString s = timeAgoInWords(d2, d1);
|
2023-05-17 16:31:47 +03:00
|
|
|
QCOMPARE(s, QLatin1String("1d"));
|
2016-02-15 19:59:01 +03:00
|
|
|
|
2016-05-12 10:25:15 +03:00
|
|
|
// Different timezones
|
2016-02-15 19:59:01 +03:00
|
|
|
QDateTime earlyTS = QDateTime::fromString("2015-01-24T09:20:30+01:00", Qt::ISODate);
|
2016-05-12 10:25:15 +03:00
|
|
|
QDateTime laterTS = QDateTime::fromString("2015-01-24T09:20:30-01:00", Qt::ISODate);
|
2016-02-15 19:59:01 +03:00
|
|
|
s = timeAgoInWords(earlyTS, laterTS);
|
2023-05-17 16:31:47 +03:00
|
|
|
QCOMPARE(s, QLatin1String("2h"));
|
2016-02-15 19:59:01 +03:00
|
|
|
|
2016-05-12 10:25:15 +03:00
|
|
|
// 'Now' in whatever timezone
|
2016-02-15 19:59:01 +03:00
|
|
|
earlyTS = QDateTime::currentDateTime();
|
|
|
|
laterTS = earlyTS;
|
|
|
|
s = timeAgoInWords(earlyTS, laterTS );
|
|
|
|
QCOMPARE(s, QLatin1String("now"));
|
|
|
|
|
|
|
|
earlyTS = earlyTS.addSecs(-6);
|
|
|
|
s = timeAgoInWords(earlyTS, laterTS );
|
2023-05-17 16:31:47 +03:00
|
|
|
QCOMPARE(s, QLatin1String("1m"));
|
2016-02-15 19:59:01 +03:00
|
|
|
}
|
2016-10-12 15:48:00 +03:00
|
|
|
|
|
|
|
void testFsCasePreserving()
|
|
|
|
{
|
2018-02-20 16:29:45 +03:00
|
|
|
QVERIFY(isMac() || isWindows() ? fsCasePreserving() : ! fsCasePreserving());
|
|
|
|
QScopedValueRollback<bool> scope(OCC::fsCasePreserving_override);
|
2020-05-20 09:52:26 +03:00
|
|
|
OCC::fsCasePreserving_override = true;
|
2016-10-12 15:48:00 +03:00
|
|
|
QVERIFY(fsCasePreserving());
|
2020-05-20 09:52:26 +03:00
|
|
|
OCC::fsCasePreserving_override = false;
|
2016-10-12 15:48:00 +03:00
|
|
|
QVERIFY(! fsCasePreserving());
|
|
|
|
}
|
|
|
|
|
|
|
|
void testFileNamesEqual()
|
|
|
|
{
|
|
|
|
QTemporaryDir dir;
|
|
|
|
QVERIFY(dir.isValid());
|
|
|
|
QDir dir2(dir.path());
|
|
|
|
QVERIFY(dir2.mkpath("test"));
|
|
|
|
if( !fsCasePreserving() ) {
|
|
|
|
QVERIFY(dir2.mkpath("TEST"));
|
|
|
|
}
|
|
|
|
QVERIFY(dir2.mkpath("test/TESTI"));
|
|
|
|
QVERIFY(dir2.mkpath("TESTI"));
|
|
|
|
|
|
|
|
QString a = dir.path();
|
|
|
|
QString b = dir.path();
|
|
|
|
|
|
|
|
QVERIFY(fileNamesEqual(a, b));
|
|
|
|
|
|
|
|
QVERIFY(fileNamesEqual(a+"/test", b+"/test")); // both exist
|
|
|
|
QVERIFY(fileNamesEqual(a+"/test/TESTI", b+"/test/../test/TESTI")); // both exist
|
|
|
|
|
2018-02-20 16:29:45 +03:00
|
|
|
QScopedValueRollback<bool> scope(OCC::fsCasePreserving_override, true);
|
2016-10-12 15:48:00 +03:00
|
|
|
QVERIFY(fileNamesEqual(a+"/test", b+"/TEST")); // both exist
|
|
|
|
|
|
|
|
QVERIFY(!fileNamesEqual(a+"/test", b+"/test/TESTI")); // both are different
|
|
|
|
|
|
|
|
dir.remove();
|
|
|
|
}
|
|
|
|
|
2018-01-23 13:12:45 +03:00
|
|
|
void testSanitizeForFileName_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("input");
|
|
|
|
QTest::addColumn<QString>("output");
|
|
|
|
|
|
|
|
QTest::newRow("")
|
|
|
|
<< "foobar"
|
|
|
|
<< "foobar";
|
|
|
|
QTest::newRow("")
|
|
|
|
<< "a/b?c<d>e\\f:g*h|i\"j"
|
|
|
|
<< "abcdefghij";
|
|
|
|
QTest::newRow("")
|
|
|
|
<< QString::fromLatin1("a\x01 b\x1f c\x80 d\x9f")
|
|
|
|
<< "a b c d";
|
|
|
|
}
|
2016-10-12 15:48:00 +03:00
|
|
|
|
2018-01-23 13:12:45 +03:00
|
|
|
void testSanitizeForFileName()
|
|
|
|
{
|
|
|
|
QFETCH(QString, input);
|
|
|
|
QFETCH(QString, output);
|
|
|
|
QCOMPARE(sanitizeForFileName(input), output);
|
|
|
|
}
|
2018-11-30 17:08:30 +03:00
|
|
|
|
|
|
|
void testNormalizeEtag()
|
|
|
|
{
|
|
|
|
QByteArray str;
|
|
|
|
|
|
|
|
#define CHECK_NORMALIZE_ETAG(TEST, EXPECT) \
|
|
|
|
str = OCC::Utility::normalizeEtag(TEST); \
|
|
|
|
QCOMPARE(str.constData(), EXPECT); \
|
|
|
|
|
|
|
|
CHECK_NORMALIZE_ETAG("foo", "foo");
|
|
|
|
CHECK_NORMALIZE_ETAG("\"foo\"", "foo");
|
|
|
|
CHECK_NORMALIZE_ETAG("\"nar123\"", "nar123");
|
|
|
|
CHECK_NORMALIZE_ETAG("", "");
|
|
|
|
CHECK_NORMALIZE_ETAG("\"\"", "");
|
|
|
|
|
|
|
|
/* Test with -gzip (all combinaison) */
|
|
|
|
CHECK_NORMALIZE_ETAG("foo-gzip", "foo");
|
|
|
|
CHECK_NORMALIZE_ETAG("\"foo\"-gzip", "foo");
|
|
|
|
CHECK_NORMALIZE_ETAG("\"foo-gzip\"", "foo");
|
|
|
|
}
|
2021-06-02 10:30:27 +03:00
|
|
|
|
|
|
|
void testIsPathWindowsDrivePartitionRoot()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// a non-root of a Windows partition
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c:/a"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c:\\a"));
|
|
|
|
|
|
|
|
// a root of a Windows partition (c, d, e)
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("c:"));
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("c:/"));
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("c:\\"));
|
|
|
|
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("d:"));
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("d:/"));
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("d:\\"));
|
|
|
|
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("e:"));
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("e:/"));
|
|
|
|
QVERIFY(isPathWindowsDrivePartitionRoot("e:\\"));
|
|
|
|
|
|
|
|
// a single character
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("a"));
|
|
|
|
|
2023-06-02 14:55:08 +03:00
|
|
|
// a missing second character
|
2021-06-02 10:30:27 +03:00
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c/"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c\\"));
|
|
|
|
|
|
|
|
// an incorrect second character
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c;"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c;/"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c;\\"));
|
|
|
|
|
|
|
|
// a non-missing, but, incorrect last character
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c:!"));
|
|
|
|
|
|
|
|
// an incorrect path length
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("cd:"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("cd:/"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("cd:\\"));
|
|
|
|
|
|
|
|
// a non-alphabetic first character
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("0:"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("0:/"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("0:\\"));
|
|
|
|
#else
|
|
|
|
// should always return false on non-Windows
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c:"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c:/"));
|
|
|
|
QVERIFY(!isPathWindowsDrivePartitionRoot("c:\\"));
|
|
|
|
#endif
|
|
|
|
}
|
2013-08-09 14:56:43 +04:00
|
|
|
};
|
|
|
|
|
2017-01-27 20:12:18 +03:00
|
|
|
QTEST_GUILESS_MAIN(TestUtility)
|
2016-03-30 18:58:15 +03:00
|
|
|
#include "testutility.moc"
|