summaryrefslogtreecommitdiffstats
path: root/tests/auto/qnetworkdiskcache
diff options
context:
space:
mode:
authorMarkus Goetz <Markus.Goetz@nokia.com>2010-06-02 11:56:19 +0200
committerMarkus Goetz <Markus.Goetz@nokia.com>2010-06-02 13:00:26 +0200
commit417f3a4ece9403d1eb37ce0ecdf74cf670bdd0ff (patch)
treee817bfa5f9f3f81d101563575211e49357b648b6 /tests/auto/qnetworkdiskcache
parent0f16c7ce8dcd6f4905d14875088c55148e41366a (diff)
QNAM: Improve child deletion order
Delete the QNetworkReply children first because they could access the QAbstractNetworkCache that is also a child of the QNetworkAccessManager. Reviewed-by: brad
Diffstat (limited to 'tests/auto/qnetworkdiskcache')
-rw-r--r--tests/auto/qnetworkdiskcache/tst_qnetworkdiskcache.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/auto/qnetworkdiskcache/tst_qnetworkdiskcache.cpp b/tests/auto/qnetworkdiskcache/tst_qnetworkdiskcache.cpp
index 8ce10fbb9c..5bbe8f645e 100644
--- a/tests/auto/qnetworkdiskcache/tst_qnetworkdiskcache.cpp
+++ b/tests/auto/qnetworkdiskcache/tst_qnetworkdiskcache.cpp
@@ -78,6 +78,56 @@ private slots:
void oldCacheVersionFile();
void sync();
+
+ void crashWhenParentingCache();
+};
+
+// FIXME same as in tst_qnetworkreply.cpp .. could be unified
+// Does not work for POST/PUT!
+class MiniHttpServer: public QTcpServer
+{
+ Q_OBJECT
+public:
+ QTcpSocket *client; // always the last one that was received
+ QByteArray dataToTransmit;
+ QByteArray receivedData;
+ bool doClose;
+ bool multiple;
+ int totalConnections;
+
+ MiniHttpServer(const QByteArray &data) : client(0), dataToTransmit(data), doClose(true), multiple(false), totalConnections(0)
+ {
+ listen();
+ connect(this, SIGNAL(newConnection()), this, SLOT(doAccept()));
+ }
+
+public slots:
+ void doAccept()
+ {
+ client = nextPendingConnection();
+ client->setParent(this);
+ ++totalConnections;
+ connect(client, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
+ }
+
+ void readyReadSlot()
+ {
+ receivedData += client->readAll();
+ int doubleEndlPos = receivedData.indexOf("\r\n\r\n");
+
+ if (doubleEndlPos != -1) {
+ // multiple requests incoming. remove the bytes of the current one
+ if (multiple)
+ receivedData.remove(0, doubleEndlPos+4);
+
+ client->write(dataToTransmit);
+ if (doClose) {
+ client->disconnectFromHost();
+ disconnect(client, 0, this, 0);
+ client = 0;
+ }
+ }
+ }
};
// Subclass that exposes the protected functions.
@@ -581,6 +631,33 @@ public:
Runner *other;
};
+void tst_QNetworkDiskCache::crashWhenParentingCache()
+{
+ // the trick here is to not send the complete response
+ // but some data. So we get a readyRead() and it gets tried
+ // to be saved to the cache
+ QByteArray data("HTTP/1.0 200 OK\r\nCache-Control: max-age=300\r\nAge: 1\r\nContent-Length: 5\r\n\r\n123");
+ MiniHttpServer server(data);
+
+ QNetworkAccessManager *manager = new QNetworkAccessManager();
+ QNetworkDiskCache *diskCache = new QNetworkDiskCache(manager); // parent to qnam!
+ // we expect the temp dir to be cleaned at some point anyway
+ diskCache->setCacheDirectory(QDir::tempPath() + "/cacheDir_" + QCoreApplication::applicationPid());
+ manager->setCache(diskCache);
+
+ QUrl url("http://127.0.0.1:" + QString::number(server.serverPort()));
+ QNetworkRequest request(url);
+ // request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
+ QNetworkReply *reply = manager->get(request); // new reply is parented to qnam
+
+ // wait for readyRead of reply!
+ connect(reply, SIGNAL(readyRead()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+ QTestEventLoop::instance().enterLoop(5);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ delete manager; // crashed before..
+}
+
void tst_QNetworkDiskCache::sync()
{
// This tests would be a nice to have, but is currently not supported.