summaryrefslogtreecommitdiffstats
path: root/tests/auto/qtemporaryfile
diff options
context:
space:
mode:
authorJoão Abecasis <joao@abecasis.name>2009-08-27 18:56:47 +0200
committerJoão Abecasis <joao@abecasis.name>2009-08-31 19:30:58 +0200
commit7c06af4528d7aa7186b12546c261d5d2a0a3641d (patch)
tree509c44f9b7f78c107845338d338e91a6293e8a9b /tests/auto/qtemporaryfile
parent9d24605add8daabfa55e1085e71ed47b14ed7d83 (diff)
Reset QTemporaryFile's state after failed open() on Windows
This fixes a regression introduced in 4.5.2 where QTemporaryFile would no longer attempt to generate a random name after a failed open. Under certain situations, this led to a non-random file being left behind in QDir::tempPath when using the fallback implementation of QFile::copy. Avoid calling QFSFileEngine::setFileName() on a template, so as not to process it as file name. By consistently not calling setFileTemplate in the constructor, we also delay allocation of the fileEngine. Changes made to that function also keep it from unnecessarily allocating the fileEngine. Task-number: 260165 Reviewed-by: Thiago Macieira
Diffstat (limited to 'tests/auto/qtemporaryfile')
-rw-r--r--tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
index 2541b48d88..73486e709b 100644
--- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
+++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
@@ -89,6 +89,8 @@ private slots:
void renameFdLeak();
void reOpenThroughQFile();
void keepOpenMode();
+ void resetTemplateAfterError();
+ void setTemplateAfterOpen();
public:
};
@@ -472,5 +474,85 @@ void tst_QTemporaryFile::keepOpenMode()
}
}
+void tst_QTemporaryFile::resetTemplateAfterError()
+{
+ // calling setFileTemplate on a failed open
+
+ QString tempPath = QDir::tempPath();
+
+ QString const fileTemplate("destination/qt_temp_file_test.XXXXXX");
+ QString const fileTemplate2(tempPath + "/qt_temp_file_test.XXXXXX");
+
+ QVERIFY2( QDir(tempPath).exists() || QDir().mkpath(tempPath), "Test precondition" );
+ QVERIFY2( !QFile::exists("destination"), "Test precondition" );
+ QVERIFY2( !QFile::exists(fileTemplate2) || QFile::remove(fileTemplate2), "Test precondition" );
+
+ QFile file(fileTemplate2);
+ QByteArray fileContent("This file is intentionally NOT left empty.");
+
+ QVERIFY( file.open(QIODevice::ReadWrite | QIODevice::Truncate) );
+ QCOMPARE( file.write(fileContent), (qint64)fileContent.size() );
+ QVERIFY( file.flush() );
+
+ QString fileName;
+ {
+ QTemporaryFile temp;
+
+ QVERIFY( temp.fileName().isEmpty() );
+ QVERIFY( !temp.fileTemplate().isEmpty() );
+
+ temp.setFileTemplate( fileTemplate );
+
+ QVERIFY( temp.fileName().isEmpty() );
+ QCOMPARE( temp.fileTemplate(), fileTemplate );
+
+ QVERIFY( !temp.open() );
+
+ QVERIFY( temp.fileName().isEmpty() );
+ QCOMPARE( temp.fileTemplate(), fileTemplate );
+
+ temp.setFileTemplate( fileTemplate2 );
+ QVERIFY( temp.open() );
+
+ fileName = temp.fileName();
+ QVERIFY( QFile::exists(fileName) );
+ QVERIFY( !fileName.isEmpty() );
+ QVERIFY2( fileName != fileTemplate2,
+ ("Generated name shouldn't be same as template: " + fileTemplate2).toLocal8Bit().constData() );
+ }
+
+ QVERIFY( !QFile::exists(fileName) );
+
+ file.seek(0);
+ QCOMPARE( QString(file.readAll()), QString(fileContent) );
+ QVERIFY( file.remove() );
+}
+
+void tst_QTemporaryFile::setTemplateAfterOpen()
+{
+ QTemporaryFile temp;
+
+ QVERIFY( temp.fileName().isEmpty() );
+ QVERIFY( !temp.fileTemplate().isEmpty() );
+
+ QVERIFY( temp.open() );
+
+ QString const fileName = temp.fileName();
+ QString const newTemplate("funny-path/funny-name-XXXXXX.tmp");
+
+ QVERIFY( !fileName.isEmpty() );
+ QVERIFY( QFile::exists(fileName) );
+ QVERIFY( !temp.fileTemplate().isEmpty() );
+ QVERIFY( temp.fileTemplate() != newTemplate );
+
+ temp.close(); // QTemporaryFile::setFileTemplate will assert on isOpen() up to 4.5.2
+ temp.setFileTemplate(newTemplate);
+ QCOMPARE( temp.fileTemplate(), newTemplate );
+
+ QVERIFY( temp.open() );
+ QCOMPARE( temp.fileName(), fileName );
+ QCOMPARE( temp.fileTemplate(), newTemplate );
+}
+
QTEST_MAIN(tst_QTemporaryFile)
#include "tst_qtemporaryfile.moc"