Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve windows update downloader #9153

Merged
merged 3 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/gui/updater/ocupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ QString OCUpdater::statusString() const
}
}

int OCUpdater::downloadState() const
OCUpdater::DownloadState OCUpdater::downloadState() const
{
return _state;
}
Expand Down Expand Up @@ -308,14 +308,19 @@ void NSISUpdater::wipeUpdateData()
void NSISUpdater::slotDownloadFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
const QUrl url(reply->url());
reply->deleteLater();
_file->close();
if (reply->error() != QNetworkReply::NoError) {
setDownloadState(DownloadFailed);
return;
}
const auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (status != 200) {
setDownloadState(DownloadFailed);
return;
}

QUrl url(reply->url());
_file->close();

QSettings settings(ConfigFile::configFile(), QSettings::IniFormat);

Expand Down Expand Up @@ -355,15 +360,17 @@ void NSISUpdater::versionInfoArrived(const UpdateInfo &info)
qCInfo(lcUpdater) << "Client is on latest version!";
setDownloadState(UpToDate);
} else {
QString url = info.downloadUrl();
const QString url = info.downloadUrl();
if (url.isEmpty()) {
showNoUrlDialog(info);
} else {
_targetFile = ConfigFile::configPath() + url.mid(url.lastIndexOf('/') + 1);
if (QFile(_targetFile).exists()) {
if (QFile::exists(_targetFile)) {
setDownloadState(DownloadComplete);
} else {
QNetworkReply *reply = qnam()->get(QNetworkRequest(QUrl(url)));
auto request = QNetworkRequest(QUrl(url));
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the default everywhere nowadays?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default are manual redirects, with the flag I enable redirects and don't limit them.

QNetworkReply *reply = qnam()->get(request);
connect(reply, &QIODevice::readyRead, this, &NSISUpdater::slotWriteFile);
connect(reply, &QNetworkReply::finished, this, &NSISUpdater::slotDownloadFinished);
setDownloadState(Downloading);
Expand Down
8 changes: 6 additions & 2 deletions src/gui/updater/ocupdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class OCUpdater : public Updater
DownloadFailed,
DownloadTimedOut,
UpdateOnlyAvailableThroughSystem };
Q_ENUM(DownloadState);

explicit OCUpdater(const QUrl &url);

void setUpdateUrl(const QUrl &url);
Expand All @@ -106,7 +108,7 @@ class OCUpdater : public Updater
void checkForUpdate() override;

QString statusString() const;
int downloadState() const;
DownloadState downloadState() const;
void setDownloadState(DownloadState state);

signals:
Expand Down Expand Up @@ -134,7 +136,7 @@ private slots:

private:
QUrl _updateUrl;
int _state;
DownloadState _state;
QNetworkAccessManager *_accessManager;
QTimer *_timeoutWatchdog; /** Timer to guard the timeout of an individual network request */
UpdateInfo _updateInfo;
Expand Down Expand Up @@ -162,6 +164,8 @@ private slots:
void versionInfoArrived(const UpdateInfo &info) override;
QScopedPointer<QTemporaryFile> _file;
QString _targetFile;

friend class TestUpdater;
};

/**
Expand Down
60 changes: 60 additions & 0 deletions test/testupdater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This software is in the public domain, furnished "as is", without technical
support, and with no warranty, express or implied, as to its usefulness for
any purpose.
*/

#include <QtTest>

#include "updater/updater.h"
#include "updater/ocupdater.h"

namespace OCC {

class TestUpdater : public QObject
{
Q_OBJECT

private slots:
void testVersionToInt()
{
auto lowVersion = Updater::Helper::versionToInt(1, 2, 80, 3000);
QCOMPARE(Updater::Helper::stringVersionToInt("1.2.80.3000"), lowVersion);

auto highVersion = Updater::Helper::versionToInt(999, 2, 80, 3000);
auto currVersion = Updater::Helper::currentVersionToInt();
QVERIFY(currVersion > 0);
QVERIFY(currVersion > lowVersion);
QVERIFY(currVersion < highVersion);
}

void testDownload_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<OCUpdater::DownloadState>("result");
// a redirect to attic
QTest::newRow("redirect") << "https://download.owncloud.com/desktop/stable/ownCloud-2.2.4.6408-setup.exe" << OCUpdater::DownloadComplete;
QTest::newRow("broken url") << "https://&" << OCUpdater::DownloadFailed;
}

void testDownload()
{
QFETCH(QString, url);
QFETCH(OCUpdater::DownloadState, result);
UpdateInfo info;
info.setDownloadUrl(url);
info.setVersionString("ownCloud 2.2.4 (build 6408)");
// esnure we do the update
info.setVersion("100.2.4.6408");
auto *updater = new NSISUpdater({});
QSignalSpy downloadSpy(updater, &NSISUpdater::slotDownloadFinished);
updater->versionInfoArrived(info);
downloadSpy.wait();
QCOMPARE(updater->downloadState(), result);
updater->deleteLater();
}
};
}

QTEST_MAIN(OCC::TestUpdater)
#include "testupdater.moc"