1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright 2014-2016 Canonical Ltd.
*
* This file is part of webbrowser-app.
*
* webbrowser-app is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* webbrowser-app is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// system
#include <utime.h>
// Qt
#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include <QtCore/QRegExp>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QtTest/QSignalSpy>
#include <QtTest/QtTest>
// local
#include "favicon-fetcher.h"
const unsigned char icon_data[] = {
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x02, 0x00, 0x01, 0x00,
0x01, 0x00, 0x38, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const int icon_data_size = 78;
class TestHTTPServer : public QTcpServer
{
Q_OBJECT
public:
TestHTTPServer(QObject* parent = 0)
: QTcpServer(parent)
{
connect(this, SIGNAL(newConnection()), SLOT(onNewConnection()));
}
QString baseURL() const
{
return "http://" + serverAddress().toString() + ":" + QString::number(serverPort());
}
Q_SIGNALS:
void gotRequest(const QString& path) const;
void gotError() const;
private Q_SLOTS:
void onNewConnection() {
if (hasPendingConnections()) {
QTcpSocket* socket = nextPendingConnection();
connect(socket, SIGNAL(readyRead()), SLOT(readClient()));
connect(socket, SIGNAL(disconnected()), SLOT(discardClient()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(gotError()));
}
}
void readClient()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if (!socket) {
return;
}
if (!socket->canReadLine()) {
return;
}
QStringList tokens = QString(socket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
if (tokens.isEmpty()) {
return;
}
if (tokens.first() != "GET") {
return;
}
QString path = tokens[1];
Q_EMIT gotRequest(path);
QTextStream response(socket);
response.setAutoDetectUnicode(true);
QRegExp icon("/\\w+\\.ico");
QRegExp redirection("^/redirect/(\\d+)/(.*)");
if (icon.exactMatch(path)) {
response << "HTTP/1.0 200 OK\r\n"
<< "Content-Length: " << icon_data_size << "\r\n"
<< "Content-Type: image/x-icon\r\n\r\n"
<< QString::fromLocal8Bit((const char*) icon_data, icon_data_size) << "\n";
} else if (redirection.exactMatch(path)) {
int n = redirection.cap(1).toInt();
response << "HTTP/1.0 303 See Other\r\n"
<< "Content-Length: 9\r\n"
<< "Content-Type: text/plain\r\n"
<< "Location: " << baseURL();
if (n == 1) {
response << "/" << redirection.cap(2);
} else {
response << "/redirect/" << (n - 1) << "/" << redirection.cap(2);
}
response << "\r\n\r\n"
<< "see other\n";
} else {
response << "HTTP/1.0 404 Not Found\r\n"
<< "Content-Length: 9\r\n"
<< "Content-Type: text/plain\r\n\r\n"
<< "not found\n";
}
response.flush();
socket->waitForBytesWritten();
socket->disconnectFromHost();
}
void discardClient()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if (socket) {
socket->deleteLater();
}
}
};
class FaviconFetcherTests : public QObject
{
Q_OBJECT
private:
FaviconFetcher* fetcher;
QSignalSpy* fetcherSpy;
TestHTTPServer* server;
QSignalSpy* serverSpy;
QSignalSpy* errorSpy;
private Q_SLOTS:
void init()
{
{
FaviconFetcher temp;
QDir(temp.cacheLocation()).removeRecursively();
}
fetcher = new FaviconFetcher;
fetcherSpy = new QSignalSpy(fetcher, SIGNAL(localUrlChanged()));
server = new TestHTTPServer;
server->listen();
serverSpy = new QSignalSpy(server, SIGNAL(gotRequest(const QString&)));
errorSpy = new QSignalSpy(server, SIGNAL(gotError()));
}
void cleanup()
{
delete errorSpy;
delete serverSpy;
delete server;
delete fetcherSpy;
delete fetcher;
}
void shouldCacheIcon()
{
QUrl url(server->baseURL() + "/favicon1.ico");
fetcher->setUrl(url);
QCOMPARE(fetcher->url(), url);
QVERIFY(fetcherSpy->wait());
QCOMPARE(serverSpy->count(), 1);
QString cached = fetcher->localUrl().path();
QVERIFY(cached.startsWith(fetcher->cacheLocation()));
QVERIFY(QFileInfo::exists(cached));
}
void shouldNotCacheLocalIcon()
{
QUrl url("file:///tmp/favicon.ico");
fetcher->setUrl(url);
QCOMPARE(fetcherSpy->count(), 1);
QVERIFY(serverSpy->isEmpty());
QCOMPARE(fetcher->localUrl(), url);
QDir cache(fetcher->cacheLocation(), "", QDir::Unsorted, QDir::Files | QDir::NoDotAndDotDot);
QCOMPARE(cache.count(), (uint) 0);
}
void shouldFailToDownloadInvalidIcon()
{
// First fetch a valid icon to ensure localUrl is initially not empty
QUrl url(server->baseURL() + "/favicon1.ico");
fetcher->setUrl(url);
QVERIFY(fetcherSpy->wait());
QVERIFY(!fetcher->localUrl().isEmpty());
// Then request an invalid one
url = QUrl(server->baseURL() + "/invalid.png");
fetcher->setUrl(url);
QVERIFY(serverSpy->wait());
QVERIFY(fetcher->localUrl().isEmpty());
}
void shouldReturnCachedIcon()
{
// First fetch an icon so that it’s cached
QUrl url(server->baseURL() + "/favicon1.ico");
fetcher->setUrl(url);
QVERIFY(fetcherSpy->wait());
QUrl localUrl = fetcher->localUrl();
QVERIFY(!localUrl.isEmpty());
// Then fetch another icon
fetcher->setUrl(QUrl(server->baseURL() + "/favicon2.ico"));
QVERIFY(fetcherSpy->wait());
QVERIFY(!fetcher->localUrl().isEmpty());
// Then fetch the first icon again, and verify it comes from the cache
serverSpy->clear();
fetcher->setUrl(url);
QCOMPARE(fetcher->localUrl(), localUrl);
QVERIFY(serverSpy->isEmpty());
}
void shouldNotCacheIcon()
{
fetcher->setShouldCache(false);
QUrl url(server->baseURL() + "/favicon1.ico");
fetcher->setUrl(url);
QCOMPARE(fetcher->url(), url);
QVERIFY(fetcherSpy->wait());
QCOMPARE(serverSpy->count(), 1);
QUrl icon = fetcher->localUrl();
QVERIFY(!icon.isLocalFile());
QCOMPARE(icon.scheme(), QString("data"));
QVERIFY(icon.path().startsWith("image/png;base64,"));
}
void shouldHandleRedirections()
{
QUrl url(server->baseURL() + "/redirect/3/favicon1.ico");
fetcher->setUrl(url);
QVERIFY(fetcherSpy->wait());
QCOMPARE(serverSpy->count(), 4);
}
void shouldNotHandleTooManyRedirections()
{
QUrl url(server->baseURL() + "/redirect/8/favicon1.ico");
QString msg("Failed to download %1 : too many redirections");
QTest::ignoreMessage(QtWarningMsg, msg.arg(url.toString()).toUtf8());
fetcher->setUrl(url);
QVERIFY(!fetcherSpy->wait(500));
QCOMPARE(serverSpy->count(), 5);
}
void shouldDiscardOldCachedIcons()
{
// First fetch an icon, and touch the cached file on disk to ensure
// it will be considered out of date next time it’s requested
QUrl url(server->baseURL() + "/favicon1.ico");
fetcher->setUrl(url);
QVERIFY(fetcherSpy->wait());
QUrl localUrl = fetcher->localUrl();
struct utimbuf ubuf;
ubuf.modtime = QDateTime::currentDateTime().addYears(-1).toTime_t();
QCOMPARE(utime(localUrl.path().toUtf8().constData(), &ubuf), 0);
// Then fetch another icon
fetcher->setUrl(QUrl(server->baseURL() + "/favicon2.ico"));
QVERIFY(fetcherSpy->wait());
QVERIFY(!fetcher->localUrl().isEmpty());
// Then fetch the first icon again, and verify it is being re-downloaded
serverSpy->clear();
fetcher->setUrl(url);
QVERIFY(fetcherSpy->wait());
QCOMPARE(fetcher->localUrl(), localUrl);
QCOMPARE(serverSpy->count(), 1);
}
void shouldCancelRequests()
{
// Issue several requests rapidly in succession, and verify that
// all the previous ones are discarded. Take into account possible
// errors (see https://launchpad.net/bugs/1498539).
int requests = 100;
int errors = 0;
for (int i = 1; i <= requests; ++i) {
QUrl url(server->baseURL() + "/favicon" + QString::number(i) + ".ico");
fetcher->setUrl(url);
QVERIFY(serverSpy->wait(500) || (errorSpy->count() == ++errors));
}
QVERIFY(fetcherSpy->wait());
QVERIFY((serverSpy->count() + errorSpy->count()) >= requests);
QCOMPARE(fetcherSpy->count(), 1);
}
};
QTEST_MAIN(FaviconFetcherTests)
#include "tst_FaviconFetcherTests.moc"
|