choke uploadProgress signals
[profile/ivi/qtbase.git] / tests / auto / network / access / qnetworkreply / tst_qnetworkreply.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44 #include <QtCore/QCryptographicHash>
45 #include <QtCore/QDataStream>
46 #include <QtCore/QUrl>
47 #include <QtCore/QEventLoop>
48 #include <QtCore/QFile>
49 #include <QtCore/QSharedPointer>
50 #include <QtCore/QScopedPointer>
51 #include <QtCore/QTemporaryFile>
52 #include <QtNetwork/QTcpServer>
53 #include <QtNetwork/QTcpSocket>
54 #include <QtNetwork/QLocalSocket>
55 #include <QtNetwork/QLocalServer>
56 #include <QtNetwork/QHostInfo>
57 #include <QtNetwork/QNetworkAccessManager>
58 #include <QtNetwork/QNetworkRequest>
59 #include <QtNetwork/QNetworkReply>
60 #include <QtNetwork/QAbstractNetworkCache>
61 #include <QtNetwork/qauthenticator.h>
62 #include <QtNetwork/qnetworkaccessmanager.h>
63 #include <QtNetwork/qnetworkrequest.h>
64 #include <QtNetwork/qnetworkreply.h>
65 #include <QtNetwork/qnetworkcookie.h>
66 #include <QtNetwork/QNetworkCookieJar>
67 #include <QtNetwork/QHttpPart>
68 #include <QtNetwork/QHttpMultiPart>
69 #include <QtNetwork/QNetworkProxyQuery>
70 #ifndef QT_NO_SSL
71 #include <QtNetwork/qsslerror.h>
72 #include <QtNetwork/qsslconfiguration.h>
73 #endif
74 #ifndef QT_NO_BEARERMANAGEMENT
75 #include <QtNetwork/qnetworkconfigmanager.h>
76 #include <QtNetwork/qnetworkconfiguration.h>
77 #include <QtNetwork/qnetworksession.h>
78 #include <QtNetwork/private/qnetworksession_p.h>
79 #endif
80 #ifdef QT_BUILD_INTERNAL
81 #include <QtNetwork/private/qnetworkaccessmanager_p.h>
82 #endif
83
84 #ifdef Q_OS_UNIX
85 # include <sys/types.h>
86 # include <unistd.h> // for getuid()
87 #endif
88 #include <time.h>
89
90 #include "../../../network-settings.h"
91
92 Q_DECLARE_METATYPE(QSharedPointer<char>)
93 Q_DECLARE_METATYPE(QNetworkReply*)
94 Q_DECLARE_METATYPE(QAuthenticator*)
95 Q_DECLARE_METATYPE(QNetworkProxy)
96 Q_DECLARE_METATYPE(QNetworkProxyQuery)
97 Q_DECLARE_METATYPE(QList<QNetworkProxy>)
98 Q_DECLARE_METATYPE(QNetworkReply::NetworkError)
99 Q_DECLARE_METATYPE(QBuffer*)
100 Q_DECLARE_METATYPE(QHttpMultiPart *)
101 Q_DECLARE_METATYPE(QList<QFile*>) // for multiparts
102 #ifndef QT_NO_SSL
103 Q_DECLARE_METATYPE(QSslConfiguration)
104 #endif
105
106 typedef QSharedPointer<QNetworkReply> QNetworkReplyPtr;
107
108 class MyCookieJar;
109 class tst_QNetworkReply: public QObject
110 {
111     Q_OBJECT
112
113     struct ProxyData {
114         ProxyData(const QNetworkProxy &p, const QByteArray &t, bool auth)
115             : tag(t), proxy(p), requiresAuthentication(auth)
116         { }
117         QByteArray tag;
118         QNetworkProxy proxy;
119         bool requiresAuthentication;
120     };
121
122     static bool seedCreated;
123     static QString createUniqueExtension() {
124         if (!seedCreated) {
125             qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) + QCoreApplication::applicationPid());
126             seedCreated = true; // not thread-safe, but who cares
127         }
128         QString s = QString("%1-%2-%3").arg(QTime(0,0,0).msecsTo(QTime::currentTime())).arg(QCoreApplication::applicationPid()).arg(qrand());
129         return s;
130     };
131
132     QEventLoop *loop;
133     enum RunSimpleRequestReturn { Timeout = 0, Success, Failure };
134     int returnCode;
135     QString testFileName;
136     QString echoProcessDir;
137 #if !defined Q_OS_WIN
138     QString wronlyFileName;
139 #endif
140     QString uniqueExtension;
141     QList<ProxyData> proxies;
142     QNetworkAccessManager manager;
143     MyCookieJar *cookieJar;
144 #ifndef QT_NO_SSL
145     QSslConfiguration storedSslConfiguration;
146     QList<QSslError> storedExpectedSslErrors;
147 #endif
148 #ifndef QT_NO_BEARERMANAGEMENT
149     QNetworkConfigurationManager *netConfMan;
150     QNetworkConfiguration networkConfiguration;
151     QScopedPointer<QNetworkSession> networkSession;
152 #endif
153
154     using QObject::connect;
155     static bool connect(const QNetworkReplyPtr &ptr, const char *signal, const QObject *receiver, const char *slot, Qt::ConnectionType ct = Qt::AutoConnection)
156     { return connect(ptr.data(), signal, receiver, slot, ct); }
157     bool connect(const QNetworkReplyPtr &ptr, const char *signal, const char *slot, Qt::ConnectionType ct = Qt::AutoConnection)
158     { return connect(ptr.data(), signal, slot, ct); }
159
160 public:
161     tst_QNetworkReply();
162     ~tst_QNetworkReply();
163     QString runSimpleRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request,
164                              QNetworkReplyPtr &reply, const QByteArray &data = QByteArray());
165     QString runMultipartRequest(const QNetworkRequest &request, QNetworkReplyPtr &reply,
166                                     QHttpMultiPart *multiPart, const QByteArray &verb);
167
168     QString runCustomRequest(const QNetworkRequest &request, QNetworkReplyPtr &reply,
169                              const QByteArray &verb, QIODevice *data);
170     int waitForFinish(QNetworkReplyPtr &reply);
171
172 public Q_SLOTS:
173     void finished();
174     void gotError();
175     void authenticationRequired(QNetworkReply*,QAuthenticator*);
176     void proxyAuthenticationRequired(const QNetworkProxy &,QAuthenticator*);
177     void pipeliningHelperSlot();
178
179 #ifndef QT_NO_SSL
180     void sslErrors(QNetworkReply*,const QList<QSslError> &);
181     void storeSslConfiguration();
182     void ignoreSslErrorListSlot(QNetworkReply *reply, const QList<QSslError> &);
183 #endif
184
185 protected Q_SLOTS:
186     void nestedEventLoops_slot();
187
188 private Q_SLOTS:
189     void init();
190     void cleanup();
191     void initTestCase();
192     void cleanupTestCase();
193
194     void stateChecking();
195     void invalidProtocol();
196     void getFromData_data();
197     void getFromData();
198     void getFromFile();
199     void getFromFileSpecial_data();
200     void getFromFileSpecial();
201     void getFromFtp_data();
202     void getFromFtp();
203     void getFromHttp_data();
204     void getFromHttp();
205     void getErrors_data();
206     void getErrors();
207     void headFromHttp_data();
208     void headFromHttp();
209     void putToFile_data();
210     void putToFile();
211     void putToFtp_data();
212     void putToFtp();
213     void putToHttp_data();
214     void putToHttp();
215     void putToHttpSynchronous_data();
216     void putToHttpSynchronous();
217     void putToHttpMultipart_data();
218     void putToHttpMultipart();
219     void postToHttp_data();
220     void postToHttp();
221     void postToHttpSynchronous_data();
222     void postToHttpSynchronous();
223     void postToHttpMultipart_data();
224     void postToHttpMultipart();
225     void deleteFromHttp_data();
226     void deleteFromHttp();
227     void putGetDeleteGetFromHttp_data();
228     void putGetDeleteGetFromHttp();
229     void sendCustomRequestToHttp_data();
230     void sendCustomRequestToHttp();
231     void connectToIPv6Address_data();
232     void connectToIPv6Address();
233
234     void ioGetFromData_data();
235     void ioGetFromData();
236     void ioGetFromFileSpecial_data();
237     void ioGetFromFileSpecial();
238     void ioGetFromFile_data();
239     void ioGetFromFile();
240     void ioGetFromFtp_data();
241     void ioGetFromFtp();
242     void ioGetFromFtpWithReuse();
243     void ioGetFromHttp();
244
245     void ioGetFromBuiltinHttp_data();
246     void ioGetFromBuiltinHttp();
247     void ioGetFromHttpWithReuseParallel();
248     void ioGetFromHttpWithReuseSequential();
249     void ioGetFromHttpWithAuth_data();
250     void ioGetFromHttpWithAuth();
251     void ioGetFromHttpWithAuthSynchronous();
252     void ioGetFromHttpWithProxyAuth();
253     void ioGetFromHttpWithProxyAuthSynchronous();
254     void ioGetFromHttpWithSocksProxy();
255 #ifndef QT_NO_SSL
256     void ioGetFromHttpsWithSslErrors();
257     void ioGetFromHttpsWithIgnoreSslErrors();
258     void ioGetFromHttpsWithSslHandshakeError();
259 #endif
260     void ioGetFromHttpBrokenServer_data();
261     void ioGetFromHttpBrokenServer();
262     void ioGetFromHttpStatus100_data();
263     void ioGetFromHttpStatus100();
264     void ioGetFromHttpNoHeaders_data();
265     void ioGetFromHttpNoHeaders();
266     void ioGetFromHttpWithCache_data();
267     void ioGetFromHttpWithCache();
268
269     void ioGetWithManyProxies_data();
270     void ioGetWithManyProxies();
271
272     void ioPutToFileFromFile_data();
273     void ioPutToFileFromFile();
274     void ioPutToFileFromSocket_data();
275     void ioPutToFileFromSocket();
276     void ioPutToFileFromLocalSocket_data();
277     void ioPutToFileFromLocalSocket();
278 #ifndef QT_NO_PROCESS
279     void ioPutToFileFromProcess_data();
280     void ioPutToFileFromProcess();
281 #endif
282     void ioPutToFtpFromFile_data();
283     void ioPutToFtpFromFile();
284     void ioPutToHttpFromFile_data();
285     void ioPutToHttpFromFile();
286     void ioPostToHttpFromFile_data();
287     void ioPostToHttpFromFile();
288     void ioPostToHttpFromSocket_data();
289     void ioPostToHttpFromSocket();
290     void ioPostToHttpFromSocketSynchronous();
291     void ioPostToHttpFromSocketSynchronous_data();
292     void ioPostToHttpFromMiddleOfFileToEnd();
293     void ioPostToHttpFromMiddleOfFileFiveBytes();
294     void ioPostToHttpFromMiddleOfQBufferFiveBytes();
295     void ioPostToHttpNoBufferFlag();
296     void ioPostToHttpUploadProgress();
297     void ioPostToHttpEmptyUploadProgress();
298
299     void lastModifiedHeaderForFile();
300     void lastModifiedHeaderForHttp();
301
302     void httpCanReadLine();
303
304     void rateControl_data();
305     void rateControl();
306
307     void downloadProgress_data();
308     void downloadProgress();
309     void uploadProgress_data();
310     void uploadProgress();
311
312     void chaining_data();
313     void chaining();
314
315     void receiveCookiesFromHttp_data();
316     void receiveCookiesFromHttp();
317     void receiveCookiesFromHttpSynchronous_data();
318     void receiveCookiesFromHttpSynchronous();
319     void sendCookies_data();
320     void sendCookies();
321     void sendCookiesSynchronous_data();
322     void sendCookiesSynchronous();
323
324     void nestedEventLoops();
325
326     void httpProxyCommands_data();
327     void httpProxyCommands();
328     void httpProxyCommandsSynchronous_data();
329     void httpProxyCommandsSynchronous();
330     void proxyChange();
331     void authorizationError_data();
332     void authorizationError();
333
334     void httpConnectionCount();
335
336     void httpReUsingConnectionSequential_data();
337     void httpReUsingConnectionSequential();
338     void httpReUsingConnectionFromFinishedSlot_data();
339     void httpReUsingConnectionFromFinishedSlot();
340
341     void httpRecursiveCreation();
342
343 #ifndef QT_NO_SSL
344     void ioPostToHttpsUploadProgress();
345     void ignoreSslErrorsList_data();
346     void ignoreSslErrorsList();
347     void ignoreSslErrorsListWithSlot_data();
348     void ignoreSslErrorsListWithSlot();
349     void sslConfiguration_data();
350     void sslConfiguration();
351 #endif
352
353     void getAndThenDeleteObject_data();
354     void getAndThenDeleteObject();
355
356     void symbianOpenCDataUrlCrash();
357
358     void getFromHttpIntoBuffer_data();
359     void getFromHttpIntoBuffer();
360     void getFromHttpIntoBuffer2_data();
361     void getFromHttpIntoBuffer2();
362     void getFromHttpIntoBufferCanReadLine();
363
364     void ioGetFromHttpWithoutContentLength();
365
366     void ioGetFromHttpBrokenChunkedEncoding();
367     void qtbug12908compressedHttpReply();
368     void compressedHttpReplyBrokenGzip();
369
370     void getFromUnreachableIp();
371
372     void qtbug4121unknownAuthentication();
373
374     void qtbug13431replyThrottling();
375
376     void httpWithNoCredentialUsage();
377
378     void qtbug15311doubleContentLength();
379
380     void qtbug18232gzipContentLengthZero();
381     void qtbug22660gzipNoContentLengthEmptyContent();
382
383     void synchronousRequest_data();
384     void synchronousRequest();
385 #ifndef QT_NO_SSL
386     void synchronousRequestSslFailure();
387 #endif
388
389     void httpAbort();
390
391     void dontInsertPartialContentIntoTheCache();
392
393     void httpUserAgent();
394     void authenticationCacheAfterCancel_data();
395     void authenticationCacheAfterCancel();
396     void authenticationWithDifferentRealm();
397     void synchronousAuthenticationCache();
398     void pipelining();
399
400     void closeDuringDownload_data();
401     void closeDuringDownload();
402
403     void ftpAuthentication_data();
404     void ftpAuthentication();
405
406     void backgroundRequest_data();
407     void backgroundRequest();
408     void backgroundRequestInterruption_data();
409     void backgroundRequestInterruption();
410     void backgroundRequestConnectInBackground_data();
411     void backgroundRequestConnectInBackground();
412
413     // NOTE: This test must be last!
414     void parentingRepliesToTheApp();
415 private:
416     QString testDataDir;
417 };
418
419 bool tst_QNetworkReply::seedCreated = false;
420
421 QT_BEGIN_NAMESPACE
422
423 namespace QTest {
424     template<>
425     char *toString(const QNetworkReply::NetworkError& code)
426     {
427         const QMetaObject *mo = &QNetworkReply::staticMetaObject;
428         int index = mo->indexOfEnumerator("NetworkError");
429         if (index == -1)
430             return qstrdup("");
431
432         QMetaEnum qme = mo->enumerator(index);
433         return qstrdup(qme.valueToKey(code));
434     }
435
436     template<>
437     char *toString(const QNetworkCookie &cookie)
438     {
439         return qstrdup(cookie.toRawForm());
440     }
441
442     template<>
443     char *toString(const QList<QNetworkCookie> &list)
444     {
445         QString result = "QList(";
446         bool first = true;
447         foreach (QNetworkCookie cookie, list) {
448             if (!first)
449                 result += ", ";
450             first = false;
451             result += QString::fromLatin1("QNetworkCookie(%1)").arg(QLatin1String(cookie.toRawForm()));
452         }
453
454         return qstrdup(result.append(')').toLocal8Bit());
455     }
456 }
457
458 QT_END_NAMESPACE
459
460 #define RUN_REQUEST(call)                       \
461     do {                                        \
462         QString errorMsg = call;                \
463         if (!errorMsg.isEmpty())                \
464             QFAIL(qPrintable(errorMsg));        \
465     } while (0);
466
467 #ifndef QT_NO_SSL
468 static void setupSslServer(QSslSocket* serverSocket)
469 {
470     QString testDataDir = QFileInfo(QFINDTESTDATA("rfc3252.txt")).absolutePath();
471     if (testDataDir.isEmpty())
472         testDataDir = QCoreApplication::applicationDirPath();
473
474     serverSocket->setProtocol(QSsl::AnyProtocol);
475     serverSocket->setLocalCertificate(testDataDir + "/certs/server.pem");
476     serverSocket->setPrivateKey(testDataDir + "/certs/server.key");
477 }
478 #endif
479
480 // Does not work for POST/PUT!
481 class MiniHttpServer: public QTcpServer
482 {
483     Q_OBJECT
484 public:
485     QTcpSocket *client; // always the last one that was received
486     QByteArray dataToTransmit;
487     QByteArray receivedData;
488     QSemaphore ready;
489     bool doClose;
490     bool doSsl;
491     bool ipv6;
492     bool multiple;
493     int totalConnections;
494
495     MiniHttpServer(const QByteArray &data, bool ssl = false, QThread *thread = 0, bool useipv6 = false)
496         : client(0), dataToTransmit(data), doClose(true), doSsl(ssl), ipv6(useipv6),
497           multiple(false), totalConnections(0)
498     {
499         if (useipv6) {
500             listen(QHostAddress::AnyIPv6);
501         } else {
502             listen(QHostAddress::AnyIPv4);
503         }
504         if (thread) {
505             connect(thread, SIGNAL(started()), this, SLOT(threadStartedSlot()));
506             moveToThread(thread);
507             thread->start();
508             ready.acquire();
509         }
510     }
511
512 protected:
513     void incomingConnection(qintptr socketDescriptor)
514     {
515         //qDebug() << "incomingConnection" << socketDescriptor << "doSsl:" << doSsl << "ipv6:" << ipv6;
516         if (!doSsl) {
517             client = new QTcpSocket;
518             client->setSocketDescriptor(socketDescriptor);
519             connectSocketSignals();
520         } else {
521 #ifndef QT_NO_SSL
522             QSslSocket *serverSocket = new QSslSocket;
523             serverSocket->setParent(this);
524             if (serverSocket->setSocketDescriptor(socketDescriptor)) {
525                 connect(serverSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
526                 setupSslServer(serverSocket);
527                 serverSocket->startServerEncryption();
528                 client = serverSocket;
529                 connectSocketSignals();
530             } else {
531                 delete serverSocket;
532                 return;
533             }
534 #endif
535         }
536         client->setParent(this);
537         ++totalConnections;
538     }
539
540     virtual void reply() {
541         // we need to emulate the bytesWrittenSlot call if the data is empty.
542         if (dataToTransmit.size() == 0)
543             QMetaObject::invokeMethod(this, "bytesWrittenSlot", Qt::QueuedConnection);
544         else
545             client->write(dataToTransmit);
546     }
547 private:
548     void connectSocketSignals()
549     {
550         //qDebug() << "connectSocketSignals" << client;
551         connect(client, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
552         connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWrittenSlot()));
553         connect(client, SIGNAL(error(QAbstractSocket::SocketError)),
554                 this, SLOT(slotError(QAbstractSocket::SocketError)));
555     }
556
557 private slots:
558 #ifndef QT_NO_SSL
559     void slotSslErrors(const QList<QSslError>& errors)
560     {
561         qDebug() << "slotSslErrors" << client->errorString() << errors;
562     }
563 #endif
564     void slotError(QAbstractSocket::SocketError err)
565     {
566         qDebug() << "slotError" << err << client->errorString();
567     }
568
569 public slots:
570     void readyReadSlot()
571     {
572         receivedData += client->readAll();
573         int doubleEndlPos = receivedData.indexOf("\r\n\r\n");
574
575         if (doubleEndlPos != -1) {
576             // multiple requests incoming. remove the bytes of the current one
577             if (multiple)
578                 receivedData.remove(0, doubleEndlPos+4);
579
580             reply();
581         }
582     }
583
584     void bytesWrittenSlot() {
585         if (doClose && client->bytesToWrite() == 0) {
586             client->disconnectFromHost();
587             disconnect(client, 0, this, 0);
588         }
589     }
590
591     void threadStartedSlot()
592     {
593         ready.release();
594     }
595 };
596
597 class MyCookieJar: public QNetworkCookieJar
598 {
599 public:
600     inline QList<QNetworkCookie> allCookies() const
601         { return QNetworkCookieJar::allCookies(); }
602     inline void setAllCookies(const QList<QNetworkCookie> &cookieList)
603         { QNetworkCookieJar::setAllCookies(cookieList); }
604 };
605
606 class MyProxyFactory: public QNetworkProxyFactory
607 {
608 public:
609     int callCount;
610     QList<QNetworkProxy> toReturn;
611     QNetworkProxyQuery lastQuery;
612     inline MyProxyFactory() { clear(); }
613
614     inline void clear()
615     {
616         callCount = 0;
617         toReturn = QList<QNetworkProxy>() << QNetworkProxy::DefaultProxy;
618         lastQuery = QNetworkProxyQuery();
619     }
620
621     virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query)
622     {
623         lastQuery = query;
624         ++callCount;
625         return toReturn;
626     }
627 };
628
629 class MyMemoryCache: public QAbstractNetworkCache
630 {
631 public:
632     typedef QPair<QNetworkCacheMetaData, QByteArray> CachedContent;
633     typedef QHash<QByteArray, CachedContent> CacheData;
634     CacheData cache;
635
636     MyMemoryCache(QObject *parent) : QAbstractNetworkCache(parent) {}
637
638     QNetworkCacheMetaData metaData(const QUrl &url)
639     {
640         return cache.value(url.toEncoded()).first;
641     }
642
643     void updateMetaData(const QNetworkCacheMetaData &metaData)
644     {
645         cache[metaData.url().toEncoded()].first = metaData;
646     }
647
648     QIODevice *data(const QUrl &url)
649     {
650         CacheData::ConstIterator it = cache.find(url.toEncoded());
651         if (it == cache.constEnd())
652             return 0;
653         QBuffer *io = new QBuffer(this);
654         io->setData(it->second);
655         io->open(QIODevice::ReadOnly);
656         io->seek(0);
657         return io;
658     }
659
660     bool remove(const QUrl &url)
661     {
662         cache.remove(url.toEncoded());
663         return true;
664     }
665
666     qint64 cacheSize() const
667     {
668         qint64 total = 0;
669         foreach (const CachedContent &entry, cache)
670             total += entry.second.size();
671         return total;
672     }
673
674     QIODevice *prepare(const QNetworkCacheMetaData &)
675     {
676         qFatal("%s: Should not have tried to add to the cache", Q_FUNC_INFO);
677         return 0;
678     }
679     void insert(QIODevice *)
680     {
681         qFatal("%s: Should not have tried to add to the cache", Q_FUNC_INFO);
682     }
683
684     void clear() { cache.clear(); }
685 };
686 Q_DECLARE_METATYPE(MyMemoryCache::CachedContent)
687 Q_DECLARE_METATYPE(MyMemoryCache::CacheData)
688
689 class MySpyMemoryCache: public QAbstractNetworkCache
690 {
691 public:
692     MySpyMemoryCache(QObject *parent) : QAbstractNetworkCache(parent) {}
693     ~MySpyMemoryCache()
694     {
695         qDeleteAll(m_buffers);
696         m_buffers.clear();
697     }
698
699     QHash<QUrl, QIODevice*> m_buffers;
700     QList<QUrl> m_insertedUrls;
701
702     QNetworkCacheMetaData metaData(const QUrl &)
703     {
704         return QNetworkCacheMetaData();
705     }
706
707     void updateMetaData(const QNetworkCacheMetaData &)
708     {
709     }
710
711     QIODevice *data(const QUrl &)
712     {
713         return 0;
714     }
715
716     bool remove(const QUrl &url)
717     {
718         delete m_buffers.take(url);
719         return m_insertedUrls.removeAll(url) > 0;
720     }
721
722     qint64 cacheSize() const
723     {
724         return 0;
725     }
726
727     QIODevice *prepare(const QNetworkCacheMetaData &metaData)
728     {
729         QBuffer* buffer = new QBuffer;
730         buffer->open(QIODevice::ReadWrite);
731         buffer->setProperty("url", metaData.url());
732         m_buffers.insert(metaData.url(), buffer);
733         return buffer;
734     }
735
736     void insert(QIODevice *buffer)
737     {
738         QUrl url = buffer->property("url").toUrl();
739         m_insertedUrls << url;
740         delete m_buffers.take(url);
741     }
742
743     void clear() { m_insertedUrls.clear(); }
744 };
745
746 class DataReader: public QObject
747 {
748     Q_OBJECT
749 public:
750     qint64 totalBytes;
751     QByteArray data;
752     QIODevice *device;
753     bool accumulate;
754     DataReader(const QNetworkReplyPtr &dev, bool acc = true) : totalBytes(0), device(dev.data()), accumulate(acc)
755     { connect(device, SIGNAL(readyRead()), SLOT(doRead()) ); }
756     DataReader(QIODevice *dev, bool acc = true) : totalBytes(0), device(dev), accumulate(acc)
757     {
758         connect(device, SIGNAL(readyRead()), SLOT(doRead()));
759     }
760
761 public slots:
762     void doRead()
763     {
764         QByteArray buffer;
765         buffer.resize(device->bytesAvailable());
766         qint64 bytesRead = device->read(buffer.data(), device->bytesAvailable());
767         if (bytesRead == -1) {
768             QTestEventLoop::instance().exitLoop();
769             return;
770         }
771         buffer.truncate(bytesRead);
772         totalBytes += bytesRead;
773
774         if (accumulate)
775             data += buffer;
776     }
777 };
778
779
780 class SocketPair: public QObject
781 {
782     Q_OBJECT
783 public:
784     QIODevice *endPoints[2];
785
786     SocketPair(QObject *parent = 0)
787         : QObject(parent)
788     {
789         endPoints[0] = endPoints[1] = 0;
790     }
791
792     bool create()
793     {
794         QTcpServer server;
795         server.listen();
796
797         QTcpSocket *active = new QTcpSocket(this);
798         active->connectToHost("127.0.0.1", server.serverPort());
799
800         // need more time as working with embedded
801         // device and testing from emualtor
802         // things tend to get slower
803         if (!active->waitForConnected(1000))
804             return false;
805
806         if (!server.waitForNewConnection(1000))
807             return false;
808
809         QTcpSocket *passive = server.nextPendingConnection();
810         passive->setParent(this);
811
812         endPoints[0] = active;
813         endPoints[1] = passive;
814         return true;
815     }
816 };
817
818 // A blocking tcp server (must be used in a thread) which supports SSL.
819 class BlockingTcpServer : public QTcpServer
820 {
821     Q_OBJECT
822 public:
823     BlockingTcpServer(bool ssl) : doSsl(ssl), sslSocket(0) {}
824
825     QTcpSocket* waitForNextConnectionSocket() {
826         waitForNewConnection(-1);
827         if (doSsl) {
828             if (!sslSocket)
829                 qFatal("%s: sslSocket should not be null after calling waitForNewConnection()",
830                        Q_FUNC_INFO);
831             return sslSocket;
832         } else {
833             //qDebug() << "returning nextPendingConnection";
834             return nextPendingConnection();
835         }
836     }
837     virtual void incomingConnection(qintptr socketDescriptor)
838     {
839 #ifndef QT_NO_SSL
840         if (doSsl) {
841             QSslSocket *serverSocket = new QSslSocket;
842             serverSocket->setParent(this);
843             serverSocket->setSocketDescriptor(socketDescriptor);
844             connect(serverSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
845             setupSslServer(serverSocket);
846             serverSocket->startServerEncryption();
847             sslSocket = serverSocket;
848         } else
849 #endif
850         {
851             QTcpServer::incomingConnection(socketDescriptor);
852         }
853     }
854 private slots:
855
856 #ifndef QT_NO_SSL
857     void slotSslErrors(const QList<QSslError>& errors)
858     {
859         qDebug() << "slotSslErrors" << sslSocket->errorString() << errors;
860     }
861 #endif
862
863 private:
864     const bool doSsl;
865     QTcpSocket* sslSocket;
866 };
867
868 // This server tries to send data as fast as possible (like most servers)
869 // but it measures how fast it was able to send it, which shows at which
870 // rate the reader is processing the data.
871 class FastSender: public QThread
872 {
873     Q_OBJECT
874     QSemaphore ready;
875     qint64 wantedSize;
876     int port;
877     enum Protocol { DebugPipe, ProvidedData };
878     const Protocol protocol;
879     const bool doSsl;
880     const bool fillKernelBuffer;
881 public:
882     int transferRate;
883     QWaitCondition cond;
884
885     QByteArray dataToTransmit;
886     int dataIndex;
887
888     // a server that sends debugpipe data
889     FastSender(qint64 size)
890         : wantedSize(size), port(-1), protocol(DebugPipe),
891           doSsl(false), fillKernelBuffer(true), transferRate(-1),
892           dataIndex(0)
893     {
894         start();
895         ready.acquire();
896     }
897
898     // a server that sends the data provided at construction time, useful for HTTP
899     FastSender(const QByteArray& data, bool https, bool fillBuffer)
900         : wantedSize(data.size()), port(-1), protocol(ProvidedData),
901           doSsl(https), fillKernelBuffer(fillBuffer), transferRate(-1),
902           dataToTransmit(data), dataIndex(0)
903     {
904         start();
905         ready.acquire();
906     }
907
908     inline int serverPort() const { return port; }
909
910     int writeNextData(QTcpSocket* socket, qint32 size)
911     {
912         if (protocol == DebugPipe) {
913             QByteArray data;
914             QDataStream stream(&data, QIODevice::WriteOnly);
915             stream << QVariantMap() << QByteArray(size, 'a');
916             socket->write((char*)&size, sizeof size);
917             socket->write(data);
918             dataIndex += size;
919             return size;
920         } else {
921             const QByteArray data = dataToTransmit.mid(dataIndex, size);
922             socket->write(data);
923             dataIndex += data.size();
924             //qDebug() << "wrote" << dataIndex << "/" << dataToTransmit.size();
925             return data.size();
926         }
927     }
928     void writeLastData(QTcpSocket* socket)
929     {
930         if (protocol == DebugPipe) {
931             QByteArray data;
932             QDataStream stream(&data, QIODevice::WriteOnly);
933             stream << QVariantMap() << QByteArray();
934             const qint32 size = data.size();
935             socket->write((char*)&size, sizeof size);
936             socket->write(data);
937         }
938     }
939
940 protected:
941     void run()
942     {
943         BlockingTcpServer server(doSsl);
944         server.listen();
945         port = server.serverPort();
946         ready.release();
947
948         QTcpSocket *client = server.waitForNextConnectionSocket();
949
950         // get the "request" packet
951         if (!client->waitForReadyRead(2000)) {
952             qDebug() << "FastSender:" << client->error() << "waiting for \"request\" packet";
953             return;
954         }
955         client->readAll();      // we're not interested in the actual contents (e.g. HTTP request)
956
957         enum { BlockSize = 1024 };
958
959         if (fillKernelBuffer) {
960
961             // write a bunch of bytes to fill up the buffers
962             bool done = false;
963             do {
964                 if (writeNextData(client, BlockSize) < BlockSize) {
965                     qDebug() << "ERROR: FastSender: not enough data to write in order to fill buffers; or client is reading too fast";
966                     return;
967                 }
968                 while (client->bytesToWrite() > 0) {
969                     if (!client->waitForBytesWritten(0)) {
970                         done = true;
971                         break;
972                     }
973                 }
974                 //qDebug() << "Filling kernel buffer: wrote" << dataIndex << "bytes";
975             } while (!done);
976
977             qDebug() << "FastSender: ok, kernel buffer is full after writing" << dataIndex << "bytes";
978         }
979
980         // Tell the client to start reading
981         emit dataReady();
982
983         // the kernel buffer is full
984         // clean up QAbstractSocket's residue:
985         while (client->bytesToWrite() > 0) {
986             qDebug() << "Still having" << client->bytesToWrite() << "bytes to write, doing that now";
987             if (!client->waitForBytesWritten(2000)) {
988                 qDebug() << "ERROR: FastSender:" << client->error() << "cleaning up residue";
989                 return;
990             }
991         }
992
993         // now write in "blocking mode", this is where the rate measuring starts
994         QTime timer;
995         timer.start();
996         //const qint64 writtenBefore = dataIndex;
997         //qint64 measuredTotalBytes = wantedSize - writtenBefore;
998         qint64 measuredSentBytes = 0;
999         while (dataIndex < wantedSize) {
1000             const int remainingBytes = wantedSize - measuredSentBytes;
1001             const int bytesToWrite = qMin(remainingBytes, static_cast<int>(BlockSize));
1002             if (bytesToWrite <= 0)
1003                 qFatal("%s: attempt to write %d bytes", Q_FUNC_INFO, bytesToWrite);
1004             measuredSentBytes += writeNextData(client, bytesToWrite);
1005
1006             while (client->bytesToWrite() > 0) {
1007                 if (!client->waitForBytesWritten(2000)) {
1008                     qDebug() << "ERROR: FastSender:" << client->error() << "during blocking write";
1009                     return;
1010                 }
1011             }
1012             /*qDebug() << "FastSender:" << bytesToWrite << "bytes written now;"
1013                      << measuredSentBytes << "measured bytes" << measuredSentBytes + writtenBefore << "total ("
1014                      << measuredSentBytes*100/measuredTotalBytes << "% complete);"
1015                      << timer.elapsed() << "ms elapsed";*/
1016         }
1017
1018         transferRate = measuredSentBytes * 1000 / timer.elapsed();
1019         qDebug() << "FastSender: flushed" << measuredSentBytes << "bytes in" << timer.elapsed() << "ms: rate =" << transferRate << "B/s";
1020
1021         // write a "close connection" packet, if the protocol needs it
1022         writeLastData(client);
1023     }
1024 signals:
1025     void dataReady();
1026 };
1027
1028 class RateControlledReader: public QObject
1029 {
1030     Q_OBJECT
1031     QIODevice *device;
1032     int bytesToRead;
1033     int interval;
1034     int readBufferSize;
1035 public:
1036     QByteArray data;
1037     qint64 totalBytesRead;
1038     RateControlledReader(QObject& senderObj, QIODevice *dev, int kbPerSec, int maxBufferSize = 0)
1039         : device(dev), readBufferSize(maxBufferSize), totalBytesRead(0)
1040     {
1041         // determine how often we have to wake up
1042         int timesPerSecond;
1043         if (readBufferSize == 0) {
1044             // The requirement is simply "N KB per seconds"
1045             timesPerSecond = 20;
1046             bytesToRead = kbPerSec * 1024 / timesPerSecond;
1047         } else {
1048             // The requirement also includes "<readBufferSize> bytes at a time"
1049             bytesToRead = readBufferSize;
1050             timesPerSecond = kbPerSec * 1024 / readBufferSize;
1051         }
1052         interval = 1000 / timesPerSecond; // in ms
1053
1054         qDebug() << "RateControlledReader: going to read" << bytesToRead
1055                  << "bytes every" << interval << "ms";
1056         qDebug() << "actual read rate will be"
1057                  << (bytesToRead * 1000 / interval) << "bytes/sec (wanted"
1058                  << kbPerSec * 1024 << "bytes/sec)";
1059
1060         // Wait for data to be readyRead
1061         bool ok = connect(&senderObj, SIGNAL(dataReady()), this, SLOT(slotDataReady()));
1062         if (!ok)
1063             qFatal("%s: Cannot connect dataReady signal", Q_FUNC_INFO);
1064     }
1065
1066     void wrapUp()
1067     {
1068         QByteArray someData = device->read(device->bytesAvailable());
1069         data += someData;
1070         totalBytesRead += someData.size();
1071         qDebug() << "wrapUp: found" << someData.size() << "bytes left. progress" << data.size();
1072         //qDebug() << "wrapUp: now bytesAvailable=" << device->bytesAvailable();
1073     }
1074
1075 private slots:
1076     void slotDataReady()
1077     {
1078         //qDebug() << "RateControlledReader: ready to go";
1079         startTimer(interval);
1080     }
1081
1082 protected:
1083     void timerEvent(QTimerEvent *)
1084     {
1085         //qDebug() << "RateControlledReader: timerEvent bytesAvailable=" << device->bytesAvailable();
1086         if (readBufferSize > 0 && device->bytesAvailable() > readBufferSize) {
1087             // This passes all the time, except in the final flush.
1088             //qFatal("%s: Too many bytes available", Q_FUNC_INFO);
1089         }
1090
1091         qint64 bytesRead = 0;
1092         QTime stopWatch;
1093         stopWatch.start();
1094         do {
1095             if (device->bytesAvailable() == 0) {
1096                 if (stopWatch.elapsed() > 20) {
1097                     qDebug() << "RateControlledReader: Not enough data available for reading, waited too much, timing out";
1098                     break;
1099                 }
1100                 if (!device->waitForReadyRead(5)) {
1101                     qDebug() << "RateControlledReader: Not enough data available for reading, even after waiting 5ms, bailing out";
1102                     break;
1103                 }
1104             }
1105             QByteArray someData = device->read(bytesToRead - bytesRead);
1106             data += someData;
1107             bytesRead += someData.size();
1108             //qDebug() << "RateControlledReader: successfully read" << someData.size() << "progress:" << data.size();
1109         } while (bytesRead < bytesToRead);
1110         totalBytesRead += bytesRead;
1111
1112         if (bytesRead < bytesToRead)
1113             qWarning() << "RateControlledReader: WARNING:" << bytesToRead - bytesRead << "bytes not read";
1114     }
1115 };
1116
1117
1118 tst_QNetworkReply::tst_QNetworkReply()
1119 {
1120     qRegisterMetaType<QNetworkReply *>(); // for QSignalSpy
1121     qRegisterMetaType<QAuthenticator *>();
1122     qRegisterMetaType<QNetworkProxy>();
1123 #ifndef QT_NO_SSL
1124     qRegisterMetaType<QList<QSslError> >();
1125 #endif
1126     qRegisterMetaType<QNetworkReply::NetworkError>();
1127
1128     uniqueExtension = createUniqueExtension();
1129     testFileName = QDir::currentPath() + "/testfile" + uniqueExtension;
1130     cookieJar = new MyCookieJar;
1131     manager.setCookieJar(cookieJar);
1132
1133     QHostInfo hostInfo = QHostInfo::fromName(QtNetworkSettings::serverName());
1134
1135     proxies << ProxyData(QNetworkProxy::NoProxy, "", false);
1136
1137     if (hostInfo.error() == QHostInfo::NoError && !hostInfo.addresses().isEmpty()) {
1138         QString proxyserver = hostInfo.addresses().first().toString();
1139         proxies << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3128), "+proxy", false)
1140                 << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3129), "+proxyauth", true)
1141                 // currently unsupported
1142                 // << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3130), "+proxyauth-ntlm", true);
1143                 << ProxyData(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyserver, 1080), "+socks", false)
1144                 << ProxyData(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyserver, 1081), "+socksauth", true);
1145     } else {
1146         printf("==================================================================\n");
1147         printf("Proxy could not be looked up. No proxy will be used while testing!\n");
1148         printf("==================================================================\n");
1149     }
1150 }
1151
1152 tst_QNetworkReply::~tst_QNetworkReply()
1153 {
1154 }
1155
1156
1157 void tst_QNetworkReply::authenticationRequired(QNetworkReply*, QAuthenticator* auth)
1158 {
1159     auth->setUser("httptest");
1160     auth->setPassword("httptest");
1161 }
1162
1163 void tst_QNetworkReply::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator* auth)
1164 {
1165     auth->setUser("qsockstest");
1166     auth->setPassword("password");
1167 }
1168
1169 #ifndef QT_NO_SSL
1170 void tst_QNetworkReply::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
1171 {
1172     reply->ignoreSslErrors();
1173     QVERIFY(!errors.isEmpty());
1174     QVERIFY(!reply->sslConfiguration().isNull());
1175 }
1176
1177 void tst_QNetworkReply::storeSslConfiguration()
1178 {
1179     storedSslConfiguration = QSslConfiguration();
1180     QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
1181     if (reply)
1182         storedSslConfiguration = reply->sslConfiguration();
1183 }
1184 #endif
1185
1186 QString tst_QNetworkReply::runMultipartRequest(const QNetworkRequest &request,
1187                                                    QNetworkReplyPtr &reply,
1188                                                    QHttpMultiPart *multiPart,
1189                                                    const QByteArray &verb)
1190 {
1191     if (verb == "POST")
1192         reply.reset(manager.post(request, multiPart));
1193     else
1194         reply.reset(manager.put(request, multiPart));
1195
1196     // the code below is copied from tst_QNetworkReply::runSimpleRequest, see below
1197     reply->setParent(this);
1198     connect(reply, SIGNAL(finished()), SLOT(finished()));
1199     connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(gotError()));
1200     multiPart->setParent(reply.data());
1201
1202     returnCode = Timeout;
1203     loop = new QEventLoop;
1204     QTimer::singleShot(25000, loop, SLOT(quit()));
1205     int code = returnCode == Timeout ? loop->exec() : returnCode;
1206     delete loop;
1207     loop = 0;
1208
1209     switch (code) {
1210     case Failure:
1211         return "Request failed: " + reply->errorString();
1212     case Timeout:
1213         return "Network timeout";
1214     }
1215     return QString();
1216 }
1217
1218 QString tst_QNetworkReply::runSimpleRequest(QNetworkAccessManager::Operation op,
1219                                             const QNetworkRequest &request,
1220                                             QNetworkReplyPtr &reply,
1221                                             const QByteArray &data)
1222 {
1223     switch (op) {
1224     case QNetworkAccessManager::HeadOperation:
1225         reply.reset(manager.head(request));
1226         break;
1227
1228     case QNetworkAccessManager::GetOperation:
1229         reply.reset(manager.get(request));
1230         break;
1231
1232     case QNetworkAccessManager::PutOperation:
1233         reply.reset(manager.put(request, data));
1234         break;
1235
1236     case QNetworkAccessManager::PostOperation:
1237         reply.reset(manager.post(request, data));
1238         break;
1239
1240     case QNetworkAccessManager::DeleteOperation:
1241         reply.reset(manager.deleteResource(request));
1242         break;
1243
1244     default:
1245         qFatal("%s: Invalid/unknown operation requested", Q_FUNC_INFO);
1246     }
1247     reply->setParent(this);
1248
1249     returnCode = Timeout;
1250     int code = Success;
1251
1252     if (request.attribute(QNetworkRequest::SynchronousRequestAttribute).toBool()) {
1253         if (reply->isFinished())
1254             code = reply->error() != QNetworkReply::NoError ? Failure : Success;
1255         else
1256             code = Failure;
1257     } else {
1258         connect(reply, SIGNAL(finished()), SLOT(finished()));
1259         connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(gotError()));
1260
1261         int count = 0;
1262         loop = new QEventLoop;
1263         QSignalSpy spy(reply.data(), SIGNAL(downloadProgress(qint64,qint64)));
1264         while (!reply->isFinished()) {
1265             QTimer::singleShot(20000, loop, SLOT(quit()));
1266             code = loop->exec();
1267             if (count == spy.count() && !reply->isFinished()) {
1268                 code = Timeout;
1269                 break;
1270             }
1271             count = spy.count();
1272         }
1273         delete loop;
1274         loop = 0;
1275     }
1276
1277     switch (code) {
1278     case Failure:
1279         return "Request failed: " + reply->errorString();
1280     case Timeout:
1281         return "Network timeout";
1282     }
1283     return QString();
1284 }
1285
1286 QString tst_QNetworkReply::runCustomRequest(const QNetworkRequest &request,
1287                                             QNetworkReplyPtr &reply,
1288                                             const QByteArray &verb,
1289                                             QIODevice *data)
1290 {
1291     reply.reset(manager.sendCustomRequest(request, verb, data));
1292     reply->setParent(this);
1293     connect(reply, SIGNAL(finished()), SLOT(finished()));
1294     connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(gotError()));
1295
1296     returnCode = Timeout;
1297     loop = new QEventLoop;
1298     QTimer::singleShot(20000, loop, SLOT(quit()));
1299     int code = returnCode == Timeout ? loop->exec() : returnCode;
1300     delete loop;
1301     loop = 0;
1302
1303     switch (code) {
1304     case Failure:
1305         return "Request failed: " + reply->errorString();
1306     case Timeout:
1307         return "Network timeout";
1308     }
1309     return QString();
1310 }
1311
1312 int tst_QNetworkReply::waitForFinish(QNetworkReplyPtr &reply)
1313 {
1314     int count = 0;
1315
1316     connect(reply, SIGNAL(finished()), SLOT(finished()));
1317     connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(gotError()));
1318     returnCode = Success;
1319     loop = new QEventLoop;
1320     QSignalSpy spy(reply.data(), SIGNAL(downloadProgress(qint64,qint64)));
1321     while (!reply->isFinished()) {
1322         QTimer::singleShot(5000, loop, SLOT(quit()));
1323         if ( loop->exec() == Timeout && count == spy.count() && !reply->isFinished()) {
1324             returnCode = Timeout;
1325             break;
1326         }
1327         count = spy.count();
1328     }
1329     delete loop;
1330     loop = 0;
1331
1332     return returnCode;
1333 }
1334
1335 void tst_QNetworkReply::finished()
1336 {
1337     loop->exit(returnCode = Success);
1338 }
1339
1340 void tst_QNetworkReply::gotError()
1341 {
1342     loop->exit(returnCode = Failure);
1343     disconnect(QObject::sender(), SIGNAL(finished()), this, 0);
1344 }
1345
1346 void tst_QNetworkReply::initTestCase()
1347 {
1348     testDataDir = QFileInfo(QFINDTESTDATA("rfc3252.txt")).absolutePath();
1349     if (testDataDir.isEmpty())
1350         testDataDir = QCoreApplication::applicationDirPath();
1351
1352     QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
1353 #if !defined Q_OS_WIN
1354     wronlyFileName = testDataDir + "/write-only" + uniqueExtension;
1355     QFile wr(wronlyFileName);
1356     QVERIFY(wr.open(QIODevice::WriteOnly | QIODevice::Truncate));
1357     wr.setPermissions(QFile::WriteOwner | QFile::WriteUser);
1358     wr.close();
1359 #endif
1360
1361     QDir::setSearchPaths("testdata", QStringList() << testDataDir);
1362 #ifndef QT_NO_SSL
1363     QSslSocket::defaultCaCertificates(); //preload certificates
1364 #endif
1365 #ifndef QT_NO_BEARERMANAGEMENT
1366     netConfMan = new QNetworkConfigurationManager(this);
1367     networkConfiguration = netConfMan->defaultConfiguration();
1368     networkSession.reset(new QNetworkSession(networkConfiguration));
1369     if (!networkSession->isOpen()) {
1370         networkSession->open();
1371         QVERIFY(networkSession->waitForOpened(30000));
1372     }
1373 #endif
1374
1375     echoProcessDir = QFINDTESTDATA("echo");
1376     QVERIFY2(!echoProcessDir.isEmpty(), qPrintable(
1377         QString::fromLatin1("Couldn't find echo dir starting from %1.").arg(QDir::currentPath())));
1378 }
1379
1380 void tst_QNetworkReply::cleanupTestCase()
1381 {
1382 #if !defined Q_OS_WIN
1383     QFile::remove(wronlyFileName);
1384 #endif
1385 #ifndef QT_NO_BEARERMANAGEMENT
1386     if (networkSession && networkSession->isOpen()) {
1387         networkSession->close();
1388     }
1389 #endif
1390 }
1391
1392 void tst_QNetworkReply::init()
1393 {
1394     cleanup();
1395 }
1396
1397 void tst_QNetworkReply::cleanup()
1398 {
1399     QFile file(testFileName);
1400     QVERIFY(!file.exists() || file.remove());
1401
1402     // clear the internal cache
1403     manager.clearAccessCache();
1404     manager.setProxy(QNetworkProxy());
1405     manager.setCache(0);
1406
1407     // clear cookies
1408     cookieJar->setAllCookies(QList<QNetworkCookie>());
1409
1410     // disconnect manager signals
1411     manager.disconnect(SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)));
1412     manager.disconnect(SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
1413     manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
1414 }
1415
1416 void tst_QNetworkReply::stateChecking()
1417 {
1418     QUrl url = QUrl("file:///");
1419     QNetworkRequest req(url);   // you can't open this file, I know
1420     QNetworkReplyPtr reply(manager.get(req));
1421
1422     QVERIFY(reply.data());
1423     QVERIFY(reply->isOpen());
1424     QVERIFY(reply->isReadable());
1425     QVERIFY(!reply->isWritable());
1426
1427     // both behaviours are OK since we might change underlying behaviour again
1428     if (!reply->isFinished())
1429         QCOMPARE(reply->errorString(), QString("Unknown error"));
1430     else
1431         QVERIFY(!reply->errorString().isEmpty());
1432
1433
1434     QCOMPARE(reply->manager(), &manager);
1435     QCOMPARE(reply->request(), req);
1436     QCOMPARE(int(reply->operation()), int(QNetworkAccessManager::GetOperation));
1437     // error and not error are OK since we might change underlying behaviour again
1438     if (!reply->isFinished())
1439         QCOMPARE(reply->error(), QNetworkReply::NoError);
1440     QCOMPARE(reply->url(), url);
1441
1442     reply->abort();
1443 }
1444
1445 void tst_QNetworkReply::invalidProtocol()
1446 {
1447     QUrl url = QUrl::fromEncoded("not-a-known-protocol://foo/bar");
1448     QNetworkRequest req(url);
1449     QNetworkReplyPtr reply;
1450
1451     QString errorMsg = "Request failed: Protocol \"not-a-known-protocol\" is unknown";
1452     QString result = runSimpleRequest(QNetworkAccessManager::GetOperation, req, reply);
1453     QCOMPARE(result, errorMsg);
1454
1455     QCOMPARE(reply->url(), url);
1456     QCOMPARE(reply->error(), QNetworkReply::ProtocolUnknownError);
1457 }
1458
1459 void tst_QNetworkReply::getFromData_data()
1460 {
1461     QTest::addColumn<QString>("request");
1462     QTest::addColumn<QByteArray>("expected");
1463     QTest::addColumn<QString>("mimeType");
1464
1465     const QString defaultMimeType("text/plain;charset=US-ASCII");
1466
1467     //QTest::newRow("empty") << "data:" << QByteArray() << defaultMimeType;
1468     QTest::newRow("empty2") << "data:," << QByteArray() << defaultMimeType;
1469     QTest::newRow("just-charset_1") << "data:charset=iso-8859-1,"
1470                                     << QByteArray() << "text/plain;charset=iso-8859-1";
1471     QTest::newRow("just-charset_2") << "data:charset = iso-8859-1 ,"
1472                                     << QByteArray() << "text/plain;charset = iso-8859-1";
1473     //QTest::newRow("just-media") << "data:text/xml" << QByteArray() << "text/xml";
1474     QTest::newRow("just-media2") << "data:text/xml," << QByteArray() << "text/xml";
1475
1476     QTest::newRow("plain_1") << "data:,foo" << QByteArray("foo") << defaultMimeType;
1477     QTest::newRow("plain_2") << "data:text/html,Hello World" << QByteArray("Hello World")
1478                              << "text/html";
1479     QTest::newRow("plain_3") << "data:text/html;charset=utf-8,Hello World"
1480                              << QByteArray("Hello World") << "text/html;charset=utf-8";
1481
1482     QTest::newRow("pct_1") << "data:,%3Cbody%20contentEditable%3Dtrue%3E%0D%0A"
1483                            << QByteArray("<body contentEditable=true>\r\n") << defaultMimeType;
1484     QTest::newRow("pct_2") << "data:text/html;charset=utf-8,%3Cbody%20contentEditable%3Dtrue%3E%0D%0A"
1485                            << QByteArray("<body contentEditable=true>\r\n")
1486                            << "text/html;charset=utf-8";
1487
1488     QTest::newRow("base64-empty_1") << "data:;base64," << QByteArray() << defaultMimeType;
1489     QTest::newRow("base64-empty_2") << "data:charset=utf-8;base64," << QByteArray()
1490                                     << "text/plain;charset=utf-8";
1491     QTest::newRow("base64-empty_3") << "data:text/html;charset=utf-8;base64,"
1492                                     << QByteArray() << "text/html;charset=utf-8";
1493
1494     QTest::newRow("base64_1") << "data:;base64,UXQgaXMgZ3JlYXQh" << QByteArray("Qt is great!")
1495                               << defaultMimeType;
1496     QTest::newRow("base64_2") << "data:charset=utf-8;base64,UXQgaXMgZ3JlYXQh"
1497                               << QByteArray("Qt is great!") << "text/plain;charset=utf-8";
1498     QTest::newRow("base64_3") << "data:text/html;charset=utf-8;base64,UXQgaXMgZ3JlYXQh"
1499                               << QByteArray("Qt is great!") << "text/html;charset=utf-8";
1500
1501     QTest::newRow("pct-nul") << "data:,a%00g" << QByteArray("a\0g", 3) << defaultMimeType;
1502     QTest::newRow("base64-nul") << "data:;base64,YQBn" << QByteArray("a\0g", 3) << defaultMimeType;
1503     QTest::newRow("pct-nonutf8") << "data:,a%E1g" << QByteArray("a\xE1g", 3) << defaultMimeType;
1504
1505     QTest::newRow("base64")
1506         << QString::fromLatin1("data:application/xml;base64,PGUvPg==")
1507         << QByteArray("<e/>")
1508         << "application/xml";
1509
1510     QTest::newRow("base64, no media type")
1511         << QString::fromLatin1("data:;base64,PGUvPg==")
1512         << QByteArray("<e/>")
1513         << defaultMimeType;
1514
1515     QTest::newRow("Percent encoding")
1516         << QString::fromLatin1("data:application/xml,%3Ce%2F%3E")
1517         << QByteArray("<e/>")
1518         << "application/xml";
1519
1520     QTest::newRow("Percent encoding, no media type")
1521         << QString::fromLatin1("data:,%3Ce%2F%3E")
1522         << QByteArray("<e/>")
1523         << defaultMimeType;
1524
1525     QTest::newRow("querychars")
1526         << QString::fromLatin1("data:,foo?x=0&y=0")
1527         << QByteArray("foo?x=0&y=0")
1528         << defaultMimeType;
1529
1530     QTest::newRow("css") << "data:text/css,div%20{%20border-right:%20solid;%20}"
1531                          << QByteArray("div { border-right: solid; }")
1532                          << "text/css";
1533 }
1534
1535 void tst_QNetworkReply::getFromData()
1536 {
1537     QFETCH(QString, request);
1538     QFETCH(QByteArray, expected);
1539     QFETCH(QString, mimeType);
1540
1541     QUrl url = QUrl::fromEncoded(request.toLatin1());
1542     QNetworkRequest req(url);
1543     QNetworkReplyPtr reply;
1544
1545     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, req, reply));
1546
1547     QCOMPARE(reply->url(), url);
1548     QCOMPARE(reply->error(), QNetworkReply::NoError);
1549
1550     QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader).toString(), mimeType);
1551     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(expected.size()));
1552     QCOMPARE(reply->readAll(), expected);
1553 }
1554
1555 void tst_QNetworkReply::getFromFile()
1556 {
1557     // create the file:
1558     QTemporaryFile file(QDir::currentPath() + "/temp-XXXXXX");
1559     file.setAutoRemove(true);
1560     QVERIFY(file.open());
1561
1562     QNetworkRequest request(QUrl::fromLocalFile(file.fileName()));
1563     QNetworkReplyPtr reply;
1564
1565     static const char fileData[] = "This is some data that is in the file.\r\n";
1566     QByteArray data = QByteArray::fromRawData(fileData, sizeof fileData - 1);
1567     QVERIFY(file.write(data) == data.size());
1568     file.flush();
1569     QCOMPARE(file.size(), qint64(data.size()));
1570
1571     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
1572
1573     QCOMPARE(reply->url(), request.url());
1574     QCOMPARE(reply->error(), QNetworkReply::NoError);
1575
1576     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), file.size());
1577     QCOMPARE(reply->readAll(), data);
1578
1579     // make the file bigger
1580     file.resize(0);
1581     const int multiply = (128 * 1024) / (sizeof fileData - 1);
1582     for (int i = 0; i < multiply; ++i)
1583         file.write(fileData, sizeof fileData - 1);
1584     file.flush();
1585
1586     // run again
1587     reply.clear();
1588
1589     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
1590     QCOMPARE(reply->url(), request.url());
1591     QCOMPARE(reply->error(), QNetworkReply::NoError);
1592
1593     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), file.size());
1594     QCOMPARE(qint64(reply->readAll().size()), file.size());
1595 }
1596
1597 void tst_QNetworkReply::getFromFileSpecial_data()
1598 {
1599     QTest::addColumn<QString>("fileName");
1600     QTest::addColumn<QString>("url");
1601
1602     QTest::newRow("resource") << ":/resource" <<  "qrc:/resource";
1603     QTest::newRow("search-path") << "testdata:/rfc3252.txt" << "testdata:/rfc3252.txt";
1604     QTest::newRow("bigfile-path") << "testdata:/bigfile" << "testdata:/bigfile";
1605 #ifdef Q_OS_WIN
1606     QTest::newRow("smb-path") << "testdata:/smb-file.txt" << "file://" + QtNetworkSettings::winServerName() + "/testshare/test.pri";
1607 #endif
1608 }
1609
1610 void tst_QNetworkReply::getFromFileSpecial()
1611 {
1612     QFETCH(QString, fileName);
1613     QFETCH(QString, url);
1614
1615     // open the resource so we can find out its size
1616     QFile resource(fileName);
1617     QVERIFY(resource.open(QIODevice::ReadOnly));
1618
1619     QNetworkRequest request;
1620     QNetworkReplyPtr reply;
1621     request.setUrl(url);
1622     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
1623
1624     QCOMPARE(reply->url(), request.url());
1625     QCOMPARE(reply->error(), QNetworkReply::NoError);
1626
1627     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), resource.size());
1628     QCOMPARE(reply->readAll(), resource.readAll());
1629 }
1630
1631 void tst_QNetworkReply::getFromFtp_data()
1632 {
1633     QTest::addColumn<QString>("referenceName");
1634     QTest::addColumn<QString>("url");
1635
1636     QTest::newRow("rfc3252.txt") << (testDataDir + "/rfc3252.txt") << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt";
1637     QTest::newRow("bigfile") << (testDataDir + "/bigfile") << "ftp://" + QtNetworkSettings::serverName() + "/qtest/bigfile";
1638 }
1639
1640 void tst_QNetworkReply::getFromFtp()
1641 {
1642     QFETCH(QString, referenceName);
1643     QFETCH(QString, url);
1644
1645     QFile reference(referenceName);
1646     QVERIFY(reference.open(QIODevice::ReadOnly));
1647
1648     QNetworkRequest request(url);
1649     QNetworkReplyPtr reply;
1650     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
1651
1652     QCOMPARE(reply->url(), request.url());
1653     QCOMPARE(reply->error(), QNetworkReply::NoError);
1654
1655     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
1656     QCOMPARE(reply->readAll(), reference.readAll());
1657 }
1658
1659 void tst_QNetworkReply::getFromHttp_data()
1660 {
1661     QTest::addColumn<QString>("referenceName");
1662     QTest::addColumn<QString>("url");
1663
1664     QTest::newRow("success-internal") << (testDataDir + "/rfc3252.txt") << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt";
1665     QTest::newRow("success-external") << (testDataDir + "/rfc3252.txt") << "http://www.ietf.org/rfc/rfc3252.txt";
1666     QTest::newRow("bigfile-internal") << (testDataDir + "/bigfile") << "http://" + QtNetworkSettings::serverName() + "/qtest/bigfile";
1667 }
1668
1669 void tst_QNetworkReply::getFromHttp()
1670 {
1671     QFETCH(QString, referenceName);
1672     QFETCH(QString, url);
1673
1674     QFile reference(referenceName);
1675     QVERIFY(reference.open(QIODevice::ReadOnly));
1676
1677     QNetworkRequest request(url);
1678     QNetworkReplyPtr reply;
1679     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
1680
1681     QCOMPARE(reply->url(), request.url());
1682     QCOMPARE(reply->error(), QNetworkReply::NoError);
1683     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
1684     QCOMPARE(reply->size(), reference.size());
1685     // only compare when the header is set.
1686     if (reply->header(QNetworkRequest::ContentLengthHeader).isValid())
1687         QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
1688
1689     // We know our internal server is apache..
1690     if (qstrcmp(QTest::currentDataTag(), "success-internal") == 0)
1691         QVERIFY(reply->header(QNetworkRequest::ServerHeader).toString().contains("Apache"));
1692
1693     QCOMPARE(reply->readAll(), reference.readAll());
1694 }
1695
1696 void tst_QNetworkReply::headFromHttp_data()
1697 {
1698     QTest::addColumn<qint64>("referenceSize");
1699     QTest::addColumn<QUrl>("url");
1700     QTest::addColumn<QString>("contentType");
1701     QTest::addColumn<QNetworkProxy>("proxy");
1702
1703     qint64 rfcsize = QFileInfo(testDataDir + "/rfc3252.txt").size();
1704     qint64 bigfilesize = QFileInfo(testDataDir + "/bigfile").size();
1705     qint64 indexsize = QFileInfo(testDataDir + "/index.html").size();
1706
1707     //testing proxies, mainly for the 407 response from http proxy
1708     for (int i = 0; i < proxies.count(); ++i) {
1709         QTest::newRow("rfc" + proxies.at(i).tag) << rfcsize << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt") << "text/plain" << proxies.at(i).proxy;
1710         QTest::newRow("bigfile" + proxies.at(i).tag) << bigfilesize << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile") << "text/plain" << proxies.at(i).proxy;
1711         QTest::newRow("index" + proxies.at(i).tag) << indexsize << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/") << "text/html" << proxies.at(i).proxy;
1712         QTest::newRow("with-authentication" + proxies.at(i).tag) << rfcsize << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt") << "text/plain" << proxies.at(i).proxy;
1713         QTest::newRow("cgi" + proxies.at(i).tag) << (qint64)-1 << QUrl("http://qt-test-server/qtest/cgi-bin/httpcachetest_expires500.cgi") << "text/html" << proxies.at(i).proxy;
1714     }
1715 }
1716
1717 void tst_QNetworkReply::headFromHttp()
1718 {
1719     QFETCH(qint64, referenceSize);
1720     QFETCH(QUrl, url);
1721     QFETCH(QString, contentType);
1722     QFETCH(QNetworkProxy, proxy);
1723
1724     QNetworkRequest request(url);
1725     QNetworkReplyPtr reply;
1726
1727     QElapsedTimer time;
1728     time.start();
1729
1730     manager.setProxy(proxy);
1731     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
1732             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
1733     connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
1734             SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
1735
1736     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::HeadOperation, request, reply));
1737
1738     manager.disconnect(SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
1739                this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
1740     manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
1741                this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
1742
1743     QVERIFY(time.elapsed() < 8000); //check authentication didn't wait for the server to timeout the http connection (15s on qt test server)
1744
1745     QCOMPARE(reply->url(), request.url());
1746     QCOMPARE(reply->error(), QNetworkReply::NoError);
1747     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
1748     // only compare when the header is set.
1749     if (reply->header(QNetworkRequest::ContentLengthHeader).isValid() && referenceSize >= 0)
1750         QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), referenceSize);
1751     if (reply->header(QNetworkRequest::ContentTypeHeader).isValid())
1752         QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader).toString(), contentType);
1753 }
1754
1755 void tst_QNetworkReply::getErrors_data()
1756 {
1757     QTest::addColumn<QString>("url");
1758     QTest::addColumn<int>("error");
1759     QTest::addColumn<int>("httpStatusCode");
1760     QTest::addColumn<bool>("dataIsEmpty");
1761
1762     // empties
1763     QTest::newRow("empty-url") << QString() << int(QNetworkReply::ProtocolUnknownError) << 0 << true;
1764     QTest::newRow("empty-scheme-host") << (testDataDir + "/rfc3252.txt") << int(QNetworkReply::ProtocolUnknownError) << 0 << true;
1765     QTest::newRow("empty-scheme") << "//" + QtNetworkSettings::winServerName() + "/testshare/test.pri"
1766             << int(QNetworkReply::ProtocolUnknownError) << 0 << true;
1767
1768     // file: errors
1769     QTest::newRow("file-host") << "file://this-host-doesnt-exist.troll.no/foo.txt"
1770 #if !defined Q_OS_WIN
1771                                << int(QNetworkReply::ProtocolInvalidOperationError) << 0 << true;
1772 #else
1773                                << int(QNetworkReply::ContentNotFoundError) << 0 << true;
1774 #endif
1775     QTest::newRow("file-no-path") << "file://localhost"
1776                                   << int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true;
1777     QTest::newRow("file-is-dir") << QUrl::fromLocalFile(QDir::currentPath()).toString()
1778                                  << int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true;
1779     QTest::newRow("file-exist") << QUrl::fromLocalFile(QDir::currentPath() + "/this-file-doesnt-exist.txt").toString()
1780                                 << int(QNetworkReply::ContentNotFoundError) << 0 << true;
1781 #if !defined Q_OS_WIN
1782     QTest::newRow("file-is-wronly") << QUrl::fromLocalFile(wronlyFileName).toString()
1783                                     << int(QNetworkReply::ContentAccessDenied) << 0 << true;
1784 #endif
1785     if (QFile::exists("/etc/shadow"))
1786         QTest::newRow("file-permissions") << "file:/etc/shadow"
1787                                           << int(QNetworkReply::ContentAccessDenied) << 0 << true;
1788
1789     // ftp: errors
1790     QTest::newRow("ftp-host") << "ftp://this-host-doesnt-exist.troll.no/foo.txt"
1791                               << int(QNetworkReply::HostNotFoundError) << 0 << true;
1792     QTest::newRow("ftp-no-path") << "ftp://" + QtNetworkSettings::serverName()
1793                                  << int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true;
1794     QTest::newRow("ftp-is-dir") << "ftp://" + QtNetworkSettings::serverName() + "/qtest"
1795                                 << int(QNetworkReply::ContentOperationNotPermittedError) << 0 << true;
1796     QTest::newRow("ftp-dir-not-readable") << "ftp://" + QtNetworkSettings::serverName() + "/pub/dir-not-readable/foo.txt"
1797                                           << int(QNetworkReply::ContentAccessDenied) << 0 << true;
1798     QTest::newRow("ftp-file-not-readable") << "ftp://" + QtNetworkSettings::serverName() + "/pub/file-not-readable.txt"
1799                                            << int(QNetworkReply::ContentAccessDenied) << 0 << true;
1800     QTest::newRow("ftp-exist") << "ftp://" + QtNetworkSettings::serverName() + "/pub/this-file-doesnt-exist.txt"
1801                                << int(QNetworkReply::ContentNotFoundError) << 0 << true;
1802
1803     // http: errors
1804     QTest::newRow("http-host") << "http://this-host-will-never-exist.troll.no/"
1805                                << int(QNetworkReply::HostNotFoundError) << 0 << true;
1806     QTest::newRow("http-exist") << "http://" + QtNetworkSettings::serverName() + "/this-file-doesnt-exist.txt"
1807                                 << int(QNetworkReply::ContentNotFoundError) << 404 << false;
1808     QTest::newRow("http-authentication") << "http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth"
1809                                          << int(QNetworkReply::AuthenticationRequiredError) << 401 << false;
1810 }
1811
1812 void tst_QNetworkReply::getErrors()
1813 {
1814     QFETCH(QString, url);
1815     QNetworkRequest request(url);
1816
1817 #ifdef Q_OS_UNIX
1818     if ((qstrcmp(QTest::currentDataTag(), "file-is-wronly") == 0) ||
1819         (qstrcmp(QTest::currentDataTag(), "file-permissions") == 0)) {
1820         if (::getuid() == 0)
1821             QSKIP("Running this test as root doesn't make sense");
1822     }
1823 #endif
1824
1825     QNetworkReplyPtr reply(manager.get(request));
1826     reply->setParent(this);     // we have expect-fails
1827
1828     if (!reply->isFinished())
1829         QCOMPARE(reply->error(), QNetworkReply::NoError);
1830
1831     // now run the request:
1832     QVERIFY(waitForFinish(reply) != Timeout);
1833
1834     QFETCH(int, error);
1835     QEXPECT_FAIL("ftp-is-dir", "QFtp cannot provide enough detail", Abort);
1836     // the line below is not necessary
1837     QEXPECT_FAIL("ftp-dir-not-readable", "QFtp cannot provide enough detail", Abort);
1838     QCOMPARE(reply->error(), QNetworkReply::NetworkError(error));
1839
1840     QTEST(reply->readAll().isEmpty(), "dataIsEmpty");
1841
1842     QVERIFY(reply->isFinished());
1843     QVERIFY(!reply->isRunning());
1844
1845     QFETCH(int, httpStatusCode);
1846     if (httpStatusCode != 0) {
1847         QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), httpStatusCode);
1848     }
1849 }
1850
1851 static inline QByteArray md5sum(const QByteArray &data)
1852 {
1853     return QCryptographicHash::hash(data, QCryptographicHash::Md5);
1854 }
1855
1856 void tst_QNetworkReply::putToFile_data()
1857 {
1858     QTest::addColumn<QByteArray>("data");
1859     QTest::addColumn<QByteArray>("md5sum");
1860
1861     QByteArray data;
1862     data = "";
1863     QTest::newRow("empty") << data << md5sum(data);
1864
1865     data = "This is a normal message.";
1866     QTest::newRow("generic") << data << md5sum(data);
1867
1868     data = "This is a message to show that Qt rocks!\r\n\n";
1869     QTest::newRow("small") << data << md5sum(data);
1870
1871     data = QByteArray("abcd\0\1\2\abcd",12);
1872     QTest::newRow("with-nul") << data << md5sum(data);
1873
1874     data = QByteArray(4097, '\4');
1875     QTest::newRow("4k+1") << data << md5sum(data);
1876
1877     data = QByteArray(128*1024+1, '\177');
1878     QTest::newRow("128k+1") << data << md5sum(data);
1879
1880     data = QByteArray(2*1024*1024+1, '\177');
1881     QTest::newRow("2MB+1") << data << md5sum(data);
1882 }
1883
1884 void tst_QNetworkReply::putToFile()
1885 {
1886     QFile file(testFileName);
1887
1888     QUrl url = QUrl::fromLocalFile(file.fileName());
1889     QNetworkRequest request(url);
1890     QNetworkReplyPtr reply;
1891
1892     QFETCH(QByteArray, data);
1893
1894     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data));
1895
1896     QCOMPARE(reply->url(), url);
1897     QCOMPARE(reply->error(), QNetworkReply::NoError);
1898     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
1899     QVERIFY(reply->readAll().isEmpty());
1900
1901     QVERIFY(file.open(QIODevice::ReadOnly));
1902     QCOMPARE(file.size(), qint64(data.size()));
1903     QByteArray contents = file.readAll();
1904     QCOMPARE(contents, data);
1905 }
1906
1907 void tst_QNetworkReply::putToFtp_data()
1908 {
1909     putToFile_data();
1910 }
1911
1912 void tst_QNetworkReply::putToFtp()
1913 {
1914     QUrl url("ftp://" + QtNetworkSettings::serverName());
1915     url.setPath(QString("/qtest/upload/qnetworkaccess-putToFtp-%1-%2")
1916                 .arg(QTest::currentDataTag())
1917                 .arg(uniqueExtension));
1918
1919     QNetworkRequest request(url);
1920     QNetworkReplyPtr reply;
1921
1922     QFETCH(QByteArray, data);
1923
1924     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data));
1925
1926     QCOMPARE(reply->url(), url);
1927     QCOMPARE(reply->error(), QNetworkReply::NoError);
1928     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
1929     QVERIFY(reply->readAll().isEmpty());
1930
1931     // download the file again from FTP to make sure it was uploaded
1932     // correctly
1933     QNetworkAccessManager qnam;
1934     QNetworkRequest req(url);
1935     QNetworkReply *r = qnam.get(req);
1936
1937     QObject::connect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
1938     int count = 0;
1939     QSignalSpy spy(r, SIGNAL(downloadProgress(qint64,qint64)));
1940     while (!r->isFinished()) {
1941         QTestEventLoop::instance().enterLoop(10);
1942         if (count == spy.count() && !r->isFinished())
1943             break;
1944         count = spy.count();
1945     }
1946     QObject::disconnect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
1947
1948     QByteArray uploaded = r->readAll();
1949     QCOMPARE(uploaded.size(), data.size());
1950     QCOMPARE(uploaded, data);
1951
1952     r->close();
1953     QObject::connect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
1954     QTestEventLoop::instance().enterLoop(10);
1955     QObject::disconnect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
1956 }
1957
1958 void tst_QNetworkReply::putToHttp_data()
1959 {
1960     putToFile_data();
1961 }
1962
1963 void tst_QNetworkReply::putToHttp()
1964 {
1965     QUrl url("http://" + QtNetworkSettings::serverName());
1966     url.setPath(QString("/dav/qnetworkaccess-putToHttp-%1-%2")
1967                 .arg(QTest::currentDataTag())
1968                 .arg(uniqueExtension));
1969
1970     QNetworkRequest request(url);
1971     QNetworkReplyPtr reply;
1972
1973     QFETCH(QByteArray, data);
1974
1975     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data));
1976
1977     QCOMPARE(reply->url(), url);
1978     QCOMPARE(reply->error(), QNetworkReply::NoError);
1979
1980     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 201); // 201 Created
1981
1982     // download the file again from HTTP to make sure it was uploaded
1983     // correctly. HTTP/0.9 is enough
1984     QTcpSocket socket;
1985     socket.connectToHost(QtNetworkSettings::serverName(), 80);
1986     socket.write("GET " + url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority) + "\r\n");
1987     if (!socket.waitForDisconnected(10000))
1988         QFAIL("Network timeout");
1989
1990     QByteArray uploadedData = socket.readAll();
1991     QCOMPARE(uploadedData, data);
1992 }
1993
1994 void tst_QNetworkReply::putToHttpSynchronous_data()
1995 {
1996     uniqueExtension = createUniqueExtension();
1997     putToFile_data();
1998 }
1999
2000 void tst_QNetworkReply::putToHttpSynchronous()
2001 {
2002     QUrl url("http://" + QtNetworkSettings::serverName());
2003     url.setPath(QString("/dav/qnetworkaccess-putToHttp-%1-%2")
2004                 .arg(QTest::currentDataTag())
2005                 .arg(uniqueExtension));
2006
2007     QNetworkRequest request(url);
2008     QNetworkReplyPtr reply;
2009
2010     QFETCH(QByteArray, data);
2011
2012     request.setAttribute(
2013             QNetworkRequest::SynchronousRequestAttribute,
2014             true);
2015
2016     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, request, reply, data));
2017
2018     QCOMPARE(reply->url(), url);
2019     QCOMPARE(reply->error(), QNetworkReply::NoError);
2020
2021     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 201); // 201 Created
2022
2023     // download the file again from HTTP to make sure it was uploaded
2024     // correctly. HTTP/0.9 is enough
2025     QTcpSocket socket;
2026     socket.connectToHost(QtNetworkSettings::serverName(), 80);
2027     socket.write("GET " + url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority) + "\r\n");
2028     if (!socket.waitForDisconnected(10000))
2029         QFAIL("Network timeout");
2030
2031     QByteArray uploadedData = socket.readAll();
2032     QCOMPARE(uploadedData, data);
2033 }
2034
2035 void tst_QNetworkReply::postToHttp_data()
2036 {
2037     putToFile_data();
2038 }
2039
2040 void tst_QNetworkReply::postToHttp()
2041 {
2042     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi");
2043
2044     QNetworkRequest request(url);
2045     request.setRawHeader("Content-Type", "application/octet-stream");
2046     QNetworkReplyPtr reply;
2047
2048     QFETCH(QByteArray, data);
2049
2050     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PostOperation, request, reply, data));
2051
2052     QCOMPARE(reply->url(), url);
2053     QCOMPARE(reply->error(), QNetworkReply::NoError);
2054
2055     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
2056
2057     QFETCH(QByteArray, md5sum);
2058     QByteArray uploadedData = reply->readAll().trimmed();
2059     QCOMPARE(uploadedData, md5sum.toHex());
2060 }
2061
2062 void tst_QNetworkReply::postToHttpSynchronous_data()
2063 {
2064     putToFile_data();
2065 }
2066
2067 void tst_QNetworkReply::postToHttpSynchronous()
2068 {
2069     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi");
2070
2071     QNetworkRequest request(url);
2072     request.setRawHeader("Content-Type", "application/octet-stream");
2073
2074     request.setAttribute(
2075             QNetworkRequest::SynchronousRequestAttribute,
2076             true);
2077
2078     QNetworkReplyPtr reply;
2079
2080     QFETCH(QByteArray, data);
2081
2082     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PostOperation, request, reply, data));
2083
2084     QCOMPARE(reply->url(), url);
2085     QCOMPARE(reply->error(), QNetworkReply::NoError);
2086
2087     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
2088
2089     QFETCH(QByteArray, md5sum);
2090     QByteArray uploadedData = reply->readAll().trimmed();
2091     QCOMPARE(uploadedData, md5sum.toHex());
2092 }
2093
2094 void tst_QNetworkReply::postToHttpMultipart_data()
2095 {
2096     QTest::addColumn<QUrl>("url");
2097     QTest::addColumn<QHttpMultiPart *>("multiPart");
2098     QTest::addColumn<QByteArray>("expectedReplyData");
2099     QTest::addColumn<QByteArray>("contentType");
2100
2101     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/multipart.cgi");
2102     QByteArray expectedData;
2103
2104
2105     // empty parts
2106
2107     QHttpMultiPart *emptyMultiPart = new QHttpMultiPart;
2108     QTest::newRow("empty") << url << emptyMultiPart << expectedData << QByteArray("mixed");
2109
2110     QHttpMultiPart *emptyRelatedMultiPart = new QHttpMultiPart;
2111     emptyRelatedMultiPart->setContentType(QHttpMultiPart::RelatedType);
2112     QTest::newRow("empty-related") << url << emptyRelatedMultiPart << expectedData << QByteArray("related");
2113
2114     QHttpMultiPart *emptyAlternativeMultiPart = new QHttpMultiPart;
2115     emptyAlternativeMultiPart->setContentType(QHttpMultiPart::AlternativeType);
2116     QTest::newRow("empty-alternative") << url << emptyAlternativeMultiPart << expectedData << QByteArray("alternative");
2117
2118
2119     // text-only parts
2120
2121     QHttpPart textPart;
2122     textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
2123     textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
2124     textPart.setBody("7 bytes");
2125     QHttpMultiPart *multiPart1 = new QHttpMultiPart;
2126     multiPart1->setContentType(QHttpMultiPart::FormDataType);
2127     multiPart1->append(textPart);
2128     expectedData = "key: text, value: 7 bytes\n";
2129     QTest::newRow("text") << url << multiPart1 << expectedData << QByteArray("form-data");
2130
2131     QHttpMultiPart *customMultiPart = new QHttpMultiPart;
2132     customMultiPart->append(textPart);
2133     expectedData = "header: Content-Type, value: 'text/plain'\n"
2134                    "header: Content-Disposition, value: 'form-data; name=\"text\"'\n"
2135                    "content: 7 bytes\n"
2136                    "\n";
2137     QTest::newRow("text-custom") << url << customMultiPart << expectedData << QByteArray("custom");
2138
2139     QHttpPart textPart2;
2140     textPart2.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
2141     textPart2.setRawHeader("myRawHeader", "myValue");
2142     textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text2\""));
2143     textPart2.setBody("some more bytes");
2144     textPart2.setBodyDevice((QIODevice *) 1); // test whether setting and unsetting of the device works
2145     textPart2.setBodyDevice(0);
2146     QHttpMultiPart *multiPart2 = new QHttpMultiPart;
2147     multiPart2->setContentType(QHttpMultiPart::FormDataType);
2148     multiPart2->append(textPart);
2149     multiPart2->append(textPart2);
2150     expectedData = "key: text2, value: some more bytes\n"
2151                    "key: text, value: 7 bytes\n";
2152     QTest::newRow("text-text") << url << multiPart2 << expectedData << QByteArray("form-data");
2153
2154
2155     QHttpPart textPart3;
2156     textPart3.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
2157     textPart3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text3\""));
2158     textPart3.setRawHeader("Content-Location", "http://my.test.location.tld");
2159     textPart3.setBody("even more bytes");
2160     QHttpMultiPart *multiPart3 = new QHttpMultiPart;
2161     multiPart3->setContentType(QHttpMultiPart::AlternativeType);
2162     multiPart3->append(textPart);
2163     multiPart3->append(textPart2);
2164     multiPart3->append(textPart3);
2165     expectedData = "header: Content-Type, value: 'text/plain'\n"
2166                    "header: Content-Disposition, value: 'form-data; name=\"text\"'\n"
2167                    "content: 7 bytes\n"
2168                    "\n"
2169                    "header: Content-Type, value: 'text/plain'\n"
2170                    "header: myRawHeader, value: 'myValue'\n"
2171                    "header: Content-Disposition, value: 'form-data; name=\"text2\"'\n"
2172                    "content: some more bytes\n"
2173                    "\n"
2174                    "header: Content-Type, value: 'text/plain'\n"
2175                    "header: Content-Disposition, value: 'form-data; name=\"text3\"'\n"
2176                    "header: Content-Location, value: 'http://my.test.location.tld'\n"
2177                    "content: even more bytes\n\n";
2178     QTest::newRow("text-text-text") << url << multiPart3 << expectedData << QByteArray("alternative");
2179
2180
2181
2182     // text and image parts
2183
2184     QHttpPart imagePart11;
2185     imagePart11.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2186     imagePart11.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage\""));
2187     imagePart11.setRawHeader("Content-Location", "http://my.test.location.tld");
2188     imagePart11.setRawHeader("Content-ID", "my@id.tld");
2189     QFile *file11 = new QFile(testDataDir + "/image1.jpg");
2190     file11->open(QIODevice::ReadOnly);
2191     imagePart11.setBodyDevice(file11);
2192     QHttpMultiPart *imageMultiPart1 = new QHttpMultiPart(QHttpMultiPart::FormDataType);
2193     imageMultiPart1->append(imagePart11);
2194     file11->setParent(imageMultiPart1);
2195     expectedData = "key: testImage, value: 87ef3bb319b004ba9e5e9c9fa713776e\n"; // md5 sum of file
2196     QTest::newRow("image") << url << imageMultiPart1 << expectedData << QByteArray("form-data");
2197
2198     QHttpPart imagePart21;
2199     imagePart21.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2200     imagePart21.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage1\""));
2201     imagePart21.setRawHeader("Content-Location", "http://my.test.location.tld");
2202     imagePart21.setRawHeader("Content-ID", "my@id.tld");
2203     QFile *file21 = new QFile(testDataDir + "/image1.jpg");
2204     file21->open(QIODevice::ReadOnly);
2205     imagePart21.setBodyDevice(file21);
2206     QHttpMultiPart *imageMultiPart2 = new QHttpMultiPart();
2207     imageMultiPart2->setContentType(QHttpMultiPart::FormDataType);
2208     imageMultiPart2->append(textPart);
2209     imageMultiPart2->append(imagePart21);
2210     file21->setParent(imageMultiPart2);
2211     QHttpPart imagePart22;
2212     imagePart22.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2213     imagePart22.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage2\""));
2214     QFile *file22 = new QFile(testDataDir + "/image2.jpg");
2215     file22->open(QIODevice::ReadOnly);
2216     imagePart22.setBodyDevice(file22);
2217     imageMultiPart2->append(imagePart22);
2218     file22->setParent(imageMultiPart2);
2219     expectedData = "key: testImage1, value: 87ef3bb319b004ba9e5e9c9fa713776e\n"
2220                    "key: text, value: 7 bytes\n"
2221                    "key: testImage2, value: 483761b893f7fb1bd2414344cd1f3dfb\n";
2222     QTest::newRow("text-image-image") << url << imageMultiPart2 << expectedData << QByteArray("form-data");
2223
2224
2225     QHttpPart imagePart31;
2226     imagePart31.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2227     imagePart31.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage1\""));
2228     imagePart31.setRawHeader("Content-Location", "http://my.test.location.tld");
2229     imagePart31.setRawHeader("Content-ID", "my@id.tld");
2230     QFile *file31 = new QFile(testDataDir + "/image1.jpg");
2231     file31->open(QIODevice::ReadOnly);
2232     imagePart31.setBodyDevice(file31);
2233     QHttpMultiPart *imageMultiPart3 = new QHttpMultiPart(QHttpMultiPart::FormDataType);
2234     imageMultiPart3->append(imagePart31);
2235     file31->setParent(imageMultiPart3);
2236     QHttpPart imagePart32;
2237     imagePart32.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2238     imagePart32.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage2\""));
2239     QFile *file32 = new QFile(testDataDir + "/image2.jpg");
2240     file32->open(QIODevice::ReadOnly);
2241     imagePart32.setBodyDevice(file31); // check that resetting works
2242     imagePart32.setBodyDevice(file32);
2243     imageMultiPart3->append(imagePart32);
2244     file32->setParent(imageMultiPart3);
2245     QHttpPart imagePart33;
2246     imagePart33.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2247     imagePart33.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage3\""));
2248     QFile *file33 = new QFile(testDataDir + "/image3.jpg");
2249     file33->open(QIODevice::ReadOnly);
2250     imagePart33.setBodyDevice(file33);
2251     imageMultiPart3->append(imagePart33);
2252     file33->setParent(imageMultiPart3);
2253     expectedData = "key: testImage1, value: 87ef3bb319b004ba9e5e9c9fa713776e\n"
2254                    "key: testImage2, value: 483761b893f7fb1bd2414344cd1f3dfb\n"
2255                    "key: testImage3, value: ab0eb6fd4fcf8b4436254870b4513033\n";
2256     QTest::newRow("3-images") << url << imageMultiPart3 << expectedData << QByteArray("form-data");
2257
2258
2259     // note: nesting multiparts is not working currently; for that, the outputDevice would need to be public
2260
2261 //    QHttpPart imagePart41;
2262 //    imagePart41.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2263 //    QFile *file41 = new QFile(testDataDir + "/image1.jpg");
2264 //    file41->open(QIODevice::ReadOnly);
2265 //    imagePart41.setBodyDevice(file41);
2266 //
2267 //    QHttpMultiPart *innerMultiPart = new QHttpMultiPart();
2268 //    innerMultiPart->setContentType(QHttpMultiPart::FormDataType);
2269 //    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant());
2270 //    innerMultiPart->append(textPart);
2271 //    innerMultiPart->append(imagePart41);
2272 //    textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant());
2273 //    innerMultiPart->append(textPart2);
2274 //
2275 //    QHttpPart nestedPart;
2276 //    nestedPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"nestedMessage"));
2277 //    nestedPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/alternative; boundary=\"" + innerMultiPart->boundary() + "\""));
2278 //    innerMultiPart->outputDevice()->open(QIODevice::ReadOnly);
2279 //    nestedPart.setBodyDevice(innerMultiPart->outputDevice());
2280 //
2281 //    QHttpMultiPart *outerMultiPart = new QHttpMultiPart;
2282 //    outerMultiPart->setContentType(QHttpMultiPart::FormDataType);
2283 //    outerMultiPart->append(textPart);
2284 //    outerMultiPart->append(nestedPart);
2285 //    outerMultiPart->append(textPart2);
2286 //    expectedData = "nothing"; // the CGI.pm module running on the test server does not understand nested multiparts
2287 //    openFiles.clear();
2288 //    openFiles << file41;
2289 //    QTest::newRow("nested") << url << outerMultiPart << expectedData << openFiles;
2290
2291
2292     // test setting large chunks of content with a byte array instead of a device (DISCOURAGED because of high memory consumption,
2293     // but we need to test that the behavior is correct)
2294     QHttpPart imagePart51;
2295     imagePart51.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
2296     imagePart51.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"testImage\""));
2297     QFile *file51 = new QFile(testDataDir + "/image1.jpg");
2298     file51->open(QIODevice::ReadOnly);
2299     QByteArray imageData = file51->readAll();
2300     file51->close();
2301     delete file51;
2302     imagePart51.setBody("7 bytes"); // check that resetting works
2303     imagePart51.setBody(imageData);
2304     QHttpMultiPart *imageMultiPart5 = new QHttpMultiPart;
2305     imageMultiPart5->setContentType(QHttpMultiPart::FormDataType);
2306     imageMultiPart5->append(imagePart51);
2307     expectedData = "key: testImage, value: 87ef3bb319b004ba9e5e9c9fa713776e\n"; // md5 sum of file
2308     QTest::newRow("image-as-content") << url << imageMultiPart5 << expectedData << QByteArray("form-data");
2309 }
2310
2311 void tst_QNetworkReply::postToHttpMultipart()
2312 {
2313     QFETCH(QUrl, url);
2314
2315     static QSet<QByteArray> boundaries;
2316
2317     QNetworkRequest request(url);
2318     QNetworkReplyPtr reply;
2319
2320     QFETCH(QHttpMultiPart *, multiPart);
2321     QFETCH(QByteArray, expectedReplyData);
2322     QFETCH(QByteArray, contentType);
2323
2324     // hack for testing the setting of the content-type header by hand:
2325     if (contentType == "custom") {
2326         QByteArray contentType("multipart/custom; boundary=\"" + multiPart->boundary() + "\"");
2327         request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
2328     }
2329
2330     QVERIFY2(! boundaries.contains(multiPart->boundary()), "boundary '" + multiPart->boundary() + "' has been created twice");
2331     boundaries.insert(multiPart->boundary());
2332
2333     RUN_REQUEST(runMultipartRequest(request, reply, multiPart, "POST"));
2334     multiPart->deleteLater();
2335
2336     QCOMPARE(reply->url(), url);
2337     QCOMPARE(reply->error(), QNetworkReply::NoError);
2338
2339     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
2340
2341     QVERIFY(multiPart->boundary().count() > 20); // check that there is randomness after the "boundary_.oOo._" string
2342     QVERIFY(multiPart->boundary().count() < 70);
2343     QByteArray replyData = reply->readAll();
2344
2345     expectedReplyData.prepend("content type: multipart/" + contentType + "; boundary=\"" + multiPart->boundary() + "\"\n");
2346 //    QEXPECT_FAIL("nested", "the server does not understand nested multipart messages", Continue); // see above
2347     QCOMPARE(replyData, expectedReplyData);
2348 }
2349
2350 void tst_QNetworkReply::putToHttpMultipart_data()
2351 {
2352     postToHttpMultipart_data();
2353 }
2354
2355 void tst_QNetworkReply::putToHttpMultipart()
2356 {
2357     QSKIP("test server script cannot handle PUT data yet");
2358     QFETCH(QUrl, url);
2359
2360     static QSet<QByteArray> boundaries;
2361
2362     QNetworkRequest request(url);
2363     QNetworkReplyPtr reply;
2364
2365     QFETCH(QHttpMultiPart *, multiPart);
2366     QFETCH(QByteArray, expectedReplyData);
2367     QFETCH(QByteArray, contentType);
2368
2369     // hack for testing the setting of the content-type header by hand:
2370     if (contentType == "custom") {
2371         QByteArray contentType("multipart/custom; boundary=\"" + multiPart->boundary() + "\"");
2372         request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
2373     }
2374
2375     QVERIFY2(! boundaries.contains(multiPart->boundary()), "boundary '" + multiPart->boundary() + "' has been created twice");
2376     boundaries.insert(multiPart->boundary());
2377
2378     RUN_REQUEST(runMultipartRequest(request, reply, multiPart, "PUT"));
2379     multiPart->deleteLater();
2380
2381     QCOMPARE(reply->url(), url);
2382     QCOMPARE(reply->error(), QNetworkReply::NoError);
2383
2384     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
2385
2386     QVERIFY(multiPart->boundary().count() > 20); // check that there is randomness after the "boundary_.oOo._" string
2387     QVERIFY(multiPart->boundary().count() < 70);
2388     QByteArray replyData = reply->readAll();
2389
2390     expectedReplyData.prepend("content type: multipart/" + contentType + "; boundary=\"" + multiPart->boundary() + "\"\n");
2391 //    QEXPECT_FAIL("nested", "the server does not understand nested multipart messages", Continue); // see above
2392     QCOMPARE(replyData, expectedReplyData);
2393 }
2394
2395 void tst_QNetworkReply::deleteFromHttp_data()
2396 {
2397     QTest::addColumn<QUrl>("url");
2398     QTest::addColumn<int>("resultCode");
2399     QTest::addColumn<QNetworkReply::NetworkError>("error");
2400
2401     // for status codes to expect, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
2402
2403     QTest::newRow("405-method-not-allowed") << QUrl("http://" + QtNetworkSettings::serverName() + "/index.html") << 405 << QNetworkReply::ContentOperationNotPermittedError;
2404     QTest::newRow("200-ok") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/http-delete.cgi?200-ok") << 200 << QNetworkReply::NoError;
2405     QTest::newRow("202-accepted") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/http-delete.cgi?202-accepted") << 202 << QNetworkReply::NoError;
2406     QTest::newRow("204-no-content") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/http-delete.cgi?204-no-content") << 204 << QNetworkReply::NoError;
2407     QTest::newRow("404-not-found") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/http-delete.cgi?404-not-found") << 404 << QNetworkReply::ContentNotFoundError;
2408 }
2409
2410 void tst_QNetworkReply::deleteFromHttp()
2411 {
2412     QFETCH(QUrl, url);
2413     QFETCH(int, resultCode);
2414     QFETCH(QNetworkReply::NetworkError, error);
2415     QNetworkRequest request(url);
2416     QNetworkReplyPtr reply;
2417     runSimpleRequest(QNetworkAccessManager::DeleteOperation, request, reply, 0);
2418     QCOMPARE(reply->url(), url);
2419     QCOMPARE(reply->error(), error);
2420     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), resultCode);
2421 }
2422
2423 void tst_QNetworkReply::putGetDeleteGetFromHttp_data()
2424 {
2425     QTest::addColumn<QUrl>("putUrl");
2426     QTest::addColumn<int>("putResultCode");
2427     QTest::addColumn<QNetworkReply::NetworkError>("putError");
2428     QTest::addColumn<QUrl>("deleteUrl");
2429     QTest::addColumn<int>("deleteResultCode");
2430     QTest::addColumn<QNetworkReply::NetworkError>("deleteError");
2431     QTest::addColumn<QUrl>("get2Url");
2432     QTest::addColumn<int>("get2ResultCode");
2433     QTest::addColumn<QNetworkReply::NetworkError>("get2Error");
2434
2435     QUrl url("http://" + QtNetworkSettings::serverName());
2436     url.setPath(QString("/dav/qnetworkaccess-putToHttp-%1-%2")
2437                 .arg(QTest::currentDataTag())
2438                 .arg(uniqueExtension));
2439
2440     // first use case: put, get (to check it is there), delete, get (to check it is not there anymore)
2441     QTest::newRow("success") << url << 201 << QNetworkReply::NoError << url << 204 << QNetworkReply::NoError << url << 404 << QNetworkReply::ContentNotFoundError;
2442
2443     QUrl wrongUrl("http://" + QtNetworkSettings::serverName());
2444     wrongUrl.setPath(QString("/dav/qnetworkaccess-thisURLisNotAvailable"));
2445
2446     // second use case: put, get (to check it is there), delete wrong URL, get (to check it is still there)
2447     QTest::newRow("delete-error") << url << 201 << QNetworkReply::NoError << wrongUrl << 404 << QNetworkReply::ContentNotFoundError << url << 200 << QNetworkReply::NoError;
2448
2449 }
2450
2451 void tst_QNetworkReply::putGetDeleteGetFromHttp()
2452 {
2453     QFETCH(QUrl, putUrl);
2454     QFETCH(int, putResultCode);
2455     QFETCH(QNetworkReply::NetworkError, putError);
2456     QFETCH(QUrl, deleteUrl);
2457     QFETCH(int, deleteResultCode);
2458     QFETCH(QNetworkReply::NetworkError, deleteError);
2459     QFETCH(QUrl, get2Url);
2460     QFETCH(int, get2ResultCode);
2461     QFETCH(QNetworkReply::NetworkError, get2Error);
2462
2463     QNetworkRequest putRequest(putUrl);
2464     QNetworkRequest deleteRequest(deleteUrl);
2465     QNetworkRequest get2Request(get2Url);
2466     QNetworkReplyPtr reply;
2467
2468     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, putRequest, reply, 0));
2469     QCOMPARE(reply->error(), putError);
2470     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), putResultCode);
2471
2472     runSimpleRequest(QNetworkAccessManager::GetOperation, putRequest, reply, 0);
2473     QCOMPARE(reply->error(), QNetworkReply::NoError);
2474     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2475
2476     runSimpleRequest(QNetworkAccessManager::DeleteOperation, deleteRequest, reply, 0);
2477     QCOMPARE(reply->error(), deleteError);
2478     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), deleteResultCode);
2479
2480     runSimpleRequest(QNetworkAccessManager::GetOperation, get2Request, reply, 0);
2481     QCOMPARE(reply->error(), get2Error);
2482     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), get2ResultCode);
2483
2484 }
2485
2486 void tst_QNetworkReply::connectToIPv6Address_data()
2487 {
2488     QTest::addColumn<QUrl>("url");
2489     QTest::addColumn<QNetworkReply::NetworkError>("error");
2490     QTest::addColumn<QByteArray>("dataToSend");
2491     QTest::addColumn<QByteArray>("hostfield");
2492     QTest::newRow("localhost") << QUrl(QByteArray("http://[::1]")) << QNetworkReply::NoError<< QByteArray("localhost") << QByteArray("[::1]");
2493     //QTest::newRow("ipv4localhost") << QUrl(QByteArray("http://127.0.0.1")) << QNetworkReply::NoError<< QByteArray("ipv4localhost") << QByteArray("127.0.0.1");
2494     //to add more test data here
2495 }
2496
2497 void tst_QNetworkReply::connectToIPv6Address()
2498 {
2499     QFETCH(QUrl, url);
2500     QFETCH(QNetworkReply::NetworkError, error);
2501     QFETCH(QByteArray, dataToSend);
2502     QFETCH(QByteArray, hostfield);
2503
2504     if (!QtNetworkSettings::hasIPv6())
2505         QSKIP("system doesn't support ipv6!");
2506
2507     QByteArray httpResponse = QByteArray("HTTP/1.0 200 OK\r\nContent-Length: ");
2508     httpResponse += QByteArray::number(dataToSend.size());
2509     httpResponse += "\r\n\r\n";
2510     httpResponse += dataToSend;
2511
2512     MiniHttpServer server(httpResponse, false, NULL/*thread*/, true/*useipv6*/);
2513     server.doClose = true;
2514
2515     url.setPort(server.serverPort());
2516     QNetworkRequest request(url);
2517
2518     QNetworkReplyPtr reply(manager.get(request));
2519     QVERIFY(waitForFinish(reply) == Success);
2520     QByteArray content = reply->readAll();
2521     //qDebug() << server.receivedData;
2522     QByteArray hostinfo = "\r\nHost: " + hostfield + ":" + QByteArray::number(server.serverPort()) + "\r\n";
2523     QVERIFY(server.receivedData.contains(hostinfo));
2524     QVERIFY(content == dataToSend);
2525     QCOMPARE(reply->url(), request.url());
2526     QVERIFY(reply->error() == error);
2527 }
2528
2529 void tst_QNetworkReply::sendCustomRequestToHttp_data()
2530 {
2531     QTest::addColumn<QUrl>("url");
2532     QTest::addColumn<QByteArray>("verb");
2533     QTest::addColumn<QBuffer *>("device");
2534     QTest::addColumn<int>("resultCode");
2535     QTest::addColumn<QNetworkReply::NetworkError>("error");
2536     QTest::addColumn<QByteArray>("expectedContent");
2537
2538     QTest::newRow("options") << QUrl("http://" + QtNetworkSettings::serverName()) <<
2539             QByteArray("OPTIONS") << (QBuffer *) 0 << 200 << QNetworkReply::NoError << QByteArray();
2540     QTest::newRow("trace") << QUrl("http://" + QtNetworkSettings::serverName()) <<
2541             QByteArray("TRACE") << (QBuffer *) 0 << 200 << QNetworkReply::NoError << QByteArray();
2542     QTest::newRow("connect") << QUrl("http://" + QtNetworkSettings::serverName()) <<
2543             QByteArray("CONNECT") << (QBuffer *) 0 << 400 << QNetworkReply::UnknownContentError << QByteArray(); // 400 = Bad Request
2544     QTest::newRow("nonsense") << QUrl("http://" + QtNetworkSettings::serverName()) <<
2545             QByteArray("NONSENSE") << (QBuffer *) 0 << 501 << QNetworkReply::ProtocolUnknownError << QByteArray(); // 501 = Method Not Implemented
2546
2547     QByteArray ba("test");
2548     QBuffer *buffer = new QBuffer;
2549     buffer->setData(ba);
2550     buffer->open(QIODevice::ReadOnly);
2551     QTest::newRow("post") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi") << QByteArray("POST")
2552             << buffer << 200 << QNetworkReply::NoError << QByteArray("098f6bcd4621d373cade4e832627b4f6\n");
2553
2554     QByteArray ba2("test");
2555     QBuffer *buffer2 = new QBuffer;
2556     buffer2->setData(ba2);
2557     buffer2->open(QIODevice::ReadOnly);
2558     QTest::newRow("put") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi") << QByteArray("PUT")
2559             << buffer2 << 200 << QNetworkReply::NoError << QByteArray("098f6bcd4621d373cade4e832627b4f6\n");
2560 }
2561
2562 void tst_QNetworkReply::sendCustomRequestToHttp()
2563 {
2564     QFETCH(QUrl, url);
2565     QNetworkRequest request(url);
2566     QNetworkReplyPtr reply;
2567     QFETCH(QByteArray, verb);
2568     QFETCH(QBuffer *, device);
2569     runCustomRequest(request, reply, verb, device);
2570     QCOMPARE(reply->url(), url);
2571     QFETCH(QNetworkReply::NetworkError, error);
2572     QCOMPARE(reply->error(), error);
2573     QFETCH(int, resultCode);
2574     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), resultCode);
2575     QFETCH(QByteArray, expectedContent);
2576     if (! expectedContent.isEmpty())
2577         QCOMPARE(reply->readAll(), expectedContent);
2578 }
2579
2580 void tst_QNetworkReply::ioGetFromData_data()
2581 {
2582     QTest::addColumn<QString>("urlStr");
2583     QTest::addColumn<QByteArray>("data");
2584
2585     QTest::newRow("data-empty") << "data:," << QByteArray();
2586     QTest::newRow("data-literal") << "data:,foo" << QByteArray("foo");
2587     QTest::newRow("data-pct") << "data:,%3Cbody%20contentEditable%3Dtrue%3E%0D%0A"
2588                            << QByteArray("<body contentEditable=true>\r\n");
2589     QTest::newRow("data-base64") << "data:;base64,UXQgaXMgZ3JlYXQh" << QByteArray("Qt is great!");
2590 }
2591
2592 void tst_QNetworkReply::ioGetFromData()
2593 {
2594     QFETCH(QString, urlStr);
2595
2596     QUrl url = QUrl::fromEncoded(urlStr.toLatin1());
2597     QNetworkRequest request(url);
2598
2599     QNetworkReplyPtr reply(manager.get(request));
2600     DataReader reader(reply);
2601
2602     connect(reply, SIGNAL(finished()),
2603             &QTestEventLoop::instance(), SLOT(exitLoop()));
2604     QTestEventLoop::instance().enterLoop(10);
2605     QVERIFY(!QTestEventLoop::instance().timeout());
2606
2607     QCOMPARE(reply->url(), request.url());
2608     QCOMPARE(reply->error(), QNetworkReply::NoError);
2609
2610     QFETCH(QByteArray, data);
2611     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toInt(), data.size());
2612     QCOMPARE(reader.data.size(), data.size());
2613     QCOMPARE(reader.data, data);
2614 }
2615
2616 void tst_QNetworkReply::ioGetFromFileSpecial_data()
2617 {
2618     getFromFileSpecial_data();
2619 }
2620
2621 void tst_QNetworkReply::ioGetFromFileSpecial()
2622 {
2623     QFETCH(QString, fileName);
2624     QFETCH(QString, url);
2625
2626     QFile resource(fileName);
2627     QVERIFY(resource.open(QIODevice::ReadOnly));
2628
2629     QNetworkRequest request;
2630     request.setUrl(url);
2631     QNetworkReplyPtr reply(manager.get(request));
2632     DataReader reader(reply);
2633
2634     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
2635     QTestEventLoop::instance().enterLoop(10);
2636     QVERIFY(!QTestEventLoop::instance().timeout());
2637
2638     QCOMPARE(reply->url(), request.url());
2639     QCOMPARE(reply->error(), QNetworkReply::NoError);
2640
2641     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), resource.size());
2642     QCOMPARE(qint64(reader.data.size()), resource.size());
2643     QCOMPARE(reader.data, resource.readAll());
2644 }
2645
2646 void tst_QNetworkReply::ioGetFromFile_data()
2647 {
2648     putToFile_data();
2649 }
2650
2651 void tst_QNetworkReply::ioGetFromFile()
2652 {
2653     QTemporaryFile file(QDir::currentPath() + "/temp-XXXXXX");
2654     file.setAutoRemove(true);
2655     QVERIFY(file.open());
2656
2657     QFETCH(QByteArray, data);
2658     QVERIFY(file.write(data) == data.size());
2659     file.flush();
2660     QCOMPARE(file.size(), qint64(data.size()));
2661
2662     QNetworkRequest request(QUrl::fromLocalFile(file.fileName()));
2663     QNetworkReplyPtr reply(manager.get(request));
2664     QVERIFY(reply->isFinished()); // a file should immediately be done
2665     DataReader reader(reply);
2666
2667     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
2668     QTestEventLoop::instance().enterLoop(10);
2669     QVERIFY(!QTestEventLoop::instance().timeout());
2670
2671     QCOMPARE(reply->url(), request.url());
2672     QCOMPARE(reply->error(), QNetworkReply::NoError);
2673
2674     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), file.size());
2675     QCOMPARE(qint64(reader.data.size()), file.size());
2676     QCOMPARE(reader.data, data);
2677 }
2678
2679 void tst_QNetworkReply::ioGetFromFtp_data()
2680 {
2681     QTest::addColumn<QString>("fileName");
2682     QTest::addColumn<qint64>("expectedSize");
2683
2684     QTest::newRow("bigfile") << "bigfile" << Q_INT64_C(519240);
2685
2686     QFile file(testDataDir + "/rfc3252.txt");
2687     QTest::newRow("rfc3252.txt") << "rfc3252.txt" << file.size();
2688 }
2689
2690 void tst_QNetworkReply::ioGetFromFtp()
2691 {
2692     QFETCH(QString, fileName);
2693     QFile reference(fileName);
2694     reference.open(QIODevice::ReadOnly); // will fail for bigfile
2695
2696     QNetworkRequest request("ftp://" + QtNetworkSettings::serverName() + "/qtest/" + fileName);
2697     QNetworkReplyPtr reply(manager.get(request));
2698     DataReader reader(reply);
2699
2700     QVERIFY(waitForFinish(reply) == Success);
2701
2702     QCOMPARE(reply->url(), request.url());
2703     QCOMPARE(reply->error(), QNetworkReply::NoError);
2704
2705     QFETCH(qint64, expectedSize);
2706     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), expectedSize);
2707     QCOMPARE(qint64(reader.data.size()), expectedSize);
2708
2709     if (reference.isOpen())
2710         QCOMPARE(reader.data, reference.readAll());
2711 }
2712
2713 void tst_QNetworkReply::ioGetFromFtpWithReuse()
2714 {
2715     QString fileName = testDataDir + "/rfc3252.txt";
2716     QFile reference(fileName);
2717     reference.open(QIODevice::ReadOnly);
2718
2719     QNetworkRequest request(QUrl("ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
2720
2721     // two concurrent (actually, consecutive) gets:
2722     QNetworkReplyPtr reply1(manager.get(request));
2723     DataReader reader1(reply1);
2724     QNetworkReplyPtr reply2(manager.get(request));
2725     DataReader reader2(reply2);
2726     QSignalSpy spy(reply1.data(), SIGNAL(finished()));
2727
2728     QVERIFY(waitForFinish(reply1) == Success);
2729     QVERIFY(waitForFinish(reply2) == Success);
2730
2731     QCOMPARE(reply1->url(), request.url());
2732     QCOMPARE(reply2->url(), request.url());
2733     QCOMPARE(reply1->error(), QNetworkReply::NoError);
2734     QCOMPARE(reply2->error(), QNetworkReply::NoError);
2735
2736     QCOMPARE(qint64(reader1.data.size()), reference.size());
2737     QCOMPARE(qint64(reader2.data.size()), reference.size());
2738     QCOMPARE(reply1->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2739     QCOMPARE(reply2->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2740
2741     QByteArray referenceData = reference.readAll();
2742     QCOMPARE(reader1.data, referenceData);
2743     QCOMPARE(reader2.data, referenceData);
2744 }
2745
2746 void tst_QNetworkReply::ioGetFromHttp()
2747 {
2748     QFile reference(testDataDir + "/rfc3252.txt");
2749     QVERIFY(reference.open(QIODevice::ReadOnly));
2750
2751     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
2752     QNetworkReplyPtr reply(manager.get(request));
2753     DataReader reader(reply);
2754
2755     QVERIFY(waitForFinish(reply) == Success);
2756
2757     QCOMPARE(reply->url(), request.url());
2758     QCOMPARE(reply->error(), QNetworkReply::NoError);
2759     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2760
2761     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2762     QCOMPARE(qint64(reader.data.size()), reference.size());
2763
2764     QCOMPARE(reader.data, reference.readAll());
2765 }
2766
2767 void tst_QNetworkReply::ioGetFromHttpWithReuseParallel()
2768 {
2769     QFile reference(testDataDir + "/rfc3252.txt");
2770     QVERIFY(reference.open(QIODevice::ReadOnly));
2771
2772     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
2773     QNetworkReplyPtr reply1(manager.get(request));
2774     QNetworkReplyPtr reply2(manager.get(request));
2775     DataReader reader1(reply1);
2776     DataReader reader2(reply2);
2777     QSignalSpy spy(reply1.data(), SIGNAL(finished()));
2778
2779     QVERIFY(waitForFinish(reply2) == Success);
2780     QVERIFY(waitForFinish(reply1) == Success);
2781
2782     QCOMPARE(reply1->url(), request.url());
2783     QCOMPARE(reply2->url(), request.url());
2784     QCOMPARE(reply1->error(), QNetworkReply::NoError);
2785     QCOMPARE(reply2->error(), QNetworkReply::NoError);
2786     QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2787     QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2788
2789     QCOMPARE(reply1->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2790     QCOMPARE(reply2->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2791     QCOMPARE(qint64(reader1.data.size()), reference.size());
2792     QCOMPARE(qint64(reader2.data.size()), reference.size());
2793
2794     QByteArray referenceData = reference.readAll();
2795     QCOMPARE(reader1.data, referenceData);
2796     QCOMPARE(reader2.data, referenceData);
2797 }
2798
2799 void tst_QNetworkReply::ioGetFromHttpWithReuseSequential()
2800 {
2801     QFile reference(testDataDir + "/rfc3252.txt");
2802     QVERIFY(reference.open(QIODevice::ReadOnly));
2803
2804     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
2805     {
2806         QNetworkReplyPtr reply(manager.get(request));
2807         DataReader reader(reply);
2808
2809         QVERIFY(waitForFinish(reply) == Success);
2810
2811         QCOMPARE(reply->url(), request.url());
2812         QCOMPARE(reply->error(), QNetworkReply::NoError);
2813         QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2814
2815         QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2816         QCOMPARE(qint64(reader.data.size()), reference.size());
2817
2818         QCOMPARE(reader.data, reference.readAll());
2819     }
2820
2821     reference.seek(0);
2822     // rinse and repeat:
2823     {
2824         QNetworkReplyPtr reply(manager.get(request));
2825         DataReader reader(reply);
2826
2827         QVERIFY(waitForFinish(reply) == Success);
2828
2829         QCOMPARE(reply->url(), request.url());
2830         QCOMPARE(reply->error(), QNetworkReply::NoError);
2831         QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2832
2833         QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), reference.size());
2834         QCOMPARE(qint64(reader.data.size()), reference.size());
2835
2836         QCOMPARE(reader.data, reference.readAll());
2837     }
2838 }
2839
2840 void tst_QNetworkReply::ioGetFromHttpWithAuth_data()
2841 {
2842     QTest::addColumn<QUrl>("url");
2843     QTest::addColumn<QByteArray>("expectedData");
2844     QTest::addColumn<int>("expectedAuth");
2845
2846     QFile reference(testDataDir + "/rfc3252.txt");
2847     reference.open(QIODevice::ReadOnly);
2848     QByteArray referenceData = reference.readAll();
2849     QTest::newRow("basic") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt") << referenceData << 1;
2850     QTest::newRow("digest") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/auth-digest/") << QByteArray("digest authentication successful\n") << 1;
2851     //if url contains username & password, then it should be used
2852     QTest::newRow("basic-in-url") << QUrl("http://httptest:httptest@" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt") << referenceData << 0;
2853     QTest::newRow("digest-in-url") << QUrl("http://httptest:httptest@" + QtNetworkSettings::serverName() + "/qtest/auth-digest/") << QByteArray("digest authentication successful\n") << 0;
2854     // if url contains incorrect credentials, expect QNAM to ask for good ones (even if cached - matches behaviour of browsers)
2855     QTest::newRow("basic-bad-user-in-url") << QUrl("http://baduser:httptest@" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt") << referenceData << 3;
2856     QTest::newRow("basic-bad-password-in-url") << QUrl("http://httptest:wrong@" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt") << referenceData << 3;
2857     QTest::newRow("digest-bad-user-in-url") << QUrl("http://baduser:httptest@" + QtNetworkSettings::serverName() + "/qtest/auth-digest/") << QByteArray("digest authentication successful\n") << 3;
2858     QTest::newRow("digest-bad-password-in-url") << QUrl("http://httptest:wrong@" + QtNetworkSettings::serverName() + "/qtest/auth-digest/") << QByteArray("digest authentication successful\n") << 3;
2859 }
2860
2861 void tst_QNetworkReply::ioGetFromHttpWithAuth()
2862 {
2863     // This test sends three requests
2864     // The first two in parallel
2865     // The third after the first two finished
2866
2867     QFETCH(QUrl, url);
2868     QFETCH(QByteArray, expectedData);
2869     QFETCH(int, expectedAuth);
2870     QNetworkRequest request(url);
2871     {
2872         QNetworkReplyPtr reply1(manager.get(request));
2873         QNetworkReplyPtr reply2(manager.get(request));
2874         DataReader reader1(reply1);
2875         DataReader reader2(reply2);
2876         QSignalSpy finishedspy(reply1.data(), SIGNAL(finished()));
2877
2878         QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2879         connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
2880                 SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2881
2882         QVERIFY(waitForFinish(reply2) == Success);
2883         QVERIFY(waitForFinish(reply1) == Success);
2884
2885         manager.disconnect(SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
2886                            this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2887
2888         QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2889         QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2890         QCOMPARE(reader1.data, expectedData);
2891         QCOMPARE(reader2.data, expectedData);
2892
2893         QCOMPARE(authspy.count(), (expectedAuth ? 1 : 0));
2894         expectedAuth = qMax(0, expectedAuth - 1);
2895     }
2896
2897     // rinse and repeat:
2898     {
2899         QNetworkReplyPtr reply(manager.get(request));
2900         DataReader reader(reply);
2901
2902         QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2903         connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
2904                 SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2905
2906         QVERIFY(waitForFinish(reply) == Success);
2907
2908         manager.disconnect(SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
2909                            this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2910
2911         QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2912         QCOMPARE(reader.data, expectedData);
2913
2914         QCOMPARE(authspy.count(), (expectedAuth ? 1 : 0));
2915         expectedAuth = qMax(0, expectedAuth - 1);
2916     }
2917
2918     // now check with synchronous calls:
2919     {
2920         request.setAttribute(
2921                 QNetworkRequest::SynchronousRequestAttribute,
2922                 true);
2923
2924         QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2925         QNetworkReplyPtr replySync(manager.get(request));
2926         QVERIFY(replySync->isFinished()); // synchronous
2927         if (expectedAuth) {
2928             // bad credentials in a synchronous request should just fail
2929             QCOMPARE(replySync->error(), QNetworkReply::AuthenticationRequiredError);
2930         } else {
2931             QCOMPARE(authspy.count(), 0);
2932
2933             // we cannot use a data reader here, since that connects to the readyRead signal,
2934             // just use readAll()
2935
2936             // the only thing we check here is that the auth cache was used when using synchronous requests
2937             QCOMPARE(replySync->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2938             QCOMPARE(replySync->readAll(), expectedData);
2939         }
2940     }
2941
2942     // check that credentials are used from cache if the same url is requested without credentials
2943     {
2944         url.setUserInfo(QString());
2945         request.setUrl(url);
2946         request.setAttribute(
2947                 QNetworkRequest::SynchronousRequestAttribute,
2948                 true);
2949
2950         QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2951         QNetworkReplyPtr replySync(manager.get(request));
2952         QVERIFY(replySync->isFinished()); // synchronous
2953         if (expectedAuth) {
2954             // bad credentials in a synchronous request should just fail
2955             QCOMPARE(replySync->error(), QNetworkReply::AuthenticationRequiredError);
2956         } else {
2957             QCOMPARE(authspy.count(), 0);
2958
2959             // we cannot use a data reader here, since that connects to the readyRead signal,
2960             // just use readAll()
2961
2962             // the only thing we check here is that the auth cache was used when using synchronous requests
2963             QCOMPARE(replySync->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
2964             QCOMPARE(replySync->readAll(), expectedData);
2965         }
2966     }
2967 }
2968
2969 void tst_QNetworkReply::ioGetFromHttpWithAuthSynchronous()
2970 {
2971     // verify that we do not enter an endless loop with synchronous calls and wrong credentials
2972     // the case when we succeed with the login is tested in ioGetFromHttpWithAuth()
2973
2974     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt"));
2975     request.setAttribute(
2976             QNetworkRequest::SynchronousRequestAttribute,
2977             true);
2978
2979     QSignalSpy authspy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
2980     QNetworkReplyPtr replySync(manager.get(request));
2981     QVERIFY(replySync->isFinished()); // synchronous
2982     QCOMPARE(replySync->error(), QNetworkReply::AuthenticationRequiredError);
2983     QCOMPARE(authspy.count(), 0);
2984     QCOMPARE(replySync->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 401);
2985 }
2986
2987 void tst_QNetworkReply::ioGetFromHttpWithProxyAuth()
2988 {
2989     // This test sends three requests
2990     // The first two in parallel
2991     // The third after the first two finished
2992     QFile reference(testDataDir + "/rfc3252.txt");
2993     QVERIFY(reference.open(QIODevice::ReadOnly));
2994
2995     QNetworkProxy proxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
2996     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
2997     {
2998         manager.setProxy(proxy);
2999         QNetworkReplyPtr reply1(manager.get(request));
3000         QNetworkReplyPtr reply2(manager.get(request));
3001         manager.setProxy(QNetworkProxy());
3002
3003         DataReader reader1(reply1);
3004         DataReader reader2(reply2);
3005         QSignalSpy finishedspy(reply1.data(), SIGNAL(finished()));
3006
3007         QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3008         connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3009                 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3010
3011         QVERIFY(waitForFinish(reply2) == Success);
3012         QVERIFY(waitForFinish(reply1) == Success);
3013
3014         manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3015                            this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3016
3017         QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3018         QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3019         QByteArray referenceData = reference.readAll();
3020         QCOMPARE(reader1.data, referenceData);
3021         QCOMPARE(reader2.data, referenceData);
3022
3023         QCOMPARE(authspy.count(), 1);
3024     }
3025
3026     reference.seek(0);
3027     // rinse and repeat:
3028     {
3029         manager.setProxy(proxy);
3030         QNetworkReplyPtr reply(manager.get(request));
3031         DataReader reader(reply);
3032         manager.setProxy(QNetworkProxy());
3033
3034         QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3035         connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3036                 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3037
3038         QVERIFY(waitForFinish(reply) == Success);
3039
3040         manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3041                            this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3042
3043         QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3044         QCOMPARE(reader.data, reference.readAll());
3045
3046         QCOMPARE(authspy.count(), 0);
3047     }
3048
3049     // now check with synchronous calls:
3050     reference.seek(0);
3051     {
3052         request.setAttribute(
3053                 QNetworkRequest::SynchronousRequestAttribute,
3054                 true);
3055
3056         QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3057         QNetworkReplyPtr replySync(manager.get(request));
3058         QVERIFY(replySync->isFinished()); // synchronous
3059         QCOMPARE(authspy.count(), 0);
3060
3061         // we cannot use a data reader here, since that connects to the readyRead signal,
3062         // just use readAll()
3063
3064         // the only thing we check here is that the proxy auth cache was used when using synchronous requests
3065         QCOMPARE(replySync->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3066         QCOMPARE(replySync->readAll(), reference.readAll());
3067     }
3068 }
3069
3070 void tst_QNetworkReply::ioGetFromHttpWithProxyAuthSynchronous()
3071 {
3072     // verify that we do not enter an endless loop with synchronous calls and wrong credentials
3073     // the case when we succeed with the login is tested in ioGetFromHttpWithAuth()
3074
3075     QNetworkProxy proxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
3076     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
3077     manager.setProxy(proxy);
3078     request.setAttribute(
3079             QNetworkRequest::SynchronousRequestAttribute,
3080             true);
3081
3082     QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3083     QNetworkReplyPtr replySync(manager.get(request));
3084     manager.setProxy(QNetworkProxy()); // reset
3085     QVERIFY(replySync->isFinished()); // synchronous
3086     QCOMPARE(replySync->error(), QNetworkReply::ProxyAuthenticationRequiredError);
3087     QCOMPARE(authspy.count(), 0);
3088     QCOMPARE(replySync->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 407);
3089 }
3090
3091 void tst_QNetworkReply::ioGetFromHttpWithSocksProxy()
3092 {
3093     // HTTP caching proxies are tested by the above function
3094     // test SOCKSv5 proxies too
3095
3096     QFile reference(testDataDir + "/rfc3252.txt");
3097     QVERIFY(reference.open(QIODevice::ReadOnly));
3098
3099     QNetworkProxy proxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080);
3100     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
3101     {
3102         manager.setProxy(proxy);
3103         QNetworkReplyPtr reply(manager.get(request));
3104         DataReader reader(reply);
3105         manager.setProxy(QNetworkProxy());
3106
3107         QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3108         connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3109                 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3110
3111         QVERIFY(waitForFinish(reply) == Success);
3112
3113         manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3114                            this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3115
3116         QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3117         QCOMPARE(reader.data, reference.readAll());
3118
3119         QCOMPARE(authspy.count(), 0);
3120     }
3121
3122     // set an invalid proxy just to make sure that we can't load
3123     proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1079);
3124     {
3125         manager.setProxy(proxy);
3126         QNetworkReplyPtr reply(manager.get(request));
3127         DataReader reader(reply);
3128         manager.setProxy(QNetworkProxy());
3129
3130         QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3131         connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3132                 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3133
3134         QVERIFY(waitForFinish(reply) == Failure);
3135
3136         manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3137                            this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3138
3139         QVERIFY(!reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid());
3140         QVERIFY(reader.data.isEmpty());
3141
3142         QVERIFY(int(reply->error()) > 0);
3143         QEXPECT_FAIL("", "QTcpSocket doesn't return enough information yet", Continue);
3144         QCOMPARE(int(reply->error()), int(QNetworkReply::ProxyConnectionRefusedError));
3145
3146         QCOMPARE(authspy.count(), 0);
3147     }
3148 }
3149
3150 #ifndef QT_NO_SSL
3151 void tst_QNetworkReply::ioGetFromHttpsWithSslErrors()
3152 {
3153     QFile reference(testDataDir + "/rfc3252.txt");
3154     QVERIFY(reference.open(QIODevice::ReadOnly));
3155
3156     QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
3157     QNetworkReplyPtr reply(manager.get(request));
3158     DataReader reader(reply);
3159
3160     QSignalSpy sslspy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)));
3161     connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
3162             SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
3163     connect(reply, SIGNAL(metaDataChanged()), SLOT(storeSslConfiguration()));
3164
3165     QVERIFY(waitForFinish(reply) == Success);
3166
3167     manager.disconnect(SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
3168                        this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
3169
3170     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3171     QCOMPARE(reader.data, reference.readAll());
3172
3173     QCOMPARE(sslspy.count(), 1);
3174
3175     QVERIFY(!storedSslConfiguration.isNull());
3176     QVERIFY(!reply->sslConfiguration().isNull());
3177 }
3178
3179 void tst_QNetworkReply::ioGetFromHttpsWithIgnoreSslErrors()
3180 {
3181     // same as above, except that we call ignoreSslErrors and don't connect
3182     // to the sslErrors() signal (which is *still* emitted)
3183
3184     QFile reference(testDataDir + "/rfc3252.txt");
3185     QVERIFY(reference.open(QIODevice::ReadOnly));
3186
3187     QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
3188
3189     QNetworkReplyPtr reply(manager.get(request));
3190     reply->ignoreSslErrors();
3191     DataReader reader(reply);
3192
3193     QSignalSpy sslspy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)));
3194     connect(reply, SIGNAL(metaDataChanged()), SLOT(storeSslConfiguration()));
3195
3196     QVERIFY(waitForFinish(reply) == Success);
3197
3198     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3199     QCOMPARE(reader.data, reference.readAll());
3200
3201     QCOMPARE(sslspy.count(), 1);
3202
3203     QVERIFY(!storedSslConfiguration.isNull());
3204     QVERIFY(!reply->sslConfiguration().isNull());
3205 }
3206
3207 void tst_QNetworkReply::ioGetFromHttpsWithSslHandshakeError()
3208 {
3209     QFile reference(testDataDir + "/rfc3252.txt");
3210     QVERIFY(reference.open(QIODevice::ReadOnly));
3211
3212     QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + ":80"));
3213
3214     QNetworkReplyPtr reply(manager.get(request));
3215     reply->ignoreSslErrors();
3216     DataReader reader(reply);
3217
3218     QSignalSpy sslspy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)));
3219     connect(reply, SIGNAL(metaDataChanged()), SLOT(storeSslConfiguration()));
3220
3221     QVERIFY(waitForFinish(reply) == Failure);
3222
3223     QCOMPARE(reply->error(), QNetworkReply::SslHandshakeFailedError);
3224     QCOMPARE(sslspy.count(), 0);
3225 }
3226 #endif
3227
3228 void tst_QNetworkReply::ioGetFromHttpBrokenServer_data()
3229 {
3230     QTest::addColumn<QByteArray>("dataToSend");
3231     QTest::addColumn<bool>("doDisconnect");
3232
3233     QTest::newRow("no-newline") << QByteArray("Hello World") << false;
3234
3235     // these are OK now, we just eat the lonely newlines
3236     //QTest::newRow("just-newline") << QByteArray("\r\n") << false;
3237     //QTest::newRow("just-2newline") << QByteArray("\r\n\r\n") << false;
3238
3239     QTest::newRow("with-newlines") << QByteArray("Long first line\r\nLong second line") << false;
3240     QTest::newRow("with-newlines2") << QByteArray("\r\nSecond line") << false;
3241     QTest::newRow("with-newlines3") << QByteArray("ICY\r\nSecond line") << false;
3242     QTest::newRow("invalid-version") << QByteArray("HTTP/123 200 \r\n") << false;
3243     QTest::newRow("invalid-version2") << QByteArray("HTTP/a.\033 200 \r\n") << false;
3244     QTest::newRow("invalid-reply-code") << QByteArray("HTTP/1.0 fuu \r\n") << false;
3245
3246     QTest::newRow("empty+disconnect") << QByteArray() << true;
3247
3248     QTest::newRow("no-newline+disconnect") << QByteArray("Hello World") << true;
3249     QTest::newRow("just-newline+disconnect") << QByteArray("\r\n") << true;
3250     QTest::newRow("just-2newline+disconnect") << QByteArray("\r\n\r\n") << true;
3251     QTest::newRow("with-newlines+disconnect") << QByteArray("Long first line\r\nLong second line") << true;
3252     QTest::newRow("with-newlines2+disconnect") << QByteArray("\r\nSecond line") << true;
3253     QTest::newRow("with-newlines3+disconnect") << QByteArray("ICY\r\nSecond line") << true;
3254
3255     QTest::newRow("invalid-version+disconnect") << QByteArray("HTTP/123 200 ") << true;
3256     QTest::newRow("invalid-version2+disconnect") << QByteArray("HTTP/a.\033 200 ") << true;
3257     QTest::newRow("invalid-reply-code+disconnect") << QByteArray("HTTP/1.0 fuu ") << true;
3258
3259     QTest::newRow("immediate disconnect") << QByteArray("") << true;
3260     QTest::newRow("justHalfStatus+disconnect") << QByteArray("HTTP/1.1") << true;
3261     QTest::newRow("justStatus+disconnect") << QByteArray("HTTP/1.1 200 OK\r\n") << true;
3262     QTest::newRow("justStatusAndHalfHeaders+disconnect") << QByteArray("HTTP/1.1 200 OK\r\nContent-L") << true;
3263
3264     QTest::newRow("halfContent+disconnect") << QByteArray("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nAB") << true;
3265
3266 }
3267
3268 void tst_QNetworkReply::ioGetFromHttpBrokenServer()
3269 {
3270     QFETCH(QByteArray, dataToSend);
3271     QFETCH(bool, doDisconnect);
3272     MiniHttpServer server(dataToSend);
3273     server.doClose = doDisconnect;
3274
3275     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
3276     QNetworkReplyPtr reply(manager.get(request));
3277     QSignalSpy spy(reply.data(), SIGNAL(error(QNetworkReply::NetworkError)));
3278
3279     QVERIFY(waitForFinish(reply) == Failure);
3280
3281     QCOMPARE(reply->url(), request.url());
3282     QCOMPARE(spy.count(), 1);
3283     QVERIFY(reply->error() != QNetworkReply::NoError);
3284 }
3285
3286 void tst_QNetworkReply::ioGetFromHttpStatus100_data()
3287 {
3288     QTest::addColumn<QByteArray>("dataToSend");
3289     QTest::addColumn<int>("statusCode");
3290     QTest::newRow("normal") << QByteArray("HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << 200;
3291     QTest::newRow("minimal") << QByteArray("HTTP/1.1 100 Continue\n\nHTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << 200;
3292     QTest::newRow("minimal2") << QByteArray("HTTP/1.1 100 Continue\n\nHTTP/1.0 200 OK\r\n\r\n") << 200;
3293     QTest::newRow("minimal3") << QByteArray("HTTP/1.1 100 Continue\n\nHTTP/1.0 200 OK\n\n") << 200;
3294     QTest::newRow("minimal+404") << QByteArray("HTTP/1.1 100 Continue\n\nHTTP/1.0 204 No Content\r\n\r\n") << 204;
3295     QTest::newRow("with_headers") << QByteArray("HTTP/1.1 100 Continue\r\nBla: x\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << 200;
3296     QTest::newRow("with_headers2") << QByteArray("HTTP/1.1 100 Continue\nBla: x\n\nHTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << 200;
3297 }
3298
3299 void tst_QNetworkReply::ioGetFromHttpStatus100()
3300 {
3301     QFETCH(QByteArray, dataToSend);
3302     QFETCH(int, statusCode);
3303     MiniHttpServer server(dataToSend);
3304     server.doClose = true;
3305
3306     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
3307     QNetworkReplyPtr reply(manager.get(request));
3308
3309     QVERIFY(waitForFinish(reply) == Success);
3310
3311     QCOMPARE(reply->url(), request.url());
3312     QCOMPARE(reply->error(), QNetworkReply::NoError);
3313     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), statusCode);
3314     QVERIFY(reply->rawHeader("bla").isNull());
3315 }
3316
3317 void tst_QNetworkReply::ioGetFromHttpNoHeaders_data()
3318 {
3319     QTest::addColumn<QByteArray>("dataToSend");
3320     QTest::newRow("justStatus+noheaders+disconnect") << QByteArray("HTTP/1.0 200 OK\r\n\r\n");
3321 }
3322
3323 void tst_QNetworkReply::ioGetFromHttpNoHeaders()
3324 {
3325     QFETCH(QByteArray, dataToSend);
3326     MiniHttpServer server(dataToSend);
3327     server.doClose = true;
3328
3329     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
3330     QNetworkReplyPtr reply(manager.get(request));
3331
3332     QVERIFY(waitForFinish(reply) == Success);
3333
3334     QCOMPARE(reply->url(), request.url());
3335     QCOMPARE(reply->error(), QNetworkReply::NoError);
3336     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
3337 }
3338
3339 void tst_QNetworkReply::ioGetFromHttpWithCache_data()
3340 {
3341     qRegisterMetaType<MyMemoryCache::CachedContent>();
3342     QTest::addColumn<QByteArray>("dataToSend");
3343     QTest::addColumn<QString>("body");
3344     QTest::addColumn<MyMemoryCache::CachedContent>("cachedReply");
3345     QTest::addColumn<int>("cacheMode");
3346     QTest::addColumn<QStringList>("extraHttpHeaders");
3347     QTest::addColumn<bool>("loadedFromCache");
3348     QTest::addColumn<bool>("networkUsed");
3349
3350     QByteArray reply200 =
3351             "HTTP/1.0 200\r\n"
3352             "Connection: keep-alive\r\n"
3353             "Content-Type: text/plain\r\n"
3354             "Cache-control: no-cache\r\n"
3355             "Content-length: 8\r\n"
3356             "\r\n"
3357             "Reloaded";
3358     QByteArray reply304 =
3359             "HTTP/1.0 304 Use Cache\r\n"
3360             "Connection: keep-alive\r\n"
3361             "\r\n";
3362
3363     QTest::newRow("not-cached,always-network")
3364             << reply200 << "Reloaded" << MyMemoryCache::CachedContent() << int(QNetworkRequest::AlwaysNetwork) << QStringList() << false << true;
3365     QTest::newRow("not-cached,prefer-network")
3366             << reply200 << "Reloaded" << MyMemoryCache::CachedContent() << int(QNetworkRequest::PreferNetwork) << QStringList() << false << true;
3367     QTest::newRow("not-cached,prefer-cache")
3368             << reply200 << "Reloaded" << MyMemoryCache::CachedContent() << int(QNetworkRequest::PreferCache) << QStringList() << false << true;
3369
3370     QDateTime present = QDateTime::currentDateTime().toUTC();
3371     QDateTime past = present.addSecs(-3600);
3372     QDateTime future = present.addSecs(3600);
3373     static const char dateFormat[] = "ddd, dd MMM yyyy hh:mm:ss 'GMT'";
3374
3375     QNetworkCacheMetaData::RawHeaderList rawHeaders;
3376     MyMemoryCache::CachedContent content;
3377     content.second = "Not-reloaded";
3378     content.first.setLastModified(past);
3379
3380     //
3381     // Set to expired
3382     //
3383     rawHeaders.clear();
3384     rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1())
3385             << QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=0"); // isn't used in cache loading
3386     content.first.setRawHeaders(rawHeaders);
3387     content.first.setLastModified(past);
3388     content.first.setExpirationDate(past);
3389
3390     QTest::newRow("expired,200,prefer-network")
3391             << reply200 << "Reloaded" << content << int(QNetworkRequest::PreferNetwork) << QStringList() << false << true;
3392     QTest::newRow("expired,200,prefer-cache")
3393             << reply200 << "Reloaded" << content << int(QNetworkRequest::PreferCache) << QStringList() << false << true;
3394
3395     QTest::newRow("expired,304,prefer-network")
3396             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << QStringList() << true << true;
3397     QTest::newRow("expired,304,prefer-cache")
3398             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << QStringList() << true << true;
3399
3400     //
3401     // Set to not-expired
3402     //
3403     rawHeaders.clear();
3404     rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1())
3405             << QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=7200"); // isn't used in cache loading
3406     content.first.setRawHeaders(rawHeaders);
3407     content.first.setExpirationDate(future);
3408
3409     QTest::newRow("not-expired,200,always-network")
3410             << reply200 << "Reloaded" << content << int(QNetworkRequest::AlwaysNetwork) << QStringList() << false << true;
3411     QTest::newRow("not-expired,200,prefer-network")
3412             << reply200 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << QStringList() << true << false;
3413     QTest::newRow("not-expired,200,prefer-cache")
3414             << reply200 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << QStringList() << true << false;
3415     QTest::newRow("not-expired,200,always-cache")
3416             << reply200 << "Not-reloaded" << content << int(QNetworkRequest::AlwaysCache) << QStringList() << true << false;
3417
3418     QTest::newRow("not-expired,304,prefer-network")
3419             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << QStringList() << true << false;
3420     QTest::newRow("not-expired,304,prefer-cache")
3421             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << QStringList() << true << false;
3422     QTest::newRow("not-expired,304,always-cache")
3423             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::AlwaysCache) << QStringList() << true << false;
3424
3425     //
3426     // Set must-revalidate now
3427     //
3428     rawHeaders.clear();
3429     rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1())
3430             << QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=7200, must-revalidate"); // must-revalidate is used
3431     content.first.setRawHeaders(rawHeaders);
3432
3433     QTest::newRow("must-revalidate,200,always-network")
3434             << reply200 << "Reloaded" << content << int(QNetworkRequest::AlwaysNetwork) << QStringList() << false << true;
3435     QTest::newRow("must-revalidate,200,prefer-network")
3436             << reply200 << "Reloaded" << content << int(QNetworkRequest::PreferNetwork) << QStringList() << false << true;
3437     QTest::newRow("must-revalidate,200,prefer-cache")
3438             << reply200 << "Reloaded" << content << int(QNetworkRequest::PreferCache) << QStringList() << false << true;
3439     QTest::newRow("must-revalidate,200,always-cache")
3440             << reply200 << "" << content << int(QNetworkRequest::AlwaysCache) << QStringList() << false << false;
3441
3442     QTest::newRow("must-revalidate,304,prefer-network")
3443             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferNetwork) << QStringList() << true << true;
3444     QTest::newRow("must-revalidate,304,prefer-cache")
3445             << reply304 << "Not-reloaded" << content << int(QNetworkRequest::PreferCache) << QStringList() << true << true;
3446     QTest::newRow("must-revalidate,304,always-cache")
3447             << reply304 << "" << content << int(QNetworkRequest::AlwaysCache) << QStringList() << false << false;
3448
3449     //
3450     // Partial content
3451     //
3452     rawHeaders.clear();
3453     rawHeaders << QNetworkCacheMetaData::RawHeader("Date", QLocale::c().toString(past, dateFormat).toLatin1())
3454             << QNetworkCacheMetaData::RawHeader("Cache-control", "max-age=7200"); // isn't used in cache loading
3455     content.first.setRawHeaders(rawHeaders);
3456     content.first.setExpirationDate(future);
3457
3458     QByteArray reply206 =
3459             "HTTP/1.0 206\r\n"
3460             "Connection: keep-alive\r\n"
3461             "Content-Type: text/plain\r\n"
3462             "Cache-control: no-cache\r\n"
3463             "Content-Range: bytes 2-6/8\r\n"
3464             "Content-length: 4\r\n"
3465             "\r\n"
3466             "load";
3467
3468     QTest::newRow("partial,dontuse-cache")
3469             << reply206 << "load" << content << int(QNetworkRequest::PreferCache) << (QStringList() << "Range" << "bytes=2-6") << false << true;
3470 }
3471
3472 void tst_QNetworkReply::ioGetFromHttpWithCache()
3473 {
3474     QFETCH(QByteArray, dataToSend);
3475     MiniHttpServer server(dataToSend);
3476     server.doClose = false;
3477
3478     MyMemoryCache *memoryCache = new MyMemoryCache(&manager);
3479     manager.setCache(memoryCache);
3480
3481     QFETCH(MyMemoryCache::CachedContent, cachedReply);
3482     QUrl url = "http://localhost:" + QString::number(server.serverPort());
3483     cachedReply.first.setUrl(url);
3484     if (!cachedReply.second.isNull())
3485         memoryCache->cache.insert(url.toEncoded(), cachedReply);
3486
3487     QFETCH(int, cacheMode);
3488     QNetworkRequest request(url);
3489     request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, cacheMode);
3490     request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
3491
3492     QFETCH(QStringList, extraHttpHeaders);
3493     QStringListIterator it(extraHttpHeaders);
3494     while (it.hasNext()) {
3495         QString header = it.next();
3496         QString value = it.next();
3497         request.setRawHeader(header.toLatin1(), value.toLatin1()); // To latin1? Deal with it!
3498     }
3499
3500     QNetworkReplyPtr reply(manager.get(request));
3501
3502     QVERIFY(waitForFinish(reply) != Timeout);
3503
3504     QTEST(reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool(), "loadedFromCache");
3505     QTEST(server.totalConnections > 0, "networkUsed");
3506     QFETCH(QString, body);
3507     QCOMPARE(reply->readAll().constData(), qPrintable(body));
3508 }
3509
3510 void tst_QNetworkReply::ioGetWithManyProxies_data()
3511 {
3512     QTest::addColumn<QList<QNetworkProxy> >("proxyList");
3513     QTest::addColumn<QNetworkProxy>("proxyUsed");
3514     QTest::addColumn<QString>("url");
3515     QTest::addColumn<QNetworkReply::NetworkError>("expectedError");
3516
3517     QList<QNetworkProxy> proxyList;
3518
3519     // All of the other functions test DefaultProxy
3520     // So let's test something else
3521
3522     // Simple tests that work:
3523
3524     // HTTP request with HTTP caching proxy
3525     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
3526     QTest::newRow("http-on-http")
3527         << proxyList << proxyList.at(0)
3528         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3529         << QNetworkReply::NoError;
3530
3531     // HTTP request with HTTP transparent proxy
3532     proxyList.clear();
3533     proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129);
3534     QTest::newRow("http-on-http2")
3535         << proxyList << proxyList.at(0)
3536         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3537         << QNetworkReply::NoError;
3538
3539     // HTTP request with SOCKS transparent proxy
3540     proxyList.clear();
3541     proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
3542     QTest::newRow("http-on-socks")
3543         << proxyList << proxyList.at(0)
3544         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3545         << QNetworkReply::NoError;
3546
3547     // FTP request with FTP caching proxy
3548     proxyList.clear();
3549     proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121);
3550     QTest::newRow("ftp-on-ftp")
3551         << proxyList << proxyList.at(0)
3552         << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3553         << QNetworkReply::NoError;
3554
3555     // The following test doesn't work because QFtp is too limited
3556     // It can only talk to its own kind of proxies
3557
3558     // FTP request with SOCKSv5 transparent proxy
3559     proxyList.clear();
3560     proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
3561     QTest::newRow("ftp-on-socks")
3562         << proxyList << proxyList.at(0)
3563         << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3564         << QNetworkReply::NoError;
3565
3566 #ifndef QT_NO_SSL
3567     // HTTPS with HTTP transparent proxy
3568     proxyList.clear();
3569     proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129);
3570     QTest::newRow("https-on-http")
3571         << proxyList << proxyList.at(0)
3572         << "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3573         << QNetworkReply::NoError;
3574
3575     // HTTPS request with SOCKS transparent proxy
3576     proxyList.clear();
3577     proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
3578     QTest::newRow("https-on-socks")
3579         << proxyList << proxyList.at(0)
3580         << "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3581         << QNetworkReply::NoError;
3582 #endif
3583
3584     // Tests that fail:
3585
3586     // HTTP request with FTP caching proxy
3587     proxyList.clear();
3588     proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121);
3589     QTest::newRow("http-on-ftp")
3590         << proxyList << QNetworkProxy()
3591         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3592         << QNetworkReply::ProxyNotFoundError;
3593
3594     // FTP request with HTTP caching proxy
3595     proxyList.clear();
3596     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
3597     QTest::newRow("ftp-on-http")
3598         << proxyList << QNetworkProxy()
3599         << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3600         << QNetworkReply::ProxyNotFoundError;
3601
3602     // FTP request with HTTP caching proxies
3603     proxyList.clear();
3604     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3605               << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3130);
3606     QTest::newRow("ftp-on-multiple-http")
3607         << proxyList << QNetworkProxy()
3608         << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3609         << QNetworkReply::ProxyNotFoundError;
3610
3611 #ifndef QT_NO_SSL
3612     // HTTPS with HTTP caching proxy
3613     proxyList.clear();
3614     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
3615     QTest::newRow("https-on-httptransparent")
3616         << proxyList << QNetworkProxy()
3617         << "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3618         << QNetworkReply::ProxyNotFoundError;
3619
3620     // HTTPS with FTP caching proxy
3621     proxyList.clear();
3622     proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121);
3623     QTest::newRow("https-on-ftp")
3624         << proxyList << QNetworkProxy()
3625         << "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3626         << QNetworkReply::ProxyNotFoundError;
3627 #endif
3628
3629     // Complex requests:
3630
3631     // HTTP request with more than one HTTP proxy
3632     proxyList.clear();
3633     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3634               << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3130);
3635     QTest::newRow("http-on-multiple-http")
3636         << proxyList << proxyList.at(0)
3637         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3638         << QNetworkReply::NoError;
3639
3640     // HTTP request with HTTP + SOCKS
3641     proxyList.clear();
3642     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3643               << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
3644     QTest::newRow("http-on-http+socks")
3645         << proxyList << proxyList.at(0)
3646         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3647         << QNetworkReply::NoError;
3648
3649     // HTTP request with FTP + HTTP + SOCKS
3650     proxyList.clear();
3651     proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121)
3652               << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3653               << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081);
3654     QTest::newRow("http-on-ftp+http+socks")
3655         << proxyList << proxyList.at(1) // second proxy should be used
3656         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3657         << QNetworkReply::NoError;
3658
3659     // HTTP request with NoProxy + HTTP
3660     proxyList.clear();
3661     proxyList << QNetworkProxy(QNetworkProxy::NoProxy)
3662               << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129);
3663     QTest::newRow("http-on-noproxy+http")
3664         << proxyList << proxyList.at(0)
3665         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3666         << QNetworkReply::NoError;
3667
3668     // HTTP request with FTP + NoProxy
3669     proxyList.clear();
3670     proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121)
3671               << QNetworkProxy(QNetworkProxy::NoProxy);
3672     QTest::newRow("http-on-ftp+noproxy")
3673         << proxyList << proxyList.at(1) // second proxy should be used
3674         << "http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3675         << QNetworkReply::NoError;
3676
3677     // FTP request with HTTP Caching + FTP
3678     proxyList.clear();
3679     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3680               << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121);
3681     QTest::newRow("ftp-on-http+ftp")
3682         << proxyList << proxyList.at(1) // second proxy should be used
3683         << "ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3684         << QNetworkReply::NoError;
3685
3686 #ifndef QT_NO_SSL
3687     // HTTPS request with HTTP Caching + HTTP transparent
3688     proxyList.clear();
3689     proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3690               << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129);
3691     QTest::newRow("https-on-httpcaching+http")
3692         << proxyList << proxyList.at(1) // second proxy should be used
3693         << "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3694         << QNetworkReply::NoError;
3695
3696     // HTTPS request with FTP + HTTP C + HTTP T
3697     proxyList.clear();
3698     proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121)
3699               << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129)
3700               << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129);
3701     QTest::newRow("https-on-ftp+httpcaching+http")
3702         << proxyList << proxyList.at(2) // skip the first two
3703         << "https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"
3704         << QNetworkReply::NoError;
3705 #endif
3706 }
3707
3708 void tst_QNetworkReply::ioGetWithManyProxies()
3709 {
3710     // Test proxy factories
3711
3712     QFile reference(testDataDir + "/rfc3252.txt");
3713     QVERIFY(reference.open(QIODevice::ReadOnly));
3714
3715     // set the proxy factory:
3716     QFETCH(QList<QNetworkProxy>, proxyList);
3717     MyProxyFactory *proxyFactory = new MyProxyFactory;
3718     proxyFactory->toReturn = proxyList;
3719     manager.setProxyFactory(proxyFactory);
3720
3721     QFETCH(QString, url);
3722     QUrl theUrl(url);
3723     QNetworkRequest request(theUrl);
3724     QNetworkReplyPtr reply(manager.get(request));
3725     DataReader reader(reply);
3726
3727     QSignalSpy authspy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3728     connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3729             SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3730 #ifndef QT_NO_SSL
3731     connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
3732             SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
3733 #endif
3734
3735     QVERIFY(waitForFinish(reply) != Timeout);
3736
3737     manager.disconnect(SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
3738                        this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
3739 #ifndef QT_NO_SSL
3740     manager.disconnect(SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
3741                        this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
3742 #endif
3743
3744     QFETCH(QNetworkReply::NetworkError, expectedError);
3745     QEXPECT_FAIL("ftp-on-socks", "QFtp is too limited and won't accept non-FTP proxies", Abort);
3746     QCOMPARE(reply->error(), expectedError);
3747
3748     // Verify that the factory was called properly
3749     QCOMPARE(proxyFactory->callCount, 1);
3750     QCOMPARE(proxyFactory->lastQuery, QNetworkProxyQuery(theUrl));
3751
3752     if (expectedError == QNetworkReply::NoError) {
3753         // request succeeded
3754         QCOMPARE(reader.data, reference.readAll());
3755
3756         // now verify that the proxies worked:
3757         QFETCH(QNetworkProxy, proxyUsed);
3758         if (proxyUsed.type() == QNetworkProxy::NoProxy) {
3759             QCOMPARE(authspy.count(), 0);
3760         } else {
3761             if (QByteArray(QTest::currentDataTag()).startsWith("ftp-"))
3762                 return;         // No authentication with current FTP or with FTP proxies
3763             QCOMPARE(authspy.count(), 1);
3764             QCOMPARE(qvariant_cast<QNetworkProxy>(authspy.at(0).at(0)), proxyUsed);
3765         }
3766     } else {
3767         // request failed
3768         QCOMPARE(authspy.count(), 0);
3769     }
3770 }
3771
3772 void tst_QNetworkReply::ioPutToFileFromFile_data()
3773 {
3774     QTest::addColumn<QString>("fileName");
3775
3776     QTest::newRow("empty") << (testDataDir + "/empty");
3777     QTest::newRow("real-file") << (testDataDir + "/rfc3252.txt");
3778     QTest::newRow("resource") << ":/resource";
3779     QTest::newRow("search-path") << "testdata:/rfc3252.txt";
3780 }
3781
3782 void tst_QNetworkReply::ioPutToFileFromFile()
3783 {
3784     QFETCH(QString, fileName);
3785     QFile sourceFile(fileName);
3786     QFile targetFile(testFileName);
3787
3788     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
3789
3790     QUrl url = QUrl::fromLocalFile(targetFile.fileName());
3791     QNetworkRequest request(url);
3792     QNetworkReplyPtr reply(manager.put(request, &sourceFile));
3793
3794     QVERIFY(waitForFinish(reply) == Success);
3795
3796     QCOMPARE(reply->url(), url);
3797     QCOMPARE(reply->error(), QNetworkReply::NoError);
3798     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
3799     QVERIFY(reply->readAll().isEmpty());
3800
3801     QVERIFY(sourceFile.atEnd());
3802     sourceFile.seek(0);         // reset it to the beginning
3803
3804     QVERIFY(targetFile.open(QIODevice::ReadOnly));
3805     QCOMPARE(targetFile.size(), sourceFile.size());
3806     QCOMPARE(targetFile.readAll(), sourceFile.readAll());
3807 }
3808
3809 void tst_QNetworkReply::ioPutToFileFromSocket_data()
3810 {
3811     putToFile_data();
3812 }
3813
3814 void tst_QNetworkReply::ioPutToFileFromSocket()
3815 {
3816     QFile file(testFileName);
3817
3818     QUrl url = QUrl::fromLocalFile(file.fileName());
3819     QNetworkRequest request(url);
3820
3821     QFETCH(QByteArray, data);
3822     SocketPair socketpair;
3823     QTRY_VERIFY(socketpair.create()); //QTRY_VERIFY as a workaround for QTBUG-24451
3824
3825 #ifdef Q_OS_WIN
3826     //128k and 2M tests regularly fail. Assumed same characteristics as ioPostToHttpFromSocket
3827     if (data.size() > 1000)
3828         QSKIP("unstable on windows - QTBUG-25386");
3829 #endif
3830     socketpair.endPoints[0]->write(data);
3831     QNetworkReplyPtr reply(manager.put(QNetworkRequest(url), socketpair.endPoints[1]));
3832     socketpair.endPoints[0]->close();
3833
3834     QVERIFY(waitForFinish(reply) == Success);
3835     QCOMPARE(reply->error(), QNetworkReply::NoError);
3836
3837     QCOMPARE(reply->url(), url);
3838     QCOMPARE(reply->error(), QNetworkReply::NoError);
3839     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
3840     QVERIFY(reply->readAll().isEmpty());
3841
3842     QVERIFY(file.open(QIODevice::ReadOnly));
3843     QCOMPARE(file.size(), qint64(data.size()));
3844     QByteArray contents = file.readAll();
3845     QCOMPARE(contents, data);
3846 }
3847
3848 void tst_QNetworkReply::ioPutToFileFromLocalSocket_data()
3849 {
3850     putToFile_data();
3851 }
3852
3853 void tst_QNetworkReply::ioPutToFileFromLocalSocket()
3854 {
3855     QString socketname = "networkreplytest";
3856     QLocalServer server;
3857     if (!server.listen(socketname)) {
3858         QLocalServer::removeServer(socketname);
3859         QVERIFY(server.listen(socketname));
3860     }
3861     QLocalSocket active;
3862     active.connectToServer(socketname);
3863     QVERIFY2(server.waitForNewConnection(10), server.errorString().toLatin1().constData());
3864     QVERIFY2(active.waitForConnected(10), active.errorString().toLatin1().constData());
3865     QVERIFY2(server.hasPendingConnections(), server.errorString().toLatin1().constData());
3866     QLocalSocket *passive = server.nextPendingConnection();
3867
3868     QFile file(testFileName);
3869     QUrl url = QUrl::fromLocalFile(file.fileName());
3870     QNetworkRequest request(url);
3871
3872     QFETCH(QByteArray, data);
3873     active.write(data);
3874     active.close();
3875     QNetworkReplyPtr reply(manager.put(QNetworkRequest(url), passive));
3876     passive->setParent(reply.data());
3877
3878 #ifdef Q_OS_WIN
3879     if (!data.isEmpty())
3880         QEXPECT_FAIL("", "QTBUG-18385", Abort);
3881 #endif
3882     QVERIFY(waitForFinish(reply) == Success);
3883     QCOMPARE(reply->error(), QNetworkReply::NoError);
3884
3885     QCOMPARE(reply->url(), url);
3886     QCOMPARE(reply->error(), QNetworkReply::NoError);
3887     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
3888     QVERIFY(reply->readAll().isEmpty());
3889
3890     QVERIFY(file.open(QIODevice::ReadOnly));
3891     QCOMPARE(file.size(), qint64(data.size()));
3892     QByteArray contents = file.readAll();
3893     QCOMPARE(contents, data);
3894 }
3895
3896 // Currently no stdin/out supported for Windows CE.
3897 #ifndef QT_NO_PROCESS
3898 void tst_QNetworkReply::ioPutToFileFromProcess_data()
3899 {
3900     putToFile_data();
3901 }
3902
3903 void tst_QNetworkReply::ioPutToFileFromProcess()
3904 {
3905 #if defined(Q_OS_WINCE)
3906     QSKIP("Currently no stdin/out supported for Windows CE");
3907 #else
3908 #ifdef Q_OS_WIN
3909     if (qstrcmp(QTest::currentDataTag(), "small") == 0)
3910         QSKIP("When passing a CR-LF-LF sequence through Windows stdio, it gets converted, "
3911               "so this test fails. Disabled on Windows");
3912 #endif
3913
3914     QFile file(testFileName);
3915
3916     QUrl url = QUrl::fromLocalFile(file.fileName());
3917     QNetworkRequest request(url);
3918
3919     QFETCH(QByteArray, data);
3920     QProcess process;
3921     QString echoExe = echoProcessDir + "/echo";
3922     process.start(echoExe, QStringList("all"));
3923     QVERIFY2(process.waitForStarted(), qPrintable(
3924         QString::fromLatin1("Could not start %1: %2").arg(echoExe, process.errorString())));
3925     process.write(data);
3926     process.closeWriteChannel();
3927
3928     QNetworkReplyPtr reply(manager.put(QNetworkRequest(url), &process));
3929
3930     QVERIFY(waitForFinish(reply) == Success);
3931
3932     QCOMPARE(reply->url(), url);
3933     QCOMPARE(reply->error(), QNetworkReply::NoError);
3934     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
3935     QVERIFY(reply->readAll().isEmpty());
3936
3937     QVERIFY(file.open(QIODevice::ReadOnly));
3938     QCOMPARE(file.size(), qint64(data.size()));
3939     QByteArray contents = file.readAll();
3940     QCOMPARE(contents, data);
3941 #endif
3942 }
3943 #endif
3944
3945 void tst_QNetworkReply::ioPutToFtpFromFile_data()
3946 {
3947     ioPutToFileFromFile_data();
3948 }
3949
3950 void tst_QNetworkReply::ioPutToFtpFromFile()
3951 {
3952     QFETCH(QString, fileName);
3953     QFile sourceFile(fileName);
3954     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
3955
3956     QUrl url("ftp://" + QtNetworkSettings::serverName());
3957     url.setPath(QString("/qtest/upload/qnetworkaccess-ioPutToFtpFromFile-%1-%2")
3958                 .arg(QTest::currentDataTag())
3959                 .arg(uniqueExtension));
3960
3961     QNetworkRequest request(url);
3962     QNetworkReplyPtr reply(manager.put(request, &sourceFile));
3963
3964     QVERIFY(waitForFinish(reply) == Success);
3965
3966     QCOMPARE(reply->url(), url);
3967     QCOMPARE(reply->error(), QNetworkReply::NoError);
3968     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
3969     QVERIFY(reply->readAll().isEmpty());
3970
3971     QVERIFY(sourceFile.atEnd());
3972     sourceFile.seek(0);         // reset it to the beginning
3973
3974     // download the file again from FTP to make sure it was uploaded
3975     // correctly
3976     QNetworkAccessManager qnam;
3977     QNetworkRequest req(url);
3978     QNetworkReply *r = qnam.get(req);
3979
3980     QObject::connect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
3981     QTestEventLoop::instance().enterLoop(3);
3982     QObject::disconnect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
3983
3984     QByteArray uploaded = r->readAll();
3985     QCOMPARE(qint64(uploaded.size()), sourceFile.size());
3986     QCOMPARE(uploaded, sourceFile.readAll());
3987
3988     r->close();
3989     QObject::connect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
3990     QTestEventLoop::instance().enterLoop(10);
3991     QObject::disconnect(r, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
3992 }
3993
3994 void tst_QNetworkReply::ioPutToHttpFromFile_data()
3995 {
3996     ioPutToFileFromFile_data();
3997 }
3998
3999 void tst_QNetworkReply::ioPutToHttpFromFile()
4000 {
4001     QFETCH(QString, fileName);
4002     QFile sourceFile(fileName);
4003     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
4004
4005     QUrl url("http://" + QtNetworkSettings::serverName());
4006     url.setPath(QString("/dav/qnetworkaccess-ioPutToHttpFromFile-%1-%2")
4007                 .arg(QTest::currentDataTag())
4008                 .arg(uniqueExtension));
4009
4010     QNetworkRequest request(url);
4011     QNetworkReplyPtr reply(manager.put(request, &sourceFile));
4012
4013     QVERIFY(waitForFinish(reply) == Success);
4014
4015     QCOMPARE(reply->url(), url);
4016     QCOMPARE(reply->error(), QNetworkReply::NoError);
4017
4018     // verify that the HTTP status code is 201 Created
4019     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 201);
4020
4021     QVERIFY(sourceFile.atEnd());
4022     sourceFile.seek(0);         // reset it to the beginning
4023
4024     // download the file again from HTTP to make sure it was uploaded
4025     // correctly
4026     reply.reset(manager.get(request));
4027
4028     QVERIFY(waitForFinish(reply) == Success);
4029
4030     QCOMPARE(reply->url(), url);
4031     QCOMPARE(reply->error(), QNetworkReply::NoError);
4032     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
4033
4034     QCOMPARE(reply->readAll(), sourceFile.readAll());
4035 }
4036
4037 void tst_QNetworkReply::ioPostToHttpFromFile_data()
4038 {
4039     ioPutToFileFromFile_data();
4040 }
4041
4042 void tst_QNetworkReply::ioPostToHttpFromFile()
4043 {
4044     QFETCH(QString, fileName);
4045     QFile sourceFile(fileName);
4046     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
4047
4048     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi");
4049     QNetworkRequest request(url);
4050     request.setRawHeader("Content-Type", "application/octet-stream");
4051
4052     QNetworkReplyPtr reply(manager.post(request, &sourceFile));
4053
4054     QVERIFY(waitForFinish(reply) == Success);
4055
4056     QCOMPARE(reply->url(), url);
4057     QCOMPARE(reply->error(), QNetworkReply::NoError);
4058
4059     // verify that the HTTP status code is 200 Ok
4060     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
4061
4062     QVERIFY(sourceFile.atEnd());
4063     sourceFile.seek(0);         // reset it to the beginning
4064
4065     QCOMPARE(reply->readAll().trimmed(), md5sum(sourceFile.readAll()).toHex());
4066 }
4067
4068 void tst_QNetworkReply::ioPostToHttpFromSocket_data()
4069 {
4070     QTest::addColumn<QByteArray>("data");
4071     QTest::addColumn<QByteArray>("md5sum");
4072     QTest::addColumn<QUrl>("url");
4073     QTest::addColumn<QNetworkProxy>("proxy");
4074     QTest::addColumn<int>("authenticationRequiredCount");
4075     QTest::addColumn<int>("proxyAuthenticationRequiredCount");
4076
4077     for (int i = 0; i < proxies.count(); ++i)
4078         for (int auth = 0; auth < 2; ++auth) {
4079             QUrl url;
4080             if (auth)
4081                 url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi";
4082             else
4083                 url = "http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi";
4084
4085             QNetworkProxy proxy = proxies.at(i).proxy;
4086             QByteArray testsuffix = QByteArray(auth ? "+auth" : "") + proxies.at(i).tag;
4087             int proxyauthcount = proxies.at(i).requiresAuthentication;
4088
4089             QByteArray data;
4090             data = "";
4091             QTest::newRow("empty" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount;
4092
4093             data = "This is a normal message.";
4094             QTest::newRow("generic" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount;
4095
4096             data = "This is a message to show that Qt rocks!\r\n\n";
4097             QTest::newRow("small" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount;
4098
4099             data = QByteArray("abcd\0\1\2\abcd",12);
4100             QTest::newRow("with-nul" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount;
4101
4102             data = QByteArray(4097, '\4');
4103             QTest::newRow("4k+1" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount;
4104
4105             data = QByteArray(128*1024+1, '\177');
4106             QTest::newRow("128k+1" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount;
4107         }
4108 }
4109
4110 void tst_QNetworkReply::ioPostToHttpFromSocket()
4111 {
4112     QFETCH(QByteArray, data);
4113     QFETCH(QUrl, url);
4114     QFETCH(QNetworkProxy, proxy);
4115 #ifdef Q_OS_WIN
4116     //QTBUG-25386 hits one of the 128k tests 50% of the time, one of the 4k tests rarely (but at least 1%)
4117     if (data.size() > 1000)
4118         QSKIP("unstable on windows - QTBUG-25386");
4119 #endif
4120     SocketPair socketpair;
4121     QTRY_VERIFY(socketpair.create()); //QTRY_VERIFY as a workaround for QTBUG-24451
4122
4123     socketpair.endPoints[0]->write(data);
4124
4125     QNetworkRequest request(url);
4126     request.setRawHeader("Content-Type", "application/octet-stream");
4127
4128     manager.setProxy(proxy);
4129     QNetworkReplyPtr reply(manager.post(request, socketpair.endPoints[1]));
4130     socketpair.endPoints[0]->close();
4131
4132     connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
4133             SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
4134     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4135             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4136
4137     QSignalSpy authenticationRequiredSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4138     QSignalSpy proxyAuthenticationRequiredSpy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
4139
4140     QVERIFY(waitForFinish(reply) == Success);
4141
4142     disconnect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
4143                this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
4144     disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4145                this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4146     QCOMPARE(reply->error(), QNetworkReply::NoError);
4147
4148     QCOMPARE(reply->url(), url);
4149     QCOMPARE(reply->error(), QNetworkReply::NoError);
4150     // verify that the HTTP status code is 200 Ok
4151     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
4152
4153     QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex());
4154
4155     QTEST(authenticationRequiredSpy.count(), "authenticationRequiredCount");
4156     QTEST(proxyAuthenticationRequiredSpy.count(), "proxyAuthenticationRequiredCount");
4157 }
4158
4159 void tst_QNetworkReply::ioPostToHttpFromSocketSynchronous_data()
4160 {
4161     QTest::addColumn<QByteArray>("data");
4162     QTest::addColumn<QByteArray>("md5sum");
4163
4164     QByteArray data;
4165     data = "";
4166     QTest::newRow("empty") << data << md5sum(data);
4167
4168     data = "This is a normal message.";
4169     QTest::newRow("generic") << data << md5sum(data);
4170
4171     data = "This is a message to show that Qt rocks!\r\n\n";
4172     QTest::newRow("small") << data << md5sum(data);
4173
4174     data = QByteArray("abcd\0\1\2\abcd",12);
4175     QTest::newRow("with-nul") << data << md5sum(data);
4176
4177     data = QByteArray(4097, '\4');
4178     QTest::newRow("4k+1") << data << md5sum(data);
4179
4180     data = QByteArray(128*1024+1, '\177');
4181     QTest::newRow("128k+1") << data << md5sum(data);
4182
4183     data = QByteArray(2*1024*1024+1, '\177');
4184     QTest::newRow("2MB+1") << data << md5sum(data);
4185 }
4186
4187 void tst_QNetworkReply::ioPostToHttpFromSocketSynchronous()
4188 {
4189     QFETCH(QByteArray, data);
4190
4191     SocketPair socketpair;
4192     QTRY_VERIFY(socketpair.create()); //QTRY_VERIFY as a workaround for QTBUG-24451
4193     socketpair.endPoints[0]->write(data);
4194     socketpair.endPoints[0]->waitForBytesWritten(5000);
4195     // ### for 4.8: make the socket pair unbuffered, to not read everything in one go in QNetworkReplyImplPrivate::setup()
4196     QTestEventLoop::instance().enterLoop(3);
4197
4198     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi");
4199     QNetworkRequest request(url);
4200     request.setRawHeader("Content-Type", "application/octet-stream");
4201     request.setAttribute(
4202             QNetworkRequest::SynchronousRequestAttribute,
4203             true);
4204
4205     QNetworkReplyPtr reply(manager.post(request, socketpair.endPoints[1]));
4206     QVERIFY(reply->isFinished());
4207     socketpair.endPoints[0]->close();
4208
4209     QCOMPARE(reply->error(), QNetworkReply::NoError);
4210
4211     QCOMPARE(reply->url(), url);
4212     QCOMPARE(reply->error(), QNetworkReply::NoError);
4213     // verify that the HTTP status code is 200 Ok
4214     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
4215
4216     QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex());
4217 }
4218
4219 // this tests checks if rewinding the POST-data to some place in the middle
4220 // worked.
4221 void tst_QNetworkReply::ioPostToHttpFromMiddleOfFileToEnd()
4222 {
4223     QFile sourceFile(testDataDir + "/rfc3252.txt");
4224     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
4225     // seeking to the middle
4226     sourceFile.seek(sourceFile.size() / 2);
4227
4228     QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi";
4229     QNetworkRequest request(url);
4230     request.setRawHeader("Content-Type", "application/octet-stream");
4231     QNetworkReplyPtr reply(manager.post(request, &sourceFile));
4232
4233     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4234             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4235
4236     QVERIFY(waitForFinish(reply) == Success);
4237
4238     disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4239                this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4240
4241     // compare half data
4242     sourceFile.seek(sourceFile.size() / 2);
4243     QByteArray data = sourceFile.readAll();
4244     QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex());
4245 }
4246
4247 void tst_QNetworkReply::ioPostToHttpFromMiddleOfFileFiveBytes()
4248 {
4249     QFile sourceFile(testDataDir + "/rfc3252.txt");
4250     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
4251     // seeking to the middle
4252     sourceFile.seek(sourceFile.size() / 2);
4253
4254     QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi";
4255     QNetworkRequest request(url);
4256     request.setRawHeader("Content-Type", "application/octet-stream");
4257     // only send 5 bytes
4258     request.setHeader(QNetworkRequest::ContentLengthHeader, 5);
4259     QVERIFY(request.header(QNetworkRequest::ContentLengthHeader).isValid());
4260     QNetworkReplyPtr reply(manager.post(request, &sourceFile));
4261
4262     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4263             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4264
4265     QVERIFY(waitForFinish(reply) == Success);
4266
4267     disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4268                this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4269
4270     // compare half data
4271     sourceFile.seek(sourceFile.size() / 2);
4272     QByteArray data = sourceFile.read(5);
4273     QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex());
4274 }
4275
4276 void tst_QNetworkReply::ioPostToHttpFromMiddleOfQBufferFiveBytes()
4277 {
4278     // test needed since a QBuffer goes with a different codepath than the QFile
4279     // tested in ioPostToHttpFromMiddleOfFileFiveBytes
4280     QBuffer uploadBuffer;
4281     uploadBuffer.open(QIODevice::ReadWrite);
4282     uploadBuffer.write("1234567890");
4283     uploadBuffer.seek(5);
4284
4285     QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi";
4286     QNetworkRequest request(url);
4287     request.setRawHeader("Content-Type", "application/octet-stream");
4288     QNetworkReplyPtr reply(manager.post(request, &uploadBuffer));
4289
4290     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4291             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4292
4293     QVERIFY(waitForFinish(reply) == Success);
4294
4295     disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4296                this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4297
4298     // compare half data
4299     uploadBuffer.seek(5);
4300     QByteArray data = uploadBuffer.read(5);
4301     QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex());
4302 }
4303
4304
4305 void tst_QNetworkReply::ioPostToHttpNoBufferFlag()
4306 {
4307     QByteArray data = QByteArray("daaaaaaataaaaaaa");
4308     // create a sequential QIODevice by feeding the data into a local TCP server
4309     SocketPair socketpair;
4310     QTRY_VERIFY(socketpair.create()); //QTRY_VERIFY as a workaround for QTBUG-24451
4311     socketpair.endPoints[0]->write(data);
4312
4313     QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi";
4314     QNetworkRequest request(url);
4315     request.setRawHeader("Content-Type", "application/octet-stream");
4316     // disallow buffering
4317     request.setAttribute(QNetworkRequest::DoNotBufferUploadDataAttribute, true);
4318     request.setHeader(QNetworkRequest::ContentLengthHeader, data.size());
4319     QNetworkReplyPtr reply(manager.post(request, socketpair.endPoints[1]));
4320     socketpair.endPoints[0]->close();
4321
4322     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4323             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4324
4325     QVERIFY(waitForFinish(reply) == Failure);
4326
4327     disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
4328                this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
4329
4330     // verify: error code is QNetworkReply::ContentReSendError
4331     QCOMPARE(reply->error(), QNetworkReply::ContentReSendError);
4332 }
4333
4334 #ifndef QT_NO_SSL
4335 class SslServer : public QTcpServer {
4336     Q_OBJECT
4337 public:
4338     SslServer() : socket(0) {};
4339     void incomingConnection(qintptr socketDescriptor) {
4340         QSslSocket *serverSocket = new QSslSocket;
4341         serverSocket->setParent(this);
4342
4343         if (serverSocket->setSocketDescriptor(socketDescriptor)) {
4344             QString testDataDir = QFileInfo(QFINDTESTDATA("rfc3252.txt")).absolutePath();
4345             if (testDataDir.isEmpty())
4346                 testDataDir = QCoreApplication::applicationDirPath();
4347
4348             connect(serverSocket, SIGNAL(encrypted()), this, SLOT(encryptedSlot()));
4349             connect(serverSocket, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
4350             serverSocket->setProtocol(QSsl::AnyProtocol);
4351             connect(serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), serverSocket, SLOT(ignoreSslErrors()));
4352             serverSocket->setLocalCertificate(testDataDir + "/certs/server.pem");
4353             serverSocket->setPrivateKey(testDataDir + "/certs/server.key");
4354             serverSocket->startServerEncryption();
4355         } else {
4356             delete serverSocket;
4357         }
4358     }
4359 signals:
4360     void newEncryptedConnection();
4361 public slots:
4362     void encryptedSlot() {
4363         socket = (QSslSocket*) sender();
4364         emit newEncryptedConnection();
4365     }
4366     void readyReadSlot() {
4367         // for the incoming sockets, not the server socket
4368         //qDebug() << static_cast<QSslSocket*>(sender())->bytesAvailable() << static_cast<QSslSocket*>(sender())->encryptedBytesAvailable();
4369     }
4370
4371 public:
4372     QSslSocket *socket;
4373 };
4374
4375 // very similar to ioPostToHttpUploadProgress but for SSL
4376 void tst_QNetworkReply::ioPostToHttpsUploadProgress()
4377 {
4378     //QFile sourceFile(testDataDir + "/bigfile");
4379     //QVERIFY(sourceFile.open(QIODevice::ReadOnly));
4380     qint64 wantedSize = 2*1024*1024; // 2 MB
4381     QByteArray sourceFile;
4382     // And in the case of SSL, the compression can fool us and let the
4383     // server send the data much faster than expected.
4384     // So better provide random data that cannot be compressed.
4385     for (int i = 0; i < wantedSize; ++i)
4386         sourceFile += (char)qrand();
4387
4388     // emulate a minimal https server
4389     SslServer server;
4390     server.listen(QHostAddress(QHostAddress::LocalHost), 0);
4391
4392     // create the request
4393     QUrl url = QUrl(QString("https://127.0.0.1:%1/").arg(server.serverPort()));
4394     QNetworkRequest request(url);
4395
4396     request.setRawHeader("Content-Type", "application/octet-stream");
4397     QNetworkReplyPtr reply(manager.post(request, sourceFile));
4398
4399     QSignalSpy spy(reply.data(), SIGNAL(uploadProgress(qint64,qint64)));
4400     connect(&server, SIGNAL(newEncryptedConnection()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4401     connect(reply, SIGNAL(sslErrors(const QList<QSslError>&)), reply.data(), SLOT(ignoreSslErrors()));
4402
4403     // get the request started and the incoming socket connected
4404     QTestEventLoop::instance().enterLoop(10);
4405     QVERIFY(!QTestEventLoop::instance().timeout());
4406     QTcpSocket *incomingSocket = server.socket;
4407     QVERIFY(incomingSocket);
4408     disconnect(&server, SIGNAL(newEncryptedConnection()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4409
4410
4411     incomingSocket->setReadBufferSize(1*1024);
4412     // some progress should have been made
4413     QTRY_VERIFY(!spy.isEmpty());
4414     QList<QVariant> args = spy.last();
4415     QVERIFY(args.at(0).toLongLong() > 0);
4416     // but not everything!
4417     QVERIFY(args.at(0).toLongLong() != sourceFile.size());
4418
4419     // set the read buffer to unlimited
4420     incomingSocket->setReadBufferSize(0);
4421     QTestEventLoop::instance().enterLoop(10);
4422     // progress should be finished
4423     QVERIFY(!spy.isEmpty());
4424     QList<QVariant> args3 = spy.last();
4425     QCOMPARE(args3.at(0).toLongLong(), args3.at(1).toLongLong());
4426     QCOMPARE(args3.at(0).toLongLong(), qint64(sourceFile.size()));
4427
4428     // after sending this, the QNAM should emit finished()
4429     incomingSocket->write("HTTP/1.0 200 OK\r\n");
4430     incomingSocket->write("Content-Length: 0\r\n");
4431     incomingSocket->write("\r\n");
4432
4433     QVERIFY(waitForFinish(reply) == Success);
4434
4435     incomingSocket->close();
4436     server.close();
4437 }
4438 #endif
4439
4440 void tst_QNetworkReply::ioGetFromBuiltinHttp_data()
4441 {
4442     QTest::addColumn<bool>("https");
4443     QTest::addColumn<int>("bufferSize");
4444     QTest::newRow("http+unlimited") << false << 0;
4445     QTest::newRow("http+limited") << false << 4096;
4446 #ifndef QT_NO_SSL
4447     QTest::newRow("https+unlimited") << true << 0;
4448     QTest::newRow("https+limited") << true << 4096;
4449 #endif
4450 }
4451
4452 void tst_QNetworkReply::ioGetFromBuiltinHttp()
4453 {
4454     QFETCH(bool, https);
4455     QFETCH(int, bufferSize);
4456
4457     QByteArray testData;
4458     // Make the data big enough so that it can fill the kernel buffer
4459     // (which seems to hold 202 KB here)
4460     const int wantedSize = 1200 * 1000;
4461     testData.reserve(wantedSize);
4462     // And in the case of SSL, the compression can fool us and let the
4463     // server send the data much faster than expected.
4464     // So better provide random data that cannot be compressed.
4465     for (int i = 0; i < wantedSize; ++i)
4466         testData += (char)qrand();
4467
4468     QByteArray httpResponse = QByteArray("HTTP/1.0 200 OK\r\nContent-Length: ");
4469     httpResponse += QByteArray::number(testData.size());
4470     httpResponse += "\r\n\r\n";
4471     httpResponse += testData;
4472
4473     qDebug() << "Server will send" << (httpResponse.size()-testData.size()) << "bytes of header and"
4474              << testData.size() << "bytes of data";
4475
4476     const bool fillKernelBuffer = bufferSize > 0;
4477     FastSender server(httpResponse, https, fillKernelBuffer);
4478
4479     QUrl url(QString("%1://127.0.0.1:%2/qtest/rfc3252.txt")
4480              .arg(https?"https":"http")
4481              .arg(server.serverPort()));
4482     QNetworkRequest request(url);
4483     QNetworkReplyPtr reply(manager.get(request));
4484     reply->setReadBufferSize(bufferSize);
4485     reply->ignoreSslErrors();
4486     const int rate = 200; // in kB per sec
4487     RateControlledReader reader(server, reply.data(), rate, bufferSize);
4488
4489     QTime loopTime;
4490     loopTime.start();
4491
4492     QVERIFY(waitForFinish(reply) == Success);
4493
4494     const int elapsedTime = loopTime.elapsed();
4495     server.wait();
4496     reader.wrapUp();
4497
4498     qDebug() << "send rate:" << server.transferRate << "B/s";
4499     qDebug() << "receive rate:" << reader.totalBytesRead * 1000 / elapsedTime
4500              << "(it received" << reader.totalBytesRead << "bytes in" << elapsedTime << "ms)";
4501
4502     QCOMPARE(reply->url(), request.url());
4503     QCOMPARE(reply->error(), QNetworkReply::NoError);
4504     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
4505
4506     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), (qint64)testData.size());
4507     if (reader.data.size() < testData.size()) { // oops?
4508         QCOMPARE(reader.data, testData.mid(0, reader.data.size()));
4509         qDebug() << "The data is incomplete, the last" << testData.size() - reader.data.size() << "bytes are missing";
4510     }
4511     QCOMPARE(reader.data.size(), testData.size());
4512     QCOMPARE(reader.data, testData);
4513
4514     // OK we got the file alright, but did setReadBufferSize work?
4515     QVERIFY(server.transferRate != -1);
4516     if (bufferSize > 0) {
4517         const int allowedDeviation = 16; // TODO find out why the send rate is 13% faster currently
4518         const int minRate = rate * 1024 * (100-allowedDeviation) / 100;
4519         const int maxRate = rate * 1024 * (100+allowedDeviation) / 100;
4520         qDebug() << minRate << "<="<< server.transferRate << "<=" << maxRate << "?";
4521         // The test takes too long to run if sending enough data to overwhelm the
4522         // reciever's kernel buffers.
4523         //QEXPECT_FAIL("http+limited", "Limiting is broken right now, check QTBUG-15065", Continue);
4524         //QEXPECT_FAIL("https+limited", "Limiting is broken right now, check QTBUG-15065", Continue);
4525         //QVERIFY(server.transferRate >= minRate && server.transferRate <= maxRate);
4526     }
4527 }
4528
4529 void tst_QNetworkReply::ioPostToHttpUploadProgress()
4530 {
4531     //test file must be larger than OS socket buffers (~830kB on MacOS 10.6)
4532     QFile sourceFile(testDataDir + "/image1.jpg");
4533     QVERIFY(sourceFile.open(QIODevice::ReadOnly));
4534
4535     // emulate a minimal http server
4536     QTcpServer server;
4537     server.listen(QHostAddress(QHostAddress::LocalHost), 0);
4538
4539     // create the request
4540     QUrl url = QUrl(QString("http://127.0.0.1:%1/").arg(server.serverPort()));
4541     QNetworkRequest request(url);
4542     request.setRawHeader("Content-Type", "application/octet-stream");
4543     QNetworkReplyPtr reply(manager.post(request, &sourceFile));
4544     QSignalSpy spy(reply.data(), SIGNAL(uploadProgress(qint64,qint64)));
4545     connect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4546
4547     // get the request started and the incoming socket connected
4548     QTestEventLoop::instance().enterLoop(10);
4549     QVERIFY(!QTestEventLoop::instance().timeout());
4550     QTcpSocket *incomingSocket = server.nextPendingConnection();
4551     QVERIFY(incomingSocket);
4552     disconnect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4553
4554     incomingSocket->setReadBufferSize(1*1024);
4555     QTestEventLoop::instance().enterLoop(5);
4556     // some progress should have been made
4557     QVERIFY(!spy.isEmpty());
4558     QList<QVariant> args = spy.last();
4559     QVERIFY(!args.isEmpty());
4560     QVERIFY(args.at(0).toLongLong() > 0);
4561     // but not everything!
4562     QVERIFY(args.at(0).toLongLong() != sourceFile.size());
4563
4564     // set the read buffer to unlimited
4565     incomingSocket->setReadBufferSize(0);
4566     QTestEventLoop::instance().enterLoop(10);
4567     // progress should be finished
4568     QVERIFY(!spy.isEmpty());
4569     QList<QVariant> args3 = spy.last();
4570     QVERIFY(!args3.isEmpty());
4571     // More progress than before
4572     QVERIFY(args3.at(0).toLongLong() > args.at(0).toLongLong());
4573     QCOMPARE(args3.at(0).toLongLong(), args3.at(1).toLongLong());
4574     // And actually finished..
4575     QCOMPARE(args3.at(0).toLongLong(), sourceFile.size());
4576
4577     // after sending this, the QNAM should emit finished()
4578     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4579     incomingSocket->write("HTTP/1.0 200 OK\r\n");
4580     incomingSocket->write("Content-Length: 0\r\n");
4581     incomingSocket->write("\r\n");
4582     QTestEventLoop::instance().enterLoop(10);
4583     // not timeouted -> finished() was emitted
4584     QVERIFY(!QTestEventLoop::instance().timeout());
4585
4586     incomingSocket->close();
4587     server.close();
4588 }
4589
4590 void tst_QNetworkReply::ioPostToHttpEmptyUploadProgress()
4591 {
4592     QByteArray ba;
4593     ba.resize(0);
4594     QBuffer buffer(&ba,0);
4595     QVERIFY(buffer.open(QIODevice::ReadOnly));
4596
4597     // emulate a minimal http server
4598     QTcpServer server;
4599     server.listen(QHostAddress(QHostAddress::LocalHost), 0);
4600
4601     // create the request
4602     QUrl url = QUrl(QString("http://127.0.0.1:%1/").arg(server.serverPort()));
4603     QNetworkRequest request(url);
4604     request.setRawHeader("Content-Type", "application/octet-stream");
4605     QNetworkReplyPtr reply(manager.post(request, &buffer));
4606     QSignalSpy spy(reply.data(), SIGNAL(uploadProgress(qint64,qint64)));
4607     connect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4608
4609
4610     // get the request started and the incoming socket connected
4611     QTestEventLoop::instance().enterLoop(10);
4612     QVERIFY(!QTestEventLoop::instance().timeout());
4613     QTcpSocket *incomingSocket = server.nextPendingConnection();
4614     QVERIFY(incomingSocket);
4615
4616     // after sending this, the QNAM should emit finished()
4617     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
4618     incomingSocket->write("HTTP/1.0 200 OK\r\n");
4619     incomingSocket->write("Content-Length: 0\r\n");
4620     incomingSocket->write("\r\n");
4621     incomingSocket->flush();
4622     QTestEventLoop::instance().enterLoop(10);
4623     // not timeouted -> finished() was emitted
4624     QVERIFY(!QTestEventLoop::instance().timeout());
4625
4626     // final check: only 1 uploadProgress has been emitted
4627     QVERIFY(spy.length() == 1);
4628     QList<QVariant> args = spy.last();
4629     QVERIFY(!args.isEmpty());
4630     QCOMPARE(args.at(0).toLongLong(), buffer.size());
4631     QCOMPARE(args.at(0).toLongLong(), buffer.size());
4632
4633     incomingSocket->close();
4634     server.close();
4635 }
4636
4637 void tst_QNetworkReply::lastModifiedHeaderForFile()
4638 {
4639     QFileInfo fileInfo(testDataDir + "/bigfile");
4640     QVERIFY(fileInfo.exists());
4641
4642     QUrl url = QUrl::fromLocalFile(fileInfo.filePath());
4643
4644     QNetworkRequest request(url);
4645     QNetworkReplyPtr reply(manager.head(request));
4646
4647     QVERIFY(waitForFinish(reply) == Success);
4648
4649     QDateTime header = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime();
4650     QCOMPARE(header, fileInfo.lastModified());
4651 }
4652
4653 void tst_QNetworkReply::lastModifiedHeaderForHttp()
4654 {
4655     // Tue, 22 May 2007 12:04:57 GMT according to webserver
4656     QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/fluke.gif";
4657
4658     QNetworkRequest request(url);
4659     QNetworkReplyPtr reply(manager.head(request));
4660
4661     QVERIFY(waitForFinish(reply) == Success);
4662
4663     QDateTime header = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime();
4664     QDateTime realDate = QDateTime::fromString("2007-05-22T12:04:57", Qt::ISODate);
4665     realDate.setTimeSpec(Qt::UTC);
4666
4667     QCOMPARE(header, realDate);
4668 }
4669
4670 void tst_QNetworkReply::httpCanReadLine()
4671 {
4672     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"));
4673     QNetworkReplyPtr reply(manager.get(request));
4674
4675     QVERIFY(waitForFinish(reply) == Success);
4676
4677     QCOMPARE(reply->error(), QNetworkReply::NoError);
4678
4679     QVERIFY(reply->canReadLine());
4680     QVERIFY(!reply->readAll().isEmpty());
4681     QVERIFY(!reply->canReadLine());
4682 }
4683
4684 void tst_QNetworkReply::rateControl_data()
4685 {
4686     QTest::addColumn<int>("rate");
4687
4688     QTest::newRow("15") << 15;
4689     QTest::newRow("40") << 40;
4690     QTest::newRow("73") << 73;
4691     QTest::newRow("80") << 80;
4692     QTest::newRow("125") << 125;
4693     QTest::newRow("250") << 250;
4694     QTest::newRow("1024") << 1024;
4695 }
4696
4697 void tst_QNetworkReply::rateControl()
4698 {
4699     QSKIP("Test disabled -- only for manual purposes");
4700     // this function tests that we aren't reading from the network
4701     // faster than the data is being consumed.
4702     QFETCH(int, rate);
4703
4704 #if !defined(QT_BUILD_INTERNAL)
4705     QSKIP("backend for testing not available!");
4706 #endif
4707
4708     // ask for 20 seconds worth of data
4709     FastSender sender(20 * rate * 1024);
4710
4711     QNetworkRequest request("debugpipe://localhost:" + QString::number(sender.serverPort()));
4712     QNetworkReplyPtr reply(manager.get(request));
4713     reply->setReadBufferSize(32768);
4714     QSignalSpy errorSpy(reply.data(), SIGNAL(error(QNetworkReply::NetworkError)));
4715
4716     RateControlledReader reader(sender, reply.data(), rate, 20);
4717
4718     // this test is designed to run for 25 seconds at most
4719     QTime loopTime;
4720     loopTime.start();
4721
4722     QVERIFY(waitForFinish(reply) == Success);
4723
4724     int elapsedTime = loopTime.elapsed();
4725
4726     if (!errorSpy.isEmpty()) {
4727         qDebug() << "ERROR!" << errorSpy[0][0] << reply->errorString();
4728     }
4729
4730     qDebug() << "tst_QNetworkReply::rateControl" << "send rate:" << sender.transferRate;
4731     qDebug() << "tst_QNetworkReply::rateControl" << "receive rate:" << reader.totalBytesRead * 1000 / elapsedTime
4732              << "(it received" << reader.totalBytesRead << "bytes in" << elapsedTime << "ms)";
4733
4734     sender.wait();
4735
4736     QCOMPARE(reply->url(), request.url());
4737     QCOMPARE(reply->error(), QNetworkReply::NoError);
4738
4739     QVERIFY(sender.transferRate != -1);
4740     int minRate = rate * 1024 * 9 / 10;
4741     int maxRate = rate * 1024 * 11 / 10;
4742     QVERIFY(sender.transferRate >= minRate);
4743     QVERIFY(sender.transferRate <= maxRate);
4744 }
4745
4746 void tst_QNetworkReply::downloadProgress_data()
4747 {
4748     QTest::addColumn<int>("loopCount");
4749
4750     QTest::newRow("empty") << 0;
4751     QTest::newRow("small") << 4;
4752     QTest::newRow("big") << 4096;
4753 }
4754
4755 void tst_QNetworkReply::downloadProgress()
4756 {
4757 #if !defined(QT_BUILD_INTERNAL)
4758     QSKIP("backend for testing not available!");
4759 #endif
4760     QTcpServer server;
4761     QVERIFY(server.listen());
4762
4763     QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(server.serverPort()) + "/?bare=1");
4764     QNetworkReplyPtr reply(manager.get(request));
4765     QSignalSpy spy(reply.data(), SIGNAL(downloadProgress(qint64,qint64)));
4766     connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
4767             &QTestEventLoop::instance(), SLOT(exitLoop()));
4768     QVERIFY(spy.isValid());
4769     QVERIFY(!reply->isFinished());
4770     QVERIFY(reply->isRunning());
4771
4772     QCoreApplication::instance()->processEvents();
4773     if (!server.hasPendingConnections())
4774         server.waitForNewConnection(1000);
4775     QVERIFY(server.hasPendingConnections());
4776     QCOMPARE(spy.count(), 0);
4777
4778     QByteArray data(128, 'a');
4779     QTcpSocket *sender = server.nextPendingConnection();
4780     QVERIFY(sender);
4781
4782     QFETCH(int, loopCount);
4783     for (int i = 1; i <= loopCount; ++i) {
4784         sender->write(data);
4785         QVERIFY2(sender->waitForBytesWritten(2000), "Network timeout");
4786
4787         spy.clear();
4788         QTestEventLoop::instance().enterLoop(2);
4789         QVERIFY(!QTestEventLoop::instance().timeout());
4790         QVERIFY(spy.count() > 0);
4791         QVERIFY(!reply->isFinished());
4792         QVERIFY(reply->isRunning());
4793
4794         QList<QVariant> args = spy.last();
4795         QCOMPARE(args.at(0).toInt(), i*data.size());
4796         QCOMPARE(args.at(1).toInt(), -1);
4797     }
4798
4799     // close the connection:
4800     delete sender;
4801
4802     spy.clear();
4803     QTestEventLoop::instance().enterLoop(2);
4804     QCOMPARE(reply->error(), QNetworkReply::NoError);
4805     QVERIFY(!QTestEventLoop::instance().timeout());
4806     QVERIFY(spy.count() > 0);
4807     QVERIFY(!reply->isRunning());
4808     QVERIFY(reply->isFinished());
4809
4810     QList<QVariant> args = spy.last();
4811     QCOMPARE(args.at(0).toInt(), loopCount * data.size());
4812     QCOMPARE(args.at(1).toInt(), loopCount * data.size());
4813 }
4814
4815 void tst_QNetworkReply::uploadProgress_data()
4816 {
4817     putToFile_data();
4818 }
4819
4820 void tst_QNetworkReply::uploadProgress()
4821 {
4822     QFETCH(QByteArray, data);
4823 #if !defined(QT_BUILD_INTERNAL)
4824     QSKIP("backend for testing not available!");
4825 #endif
4826     QTcpServer server;
4827     QVERIFY(server.listen());
4828
4829     QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(server.serverPort()) + "/?bare=1");
4830     QNetworkReplyPtr reply(manager.put(request, data));
4831     QSignalSpy spy(reply.data(), SIGNAL(uploadProgress(qint64,qint64)));
4832     QSignalSpy finished(reply.data(), SIGNAL(finished()));
4833     QVERIFY(spy.isValid());
4834     QVERIFY(finished.isValid());
4835
4836     QCoreApplication::instance()->processEvents();
4837     if (!server.hasPendingConnections())
4838         server.waitForNewConnection(1000);
4839     QVERIFY(server.hasPendingConnections());
4840
4841     QTcpSocket *receiver = server.nextPendingConnection();
4842     if (finished.count() == 0) {
4843         // it's not finished yet, so wait for it to be
4844         QVERIFY(waitForFinish(reply) == Success);
4845     }
4846     delete receiver;
4847
4848     QVERIFY(finished.count() > 0);
4849     QVERIFY(spy.count() > 0);
4850
4851     QList<QVariant> args = spy.last();
4852     QCOMPARE(args.at(0).toInt(), data.size());
4853     QCOMPARE(args.at(1).toInt(), data.size());
4854 }
4855
4856 void tst_QNetworkReply::chaining_data()
4857 {
4858     putToFile_data();
4859 }
4860
4861 void tst_QNetworkReply::chaining()
4862 {
4863     QTemporaryFile sourceFile(QDir::currentPath() + "/temp-XXXXXX");
4864     sourceFile.setAutoRemove(true);
4865     QVERIFY(sourceFile.open());
4866
4867     QFETCH(QByteArray, data);
4868     QVERIFY(sourceFile.write(data) == data.size());
4869     sourceFile.flush();
4870     QCOMPARE(sourceFile.size(), qint64(data.size()));
4871
4872     QNetworkRequest request(QUrl::fromLocalFile(sourceFile.fileName()));
4873     QNetworkReplyPtr getReply(manager.get(request));
4874
4875     QFile targetFile(testFileName);
4876     QUrl url = QUrl::fromLocalFile(targetFile.fileName());
4877     request.setUrl(url);
4878     QNetworkReplyPtr putReply(manager.put(request, getReply.data()));
4879
4880     QVERIFY(waitForFinish(putReply) == Success);
4881
4882     QCOMPARE(getReply->url(), QUrl::fromLocalFile(sourceFile.fileName()));
4883     QCOMPARE(getReply->error(), QNetworkReply::NoError);
4884     QCOMPARE(getReply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), sourceFile.size());
4885
4886     QCOMPARE(putReply->url(), url);
4887     QCOMPARE(putReply->error(), QNetworkReply::NoError);
4888     QCOMPARE(putReply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), Q_INT64_C(0));
4889     QVERIFY(putReply->readAll().isEmpty());
4890
4891     QVERIFY(sourceFile.atEnd());
4892     sourceFile.seek(0);         // reset it to the beginning
4893
4894     QVERIFY(targetFile.open(QIODevice::ReadOnly));
4895     QCOMPARE(targetFile.size(), sourceFile.size());
4896     QCOMPARE(targetFile.readAll(), sourceFile.readAll());
4897 }
4898
4899 void tst_QNetworkReply::receiveCookiesFromHttp_data()
4900 {
4901     QTest::addColumn<QString>("cookieString");
4902     QTest::addColumn<QList<QNetworkCookie> >("expectedCookiesFromHttp");
4903     QTest::addColumn<QList<QNetworkCookie> >("expectedCookiesInJar");
4904
4905     QTest::newRow("empty") << "" << QList<QNetworkCookie>() << QList<QNetworkCookie>();
4906
4907     QList<QNetworkCookie> header, jar;
4908     QNetworkCookie cookie("a", "b");
4909     header << cookie;
4910     cookie.setDomain(QtNetworkSettings::serverName());
4911     cookie.setPath("/qtest/cgi-bin/");
4912     jar << cookie;
4913     QTest::newRow("simple-cookie") << "a=b" << header << jar;
4914
4915     header << QNetworkCookie("c", "d");
4916     cookie.setName("c");
4917     cookie.setValue("d");
4918     jar << cookie;
4919     QTest::newRow("two-cookies") << "a=b, c=d" << header << jar;
4920     QTest::newRow("two-cookies-2") << "a=b\nc=d" << header << jar;
4921
4922     header.clear();
4923     jar.clear();
4924     cookie = QNetworkCookie("a", "b");
4925     cookie.setPath("/not/part-of-path");
4926     header << cookie;
4927     cookie.setDomain(QtNetworkSettings::serverName());
4928     jar << cookie;
4929     QTest::newRow("invalid-cookie-path") << "a=b; path=/not/part-of-path" << header << jar;
4930
4931     jar.clear();
4932     cookie = QNetworkCookie("a", "b");
4933     cookie.setDomain(".example.com");
4934     header.clear();
4935     header << cookie;
4936     QTest::newRow("invalid-cookie-domain") << "a=b; domain=.example.com" << header << jar;
4937 }
4938
4939 void tst_QNetworkReply::receiveCookiesFromHttp()
4940 {
4941     QFETCH(QString, cookieString);
4942
4943     QByteArray data = cookieString.toLatin1() + '\n';
4944     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/set-cookie.cgi");
4945     QNetworkRequest request(url);
4946     request.setRawHeader("Content-Type", "application/octet-stream");
4947     QNetworkReplyPtr reply;
4948     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PostOperation, request, reply, data));
4949
4950     QCOMPARE(reply->url(), url);
4951     QCOMPARE(reply->error(), QNetworkReply::NoError);
4952
4953     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
4954
4955     QList<QNetworkCookie> setCookies =
4956         qvariant_cast<QList<QNetworkCookie> >(reply->header(QNetworkRequest::SetCookieHeader));
4957     QTEST(setCookies, "expectedCookiesFromHttp");
4958     QTEST(cookieJar->allCookies(), "expectedCookiesInJar");
4959 }
4960
4961 void tst_QNetworkReply::receiveCookiesFromHttpSynchronous_data()
4962 {
4963     tst_QNetworkReply::receiveCookiesFromHttp_data();
4964 }
4965
4966 void tst_QNetworkReply::receiveCookiesFromHttpSynchronous()
4967 {
4968     QFETCH(QString, cookieString);
4969
4970     QByteArray data = cookieString.toLatin1() + '\n';
4971     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/set-cookie.cgi");
4972
4973     QNetworkRequest request(url);
4974     request.setRawHeader("Content-Type", "application/octet-stream");
4975     request.setAttribute(
4976             QNetworkRequest::SynchronousRequestAttribute,
4977             true);
4978
4979     QNetworkReplyPtr reply;
4980     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PostOperation, request, reply, data));
4981
4982     QCOMPARE(reply->url(), url);
4983     QCOMPARE(reply->error(), QNetworkReply::NoError);
4984
4985     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
4986
4987     QList<QNetworkCookie> setCookies =
4988             qvariant_cast<QList<QNetworkCookie> >(reply->header(QNetworkRequest::SetCookieHeader));
4989     QTEST(setCookies, "expectedCookiesFromHttp");
4990     QTEST(cookieJar->allCookies(), "expectedCookiesInJar");
4991 }
4992
4993 void tst_QNetworkReply::sendCookies_data()
4994 {
4995     QTest::addColumn<QList<QNetworkCookie> >("cookiesToSet");
4996     QTest::addColumn<QString>("expectedCookieString");
4997
4998     QList<QNetworkCookie> list;
4999     QTest::newRow("empty") << list << "";
5000
5001     QNetworkCookie cookie("a", "b");
5002     cookie.setPath("/");
5003     cookie.setDomain("example.com");
5004     list << cookie;
5005     QTest::newRow("no-match-domain") << list << "";
5006
5007     cookie.setDomain(QtNetworkSettings::serverName());
5008     cookie.setPath("/something/else");
5009     list << cookie;
5010     QTest::newRow("no-match-path") << list << "";
5011
5012     cookie.setPath("/");
5013     list << cookie;
5014     QTest::newRow("simple-cookie") << list << "a=b";
5015
5016     cookie.setPath("/qtest");
5017     cookie.setValue("longer");
5018     list << cookie;
5019     QTest::newRow("two-cookies") << list << "a=longer; a=b";
5020
5021     list.clear();
5022     cookie = QNetworkCookie("a", "b");
5023     cookie.setPath("/");
5024     cookie.setDomain("." + QtNetworkSettings::serverDomainName());
5025     list << cookie;
5026     QTest::newRow("domain-match") << list << "a=b";
5027
5028     // but it shouldn't match this:
5029     cookie.setDomain(QtNetworkSettings::serverDomainName());
5030     list << cookie;
5031     QTest::newRow("domain-match-2") << list << "a=b";
5032 }
5033
5034 void tst_QNetworkReply::sendCookies()
5035 {
5036     QFETCH(QString, expectedCookieString);
5037     QFETCH(QList<QNetworkCookie>, cookiesToSet);
5038     cookieJar->setAllCookies(cookiesToSet);
5039
5040     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/get-cookie.cgi");
5041     QNetworkRequest request(url);
5042     QNetworkReplyPtr reply;
5043     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
5044
5045     QCOMPARE(reply->url(), url);
5046     QCOMPARE(reply->error(), QNetworkReply::NoError);
5047
5048     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
5049
5050     QCOMPARE(QString::fromLatin1(reply->readAll()).trimmed(), expectedCookieString);
5051 }
5052
5053 void tst_QNetworkReply::sendCookiesSynchronous_data()
5054 {
5055     tst_QNetworkReply::sendCookies_data();
5056 }
5057
5058 void tst_QNetworkReply::sendCookiesSynchronous()
5059 {
5060     QFETCH(QString, expectedCookieString);
5061     QFETCH(QList<QNetworkCookie>, cookiesToSet);
5062     cookieJar->setAllCookies(cookiesToSet);
5063
5064     QUrl url("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/get-cookie.cgi");
5065     QNetworkRequest request(url);
5066
5067     request.setAttribute(
5068             QNetworkRequest::SynchronousRequestAttribute,
5069             true);
5070
5071     QNetworkReplyPtr reply;
5072     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply));
5073
5074     QCOMPARE(reply->url(), url);
5075     QCOMPARE(reply->error(), QNetworkReply::NoError);
5076
5077     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); // 200 Ok
5078
5079     QCOMPARE(QString::fromLatin1(reply->readAll()).trimmed(), expectedCookieString);
5080 }
5081
5082 void tst_QNetworkReply::nestedEventLoops_slot()
5083 {
5084     QEventLoop subloop;
5085
5086     // 16 seconds: fluke times out in 15 seconds, which triggers a QTcpSocket error
5087     QTimer::singleShot(16000, &subloop, SLOT(quit()));
5088     subloop.exec();
5089
5090     QTestEventLoop::instance().exitLoop();
5091 }
5092
5093 void tst_QNetworkReply::nestedEventLoops()
5094 {
5095     // Slightly fragile test, it may not be testing anything
5096     // This is certifying that we're not running into the same issue
5097     // that QHttp had (task 200432): the QTcpSocket connection is
5098     // closed by the remote end because of the kept-alive HTTP
5099     // connection timed out.
5100     //
5101     // The exact time required for this to happen is not exactly
5102     // defined. Our server (Apache httpd) times out after 15
5103     // seconds. (see above)
5104
5105     qDebug("Takes 16 seconds to run, please wait");
5106
5107     QUrl url("http://" + QtNetworkSettings::serverName());
5108     QNetworkRequest request(url);
5109     QNetworkReplyPtr reply(manager.get(request));
5110
5111     QSignalSpy finishedspy(reply.data(), SIGNAL(finished()));
5112     QSignalSpy errorspy(reply.data(), SIGNAL(error(QNetworkReply::NetworkError)));
5113
5114     connect(reply, SIGNAL(finished()), SLOT(nestedEventLoops_slot()));
5115     QTestEventLoop::instance().enterLoop(20);
5116     QVERIFY2(!QTestEventLoop::instance().timeout(), "Network timeout");
5117
5118     QCOMPARE(finishedspy.count(), 1);
5119     QCOMPARE(errorspy.count(), 0);
5120 }
5121
5122 void tst_QNetworkReply::httpProxyCommands_data()
5123 {
5124     QTest::addColumn<QUrl>("url");
5125     QTest::addColumn<QByteArray>("responseToSend");
5126     QTest::addColumn<QString>("expectedCommand");
5127
5128     QTest::newRow("http")
5129         << QUrl("http://0.0.0.0:4443/http-request")
5130         << QByteArray("HTTP/1.0 200 OK\r\nProxy-Connection: close\r\nContent-Length: 1\r\n\r\n1")
5131         << "GET http://0.0.0.0:4443/http-request HTTP/1.";
5132 #ifndef QT_NO_SSL
5133     QTest::newRow("https")
5134         << QUrl("https://0.0.0.0:4443/https-request")
5135         << QByteArray("HTTP/1.0 200 Connection Established\r\n\r\n")
5136         << "CONNECT 0.0.0.0:4443 HTTP/1.";
5137 #endif
5138 }
5139
5140 void tst_QNetworkReply::httpProxyCommands()
5141 {
5142     QFETCH(QUrl, url);
5143     QFETCH(QByteArray, responseToSend);
5144     QFETCH(QString, expectedCommand);
5145
5146     MiniHttpServer proxyServer(responseToSend);
5147     QNetworkProxy proxy(QNetworkProxy::HttpProxy, "127.0.0.1", proxyServer.serverPort());
5148
5149     manager.setProxy(proxy);
5150     QNetworkRequest request(url);
5151     request.setRawHeader("User-Agent", "QNetworkReplyAutoTest/1.0");
5152     QNetworkReplyPtr reply(manager.get(request));
5153     //clearing the proxy here causes the test to fail.
5154     //the proxy isn't used until after the bearer has been started
5155     //which is correct in general, because system proxy isn't known until that time.
5156     //removing this line is safe, as the proxy is also reset by the cleanup() function
5157     //manager.setProxy(QNetworkProxy());
5158
5159     // wait for the finished signal
5160     QVERIFY(waitForFinish(reply) != Timeout);
5161
5162     //qDebug() << reply->error() << reply->errorString();
5163     //qDebug() << proxyServer.receivedData;
5164
5165     // we don't really care if the request succeeded
5166     // especially since it won't succeed in the HTTPS case
5167     // so just check that the command was correct
5168
5169     QString receivedHeader = proxyServer.receivedData.left(expectedCommand.length());
5170     QCOMPARE(receivedHeader, expectedCommand);
5171
5172     //QTBUG-17223 - make sure the user agent from the request is sent to proxy server even for CONNECT
5173     int uapos = proxyServer.receivedData.indexOf("User-Agent");
5174     int uaend = proxyServer.receivedData.indexOf("\r\n", uapos);
5175     QByteArray uaheader = proxyServer.receivedData.mid(uapos, uaend - uapos);
5176     QCOMPARE(uaheader, QByteArray("User-Agent: QNetworkReplyAutoTest/1.0"));
5177 }
5178
5179 class ProxyChangeHelper : public QObject {
5180     Q_OBJECT
5181 public:
5182     ProxyChangeHelper() : QObject(), signalCount(0) {};
5183 public slots:
5184     void finishedSlot() {
5185         signalCount++;
5186         if (signalCount == 2)
5187             QMetaObject::invokeMethod(&QTestEventLoop::instance(), "exitLoop", Qt::QueuedConnection);
5188     }
5189 private:
5190    int signalCount;
5191 };
5192
5193 void tst_QNetworkReply::httpProxyCommandsSynchronous_data()
5194 {
5195     httpProxyCommands_data();
5196 }
5197
5198 struct QThreadCleanup
5199 {
5200     static inline void cleanup(QThread *thread)
5201     {
5202         thread->quit();
5203         if (thread->wait(3000))
5204             delete thread;
5205         else
5206             qWarning("thread hung, leaking memory so test can finish");
5207     }
5208 };
5209
5210 struct QDeleteLaterCleanup
5211 {
5212     static inline void cleanup(QObject *o)
5213     {
5214         o->deleteLater();
5215     }
5216 };
5217
5218 void tst_QNetworkReply::httpProxyCommandsSynchronous()
5219 {
5220     QFETCH(QUrl, url);
5221     QFETCH(QByteArray, responseToSend);
5222     QFETCH(QString, expectedCommand);
5223
5224     // when using synchronous commands, we need a different event loop for
5225     // the server thread, because the client is never returning to the
5226     // event loop
5227     QScopedPointer<QThread, QThreadCleanup> serverThread(new QThread);
5228     QScopedPointer<MiniHttpServer, QDeleteLaterCleanup> proxyServer(new MiniHttpServer(responseToSend, false, serverThread.data()));
5229     QNetworkProxy proxy(QNetworkProxy::HttpProxy, "127.0.0.1", proxyServer->serverPort());
5230
5231     manager.setProxy(proxy);
5232     QNetworkRequest request(url);
5233
5234     // send synchronous request
5235     request.setAttribute(
5236             QNetworkRequest::SynchronousRequestAttribute,
5237             true);
5238
5239     QNetworkReplyPtr reply(manager.get(request));
5240     QVERIFY(reply->isFinished()); // synchronous
5241     manager.setProxy(QNetworkProxy());
5242
5243     //qDebug() << reply->error() << reply->errorString();
5244
5245     // we don't really care if the request succeeded
5246     // especially since it won't succeed in the HTTPS case
5247     // so just check that the command was correct
5248
5249     QString receivedHeader = proxyServer->receivedData.left(expectedCommand.length());
5250     QCOMPARE(receivedHeader, expectedCommand);
5251 }
5252
5253 void tst_QNetworkReply::proxyChange()
5254 {
5255     ProxyChangeHelper helper;
5256     MiniHttpServer proxyServer(
5257         "HTTP/1.0 200 OK\r\nProxy-Connection: keep-alive\r\n"
5258         "Content-Length: 1\r\n\r\n1");
5259     QNetworkProxy dummyProxy(QNetworkProxy::HttpProxy, "127.0.0.1", proxyServer.serverPort());
5260     QNetworkRequest req(QUrl("http://" + QtNetworkSettings::serverName()));
5261     proxyServer.doClose = false;
5262
5263     manager.setProxy(dummyProxy);
5264     QNetworkReplyPtr reply1(manager.get(req));
5265     connect(reply1, SIGNAL(finished()), &helper, SLOT(finishedSlot()));
5266
5267     manager.setProxy(QNetworkProxy());
5268     QNetworkReplyPtr reply2(manager.get(req));
5269     connect(reply2, SIGNAL(finished()), &helper, SLOT(finishedSlot()));
5270
5271     QTestEventLoop::instance().enterLoop(20);
5272     QVERIFY(!QTestEventLoop::instance().timeout());
5273
5274     // verify that the replies succeeded
5275     QCOMPARE(reply1->error(), QNetworkReply::NoError);
5276     QCOMPARE(reply1->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
5277     QVERIFY(reply1->size() == 1);
5278
5279     QCOMPARE(reply2->error(), QNetworkReply::NoError);
5280     QCOMPARE(reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
5281     QVERIFY(reply2->size() > 1);
5282
5283     // now try again and get an error
5284     // this verifies that we reuse the already-open connection
5285
5286     proxyServer.doClose = true;
5287     proxyServer.dataToTransmit =
5288         "HTTP/1.0 403 Forbidden\r\nProxy-Connection: close\r\n"
5289         "Content-Length: 1\r\n\r\n1";
5290
5291     manager.setProxy(dummyProxy);
5292     QNetworkReplyPtr reply3(manager.get(req));
5293
5294     QVERIFY(waitForFinish(reply3) == Failure);
5295
5296     QVERIFY(int(reply3->error()) > 0);
5297 }
5298
5299 void tst_QNetworkReply::authorizationError_data()
5300 {
5301
5302     QTest::addColumn<QString>("url");
5303     QTest::addColumn<int>("errorSignalCount");
5304     QTest::addColumn<int>("finishedSignalCount");
5305     QTest::addColumn<int>("error");
5306     QTest::addColumn<int>("httpStatusCode");
5307     QTest::addColumn<QString>("httpBody");
5308
5309     QTest::newRow("unknown-authorization-method") << "http://" + QtNetworkSettings::serverName() +
5310                                                      "/qtest/cgi-bin/http-unknown-authentication-method.cgi?401-authorization-required" << 1 << 1
5311                                                   << int(QNetworkReply::AuthenticationRequiredError) << 401 << "authorization required";
5312     QTest::newRow("unknown-proxy-authorization-method") << "http://" + QtNetworkSettings::serverName() +
5313                                                            "/qtest/cgi-bin/http-unknown-authentication-method.cgi?407-proxy-authorization-required" << 1 << 1
5314                                                         << int(QNetworkReply::ProxyAuthenticationRequiredError) << 407
5315                                                         << "authorization required";
5316 }
5317
5318 void tst_QNetworkReply::authorizationError()
5319 {
5320     QFETCH(QString, url);
5321     QNetworkRequest request(url);
5322     QNetworkReplyPtr reply(manager.get(request));
5323
5324     QCOMPARE(reply->error(), QNetworkReply::NoError);
5325
5326     QSignalSpy errorSpy(reply.data(), SIGNAL(error(QNetworkReply::NetworkError)));
5327     QSignalSpy finishedSpy(reply.data(), SIGNAL(finished()));
5328     // now run the request:
5329     QVERIFY(waitForFinish(reply) == Failure);
5330
5331     QFETCH(int, errorSignalCount);
5332     QCOMPARE(errorSpy.count(), errorSignalCount);
5333     QFETCH(int, finishedSignalCount);
5334     QCOMPARE(finishedSpy.count(), finishedSignalCount);
5335     QFETCH(int, error);
5336     QCOMPARE(reply->error(), QNetworkReply::NetworkError(error));
5337
5338     QFETCH(int, httpStatusCode);
5339     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), httpStatusCode);
5340
5341     QFETCH(QString, httpBody);
5342     QCOMPARE(qint64(reply->size()), qint64(httpBody.size()));
5343     QCOMPARE(QString(reply->readAll()), httpBody);
5344 }
5345
5346 void tst_QNetworkReply::httpConnectionCount()
5347 {
5348     QTcpServer server;
5349     QVERIFY(server.listen());
5350     QCoreApplication::instance()->processEvents();
5351
5352     for (int i = 0; i < 10; i++) {
5353         QNetworkRequest request (QUrl("http://127.0.0.1:" + QString::number(server.serverPort()) + "/" +  QString::number(i)));
5354         QNetworkReply* reply = manager.get(request);
5355         reply->setParent(&server);
5356     }
5357
5358     int pendingConnectionCount = 0;
5359     QTime time;
5360     time.start();
5361
5362     while(pendingConnectionCount <= 20) {
5363         QTestEventLoop::instance().enterLoop(1);
5364         QTcpSocket *socket = server.nextPendingConnection();
5365         while (socket != 0) {
5366             pendingConnectionCount++;
5367             socket->setParent(&server);
5368             socket = server.nextPendingConnection();
5369         }
5370
5371         // at max. wait 10 sec
5372         if (time.elapsed() > 10000)
5373             break;
5374     }
5375
5376     QCOMPARE(pendingConnectionCount, 6);
5377 }
5378
5379 void tst_QNetworkReply::httpReUsingConnectionSequential_data()
5380 {
5381     QTest::addColumn<bool>("doDeleteLater");
5382     QTest::newRow("deleteLater") << true;
5383     QTest::newRow("noDeleteLater") << false;
5384 }
5385
5386 void tst_QNetworkReply::httpReUsingConnectionSequential()
5387 {
5388     QFETCH(bool, doDeleteLater);
5389
5390     QByteArray response("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
5391     MiniHttpServer server(response);
5392     server.multiple = true;
5393     server.doClose = false;
5394
5395     QUrl url;
5396     url.setScheme("http");
5397     url.setPort(server.serverPort());
5398     url.setHost("127.0.0.1");
5399     // first request
5400     QNetworkReply* reply1 = manager.get(QNetworkRequest(url));
5401     connect(reply1, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
5402     QTestEventLoop::instance().enterLoop(2);
5403     QVERIFY(!QTestEventLoop::instance().timeout());
5404     QVERIFY(!reply1->error());
5405     int reply1port = server.client->peerPort();
5406
5407     if (doDeleteLater)
5408         reply1->deleteLater();
5409
5410     // finished received, send the next one
5411     QNetworkReply*reply2 = manager.get(QNetworkRequest(url));
5412     connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
5413     QTestEventLoop::instance().enterLoop(2);
5414     QVERIFY(!QTestEventLoop::instance().timeout());
5415     QVERIFY(!reply2->error());
5416     int reply2port = server.client->peerPort(); // should still be the same object
5417
5418     QVERIFY(reply1port > 0);
5419     QCOMPARE(server.totalConnections, 1);
5420     QCOMPARE(reply2port, reply1port);
5421
5422     if (!doDeleteLater)
5423         reply1->deleteLater(); // only do it if it was not done earlier
5424     reply2->deleteLater();
5425 }
5426
5427 class HttpReUsingConnectionFromFinishedSlot : public QObject {
5428     Q_OBJECT
5429 public:
5430     QNetworkReply* reply1;
5431     QNetworkReply* reply2;
5432     QUrl url;
5433     QNetworkAccessManager manager;
5434 public slots:
5435     void finishedSlot() {
5436         QVERIFY(!reply1->error());
5437
5438         QFETCH(bool, doDeleteLater);
5439         if (doDeleteLater) {
5440             reply1->deleteLater();
5441             reply1 = 0;
5442         }
5443
5444         // kick off 2nd request and exit the loop when it is done
5445         reply2 = manager.get(QNetworkRequest(url));
5446         reply2->setParent(this);
5447         connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
5448     }
5449 };
5450
5451 void tst_QNetworkReply::httpReUsingConnectionFromFinishedSlot_data()
5452 {
5453     httpReUsingConnectionSequential_data();
5454 }
5455
5456 void tst_QNetworkReply::httpReUsingConnectionFromFinishedSlot()
5457 {
5458     QByteArray response("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
5459     MiniHttpServer server(response);
5460     server.multiple = true;
5461     server.doClose = false;
5462
5463     HttpReUsingConnectionFromFinishedSlot helper;
5464     helper.reply1 = 0;
5465     helper.reply2 = 0;
5466     helper.url.setScheme("http");
5467     helper.url.setPort(server.serverPort());
5468     helper.url.setHost("127.0.0.1");
5469
5470     // first request
5471     helper.reply1 = helper.manager.get(QNetworkRequest(helper.url));
5472     helper.reply1->setParent(&helper);
5473     connect(helper.reply1, SIGNAL(finished()), &helper, SLOT(finishedSlot()));
5474     QTestEventLoop::instance().enterLoop(4);
5475     QVERIFY(!QTestEventLoop::instance().timeout());
5476
5477     QVERIFY(helper.reply2);
5478     QVERIFY(!helper.reply2->error());
5479
5480     QCOMPARE(server.totalConnections, 1);
5481 }
5482
5483 class HttpRecursiveCreationHelper : public QObject {
5484     Q_OBJECT
5485 public:
5486
5487     HttpRecursiveCreationHelper():
5488             QObject(0),
5489             requestsStartedCount_finished(0),
5490             requestsStartedCount_readyRead(0),
5491             requestsFinishedCount(0)
5492     {
5493     }
5494     QNetworkAccessManager manager;
5495     int requestsStartedCount_finished;
5496     int requestsStartedCount_readyRead;
5497     int requestsFinishedCount;
5498 public slots:
5499     void finishedSlot() {
5500         requestsFinishedCount++;
5501
5502         QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
5503         QVERIFY(!reply->error());
5504         QVERIFY(reply->bytesAvailable() == 27906);
5505
5506         if (requestsFinishedCount == 60) {
5507             QTestEventLoop::instance().exitLoop();
5508             return;
5509         }
5510
5511         if (requestsStartedCount_finished < 30) {
5512             startOne();
5513             requestsStartedCount_finished++;
5514         }
5515
5516         reply->deleteLater();
5517     }
5518     void readyReadSlot() {
5519         QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
5520         QVERIFY(!reply->error());
5521
5522         if (requestsStartedCount_readyRead < 30 && reply->bytesAvailable() > 27906/2) {
5523             startOne();
5524             requestsStartedCount_readyRead++;
5525         }
5526     }
5527     void startOne() {
5528         QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/fluke.gif";
5529         QNetworkRequest request(url);
5530         QNetworkReply *reply = manager.get(request);
5531         reply->setParent(this);
5532         connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));
5533         connect(reply, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
5534     }
5535 };
5536
5537 void tst_QNetworkReply::httpRecursiveCreation()
5538 {
5539     // this test checks if creation of new requests to the same host properly works
5540     // from readyRead() and finished() signals
5541     HttpRecursiveCreationHelper helper;
5542     helper.startOne();
5543     QTestEventLoop::instance().enterLoop(30);
5544     QVERIFY(!QTestEventLoop::instance().timeout());
5545 }
5546
5547 #ifndef QT_NO_SSL
5548 void tst_QNetworkReply::ignoreSslErrorsList_data()
5549 {
5550     QTest::addColumn<QString>("url");
5551     QTest::addColumn<QList<QSslError> >("expectedSslErrors");
5552     QTest::addColumn<QNetworkReply::NetworkError>("expectedNetworkError");
5553
5554     QList<QSslError> expectedSslErrors;
5555     QList<QSslCertificate> certs = QSslCertificate::fromPath(testDataDir + "/certs/qt-test-server-cacert.pem");
5556     QSslError rightError(QSslError::SelfSignedCertificate, certs.at(0));
5557     QSslError wrongError(QSslError::SelfSignedCertificate);
5558
5559     QTest::newRow("SSL-failure-empty-list") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::SslHandshakeFailedError;
5560     expectedSslErrors.append(wrongError);
5561     QTest::newRow("SSL-failure-wrong-error") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::SslHandshakeFailedError;
5562     expectedSslErrors.append(rightError);
5563     QTest::newRow("allErrorsInExpectedList1") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::NoError;
5564     expectedSslErrors.removeAll(wrongError);
5565     QTest::newRow("allErrorsInExpectedList2") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::NoError;
5566     expectedSslErrors.removeAll(rightError);
5567     QTest::newRow("SSL-failure-empty-list-again") << "https://" + QtNetworkSettings::serverName() + "/index.html" << expectedSslErrors << QNetworkReply::SslHandshakeFailedError;
5568 }
5569
5570 void tst_QNetworkReply::ignoreSslErrorsList()
5571 {
5572     QFETCH(QString, url);
5573     QNetworkRequest request(url);
5574     QNetworkReplyPtr reply(manager.get(request));
5575
5576     QFETCH(QList<QSslError>, expectedSslErrors);
5577     reply->ignoreSslErrors(expectedSslErrors);
5578
5579     QVERIFY(waitForFinish(reply) != Timeout);
5580
5581     QFETCH(QNetworkReply::NetworkError, expectedNetworkError);
5582     QCOMPARE(reply->error(), expectedNetworkError);
5583 }
5584
5585 void tst_QNetworkReply::ignoreSslErrorsListWithSlot_data()
5586 {
5587     ignoreSslErrorsList_data();
5588 }
5589
5590 // this is not a test, just a slot called in the test below
5591 void tst_QNetworkReply::ignoreSslErrorListSlot(QNetworkReply *reply, const QList<QSslError> &)
5592 {
5593     reply->ignoreSslErrors(storedExpectedSslErrors);
5594 }
5595
5596 // do the same as in ignoreSslErrorsList, but ignore the errors in the slot
5597 void tst_QNetworkReply::ignoreSslErrorsListWithSlot()
5598 {
5599     QFETCH(QString, url);
5600     QNetworkRequest request(url);
5601     QNetworkReplyPtr reply(manager.get(request));
5602
5603     QFETCH(QList<QSslError>, expectedSslErrors);
5604     // store the errors to ignore them later in the slot connected below
5605     storedExpectedSslErrors = expectedSslErrors;
5606     connect(&manager, SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)),
5607             this, SLOT(ignoreSslErrorListSlot(QNetworkReply *, const QList<QSslError> &)));
5608
5609
5610     QVERIFY(waitForFinish(reply) != Timeout);
5611
5612     QFETCH(QNetworkReply::NetworkError, expectedNetworkError);
5613     QCOMPARE(reply->error(), expectedNetworkError);
5614 }
5615
5616 void tst_QNetworkReply::sslConfiguration_data()
5617 {
5618     QTest::addColumn<QSslConfiguration>("configuration");
5619     QTest::addColumn<bool>("works");
5620
5621     QTest::newRow("empty") << QSslConfiguration() << false;
5622     QSslConfiguration conf = QSslConfiguration::defaultConfiguration();
5623     QTest::newRow("default") << conf << false; // does not contain test server cert
5624     QList<QSslCertificate> testServerCert = QSslCertificate::fromPath(testDataDir + "/certs/qt-test-server-cacert.pem");
5625     conf.setCaCertificates(testServerCert);
5626     QTest::newRow("set-root-cert") << conf << true;
5627     conf.setProtocol(QSsl::SecureProtocols);
5628     QTest::newRow("secure") << conf << true;
5629 }
5630
5631 void tst_QNetworkReply::sslConfiguration()
5632 {
5633     QNetworkRequest request(QUrl("https://" + QtNetworkSettings::serverName() + "/index.html"));
5634     QFETCH(QSslConfiguration, configuration);
5635     request.setSslConfiguration(configuration);
5636     QNetworkReplyPtr reply(manager.get(request));
5637
5638     QVERIFY(waitForFinish(reply) != Timeout);
5639
5640     QFETCH(bool, works);
5641     QNetworkReply::NetworkError expectedError = works ? QNetworkReply::NoError : QNetworkReply::SslHandshakeFailedError;
5642     QCOMPARE(reply->error(), expectedError);
5643 }
5644
5645 #endif // QT_NO_SSL
5646
5647 void tst_QNetworkReply::getAndThenDeleteObject_data()
5648 {
5649     QTest::addColumn<bool>("replyFirst");
5650
5651     QTest::newRow("delete-reply-first") << true;
5652     QTest::newRow("delete-qnam-first") << false;
5653 }
5654
5655 void tst_QNetworkReply::getAndThenDeleteObject()
5656 {
5657     QSKIP("unstable test - reply may be finished too early");
5658     // yes, this will leak if the testcase fails. I don't care. It must not fail then :P
5659     QNetworkAccessManager *manager = new QNetworkAccessManager();
5660     QNetworkRequest request("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile");
5661     QNetworkReply *reply = manager->get(request);
5662     reply->setReadBufferSize(1);
5663     reply->setParent((QObject*)0); // must be 0 because else it is the manager
5664
5665     QTime stopWatch;
5666     stopWatch.start();
5667     forever {
5668         QCoreApplication::instance()->processEvents();
5669         if (reply->bytesAvailable())
5670             break;
5671         if (stopWatch.elapsed() >= 30000)
5672             break;
5673     }
5674
5675     QVERIFY(reply->bytesAvailable());
5676     QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
5677     QVERIFY(!reply->isFinished()); // must not be finished
5678
5679     QFETCH(bool, replyFirst);
5680
5681     if (replyFirst) {
5682         delete reply;
5683         delete manager;
5684     } else {
5685         delete manager;
5686         delete reply;
5687     }
5688 }
5689
5690 // see https://bugs.webkit.org/show_bug.cgi?id=38935
5691 void tst_QNetworkReply::symbianOpenCDataUrlCrash()
5692 {
5693     QString requestUrl("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAWCAYAAAA1vze2AAAAB3RJTUUH2AUSEgolrgBvVQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAARnQU1BAACxjwv8YQUAAAHlSURBVHja5VbNShxBEK6ZaXtnHTebQPA1gngNmfaeq+QNPIlIXkC9iQdJxJNvEHLN3VkxhxxE8gTmEhAVddXZ6Z3f9Ndriz89/sHmkBQUVVT1fB9d9c3uOERUKTunIdn3HzstxGpYBDS4wZk7TAJj/wlJ90J+jnuygqs8svSj+/rGHBos3rE18XBvfU3no7NzlJfUaY/5whAwl8Lr/WDUv4ODxTMb+P5xLExe5LmO559WqTX/MQR4WZYEAtSePS4pE0qSnuhnRUcBU5Gm2k9XljU4Z26I3NRxBrd80rj2fh+KNE0FY4xevRgTjREvPFpasAK8Xli6MUbbuKw3afAGgSBXozo5u4hkmncAlkl5wx8iMGbdyQjnCFEiEwGiosj1UQA/x2rVddiVoi+l4IxE0PTDnx+mrQBvvnx9cFz3krhVvuhzFn579/aq/n5rW8fbtTqiWhIQZEo17YBvbkxOXNVndnYpTvod7AtiuN2re0+siwcB9oH8VxxrNwQQAhzyRs30n7wTI2HIN2g2QtQwjjhJIQatOq7E8bIVCLwzpl83Lvtvl+NohWWlE8UZTWEMAGCcR77fHKhPnZF5tYie6dfdxCphACmLPM+j8bYfmTryg64kV9Vh3mV8jP0b/4wO/YUPiT/8i0MLf55lSQAAAABJRU5ErkJggg==");
5694     QUrl url = QUrl::fromEncoded(requestUrl.toLatin1());
5695     QNetworkRequest req(url);
5696     QNetworkReplyPtr reply;
5697
5698     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, req, reply));
5699
5700     QCOMPARE(reply->url(), url);
5701     QCOMPARE(reply->error(), QNetworkReply::NoError);
5702
5703     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(598));
5704 }
5705
5706 void tst_QNetworkReply::getFromHttpIntoBuffer_data()
5707 {
5708     QTest::addColumn<QUrl>("url");
5709
5710     QTest::newRow("rfc-internal") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt");
5711 }
5712
5713 // Please note that the whole "zero copy" download buffer API is private right now. Do not use it.
5714 void tst_QNetworkReply::getFromHttpIntoBuffer()
5715 {
5716     QFETCH(QUrl, url);
5717     QNetworkRequest request(url);
5718     request.setAttribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute, 1024*128); // 128 kB
5719
5720     QNetworkAccessManager manager;
5721     QNetworkReply *reply = manager.get(request);
5722     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
5723     QTestEventLoop::instance().enterLoop(10);
5724     QVERIFY(!QTestEventLoop::instance().timeout());
5725     QVERIFY(reply->isFinished());
5726
5727     QFile reference(testDataDir + "/rfc3252.txt");
5728     QVERIFY(reference.open(QIODevice::ReadOnly));
5729
5730     QCOMPARE(reference.bytesAvailable(), reply->bytesAvailable());
5731     QCOMPARE(reference.size(), reply->size());
5732
5733     // Compare the memory buffer
5734     QVariant downloadBufferAttribute = reply->attribute(QNetworkRequest::DownloadBufferAttribute);
5735     QVERIFY(downloadBufferAttribute.isValid());
5736     QSharedPointer<char> sharedPointer = downloadBufferAttribute.value<QSharedPointer<char> >();
5737     bool memoryComparison =
5738             (0 == memcmp(static_cast<void*>(reference.readAll().data()),
5739                          sharedPointer.data(), reference.size()));
5740     QVERIFY(memoryComparison);
5741
5742     // Make sure the normal reading works
5743     reference.seek(0);
5744     QCOMPARE(reply->read(42), reference.read(42));
5745     QCOMPARE(reply->getChar(0), reference.getChar(0));
5746     QCOMPARE(reply->peek(23), reference.peek(23));
5747     QCOMPARE(reply->readLine(), reference.readLine());
5748     QCOMPARE(reference.bytesAvailable(), reply->bytesAvailable());
5749     QCOMPARE(reply->readAll(), reference.readAll());
5750     QVERIFY(reply->atEnd());
5751 }
5752
5753 // FIXME we really need to consolidate all those server implementations
5754 class GetFromHttpIntoBuffer2Server : QObject {
5755     Q_OBJECT
5756     qint64 dataSize;
5757     qint64 dataSent;
5758     QTcpServer server;
5759     QTcpSocket *client;
5760     bool serverSendsContentLength;
5761     bool chunkedEncoding;
5762
5763 public:
5764     GetFromHttpIntoBuffer2Server (qint64 ds, bool sscl, bool ce) : dataSize(ds), dataSent(0),
5765     client(0), serverSendsContentLength(sscl), chunkedEncoding(ce) {
5766         server.listen();
5767         connect(&server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
5768     }
5769
5770     int serverPort() {
5771         return server.serverPort();
5772     }
5773
5774 public slots:
5775
5776     void newConnectionSlot() {
5777         client = server.nextPendingConnection();
5778         client->setParent(this);
5779         connect(client, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
5780         connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWrittenSlot(qint64)));
5781     }
5782
5783     void readyReadSlot() {
5784         client->readAll();
5785         client->write("HTTP/1.0 200 OK\n");
5786         if (serverSendsContentLength)
5787             client->write(QString("Content-Length: " + QString::number(dataSize) + "\n").toLatin1());
5788         if (chunkedEncoding)
5789             client->write(QString("Transfer-Encoding: chunked\n").toLatin1());
5790         client->write("Connection: close\n\n");
5791     }
5792
5793     void bytesWrittenSlot(qint64 amount) {
5794         Q_UNUSED(amount);
5795         if (dataSent == dataSize && client) {
5796             // close eventually
5797
5798             // chunked encoding: we have to send a last "empty" chunk
5799             if (chunkedEncoding)
5800                 client->write(QString("0\r\n\r\n").toLatin1());
5801
5802             client->disconnectFromHost();
5803             server.close();
5804             client = 0;
5805             return;
5806         }
5807
5808         // send data
5809         if (client && client->bytesToWrite() < 100*1024 && dataSent < dataSize) {
5810             qint64 amount = qMin(qint64(16*1024), dataSize - dataSent);
5811             QByteArray data(amount, '@');
5812
5813             if (chunkedEncoding) {
5814                 client->write(QString(QString("%1").arg(amount,0,16).toUpper() + "\r\n").toLatin1());
5815                 client->write(data.constData(), amount);
5816                 client->write(QString("\r\n").toLatin1());
5817             } else {
5818                 client->write(data.constData(), amount);
5819             }
5820
5821             dataSent += amount;
5822         }
5823     }
5824 };
5825
5826 class GetFromHttpIntoBuffer2Client : QObject {
5827     Q_OBJECT
5828 private:
5829     bool useDownloadBuffer;
5830     QNetworkReply *reply;
5831     qint64 uploadSize;
5832     QList<qint64> bytesAvailableList;
5833 public:
5834     GetFromHttpIntoBuffer2Client (QNetworkReply *reply, bool useDownloadBuffer, qint64 uploadSize)
5835         : useDownloadBuffer(useDownloadBuffer), reply(reply), uploadSize(uploadSize)
5836     {
5837         connect(reply, SIGNAL(metaDataChanged()), this, SLOT(metaDataChangedSlot()));
5838         connect(reply, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
5839         connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));
5840     }
5841
5842     public slots:
5843     void metaDataChangedSlot() {
5844         if (useDownloadBuffer) {
5845             QSharedPointer<char> sharedPointer = qvariant_cast<QSharedPointer<char> >(reply->attribute(QNetworkRequest::DownloadBufferAttribute));
5846             QVERIFY(!sharedPointer.isNull()); // It will be 0 if it failed
5847         }
5848
5849         // metaDataChanged needs to come before everything else
5850         QVERIFY(bytesAvailableList.isEmpty());
5851     }
5852
5853     void readyReadSlot() {
5854         QVERIFY(!reply->isFinished());
5855
5856         qint64 bytesAvailable = reply->bytesAvailable();
5857
5858         // bytesAvailable must never be 0
5859         QVERIFY(bytesAvailable != 0);
5860
5861         if (bytesAvailableList.length() < 5) {
5862             // We assume that the first few times the bytes available must be less than the complete size, e.g.
5863             // the bytesAvailable() function works correctly in case of a downloadBuffer.
5864             QVERIFY(bytesAvailable < uploadSize);
5865         }
5866         if (!bytesAvailableList.isEmpty()) {
5867             // Also check that the same bytesAvailable is not coming twice in a row
5868             QVERIFY(bytesAvailableList.last() != bytesAvailable);
5869         }
5870
5871         bytesAvailableList.append(bytesAvailable);
5872         // Add bytesAvailable to a list an parse
5873     }
5874
5875     void finishedSlot() {
5876         // We should have already received all readyRead
5877         QVERIFY(!bytesAvailableList.isEmpty());
5878         QVERIFY(bytesAvailableList.last() == uploadSize);
5879     }
5880 };
5881
5882 void tst_QNetworkReply::getFromHttpIntoBuffer2_data()
5883 {
5884     QTest::addColumn<bool>("useDownloadBuffer");
5885
5886     QTest::newRow("use-download-buffer") << true;
5887     QTest::newRow("do-not-use-download-buffer") << false;
5888 }
5889
5890 // This test checks mostly that signal emissions are in correct order
5891 // Please note that the whole "zero copy" download buffer API is private right now. Do not use it.
5892 void tst_QNetworkReply::getFromHttpIntoBuffer2()
5893 {
5894     QFETCH(bool, useDownloadBuffer);
5895
5896     // On my Linux Desktop the results are already visible with 128 kB, however we use this to have good results.
5897 #if defined(Q_OS_WINCE_WM)
5898     // Show some mercy to non-desktop platform/s
5899     enum {UploadSize = 4*1024*1024}; // 4 MB
5900 #else
5901     enum {UploadSize = 32*1024*1024}; // 32 MB
5902 #endif
5903
5904     GetFromHttpIntoBuffer2Server server(UploadSize, true, false);
5905
5906     QNetworkRequest request(QUrl("http://127.0.0.1:" + QString::number(server.serverPort()) + "/?bare=1"));
5907     if (useDownloadBuffer)
5908         request.setAttribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute, 1024*1024*128); // 128 MB is max allowed
5909
5910     QNetworkAccessManager manager;
5911     QNetworkReplyPtr reply(manager.get(request));
5912
5913     GetFromHttpIntoBuffer2Client client(reply.data(), useDownloadBuffer, UploadSize);
5914
5915     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
5916     QTestEventLoop::instance().enterLoop(40);
5917     QCOMPARE(reply->error(), QNetworkReply::NoError);
5918     QVERIFY(!QTestEventLoop::instance().timeout());
5919 }
5920
5921
5922 void tst_QNetworkReply::getFromHttpIntoBufferCanReadLine()
5923 {
5924     QString header("HTTP/1.0 200 OK\r\nContent-Length: 7\r\n\r\nxxx\nxxx");
5925
5926     MiniHttpServer server(header.toLatin1());
5927     server.doClose = true;
5928
5929     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
5930     request.setAttribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute, 1024*1024*128); // 128 MB is max allowed
5931     QNetworkReplyPtr reply(manager.get(request));
5932
5933     QVERIFY(waitForFinish(reply) == Success);
5934
5935     QCOMPARE(reply->error(), QNetworkReply::NoError);
5936     QVERIFY(reply->canReadLine());
5937     QCOMPARE(reply->read(1), QByteArray("x"));
5938     QVERIFY(reply->canReadLine());
5939     QCOMPARE(reply->read(3), QByteArray("xx\n"));
5940     QVERIFY(!reply->canReadLine());
5941     QCOMPARE(reply->readAll(), QByteArray("xxx"));
5942     QVERIFY(!reply->canReadLine());
5943 }
5944
5945
5946
5947 // Is handled somewhere else too, introduced this special test to have it more accessible
5948 void tst_QNetworkReply::ioGetFromHttpWithoutContentLength()
5949 {
5950     QByteArray dataToSend("HTTP/1.0 200 OK\r\n\r\nHALLO! 123!");
5951     MiniHttpServer server(dataToSend);
5952     server.doClose = true;
5953
5954     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
5955     QNetworkReplyPtr reply(manager.get(request));
5956
5957     QVERIFY(waitForFinish(reply) == Success);
5958
5959     QCOMPARE(reply->url(), request.url());
5960     QVERIFY(reply->isFinished());
5961     QVERIFY(reply->error() == QNetworkReply::NoError);
5962 }
5963
5964 // Is handled somewhere else too, introduced this special test to have it more accessible
5965 void tst_QNetworkReply::ioGetFromHttpBrokenChunkedEncoding()
5966 {
5967     // This is wrong chunked encoding because of the X. What actually has to follow is \r\n
5968     // and then the declaration of the final 0 chunk
5969     QByteArray dataToSend("HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nABCX");
5970     MiniHttpServer server(dataToSend);
5971     server.doClose = false; // FIXME
5972
5973     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
5974     QNetworkReplyPtr reply(manager.get(request));
5975
5976     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
5977     QTestEventLoop::instance().enterLoop(10);
5978
5979     QEXPECT_FAIL(0, "We should close the socket and not just do nothing", Continue);
5980     QVERIFY(!QTestEventLoop::instance().timeout());
5981     QEXPECT_FAIL(0, "We should close the socket and not just do nothing", Continue);
5982     QVERIFY(reply->isFinished());
5983     QCOMPARE(reply->error(), QNetworkReply::NoError);
5984 }
5985
5986 // TODO:
5987 // Prepare a gzip that has one chunk that expands to the size mentioned in the bugreport.
5988 // Then have a custom HTTP server that waits after this chunk so the returning gets
5989 // triggered.
5990 void tst_QNetworkReply::qtbug12908compressedHttpReply()
5991 {
5992     QString header("HTTP/1.0 200 OK\r\nContent-Encoding: gzip\r\nContent-Length: 63\r\n\r\n");
5993
5994     // dd if=/dev/zero of=qtbug-12908 bs=16384  count=1 && gzip qtbug-12908 && base64 -w 0 qtbug-12908.gz
5995     QString encodedFile("H4sICDdDaUwAA3F0YnVnLTEyOTA4AO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA");
5996     QByteArray decodedFile = QByteArray::fromBase64(encodedFile.toLatin1());
5997     QCOMPARE(decodedFile.size(), 63);
5998
5999     MiniHttpServer server(header.toLatin1() + decodedFile);
6000     server.doClose = true;
6001
6002     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6003     QNetworkReplyPtr reply(manager.get(request));
6004
6005     QVERIFY(waitForFinish(reply) == Success);
6006
6007     QCOMPARE(reply->error(), QNetworkReply::NoError);
6008     QCOMPARE(reply->size(), qint64(16384));
6009     QCOMPARE(reply->readAll(), QByteArray(16384, '\0'));
6010 }
6011
6012 void tst_QNetworkReply::compressedHttpReplyBrokenGzip()
6013 {
6014     QString header("HTTP/1.0 200 OK\r\nContent-Encoding: gzip\r\nContent-Length: 63\r\n\r\n");
6015
6016     // dd if=/dev/zero of=qtbug-12908 bs=16384  count=1 && gzip qtbug-12908 && base64 -w 0 qtbug-12908.gz
6017     // Then change "BMQ" to "BMX"
6018     QString encodedFile("H4sICDdDaUwAA3F0YnVnLTEyOTA4AO3BMXEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAIC3AYbSVKsAQAAA");
6019     QByteArray decodedFile = QByteArray::fromBase64(encodedFile.toLatin1());
6020     QCOMPARE(decodedFile.size(), 63);
6021
6022     MiniHttpServer server(header.toLatin1() + decodedFile);
6023     server.doClose = true;
6024
6025     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6026     QNetworkReplyPtr reply(manager.get(request));
6027
6028     QVERIFY(waitForFinish(reply) == Failure);
6029
6030     QCOMPARE(reply->error(), QNetworkReply::ProtocolFailure);
6031 }
6032
6033 // TODO add similar test for FTP
6034 void tst_QNetworkReply::getFromUnreachableIp()
6035 {
6036     QNetworkAccessManager manager;
6037
6038     QNetworkRequest request(QUrl("http://255.255.255.255/42/23/narf/narf/narf"));
6039     QNetworkReplyPtr reply(manager.get(request));
6040
6041     QVERIFY(waitForFinish(reply) == Failure);
6042
6043     QVERIFY(reply->error() != QNetworkReply::NoError);
6044 }
6045
6046 void tst_QNetworkReply::qtbug4121unknownAuthentication()
6047 {
6048     MiniHttpServer server(QByteArray("HTTP/1.1 401 bla\r\nWWW-Authenticate: crap\r\nContent-Length: 0\r\n\r\n"));
6049     server.doClose = false;
6050
6051     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6052     QNetworkAccessManager manager;
6053     QNetworkReplyPtr reply(manager.get(request));
6054
6055     QSignalSpy authSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
6056     QSignalSpy finishedSpy(&manager, SIGNAL(finished(QNetworkReply*)));
6057     QSignalSpy errorSpy(reply.data(), SIGNAL(error(QNetworkReply::NetworkError)));
6058
6059     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6060     QTestEventLoop::instance().enterLoop(10);
6061     QVERIFY(!QTestEventLoop::instance().timeout());
6062
6063     QCOMPARE(authSpy.count(), 0);
6064     QCOMPARE(finishedSpy.count(), 1);
6065     QCOMPARE(errorSpy.count(), 1);
6066
6067     QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
6068 }
6069
6070 void tst_QNetworkReply::authenticationCacheAfterCancel_data()
6071 {
6072     QTest::addColumn<QNetworkProxy>("proxy");
6073     QTest::addColumn<bool>("proxyAuth");
6074     QTest::addColumn<QUrl>("url");
6075     for (int i = 0; i < proxies.count(); ++i) {
6076         QTest::newRow("http" + proxies.at(i).tag) << proxies.at(i).proxy << proxies.at(i).requiresAuthentication << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt");
6077 #ifndef QT_NO_SSL
6078         QTest::newRow("https" + proxies.at(i).tag) << proxies.at(i).proxy << proxies.at(i).requiresAuthentication << QUrl("https://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt");
6079 #endif
6080     }
6081 }
6082
6083 class AuthenticationCacheHelper : public QObject
6084 {
6085     Q_OBJECT
6086 public:
6087     AuthenticationCacheHelper()
6088     {}
6089 public slots:
6090     void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
6091     {
6092         if (!proxyPassword.isNull()) {
6093             auth->setUser(proxyUserName);
6094             auth->setPassword(proxyPassword);
6095             //clear credentials, if they are asked again, they were bad
6096             proxyUserName.clear();
6097             proxyPassword.clear();
6098         }
6099     }
6100     void authenticationRequired(QNetworkReply*,QAuthenticator *auth)
6101     {
6102         if (!httpPassword.isNull()) {
6103             auth->setUser(httpUserName);
6104             auth->setPassword(httpPassword);
6105             //clear credentials, if they are asked again, they were bad
6106             httpUserName.clear();
6107             httpPassword.clear();
6108         }
6109     }
6110 public:
6111     QString httpUserName;
6112     QString httpPassword;
6113     QString proxyUserName;
6114     QString proxyPassword;
6115 };
6116
6117 /* Purpose of this test is to check credentials are cached correctly.
6118  - If user cancels authentication dialog (i.e. nothing is set to the QAuthenticator by the callback) then this is not cached
6119  - if user supplies a wrong password, then this is not cached
6120  - if user supplies a correct user/password combination then this is cached
6121
6122  Test is checking both the proxyAuthenticationRequired and authenticationRequired signals.
6123  */
6124 void tst_QNetworkReply::authenticationCacheAfterCancel()
6125 {
6126     QFETCH(QNetworkProxy, proxy);
6127     QFETCH(bool, proxyAuth);
6128     QFETCH(QUrl, url);
6129     QNetworkAccessManager manager;
6130 #ifndef QT_NO_SSL
6131     connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
6132             SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
6133 #endif
6134     manager.setProxy(proxy);
6135     QSignalSpy authSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
6136     QSignalSpy proxyAuthSpy(&manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
6137
6138     AuthenticationCacheHelper helper;
6139     connect(&manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)), &helper, SLOT(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
6140     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), &helper, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
6141
6142     QNetworkRequest request(url);
6143     QNetworkReplyPtr reply;
6144     if (proxyAuth) {
6145         //should fail due to no credentials
6146         reply.reset(manager.get(request));
6147         connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6148         QTestEventLoop::instance().enterLoop(10);
6149         QVERIFY(!QTestEventLoop::instance().timeout());
6150
6151         QCOMPARE(reply->error(), QNetworkReply::ProxyAuthenticationRequiredError);
6152         QCOMPARE(authSpy.count(), 0);
6153         QCOMPARE(proxyAuthSpy.count(), 1);
6154         proxyAuthSpy.clear();
6155
6156         //should fail due to bad credentials
6157         helper.proxyUserName = "qsockstest";
6158         helper.proxyPassword = "badpassword";
6159         reply.reset(manager.get(request));
6160         connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6161         QTestEventLoop::instance().enterLoop(10);
6162         QVERIFY(!QTestEventLoop::instance().timeout());
6163
6164         QEXPECT_FAIL("http+socksauth", "QTBUG-23136 - danted accepts bad authentication but blocks the connection", Continue);
6165         QEXPECT_FAIL("https+socksauth", "QTBUG-23136 - danted accepts bad authentication but blocks the connection", Continue);
6166
6167         QCOMPARE(reply->error(), QNetworkReply::ProxyAuthenticationRequiredError);
6168         QCOMPARE(authSpy.count(), 0);
6169         QVERIFY(proxyAuthSpy.count() > 0);
6170         proxyAuthSpy.clear();
6171
6172         //QTBUG-23136 workaround
6173         if (proxy.port() == 1081) {
6174 #ifdef QT_BUILD_INTERNAL
6175             QNetworkAccessManagerPrivate::clearCache(&manager);
6176 #else
6177             return; //XFAIL result above
6178 #endif
6179         }
6180
6181         //next proxy auth should succeed, due to correct credentials
6182         helper.proxyUserName = "qsockstest";
6183         helper.proxyPassword = "password";
6184     }
6185
6186     //should fail due to no credentials
6187     reply.reset(manager.get(request));
6188     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6189     QTestEventLoop::instance().enterLoop(10);
6190     QVERIFY(!QTestEventLoop::instance().timeout());
6191
6192     QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
6193     QVERIFY(authSpy.count() > 0);
6194     authSpy.clear();
6195     if (proxyAuth) {
6196         QVERIFY(proxyAuthSpy.count() > 0);
6197         proxyAuthSpy.clear();
6198     }
6199
6200     //should fail due to bad credentials
6201     helper.httpUserName = "baduser";
6202     helper.httpPassword = "badpassword";
6203     reply.reset(manager.get(request));
6204     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6205     QTestEventLoop::instance().enterLoop(10);
6206     QVERIFY(!QTestEventLoop::instance().timeout());
6207
6208     QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
6209     QVERIFY(authSpy.count() > 0);
6210     authSpy.clear();
6211     if (proxyAuth) {
6212         //should be supplied from cache
6213         QCOMPARE(proxyAuthSpy.count(), 0);
6214         proxyAuthSpy.clear();
6215     }
6216
6217     //next auth should succeed, due to correct credentials
6218     helper.httpUserName = "httptest";
6219     helper.httpPassword = "httptest";
6220
6221     reply.reset(manager.get(request));
6222     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6223     QTestEventLoop::instance().enterLoop(10);
6224     QVERIFY(!QTestEventLoop::instance().timeout());
6225
6226     QCOMPARE(reply->error(), QNetworkReply::NoError);
6227     QVERIFY(authSpy.count() > 0);
6228     authSpy.clear();
6229     if (proxyAuth) {
6230         //should be supplied from cache
6231         QCOMPARE(proxyAuthSpy.count(), 0);
6232         proxyAuthSpy.clear();
6233     }
6234
6235     //next auth should use cached credentials
6236     reply.reset(manager.get(request));
6237     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6238     QTestEventLoop::instance().enterLoop(10);
6239     QVERIFY(!QTestEventLoop::instance().timeout());
6240
6241     QCOMPARE(reply->error(), QNetworkReply::NoError);
6242     //should be supplied from cache
6243     QCOMPARE(authSpy.count(), 0);
6244     authSpy.clear();
6245     if (proxyAuth) {
6246         //should be supplied from cache
6247         QCOMPARE(proxyAuthSpy.count(), 0);
6248         proxyAuthSpy.clear();
6249     }
6250
6251 }
6252
6253 void tst_QNetworkReply::authenticationWithDifferentRealm()
6254 {
6255     AuthenticationCacheHelper helper;
6256     QNetworkAccessManager manager;
6257 #ifndef QT_NO_SSL
6258     connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
6259             SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
6260 #endif
6261     connect(&manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)), &helper, SLOT(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
6262     connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), &helper, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
6263
6264     helper.httpUserName = "httptest";
6265     helper.httpPassword = "httptest";
6266
6267     QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfcs-auth/rfc3252.txt"));
6268     QNetworkReply* reply = manager.get(request);
6269     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6270     QTestEventLoop::instance().enterLoop(10);
6271     QVERIFY(!QTestEventLoop::instance().timeout());
6272     QCOMPARE(reply->error(), QNetworkReply::NoError);
6273
6274     helper.httpUserName = "httptest";
6275     helper.httpPassword = "httptest";
6276
6277     request.setUrl(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/auth-digest/"));
6278     reply = manager.get(request);
6279     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6280     QTestEventLoop::instance().enterLoop(10);
6281     QVERIFY(!QTestEventLoop::instance().timeout());
6282     QCOMPARE(reply->error(), QNetworkReply::NoError);
6283 }
6284
6285 class QtBug13431Helper : public QObject {
6286     Q_OBJECT
6287 public:
6288     QNetworkReply* m_reply;
6289     QTimer m_dlTimer;
6290 public slots:
6291     void replyFinished(QNetworkReply*) {
6292         QTestEventLoop::instance().exitLoop();
6293     }
6294
6295     void onReadAndReschedule() {
6296         const qint64 bytesReceived = m_reply->bytesAvailable();
6297         if (bytesReceived && m_reply->readBufferSize()) {
6298            QByteArray data = m_reply->read(bytesReceived);
6299            // reschedule read
6300            const int millisecDelay = static_cast<int>(bytesReceived * 1000 / m_reply->readBufferSize());
6301            m_dlTimer.start(millisecDelay);
6302         }
6303         else {
6304            // reschedule read
6305            m_dlTimer.start(200);
6306         }
6307     }
6308 };
6309
6310 void tst_QNetworkReply::qtbug13431replyThrottling()
6311 {
6312     QtBug13431Helper helper;
6313
6314     QNetworkAccessManager nam;
6315     connect(&nam, SIGNAL(finished(QNetworkReply*)), &helper, SLOT(replyFinished(QNetworkReply*)));
6316
6317     // Download a bigger file
6318     QNetworkRequest netRequest(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile"));
6319     helper.m_reply = nam.get(netRequest);
6320     // Set the throttle
6321     helper.m_reply->setReadBufferSize(36000);
6322
6323     // Schedule a timer that tries to read
6324
6325     connect(&helper.m_dlTimer, SIGNAL(timeout()), &helper, SLOT(onReadAndReschedule()));
6326     helper.m_dlTimer.setSingleShot(true);
6327     helper.m_dlTimer.start(0);
6328
6329     QTestEventLoop::instance().enterLoop(30);
6330     QVERIFY(!QTestEventLoop::instance().timeout());
6331     QVERIFY(helper.m_reply->isFinished());
6332     QCOMPARE(helper.m_reply->error(), QNetworkReply::NoError);
6333 }
6334
6335 void tst_QNetworkReply::httpWithNoCredentialUsage()
6336 {
6337     QNetworkAccessManager manager;
6338
6339     QSignalSpy authSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)));
6340     QSignalSpy finishedSpy(&manager, SIGNAL(finished(QNetworkReply*)));
6341
6342     // Get with credentials, to preload authentication cache
6343     {
6344         QNetworkRequest request(QUrl("http://httptest:httptest@" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
6345         QNetworkReplyPtr reply(manager.get(request));
6346         QVERIFY(waitForFinish(reply) == Success);
6347         // credentials in URL, so don't expect authentication signal
6348         QCOMPARE(authSpy.count(), 0);
6349         QCOMPARE(finishedSpy.count(), 1);
6350         finishedSpy.clear();
6351     }
6352
6353     // Get with cached credentials (normal usage)
6354     {
6355         QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
6356         QNetworkReplyPtr reply(manager.get(request));
6357         QVERIFY(waitForFinish(reply) == Success);
6358         // credentials in cache, so don't expect authentication signal
6359         QCOMPARE(authSpy.count(), 0);
6360         QCOMPARE(finishedSpy.count(), 1);
6361         finishedSpy.clear();
6362     }
6363
6364     // Do not use cached credentials (webkit cross origin usage)
6365     {
6366         QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"));
6367         request.setAttribute(QNetworkRequest::AuthenticationReuseAttribute, QNetworkRequest::Manual);
6368         QNetworkReplyPtr reply(manager.get(request));
6369
6370         QSignalSpy errorSpy(reply.data(), SIGNAL(error(QNetworkReply::NetworkError)));
6371
6372         connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection);
6373         QTestEventLoop::instance().enterLoop(10);
6374         QVERIFY(!QTestEventLoop::instance().timeout());
6375
6376         // We check if authenticationRequired was emitted, however we do not anything in it so it should be 401
6377         QCOMPARE(authSpy.count(), 1);
6378         QCOMPARE(finishedSpy.count(), 1);
6379         QCOMPARE(errorSpy.count(), 1);
6380
6381         QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
6382     }
6383 }
6384
6385 void tst_QNetworkReply::qtbug15311doubleContentLength()
6386 {
6387     QByteArray response("HTTP/1.0 200 OK\r\nContent-Length: 3\r\nServer: bogus\r\nContent-Length: 3\r\n\r\nABC");
6388     MiniHttpServer server(response);
6389     server.doClose = true;
6390
6391     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6392     QNetworkReplyPtr reply(manager.get(request));
6393
6394     QVERIFY(waitForFinish(reply) == Success);
6395
6396     QVERIFY(reply->isFinished());
6397     QCOMPARE(reply->error(), QNetworkReply::NoError);
6398     QCOMPARE(reply->size(), qint64(3));
6399     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(3));
6400     QCOMPARE(reply->rawHeader("Content-length"), QByteArray("3, 3"));
6401     QCOMPARE(reply->readAll(), QByteArray("ABC"));
6402 }
6403
6404 void tst_QNetworkReply::qtbug18232gzipContentLengthZero()
6405 {
6406     QByteArray response("HTTP/1.0 200 OK\r\nContent-Encoding: gzip\r\nContent-Length: 0\r\n\r\n");
6407     MiniHttpServer server(response);
6408     server.doClose = true;
6409
6410     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6411     QNetworkReplyPtr reply(manager.get(request));
6412
6413     QVERIFY(waitForFinish(reply) == Success);
6414
6415     QVERIFY(reply->isFinished());
6416     QCOMPARE(reply->error(), QNetworkReply::NoError);
6417     QCOMPARE(reply->size(), qint64(0));
6418     QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(0));
6419     QCOMPARE(reply->readAll(), QByteArray());
6420 }
6421
6422 // Reproduced a crash in QHttpNetworkReplyPrivate::gunzipBodyPartiallyEnd
6423 // where zlib inflateEnd was called for uninitialized zlib stream
6424 void tst_QNetworkReply::qtbug22660gzipNoContentLengthEmptyContent()
6425 {
6426     // Response with no Content-Length in header and empty content
6427     QByteArray response("HTTP/1.0 200 OK\r\nContent-Encoding: gzip\r\n\r\n");
6428     MiniHttpServer server(response);
6429     server.doClose = true;
6430
6431     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6432     QNetworkReplyPtr reply(manager.get(request));
6433
6434     QVERIFY(waitForFinish(reply) == Success);
6435
6436     QVERIFY(reply->isFinished());
6437     QCOMPARE(reply->error(), QNetworkReply::NoError);
6438     QCOMPARE(reply->size(), qint64(0));
6439     QVERIFY(!reply->header(QNetworkRequest::ContentLengthHeader).isValid());
6440     QCOMPARE(reply->readAll(), QByteArray());
6441 }
6442
6443 void tst_QNetworkReply::synchronousRequest_data()
6444 {
6445     QTest::addColumn<QUrl>("url");
6446     QTest::addColumn<QString>("expected");
6447     QTest::addColumn<bool>("checkContentLength");
6448     QTest::addColumn<QString>("mimeType");
6449
6450     // ### cache, auth, proxies
6451
6452     QTest::newRow("http")
6453         << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")
6454         << QString("file:" + testDataDir + "/rfc3252.txt")
6455         << true
6456         << QString("text/plain");
6457
6458     QTest::newRow("http-gzip")
6459         << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/deflate/rfc3252.txt")
6460         << QString("file:" + testDataDir + "/rfc3252.txt")
6461         << false // don't check content length, because it's gzip encoded
6462         //  ### we would need to enflate (un-deflate) the file content and compare the sizes
6463         << QString("text/plain");
6464
6465 #ifndef QT_NO_SSL
6466     QTest::newRow("https")
6467         << QUrl("https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt")
6468         << QString("file:" + testDataDir + "/rfc3252.txt")
6469         << true
6470         << QString("text/plain");
6471 #endif
6472
6473     QTest::newRow("data")
6474         << QUrl(QString::fromLatin1("data:text/plain,hello world"))
6475         << QString("data:hello world")
6476         << true // check content length
6477         << QString("text/plain");
6478
6479     QTest::newRow("simple-file")
6480         << QUrl::fromLocalFile(testDataDir + "/rfc3252.txt")
6481         << QString("file:" + testDataDir + "/rfc3252.txt")
6482         << true
6483         << QString();
6484 }
6485
6486 // FIXME add testcase for failing network etc
6487 void tst_QNetworkReply::synchronousRequest()
6488 {
6489     QFETCH(QUrl, url);
6490     QFETCH(QString, expected);
6491     QFETCH(bool, checkContentLength);
6492     QFETCH(QString, mimeType);
6493
6494     QNetworkRequest request(url);
6495
6496 #ifndef QT_NO_SSL
6497     // workaround for HTTPS requests: add self-signed server cert to list of CA certs,
6498     // since we cannot react to the sslErrors() signal
6499     // to fix this properly we would need to have an ignoreSslErrors() method in the
6500     // QNetworkRequest, see http://bugreports.qt-project.org/browse/QTBUG-14774
6501     if (url.scheme() == "https") {
6502         QSslConfiguration sslConf;
6503         QList<QSslCertificate> certs = QSslCertificate::fromPath(testDataDir + "/certs/qt-test-server-cacert.pem");
6504         sslConf.setCaCertificates(certs);
6505         request.setSslConfiguration(sslConf);
6506     }
6507 #endif
6508
6509     request.setAttribute(
6510             QNetworkRequest::SynchronousRequestAttribute,
6511             true);
6512
6513     QNetworkReplyPtr reply;
6514     QSignalSpy finishedSpy(&manager, SIGNAL(finished(QNetworkReply*)));
6515     QSignalSpy sslErrorsSpy(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)));
6516     RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply, 0));
6517     QVERIFY(reply->isFinished());
6518     QCOMPARE(finishedSpy.count(), 0);
6519     QCOMPARE(sslErrorsSpy.count(), 0);
6520
6521     QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader).toString(), mimeType);
6522
6523     QByteArray expectedContent;
6524
6525     if (expected.startsWith("file:")) {
6526         QString path = expected.mid(5);
6527         QFile file(path);
6528         file.open(QIODevice::ReadOnly);
6529         expectedContent = file.readAll();
6530     } else if (expected.startsWith("data:")) {
6531         expectedContent = expected.mid(5).toUtf8();
6532     }
6533
6534     if (checkContentLength)
6535         QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(expectedContent.size()));
6536     QCOMPARE(reply->readAll(), expectedContent);
6537
6538     reply->deleteLater();
6539 }
6540
6541 #ifndef QT_NO_SSL
6542 void tst_QNetworkReply::synchronousRequestSslFailure()
6543 {
6544     // test that SSL won't be accepted with self-signed certificate,
6545     // and that we do not emit the sslError signal (in the manager that is,
6546     // in the reply we don't care)
6547
6548     QUrl url("https://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt");
6549     QNetworkRequest request(url);
6550     request.setAttribute(
6551             QNetworkRequest::SynchronousRequestAttribute,
6552             true);
6553     QNetworkReplyPtr reply;
6554     QSignalSpy sslErrorsSpy(&manager, SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)));
6555     runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply, 0);
6556     QVERIFY(reply->isFinished());
6557     QCOMPARE(reply->error(), QNetworkReply::SslHandshakeFailedError);
6558     QCOMPARE(sslErrorsSpy.count(), 0);
6559 }
6560 #endif
6561
6562 class HttpAbortHelper : public QObject
6563 {
6564     Q_OBJECT
6565 public:
6566     HttpAbortHelper(QNetworkReply *parent)
6567     : QObject(parent)
6568     {
6569         mReply = parent;
6570         connect(parent, SIGNAL(readyRead()), this, SLOT(readyRead()));
6571     }
6572
6573     ~HttpAbortHelper()
6574     {
6575     }
6576
6577 public slots:
6578     void readyRead()
6579     {
6580         mReply->abort();
6581         QMetaObject::invokeMethod(&QTestEventLoop::instance(), "exitLoop", Qt::QueuedConnection);
6582     }
6583
6584 private:
6585     QNetworkReply *mReply;
6586 };
6587
6588 void tst_QNetworkReply::httpAbort()
6589 {
6590     // FIXME Also implement one where we do a big upload and then abort().
6591     // It must not crash either.
6592
6593     // Abort after the first readyRead()
6594     QNetworkRequest request("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile");
6595     QNetworkReplyPtr reply(manager.get(request));
6596     HttpAbortHelper replyHolder(reply.data());
6597     QTestEventLoop::instance().enterLoop(10);
6598     QVERIFY(!QTestEventLoop::instance().timeout());
6599     QCOMPARE(reply->error(), QNetworkReply::OperationCanceledError);
6600     QVERIFY(reply->isFinished());
6601
6602     // Abort immediately after the get()
6603     QNetworkReplyPtr reply2(manager.get(request));
6604     connect(reply2, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
6605     reply2->abort();
6606     QCOMPARE(reply2->error(), QNetworkReply::OperationCanceledError);
6607     QVERIFY(reply2->isFinished());
6608
6609     // Abort after the finished()
6610     QNetworkRequest request3("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt");
6611     QNetworkReplyPtr reply3(manager.get(request3));
6612
6613     QVERIFY(waitForFinish(reply3) == Success);
6614
6615     QVERIFY(reply3->isFinished());
6616     reply3->abort();
6617     QCOMPARE(reply3->error(), QNetworkReply::NoError);
6618 }
6619
6620 void tst_QNetworkReply::dontInsertPartialContentIntoTheCache()
6621 {
6622     QByteArray reply206 =
6623             "HTTP/1.0 206\r\n"
6624             "Connection: keep-alive\r\n"
6625             "Content-Type: text/plain\r\n"
6626             "Cache-control: no-cache\r\n"
6627             "Content-Range: bytes 2-6/8\r\n"
6628             "Content-length: 4\r\n"
6629             "\r\n"
6630             "load";
6631
6632     MiniHttpServer server(reply206);
6633     server.doClose = false;
6634
6635     MySpyMemoryCache *memoryCache = new MySpyMemoryCache(&manager);
6636     manager.setCache(memoryCache);
6637
6638     QUrl url = "http://localhost:" + QString::number(server.serverPort());
6639     QNetworkRequest request(url);
6640     request.setRawHeader("Range", "bytes=2-6");
6641
6642     QNetworkReplyPtr reply(manager.get(request));
6643
6644     QVERIFY(waitForFinish(reply) == Success);
6645
6646     QVERIFY(server.totalConnections > 0);
6647     QCOMPARE(reply->readAll().constData(), "load");
6648     QCOMPARE(memoryCache->m_insertedUrls.count(), 0);
6649 }
6650
6651 void tst_QNetworkReply::httpUserAgent()
6652 {
6653     QByteArray response("HTTP/1.0 200 OK\r\n\r\n");
6654     MiniHttpServer server(response);
6655     server.doClose = true;
6656
6657     QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
6658     request.setHeader(QNetworkRequest::UserAgentHeader, "abcDEFghi");
6659     QNetworkReplyPtr reply(manager.get(request));
6660
6661     QVERIFY(waitForFinish(reply) == Success);
6662
6663     QVERIFY(reply->isFinished());
6664     QCOMPARE(reply->error(), QNetworkReply::NoError);
6665     QVERIFY(server.receivedData.contains("\r\nUser-Agent: abcDEFghi\r\n"));
6666 }
6667
6668 void tst_QNetworkReply::synchronousAuthenticationCache()
6669 {
6670     class MiniAuthServer : public MiniHttpServer {
6671     public:
6672         MiniAuthServer(QThread *thread) : MiniHttpServer(QByteArray(), false, thread) {};
6673         virtual void reply() {
6674
6675             dataToTransmit =
6676                 "HTTP/1.0 401 Unauthorized\r\n"
6677                 "WWW-Authenticate: Basic realm=\"QNetworkAccessManager Test Realm\"\r\n"
6678                 "Content-Length: 4\r\n"
6679                 "Connection: close\r\n"
6680                 "Content-Type: text/plain\r\n"
6681                 "\r\n"
6682                 "auth";
6683             QRegExp rx("Authorization: Basic ([^\r\n]*)\r\n");
6684             if (rx.indexIn(receivedData) > 0) {
6685                 if (QByteArray::fromBase64(rx.cap(1).toLatin1()) == "login:password") {
6686                     dataToTransmit =
6687                           "HTTP/1.0 200 OK\r\n"
6688                           "Content-Type: text/plain\r\n"
6689                           "Content-Length: 2\r\n"
6690                           "\r\n"
6691                           "OK";
6692                 }
6693             }
6694             receivedData.clear();
6695             MiniHttpServer::reply();
6696         }
6697     };
6698
6699     // when using synchronous commands, we need a different event loop for
6700     // the server thread, because the client is never returning to the
6701     // event loop
6702     QScopedPointer<QThread, QThreadCleanup> serverThread(new QThread);
6703     QScopedPointer<MiniHttpServer, QDeleteLaterCleanup> server(new MiniAuthServer(serverThread.data()));
6704     server->doClose = true;
6705
6706     //1)  URL without credentials, we are not authenticated
6707     {
6708         QUrl url = "http://localhost:" + QString::number(server->serverPort()) + "/path";
6709         QNetworkRequest request(url);
6710         request.setAttribute(QNetworkRequest::SynchronousRequestAttribute, true);
6711
6712         QNetworkReplyPtr reply(manager.get(request));
6713         QVERIFY(reply->isFinished());
6714         QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError);
6715     }
6716
6717     //2)  URL with credentials, we are authenticated
6718     {
6719         QUrl url = "http://login:password@localhost:" + QString::number(server->serverPort()) + "/path2";
6720         QNetworkRequest request(url);
6721         request.setAttribute(QNetworkRequest::SynchronousRequestAttribute, true);
6722
6723         QNetworkReplyPtr reply(manager.get(request));
6724         QVERIFY(reply->isFinished());
6725         QCOMPARE(reply->error(), QNetworkReply::NoError);
6726         QCOMPARE(reply->readAll().constData(), "OK");
6727     }
6728
6729     //3)  URL without credentials, we are authenticated because they are cached
6730     {
6731         QUrl url = "http://localhost:" + QString::number(server->serverPort()) + "/path3";
6732         QNetworkRequest request(url);
6733         request.setAttribute(QNetworkRequest::SynchronousRequestAttribute, true);
6734
6735         QNetworkReplyPtr reply(manager.get(request));
6736         QVERIFY(reply->isFinished());
6737         QCOMPARE(reply->error(), QNetworkReply::NoError);
6738         QCOMPARE(reply->readAll().constData(), "OK");
6739     }
6740 }
6741
6742 void tst_QNetworkReply::pipelining()
6743 {
6744     QString urlString("http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/echo.cgi?");
6745     QList<QNetworkReplyPtr> replies;
6746     for (int a = 0; a < 20; a++) {
6747         QNetworkRequest request(urlString + QString::number(a));
6748         request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, QVariant(true));
6749         replies.append(QNetworkReplyPtr(manager.get(request)));
6750         connect(replies.at(a), SIGNAL(finished()), this, SLOT(pipeliningHelperSlot()));
6751     }
6752     QTestEventLoop::instance().enterLoop(20);
6753     QVERIFY(!QTestEventLoop::instance().timeout());
6754 }
6755
6756 void tst_QNetworkReply::pipeliningHelperSlot() {
6757     static int a = 0;
6758
6759     // check that pipelining was used in at least one of the replies
6760     static bool pipeliningWasUsed = false;
6761     QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
6762     bool pipeliningWasUsedInReply = reply->attribute(QNetworkRequest::HttpPipeliningWasUsedAttribute).toBool();
6763     if (pipeliningWasUsedInReply)
6764         pipeliningWasUsed = true;
6765
6766     // check that the contents match (the response to echo.cgi?3 should return 3 etc.)
6767     QString urlQueryString = reply->url().query();
6768     QString content = reply->readAll();
6769     QVERIFY2(urlQueryString == content, "data corruption with pipelining detected");
6770
6771     a++;
6772
6773     if (a == 20) { // all replies have finished
6774         QTestEventLoop::instance().exitLoop();
6775         QVERIFY2(pipeliningWasUsed, "pipelining was not used in any of the replies when trying to test pipelining");
6776     }
6777 }
6778
6779 void tst_QNetworkReply::closeDuringDownload_data()
6780 {
6781     QTest::addColumn<QUrl>("url");
6782     QTest::newRow("http") << QUrl("http://" + QtNetworkSettings::serverName() + "/bigfile");
6783     QTest::newRow("ftp") << QUrl("ftp://" + QtNetworkSettings::serverName() + "/qtest/bigfile");
6784 }
6785
6786 void tst_QNetworkReply::closeDuringDownload()
6787 {
6788     QFETCH(QUrl, url);
6789     QNetworkRequest request(url);
6790     QNetworkReply* reply = manager.get(request);
6791     connect(reply, SIGNAL(readyRead()), &QTestEventLoop::instance(), SLOT(exitLoop()));
6792     QTestEventLoop::instance().enterLoop(10);
6793     QVERIFY(!QTestEventLoop::instance().timeout());
6794     connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
6795     reply->close();
6796     reply->deleteLater();
6797     QTest::qWait(1000); //cancelling ftp takes some time, this avoids a warning caused by test's cleanup() destroying the connection cache before the abort is finished
6798 }
6799
6800 void tst_QNetworkReply::ftpAuthentication_data()
6801 {
6802     QTest::addColumn<QString>("referenceName");
6803     QTest::addColumn<QString>("url");
6804     QTest::addColumn<int>("error");
6805
6806     QTest::newRow("invalidPassword") << (testDataDir + "/rfc3252.txt") << "ftp://ftptest:invalid@" + QtNetworkSettings::serverName() + "/home/qt-test-server/ftp/qtest/rfc3252.txt" << int(QNetworkReply::AuthenticationRequiredError);
6807     QTest::newRow("validPassword") << (testDataDir + "/rfc3252.txt") << "ftp://ftptest:password@" + QtNetworkSettings::serverName() + "/home/qt-test-server/ftp/qtest/rfc3252.txt" << int(QNetworkReply::NoError);
6808 }
6809
6810 void tst_QNetworkReply::ftpAuthentication()
6811 {
6812     QFETCH(QString, referenceName);
6813     QFETCH(QString, url);
6814     QFETCH(int, error);
6815
6816     QFile reference(referenceName);
6817     QVERIFY(reference.open(QIODevice::ReadOnly));
6818
6819     QNetworkRequest request(url);
6820     QNetworkReplyPtr reply;
6821     runSimpleRequest(QNetworkAccessManager::GetOperation, request, reply);
6822
6823     QCOMPARE(reply->url(), request.url());
6824     QCOMPARE(reply->error(), QNetworkReply::NetworkError(error));
6825 }
6826
6827 void tst_QNetworkReply::backgroundRequest_data()
6828 {
6829     QTest::addColumn<QUrl>("url");
6830     QTest::addColumn<bool>("background");
6831     QTest::addColumn<int>("policy");
6832     QTest::addColumn<QNetworkReply::NetworkError>("error");
6833
6834     QUrl httpurl("http://" + QtNetworkSettings::serverName());
6835     QUrl httpsurl("https://" + QtNetworkSettings::serverName());
6836     QUrl ftpurl("ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt");
6837
6838     QTest::newRow("http, fg, normal") << httpurl << false << (int)QNetworkSession::NoPolicy << QNetworkReply::NoError;
6839     QTest::newRow("http, bg, normal") << httpurl << true << (int)QNetworkSession::NoPolicy << QNetworkReply::NoError;
6840     QTest::newRow("http, fg, nobg") << httpurl << false << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::NoError;
6841     QTest::newRow("http, bg, nobg") << httpurl << true << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::BackgroundRequestNotAllowedError;
6842
6843 #ifndef QT_NO_SSL
6844     QTest::newRow("https, fg, normal") << httpsurl << false << (int)QNetworkSession::NoPolicy << QNetworkReply::NoError;
6845     QTest::newRow("https, bg, normal") << httpsurl << true << (int)QNetworkSession::NoPolicy << QNetworkReply::NoError;
6846     QTest::newRow("https, fg, nobg") << httpsurl << false << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::NoError;
6847     QTest::newRow("https, bg, nobg") << httpsurl << true << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::BackgroundRequestNotAllowedError;
6848 #endif
6849
6850     QTest::newRow("ftp, fg, normal") << ftpurl << false << (int)QNetworkSession::NoPolicy << QNetworkReply::NoError;
6851     QTest::newRow("ftp, bg, normal") << ftpurl << true << (int)QNetworkSession::NoPolicy << QNetworkReply::NoError;
6852     QTest::newRow("ftp, fg, nobg") << ftpurl << false << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::NoError;
6853     QTest::newRow("ftp, bg, nobg") << ftpurl << true << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::BackgroundRequestNotAllowedError;
6854
6855 }
6856
6857 //test purpose: background requests can't be started when not allowed
6858 void tst_QNetworkReply::backgroundRequest()
6859 {
6860 #ifdef QT_BUILD_INTERNAL
6861 #ifndef QT_NO_BEARERMANAGEMENT
6862     QFETCH(QUrl, url);
6863     QFETCH(bool, background);
6864     QFETCH(int, policy);
6865     QFETCH(QNetworkReply::NetworkError, error);
6866
6867     QNetworkRequest request(url);
6868
6869     if (background)
6870         request.setAttribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(true));
6871
6872     //this preconstructs the session so we can change policies in advance
6873     manager.setConfiguration(networkConfiguration);
6874
6875 #ifndef QT_NO_SSL
6876     connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
6877         SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
6878 #endif
6879
6880     const QWeakPointer<const QNetworkSession> session = QNetworkAccessManagerPrivate::getNetworkSession(&manager);
6881     QVERIFY(session);
6882     QNetworkSession::UsagePolicies original = session.data()->usagePolicies();
6883     QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), QNetworkSession::UsagePolicies(policy));
6884
6885     QNetworkReplyPtr reply(manager.get(request));
6886
6887     QVERIFY(waitForFinish(reply) != Timeout);
6888     if (session)
6889         QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), original);
6890
6891     QVERIFY(reply->isFinished());
6892     QCOMPARE(reply->error(), error);
6893 #endif
6894 #endif
6895 }
6896
6897 void tst_QNetworkReply::backgroundRequestInterruption_data()
6898 {
6899     QTest::addColumn<QUrl>("url");
6900     QTest::addColumn<bool>("background");
6901     QTest::addColumn<QNetworkReply::NetworkError>("error");
6902
6903     QUrl httpurl("http://" + QtNetworkSettings::serverName() + "/qtest/mediumfile");
6904     QUrl httpsurl("https://" + QtNetworkSettings::serverName() + "/qtest/mediumfile");
6905     QUrl ftpurl("ftp://" + QtNetworkSettings::serverName() + "/qtest/bigfile");
6906
6907     QTest::newRow("http, fg, nobg") << httpurl << false << QNetworkReply::NoError;
6908     QTest::newRow("http, bg, nobg") << httpurl << true << QNetworkReply::BackgroundRequestNotAllowedError;
6909
6910 #ifndef QT_NO_SSL
6911     QTest::newRow("https, fg, nobg") << httpsurl << false << QNetworkReply::NoError;
6912     QTest::newRow("https, bg, nobg") << httpsurl << true  << QNetworkReply::BackgroundRequestNotAllowedError;
6913 #endif
6914
6915     QTest::newRow("ftp, fg, nobg") << ftpurl << false << QNetworkReply::NoError;
6916     QTest::newRow("ftp, bg, nobg") << ftpurl << true << QNetworkReply::BackgroundRequestNotAllowedError;
6917
6918 }
6919
6920 //test purpose: background requests in progress are aborted when policy changes to disallow them
6921 void tst_QNetworkReply::backgroundRequestInterruption()
6922 {
6923 #ifdef QT_BUILD_INTERNAL
6924 #ifndef QT_NO_BEARERMANAGEMENT
6925     QFETCH(QUrl, url);
6926     QFETCH(bool, background);
6927     QFETCH(QNetworkReply::NetworkError, error);
6928
6929     QNetworkRequest request(url);
6930
6931     if (background)
6932         request.setAttribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(true));
6933
6934     //this preconstructs the session so we can change policies in advance
6935     manager.setConfiguration(networkConfiguration);
6936
6937 #ifndef QT_NO_SSL
6938     connect(&manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
6939         SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
6940 #endif
6941
6942     const QWeakPointer<const QNetworkSession> session = QNetworkAccessManagerPrivate::getNetworkSession(&manager);
6943     QVERIFY(session);
6944     QNetworkSession::UsagePolicies original = session.data()->usagePolicies();
6945     QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), QNetworkSession::NoPolicy);
6946
6947     request.setAttribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute, 8192);
6948     QNetworkReplyPtr reply(manager.get(request));
6949     reply->setReadBufferSize(1024);
6950
6951     QSignalSpy spy(reply.data(), SIGNAL(readyRead()));
6952     QTRY_VERIFY(spy.count() > 0);
6953
6954     QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), QNetworkSession::NoBackgroundTrafficPolicy);
6955
6956     // After we have changed the policy we can download at full speed.
6957     reply->setReadBufferSize(0);
6958
6959     QVERIFY(waitForFinish(reply) != Timeout);
6960     if (session)
6961         QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), original);
6962
6963     QVERIFY(reply->isFinished());
6964     QCOMPARE(reply->error(), error);
6965 #endif
6966 #endif
6967 }
6968
6969 void tst_QNetworkReply::backgroundRequestConnectInBackground_data()
6970 {
6971     QTest::addColumn<QUrl>("url");
6972     QTest::addColumn<bool>("background");
6973
6974     QUrl httpurl("http://" + QtNetworkSettings::serverName());
6975     QUrl ftpurl("ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt");
6976
6977     QTest::newRow("http, fg") << httpurl << false;
6978     QTest::newRow("http, bg") << httpurl << true;
6979
6980     QTest::newRow("ftp, fg") << ftpurl << false;
6981     QTest::newRow("ftp, bg") << ftpurl << true;
6982 }
6983
6984 //test purpose: check that backgroundness is propagated to the network session
6985 void tst_QNetworkReply::backgroundRequestConnectInBackground()
6986 {
6987 #ifdef QT_BUILD_INTERNAL
6988 #ifndef QT_NO_BEARERMANAGEMENT
6989     QFETCH(QUrl, url);
6990     QFETCH(bool, background);
6991
6992     QNetworkRequest request(url);
6993
6994     if (background)
6995         request.setAttribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(true));
6996
6997     QWeakPointer<const QNetworkSession> session = QNetworkAccessManagerPrivate::getNetworkSession(&manager);
6998     //force QNAM to reopen the session.
6999     if (session && session.data()->isOpen()) {
7000         const_cast<QNetworkSession *>(session.data())->close();
7001         QCoreApplication::processEvents(); //let signals propagate inside QNAM
7002     }
7003
7004     //this preconstructs the session so we can change policies in advance
7005     manager.setConfiguration(networkConfiguration);
7006
7007     session = QNetworkAccessManagerPrivate::getNetworkSession(&manager);
7008     QVERIFY(session);
7009     QNetworkSession::UsagePolicies original = session.data()->usagePolicies();
7010     QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), QNetworkSession::NoPolicy);
7011
7012     QNetworkReplyPtr reply(manager.get(request));
7013
7014     QVERIFY(waitForFinish(reply) != Timeout);
7015     session = QNetworkAccessManagerPrivate::getNetworkSession(&manager);
7016     if (session) {
7017         QVariant cib = session.data()->sessionProperty(QStringLiteral("ConnectInBackground"));
7018         if (!cib.isValid())
7019             QSKIP("inconclusive - ConnectInBackground session property not supported by the bearer plugin");
7020         QCOMPARE(cib.toBool(), background);
7021         QNetworkSessionPrivate::setUsagePolicies(*const_cast<QNetworkSession *>(session.data()), original);
7022     } else {
7023         QSKIP("inconclusive - network session has been destroyed");
7024     }
7025
7026     QVERIFY(reply->isFinished());
7027 #endif
7028 #endif
7029 }
7030
7031 // NOTE: This test must be last testcase in tst_qnetworkreply!
7032 void tst_QNetworkReply::parentingRepliesToTheApp()
7033 {
7034     QNetworkRequest request (QUrl("http://" + QtNetworkSettings::serverName()));
7035     manager.get(request)->setParent(this); // parent to this object
7036     manager.get(request)->setParent(qApp); // parent to the app
7037 }
7038
7039 QTEST_MAIN(tst_QNetworkReply)
7040
7041 #include "tst_qnetworkreply.moc"