| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include <QtTest/QtTest> |
| 30 |
|
| 31 |
#include "http2srv.h" |
| 32 |
|
| 33 |
#include <QtNetwork/private/http2protocol_p.h> |
| 34 |
#include <QtNetwork/qnetworkaccessmanager.h> |
| 35 |
#include <QtNetwork/qhttp2configuration.h> |
| 36 |
#include <QtNetwork/qnetworkrequest.h> |
| 37 |
#include <QtNetwork/qnetworkreply.h> |
| 38 |
|
| 39 |
#include <QtCore/qglobal.h> |
| 40 |
#include <QtCore/qobject.h> |
| 41 |
#include <QtCore/qthread.h> |
| 42 |
#include <QtCore/qurl.h> |
| 43 |
|
| 44 |
#ifndef QT_NO_SSL |
| 45 |
#ifndef QT_NO_OPENSSL |
| 46 |
#include <QtNetwork/private/qsslsocket_openssl_symbols_p.h> |
| 47 |
#endif // NO_OPENSSL |
| 48 |
#endif // NO_SSL |
| 49 |
|
| 50 |
#include <cstdlib> |
| 51 |
#include <memory> |
| 52 |
#include <string> |
| 53 |
|
| 54 |
#include "emulationdetector.h" |
| 55 |
|
| 56 |
#if (!defined(QT_NO_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(OPENSSL_NO_TLSEXT)) \ |
| 57 |
|| QT_CONFIG(schannel) |
| 58 |
// HTTP/2 over TLS requires ALPN/NPN to negotiate the protocol version. |
| 59 |
const bool clearTextHTTP2 = false; |
| 60 |
#else |
| 61 |
// No ALPN/NPN support to negotiate HTTP/2, we'll use cleartext 'h2c' with |
| 62 |
// a protocol upgrade procedure. |
| 63 |
const bool clearTextHTTP2 = true; |
| 64 |
#endif |
| 65 |
|
| 66 |
Q_DECLARE_METATYPE(H2Type) |
| 67 |
Q_DECLARE_METATYPE(QNetworkRequest::Attribute) |
| 68 |
|
| 69 |
QT_BEGIN_NAMESPACE |
| 70 |
|
| 71 |
QHttp2Configuration qt_defaultH2Configuration() |
| 72 |
{ |
| 73 |
QHttp2Configuration config; |
| 74 |
config.setStreamReceiveWindowSize(Http2::qtDefaultStreamReceiveWindowSize); |
| 75 |
config.setSessionReceiveWindowSize(Http2::maxSessionReceiveWindowSize); |
| 76 |
config.setServerPushEnabled(false); |
| 77 |
return config; |
| 78 |
} |
| 79 |
|
| 80 |
RawSettings qt_H2ConfigurationToSettings(const QHttp2Configuration &config = qt_defaultH2Configuration()) |
| 81 |
{ |
| 82 |
RawSettings settings; |
| 83 |
settings[Http2::Settings::ENABLE_PUSH_ID] = config.serverPushEnabled(); |
| 84 |
settings[Http2::Settings::INITIAL_WINDOW_SIZE_ID] = config.streamReceiveWindowSize(); |
| 85 |
if (config.maxFrameSize() != Http2::minPayloadLimit) |
| 86 |
settings[Http2::Settings::MAX_FRAME_SIZE_ID] = config.maxFrameSize(); |
| 87 |
return settings; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
class tst_Http2 : public QObject |
| 92 |
{ |
| 93 |
Q_OBJECT |
| 94 |
public: |
| 95 |
tst_Http2(); |
| 96 |
~tst_Http2(); |
| 97 |
public slots: |
| 98 |
void init(); |
| 99 |
private slots: |
| 100 |
// Tests: |
| 101 |
void singleRequest_data(); |
| 102 |
void singleRequest(); |
| 103 |
void multipleRequests(); |
| 104 |
void flowControlClientSide(); |
| 105 |
void flowControlServerSide(); |
| 106 |
void pushPromise(); |
| 107 |
void goaway_data(); |
| 108 |
void goaway(); |
| 109 |
void earlyResponse(); |
| 110 |
void connectToHost_data(); |
| 111 |
void connectToHost(); |
| 112 |
void maxFrameSize(); |
| 113 |
void http2DATAFrames(); |
| 114 |
|
| 115 |
void authenticationRequired_data(); |
| 116 |
void authenticationRequired(); |
| 117 |
|
| 118 |
void redirect_data(); |
| 119 |
void redirect(); |
| 120 |
|
| 121 |
void abortOnEncrypted(); |
| 122 |
|
| 123 |
protected slots: |
| 124 |
// Slots to listen to our in-process server: |
| 125 |
void serverStarted(quint16 port); |
| 126 |
void clientPrefaceOK(); |
| 127 |
void clientPrefaceError(); |
| 128 |
void serverSettingsAcked(); |
| 129 |
void invalidFrame(); |
| 130 |
void invalidRequest(quint32 streamID); |
| 131 |
void decompressionFailed(quint32 streamID); |
| 132 |
void receivedRequest(quint32 streamID); |
| 133 |
void receivedData(quint32 streamID); |
| 134 |
void windowUpdated(quint32 streamID); |
| 135 |
void replyFinished(); |
| 136 |
void replyFinishedWithError(); |
| 137 |
|
| 138 |
private: |
| 139 |
void clearHTTP2State(); |
| 140 |
// Run event for 'ms' milliseconds. |
| 141 |
// The default value '5000' is enough for |
| 142 |
// small payload. |
| 143 |
void runEventLoop(int ms = 5000); |
| 144 |
void stopEventLoop(); |
| 145 |
Http2Server *newServer(const RawSettings &serverSettings, H2Type connectionType, |
| 146 |
const RawSettings &clientSettings = qt_H2ConfigurationToSettings()); |
| 147 |
// Send a get or post request, depending on a payload (empty or not). |
| 148 |
void sendRequest(int streamNumber, |
| 149 |
QNetworkRequest::Priority priority = QNetworkRequest::NormalPriority, |
| 150 |
const QByteArray &payload = QByteArray(), |
| 151 |
const QHttp2Configuration &clientConfiguration = qt_defaultH2Configuration()); |
| 152 |
QUrl requestUrl(H2Type connnectionType) const; |
| 153 |
|
| 154 |
quint16 serverPort = 0; |
| 155 |
QThread *workerThread = nullptr; |
| 156 |
std::unique_ptr<QNetworkAccessManager> manager; |
| 157 |
|
| 158 |
QTestEventLoop eventLoop; |
| 159 |
|
| 160 |
int nRequests = 0; |
| 161 |
int nSentRequests = 0; |
| 162 |
|
| 163 |
int windowUpdates = 0; |
| 164 |
bool prefaceOK = false; |
| 165 |
bool serverGotSettingsACK = false; |
| 166 |
bool POSTResponseHEADOnly = true; |
| 167 |
|
| 168 |
static const RawSettings defaultServerSettings; |
| 169 |
}; |
| 170 |
|
| 171 |
#define STOP_ON_FAILURE \ |
| 172 |
if (QTest::currentTestFailed()) \ |
| 173 |
return; |
| 174 |
|
| 175 |
const RawSettings tst_Http2::defaultServerSettings{{Http2::Settings::MAX_CONCURRENT_STREAMS_ID, 100}}; |
| 176 |
|
| 177 |
namespace { |
| 178 |
|
| 179 |
// Our server lives/works on a different thread so we invoke its 'deleteLater' |
| 180 |
// instead of simple 'delete'. |
| 181 |
struct ServerDeleter |
| 182 |
{ |
| 183 |
static void cleanup(Http2Server *srv) |
| 184 |
{ |
| 185 |
if (srv) { |
| 186 |
srv->stopSendingDATAFrames(); |
| 187 |
QMetaObject::invokeMethod(srv, "deleteLater", Qt::QueuedConnection); |
| 188 |
} |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
using ServerPtr = QScopedPointer<Http2Server, ServerDeleter>; |
| 193 |
|
| 194 |
H2Type defaultConnectionType() |
| 195 |
{ |
| 196 |
return clearTextHTTP2 ? H2Type::h2c : H2Type::h2Alpn; |
| 197 |
} |
| 198 |
|
| 199 |
} // unnamed namespace |
| 200 |
|
| 201 |
tst_Http2::tst_Http2() |
| 202 |
: workerThread(new QThread) |
| 203 |
{ |
| 204 |
workerThread->start(); |
| 205 |
} |
| 206 |
|
| 207 |
tst_Http2::~tst_Http2() |
| 208 |
{ |
| 209 |
workerThread->quit(); |
| 210 |
workerThread->wait(5000); |
| 211 |
|
| 212 |
if (workerThread->isFinished()) { |
| 213 |
delete workerThread; |
| 214 |
} else { |
| 215 |
connect(workerThread, &QThread::finished, |
| 216 |
workerThread, &QThread::deleteLater); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void tst_Http2::init() |
| 221 |
{ |
| 222 |
manager.reset(new QNetworkAccessManager); |
| 223 |
} |
| 224 |
|
| 225 |
void tst_Http2::singleRequest_data() |
| 226 |
{ |
| 227 |
QTest::addColumn<QNetworkRequest::Attribute>("h2Attribute"); |
| 228 |
QTest::addColumn<H2Type>("connectionType"); |
| 229 |
|
| 230 |
// 'Clear text' that should always work, either via the protocol upgrade |
| 231 |
// or as direct. |
| 232 |
QTest::addRow("h2c-upgrade") << QNetworkRequest::Http2AllowedAttribute << H2Type::h2c; |
| 233 |
QTest::addRow("h2c-direct") << QNetworkRequest::Http2DirectAttribute << H2Type::h2cDirect; |
| 234 |
|
| 235 |
if (!clearTextHTTP2) { |
| 236 |
// Qt with TLS where TLS-backend supports ALPN. |
| 237 |
QTest::addRow("h2-ALPN") << QNetworkRequest::Http2AllowedAttribute << H2Type::h2Alpn; |
| 238 |
} |
| 239 |
|
| 240 |
#if QT_CONFIG(ssl) |
| 241 |
QTest::addRow("h2-direct") << QNetworkRequest::Http2DirectAttribute << H2Type::h2Direct; |
| 242 |
#endif |
| 243 |
} |
| 244 |
|
| 245 |
void tst_Http2::singleRequest() |
| 246 |
{ |
| 247 |
clearHTTP2State(); |
| 248 |
|
| 249 |
#if QT_CONFIG(securetransport) |
| 250 |
// Normally on macOS we use plain text only for SecureTransport |
| 251 |
// does not support ALPN on the server side. With 'direct encrytped' |
| 252 |
// we have to use TLS sockets (== private key) and thus suppress a |
| 253 |
// keychain UI asking for permission to use a private key. |
| 254 |
// Our CI has this, but somebody testing locally - will have a problem. |
| 255 |
qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1")); |
| 256 |
auto envRollback = qScopeGuard([](){ |
| 257 |
qunsetenv("QT_SSL_USE_TEMPORARY_KEYCHAIN"); |
| 258 |
}); |
| 259 |
#endif |
| 260 |
|
| 261 |
serverPort = 0; |
| 262 |
nRequests = 1; |
| 263 |
|
| 264 |
QFETCH(const H2Type, connectionType); |
| 265 |
ServerPtr srv(newServer(defaultServerSettings, connectionType)); |
| 266 |
|
| 267 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 268 |
runEventLoop(); |
| 269 |
|
| 270 |
QVERIFY(serverPort != 0); |
| 271 |
|
| 272 |
auto url = requestUrl(connectionType); |
| 273 |
url.setPath("/index.html"); |
| 274 |
|
| 275 |
QNetworkRequest request(url); |
| 276 |
QFETCH(const QNetworkRequest::Attribute, h2Attribute); |
| 277 |
request.setAttribute(h2Attribute, QVariant(true)); |
| 278 |
|
| 279 |
auto reply = manager->get(request); |
| 280 |
#if QT_CONFIG(ssl) |
| 281 |
QSignalSpy encSpy(reply, &QNetworkReply::encrypted); |
| 282 |
#endif // QT_CONFIG(ssl) |
| 283 |
|
| 284 |
connect(reply, &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 285 |
// Since we're using self-signed certificates, |
| 286 |
// ignore SSL errors: |
| 287 |
reply->ignoreSslErrors(); |
| 288 |
|
| 289 |
runEventLoop(); |
| 290 |
STOP_ON_FAILURE |
| 291 |
|
| 292 |
QVERIFY(nRequests == 0); |
| 293 |
QVERIFY(prefaceOK); |
| 294 |
QVERIFY(serverGotSettingsACK); |
| 295 |
|
| 296 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 297 |
QVERIFY(reply->isFinished()); |
| 298 |
|
| 299 |
#if QT_CONFIG(ssl) |
| 300 |
if (connectionType == H2Type::h2Alpn || connectionType == H2Type::h2Direct) |
| 301 |
QCOMPARE(encSpy.count(), 1); |
| 302 |
#endif // QT_CONFIG(ssl) |
| 303 |
} |
| 304 |
|
| 305 |
void tst_Http2::multipleRequests() |
| 306 |
{ |
| 307 |
clearHTTP2State(); |
| 308 |
|
| 309 |
serverPort = 0; |
| 310 |
nRequests = 10; |
| 311 |
|
| 312 |
ServerPtr srv(newServer(defaultServerSettings, defaultConnectionType())); |
| 313 |
|
| 314 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 315 |
|
| 316 |
runEventLoop(); |
| 317 |
QVERIFY(serverPort != 0); |
| 318 |
|
| 319 |
// Just to make the order a bit more interesting |
| 320 |
// we'll index this randomly: |
| 321 |
const QNetworkRequest::Priority priorities[] = { |
| 322 |
QNetworkRequest::HighPriority, |
| 323 |
QNetworkRequest::NormalPriority, |
| 324 |
QNetworkRequest::LowPriority |
| 325 |
}; |
| 326 |
|
| 327 |
for (int i = 0; i < nRequests; ++i) |
| 328 |
sendRequest(i, priorities[QRandomGenerator::global()->bounded(3)]); |
| 329 |
|
| 330 |
runEventLoop(); |
| 331 |
STOP_ON_FAILURE |
| 332 |
|
| 333 |
QVERIFY(nRequests == 0); |
| 334 |
QVERIFY(prefaceOK); |
| 335 |
QVERIFY(serverGotSettingsACK); |
| 336 |
} |
| 337 |
|
| 338 |
void tst_Http2::flowControlClientSide() |
| 339 |
{ |
| 340 |
// Create a server but impose limits: |
| 341 |
// 1. Small client receive windows so server's responses cause client |
| 342 |
// streams to suspend and protocol handler has to send WINDOW_UPDATE |
| 343 |
// frames. |
| 344 |
// 2. Few concurrent streams supported by the server, to test protocol |
| 345 |
// handler in the client can suspend and then resume streams. |
| 346 |
using namespace Http2; |
| 347 |
|
| 348 |
clearHTTP2State(); |
| 349 |
|
| 350 |
serverPort = 0; |
| 351 |
nRequests = 10; |
| 352 |
windowUpdates = 0; |
| 353 |
|
| 354 |
QHttp2Configuration params; |
| 355 |
// A small window size for a session, and even a smaller one per stream - |
| 356 |
// this will result in WINDOW_UPDATE frames both on connection stream and |
| 357 |
// per stream. |
| 358 |
params.setSessionReceiveWindowSize(Http2::defaultSessionWindowSize * 5); |
| 359 |
params.setStreamReceiveWindowSize(Http2::defaultSessionWindowSize); |
| 360 |
|
| 361 |
const RawSettings serverSettings = {{Settings::MAX_CONCURRENT_STREAMS_ID, quint32(3)}}; |
| 362 |
ServerPtr srv(newServer(serverSettings, defaultConnectionType(), qt_H2ConfigurationToSettings(params))); |
| 363 |
|
| 364 |
const QByteArray respond(int(Http2::defaultSessionWindowSize * 10), 'x'); |
| 365 |
srv->setResponseBody(respond); |
| 366 |
|
| 367 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 368 |
|
| 369 |
runEventLoop(); |
| 370 |
QVERIFY(serverPort != 0); |
| 371 |
|
| 372 |
for (int i = 0; i < nRequests; ++i) |
| 373 |
sendRequest(i, QNetworkRequest::NormalPriority, {}, params); |
| 374 |
|
| 375 |
runEventLoop(120000); |
| 376 |
STOP_ON_FAILURE |
| 377 |
|
| 378 |
QVERIFY(nRequests == 0); |
| 379 |
QVERIFY(prefaceOK); |
| 380 |
QVERIFY(serverGotSettingsACK); |
| 381 |
QVERIFY(windowUpdates > 0); |
| 382 |
} |
| 383 |
|
| 384 |
void tst_Http2::flowControlServerSide() |
| 385 |
{ |
| 386 |
// Quite aggressive test: |
| 387 |
// low MAX_FRAME_SIZE forces a lot of small DATA frames, |
| 388 |
// payload size exceedes stream/session RECV window sizes |
| 389 |
// so that our implementation should deal with WINDOW_UPDATE |
| 390 |
// on a session/stream level correctly + resume/suspend streams |
| 391 |
// to let all replies finish without any error. |
| 392 |
using namespace Http2; |
| 393 |
|
| 394 |
if (EmulationDetector::isRunningArmOnX86()) |
| 395 |
QSKIP("Test is too slow to run on emulator"); |
| 396 |
|
| 397 |
clearHTTP2State(); |
| 398 |
|
| 399 |
serverPort = 0; |
| 400 |
nRequests = 10; |
| 401 |
|
| 402 |
const RawSettings serverSettings = {{Settings::MAX_CONCURRENT_STREAMS_ID, 7}}; |
| 403 |
|
| 404 |
ServerPtr srv(newServer(serverSettings, defaultConnectionType())); |
| 405 |
|
| 406 |
const QByteArray payload(int(Http2::defaultSessionWindowSize * 500), 'x'); |
| 407 |
|
| 408 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 409 |
|
| 410 |
runEventLoop(); |
| 411 |
QVERIFY(serverPort != 0); |
| 412 |
|
| 413 |
for (int i = 0; i < nRequests; ++i) |
| 414 |
sendRequest(i, QNetworkRequest::NormalPriority, payload); |
| 415 |
|
| 416 |
runEventLoop(120000); |
| 417 |
STOP_ON_FAILURE |
| 418 |
|
| 419 |
QVERIFY(nRequests == 0); |
| 420 |
QVERIFY(prefaceOK); |
| 421 |
QVERIFY(serverGotSettingsACK); |
| 422 |
} |
| 423 |
|
| 424 |
void tst_Http2::pushPromise() |
| 425 |
{ |
| 426 |
// We will first send some request, the server should reply and also emulate |
| 427 |
// PUSH_PROMISE sending us another response as promised. |
| 428 |
using namespace Http2; |
| 429 |
|
| 430 |
clearHTTP2State(); |
| 431 |
|
| 432 |
serverPort = 0; |
| 433 |
nRequests = 1; |
| 434 |
|
| 435 |
QHttp2Configuration params; |
| 436 |
// Defaults are good, except ENABLE_PUSH: |
| 437 |
params.setServerPushEnabled(true); |
| 438 |
|
| 439 |
ServerPtr srv(newServer(defaultServerSettings, defaultConnectionType(), qt_H2ConfigurationToSettings(params))); |
| 440 |
srv->enablePushPromise(true, QByteArray("/script.js")); |
| 441 |
|
| 442 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 443 |
runEventLoop(); |
| 444 |
|
| 445 |
QVERIFY(serverPort != 0); |
| 446 |
|
| 447 |
auto url = requestUrl(defaultConnectionType()); |
| 448 |
url.setPath("/index.html"); |
| 449 |
|
| 450 |
QNetworkRequest request(url); |
| 451 |
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, QVariant(true)); |
| 452 |
request.setHttp2Configuration(params); |
| 453 |
|
| 454 |
auto reply = manager->get(request); |
| 455 |
connect(reply, &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 456 |
// Since we're using self-signed certificates, ignore SSL errors: |
| 457 |
reply->ignoreSslErrors(); |
| 458 |
|
| 459 |
runEventLoop(); |
| 460 |
STOP_ON_FAILURE |
| 461 |
|
| 462 |
QVERIFY(nRequests == 0); |
| 463 |
QVERIFY(prefaceOK); |
| 464 |
QVERIFY(serverGotSettingsACK); |
| 465 |
|
| 466 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 467 |
QVERIFY(reply->isFinished()); |
| 468 |
|
| 469 |
// Now, the most interesting part! |
| 470 |
nSentRequests = 0; |
| 471 |
nRequests = 1; |
| 472 |
// Create an additional request (let's say, we parsed reply and realized we |
| 473 |
// need another resource): |
| 474 |
|
| 475 |
url.setPath("/script.js"); |
| 476 |
QNetworkRequest promisedRequest(url); |
| 477 |
promisedRequest.setAttribute(QNetworkRequest::Http2AllowedAttribute, QVariant(true)); |
| 478 |
reply = manager->get(promisedRequest); |
| 479 |
connect(reply, &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 480 |
reply->ignoreSslErrors(); |
| 481 |
|
| 482 |
runEventLoop(); |
| 483 |
|
| 484 |
// Let's check that NO request was actually made: |
| 485 |
QCOMPARE(nSentRequests, 0); |
| 486 |
// Decreased by replyFinished(): |
| 487 |
QCOMPARE(nRequests, 0); |
| 488 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 489 |
QVERIFY(reply->isFinished()); |
| 490 |
} |
| 491 |
|
| 492 |
void tst_Http2::goaway_data() |
| 493 |
{ |
| 494 |
// For now we test only basic things in two very simple scenarios: |
| 495 |
// - server sends GOAWAY immediately or |
| 496 |
// - server waits for some time (enough for ur to init several streams on a |
| 497 |
// client side); then suddenly it replies with GOAWAY, never processing any |
| 498 |
// request. |
| 499 |
if (clearTextHTTP2) |
| 500 |
QSKIP("This test requires TLS with ALPN to work"); |
| 501 |
|
| 502 |
QTest::addColumn<int>("responseTimeoutMS"); |
| 503 |
QTest::newRow("ImmediateGOAWAY") << 0; |
| 504 |
QTest::newRow("DelayedGOAWAY") << 1000; |
| 505 |
} |
| 506 |
|
| 507 |
void tst_Http2::goaway() |
| 508 |
{ |
| 509 |
using namespace Http2; |
| 510 |
|
| 511 |
QFETCH(const int, responseTimeoutMS); |
| 512 |
|
| 513 |
clearHTTP2State(); |
| 514 |
|
| 515 |
serverPort = 0; |
| 516 |
nRequests = 3; |
| 517 |
|
| 518 |
ServerPtr srv(newServer(defaultServerSettings, defaultConnectionType())); |
| 519 |
srv->emulateGOAWAY(responseTimeoutMS); |
| 520 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 521 |
runEventLoop(); |
| 522 |
|
| 523 |
QVERIFY(serverPort != 0); |
| 524 |
|
| 525 |
auto url = requestUrl(defaultConnectionType()); |
| 526 |
// We have to store these replies, so that we can check errors later. |
| 527 |
std::vector<QNetworkReply *> replies(nRequests); |
| 528 |
for (int i = 0; i < nRequests; ++i) { |
| 529 |
url.setPath(QString("/%1").arg(i)); |
| 530 |
QNetworkRequest request(url); |
| 531 |
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, QVariant(true)); |
| 532 |
replies[i] = manager->get(request); |
| 533 |
QCOMPARE(replies[i]->error(), QNetworkReply::NoError); |
| 534 |
connect(replies[i], &QNetworkReply::errorOccurred, this, &tst_Http2::replyFinishedWithError); |
| 535 |
// Since we're using self-signed certificates, ignore SSL errors: |
| 536 |
replies[i]->ignoreSslErrors(); |
| 537 |
} |
| 538 |
|
| 539 |
runEventLoop(5000 + responseTimeoutMS); |
| 540 |
STOP_ON_FAILURE |
| 541 |
|
| 542 |
// No request processed, no 'replyFinished' slot calls: |
| 543 |
QCOMPARE(nRequests, 0); |
| 544 |
// Our server did not bother to send anything except a single GOAWAY frame: |
| 545 |
QVERIFY(!prefaceOK); |
| 546 |
QVERIFY(!serverGotSettingsACK); |
| 547 |
} |
| 548 |
|
| 549 |
void tst_Http2::earlyResponse() |
| 550 |
{ |
| 551 |
// In this test we'd like to verify client side can handle HEADERS frame while |
| 552 |
// its stream is in 'open' state. To achieve this, we send a POST request |
| 553 |
// with some payload, so that the client is first sending HEADERS and then |
| 554 |
// DATA frames without END_STREAM flag set yet (thus the stream is in Stream::open |
| 555 |
// state). Upon receiving the client's HEADERS frame our server ('redirector') |
| 556 |
// immediately (without trying to read any DATA frames) responds with status |
| 557 |
// code 308. The client should properly handle this. |
| 558 |
|
| 559 |
clearHTTP2State(); |
| 560 |
|
| 561 |
serverPort = 0; |
| 562 |
nRequests = 1; |
| 563 |
|
| 564 |
ServerPtr targetServer(newServer(defaultServerSettings, defaultConnectionType())); |
| 565 |
|
| 566 |
QMetaObject::invokeMethod(targetServer.data(), "startServer", Qt::QueuedConnection); |
| 567 |
runEventLoop(); |
| 568 |
|
| 569 |
QVERIFY(serverPort != 0); |
| 570 |
|
| 571 |
const quint16 targetPort = serverPort; |
| 572 |
serverPort = 0; |
| 573 |
|
| 574 |
ServerPtr redirector(newServer(defaultServerSettings, defaultConnectionType())); |
| 575 |
redirector->redirectOpenStream(targetPort); |
| 576 |
|
| 577 |
QMetaObject::invokeMethod(redirector.data(), "startServer", Qt::QueuedConnection); |
| 578 |
runEventLoop(); |
| 579 |
|
| 580 |
QVERIFY(serverPort); |
| 581 |
sendRequest(1, QNetworkRequest::NormalPriority, {1000000, Qt::Uninitialized}); |
| 582 |
|
| 583 |
runEventLoop(); |
| 584 |
STOP_ON_FAILURE |
| 585 |
|
| 586 |
QVERIFY(nRequests == 0); |
| 587 |
QVERIFY(prefaceOK); |
| 588 |
QVERIFY(serverGotSettingsACK); |
| 589 |
} |
| 590 |
|
| 591 |
void tst_Http2::connectToHost_data() |
| 592 |
{ |
| 593 |
// The attribute to set on a new request: |
| 594 |
QTest::addColumn<QNetworkRequest::Attribute>("requestAttribute"); |
| 595 |
// The corresponding (to the attribute above) connection type the |
| 596 |
// server will use: |
| 597 |
QTest::addColumn<H2Type>("connectionType"); |
| 598 |
|
| 599 |
#if QT_CONFIG(ssl) |
| 600 |
QTest::addRow("encrypted-h2-direct") << QNetworkRequest::Http2DirectAttribute << H2Type::h2Direct; |
| 601 |
if (!clearTextHTTP2) |
| 602 |
QTest::addRow("encrypted-h2-ALPN") << QNetworkRequest::Http2AllowedAttribute << H2Type::h2Alpn; |
| 603 |
#endif // QT_CONFIG(ssl) |
| 604 |
// This works for all configurations, tests 'preconnect-http' scheme: |
| 605 |
// h2 with protocol upgrade is not working for now (the logic is a bit |
| 606 |
// complicated there ...). |
| 607 |
QTest::addRow("h2-direct") << QNetworkRequest::Http2DirectAttribute << H2Type::h2cDirect; |
| 608 |
} |
| 609 |
|
| 610 |
void tst_Http2::connectToHost() |
| 611 |
{ |
| 612 |
// QNetworkAccessManager::connectToHostEncrypted() and connectToHost() |
| 613 |
// creates a special request with 'preconnect-https' or 'preconnect-http' |
| 614 |
// schemes. At the level of the protocol handler we are supposed to report |
| 615 |
// these requests as finished and wait for the real requests. This test will |
| 616 |
// connect to a server with the first reply 'finished' signal meaning we |
| 617 |
// indeed connected. At this point we check that a client preface was not |
| 618 |
// sent yet, and no response received. Then we send the second (the real) |
| 619 |
// request and do our usual checks. Since our server closes its listening |
| 620 |
// socket on the first incoming connection (would not accept a new one), |
| 621 |
// the successful completion of the second requests also means we were able |
| 622 |
// to find a cached connection and re-use it. |
| 623 |
|
| 624 |
QFETCH(const QNetworkRequest::Attribute, requestAttribute); |
| 625 |
QFETCH(const H2Type, connectionType); |
| 626 |
|
| 627 |
clearHTTP2State(); |
| 628 |
|
| 629 |
serverPort = 0; |
| 630 |
nRequests = 2; |
| 631 |
|
| 632 |
ServerPtr targetServer(newServer(defaultServerSettings, connectionType)); |
| 633 |
|
| 634 |
#if QT_CONFIG(ssl) |
| 635 |
Q_ASSERT(!clearTextHTTP2 || connectionType != H2Type::h2Alpn); |
| 636 |
|
| 637 |
#if QT_CONFIG(securetransport) |
| 638 |
// Normally on macOS we use plain text only for SecureTransport |
| 639 |
// does not support ALPN on the server side. With 'direct encrytped' |
| 640 |
// we have to use TLS sockets (== private key) and thus suppress a |
| 641 |
// keychain UI asking for permission to use a private key. |
| 642 |
// Our CI has this, but somebody testing locally - will have a problem. |
| 643 |
qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1")); |
| 644 |
auto envRollback = qScopeGuard([](){ |
| 645 |
qunsetenv("QT_SSL_USE_TEMPORARY_KEYCHAIN"); |
| 646 |
}); |
| 647 |
#endif // QT_CONFIG(securetransport) |
| 648 |
|
| 649 |
#else |
| 650 |
Q_ASSERT(connectionType == H2Type::h2c || connectionType == H2Type::h2cDirect); |
| 651 |
Q_ASSERT(targetServer->isClearText()); |
| 652 |
#endif // QT_CONFIG(ssl) |
| 653 |
|
| 654 |
QMetaObject::invokeMethod(targetServer.data(), "startServer", Qt::QueuedConnection); |
| 655 |
runEventLoop(); |
| 656 |
|
| 657 |
QVERIFY(serverPort != 0); |
| 658 |
|
| 659 |
auto url = requestUrl(connectionType); |
| 660 |
url.setPath("/index.html"); |
| 661 |
|
| 662 |
QNetworkReply *reply = nullptr; |
| 663 |
// Here some mess with how we create this first reply: |
| 664 |
#if QT_CONFIG(ssl) |
| 665 |
if (!targetServer->isClearText()) { |
| 666 |
// Let's emulate what QNetworkAccessManager::connectToHostEncrypted() does. |
| 667 |
// Alas, we cannot use it directly, since it does not return the reply and |
| 668 |
// also does not know the difference between H2 with ALPN or direct. |
| 669 |
auto copyUrl = url; |
| 670 |
copyUrl.setScheme(QLatin1String("preconnect-https")); |
| 671 |
QNetworkRequest request(copyUrl); |
| 672 |
request.setAttribute(requestAttribute, true); |
| 673 |
reply = manager->get(request); |
| 674 |
// Since we're using self-signed certificates, ignore SSL errors: |
| 675 |
reply->ignoreSslErrors(); |
| 676 |
} else |
| 677 |
#endif // QT_CONFIG(ssl) |
| 678 |
{ |
| 679 |
// Emulating what QNetworkAccessManager::connectToHost() does with |
| 680 |
// additional information that it cannot provide (the attribute). |
| 681 |
auto copyUrl = url; |
| 682 |
copyUrl.setScheme(QLatin1String("preconnect-http")); |
| 683 |
QNetworkRequest request(copyUrl); |
| 684 |
request.setAttribute(requestAttribute, true); |
| 685 |
reply = manager->get(request); |
| 686 |
} |
| 687 |
|
| 688 |
connect(reply, &QNetworkReply::finished, [this, reply]() { |
| 689 |
--nRequests; |
| 690 |
eventLoop.exitLoop(); |
| 691 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 692 |
QVERIFY(reply->isFinished()); |
| 693 |
// Nothing received back: |
| 694 |
QVERIFY(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isNull()); |
| 695 |
QCOMPARE(reply->readAll().size(), 0); |
| 696 |
}); |
| 697 |
|
| 698 |
runEventLoop(); |
| 699 |
STOP_ON_FAILURE |
| 700 |
|
| 701 |
QCOMPARE(nRequests, 1); |
| 702 |
|
| 703 |
QNetworkRequest request(url); |
| 704 |
request.setAttribute(requestAttribute, QVariant(true)); |
| 705 |
reply = manager->get(request); |
| 706 |
connect(reply, &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 707 |
// Note, unlike the first request, when the connection is ecnrytped, we |
| 708 |
// do not ignore TLS errors on this reply - we should re-use existing |
| 709 |
// connection, there TLS errors were already ignored. |
| 710 |
|
| 711 |
runEventLoop(); |
| 712 |
STOP_ON_FAILURE |
| 713 |
|
| 714 |
QVERIFY(nRequests == 0); |
| 715 |
QVERIFY(prefaceOK); |
| 716 |
QVERIFY(serverGotSettingsACK); |
| 717 |
|
| 718 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 719 |
QVERIFY(reply->isFinished()); |
| 720 |
} |
| 721 |
|
| 722 |
void tst_Http2::maxFrameSize() |
| 723 |
{ |
| 724 |
#if !QT_CONFIG(ssl) |
| 725 |
QSKIP("TLS support is needed for this test"); |
| 726 |
#endif // QT_CONFIG(ssl) |
| 727 |
|
| 728 |
// Here we test we send 'MAX_FRAME_SIZE' setting in our |
| 729 |
// 'SETTINGS'. If done properly, our server will not chunk |
| 730 |
// the payload into several DATA frames. |
| 731 |
|
| 732 |
#if QT_CONFIG(securetransport) |
| 733 |
// Normally on macOS we use plain text only for SecureTransport |
| 734 |
// does not support ALPN on the server side. With 'direct encrytped' |
| 735 |
// we have to use TLS sockets (== private key) and thus suppress a |
| 736 |
// keychain UI asking for permission to use a private key. |
| 737 |
// Our CI has this, but somebody testing locally - will have a problem. |
| 738 |
qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1")); |
| 739 |
auto envRollback = qScopeGuard([](){ |
| 740 |
qunsetenv("QT_SSL_USE_TEMPORARY_KEYCHAIN"); |
| 741 |
}); |
| 742 |
#endif // QT_CONFIG(securetransport) |
| 743 |
|
| 744 |
auto connectionType = H2Type::h2Alpn; |
| 745 |
auto attribute = QNetworkRequest::Http2AllowedAttribute; |
| 746 |
if (clearTextHTTP2) { |
| 747 |
connectionType = H2Type::h2Direct; |
| 748 |
attribute = QNetworkRequest::Http2DirectAttribute; |
| 749 |
} |
| 750 |
|
| 751 |
auto h2Config = qt_defaultH2Configuration(); |
| 752 |
h2Config.setMaxFrameSize(Http2::minPayloadLimit * 3); |
| 753 |
|
| 754 |
serverPort = 0; |
| 755 |
nRequests = 1; |
| 756 |
|
| 757 |
ServerPtr srv(newServer(defaultServerSettings, connectionType, |
| 758 |
qt_H2ConfigurationToSettings(h2Config))); |
| 759 |
srv->setResponseBody(QByteArray(Http2::minPayloadLimit * 2, 'q')); |
| 760 |
QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); |
| 761 |
runEventLoop(); |
| 762 |
QVERIFY(serverPort != 0); |
| 763 |
|
| 764 |
const QSignalSpy frameCounter(srv.data(), &Http2Server::sendingData); |
| 765 |
auto url = requestUrl(connectionType); |
| 766 |
url.setPath(QString("/stream1.html")); |
| 767 |
|
| 768 |
QNetworkRequest request(url); |
| 769 |
request.setAttribute(attribute, QVariant(true)); |
| 770 |
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); |
| 771 |
request.setHttp2Configuration(h2Config); |
| 772 |
|
| 773 |
QNetworkReply *reply = manager->get(request); |
| 774 |
reply->ignoreSslErrors(); |
| 775 |
connect(reply, &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 776 |
|
| 777 |
runEventLoop(); |
| 778 |
STOP_ON_FAILURE |
| 779 |
|
| 780 |
// Normally, with a 16kb limit, our server would split such |
| 781 |
// a response into 3 'DATA' frames (16kb + 16kb + 0|END_STREAM). |
| 782 |
QCOMPARE(frameCounter.count(), 1); |
| 783 |
|
| 784 |
QVERIFY(nRequests == 0); |
| 785 |
QVERIFY(prefaceOK); |
| 786 |
QVERIFY(serverGotSettingsACK); |
| 787 |
} |
| 788 |
|
| 789 |
void tst_Http2::http2DATAFrames() |
| 790 |
{ |
| 791 |
using namespace Http2; |
| 792 |
|
| 793 |
{ |
| 794 |
// 0. DATA frame with payload, no padding. |
| 795 |
|
| 796 |
FrameWriter writer(FrameType::DATA, FrameFlag::EMPTY, 1); |
| 797 |
writer.append(uchar(1)); |
| 798 |
writer.append(uchar(2)); |
| 799 |
writer.append(uchar(3)); |
| 800 |
|
| 801 |
const Frame frame = writer.outboundFrame(); |
| 802 |
const auto &buffer = frame.buffer; |
| 803 |
// Frame's header is 9 bytes + 3 bytes of payload |
| 804 |
// (+ 0 bytes of padding and no padding length): |
| 805 |
QCOMPARE(int(buffer.size()), 12); |
| 806 |
|
| 807 |
QVERIFY(!frame.padding()); |
| 808 |
QCOMPARE(int(frame.payloadSize()), 3); |
| 809 |
QCOMPARE(int(frame.dataSize()), 3); |
| 810 |
QCOMPARE(frame.dataBegin() - buffer.data(), 9); |
| 811 |
QCOMPARE(char(*frame.dataBegin()), uchar(1)); |
| 812 |
} |
| 813 |
|
| 814 |
{ |
| 815 |
// 1. DATA with padding. |
| 816 |
|
| 817 |
const int padLength = 10; |
| 818 |
FrameWriter writer(FrameType::DATA, FrameFlag::END_STREAM | FrameFlag::PADDED, 1); |
| 819 |
writer.append(uchar(padLength)); // The length of padding is 1 byte long. |
| 820 |
writer.append(uchar(1)); |
| 821 |
for (int i = 0; i < padLength; ++i) |
| 822 |
writer.append(uchar(0)); |
| 823 |
|
| 824 |
const Frame frame = writer.outboundFrame(); |
| 825 |
const auto &buffer = frame.buffer; |
| 826 |
// Frame's header is 9 bytes + 1 byte for padding length |
| 827 |
// + 1 byte of data + 10 bytes of padding: |
| 828 |
QCOMPARE(int(buffer.size()), 21); |
| 829 |
|
| 830 |
QCOMPARE(frame.padding(), padLength); |
| 831 |
QCOMPARE(int(frame.payloadSize()), 12); // Includes padding, its length + data. |
| 832 |
QCOMPARE(int(frame.dataSize()), 1); |
| 833 |
|
| 834 |
// Skipping 9 bytes long header and padding length: |
| 835 |
QCOMPARE(frame.dataBegin() - buffer.data(), 10); |
| 836 |
|
| 837 |
QCOMPARE(char(frame.dataBegin()[0]), uchar(1)); |
| 838 |
QCOMPARE(char(frame.dataBegin()[1]), uchar(0)); |
| 839 |
|
| 840 |
QVERIFY(frame.flags().testFlag(FrameFlag::END_STREAM)); |
| 841 |
QVERIFY(frame.flags().testFlag(FrameFlag::PADDED)); |
| 842 |
} |
| 843 |
{ |
| 844 |
// 2. DATA with PADDED flag, but 0 as padding length. |
| 845 |
|
| 846 |
FrameWriter writer(FrameType::DATA, FrameFlag::END_STREAM | FrameFlag::PADDED, 1); |
| 847 |
|
| 848 |
writer.append(uchar(0)); // Number of padding bytes is 1 byte long. |
| 849 |
writer.append(uchar(1)); |
| 850 |
|
| 851 |
const Frame frame = writer.outboundFrame(); |
| 852 |
const auto &buffer = frame.buffer; |
| 853 |
|
| 854 |
// Frame's header is 9 bytes + 1 byte for padding length + 1 byte of data |
| 855 |
// + 0 bytes of padding: |
| 856 |
QCOMPARE(int(buffer.size()), 11); |
| 857 |
|
| 858 |
QCOMPARE(frame.padding(), 0); |
| 859 |
QCOMPARE(int(frame.payloadSize()), 2); // Includes padding (0 bytes), its length + data. |
| 860 |
QCOMPARE(int(frame.dataSize()), 1); |
| 861 |
|
| 862 |
// Skipping 9 bytes long header and padding length: |
| 863 |
QCOMPARE(frame.dataBegin() - buffer.data(), 10); |
| 864 |
|
| 865 |
QCOMPARE(char(*frame.dataBegin()), uchar(1)); |
| 866 |
|
| 867 |
QVERIFY(frame.flags().testFlag(FrameFlag::END_STREAM)); |
| 868 |
QVERIFY(frame.flags().testFlag(FrameFlag::PADDED)); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
void tst_Http2::authenticationRequired_data() |
| 873 |
{ |
| 874 |
QTest::addColumn<bool>("success"); |
| 875 |
QTest::addColumn<bool>("responseHEADOnly"); |
| 876 |
|
| 877 |
QTest::addRow("failed-auth") << false << true; |
| 878 |
QTest::addRow("successful-auth") << true << true; |
| 879 |
// Include a DATA frame in the response from the remote server. An example would be receiving a |
| 880 |
// JSON response on a request along with the 401 error. |
| 881 |
QTest::addRow("failed-auth-with-response") << false << false; |
| 882 |
QTest::addRow("successful-auth-with-response") << true << false; |
| 883 |
} |
| 884 |
|
| 885 |
void tst_Http2::authenticationRequired() |
| 886 |
{ |
| 887 |
clearHTTP2State(); |
| 888 |
serverPort = 0; |
| 889 |
QFETCH(const bool, responseHEADOnly); |
| 890 |
POSTResponseHEADOnly = responseHEADOnly; |
| 891 |
|
| 892 |
QFETCH(const bool, success); |
| 893 |
|
| 894 |
ServerPtr targetServer(newServer(defaultServerSettings, defaultConnectionType())); |
| 895 |
targetServer->setResponseBody("Hello"); |
| 896 |
targetServer->setAuthenticationHeader("Basic realm=\"Shadow\""); |
| 897 |
|
| 898 |
QMetaObject::invokeMethod(targetServer.data(), "startServer", Qt::QueuedConnection); |
| 899 |
runEventLoop(); |
| 900 |
|
| 901 |
QVERIFY(serverPort != 0); |
| 902 |
|
| 903 |
nRequests = 1; |
| 904 |
|
| 905 |
auto url = requestUrl(defaultConnectionType()); |
| 906 |
url.setPath("/index.html"); |
| 907 |
QNetworkRequest request(url); |
| 908 |
|
| 909 |
QByteArray expectedBody = "Hello, World!"; |
| 910 |
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); |
| 911 |
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, QVariant(true)); |
| 912 |
QScopedPointer<QNetworkReply> reply; |
| 913 |
reply.reset(manager->post(request, expectedBody)); |
| 914 |
|
| 915 |
bool authenticationRequested = false; |
| 916 |
connect(manager.get(), &QNetworkAccessManager::authenticationRequired, reply.get(), |
| 917 |
[&](QNetworkReply *, QAuthenticator *auth) { |
| 918 |
authenticationRequested = true; |
| 919 |
if (success) { |
| 920 |
auth->setUser("admin"); |
| 921 |
auth->setPassword("admin"); |
| 922 |
} |
| 923 |
}); |
| 924 |
|
| 925 |
QByteArray receivedBody; |
| 926 |
connect(targetServer.get(), &Http2Server::receivedDATAFrame, reply.get(), |
| 927 |
[&receivedBody](quint32 streamID, const QByteArray &body) { |
| 928 |
if (streamID == 3) // The expected body is on the retry, so streamID == 3 |
| 929 |
receivedBody += body; |
| 930 |
}); |
| 931 |
|
| 932 |
if (success) |
| 933 |
connect(reply.get(), &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 934 |
else |
| 935 |
connect(reply.get(), &QNetworkReply::errorOccurred, this, &tst_Http2::replyFinishedWithError); |
| 936 |
// Since we're using self-signed certificates, |
| 937 |
// ignore SSL errors: |
| 938 |
reply->ignoreSslErrors(); |
| 939 |
|
| 940 |
runEventLoop(); |
| 941 |
STOP_ON_FAILURE |
| 942 |
|
| 943 |
if (!success) |
| 944 |
QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError); |
| 945 |
// else: no error (is checked in tst_Http2::replyFinished) |
| 946 |
|
| 947 |
QVERIFY(authenticationRequested); |
| 948 |
|
| 949 |
const auto isAuthenticated = [](QByteArray bv) { |
| 950 |
return bv == "Basic YWRtaW46YWRtaW4="; // admin:admin |
| 951 |
}; |
| 952 |
// Get the "authorization" header out from the server and make sure it's as expected: |
| 953 |
auto reqAuthHeader = targetServer->requestAuthorizationHeader(); |
| 954 |
QCOMPARE(isAuthenticated(reqAuthHeader), success); |
| 955 |
if (success) |
| 956 |
QCOMPARE(receivedBody, expectedBody); |
| 957 |
// In the `!success` case we need to wait for the server to emit this or it might cause issues |
| 958 |
// in the next test running after this. In the `success` case we anyway expect it to have been |
| 959 |
// received. |
| 960 |
QTRY_VERIFY(serverGotSettingsACK); |
| 961 |
} |
| 962 |
|
| 963 |
void tst_Http2::redirect_data() |
| 964 |
{ |
| 965 |
QTest::addColumn<int>("maxRedirects"); |
| 966 |
QTest::addColumn<int>("redirectCount"); |
| 967 |
QTest::addColumn<bool>("success"); |
| 968 |
|
| 969 |
QTest::addRow("1-redirects-none-allowed-failure") << 0 << 1 << false; |
| 970 |
QTest::addRow("1-redirects-success") << 1 << 1 << true; |
| 971 |
QTest::addRow("2-redirects-1-allowed-failure") << 1 << 2 << false; |
| 972 |
} |
| 973 |
|
| 974 |
void tst_Http2::redirect() |
| 975 |
{ |
| 976 |
QFETCH(const int, maxRedirects); |
| 977 |
QFETCH(const int, redirectCount); |
| 978 |
QFETCH(const bool, success); |
| 979 |
const QByteArray redirectUrl = "/b.html"; |
| 980 |
|
| 981 |
clearHTTP2State(); |
| 982 |
serverPort = 0; |
| 983 |
|
| 984 |
ServerPtr targetServer(newServer(defaultServerSettings, defaultConnectionType())); |
| 985 |
targetServer->setRedirect(redirectUrl, redirectCount); |
| 986 |
|
| 987 |
QMetaObject::invokeMethod(targetServer.data(), "startServer", Qt::QueuedConnection); |
| 988 |
runEventLoop(); |
| 989 |
|
| 990 |
QVERIFY(serverPort != 0); |
| 991 |
|
| 992 |
nRequests = 1 + maxRedirects; |
| 993 |
|
| 994 |
auto originalUrl = requestUrl(defaultConnectionType()); |
| 995 |
auto url = originalUrl; |
| 996 |
url.setPath("/index.html"); |
| 997 |
QNetworkRequest request(url); |
| 998 |
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, QVariant(true)); |
| 999 |
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); |
| 1000 |
request.setMaximumRedirectsAllowed(maxRedirects); |
| 1001 |
|
| 1002 |
QScopedPointer<QNetworkReply> reply; |
| 1003 |
reply.reset(manager->get(request)); |
| 1004 |
|
| 1005 |
if (success) { |
| 1006 |
connect(reply.get(), &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 1007 |
} else { |
| 1008 |
connect(reply.get(), &QNetworkReply::errorOccurred, this, |
| 1009 |
&tst_Http2::replyFinishedWithError); |
| 1010 |
} |
| 1011 |
|
| 1012 |
// Since we're using self-signed certificates, |
| 1013 |
// ignore SSL errors: |
| 1014 |
reply->ignoreSslErrors(); |
| 1015 |
|
| 1016 |
runEventLoop(); |
| 1017 |
STOP_ON_FAILURE |
| 1018 |
|
| 1019 |
if (success) { |
| 1020 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 1021 |
QCOMPARE(reply->url().toString(), |
| 1022 |
originalUrl.resolved(QString::fromLatin1(redirectUrl)).toString()); |
| 1023 |
} else if (maxRedirects < redirectCount) { |
| 1024 |
QCOMPARE(reply->error(), QNetworkReply::TooManyRedirectsError); |
| 1025 |
} |
| 1026 |
QTRY_VERIFY(serverGotSettingsACK); |
| 1027 |
} |
| 1028 |
|
| 1029 |
void tst_Http2::abortOnEncrypted() |
| 1030 |
{ |
| 1031 |
#if !QT_CONFIG(ssl) |
| 1032 |
QSKIP("TLS support is needed for this test"); |
| 1033 |
#else |
| 1034 |
|
| 1035 |
#if QT_CONFIG(securetransport) |
| 1036 |
// Normally on macOS we use plain text only for SecureTransport |
| 1037 |
// does not support ALPN on the server side. With 'direct encrytped' |
| 1038 |
// we have to use TLS sockets (== private key) and thus suppress a |
| 1039 |
// keychain UI asking for permission to use a private key. |
| 1040 |
// Our CI has this, but somebody testing locally - will have a problem. |
| 1041 |
qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1")); |
| 1042 |
auto envRollback = qScopeGuard([](){ |
| 1043 |
qunsetenv("QT_SSL_USE_TEMPORARY_KEYCHAIN"); |
| 1044 |
}); |
| 1045 |
#endif |
| 1046 |
|
| 1047 |
clearHTTP2State(); |
| 1048 |
serverPort = 0; |
| 1049 |
|
| 1050 |
ServerPtr targetServer(newServer(defaultServerSettings, H2Type::h2Direct)); |
| 1051 |
|
| 1052 |
QMetaObject::invokeMethod(targetServer.data(), "startServer", Qt::QueuedConnection); |
| 1053 |
runEventLoop(); |
| 1054 |
|
| 1055 |
nRequests = 1; |
| 1056 |
nSentRequests = 0; |
| 1057 |
|
| 1058 |
const auto url = requestUrl(H2Type::h2Direct); |
| 1059 |
QNetworkRequest request(url); |
| 1060 |
request.setAttribute(QNetworkRequest::Http2DirectAttribute, true); |
| 1061 |
|
| 1062 |
std::unique_ptr<QNetworkReply> reply{manager->get(request)}; |
| 1063 |
reply->ignoreSslErrors(); |
| 1064 |
connect(reply.get(), &QNetworkReply::encrypted, reply.get(), [reply = reply.get()](){ |
| 1065 |
reply->abort(); |
| 1066 |
}); |
| 1067 |
connect(reply.get(), &QNetworkReply::errorOccurred, this, &tst_Http2::replyFinishedWithError); |
| 1068 |
|
| 1069 |
runEventLoop(); |
| 1070 |
STOP_ON_FAILURE |
| 1071 |
|
| 1072 |
QCOMPARE(nRequests, 0); |
| 1073 |
QCOMPARE(reply->error(), QNetworkReply::OperationCanceledError); |
| 1074 |
|
| 1075 |
const bool res = QTest::qWaitFor( |
| 1076 |
[this, server = targetServer.get()]() { |
| 1077 |
return serverGotSettingsACK || prefaceOK || nSentRequests > 0; |
| 1078 |
}, |
| 1079 |
500); |
| 1080 |
QVERIFY(!res); |
| 1081 |
#endif // QT_CONFIG(ssl) |
| 1082 |
} |
| 1083 |
|
| 1084 |
void tst_Http2::serverStarted(quint16 port) |
| 1085 |
{ |
| 1086 |
serverPort = port; |
| 1087 |
stopEventLoop(); |
| 1088 |
} |
| 1089 |
|
| 1090 |
void tst_Http2::clearHTTP2State() |
| 1091 |
{ |
| 1092 |
windowUpdates = 0; |
| 1093 |
prefaceOK = false; |
| 1094 |
serverGotSettingsACK = false; |
| 1095 |
POSTResponseHEADOnly = true; |
| 1096 |
} |
| 1097 |
|
| 1098 |
void tst_Http2::runEventLoop(int ms) |
| 1099 |
{ |
| 1100 |
eventLoop.enterLoopMSecs(ms); |
| 1101 |
} |
| 1102 |
|
| 1103 |
void tst_Http2::stopEventLoop() |
| 1104 |
{ |
| 1105 |
eventLoop.exitLoop(); |
| 1106 |
} |
| 1107 |
|
| 1108 |
Http2Server *tst_Http2::newServer(const RawSettings &serverSettings, H2Type connectionType, |
| 1109 |
const RawSettings &clientSettings) |
| 1110 |
{ |
| 1111 |
using namespace Http2; |
| 1112 |
auto srv = new Http2Server(connectionType, serverSettings, clientSettings); |
| 1113 |
|
| 1114 |
using Srv = Http2Server; |
| 1115 |
using Cl = tst_Http2; |
| 1116 |
|
| 1117 |
connect(srv, &Srv::serverStarted, this, &Cl::serverStarted); |
| 1118 |
connect(srv, &Srv::clientPrefaceOK, this, &Cl::clientPrefaceOK); |
| 1119 |
connect(srv, &Srv::clientPrefaceError, this, &Cl::clientPrefaceError); |
| 1120 |
connect(srv, &Srv::serverSettingsAcked, this, &Cl::serverSettingsAcked); |
| 1121 |
connect(srv, &Srv::invalidFrame, this, &Cl::invalidFrame); |
| 1122 |
connect(srv, &Srv::invalidRequest, this, &Cl::invalidRequest); |
| 1123 |
connect(srv, &Srv::receivedRequest, this, &Cl::receivedRequest); |
| 1124 |
connect(srv, &Srv::receivedData, this, &Cl::receivedData); |
| 1125 |
connect(srv, &Srv::windowUpdate, this, &Cl::windowUpdated); |
| 1126 |
|
| 1127 |
srv->moveToThread(workerThread); |
| 1128 |
|
| 1129 |
return srv; |
| 1130 |
} |
| 1131 |
|
| 1132 |
void tst_Http2::sendRequest(int streamNumber, |
| 1133 |
QNetworkRequest::Priority priority, |
| 1134 |
const QByteArray &payload, |
| 1135 |
const QHttp2Configuration &h2Config) |
| 1136 |
{ |
| 1137 |
auto url = requestUrl(defaultConnectionType()); |
| 1138 |
url.setPath(QString("/stream%1.html").arg(streamNumber)); |
| 1139 |
|
| 1140 |
QNetworkRequest request(url); |
| 1141 |
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, QVariant(true)); |
| 1142 |
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, QVariant(true)); |
| 1143 |
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); |
| 1144 |
request.setPriority(priority); |
| 1145 |
request.setHttp2Configuration(h2Config); |
| 1146 |
|
| 1147 |
QNetworkReply *reply = nullptr; |
| 1148 |
if (payload.size()) |
| 1149 |
reply = manager->post(request, payload); |
| 1150 |
else |
| 1151 |
reply = manager->get(request); |
| 1152 |
|
| 1153 |
reply->ignoreSslErrors(); |
| 1154 |
connect(reply, &QNetworkReply::finished, this, &tst_Http2::replyFinished); |
| 1155 |
} |
| 1156 |
|
| 1157 |
QUrl tst_Http2::requestUrl(H2Type connectionType) const |
| 1158 |
{ |
| 1159 |
#if !QT_CONFIG(ssl) |
| 1160 |
Q_ASSERT(connectionType != H2Type::h2Alpn && connectionType != H2Type::h2Direct); |
| 1161 |
#endif |
| 1162 |
static auto url = QUrl(QLatin1String(clearTextHTTP2 ? "http://127.0.0.1" : "https://127.0.0.1")); |
| 1163 |
url.setPort(serverPort); |
| 1164 |
// Clear text may mean no-TLS-at-all or crappy-TLS-without-ALPN. |
| 1165 |
switch (connectionType) { |
| 1166 |
case H2Type::h2Alpn: |
| 1167 |
case H2Type::h2Direct: |
| 1168 |
url.setScheme(QStringLiteral("https")); |
| 1169 |
break; |
| 1170 |
case H2Type::h2c: |
| 1171 |
case H2Type::h2cDirect: |
| 1172 |
url.setScheme(QStringLiteral("http")); |
| 1173 |
break; |
| 1174 |
} |
| 1175 |
|
| 1176 |
return url; |
| 1177 |
} |
| 1178 |
|
| 1179 |
void tst_Http2::clientPrefaceOK() |
| 1180 |
{ |
| 1181 |
prefaceOK = true; |
| 1182 |
} |
| 1183 |
|
| 1184 |
void tst_Http2::clientPrefaceError() |
| 1185 |
{ |
| 1186 |
prefaceOK = false; |
| 1187 |
} |
| 1188 |
|
| 1189 |
void tst_Http2::serverSettingsAcked() |
| 1190 |
{ |
| 1191 |
serverGotSettingsACK = true; |
| 1192 |
if (!nRequests) |
| 1193 |
stopEventLoop(); |
| 1194 |
} |
| 1195 |
|
| 1196 |
void tst_Http2::invalidFrame() |
| 1197 |
{ |
| 1198 |
} |
| 1199 |
|
| 1200 |
void tst_Http2::invalidRequest(quint32 streamID) |
| 1201 |
{ |
| 1202 |
Q_UNUSED(streamID) |
| 1203 |
} |
| 1204 |
|
| 1205 |
void tst_Http2::decompressionFailed(quint32 streamID) |
| 1206 |
{ |
| 1207 |
Q_UNUSED(streamID) |
| 1208 |
} |
| 1209 |
|
| 1210 |
void tst_Http2::receivedRequest(quint32 streamID) |
| 1211 |
{ |
| 1212 |
++nSentRequests; |
| 1213 |
qDebug() << " server got a request on stream" << streamID; |
| 1214 |
Http2Server *srv = qobject_cast<Http2Server *>(sender()); |
| 1215 |
Q_ASSERT(srv); |
| 1216 |
QMetaObject::invokeMethod(srv, "sendResponse", Qt::QueuedConnection, |
| 1217 |
Q_ARG(quint32, streamID), |
| 1218 |
Q_ARG(bool, false /*non-empty body*/)); |
| 1219 |
} |
| 1220 |
|
| 1221 |
void tst_Http2::receivedData(quint32 streamID) |
| 1222 |
{ |
| 1223 |
qDebug() << " server got a 'POST' request on stream" << streamID; |
| 1224 |
Http2Server *srv = qobject_cast<Http2Server *>(sender()); |
| 1225 |
Q_ASSERT(srv); |
| 1226 |
QMetaObject::invokeMethod(srv, "sendResponse", Qt::QueuedConnection, |
| 1227 |
Q_ARG(quint32, streamID), |
| 1228 |
Q_ARG(bool, POSTResponseHEADOnly /*true = HEADERS only*/)); |
| 1229 |
} |
| 1230 |
|
| 1231 |
void tst_Http2::windowUpdated(quint32 streamID) |
| 1232 |
{ |
| 1233 |
Q_UNUSED(streamID) |
| 1234 |
|
| 1235 |
++windowUpdates; |
| 1236 |
} |
| 1237 |
|
| 1238 |
void tst_Http2::replyFinished() |
| 1239 |
{ |
| 1240 |
QVERIFY(nRequests); |
| 1241 |
|
| 1242 |
if (const auto reply = qobject_cast<QNetworkReply *>(sender())) { |
| 1243 |
if (reply->error() != QNetworkReply::NoError) |
| 1244 |
stopEventLoop(); |
| 1245 |
|
| 1246 |
QCOMPARE(reply->error(), QNetworkReply::NoError); |
| 1247 |
|
| 1248 |
const QVariant http2Used(reply->attribute(QNetworkRequest::Http2WasUsedAttribute)); |
| 1249 |
if (!http2Used.isValid() || !http2Used.toBool()) |
| 1250 |
stopEventLoop(); |
| 1251 |
|
| 1252 |
QVERIFY(http2Used.isValid()); |
| 1253 |
QVERIFY(http2Used.toBool()); |
| 1254 |
|
| 1255 |
const QVariant spdyUsed(reply->attribute(QNetworkRequest::SpdyWasUsedAttribute)); |
| 1256 |
if (!spdyUsed.isValid() || spdyUsed.toBool()) |
| 1257 |
stopEventLoop(); |
| 1258 |
|
| 1259 |
QVERIFY(spdyUsed.isValid()); |
| 1260 |
QVERIFY(!spdyUsed.toBool()); |
| 1261 |
|
| 1262 |
const QVariant code(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)); |
| 1263 |
if (!code.isValid() || !code.canConvert<int>() || code.value<int>() != 200) |
| 1264 |
stopEventLoop(); |
| 1265 |
|
| 1266 |
QVERIFY(code.isValid()); |
| 1267 |
QVERIFY(code.canConvert<int>()); |
| 1268 |
QCOMPARE(code.value<int>(), 200); |
| 1269 |
} |
| 1270 |
|
| 1271 |
--nRequests; |
| 1272 |
if (!nRequests && serverGotSettingsACK) |
| 1273 |
stopEventLoop(); |
| 1274 |
} |
| 1275 |
|
| 1276 |
void tst_Http2::replyFinishedWithError() |
| 1277 |
{ |
| 1278 |
QVERIFY(nRequests); |
| 1279 |
|
| 1280 |
if (const auto reply = qobject_cast<QNetworkReply *>(sender())) { |
| 1281 |
// For now this is a 'generic' code, it just verifies some error was |
| 1282 |
// reported without testing its type. |
| 1283 |
if (reply->error() == QNetworkReply::NoError) |
| 1284 |
stopEventLoop(); |
| 1285 |
QVERIFY(reply->error() != QNetworkReply::NoError); |
| 1286 |
} |
| 1287 |
|
| 1288 |
--nRequests; |
| 1289 |
if (!nRequests) |
| 1290 |
stopEventLoop(); |
| 1291 |
} |
| 1292 |
|
| 1293 |
QT_END_NAMESPACE |
| 1294 |
|
| 1295 |
QTEST_MAIN(tst_Http2) |
| 1296 |
|
| 1297 |
#include "tst_http2.moc" |