tst_qsslsocket*: don't inherit from QSharedPointer
[profile/ivi/qtbase.git] / tests / auto / network / ssl / qsslsocket_onDemandCertificates_member / tst_qsslsocket_onDemandCertificates_member.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtNetwork>
44 #include <QtTest/QtTest>
45
46 #include <QNetworkProxy>
47 #include <QAuthenticator>
48
49 #include "private/qhostinfo_p.h"
50
51 #include "../../../network-settings.h"
52
53 #ifndef QT_NO_OPENSSL
54 typedef QSharedPointer<QSslSocket> QSslSocketPtr;
55 #endif
56
57 class tst_QSslSocket_onDemandCertificates_member : public QObject
58 {
59     Q_OBJECT
60
61     int proxyAuthCalled;
62
63 public:
64     tst_QSslSocket_onDemandCertificates_member();
65     virtual ~tst_QSslSocket_onDemandCertificates_member();
66
67 #ifndef QT_NO_OPENSSL
68     QSslSocketPtr newSocket();
69 #endif
70
71 public slots:
72     void initTestCase_data();
73     void initTestCase();
74     void init();
75     void cleanup();
76     void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth);
77
78 #ifndef QT_NO_OPENSSL
79 private slots:
80     void onDemandRootCertLoadingMemberMethods();
81
82 private:
83     QSslSocket *socket;
84 #endif // QT_NO_OPENSSL
85 };
86
87 tst_QSslSocket_onDemandCertificates_member::tst_QSslSocket_onDemandCertificates_member()
88 {
89 }
90
91 tst_QSslSocket_onDemandCertificates_member::~tst_QSslSocket_onDemandCertificates_member()
92 {
93 }
94
95 enum ProxyTests {
96     NoProxy = 0x00,
97     Socks5Proxy = 0x01,
98     HttpProxy = 0x02,
99     TypeMask = 0x0f,
100
101     NoAuth = 0x00,
102     AuthBasic = 0x10,
103     AuthNtlm = 0x20,
104     AuthMask = 0xf0
105 };
106
107 void tst_QSslSocket_onDemandCertificates_member::initTestCase_data()
108 {
109     QTest::addColumn<bool>("setProxy");
110     QTest::addColumn<int>("proxyType");
111
112     QTest::newRow("WithoutProxy") << false << 0;
113     QTest::newRow("WithSocks5Proxy") << true << int(Socks5Proxy);
114     QTest::newRow("WithSocks5ProxyAuth") << true << int(Socks5Proxy | AuthBasic);
115
116     QTest::newRow("WithHttpProxy") << true << int(HttpProxy);
117     QTest::newRow("WithHttpProxyBasicAuth") << true << int(HttpProxy | AuthBasic);
118     // uncomment the line below when NTLM works
119 //    QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm);
120 }
121
122 void tst_QSslSocket_onDemandCertificates_member::initTestCase()
123 {
124     QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
125 }
126
127 void tst_QSslSocket_onDemandCertificates_member::init()
128 {
129     QFETCH_GLOBAL(bool, setProxy);
130     if (setProxy) {
131         QFETCH_GLOBAL(int, proxyType);
132         QString testServer = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString();
133         QNetworkProxy proxy;
134
135         switch (proxyType) {
136         case Socks5Proxy:
137             proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, testServer, 1080);
138             break;
139
140         case Socks5Proxy | AuthBasic:
141             proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, testServer, 1081);
142             break;
143
144         case HttpProxy | NoAuth:
145             proxy = QNetworkProxy(QNetworkProxy::HttpProxy, testServer, 3128);
146             break;
147
148         case HttpProxy | AuthBasic:
149             proxy = QNetworkProxy(QNetworkProxy::HttpProxy, testServer, 3129);
150             break;
151
152         case HttpProxy | AuthNtlm:
153             proxy = QNetworkProxy(QNetworkProxy::HttpProxy, testServer, 3130);
154             break;
155         }
156         QNetworkProxy::setApplicationProxy(proxy);
157     }
158
159     qt_qhostinfo_clear_cache();
160 }
161
162 void tst_QSslSocket_onDemandCertificates_member::cleanup()
163 {
164     QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy);
165 }
166
167 #ifndef QT_NO_OPENSSL
168 QSslSocketPtr tst_QSslSocket_onDemandCertificates_member::newSocket()
169 {
170     QSslSocket *socket = new QSslSocket;
171
172     proxyAuthCalled = 0;
173     connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
174             SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
175             Qt::DirectConnection);
176
177     return QSslSocketPtr(socket);
178 }
179 #endif
180
181 void tst_QSslSocket_onDemandCertificates_member::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
182 {
183     ++proxyAuthCalled;
184     auth->setUser("qsockstest");
185     auth->setPassword("password");
186 }
187
188 #ifndef QT_NO_OPENSSL
189
190 void tst_QSslSocket_onDemandCertificates_member::onDemandRootCertLoadingMemberMethods()
191 {
192     QString host("qt.nokia.com");
193
194     // not using any root certs -> should not work
195     QSslSocketPtr socket2 = newSocket();
196     this->socket = socket2.data();
197     socket2->setCaCertificates(QList<QSslCertificate>());
198     socket2->connectToHostEncrypted(host, 443);
199     QVERIFY(!socket2->waitForEncrypted());
200
201     // default: using on demand loading -> should work
202     QSslSocketPtr socket = newSocket();
203     this->socket = socket.data();
204     socket->connectToHostEncrypted(host, 443);
205     QEXPECT_FAIL("", "QTBUG-20983 fails", Abort);
206     QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString()));
207
208     // not using any root certs again -> should not work
209     QSslSocketPtr socket3 = newSocket();
210     this->socket = socket3.data();
211     socket3->setCaCertificates(QList<QSslCertificate>());
212     socket3->connectToHostEncrypted(host, 443);
213     QVERIFY(!socket3->waitForEncrypted());
214
215     // setting empty SSL configuration explicitly -> should not work
216     QSslSocketPtr socket4 = newSocket();
217     this->socket = socket4.data();
218     socket4->setSslConfiguration(QSslConfiguration());
219     socket4->connectToHostEncrypted(host, 443);
220     QVERIFY(!socket4->waitForEncrypted());
221 }
222
223 #endif // QT_NO_OPENSSL
224
225 QTEST_MAIN(tst_QSslSocket_onDemandCertificates_member)
226 #include "tst_qsslsocket_onDemandCertificates_member.moc"