author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
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:
3
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 |
#include <QtTest/QtTest> |
|
43 |
#include <QtNetwork/QtNetwork> |
|
44 |
||
45 |
#ifdef Q_OS_SYMBIAN |
|
46 |
// In Symbian OS test data is located in applications private dir |
|
47 |
// Current path (C:\private\<UID>) contains only ascii chars |
|
48 |
//#define SRCDIR QDir::currentPath() |
|
49 |
#define SRCDIR "." |
|
50 |
#endif |
|
51 |
||
52 |
#include "../network-settings.h" |
|
53 |
||
54 |
class tst_NetworkSelfTest: public QObject |
|
55 |
{ |
|
56 |
Q_OBJECT |
|
57 |
QHostAddress cachedIpAddress; |
|
58 |
public: |
|
59 |
tst_NetworkSelfTest(); |
|
60 |
virtual ~tst_NetworkSelfTest(); |
|
61 |
||
62 |
QHostAddress serverIpAddress(); |
|
63 |
||
64 |
private slots: |
|
65 |
void hostTest(); |
|
66 |
void dnsResolution_data(); |
|
67 |
void dnsResolution(); |
|
68 |
void serverReachability(); |
|
69 |
void remotePortsOpen_data(); |
|
70 |
void remotePortsOpen(); |
|
71 |
void fileLineEndingTest(); |
|
72 |
||
73 |
// specific protocol tests |
|
74 |
void ftpServer(); |
|
75 |
void imapServer(); |
|
76 |
void httpServer(); |
|
77 |
void httpsServer(); |
|
78 |
void httpProxy(); |
|
79 |
void httpProxyBasicAuth(); |
|
80 |
void httpProxyNtlmAuth(); |
|
81 |
void socks5Proxy(); |
|
82 |
void socks5ProxyAuth(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
// ssl supported test |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
void supportsSsl(); |
0 | 86 |
}; |
87 |
||
88 |
class Chat |
|
89 |
{ |
|
90 |
public: |
|
91 |
enum Type { |
|
92 |
Reconnect, |
|
93 |
Send, |
|
94 |
Expect, |
|
95 |
SkipBytes, |
|
96 |
DiscardUntil, |
|
97 |
DiscardUntilDisconnect, |
|
98 |
Disconnect, |
|
99 |
RemoteDisconnect, |
|
100 |
StartEncryption |
|
101 |
}; |
|
102 |
Chat(Type t, const QByteArray &d) |
|
103 |
: data(d), type(t) |
|
104 |
{ |
|
105 |
} |
|
106 |
Chat(Type t, int val = 0) |
|
107 |
: value(val), type(t) |
|
108 |
{ |
|
109 |
} |
|
110 |
||
111 |
static inline Chat send(const QByteArray &data) |
|
112 |
{ return Chat(Send, data); } |
|
113 |
static inline Chat expect(const QByteArray &data) |
|
114 |
{ return Chat(Expect, data); } |
|
115 |
static inline Chat discardUntil(const QByteArray &data) |
|
116 |
{ return Chat(DiscardUntil, data); } |
|
117 |
static inline Chat skipBytes(int count) |
|
118 |
{ return Chat(SkipBytes, count); } |
|
119 |
||
120 |
QByteArray data; |
|
121 |
int value; |
|
122 |
Type type; |
|
123 |
}; |
|
124 |
||
125 |
static QString prettyByteArray(const QByteArray &array) |
|
126 |
{ |
|
127 |
// any control chars? |
|
128 |
QString result; |
|
129 |
result.reserve(array.length() + array.length() / 3); |
|
130 |
for (int i = 0; i < array.length(); ++i) { |
|
131 |
char c = array.at(i); |
|
132 |
switch (c) { |
|
133 |
case '\n': |
|
134 |
result += "\\n"; |
|
135 |
continue; |
|
136 |
case '\r': |
|
137 |
result += "\\r"; |
|
138 |
continue; |
|
139 |
case '\t': |
|
140 |
result += "\\t"; |
|
141 |
continue; |
|
142 |
case '"': |
|
143 |
result += "\\\""; |
|
144 |
continue; |
|
145 |
default: |
|
146 |
break; |
|
147 |
} |
|
148 |
||
149 |
if (c < 0x20 || uchar(c) >= 0x7f) { |
|
150 |
result += '\\'; |
|
151 |
result += QString::number(uchar(c), 8); |
|
152 |
} else { |
|
153 |
result += c; |
|
154 |
} |
|
155 |
} |
|
156 |
return result; |
|
157 |
} |
|
158 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
static bool doSocketRead(QTcpSocket *socket, int minBytesAvailable, int timeout = 4000) |
0 | 160 |
{ |
161 |
QTime timer; |
|
162 |
timer.start(); |
|
163 |
forever { |
|
164 |
if (socket->bytesAvailable() >= minBytesAvailable) |
|
165 |
return true; |
|
166 |
if (socket->state() == QAbstractSocket::UnconnectedState |
|
167 |
|| timer.elapsed() >= timeout) |
|
168 |
return false; |
|
169 |
if (!socket->waitForReadyRead(timeout - timer.elapsed())) |
|
170 |
return false; |
|
171 |
} |
|
172 |
} |
|
173 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
static bool doSocketFlush(QTcpSocket *socket, int timeout = 4000) |
0 | 175 |
{ |
176 |
#ifndef QT_NO_OPENSSL |
|
177 |
QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket); |
|
178 |
#endif |
|
179 |
QTime timer; |
|
180 |
timer.start(); |
|
181 |
forever { |
|
182 |
if (socket->bytesToWrite() == 0 |
|
183 |
#ifndef QT_NO_OPENSSL |
|
184 |
&& sslSocket->encryptedBytesToWrite() == 0 |
|
185 |
#endif |
|
186 |
) |
|
187 |
return true; |
|
188 |
if (socket->state() == QAbstractSocket::UnconnectedState |
|
189 |
|| timer.elapsed() >= timeout) |
|
190 |
return false; |
|
191 |
if (!socket->waitForBytesWritten(timeout - timer.elapsed())) |
|
192 |
return false; |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
static void netChat(int port, const QList<Chat> &chat) |
|
197 |
{ |
|
198 |
#ifndef QT_NO_OPENSSL |
|
199 |
QSslSocket socket; |
|
200 |
#else |
|
201 |
QTcpSocket socket; |
|
202 |
#endif |
|
203 |
||
204 |
socket.connectToHost(QtNetworkSettings::serverName(), port); |
|
205 |
qDebug() << 0 << "Connecting to server on port" << port; |
|
206 |
QVERIFY2(socket.waitForConnected(10000), |
|
207 |
QString("Failed to connect to server in step 0: %1").arg(socket.errorString()).toLocal8Bit()); |
|
208 |
||
209 |
// now start the chat |
|
210 |
QList<Chat>::ConstIterator it = chat.constBegin(); |
|
211 |
for (int i = 1; it != chat.constEnd(); ++it, ++i) { |
|
212 |
switch (it->type) { |
|
213 |
case Chat::Expect: { |
|
214 |
qDebug() << i << "Expecting" << prettyByteArray(it->data); |
|
215 |
if (!doSocketRead(&socket, it->data.length())) |
|
216 |
QFAIL(QString("Failed to receive data in step %1: timeout").arg(i).toLocal8Bit()); |
|
217 |
||
218 |
// pop that many bytes off the socket |
|
219 |
QByteArray received = socket.read(it->data.length()); |
|
220 |
||
221 |
// is it what we expected? |
|
222 |
QVERIFY2(received == it->data, |
|
223 |
QString("Did not receive expected data in step %1: data received was:\n%2") |
|
224 |
.arg(i).arg(prettyByteArray(received)).toLocal8Bit()); |
|
225 |
||
226 |
break; |
|
227 |
} |
|
228 |
||
229 |
case Chat::DiscardUntil: |
|
230 |
qDebug() << i << "Discarding until" << prettyByteArray(it->data); |
|
231 |
while (true) { |
|
232 |
// scan the buffer until we have our string |
|
233 |
if (!doSocketRead(&socket, it->data.length())) |
|
234 |
QFAIL(QString("Failed to receive data in step %1: timeout").arg(i).toLocal8Bit()); |
|
235 |
||
236 |
QByteArray buffer; |
|
237 |
buffer.resize(socket.bytesAvailable()); |
|
238 |
socket.peek(buffer.data(), socket.bytesAvailable()); |
|
239 |
||
240 |
int pos = buffer.indexOf(it->data); |
|
241 |
if (pos == -1) { |
|
242 |
// data not found, keep trying |
|
243 |
continue; |
|
244 |
} |
|
245 |
||
246 |
buffer = socket.read(pos + it->data.length()); |
|
247 |
qDebug() << i << "Discarded" << prettyByteArray(buffer); |
|
248 |
break; |
|
249 |
} |
|
250 |
break; |
|
251 |
||
252 |
case Chat::SkipBytes: { |
|
253 |
qDebug() << i << "Skipping" << it->value << "bytes"; |
|
254 |
if (!doSocketRead(&socket, it->value)) |
|
255 |
QFAIL(QString("Failed to receive data in step %1: timeout").arg(i).toLocal8Bit()); |
|
256 |
||
257 |
// now discard the bytes |
|
258 |
QByteArray buffer = socket.read(it->value); |
|
259 |
qDebug() << i << "Skipped" << prettyByteArray(buffer); |
|
260 |
break; |
|
261 |
} |
|
262 |
||
263 |
case Chat::Send: { |
|
264 |
qDebug() << i << "Sending" << prettyByteArray(it->data); |
|
265 |
socket.write(it->data); |
|
266 |
if (!doSocketFlush(&socket)) { |
|
267 |
QVERIFY2(socket.state() == QAbstractSocket::ConnectedState, |
|
268 |
QString("Socket disconnected while sending data in step %1").arg(i).toLocal8Bit()); |
|
269 |
QFAIL(QString("Failed to send data in step %1: timeout").arg(i).toLocal8Bit()); |
|
270 |
} |
|
271 |
break; |
|
272 |
} |
|
273 |
||
274 |
case Chat::Disconnect: |
|
275 |
qDebug() << i << "Disconnecting from host"; |
|
276 |
socket.disconnectFromHost(); |
|
277 |
||
278 |
// is this the last command? |
|
279 |
if (it + 1 != chat.constEnd()) |
|
280 |
break; |
|
281 |
||
282 |
// fall through: |
|
283 |
case Chat::RemoteDisconnect: |
|
284 |
case Chat::DiscardUntilDisconnect: |
|
285 |
qDebug() << i << "Waiting for remote disconnect"; |
|
286 |
if (socket.state() != QAbstractSocket::UnconnectedState) |
|
287 |
socket.waitForDisconnected(10000); |
|
288 |
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, |
|
289 |
QString("Socket did not disconnect as expected in step %1").arg(i).toLocal8Bit()); |
|
290 |
||
291 |
// any data left? |
|
292 |
if (it->type == Chat::DiscardUntilDisconnect) { |
|
293 |
QByteArray buffer = socket.readAll(); |
|
294 |
qDebug() << i << "Discarded in the process:" << prettyByteArray(buffer); |
|
295 |
} |
|
296 |
||
297 |
if (socket.bytesAvailable() != 0) |
|
298 |
QFAIL(QString("Unexpected bytes still on buffer when disconnecting in step %1:\n%2") |
|
299 |
.arg(i).arg(prettyByteArray(socket.readAll())).toLocal8Bit()); |
|
300 |
break; |
|
301 |
||
302 |
case Chat::Reconnect: |
|
303 |
qDebug() << i << "Reconnecting to server on port" << port; |
|
304 |
socket.connectToHost(QtNetworkSettings::serverName(), port); |
|
305 |
QVERIFY2(socket.waitForConnected(10000), |
|
306 |
QString("Failed to reconnect to server in step %1: %2").arg(i).arg(socket.errorString()).toLocal8Bit()); |
|
307 |
break; |
|
308 |
||
309 |
case Chat::StartEncryption: |
|
310 |
#ifdef QT_NO_OPENSSL |
|
311 |
QFAIL("Internal error: SSL required for this test"); |
|
312 |
#else |
|
313 |
qDebug() << i << "Starting client encryption"; |
|
314 |
socket.ignoreSslErrors(); |
|
315 |
socket.startClientEncryption(); |
|
316 |
QVERIFY2(socket.waitForEncrypted(5000), |
|
317 |
QString("Failed to start client encryption in step %1: %2").arg(i) |
|
318 |
.arg(socket.errorString()).toLocal8Bit()); |
|
319 |
break; |
|
320 |
#endif |
|
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
tst_NetworkSelfTest::tst_NetworkSelfTest() |
|
326 |
{ |
|
327 |
Q_SET_DEFAULT_IAP |
|
328 |
} |
|
329 |
||
330 |
tst_NetworkSelfTest::~tst_NetworkSelfTest() |
|
331 |
{ |
|
332 |
} |
|
333 |
||
334 |
QHostAddress tst_NetworkSelfTest::serverIpAddress() |
|
335 |
{ |
|
336 |
if (cachedIpAddress.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) { |
|
337 |
// need resolving |
|
338 |
QHostInfo resolved = QHostInfo::fromName(QtNetworkSettings::serverName()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
339 |
if(resolved.error() != QHostInfo::NoError || |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
!resolved.addresses().isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
341 |
qWarning("QHostInfo::fromName failed (%d).", resolved.error()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
342 |
return QHostAddress(QHostAddress::Null); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
343 |
} |
0 | 344 |
cachedIpAddress = resolved.addresses().first(); |
345 |
} |
|
346 |
return cachedIpAddress; |
|
347 |
} |
|
348 |
||
349 |
void tst_NetworkSelfTest::hostTest() |
|
350 |
{ |
|
351 |
// this is a localhost self-test |
|
352 |
QHostInfo localhost = QHostInfo::fromName("localhost"); |
|
353 |
QCOMPARE(localhost.error(), QHostInfo::NoError); |
|
354 |
QVERIFY(!localhost.addresses().isEmpty()); |
|
355 |
||
356 |
QTcpServer server; |
|
357 |
QVERIFY(server.listen()); |
|
358 |
||
359 |
QTcpSocket socket; |
|
360 |
socket.connectToHost("127.0.0.1", server.serverPort()); |
|
361 |
QVERIFY(socket.waitForConnected(10000)); |
|
362 |
} |
|
363 |
||
364 |
void tst_NetworkSelfTest::dnsResolution_data() |
|
365 |
{ |
|
366 |
QTest::addColumn<QString>("hostName"); |
|
367 |
QTest::newRow("local-name") << QtNetworkSettings::serverLocalName(); |
|
368 |
QTest::newRow("fqdn") << QtNetworkSettings::serverName(); |
|
369 |
} |
|
370 |
||
371 |
void tst_NetworkSelfTest::dnsResolution() |
|
372 |
{ |
|
373 |
QFETCH(QString, hostName); |
|
374 |
QHostInfo resolved = QHostInfo::fromName(hostName); |
|
375 |
QVERIFY2(resolved.error() == QHostInfo::NoError, |
|
376 |
QString("Failed to resolve hostname %1: %2").arg(hostName, resolved.errorString()).toLocal8Bit()); |
|
377 |
QVERIFY2(resolved.addresses().size() > 0, "Got 0 addresses for server IP"); |
|
378 |
||
379 |
cachedIpAddress = resolved.addresses().first(); |
|
380 |
} |
|
381 |
||
382 |
void tst_NetworkSelfTest::serverReachability() |
|
383 |
{ |
|
384 |
// check that we get a proper error connecting to port 12346 |
|
385 |
QTcpSocket socket; |
|
386 |
socket.connectToHost(QtNetworkSettings::serverName(), 12346); |
|
387 |
||
388 |
QTime timer; |
|
389 |
timer.start(); |
|
390 |
socket.waitForConnected(10000); |
|
391 |
QVERIFY2(timer.elapsed() < 9900, "Connection to closed port timed out instead of refusing, something is wrong"); |
|
392 |
||
393 |
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!"); |
|
394 |
QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError, |
|
395 |
QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit()); |
|
396 |
} |
|
397 |
||
398 |
void tst_NetworkSelfTest::remotePortsOpen_data() |
|
399 |
{ |
|
400 |
QTest::addColumn<int>("portNumber"); |
|
401 |
QTest::newRow("ftp") << 21; |
|
402 |
QTest::newRow("ssh") << 22; |
|
403 |
QTest::newRow("imap") << 143; |
|
404 |
QTest::newRow("http") << 80; |
|
405 |
QTest::newRow("https") << 443; |
|
406 |
QTest::newRow("http-proxy") << 3128; |
|
407 |
QTest::newRow("http-proxy-auth-basic") << 3129; |
|
408 |
QTest::newRow("http-proxy-auth-ntlm") << 3130; |
|
409 |
QTest::newRow("socks5-proxy") << 1080; |
|
410 |
QTest::newRow("socks5-proxy-auth") << 1081; |
|
411 |
} |
|
412 |
||
413 |
void tst_NetworkSelfTest::remotePortsOpen() |
|
414 |
{ |
|
415 |
#ifdef Q_OS_SYMBIAN |
|
416 |
if (qstrcmp(QTest::currentDataTag(), "http-proxy-auth-ntlm") == 0) |
|
417 |
QSKIP("NTML authentication not yet supported in Symbian", SkipSingle); |
|
418 |
#endif |
|
419 |
||
420 |
QFETCH(int, portNumber); |
|
421 |
QTcpSocket socket; |
|
422 |
socket.connectToHost(QtNetworkSettings::serverName(), portNumber); |
|
423 |
||
424 |
if (!socket.waitForConnected(10000)) { |
|
425 |
if (socket.error() == QAbstractSocket::SocketTimeoutError) |
|
426 |
QFAIL(QString("Network timeout connecting to the server on port %1").arg(portNumber).toLocal8Bit()); |
|
427 |
else |
|
428 |
QFAIL(QString("Error connecting to server on port %1: %2").arg(portNumber).arg(socket.errorString()).toLocal8Bit()); |
|
429 |
} |
|
430 |
QVERIFY(socket.state() == QAbstractSocket::ConnectedState); |
|
431 |
} |
|
432 |
||
433 |
||
434 |
void tst_NetworkSelfTest::fileLineEndingTest() |
|
435 |
{ |
|
436 |
QString referenceName = SRCDIR "/rfc3252.txt"; |
|
437 |
long long expectedReferenceSize = 25962; |
|
438 |
||
439 |
QString lineEndingType("LF"); |
|
440 |
||
441 |
QFile reference(referenceName); |
|
442 |
QVERIFY(reference.open(QIODevice::ReadOnly)); |
|
443 |
QByteArray byteLine = reference.readLine(); |
|
444 |
if(byteLine.endsWith("\r\n")) |
|
445 |
lineEndingType = "CRLF"; |
|
446 |
else if(byteLine.endsWith("\r")) |
|
447 |
lineEndingType = "CR"; |
|
448 |
||
449 |
QString referenceAsTextData; |
|
450 |
QFile referenceAsText(referenceName); |
|
451 |
QVERIFY(referenceAsText.open(QIODevice::ReadOnly)); |
|
452 |
referenceAsTextData = referenceAsText.readAll(); |
|
453 |
||
454 |
QVERIFY2(expectedReferenceSize == referenceAsTextData.length(), QString("Reference file %1 has %2 as line ending and file size not matching - Git checkout issue !?!").arg(referenceName, lineEndingType).toLocal8Bit()); |
|
455 |
QVERIFY2(!lineEndingType.compare("LF"), QString("Reference file %1 has %2 as line ending - Git checkout issue !?!").arg(referenceName, lineEndingType).toLocal8Bit()); |
|
456 |
} |
|
457 |
||
458 |
static QList<Chat> ftpChat() |
|
459 |
{ |
|
460 |
return QList<Chat>() << Chat::expect("220") |
|
461 |
<< Chat::discardUntil("\r\n") |
|
462 |
<< Chat::send("USER anonymous\r\n") |
|
463 |
<< Chat::expect("331") |
|
464 |
<< Chat::discardUntil("\r\n") |
|
465 |
<< Chat::send("PASS user@hostname\r\n") |
|
466 |
<< Chat::expect("230") |
|
467 |
<< Chat::discardUntil("\r\n") |
|
468 |
<< Chat::send("QUIT\r\n") |
|
469 |
<< Chat::expect("221") |
|
470 |
<< Chat::discardUntil("\r\n") |
|
471 |
<< Chat::RemoteDisconnect; |
|
472 |
} |
|
473 |
||
474 |
void tst_NetworkSelfTest::ftpServer() |
|
475 |
{ |
|
476 |
netChat(21, ftpChat()); |
|
477 |
} |
|
478 |
||
479 |
void tst_NetworkSelfTest::imapServer() |
|
480 |
{ |
|
481 |
netChat(143, QList<Chat>() |
|
482 |
<< Chat::expect("* OK ") |
|
483 |
<< Chat::discardUntil("\r\n") |
|
484 |
<< Chat::send("1 CAPABILITY\r\n") |
|
485 |
<< Chat::expect("* CAPABILITY ") |
|
486 |
<< Chat::discardUntil("1 OK") |
|
487 |
<< Chat::discardUntil("\r\n") |
|
488 |
<< Chat::send("2 LOGOUT\r\n") |
|
489 |
<< Chat::discardUntil("2 OK") |
|
490 |
<< Chat::discardUntil("\r\n") |
|
491 |
<< Chat::RemoteDisconnect); |
|
492 |
} |
|
493 |
||
494 |
void tst_NetworkSelfTest::httpServer() |
|
495 |
{ |
|
496 |
netChat(80, QList<Chat>() |
|
497 |
// HTTP/0.9 chat: |
|
498 |
<< Chat::send("GET /\r\n") |
|
499 |
<< Chat::DiscardUntilDisconnect |
|
500 |
||
501 |
// HTTP/1.0 chat: |
|
502 |
<< Chat::Reconnect |
|
503 |
<< Chat::send("GET / HTTP/1.0\r\n" |
|
504 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
505 |
"Connection: close\r\n" |
|
506 |
"\r\n") |
|
507 |
<< Chat::expect("HTTP/1.") |
|
508 |
<< Chat::discardUntil(" ") |
|
509 |
<< Chat::expect("200 ") |
|
510 |
<< Chat::DiscardUntilDisconnect |
|
511 |
||
512 |
// HTTP/1.0 POST: |
|
513 |
<< Chat::Reconnect |
|
514 |
<< Chat::send("POST / HTTP/1.0\r\n" |
|
515 |
"Content-Length: 5\r\n" |
|
516 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
517 |
"Connection: close\r\n" |
|
518 |
"\r\n" |
|
519 |
"Hello") |
|
520 |
<< Chat::expect("HTTP/1.") |
|
521 |
<< Chat::discardUntil(" ") |
|
522 |
<< Chat::expect("200 ") |
|
523 |
<< Chat::DiscardUntilDisconnect |
|
524 |
); |
|
525 |
} |
|
526 |
||
527 |
void tst_NetworkSelfTest::httpsServer() |
|
528 |
{ |
|
529 |
#ifndef QT_NO_OPENSSL |
|
530 |
netChat(443, QList<Chat>() |
|
531 |
<< Chat::StartEncryption |
|
532 |
<< Chat::send("GET / HTTP/1.0\r\n" |
|
533 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
534 |
"Connection: close\r\n" |
|
535 |
"\r\n") |
|
536 |
<< Chat::expect("HTTP/1.") |
|
537 |
<< Chat::discardUntil(" ") |
|
538 |
<< Chat::expect("200 ") |
|
539 |
<< Chat::DiscardUntilDisconnect); |
|
540 |
#else |
|
541 |
QSKIP("SSL not enabled, cannot test", SkipAll); |
|
542 |
#endif |
|
543 |
} |
|
544 |
||
545 |
void tst_NetworkSelfTest::httpProxy() |
|
546 |
{ |
|
547 |
netChat(3128, QList<Chat>() |
|
548 |
// proxy GET by IP |
|
549 |
<< Chat::send("GET http://" + serverIpAddress().toString().toLatin1() + "/ HTTP/1.0\r\n" |
|
550 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
551 |
"Proxy-connection: close\r\n" |
|
552 |
"\r\n") |
|
553 |
<< Chat::expect("HTTP/1.") |
|
554 |
<< Chat::discardUntil(" ") |
|
555 |
<< Chat::expect("200 ") |
|
556 |
<< Chat::DiscardUntilDisconnect |
|
557 |
||
558 |
// proxy GET by hostname |
|
559 |
<< Chat::Reconnect |
|
560 |
<< Chat::send("GET http://" + QtNetworkSettings::serverName().toLatin1() + "/ HTTP/1.0\r\n" |
|
561 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
562 |
"Proxy-connection: close\r\n" |
|
563 |
"\r\n") |
|
564 |
<< Chat::expect("HTTP/1.") |
|
565 |
<< Chat::discardUntil(" ") |
|
566 |
<< Chat::expect("200 ") |
|
567 |
<< Chat::DiscardUntilDisconnect |
|
568 |
||
569 |
// proxy CONNECT by IP |
|
570 |
<< Chat::Reconnect |
|
571 |
<< Chat::send("CONNECT " + serverIpAddress().toString().toLatin1() + ":21 HTTP/1.0\r\n" |
|
572 |
"\r\n") |
|
573 |
<< Chat::expect("HTTP/1.") |
|
574 |
<< Chat::discardUntil(" ") |
|
575 |
<< Chat::expect("200 ") |
|
576 |
<< Chat::discardUntil("\r\n\r\n") |
|
577 |
<< ftpChat() |
|
578 |
||
579 |
// proxy CONNECT by hostname |
|
580 |
<< Chat::Reconnect |
|
581 |
<< Chat::send("CONNECT " + QtNetworkSettings::serverName().toLatin1() + ":21 HTTP/1.0\r\n" |
|
582 |
"\r\n") |
|
583 |
<< Chat::expect("HTTP/1.") |
|
584 |
<< Chat::discardUntil(" ") |
|
585 |
<< Chat::expect("200 ") |
|
586 |
<< Chat::discardUntil("\r\n\r\n") |
|
587 |
<< ftpChat() |
|
588 |
); |
|
589 |
} |
|
590 |
||
591 |
void tst_NetworkSelfTest::httpProxyBasicAuth() |
|
592 |
{ |
|
593 |
netChat(3129, QList<Chat>() |
|
594 |
// test auth required response |
|
595 |
<< Chat::send("GET http://" + QtNetworkSettings::serverName().toLatin1() + "/ HTTP/1.0\r\n" |
|
596 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
597 |
"Proxy-connection: close\r\n" |
|
598 |
"\r\n") |
|
599 |
<< Chat::expect("HTTP/1.") |
|
600 |
<< Chat::discardUntil(" ") |
|
601 |
<< Chat::expect("407 ") |
|
602 |
<< Chat::discardUntil("\r\nProxy-Authenticate: Basic realm=\"") |
|
603 |
<< Chat::DiscardUntilDisconnect |
|
604 |
||
605 |
// now try sending our credentials |
|
606 |
<< Chat::Reconnect |
|
607 |
<< Chat::send("GET http://" + QtNetworkSettings::serverName().toLatin1() + "/ HTTP/1.0\r\n" |
|
608 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
609 |
"Proxy-connection: close\r\n" |
|
610 |
"Proxy-Authorization: Basic cXNvY2tzdGVzdDpwYXNzd29yZA==\r\n" |
|
611 |
"\r\n") |
|
612 |
<< Chat::expect("HTTP/1.") |
|
613 |
<< Chat::discardUntil(" ") |
|
614 |
<< Chat::expect("200 ") |
|
615 |
<< Chat::DiscardUntilDisconnect); |
|
616 |
} |
|
617 |
||
618 |
void tst_NetworkSelfTest::httpProxyNtlmAuth() |
|
619 |
{ |
|
620 |
#ifdef Q_OS_SYMBIAN |
|
621 |
QSKIP("NTML authentication not yet supported in Symbian", SkipAll); |
|
622 |
#else |
|
623 |
netChat(3130, QList<Chat>() |
|
624 |
// test auth required response |
|
625 |
<< Chat::send("GET http://" + QtNetworkSettings::serverName().toLatin1() + "/ HTTP/1.0\r\n" |
|
626 |
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n" |
|
627 |
"Proxy-connection: keep-alive\r\n" // NTLM auth will disconnect |
|
628 |
"\r\n") |
|
629 |
<< Chat::expect("HTTP/1.") |
|
630 |
<< Chat::discardUntil(" ") |
|
631 |
<< Chat::expect("407 ") |
|
632 |
<< Chat::discardUntil("\r\nProxy-Authenticate: NTLM\r\n") |
|
633 |
<< Chat::DiscardUntilDisconnect |
|
634 |
); |
|
635 |
#endif |
|
636 |
} |
|
637 |
||
638 |
// SOCKSv5 is a binary protocol |
|
639 |
static const char handshakeNoAuth[] = "\5\1\0"; |
|
640 |
static const char handshakeOkNoAuth[] = "\5\0"; |
|
641 |
static const char handshakeAuthPassword[] = "\5\1\2\1\12qsockstest\10password"; |
|
642 |
static const char handshakeOkPasswdAuth[] = "\5\2\1\0"; |
|
643 |
static const char handshakeAuthNotOk[] = "\5\377"; |
|
644 |
static const char connect1[] = "\5\1\0\1\177\0\0\1\0\25"; // Connect IPv4 127.0.0.1 port 21 |
|
645 |
static const char connect1a[] = "\5\1\0\1"; // just "Connect to IPv4" |
|
646 |
static const char connect1b[] = "\0\25"; // just "port 21" |
|
647 |
static const char connect2[] = "\5\1\0\3\11localhost\0\25"; // Connect hostname localhost 21 |
|
648 |
static const char connect2a[] = "\5\1\0\3"; // just "Connect to hostname" |
|
649 |
static const char connected[] = "\5\0\0"; |
|
650 |
||
651 |
#define QBA(x) (QByteArray::fromRawData(x, -1 + sizeof(x))) |
|
652 |
||
653 |
void tst_NetworkSelfTest::socks5Proxy() |
|
654 |
{ |
|
655 |
union { |
|
656 |
char buf[4]; |
|
657 |
quint32 data; |
|
658 |
} ip4Address; |
|
659 |
ip4Address.data = qToBigEndian(serverIpAddress().toIPv4Address()); |
|
660 |
||
661 |
netChat(1080, QList<Chat>() |
|
662 |
// IP address connection |
|
663 |
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) |
|
664 |
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) |
|
665 |
<< Chat::send(QByteArray(connect1, -1 + sizeof connect1)) |
|
666 |
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) |
|
667 |
<< Chat::expect("\1") // IPv4 address following |
|
668 |
<< Chat::skipBytes(6) // the server's local address and port |
|
669 |
<< ftpChat() |
|
670 |
||
671 |
// connect by IP |
|
672 |
<< Chat::Reconnect |
|
673 |
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) |
|
674 |
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) |
|
675 |
<< Chat::send(QBA(connect1a) + QByteArray::fromRawData(ip4Address.buf, 4) + QBA(connect1b)) |
|
676 |
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) |
|
677 |
<< Chat::expect("\1") // IPv4 address following |
|
678 |
<< Chat::skipBytes(6) // the server's local address and port |
|
679 |
<< ftpChat() |
|
680 |
||
681 |
// connect to "localhost" by hostname |
|
682 |
<< Chat::Reconnect |
|
683 |
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) |
|
684 |
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) |
|
685 |
<< Chat::send(QByteArray(connect2, -1 + sizeof connect2)) |
|
686 |
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) |
|
687 |
<< Chat::expect("\1") // IPv4 address following |
|
688 |
<< Chat::skipBytes(6) // the server's local address and port |
|
689 |
<< ftpChat() |
|
690 |
||
691 |
// connect to server by its official name |
|
692 |
<< Chat::Reconnect |
|
693 |
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) |
|
694 |
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) |
|
695 |
<< Chat::send(QBA(connect2a) + char(QtNetworkSettings::serverName().size()) + QtNetworkSettings::serverName().toLatin1() + QBA(connect1b)) |
|
696 |
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) |
|
697 |
<< Chat::expect("\1") // IPv4 address following |
|
698 |
<< Chat::skipBytes(6) // the server's local address and port |
|
699 |
<< ftpChat() |
|
700 |
); |
|
701 |
} |
|
702 |
||
703 |
void tst_NetworkSelfTest::socks5ProxyAuth() |
|
704 |
{ |
|
705 |
netChat(1081, QList<Chat>() |
|
706 |
// unauthenticated connect -- will get error |
|
707 |
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) |
|
708 |
<< Chat::expect(QByteArray(handshakeAuthNotOk, -1 + sizeof handshakeAuthNotOk)) |
|
709 |
<< Chat::RemoteDisconnect |
|
710 |
||
711 |
// now try to connect with authentication |
|
712 |
<< Chat::Reconnect |
|
713 |
<< Chat::send(QByteArray(handshakeAuthPassword, -1 + sizeof handshakeAuthPassword)) |
|
714 |
<< Chat::expect(QByteArray(handshakeOkPasswdAuth, -1 + sizeof handshakeOkPasswdAuth)) |
|
715 |
<< Chat::send(QByteArray(connect1, -1 + sizeof connect1)) |
|
716 |
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) |
|
717 |
<< Chat::expect("\1") // IPv4 address following |
|
718 |
<< Chat::skipBytes(6) // the server's local address and port |
|
719 |
<< ftpChat() |
|
720 |
); |
|
721 |
} |
|
722 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
723 |
void tst_NetworkSelfTest::supportsSsl() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
724 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
725 |
#ifdef QT_NO_OPENSSL |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
726 |
QFAIL("SSL not compiled in"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
727 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
728 |
QVERIFY(QSslSocket::supportsSsl()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
729 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
730 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
731 |
|
0 | 732 |
QTEST_MAIN(tst_NetworkSelfTest) |
733 |
#include "tst_networkselftest.moc" |