Fix saving of cookies

Fixes: #7700
This commit is contained in:
Hannah von Reth 2020-01-24 17:57:34 +01:00 committed by Kevin Ottens
parent 3a3ccb0834
commit 9564e5e92e
No known key found for this signature in database
GPG key ID: 074BBBCB8DECC9E2
5 changed files with 77 additions and 11 deletions

View file

@ -258,7 +258,10 @@ void AccountManager::saveAccountHelper(Account *acc, QSettings &settings, bool s
auto *jar = qobject_cast<CookieJar *>(acc->_am->cookieJar());
if (jar) {
qCInfo(lcAccountManager) << "Saving cookies." << acc->cookieJarPath();
jar->save(acc->cookieJarPath());
if (!jar->save(acc->cookieJarPath()))
{
qCWarning(lcAccountManager) << "Failed to save cookies to" << acc->cookieJarPath();
}
}
}
}

View file

@ -21,6 +21,7 @@
#include <QLoggingCategory>
#include <QNetworkCookie>
#include <QDataStream>
#include <QDir>
namespace OCC {
@ -95,27 +96,45 @@ void CookieJar::clearSessionCookies()
setAllCookies(removeExpired(allCookies()));
}
void CookieJar::save(const QString &fileName)
bool CookieJar::save(const QString &fileName)
{
QFile file;
file.setFileName(fileName);
const QFileInfo info(fileName);
if (!info.dir().exists())
{
info.dir().mkpath(".");
}
qCDebug(lcCookieJar) << fileName;
file.open(QIODevice::WriteOnly);
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
return false;
}
QDataStream stream(&file);
stream << removeExpired(allCookies());
file.close();
return true;
}
void CookieJar::restore(const QString &fileName)
bool CookieJar::restore(const QString &fileName)
{
QFile file;
file.setFileName(fileName);
file.open(QIODevice::ReadOnly);
const QFileInfo info(fileName);
if (!info.exists())
{
return false;
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
return false;
}
QDataStream stream(&file);
QList<QNetworkCookie> list;
stream >> list;
setAllCookies(removeExpired(list));
file.close();
return true;
}
QList<QNetworkCookie> CookieJar::removeExpired(const QList<QNetworkCookie> &cookies)

View file

@ -39,8 +39,8 @@ public:
using QNetworkCookieJar::setAllCookies;
using QNetworkCookieJar::allCookies;
void save(const QString &fileName);
void restore(const QString &fileName);
bool save(const QString &fileName);
bool restore(const QString &fileName);
signals:
void newCookiesForUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);

View file

@ -37,6 +37,7 @@ nextcloud_add_test(OwnSql "")
nextcloud_add_test(SyncJournalDB "")
nextcloud_add_test(SyncFileItem "")
nextcloud_add_test(ConcatUrl "")
nextcloud_add_test(Cookies "")
nextcloud_add_test(XmlParse "")
nextcloud_add_test(ChecksumValidator "")

43
test/testcookies.cpp Normal file
View file

@ -0,0 +1,43 @@
/*
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 "libsync/cookiejar.h"
using namespace OCC;
class TestCookies : public QObject
{
Q_OBJECT
private slots:
void testCookies()
{
QTemporaryDir tmp;
const QString nonexistingPath = tmp.filePath("someNonexistingDir/test.db");
QNetworkCookie cookieA = QNetworkCookie("foo", "bar");
// tomorrow rounded
cookieA.setExpirationDate(QDateTime(QDateTime::currentDateTimeUtc().addDays(1).date()));
const QList<QNetworkCookie> cookies = {cookieA, QNetworkCookie("foo2", "bar")};
CookieJar jar;
jar.setAllCookies(cookies);
QCOMPARE(cookies, jar.allCookies());
QVERIFY(jar.save(tmp.filePath("test.db")));
// ensure we are able to create a cookie jar in a non exisitning folder (mkdir)
QVERIFY(jar.save(nonexistingPath));
CookieJar jar2;
QVERIFY(jar2.restore(nonexistingPath));
// here we should have only cookieA as the second one was a session cookie
QCOMPARE(QList<QNetworkCookie>{cookieA}, jar2.allCookies());
}
};
QTEST_APPLESS_MAIN(TestCookies)
#include "testcookies.moc"