File: tst_OxideCookieHelper.cpp

package info (click to toggle)
morph-browser 1.1.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,424 kB
  • sloc: cpp: 12,201; javascript: 2,042; xml: 92; makefile: 43
file content (409 lines) | stat: -rw-r--r-- 14,953 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * Copyright 2014 Canonical Ltd.
 *
 * This file is part of webbrowser-app.
 *
 * webbrowser-app is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * webbrowser-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Qt
#include <QtCore/QDir>
#include <QtCore/QList>
#include <QtCore/QSet>
#include <QtCore/QTimer>
#include <QtNetwork/QNetworkCookie>
#include <QtTest/QSignalSpy>
#include <QtTest/QtTest>

// local
#include "oxide-cookie-helper.h"

typedef QList<QNetworkCookie> Cookies;

/* Fake Oxide backend implementation */
class CookieBackend : public QObject
{
    Q_OBJECT

public:
    CookieBackend(QObject *parent = 0):
        QObject(parent),
        m_lastRequestId(0)
    {}

    void sendReply(int requestId, const QList<QNetworkCookie>& failedCookies) {
        QVariant failedCookiesVariant =
            OxideCookieHelper::variantFromCookies(failedCookies);
        Q_EMIT setCookiesResponse(requestId, failedCookiesVariant);
    }

    void setFailUrls(const QSet<QUrl>& urls) { m_failUrls = urls; }

public Q_SLOTS:
    int setNetworkCookies(const QUrl& url, const QList<QNetworkCookie>& cookies) {
        if (m_failUrls.contains(url)) return -1;
        m_lastRequestId++;
        /* Handle host cookies */
        QList<QNetworkCookie> restoredCookies;
        Q_FOREACH(const QNetworkCookie& cookie, cookies) {
            QNetworkCookie c(cookie);
            if (c.domain().isEmpty()) {
                c.setDomain(url.host());
            }
            restoredCookies.append(c);
        }
        Q_EMIT setNetworkCookiesCalled(m_lastRequestId, url, restoredCookies);
        return m_lastRequestId;
    }

    void onTimerTimeout() {
        QObject *timer = sender();
        int requestId = timer->property("requestId").toInt();
        Cookies failedCookies = timer->property("failedCookies").value<Cookies>();
        sendReply(requestId, failedCookies);
    }

Q_SIGNALS:
    void setCookiesResponse(int requestId, const QVariant& failedCookies);
    void setNetworkCookiesCalled(int requestId, const QUrl& url,
                                 const QList<QNetworkCookie>& cookies);
private:
    int m_lastRequestId;
    QSet<QUrl> m_failUrls;
};

class OxideCookieHelperTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void testSetCookiesSanity();
    void testSetCookies_data();
    void testSetCookies();
    void testSetCookiesSubDomain();
    void testImmediateFailure_data();
    void testImmediateFailure();
};

void OxideCookieHelperTest::testSetCookiesSanity()
{
    OxideCookieHelper helper;
    QSignalSpy cookiesSet(&helper,
                          SIGNAL(cookiesSet(const QList<QNetworkCookie>&)));

    QVERIFY(helper.oxideStoreBackend() == 0);

    Cookies cookies = QNetworkCookie::parseCookies("a=2\nb=3");
    QCOMPARE(cookies.count(), 2);

    helper.setCookies(cookies);
    QTest::qWait(10);
    QCOMPARE(cookiesSet.count(), 0);

    CookieBackend backend;
    helper.setOxideStoreBackend(&backend);
    QCOMPARE(helper.oxideStoreBackend(), &backend);
    QCOMPARE(helper.property("oxideStoreBackend").value<QObject*>(),
             &backend);
}

void OxideCookieHelperTest::testSetCookies_data()
{
    QTest::addColumn<QString>("domain1");
    QTest::addColumn<QString>("cookies1");
    QTest::addColumn<int>("timeout1");
    QTest::addColumn<QString>("failedcookies1");

    QTest::addColumn<QString>("domain2");
    QTest::addColumn<QString>("cookies2");
    QTest::addColumn<int>("timeout2");
    QTest::addColumn<QString>("failedcookies2");

    QTest::addColumn<QString>("domain3");
    QTest::addColumn<QString>("cookies3");
    QTest::addColumn<int>("timeout3");
    QTest::addColumn<QString>("failedcookies3");

    QTest::newRow("empty") <<
        QString() << QString() << 0 << QString() <<
        QString() << QString() << 0 << QString() <<
        QString() << QString() << 0 << QString();

    QTest::newRow("one domain, success") <<
        "example.org" <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly\n"
        "c=something; Domain=example.org; Secure" << 10 << QString() <<
        QString() << QString() << 0 << QString() <<
        QString() << QString() << 0 << QString();

    QTest::newRow("one domain, with one failure") <<
        "example.org" <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly\n"
        "c=something; Domain=example.org; Secure" << 10 <<
        "b=2; Domain=example.org; HttpOnly" <<
        QString() << QString() << 0 << QString() <<
        QString() << QString() << 0 << QString();

    QTest::newRow("three domains, success") <<
        "example.org" <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "m=2; Domain=example.org; HttpOnly\n"
        "z=something; Domain=example.org; Secure" << 10 << QString() <<

        "domain.net" <<
        "c=4; Domain=domain.net; HttpOnly\n"
        "r=no; Domain=domain.net; Expires=Wed, 13 Jan 2021 22:23:01 GMT" << 20 << QString() <<

        "sub.site.org" <<
        "d=yes; Domain=sub.site.org; Secure\n"
        "e=3; Domain=sub.site.org; HttpOnly\n"
        "z=last; Domain=sub.site.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT" << 40 << QString();

    QTest::newRow("three domains, some failures") <<
        "example.org" <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "m=2; Domain=example.org; HttpOnly\n"
        "z=something; Domain=example.org; Secure" << 10 << QString() <<

        "domain.net" <<
        "c=4; Domain=domain.net; HttpOnly\n"
        "r=no; Domain=domain.net; Expires=Wed, 13 Jan 2021 22:23:01 GMT" << 40 <<
        "c=4; Domain=domain.net; HttpOnly" <<

        "sub.site.org" <<
        "d=yes; Domain=sub.site.org; Secure\n"
        "e=3; Domain=sub.site.org; HttpOnly\n"
        "z=last; Domain=sub.site.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT" << 20 <<
        "e=3; Domain=sub.site.org; HttpOnly\n"
        "z=last; Domain=sub.site.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT";
}

struct DomainData {
    DomainData(): timeout(0) {}
    DomainData(const QString& rawCookies, int t, const QString& rawFailedCookies):
        cookies(QNetworkCookie::parseCookies(rawCookies.toUtf8())),
        timeout(t),
        failedCookies(QNetworkCookie::parseCookies(rawFailedCookies.toUtf8()))
    {}

    Cookies cookies;
    int timeout;
    Cookies failedCookies;
};

static bool cookieCompare(const QNetworkCookie a, const QNetworkCookie b)
{
    return a.name() < b.name();
}

void OxideCookieHelperTest::testSetCookies()
{
    QFETCH(QString, domain1);
    QFETCH(QString, cookies1);
    QFETCH(int, timeout1);
    QFETCH(QString, failedcookies1);

    QFETCH(QString, domain2);
    QFETCH(QString, cookies2);
    QFETCH(int, timeout2);
    QFETCH(QString, failedcookies2);

    QFETCH(QString, domain3);
    QFETCH(QString, cookies3);
    QFETCH(int, timeout3);
    QFETCH(QString, failedcookies3);

    /* Build a data structure easier to handle */
    QMap<QString,DomainData> domains;
    if (!domain1.isEmpty()) domains[domain1] = DomainData(cookies1, timeout1, failedcookies1);
    if (!domain2.isEmpty()) domains[domain2] = DomainData(cookies2, timeout2, failedcookies2);
    if (!domain3.isEmpty()) domains[domain3] = DomainData(cookies3, timeout3, failedcookies3);

    OxideCookieHelper helper;
    QSignalSpy cookiesSet(&helper,
                          SIGNAL(cookiesSet(const QList<QNetworkCookie>&)));

    CookieBackend backend;
    QSignalSpy setNetworkCookiesCalled(&backend,
                                       SIGNAL(setNetworkCookiesCalled(int,const QUrl&,const QList<QNetworkCookie>&)));
    helper.setOxideStoreBackend(&backend);

    /* Build the list of cookies */
    Cookies cookies;
    Cookies expectedFailedCookies;
    Q_FOREACH(const DomainData &domainData, domains) {
        cookies.append(domainData.cookies);
        expectedFailedCookies.append(domainData.failedCookies);
    }
    qSort(cookies.begin(), cookies.end(), cookieCompare);

    helper.setCookies(cookies);
    QCOMPARE(setNetworkCookiesCalled.count(), domains.count());
    QList<QVariantList> setNetworkCookiesCalls = setNetworkCookiesCalled;
    Q_FOREACH(const QVariantList &args, setNetworkCookiesCalls) {
        int requestId = args.at(0).toInt();
        QUrl url = args.at(1).toUrl();
        Cookies domainCookies = args.at(2).value<Cookies>();

        QVERIFY(domains.contains(url.host()));
        /* Compare the cookies lists; we don't care about the order, so let's
         * sort them before comparing */
        const DomainData &domainData = domains[url.host()];
        qSort(domainCookies.begin(), domainCookies.end(), cookieCompare);
        Cookies expectedCookies = domainData.cookies;
        qSort(expectedCookies.begin(), expectedCookies.end(), cookieCompare);
        QCOMPARE(domainCookies, expectedCookies);

        QTimer* timer = new QTimer(&backend);
        timer->setSingleShot(true);
        timer->setInterval(domainData.timeout);
        timer->setProperty("requestId", requestId);
        timer->setProperty("failedCookies", QVariant::fromValue(domainData.failedCookies));
        QObject::connect(timer, SIGNAL(timeout()),
                         &backend, SLOT(onTimerTimeout()));
        timer->start();
    }

    QVERIFY(cookiesSet.wait());
    QCOMPARE(cookiesSet.count(), 1);

    /* Compare the failed cookies */
    qSort(expectedFailedCookies.begin(), expectedFailedCookies.end(), cookieCompare);
    Cookies failedCookies = cookiesSet.at(0).at(0).value<Cookies>();
    qSort(failedCookies.begin(), failedCookies.end(), cookieCompare);
    QCOMPARE(failedCookies, expectedFailedCookies);
}

void OxideCookieHelperTest::testSetCookiesSubDomain()
{
    OxideCookieHelper helper;
    QSignalSpy cookiesSet(&helper,
                          SIGNAL(cookiesSet(const QList<QNetworkCookie>&)));

    Cookies cookies = QNetworkCookie::parseCookies("a=2; Domain=.example.org");
    QCOMPARE(cookies.count(), 1);

    CookieBackend backend;
    QSignalSpy setNetworkCookiesCalled(&backend,
                                       SIGNAL(setNetworkCookiesCalled(int,const QUrl&,const QList<QNetworkCookie>&)));
    helper.setOxideStoreBackend(&backend);

    helper.setCookies(cookies);
    QCOMPARE(setNetworkCookiesCalled.count(), 1);
    QUrl url = setNetworkCookiesCalled.at(0).at(1).toUrl();

    QCOMPARE(url.host(), QString("example.org"));
}

void OxideCookieHelperTest::testImmediateFailure_data()
{
    QTest::addColumn<QString>("rawCookies");
    QTest::addColumn<QSet<QUrl> >("failUrls");
    QTest::addColumn<QString>("rawFailedcookies");

    QSet<QUrl> failUrls;

    failUrls.insert(QUrl("http://example.org"));
    QTest::newRow("one domain, one failure") <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly" <<
        failUrls <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly";
    failUrls.clear();

    failUrls.insert(QUrl("http://domain.net"));
    QTest::newRow("multiple domains, one failure") <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly\n"
        "c=4; Domain=domain.net; HttpOnly\n"
        "d=yes; Domain=sub.site.org; Secure\n"
        "e=3; Domain=sub.site.org; HttpOnly\n"
        "z=last; Domain=sub.site.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT" <<
        failUrls <<
        "c=4; Domain=domain.net; HttpOnly";
    failUrls.clear();

    failUrls.insert(QUrl("http://example.org"));
    failUrls.insert(QUrl("http://domain.net"));
    failUrls.insert(QUrl("http://sub.site.org"));
    QTest::newRow("multiple domains, all failures") <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly\n"
        "c=4; Domain=domain.net; HttpOnly\n"
        "d=yes; Domain=sub.site.org; Secure\n"
        "e=3; Domain=sub.site.org; HttpOnly\n"
        "z=last; Domain=sub.site.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT" <<
        failUrls <<
        "a=0; Domain=example.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT\n"
        "b=2; Domain=example.org; HttpOnly\n"
        "c=4; Domain=domain.net; HttpOnly\n"
        "d=yes; Domain=sub.site.org; Secure\n"
        "e=3; Domain=sub.site.org; HttpOnly\n"
        "z=last; Domain=sub.site.org; Expires=Wed, 13 Jan 2021 22:23:01 GMT";
    failUrls.clear();
}

void OxideCookieHelperTest::testImmediateFailure()
{
    QFETCH(QString, rawCookies);
    QFETCH(QSet<QUrl>, failUrls);
    QFETCH(QString, rawFailedcookies);

    OxideCookieHelper helper;
    QSignalSpy cookiesSet(&helper,
                          SIGNAL(cookiesSet(const QList<QNetworkCookie>&)));

    CookieBackend backend;
    QSignalSpy setNetworkCookiesCalled(&backend,
                                       SIGNAL(setNetworkCookiesCalled(int,const QUrl&,const QList<QNetworkCookie>&)));
    helper.setOxideStoreBackend(&backend);
    backend.setFailUrls(failUrls);

    /* Build the list of cookies */
    Cookies cookies = QNetworkCookie::parseCookies(rawCookies.toUtf8());
    Cookies expectedFailedCookies =
        QNetworkCookie::parseCookies(rawFailedcookies.toUtf8());

    helper.setCookies(cookies);

    /* If there were valid calls, reply to them */
    QList<QVariantList> setNetworkCookiesCalls = setNetworkCookiesCalled;
    Q_FOREACH(const QVariantList &args, setNetworkCookiesCalls) {
        int requestId = args.at(0).toInt();
        QUrl url = args.at(1).toUrl();
        Cookies domainCookies = args.at(2).value<Cookies>();

        QTimer* timer = new QTimer(&backend);
        timer->setSingleShot(true);
        timer->setInterval(5);
        timer->setProperty("requestId", requestId);
        QObject::connect(timer, SIGNAL(timeout()),
                         &backend, SLOT(onTimerTimeout()));
        timer->start();
    }

    QVERIFY(cookiesSet.wait());
    QCOMPARE(cookiesSet.count(), 1);

    /* Compare the failed cookies */
    qSort(expectedFailedCookies.begin(), expectedFailedCookies.end(), cookieCompare);
    Cookies failedCookies = cookiesSet.at(0).at(0).value<Cookies>();
    qSort(failedCookies.begin(), failedCookies.end(), cookieCompare);
    QCOMPARE(failedCookies, expectedFailedCookies);
}

QTEST_MAIN(OxideCookieHelperTest)
#include "tst_OxideCookieHelper.moc"