diff --git a/webenginepart/autotests/CMakeLists.txt b/webenginepart/autotests/CMakeLists.txt --- a/webenginepart/autotests/CMakeLists.txt +++ b/webenginepart/autotests/CMakeLists.txt @@ -1,3 +1,7 @@ +if(BUILD_TESTING) + add_definitions(-DBUILD_TESTING) +endif(BUILD_TESTING) + include(ECMAddTests) find_package(Qt5Test ${QT_MIN_VERSION} CONFIG REQUIRED) diff --git a/webenginepart/autotests/webenginepartcookiejar_test.h b/webenginepart/autotests/webenginepartcookiejar_test.h --- a/webenginepart/autotests/webenginepartcookiejar_test.h +++ b/webenginepart/autotests/webenginepartcookiejar_test.h @@ -26,6 +26,7 @@ #include #include +#include class QWebEngineCookieStore; class WebEnginePartCookieJar; @@ -60,9 +61,16 @@ void testCookieAddedToStoreAreAddedToKCookieServer(); void testCookieRemovedFromStoreAreRemovedFromKCookieServer_data(); void testCookieRemovedFromStoreAreRemovedFromKCookieServer(); + void testPersistentCookiesAreAddedToStoreOnCreation(); + void testSessionCookiesAreNotAddedToStoreOnCreation(); + +#ifdef BUILD_TESTING + +#endif private: + QDBusError addCookieToKCookieServer(const QNetworkCookie &cookie, const QString &host, const QString &scheme = QString()); void deleteCookies(const QList &cookies); QList findTestCookies(); QString m_cookieName; diff --git a/webenginepart/autotests/webenginepartcookiejar_test.cpp b/webenginepart/autotests/webenginepartcookiejar_test.cpp --- a/webenginepart/autotests/webenginepartcookiejar_test.cpp +++ b/webenginepart/autotests/webenginepartcookiejar_test.cpp @@ -246,20 +246,9 @@ QFETCH(const QString, domain); QFETCH(const QString, host); - const QString url = "https://" + host; - - //cookie is in the "format" used by QWebEngineCookieStore, which means that, if the domain should be empty, - //it is stored as a domain not starting with a dot. KCookieServer, instead, wants cookies without domains - //to actually have no domain, so we have to change it - QNetworkCookie kcookieServerCookie(cookie); - if (!kcookieServerCookie.domain().startsWith('.')) { - kcookieServerCookie.setDomain(QString()); - } - const QByteArray setCookie = "Set-Cookie: " + kcookieServerCookie.toRawForm(); - //Add cookie to KCookieServer - QDBusMessage rep = m_server->call(QDBus::Block, "addCookies", url, setCookie, static_cast(0)); - QVERIFY2(!m_server->lastError().isValid(), qPrintable(m_server->lastError().message())); + QDBusError e = addCookieToKCookieServer(cookie, host); + QVERIFY2(!e.isValid(), qPrintable(m_server->lastError().message())); //Ensure cookie has been added to KCookieServer QDBusReply reply = m_server->call(QDBus::Block, "findCookies", QVariant::fromValue(QList{2}), domain, host, "", ""); @@ -274,3 +263,65 @@ cookies = reply.value(); QVERIFY2(!cookies.contains(name), "Cookie wasn't removed from server"); } + +QDBusError TestWebEnginePartCookieJar::addCookieToKCookieServer(const QNetworkCookie& cookie, const QString& host, const QString& scheme) +{ + QUrl url; + url.setHost(host); + if (!scheme.isEmpty()) { + url.setScheme(scheme); + } else { + url.setScheme(cookie.isSecure() ? "https" : "http"); + } + const QByteArray setCookie = "Set-Cookie: " + cookie.toRawForm(); + m_server->call(QDBus::Block, "addCookies", url.toString(), setCookie, static_cast(0)); + return m_server->lastError(); +} + +void TestWebEnginePartCookieJar::testPersistentCookiesAreAddedToStoreOnCreation() +{ + delete m_jar; + QDateTime exp = QDateTime::currentDateTime().addYears(1); + QString baseCookieName = m_cookieName + "-startup"; + QList data { + {baseCookieName + "-persistent", "test-value", ".yyy.xxx.com", "/abc/def/", "zzz.yyy.xxx.com", QDateTime::currentDateTime().addYears(1), true}, + {baseCookieName + "-no-path", "test-value", ".yyy.xxx.com", "", "zzz.yyy.xxx.com", QDateTime::currentDateTime().addYears(1), true}, + {baseCookieName + "-no-domain", "test-value", "", "/abc/def/", "zzz.yyy.xxx.com", QDateTime::currentDateTime().addYears(1), true}, + {baseCookieName + "-no-secure", "test-value", ".yyy.xxx.com", "/abc/def/", "zzz.yyy.xxx.com", QDateTime::currentDateTime().addYears(1), false} + }; + QList cookies; + for(const CookieData &d: data){ + const QNetworkCookie c = d.cookie(); + cookies << c; + QDBusError e = addCookieToKCookieServer(c, d.host); + QVERIFY2(!e.isValid(), qPrintable(e.message())); + } + m_jar = new WebEnginePartCookieJar(m_profile, this); + QList cookiesInsertedIntoJar; + for(const QNetworkCookie &c: m_jar->m_testCookies){ + if(QString(c.name()).startsWith(baseCookieName)) { + cookiesInsertedIntoJar << c; + } + } + + QCOMPARE(cookiesInsertedIntoJar.count(), cookies.count()); + QEXPECT_FAIL("", "There's a small difference (msecs) in the expiration dates", Abort); + QCOMPARE(cookiesInsertedIntoJar, cookies); +} + +void TestWebEnginePartCookieJar::testSessionCookiesAreNotAddedToStoreOnCreation() +{ + delete m_jar; + CookieData data{m_cookieName + "-startup-session", "test-value", ".yyy.xxx.com", "/abc/def", "zzz.yyy.xxx.com", QDateTime(), true}; + QDBusError e = addCookieToKCookieServer(data.cookie(), data.host); + QVERIFY2(!e.isValid(), qPrintable(e.message())); + m_jar = new WebEnginePartCookieJar(m_profile, this); + QList cookiesInsertedIntoJar; + for(const QNetworkCookie &c: m_jar->m_testCookies) { + if (c.name() == data.name) { + cookiesInsertedIntoJar << c; + } + } + QVERIFY2(cookiesInsertedIntoJar.isEmpty(), "Session cookies inserted into cookie store"); +} + diff --git a/webenginepart/src/CMakeLists.txt b/webenginepart/src/CMakeLists.txt --- a/webenginepart/src/CMakeLists.txt +++ b/webenginepart/src/CMakeLists.txt @@ -3,6 +3,10 @@ add_definitions(-DTRANSLATION_DOMAIN=\"webenginepart\") +if(BUILD_TESTING) + add_definitions(-DBUILD_TESTING) +endif(BUILD_TESTING) + include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}) set(kwebenginepartlib_LIB_SRCS diff --git a/webenginepart/src/webenginepartcookiejar.h b/webenginepart/src/webenginepartcookiejar.h --- a/webenginepart/src/webenginepartcookiejar.h +++ b/webenginepart/src/webenginepartcookiejar.h @@ -332,6 +332,11 @@ */ CookieList m_cookiesLoadedFromKCookieServer; +#ifdef BUILD_TESTING + QList m_testCookies; + friend class TestWebEnginePartCookieJar; +#endif + }; /** diff --git a/webenginepart/src/webenginepartcookiejar.cpp b/webenginepart/src/webenginepartcookiejar.cpp --- a/webenginepart/src/webenginepartcookiejar.cpp +++ b/webenginepart/src/webenginepartcookiejar.cpp @@ -291,6 +291,9 @@ continue; } m_cookiesLoadedFromKCookieServer << cookie; +#ifdef BUILD_TESTING + m_testCookies << cookie; +#endif m_cookieStore->setCookie(cookie); } }