From c68f8db62b3492074a73e754d4668c2d693cdafa Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Fri, 22 Mar 2024 18:06:01 +0100 Subject: [PATCH] Tests: make some QFile::open calls fail In some cases calls to QFile::open are made outside of a testfunction (so we can't use QVERIFY), or even in standalone executables that are executed by tests. Make them fail "visibly". Change-Id: Iec4d56f6d874be16aa2e9ad6974eeec2a98caa3f Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira --- tests/auto/corelib/io/qfile/stdinprocess/main.cpp | 9 ++++++--- .../auto/corelib/io/qprocess/fileWriterProcess/main.cpp | 3 ++- .../corelib/serialization/qxmlstream/tst_qxmlstream.cpp | 7 ++++--- .../kernel/qfileopenevent/test/tst_qfileopenevent.cpp | 3 ++- tests/auto/network/access/http2/http2srv.cpp | 3 ++- tests/auto/network/access/qnetworkreply/echo/main.cpp | 6 ++++-- .../network/access/qnetworkreply/tst_qnetworkreply.cpp | 4 ++++ 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/tests/auto/corelib/io/qfile/stdinprocess/main.cpp b/tests/auto/corelib/io/qfile/stdinprocess/main.cpp index dbcd00bb0ab..0f92ba26708 100644 --- a/tests/auto/corelib/io/qfile/stdinprocess/main.cpp +++ b/tests/auto/corelib/io/qfile/stdinprocess/main.cpp @@ -16,13 +16,16 @@ int main(int argc, char *argv[]) QFile file; if (strcmp(argv[1], "all") == 0) { - file.open(stdin, QFile::ReadWrite); + if (!file.open(stdin, QFile::ReadWrite)) + return 1; printf("%s", file.readAll().constData()); } else if (strcmp(argv[1], "line") == 0) { if (strcmp(argv[2], "0") == 0) { - file.open(stdin, QFile::ReadWrite); + if (!file.open(stdin, QFile::ReadWrite)) + return 1; } else { - file.open(0, QFile::ReadWrite); + if (!file.open(0, QFile::ReadWrite)) + return 1; } char line[1024]; diff --git a/tests/auto/corelib/io/qprocess/fileWriterProcess/main.cpp b/tests/auto/corelib/io/qprocess/fileWriterProcess/main.cpp index 43b3f771886..0349a480671 100644 --- a/tests/auto/corelib/io/qprocess/fileWriterProcess/main.cpp +++ b/tests/auto/corelib/io/qprocess/fileWriterProcess/main.cpp @@ -9,7 +9,8 @@ int main(int argc, char **argv) { QCoreApplication ca(argc, argv); QFile f; - f.open(stdin, QIODevice::ReadOnly); + if (!f.open(stdin, QIODevice::ReadOnly)) + return 1; QByteArray input; char buf[1024]; qint64 len; diff --git a/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp b/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp index 0b7d9ab0ede..436ff676f61 100644 --- a/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp +++ b/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp @@ -95,8 +95,8 @@ static QByteArray makeCanonical(const QString &filename, bool testIncremental = false) { QFile file(filename); - file.open(QIODevice::ReadOnly); - + if (!file.open(QIODevice::ReadOnly)) + qFatal("Could not open file %s", qPrintable(filename)); QXmlStreamReader reader; QByteArray buffer; @@ -747,7 +747,8 @@ void tst_QXmlStream::reportSuccess_data() const QByteArray tst_QXmlStream::readFile(const QString &filename) { QFile file(filename); - file.open(QIODevice::ReadOnly); + if (!file.open(QIODevice::ReadOnly)) + qFatal("Could not open file %s", qPrintable(filename)); QXmlStreamReader reader; diff --git a/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp b/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp index 823f7c23edd..4b9a23ffcf9 100644 --- a/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp +++ b/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp @@ -79,7 +79,8 @@ void tst_qfileopenevent::constructor() QByteArray tst_qfileopenevent::readFileContent(QFileOpenEvent& event) { QFile file(event.file()); - file.open(QFile::ReadOnly); + if (!file.open(QFile::ReadOnly)) + qFatal("Cannot open file %s", qPrintable(event.file())); file.seek(0); QByteArray data = file.readAll(); return data; diff --git a/tests/auto/network/access/http2/http2srv.cpp b/tests/auto/network/access/http2/http2srv.cpp index 7c5ccc8ff50..b52ea5527bd 100644 --- a/tests/auto/network/access/http2/http2srv.cpp +++ b/tests/auto/network/access/http2/http2srv.cpp @@ -328,7 +328,8 @@ void Http2Server::incomingConnection(qintptr socketDescriptor) connect(sslSocket, SIGNAL(sslErrors(QList)), this, SLOT(ignoreErrorSlot())); QFile file(QT_TESTCASE_SOURCEDIR "/certs/fluke.key"); - file.open(QIODevice::ReadOnly); + if (!file.open(QIODevice::ReadOnly)) + qFatal("Cannot open certificate file %s", qPrintable(file.fileName())); QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); sslSocket->setPrivateKey(key); auto localCert = QSslCertificate::fromPath(QT_TESTCASE_SOURCEDIR "/certs/fluke.cert"); diff --git a/tests/auto/network/access/qnetworkreply/echo/main.cpp b/tests/auto/network/access/qnetworkreply/echo/main.cpp index d03e44a90c4..b10eaa745c3 100644 --- a/tests/auto/network/access/qnetworkreply/echo/main.cpp +++ b/tests/auto/network/access/qnetworkreply/echo/main.cpp @@ -12,11 +12,13 @@ int main(int argc, char **) } QFile file; - file.open(stdin, QFile::ReadWrite); + if (!file.open(stdin, QFile::ReadWrite)) + return 1; QByteArray data = file.readAll(); file.close(); - file.open(stdout, QFile::WriteOnly); + if (!file.open(stdout, QFile::WriteOnly)) + return 1; file.write(data); file.close(); return 0; diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 5a6fe9f6ae7..a4a05b18f5f 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -5088,6 +5088,10 @@ void tst_QNetworkReply::ioPutToFileFromProcess() QByteArray contents = file.readAll(); QCOMPARE(contents, data); + if (process.state() == QProcess::Running) + QVERIFY(process.waitForFinished()); + QCOMPARE(process.exitCode(), 0); + #endif // QT_CONFIG(process) }