tests/auto/qfile/tst_qfile.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    77 
    77 
    78 #if defined(Q_OS_SYMBIAN)
    78 #if defined(Q_OS_SYMBIAN)
    79 # define SRCDIR ""
    79 # define SRCDIR ""
    80 #endif
    80 #endif
    81 
    81 
       
    82 #ifndef STDIN_FILENO
       
    83 #define STDIN_FILENO 0
       
    84 #endif
       
    85 
       
    86 #ifndef STDOUT_FILENO
       
    87 #define STDOUT_FILENO 1
       
    88 #endif
       
    89 
       
    90 #ifndef STDERR_FILENO
       
    91 #define STDERR_FILENO 2
       
    92 #endif
       
    93 
       
    94 #ifndef QT_OPEN_BINARY
       
    95 #define QT_OPEN_BINARY 0
       
    96 #endif
       
    97 
    82 Q_DECLARE_METATYPE(QFile::FileError)
    98 Q_DECLARE_METATYPE(QFile::FileError)
    83 
    99 
    84 //TESTED_CLASS=
   100 //TESTED_CLASS=
    85 //TESTED_FILES=
   101 //TESTED_FILES=
    86 
   102 
   103     void open_data();
   119     void open_data();
   104     void open();
   120     void open();
   105     void openUnbuffered();
   121     void openUnbuffered();
   106     void size_data();
   122     void size_data();
   107     void size();
   123     void size();
       
   124     void sizeNoExist();
   108     void seek();
   125     void seek();
   109     void setSize();
   126     void setSize();
   110     void setSizeSeek();
   127     void setSizeSeek();
   111     void atEnd();
   128     void atEnd();
   112     void readLine();
   129     void readLine();
   185     void mapResource_data();
   202     void mapResource_data();
   186     void mapResource();
   203     void mapResource();
   187     void mapOpenMode_data();
   204     void mapOpenMode_data();
   188     void mapOpenMode();
   205     void mapOpenMode();
   189 
   206 
       
   207     void openStandardStreams();
       
   208 
   190     // --- Task related tests below this line
   209     // --- Task related tests below this line
   191     void task167217();
   210     void task167217();
   192 
   211 
   193     void openDirectory();
   212     void openDirectory();
       
   213     void writeNothing();
   194 
   214 
   195 public:
   215 public:
   196 // disabled this test for the moment... it hangs
   216 // disabled this test for the moment... it hangs
   197     void invalidFile_data();
   217     void invalidFile_data();
   198     void invalidFile();
   218     void invalidFile();
       
   219 
       
   220 private:
       
   221     enum FileType { OpenQFile, OpenFd, OpenStream };
       
   222 
       
   223     void openStandardStreamsFileDescriptors();
       
   224     void openStandardStreamsBufferedStreams();
       
   225 
       
   226     bool openFd(QFile &file, QIODevice::OpenMode mode)
       
   227     {
       
   228         int fdMode = QT_OPEN_LARGEFILE | QT_OPEN_BINARY;
       
   229 
       
   230         // File will be truncated if in Write mode.
       
   231         if (mode & QIODevice::WriteOnly)
       
   232             fdMode |= QT_OPEN_WRONLY | QT_OPEN_TRUNC;
       
   233         if (mode & QIODevice::ReadOnly)
       
   234             fdMode |= QT_OPEN_RDONLY;
       
   235 
       
   236         fd_ = QT_OPEN(qPrintable(file.fileName()), fdMode);
       
   237 
       
   238         return (-1 != fd_) && file.open(fd_, mode);
       
   239     }
       
   240 
       
   241     bool openStream(QFile &file, QIODevice::OpenMode mode)
       
   242     {
       
   243         char const *streamMode = "";
       
   244 
       
   245         // File will be truncated if in Write mode.
       
   246         if (mode & QIODevice::WriteOnly)
       
   247             streamMode = "wb+";
       
   248         else if (mode & QIODevice::ReadOnly)
       
   249             streamMode = "rb";
       
   250 
       
   251         stream_ = QT_FOPEN(qPrintable(file.fileName()), streamMode);
       
   252 
       
   253         return stream_ && file.open(stream_, mode);
       
   254     }
       
   255 
       
   256     bool openFile(QFile &file, QIODevice::OpenMode mode, FileType type = OpenQFile)
       
   257     {
       
   258         if (mode & QIODevice::WriteOnly && !file.exists())
       
   259         {
       
   260             // Make sure the file exists
       
   261             QFile createFile(file.fileName());
       
   262             if (!createFile.open(QIODevice::ReadWrite))
       
   263                 return false;
       
   264         }
       
   265 
       
   266         // Note: openFd and openStream will truncate the file if write mode.
       
   267         switch (type)
       
   268         {
       
   269             case OpenQFile:
       
   270                 return file.open(mode);
       
   271 
       
   272             case OpenFd:
       
   273                 return openFd(file, mode);
       
   274 
       
   275             case OpenStream:
       
   276                 return openStream(file, mode);
       
   277         }
       
   278 
       
   279         return false;
       
   280     }
       
   281 
       
   282     void closeFile(QFile &file)
       
   283     {
       
   284         file.close();
       
   285 
       
   286         if (-1 != fd_)
       
   287             QT_CLOSE(fd_);
       
   288         if (stream_)
       
   289             ::fclose(stream_);
       
   290 
       
   291         fd_ = -1;
       
   292         stream_ = 0;
       
   293     }
       
   294 
       
   295     int fd_;
       
   296     FILE *stream_;
   199 };
   297 };
   200 
   298 
   201 tst_QFile::tst_QFile()
   299 tst_QFile::tst_QFile()
   202 {
   300 {
   203 }
   301 }
   209 
   307 
   210 void tst_QFile::init()
   308 void tst_QFile::init()
   211 {
   309 {
   212 // TODO: Add initialization code here.
   310 // TODO: Add initialization code here.
   213 // This will be executed immediately before each test is run.
   311 // This will be executed immediately before each test is run.
       
   312     fd_ = -1;
       
   313     stream_ = 0;
   214 }
   314 }
   215 
   315 
   216 void tst_QFile::cleanup()
   316 void tst_QFile::cleanup()
   217 {
   317 {
   218 // TODO: Add cleanup code here.
   318 // TODO: Add cleanup code here.
   237     // for renameMultiple()
   337     // for renameMultiple()
   238     QFile::remove("file-to-be-renamed.txt");
   338     QFile::remove("file-to-be-renamed.txt");
   239     QFile::remove("existing-file.txt");
   339     QFile::remove("existing-file.txt");
   240     QFile::remove("file-renamed-once.txt");
   340     QFile::remove("file-renamed-once.txt");
   241     QFile::remove("file-renamed-twice.txt");
   341     QFile::remove("file-renamed-twice.txt");
       
   342 
       
   343     if (-1 != fd_)
       
   344         QT_CLOSE(fd_);
       
   345     if (stream_)
       
   346         ::fclose(stream_);
   242 }
   347 }
   243 
   348 
   244 void tst_QFile::initTestCase()
   349 void tst_QFile::initTestCase()
   245 {
   350 {
   246     QFile::remove("noreadfile");
   351     QFile::remove("noreadfile");
   436 }
   541 }
   437 
   542 
   438 void tst_QFile::size_data()
   543 void tst_QFile::size_data()
   439 {
   544 {
   440     QTest::addColumn<QString>("filename");
   545     QTest::addColumn<QString>("filename");
   441     QTest::addColumn<int>("size");
   546     QTest::addColumn<qint64>("size");
   442 
   547 
   443     QTest::newRow( "exist01" ) << QString(SRCDIR "testfile.txt") << 245;
   548     QTest::newRow( "exist01" ) << QString(SRCDIR "testfile.txt") << (qint64)245;
   444     QTest::newRow( "nonexist01" ) << QString("foo.txt") << 0;
       
   445 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
   549 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
   446     // Only test UNC on Windows./
   550     // Only test UNC on Windows./
   447     QTest::newRow("unc") << "//" + QString(QtNetworkSettings::winServerName() + "/testsharewritable/test.pri") << 34;
   551     QTest::newRow("unc") << "//" + QString(QtNetworkSettings::winServerName() + "/testsharewritable/test.pri") << (qint64)34;
   448 #endif
   552 #endif
   449 }
   553 }
   450 
   554 
   451 void tst_QFile::size()
   555 void tst_QFile::size()
   452 {
   556 {
   453     QFETCH( QString, filename );
   557     QFETCH( QString, filename );
   454     QFile f( filename );
   558     QFETCH( qint64, size );
   455     QTEST( (int)f.size(), "size" );
   559 
   456     if (f.open(QFile::ReadOnly))
   560 #ifdef Q_WS_WINCE
   457         QTEST( (int)f.size(), "size" );
   561         filename = QFileInfo(filename).absoluteFilePath();
       
   562 #endif
       
   563 
       
   564     {
       
   565         QFile f( filename );
       
   566         QCOMPARE( f.size(), size );
       
   567 
       
   568         QVERIFY( f.open(QIODevice::ReadOnly) );
       
   569         QCOMPARE( f.size(), size );
       
   570     }
       
   571 
       
   572     {
       
   573         QFile f;
       
   574         FILE* stream = QT_FOPEN(filename.toLocal8Bit().constData(), "rb");
       
   575         QVERIFY( stream );
       
   576         QVERIFY( f.open(stream, QIODevice::ReadOnly) );
       
   577         QCOMPARE( f.size(), size );
       
   578 
       
   579         f.close();
       
   580         fclose(stream);
       
   581     }
       
   582 
       
   583     {
       
   584 #ifdef Q_WS_WINCE
       
   585         QSKIP("Currently low level file I/O not well supported on Windows CE", SkipSingle);
       
   586 #endif
       
   587         QFile f;
       
   588 
       
   589         int fd = QT_OPEN(filename.toLocal8Bit().constData(), QT_OPEN_RDONLY);
       
   590 
       
   591         QVERIFY( fd != -1 );
       
   592         QVERIFY( f.open(fd, QIODevice::ReadOnly) );
       
   593         QCOMPARE( f.size(), size );
       
   594 
       
   595         f.close();
       
   596         QT_CLOSE(fd);
       
   597     }
       
   598 }
       
   599 
       
   600 void tst_QFile::sizeNoExist()
       
   601 {
       
   602     QFile file("nonexist01");
       
   603     QVERIFY( !file.exists() );
       
   604     QCOMPARE( file.size(), (qint64)0 );
       
   605     QVERIFY( !file.open(QIODevice::ReadOnly) );
   458 }
   606 }
   459 
   607 
   460 void tst_QFile::seek()
   608 void tst_QFile::seek()
   461 {
   609 {
   462     QFile::remove("newfile.txt");
   610     QFile::remove("newfile.txt");
   465     QCOMPARE(file.size(), qint64(0));
   613     QCOMPARE(file.size(), qint64(0));
   466     QCOMPARE(file.pos(), qint64(0));
   614     QCOMPARE(file.pos(), qint64(0));
   467     QVERIFY(file.seek(10));
   615     QVERIFY(file.seek(10));
   468     QCOMPARE(file.pos(), qint64(10));
   616     QCOMPARE(file.pos(), qint64(10));
   469     QCOMPARE(file.size(), qint64(0));
   617     QCOMPARE(file.size(), qint64(0));
       
   618     file.close();
   470     QFile::remove("newfile.txt");
   619     QFile::remove("newfile.txt");
   471 }
   620 }
   472 
   621 
   473 void tst_QFile::setSize()
   622 void tst_QFile::setSize()
   474 {
   623 {
   613 #else
   762 #else
   614     QByteArray lotsOfData(1024, '@'); // 10 megs
   763     QByteArray lotsOfData(1024, '@'); // 10 megs
   615 
   764 
   616     QProcess process;
   765     QProcess process;
   617     process.start("stdinprocess/stdinprocess all");
   766     process.start("stdinprocess/stdinprocess all");
       
   767     QVERIFY( process.waitForStarted() );
   618     for (int i = 0; i < 5; ++i) {
   768     for (int i = 0; i < 5; ++i) {
   619         QTest::qWait(1000);
   769         QTest::qWait(1000);
   620         process.write(lotsOfData);
   770         process.write(lotsOfData);
   621         while (process.bytesToWrite() > 0) {
   771         while (process.bytesToWrite() > 0) {
   622             QVERIFY(process.waitForBytesWritten());
   772             QVERIFY(process.waitForBytesWritten());
   989     // Fallback copy of closed file.
  1139     // Fallback copy of closed file.
   990     QVERIFY(file.copy("file-copy-destination.txt"));
  1140     QVERIFY(file.copy("file-copy-destination.txt"));
   991     QVERIFY(QFile::exists("file-copy-destination.txt"));
  1141     QVERIFY(QFile::exists("file-copy-destination.txt"));
   992     QVERIFY(!file.isOpen());
  1142     QVERIFY(!file.isOpen());
   993 
  1143 
       
  1144 #ifdef Q_WS_WINCE
   994     // Need to reset permissions on Windows to be able to delete
  1145     // Need to reset permissions on Windows to be able to delete
   995     QVERIFY(QFile::setPermissions("file-copy-destination.txt",
  1146     QVERIFY(QFile::setPermissions("file-copy-destination.txt",
   996             QFile::ReadOwner | QFile::WriteOwner));
  1147             QFile::WriteOther));
       
  1148 #else
       
  1149      // Need to reset permissions on Windows to be able to delete
       
  1150     QVERIFY(QFile::setPermissions("file-copy-destination.txt",
       
  1151            QFile::ReadOwner | QFile::WriteOwner));
       
  1152 #endif
   997     QVERIFY(QFile::remove("file-copy-destination.txt"));
  1153     QVERIFY(QFile::remove("file-copy-destination.txt"));
   998 
  1154 
   999     // Fallback copy of open file.
  1155     // Fallback copy of open file.
  1000     QVERIFY(file.open(QIODevice::ReadOnly));
  1156     QVERIFY(file.open(QIODevice::ReadOnly));
  1001     QVERIFY(file.copy("file-copy-destination.txt"));
  1157     QVERIFY(file.copy("file-copy-destination.txt"));
  1002     QVERIFY(QFile::exists("file-copy-destination.txt"));
  1158     QVERIFY(QFile::exists("file-copy-destination.txt"));
  1003     QVERIFY(!file.isOpen());
  1159     QVERIFY(!file.isOpen());
  1004 
  1160 
       
  1161     file.close(); 
  1005     QFile::remove("file-copy-destination.txt");
  1162     QFile::remove("file-copy-destination.txt");
  1006 }
  1163 }
  1007 
  1164 
  1008 #ifdef Q_OS_WIN
  1165 #ifdef Q_OS_WIN
  1009 #include <objbase.h>
  1166 #include <objbase.h>
  1907 }
  2064 }
  1908 
  2065 
  1909 void tst_QFile::writeLargeDataBlock_data()
  2066 void tst_QFile::writeLargeDataBlock_data()
  1910 {
  2067 {
  1911     QTest::addColumn<QString>("fileName");
  2068     QTest::addColumn<QString>("fileName");
  1912 
  2069     QTest::addColumn<int>("type");
  1913     QTest::newRow("localfile") << QString("./largeblockfile.txt");
  2070 
       
  2071     QTest::newRow("localfile-QFile")  << "./largeblockfile.txt" << (int)OpenQFile;
       
  2072     QTest::newRow("localfile-Fd")     << "./largeblockfile.txt" << (int)OpenFd;
       
  2073     QTest::newRow("localfile-Stream") << "./largeblockfile.txt" << (int)OpenStream;
       
  2074 
  1914 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
  2075 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
  1915     // Some semi-randomness to avoid collisions.
  2076     // Some semi-randomness to avoid collisions.
  1916     QTest::newRow("unc file")
  2077     QTest::newRow("unc file")
  1917         << QString("//" + QtNetworkSettings::winServerName() + "/TESTSHAREWRITABLE/largefile-%1-%2.txt")
  2078         << QString("//" + QtNetworkSettings::winServerName() + "/TESTSHAREWRITABLE/largefile-%1-%2.txt")
  1918         .arg(QHostInfo::localHostName())
  2079         .arg(QHostInfo::localHostName())
  1919         .arg(QTime::currentTime().msec());
  2080         .arg(QTime::currentTime().msec()) << (int)OpenQFile;
  1920 #endif
  2081 #endif
       
  2082 }
       
  2083 
       
  2084 static QByteArray getLargeDataBlock()
       
  2085 {
       
  2086     static QByteArray array;
       
  2087 
       
  2088     if (array.isNull())
       
  2089     {
       
  2090 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
       
  2091         int resizeSize = 1024 * 1024; // WinCE and Symbian do not have much space
       
  2092 #else
       
  2093         int resizeSize = 64 * 1024 * 1024;
       
  2094 #endif
       
  2095         array.resize(resizeSize);
       
  2096         for (int i = 0; i < array.size(); ++i)
       
  2097             array[i] = uchar(i);
       
  2098     }
       
  2099 
       
  2100     return array;
  1921 }
  2101 }
  1922 
  2102 
  1923 void tst_QFile::writeLargeDataBlock()
  2103 void tst_QFile::writeLargeDataBlock()
  1924 {
  2104 {
  1925     QFETCH(QString, fileName);
  2105     QFETCH(QString, fileName);
  1926 
  2106     QFETCH( int, type );
  1927     // Generate a 64MB array with well defined contents.
  2107 
  1928     QByteArray array;
  2108     QByteArray const originalData = getLargeDataBlock();
  1929 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
  2109 
  1930 	int resizeSize = 1024 * 1024; // WinCE and Symbian do not have much space
  2110     {
  1931 #else
  2111         QFile file(fileName);
  1932 	int resizeSize = 64 * 1024 * 1024;
  2112 
  1933 #endif
  2113         QVERIFY2( openFile(file, QIODevice::WriteOnly, (FileType)type),
  1934     array.resize(resizeSize);
  2114             qPrintable(QString("Couldn't open file for writing: [%1]").arg(fileName)) );
  1935     for (int i = 0; i < array.size(); ++i)
  2115         QCOMPARE( file.write(originalData), (qint64)originalData.size() );
  1936         array[i] = uchar(i);
  2116         QVERIFY( file.flush() );
  1937 
  2117 
  1938     // Remove and open the target file
  2118         closeFile(file);
  1939     QFile file(fileName);
  2119     }
  1940     file.remove();
  2120 
  1941     if (file.open(QFile::WriteOnly)) {
  2121     QByteArray readData;
  1942         QCOMPARE(file.write(array), qint64(array.size()));
  2122 
  1943         file.close();
  2123     {
  1944         QVERIFY(file.open(QFile::ReadOnly));
  2124         QFile file(fileName);
  1945         array.clear();
  2125 
  1946         array = file.readAll();
  2126         QVERIFY2( openFile(file, QIODevice::ReadOnly, (FileType)type),
  1947         file.remove();
  2127             qPrintable(QString("Couldn't open file for reading: [%1]").arg(fileName)) );
  1948     } else {
  2128         readData = file.readAll();
  1949         QFAIL(qPrintable(QString("Couldn't open file for writing: [%1]").arg(fileName)));
  2129         closeFile(file);
  1950     }
  2130     }
  1951     // Check that we got the right content
  2131 
  1952     QCOMPARE(array.size(), resizeSize);
  2132     QCOMPARE( readData, originalData );
  1953     for (int i = 0; i < array.size(); ++i) {
  2133     QVERIFY( QFile::remove(fileName) );
  1954         if (array[i] != char(i)) {
       
  1955             QFAIL(qPrintable(QString("Wrong contents! Char at %1 = %2, expected %3")
       
  1956                   .arg(i).arg(int(uchar(array[i]))).arg(int(uchar(i)))));
       
  1957         }
       
  1958     }
       
  1959 }
  2134 }
  1960 
  2135 
  1961 void tst_QFile::readFromWriteOnlyFile()
  2136 void tst_QFile::readFromWriteOnlyFile()
  1962 {
  2137 {
  1963     QFile file("writeonlyfile");
  2138     QFile file("writeonlyfile");
  2082     f.open(QFile::WriteOnly);
  2257     f.open(QFile::WriteOnly);
  2083     f.close();
  2258     f.close();
  2084 
  2259 
  2085     QFile file(source);
  2260     QFile file(source);
  2086     QCOMPARE(file.rename(destination), result);
  2261     QCOMPARE(file.rename(destination), result);
       
  2262 
  2087     if (result)
  2263     if (result)
  2088         QCOMPARE(file.error(), QFile::NoError);
  2264         QCOMPARE(file.error(), QFile::NoError);
  2089     else
  2265     else
  2090         QCOMPARE(file.error(), QFile::RenameError);
  2266         QCOMPARE(file.error(), QFile::RenameError);
  2091 
  2267 
  2210         writeFile.write(QByteArray(1 << j, '@'));
  2386         writeFile.write(QByteArray(1 << j, '@'));
  2211         writeFile.flush();
  2387         writeFile.flush();
  2212         QCOMPARE(readFile.read(1 << j).size(), 1 << j);
  2388         QCOMPARE(readFile.read(1 << j).size(), 1 << j);
  2213     }
  2389     }
  2214 
  2390 
       
  2391     readFile.close();
  2215     QFile::remove(QLatin1String("appendfile.txt"));
  2392     QFile::remove(QLatin1String("appendfile.txt"));
  2216 }
  2393 }
  2217 
  2394 
  2218 void tst_QFile::miscWithUncPathAsCurrentDir()
  2395 void tst_QFile::miscWithUncPathAsCurrentDir()
  2219 {
  2396 {
  2334             QVERIFY(file.seek(245));
  2511             QVERIFY(file.seek(245));
  2335             QVERIFY(file.atEnd());
  2512             QVERIFY(file.atEnd());
  2336         }
  2513         }
  2337 
  2514 
  2338         QByteArray ret = file.read(10);
  2515         QByteArray ret = file.read(10);
  2339         QVERIFY(ret.isNull());
  2516         QVERIFY(ret.isEmpty());
  2340         QVERIFY(file.error() == QFile::NoError);
  2517         QVERIFY(file.error() == QFile::NoError);
  2341         QVERIFY(file.atEnd());
  2518         QVERIFY(file.atEnd());
  2342 
  2519 
  2343         // Do it again to ensure that we get the same result
  2520         // Do it again to ensure that we get the same result
  2344         ret = file.read(10);
  2521         ret = file.read(10);
  2345         QVERIFY(ret.isNull());
  2522         QVERIFY(ret.isEmpty());
  2346         QVERIFY(file.error() == QFile::NoError);
  2523         QVERIFY(file.error() == QFile::NoError);
  2347         QVERIFY(file.atEnd());
  2524         QVERIFY(file.atEnd());
  2348     }
  2525     }
  2349 
  2526 
  2350     {
  2527     {
  2451     QFETCH(int, offset);
  2628     QFETCH(int, offset);
  2452     QFETCH(int, size);
  2629     QFETCH(int, size);
  2453     QFETCH(QFile::FileError, error);
  2630     QFETCH(QFile::FileError, error);
  2454 
  2631 
  2455     QString fileName = QDir::currentPath() + '/' + "qfile_map_testfile";
  2632     QString fileName = QDir::currentPath() + '/' + "qfile_map_testfile";
       
  2633 
       
  2634 #ifdef Q_WS_WINCE
       
  2635      fileName = QFileInfo(fileName).absoluteFilePath();
       
  2636 #endif
       
  2637 
  2456     if (QFile::exists(fileName)) {
  2638     if (QFile::exists(fileName)) {
  2457         QVERIFY(QFile::setPermissions(fileName,
  2639         QVERIFY(QFile::setPermissions(fileName,
  2458             QFile::WriteOwner | QFile::ReadOwner | QFile::WriteUser | QFile::ReadUser));
  2640             QFile::WriteOwner | QFile::ReadOwner | QFile::WriteUser | QFile::ReadUser));
  2459 	QFile::remove(fileName);
  2641         QFile::remove(fileName);
  2460     }
  2642     }
  2461     QFile file(fileName);
  2643     QFile file(fileName);
  2462 
  2644 
  2463     // invalid, not open
  2645     // invalid, not open
  2464     uchar *memory = file.map(0, size);
  2646     uchar *memory = file.map(0, size);
  2493     QVERIFY(memory[0] == 'Q');
  2675     QVERIFY(memory[0] == 'Q');
  2494     QVERIFY(file.unmap(memory));
  2676     QVERIFY(file.unmap(memory));
  2495     QCOMPARE(file.error(), QFile::NoError);
  2677     QCOMPARE(file.error(), QFile::NoError);
  2496 
  2678 
  2497     // hpux wont let you map multiple times.
  2679     // hpux wont let you map multiple times.
  2498 #if !defined(Q_OS_HPUX) && !defined(Q_USE_DEPRECATED_MAP_API)
  2680 #if !defined(Q_OS_HPUX) && !defined(Q_USE_DEPRECATED_MAP_API) && !defined(Q_OS_WINCE)
  2499     // exotic test to make sure that multiple maps work
  2681     // exotic test to make sure that multiple maps work
       
  2682 
       
  2683     // note: windows ce does not reference count mutliple maps
       
  2684     // it's essentially just the same reference but it 
       
  2685     // cause a resource lock on the file which prevents it 
       
  2686     // from being removed    uchar *memory1 = file.map(0, file.size());
  2500     uchar *memory1 = file.map(0, file.size());
  2687     uchar *memory1 = file.map(0, file.size());
  2501     QCOMPARE(file.error(), QFile::NoError);
  2688     QCOMPARE(file.error(), QFile::NoError);
  2502     uchar *memory2 = file.map(0, file.size());
  2689     uchar *memory2 = file.map(0, file.size());
  2503     QCOMPARE(file.error(), QFile::NoError);
  2690     QCOMPARE(file.error(), QFile::NoError);
  2504     QVERIFY(memory1);
  2691     QVERIFY(memory1);
  2530         memory = file.map(offset, size);
  2717         memory = file.map(offset, size);
  2531         QCOMPARE(file.error(), QFile::PermissionsError);
  2718         QCOMPARE(file.error(), QFile::PermissionsError);
  2532         QVERIFY(!memory);
  2719         QVERIFY(!memory);
  2533         QVERIFY(file.setPermissions(originalPermissions));
  2720         QVERIFY(file.setPermissions(originalPermissions));
  2534     }
  2721     }
  2535 
       
  2536     QVERIFY(file.remove());
  2722     QVERIFY(file.remove());
  2537 }
  2723 }
  2538 
  2724 
  2539 void tst_QFile::mapResource_data()
  2725 void tst_QFile::mapResource_data()
  2540 {
  2726 {
  2626     file.close();
  2812     file.close();
  2627 }
  2813 }
  2628 
  2814 
  2629 void tst_QFile::openDirectory()
  2815 void tst_QFile::openDirectory()
  2630 {
  2816 {
  2631     QFile f1("resources");
  2817     QFile f1(SRCDIR "resources");
       
  2818     // it's a directory, it must exist
       
  2819     QVERIFY(f1.exists());
       
  2820 
       
  2821     // ...but not be openable
  2632     QVERIFY(!f1.open(QIODevice::ReadOnly));
  2822     QVERIFY(!f1.open(QIODevice::ReadOnly));
  2633     f1.close();
  2823     f1.close();
  2634     QVERIFY(!f1.open(QIODevice::ReadOnly|QIODevice::Unbuffered));
  2824     QVERIFY(!f1.open(QIODevice::ReadOnly|QIODevice::Unbuffered));
       
  2825     f1.close();
       
  2826     QVERIFY(!f1.open(QIODevice::ReadWrite));
       
  2827     f1.close();
       
  2828     QVERIFY(!f1.open(QIODevice::WriteOnly));
       
  2829     f1.close();
       
  2830     QVERIFY(!f1.open(QIODevice::WriteOnly|QIODevice::Unbuffered));
       
  2831     f1.close();
       
  2832 }
       
  2833 
       
  2834 void tst_QFile::openStandardStreamsFileDescriptors()
       
  2835 {
       
  2836 #ifdef Q_WS_WINCE
       
  2837     //allthough Windows CE (not mobile!) has functions that allow redirecting
       
  2838     //the standard file descriptors to a file (see SetStdioPathW/GetStdioPathW)
       
  2839     //it does not have functions to simply open them like below .
       
  2840     QSKIP("Opening standard streams on Windows CE via descriptor not implemented", SkipAll);
       
  2841 #endif
       
  2842     // Using file descriptors
       
  2843     {
       
  2844         QFile in;
       
  2845         in.open(STDIN_FILENO, QIODevice::ReadOnly);
       
  2846         QCOMPARE( in.pos(), (qint64)0 );
       
  2847         QCOMPARE( in.size(), (qint64)0 );
       
  2848         QVERIFY( in.isSequential() );
       
  2849     }
       
  2850 
       
  2851     {
       
  2852         QFile out;
       
  2853         out.open(STDOUT_FILENO, QIODevice::WriteOnly);
       
  2854         QCOMPARE( out.pos(), (qint64)0 );
       
  2855         QCOMPARE( out.size(), (qint64)0 );
       
  2856         QVERIFY( out.isSequential() );
       
  2857     }
       
  2858 
       
  2859     {
       
  2860         QFile err;
       
  2861         err.open(STDERR_FILENO, QIODevice::WriteOnly);
       
  2862         QCOMPARE( err.pos(), (qint64)0 );
       
  2863         QCOMPARE( err.size(), (qint64)0 );
       
  2864         QVERIFY( err.isSequential() );
       
  2865     }
       
  2866 }
       
  2867 
       
  2868 void tst_QFile::openStandardStreamsBufferedStreams()
       
  2869 {
       
  2870 #if defined (Q_OS_WIN) || defined(Q_OS_SYMBIAN)
       
  2871     QSKIP("Unix only test.", SkipAll);
       
  2872 #endif
       
  2873     // Using streams
       
  2874     {
       
  2875         QFile in;
       
  2876         in.open(stdin, QIODevice::ReadOnly);
       
  2877         QCOMPARE( in.pos(), (qint64)0 );
       
  2878         QCOMPARE( in.size(), (qint64)0 );
       
  2879         QVERIFY( in.isSequential() );
       
  2880     }
       
  2881 
       
  2882     {
       
  2883         QFile out;
       
  2884         out.open(stdout, QIODevice::WriteOnly);
       
  2885         QCOMPARE( out.pos(), (qint64)0 );
       
  2886         QCOMPARE( out.size(), (qint64)0 );
       
  2887         QVERIFY( out.isSequential() );
       
  2888     }
       
  2889 
       
  2890     {
       
  2891         QFile err;
       
  2892         err.open(stderr, QIODevice::WriteOnly);
       
  2893         QCOMPARE( err.pos(), (qint64)0 );
       
  2894         QCOMPARE( err.size(), (qint64)0 );
       
  2895         QVERIFY( err.isSequential() );
       
  2896     }
       
  2897 }
       
  2898 
       
  2899 void tst_QFile::openStandardStreams()
       
  2900 {
       
  2901     openStandardStreamsFileDescriptors();
       
  2902     openStandardStreamsBufferedStreams();
       
  2903 }
       
  2904 
       
  2905 void tst_QFile::writeNothing()
       
  2906 {
       
  2907     for (int i = 0; i < 3; ++i) {
       
  2908         QFile file("file.txt");
       
  2909         QVERIFY( openFile(file, QIODevice::WriteOnly | QIODevice::Unbuffered, FileType(i)) );
       
  2910         QVERIFY( 0 == file.write((char *)0, 0) );
       
  2911         QCOMPARE( file.error(), QFile::NoError );
       
  2912         closeFile(file);
       
  2913     }
  2635 }
  2914 }
  2636 
  2915 
  2637 QTEST_MAIN(tst_QFile)
  2916 QTEST_MAIN(tst_QFile)
  2638 #include "tst_qfile.moc"
  2917 #include "tst_qfile.moc"