author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 33 | 3e2da88830cd |
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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qtextstream.h> |
|
46 |
#include <QtNetwork/qlocalsocket.h> |
|
47 |
#include <QtNetwork/qlocalserver.h> |
|
48 |
#include "../../shared/util.h" |
|
49 |
||
50 |
#ifdef Q_OS_SYMBIAN |
|
51 |
#include <unistd.h> |
|
52 |
#endif |
|
53 |
//TESTED_CLASS=QLocalServer, QLocalSocket |
|
54 |
//TESTED_FILES=network/socket/qlocalserver.cpp network/socket/qlocalsocket.cpp |
|
55 |
#ifdef Q_OS_SYMBIAN |
|
56 |
#define STRINGIFY(x) #x |
|
57 |
#define TOSTRING(x) STRINGIFY(x) |
|
58 |
#define SRCDIR "C:/Private/" TOSTRING(SYMBIAN_SRCDIR_UID) "/" |
|
59 |
#endif |
|
60 |
Q_DECLARE_METATYPE(QLocalSocket::LocalSocketError) |
|
61 |
Q_DECLARE_METATYPE(QLocalSocket::LocalSocketState) |
|
62 |
||
63 |
class tst_QLocalSocket : public QObject |
|
64 |
{ |
|
65 |
Q_OBJECT |
|
66 |
||
67 |
public: |
|
68 |
tst_QLocalSocket(); |
|
69 |
virtual ~tst_QLocalSocket(); |
|
70 |
||
71 |
public Q_SLOTS: |
|
72 |
void init(); |
|
73 |
void cleanup(); |
|
74 |
||
75 |
private slots: |
|
76 |
// basics |
|
77 |
void server_basic(); |
|
78 |
void server_connectionsCount(); |
|
79 |
void socket_basic(); |
|
80 |
||
81 |
void listen_data(); |
|
82 |
void listen(); |
|
83 |
||
84 |
void listenAndConnect_data(); |
|
85 |
void listenAndConnect(); |
|
86 |
||
87 |
void sendData_data(); |
|
88 |
void sendData(); |
|
89 |
||
90 |
void readBufferOverflow(); |
|
91 |
||
92 |
void fullPath(); |
|
93 |
||
94 |
void hitMaximumConnections_data(); |
|
95 |
void hitMaximumConnections(); |
|
96 |
||
97 |
void setSocketDescriptor(); |
|
98 |
||
99 |
void threadedConnection_data(); |
|
100 |
void threadedConnection(); |
|
101 |
||
102 |
void processConnection_data(); |
|
103 |
void processConnection(); |
|
104 |
||
105 |
void longPath(); |
|
106 |
void waitForDisconnect(); |
|
107 |
void waitForDisconnectByServer(); |
|
108 |
||
109 |
void removeServer(); |
|
110 |
||
111 |
void recycleServer(); |
|
112 |
||
113 |
void multiConnect(); |
|
114 |
void writeOnlySocket(); |
|
115 |
void writeToClientAndDisconnect(); |
|
116 |
void debug(); |
|
117 |
void bytesWrittenSignal(); |
|
118 |
||
119 |
||
120 |
#ifdef Q_OS_SYMBIAN |
|
121 |
private: |
|
122 |
void unlink(QString serverName); |
|
123 |
#endif |
|
124 |
}; |
|
125 |
||
126 |
tst_QLocalSocket::tst_QLocalSocket() |
|
127 |
{ |
|
128 |
if (!QFile::exists("lackey/lackey" |
|
129 |
#ifdef Q_OS_WIN |
|
130 |
".exe" |
|
131 |
#endif |
|
132 |
)) |
|
133 |
qWarning() << "lackey executable doesn't exists!"; |
|
134 |
} |
|
135 |
||
136 |
tst_QLocalSocket::~tst_QLocalSocket() |
|
137 |
{ |
|
138 |
} |
|
139 |
||
140 |
void tst_QLocalSocket::init() |
|
141 |
{ |
|
142 |
qRegisterMetaType<QLocalSocket::LocalSocketState>("QLocalSocket::LocalSocketState"); |
|
143 |
qRegisterMetaType<QLocalSocket::LocalSocketError>("QLocalSocket::LocalSocketError"); |
|
144 |
} |
|
145 |
||
146 |
void tst_QLocalSocket::cleanup() |
|
147 |
{ |
|
148 |
} |
|
149 |
||
150 |
class LocalServer : public QLocalServer |
|
151 |
{ |
|
152 |
Q_OBJECT |
|
153 |
||
154 |
public: |
|
155 |
LocalServer() : QLocalServer() |
|
156 |
{ |
|
157 |
connect(this, SIGNAL(newConnection()), this, SLOT(slotNewConnection())); |
|
158 |
} |
|
159 |
||
160 |
bool listen(const QString &name) |
|
161 |
{ |
|
162 |
removeServer(name); |
|
163 |
return QLocalServer::listen(name); |
|
164 |
} |
|
165 |
||
166 |
QList<int> hits; |
|
167 |
||
168 |
protected: |
|
169 |
void incomingConnection(quintptr socketDescriptor) |
|
170 |
{ |
|
171 |
hits.append(socketDescriptor); |
|
172 |
QLocalServer::incomingConnection(socketDescriptor); |
|
173 |
} |
|
174 |
||
175 |
private slots: |
|
176 |
void slotNewConnection() { |
|
177 |
QVERIFY(!hits.isEmpty()); |
|
178 |
QVERIFY(hasPendingConnections()); |
|
179 |
} |
|
180 |
}; |
|
181 |
||
182 |
class LocalSocket : public QLocalSocket |
|
183 |
{ |
|
184 |
Q_OBJECT |
|
185 |
||
186 |
public: |
|
187 |
LocalSocket(QObject *parent = 0) : QLocalSocket(parent) |
|
188 |
{ |
|
189 |
connect(this, SIGNAL(connected()), |
|
190 |
this, SLOT(slotConnected())); |
|
191 |
connect(this, SIGNAL(disconnected()), |
|
192 |
this, SLOT(slotDisconnected())); |
|
193 |
connect(this, SIGNAL(error(QLocalSocket::LocalSocketError)), |
|
194 |
this, SLOT(slotError(QLocalSocket::LocalSocketError))); |
|
195 |
connect(this, SIGNAL(stateChanged(QLocalSocket::LocalSocketState)), |
|
196 |
this, SLOT(slotStateChanged(QLocalSocket::LocalSocketState))); |
|
197 |
connect(this, SIGNAL(readyRead()), |
|
198 |
this, SLOT(slotReadyRead())); |
|
199 |
} |
|
200 |
||
201 |
private slots: |
|
202 |
void slotConnected() |
|
203 |
{ |
|
204 |
QCOMPARE(state(), QLocalSocket::ConnectedState); |
|
205 |
} |
|
206 |
void slotDisconnected() |
|
207 |
{ |
|
208 |
QCOMPARE(state(), QLocalSocket::UnconnectedState); |
|
209 |
} |
|
210 |
void slotError(QLocalSocket::LocalSocketError newError) |
|
211 |
{ |
|
212 |
QVERIFY(errorString() != "Unknown error"); |
|
213 |
QCOMPARE(error(), newError); |
|
214 |
} |
|
215 |
void slotStateChanged(QLocalSocket::LocalSocketState newState) |
|
216 |
{ |
|
217 |
QCOMPARE(state(), newState); |
|
218 |
} |
|
219 |
void slotReadyRead() |
|
220 |
{ |
|
221 |
QVERIFY(bytesAvailable() > 0); |
|
222 |
} |
|
223 |
}; |
|
224 |
||
225 |
// basic test make sure no segfaults and check default values |
|
226 |
void tst_QLocalSocket::server_basic() |
|
227 |
{ |
|
228 |
LocalServer server; |
|
229 |
QSignalSpy spyNewConnection(&server, SIGNAL(newConnection())); |
|
230 |
server.close(); |
|
231 |
QCOMPARE(server.errorString(), QString()); |
|
232 |
QCOMPARE(server.hasPendingConnections(), false); |
|
233 |
QCOMPARE(server.isListening(), false); |
|
234 |
QCOMPARE(server.maxPendingConnections(), 30); |
|
235 |
QCOMPARE(server.nextPendingConnection(), (QLocalSocket*)0); |
|
236 |
QCOMPARE(server.serverName(), QString()); |
|
237 |
QCOMPARE(server.fullServerName(), QString()); |
|
238 |
QCOMPARE(server.serverError(), QAbstractSocket::UnknownSocketError); |
|
239 |
server.setMaxPendingConnections(20); |
|
240 |
bool timedOut = true; |
|
241 |
QCOMPARE(server.waitForNewConnection(3000, &timedOut), false); |
|
242 |
QVERIFY(!timedOut); |
|
243 |
QCOMPARE(server.listen(QString()), false); |
|
244 |
||
245 |
QCOMPARE(server.hits.count(), 0); |
|
246 |
QCOMPARE(spyNewConnection.count(), 0); |
|
247 |
} |
|
248 |
||
249 |
void tst_QLocalSocket::server_connectionsCount() |
|
250 |
{ |
|
251 |
LocalServer server; |
|
252 |
server.setMaxPendingConnections(10); |
|
253 |
QCOMPARE(server.maxPendingConnections(), 10); |
|
254 |
} |
|
255 |
||
256 |
// basic test make sure no segfaults and check default values |
|
257 |
void tst_QLocalSocket::socket_basic() |
|
258 |
{ |
|
259 |
LocalSocket socket; |
|
260 |
QSignalSpy spyConnected(&socket, SIGNAL(connected())); |
|
261 |
QSignalSpy spyDisconnected(&socket, SIGNAL(disconnected())); |
|
262 |
QSignalSpy spyError(&socket, SIGNAL(error(QLocalSocket::LocalSocketError))); |
|
263 |
QSignalSpy spyStateChanged(&socket, SIGNAL(stateChanged(QLocalSocket::LocalSocketState))); |
|
264 |
QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead())); |
|
265 |
||
266 |
QCOMPARE(socket.serverName(), QString()); |
|
267 |
QCOMPARE(socket.fullServerName(), QString()); |
|
268 |
socket.abort(); |
|
269 |
QVERIFY(socket.bytesAvailable() == 0); |
|
270 |
QVERIFY(socket.bytesToWrite() == 0); |
|
271 |
QCOMPARE(socket.canReadLine(), false); |
|
272 |
socket.close(); |
|
273 |
socket.disconnectFromServer(); |
|
274 |
QCOMPARE(QLocalSocket::UnknownSocketError, socket.error()); |
|
275 |
QVERIFY(socket.errorString() != QString()); |
|
276 |
QCOMPARE(socket.flush(), false); |
|
277 |
QCOMPARE(socket.isValid(), false); |
|
278 |
QVERIFY(socket.readBufferSize() == 0); |
|
279 |
socket.setReadBufferSize(0); |
|
280 |
//QCOMPARE(socket.socketDescriptor(), -1); |
|
281 |
QCOMPARE(socket.state(), QLocalSocket::UnconnectedState); |
|
282 |
QCOMPARE(socket.waitForConnected(0), false); |
|
283 |
QCOMPARE(socket.waitForDisconnected(0), false); |
|
284 |
QCOMPARE(socket.waitForReadyRead(0), false); |
|
285 |
||
286 |
QCOMPARE(spyConnected.count(), 0); |
|
287 |
QCOMPARE(spyDisconnected.count(), 0); |
|
288 |
QCOMPARE(spyError.count(), 0); |
|
289 |
QCOMPARE(spyStateChanged.count(), 0); |
|
290 |
QCOMPARE(spyReadyRead.count(), 0); |
|
291 |
} |
|
292 |
||
293 |
void tst_QLocalSocket::listen_data() |
|
294 |
{ |
|
295 |
QTest::addColumn<QString>("name"); |
|
296 |
QTest::addColumn<bool>("canListen"); |
|
297 |
QTest::addColumn<bool>("close"); |
|
298 |
QTest::newRow("null") << QString() << false << false; |
|
299 |
QTest::newRow("tst_localsocket") << "tst_localsocket" << true << true; |
|
300 |
QTest::newRow("tst_localsocket") << "tst_localsocket" << true << false; |
|
301 |
} |
|
302 |
||
303 |
// start a server that listens, but don't connect a socket, make sure everything is in order |
|
304 |
void tst_QLocalSocket::listen() |
|
305 |
{ |
|
306 |
LocalServer server; |
|
307 |
QSignalSpy spyNewConnection(&server, SIGNAL(newConnection())); |
|
308 |
||
309 |
QFETCH(QString, name); |
|
310 |
#ifdef Q_OS_SYMBIAN |
|
311 |
unlink(name); |
|
312 |
#endif |
|
313 |
QFETCH(bool, canListen); |
|
314 |
QFETCH(bool, close); |
|
315 |
QVERIFY2((server.listen(name) == canListen), server.errorString().toLatin1().constData()); |
|
316 |
||
317 |
// test listening |
|
318 |
QCOMPARE(server.serverName(), name); |
|
319 |
QVERIFY(server.fullServerName().contains(name)); |
|
320 |
QCOMPARE(server.isListening(), canListen); |
|
321 |
QCOMPARE(server.hasPendingConnections(), false); |
|
322 |
QCOMPARE(server.nextPendingConnection(), (QLocalSocket*)0); |
|
323 |
QCOMPARE(server.hits.count(), 0); |
|
324 |
QCOMPARE(spyNewConnection.count(), 0); |
|
325 |
if (canListen) { |
|
326 |
QVERIFY(server.errorString() == QString()); |
|
327 |
QCOMPARE(server.serverError(), QAbstractSocket::UnknownSocketError); |
|
328 |
// already isListening |
|
329 |
QVERIFY(!server.listen(name)); |
|
330 |
} else { |
|
331 |
QVERIFY(server.errorString() != QString()); |
|
332 |
QCOMPARE(server.serverError(), QAbstractSocket::HostNotFoundError); |
|
333 |
} |
|
334 |
QCOMPARE(server.maxPendingConnections(), 30); |
|
335 |
bool timedOut = false; |
|
336 |
QCOMPARE(server.waitForNewConnection(3000, &timedOut), false); |
|
337 |
QCOMPARE(timedOut, canListen); |
|
338 |
if (close) |
|
339 |
server.close(); |
|
340 |
} |
|
341 |
||
342 |
void tst_QLocalSocket::listenAndConnect_data() |
|
343 |
{ |
|
344 |
QTest::addColumn<QString>("name"); |
|
345 |
QTest::addColumn<bool>("canListen"); |
|
346 |
QTest::addColumn<int>("connections"); |
|
347 |
for (int i = 0; i < 3; ++i) { |
|
348 |
int connections = i; |
|
349 |
if (i == 2) |
|
350 |
connections = 5; |
|
351 |
QTest::newRow(QString("null %1").arg(i).toLatin1()) << QString() << false << connections; |
|
352 |
QTest::newRow(QString("tst_localsocket %1").arg(i).toLatin1()) << "tst_localsocket" << true << connections; |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
void tst_QLocalSocket::listenAndConnect() |
|
357 |
{ |
|
358 |
LocalServer server; |
|
359 |
QSignalSpy spyNewConnection(&server, SIGNAL(newConnection())); |
|
360 |
||
361 |
QFETCH(QString, name); |
|
362 |
QFETCH(bool, canListen); |
|
363 |
#ifdef Q_OS_SYMBIAN |
|
364 |
unlink(name); |
|
365 |
#endif |
|
366 |
QCOMPARE(server.listen(name), canListen); |
|
367 |
QTest::qWait(1000); |
|
368 |
//QVERIFY(!server.errorString().isEmpty()); |
|
369 |
QCOMPARE(server.serverError(), |
|
370 |
canListen ? QAbstractSocket::UnknownSocketError : QAbstractSocket::HostNotFoundError); |
|
371 |
||
372 |
// test creating connection(s) |
|
373 |
QFETCH(int, connections); |
|
374 |
QList<QLocalSocket*> sockets; |
|
375 |
for (int i = 0; i < connections; ++i) { |
|
376 |
LocalSocket *socket = new LocalSocket; |
|
377 |
||
378 |
QSignalSpy spyConnected(socket, SIGNAL(connected())); |
|
379 |
QSignalSpy spyDisconnected(socket, SIGNAL(disconnected())); |
|
380 |
QSignalSpy spyError(socket, SIGNAL(error(QLocalSocket::LocalSocketError))); |
|
381 |
QSignalSpy spyStateChanged(socket, SIGNAL(stateChanged(QLocalSocket::LocalSocketState))); |
|
382 |
QSignalSpy spyReadyRead(socket, SIGNAL(readyRead())); |
|
383 |
||
384 |
socket->connectToServer(name); |
|
385 |
#if defined(QT_LOCALSOCKET_TCP) |
|
386 |
QTest::qWait(250); |
|
387 |
#endif |
|
388 |
||
389 |
QCOMPARE(socket->serverName(), name); |
|
390 |
QVERIFY(socket->fullServerName().contains(name)); |
|
391 |
sockets.append(socket); |
|
392 |
if (canListen) { |
|
393 |
QCOMPARE(socket->errorString(), QString("Unknown error")); |
|
394 |
QCOMPARE(socket->error(), QLocalSocket::UnknownSocketError); |
|
395 |
QCOMPARE(socket->state(), QLocalSocket::ConnectedState); |
|
396 |
//QVERIFY(socket->socketDescriptor() != -1); |
|
397 |
QCOMPARE(spyError.count(), 0); |
|
398 |
} else { |
|
399 |
QVERIFY(socket->errorString() != QString()); |
|
400 |
QVERIFY(socket->error() != QLocalSocket::UnknownSocketError); |
|
401 |
QCOMPARE(socket->state(), QLocalSocket::UnconnectedState); |
|
402 |
//QVERIFY(socket->socketDescriptor() == -1); |
|
403 |
QCOMPARE(qVariantValue<QLocalSocket::LocalSocketError>(spyError.first()[0]), |
|
404 |
QLocalSocket::ServerNotFoundError); |
|
405 |
} |
|
406 |
||
407 |
QVERIFY(socket->bytesAvailable() == 0); |
|
408 |
QVERIFY(socket->bytesToWrite() == 0); |
|
409 |
QCOMPARE(socket->canReadLine(), false); |
|
410 |
QCOMPARE(socket->flush(), false); |
|
411 |
QCOMPARE(socket->isValid(), canListen); |
|
412 |
QCOMPARE(socket->readBufferSize(), (qint64)0); |
|
413 |
QCOMPARE(socket->waitForConnected(0), canListen); |
|
414 |
QCOMPARE(socket->waitForReadyRead(0), false); |
|
415 |
||
416 |
QTRY_COMPARE(spyConnected.count(), canListen ? 1 : 0); |
|
417 |
QCOMPARE(spyDisconnected.count(), 0); |
|
418 |
||
419 |
// error signals |
|
420 |
QVERIFY(spyError.count() >= 0); |
|
421 |
if (canListen) { |
|
422 |
if (spyError.count() > 0) |
|
423 |
QCOMPARE(qVariantValue<QLocalSocket::LocalSocketError>(spyError.first()[0]), |
|
424 |
QLocalSocket::SocketTimeoutError); |
|
425 |
} else { |
|
426 |
QCOMPARE(qVariantValue<QLocalSocket::LocalSocketError>(spyError.first()[0]), |
|
427 |
QLocalSocket::ServerNotFoundError); |
|
428 |
} |
|
429 |
||
430 |
// Check first and last state |
|
431 |
QCOMPARE(qVariantValue<QLocalSocket::LocalSocketState>(spyStateChanged.first()[0]), |
|
432 |
QLocalSocket::ConnectingState); |
|
433 |
#if 0 |
|
434 |
for (int j = 0; j < spyStateChanged.count(); ++j) { |
|
435 |
QLocalSocket::LocalSocketState s; |
|
436 |
s = qVariantValue<QLocalSocket::LocalSocketState>(spyStateChanged.at(j).at(0)); |
|
437 |
qDebug() << s; |
|
438 |
} |
|
439 |
#endif |
|
440 |
if (canListen) |
|
441 |
QCOMPARE(qVariantValue<QLocalSocket::LocalSocketState>(spyStateChanged.last()[0]), |
|
442 |
QLocalSocket::ConnectedState); |
|
443 |
QCOMPARE(spyStateChanged.count(), 2); |
|
444 |
QCOMPARE(spyReadyRead.count(), 0); |
|
445 |
||
446 |
bool timedOut = true; |
|
447 |
QCOMPARE(server.waitForNewConnection(3000, &timedOut), canListen); |
|
448 |
QVERIFY(!timedOut); |
|
449 |
QCOMPARE(server.hasPendingConnections(), canListen); |
|
450 |
QCOMPARE(server.isListening(), canListen); |
|
451 |
// NOTE: socket disconnecting is not tested here |
|
452 |
||
453 |
// server checks post connection |
|
454 |
if (canListen) { |
|
455 |
QCOMPARE(server.serverName(), name); |
|
456 |
QVERIFY(server.fullServerName().contains(name)); |
|
457 |
QVERIFY(server.nextPendingConnection() != (QLocalSocket*)0); |
|
458 |
QTRY_COMPARE(server.hits.count(), i + 1); |
|
459 |
QCOMPARE(spyNewConnection.count(), i + 1); |
|
460 |
QVERIFY(server.errorString() == QString()); |
|
461 |
QCOMPARE(server.serverError(), QAbstractSocket::UnknownSocketError); |
|
462 |
} else { |
|
463 |
QVERIFY(server.serverName().isEmpty()); |
|
464 |
QVERIFY(server.fullServerName().isEmpty()); |
|
465 |
QVERIFY(server.nextPendingConnection() == (QLocalSocket*)0); |
|
466 |
QCOMPARE(spyNewConnection.count(), 0); |
|
467 |
QCOMPARE(server.hits.count(), 0); |
|
468 |
QVERIFY(server.errorString() != QString()); |
|
469 |
QCOMPARE(server.serverError(), QAbstractSocket::HostNotFoundError); |
|
470 |
} |
|
471 |
} |
|
472 |
qDeleteAll(sockets.begin(), sockets.end()); |
|
473 |
||
474 |
server.close(); |
|
475 |
||
476 |
QCOMPARE(server.hits.count(), (canListen ? connections : 0)); |
|
477 |
QCOMPARE(spyNewConnection.count(), (canListen ? connections : 0)); |
|
478 |
} |
|
479 |
||
480 |
void tst_QLocalSocket::sendData_data() |
|
481 |
{ |
|
482 |
listenAndConnect_data(); |
|
483 |
} |
|
484 |
||
485 |
void tst_QLocalSocket::sendData() |
|
486 |
{ |
|
487 |
QFETCH(QString, name); |
|
488 |
#ifdef Q_OS_SYMBIAN |
|
489 |
unlink(name); |
|
490 |
#endif |
|
491 |
QFETCH(bool, canListen); |
|
492 |
||
493 |
LocalServer server; |
|
494 |
QSignalSpy spy(&server, SIGNAL(newConnection())); |
|
495 |
||
496 |
QCOMPARE(server.listen(name), canListen); |
|
497 |
||
498 |
LocalSocket socket; |
|
499 |
QSignalSpy spyConnected(&socket, SIGNAL(connected())); |
|
500 |
QSignalSpy spyDisconnected(&socket, SIGNAL(disconnected())); |
|
501 |
QSignalSpy spyError(&socket, SIGNAL(error(QLocalSocket::LocalSocketError))); |
|
502 |
QSignalSpy spyStateChanged(&socket, SIGNAL(stateChanged(QLocalSocket::LocalSocketState))); |
|
503 |
QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead())); |
|
504 |
||
505 |
// test creating a connection |
|
506 |
socket.connectToServer(name); |
|
507 |
bool timedOut = true; |
|
508 |
||
509 |
QCOMPARE(server.waitForNewConnection(3000, &timedOut), canListen); |
|
510 |
||
511 |
#if defined(QT_LOCALSOCKET_TCP) |
|
512 |
QTest::qWait(250); |
|
513 |
#endif |
|
514 |
QVERIFY(!timedOut); |
|
515 |
QCOMPARE(spyConnected.count(), canListen ? 1 : 0); |
|
516 |
QCOMPARE(socket.state(), canListen ? QLocalSocket::ConnectedState : QLocalSocket::UnconnectedState); |
|
517 |
||
518 |
// test sending/receiving data |
|
519 |
if (server.hasPendingConnections()) { |
|
520 |
QString testLine = "test"; |
|
521 |
#ifdef Q_OS_SYMBIAN |
|
522 |
for (int i = 0; i < 25 * 1024; ++i) |
|
523 |
#else |
|
524 |
for (int i = 0; i < 50000; ++i) |
|
525 |
#endif |
|
526 |
testLine += "a"; |
|
527 |
QLocalSocket *serverSocket = server.nextPendingConnection(); |
|
528 |
QVERIFY(serverSocket); |
|
529 |
QCOMPARE(serverSocket->state(), QLocalSocket::ConnectedState); |
|
530 |
QTextStream out(serverSocket); |
|
531 |
QTextStream in(&socket); |
|
532 |
out << testLine << endl; |
|
533 |
bool wrote = serverSocket->waitForBytesWritten(3000); |
|
534 |
||
535 |
if (!socket.canReadLine()) |
|
536 |
QVERIFY(socket.waitForReadyRead()); |
|
537 |
||
538 |
QVERIFY(socket.bytesAvailable() >= 0); |
|
539 |
QCOMPARE(socket.bytesToWrite(), (qint64)0); |
|
540 |
QCOMPARE(socket.flush(), false); |
|
541 |
QCOMPARE(socket.isValid(), canListen); |
|
542 |
QCOMPARE(socket.readBufferSize(), (qint64)0); |
|
543 |
QCOMPARE(spyReadyRead.count(), 1); |
|
544 |
||
545 |
QVERIFY(testLine.startsWith(in.readLine())); |
|
546 |
||
547 |
QVERIFY(wrote || serverSocket->waitForBytesWritten(1000)); |
|
548 |
||
549 |
QCOMPARE(serverSocket->errorString(), QString("Unknown error")); |
|
550 |
QCOMPARE(socket.errorString(), QString("Unknown error")); |
|
551 |
} |
|
552 |
||
553 |
socket.disconnectFromServer(); |
|
554 |
QCOMPARE(spyConnected.count(), canListen ? 1 : 0); |
|
555 |
QCOMPARE(spyDisconnected.count(), canListen ? 1 : 0); |
|
556 |
QCOMPARE(spyError.count(), canListen ? 0 : 1); |
|
557 |
QCOMPARE(spyStateChanged.count(), canListen ? 4 : 2); |
|
558 |
QCOMPARE(spyReadyRead.count(), canListen ? 1 : 0); |
|
559 |
||
560 |
server.close(); |
|
561 |
||
562 |
QCOMPARE(server.hits.count(), (canListen ? 1 : 0)); |
|
563 |
QCOMPARE(spy.count(), (canListen ? 1 : 0)); |
|
564 |
} |
|
565 |
||
566 |
void tst_QLocalSocket::readBufferOverflow() |
|
567 |
{ |
|
568 |
const int readBufferSize = 128; |
|
569 |
const int dataBufferSize = readBufferSize * 2; |
|
570 |
const QString serverName = QLatin1String("myPreciousTestServer"); |
|
571 |
LocalServer server; |
|
572 |
server.listen(serverName); |
|
573 |
QVERIFY(server.isListening()); |
|
574 |
||
575 |
LocalSocket client; |
|
576 |
client.setReadBufferSize(readBufferSize); |
|
577 |
client.connectToServer(serverName); |
|
578 |
||
579 |
bool timedOut = true; |
|
580 |
QVERIFY(server.waitForNewConnection(3000, &timedOut)); |
|
581 |
QVERIFY(!timedOut); |
|
582 |
||
583 |
QCOMPARE(client.state(), QLocalSocket::ConnectedState); |
|
584 |
QVERIFY(server.hasPendingConnections()); |
|
585 |
||
586 |
QLocalSocket* serverSocket = server.nextPendingConnection(); |
|
587 |
char buffer[dataBufferSize]; |
|
588 |
memset(buffer, 0, dataBufferSize); |
|
589 |
serverSocket->write(buffer, dataBufferSize); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
590 |
serverSocket->waitForBytesWritten(); |
0 | 591 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
592 |
// wait until the first 128 bytes are ready to read |
0 | 593 |
QVERIFY(client.waitForReadyRead()); |
594 |
QCOMPARE(client.read(buffer, readBufferSize), qint64(readBufferSize)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
595 |
// wait until the second 128 bytes are ready to read |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
596 |
QVERIFY(client.waitForReadyRead()); |
0 | 597 |
QCOMPARE(client.read(buffer, readBufferSize), qint64(readBufferSize)); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
598 |
// no more bytes available |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
599 |
QVERIFY(client.bytesAvailable() == 0); |
0 | 600 |
} |
601 |
||
602 |
// QLocalSocket/Server can take a name or path, check that it works as expected |
|
603 |
void tst_QLocalSocket::fullPath() |
|
604 |
{ |
|
605 |
QLocalServer server; |
|
606 |
QString name = "qlocalsocket_pathtest"; |
|
607 |
#if defined(Q_OS_SYMBIAN) |
|
608 |
QString path = ""; |
|
609 |
#elif defined(QT_LOCALSOCKET_TCP) |
|
610 |
QString path = "QLocalServer"; |
|
611 |
#elif defined(Q_OS_WIN) |
|
612 |
QString path = "\\\\.\\pipe\\"; |
|
613 |
#else |
|
614 |
QString path = "/tmp"; |
|
615 |
#endif |
|
616 |
QString serverName = path + '/' + name; |
|
617 |
QVERIFY2(server.listen(serverName), server.errorString().toLatin1().constData()); |
|
618 |
QCOMPARE(server.serverName(), serverName); |
|
619 |
QCOMPARE(server.fullServerName(), serverName); |
|
620 |
||
621 |
LocalSocket socket; |
|
622 |
socket.connectToServer(serverName); |
|
623 |
||
624 |
QCOMPARE(socket.serverName(), serverName); |
|
625 |
QCOMPARE(socket.fullServerName(), serverName); |
|
626 |
socket.disconnectFromServer(); |
|
627 |
#ifdef QT_LOCALSOCKET_TCP |
|
628 |
QTest::qWait(250); |
|
629 |
#endif |
|
630 |
QCOMPARE(socket.serverName(), QString()); |
|
631 |
QCOMPARE(socket.fullServerName(), QString()); |
|
632 |
} |
|
633 |
||
634 |
void tst_QLocalSocket::hitMaximumConnections_data() |
|
635 |
{ |
|
636 |
QTest::addColumn<int>("max"); |
|
637 |
QTest::newRow("none") << 0; |
|
638 |
QTest::newRow("1") << 1; |
|
639 |
QTest::newRow("3") << 3; |
|
640 |
} |
|
641 |
||
642 |
void tst_QLocalSocket::hitMaximumConnections() |
|
643 |
{ |
|
644 |
QFETCH(int, max); |
|
645 |
LocalServer server; |
|
646 |
QString name = "tst_localsocket"; |
|
647 |
#ifdef Q_OS_SYMBIAN |
|
648 |
unlink(name); |
|
649 |
#endif |
|
650 |
server.setMaxPendingConnections(max); |
|
651 |
QVERIFY2(server.listen(name), server.errorString().toLatin1().constData()); |
|
652 |
int connections = server.maxPendingConnections() + 1; |
|
653 |
QList<QLocalSocket*> sockets; |
|
654 |
for (int i = 0; i < connections; ++i) { |
|
655 |
LocalSocket *socket = new LocalSocket; |
|
656 |
sockets.append(socket); |
|
657 |
socket->connectToServer(name); |
|
658 |
} |
|
659 |
bool timedOut = true; |
|
660 |
QVERIFY(server.waitForNewConnection(3000, &timedOut)); |
|
661 |
QVERIFY(!timedOut); |
|
662 |
QVERIFY(server.hits.count() > 0); |
|
663 |
qDeleteAll(sockets.begin(), sockets.end()); |
|
664 |
} |
|
665 |
||
666 |
// check that state and mode are kept |
|
667 |
void tst_QLocalSocket::setSocketDescriptor() |
|
668 |
{ |
|
669 |
LocalSocket socket; |
|
670 |
quintptr minusOne = -1; |
|
671 |
socket.setSocketDescriptor(minusOne, QLocalSocket::ConnectingState, QIODevice::Append); |
|
672 |
QCOMPARE(socket.socketDescriptor(), minusOne); |
|
673 |
QCOMPARE(socket.state(), QLocalSocket::ConnectingState); |
|
674 |
QVERIFY((socket.openMode() & QIODevice::Append) != 0); |
|
675 |
} |
|
676 |
||
677 |
class Client : public QThread |
|
678 |
{ |
|
679 |
||
680 |
public: |
|
681 |
void run() |
|
682 |
{ |
|
683 |
QString testLine = "test"; |
|
684 |
LocalSocket socket; |
|
685 |
QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead())); |
|
686 |
int tries = 0; |
|
687 |
do { |
|
688 |
socket.connectToServer("qlocalsocket_threadtest"); |
|
689 |
if (socket.error() != QLocalSocket::ServerNotFoundError |
|
690 |
&& socket.error() != QLocalSocket::ConnectionRefusedError) |
|
691 |
break; |
|
692 |
QTest::qWait(100); |
|
693 |
++tries; |
|
694 |
} while ((socket.error() == QLocalSocket::ServerNotFoundError |
|
695 |
|| socket.error() == QLocalSocket::ConnectionRefusedError) |
|
696 |
&& tries < 1000); |
|
697 |
if (tries == 0 && socket.state() != QLocalSocket::ConnectedState) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
698 |
QVERIFY(socket.waitForConnected(7000)); |
0 | 699 |
QVERIFY(socket.state() == QLocalSocket::ConnectedState); |
700 |
} |
|
701 |
||
702 |
// We should *not* have this signal yet! |
|
703 |
if (tries == 0) |
|
704 |
QCOMPARE(spyReadyRead.count(), 0); |
|
705 |
socket.waitForReadyRead(); |
|
706 |
QCOMPARE(spyReadyRead.count(), 1); |
|
707 |
QTextStream in(&socket); |
|
708 |
QCOMPARE(in.readLine(), testLine); |
|
709 |
socket.close(); |
|
710 |
} |
|
711 |
}; |
|
712 |
||
713 |
class Server : public QThread |
|
714 |
{ |
|
715 |
||
716 |
public: |
|
717 |
int clients; |
|
718 |
void run() |
|
719 |
{ |
|
720 |
QString testLine = "test"; |
|
721 |
LocalServer server; |
|
722 |
server.setMaxPendingConnections(10); |
|
723 |
QVERIFY2(server.listen("qlocalsocket_threadtest"), |
|
724 |
server.errorString().toLatin1().constData()); |
|
725 |
int done = clients; |
|
726 |
while (done > 0) { |
|
727 |
bool timedOut = true; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
728 |
QVERIFY(server.waitForNewConnection(7000, &timedOut)); |
0 | 729 |
QVERIFY(!timedOut); |
730 |
QLocalSocket *serverSocket = server.nextPendingConnection(); |
|
731 |
QVERIFY(serverSocket); |
|
732 |
QTextStream out(serverSocket); |
|
733 |
out << testLine << endl; |
|
734 |
QCOMPARE(serverSocket->state(), QLocalSocket::ConnectedState); |
|
735 |
QVERIFY2(serverSocket->waitForBytesWritten(), serverSocket->errorString().toLatin1().constData()); |
|
736 |
QCOMPARE(serverSocket->errorString(), QString("Unknown error")); |
|
737 |
--done; |
|
738 |
delete serverSocket; |
|
739 |
} |
|
740 |
QCOMPARE(server.hits.count(), clients); |
|
741 |
} |
|
742 |
}; |
|
743 |
||
744 |
void tst_QLocalSocket::threadedConnection_data() |
|
745 |
{ |
|
746 |
QTest::addColumn<int>("threads"); |
|
747 |
QTest::newRow("1 client") << 1; |
|
748 |
QTest::newRow("2 clients") << 2; |
|
749 |
#ifdef Q_OS_WINCE |
|
750 |
QTest::newRow("4 clients") << 4; |
|
751 |
#endif |
|
752 |
#ifndef Q_OS_WIN |
|
753 |
QTest::newRow("5 clients") << 5; |
|
754 |
QTest::newRow("10 clients") << 10; |
|
755 |
#endif |
|
756 |
#ifndef Q_OS_WINCE |
|
757 |
QTest::newRow("20 clients") << 20; |
|
758 |
#endif |
|
759 |
} |
|
760 |
||
761 |
void tst_QLocalSocket::threadedConnection() |
|
762 |
{ |
|
763 |
#ifdef Q_OS_SYMBIAN |
|
764 |
unlink("qlocalsocket_threadtest"); |
|
765 |
#endif |
|
766 |
||
767 |
QFETCH(int, threads); |
|
768 |
Server server; |
|
769 |
#if defined(Q_OS_SYMBIAN) |
|
770 |
server.setStackSize(0x14000); |
|
771 |
#endif |
|
772 |
server.clients = threads; |
|
773 |
server.start(); |
|
774 |
||
775 |
QList<Client*> clients; |
|
776 |
for (int i = 0; i < threads; ++i) { |
|
777 |
clients.append(new Client()); |
|
778 |
#if defined(Q_OS_SYMBIAN) |
|
779 |
clients.last()->setStackSize(0x14000); |
|
780 |
#endif |
|
781 |
clients.last()->start(); |
|
782 |
} |
|
783 |
||
784 |
server.wait(); |
|
785 |
while (!clients.isEmpty()) { |
|
786 |
QVERIFY(clients.first()->wait(3000)); |
|
787 |
Client *client =clients.takeFirst(); |
|
788 |
client->terminate(); |
|
789 |
delete client; |
|
790 |
} |
|
791 |
} |
|
792 |
||
793 |
void tst_QLocalSocket::processConnection_data() |
|
794 |
{ |
|
795 |
QTest::addColumn<int>("processes"); |
|
796 |
QTest::newRow("1 client") << 1; |
|
797 |
#ifndef Q_OS_WIN |
|
798 |
QTest::newRow("2 clients") << 2; |
|
799 |
QTest::newRow("5 clients") << 5; |
|
800 |
#endif |
|
801 |
QTest::newRow("30 clients") << 30; |
|
802 |
} |
|
803 |
||
804 |
/*! |
|
805 |
Create external processes that produce and consume. |
|
806 |
*/ |
|
807 |
void tst_QLocalSocket::processConnection() |
|
808 |
{ |
|
809 |
#if defined(QT_NO_PROCESS) || defined(Q_CC_NOKIAX86) |
|
810 |
QSKIP("Qt was compiled with QT_NO_PROCESS", SkipAll); |
|
811 |
#else |
|
812 |
QFETCH(int, processes); |
|
813 |
QStringList serverArguments = QStringList() << SRCDIR "lackey/scripts/server.js" << QString::number(processes); |
|
814 |
QProcess producer; |
|
815 |
producer.setProcessChannelMode(QProcess::ForwardedChannels); |
|
816 |
#ifdef Q_WS_QWS |
|
817 |
serverArguments << "-qws"; |
|
818 |
#endif |
|
819 |
QList<QProcess*> consumers; |
|
820 |
producer.start("lackey/lackey", serverArguments); |
|
821 |
QVERIFY(producer.waitForStarted(-1)); |
|
822 |
QTest::qWait(2000); |
|
823 |
for (int i = 0; i < processes; ++i) { |
|
824 |
QStringList arguments = QStringList() << SRCDIR "lackey/scripts/client.js"; |
|
825 |
#ifdef Q_WS_QWS |
|
826 |
arguments << "-qws"; |
|
827 |
#endif |
|
828 |
QProcess *p = new QProcess; |
|
829 |
p->setProcessChannelMode(QProcess::ForwardedChannels); |
|
830 |
consumers.append(p); |
|
831 |
p->start("lackey/lackey", arguments); |
|
832 |
} |
|
833 |
||
834 |
while (!consumers.isEmpty()) { |
|
835 |
consumers.first()->waitForFinished(20000); |
|
836 |
QCOMPARE(consumers.first()->exitStatus(), QProcess::NormalExit); |
|
837 |
QCOMPARE(consumers.first()->exitCode(), 0); |
|
838 |
QProcess *consumer = consumers.takeFirst(); |
|
839 |
consumer->terminate(); |
|
840 |
delete consumer; |
|
841 |
} |
|
842 |
producer.waitForFinished(15000); |
|
843 |
#endif |
|
844 |
} |
|
845 |
||
846 |
void tst_QLocalSocket::longPath() |
|
847 |
{ |
|
848 |
#ifndef Q_OS_WIN |
|
849 |
QString name; |
|
850 |
for (int i = 0; i < 256; ++i) |
|
851 |
name += 'a'; |
|
852 |
LocalServer server; |
|
853 |
QVERIFY(!server.listen(name)); |
|
854 |
||
855 |
LocalSocket socket; |
|
856 |
socket.connectToServer(name); |
|
857 |
QCOMPARE(socket.state(), QLocalSocket::UnconnectedState); |
|
858 |
#endif |
|
859 |
} |
|
860 |
||
861 |
void tst_QLocalSocket::waitForDisconnect() |
|
862 |
{ |
|
863 |
QString name = "tst_localsocket"; |
|
864 |
#ifdef Q_OS_SYMBIAN |
|
865 |
unlink(name); |
|
866 |
#endif |
|
867 |
LocalServer server; |
|
868 |
QVERIFY(server.listen(name)); |
|
869 |
LocalSocket socket; |
|
870 |
socket.connectToServer(name); |
|
871 |
QVERIFY(socket.waitForConnected(3000)); |
|
872 |
QVERIFY(server.waitForNewConnection(3000)); |
|
873 |
QLocalSocket *serverSocket = server.nextPendingConnection(); |
|
874 |
QVERIFY(serverSocket); |
|
875 |
socket.disconnectFromServer(); |
|
876 |
QTime timer; |
|
877 |
timer.start(); |
|
878 |
QVERIFY(serverSocket->waitForDisconnected(3000)); |
|
879 |
QVERIFY(timer.elapsed() < 2000); |
|
880 |
} |
|
881 |
||
882 |
void tst_QLocalSocket::waitForDisconnectByServer() |
|
883 |
{ |
|
884 |
QString name = "tst_localsocket"; |
|
885 |
LocalServer server; |
|
886 |
QVERIFY(server.listen(name)); |
|
887 |
LocalSocket socket; |
|
888 |
QSignalSpy spy(&socket, SIGNAL(disconnected())); |
|
889 |
QVERIFY(spy.isValid()); |
|
890 |
socket.connectToServer(name); |
|
891 |
QVERIFY(socket.waitForConnected(3000)); |
|
892 |
QVERIFY(server.waitForNewConnection(3000)); |
|
893 |
QLocalSocket *serverSocket = server.nextPendingConnection(); |
|
894 |
QVERIFY(serverSocket); |
|
895 |
serverSocket->close(); |
|
896 |
QVERIFY(serverSocket->state() == QLocalSocket::UnconnectedState); |
|
897 |
QVERIFY(socket.waitForDisconnected(3000)); |
|
898 |
QCOMPARE(spy.count(), 1); |
|
899 |
} |
|
900 |
||
901 |
void tst_QLocalSocket::removeServer() |
|
902 |
{ |
|
903 |
// this is a hostile takeover, but recovering from a crash results in the same |
|
904 |
QLocalServer server, server2; |
|
905 |
QVERIFY(QLocalServer::removeServer("cleanuptest")); |
|
906 |
QVERIFY(server.listen("cleanuptest")); |
|
907 |
#ifndef Q_OS_WIN |
|
908 |
// on Windows, there can be several sockets listening on the same pipe |
|
909 |
// on Unix, there can only be one socket instance |
|
910 |
QVERIFY(! server2.listen("cleanuptest")); |
|
911 |
#endif |
|
912 |
QVERIFY(QLocalServer::removeServer("cleanuptest")); |
|
913 |
QVERIFY(server2.listen("cleanuptest")); |
|
914 |
} |
|
915 |
||
916 |
void tst_QLocalSocket::recycleServer() |
|
917 |
{ |
|
918 |
#ifdef Q_OS_SYMBIAN |
|
919 |
unlink("recycletest1"); |
|
920 |
#endif |
|
921 |
||
922 |
QLocalServer server; |
|
923 |
QLocalSocket client; |
|
924 |
||
925 |
QVERIFY(server.listen("recycletest1")); |
|
926 |
client.connectToServer("recycletest1"); |
|
927 |
QVERIFY(client.waitForConnected(201)); |
|
928 |
QVERIFY(server.waitForNewConnection(201)); |
|
929 |
QVERIFY(server.nextPendingConnection() != 0); |
|
930 |
||
931 |
server.close(); |
|
932 |
client.disconnectFromServer(); |
|
933 |
qApp->processEvents(); |
|
934 |
||
935 |
QVERIFY(server.listen("recycletest2")); |
|
936 |
client.connectToServer("recycletest2"); |
|
937 |
QVERIFY(client.waitForConnected(202)); |
|
938 |
QVERIFY(server.waitForNewConnection(202)); |
|
939 |
QVERIFY(server.nextPendingConnection() != 0); |
|
940 |
} |
|
941 |
||
942 |
void tst_QLocalSocket::multiConnect() |
|
943 |
{ |
|
944 |
QLocalServer server; |
|
945 |
QLocalSocket client1; |
|
946 |
QLocalSocket client2; |
|
947 |
QLocalSocket client3; |
|
948 |
||
949 |
QVERIFY(server.listen("multiconnect")); |
|
950 |
||
951 |
client1.connectToServer("multiconnect"); |
|
952 |
client2.connectToServer("multiconnect"); |
|
953 |
client3.connectToServer("multiconnect"); |
|
954 |
||
955 |
QVERIFY(client1.waitForConnected(201)); |
|
956 |
QVERIFY(client2.waitForConnected(202)); |
|
957 |
QVERIFY(client3.waitForConnected(203)); |
|
958 |
||
959 |
QVERIFY(server.waitForNewConnection(201)); |
|
960 |
QVERIFY(server.nextPendingConnection() != 0); |
|
961 |
QVERIFY(server.waitForNewConnection(202)); |
|
962 |
QVERIFY(server.nextPendingConnection() != 0); |
|
963 |
QVERIFY(server.waitForNewConnection(203)); |
|
964 |
QVERIFY(server.nextPendingConnection() != 0); |
|
965 |
} |
|
966 |
||
967 |
void tst_QLocalSocket::writeOnlySocket() |
|
968 |
{ |
|
969 |
QLocalServer server; |
|
970 |
#ifdef Q_OS_SYMBIAN |
|
971 |
unlink("writeOnlySocket"); |
|
972 |
#endif |
|
973 |
QVERIFY(server.listen("writeOnlySocket")); |
|
974 |
||
975 |
QLocalSocket client; |
|
976 |
client.connectToServer("writeOnlySocket", QIODevice::WriteOnly); |
|
977 |
QVERIFY(client.waitForConnected()); |
|
978 |
#if defined(Q_OS_SYMBIAN) |
|
979 |
QTest::qWait(250); |
|
980 |
#endif |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
981 |
QVERIFY(server.waitForNewConnection(200)); |
0 | 982 |
QLocalSocket* serverSocket = server.nextPendingConnection(); |
983 |
QVERIFY(serverSocket); |
|
984 |
||
985 |
QCOMPARE(client.bytesAvailable(), qint64(0)); |
|
986 |
QCOMPARE(client.state(), QLocalSocket::ConnectedState); |
|
987 |
} |
|
988 |
||
989 |
void tst_QLocalSocket::writeToClientAndDisconnect() |
|
990 |
{ |
|
991 |
#ifdef Q_OS_SYMBIAN |
|
992 |
unlink("writeAndDisconnectServer"); |
|
993 |
#endif |
|
994 |
||
995 |
QLocalServer server; |
|
996 |
QLocalSocket client; |
|
997 |
||
998 |
QVERIFY(server.listen("writeAndDisconnectServer")); |
|
999 |
client.connectToServer("writeAndDisconnectServer"); |
|
1000 |
QVERIFY(client.waitForConnected(200)); |
|
1001 |
QVERIFY(server.waitForNewConnection(200)); |
|
1002 |
QLocalSocket* clientSocket = server.nextPendingConnection(); |
|
1003 |
QVERIFY(clientSocket); |
|
1004 |
||
1005 |
char buffer[100]; |
|
1006 |
memset(buffer, 0, sizeof(buffer)); |
|
1007 |
QCOMPARE(clientSocket->write(buffer, sizeof(buffer)), (qint64)sizeof(buffer)); |
|
1008 |
clientSocket->waitForBytesWritten(); |
|
1009 |
clientSocket->disconnectFromServer(); |
|
1010 |
QVERIFY(client.waitForReadyRead()); |
|
1011 |
QCOMPARE(client.read(buffer, sizeof(buffer)), (qint64)sizeof(buffer)); |
|
1012 |
QVERIFY(client.waitForDisconnected()); |
|
1013 |
QCOMPARE(client.state(), QLocalSocket::UnconnectedState); |
|
1014 |
} |
|
1015 |
||
1016 |
void tst_QLocalSocket::debug() |
|
1017 |
{ |
|
1018 |
// Make sure this compiles |
|
1019 |
qDebug() << QLocalSocket::ConnectionRefusedError << QLocalSocket::UnconnectedState; |
|
1020 |
} |
|
1021 |
||
1022 |
class WriteThread : public QThread |
|
1023 |
{ |
|
1024 |
Q_OBJECT |
|
1025 |
public: |
|
1026 |
void run() { |
|
1027 |
QLocalSocket socket; |
|
1028 |
socket.connectToServer("qlocalsocket_readyread"); |
|
1029 |
||
1030 |
if (!socket.waitForConnected(3000)) |
|
1031 |
exec(); |
|
1032 |
connect(&socket, SIGNAL(bytesWritten(qint64)), |
|
1033 |
this, SLOT(bytesWritten(qint64)), Qt::QueuedConnection); |
|
1034 |
socket.write("testing\n"); |
|
1035 |
exec(); |
|
1036 |
} |
|
1037 |
public slots: |
|
1038 |
void bytesWritten(qint64) { |
|
1039 |
exit(); |
|
1040 |
} |
|
1041 |
||
1042 |
private: |
|
1043 |
}; |
|
1044 |
||
1045 |
/* |
|
1046 |
Tests the emission of the bytesWritten(qint64) |
|
1047 |
signal. |
|
1048 |
||
1049 |
Create a thread that will write to a socket. |
|
1050 |
If the bytesWritten(qint64) signal is generated, |
|
1051 |
the slot connected to it will exit the thread, |
|
1052 |
indicating test success. |
|
1053 |
||
1054 |
*/ |
|
1055 |
void tst_QLocalSocket::bytesWrittenSignal() |
|
1056 |
{ |
|
1057 |
QLocalServer server; |
|
1058 |
QVERIFY(server.listen("qlocalsocket_readyread")); |
|
1059 |
WriteThread writeThread; |
|
1060 |
writeThread.start(); |
|
1061 |
bool timedOut = false; |
|
1062 |
QVERIFY(server.waitForNewConnection(3000, &timedOut)); |
|
1063 |
QVERIFY(!timedOut); |
|
1064 |
QTest::qWait(2000); |
|
1065 |
QVERIFY(writeThread.wait(2000)); |
|
1066 |
} |
|
1067 |
||
1068 |
#ifdef Q_OS_SYMBIAN |
|
1069 |
void tst_QLocalSocket::unlink(QString name) |
|
1070 |
{ |
|
1071 |
if(name.length() == 0) |
|
1072 |
return; |
|
1073 |
||
1074 |
QString fullName; |
|
1075 |
// determine the full server path |
|
1076 |
if (name.startsWith(QLatin1Char('/'))) { |
|
1077 |
fullName = name; |
|
1078 |
} else { |
|
1079 |
fullName = QDir::cleanPath(QDir::tempPath()); |
|
1080 |
fullName += QLatin1Char('/') + name; |
|
1081 |
fullName = QDir::toNativeSeparators(fullName); |
|
1082 |
} |
|
1083 |
||
1084 |
int result = ::unlink(fullName.toUtf8().data()); |
|
1085 |
||
1086 |
if(result != 0) { |
|
1087 |
qWarning() << "Unlinking " << fullName << " failed with " << strerror(errno); |
|
1088 |
} |
|
1089 |
} |
|
1090 |
#endif |
|
1091 |
QTEST_MAIN(tst_QLocalSocket) |
|
1092 |
#include "tst_qlocalsocket.moc" |
|
1093 |