|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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("/"); |
|
123 cookie.setDomain("www.foo.tld"); |
|
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 |
|
151 // security test: |
|
152 result.clear(); |
|
153 preset.clear(); |
|
154 cookie.setDomain("something.completely.different"); |
|
155 QTest::newRow("security-domain-1") << preset << cookie << "http://www.foo.tld" << result << false; |
|
156 |
|
157 cookie.setDomain("www.foo.tld"); |
|
158 cookie.setPath("/something"); |
|
159 QTest::newRow("security-path-1") << preset << cookie << "http://www.foo.tld" << result << false; |
|
160 |
|
161 // setting the defaults: |
|
162 QNetworkCookie finalCookie = cookie; |
|
163 finalCookie.setPath("/something/"); |
|
164 cookie.setPath(""); |
|
165 cookie.setDomain(""); |
|
166 result.clear(); |
|
167 result += finalCookie; |
|
168 QTest::newRow("defaults-1") << preset << cookie << "http://www.foo.tld/something/" << result << true; |
|
169 |
|
170 finalCookie.setPath("/"); |
|
171 result.clear(); |
|
172 result += finalCookie; |
|
173 QTest::newRow("defaults-2") << preset << cookie << "http://www.foo.tld" << result << true; |
|
174 |
|
175 // security test: do not accept cookie domains like ".com" nor ".com." (see RFC 2109 section 4.3.2) |
|
176 result.clear(); |
|
177 preset.clear(); |
|
178 cookie.setDomain(".com"); |
|
179 QTest::newRow("rfc2109-4.3.2-ex3") << preset << cookie << "http://x.foo.com" << result << false; |
|
180 |
|
181 result.clear(); |
|
182 preset.clear(); |
|
183 cookie.setDomain(".com."); |
|
184 QTest::newRow("rfc2109-4.3.2-ex3-2") << preset << cookie << "http://x.foo.com" << result << false; |
|
185 } |
|
186 |
|
187 void tst_QNetworkCookieJar::setCookiesFromUrl() |
|
188 { |
|
189 QFETCH(QList<QNetworkCookie>, preset); |
|
190 QFETCH(QNetworkCookie, newCookie); |
|
191 QFETCH(QString, referenceUrl); |
|
192 QFETCH(QList<QNetworkCookie>, expectedResult); |
|
193 QFETCH(bool, setCookies); |
|
194 |
|
195 QList<QNetworkCookie> cookieList; |
|
196 cookieList += newCookie; |
|
197 MyCookieJar jar; |
|
198 jar.setAllCookies(preset); |
|
199 QCOMPARE(jar.setCookiesFromUrl(cookieList, referenceUrl), setCookies); |
|
200 |
|
201 QList<QNetworkCookie> result = jar.allCookies(); |
|
202 foreach (QNetworkCookie cookie, expectedResult) { |
|
203 QVERIFY2(result.contains(cookie), cookie.toRawForm()); |
|
204 result.removeAll(cookie); |
|
205 } |
|
206 QVERIFY2(result.isEmpty(), QTest::toString(result)); |
|
207 } |
|
208 |
|
209 void tst_QNetworkCookieJar::cookiesForUrl_data() |
|
210 { |
|
211 QTest::addColumn<QList<QNetworkCookie> >("allCookies"); |
|
212 QTest::addColumn<QString>("url"); |
|
213 QTest::addColumn<QList<QNetworkCookie> >("expectedResult"); |
|
214 |
|
215 QList<QNetworkCookie> allCookies; |
|
216 QList<QNetworkCookie> result; |
|
217 |
|
218 QTest::newRow("no-cookies") << allCookies << "http://foo.bar/" << result; |
|
219 |
|
220 QNetworkCookie cookie; |
|
221 cookie.setName("a"); |
|
222 cookie.setPath("/web"); |
|
223 cookie.setDomain(".nokia.com"); |
|
224 allCookies += cookie; |
|
225 |
|
226 QTest::newRow("no-match-1") << allCookies << "http://foo.bar/" << result; |
|
227 QTest::newRow("no-match-2") << allCookies << "http://foo.bar/web" << result; |
|
228 QTest::newRow("no-match-3") << allCookies << "http://foo.bar/web/wiki" << result; |
|
229 QTest::newRow("no-match-4") << allCookies << "http://nokia.com" << result; |
|
230 QTest::newRow("no-match-5") << allCookies << "http://qt.nokia.com" << result; |
|
231 QTest::newRow("no-match-6") << allCookies << "http://nokia.com/webinar" << result; |
|
232 QTest::newRow("no-match-7") << allCookies << "http://qt.nokia.com/webinar" << result; |
|
233 |
|
234 result = allCookies; |
|
235 QTest::newRow("match-1") << allCookies << "http://nokia.com/web" << result; |
|
236 QTest::newRow("match-2") << allCookies << "http://nokia.com/web/" << result; |
|
237 QTest::newRow("match-3") << allCookies << "http://nokia.com/web/content" << result; |
|
238 QTest::newRow("match-4") << allCookies << "http://qt.nokia.com/web" << result; |
|
239 QTest::newRow("match-4") << allCookies << "http://qt.nokia.com/web/" << result; |
|
240 QTest::newRow("match-6") << allCookies << "http://qt.nokia.com/web/content" << result; |
|
241 |
|
242 cookie.setPath("/web/wiki"); |
|
243 allCookies += cookie; |
|
244 |
|
245 // exact same results as before: |
|
246 QTest::newRow("one-match-1") << allCookies << "http://nokia.com/web" << result; |
|
247 QTest::newRow("one-match-2") << allCookies << "http://nokia.com/web/" << result; |
|
248 QTest::newRow("one-match-3") << allCookies << "http://nokia.com/web/content" << result; |
|
249 QTest::newRow("one-match-4") << allCookies << "http://qt.nokia.com/web" << result; |
|
250 QTest::newRow("one-match-4") << allCookies << "http://qt.nokia.com/web/" << result; |
|
251 QTest::newRow("one-match-6") << allCookies << "http://qt.nokia.com/web/content" << result; |
|
252 |
|
253 result.prepend(cookie); // longer path, it must match first |
|
254 QTest::newRow("two-matches-1") << allCookies << "http://nokia.com/web/wiki" << result; |
|
255 QTest::newRow("two-matches-2") << allCookies << "http://qt.nokia.com/web/wiki" << result; |
|
256 |
|
257 // invert the order; |
|
258 allCookies.clear(); |
|
259 allCookies << result.at(1) << result.at(0); |
|
260 QTest::newRow("two-matches-3") << allCookies << "http://nokia.com/web/wiki" << result; |
|
261 QTest::newRow("two-matches-4") << allCookies << "http://qt.nokia.com/web/wiki" << result; |
|
262 |
|
263 // expired cookie |
|
264 allCookies.clear(); |
|
265 cookie.setExpirationDate(QDateTime::fromString("09-Nov-1999", "dd-MMM-yyyy")); |
|
266 allCookies += cookie; |
|
267 result.clear(); |
|
268 QTest::newRow("exp-match-1") << allCookies << "http://nokia.com/web" << result; |
|
269 QTest::newRow("exp-match-2") << allCookies << "http://nokia.com/web/" << result; |
|
270 QTest::newRow("exp-match-3") << allCookies << "http://nokia.com/web/content" << result; |
|
271 QTest::newRow("exp-match-4") << allCookies << "http://qt.nokia.com/web" << result; |
|
272 QTest::newRow("exp-match-4") << allCookies << "http://qt.nokia.com/web/" << result; |
|
273 QTest::newRow("exp-match-6") << allCookies << "http://qt.nokia.com/web/content" << result; |
|
274 } |
|
275 |
|
276 void tst_QNetworkCookieJar::cookiesForUrl() |
|
277 { |
|
278 QFETCH(QList<QNetworkCookie>, allCookies); |
|
279 QFETCH(QString, url); |
|
280 QFETCH(QList<QNetworkCookie>, expectedResult); |
|
281 |
|
282 MyCookieJar jar; |
|
283 jar.setAllCookies(allCookies); |
|
284 |
|
285 QList<QNetworkCookie> result = jar.cookiesForUrl(url); |
|
286 QCOMPARE(result, expectedResult); |
|
287 } |
|
288 |
|
289 QTEST_MAIN(tst_QNetworkCookieJar) |
|
290 #include "tst_qnetworkcookiejar.moc" |
|
291 |