|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 // Just to get Q_OS_SYMBIAN |
|
43 #include <qglobal.h> |
|
44 |
|
45 #include <QtTest/QtTest> |
|
46 |
|
47 |
|
48 #include <qcoreapplication.h> |
|
49 #include <qtcpsocket.h> |
|
50 #include <qtcpserver.h> |
|
51 #include <qhostaddress.h> |
|
52 #include <qprocess.h> |
|
53 #include <qstringlist.h> |
|
54 #include <qplatformdefs.h> |
|
55 #include <qhostinfo.h> |
|
56 |
|
57 #include <QNetworkProxy> |
|
58 Q_DECLARE_METATYPE(QNetworkProxy) |
|
59 Q_DECLARE_METATYPE(QList<QNetworkProxy>) |
|
60 |
|
61 #include "../../auto/network-settings.h" |
|
62 |
|
63 //TESTED_CLASS= |
|
64 //TESTED_FILES= |
|
65 |
|
66 class tst_QTcpServer : public QObject |
|
67 { |
|
68 Q_OBJECT |
|
69 |
|
70 public: |
|
71 tst_QTcpServer(); |
|
72 virtual ~tst_QTcpServer(); |
|
73 |
|
74 |
|
75 public slots: |
|
76 void initTestCase_data(); |
|
77 void init(); |
|
78 void cleanup(); |
|
79 private slots: |
|
80 void ipv4LoopbackPerformanceTest(); |
|
81 void ipv6LoopbackPerformanceTest(); |
|
82 void ipv4PerformanceTest(); |
|
83 }; |
|
84 |
|
85 tst_QTcpServer::tst_QTcpServer() |
|
86 { |
|
87 Q_SET_DEFAULT_IAP |
|
88 } |
|
89 |
|
90 tst_QTcpServer::~tst_QTcpServer() |
|
91 { |
|
92 } |
|
93 |
|
94 void tst_QTcpServer::initTestCase_data() |
|
95 { |
|
96 QTest::addColumn<bool>("setProxy"); |
|
97 QTest::addColumn<int>("proxyType"); |
|
98 |
|
99 QTest::newRow("WithoutProxy") << false << 0; |
|
100 QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy); |
|
101 } |
|
102 |
|
103 void tst_QTcpServer::init() |
|
104 { |
|
105 QFETCH_GLOBAL(bool, setProxy); |
|
106 if (setProxy) { |
|
107 QFETCH_GLOBAL(int, proxyType); |
|
108 if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
109 QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080)); |
|
110 } |
|
111 } |
|
112 } |
|
113 |
|
114 void tst_QTcpServer::cleanup() |
|
115 { |
|
116 QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
|
117 } |
|
118 |
|
119 //---------------------------------------------------------------------------------- |
|
120 void tst_QTcpServer::ipv4LoopbackPerformanceTest() |
|
121 { |
|
122 QFETCH_GLOBAL(bool, setProxy); |
|
123 if (setProxy) |
|
124 return; |
|
125 |
|
126 QTcpServer server; |
|
127 QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
128 |
|
129 QVERIFY(server.isListening()); |
|
130 |
|
131 QTcpSocket clientA; |
|
132 clientA.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
133 QVERIFY(clientA.waitForConnected(5000)); |
|
134 QVERIFY(clientA.state() == QAbstractSocket::ConnectedState); |
|
135 |
|
136 QVERIFY(server.waitForNewConnection()); |
|
137 QTcpSocket *clientB = server.nextPendingConnection(); |
|
138 QVERIFY(clientB); |
|
139 |
|
140 QByteArray buffer(16384, '@'); |
|
141 QTime stopWatch; |
|
142 stopWatch.start(); |
|
143 qlonglong totalWritten = 0; |
|
144 while (stopWatch.elapsed() < 5000) { |
|
145 QVERIFY(clientA.write(buffer.data(), buffer.size()) > 0); |
|
146 clientA.flush(); |
|
147 totalWritten += buffer.size(); |
|
148 while (clientB->waitForReadyRead(100)) { |
|
149 if (clientB->bytesAvailable() == 16384) |
|
150 break; |
|
151 } |
|
152 clientB->read(buffer.data(), buffer.size()); |
|
153 clientB->write(buffer.data(), buffer.size()); |
|
154 clientB->flush(); |
|
155 totalWritten += buffer.size(); |
|
156 while (clientA.waitForReadyRead(100)) { |
|
157 if (clientA.bytesAvailable() == 16384) |
|
158 break; |
|
159 } |
|
160 clientA.read(buffer.data(), buffer.size()); |
|
161 } |
|
162 |
|
163 qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s", |
|
164 server.serverAddress().toString().toLatin1().constData(), |
|
165 totalWritten / (1024.0 * 1024.0), |
|
166 stopWatch.elapsed() / 1000.0, |
|
167 (totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
168 |
|
169 delete clientB; |
|
170 } |
|
171 |
|
172 //---------------------------------------------------------------------------------- |
|
173 void tst_QTcpServer::ipv6LoopbackPerformanceTest() |
|
174 { |
|
175 QFETCH_GLOBAL(bool, setProxy); |
|
176 if (setProxy) |
|
177 return; |
|
178 |
|
179 #if defined(Q_OS_SYMBIAN) |
|
180 QSKIP("Symbian: IPv6 is not yet supported", SkipAll); |
|
181 #endif |
|
182 QTcpServer server; |
|
183 if (!server.listen(QHostAddress::LocalHostIPv6, 0)) { |
|
184 QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError); |
|
185 } else { |
|
186 QTcpSocket clientA; |
|
187 clientA.connectToHost(server.serverAddress(), server.serverPort()); |
|
188 QVERIFY(clientA.waitForConnected(5000)); |
|
189 |
|
190 QVERIFY(server.waitForNewConnection(5000)); |
|
191 QTcpSocket *clientB = server.nextPendingConnection(); |
|
192 QVERIFY(clientB); |
|
193 |
|
194 QByteArray buffer(16384, '@'); |
|
195 QTime stopWatch; |
|
196 stopWatch.start(); |
|
197 qlonglong totalWritten = 0; |
|
198 while (stopWatch.elapsed() < 5000) { |
|
199 clientA.write(buffer.data(), buffer.size()); |
|
200 clientA.flush(); |
|
201 totalWritten += buffer.size(); |
|
202 while (clientB->waitForReadyRead(100)) { |
|
203 if (clientB->bytesAvailable() == 16384) |
|
204 break; |
|
205 } |
|
206 clientB->read(buffer.data(), buffer.size()); |
|
207 clientB->write(buffer.data(), buffer.size()); |
|
208 clientB->flush(); |
|
209 totalWritten += buffer.size(); |
|
210 while (clientA.waitForReadyRead(100)) { |
|
211 if (clientA.bytesAvailable() == 16384) |
|
212 break; |
|
213 } |
|
214 clientA.read(buffer.data(), buffer.size()); |
|
215 } |
|
216 |
|
217 qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s", |
|
218 server.serverAddress().toString().toLatin1().constData(), |
|
219 totalWritten / (1024.0 * 1024.0), |
|
220 stopWatch.elapsed() / 1000.0, |
|
221 (totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
222 delete clientB; |
|
223 } |
|
224 } |
|
225 |
|
226 //---------------------------------------------------------------------------------- |
|
227 void tst_QTcpServer::ipv4PerformanceTest() |
|
228 { |
|
229 QTcpSocket probeSocket; |
|
230 probeSocket.connectToHost(QtNetworkSettings::serverName(), 143); |
|
231 QVERIFY(probeSocket.waitForConnected(5000)); |
|
232 |
|
233 QTcpServer server; |
|
234 QVERIFY(server.listen(probeSocket.localAddress(), 0)); |
|
235 |
|
236 QTcpSocket clientA; |
|
237 clientA.connectToHost(server.serverAddress(), server.serverPort()); |
|
238 QVERIFY(clientA.waitForConnected(5000)); |
|
239 |
|
240 QVERIFY(server.waitForNewConnection(5000)); |
|
241 QTcpSocket *clientB = server.nextPendingConnection(); |
|
242 QVERIFY(clientB); |
|
243 |
|
244 QByteArray buffer(16384, '@'); |
|
245 QTime stopWatch; |
|
246 stopWatch.start(); |
|
247 qlonglong totalWritten = 0; |
|
248 while (stopWatch.elapsed() < 5000) { |
|
249 qlonglong writtenA = clientA.write(buffer.data(), buffer.size()); |
|
250 clientA.flush(); |
|
251 totalWritten += buffer.size(); |
|
252 while (clientB->waitForReadyRead(100)) { |
|
253 if (clientB->bytesAvailable() == writtenA) |
|
254 break; |
|
255 } |
|
256 clientB->read(buffer.data(), buffer.size()); |
|
257 qlonglong writtenB = clientB->write(buffer.data(), buffer.size()); |
|
258 clientB->flush(); |
|
259 totalWritten += buffer.size(); |
|
260 while (clientA.waitForReadyRead(100)) { |
|
261 if (clientA.bytesAvailable() == writtenB) |
|
262 break; |
|
263 } |
|
264 clientA.read(buffer.data(), buffer.size()); |
|
265 } |
|
266 |
|
267 qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s", |
|
268 probeSocket.localAddress().toString().toLatin1().constData(), |
|
269 totalWritten / (1024.0 * 1024.0), |
|
270 stopWatch.elapsed() / 1000.0, |
|
271 (totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
272 |
|
273 delete clientB; |
|
274 } |
|
275 |
|
276 QTEST_MAIN(tst_QTcpServer) |
|
277 #include "tst_qtcpserver.moc" |