author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
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 |
||
46 |
#include <qapplication.h> |
|
47 |
||
48 |
||
49 |
||
50 |
||
51 |
#include <q3socket.h> |
|
52 |
#include "../network-settings.h" |
|
53 |
//TESTED_CLASS= |
|
54 |
//TESTED_FILES=network/q3socket.h network/q3socket.cpp |
|
55 |
||
56 |
class tst_Q3Socket : public QObject |
|
57 |
{ |
|
58 |
Q_OBJECT |
|
59 |
||
60 |
public: |
|
61 |
tst_Q3Socket(); |
|
62 |
virtual ~tst_Q3Socket(); |
|
63 |
||
64 |
||
65 |
public slots: |
|
66 |
void initTestCase(); |
|
67 |
void cleanupTestCase(); |
|
68 |
void init(); |
|
69 |
void cleanup(); |
|
70 |
private slots: |
|
71 |
void peerAddress_data(); |
|
72 |
void peerAddress(); |
|
73 |
void emitConnectionRefused(); |
|
74 |
void readBufferSize(); |
|
75 |
void connectionAttempts_data(); |
|
76 |
void connectionAttempts(); |
|
77 |
void canReadLine(); |
|
78 |
||
79 |
protected slots: |
|
80 |
void peerAddress_connected(); |
|
81 |
void peerAddress_connectionClosed(); |
|
82 |
void emitConnectionRefused_error(int); |
|
83 |
void connectionAttempts_connected(); |
|
84 |
void connectionAttempts_error(int); |
|
85 |
||
86 |
private: |
|
87 |
Q3Socket *socket; |
|
88 |
||
89 |
QHostAddress *peerAddress_addrConnected; |
|
90 |
uint peerAddress_portConnected; |
|
91 |
QHostAddress *peerAddress_addrClosed; |
|
92 |
uint peerAddress_portClosed; |
|
93 |
bool emitConnectionRefused_errorReceived; |
|
94 |
}; |
|
95 |
||
96 |
tst_Q3Socket::tst_Q3Socket() |
|
97 |
{ |
|
98 |
} |
|
99 |
||
100 |
tst_Q3Socket::~tst_Q3Socket() |
|
101 |
{ |
|
102 |
} |
|
103 |
||
104 |
void tst_Q3Socket::initTestCase() |
|
105 |
{ |
|
106 |
} |
|
107 |
||
108 |
void tst_Q3Socket::cleanupTestCase() |
|
109 |
{ |
|
110 |
} |
|
111 |
||
112 |
void tst_Q3Socket::init() |
|
113 |
{ |
|
114 |
} |
|
115 |
||
116 |
void tst_Q3Socket::cleanup() |
|
117 |
{ |
|
118 |
} |
|
119 |
||
120 |
void tst_Q3Socket::peerAddress_data() |
|
121 |
{ |
|
122 |
QTest::addColumn<QString>("host"); |
|
123 |
QTest::addColumn<uint>("port"); |
|
124 |
QTest::addColumn<bool>("peerClosesConnection"); |
|
125 |
QTest::addColumn<QString>("peerAddr"); |
|
126 |
||
127 |
QTest::newRow( "echo" ) << QtNetworkSettings::serverLocalName() << (uint)7 << false |
|
128 |
<< QtNetworkSettings::serverIP().toString(); |
|
129 |
QTest::newRow( "daytime" ) << QtNetworkSettings::serverLocalName() << (uint)13 << true |
|
130 |
<< QtNetworkSettings::serverIP().toString(); |
|
131 |
} |
|
132 |
||
133 |
void tst_Q3Socket::peerAddress() |
|
134 |
{ |
|
135 |
// init |
|
136 |
QFETCH( QString, host ); |
|
137 |
QFETCH( uint, port ); |
|
138 |
QFETCH( QString, peerAddr ); |
|
139 |
QFETCH( bool, peerClosesConnection ); |
|
140 |
||
141 |
socket = new Q3Socket; |
|
142 |
connect( socket, SIGNAL(connected()), SLOT(peerAddress_connected()) ); |
|
143 |
connect( socket, SIGNAL(connectionClosed()), SLOT(peerAddress_connectionClosed()) ); |
|
144 |
||
145 |
peerAddress_addrConnected = 0; |
|
146 |
peerAddress_addrClosed = 0; |
|
147 |
||
148 |
// connect to host |
|
149 |
socket->connectToHost( host, port ); |
|
150 |
QTestEventLoop::instance().enterLoop( 30 ); |
|
151 |
if ( QTestEventLoop::instance().timeout() && peerAddress_addrConnected==0 ) |
|
152 |
QFAIL( "Connection timed out" ); |
|
153 |
||
154 |
// test |
|
155 |
QHostAddress pa; |
|
156 |
QVERIFY( pa.setAddress(peerAddr) ); |
|
157 |
||
158 |
QTEST( peerAddress_addrConnected->toString(), "peerAddr" ); // results in nicer output than the test below |
|
159 |
QVERIFY( *peerAddress_addrConnected == pa ); |
|
160 |
QCOMPARE( peerAddress_portConnected, port ); |
|
161 |
||
162 |
if ( peerClosesConnection ) { |
|
163 |
QVERIFY( peerAddress_addrClosed != 0 ); |
|
164 |
||
165 |
QCOMPARE( peerAddress_addrClosed->toString(), QString() ); // results in nicer output than the test below |
|
166 |
QVERIFY( *peerAddress_addrClosed == QHostAddress::Null ); |
|
167 |
QCOMPARE( peerAddress_portClosed, (uint)0 ); |
|
168 |
} else { |
|
169 |
QVERIFY( peerAddress_addrClosed == 0 ); |
|
170 |
} |
|
171 |
||
172 |
// cleanup |
|
173 |
delete peerAddress_addrConnected; |
|
174 |
delete peerAddress_addrClosed; |
|
175 |
delete socket; |
|
176 |
} |
|
177 |
||
178 |
void tst_Q3Socket::peerAddress_connected() |
|
179 |
{ |
|
180 |
peerAddress_addrConnected = new QHostAddress( socket->peerAddress() ); |
|
181 |
peerAddress_portConnected = socket->peerPort(); |
|
182 |
QTestEventLoop::instance().changeInterval( 5 ); // enough time to get the closed |
|
183 |
} |
|
184 |
||
185 |
void tst_Q3Socket::peerAddress_connectionClosed() |
|
186 |
{ |
|
187 |
peerAddress_addrClosed = new QHostAddress( socket->peerAddress() ); |
|
188 |
peerAddress_portClosed = socket->peerPort(); |
|
189 |
QTestEventLoop::instance().exitLoop(); |
|
190 |
} |
|
191 |
||
192 |
void tst_Q3Socket::emitConnectionRefused() |
|
193 |
{ |
|
194 |
Q3Socket sock; |
|
195 |
connect( &sock, SIGNAL(error(int)), SLOT(emitConnectionRefused_error(int)) ); |
|
196 |
sock.connectToHost( QtNetworkSettings::serverLocalName(), 12331 ); |
|
197 |
||
198 |
emitConnectionRefused_errorReceived = false; |
|
199 |
QTestEventLoop::instance().enterLoop( 30 ); |
|
200 |
QVERIFY(emitConnectionRefused_errorReceived); |
|
201 |
} |
|
202 |
||
203 |
void tst_Q3Socket::emitConnectionRefused_error( int signum ) |
|
204 |
{ |
|
205 |
if ( signum == Q3Socket::ErrConnectionRefused ) |
|
206 |
emitConnectionRefused_errorReceived = true; |
|
207 |
||
208 |
QTestEventLoop::instance().exitLoop(); |
|
209 |
} |
|
210 |
||
211 |
void tst_Q3Socket::readBufferSize() |
|
212 |
{ |
|
213 |
const int bufferSize = 1024; |
|
214 |
||
215 |
Q3Socket sock; |
|
216 |
sock.setReadBufferSize(bufferSize); |
|
217 |
QCOMPARE((int)sock.readBufferSize(), bufferSize); |
|
218 |
||
219 |
sock.connectToHost("localhost", 0); |
|
220 |
||
221 |
QCOMPARE((int)sock.readBufferSize(), bufferSize); |
|
222 |
} |
|
223 |
||
224 |
void tst_Q3Socket::connectionAttempts_data() |
|
225 |
{ |
|
226 |
QTest::addColumn<QString>("host"); |
|
227 |
QTest::addColumn<int>("port"); |
|
228 |
QTest::addColumn<bool>("expectedResult"); |
|
229 |
||
230 |
QTest::newRow("fluke port 80") << QtNetworkSettings::serverLocalName() << 80 << true; |
|
231 |
QTest::newRow("fluke port 79") << QtNetworkSettings::serverLocalName() << 79 << false; |
|
232 |
} |
|
233 |
||
234 |
void tst_Q3Socket::connectionAttempts() |
|
235 |
{ |
|
236 |
QFETCH(QString, host); |
|
237 |
QFETCH(int, port); |
|
238 |
QFETCH(bool, expectedResult); |
|
239 |
||
240 |
Q3Socket sock; |
|
241 |
sock.connectToHost(host, port); |
|
242 |
connect(&sock, SIGNAL(connected()), SLOT(connectionAttempts_connected())); |
|
243 |
connect(&sock, SIGNAL(error(int)), SLOT(connectionAttempts_error(int))); |
|
244 |
QTestEventLoop::instance().enterLoop(10); |
|
245 |
||
246 |
if (QTestEventLoop::instance().timeout()) |
|
247 |
return; |
|
248 |
||
249 |
QCOMPARE(sock.state() == Q3Socket::Connected, expectedResult); |
|
250 |
} |
|
251 |
||
252 |
void tst_Q3Socket::connectionAttempts_connected() |
|
253 |
{ |
|
254 |
QTestEventLoop::instance().exitLoop(); |
|
255 |
} |
|
256 |
||
257 |
void tst_Q3Socket::connectionAttempts_error(int) |
|
258 |
{ |
|
259 |
QTestEventLoop::instance().exitLoop(); |
|
260 |
} |
|
261 |
||
262 |
void tst_Q3Socket::canReadLine() |
|
263 |
{ |
|
264 |
QEventLoop loop; |
|
265 |
||
266 |
Q3Socket socket; |
|
267 |
QVERIFY(!socket.canReadLine()); |
|
268 |
connect(&socket, SIGNAL(connected()), &loop, SLOT(quit())); |
|
269 |
// timeout error will fail in #238 |
|
270 |
connect(&socket, SIGNAL(error(int)), &loop, SLOT(quit())); |
|
271 |
socket.connectToHost(QtNetworkSettings::serverLocalName(), 143); |
|
272 |
||
273 |
loop.exec(); |
|
274 |
||
275 |
QCOMPARE(socket.state(), Q3Socket::Connected); |
|
276 |
||
277 |
while (!socket.canReadLine()) |
|
278 |
QVERIFY(socket.waitForMore(5000) > 0); |
|
279 |
||
280 |
QVERIFY(socket.canReadLine()); |
|
281 |
socket.readLine(); |
|
282 |
QVERIFY(!socket.canReadLine()); |
|
283 |
socket.ungetChar('\n'); |
|
284 |
QVERIFY(socket.canReadLine()); |
|
285 |
} |
|
286 |
||
287 |
QTEST_MAIN(tst_Q3Socket) |
|
288 |
#include "tst_qsocket.moc" |