author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 09:15:16 +0300 | |
branch | RCL_3 |
changeset 10 | 68d3b337861b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#ifdef Q_OS_WIN |
|
46 |
#include <winsock2.h> |
|
47 |
#endif |
|
48 |
||
49 |
#include <qcoreapplication.h> |
|
50 |
||
51 |
||
52 |
#include <qdatastream.h> |
|
53 |
#include <private/qnativesocketengine_p.h> |
|
54 |
||
55 |
#include <qhostaddress.h> |
|
56 |
#include <qdatetime.h> |
|
57 |
||
58 |
#ifdef Q_OS_UNIX |
|
59 |
#include <unistd.h> |
|
60 |
#include <sys/types.h> |
|
61 |
#include <sys/socket.h> |
|
62 |
#endif |
|
63 |
||
64 |
#include <stddef.h> |
|
65 |
||
66 |
||
67 |
#include <qstringlist.h> |
|
68 |
||
69 |
#include "../network-settings.h" |
|
70 |
||
71 |
//TESTED_FILES=network/qnativesocketengine.cpp network/qnativesocketengine_p.h network/qnativesocketengine_unix.cpp |
|
72 |
||
73 |
class tst_QNativeSocketEngine : public QObject |
|
74 |
{ |
|
75 |
Q_OBJECT |
|
76 |
||
77 |
public: |
|
78 |
tst_QNativeSocketEngine(); |
|
79 |
virtual ~tst_QNativeSocketEngine(); |
|
80 |
||
81 |
||
82 |
public slots: |
|
83 |
void init(); |
|
84 |
void cleanup(); |
|
85 |
private slots: |
|
86 |
void construction(); |
|
87 |
void simpleConnectToIMAP(); |
|
88 |
void udpLoopbackTest(); |
|
89 |
void udpIPv6LoopbackTest(); |
|
90 |
void broadcastTest(); |
|
91 |
void serverTest(); |
|
92 |
void udpLoopbackPerformance(); |
|
93 |
void tcpLoopbackPerformance(); |
|
94 |
void readWriteBufferSize(); |
|
95 |
void tooManySockets(); |
|
96 |
void bind(); |
|
97 |
void networkError(); |
|
98 |
void setSocketDescriptor(); |
|
99 |
void invalidSend(); |
|
100 |
void receiveUrgentData(); |
|
101 |
}; |
|
102 |
||
103 |
tst_QNativeSocketEngine::tst_QNativeSocketEngine() |
|
104 |
{ |
|
105 |
Q_SET_DEFAULT_IAP |
|
106 |
} |
|
107 |
||
108 |
tst_QNativeSocketEngine::~tst_QNativeSocketEngine() |
|
109 |
{ |
|
110 |
} |
|
111 |
||
112 |
void tst_QNativeSocketEngine::init() |
|
113 |
{ |
|
114 |
} |
|
115 |
||
116 |
void tst_QNativeSocketEngine::cleanup() |
|
117 |
{ |
|
118 |
} |
|
119 |
||
120 |
//--------------------------------------------------------------------------- |
|
121 |
void tst_QNativeSocketEngine::construction() |
|
122 |
{ |
|
123 |
QNativeSocketEngine socketDevice; |
|
124 |
||
125 |
QVERIFY(!socketDevice.isValid()); |
|
126 |
||
127 |
// Initialize device |
|
128 |
QVERIFY(socketDevice.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); |
|
129 |
QVERIFY(socketDevice.isValid()); |
|
130 |
QVERIFY(socketDevice.protocol() == QAbstractSocket::IPv4Protocol); |
|
131 |
QVERIFY(socketDevice.socketType() == QAbstractSocket::TcpSocket); |
|
132 |
QVERIFY(socketDevice.state() == QAbstractSocket::UnconnectedState); |
|
133 |
QVERIFY(socketDevice.socketDescriptor() != -1); |
|
134 |
QVERIFY(socketDevice.localAddress() == QHostAddress()); |
|
135 |
QVERIFY(socketDevice.localPort() == 0); |
|
136 |
QVERIFY(socketDevice.peerAddress() == QHostAddress()); |
|
137 |
QVERIFY(socketDevice.peerPort() == 0); |
|
138 |
QVERIFY(socketDevice.error() == QAbstractSocket::UnknownSocketError); |
|
139 |
||
140 |
QTest::ignoreMessage(QtWarningMsg, "QNativeSocketEngine::bytesAvailable() was called in QAbstractSocket::UnconnectedState"); |
|
141 |
QVERIFY(socketDevice.bytesAvailable() == 0); |
|
142 |
||
143 |
QTest::ignoreMessage(QtWarningMsg, "QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedState"); |
|
144 |
QVERIFY(!socketDevice.hasPendingDatagrams()); |
|
145 |
} |
|
146 |
||
147 |
//--------------------------------------------------------------------------- |
|
148 |
void tst_QNativeSocketEngine::simpleConnectToIMAP() |
|
149 |
{ |
|
150 |
QNativeSocketEngine socketDevice; |
|
151 |
||
152 |
// Initialize device |
|
153 |
QVERIFY(socketDevice.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); |
|
154 |
QVERIFY(socketDevice.state() == QAbstractSocket::UnconnectedState); |
|
155 |
||
156 |
const bool isConnected = socketDevice.connectToHost(QtNetworkSettings::serverIP(), 143); |
|
157 |
if (!isConnected) { |
|
158 |
QVERIFY(socketDevice.state() == QAbstractSocket::ConnectingState); |
|
159 |
QVERIFY(socketDevice.waitForWrite()); |
|
160 |
QVERIFY(socketDevice.state() == QAbstractSocket::ConnectedState); |
|
161 |
} |
|
162 |
QVERIFY(socketDevice.state() == QAbstractSocket::ConnectedState); |
|
163 |
QVERIFY(socketDevice.peerAddress() == QtNetworkSettings::serverIP()); |
|
164 |
||
165 |
// Wait for the greeting |
|
166 |
QVERIFY(socketDevice.waitForRead()); |
|
167 |
||
168 |
// Read the greeting |
|
169 |
qint64 available = socketDevice.bytesAvailable(); |
|
170 |
QVERIFY(available > 0); |
|
171 |
QByteArray array; |
|
172 |
array.resize(available); |
|
173 |
QVERIFY(socketDevice.read(array.data(), array.size()) == available); |
|
174 |
||
175 |
// Check that the greeting is what we expect it to be |
|
176 |
QCOMPARE(array.constData(), QtNetworkSettings::expectedReplyIMAP().constData()); |
|
177 |
||
178 |
// Write a logout message |
|
179 |
QByteArray array2 = "ZZZ LOGOUT\r\n"; |
|
180 |
QVERIFY(socketDevice.write(array2.data(), |
|
181 |
array2.size()) == array2.size()); |
|
182 |
||
183 |
// Wait for the response |
|
184 |
QVERIFY(socketDevice.waitForRead()); |
|
185 |
||
186 |
available = socketDevice.bytesAvailable(); |
|
187 |
QVERIFY(available > 0); |
|
188 |
array.resize(available); |
|
189 |
QVERIFY(socketDevice.read(array.data(), array.size()) == available); |
|
190 |
||
191 |
// Check that the greeting is what we expect it to be |
|
192 |
QCOMPARE(array.constData(), |
|
193 |
"* BYE LOGOUT received\r\n" |
|
194 |
"ZZZ OK Completed\r\n"); |
|
195 |
||
196 |
// Wait for the response |
|
197 |
QVERIFY(socketDevice.waitForRead()); |
|
198 |
char c; |
|
199 |
QVERIFY(socketDevice.read(&c, sizeof(c)) == -1); |
|
200 |
QVERIFY(socketDevice.error() == QAbstractSocket::RemoteHostClosedError); |
|
201 |
QVERIFY(socketDevice.state() == QAbstractSocket::UnconnectedState); |
|
202 |
} |
|
203 |
||
204 |
//--------------------------------------------------------------------------- |
|
205 |
void tst_QNativeSocketEngine::udpLoopbackTest() |
|
206 |
{ |
|
207 |
#ifdef SYMBIAN_WINSOCK_CONNECTIVITY |
|
208 |
QSKIP("Not working on Emulator without WinPCAP", SkipAll); |
|
209 |
#endif |
|
210 |
QNativeSocketEngine udpSocket; |
|
211 |
||
212 |
// Initialize device #1 |
|
213 |
QVERIFY(udpSocket.initialize(QAbstractSocket::UdpSocket)); |
|
214 |
QVERIFY(udpSocket.isValid()); |
|
215 |
QVERIFY(udpSocket.socketDescriptor() != -1); |
|
216 |
QVERIFY(udpSocket.protocol() == QAbstractSocket::IPv4Protocol); |
|
217 |
QVERIFY(udpSocket.socketType() == QAbstractSocket::UdpSocket); |
|
218 |
QVERIFY(udpSocket.state() == QAbstractSocket::UnconnectedState); |
|
219 |
||
220 |
// Bind #1 to localhost |
|
221 |
QVERIFY(udpSocket.bind(QHostAddress("127.0.0.1"), 0)); |
|
222 |
QVERIFY(udpSocket.state() == QAbstractSocket::BoundState); |
|
223 |
quint16 port = udpSocket.localPort(); |
|
224 |
QVERIFY(port != 0); |
|
225 |
||
226 |
// Initialize device #2 |
|
227 |
QNativeSocketEngine udpSocket2; |
|
228 |
QVERIFY(udpSocket2.initialize(QAbstractSocket::UdpSocket)); |
|
229 |
||
230 |
// Connect device #2 to #1 |
|
231 |
QVERIFY(udpSocket2.connectToHost(QHostAddress("127.0.0.1"), port)); |
|
232 |
QVERIFY(udpSocket2.state() == QAbstractSocket::ConnectedState); |
|
233 |
||
234 |
// Write a message to #1 |
|
235 |
QByteArray message1 = "hei der"; |
|
236 |
QVERIFY(udpSocket2.write(message1.data(), |
|
237 |
message1.size()) == message1.size()); |
|
238 |
||
239 |
// Read the message from #2 |
|
240 |
QVERIFY(udpSocket.waitForRead()); |
|
241 |
QVERIFY(udpSocket.hasPendingDatagrams()); |
|
242 |
qint64 available = udpSocket.pendingDatagramSize(); |
|
243 |
QVERIFY(available > 0); |
|
244 |
QByteArray answer; |
|
245 |
answer.resize(available); |
|
246 |
QHostAddress senderAddress; |
|
247 |
quint16 senderPort = 0; |
|
248 |
QVERIFY(udpSocket.readDatagram(answer.data(), answer.size(), |
|
249 |
&senderAddress, |
|
250 |
&senderPort) == message1.size()); |
|
251 |
QVERIFY(senderAddress == QHostAddress("127.0.0.1")); |
|
252 |
QVERIFY(senderPort != 0); |
|
253 |
} |
|
254 |
||
255 |
//--------------------------------------------------------------------------- |
|
256 |
void tst_QNativeSocketEngine::udpIPv6LoopbackTest() |
|
257 |
{ |
|
258 |
#if defined(Q_OS_SYMBIAN) |
|
259 |
QSKIP("Symbian: IPv6 is not yet supported", SkipAll); |
|
260 |
#endif |
|
261 |
QNativeSocketEngine udpSocket; |
|
262 |
||
263 |
// Initialize device #1 |
|
264 |
bool init = udpSocket.initialize(QAbstractSocket::UdpSocket, QAbstractSocket::IPv6Protocol); |
|
265 |
||
266 |
if (!init) { |
|
267 |
QVERIFY(udpSocket.error() == QAbstractSocket::UnsupportedSocketOperationError); |
|
268 |
} else { |
|
269 |
QVERIFY(udpSocket.protocol() == QAbstractSocket::IPv6Protocol); |
|
270 |
||
271 |
// Bind #1 to localhost |
|
272 |
QVERIFY(udpSocket.bind(QHostAddress("::1"), 0)); |
|
273 |
QVERIFY(udpSocket.state() == QAbstractSocket::BoundState); |
|
274 |
quint16 port = udpSocket.localPort(); |
|
275 |
QVERIFY(port != 0); |
|
276 |
||
277 |
// Initialize device #2 |
|
278 |
QNativeSocketEngine udpSocket2; |
|
279 |
QVERIFY(udpSocket2.initialize(QAbstractSocket::UdpSocket, QAbstractSocket::IPv6Protocol)); |
|
280 |
||
281 |
// Connect device #2 to #1 |
|
282 |
QVERIFY(udpSocket2.connectToHost(QHostAddress("::1"), port)); |
|
283 |
QVERIFY(udpSocket2.state() == QAbstractSocket::ConnectedState); |
|
284 |
||
285 |
// Write a message to #1 |
|
286 |
QByteArray message1 = "hei der"; |
|
287 |
QVERIFY(udpSocket2.write(message1.data(), |
|
288 |
message1.size()) == message1.size()); |
|
289 |
||
290 |
// Read the message from #2 |
|
291 |
QVERIFY(udpSocket.waitForRead()); |
|
292 |
QVERIFY(udpSocket.hasPendingDatagrams()); |
|
293 |
qint64 available = udpSocket.pendingDatagramSize(); |
|
294 |
QVERIFY(available > 0); |
|
295 |
QByteArray answer; |
|
296 |
answer.resize(available); |
|
297 |
QHostAddress senderAddress; |
|
298 |
quint16 senderPort = 0; |
|
299 |
QVERIFY(udpSocket.readDatagram(answer.data(), answer.size(), |
|
300 |
&senderAddress, |
|
301 |
&senderPort) == message1.size()); |
|
302 |
QVERIFY(senderAddress == QHostAddress("::1")); |
|
303 |
QVERIFY(senderPort != 0); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
//--------------------------------------------------------------------------- |
|
308 |
void tst_QNativeSocketEngine::broadcastTest() |
|
309 |
{ |
|
310 |
#ifdef Q_OS_AIX |
|
311 |
QSKIP("Broadcast does not work on darko", SkipAll); |
|
312 |
#endif |
|
313 |
QNativeSocketEngine broadcastSocket; |
|
314 |
||
315 |
// Initialize a regular Udp socket |
|
316 |
QVERIFY(broadcastSocket.initialize(QAbstractSocket::UdpSocket)); |
|
317 |
||
318 |
// Bind to any port on all interfaces |
|
319 |
QVERIFY(broadcastSocket.bind(QHostAddress::Any, 0)); |
|
320 |
QVERIFY(broadcastSocket.state() == QAbstractSocket::BoundState); |
|
321 |
quint16 port = broadcastSocket.localPort(); |
|
322 |
QVERIFY(port > 0); |
|
323 |
||
324 |
// Broadcast an inappropriate troll message |
|
325 |
QByteArray trollMessage |
|
326 |
= "MOOT wtf is a MOOT? talk english not your sutpiD ENGLISH."; |
|
327 |
QVERIFY(broadcastSocket.writeDatagram(trollMessage.data(), |
|
328 |
trollMessage.size(), |
|
329 |
QHostAddress::Broadcast, |
|
330 |
port) == trollMessage.size()); |
|
331 |
||
332 |
// Wait until we receive it ourselves |
|
333 |
#if defined(Q_OS_FREEBSD) |
|
334 |
QEXPECT_FAIL("", "Broadcasting to 255.255.255.255 does not work on FreeBSD", Abort); |
|
335 |
#endif |
|
336 |
QVERIFY(broadcastSocket.waitForRead()); |
|
337 |
QVERIFY(broadcastSocket.hasPendingDatagrams()); |
|
338 |
||
339 |
qlonglong available = broadcastSocket.pendingDatagramSize(); |
|
340 |
QByteArray response; |
|
341 |
response.resize(available); |
|
342 |
QVERIFY(broadcastSocket.readDatagram(response.data(), response.size()) |
|
343 |
== response.size()); |
|
344 |
QCOMPARE(response, trollMessage); |
|
345 |
||
346 |
} |
|
347 |
||
348 |
//--------------------------------------------------------------------------- |
|
349 |
void tst_QNativeSocketEngine::serverTest() |
|
350 |
{ |
|
351 |
QNativeSocketEngine server; |
|
352 |
||
353 |
// Initialize a Tcp socket |
|
354 |
QVERIFY(server.initialize(QAbstractSocket::TcpSocket)); |
|
355 |
||
356 |
// Bind to any port on all interfaces |
|
357 |
QVERIFY(server.bind(QHostAddress("0.0.0.0"), 0)); |
|
358 |
QVERIFY(server.state() == QAbstractSocket::BoundState); |
|
359 |
quint16 port = server.localPort(); |
|
360 |
||
361 |
// Listen for incoming connections |
|
362 |
QVERIFY(server.listen()); |
|
363 |
QVERIFY(server.state() == QAbstractSocket::ListeningState); |
|
364 |
||
365 |
// Initialize a Tcp socket |
|
366 |
QNativeSocketEngine client; |
|
367 |
QVERIFY(client.initialize(QAbstractSocket::TcpSocket)); |
|
368 |
if (!client.connectToHost(QHostAddress("127.0.0.1"), port)) { |
|
369 |
QVERIFY(client.state() == QAbstractSocket::ConnectingState); |
|
370 |
QVERIFY(client.waitForWrite()); |
|
371 |
QVERIFY(client.state() == QAbstractSocket::ConnectedState); |
|
372 |
} |
|
373 |
||
374 |
// The server accepts the connection |
|
375 |
int socketDescriptor = server.accept(); |
|
376 |
QVERIFY(socketDescriptor > 0); |
|
377 |
||
378 |
// A socket device is initialized on the server side, passing the |
|
379 |
// socket descriptor from accept(). It's pre-connected. |
|
380 |
QNativeSocketEngine serverSocket; |
|
381 |
QVERIFY(serverSocket.initialize(socketDescriptor)); |
|
382 |
QVERIFY(serverSocket.state() == QAbstractSocket::ConnectedState); |
|
383 |
||
384 |
// The server socket sends a greeting to the clietn |
|
385 |
QByteArray greeting = "Greetings!"; |
|
386 |
QVERIFY(serverSocket.write(greeting.data(), |
|
387 |
greeting.size()) == greeting.size()); |
|
388 |
||
389 |
// The client waits for the greeting to arrive |
|
390 |
QVERIFY(client.waitForRead()); |
|
391 |
qint64 available = client.bytesAvailable(); |
|
392 |
QVERIFY(available > 0); |
|
393 |
||
394 |
// The client reads the greeting and checks that it's correct |
|
395 |
QByteArray response; |
|
396 |
response.resize(available); |
|
397 |
QVERIFY(client.read(response.data(), |
|
398 |
response.size()) == response.size()); |
|
399 |
QCOMPARE(response, greeting); |
|
400 |
} |
|
401 |
||
402 |
//--------------------------------------------------------------------------- |
|
403 |
void tst_QNativeSocketEngine::udpLoopbackPerformance() |
|
404 |
{ |
|
405 |
#ifdef SYMBIAN_WINSOCK_CONNECTIVITY |
|
406 |
QSKIP("Not working on Emulator without WinPCAP", SkipAll); |
|
407 |
#endif |
|
408 |
QNativeSocketEngine udpSocket; |
|
409 |
||
410 |
// Initialize device #1 |
|
411 |
QVERIFY(udpSocket.initialize(QAbstractSocket::UdpSocket)); |
|
412 |
QVERIFY(udpSocket.isValid()); |
|
413 |
QVERIFY(udpSocket.socketDescriptor() != -1); |
|
414 |
QVERIFY(udpSocket.protocol() == QAbstractSocket::IPv4Protocol); |
|
415 |
QVERIFY(udpSocket.socketType() == QAbstractSocket::UdpSocket); |
|
416 |
QVERIFY(udpSocket.state() == QAbstractSocket::UnconnectedState); |
|
417 |
||
418 |
// Bind #1 to localhost |
|
419 |
QVERIFY(udpSocket.bind(QHostAddress("127.0.0.1"), 0)); |
|
420 |
QVERIFY(udpSocket.state() == QAbstractSocket::BoundState); |
|
421 |
quint16 port = udpSocket.localPort(); |
|
422 |
QVERIFY(port != 0); |
|
423 |
||
424 |
// Initialize device #2 |
|
425 |
QNativeSocketEngine udpSocket2; |
|
426 |
QVERIFY(udpSocket2.initialize(QAbstractSocket::UdpSocket)); |
|
427 |
||
428 |
// Connect device #2 to #1 |
|
429 |
QVERIFY(udpSocket2.connectToHost(QHostAddress("127.0.0.1"), port)); |
|
430 |
QVERIFY(udpSocket2.state() == QAbstractSocket::ConnectedState); |
|
431 |
||
432 |
const int messageSize = 8192; |
|
433 |
QByteArray message1(messageSize, '@'); |
|
434 |
QByteArray answer(messageSize, '@'); |
|
435 |
||
436 |
QHostAddress localhost = QHostAddress::LocalHost; |
|
437 |
||
438 |
qlonglong readBytes = 0; |
|
439 |
QTime timer; |
|
440 |
timer.start(); |
|
441 |
while (timer.elapsed() < 5000) { |
|
442 |
udpSocket2.write(message1.data(), message1.size()); |
|
443 |
udpSocket.waitForRead(); |
|
444 |
while (udpSocket.hasPendingDatagrams()) { |
|
445 |
readBytes += (qlonglong) udpSocket.readDatagram(answer.data(), |
|
446 |
answer.size()); |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
qDebug("\t\t%.1fMB/%.1fs: %.1fMB/s", |
|
451 |
readBytes / (1024.0 * 1024.0), |
|
452 |
timer.elapsed() / 1024.0, |
|
453 |
(readBytes / (timer.elapsed() / 1000.0)) / (1024 * 1024)); |
|
454 |
} |
|
455 |
||
456 |
//--------------------------------------------------------------------------- |
|
457 |
void tst_QNativeSocketEngine::tcpLoopbackPerformance() |
|
458 |
{ |
|
459 |
QNativeSocketEngine server; |
|
460 |
||
461 |
// Initialize a Tcp socket |
|
462 |
QVERIFY(server.initialize(QAbstractSocket::TcpSocket)); |
|
463 |
||
464 |
// Bind to any port on all interfaces |
|
465 |
QVERIFY(server.bind(QHostAddress("0.0.0.0"), 0)); |
|
466 |
QVERIFY(server.state() == QAbstractSocket::BoundState); |
|
467 |
quint16 port = server.localPort(); |
|
468 |
||
469 |
// Listen for incoming connections |
|
470 |
QVERIFY(server.listen()); |
|
471 |
QVERIFY(server.state() == QAbstractSocket::ListeningState); |
|
472 |
||
473 |
// Initialize a Tcp socket |
|
474 |
QNativeSocketEngine client; |
|
475 |
QVERIFY(client.initialize(QAbstractSocket::TcpSocket)); |
|
476 |
||
477 |
// Connect to our server |
|
478 |
if (!client.connectToHost(QHostAddress("127.0.0.1"), port)) { |
|
479 |
QVERIFY(client.state() == QAbstractSocket::ConnectingState); |
|
480 |
QVERIFY(client.waitForWrite()); |
|
481 |
QVERIFY(client.state() == QAbstractSocket::ConnectedState); |
|
482 |
} |
|
483 |
||
484 |
// The server accepts the connectio |
|
485 |
int socketDescriptor = server.accept(); |
|
486 |
QVERIFY(socketDescriptor > 0); |
|
487 |
||
488 |
// A socket device is initialized on the server side, passing the |
|
489 |
// socket descriptor from accept(). It's pre-connected. |
|
490 |
QNativeSocketEngine serverSocket; |
|
491 |
QVERIFY(serverSocket.initialize(socketDescriptor)); |
|
492 |
QVERIFY(serverSocket.state() == QAbstractSocket::ConnectedState); |
|
493 |
||
494 |
const int messageSize = 1024 * 256; |
|
495 |
QByteArray message1(messageSize, '@'); |
|
496 |
QByteArray answer(messageSize, '@'); |
|
497 |
||
498 |
QTime timer; |
|
499 |
timer.start(); |
|
500 |
qlonglong readBytes = 0; |
|
501 |
while (timer.elapsed() < 5000) { |
|
502 |
qlonglong written = serverSocket.write(message1.data(), message1.size()); |
|
503 |
while (written > 0) { |
|
504 |
client.waitForRead(); |
|
505 |
if (client.bytesAvailable() > 0) { |
|
506 |
qlonglong readNow = client.read(answer.data(), answer.size()); |
|
507 |
written -= readNow; |
|
508 |
readBytes += readNow; |
|
509 |
} |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
qDebug("\t\t%.1fMB/%.1fs: %.1fMB/s", |
|
514 |
readBytes / (1024.0 * 1024.0), |
|
515 |
timer.elapsed() / 1024.0, |
|
516 |
(readBytes / (timer.elapsed() / 1000.0)) / (1024 * 1024)); |
|
517 |
} |
|
518 |
||
519 |
//--------------------------------------------------------------------------- |
|
520 |
void tst_QNativeSocketEngine::readWriteBufferSize() |
|
521 |
{ |
|
522 |
QNativeSocketEngine device; |
|
523 |
||
524 |
QVERIFY(device.initialize(QAbstractSocket::TcpSocket)); |
|
525 |
||
526 |
qint64 bufferSize = device.receiveBufferSize(); |
|
527 |
QVERIFY(bufferSize != -1); |
|
528 |
device.setReceiveBufferSize(bufferSize + 1); |
|
529 |
#if defined(Q_OS_WINCE) |
|
530 |
QEXPECT_FAIL(0, "Not supported by default on WinCE", Continue); |
|
531 |
#endif |
|
532 |
QVERIFY(device.receiveBufferSize() > bufferSize); |
|
533 |
||
534 |
bufferSize = device.sendBufferSize(); |
|
535 |
QVERIFY(bufferSize != -1); |
|
536 |
device.setSendBufferSize(bufferSize + 1); |
|
537 |
QVERIFY(device.sendBufferSize() > bufferSize); |
|
538 |
||
539 |
} |
|
540 |
||
541 |
//--------------------------------------------------------------------------- |
|
542 |
void tst_QNativeSocketEngine::tooManySockets() |
|
543 |
{ |
|
544 |
#if defined Q_OS_WIN |
|
545 |
QSKIP("Certain windows machines suffocate and spend too much time in this test.", SkipAll); |
|
546 |
#endif |
|
547 |
QList<QNativeSocketEngine *> sockets; |
|
548 |
QNativeSocketEngine *socketLayer = 0; |
|
549 |
for (;;) { |
|
550 |
socketLayer = new QNativeSocketEngine; |
|
551 |
sockets.append(socketLayer); |
|
552 |
||
553 |
if (!socketLayer->initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)) |
|
554 |
break; |
|
555 |
} |
|
556 |
||
557 |
QCOMPARE(socketLayer->error(), QAbstractSocket::SocketResourceError); |
|
558 |
||
559 |
qDeleteAll(sockets); |
|
560 |
} |
|
561 |
||
562 |
//--------------------------------------------------------------------------- |
|
563 |
void tst_QNativeSocketEngine::bind() |
|
564 |
{ |
|
565 |
#if !defined Q_OS_WIN && !defined Q_OS_SYMBIAN |
|
566 |
QNativeSocketEngine binder; |
|
567 |
QVERIFY(binder.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); |
|
568 |
QVERIFY(!binder.bind(QHostAddress::Any, 82)); |
|
569 |
QVERIFY(binder.error() == QAbstractSocket::SocketAccessError); |
|
570 |
#endif |
|
571 |
||
572 |
QNativeSocketEngine binder2; |
|
573 |
QVERIFY(binder2.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); |
|
574 |
QVERIFY(binder2.bind(QHostAddress::Any, 31180)); |
|
575 |
||
576 |
QNativeSocketEngine binder3; |
|
577 |
QVERIFY(binder3.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); |
|
578 |
QVERIFY(!binder3.bind(QHostAddress::Any, 31180)); |
|
579 |
||
580 |
#ifdef SYMBIAN_WINSOCK_CONNECTIVITY |
|
581 |
qDebug("On Symbian Emulator (WinSock) we get EADDRNOTAVAIL instead of EADDRINUSE"); |
|
582 |
QVERIFY(binder3.error() == QAbstractSocket::SocketAddressNotAvailableError); |
|
583 |
#else |
|
584 |
QVERIFY(binder3.error() == QAbstractSocket::AddressInUseError); |
|
585 |
#endif |
|
586 |
} |
|
587 |
||
588 |
//--------------------------------------------------------------------------- |
|
589 |
void tst_QNativeSocketEngine::networkError() |
|
590 |
{ |
|
591 |
QNativeSocketEngine client; |
|
592 |
||
593 |
QVERIFY(client.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); |
|
594 |
||
595 |
const bool isConnected = client.connectToHost(QtNetworkSettings::serverIP(), 143); |
|
596 |
if (!isConnected) { |
|
597 |
QVERIFY(client.state() == QAbstractSocket::ConnectingState); |
|
598 |
QVERIFY(client.waitForWrite()); |
|
599 |
QVERIFY(client.state() == QAbstractSocket::ConnectedState); |
|
600 |
} |
|
601 |
QVERIFY(client.state() == QAbstractSocket::ConnectedState); |
|
602 |
||
603 |
// An unexpected network error! |
|
604 |
#ifdef Q_OS_WIN |
|
605 |
// could use shutdown to produce different errors |
|
606 |
::closesocket(client.socketDescriptor()); |
|
607 |
#else |
|
608 |
::close(client.socketDescriptor()); |
|
609 |
#endif |
|
610 |
||
611 |
QVERIFY(client.read(0, 0) == -1); |
|
612 |
} |
|
613 |
||
614 |
//--------------------------------------------------------------------------- |
|
615 |
void tst_QNativeSocketEngine::setSocketDescriptor() |
|
616 |
{ |
|
617 |
QNativeSocketEngine socket1; |
|
618 |
QVERIFY(socket1.initialize(QAbstractSocket::TcpSocket)); |
|
619 |
||
620 |
QNativeSocketEngine socket2; |
|
621 |
QVERIFY(socket2.initialize(socket1.socketDescriptor())); |
|
622 |
} |
|
623 |
||
624 |
//--------------------------------------------------------------------------- |
|
625 |
void tst_QNativeSocketEngine::invalidSend() |
|
626 |
{ |
|
627 |
QNativeSocketEngine socket; |
|
628 |
QVERIFY(socket.initialize(QAbstractSocket::TcpSocket)); |
|
629 |
||
630 |
QTest::ignoreMessage(QtWarningMsg, "QNativeSocketEngine::writeDatagram() was" |
|
631 |
" called by a socket other than QAbstractSocket::UdpSocket"); |
|
632 |
QCOMPARE(socket.writeDatagram("hei", 3, QHostAddress::LocalHost, 143), |
|
633 |
(qlonglong) -1); |
|
634 |
} |
|
635 |
||
636 |
//--------------------------------------------------------------------------- |
|
637 |
void tst_QNativeSocketEngine::receiveUrgentData() |
|
638 |
{ |
|
639 |
QNativeSocketEngine server; |
|
640 |
||
641 |
QVERIFY(server.initialize(QAbstractSocket::TcpSocket)); |
|
642 |
||
643 |
// Bind to any port on all interfaces |
|
644 |
QVERIFY(server.bind(QHostAddress("0.0.0.0"), 0)); |
|
645 |
QVERIFY(server.state() == QAbstractSocket::BoundState); |
|
646 |
quint16 port = server.localPort(); |
|
647 |
||
648 |
QVERIFY(server.listen()); |
|
649 |
QVERIFY(server.state() == QAbstractSocket::ListeningState); |
|
650 |
||
651 |
QNativeSocketEngine client; |
|
652 |
QVERIFY(client.initialize(QAbstractSocket::TcpSocket)); |
|
653 |
||
654 |
if (!client.connectToHost(QHostAddress("127.0.0.1"), port)) { |
|
655 |
QVERIFY(client.state() == QAbstractSocket::ConnectingState); |
|
656 |
QVERIFY(client.waitForWrite()); |
|
657 |
QVERIFY(client.state() == QAbstractSocket::ConnectedState); |
|
658 |
} |
|
659 |
||
660 |
int socketDescriptor = server.accept(); |
|
661 |
QVERIFY(socketDescriptor > 0); |
|
662 |
||
663 |
QNativeSocketEngine serverSocket; |
|
664 |
QVERIFY(serverSocket.initialize(socketDescriptor)); |
|
665 |
QVERIFY(serverSocket.state() == QAbstractSocket::ConnectedState); |
|
666 |
||
667 |
char msg; |
|
668 |
int available; |
|
669 |
QByteArray response; |
|
670 |
||
671 |
#if defined Q_OS_HPUX |
|
672 |
QSKIP("Native OOB data test doesn't work on HP-UX.", SkipAll); |
|
673 |
#elif defined (Q_OS_WINCE) |
|
674 |
QSKIP("Native OOB data test doesn't work on WinCE.", SkipAll); |
|
675 |
#endif |
|
676 |
||
677 |
// The server sends an urgent message |
|
678 |
msg = 'Q'; |
|
679 |
QCOMPARE(int(::send(socketDescriptor, &msg, sizeof(msg), MSG_OOB)), 1); |
|
680 |
||
681 |
// The client receives the urgent message |
|
682 |
QVERIFY(client.waitForRead()); |
|
683 |
available = client.bytesAvailable(); |
|
684 |
QCOMPARE(available, 1); |
|
685 |
response.resize(available); |
|
686 |
QCOMPARE(client.read(response.data(), response.size()), qint64(1)); |
|
687 |
QCOMPARE(response.at(0), msg); |
|
688 |
||
689 |
// The client sends an urgent message |
|
690 |
msg = 'T'; |
|
691 |
int clientDescriptor = client.socketDescriptor(); |
|
692 |
QCOMPARE(int(::send(clientDescriptor, &msg, sizeof(msg), MSG_OOB)), 1); |
|
693 |
||
694 |
// The server receives the urgent message |
|
695 |
QVERIFY(serverSocket.waitForRead()); |
|
696 |
available = serverSocket.bytesAvailable(); |
|
697 |
QCOMPARE(available, 1); |
|
698 |
response.resize(available); |
|
699 |
QCOMPARE(serverSocket.read(response.data(), response.size()), qint64(1)); |
|
700 |
QCOMPARE(response.at(0), msg); |
|
701 |
||
702 |
} |
|
703 |
||
704 |
QTEST_MAIN(tst_QNativeSocketEngine) |
|
705 |
#include "tst_qnativesocketengine.moc" |