diff options
Diffstat (limited to 'tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp')
-rw-r--r-- | tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp | 78 |
1 files changed, 72 insertions, 6 deletions
diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp index d2a2546..2daa0f6 100644 --- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp @@ -51,6 +51,13 @@ #if defined(Q_OS_WIN) # include <windows.h> #endif +#if defined(Q_OS_UNIX) +# include <sys/types.h> +# include <sys/stat.h> +# include <errno.h> +# include <fcntl.h> // open(2) +# include <unistd.h> // close(2) +#endif //TESTED_CLASS= //TESTED_FILES= @@ -77,6 +84,8 @@ private slots: void resize(); void openOnRootDrives(); void stressTest(); + void rename(); + void renameFdLeak(); public: }; @@ -143,7 +152,6 @@ void tst_QTemporaryFile::fileTemplate_data() void tst_QTemporaryFile::fileTemplate() { -#if QT_VERSION >= 0x040200 QFETCH(QString, constructorTemplate); QFETCH(QString, suffix); QFETCH(QString, fileTemplate); @@ -156,7 +164,6 @@ void tst_QTemporaryFile::fileTemplate() QCOMPARE(file.fileName().right(suffix.length()), suffix); file.close(); -#endif } @@ -233,9 +240,6 @@ void tst_QTemporaryFile::write() void tst_QTemporaryFile::openCloseOpenClose() { -#if QT_VERSION < 0x040101 - QSKIP("Until Qt 4.1.1, QTemporaryFile would create a new name every time open() was called.", SkipSingle); -#endif QString fileName; { // Create a temp file @@ -247,7 +251,7 @@ void tst_QTemporaryFile::openCloseOpenClose() QVERIFY(QFile::exists(fileName)); file.close(); - // Check that it still exists after being closed + // Check that it still exists after being closed QVERIFY(QFile::exists(fileName)); QVERIFY(!file.isOpen()); QVERIFY(file.open()); @@ -335,5 +339,67 @@ void tst_QTemporaryFile::stressTest() } } +void tst_QTemporaryFile::rename() +{ + // This test checks that the temporary file is deleted, even after a + // rename. + + QDir dir; + QVERIFY(!dir.exists("temporary-file.txt")); + + QString tempname; + { + QTemporaryFile file(dir.filePath("temporary-file.XXXXXX")); + + QVERIFY(file.open()); + tempname = file.fileName(); + QVERIFY(dir.exists(tempname)); + + QVERIFY(file.rename("temporary-file.txt")); + QVERIFY(!dir.exists(tempname)); + QVERIFY(dir.exists("temporary-file.txt")); + } + + QVERIFY(!dir.exists(tempname)); + QVERIFY(!dir.exists("temporary-file.txt")); +} + +void tst_QTemporaryFile::renameFdLeak() +{ +#ifdef Q_OS_UNIX + // Test this on Unix only + + // Open a bunch of files to force the fd count to go up + static const int count = 10; + int bunch_of_files[count]; + for (int i = 0; i < count; ++i) { + bunch_of_files[i] = ::open(SRCDIR "tst_qtemporaryfile.cpp", O_RDONLY); + QVERIFY(bunch_of_files[i] != -1); + } + + int fd; + { + QTemporaryFile file; + file.setAutoRemove(false); + QVERIFY(file.open()); + + // close the bunch of files + for (int i = 0; i < count; ++i) + ::close(bunch_of_files[i]); + + // save the file descriptor for later + fd = file.handle(); + + // rename the file to something + QString newPath = QDir::tempPath() + "/tst_qtemporaryfile-renameFdLeak-" + QString::number(getpid()); + file.rename(newPath); + QFile::remove(newPath); + } + + // check if QTemporaryFile closed the file + QVERIFY(::close(fd) == -1 && errno == EBADF); +#endif +} + QTEST_MAIN(tst_QTemporaryFile) #include "tst_qtemporaryfile.moc" |