From 9564e5e92e071549ae956a566718c31bca8fd3d5 Mon Sep 17 00:00:00 2001 From: Hannah von Reth Date: Fri, 24 Jan 2020 17:57:34 +0100 Subject: [PATCH] Fix saving of cookies Fixes: #7700 --- src/gui/accountmanager.cpp | 5 ++++- src/libsync/cookiejar.cpp | 35 ++++++++++++++++++++++++------- src/libsync/cookiejar.h | 4 ++-- test/CMakeLists.txt | 1 + test/testcookies.cpp | 43 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 test/testcookies.cpp diff --git a/src/gui/accountmanager.cpp b/src/gui/accountmanager.cpp index 577320447..85f475cac 100644 --- a/src/gui/accountmanager.cpp +++ b/src/gui/accountmanager.cpp @@ -258,7 +258,10 @@ void AccountManager::saveAccountHelper(Account *acc, QSettings &settings, bool s auto *jar = qobject_cast(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(); + } } } } diff --git a/src/libsync/cookiejar.cpp b/src/libsync/cookiejar.cpp index ded4eaeed..7e6158f84 100644 --- a/src/libsync/cookiejar.cpp +++ b/src/libsync/cookiejar.cpp @@ -21,6 +21,7 @@ #include #include #include +#include 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 list; stream >> list; setAllCookies(removeExpired(list)); file.close(); + return true; } QList CookieJar::removeExpired(const QList &cookies) diff --git a/src/libsync/cookiejar.h b/src/libsync/cookiejar.h index 245b546b9..ecdd69971 100644 --- a/src/libsync/cookiejar.h +++ b/src/libsync/cookiejar.h @@ -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 &cookieList, const QUrl &url); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 682f42fcc..dc4100e97 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 "") diff --git a/test/testcookies.cpp b/test/testcookies.cpp new file mode 100644 index 000000000..a5053abd9 --- /dev/null +++ b/test/testcookies.cpp @@ -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 + +#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 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{cookieA}, jar2.allCookies()); + + } + +}; + +QTEST_APPLESS_MAIN(TestCookies) +#include "testcookies.moc"