diff options
Diffstat (limited to 'tests/auto/corelib/io')
-rw-r--r-- | tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp | 48 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp | 62 | ||||
-rw-r--r-- | tests/auto/corelib/io/qfilesystemwatcher/BLACKLIST | 3 | ||||
-rw-r--r-- | tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp | 9 | ||||
-rw-r--r-- | tests/auto/corelib/io/qprocess/tst_qprocess.cpp | 104 | ||||
-rw-r--r-- | tests/auto/corelib/io/qsettings/tst_qsettings.cpp | 4 | ||||
-rw-r--r-- | tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp | 8 | ||||
-rw-r--r-- | tests/auto/corelib/io/qurl/tst_qurl.cpp | 10 |
8 files changed, 202 insertions, 46 deletions
diff --git a/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp b/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp index 4968742110..b02400e349 100644 --- a/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp +++ b/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp @@ -36,12 +36,14 @@ class tst_QBuffer : public QObject Q_OBJECT private slots: void open(); + void openWriteOnlyDoesNotTruncate(); void getSetCheck(); void readBlock(); void readBlockPastEnd(); void writeBlock_data(); void writeBlock(); void seek(); + void invalidSeeks(); void seekTest_data(); void seekTest(); void read_rawdata(); @@ -130,6 +132,29 @@ void tst_QBuffer::open() b.close(); } +void tst_QBuffer::openWriteOnlyDoesNotTruncate() +{ + QBuffer b; + const auto data = QByteArrayLiteral("Hey, presto!"); + + { + QVERIFY(b.open(QIODevice::WriteOnly)); + b.write(data); + b.close(); + } + { + QVERIFY(b.open(QIODevice::ReadOnly)); + QCOMPARE(b.readAll(), data); + b.close(); + } + { + QVERIFY(b.open(QIODevice::WriteOnly)); + QCOMPARE(b.size(), data.size()); + QCOMPARE(b.pos(), 0); + b.close(); + } +} + // some status() tests, too void tst_QBuffer::readBlock() { @@ -286,6 +311,29 @@ void tst_QBuffer::seek() QCOMPARE(buffer.size(), pos); } +void tst_QBuffer::invalidSeeks() +{ + if (sizeof(qsizetype) == sizeof(qint64)) { + // sizeof(qsizetype) == sizeof(qint64), so +1 would overflow + QSKIP("This is a 32-bit-only test."); + } else { + QBuffer buffer; + buffer.open(QIODevice::WriteOnly); + QCOMPARE(buffer.buffer().size(), qsizetype(0)); + QCOMPARE(buffer.pos(), qint64(0)); + constexpr qint64 MaxQByteArrayCapacity = (std::numeric_limits<qsizetype>::max)(); + // this should fail fast, not after trying to allocate nearly 2 GiB of data, + // potentially crashing in the process: + QVERIFY(!buffer.seek(2 * MaxQByteArrayCapacity - 1)); + QCOMPARE(buffer.buffer().size(), qsizetype(0)); + QCOMPARE(buffer.pos(), qint64(0)); + // ditto: + QVERIFY(!buffer.seek(MaxQByteArrayCapacity + 1)); + QCOMPARE(buffer.buffer().size(), qsizetype(0)); + QCOMPARE(buffer.pos(), qint64(0)); + } +} + void tst_QBuffer::seekTest_data() { writeBlock_data(); diff --git a/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp b/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp index 66720d28e0..011af527b4 100644 --- a/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp +++ b/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp @@ -35,43 +35,45 @@ class tst_QDataUrl : public QObject Q_OBJECT private slots: - void nonData(); - void emptyData(); - void alreadyPercentageEncoded(); + void decode_data(); + void decode(); }; -void tst_QDataUrl::nonData() +void tst_QDataUrl::decode_data() { - QLatin1String data("http://test.com"); - QUrl url(data); - QString mimeType; - QByteArray payload; - bool result = qDecodeDataUrl(url, mimeType, payload); - QVERIFY(!result); -} + QTest::addColumn<QString>("input"); + QTest::addColumn<bool>("result"); + QTest::addColumn<QString>("mimeType"); + QTest::addColumn<QByteArray>("payload"); -void tst_QDataUrl::emptyData() -{ - QLatin1String data("data:text/plain"); - QUrl url(data); - QString mimeType; - QByteArray payload; - bool result = qDecodeDataUrl(url, mimeType, payload); - QVERIFY(result); - QCOMPARE(mimeType, QLatin1String("text/plain;charset=US-ASCII")); - QVERIFY(payload.isNull()); + auto row = [](const char *tag, const char *url, bool success, QString mimeType = {}, QByteArray payload = {}) { + QTest::newRow(tag) << url << success <<mimeType << payload; + }; + + row("nonData", "http://test.com", false); + row("emptyData", "data:text/plain", true, + QLatin1String("text/plain;charset=US-ASCII")); + row("alreadyPercentageEncoded", "data:text/plain,%E2%88%9A", true, + QLatin1String("text/plain"), QByteArray::fromPercentEncoding("%E2%88%9A")); } -void tst_QDataUrl::alreadyPercentageEncoded() +void tst_QDataUrl::decode() { - QLatin1String data("data:text/plain,%E2%88%9A"); - QUrl url(data); - QString mimeType; - QByteArray payload; - bool result = qDecodeDataUrl(url, mimeType, payload); - QVERIFY(result); - QCOMPARE(mimeType, QLatin1String("text/plain")); - QCOMPARE(payload, QByteArray::fromPercentEncoding("%E2%88%9A")); + QFETCH(const QString, input); + QFETCH(const bool, result); + QFETCH(const QString, mimeType); + QFETCH(const QByteArray, payload); + + QString actualMimeType; + QByteArray actualPayload; + + QUrl url(input); + const bool actualResult = qDecodeDataUrl(url, actualMimeType, actualPayload); + + QCOMPARE(actualResult, result); + QCOMPARE(actualMimeType, mimeType); + QCOMPARE(actualPayload, payload); + QCOMPARE(actualPayload.isNull(), payload.isNull()); // assume nullness is significant } QTEST_MAIN(tst_QDataUrl) diff --git a/tests/auto/corelib/io/qfilesystemwatcher/BLACKLIST b/tests/auto/corelib/io/qfilesystemwatcher/BLACKLIST index 457499591d..90b714758a 100644 --- a/tests/auto/corelib/io/qfilesystemwatcher/BLACKLIST +++ b/tests/auto/corelib/io/qfilesystemwatcher/BLACKLIST @@ -1,7 +1,6 @@ # QTBUG-33574 QTBUG-30943 [signalsEmittedAfterFileMoved] -windows-7sp1 -windows-10 +windows [watchFileAndItsDirectory:native backend-testfile] osx windows diff --git a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp index da5327594c..4a04e0f7c6 100644 --- a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp +++ b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp @@ -561,7 +561,8 @@ protected: qint64 readData(char *data, qint64 maxSize) override { maxSize = qMin(maxSize, qint64(buf->size() - offset)); - memcpy(data, buf->constData() + offset, maxSize); + if (maxSize > 0) + memcpy(data, buf->constData() + offset, maxSize); offset += maxSize; return maxSize; } @@ -604,13 +605,15 @@ protected: qint64 readData(char *data, qint64 maxSize) override { maxSize = qMin(maxSize, qint64(buf.size() - pos())); - memcpy(data, buf.constData() + pos(), maxSize); + if (maxSize > 0) + memcpy(data, buf.constData() + pos(), maxSize); return maxSize; } qint64 writeData(const char *data, qint64 maxSize) override { maxSize = qMin(maxSize, qint64(buf.size() - pos())); - memcpy(buf.data() + pos(), data, maxSize); + if (maxSize > 0) + memcpy(buf.data() + pos(), data, maxSize); return maxSize; } diff --git a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp index bc9df3f1f3..ac3b998218 100644 --- a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp +++ b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. -** Copyright (C) 2020 Intel Corporation. +** Copyright (C) 2021 The Qt Company Ltd. +** Copyright (C) 2022 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -39,6 +39,7 @@ #include <QtCore/QRegExp> #include <QtCore/QDebug> #include <QtCore/QMetaType> +#include <QtCore/QScopeGuard> #include <QtNetwork/QHostInfo> #include <qplatformdefs.h> @@ -149,6 +150,8 @@ private slots: void startStopStartStopBuffers(); void processEventsInAReadyReadSlot_data(); void processEventsInAReadyReadSlot(); + void startFromCurrentWorkingDir_data(); + void startFromCurrentWorkingDir(); // keep these at the end, since they use lots of processes and sometimes // caused obscure failures to occur in tests that followed them (esp. on the Mac) @@ -1271,17 +1274,15 @@ void tst_QProcess::processesInMultipleThreads() threadCount = qMax(threadCount, QThread::idealThreadCount() + 2); QVector<TestThread *> threads(threadCount); + auto cleanup = qScopeGuard([&threads]() { qDeleteAll(threads); }); for (int j = 0; j < threadCount; ++j) threads[j] = new TestThread; for (int j = 0; j < threadCount; ++j) threads[j]->start(); - for (int j = 0; j < threadCount; ++j) { + for (int j = 0; j < threadCount; ++j) QVERIFY(threads[j]->wait(10000)); - } - for (int j = 0; j < threadCount; ++j) { + for (int j = 0; j < threadCount; ++j) QCOMPARE(threads[j]->code(), 0); - } - qDeleteAll(threads); } } @@ -2732,5 +2733,94 @@ void tst_QProcess::finishProcessBeforeReadingDone_deprecated() #endif +enum class ChdirMode { + None = 0, + InParent, + InChild +}; +Q_DECLARE_METATYPE(ChdirMode) + +void tst_QProcess::startFromCurrentWorkingDir_data() +{ + qRegisterMetaType<ChdirMode>(); + QTest::addColumn<QString>("programPrefix"); + QTest::addColumn<ChdirMode>("chdirMode"); + QTest::addColumn<bool>("success"); + + constexpr bool IsWindows = true +#ifdef Q_OS_UNIX + && false +#endif + ; + + // baseline: trying to execute the directory, this can't possibly succeed! + QTest::newRow("plain-same-cwd") << QString() << ChdirMode::None << false; + + // cross-platform behavior: neither OS searches the setWorkingDirectory() + // dir without "./" + QTest::newRow("plain-child-chdir") << QString() << ChdirMode::InChild << false; + + // cross-platform behavior: both OSes search the parent's CWD with "./" + QTest::newRow("prefixed-parent-chdir") << "./" << ChdirMode::InParent << true; + + // opposite behaviors: Windows searches the parent's CWD and Unix searches + // the child's with "./" + QTest::newRow("prefixed-child-chdir") << "./" << ChdirMode::InChild << !IsWindows; + + // Windows searches the parent's CWD without "./" + QTest::newRow("plain-parent-chdir") << QString() << ChdirMode::InParent << IsWindows; +} + +void tst_QProcess::startFromCurrentWorkingDir() +{ + QFETCH(QString, programPrefix); + QFETCH(ChdirMode, chdirMode); + QFETCH(bool, success); + + QProcess process; + qRegisterMetaType<QProcess::ProcessError>(); + QSignalSpy errorSpy(&process, &QProcess::errorOccurred); + QVERIFY(errorSpy.isValid()); + + // both the dir name and the executable name + const QString target = QStringLiteral("testProcessNormal"); + process.setProgram(programPrefix + target); + +#ifdef Q_OS_UNIX + // Reset PATH, to be sure it doesn't contain . or the empty path. + // We can't do this on Windows because DLLs are searched in PATH + // and Windows always searches "." anyway. + auto restoreEnv = qScopeGuard([old = qgetenv("PATH")] { + qputenv("PATH", old); + }); + qputenv("PATH", "/"); +#endif + + switch (chdirMode) { + case ChdirMode::InParent: { + auto restoreCwd = qScopeGuard([old = QDir::currentPath()] { + QDir::setCurrent(old); + }); + QVERIFY(QDir::setCurrent(target)); + process.start(); + break; + } + case ChdirMode::InChild: + process.setWorkingDirectory(target); + Q_FALLTHROUGH(); + case ChdirMode::None: + process.start(); + break; + } + + QCOMPARE(process.waitForStarted(), success); + QCOMPARE(errorSpy.count(), int(!success)); + if (success) { + QVERIFY(process.waitForFinished()); + } else { + QCOMPARE(process.error(), QProcess::FailedToStart); + } +} + QTEST_MAIN(tst_QProcess) #include "tst_qprocess.moc" diff --git a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp index 84f4a47585..7c69f5f4b5 100644 --- a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp +++ b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp @@ -742,6 +742,10 @@ void tst_QSettings::embeddedZeroByte_data() QTest::newRow("@bytearray\\0") << QVariant(bytes); QTest::newRow("@string\\0") << QVariant(QString::fromLatin1(bytes.data(), bytes.size())); + + bytes = QByteArray("@\xdd\x7d", 3); + QTest::newRow("@-prefixed data") << QVariant(bytes); + QTest::newRow("@-prefixed data as string") << QVariant(QString::fromLatin1(bytes.data(), bytes.size())); } void tst_QSettings::embeddedZeroByte() diff --git a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp index afbd64c405..a0aefac268 100644 --- a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp +++ b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp @@ -532,7 +532,12 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() d.mkdir("runtime"); QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ExeGroup | QFile::ExeOther); - return updateRuntimeDir(p); + updateRuntimeDir(p); + QTest::ignoreMessage(QtWarningMsg, + QString("QStandardPaths: wrong permissions on runtime directory %1, " + "0711 instead of 0700") + .arg(p).toLatin1()); + return fallbackXdgRuntimeDir(); }); addRow("environment:wrong-owner", [](QDir &) { @@ -597,6 +602,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() clearRuntimeDir(); QString p = fallbackXdgRuntimeDir(); d.mkdir(p); // probably has wrong permissions + QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); return p; }); diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index d30ec1d57d..c527f1ce3a 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -1334,6 +1334,10 @@ void tst_QUrl::fromLocalFile_data() << QString(QString("//somehost") + suffix).replace('/', '\\') << QString("file://somehost") + suffix << QString(suffix); + QTest::addRow("windows-backslash-extlen-%s", pathDescription) + << QString(QString("//?") + suffix).replace('/', '\\') + << QString("file:////%3F") + suffix + << QString("//?") + suffix; #endif QTest::addRow("windows-extlen-%s", pathDescription) << QString("//?") + suffix @@ -1914,6 +1918,8 @@ void tst_QUrl::ipv6_data() QTest::addColumn<bool>("isValid"); QTest::addColumn<QString>("output"); + QTest::newRow("empty") << "//[]" << false << ""; + QTest::newRow("case 1") << QString::fromLatin1("//[56:56:56:56:56:56:56:56]") << true << "//[56:56:56:56:56:56:56:56]"; QTest::newRow("case 2") << QString::fromLatin1("//[::56:56:56:56:56:56:56]") << true @@ -3464,12 +3470,10 @@ void tst_QUrl::effectiveTLDs_data() QTest::newRow("yes0") << QUrl::fromEncoded("http://test.co.uk") << ".co.uk"; QTest::newRow("yes1") << QUrl::fromEncoded("http://test.com") << ".com"; QTest::newRow("yes2") << QUrl::fromEncoded("http://www.test.de") << ".de"; - QTest::newRow("yes3") << QUrl::fromEncoded("http://test.ulm.museum") << ".ulm.museum"; QTest::newRow("yes4") << QUrl::fromEncoded("http://www.com.krodsherad.no") << ".krodsherad.no"; QTest::newRow("yes5") << QUrl::fromEncoded("http://www.co.uk.1.bg") << ".1.bg"; QTest::newRow("yes6") << QUrl::fromEncoded("http://www.com.com.cn") << ".com.cn"; QTest::newRow("yes7") << QUrl::fromEncoded("http://www.test.org.ws") << ".org.ws"; - QTest::newRow("yes9") << QUrl::fromEncoded("http://www.com.co.uk.wallonie.museum") << ".wallonie.museum"; QTest::newRow("yes10") << QUrl::fromEncoded("http://www.com.evje-og-hornnes.no") << ".evje-og-hornnes.no"; QTest::newRow("yes11") << QUrl::fromEncoded("http://www.bla.kamijima.ehime.jp") << ".kamijima.ehime.jp"; QTest::newRow("yes12") << QUrl::fromEncoded("http://www.bla.kakuda.miyagi.jp") << ".kakuda.miyagi.jp"; @@ -3491,7 +3495,7 @@ void tst_QUrl::effectiveTLDs_data() << ".app.os.stg.fedoraproject.org"; QTest::newRow("development.run") << QUrl::fromEncoded("http://test.development.run") << ".development.run"; QTest::newRow("crafting.xyz") << QUrl::fromEncoded("http://test.crafting.xyz") << ".crafting.xyz"; - QTest::newRow("nym.ie") << QUrl::fromEncoded("http://shamus.nym.ie") << ".nym.ie"; + QTest::newRow("nym.ie") << QUrl::fromEncoded("http://shamus.nym.ie") << ".ie"; QTest::newRow("vapor.cloud") << QUrl::fromEncoded("http://test.vapor.cloud") << ".vapor.cloud"; QTest::newRow("official.academy") << QUrl::fromEncoded("http://acredited.official.academy") << ".official.academy"; } |