diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2016-08-25 14:52:05 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-07-07 18:09:24 +0000 |
commit | cf9187f8c936ed5f811ae70a452ffbee86860f39 (patch) | |
tree | ea1e708c9c6b2f1e47180e2595c9246010e76a79 /tests/auto/corelib/io/qfileinfo | |
parent | 3a9e135d5561ddbc8b6664f5aadf58fd07b02ec1 (diff) |
tst_QFileInfo: generalize old-file test
Now that QFileDevice::setFileTime() provides a portable way to prepare
our test file, we can verify QFileInfo does handle the distant past,
notably including negative time_t values. The old MS-specific code
used a time back in 1601, which we can't hope to support
cross-platform, so use one in 1901 that's a little inside the range of
32-bit time_t.
Task-number: QTBUG-47985
Change-Id: I2de3e79d8c7864221f92395813b63f373e4d8a3b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/io/qfileinfo')
-rw-r--r-- | tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp | 72 |
1 files changed, 23 insertions, 49 deletions
diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index 87b579bb2b..4c015be5aa 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -1204,55 +1204,29 @@ void tst_QFileInfo::fileTimes() void tst_QFileInfo::fileTimes_oldFile() { - // This is not supported on WinRT -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - // All files are opened in share mode (both read and write). - DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; - - // All files on Windows can be read; there's no such thing as an - // unreadable file. Add GENERIC_WRITE if WriteOnly is passed. - int accessRights = GENERIC_READ | GENERIC_WRITE; - - SECURITY_ATTRIBUTES securityAtts = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; - - // Regular file mode. In Unbuffered mode, pass the no-buffering flag. - DWORD flagsAndAtts = FILE_ATTRIBUTE_NORMAL; - - // WriteOnly can create files, ReadOnly cannot. - DWORD creationDisp = OPEN_ALWAYS; - - // Create the file handle. - HANDLE fileHandle = CreateFile(L"oldfile.txt", - accessRights, - shareMode, - &securityAtts, - creationDisp, - flagsAndAtts, - NULL); - - // Set file times back to 1601. - SYSTEMTIME stime; - stime.wYear = 1601; - stime.wMonth = 1; - stime.wDayOfWeek = 1; - stime.wDay = 1; - stime.wHour = 1; - stime.wMinute = 0; - stime.wSecond = 0; - stime.wMilliseconds = 0; - - FILETIME ctime; - QVERIFY(SystemTimeToFileTime(&stime, &ctime)); - FILETIME atime = ctime; - FILETIME mtime = atime; - QVERIFY(fileHandle); - QVERIFY(SetFileTime(fileHandle, &ctime, &atime, &mtime) != 0); - - CloseHandle(fileHandle); - - QFileInfo info("oldfile.txt"); - QCOMPARE(info.lastModified(), QDateTime(QDate(1601, 1, 1), QTime(1, 0), Qt::UTC).toLocalTime()); -#endif + // This is 2^{31} seconds before 1970-01-01 15:14:8, + // i.e. shortly after the start of time_t, in any time-zone: + const QDateTime early(QDate(1901, 12, 14), QTime(12, 0)); + QFile file("ancientfile.txt"); + file.open(QIODevice::WriteOnly); + file.write("\n", 1); + file.close(); + + /* + QFile's setFileTime calls QFSFileEngine::setFileTime() which fails unless + the file is open at the time. Of course, when writing, close() changes + modification time, so need to re-open for read in order to setFileTime(). + */ + file.open(QIODevice::ReadOnly); + bool ok = file.setFileTime(early, QFileDevice::FileModificationTime); + file.close(); + + if (ok) { + QFileInfo info(file.fileName()); + QCOMPARE(info.lastModified(), early); + } else { + QSKIP("Unable to set file metadata to ancient values"); + } } void tst_QFileInfo::isSymLink_data() |