From cd45d0f712f844d05b88801bc000550db0856043 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 25 Oct 2017 13:36:33 +0200 Subject: tst_QFile: Introduce StdioFileGuard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guard the FILE * obtained by fopen() by a RAI class ensuring the file is closed on destruction. Change-Id: I9297f91ca2120238f3a44bad92bca5f920e01aa8 Reviewed-by: Jędrzej Nowacki --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 64 +++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'tests/auto/corelib/io/qfile') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 32165ad2b8..450229e01e 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -109,6 +109,30 @@ QT_END_NAMESPACE Q_DECLARE_METATYPE(QFile::FileError) + +class StdioFileGuard +{ + Q_DISABLE_COPY(StdioFileGuard) +public: + explicit StdioFileGuard(FILE *f = nullptr) : m_file(f) {} + ~StdioFileGuard() { close(); } + + operator FILE *() const { return m_file; } + + void close(); + +private: + FILE * m_file; +}; + +void StdioFileGuard::close() +{ + if (m_file != nullptr) { + fclose(m_file); + m_file = nullptr; + } +} + class tst_QFile : public QObject { Q_OBJECT @@ -660,14 +684,13 @@ void tst_QFile::size() } { - QFile f; - FILE* stream = QT_FOPEN(filename.toLocal8Bit().constData(), "rb"); + StdioFileGuard stream(QT_FOPEN(filename.toLocal8Bit().constData(), "rb")); QVERIFY( stream ); + QFile f; QVERIFY( f.open(stream, QIODevice::ReadOnly) ); QCOMPARE( f.size(), size ); f.close(); - fclose(stream); } { @@ -1598,6 +1621,7 @@ void tst_QFile::largeUncFileSupport() qint64 dataOffset = Q_INT64_C(8589914592); QByteArray knownData("LargeFile content at offset 8589914592"); QString largeFile("//" + QtNetworkSettings::winServerName() + "/testsharelargefile/file.bin"); + const QByteArray largeFileEncoded = QFile::encodeName(largeFile); { // 1) Native file handling. @@ -1612,24 +1636,24 @@ void tst_QFile::largeUncFileSupport() } { // 2) stdlib file handling. + StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb")); + QVERIFY(fh); QFile file; - FILE *fh = fopen(QFile::encodeName(largeFile).data(), "rb"); QVERIFY(file.open(fh, QIODevice::ReadOnly)); QCOMPARE(file.size(), size); QVERIFY(file.seek(dataOffset)); QCOMPARE(file.read(knownData.size()), knownData); - fclose(fh); } { // 3) stdio file handling. - QFile file; - FILE *fh = fopen(QFile::encodeName(largeFile).data(), "rb"); + StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb")); + QVERIFY(fh); int fd = int(_fileno(fh)); + QFile file; QVERIFY(file.open(fd, QIODevice::ReadOnly)); QCOMPARE(file.size(), size); QVERIFY(file.seek(dataOffset)); QCOMPARE(file.read(knownData.size()), knownData); - fclose(fh); } } #endif @@ -1670,7 +1694,7 @@ void tst_QFile::bufferedRead() file.write("abcdef"); file.close(); - FILE *stdFile = fopen("stdfile.txt", "r"); + StdioFileGuard stdFile(fopen("stdfile.txt", "r")); QVERIFY(stdFile); char c; QCOMPARE(int(fread(&c, 1, 1, stdFile)), 1); @@ -1685,8 +1709,6 @@ void tst_QFile::bufferedRead() QCOMPARE(c, 'b'); QCOMPARE(file.pos(), qlonglong(2)); } - - fclose(stdFile); } #ifdef Q_OS_UNIX @@ -1815,7 +1837,7 @@ void tst_QFile::FILEReadWrite() f.close(); } - FILE *fp = fopen("FILEReadWrite.txt", "r+b"); + StdioFileGuard fp(fopen("FILEReadWrite.txt", "r+b")); QVERIFY(fp); QFile file; QVERIFY2(file.open(fp, QFile::ReadWrite), msgOpenFailed(file).constData()); @@ -1850,7 +1872,7 @@ void tst_QFile::FILEReadWrite() } file.close(); - fclose(fp); + fp.close(); // check modified file { @@ -2435,11 +2457,10 @@ void tst_QFile::virtualFile() void tst_QFile::textFile() { -#if defined(Q_OS_WIN) - FILE *fs = ::fopen("writeabletextfile", "wt"); -#else - FILE *fs = ::fopen("writeabletextfile", "w"); -#endif + const char *openMode = QOperatingSystemVersion::current().type() != QOperatingSystemVersion::Windows + ? "w" : "wt"; + StdioFileGuard fs(fopen("writeabletextfile", openMode)); + QVERIFY(fs); QFile f; QByteArray part1("This\nis\na\nfile\nwith\nnewlines\n"); QByteArray part2("Add\nsome\nmore\nnewlines\n"); @@ -2448,7 +2469,7 @@ void tst_QFile::textFile() f.write(part1); f.write(part2); f.close(); - ::fclose(fs); + fs.close(); QFile file("writeabletextfile"); QVERIFY2(file.open(QIODevice::ReadOnly), msgOpenFailed(file).constData()); @@ -2704,11 +2725,12 @@ void tst_QFile::handle() //test round trip of adopted stdio file handle QFile file2; - FILE *fp = fopen(qPrintable(m_testSourceFile), "r"); + StdioFileGuard fp(fopen(qPrintable(m_testSourceFile), "r")); + QVERIFY(fp); file2.open(fp, QIODevice::ReadOnly); QCOMPARE(int(file2.handle()), int(fileno(fp))); QCOMPARE(int(file2.handle()), int(fileno(fp))); - fclose(fp); + fp.close(); //test round trip of adopted posix file handle #ifdef Q_OS_UNIX -- cgit v1.2.3 From 1c3dc8cfb8e72770d56f2fbe131adbfe542a51c7 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 25 Oct 2017 13:57:26 +0200 Subject: tst_QFile::largeUncFileSupport(): Use QTRY_VERIFY() to open the file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Open failures due to sharing violations have been observed in Coin. Change-Id: If7fbe01a454b3c343c0b87f73db50c28eae901c3 Reviewed-by: Edward Welbourne Reviewed-by: Jędrzej Nowacki --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 36 ++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'tests/auto/corelib/io/qfile') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 450229e01e..5f3ebeadd7 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -1615,6 +1615,27 @@ void tst_QFile::writeTextFile() } #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +// Helper for executing QFile::open() with warning in QTRY_VERIFY(), which evaluates the condition +// multiple times +static bool qFileOpen(QFile &file, QIODevice::OpenMode ioFlags) +{ + const bool result = file.isOpen() || file.open(ioFlags); + if (!result) + qWarning() << "Cannot open" << file.fileName() << ':' << file.errorString(); + return result; +} + +// Helper for executing fopen() with warning in QTRY_VERIFY(), which evaluates the condition +// multiple times +static bool fOpen(const QByteArray &fileName, const char *mode, FILE **file) +{ + if (*file == nullptr) + *file = fopen(fileName.constData(), mode); + if (*file == nullptr) + qWarning("Cannot open %s: %s", fileName.constData(), strerror(errno)); + return *file != nullptr; +} + void tst_QFile::largeUncFileSupport() { qint64 size = Q_INT64_C(8589934592); @@ -1629,15 +1650,18 @@ void tst_QFile::largeUncFileSupport() QVERIFY2(file.exists(), msgFileDoesNotExist(largeFile)); QCOMPARE(file.size(), size); - QVERIFY2(file.open(QIODevice::ReadOnly), msgOpenFailed(file).constData()); + // Retry in case of sharing violation + QTRY_VERIFY2(qFileOpen(file, QIODevice::ReadOnly), msgOpenFailed(file).constData()); QCOMPARE(file.size(), size); QVERIFY(file.seek(dataOffset)); QCOMPARE(file.read(knownData.size()), knownData); } { // 2) stdlib file handling. - StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb")); - QVERIFY(fh); + FILE *fhF = nullptr; + // Retry in case of sharing violation + QTRY_VERIFY(fOpen(largeFileEncoded, "rb", &fhF)); + StdioFileGuard fh(fhF); QFile file; QVERIFY(file.open(fh, QIODevice::ReadOnly)); QCOMPARE(file.size(), size); @@ -1646,8 +1670,10 @@ void tst_QFile::largeUncFileSupport() } { // 3) stdio file handling. - StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb")); - QVERIFY(fh); + FILE *fhF = nullptr; + // Retry in case of sharing violation + QTRY_VERIFY(fOpen(largeFileEncoded, "rb", &fhF)); + StdioFileGuard fh(fhF); int fd = int(_fileno(fh)); QFile file; QVERIFY(file.open(fd, QIODevice::ReadOnly)); -- cgit v1.2.3 From e27bd981373cc3d22e54d8cbe030e5c329f9beda Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Wed, 8 Nov 2017 12:10:29 +0100 Subject: qfile tests: Make sure files are writable before deleting them On some platforms (like UWP) files that are copied during qfile auto tests are not writable by default. The cleanup will fail for these files if the permissions are not set accordingly. Change-Id: Id925dcadfc6b505c87f1f55d5ea05e286b60a5a5 Reviewed-by: Maurice Kalinowski Reviewed-by: Friedemann Kleint --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/auto/corelib/io/qfile') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 5f3ebeadd7..8db89b9f05 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -405,6 +405,8 @@ void tst_QFile::cleanup() QDir remainingDir(absoluteFilePath); QVERIFY2(remainingDir.removeRecursively(), qPrintable(absoluteFilePath)); } else { + if (!(QFile::permissions(absoluteFilePath) & QFile::WriteUser)) + QVERIFY2(QFile::setPermissions(absoluteFilePath, QFile::WriteUser), qPrintable(absoluteFilePath)); QVERIFY2(QFile::remove(absoluteFilePath), qPrintable(absoluteFilePath)); } } -- cgit v1.2.3 From bbc68dc815122e6be9ea44adadd93ffb715c7792 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Wed, 8 Nov 2017 12:12:06 +0100 Subject: Fix tst_QFile::useQFileInAFileHandler for systems using resources for test data Resource files are extracted to m_dataDir in tst_QFile::initTestCase. Instead of trying to access the file from the resource on systems that use qrc for bundling the test data, we have to use the files that were extracted at the beginning of the test. Change-Id: I35453fbdeb27e317d1342ff1cb7bbea9cebea14d Reviewed-by: Maurice Kalinowski Reviewed-by: Friedemann Kleint --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tests/auto/corelib/io/qfile') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 8db89b9f05..8a3d490925 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -2190,12 +2190,20 @@ public: if (fileName.startsWith(":!")) { QDir dir; - QString realFile = QFINDTESTDATA(fileName.mid(2)); +#ifndef BUILTIN_TESTDATA + const QString realFile = QFINDTESTDATA(fileName.mid(2)); +#else + const QString realFile = m_dataDir->filePath(fileName.mid(2)); +#endif if (dir.exists(realFile)) return new QFSFileEngine(realFile); } return 0; } + +#ifdef BUILTIN_TESTDATA + QSharedPointer m_dataDir; +#endif }; #endif @@ -2204,6 +2212,9 @@ void tst_QFile::useQFileInAFileHandler() { // This test should not dead-lock MyRecursiveHandler handler; +#ifdef BUILTIN_TESTDATA + handler.m_dataDir = m_dataDir; +#endif QFile file(":!tst_qfile.cpp"); QVERIFY(file.exists()); } -- cgit v1.2.3 From 82c3c9d45d5fad0ed18e096f09729579a0902a71 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Wed, 8 Nov 2017 13:58:19 +0100 Subject: Fix tst_QFile::handle for systems using builtin test data To obtain the file's handle, we need to obtain it from the extracted test data instead of qrc. Change-Id: I89c5c3f3a7da7e36205a439581a6d83efffdc07c Reviewed-by: Maurice Kalinowski Reviewed-by: Friedemann Kleint --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tests/auto/corelib/io/qfile') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 8a3d490925..16a3cfd766 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -445,8 +445,6 @@ void tst_QFile::initTestCase() m_stdinProcessDir = QFINDTESTDATA("stdinprocess"); QVERIFY(!m_stdinProcessDir.isEmpty()); #endif - m_testSourceFile = QFINDTESTDATA("tst_qfile.cpp"); - QVERIFY(!m_testSourceFile.isEmpty()); m_testLogFile = QFINDTESTDATA("testlog.txt"); QVERIFY(!m_testLogFile.isEmpty()); m_dosFile = QFINDTESTDATA("dosfile.txt"); @@ -459,12 +457,15 @@ void tst_QFile::initTestCase() QVERIFY(!m_twoDotsFile.isEmpty()); #ifndef BUILTIN_TESTDATA + m_testSourceFile = QFINDTESTDATA("tst_qfile.cpp"); + QVERIFY(!m_testSourceFile.isEmpty()); m_testFile = QFINDTESTDATA("testfile.txt"); QVERIFY(!m_testFile.isEmpty()); #else m_dataDir = QEXTRACTTESTDATA("/"); QVERIFY2(!m_dataDir.isNull(), qPrintable("Could not extract test data")); m_testFile = m_dataDir->path() + "/testfile.txt"; + m_testSourceFile = m_dataDir->path() + "/tst_qfile.cpp"; #endif m_resourcesDir = QFINDTESTDATA("resources"); QVERIFY(!m_resourcesDir.isEmpty()); -- cgit v1.2.3 From 579d0cb2bed193ccb1901b121a360f85d1c57a54 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Wed, 8 Nov 2017 14:09:11 +0100 Subject: Fix tst_QFile::openDirectory for systems using builtin test data To obtain "proper" directory behavior, we have to check against the extracted "resources" directory instead of its qrc counterpart. Change-Id: I4996ba74419945f78d356ad953a5b826ff663687 Reviewed-by: Maurice Kalinowski --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tests/auto/corelib/io/qfile') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 16a3cfd766..5f0eae6fc3 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -461,14 +461,15 @@ void tst_QFile::initTestCase() QVERIFY(!m_testSourceFile.isEmpty()); m_testFile = QFINDTESTDATA("testfile.txt"); QVERIFY(!m_testFile.isEmpty()); + m_resourcesDir = QFINDTESTDATA("resources"); + QVERIFY(!m_resourcesDir.isEmpty()); #else m_dataDir = QEXTRACTTESTDATA("/"); QVERIFY2(!m_dataDir.isNull(), qPrintable("Could not extract test data")); m_testFile = m_dataDir->path() + "/testfile.txt"; m_testSourceFile = m_dataDir->path() + "/tst_qfile.cpp"; + m_resourcesDir = m_dataDir->path() + "/resources"; #endif - m_resourcesDir = QFINDTESTDATA("resources"); - QVERIFY(!m_resourcesDir.isEmpty()); m_noEndOfLineFile = QFINDTESTDATA("noendofline.txt"); QVERIFY(!m_noEndOfLineFile.isEmpty()); -- cgit v1.2.3