tst_QFileInfo: fix running with systems without /etc/passwd

Clear Linux containers running as root may have no /etc/passwd. But
they'll have /etc/machine-id because systemd creates that. Also test
/proc/version (a Linux-specific file) because that isn't writeable even
by root.

Take the opportunity to check with access() instead of assuming root and
only root can write to the file.

Change-Id: Ibdc95e9af7bd456a94ecfffd1603e8359604752b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Thiago Macieira 2020-04-08 14:26:50 -03:00
parent f0ec2eb151
commit 7cd2d2b751

View File

@ -1887,10 +1887,11 @@ void tst_QFileInfo::isWritable()
#if defined (Q_OS_QNX) // On QNX /etc is usually on a read-only filesystem
QVERIFY(!QFileInfo("/etc/passwd").isWritable());
#elif defined (Q_OS_UNIX) && !defined(Q_OS_VXWORKS) // VxWorks does not have users/groups
if (::getuid() == 0)
QVERIFY(QFileInfo("/etc/passwd").isWritable());
else
QVERIFY(!QFileInfo("/etc/passwd").isWritable());
for (const char *attempt : { "/etc/passwd", "/etc/machine-id", "/proc/version" }) {
if (access(attempt, F_OK) == -1)
continue;
QCOMPARE(QFileInfo(attempt).isWritable(), ::access(attempt, W_OK) == 0);
}
#endif
}