tst_qstandardpaths: check the return value of QFile::open

If QFile::open() fails in any of the these helper functions, the test
should fail.

Task-number: QTBUG-123623
Change-Id: I3e4d65eccd3be32eed673d9607ef468ddc0fd6e5
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Ahmad Samir 2024-03-30 14:53:17 +02:00
parent c0fdd4b451
commit 60f15da3ca

View File

@ -533,7 +533,7 @@ void tst_qstandardpaths::testFindExecutableLinkToDirectory()
QFile::remove(target); QFile::remove(target);
} }
using RuntimeDirSetup = QString (*)(QDir &); using RuntimeDirSetup = std::optional<QString> (*)(QDir &);
Q_DECLARE_METATYPE(RuntimeDirSetup); Q_DECLARE_METATYPE(RuntimeDirSetup);
void tst_qstandardpaths::testRuntimeDirectory() void tst_qstandardpaths::testRuntimeDirectory()
@ -593,18 +593,18 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
QSKIP("Running this test as root doesn't make sense"); QSKIP("Running this test as root doesn't make sense");
# endif # endif
addRow("environment:non-existing", [](QDir &d) { addRow("environment:non-existing", [](QDir &d) -> std::optional<QString> {
return updateRuntimeDir(d.filePath("runtime")); return updateRuntimeDir(d.filePath("runtime"));
}); });
addRow("environment:existing", [](QDir &d) { addRow("environment:existing", [](QDir &d) -> std::optional<QString> {
QString p = d.filePath("runtime"); QString p = d.filePath("runtime");
d.mkdir("runtime"); d.mkdir("runtime");
QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner);
return updateRuntimeDir(p); return updateRuntimeDir(p);
}); });
addRow("environment-to-existing-wrong-perm", [](QDir &d) { addRow("environment-to-existing-wrong-perm", [](QDir &d) -> std::optional<QString> {
QString p = d.filePath("runtime"); QString p = d.filePath("runtime");
d.mkdir("runtime"); d.mkdir("runtime");
QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
@ -617,7 +617,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return fallbackXdgRuntimeDir(); return fallbackXdgRuntimeDir();
}); });
addRow("environment:wrong-owner", [](QDir &) { addRow("environment:wrong-owner", [](QDir &) -> std::optional<QString> {
QT_STATBUF st; QT_STATBUF st;
QT_STAT("/", &st); QT_STAT("/", &st);
@ -632,10 +632,18 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return fallbackXdgRuntimeDir(); return fallbackXdgRuntimeDir();
}); });
addRow("environment:file", [](QDir &d) { // static so that it can be used in RuntimeDirSetup callable without capturing
static auto failedToOpen = [](const QFile &f) {
qCritical("QFile::Open: failed to open '%s': %s",
qPrintable(f.fileName()), qPrintable(f.errorString()));
return std::nullopt;
};
addRow("environment:file", [](QDir &d) -> std::optional<QString> {
QString p = d.filePath("file"); QString p = d.filePath("file");
QFile f(p); QFile f(p);
f.open(QIODevice::WriteOnly); if (!f.open(QIODevice::WriteOnly))
return failedToOpen(f);
f.setPermissions(QFile::ReadOwner | QFile::WriteOwner); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
updateRuntimeDir(p); updateRuntimeDir(p);
@ -646,7 +654,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return fallbackXdgRuntimeDir(); return fallbackXdgRuntimeDir();
}); });
addRow("environment:broken-symlink", [](QDir &d) { addRow("environment:broken-symlink", [](QDir &d) -> std::optional<QString> {
QString p = d.filePath("link"); QString p = d.filePath("link");
QFile::link(d.filePath("this-goes-nowhere"), p); QFile::link(d.filePath("this-goes-nowhere"), p);
updateRuntimeDir(p); updateRuntimeDir(p);
@ -657,7 +665,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return fallbackXdgRuntimeDir(); return fallbackXdgRuntimeDir();
}); });
addRow("environment:symlink-to-dir", [](QDir &d) { addRow("environment:symlink-to-dir", [](QDir &d) -> std::optional<QString> {
QString p = d.filePath("link"); QString p = d.filePath("link");
d.mkdir("dir"); d.mkdir("dir");
QFile::link(d.filePath("dir"), p); QFile::link(d.filePath("dir"), p);
@ -670,12 +678,12 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return fallbackXdgRuntimeDir(); return fallbackXdgRuntimeDir();
}); });
addRow("no-environment:non-existing", [](QDir &) { addRow("no-environment:non-existing", [](QDir &) -> std::optional<QString> {
clearRuntimeDir(); clearRuntimeDir();
return fallbackXdgRuntimeDir(); return fallbackXdgRuntimeDir();
}); });
addRow("no-environment:existing", [](QDir &d) { addRow("no-environment:existing", [](QDir &d) -> std::optional<QString> {
clearRuntimeDir(); clearRuntimeDir();
QString p = fallbackXdgRuntimeDir(); QString p = fallbackXdgRuntimeDir();
d.mkdir(p); // probably has wrong permissions d.mkdir(p); // probably has wrong permissions
@ -683,10 +691,11 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return p; return p;
}); });
addRow("no-environment:fallback-is-file", [](QDir &) { addRow("no-environment:fallback-is-file", [](QDir &) -> std::optional<QString> {
QString p = fallbackXdgRuntimeDir(); QString p = fallbackXdgRuntimeDir();
QFile f(p); QFile f(p);
f.open(QIODevice::WriteOnly); if (!f.open(QIODevice::WriteOnly))
return failedToOpen(f);
f.setPermissions(QFile::ReadOwner | QFile::WriteOwner); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
clearRuntimeDir(); clearRuntimeDir();
@ -697,10 +706,11 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
return QString(); return QString();
}); });
addRow("environment-and-fallback-are-files", [](QDir &d) { addRow("environment-and-fallback-are-files", [](QDir &d) -> std::optional<QString> {
QString p = d.filePath("file1"); QString p = d.filePath("file1");
QFile f(p); QFile f(p);
f.open(QIODevice::WriteOnly); if (!f.open(QIODevice::WriteOnly))
return failedToOpen(f);
f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup);
updateRuntimeDir(p); updateRuntimeDir(p);
QTest::ignoreMessage(QtWarningMsg, QTest::ignoreMessage(QtWarningMsg,
@ -710,7 +720,8 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data()
f.close(); f.close();
f.setFileName(fallbackXdgRuntimeDir()); f.setFileName(fallbackXdgRuntimeDir());
f.open(QIODevice::WriteOnly); if (!f.open(QIODevice::WriteOnly))
return failedToOpen(f);
f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup);
QTest::ignoreMessage(QtWarningMsg, QTest::ignoreMessage(QtWarningMsg,
QString("QStandardPaths: runtime directory '%1' is not a directory, " QString("QStandardPaths: runtime directory '%1' is not a directory, "
@ -750,7 +761,9 @@ void tst_qstandardpaths::testCustomRuntimeDirectory()
qputenv("TMPDIR", QFile::encodeName(tempDir.path())); qputenv("TMPDIR", QFile::encodeName(tempDir.path()));
QFETCH(RuntimeDirSetup, setup); QFETCH(RuntimeDirSetup, setup);
QString expected = setup(d); std::optional<QString> opt = setup(d);
QVERIFY(opt);
QString expected = *opt;
QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation); QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
QCOMPARE(runtimeDir, expected); QCOMPARE(runtimeDir, expected);