wasm: Add auto test for qfile

Problems:
* mmap of size 0 works, add test in QFSFileEnginePrivate::map to
  make sure wasm platform behaves as the other platforms
* qwe// is a valid filename on wasm, QSKIP test
* applicationFilePath does not exist on wasm.
* /dev/zero does not exist on wasm, file bug, skip test
* blocking pipes do not exist on wasm, QSKIP test
* socketpair does not exist on wasm, QSKIP test

Change-Id: I1705c543782cbcb92a7ebed2fc2792613524c686
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Even Oscar Andersen 2024-02-22 09:52:27 +01:00
parent 3e049f0655
commit c0fdd4b451
3 changed files with 42 additions and 21 deletions

View File

@ -565,11 +565,11 @@ uchar *QFSFileEnginePrivate::map(qint64 offset, qint64 size, QFile::MemoryMapFla
} }
if (offset < 0 || offset > maxFileOffset if (offset < 0 || offset > maxFileOffset
|| size < 0 || quint64(size) > quint64(size_t(-1))) { || size <= 0
|| quint64(size) > quint64(size_t(-1))) {
q->setError(QFile::UnspecifiedError, qt_error_string(EINVAL)); q->setError(QFile::UnspecifiedError, qt_error_string(EINVAL));
return nullptr; return nullptr;
} }
// If we know the mapping will extend beyond EOF, fail early to avoid // If we know the mapping will extend beyond EOF, fail early to avoid
// undefined behavior. Otherwise, let mmap have its say. // undefined behavior. Otherwise, let mmap have its say.
if (doStat(QFileSystemMetaData::SizeAttribute) if (doStat(QFileSystemMetaData::SizeAttribute)

View File

@ -63,6 +63,7 @@ if(WASM)
add_subdirectory(corelib/io/qbuffer) add_subdirectory(corelib/io/qbuffer)
add_subdirectory(corelib/io/qabstractfileengine) add_subdirectory(corelib/io/qabstractfileengine)
add_subdirectory(corelib/io/qsettings) add_subdirectory(corelib/io/qsettings)
add_subdirectory(corelib/io/qfile)
add_subdirectory(corelib/serialization) add_subdirectory(corelib/serialization)
add_subdirectory(corelib/text) add_subdirectory(corelib/text)
add_subdirectory(corelib/thread) add_subdirectory(corelib/thread)

View File

@ -202,6 +202,7 @@ private slots:
void flush(); void flush();
void bufferedRead(); void bufferedRead();
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
void isSequential_data();
void isSequential(); void isSequential();
#endif #endif
void decodeName_data(); void decodeName_data();
@ -232,7 +233,7 @@ private slots:
void virtualFile_data(); void virtualFile_data();
void virtualFile(); void virtualFile();
#endif #endif
#ifdef Q_OS_UNIX #if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
void unixPipe_data(); void unixPipe_data();
void unixPipe(); void unixPipe();
void unixFifo_data() { unixPipe_data(); } void unixFifo_data() { unixPipe_data(); }
@ -645,7 +646,7 @@ void tst_QFile::open()
QFETCH( bool, ok ); QFETCH( bool, ok );
#if defined(Q_OS_UNIX) && !defined(Q_OS_VXWORKS) #if defined(Q_OS_UNIX) && !defined(Q_OS_VXWORKS) && !defined(Q_OS_WASM)
if (::getuid() == 0) if (::getuid() == 0)
// root and Chuck Norris don't care for file permissions. Skip. // root and Chuck Norris don't care for file permissions. Skip.
QSKIP("Running this test as root doesn't make sense"); QSKIP("Running this test as root doesn't make sense");
@ -1219,6 +1220,11 @@ static inline QChar invalidDriveLetter()
void tst_QFile::invalidFile_data() void tst_QFile::invalidFile_data()
{ {
QTest::addColumn<QString>("fileName"); QTest::addColumn<QString>("fileName");
#if defined(Q_OS_WASM)
QSKIP("No invalid files on wasm");
#endif
#if !defined(Q_OS_WIN) #if !defined(Q_OS_WIN)
QTest::newRow( "x11" ) << QString( "qwe//" ); QTest::newRow( "x11" ) << QString( "qwe//" );
#else #else
@ -1233,7 +1239,6 @@ void tst_QFile::invalidFile_data()
QTest::newRow( "pipe" ) << QString( "fail|invalid" ); QTest::newRow( "pipe" ) << QString( "fail|invalid" );
#endif #endif
} }
void tst_QFile::invalidFile() void tst_QFile::invalidFile()
{ {
QFETCH( QString, fileName ); QFETCH( QString, fileName );
@ -1376,7 +1381,10 @@ void tst_QFile::permissions_data()
QTest::addColumn<bool>("expected"); QTest::addColumn<bool>("expected");
QTest::addColumn<bool>("create"); QTest::addColumn<bool>("create");
#ifndef Q_OS_WASM
// Application path is empty on wasm
QTest::newRow("data0") << QCoreApplication::instance()->applicationFilePath() << uint(QFile::ExeUser) << true << false; QTest::newRow("data0") << QCoreApplication::instance()->applicationFilePath() << uint(QFile::ExeUser) << true << false;
#endif
QTest::newRow("data1") << m_testSourceFile << uint(QFile::ReadUser) << true << false; QTest::newRow("data1") << m_testSourceFile << uint(QFile::ReadUser) << true << false;
QTest::newRow("readonly") << QString::fromLatin1("readonlyfile") << uint(QFile::WriteUser) << false << false; QTest::newRow("readonly") << QString::fromLatin1("readonlyfile") << uint(QFile::WriteUser) << false << false;
QTest::newRow("longfile") << QString::fromLatin1("longFileNamelongFileNamelongFileNamelongFileName" QTest::newRow("longfile") << QString::fromLatin1("longFileNamelongFileNamelongFileNamelongFileName"
@ -1407,7 +1415,7 @@ void tst_QFile::permissions()
QFile::Permissions staticResult = QFile::permissions(file) & perms; QFile::Permissions staticResult = QFile::permissions(file) & perms;
if (create) { if (create) {
QFile::remove(file); QVERIFY(QFile::remove(file));
} }
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
@ -1911,20 +1919,26 @@ void tst_QFile::bufferedRead()
} }
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
void tst_QFile::isSequential_data()
{
QTest::addColumn<QString>("deviceName");
QTest::addColumn<bool>("acceptFailOpen");
QTest::newRow("/dev/null") << QString("/dev/null") << false;
QTest::newRow("/dev/tty") << QString("/dev/tty") << true;
QTest::newRow("/dev/zero") << QString("/dev/zero") << false;
}
void tst_QFile::isSequential() void tst_QFile::isSequential()
{ {
QFile zero("/dev/zero"); QFETCH(QString, deviceName);
QVERIFY2(zero.open(QFile::ReadOnly), msgOpenFailed(zero).constData()); QFETCH(bool, acceptFailOpen);
QVERIFY(zero.isSequential());
QFile null("/dev/null"); if (access(deviceName.toUtf8().data(), R_OK) == 0) {
QVERIFY(null.open(QFile::ReadOnly)); QFile device(deviceName);
QVERIFY(null.isSequential()); QVERIFY2(device.open(QFile::ReadOnly) || acceptFailOpen, msgOpenFailed(device).constData());
QVERIFY(!device.isOpen() || device.isSequential());
// /dev/tty will fail to open if we don't have a controlling TTY }
QFile tty("/dev/tty");
if (tty.open(QFile::ReadOnly))
QVERIFY(tty.isSequential());
} }
#endif #endif
@ -2720,7 +2734,13 @@ void tst_QFile::virtualFile()
} }
#endif // defined(Q_OS_LINUX) || defined(Q_OS_AIX) || defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) #endif // defined(Q_OS_LINUX) || defined(Q_OS_AIX) || defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
#ifdef Q_OS_UNIX #if defined (Q_OS_UNIX) && !defined(Q_OS_WASM)
// wasm does not have working fifo
// https://github.com/nodejs/node/issues/38344
// wasm does not have blocking pipe I/O
// https://github.com/emscripten-core/emscripten/issues/13214
// wasm does not, by default, have socketpair
// https://emscripten.org/docs/porting/networking.html
static void unixPipe_helper(int pipes[2]) static void unixPipe_helper(int pipes[2])
{ {
// start a thread and wait for it to write a first byte // start a thread and wait for it to write a first byte
@ -2839,7 +2859,7 @@ void tst_QFile::socketPair()
qt_safe_close(pipes[1]); qt_safe_close(pipes[1]);
#endif #endif
} }
#endif #endif /* UNIX && !WASM; */
void tst_QFile::textFile() void tst_QFile::textFile()
{ {
@ -3949,7 +3969,7 @@ void tst_QFile::moveToTrash_data()
if (!temp.open()) if (!temp.open())
QSKIP("Failed to create temporary file!"); QSKIP("Failed to create temporary file!");
QTest::newRow("temporary file") << temp.fileName() << true << true; QTest::newRow("temporary file") << temp.fileName() << true << true;
#ifdef Q_OS_UNIX #if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
if (QDir::tempPath() == "/tmp") if (QDir::tempPath() == "/tmp")
QTest::newRow("var-temporary file") << "/var" + temp.fileName() << true << true; QTest::newRow("var-temporary file") << "/var" + temp.fileName() << true << true;
#endif #endif
@ -3962,7 +3982,7 @@ void tst_QFile::moveToTrash_data()
QTest::newRow("temporary dir") QTest::newRow("temporary dir")
<< tempDir.path() + QLatin1Char('/') << tempDir.path() + QLatin1Char('/')
<< true << true; << true << true;
#ifdef Q_OS_UNIX #if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
if (QDir::tempPath() == "/tmp") if (QDir::tempPath() == "/tmp")
QTest::newRow("var-temporary dir") << "/var" + tempDir.path() << true << true; QTest::newRow("var-temporary dir") << "/var" + tempDir.path() << true << true;
#endif #endif