From a1e94bcfbb7b2814340f481b632ebab224eddda5 Mon Sep 17 00:00:00 2001 From: Sami Nurmenniemi Date: Tue, 18 Apr 2017 13:29:04 +0300 Subject: [PATCH] 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 Reviewed-by: Thiago Macieira --- .../io/qstandardpaths/qstandardpaths.pro | 2 ++ .../io/qstandardpaths/tst_qstandardpaths.cpp | 16 +++++++++++++-- .../io/qtemporarydir/qtemporarydir.pro | 2 ++ .../io/qtemporarydir/tst_qtemporarydir.cpp | 8 ++++++++ .../access/qnetworkreply/test/test.pro | 2 ++ .../qnetworkreply/tst_qnetworkreply.cpp | 20 +++++++++++++++++-- 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/tests/auto/corelib/io/qstandardpaths/qstandardpaths.pro b/tests/auto/corelib/io/qstandardpaths/qstandardpaths.pro index c72d9e4fad..9fd7047405 100644 --- a/tests/auto/corelib/io/qstandardpaths/qstandardpaths.pro +++ b/tests/auto/corelib/io/qstandardpaths/qstandardpaths.pro @@ -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 diff --git a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp index 0a00e00d83..3de777653e 100644 --- a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp +++ b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp @@ -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)); diff --git a/tests/auto/corelib/io/qtemporarydir/qtemporarydir.pro b/tests/auto/corelib/io/qtemporarydir/qtemporarydir.pro index 4a69971f78..351e263093 100644 --- a/tests/auto/corelib/io/qtemporarydir/qtemporarydir.pro +++ b/tests/auto/corelib/io/qtemporarydir/qtemporarydir.pro @@ -1,5 +1,7 @@ CONFIG += testcase TARGET = tst_qtemporarydir SOURCES += tst_qtemporarydir.cpp +INCLUDEPATH += ../../../../shared/ +HEADERS += ../../../../shared/emulationdetector.h QT = core testlib diff --git a/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp b/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp index 758bbead84..4bed8d0fd6 100644 --- a/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp +++ b/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp @@ -42,6 +42,7 @@ # include # include #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()); diff --git a/tests/auto/network/access/qnetworkreply/test/test.pro b/tests/auto/network/access/qnetworkreply/test/test.pro index 8aeec88fd2..0dcf5a250c 100644 --- a/tests/auto/network/access/qnetworkreply/test/test.pro +++ b/tests/auto/network/access/qnetworkreply/test/test.pro @@ -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 diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index fbd8f5a780..855b1f9041 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -96,6 +96,8 @@ Q_DECLARE_METATYPE(QAuthenticator*) Q_DECLARE_METATYPE(QNetworkProxyQuery) #endif +#include "emulationdetector.h" + typedef QSharedPointer 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