Check for administrative rights in tst_qsettings.

Introduce function checking whether the system settings in
native format can be written to. If not, default to user scope or
skip the respective tests instead of failing.

Task-number: QTBUG-37822
Change-Id: I330aff9b79bb22254216f022af807e010bc8e8ba
Reviewed-by: Andrew Knight <andrew.knight@digia.com>
This commit is contained in:
Friedemann Kleint 2014-03-26 09:17:27 +01:00 committed by The Qt Project
parent aa2980d36d
commit 9a9feab102

View File

@ -73,10 +73,30 @@ Q_DECLARE_METATYPE(QSettings::Format)
QT_FORWARD_DECLARE_CLASS(QSettings)
static inline bool canWriteNativeSystemSettings()
{
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
HKEY key;
const LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software", 0, KEY_WRITE, &key);
if (result == ERROR_SUCCESS)
RegCloseKey(key);
else
qErrnoWarning(result, "RegOpenKeyEx failed");
return result == ERROR_SUCCESS;
#else // Q_OS_WIN && !Q_OS_WINRT
return true;
#endif
}
static const char insufficientPermissionSkipMessage[] = "Insufficient permissions for this test.";
class tst_QSettings : public QObject
{
Q_OBJECT
public:
tst_QSettings() : m_canWriteNativeSystemSettings(canWriteNativeSystemSettings()) {}
public slots:
void initTestCase();
void init();
@ -145,6 +165,9 @@ private slots:
void testByteArray_data();
void testByteArray();
private:
const bool m_canWriteNativeSystemSettings;
};
// Testing get/set functions
@ -259,6 +282,8 @@ static void populateWithFormats()
void tst_QSettings::initTestCase()
{
if (!m_canWriteNativeSystemSettings)
qWarning("The test is not running with administrative rights. Some tests will be skipped.");
QSettings::Format custom1 = QSettings::registerFormat("custom1", readCustom1File, writeCustom1File);
QSettings::Format custom2 = QSettings::registerFormat("custom2", readCustom2File, writeCustom2File
#ifndef QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER
@ -276,17 +301,19 @@ void tst_QSettings::init()
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
QSettings("HKEY_CURRENT_USER\\Software\\software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_CURRENT_USER\\Software\\other.software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\other.software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_CURRENT_USER\\Software\\foo", QSettings::NativeFormat).clear();
QSettings("HKEY_CURRENT_USER\\Software\\bar", QSettings::NativeFormat).clear();
QSettings("HKEY_CURRENT_USER\\Software\\bat", QSettings::NativeFormat).clear();
QSettings("HKEY_CURRENT_USER\\Software\\baz", QSettings::NativeFormat).clear();
if (m_canWriteNativeSystemSettings) {
QSettings("HKEY_LOCAL_MACHINE\\Software\\software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\other.software.org", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\foo", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\bar", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\bat", QSettings::NativeFormat).clear();
QSettings("HKEY_LOCAL_MACHINE\\Software\\baz", QSettings::NativeFormat).clear();
}
if (QDir(settingsPath()).exists()) {
#if defined(Q_OS_WINCE)
removePath(settingsPath());
@ -334,6 +361,9 @@ void tst_QSettings::ctor()
{
QFETCH(QSettings::Format, format);
if (!m_canWriteNativeSystemSettings && format == QSettings::NativeFormat)
QSKIP(insufficientPermissionSkipMessage);
{
QSettings settings1(format, QSettings::UserScope, "software.org", "KillerAPP");
QSettings settings2(format, QSettings::UserScope, "software.org");
@ -1263,43 +1293,55 @@ void tst_QSettings::remove()
QSettings settings1(QSettings::UserScope, "software.org", "KillerAPP");
QSettings settings2(QSettings::UserScope, "software.org");
QSettings settings3(QSettings::SystemScope, "software.org", "KillerAPP");
QSettings settings4(QSettings::SystemScope, "software.org");
settings4.setValue("key 1", "doodah");
settings3.setValue("key 1", "blah");
QScopedPointer<QSettings> settings3;
QScopedPointer<QSettings> settings4;
if (m_canWriteNativeSystemSettings) {
settings3.reset(new QSettings(QSettings::SystemScope, "software.org", "KillerAPP"));
settings4.reset(new QSettings(QSettings::SystemScope, "software.org"));
settings3->setValue("key 1", "blah");
settings4->setValue("key 1", "doodah");
}
settings2.setValue("key 1", "whoa");
settings1.setValue("key 1", "gurgle");
QCOMPARE(settings1.value("key 1").toString(), QString("gurgle"));
QCOMPARE(settings2.value("key 1").toString(), QString("whoa"));
#if !defined(Q_OS_BLACKBERRY)
QCOMPARE(settings3.value("key 1").toString(), QString("blah"));
QCOMPARE(settings4.value("key 1").toString(), QString("doodah"));
if (m_canWriteNativeSystemSettings) {
QCOMPARE(settings3->value("key 1").toString(), QString("blah"));
QCOMPARE(settings4->value("key 1").toString(), QString("doodah"));
}
settings1.remove("key 1");
QCOMPARE(settings1.value("key 1").toString(), QString("whoa"));
QCOMPARE(settings2.value("key 1").toString(), QString("whoa"));
QCOMPARE(settings3.value("key 1").toString(), QString("blah"));
QCOMPARE(settings4.value("key 1").toString(), QString("doodah"));
if (m_canWriteNativeSystemSettings) {
QCOMPARE(settings3->value("key 1").toString(), QString("blah"));
QCOMPARE(settings4->value("key 1").toString(), QString("doodah"));
}
if (m_canWriteNativeSystemSettings) {
settings2.remove("key 1");
QCOMPARE(settings1.value("key 1").toString(), QString("blah"));
QCOMPARE(settings2.value("key 1").toString(), QString("doodah"));
QCOMPARE(settings3.value("key 1").toString(), QString("blah"));
QCOMPARE(settings4.value("key 1").toString(), QString("doodah"));
QCOMPARE(settings3->value("key 1").toString(), QString("blah"));
QCOMPARE(settings4->value("key 1").toString(), QString("doodah"));
settings3.remove("key 1");
settings3->remove("key 1");
QCOMPARE(settings1.value("key 1").toString(), QString("doodah"));
QCOMPARE(settings2.value("key 1").toString(), QString("doodah"));
QCOMPARE(settings3.value("key 1").toString(), QString("doodah"));
QCOMPARE(settings4.value("key 1").toString(), QString("doodah"));
QCOMPARE(settings3->value("key 1").toString(), QString("doodah"));
QCOMPARE(settings4->value("key 1").toString(), QString("doodah"));
settings4.remove("key 1");
settings4->remove("key 1");
QVERIFY(!settings1.contains("key 1"));
QVERIFY(!settings2.contains("key 1"));
QVERIFY(!settings3.contains("key 1"));
QVERIFY(!settings4.contains("key 1"));
QVERIFY(!settings3->contains("key 1"));
QVERIFY(!settings4->contains("key 1"));
}
#else
settings1.remove("key 1");
QCOMPARE(settings2.value("key 1").toString(), QString("whoa"));
@ -1315,13 +1357,18 @@ void tst_QSettings::remove()
settings1.clear();
settings2.clear();
settings3.clear();
settings4.clear();
if (m_canWriteNativeSystemSettings) {
settings3->clear();
settings4->clear();
}
settings1.sync();
settings2.sync();
settings3.sync();
settings4.sync();
if (m_canWriteNativeSystemSettings) {
settings3->sync();
settings4->sync();
}
/*
Check that recursive removes work correctly when some of the
@ -1556,6 +1603,9 @@ void tst_QSettings::setFallbacksEnabled()
{
QFETCH(QSettings::Format, format);
if (!m_canWriteNativeSystemSettings && format == QSettings::NativeFormat)
QSKIP(insufficientPermissionSkipMessage);
QSettings settings1(format, QSettings::UserScope, "software.org", "KillerAPP");
QSettings settings2(format, QSettings::UserScope, "software.org");
QSettings settings3(format, QSettings::SystemScope, "software.org", "KillerAPP");
@ -2771,8 +2821,10 @@ void tst_QSettings::childGroups()
{
QFETCH(QSettings::Format, format);
const QSettings::Scope scope = m_canWriteNativeSystemSettings ? QSettings::SystemScope : QSettings::UserScope;
{
QSettings settings(format, QSettings::SystemScope, "software.org");
QSettings settings(format, scope, "software.org");
settings.setValue("alpha", "1");
settings.setValue("alpha/a", "2");
settings.setValue("alpha/b", "3");
@ -2789,7 +2841,7 @@ void tst_QSettings::childGroups()
for (int pass = 0; pass < 3; ++pass) {
QConfFile::clearCache();
QSettings settings(format, QSettings::SystemScope, "software.org");
QSettings settings(format, scope, "software.org");
settings.setFallbacksEnabled(false);
if (pass == 1) {
settings.value("gamma/d");
@ -2837,8 +2889,10 @@ void tst_QSettings::childKeys()
{
QFETCH(QSettings::Format, format);
const QSettings::Scope scope = m_canWriteNativeSystemSettings ? QSettings::SystemScope : QSettings::UserScope;
{
QSettings settings(format, QSettings::SystemScope, "software.org");
QSettings settings(format, scope, "software.org");
settings.setValue("alpha", "1");
settings.setValue("alpha/a", "2");
settings.setValue("alpha/b", "3");
@ -2855,7 +2909,7 @@ void tst_QSettings::childKeys()
for (int pass = 0; pass < 3; ++pass) {
QConfFile::clearCache();
QSettings settings(format, QSettings::SystemScope, "software.org");
QSettings settings(format, scope, "software.org");
settings.setFallbacksEnabled(false);
if (pass == 1) {
settings.value("gamma/d");
@ -2907,15 +2961,17 @@ void tst_QSettings::allKeys()
allKeys << "alpha" << "alpha/a" << "alpha/b" << "alpha/c" << "beta" << "gamma" << "gamma/d"
<< "gamma/d/e" << "gamma/f/g" << "omicron/h/i/j/x" << "omicron/h/i/k/y" << "zeta/z";
const QSettings::Scope scope = m_canWriteNativeSystemSettings ? QSettings::SystemScope : QSettings::UserScope;
{
QSettings settings(format, QSettings::SystemScope, "software.org");
QSettings settings(format, scope, "software.org");
for (int i = 0; i < allKeys.size(); ++i)
settings.setValue(allKeys.at(i), QString::number(i + 1));
}
for (int pass = 0; pass < 3; ++pass) {
QConfFile::clearCache();
QSettings settings(format, QSettings::SystemScope, "software.org");
QSettings settings(format, scope, "software.org");
settings.setFallbacksEnabled(false);
if (pass == 1) {