author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 14 May 2010 16:40:13 +0300 | |
changeset 22 | 79de32ba3296 |
parent 18 | 2f34d5167611 |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <QtNetwork/QNetworkCookieJar> |
|
45 |
||
46 |
class tst_QNetworkCookieJar: public QObject |
|
47 |
{ |
|
48 |
Q_OBJECT |
|
49 |
||
50 |
private slots: |
|
51 |
void getterSetter(); |
|
52 |
void setCookiesFromUrl_data(); |
|
53 |
void setCookiesFromUrl(); |
|
54 |
void cookiesForUrl_data(); |
|
55 |
void cookiesForUrl(); |
|
56 |
}; |
|
57 |
||
58 |
QT_BEGIN_NAMESPACE |
|
59 |
||
60 |
namespace QTest { |
|
61 |
template<> |
|
62 |
char *toString(const QNetworkCookie &cookie) |
|
63 |
{ |
|
64 |
return qstrdup(cookie.toRawForm()); |
|
65 |
} |
|
66 |
||
67 |
template<> |
|
68 |
char *toString(const QList<QNetworkCookie> &list) |
|
69 |
{ |
|
70 |
QString result = "QList("; |
|
71 |
bool first = true; |
|
72 |
foreach (QNetworkCookie cookie, list) { |
|
73 |
if (!first) |
|
74 |
result += ", "; |
|
75 |
first = false; |
|
76 |
result += QString::fromLatin1("QNetworkCookie(%1)").arg(QLatin1String(cookie.toRawForm())); |
|
77 |
} |
|
78 |
||
79 |
return qstrdup(result.append(')').toLocal8Bit()); |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
QT_END_NAMESPACE |
|
84 |
||
85 |
class MyCookieJar: public QNetworkCookieJar |
|
86 |
{ |
|
87 |
public: |
|
88 |
inline QList<QNetworkCookie> allCookies() const |
|
89 |
{ return QNetworkCookieJar::allCookies(); } |
|
90 |
inline void setAllCookies(const QList<QNetworkCookie> &cookieList) |
|
91 |
{ QNetworkCookieJar::setAllCookies(cookieList); } |
|
92 |
}; |
|
93 |
||
94 |
void tst_QNetworkCookieJar::getterSetter() |
|
95 |
{ |
|
96 |
MyCookieJar jar; |
|
97 |
||
98 |
QVERIFY(jar.allCookies().isEmpty()); |
|
99 |
||
100 |
QList<QNetworkCookie> list; |
|
101 |
QNetworkCookie cookie; |
|
102 |
cookie.setName("a"); |
|
103 |
list << cookie; |
|
104 |
||
105 |
jar.setAllCookies(list); |
|
106 |
QCOMPARE(jar.allCookies(), list); |
|
107 |
} |
|
108 |
||
109 |
void tst_QNetworkCookieJar::setCookiesFromUrl_data() |
|
110 |
{ |
|
111 |
QTest::addColumn<QList<QNetworkCookie> >("preset"); |
|
112 |
QTest::addColumn<QNetworkCookie>("newCookie"); |
|
113 |
QTest::addColumn<QString>("referenceUrl"); |
|
114 |
QTest::addColumn<QList<QNetworkCookie> >("expectedResult"); |
|
115 |
QTest::addColumn<bool>("setCookies"); |
|
116 |
||
117 |
QList<QNetworkCookie> preset; |
|
118 |
QList<QNetworkCookie> result; |
|
119 |
QNetworkCookie cookie; |
|
120 |
||
121 |
cookie.setName("a"); |
|
122 |
cookie.setPath("/"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
123 |
cookie.setDomain(".foo.tld"); |
0 | 124 |
result += cookie; |
125 |
QTest::newRow("just-add") << preset << cookie << "http://www.foo.tld" << result << true; |
|
126 |
||
127 |
preset = result; |
|
128 |
QTest::newRow("replace-1") << preset << cookie << "http://www.foo.tld" << result << true; |
|
129 |
||
130 |
cookie.setValue("bc"); |
|
131 |
result.clear(); |
|
132 |
result += cookie; |
|
133 |
QTest::newRow("replace-2") << preset << cookie << "http://www.foo.tld" << result << true; |
|
134 |
||
135 |
preset = result; |
|
136 |
cookie.setName("d"); |
|
137 |
result += cookie; |
|
138 |
QTest::newRow("append") << preset << cookie << "http://www.foo.tld" << result << true; |
|
139 |
||
140 |
cookie = preset.at(0); |
|
141 |
result = preset; |
|
142 |
cookie.setPath("/something"); |
|
143 |
result += cookie; |
|
144 |
QTest::newRow("diff-path") << preset << cookie << "http://www.foo.tld/something" << result << true; |
|
145 |
||
146 |
preset.clear(); |
|
147 |
preset += cookie; |
|
148 |
cookie.setPath("/"); |
|
149 |
QTest::newRow("diff-path-order") << preset << cookie << "http://www.foo.tld" << result << true; |
|
150 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
preset.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
result.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
QNetworkCookie finalCookie = cookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
cookie.setDomain("foo.tld"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
finalCookie.setDomain(".foo.tld"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
result += finalCookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
QTest::newRow("should-add-dot-prefix") << preset << cookie << "http://www.foo.tld" << result << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
result.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
cookie.setDomain(""); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
finalCookie.setDomain("www.foo.tld"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
result += finalCookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
QTest::newRow("should-set-default-domain") << preset << cookie << "http://www.foo.tld" << result << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
|
0 | 165 |
// security test: |
166 |
result.clear(); |
|
167 |
preset.clear(); |
|
168 |
cookie.setDomain("something.completely.different"); |
|
169 |
QTest::newRow("security-domain-1") << preset << cookie << "http://www.foo.tld" << result << false; |
|
170 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
// we want the cookie to be accepted although the path does not match, see QTBUG-5815 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
cookie.setDomain(".foo.tld"); |
0 | 173 |
cookie.setPath("/something"); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
result += cookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
QTest::newRow("security-path-1") << preset << cookie << "http://www.foo.tld" << result << true; |
0 | 176 |
|
177 |
// setting the defaults: |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
finalCookie = cookie; |
0 | 179 |
finalCookie.setPath("/something/"); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
finalCookie.setDomain("www.foo.tld"); |
0 | 181 |
cookie.setPath(""); |
182 |
cookie.setDomain(""); |
|
183 |
result.clear(); |
|
184 |
result += finalCookie; |
|
185 |
QTest::newRow("defaults-1") << preset << cookie << "http://www.foo.tld/something/" << result << true; |
|
186 |
||
187 |
finalCookie.setPath("/"); |
|
188 |
result.clear(); |
|
189 |
result += finalCookie; |
|
190 |
QTest::newRow("defaults-2") << preset << cookie << "http://www.foo.tld" << result << true; |
|
191 |
||
192 |
// security test: do not accept cookie domains like ".com" nor ".com." (see RFC 2109 section 4.3.2) |
|
193 |
result.clear(); |
|
194 |
preset.clear(); |
|
195 |
cookie.setDomain(".com"); |
|
196 |
QTest::newRow("rfc2109-4.3.2-ex3") << preset << cookie << "http://x.foo.com" << result << false; |
|
197 |
||
198 |
result.clear(); |
|
199 |
preset.clear(); |
|
200 |
cookie.setDomain(".com."); |
|
201 |
QTest::newRow("rfc2109-4.3.2-ex3-2") << preset << cookie << "http://x.foo.com" << result << false; |
|
202 |
} |
|
203 |
||
204 |
void tst_QNetworkCookieJar::setCookiesFromUrl() |
|
205 |
{ |
|
206 |
QFETCH(QList<QNetworkCookie>, preset); |
|
207 |
QFETCH(QNetworkCookie, newCookie); |
|
208 |
QFETCH(QString, referenceUrl); |
|
209 |
QFETCH(QList<QNetworkCookie>, expectedResult); |
|
210 |
QFETCH(bool, setCookies); |
|
211 |
||
212 |
QList<QNetworkCookie> cookieList; |
|
213 |
cookieList += newCookie; |
|
214 |
MyCookieJar jar; |
|
215 |
jar.setAllCookies(preset); |
|
216 |
QCOMPARE(jar.setCookiesFromUrl(cookieList, referenceUrl), setCookies); |
|
217 |
||
218 |
QList<QNetworkCookie> result = jar.allCookies(); |
|
219 |
foreach (QNetworkCookie cookie, expectedResult) { |
|
220 |
QVERIFY2(result.contains(cookie), cookie.toRawForm()); |
|
221 |
result.removeAll(cookie); |
|
222 |
} |
|
223 |
QVERIFY2(result.isEmpty(), QTest::toString(result)); |
|
224 |
} |
|
225 |
||
226 |
void tst_QNetworkCookieJar::cookiesForUrl_data() |
|
227 |
{ |
|
228 |
QTest::addColumn<QList<QNetworkCookie> >("allCookies"); |
|
229 |
QTest::addColumn<QString>("url"); |
|
230 |
QTest::addColumn<QList<QNetworkCookie> >("expectedResult"); |
|
231 |
||
232 |
QList<QNetworkCookie> allCookies; |
|
233 |
QList<QNetworkCookie> result; |
|
234 |
||
235 |
QTest::newRow("no-cookies") << allCookies << "http://foo.bar/" << result; |
|
236 |
||
237 |
QNetworkCookie cookie; |
|
238 |
cookie.setName("a"); |
|
239 |
cookie.setPath("/web"); |
|
240 |
cookie.setDomain(".nokia.com"); |
|
241 |
allCookies += cookie; |
|
242 |
||
243 |
QTest::newRow("no-match-1") << allCookies << "http://foo.bar/" << result; |
|
244 |
QTest::newRow("no-match-2") << allCookies << "http://foo.bar/web" << result; |
|
245 |
QTest::newRow("no-match-3") << allCookies << "http://foo.bar/web/wiki" << result; |
|
246 |
QTest::newRow("no-match-4") << allCookies << "http://nokia.com" << result; |
|
247 |
QTest::newRow("no-match-5") << allCookies << "http://qt.nokia.com" << result; |
|
248 |
QTest::newRow("no-match-6") << allCookies << "http://nokia.com/webinar" << result; |
|
249 |
QTest::newRow("no-match-7") << allCookies << "http://qt.nokia.com/webinar" << result; |
|
250 |
||
251 |
result = allCookies; |
|
252 |
QTest::newRow("match-1") << allCookies << "http://nokia.com/web" << result; |
|
253 |
QTest::newRow("match-2") << allCookies << "http://nokia.com/web/" << result; |
|
254 |
QTest::newRow("match-3") << allCookies << "http://nokia.com/web/content" << result; |
|
255 |
QTest::newRow("match-4") << allCookies << "http://qt.nokia.com/web" << result; |
|
256 |
QTest::newRow("match-4") << allCookies << "http://qt.nokia.com/web/" << result; |
|
257 |
QTest::newRow("match-6") << allCookies << "http://qt.nokia.com/web/content" << result; |
|
258 |
||
259 |
cookie.setPath("/web/wiki"); |
|
260 |
allCookies += cookie; |
|
261 |
||
262 |
// exact same results as before: |
|
263 |
QTest::newRow("one-match-1") << allCookies << "http://nokia.com/web" << result; |
|
264 |
QTest::newRow("one-match-2") << allCookies << "http://nokia.com/web/" << result; |
|
265 |
QTest::newRow("one-match-3") << allCookies << "http://nokia.com/web/content" << result; |
|
266 |
QTest::newRow("one-match-4") << allCookies << "http://qt.nokia.com/web" << result; |
|
267 |
QTest::newRow("one-match-4") << allCookies << "http://qt.nokia.com/web/" << result; |
|
268 |
QTest::newRow("one-match-6") << allCookies << "http://qt.nokia.com/web/content" << result; |
|
269 |
||
270 |
result.prepend(cookie); // longer path, it must match first |
|
271 |
QTest::newRow("two-matches-1") << allCookies << "http://nokia.com/web/wiki" << result; |
|
272 |
QTest::newRow("two-matches-2") << allCookies << "http://qt.nokia.com/web/wiki" << result; |
|
273 |
||
274 |
// invert the order; |
|
275 |
allCookies.clear(); |
|
276 |
allCookies << result.at(1) << result.at(0); |
|
277 |
QTest::newRow("two-matches-3") << allCookies << "http://nokia.com/web/wiki" << result; |
|
278 |
QTest::newRow("two-matches-4") << allCookies << "http://qt.nokia.com/web/wiki" << result; |
|
279 |
||
280 |
// expired cookie |
|
281 |
allCookies.clear(); |
|
282 |
cookie.setExpirationDate(QDateTime::fromString("09-Nov-1999", "dd-MMM-yyyy")); |
|
283 |
allCookies += cookie; |
|
284 |
result.clear(); |
|
285 |
QTest::newRow("exp-match-1") << allCookies << "http://nokia.com/web" << result; |
|
286 |
QTest::newRow("exp-match-2") << allCookies << "http://nokia.com/web/" << result; |
|
287 |
QTest::newRow("exp-match-3") << allCookies << "http://nokia.com/web/content" << result; |
|
288 |
QTest::newRow("exp-match-4") << allCookies << "http://qt.nokia.com/web" << result; |
|
289 |
QTest::newRow("exp-match-4") << allCookies << "http://qt.nokia.com/web/" << result; |
|
290 |
QTest::newRow("exp-match-6") << allCookies << "http://qt.nokia.com/web/content" << result; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
// path matching |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
allCookies.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
QNetworkCookie anotherCookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
anotherCookie.setName("a"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
anotherCookie.setPath("/web"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
anotherCookie.setDomain(".nokia.com"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
allCookies += anotherCookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
result.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
300 |
QTest::newRow("path-unmatch-1") << allCookies << "http://nokia.com/" << result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
301 |
QTest::newRow("path-unmatch-2") << allCookies << "http://nokia.com/something/else" << result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
302 |
result += anotherCookie; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
303 |
QTest::newRow("path-match-1") << allCookies << "http://nokia.com/web" << result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
304 |
QTest::newRow("path-match-2") << allCookies << "http://nokia.com/web/" << result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
305 |
QTest::newRow("path-match-3") << allCookies << "http://nokia.com/web/content" << result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
306 |
|
0 | 307 |
} |
308 |
||
309 |
void tst_QNetworkCookieJar::cookiesForUrl() |
|
310 |
{ |
|
311 |
QFETCH(QList<QNetworkCookie>, allCookies); |
|
312 |
QFETCH(QString, url); |
|
313 |
QFETCH(QList<QNetworkCookie>, expectedResult); |
|
314 |
||
315 |
MyCookieJar jar; |
|
316 |
jar.setAllCookies(allCookies); |
|
317 |
||
318 |
QList<QNetworkCookie> result = jar.cookiesForUrl(url); |
|
319 |
QCOMPARE(result, expectedResult); |
|
320 |
} |
|
321 |
||
322 |
QTEST_MAIN(tst_QNetworkCookieJar) |
|
323 |
#include "tst_qnetworkcookiejar.moc" |
|
324 |