| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
|
| 30 |
#include <QtTest/QtTest> |
| 31 |
#include <QtCore/QJsonArray> |
| 32 |
#include <QtCore/QJsonDocument> |
| 33 |
#include <QtCore/QJsonObject> |
| 34 |
#include <QtCore/QJsonValue> |
| 35 |
#include <QtNetwork/QNetworkCookieJar> |
| 36 |
#include <QtNetwork/QNetworkCookie> |
| 37 |
#include <QtNetwork/QNetworkRequest> |
| 38 |
#if QT_CONFIG(topleveldomain) |
| 39 |
#include "private/qtldurl_p.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
#include <memory> |
| 43 |
|
| 44 |
class tst_QNetworkCookieJar: public QObject |
| 45 |
{ |
| 46 |
Q_OBJECT |
| 47 |
|
| 48 |
private slots: |
| 49 |
void getterSetter(); |
| 50 |
void setCookiesFromUrl_data(); |
| 51 |
void setCookiesFromUrl(); |
| 52 |
void cookiesForUrl_data(); |
| 53 |
void cookiesForUrl(); |
| 54 |
#if defined(QT_BUILD_INTERNAL) && QT_CONFIG(topleveldomain) |
| 55 |
void effectiveTLDs_data(); |
| 56 |
void effectiveTLDs(); |
| 57 |
#endif |
| 58 |
void rfc6265_data(); |
| 59 |
void rfc6265(); |
| 60 |
}; |
| 61 |
|
| 62 |
class MyCookieJar: public QNetworkCookieJar |
| 63 |
{ |
| 64 |
public: |
| 65 |
~MyCookieJar() override; |
| 66 |
using QNetworkCookieJar::allCookies; |
| 67 |
using QNetworkCookieJar::setAllCookies; |
| 68 |
}; |
| 69 |
|
| 70 |
MyCookieJar::~MyCookieJar() = default; |
| 71 |
|
| 72 |
void tst_QNetworkCookieJar::getterSetter() |
| 73 |
{ |
| 74 |
MyCookieJar jar; |
| 75 |
|
| 76 |
QVERIFY(jar.allCookies().isEmpty()); |
| 77 |
|
| 78 |
QList<QNetworkCookie> list; |
| 79 |
QNetworkCookie cookie; |
| 80 |
cookie.setName("a"); |
| 81 |
list << cookie; |
| 82 |
|
| 83 |
jar.setAllCookies(list); |
| 84 |
QCOMPARE(jar.allCookies(), list); |
| 85 |
} |
| 86 |
|
| 87 |
void tst_QNetworkCookieJar::setCookiesFromUrl_data() |
| 88 |
{ |
| 89 |
QTest::addColumn<QList<QNetworkCookie> >("preset"); |
| 90 |
QTest::addColumn<QNetworkCookie>("newCookie"); |
| 91 |
QTest::addColumn<QString>("referenceUrl"); |
| 92 |
QTest::addColumn<QList<QNetworkCookie> >("expectedResult"); |
| 93 |
QTest::addColumn<bool>("setCookies"); |
| 94 |
|
| 95 |
QList<QNetworkCookie> preset; |
| 96 |
QList<QNetworkCookie> result; |
| 97 |
QNetworkCookie cookie; |
| 98 |
|
| 99 |
cookie.setName("a"); |
| 100 |
cookie.setPath("/"); |
| 101 |
cookie.setDomain(".foo.tld"); |
| 102 |
result += cookie; |
| 103 |
QTest::newRow("just-add") << preset << cookie << "http://www.foo.tld" << result << true; |
| 104 |
|
| 105 |
preset = result; |
| 106 |
QTest::newRow("replace-1") << preset << cookie << "http://www.foo.tld" << result << true; |
| 107 |
|
| 108 |
cookie.setValue("bc"); |
| 109 |
result.clear(); |
| 110 |
result += cookie; |
| 111 |
QTest::newRow("replace-2") << preset << cookie << "http://www.foo.tld" << result << true; |
| 112 |
|
| 113 |
preset = result; |
| 114 |
cookie.setName("d"); |
| 115 |
result += cookie; |
| 116 |
QTest::newRow("append") << preset << cookie << "http://www.foo.tld" << result << true; |
| 117 |
|
| 118 |
cookie = preset.at(0); |
| 119 |
result = preset; |
| 120 |
cookie.setPath("/something"); |
| 121 |
result += cookie; |
| 122 |
QTest::newRow("diff-path") << preset << cookie << "http://www.foo.tld/something" << result << true; |
| 123 |
|
| 124 |
preset.clear(); |
| 125 |
preset += cookie; |
| 126 |
cookie.setPath("/"); |
| 127 |
QTest::newRow("diff-path-order") << preset << cookie << "http://www.foo.tld" << result << true; |
| 128 |
|
| 129 |
preset.clear(); |
| 130 |
result.clear(); |
| 131 |
QNetworkCookie finalCookie = cookie; |
| 132 |
cookie.setDomain("foo.tld"); |
| 133 |
finalCookie.setDomain(".foo.tld"); |
| 134 |
result += finalCookie; |
| 135 |
QTest::newRow("should-add-dot-prefix") << preset << cookie << "http://www.foo.tld" << result << true; |
| 136 |
|
| 137 |
result.clear(); |
| 138 |
cookie.setDomain(""); |
| 139 |
finalCookie.setDomain("www.foo.tld"); |
| 140 |
result += finalCookie; |
| 141 |
QTest::newRow("should-set-default-domain") << preset << cookie << "http://www.foo.tld" << result << true; |
| 142 |
|
| 143 |
// security test: |
| 144 |
result.clear(); |
| 145 |
preset.clear(); |
| 146 |
cookie.setDomain("something.completely.different"); |
| 147 |
QTest::newRow("security-domain-1") << preset << cookie << "http://www.foo.tld" << result << false; |
| 148 |
|
| 149 |
// we want the cookie to be accepted although the path does not match, see QTBUG-5815 |
| 150 |
cookie.setDomain(".foo.tld"); |
| 151 |
cookie.setPath("/something"); |
| 152 |
result += cookie; |
| 153 |
QTest::newRow("security-path-1") << preset << cookie << "http://www.foo.tld" << result << true; |
| 154 |
|
| 155 |
// check effective TLDs |
| 156 |
// 1. co.uk is an effective TLD, should be denied |
| 157 |
result.clear(); |
| 158 |
preset.clear(); |
| 159 |
cookie.setPath("/"); |
| 160 |
cookie.setDomain(".co.uk"); |
| 161 |
QTest::newRow("effective-tld1-denied") << preset << cookie << "http://something.co.uk" << result << false; |
| 162 |
cookie.setDomain("co.uk"); |
| 163 |
QTest::newRow("effective-tld1-denied2") << preset << cookie << "http://something.co.uk" << result << false; |
| 164 |
cookie.setDomain(".something.co.uk"); |
| 165 |
result += cookie; |
| 166 |
QTest::newRow("effective-tld1-accepted") << preset << cookie << "http://something.co.uk" << result << true; |
| 167 |
|
| 168 |
// 2. anything .ck is an effective TLD ('*.ck'), but 'www.ck' is an exception |
| 169 |
result.clear(); |
| 170 |
preset.clear(); |
| 171 |
cookie.setDomain(".ck"); |
| 172 |
QTest::newRow("effective-tld.ck-denied") << preset << cookie << "http://foo.ck" << result << false; |
| 173 |
result.clear(); |
| 174 |
preset.clear(); |
| 175 |
cookie.setDomain(".foo.ck"); |
| 176 |
result += cookie; |
| 177 |
QTest::newRow("effective-tld2-accepted2") << preset << cookie << "http://foo.ck" << result << true; |
| 178 |
result.clear(); |
| 179 |
QTest::newRow("effective-tld2-denied2") << preset << cookie << "http://www.foo.ck" << result << false; |
| 180 |
QTest::newRow("effective-tld2-denied3") << preset << cookie << "http://www.anything.foo.ck" << result << false; |
| 181 |
cookie.setDomain(".www.ck"); |
| 182 |
result += cookie; |
| 183 |
QTest::newRow("effective-tld2-accepted") << preset << cookie << "http://www.www.ck" << result << true; |
| 184 |
|
| 185 |
result.clear(); |
| 186 |
preset.clear(); |
| 187 |
cookie.setDomain("127.0.0.1"); |
| 188 |
result += cookie; |
| 189 |
QTest::newRow("IPv4-address-as-domain") << preset << cookie << "http://127.0.0.1/" << result << true; |
| 190 |
|
| 191 |
result.clear(); |
| 192 |
preset.clear(); |
| 193 |
cookie.setDomain("fe80::250:56ff:fec0:1"); |
| 194 |
result += cookie; |
| 195 |
QTest::newRow("IPv6-address-as-domain") << preset << cookie << "http://[fe80::250:56ff:fec0:1]/" << result << true; |
| 196 |
|
| 197 |
// setting the defaults: |
| 198 |
finalCookie = cookie; |
| 199 |
finalCookie.setPath("/something/"); |
| 200 |
finalCookie.setDomain("www.foo.tld"); |
| 201 |
cookie.setPath(""); |
| 202 |
cookie.setDomain(""); |
| 203 |
result.clear(); |
| 204 |
result += finalCookie; |
| 205 |
QTest::newRow("defaults-1") << preset << cookie << "http://www.foo.tld/something/" << result << true; |
| 206 |
|
| 207 |
finalCookie.setPath("/"); |
| 208 |
result.clear(); |
| 209 |
result += finalCookie; |
| 210 |
QTest::newRow("defaults-2") << preset << cookie << "http://www.foo.tld" << result << true; |
| 211 |
|
| 212 |
// security test: do not accept cookie domains like ".com" nor ".com." (see RFC 2109 section 4.3.2) |
| 213 |
result.clear(); |
| 214 |
preset.clear(); |
| 215 |
cookie.setDomain(".com"); |
| 216 |
QTest::newRow("rfc2109-4.3.2-ex3") << preset << cookie << "http://x.foo.com" << result << false; |
| 217 |
|
| 218 |
result.clear(); |
| 219 |
preset.clear(); |
| 220 |
cookie.setDomain(".com."); |
| 221 |
QTest::newRow("rfc2109-4.3.2-ex3-2") << preset << cookie << "http://x.foo.com" << result << false; |
| 222 |
|
| 223 |
// When using a TLD as a hostname the hostname should still get cookies (QTBUG-52040) |
| 224 |
// ... and nothing else should get the cookies. |
| 225 |
result.clear(); |
| 226 |
preset.clear(); |
| 227 |
cookie.setPath("/"); |
| 228 |
cookie.setDomain(".support"); |
| 229 |
result += cookie; |
| 230 |
QTest::newRow("TLD-as-domain-accepted") << preset << cookie << "http://support" << result << true; |
| 231 |
result.clear(); |
| 232 |
QTest::newRow("TLD-as-domain-rejected") << preset << cookie << "http://a.support" << result << false; |
| 233 |
// Now test with no domain in the cookie, use the domain from the url (matching TLD) |
| 234 |
cookie.setDomain("support"); |
| 235 |
result += cookie; |
| 236 |
cookie.setDomain(""); |
| 237 |
QTest::newRow("TLD-as-domain-accepted2") << preset << cookie << "http://support" << result << true; |
| 238 |
} |
| 239 |
|
| 240 |
void tst_QNetworkCookieJar::setCookiesFromUrl() |
| 241 |
{ |
| 242 |
QFETCH(QList<QNetworkCookie>, preset); |
| 243 |
QFETCH(QNetworkCookie, newCookie); |
| 244 |
QFETCH(QString, referenceUrl); |
| 245 |
QFETCH(QList<QNetworkCookie>, expectedResult); |
| 246 |
QFETCH(bool, setCookies); |
| 247 |
|
| 248 |
QList<QNetworkCookie> cookieList; |
| 249 |
cookieList += newCookie; |
| 250 |
MyCookieJar jar; |
| 251 |
jar.setAllCookies(preset); |
| 252 |
QCOMPARE(jar.setCookiesFromUrl(cookieList, referenceUrl), setCookies); |
| 253 |
|
| 254 |
QList<QNetworkCookie> result = jar.allCookies(); |
| 255 |
foreach (QNetworkCookie cookie, expectedResult) { |
| 256 |
QVERIFY2(result.contains(cookie), cookie.toRawForm()); |
| 257 |
result.removeAll(cookie); |
| 258 |
} |
| 259 |
QVERIFY2(result.isEmpty(), std::unique_ptr<char[]>(QTest::toString(result)).get()); |
| 260 |
} |
| 261 |
|
| 262 |
void tst_QNetworkCookieJar::cookiesForUrl_data() |
| 263 |
{ |
| 264 |
QTest::addColumn<QList<QNetworkCookie> >("allCookies"); |
| 265 |
QTest::addColumn<QString>("url"); |
| 266 |
QTest::addColumn<QList<QNetworkCookie> >("expectedResult"); |
| 267 |
|
| 268 |
QList<QNetworkCookie> allCookies; |
| 269 |
QList<QNetworkCookie> result; |
| 270 |
|
| 271 |
QTest::newRow("no-cookies") << allCookies << "http://foo.bar/" << result; |
| 272 |
|
| 273 |
QNetworkCookie cookie; |
| 274 |
cookie.setName("a"); |
| 275 |
cookie.setPath("/web"); |
| 276 |
cookie.setDomain(".qt-project.org"); |
| 277 |
allCookies += cookie; |
| 278 |
|
| 279 |
QTest::newRow("no-match-1") << allCookies << "http://foo.bar/" << result; |
| 280 |
QTest::newRow("no-match-2") << allCookies << "http://foo.bar/web" << result; |
| 281 |
QTest::newRow("no-match-3") << allCookies << "http://foo.bar/web/wiki" << result; |
| 282 |
QTest::newRow("no-match-4") << allCookies << "http://qt-project.org" << result; |
| 283 |
QTest::newRow("no-match-5") << allCookies << "http://qt-project.org" << result; |
| 284 |
QTest::newRow("no-match-6") << allCookies << "http://qt-project.org/webinar" << result; |
| 285 |
QTest::newRow("no-match-7") << allCookies << "http://qt-project.org/webinar" << result; |
| 286 |
QTest::newRow("no-match-8") << allCookies << "http://qt-project.org./web" << result; |
| 287 |
QTest::newRow("no-match-9") << allCookies << "http://qt-project.org./web" << result; |
| 288 |
|
| 289 |
result = allCookies; |
| 290 |
QTest::newRow("match-1") << allCookies << "http://qt-project.org/web" << result; |
| 291 |
QTest::newRow("match-2") << allCookies << "http://qt-project.org/web/" << result; |
| 292 |
QTest::newRow("match-3") << allCookies << "http://qt-project.org/web/content" << result; |
| 293 |
QTest::newRow("match-4") << allCookies << "http://qt-project.org/web" << result; |
| 294 |
QTest::newRow("match-5") << allCookies << "http://qt-project.org/web/" << result; |
| 295 |
QTest::newRow("match-6") << allCookies << "http://qt-project.org/web/content" << result; |
| 296 |
|
| 297 |
cookie.setPath("/web/wiki"); |
| 298 |
allCookies += cookie; |
| 299 |
|
| 300 |
// exact same results as before: |
| 301 |
QTest::newRow("one-match-1") << allCookies << "http://qt-project.org/web" << result; |
| 302 |
QTest::newRow("one-match-2") << allCookies << "http://qt-project.org/web/" << result; |
| 303 |
QTest::newRow("one-match-3") << allCookies << "http://qt-project.org/web/content" << result; |
| 304 |
QTest::newRow("one-match-4") << allCookies << "http://qt-project.org/web" << result; |
| 305 |
QTest::newRow("one-match-5") << allCookies << "http://qt-project.org/web/" << result; |
| 306 |
QTest::newRow("one-match-6") << allCookies << "http://qt-project.org/web/content" << result; |
| 307 |
|
| 308 |
result.prepend(cookie); // longer path, it must match first |
| 309 |
QTest::newRow("two-matches-1") << allCookies << "http://qt-project.org/web/wiki" << result; |
| 310 |
QTest::newRow("two-matches-2") << allCookies << "http://qt-project.org/web/wiki" << result; |
| 311 |
|
| 312 |
// invert the order; |
| 313 |
allCookies.clear(); |
| 314 |
allCookies << result.at(1) << result.at(0); |
| 315 |
QTest::newRow("two-matches-3") << allCookies << "http://qt-project.org/web/wiki" << result; |
| 316 |
QTest::newRow("two-matches-4") << allCookies << "http://qt-project.org/web/wiki" << result; |
| 317 |
|
| 318 |
// expired cookie |
| 319 |
allCookies.clear(); |
| 320 |
cookie.setExpirationDate(QDateTime::fromString("09-Nov-1999", "dd-MMM-yyyy")); |
| 321 |
allCookies += cookie; |
| 322 |
result.clear(); |
| 323 |
QTest::newRow("exp-match-1") << allCookies << "http://qt-project.org/web" << result; |
| 324 |
QTest::newRow("exp-match-2") << allCookies << "http://qt-project.org/web/" << result; |
| 325 |
QTest::newRow("exp-match-3") << allCookies << "http://qt-project.org/web/content" << result; |
| 326 |
QTest::newRow("exp-match-4") << allCookies << "http://qt-project.org/web" << result; |
| 327 |
QTest::newRow("exp-match-5") << allCookies << "http://qt-project.org/web/" << result; |
| 328 |
QTest::newRow("exp-match-6") << allCookies << "http://qt-project.org/web/content" << result; |
| 329 |
|
| 330 |
// path matching |
| 331 |
allCookies.clear(); |
| 332 |
QNetworkCookie anotherCookie; |
| 333 |
anotherCookie.setName("a"); |
| 334 |
anotherCookie.setPath("/web"); |
| 335 |
anotherCookie.setDomain(".qt-project.org"); |
| 336 |
allCookies += anotherCookie; |
| 337 |
result.clear(); |
| 338 |
QTest::newRow("path-unmatch-1") << allCookies << "http://qt-project.org/" << result; |
| 339 |
QTest::newRow("path-unmatch-2") << allCookies << "http://qt-project.org/something/else" << result; |
| 340 |
result += anotherCookie; |
| 341 |
QTest::newRow("path-match-1") << allCookies << "http://qt-project.org/web" << result; |
| 342 |
QTest::newRow("path-match-2") << allCookies << "http://qt-project.org/web/" << result; |
| 343 |
QTest::newRow("path-match-3") << allCookies << "http://qt-project.org/web/content" << result; |
| 344 |
|
| 345 |
// secure cookies |
| 346 |
allCookies.clear(); |
| 347 |
result.clear(); |
| 348 |
QNetworkCookie secureCookie; |
| 349 |
secureCookie.setName("a"); |
| 350 |
secureCookie.setPath("/web"); |
| 351 |
secureCookie.setDomain(".qt-project.org"); |
| 352 |
secureCookie.setSecure(true); |
| 353 |
allCookies += secureCookie; |
| 354 |
QTest::newRow("no-match-secure-1") << allCookies << "http://qt-project.org/web" << result; |
| 355 |
QTest::newRow("no-match-secure-2") << allCookies << "http://qt-project.org/web" << result; |
| 356 |
result += secureCookie; |
| 357 |
QTest::newRow("match-secure-1") << allCookies << "https://qt-project.org/web" << result; |
| 358 |
QTest::newRow("match-secure-2") << allCookies << "https://qt-project.org/web" << result; |
| 359 |
|
| 360 |
// domain ending in . |
| 361 |
allCookies.clear(); |
| 362 |
result.clear(); |
| 363 |
QNetworkCookie cookieDot; |
| 364 |
cookieDot.setDomain(".example.com."); |
| 365 |
cookieDot.setName("a"); |
| 366 |
allCookies += cookieDot; |
| 367 |
QTest::newRow("no-match-domain-dot") << allCookies << "http://example.com" << result; |
| 368 |
result += cookieDot; |
| 369 |
QTest::newRow("match-domain-dot") << allCookies << "http://example.com." << result; |
| 370 |
|
| 371 |
// Root path in cookie, empty url path |
| 372 |
allCookies.clear(); |
| 373 |
QNetworkCookie rootCookie; |
| 374 |
rootCookie.setName("a"); |
| 375 |
rootCookie.setPath("/"); |
| 376 |
rootCookie.setDomain("qt-project.org"); |
| 377 |
allCookies += rootCookie; |
| 378 |
result.clear(); |
| 379 |
result += rootCookie; |
| 380 |
QTest::newRow("root-path-match") << allCookies << "http://qt-project.org" << result; |
| 381 |
|
| 382 |
// Domain in cookie happens to match a TLD |
| 383 |
allCookies.clear(); |
| 384 |
QNetworkCookie tldCookie; |
| 385 |
tldCookie.setDomain(".support"); |
| 386 |
tldCookie.setName("a"); |
| 387 |
tldCookie.setValue("b"); |
| 388 |
allCookies += tldCookie; |
| 389 |
result.clear(); |
| 390 |
result += tldCookie; |
| 391 |
QTest::newRow("tld-cookie-match") << allCookies << "http://support/" << result; |
| 392 |
result.clear(); |
| 393 |
QTest::newRow("tld-cookie-no-match") << allCookies << "http://a.support/" << result; |
| 394 |
} |
| 395 |
|
| 396 |
void tst_QNetworkCookieJar::cookiesForUrl() |
| 397 |
{ |
| 398 |
QFETCH(QList<QNetworkCookie>, allCookies); |
| 399 |
QFETCH(QString, url); |
| 400 |
QFETCH(QList<QNetworkCookie>, expectedResult); |
| 401 |
|
| 402 |
MyCookieJar jar; |
| 403 |
jar.setAllCookies(allCookies); |
| 404 |
|
| 405 |
QList<QNetworkCookie> result = jar.cookiesForUrl(url); |
| 406 |
QCOMPARE(result, expectedResult); |
| 407 |
} |
| 408 |
|
| 409 |
// This test requires private API. |
| 410 |
#if defined(QT_BUILD_INTERNAL) && QT_CONFIG(topleveldomain) |
| 411 |
void tst_QNetworkCookieJar::effectiveTLDs_data() |
| 412 |
{ |
| 413 |
QTest::addColumn<QString>("domain"); |
| 414 |
QTest::addColumn<bool>("isTLD"); |
| 415 |
|
| 416 |
QTest::newRow("yes1") << "com" << true; |
| 417 |
QTest::newRow("yes2") << "de" << true; |
| 418 |
QTest::newRow("yes4") << "krodsherad.no" << true; |
| 419 |
QTest::newRow("yes5") << "1.bg" << true; |
| 420 |
QTest::newRow("yes6") << "com.cn" << true; |
| 421 |
QTest::newRow("yes7") << "org.ws" << true; |
| 422 |
QTest::newRow("yes8") << "co.uk" << true; |
| 423 |
QTest::newRow("yes10") << "hk.com" << true; |
| 424 |
QTest::newRow("yes11") << "hk.org" << true; |
| 425 |
|
| 426 |
QTest::newRow("no1") << "anything.com" << false; |
| 427 |
QTest::newRow("no2") << "anything.de" << false; |
| 428 |
QTest::newRow("no3") << "eselsberg.ulm.museum" << false; |
| 429 |
QTest::newRow("no4") << "noe.krodsherad.no" << false; |
| 430 |
QTest::newRow("no5") << "2.1.bg" << false; |
| 431 |
QTest::newRow("no6") << "foo.com.cn" << false; |
| 432 |
QTest::newRow("no7") << "something.org.ws" << false; |
| 433 |
QTest::newRow("no8") << "teatime.co.uk" << false; |
| 434 |
QTest::newRow("no9") << "bla" << false; |
| 435 |
QTest::newRow("no10") << "bla.bla" << false; |
| 436 |
QTest::newRow("no11") << "mosreg.ru" << false; |
| 437 |
|
| 438 |
const ushort s1[] = {0x74, 0x72, 0x61, 0x6e, 0xf8, 0x79, 0x2e, 0x6e, 0x6f, 0x00}; // xn--trany-yua.no |
| 439 |
const ushort s3[] = {0x7ec4, 0x7e54, 0x2e, 0x68, 0x6b, 0x00}; // xn--mk0axi.hk |
| 440 |
const ushort s4[] = {0x7f51, 0x7edc, 0x2e, 0x63, 0x6e, 0x00}; // xn--io0a7i.cn |
| 441 |
const ushort s5[] = {0x72, 0xe1, 0x68, 0x6b, 0x6b, 0x65, 0x72, 0xe1, 0x76, 0x6a, 0x75, 0x2e, 0x6e, 0x6f, 0x00}; // xn--rhkkervju-01af.no |
| 442 |
const ushort s6[] = {0xb9a, 0xbbf, 0xb99, 0xbcd, 0xb95, 0xbaa, 0xbcd, 0xbaa, 0xbc2, 0xbb0, 0xbcd, 0x00}; // xn--clchc0ea0b2g2a9gcd |
| 443 |
const ushort s7[] = {0x627, 0x644, 0x627, 0x631, 0x62f, 0x646, 0x00}; // xn--mgbayh7gpa |
| 444 |
QTest::newRow("yes-specialchars1") << QString::fromUtf16(s1) << true; |
| 445 |
QTest::newRow("yes-specialchars3") << QString::fromUtf16(s3) << true; |
| 446 |
QTest::newRow("yes-specialchars4") << QString::fromUtf16(s4) << true; |
| 447 |
QTest::newRow("yes-specialchars5") << QString::fromUtf16(s5) << true; |
| 448 |
QTest::newRow("yes-specialchars6") << QString::fromUtf16(s6) << true; |
| 449 |
QTest::newRow("yes-specialchars7") << QString::fromUtf16(s7) << true; |
| 450 |
|
| 451 |
QTest::newRow("no-specialchars1") << QString::fromUtf16(s1).prepend("something") << false; |
| 452 |
QTest::newRow("no-specialchars3") << QString::fromUtf16(s3).prepend("foo") << false; |
| 453 |
QTest::newRow("no-specialchars4") << QString::fromUtf16(s4).prepend("bar") << false; |
| 454 |
QTest::newRow("no-specialchars6") << QString::fromUtf16(s6).prepend(QLatin1Char('.') + QString::fromUtf16(s6)) << false; |
| 455 |
QTest::newRow("no-specialchars7") << QString::fromUtf16(s7).prepend("bla") << false; |
| 456 |
|
| 457 |
QTest::newRow("exception1") << "pref.iwate.jp" << false; |
| 458 |
QTest::newRow("exception2") << "omanpost.om" << false; |
| 459 |
QTest::newRow("exception3") << "omantel.om" << false; |
| 460 |
QTest::newRow("exception4") << "gobiernoelectronico.ar" << false; |
| 461 |
QTest::newRow("exception5") << "pref.ishikawa.jp" << false; |
| 462 |
|
| 463 |
QTest::newRow("yes-wildcard1") << "*.jm" << true; |
| 464 |
QTest::newRow("yes-wildcard1.5") << "anything.jm" << true; |
| 465 |
QTest::newRow("yes-wildcard2") << "something.kh" << true; |
| 466 |
QTest::newRow("no-wildcard3") << "whatever.uk" << false; // was changed at some point |
| 467 |
QTest::newRow("yes-wildcard4") << "anything.sendai.jp" << true; |
| 468 |
QTest::newRow("yes-wildcard5") << "foo.sch.uk" << true; |
| 469 |
QTest::newRow("yes-platform.sh") << "eu.platform.sh" << true; |
| 470 |
QTest::newRow("no-platform.sh") << "something.platform.sh" << false; |
| 471 |
} |
| 472 |
|
| 473 |
void tst_QNetworkCookieJar::effectiveTLDs() |
| 474 |
{ |
| 475 |
QFETCH(QString, domain); |
| 476 |
QFETCH(bool, isTLD); |
| 477 |
QCOMPARE(qIsEffectiveTLD(domain), isTLD); |
| 478 |
} |
| 479 |
#endif |
| 480 |
|
| 481 |
void tst_QNetworkCookieJar::rfc6265_data() |
| 482 |
{ |
| 483 |
QTest::addColumn<QStringList>("received"); |
| 484 |
QTest::addColumn<QList<QNetworkCookie> >("sent"); |
| 485 |
QTest::addColumn<QString>("sentTo"); |
| 486 |
|
| 487 |
QFile file(QFINDTESTDATA("parser.json")); |
| 488 |
QVERIFY(file.open(QFile::ReadOnly | QFile::Text)); |
| 489 |
QJsonDocument document; |
| 490 |
document = QJsonDocument::fromJson(file.readAll()); |
| 491 |
QVERIFY(!document.isNull()); |
| 492 |
QVERIFY(document.isArray()); |
| 493 |
|
| 494 |
foreach (const QJsonValue& testCase, document.array()) { |
| 495 |
QJsonObject testObject = testCase.toObject(); |
| 496 |
|
| 497 |
//"test" - the test case name |
| 498 |
QString testCaseName = testObject.value("test").toString(); |
| 499 |
if (testCaseName.toLower().startsWith("disabled")) |
| 500 |
continue; |
| 501 |
|
| 502 |
//"received" - the cookies received from the server |
| 503 |
QJsonArray received = testObject.value("received").toArray(); |
| 504 |
QStringList receivedList; |
| 505 |
foreach (const QJsonValue& receivedCookie, received) |
| 506 |
receivedList.append(receivedCookie.toString()); |
| 507 |
|
| 508 |
//"sent" - the cookies sent back to the server |
| 509 |
QJsonArray sent = testObject.value("sent").toArray(); |
| 510 |
QList<QNetworkCookie> sentList; |
| 511 |
foreach (const QJsonValue& sentCookie, sent) { |
| 512 |
QJsonObject sentCookieObject = sentCookie.toObject(); |
| 513 |
QNetworkCookie cookie; |
| 514 |
cookie.setName(sentCookieObject.value("name").toString().toUtf8()); |
| 515 |
cookie.setValue(sentCookieObject.value("value").toString().toUtf8()); |
| 516 |
sentList.append(cookie); |
| 517 |
} |
| 518 |
|
| 519 |
//"sent-to" - the relative url where cookies are sent |
| 520 |
QTest::newRow(qPrintable(testCaseName)) << receivedList << sentList << testObject.value("sent-to").toString(); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
void tst_QNetworkCookieJar::rfc6265() |
| 525 |
{ |
| 526 |
QFETCH(QStringList, received); |
| 527 |
QFETCH(QList<QNetworkCookie>, sent); |
| 528 |
QFETCH(QString, sentTo); |
| 529 |
|
| 530 |
QUrl receivedUrl("http://home.example.org:8888/cookie-parser"); |
| 531 |
QUrl sentUrl("http://home.example.org:8888/cookie-parser-result"); |
| 532 |
if (!sentTo.isEmpty()) |
| 533 |
sentUrl = receivedUrl.resolved(sentTo); |
| 534 |
|
| 535 |
QNetworkCookieJar jar; |
| 536 |
QList<QNetworkCookie> receivedCookies; |
| 537 |
foreach (const QString &cookieLine, received) |
| 538 |
receivedCookies.append(QNetworkCookie::parseCookies(cookieLine.toUtf8())); |
| 539 |
|
| 540 |
jar.setCookiesFromUrl(receivedCookies, receivedUrl); |
| 541 |
QList<QNetworkCookie> cookiesToSend = jar.cookiesForUrl(sentUrl); |
| 542 |
|
| 543 |
//compare cookies only using name/value, as the metadata isn't sent over the network |
| 544 |
QCOMPARE(cookiesToSend.count(), sent.count()); |
| 545 |
bool ok = true; |
| 546 |
for (int i = 0; i < cookiesToSend.count(); i++) { |
| 547 |
if (cookiesToSend.at(i).name() != sent.at(i).name()) { |
| 548 |
ok = false; |
| 549 |
break; |
| 550 |
} |
| 551 |
if (cookiesToSend.at(i).value() != sent.at(i).value()) { |
| 552 |
ok = false; |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
if (!ok) { |
| 557 |
QNetworkRequest r(sentUrl); |
| 558 |
r.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(cookiesToSend)); |
| 559 |
QString actual = QString::fromUtf8(r.rawHeader("Cookie")); |
| 560 |
r.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(sent)); |
| 561 |
QString expected = QString::fromUtf8(r.rawHeader("Cookie")); |
| 562 |
|
| 563 |
QVERIFY2(ok, qPrintable(QString("Expected: %1\nActual: %2").arg(expected).arg(actual))); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
QTEST_MAIN(tst_QNetworkCookieJar) |
| 568 |
#include "tst_qnetworkcookiejar.moc" |
| 569 |
|