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
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtNetwork/QNetworkCookieJar>
class tst_QNetworkCookieJar: public QObject
{
Q_OBJECT
private slots:
void getterSetter();
void setCookiesFromUrl_data();
void setCookiesFromUrl();
void cookiesForUrl_data();
void cookiesForUrl();
};
QT_BEGIN_NAMESPACE
namespace QTest {
template<>
char *toString(const QNetworkCookie &cookie)
{
return qstrdup(cookie.toRawForm());
}
template<>
char *toString(const QList<QNetworkCookie> &list)
{
QString result = "QList(";
bool first = true;
foreach (QNetworkCookie cookie, list) {
if (!first)
result += ", ";
first = false;
result += QString::fromLatin1("QNetworkCookie(%1)").arg(QLatin1String(cookie.toRawForm()));
}
return qstrdup(result.append(')').toLocal8Bit());
}
}
QT_END_NAMESPACE
class MyCookieJar: public QNetworkCookieJar
{
public:
inline QList<QNetworkCookie> allCookies() const
{ return QNetworkCookieJar::allCookies(); }
inline void setAllCookies(const QList<QNetworkCookie> &cookieList)
{ QNetworkCookieJar::setAllCookies(cookieList); }
};
void tst_QNetworkCookieJar::getterSetter()
{
MyCookieJar jar;
QVERIFY(jar.allCookies().isEmpty());
QList<QNetworkCookie> list;
QNetworkCookie cookie;
cookie.setName("a");
list << cookie;
jar.setAllCookies(list);
QCOMPARE(jar.allCookies(), list);
}
void tst_QNetworkCookieJar::setCookiesFromUrl_data()
{
QTest::addColumn<QList<QNetworkCookie> >("preset");
QTest::addColumn<QNetworkCookie>("newCookie");
QTest::addColumn<QString>("referenceUrl");
QTest::addColumn<QList<QNetworkCookie> >("expectedResult");
QTest::addColumn<bool>("setCookies");
QList<QNetworkCookie> preset;
QList<QNetworkCookie> result;
QNetworkCookie cookie;
cookie.setName("a");
cookie.setPath("/");
cookie.setDomain("www.foo.tld");
result += cookie;
QTest::newRow("just-add") << preset << cookie << "http://www.foo.tld" << result << true;
preset = result;
QTest::newRow("replace-1") << preset << cookie << "http://www.foo.tld" << result << true;
cookie.setValue("bc");
result.clear();
result += cookie;
QTest::newRow("replace-2") << preset << cookie << "http://www.foo.tld" << result << true;
preset = result;
cookie.setName("d");
result += cookie;
QTest::newRow("append") << preset << cookie << "http://www.foo.tld" << result << true;
cookie = preset.at(0);
result = preset;
cookie.setPath("/something");
result += cookie;
QTest::newRow("diff-path") << preset << cookie << "http://www.foo.tld/something" << result << true;
preset.clear();
preset += cookie;
cookie.setPath("/");
QTest::newRow("diff-path-order") << preset << cookie << "http://www.foo.tld" << result << true;
// security test:
result.clear();
preset.clear();
cookie.setDomain("something.completely.different");
QTest::newRow("security-domain-1") << preset << cookie << "http://www.foo.tld" << result << false;
cookie.setDomain("www.foo.tld");
cookie.setPath("/something");
QTest::newRow("security-path-1") << preset << cookie << "http://www.foo.tld" << result << false;
// setting the defaults:
QNetworkCookie finalCookie = cookie;
finalCookie.setPath("/something/");
cookie.setPath("");
cookie.setDomain("");
result.clear();
result += finalCookie;
QTest::newRow("defaults-1") << preset << cookie << "http://www.foo.tld/something/" << result << true;
finalCookie.setPath("/");
result.clear();
result += finalCookie;
QTest::newRow("defaults-2") << preset << cookie << "http://www.foo.tld" << result << true;
// security test: do not accept cookie domains like ".com" nor ".com." (see RFC 2109 section 4.3.2)
result.clear();
preset.clear();
cookie.setDomain(".com");
QTest::newRow("rfc2109-4.3.2-ex3") << preset << cookie << "http://x.foo.com" << result << false;
result.clear();
preset.clear();
cookie.setDomain(".com.");
QTest::newRow("rfc2109-4.3.2-ex3-2") << preset << cookie << "http://x.foo.com" << result << false;
}
void tst_QNetworkCookieJar::setCookiesFromUrl()
{
QFETCH(QList<QNetworkCookie>, preset);
QFETCH(QNetworkCookie, newCookie);
QFETCH(QString, referenceUrl);
QFETCH(QList<QNetworkCookie>, expectedResult);
QFETCH(bool, setCookies);
QList<QNetworkCookie> cookieList;
cookieList += newCookie;
MyCookieJar jar;
jar.setAllCookies(preset);
QCOMPARE(jar.setCookiesFromUrl(cookieList, referenceUrl), setCookies);
QList<QNetworkCookie> result = jar.allCookies();
foreach (QNetworkCookie cookie, expectedResult) {
QVERIFY2(result.contains(cookie), cookie.toRawForm());
result.removeAll(cookie);
}
QVERIFY2(result.isEmpty(), QTest::toString(result));
}
void tst_QNetworkCookieJar::cookiesForUrl_data()
{
QTest::addColumn<QList<QNetworkCookie> >("allCookies");
QTest::addColumn<QString>("url");
QTest::addColumn<QList<QNetworkCookie> >("expectedResult");
QList<QNetworkCookie> allCookies;
QList<QNetworkCookie> result;
QTest::newRow("no-cookies") << allCookies << "http://foo.bar/" << result;
QNetworkCookie cookie;
cookie.setName("a");
cookie.setPath("/web");
cookie.setDomain(".nokia.com");
allCookies += cookie;
QTest::newRow("no-match-1") << allCookies << "http://foo.bar/" << result;
QTest::newRow("no-match-2") << allCookies << "http://foo.bar/web" << result;
QTest::newRow("no-match-3") << allCookies << "http://foo.bar/web/wiki" << result;
QTest::newRow("no-match-4") << allCookies << "http://nokia.com" << result;
QTest::newRow("no-match-5") << allCookies << "http://qt.nokia.com" << result;
QTest::newRow("no-match-6") << allCookies << "http://nokia.com/webinar" << result;
QTest::newRow("no-match-7") << allCookies << "http://qt.nokia.com/webinar" << result;
result = allCookies;
QTest::newRow("match-1") << allCookies << "http://nokia.com/web" << result;
QTest::newRow("match-2") << allCookies << "http://nokia.com/web/" << result;
QTest::newRow("match-3") << allCookies << "http://nokia.com/web/content" << result;
QTest::newRow("match-4") << allCookies << "http://qt.nokia.com/web" << result;
QTest::newRow("match-4") << allCookies << "http://qt.nokia.com/web/" << result;
QTest::newRow("match-6") << allCookies << "http://qt.nokia.com/web/content" << result;
cookie.setPath("/web/wiki");
allCookies += cookie;
// exact same results as before:
QTest::newRow("one-match-1") << allCookies << "http://nokia.com/web" << result;
QTest::newRow("one-match-2") << allCookies << "http://nokia.com/web/" << result;
QTest::newRow("one-match-3") << allCookies << "http://nokia.com/web/content" << result;
QTest::newRow("one-match-4") << allCookies << "http://qt.nokia.com/web" << result;
QTest::newRow("one-match-4") << allCookies << "http://qt.nokia.com/web/" << result;
QTest::newRow("one-match-6") << allCookies << "http://qt.nokia.com/web/content" << result;
result.prepend(cookie); // longer path, it must match first
QTest::newRow("two-matches-1") << allCookies << "http://nokia.com/web/wiki" << result;
QTest::newRow("two-matches-2") << allCookies << "http://qt.nokia.com/web/wiki" << result;
// invert the order;
allCookies.clear();
allCookies << result.at(1) << result.at(0);
QTest::newRow("two-matches-3") << allCookies << "http://nokia.com/web/wiki" << result;
QTest::newRow("two-matches-4") << allCookies << "http://qt.nokia.com/web/wiki" << result;
// expired cookie
allCookies.clear();
cookie.setExpirationDate(QDateTime::fromString("09-Nov-1999", "dd-MMM-yyyy"));
allCookies += cookie;
result.clear();
QTest::newRow("exp-match-1") << allCookies << "http://nokia.com/web" << result;
QTest::newRow("exp-match-2") << allCookies << "http://nokia.com/web/" << result;
QTest::newRow("exp-match-3") << allCookies << "http://nokia.com/web/content" << result;
QTest::newRow("exp-match-4") << allCookies << "http://qt.nokia.com/web" << result;
QTest::newRow("exp-match-4") << allCookies << "http://qt.nokia.com/web/" << result;
QTest::newRow("exp-match-6") << allCookies << "http://qt.nokia.com/web/content" << result;
}
void tst_QNetworkCookieJar::cookiesForUrl()
{
QFETCH(QList<QNetworkCookie>, allCookies);
QFETCH(QString, url);
QFETCH(QList<QNetworkCookie>, expectedResult);
MyCookieJar jar;
jar.setAllCookies(allCookies);
QList<QNetworkCookie> result = jar.cookiesForUrl(url);
QCOMPARE(result, expectedResult);
}
QTEST_MAIN(tst_QNetworkCookieJar)
#include "tst_qnetworkcookiejar.moc"
|