0
|
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/QtNetwork>
|
|
45 |
#include <qnetworkdiskcache.h>
|
|
46 |
#include "../../shared/util.h"
|
|
47 |
|
|
48 |
#define EXAMPLE_URL "http://user:pass@www.example.com/#foo"
|
|
49 |
|
|
50 |
class tst_QNetworkDiskCache : public QObject
|
|
51 |
{
|
|
52 |
Q_OBJECT
|
|
53 |
|
|
54 |
public slots:
|
|
55 |
void initTestCase();
|
|
56 |
void cleanupTestCase();
|
|
57 |
void init();
|
|
58 |
void cleanup();
|
|
59 |
|
|
60 |
private slots:
|
|
61 |
void qnetworkdiskcache_data();
|
|
62 |
void qnetworkdiskcache();
|
|
63 |
|
|
64 |
void prepare();
|
|
65 |
void cacheSize();
|
|
66 |
void clear();
|
|
67 |
void data_data();
|
|
68 |
void data();
|
|
69 |
void metaData();
|
|
70 |
void remove();
|
|
71 |
void setCacheDirectory_data();
|
|
72 |
void setCacheDirectory();
|
|
73 |
void updateMetaData();
|
|
74 |
void fileMetaData();
|
|
75 |
void expire();
|
|
76 |
|
|
77 |
void oldCacheVersionFile_data();
|
|
78 |
void oldCacheVersionFile();
|
|
79 |
|
|
80 |
void sync();
|
|
81 |
};
|
|
82 |
|
|
83 |
// Subclass that exposes the protected functions.
|
|
84 |
class SubQNetworkDiskCache : public QNetworkDiskCache
|
|
85 |
{
|
|
86 |
public:
|
|
87 |
~SubQNetworkDiskCache()
|
|
88 |
{
|
|
89 |
if (!cacheDirectory().isEmpty())
|
|
90 |
clear();
|
|
91 |
}
|
|
92 |
|
|
93 |
QNetworkCacheMetaData call_fileMetaData(QString const &fileName)
|
|
94 |
{ return SubQNetworkDiskCache::fileMetaData(fileName); }
|
|
95 |
|
|
96 |
qint64 call_expire()
|
|
97 |
{ return SubQNetworkDiskCache::expire(); }
|
|
98 |
|
|
99 |
void setupWithOne(const QUrl &url, const QNetworkCacheMetaData &metaData = QNetworkCacheMetaData())
|
|
100 |
{
|
|
101 |
setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
102 |
|
|
103 |
QIODevice *d = 0;
|
|
104 |
if (metaData.isValid()) {
|
|
105 |
d = prepare(metaData);
|
|
106 |
} else {
|
|
107 |
QNetworkCacheMetaData m;
|
|
108 |
m.setUrl(url);
|
|
109 |
QNetworkCacheMetaData::RawHeader header("content-type", "text/html");
|
|
110 |
QNetworkCacheMetaData::RawHeaderList list;
|
|
111 |
list.append(header);
|
|
112 |
m.setRawHeaders(list);
|
|
113 |
d = prepare(m);
|
|
114 |
}
|
|
115 |
d->write("Hello World!");
|
|
116 |
insert(d);
|
|
117 |
}
|
|
118 |
};
|
|
119 |
|
|
120 |
// This will be called before the first test function is executed.
|
|
121 |
// It is only called once.
|
|
122 |
void tst_QNetworkDiskCache::initTestCase()
|
|
123 |
{
|
|
124 |
SubQNetworkDiskCache cache;
|
|
125 |
cache.setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
126 |
cache.clear();
|
|
127 |
QString s = QDir::tempPath() + "/diskCache/";
|
|
128 |
QDir dir;
|
|
129 |
dir.rmdir(s + "http");
|
|
130 |
dir.rmdir(s + "https");
|
|
131 |
dir.rmdir(s + "prepared");
|
|
132 |
dir.rmdir(s);
|
|
133 |
}
|
|
134 |
|
|
135 |
// This will be called after the last test function is executed.
|
|
136 |
// It is only called once.
|
|
137 |
void tst_QNetworkDiskCache::cleanupTestCase()
|
|
138 |
{
|
|
139 |
}
|
|
140 |
|
|
141 |
// This will be called before each test function is executed.
|
|
142 |
void tst_QNetworkDiskCache::init()
|
|
143 |
{
|
|
144 |
}
|
|
145 |
|
|
146 |
// This will be called after every test function.
|
|
147 |
void tst_QNetworkDiskCache::cleanup()
|
|
148 |
{
|
|
149 |
}
|
|
150 |
|
|
151 |
void tst_QNetworkDiskCache::qnetworkdiskcache_data()
|
|
152 |
{
|
|
153 |
}
|
|
154 |
|
|
155 |
void tst_QNetworkDiskCache::qnetworkdiskcache()
|
|
156 |
{
|
|
157 |
QUrl url(EXAMPLE_URL);
|
|
158 |
SubQNetworkDiskCache cache;
|
|
159 |
QCOMPARE(cache.cacheDirectory(), QString());
|
|
160 |
QCOMPARE(cache.cacheSize(), qint64(0));
|
|
161 |
cache.clear();
|
|
162 |
QCOMPARE(cache.metaData(QUrl()), QNetworkCacheMetaData());
|
|
163 |
QCOMPARE(cache.remove(QUrl()), false);
|
|
164 |
QCOMPARE(cache.remove(url), false);
|
|
165 |
cache.insert((QIODevice*)0);
|
|
166 |
cache.setCacheDirectory(QString());
|
|
167 |
cache.updateMetaData(QNetworkCacheMetaData());
|
|
168 |
cache.prepare(QNetworkCacheMetaData());
|
|
169 |
QCOMPARE(cache.call_fileMetaData(QString()), QNetworkCacheMetaData());
|
|
170 |
|
|
171 |
// leave one hanging around...
|
|
172 |
QNetworkDiskCache badCache;
|
|
173 |
QNetworkCacheMetaData metaData;
|
|
174 |
metaData.setUrl(url);
|
|
175 |
badCache.prepare(metaData);
|
|
176 |
badCache.setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
177 |
badCache.prepare(metaData);
|
|
178 |
}
|
|
179 |
|
|
180 |
void tst_QNetworkDiskCache::prepare()
|
|
181 |
{
|
|
182 |
SubQNetworkDiskCache cache;
|
|
183 |
cache.setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
184 |
|
|
185 |
QUrl url(EXAMPLE_URL);
|
|
186 |
QNetworkCacheMetaData metaData;
|
|
187 |
metaData.setUrl(url);
|
|
188 |
|
|
189 |
cache.prepare(metaData);
|
|
190 |
cache.remove(url);
|
|
191 |
}
|
|
192 |
|
|
193 |
// public qint64 cacheSize() const
|
|
194 |
void tst_QNetworkDiskCache::cacheSize()
|
|
195 |
{
|
|
196 |
SubQNetworkDiskCache cache;
|
|
197 |
cache.setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
198 |
QCOMPARE(cache.cacheSize(), qint64(0));
|
|
199 |
|
|
200 |
QUrl url(EXAMPLE_URL);
|
|
201 |
QNetworkCacheMetaData metaData;
|
|
202 |
metaData.setUrl(url);
|
|
203 |
QIODevice *d = cache.prepare(metaData);
|
|
204 |
cache.insert(d);
|
|
205 |
QVERIFY(cache.cacheSize() > qint64(0));
|
|
206 |
|
|
207 |
cache.clear();
|
|
208 |
QCOMPARE(cache.cacheSize(), qint64(0));
|
|
209 |
}
|
|
210 |
|
|
211 |
static QStringList countFiles(const QString dir)
|
|
212 |
{
|
|
213 |
QStringList list;
|
|
214 |
QDir::Filters filter(QDir::AllEntries | QDir::NoDotAndDotDot);
|
|
215 |
QDirIterator it(dir, filter, QDirIterator::Subdirectories);
|
|
216 |
while (it.hasNext())
|
|
217 |
list.append(it.next());
|
|
218 |
return list;
|
|
219 |
}
|
|
220 |
|
|
221 |
// public void clear()
|
|
222 |
void tst_QNetworkDiskCache::clear()
|
|
223 |
{
|
|
224 |
SubQNetworkDiskCache cache;
|
|
225 |
QUrl url(EXAMPLE_URL);
|
|
226 |
cache.setupWithOne(url);
|
|
227 |
QVERIFY(cache.cacheSize() > qint64(0));
|
|
228 |
|
|
229 |
QString cacheDirectory = cache.cacheDirectory();
|
|
230 |
QCOMPARE(countFiles(cacheDirectory).count(), 3);
|
|
231 |
cache.clear();
|
|
232 |
QCOMPARE(countFiles(cacheDirectory).count(), 2);
|
|
233 |
|
|
234 |
// don't delete files that it didn't create
|
|
235 |
QTemporaryFile file(cacheDirectory + "/cache_XXXXXX");
|
|
236 |
if (file.open()) {
|
|
237 |
QCOMPARE(countFiles(cacheDirectory).count(), 3);
|
|
238 |
cache.clear();
|
|
239 |
QCOMPARE(countFiles(cacheDirectory).count(), 3);
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
Q_DECLARE_METATYPE(QNetworkCacheMetaData)
|
|
244 |
void tst_QNetworkDiskCache::data_data()
|
|
245 |
{
|
|
246 |
QTest::addColumn<QNetworkCacheMetaData>("data");
|
|
247 |
|
|
248 |
QTest::newRow("null") << QNetworkCacheMetaData();
|
|
249 |
|
|
250 |
QUrl url(EXAMPLE_URL);
|
|
251 |
QNetworkCacheMetaData metaData;
|
|
252 |
metaData.setUrl(url);
|
|
253 |
QNetworkCacheMetaData::RawHeaderList headers;
|
|
254 |
headers.append(QNetworkCacheMetaData::RawHeader("type", "bin"));
|
|
255 |
metaData.setRawHeaders(headers);
|
|
256 |
QTest::newRow("null") << metaData;
|
|
257 |
}
|
|
258 |
|
|
259 |
// public QIODevice* data(QUrl const& url)
|
|
260 |
void tst_QNetworkDiskCache::data()
|
|
261 |
{
|
|
262 |
#ifdef Q_OS_SYMBIAN
|
|
263 |
QSKIP("Due to mmap(...) bug in Open C [Temtrack DEF142242]", SkipAll);
|
|
264 |
#endif
|
|
265 |
QFETCH(QNetworkCacheMetaData, data);
|
|
266 |
SubQNetworkDiskCache cache;
|
|
267 |
QUrl url(EXAMPLE_URL);
|
|
268 |
cache.setupWithOne(url, data);
|
|
269 |
|
|
270 |
// flush the cache
|
|
271 |
QTemporaryFile file(cache.cacheDirectory() + "/cache_XXXXXX.cache");
|
|
272 |
if (file.open()) {
|
|
273 |
cache.call_fileMetaData(file.fileName());
|
|
274 |
}
|
|
275 |
|
|
276 |
for (int i = 0; i < 3; ++i) {
|
|
277 |
QIODevice *d = cache.data(url);
|
|
278 |
QVERIFY(d);
|
|
279 |
QCOMPARE(d->readAll(), QByteArray("Hello World!"));
|
|
280 |
delete d;
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
// public QNetworkCacheMetaData metaData(QUrl const& url)
|
|
285 |
void tst_QNetworkDiskCache::metaData()
|
|
286 |
{
|
|
287 |
SubQNetworkDiskCache cache;
|
|
288 |
|
|
289 |
QUrl url(EXAMPLE_URL);
|
|
290 |
QNetworkCacheMetaData metaData;
|
|
291 |
metaData.setUrl(url);
|
|
292 |
QNetworkCacheMetaData::RawHeaderList headers;
|
|
293 |
headers.append(QNetworkCacheMetaData::RawHeader("type", "bin"));
|
|
294 |
metaData.setRawHeaders(headers);
|
|
295 |
metaData.setLastModified(QDateTime::currentDateTime());
|
|
296 |
metaData.setExpirationDate(QDateTime::currentDateTime());
|
|
297 |
metaData.setSaveToDisk(true);
|
|
298 |
|
|
299 |
cache.setupWithOne(url, metaData);
|
|
300 |
|
|
301 |
for (int i = 0; i < 3; ++i) {
|
|
302 |
QNetworkCacheMetaData cacheMetaData = cache.metaData(url);
|
|
303 |
QVERIFY(cacheMetaData.isValid());
|
|
304 |
QCOMPARE(metaData, cacheMetaData);
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
// public bool remove(QUrl const& url)
|
|
309 |
void tst_QNetworkDiskCache::remove()
|
|
310 |
{
|
|
311 |
SubQNetworkDiskCache cache;
|
|
312 |
QUrl url(EXAMPLE_URL);
|
|
313 |
cache.setupWithOne(url);
|
|
314 |
QString cacheDirectory = cache.cacheDirectory();
|
|
315 |
QCOMPARE(countFiles(cacheDirectory).count(), 3);
|
|
316 |
cache.remove(url);
|
|
317 |
QCOMPARE(countFiles(cacheDirectory).count(), 2);
|
|
318 |
}
|
|
319 |
|
|
320 |
void tst_QNetworkDiskCache::setCacheDirectory_data()
|
|
321 |
{
|
|
322 |
QTest::addColumn<QString>("cacheDir");
|
|
323 |
QTest::newRow("null") << QString();
|
|
324 |
QDir dir("foo");
|
|
325 |
QTest::newRow("foo") << dir.absolutePath() + QString("/");
|
|
326 |
}
|
|
327 |
|
|
328 |
// public void setCacheDirectory(QString const& cacheDir)
|
|
329 |
void tst_QNetworkDiskCache::setCacheDirectory()
|
|
330 |
{
|
|
331 |
QFETCH(QString, cacheDir);
|
|
332 |
|
|
333 |
SubQNetworkDiskCache cache;
|
|
334 |
cache.setCacheDirectory(cacheDir);
|
|
335 |
QCOMPARE(cache.cacheDirectory(), cacheDir);
|
|
336 |
}
|
|
337 |
|
|
338 |
// public void updateMetaData(QNetworkCacheMetaData const& metaData)
|
|
339 |
void tst_QNetworkDiskCache::updateMetaData()
|
|
340 |
{
|
|
341 |
#ifdef Q_OS_SYMBIAN
|
|
342 |
QSKIP("Due to mmap(...) bug in Open C [Temtrack DEF142242]", SkipAll);
|
|
343 |
#endif
|
|
344 |
QUrl url(EXAMPLE_URL);
|
|
345 |
SubQNetworkDiskCache cache;
|
|
346 |
cache.setupWithOne(url);
|
|
347 |
|
|
348 |
QNetworkCacheMetaData metaData = cache.metaData(url);
|
|
349 |
metaData.setLastModified(QDateTime::currentDateTime());
|
|
350 |
cache.updateMetaData(metaData);
|
|
351 |
QNetworkCacheMetaData newMetaData = cache.metaData(url);
|
|
352 |
QCOMPARE(newMetaData, metaData);
|
|
353 |
}
|
|
354 |
|
|
355 |
// protected QNetworkCacheMetaData fileMetaData(QString const& fileName)
|
|
356 |
void tst_QNetworkDiskCache::fileMetaData()
|
|
357 |
{
|
|
358 |
SubQNetworkDiskCache cache;
|
|
359 |
QUrl url(EXAMPLE_URL);
|
|
360 |
cache.setupWithOne(url);
|
|
361 |
|
|
362 |
url.setPassword(QString());
|
|
363 |
url.setFragment(QString());
|
|
364 |
|
|
365 |
QString cacheDirectory = cache.cacheDirectory();
|
|
366 |
QStringList list = countFiles(cacheDirectory);
|
|
367 |
QCOMPARE(list.count(), 3);
|
|
368 |
foreach(QString fileName, list) {
|
|
369 |
QFileInfo info(fileName);
|
|
370 |
if (info.isFile()) {
|
|
371 |
QNetworkCacheMetaData metaData = cache.call_fileMetaData(fileName);
|
|
372 |
QCOMPARE(metaData.url(), url);
|
|
373 |
}
|
|
374 |
}
|
|
375 |
|
|
376 |
QTemporaryFile file(cacheDirectory + "/qt_temp.XXXXXX");
|
|
377 |
if (file.open()) {
|
|
378 |
QNetworkCacheMetaData metaData = cache.call_fileMetaData(file.fileName());
|
|
379 |
QVERIFY(!metaData.isValid());
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
// protected qint64 expire()
|
|
384 |
void tst_QNetworkDiskCache::expire()
|
|
385 |
{
|
|
386 |
SubQNetworkDiskCache cache;
|
|
387 |
cache.setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
388 |
QCOMPARE(cache.call_expire(), (qint64)0);
|
|
389 |
QUrl url(EXAMPLE_URL);
|
|
390 |
cache.setupWithOne(url);
|
|
391 |
QVERIFY(cache.call_expire() > (qint64)0);
|
|
392 |
qint64 limit = (1024 * 1024 / 4) * 5;
|
|
393 |
cache.setMaximumCacheSize(limit);
|
|
394 |
|
|
395 |
qint64 max = cache.maximumCacheSize();
|
|
396 |
QCOMPARE(max, limit);
|
|
397 |
for (int i = 0; i < 10; ++i) {
|
|
398 |
if (i % 3 == 0)
|
|
399 |
QTest::qWait(2000);
|
|
400 |
QNetworkCacheMetaData m;
|
|
401 |
m.setUrl(QUrl("http://www.foo.com/" + QString::number(i)));
|
|
402 |
QIODevice *d = cache.prepare(m);
|
|
403 |
QString bigString;
|
|
404 |
bigString.fill(QLatin1Char('Z'), (1024 * 1024 / 4));
|
|
405 |
d->write(bigString.toLatin1().data());
|
|
406 |
cache.insert(d);
|
|
407 |
QVERIFY(cache.call_expire() < max);
|
|
408 |
}
|
|
409 |
|
|
410 |
QString cacheDirectory = cache.cacheDirectory();
|
|
411 |
QStringList list = countFiles(cacheDirectory);
|
|
412 |
QStringList cacheList;
|
|
413 |
foreach(QString fileName, list) {
|
|
414 |
QFileInfo info(fileName);
|
|
415 |
if (info.isFile()) {
|
|
416 |
QNetworkCacheMetaData metaData = cache.call_fileMetaData(fileName);
|
|
417 |
cacheList.append(metaData.url().toString());
|
|
418 |
}
|
|
419 |
}
|
|
420 |
qSort(cacheList);
|
|
421 |
for (int i = 0; i < cacheList.count(); ++i) {
|
|
422 |
QString fileName = cacheList[i];
|
|
423 |
QCOMPARE(fileName, QString("http://www.foo.com/%1").arg(i + 6));
|
|
424 |
}
|
|
425 |
}
|
|
426 |
|
|
427 |
void tst_QNetworkDiskCache::oldCacheVersionFile_data()
|
|
428 |
{
|
|
429 |
QTest::addColumn<int>("pass");
|
|
430 |
QTest::newRow("0") << 0;
|
|
431 |
QTest::newRow("1") << 1;
|
|
432 |
}
|
|
433 |
|
|
434 |
void tst_QNetworkDiskCache::oldCacheVersionFile()
|
|
435 |
{
|
|
436 |
QFETCH(int, pass);
|
|
437 |
SubQNetworkDiskCache cache;
|
|
438 |
QUrl url(EXAMPLE_URL);
|
|
439 |
cache.setupWithOne(url);
|
|
440 |
|
|
441 |
if (pass == 0) {
|
|
442 |
QString name;
|
|
443 |
{
|
|
444 |
QTemporaryFile file(cache.cacheDirectory() + "/cache_XXXXXX.cache");
|
|
445 |
file.setAutoRemove(false);
|
|
446 |
QVERIFY(file.open());
|
|
447 |
QDataStream out(&file);
|
|
448 |
out << qint32(0xe8);
|
|
449 |
out << qint32(2);
|
|
450 |
name = file.fileName();
|
|
451 |
file.close();
|
|
452 |
}
|
|
453 |
|
|
454 |
QVERIFY(QFile::exists(name));
|
|
455 |
QNetworkCacheMetaData metaData = cache.call_fileMetaData(name);
|
|
456 |
QVERIFY(!metaData.isValid());
|
|
457 |
QVERIFY(!QFile::exists(name));
|
|
458 |
} else {
|
|
459 |
QStringList files = countFiles(cache.cacheDirectory());
|
|
460 |
QCOMPARE(files.count(), 3);
|
|
461 |
// find the file
|
|
462 |
QString cacheFile;
|
|
463 |
foreach (QString file, files) {
|
|
464 |
QFileInfo info(file);
|
|
465 |
if (info.isFile())
|
|
466 |
cacheFile = file;
|
|
467 |
}
|
|
468 |
QVERIFY(QFile::exists(cacheFile));
|
|
469 |
|
|
470 |
QFile file(cacheFile);
|
|
471 |
QVERIFY(file.open(QFile::ReadWrite));
|
|
472 |
QDataStream out(&file);
|
|
473 |
out << qint32(0xe8);
|
|
474 |
out << qint32(2);
|
|
475 |
file.close();
|
|
476 |
|
|
477 |
QIODevice *device = cache.data(url);
|
|
478 |
QVERIFY(!device);
|
|
479 |
QVERIFY(!QFile::exists(cacheFile));
|
|
480 |
}
|
|
481 |
}
|
|
482 |
|
|
483 |
class Runner : public QThread
|
|
484 |
{
|
|
485 |
|
|
486 |
public:
|
|
487 |
Runner()
|
|
488 |
: QThread()
|
|
489 |
, other(0)
|
|
490 |
{}
|
|
491 |
|
|
492 |
void run()
|
|
493 |
{
|
|
494 |
QByteArray longString = "Hello World, this is some long string, well not really that long";
|
|
495 |
for (int j = 0; j < 10; ++j)
|
|
496 |
longString += longString;
|
|
497 |
QByteArray longString2 = "Help, I am stuck in an autotest!";
|
|
498 |
QUrl url(EXAMPLE_URL);
|
|
499 |
|
|
500 |
QNetworkCacheMetaData metaData;
|
|
501 |
metaData.setUrl(url);
|
|
502 |
QNetworkCacheMetaData::RawHeaderList headers;
|
|
503 |
headers.append(QNetworkCacheMetaData::RawHeader("type", "bin"));
|
|
504 |
metaData.setRawHeaders(headers);
|
|
505 |
metaData.setLastModified(dt);
|
|
506 |
metaData.setSaveToDisk(true);
|
|
507 |
|
|
508 |
QNetworkCacheMetaData metaData2 = metaData;
|
|
509 |
metaData2.setExpirationDate(dt);
|
|
510 |
|
|
511 |
QNetworkDiskCache cache;
|
|
512 |
cache.setCacheDirectory(QDir::tempPath() + "/diskCache");
|
|
513 |
|
|
514 |
int read = 0;
|
|
515 |
|
|
516 |
int i = 0;
|
|
517 |
for (; i < 5000; ++i) {
|
|
518 |
if (other && other->isFinished())
|
|
519 |
break;
|
|
520 |
|
|
521 |
if (write) {
|
|
522 |
QNetworkCacheMetaData m;
|
|
523 |
if (qrand() % 2 == 0)
|
|
524 |
m = metaData;
|
|
525 |
else
|
|
526 |
m = metaData2;
|
|
527 |
|
|
528 |
if (qrand() % 20 == 1) {
|
|
529 |
//qDebug() << "write update";
|
|
530 |
cache.updateMetaData(m);
|
|
531 |
continue;
|
|
532 |
}
|
|
533 |
|
|
534 |
QIODevice *device = cache.prepare(m);
|
|
535 |
if (qrand() % 20 == 1) {
|
|
536 |
//qDebug() << "write remove";
|
|
537 |
cache.remove(url);
|
|
538 |
continue;
|
|
539 |
}
|
|
540 |
QVERIFY(device);
|
|
541 |
if (qrand() % 2 == 0)
|
|
542 |
device->write(longString);
|
|
543 |
else
|
|
544 |
device->write(longString2);
|
|
545 |
//qDebug() << "write write" << device->size();
|
|
546 |
cache.insert(device);
|
|
547 |
continue;
|
|
548 |
}
|
|
549 |
|
|
550 |
QNetworkCacheMetaData gotMetaData = cache.metaData(url);
|
|
551 |
if (gotMetaData.isValid()) {
|
|
552 |
QVERIFY(gotMetaData == metaData || gotMetaData == metaData2);
|
|
553 |
QIODevice *d = cache.data(url);
|
|
554 |
if (d) {
|
|
555 |
QByteArray x = d->readAll();
|
|
556 |
if (x != longString && x != longString2) {
|
|
557 |
qDebug() << x.length() << QString(x);
|
|
558 |
gotMetaData = cache.metaData(url);
|
|
559 |
qDebug() << (gotMetaData.url().toString())
|
|
560 |
<< gotMetaData.lastModified()
|
|
561 |
<< gotMetaData.expirationDate()
|
|
562 |
<< gotMetaData.saveToDisk();
|
|
563 |
}
|
|
564 |
if (gotMetaData.isValid())
|
|
565 |
QVERIFY(x == longString || x == longString2);
|
|
566 |
read++;
|
|
567 |
delete d;
|
|
568 |
}
|
|
569 |
}
|
|
570 |
if (qrand() % 5 == 1)
|
|
571 |
cache.remove(url);
|
|
572 |
if (qrand() % 5 == 1)
|
|
573 |
cache.clear();
|
|
574 |
sleep(0);
|
|
575 |
}
|
|
576 |
//qDebug() << "read!" << read << i;
|
|
577 |
}
|
|
578 |
|
|
579 |
QDateTime dt;
|
|
580 |
bool write;
|
|
581 |
Runner *other;
|
|
582 |
};
|
|
583 |
|
|
584 |
void tst_QNetworkDiskCache::sync()
|
|
585 |
{
|
|
586 |
// This tests would be a nice to have, but is currently not supported.
|
|
587 |
return;
|
|
588 |
|
|
589 |
QTime midnight(0, 0, 0);
|
|
590 |
qsrand(midnight.secsTo(QTime::currentTime()));
|
|
591 |
Runner reader;
|
|
592 |
reader.dt = QDateTime::currentDateTime();
|
|
593 |
reader.write = false;
|
|
594 |
|
|
595 |
Runner writer;
|
|
596 |
writer.dt = reader.dt;
|
|
597 |
writer.write = true;
|
|
598 |
|
|
599 |
writer.other = &reader;
|
|
600 |
reader.other = &writer;
|
|
601 |
|
|
602 |
writer.start();
|
|
603 |
reader.start();
|
|
604 |
writer.wait();
|
|
605 |
reader.wait();
|
|
606 |
}
|
|
607 |
|
|
608 |
QTEST_MAIN(tst_QNetworkDiskCache)
|
|
609 |
#include "tst_qnetworkdiskcache.moc"
|
|
610 |
|