Fix tests that assume system files are owned by root for qemu

If QEMU is provided sysroot with QEMU_LD_PREFIX, it opens files from there. If their
owner is the current user, testing their access rights based on assumption that they
are root fails. Skip the tests in that case similarly as is already done when the
tests are run as root.

This fixes following tests:
- tst_QTemporaryDir::nonWritableCurrentDir
- tst_QNetworkReply::getErrors(file-permissions)
- tst_qstandardpaths::testCustomRuntimeDirectory

Task-number: QTBUG-59966
Change-Id: I972ce37b4b5a7747cdd732a8e4a737ef09cbc6a5
Reviewed-by: Teemu Holappa <teemu.holappa@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Sami Nurmenniemi 2017-04-18 13:29:04 +03:00
parent 9b52cfd64c
commit a1e94bcfbb
6 changed files with 46 additions and 4 deletions

View File

@ -1,5 +1,7 @@
CONFIG += testcase
TARGET = tst_qstandardpaths
QT = core testlib
INCLUDEPATH += ../../../../shared/
HEADERS += ../../../../shared/emulationdetector.h
SOURCES = tst_qstandardpaths.cpp
TESTDATA += tst_qstandardpaths.cpp qstandardpaths.pro

View File

@ -46,6 +46,8 @@
#define Q_XDG_PLATFORM
#endif
#include "emulationdetector.h"
// Update this when adding new enum values; update enumNames too
static const int MaxStandardLocation = QStandardPaths::AppConfigLocation;
@ -485,14 +487,24 @@ void tst_qstandardpaths::testCustomRuntimeDirectory()
EnvVarRestorer restorer;
// When $XDG_RUNTIME_DIR points to a directory with wrong ownership, QStandardPaths should warn
qputenv("XDG_RUNTIME_DIR", QFile::encodeName("/tmp"));
QByteArray rootOwnedFileName = "/tmp";
if (EmulationDetector::isRunningArmOnX86()) {
// Directory "tmp" under toolchain sysroot is detected by qemu and has same uid as current user.
// Try /opt instead, it might not be located in the sysroot.
QFileInfo rootOwnedFile = QFileInfo(QString::fromLatin1(rootOwnedFileName));
if (rootOwnedFile.ownerId() == ::geteuid()) {
rootOwnedFileName = "/opt";
}
}
qputenv("XDG_RUNTIME_DIR", QFile::encodeName(rootOwnedFileName));
// It's very unlikely that /tmp is 0600 or that we can chmod it
// The call below outputs
// "QStandardPaths: wrong ownership on runtime directory /tmp, 0 instead of $UID"
// but we can't reliably expect that it's owned by uid 0, I think.
const uid_t uid = geteuid();
QTest::ignoreMessage(QtWarningMsg,
qPrintable(QString::fromLatin1("QStandardPaths: wrong ownership on runtime directory /tmp, 0 instead of %1").arg(uid)));
qPrintable(QString::fromLatin1("QStandardPaths: wrong ownership on runtime directory " + rootOwnedFileName + ", 0 instead of %1").arg(uid)));
const QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
QVERIFY2(runtimeDir.isEmpty(), qPrintable(runtimeDir));

View File

@ -1,5 +1,7 @@
CONFIG += testcase
TARGET = tst_qtemporarydir
SOURCES += tst_qtemporarydir.cpp
INCLUDEPATH += ../../../../shared/
HEADERS += ../../../../shared/emulationdetector.h
QT = core testlib

View File

@ -42,6 +42,7 @@
# include <sys/types.h>
# include <unistd.h>
#endif
#include "emulationdetector.h"
class tst_QTemporaryDir : public QObject
{
@ -316,6 +317,13 @@ void tst_QTemporaryDir::nonWritableCurrentDir()
const QFileInfo nonWritableDirFi = QFileInfo(QLatin1String(nonWritableDir));
QVERIFY(nonWritableDirFi.isDir());
if (EmulationDetector::isRunningArmOnX86()) {
if (nonWritableDirFi.ownerId() == ::geteuid()) {
QSKIP("Sysroot directories are owned by the current user");
}
}
QVERIFY(!nonWritableDirFi.isWritable());
ChdirOnReturn cor(QDir::currentPath());

View File

@ -1,6 +1,8 @@
CONFIG += testcase
testcase.timeout = 600 # this test is slow
CONFIG -= debug_and_release_target
INCLUDEPATH += ../../../../../shared/
HEADERS += ../../../../../shared/emulationdetector.h
SOURCES += ../tst_qnetworkreply.cpp
TARGET = ../tst_qnetworkreply

View File

@ -96,6 +96,8 @@ Q_DECLARE_METATYPE(QAuthenticator*)
Q_DECLARE_METATYPE(QNetworkProxyQuery)
#endif
#include "emulationdetector.h"
typedef QSharedPointer<QNetworkReply> QNetworkReplyPtr;
class MyCookieJar;
@ -135,6 +137,7 @@ class tst_QNetworkReply: public QObject
}
static const QByteArray httpEmpty200Response;
static const QString filePermissionFileName;
QEventLoop *loop;
enum RunSimpleRequestReturn { Timeout = 0, Success, Failure };
@ -499,6 +502,8 @@ private:
const QByteArray tst_QNetworkReply::httpEmpty200Response =
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
const QString tst_QNetworkReply::filePermissionFileName = "/etc/shadow";
bool tst_QNetworkReply::seedCreated = false;
#define RUN_REQUEST(call) \
@ -1915,8 +1920,10 @@ void tst_QNetworkReply::getErrors_data()
QTest::newRow("file-is-wronly") << QUrl::fromLocalFile(wronlyFileName).toString()
<< int(QNetworkReply::ContentAccessDenied) << 0 << true;
#endif
if (QFile::exists("/etc/shadow"))
QTest::newRow("file-permissions") << "file:/etc/shadow"
if (QFile::exists(filePermissionFileName))
QTest::newRow("file-permissions") << "file:" + filePermissionFileName
<< int(QNetworkReply::ContentAccessDenied) << 0 << true;
// ftp: errors
@ -1952,6 +1959,15 @@ void tst_QNetworkReply::getErrors()
(qstrcmp(QTest::currentDataTag(), "file-permissions") == 0)) {
if (::getuid() == 0)
QSKIP("Running this test as root doesn't make sense");
}
if (EmulationDetector::isRunningArmOnX86()
&& qstrcmp(QTest::currentDataTag(), "file-permissions") == 0) {
QFileInfo filePermissionFile = QFileInfo(filePermissionFileName.toLatin1());
if (filePermissionFile.ownerId() == ::geteuid()) {
QSKIP("Sysroot directories are owned by the current user");
}
}
#endif