Skip to content

Commit 0f0ee9b

Browse files
committed
More SSL error handling tests
1 parent b23f08f commit 0f0ee9b

File tree

1 file changed

+95
-2
lines changed

1 file changed

+95
-2
lines changed

tests/src/core/testqgsnetworkaccessmanager.cpp

+95-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ class BackgroundRequest : public QThread
5959
QNetworkRequest mRequest;
6060
};
6161

62+
class TestSslErrorHandler : public QgsSslErrorHandler
63+
{
64+
Q_OBJECT
65+
66+
protected:
67+
68+
void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors ) override
69+
{
70+
QCOMPARE( errors.at( 0 ).error(), QSslError::SelfSignedCertificate );
71+
reply->ignoreSslErrors();
72+
}
73+
74+
};
75+
6276
class TestQgsNetworkAccessManager : public QObject
6377
{
6478
Q_OBJECT
@@ -75,6 +89,7 @@ class TestQgsNetworkAccessManager : public QObject
7589
void fetchEncodedContent(); //test fetching url content encoded as utf-8
7690
void fetchPost();
7791
void fetchBadSsl();
92+
void testSslErrorHandler();
7893
void fetchTimeout();
7994

8095
};
@@ -329,6 +344,7 @@ void TestQgsNetworkAccessManager::fetchBadSsl()
329344
bool loaded = false;
330345
bool gotRequestAboutToBeCreatedSignal = false;
331346
bool gotSslError = false;
347+
bool gotRequestEncounteredSslError = false;
332348
int requestId = -1;
333349
QUrl u = QUrl( QStringLiteral( "https://expired.badssl.com" ) );
334350
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( const QgsNetworkRequestParameters & params )
@@ -351,9 +367,83 @@ void TestQgsNetworkAccessManager::fetchBadSsl()
351367
QCOMPARE( errors.at( 0 ).error(), QSslError::CertificateExpired );
352368
gotSslError = true;
353369
} );
370+
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::requestEncounteredSslErrors, &context, [&]( int errorRequestId, const QList<QSslError> &errors )
371+
{
372+
QCOMPARE( errors.at( 0 ).error(), QSslError::CertificateExpired );
373+
QCOMPARE( errorRequestId, requestId );
374+
gotRequestEncounteredSslError = true;
375+
} );
376+
QgsNetworkAccessManager::instance()->get( QNetworkRequest( u ) );
377+
378+
while ( !loaded || !gotSslError || !gotRequestAboutToBeCreatedSignal || !gotRequestEncounteredSslError )
379+
{
380+
qApp->processEvents();
381+
}
382+
383+
QVERIFY( gotRequestAboutToBeCreatedSignal );
384+
385+
gotRequestAboutToBeCreatedSignal = false;
386+
loaded = false;
387+
gotSslError = false;
388+
gotRequestEncounteredSslError = false;
389+
BackgroundRequest *thread = new BackgroundRequest( QNetworkRequest( u ) );
390+
391+
thread->start();
392+
393+
while ( !loaded || !gotSslError || !gotRequestAboutToBeCreatedSignal || !gotRequestEncounteredSslError )
394+
{
395+
qApp->processEvents();
396+
}
397+
QVERIFY( gotRequestAboutToBeCreatedSignal );
398+
thread->exit();
399+
thread->wait();
400+
thread->deleteLater();
401+
}
402+
403+
void TestQgsNetworkAccessManager::testSslErrorHandler()
404+
{
405+
if ( QgsTest::isTravis() )
406+
QSKIP( "This test is disabled on Travis CI environment" );
407+
408+
QgsNetworkAccessManager::instance()->setSslErrorHandler( qgis::make_unique< TestSslErrorHandler >() );
409+
410+
QObject context;
411+
//test fetching from a blank url
412+
bool loaded = false;
413+
bool gotRequestAboutToBeCreatedSignal = false;
414+
bool gotSslError = false;
415+
int requestId = -1;
416+
bool gotRequestEncounteredSslError = false;
417+
QUrl u = QUrl( QStringLiteral( "https://self-signed.badssl.com/" ) );
418+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( const QgsNetworkRequestParameters & params )
419+
{
420+
gotRequestAboutToBeCreatedSignal = true;
421+
requestId = params.requestId();
422+
QVERIFY( requestId > 0 );
423+
QCOMPARE( params.operation(), QNetworkAccessManager::GetOperation );
424+
QCOMPARE( params.request().url(), u );
425+
} );
426+
connect( QgsNetworkAccessManager::instance(), qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ), &context, [&]( const QgsNetworkReplyContent & reply )
427+
{
428+
QCOMPARE( reply.error(), QNetworkReply::NoError ); // because handler ignores error
429+
QCOMPARE( reply.requestId(), requestId );
430+
QCOMPARE( reply.request().url(), u );
431+
loaded = true;
432+
} );
433+
connect( QgsNetworkAccessManager::instance(), &QNetworkAccessManager::sslErrors, &context, [&]( QNetworkReply *, const QList<QSslError> &errors )
434+
{
435+
QCOMPARE( errors.at( 0 ).error(), QSslError::SelfSignedCertificate );
436+
gotSslError = true;
437+
} );
438+
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::requestEncounteredSslErrors, &context, [&]( int errorRequestId, const QList<QSslError> &errors )
439+
{
440+
QCOMPARE( errors.at( 0 ).error(), QSslError::SelfSignedCertificate );
441+
QCOMPARE( errorRequestId, requestId );
442+
gotRequestEncounteredSslError = true;
443+
} );
354444
QgsNetworkAccessManager::instance()->get( QNetworkRequest( u ) );
355445

356-
while ( !loaded && !gotSslError && !gotRequestAboutToBeCreatedSignal )
446+
while ( !loaded || !gotSslError || !gotRequestAboutToBeCreatedSignal || !gotRequestEncounteredSslError )
357447
{
358448
qApp->processEvents();
359449
}
@@ -363,18 +453,21 @@ void TestQgsNetworkAccessManager::fetchBadSsl()
363453
gotRequestAboutToBeCreatedSignal = false;
364454
loaded = false;
365455
gotSslError = false;
456+
gotRequestEncounteredSslError = false;
366457
BackgroundRequest *thread = new BackgroundRequest( QNetworkRequest( u ) );
367458

368459
thread->start();
369460

370-
while ( !loaded && !gotSslError && !gotRequestAboutToBeCreatedSignal )
461+
while ( !loaded || !gotSslError || !gotRequestAboutToBeCreatedSignal || !gotRequestEncounteredSslError )
371462
{
372463
qApp->processEvents();
373464
}
374465
QVERIFY( gotRequestAboutToBeCreatedSignal );
375466
thread->exit();
376467
thread->wait();
377468
thread->deleteLater();
469+
470+
QgsNetworkAccessManager::instance()->setSslErrorHandler( qgis::make_unique< QgsSslErrorHandler >() );
378471
}
379472

380473
void TestQgsNetworkAccessManager::fetchTimeout()

0 commit comments

Comments
 (0)