| 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 |  | 
| 30 | #include <QtNetwork> | 
| 31 | #include <QtTest/QtTest> | 
| 32 |  | 
| 33 | #include <QNetworkProxy> | 
| 34 | #include <QAuthenticator> | 
| 35 |  | 
| 36 | #include "private/qhostinfo_p.h" | 
| 37 |  | 
| 38 | #include "../../../network-settings.h" | 
| 39 |  | 
| 40 | #ifndef QT_NO_OPENSSL | 
| 41 | typedef QSharedPointer<QSslSocket> QSslSocketPtr; | 
| 42 | #endif | 
| 43 |  | 
| 44 | class tst_QSslSocket_onDemandCertificates_static : public QObject | 
| 45 | { | 
| 46 |     Q_OBJECT | 
| 47 |  | 
| 48 |     int proxyAuthCalled; | 
| 49 |  | 
| 50 | public: | 
| 51 |  | 
| 52 | #ifndef QT_NO_OPENSSL | 
| 53 |     QSslSocketPtr newSocket(); | 
| 54 | #endif | 
| 55 |  | 
| 56 | public slots: | 
| 57 |     void initTestCase_data(); | 
| 58 |     void initTestCase(); | 
| 59 |     void init(); | 
| 60 |     void cleanup(); | 
| 61 |     void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth); | 
| 62 |  | 
| 63 | #ifndef QT_NO_OPENSSL | 
| 64 | private slots: | 
| 65 |     void onDemandRootCertLoadingStaticMethods(); | 
| 66 |  | 
| 67 | private: | 
| 68 |     QSslSocket *socket; | 
| 69 | #endif // QT_NO_OPENSSL | 
| 70 | }; | 
| 71 |  | 
| 72 | enum ProxyTests { | 
| 73 |     NoProxy = 0x00, | 
| 74 |     Socks5Proxy = 0x01, | 
| 75 |     HttpProxy = 0x02, | 
| 76 |     TypeMask = 0x0f, | 
| 77 |  | 
| 78 |     NoAuth = 0x00, | 
| 79 |     AuthBasic = 0x10, | 
| 80 |     AuthNtlm = 0x20, | 
| 81 |     AuthMask = 0xf0 | 
| 82 | }; | 
| 83 |  | 
| 84 | void tst_QSslSocket_onDemandCertificates_static::initTestCase_data() | 
| 85 | { | 
| 86 |     QTest::addColumn<bool>(name: "setProxy" ); | 
| 87 |     QTest::addColumn<int>(name: "proxyType" ); | 
| 88 |  | 
| 89 |     QTest::newRow(dataTag: "WithoutProxy" ) << false << 0; | 
| 90 |     QTest::newRow(dataTag: "WithSocks5Proxy" ) << true << int(Socks5Proxy); | 
| 91 |     QTest::newRow(dataTag: "WithSocks5ProxyAuth" ) << true << int(Socks5Proxy | AuthBasic); | 
| 92 |  | 
| 93 |     QTest::newRow(dataTag: "WithHttpProxy" ) << true << int(HttpProxy); | 
| 94 |     QTest::newRow(dataTag: "WithHttpProxyBasicAuth" ) << true << int(HttpProxy | AuthBasic); | 
| 95 |     // uncomment the line below when NTLM works | 
| 96 | //    QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm); | 
| 97 | } | 
| 98 |  | 
| 99 | void tst_QSslSocket_onDemandCertificates_static::initTestCase() | 
| 100 | { | 
| 101 | #ifdef QT_TEST_SERVER | 
| 102 |     QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1080)); | 
| 103 |     QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1081)); | 
| 104 |     QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3128)); | 
| 105 |     QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3129)); | 
| 106 |     QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3130)); | 
| 107 | #else | 
| 108 |     if (!QtNetworkSettings::verifyTestNetworkSettings()) | 
| 109 |         QSKIP("No network test server available" ); | 
| 110 | #endif // QT_TEST_SERVER | 
| 111 | } | 
| 112 |  | 
| 113 | void tst_QSslSocket_onDemandCertificates_static::init() | 
| 114 | { | 
| 115 |     QFETCH_GLOBAL(bool, setProxy); | 
| 116 |     if (setProxy) { | 
| 117 |         QFETCH_GLOBAL(int, proxyType); | 
| 118 |         const auto socksAddr = QtNetworkSettings::socksProxyServerIp().toString(); | 
| 119 |         const auto squidAddr = QtNetworkSettings::httpProxyServerIp().toString(); | 
| 120 |  | 
| 121 |         QNetworkProxy proxy; | 
| 122 |  | 
| 123 |         switch (proxyType) { | 
| 124 |         case Socks5Proxy: | 
| 125 |             proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, socksAddr, 1080); | 
| 126 |             break; | 
| 127 |  | 
| 128 |         case Socks5Proxy | AuthBasic: | 
| 129 |             proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, socksAddr, 1081); | 
| 130 |             break; | 
| 131 |  | 
| 132 |         case HttpProxy | NoAuth: | 
| 133 |             proxy = QNetworkProxy(QNetworkProxy::HttpProxy, squidAddr, 3128); | 
| 134 |             break; | 
| 135 |  | 
| 136 |         case HttpProxy | AuthBasic: | 
| 137 |             proxy = QNetworkProxy(QNetworkProxy::HttpProxy, squidAddr, 3129); | 
| 138 |             break; | 
| 139 |  | 
| 140 |         case HttpProxy | AuthNtlm: | 
| 141 |             proxy = QNetworkProxy(QNetworkProxy::HttpProxy, squidAddr, 3130); | 
| 142 |             break; | 
| 143 |         } | 
| 144 |         QNetworkProxy::setApplicationProxy(proxy); | 
| 145 |     } | 
| 146 |  | 
| 147 |     qt_qhostinfo_clear_cache(); | 
| 148 | } | 
| 149 |  | 
| 150 | void tst_QSslSocket_onDemandCertificates_static::cleanup() | 
| 151 | { | 
| 152 |     QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); | 
| 153 | } | 
| 154 |  | 
| 155 | #ifndef QT_NO_OPENSSL | 
| 156 | QSslSocketPtr tst_QSslSocket_onDemandCertificates_static::newSocket() | 
| 157 | { | 
| 158 |     QSslSocket *socket = new QSslSocket; | 
| 159 |  | 
| 160 |     proxyAuthCalled = 0; | 
| 161 |     connect(asender: socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), | 
| 162 |             SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), | 
| 163 |             atype: Qt::DirectConnection); | 
| 164 |  | 
| 165 |     return QSslSocketPtr(socket); | 
| 166 | } | 
| 167 | #endif | 
| 168 |  | 
| 169 | void tst_QSslSocket_onDemandCertificates_static::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth) | 
| 170 | { | 
| 171 |     ++proxyAuthCalled; | 
| 172 |     auth->setUser("qsockstest" ); | 
| 173 |     auth->setPassword("password" ); | 
| 174 | } | 
| 175 |  | 
| 176 | #ifndef QT_NO_OPENSSL | 
| 177 |  | 
| 178 | void tst_QSslSocket_onDemandCertificates_static::onDemandRootCertLoadingStaticMethods() | 
| 179 | { | 
| 180 |     QString host("www.qt.io" ); | 
| 181 |  | 
| 182 |     // setting empty default configuration -> should not work | 
| 183 |     QSslConfiguration conf; | 
| 184 |     QSslConfiguration originalDefaultConf = QSslConfiguration::defaultConfiguration(); | 
| 185 |     QSslConfiguration::setDefaultConfiguration(conf); | 
| 186 |     QSslSocketPtr socket4 = newSocket(); | 
| 187 |     this->socket = socket4.data(); | 
| 188 |     socket4->connectToHostEncrypted(hostName: host, port: 443); | 
| 189 |     QVERIFY(!socket4->waitForEncrypted(4000)); | 
| 190 |     QSslConfiguration::setDefaultConfiguration(originalDefaultConf); // restore old behaviour for run with proxies etc. | 
| 191 | } | 
| 192 |  | 
| 193 | #endif // QT_NO_OPENSSL | 
| 194 |  | 
| 195 | QTEST_MAIN(tst_QSslSocket_onDemandCertificates_static) | 
| 196 | #include "tst_qsslsocket_onDemandCertificates_static.moc" | 
| 197 |  |