diff options
author | Shane Kearns <shane.kearns@accenture.com> | 2010-12-02 18:07:29 (GMT) |
---|---|---|
committer | Shane Kearns <shane.kearns@accenture.com> | 2010-12-20 15:57:33 (GMT) |
commit | 37e630fba7bb1dde3e11567f52a1bd28fafbaec6 (patch) | |
tree | a3f942636ecf14168e88719ec2dc44f38637305d /tests/auto | |
parent | b98e374e94e81b66be0dc009356a729e9b97dce8 (diff) | |
download | Qt-37e630fba7bb1dde3e11567f52a1bd28fafbaec6.zip Qt-37e630fba7bb1dde3e11567f52a1bd28fafbaec6.tar.gz Qt-37e630fba7bb1dde3e11567f52a1bd28fafbaec6.tar.bz2 |
Add autotests for AutoCloseHandle / DontCloseHandle and RFile adoption
Extended the existing tests which open a file from a handle to use
symbian RFile as well (when testing on symbian).
Added a new test to check the file handle is open/closed after closing
a QFile, based on whether the file was opened with AutoCloseHandle or
not.
Reviewed-by: mread
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/qfile/test/test.pro | 4 | ||||
-rw-r--r-- | tests/auto/qfile/tst_qfile.cpp | 147 |
2 files changed, 141 insertions, 10 deletions
diff --git a/tests/auto/qfile/test/test.pro b/tests/auto/qfile/test/test.pro index 673eacc..c0049b0 100644 --- a/tests/auto/qfile/test/test.pro +++ b/tests/auto/qfile/test/test.pro @@ -37,4 +37,6 @@ win32 { LIBS+=-lole32 -luuid } - +symbian { + LIBS+=-lefsrv +} diff --git a/tests/auto/qfile/tst_qfile.cpp b/tests/auto/qfile/tst_qfile.cpp index c19079f..d9b8283 100644 --- a/tests/auto/qfile/tst_qfile.cpp +++ b/tests/auto/qfile/tst_qfile.cpp @@ -70,6 +70,8 @@ #elif defined(Q_OS_WINCE) # include <qplatformdefs.h> # include <private/qfsfileengine_p.h> +#elif defined(Q_OS_SYMBIAN) +# include <f32file.h> #endif #include <stdio.h> @@ -221,6 +223,8 @@ private slots: #endif void caseSensitivity(); + void autocloseHandle(); + // --- Task related tests below this line void task167217(); @@ -233,12 +237,20 @@ public: void invalidFile(); private: - enum FileType { OpenQFile, OpenFd, OpenStream }; + enum FileType { + OpenQFile, + OpenFd, + OpenStream, +#ifdef Q_OS_SYMBIAN + OpenRFile, +#endif + NumberOfFileTypes + }; void openStandardStreamsFileDescriptors(); void openStandardStreamsBufferedStreams(); - bool openFd(QFile &file, QIODevice::OpenMode mode) + bool openFd(QFile &file, QIODevice::OpenMode mode, QFile::FileHandleFlags handleFlags) { int fdMode = QT_OPEN_LARGEFILE | QT_OPEN_BINARY; @@ -250,10 +262,10 @@ private: fd_ = QT_OPEN(qPrintable(file.fileName()), fdMode); - return (-1 != fd_) && file.open(fd_, mode); + return (-1 != fd_) && file.open(fd_, mode, handleFlags); } - bool openStream(QFile &file, QIODevice::OpenMode mode) + bool openStream(QFile &file, QIODevice::OpenMode mode, QFile::FileHandleFlags handleFlags) { char const *streamMode = ""; @@ -265,10 +277,37 @@ private: stream_ = QT_FOPEN(qPrintable(file.fileName()), streamMode); - return stream_ && file.open(stream_, mode); + return stream_ && file.open(stream_, mode, handleFlags); + } + +#ifdef Q_OS_SYMBIAN + bool openRFile(QFile &file, QIODevice::OpenMode mode, QFile::FileHandleFlags handleFlags) + { + //connect file server first time + if (!rfs_.Handle() && rfs_.Connect() != KErrNone) + return false; + //symbian does not like ./ in filenames, so open by absolute path + QString fileName(QDir::toNativeSeparators(QFileInfo(file).absoluteFilePath())); + TPtrC fn(fileName.utf16(), fileName.length()); + TInt smode = 0; + if (mode & QIODevice::WriteOnly) + smode |= EFileWrite; + if (mode & QIODevice::ReadOnly) + smode |= EFileRead; + TInt r; + if ((mode & QIODevice::Truncate) || (!(mode & QIODevice::ReadOnly) && !(mode & QIODevice::Append))) { + r = rfile_.Replace(rfs_, fn, smode); + } else { + r = rfile_.Open(rfs_, fn, smode); + if (r == KErrNotFound && (mode & QIODevice::WriteOnly)) { + r = rfile_.Create(rfs_, fn, smode); + } + } + return (r == KErrNone) && file.open(rfile_, mode, handleFlags); } +#endif - bool openFile(QFile &file, QIODevice::OpenMode mode, FileType type = OpenQFile) + bool openFile(QFile &file, QIODevice::OpenMode mode, FileType type = OpenQFile, QFile::FileHandleFlags handleFlags = QFile::DontCloseHandle) { if (mode & QIODevice::WriteOnly && !file.exists()) { @@ -285,10 +324,14 @@ private: return file.open(mode); case OpenFd: - return openFd(file, mode); + return openFd(file, mode, handleFlags); case OpenStream: - return openStream(file, mode); + return openStream(file, mode, handleFlags); +#ifdef Q_OS_SYMBIAN + case OpenRFile: + return openRFile(file, mode, handleFlags); +#endif } return false; @@ -302,6 +345,8 @@ private: QT_CLOSE(fd_); if (stream_) ::fclose(stream_); + if (rfile_.SubSessionHandle()) + rfile_.Close(); fd_ = -1; stream_ = 0; @@ -309,6 +354,10 @@ private: int fd_; FILE *stream_; +#ifdef Q_OS_SYMBIAN + RFs rfs_; + RFile rfile_; +#endif }; tst_QFile::tst_QFile() @@ -2214,6 +2263,9 @@ void tst_QFile::writeLargeDataBlock_data() QTest::newRow("localfile-QFile") << "./largeblockfile.txt" << (int)OpenQFile; QTest::newRow("localfile-Fd") << "./largeblockfile.txt" << (int)OpenFd; QTest::newRow("localfile-Stream") << "./largeblockfile.txt" << (int)OpenStream; +#ifdef Q_OS_SYMBIAN + QTest::newRow("localfile-RFile") << "./largeblockfile.txt" << (int)OpenRFile; +#endif #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) // Some semi-randomness to avoid collisions. @@ -3100,7 +3152,7 @@ void tst_QFile::openStandardStreams() void tst_QFile::writeNothing() { - for (int i = 0; i < 3; ++i) { + for (int i = 0; i < NumberOfFileTypes; ++i) { QFile file("file.txt"); QVERIFY( openFile(file, QIODevice::WriteOnly | QIODevice::Unbuffered, FileType(i)) ); QVERIFY( 0 == file.write((char *)0, 0) ); @@ -3116,6 +3168,9 @@ void tst_QFile::resize_data() QTest::newRow("native") << int(OpenQFile); QTest::newRow("fileno") << int(OpenFd); QTest::newRow("stream") << int(OpenStream); +#ifdef Q_OS_SYMBIAN + QTest::newRow("rfile") << int(OpenRFile); +#endif } void tst_QFile::resize() @@ -3227,5 +3282,79 @@ void tst_QFile::caseSensitivity() } } +void tst_QFile::autocloseHandle() +{ +#ifdef Q_OS_SYMBIAN + // these tests are a bit different, because using a closed file handle results in a panic rather than error + { + QFile file("readonlyfile"); + QFile file2("readonlyfile"); + QVERIFY(openFile(file, QIODevice::ReadOnly, OpenRFile, QFile::AutoCloseHandle)); + // file is opened with mandatory lock, so opening again should fail + QVERIFY(!file2.open(QIODevice::ReadOnly)); + + file.close(); + // opening again should now succeed (because handle is closed) + QVERIFY(file2.open(QIODevice::ReadOnly)); + } + + { + QFile file("readonlyfile"); + QFile file2("readonlyfile"); + QVERIFY(openFile(file, QIODevice::ReadOnly, OpenRFile, QFile::DontCloseHandle)); + // file is opened with mandatory lock, so opening again should fail + QVERIFY(!file2.open(QIODevice::ReadOnly)); + + file.close(); + // opening again should still fail (because handle is not auto closed) + QVERIFY(!file2.open(QIODevice::ReadOnly)); + + rfile_.Close(); + // now it should succeed + QVERIFY(file2.open(QIODevice::ReadOnly)); + } +#endif + + { + QFile file("readonlyfile"); + QVERIFY(openFile(file, QIODevice::ReadOnly, OpenFd, QFile::AutoCloseHandle)); + file.close(); + //file is closed, read should fail + char buf; + QCOMPARE(::read(fd_, &buf, 1), -1); + QVERIFY(errno = EBADF); + } + + { + QFile file("readonlyfile"); + QVERIFY(openFile(file, QIODevice::ReadOnly, OpenFd, QFile::DontCloseHandle)); + file.close(); + //file is not closed, read should succeed + char buf; + QCOMPARE(::read(fd_, &buf, 1), 1); + ::close(fd_); + } + + { + QFile file("readonlyfile"); + QVERIFY(openFile(file, QIODevice::ReadOnly, OpenStream, QFile::AutoCloseHandle)); + file.close(); + //file is closed, read should fail + char buf; + QCOMPARE(int(::fread(&buf, 1, 1, stream_)), 0); + QVERIFY(::ferror(stream_)); //actual error seems to be OS dependent + } + + { + QFile file("readonlyfile"); + QVERIFY(openFile(file, QIODevice::ReadOnly, OpenStream, QFile::DontCloseHandle)); + file.close(); + //file is not closed, read should succeed + char buf; + QCOMPARE(int(::fread(&buf, 1, 1, stream_)), 1); + ::fclose(stream_); + } +} + QTEST_MAIN(tst_QFile) #include "tst_qfile.moc" |