@@ -59,6 +59,20 @@ class BackgroundRequest : public QThread
59
59
QNetworkRequest mRequest ;
60
60
};
61
61
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
+
62
76
class TestQgsNetworkAccessManager : public QObject
63
77
{
64
78
Q_OBJECT
@@ -75,6 +89,7 @@ class TestQgsNetworkAccessManager : public QObject
75
89
void fetchEncodedContent (); // test fetching url content encoded as utf-8
76
90
void fetchPost ();
77
91
void fetchBadSsl ();
92
+ void testSslErrorHandler ();
78
93
void fetchTimeout ();
79
94
80
95
};
@@ -329,6 +344,7 @@ void TestQgsNetworkAccessManager::fetchBadSsl()
329
344
bool loaded = false ;
330
345
bool gotRequestAboutToBeCreatedSignal = false ;
331
346
bool gotSslError = false ;
347
+ bool gotRequestEncounteredSslError = false ;
332
348
int requestId = -1 ;
333
349
QUrl u = QUrl ( QStringLiteral ( " https://expired.badssl.com" ) );
334
350
connect ( QgsNetworkAccessManager::instance (), qgis::overload< QgsNetworkRequestParameters >::of ( &QgsNetworkAccessManager::requestAboutToBeCreated ), &context, [&]( const QgsNetworkRequestParameters & params )
@@ -351,9 +367,83 @@ void TestQgsNetworkAccessManager::fetchBadSsl()
351
367
QCOMPARE ( errors.at ( 0 ).error (), QSslError::CertificateExpired );
352
368
gotSslError = true ;
353
369
} );
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
+ } );
354
444
QgsNetworkAccessManager::instance ()->get ( QNetworkRequest ( u ) );
355
445
356
- while ( !loaded && !gotSslError && !gotRequestAboutToBeCreatedSignal )
446
+ while ( !loaded || !gotSslError || !gotRequestAboutToBeCreatedSignal || !gotRequestEncounteredSslError )
357
447
{
358
448
qApp->processEvents ();
359
449
}
@@ -363,18 +453,21 @@ void TestQgsNetworkAccessManager::fetchBadSsl()
363
453
gotRequestAboutToBeCreatedSignal = false ;
364
454
loaded = false ;
365
455
gotSslError = false ;
456
+ gotRequestEncounteredSslError = false ;
366
457
BackgroundRequest *thread = new BackgroundRequest ( QNetworkRequest ( u ) );
367
458
368
459
thread->start ();
369
460
370
- while ( !loaded && !gotSslError && !gotRequestAboutToBeCreatedSignal )
461
+ while ( !loaded || !gotSslError || !gotRequestAboutToBeCreatedSignal || !gotRequestEncounteredSslError )
371
462
{
372
463
qApp->processEvents ();
373
464
}
374
465
QVERIFY ( gotRequestAboutToBeCreatedSignal );
375
466
thread->exit ();
376
467
thread->wait ();
377
468
thread->deleteLater ();
469
+
470
+ QgsNetworkAccessManager::instance ()->setSslErrorHandler ( qgis::make_unique< QgsSslErrorHandler >() );
378
471
}
379
472
380
473
void TestQgsNetworkAccessManager::fetchTimeout ()
0 commit comments