From 6a93f37440a3dcc72bb5e496dac90a8f95d63eab Mon Sep 17 00:00:00 2001 From: Katja Marttila Date: Thu, 9 Apr 2020 13:15:52 +0300 Subject: Add possibility to auto accept or reject IFW queries from CLI Added new command line switches accept-messages and reject-messages to automatically accept or reject installer message queries. Also removed Retry option in CLI when archive or hash download failed. With GUI it is possible to try Retry in case either is missing but without GUI Retry will cause infinite loop. Added unit tests for accept-messages and reject-messages using CLI. Task-number: QTIFW-1736 Change-Id: Ie87d2d8157be772b790415c96e5b94fc882d4be7 Reviewed-by: Arttu Tarkiainen --- src/libs/installer/commandlineparser.cpp | 6 ++ src/libs/installer/constants.h | 4 + src/libs/installer/downloadarchivesjob.cpp | 9 +- src/libs/installer/messageboxhandler.cpp | 8 +- src/libs/kdtools/filedownloader.cpp | 2 +- src/sdk/sdkapp.h | 5 + .../data/invalidhash/B/1.0.0-1content.7z | Bin 0 -> 144 bytes .../data/invalidhash/B/1.0.0-1content.7z.sha1 | 1 + .../messageboxhandler/data/invalidhash/Updates.xml | 15 +++ .../data/invalidoperation/A/1.0.2-1meta.7z | Bin 0 -> 841 bytes .../data/invalidoperation/Updates.xml | 13 +++ .../data/missingarchive/C/1.0.0content.7z.sha1 | 1 + .../data/missingarchive/Updates.xml | 16 +++ .../messageboxhandler/messageboxhandler.pro | 4 + .../auto/installer/messageboxhandler/settings.qrc | 11 ++ .../messageboxhandler/tst_messageboxhandler.cpp | 119 +++++++++++++++++++-- 16 files changed, 202 insertions(+), 12 deletions(-) create mode 100644 tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z create mode 100644 tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z.sha1 create mode 100644 tests/auto/installer/messageboxhandler/data/invalidhash/Updates.xml create mode 100644 tests/auto/installer/messageboxhandler/data/invalidoperation/A/1.0.2-1meta.7z create mode 100644 tests/auto/installer/messageboxhandler/data/invalidoperation/Updates.xml create mode 100644 tests/auto/installer/messageboxhandler/data/missingarchive/C/1.0.0content.7z.sha1 create mode 100644 tests/auto/installer/messageboxhandler/data/missingarchive/Updates.xml create mode 100644 tests/auto/installer/messageboxhandler/settings.qrc diff --git a/src/libs/installer/commandlineparser.cpp b/src/libs/installer/commandlineparser.cpp index 8c0fb1ee7..dc4569fbd 100644 --- a/src/libs/installer/commandlineparser.cpp +++ b/src/libs/installer/commandlineparser.cpp @@ -146,6 +146,12 @@ CommandLineParser::CommandLineParser() QLatin1String("Create a local repository inside the installation directory. This option " "has no effect on online installers."))); + // Message query options + m_parser.addOption(QCommandLineOption(QStringList() << CommandLineOptions::scAcceptMessageQuery, + QLatin1String("Accepts all message queries without user input."))); + m_parser.addOption(QCommandLineOption(QStringList() << CommandLineOptions::scRejectMessageQuery, + QLatin1String("Rejects all message queries without user input."))); + // Developer options m_parser.addOption(QCommandLineOption(QStringList() << CommandLineOptions::scScriptShort << CommandLineOptions::scScriptLong, diff --git a/src/libs/installer/constants.h b/src/libs/installer/constants.h index 3759e6157..1d9be8525 100644 --- a/src/libs/installer/constants.h +++ b/src/libs/installer/constants.h @@ -151,6 +151,10 @@ static const QLatin1String scStartPackageManagerLong("start-package-manager"); static const QLatin1String scStartUninstallerShort("sr"); static const QLatin1String scStartUninstallerLong("start-uninstaller"); +// Message acceptance options +static const QLatin1String scAcceptMessageQuery("accept-messages"); +static const QLatin1String scRejectMessageQuery("reject-messages"); + // Misc installation options static const QLatin1String scRootShort("t"); static const QLatin1String scRootLong("root"); diff --git a/src/libs/installer/downloadarchivesjob.cpp b/src/libs/installer/downloadarchivesjob.cpp index 102783133..4d9d6b325 100644 --- a/src/libs/installer/downloadarchivesjob.cpp +++ b/src/libs/installer/downloadarchivesjob.cpp @@ -215,7 +215,10 @@ void DownloadArchivesJob::registerFile() "downloading failed. This is a temporary error, please retry."), QMessageBox::Retry | QMessageBox::Cancel, QMessageBox::Cancel); - if (res == QMessageBox::Cancel) { + // If run from command line instance, do not continue if hash verification failed. + // Same download is tried again and again causing infinite loop if hash not + // fixed to repositories. + if (res == QMessageBox::Cancel || m_core->isCommandLineInstance()) { finishWithError(tr("Cannot verify Hash")); return; } @@ -249,7 +252,9 @@ void DownloadArchivesJob::downloadFailed(const QString &error) QLatin1String("archiveDownloadError"), tr("Download Error"), tr("Cannot download archive %1: %2") .arg(m_archivesToDownload.first().second, error), QMessageBox::Retry | QMessageBox::Cancel); - if (b == QMessageBox::Retry) + // Do not call fetchNextArchiveHash when using command line instance, + // installer tries to download the same archive causing infinite loop + if (b == QMessageBox::Retry && !m_core->isCommandLineInstance()) QMetaObject::invokeMethod(this, "fetchNextArchiveHash", Qt::QueuedConnection); else downloadCanceled(); diff --git a/src/libs/installer/messageboxhandler.cpp b/src/libs/installer/messageboxhandler.cpp index cb37254eb..4562f0f3c 100644 --- a/src/libs/installer/messageboxhandler.cpp +++ b/src/libs/installer/messageboxhandler.cpp @@ -406,8 +406,12 @@ QMessageBox::StandardButton MessageBoxHandler::showMessageBox(MessageType messag qCDebug(QInstaller::lcInstallerInstallLog).nospace() << "Created " << messageTypeHash.value(messageType).toUtf8().constData() << " message box " << identifier << ": " << title << ", " << text; - if (qobject_cast (qApp) == nullptr) - return defaultButton; + if (qobject_cast (qApp) == nullptr) { + if (m_defaultAction != AskUser) + return autoReply(buttons); + else + return defaultButton; + } if (m_automaticAnswers.contains(identifier)) return m_automaticAnswers.value(identifier); diff --git a/src/libs/kdtools/filedownloader.cpp b/src/libs/kdtools/filedownloader.cpp index b816b5fd5..276d5bbef 100644 --- a/src/libs/kdtools/filedownloader.cpp +++ b/src/libs/kdtools/filedownloader.cpp @@ -814,7 +814,7 @@ void KDUpdater::LocalFileDownloader::doDownload() if (!d->source->open(QFile::ReadOnly)) { onError(); setDownloadAborted(tr("Cannot open file \"%1\" for reading: %2").arg(QFileInfo(localFile) - .fileName(), d->source->errorString())); + .fileName(), d->source ? d->source->errorString() : tr("File not found"))); return; } diff --git a/src/sdk/sdkapp.h b/src/sdk/sdkapp.h index ab0b9eb2f..3c0dd0da8 100644 --- a/src/sdk/sdkapp.h +++ b/src/sdk/sdkapp.h @@ -274,6 +274,11 @@ public: .isSet(CommandLineOptions::scCreateLocalRepositoryLong) || m_core->settings().createLocalRepository()); + if (m_parser.isSet(CommandLineOptions::scAcceptMessageQuery)) + m_core->autoAcceptMessageBoxes(); + if (m_parser.isSet(CommandLineOptions::scRejectMessageQuery)) + m_core->autoRejectMessageBoxes(); + return true; } diff --git a/tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z b/tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z new file mode 100644 index 000000000..4663e2b7c Binary files /dev/null and b/tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z differ diff --git a/tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z.sha1 b/tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z.sha1 new file mode 100644 index 000000000..281381b97 --- /dev/null +++ b/tests/auto/installer/messageboxhandler/data/invalidhash/B/1.0.0-1content.7z.sha1 @@ -0,0 +1 @@ +1c99fe9980cb71dde6a8468c9aa09b4153bc3bff \ No newline at end of file diff --git a/tests/auto/installer/messageboxhandler/data/invalidhash/Updates.xml b/tests/auto/installer/messageboxhandler/data/invalidhash/Updates.xml new file mode 100644 index 000000000..2dd2ca2d5 --- /dev/null +++ b/tests/auto/installer/messageboxhandler/data/invalidhash/Updates.xml @@ -0,0 +1,15 @@ + + {AnyApplication} + 1.0.0 + true + + B + B + Example component B + 1.0.0-1 + 2015-01-01 + true + + content.7z + + diff --git a/tests/auto/installer/messageboxhandler/data/invalidoperation/A/1.0.2-1meta.7z b/tests/auto/installer/messageboxhandler/data/invalidoperation/A/1.0.2-1meta.7z new file mode 100644 index 000000000..3653317c6 Binary files /dev/null and b/tests/auto/installer/messageboxhandler/data/invalidoperation/A/1.0.2-1meta.7z differ diff --git a/tests/auto/installer/messageboxhandler/data/invalidoperation/Updates.xml b/tests/auto/installer/messageboxhandler/data/invalidoperation/Updates.xml new file mode 100644 index 000000000..77b5a9956 --- /dev/null +++ b/tests/auto/installer/messageboxhandler/data/invalidoperation/Updates.xml @@ -0,0 +1,13 @@ + + {AnyApplication} + 1.0.0 + + A + A + Example component A + 1.0.2-1 + 2015-01-01 + true + + + diff --git a/tests/auto/installer/messageboxhandler/data/missingarchive/C/1.0.0content.7z.sha1 b/tests/auto/installer/messageboxhandler/data/missingarchive/C/1.0.0content.7z.sha1 new file mode 100644 index 000000000..281381b97 --- /dev/null +++ b/tests/auto/installer/messageboxhandler/data/missingarchive/C/1.0.0content.7z.sha1 @@ -0,0 +1 @@ +1c99fe9980cb71dde6a8468c9aa09b4153bc3bff \ No newline at end of file diff --git a/tests/auto/installer/messageboxhandler/data/missingarchive/Updates.xml b/tests/auto/installer/messageboxhandler/data/missingarchive/Updates.xml new file mode 100644 index 000000000..0c5f7e211 --- /dev/null +++ b/tests/auto/installer/messageboxhandler/data/missingarchive/Updates.xml @@ -0,0 +1,16 @@ + + {AnyApplication} + 1.0.0 + true + + C + C + Example component C + 1.0.0 + 2015-01-01 + true + + content.7z + 3c940d54a1643ae8020162797860e827f8d246ca + + diff --git a/tests/auto/installer/messageboxhandler/messageboxhandler.pro b/tests/auto/installer/messageboxhandler/messageboxhandler.pro index c63b2e35b..3bb0f7b64 100644 --- a/tests/auto/installer/messageboxhandler/messageboxhandler.pro +++ b/tests/auto/installer/messageboxhandler/messageboxhandler.pro @@ -3,3 +3,7 @@ include(../../qttest.pri) QT += qml widgets SOURCES += tst_messageboxhandler.cpp + +RESOURCES += \ + settings.qrc \ + ..\shared\config.qrc diff --git a/tests/auto/installer/messageboxhandler/settings.qrc b/tests/auto/installer/messageboxhandler/settings.qrc new file mode 100644 index 000000000..51e4bba19 --- /dev/null +++ b/tests/auto/installer/messageboxhandler/settings.qrc @@ -0,0 +1,11 @@ + + + data/invalidhash/Updates.xml + data/invalidhash/B/1.0.0-1content.7z + data/invalidhash/B/1.0.0-1content.7z.sha1 + data/invalidoperation/Updates.xml + data/invalidoperation/A/1.0.2-1meta.7z + data/missingarchive/Updates.xml + data/missingarchive/C/1.0.0content.7z.sha1 + + diff --git a/tests/auto/installer/messageboxhandler/tst_messageboxhandler.cpp b/tests/auto/installer/messageboxhandler/tst_messageboxhandler.cpp index dd127f626..1c819fa7c 100644 --- a/tests/auto/installer/messageboxhandler/tst_messageboxhandler.cpp +++ b/tests/auto/installer/messageboxhandler/tst_messageboxhandler.cpp @@ -1,7 +1,42 @@ +/************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + #include #include #include #include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -26,7 +61,15 @@ QT_END_NAMESPACE class tst_MessageBoxHandler : public QObject { Q_OBJECT -public: +private: + void setRepository(const QString &repository) { + core->cancelMetaInfoJob(); + QSet repoList; + Repository repo = Repository::fromUserInput(repository); + repoList.insert(repo); + core->settings().setDefaultRepositories(repoList); + } + private slots: void initTestCase() { @@ -46,12 +89,21 @@ private slots: if (enumValue == QMessageBox::LastButton) break; } + + QInstaller::init(); //This will eat debug output + + core = new PackageManagerCore(BinaryContent::MagicInstallerMarker, QList (), + QString(), Protocol::DefaultAuthorizationKey, Protocol::Mode::Production, + QHash(), true); + core->setAllowedRunningProcesses(QStringList() << QCoreApplication::applicationFilePath()); + m_installDir = QInstaller::generateTemporaryFileName(); + QDir().mkpath(m_installDir); + core->setValue(scTargetDir, m_installDir); } void testScriptButtonValues() { - PackageManagerCore core; - ScriptEngine *scriptEngine = new ScriptEngine(&core); + ScriptEngine *scriptEngine = new ScriptEngine(core); QMapIterator i(m_standardButtonValueMap); while (i.hasNext()) { i.next(); @@ -67,8 +119,6 @@ private slots: void testDefaultAction() { - const char ignoreMessage[] = "Created critical message box \"TestError\": \"A test error\", " - "\"This is a test error message.\""; srand(time(0)); /* initialize random seed: */ int standardButtons = QMessageBox::NoButton; @@ -82,8 +132,6 @@ private slots: // use only every 5th run to reduce the time which it takes to run this test if (iSecret > 2) continue; - - QTest::ignoreMessage(QtDebugMsg, ignoreMessage); int returnButton = MessageBoxHandler::instance()->critical(QLatin1String("TestError"), QLatin1String("A test error"), QLatin1String("This is a test error message."), static_cast(standardButtons)); @@ -101,9 +149,66 @@ private slots: } while (standardButtons < m_maxStandardButtons); } + void invalidOperationAutoReject() + { + setRepository(":///data/invalidoperation"); + core->autoRejectMessageBoxes(); + core->installDefaultComponentsSilently(); + QCOMPARE(PackageManagerCore::Canceled, core->status()); + } + + void invalidOperationAutoAccept() + { + setRepository(":///data/invalidoperation"); + core->autoAcceptMessageBoxes(); + core->installDefaultComponentsSilently(); + QCOMPARE(PackageManagerCore::Running, core->status()); + } + + void invalidHashAutoReject() + { + setRepository(":///data/invalidhash"); + core->autoRejectMessageBoxes(); + core->installSelectedComponentsSilently(QStringList () << "B"); + QCOMPARE(PackageManagerCore::Failure, core->status()); + } + + void invalidHashAutoAccept() + { + setRepository(":///data/invalidhash"); + core->autoAcceptMessageBoxes(); + core->installSelectedComponentsSilently(QStringList () << "B"); + QCOMPARE(PackageManagerCore::Failure, core->status()); + } + + void missingArchiveAutoReject() + { + setRepository(":///data/missingarchive"); + core->autoRejectMessageBoxes(); + core->installSelectedComponentsSilently(QStringList () << "C"); + QCOMPARE(PackageManagerCore::Canceled, core->status()); + } + + void missingArchiveAutoAccept() + { + setRepository(":///data/missingarchive"); + core->autoAcceptMessageBoxes(); + core->installSelectedComponentsSilently(QStringList () << "C"); + QCOMPARE(PackageManagerCore::Canceled, core->status()); + } + + void cleanupTestCase() + { + core->deleteLater(); + QDir dir(m_installDir); + QVERIFY(dir.removeRecursively()); + } + private: QMap m_standardButtonValueMap; int m_maxStandardButtons; + PackageManagerCore *core; + QString m_installDir; }; QTEST_MAIN(tst_MessageBoxHandler) -- cgit v1.2.3