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();
+ }
}
}
}
#include <QLoggingCategory>
#include <QNetworkCookie>
#include <QDataStream>
+#include <QDir>
namespace OCC {
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)
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);
nextcloud_add_test(SyncJournalDB "")
nextcloud_add_test(SyncFileItem "")
nextcloud_add_test(ConcatUrl "")
+nextcloud_add_test(Cookies "")
nextcloud_add_test(XmlParse "")
nextcloud_add_test(ChecksumValidator "")
--- /dev/null
+/*
+ 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"