Changeset View
Changeset View
Standalone View
Standalone View
webenginepart/autotests/webenginepartcookiejar_test.cpp
Show All 26 Lines | |||||
27 | #include <QTest> | 27 | #include <QTest> | ||
28 | 28 | | |||
29 | #include <QNetworkCookie> | 29 | #include <QNetworkCookie> | ||
30 | #include <QWebEngineCookieStore> | 30 | #include <QWebEngineCookieStore> | ||
31 | #include <QWebEngineProfile> | 31 | #include <QWebEngineProfile> | ||
32 | #include <QDBusInterface> | 32 | #include <QDBusInterface> | ||
33 | #include <QDBusReply> | 33 | #include <QDBusReply> | ||
34 | 34 | | |||
35 | #include <algorithm> | ||||
36 | | ||||
37 | namespace QTest { | ||||
38 | template <> | ||||
39 | char *toString(const QNetworkCookie &cookie){ | ||||
40 | QByteArray ba = "QNetworkCookie{"; | ||||
41 | ba += "\nname: " + cookie.name(); | ||||
42 | ba += "\ndomain: " + (cookie.domain().isEmpty() ? "<EMPTY>" : cookie.domain()); | ||||
43 | ba += "\npath: " + (cookie.path().isEmpty() ? "<EMPTY>" : cookie.path()); | ||||
44 | ba += "\nvalue: " + cookie.value(); | ||||
45 | ba += "\nexpiration: " + (cookie.expirationDate().isValid() ? QString::number(cookie.expirationDate().toMSecsSinceEpoch()) : "<INVALID>"); | ||||
46 | ba += "\nsecure: " + QString::number(cookie.isSecure()); | ||||
47 | ba += "\nhttp: only" + QString::number(cookie.isHttpOnly()); | ||||
48 | return qstrdup(ba.data()); | ||||
49 | } | ||||
50 | } | ||||
51 | | ||||
35 | QTEST_MAIN(TestWebEnginePartCookieJar); | 52 | QTEST_MAIN(TestWebEnginePartCookieJar); | ||
36 | 53 | | |||
37 | void TestWebEnginePartCookieJar::initTestCase() | 54 | void TestWebEnginePartCookieJar::initTestCase() | ||
38 | { | 55 | { | ||
39 | m_cookieName = "webenginepartcookiejartest"; | 56 | m_cookieName = "webenginepartcookiejartest"; | ||
40 | } | 57 | } | ||
41 | 58 | | |||
42 | void TestWebEnginePartCookieJar::init() | 59 | void TestWebEnginePartCookieJar::init() | ||
▲ Show 20 Lines • Show All 198 Lines • ▼ Show 20 Line(s) | |||||
241 | 258 | | |||
242 | void TestWebEnginePartCookieJar::testCookieRemovedFromStoreAreRemovedFromKCookieServer() | 259 | void TestWebEnginePartCookieJar::testCookieRemovedFromStoreAreRemovedFromKCookieServer() | ||
243 | { | 260 | { | ||
244 | QFETCH(const QNetworkCookie, cookie); | 261 | QFETCH(const QNetworkCookie, cookie); | ||
245 | QFETCH(const QString, name); | 262 | QFETCH(const QString, name); | ||
246 | QFETCH(const QString, domain); | 263 | QFETCH(const QString, domain); | ||
247 | QFETCH(const QString, host); | 264 | QFETCH(const QString, host); | ||
248 | 265 | | |||
249 | const QString url = "https://" + host; | | |||
250 | | ||||
251 | //cookie is in the "format" used by QWebEngineCookieStore, which means that, if the domain should be empty, | | |||
252 | //it is stored as a domain not starting with a dot. KCookieServer, instead, wants cookies without domains | | |||
253 | //to actually have no domain, so we have to change it | | |||
254 | QNetworkCookie kcookieServerCookie(cookie); | | |||
255 | if (!kcookieServerCookie.domain().startsWith('.')) { | | |||
256 | kcookieServerCookie.setDomain(QString()); | | |||
257 | } | | |||
258 | const QByteArray setCookie = "Set-Cookie: " + kcookieServerCookie.toRawForm(); | | |||
259 | | ||||
260 | //Add cookie to KCookieServer | 266 | //Add cookie to KCookieServer | ||
261 | QDBusMessage rep = m_server->call(QDBus::Block, "addCookies", url, setCookie, static_cast<qlonglong>(0)); | 267 | QDBusError e = addCookieToKCookieServer(cookie, host); | ||
262 | QVERIFY2(!m_server->lastError().isValid(), qPrintable(m_server->lastError().message())); | 268 | QVERIFY2(!e.isValid(), qPrintable(m_server->lastError().message())); | ||
263 | 269 | | |||
264 | //Ensure cookie has been added to KCookieServer | 270 | //Ensure cookie has been added to KCookieServer | ||
265 | QDBusReply<QStringList> reply = m_server->call(QDBus::Block, "findCookies", QVariant::fromValue(QList<int>{2}), domain, host, "", ""); | 271 | QDBusReply<QStringList> reply = m_server->call(QDBus::Block, "findCookies", QVariant::fromValue(QList<int>{2}), domain, host, "", ""); | ||
266 | QVERIFY2(reply.isValid(), qPrintable(reply.error().message())); | 272 | QVERIFY2(reply.isValid(), qPrintable(reply.error().message())); | ||
267 | QStringList cookies = reply.value(); | 273 | QStringList cookies = reply.value(); | ||
268 | QVERIFY2(cookies.contains(name), "Cookie wasn't added to server"); | 274 | QVERIFY2(cookies.contains(name), "Cookie wasn't added to server"); | ||
269 | 275 | | |||
270 | //Emit QWebEngineCookieStore::cookieRemoved signal and check that cookie has indeed been removed | 276 | //Emit QWebEngineCookieStore::cookieRemoved signal and check that cookie has indeed been removed | ||
271 | emit m_store->cookieRemoved(cookie); | 277 | emit m_store->cookieRemoved(cookie); | ||
272 | reply = m_server->call(QDBus::Block, "findCookies", QVariant::fromValue(QList<int>{2}), domain, "", "", ""); | 278 | reply = m_server->call(QDBus::Block, "findCookies", QVariant::fromValue(QList<int>{2}), domain, "", "", ""); | ||
273 | QVERIFY2(reply.isValid(), qPrintable(reply.error().message())); | 279 | QVERIFY2(reply.isValid(), qPrintable(reply.error().message())); | ||
274 | cookies = reply.value(); | 280 | cookies = reply.value(); | ||
275 | QVERIFY2(!cookies.contains(name), "Cookie wasn't removed from server"); | 281 | QVERIFY2(!cookies.contains(name), "Cookie wasn't removed from server"); | ||
276 | } | 282 | } | ||
283 | | ||||
284 | QDBusError TestWebEnginePartCookieJar::addCookieToKCookieServer(const QNetworkCookie& _cookie, const QString& host) | ||||
285 | { | ||||
286 | QNetworkCookie cookie(_cookie); | ||||
287 | QUrl url; | ||||
288 | url.setHost(host); | ||||
289 | url.setScheme(cookie.isSecure() ? "https" : "http"); | ||||
290 | if (!cookie.domain().startsWith('.')) { | ||||
291 | cookie.setDomain(QString()); | ||||
292 | } | ||||
293 | const QByteArray setCookie = "Set-Cookie: " + cookie.toRawForm(); | ||||
294 | m_server->call(QDBus::Block, "addCookies", url.toString(), setCookie, static_cast<qlonglong>(0)); | ||||
295 | return m_server->lastError(); | ||||
296 | } | ||||
297 | | ||||
298 | void TestWebEnginePartCookieJar::testPersistentCookiesAreAddedToStoreOnCreation() | ||||
299 | { | ||||
300 | delete m_jar; | ||||
301 | QDateTime exp = QDateTime::currentDateTime().addYears(1); | ||||
302 | QString baseCookieName = m_cookieName + "-startup"; | ||||
303 | QList<CookieData> data { | ||||
304 | {baseCookieName + "-persistent", "test-value", ".yyy.xxx.com", "/abc/def/", "zzz.yyy.xxx.com", currentDateTime().addYears(1), true}, | ||||
305 | {baseCookieName + "-no-path", "test-value", ".yyy.xxx.com", "", "zzz.yyy.xxx.com", currentDateTime().addYears(1), true}, | ||||
306 | {baseCookieName + "-no-domain", "test-value", "", "/abc/def/", "zzz.yyy.xxx.com", currentDateTime().addYears(1), true}, | ||||
307 | {baseCookieName + "-no-secure", "test-value", ".yyy.xxx.com", "/abc/def/", "zzz.yyy.xxx.com", currentDateTime().addYears(1), false} | ||||
308 | }; | ||||
309 | QList<QNetworkCookie> expected; | ||||
310 | for(const CookieData &d: data){ | ||||
311 | QNetworkCookie c = d.cookie(); | ||||
312 | QDBusError e = addCookieToKCookieServer(c, d.host); | ||||
313 | QVERIFY2(!e.isValid(), qPrintable(e.message())); | ||||
314 | //In case of an empty domain, WebEnginePartCookieJar will use QNetworkCookie::normalize on the cookie | ||||
315 | if (c.domain().isEmpty()) { | ||||
316 | c.setDomain(d.host); | ||||
317 | } | ||||
318 | expected << c; | ||||
319 | } | ||||
320 | m_jar = new WebEnginePartCookieJar(m_profile, this); | ||||
321 | QList<QNetworkCookie> cookiesInsertedIntoJar; | ||||
322 | for(const QNetworkCookie &c: qAsConst(m_jar->m_testCookies)){ | ||||
dfaure: qAsConst | |||||
323 | if(QString(c.name()).startsWith(baseCookieName)) { | ||||
324 | cookiesInsertedIntoJar << c; | ||||
325 | } | ||||
326 | } | ||||
327 | | ||||
328 | //Ensure that cookies in the two lists are in the same order before comparing them | ||||
329 | //(the order in cookiesInsertedIntoJar depends on the order KCookieServer::findCookies | ||||
330 | //returns them) | ||||
331 | auto sortLambda = [](const QNetworkCookie &c1, const QNetworkCookie &c2){ | ||||
dfaure: Const ref missing for c1 and c2 | |||||
332 | return c1.name() < c2.name(); | ||||
333 | }; | ||||
334 | std::sort(cookiesInsertedIntoJar.begin(), cookiesInsertedIntoJar.end(), sortLambda); | ||||
335 | std::sort(expected.begin(), expected.end(), sortLambda); | ||||
336 | | ||||
337 | QCOMPARE(cookiesInsertedIntoJar, expected); | ||||
338 | } | ||||
339 | | ||||
340 | void TestWebEnginePartCookieJar::testSessionCookiesAreNotAddedToStoreOnCreation() | ||||
341 | { | ||||
342 | delete m_jar; | ||||
343 | CookieData data{m_cookieName + "-startup-session", "test-value", ".yyy.xxx.com", "/abc/def", "zzz.yyy.xxx.com", QDateTime(), true}; | ||||
344 | QDBusError e = addCookieToKCookieServer(data.cookie(), data.host); | ||||
345 | QVERIFY2(!e.isValid(), qPrintable(e.message())); | ||||
346 | m_jar = new WebEnginePartCookieJar(m_profile, this); | ||||
347 | QList<QNetworkCookie> cookiesInsertedIntoJar; | ||||
348 | for(const QNetworkCookie &c: qAsConst(m_jar->m_testCookies)) { | ||||
dfaure: qAsConst | |||||
349 | if (c.name() == data.name) { | ||||
350 | cookiesInsertedIntoJar << c; | ||||
351 | } | ||||
352 | } | ||||
353 | QVERIFY2(cookiesInsertedIntoJar.isEmpty(), "Session cookies inserted into cookie store"); | ||||
354 | } | ||||
355 | |
qAsConst