0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
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);
|
|
590 |
serverSocket->flush();
|
|
591 |
|
|
592 |
QVERIFY(client.waitForReadyRead());
|
|
593 |
QCOMPARE(client.read(buffer, readBufferSize), qint64(readBufferSize));
|
|
594 |
#if defined(QT_LOCALSOCKET_TCP) || defined(Q_OS_SYMBIAN)
|
|
595 |
QTest::qWait(250);
|
|
596 |
#endif
|
|
597 |
QCOMPARE(client.read(buffer, readBufferSize), qint64(readBufferSize));
|
|
598 |
}
|
|
599 |
|
|
600 |
// QLocalSocket/Server can take a name or path, check that it works as expected
|
|
601 |
void tst_QLocalSocket::fullPath()
|
|
602 |
{
|
|
603 |
QLocalServer server;
|
|
604 |
QString name = "qlocalsocket_pathtest";
|
|
605 |
#if defined(Q_OS_SYMBIAN)
|
|
606 |
QString path = "";
|
|
607 |
#elif defined(QT_LOCALSOCKET_TCP)
|
|
608 |
QString path = "QLocalServer";
|
|
609 |
#elif defined(Q_OS_WIN)
|
|
610 |
QString path = "\\\\.\\pipe\\";
|
|
611 |
#else
|
|
612 |
QString path = "/tmp";
|
|
613 |
#endif
|
|
614 |
QString serverName = path + '/' + name;
|
|
615 |
QVERIFY2(server.listen(serverName), server.errorString().toLatin1().constData());
|
|
616 |
QCOMPARE(server.serverName(), serverName);
|
|
617 |
QCOMPARE(server.fullServerName(), serverName);
|
|
618 |
|
|
619 |
LocalSocket socket;
|
|
620 |
socket.connectToServer(serverName);
|
|
621 |
|
|
622 |
QCOMPARE(socket.serverName(), serverName);
|
|
623 |
QCOMPARE(socket.fullServerName(), serverName);
|
|
624 |
socket.disconnectFromServer();
|
|
625 |
#ifdef QT_LOCALSOCKET_TCP
|
|
626 |
QTest::qWait(250);
|
|
627 |
#endif
|
|
628 |
QCOMPARE(socket.serverName(), QString());
|
|
629 |
QCOMPARE(socket.fullServerName(), QString());
|
|
630 |
}
|
|
631 |
|
|
632 |
void tst_QLocalSocket::hitMaximumConnections_data()
|
|
633 |
{
|
|
634 |
QTest::addColumn<int>("max");
|
|
635 |
QTest::newRow("none") << 0;
|
|
636 |
QTest::newRow("1") << 1;
|
|
637 |
QTest::newRow("3") << 3;
|
|
638 |
}
|
|
639 |
|
|
640 |
void tst_QLocalSocket::hitMaximumConnections()
|
|
641 |
{
|
|
642 |
QFETCH(int, max);
|
|
643 |
LocalServer server;
|
|
644 |
QString name = "tst_localsocket";
|
|
645 |
#ifdef Q_OS_SYMBIAN
|
|
646 |
unlink(name);
|
|
647 |
#endif
|
|
648 |
server.setMaxPendingConnections(max);
|
|
649 |
QVERIFY2(server.listen(name), server.errorString().toLatin1().constData());
|
|
650 |
int connections = server.maxPendingConnections() + 1;
|
|
651 |
QList<QLocalSocket*> sockets;
|
|
652 |
for (int i = 0; i < connections; ++i) {
|
|
653 |
LocalSocket *socket = new LocalSocket;
|
|
654 |
sockets.append(socket);
|
|
655 |
socket->connectToServer(name);
|
|
656 |
}
|
|
657 |
bool timedOut = true;
|
|
658 |
QVERIFY(server.waitForNewConnection(3000, &timedOut));
|
|
659 |
QVERIFY(!timedOut);
|
|
660 |
QVERIFY(server.hits.count() > 0);
|
|
661 |
qDeleteAll(sockets.begin(), sockets.end());
|
|
662 |
}
|
|
663 |
|
|
664 |
// check that state and mode are kept
|
|
665 |
void tst_QLocalSocket::setSocketDescriptor()
|
|
666 |
{
|
|
667 |
LocalSocket socket;
|
|
668 |
quintptr minusOne = -1;
|
|
669 |
socket.setSocketDescriptor(minusOne, QLocalSocket::ConnectingState, QIODevice::Append);
|
|
670 |
QCOMPARE(socket.socketDescriptor(), minusOne);
|
|
671 |
QCOMPARE(socket.state(), QLocalSocket::ConnectingState);
|
|
672 |
QVERIFY((socket.openMode() & QIODevice::Append) != 0);
|
|
673 |
}
|
|
674 |
|
|
675 |
class Client : public QThread
|
|
676 |
{
|
|
677 |
|
|
678 |
public:
|
|
679 |
void run()
|
|
680 |
{
|
|
681 |
QString testLine = "test";
|
|
682 |
LocalSocket socket;
|
|
683 |
QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead()));
|
|
684 |
int tries = 0;
|
|
685 |
do {
|
|
686 |
socket.connectToServer("qlocalsocket_threadtest");
|
|
687 |
if (socket.error() != QLocalSocket::ServerNotFoundError
|
|
688 |
&& socket.error() != QLocalSocket::ConnectionRefusedError)
|
|
689 |
break;
|
|
690 |
QTest::qWait(100);
|
|
691 |
++tries;
|
|
692 |
} while ((socket.error() == QLocalSocket::ServerNotFoundError
|
|
693 |
|| socket.error() == QLocalSocket::ConnectionRefusedError)
|
|
694 |
&& tries < 1000);
|
|
695 |
if (tries == 0 && socket.state() != QLocalSocket::ConnectedState) {
|
|
696 |
QVERIFY(socket.waitForConnected(3000));
|
|
697 |
QVERIFY(socket.state() == QLocalSocket::ConnectedState);
|
|
698 |
}
|
|
699 |
|
|
700 |
// We should *not* have this signal yet!
|
|
701 |
if (tries == 0)
|
|
702 |
QCOMPARE(spyReadyRead.count(), 0);
|
|
703 |
socket.waitForReadyRead();
|
|
704 |
QCOMPARE(spyReadyRead.count(), 1);
|
|
705 |
QTextStream in(&socket);
|
|
706 |
QCOMPARE(in.readLine(), testLine);
|
|
707 |
socket.close();
|
|
708 |
}
|
|
709 |
};
|
|
710 |
|
|
711 |
class Server : public QThread
|
|
712 |
{
|
|
713 |
|
|
714 |
public:
|
|
715 |
int clients;
|
|
716 |
void run()
|
|
717 |
{
|
|
718 |
QString testLine = "test";
|
|
719 |
LocalServer server;
|
|
720 |
server.setMaxPendingConnections(10);
|
|
721 |
QVERIFY2(server.listen("qlocalsocket_threadtest"),
|
|
722 |
server.errorString().toLatin1().constData());
|
|
723 |
int done = clients;
|
|
724 |
while (done > 0) {
|
|
725 |
bool timedOut = true;
|
|
726 |
QVERIFY(server.waitForNewConnection(3000, &timedOut));
|
|
727 |
QVERIFY(!timedOut);
|
|
728 |
QLocalSocket *serverSocket = server.nextPendingConnection();
|
|
729 |
QVERIFY(serverSocket);
|
|
730 |
QTextStream out(serverSocket);
|
|
731 |
out << testLine << endl;
|
|
732 |
QCOMPARE(serverSocket->state(), QLocalSocket::ConnectedState);
|
|
733 |
QVERIFY2(serverSocket->waitForBytesWritten(), serverSocket->errorString().toLatin1().constData());
|
|
734 |
QCOMPARE(serverSocket->errorString(), QString("Unknown error"));
|
|
735 |
--done;
|
|
736 |
delete serverSocket;
|
|
737 |
}
|
|
738 |
QCOMPARE(server.hits.count(), clients);
|
|
739 |
}
|
|
740 |
};
|
|
741 |
|
|
742 |
void tst_QLocalSocket::threadedConnection_data()
|
|
743 |
{
|
|
744 |
QTest::addColumn<int>("threads");
|
|
745 |
QTest::newRow("1 client") << 1;
|
|
746 |
QTest::newRow("2 clients") << 2;
|
|
747 |
#ifdef Q_OS_WINCE
|
|
748 |
QTest::newRow("4 clients") << 4;
|
|
749 |
#endif
|
|
750 |
#ifndef Q_OS_WIN
|
|
751 |
QTest::newRow("5 clients") << 5;
|
|
752 |
QTest::newRow("10 clients") << 10;
|
|
753 |
#endif
|
|
754 |
#ifndef Q_OS_WINCE
|
|
755 |
QTest::newRow("20 clients") << 20;
|
|
756 |
#endif
|
|
757 |
}
|
|
758 |
|
|
759 |
void tst_QLocalSocket::threadedConnection()
|
|
760 |
{
|
|
761 |
#ifdef Q_OS_SYMBIAN
|
|
762 |
unlink("qlocalsocket_threadtest");
|
|
763 |
#endif
|
|
764 |
|
|
765 |
QFETCH(int, threads);
|
|
766 |
Server server;
|
|
767 |
#if defined(Q_OS_SYMBIAN)
|
|
768 |
server.setStackSize(0x14000);
|
|
769 |
#endif
|
|
770 |
server.clients = threads;
|
|
771 |
server.start();
|
|
772 |
|
|
773 |
QList<Client*> clients;
|
|
774 |
for (int i = 0; i < threads; ++i) {
|
|
775 |
clients.append(new Client());
|
|
776 |
#if defined(Q_OS_SYMBIAN)
|
|
777 |
clients.last()->setStackSize(0x14000);
|
|
778 |
#endif
|
|
779 |
clients.last()->start();
|
|
780 |
}
|
|
781 |
|
|
782 |
server.wait();
|
|
783 |
while (!clients.isEmpty()) {
|
|
784 |
QVERIFY(clients.first()->wait(3000));
|
|
785 |
Client *client =clients.takeFirst();
|
|
786 |
client->terminate();
|
|
787 |
delete client;
|
|
788 |
}
|
|
789 |
}
|
|
790 |
|
|
791 |
void tst_QLocalSocket::processConnection_data()
|
|
792 |
{
|
|
793 |
QTest::addColumn<int>("processes");
|
|
794 |
QTest::newRow("1 client") << 1;
|
|
795 |
#ifndef Q_OS_WIN
|
|
796 |
QTest::newRow("2 clients") << 2;
|
|
797 |
QTest::newRow("5 clients") << 5;
|
|
798 |
#endif
|
|
799 |
QTest::newRow("30 clients") << 30;
|
|
800 |
}
|
|
801 |
|
|
802 |
/*!
|
|
803 |
Create external processes that produce and consume.
|
|
804 |
*/
|
|
805 |
void tst_QLocalSocket::processConnection()
|
|
806 |
{
|
|
807 |
#if defined(QT_NO_PROCESS) || defined(Q_CC_NOKIAX86)
|
|
808 |
QSKIP("Qt was compiled with QT_NO_PROCESS", SkipAll);
|
|
809 |
#else
|
|
810 |
QFETCH(int, processes);
|
|
811 |
QStringList serverArguments = QStringList() << SRCDIR "lackey/scripts/server.js" << QString::number(processes);
|
|
812 |
QProcess producer;
|
|
813 |
producer.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
814 |
#ifdef Q_WS_QWS
|
|
815 |
serverArguments << "-qws";
|
|
816 |
#endif
|
|
817 |
QList<QProcess*> consumers;
|
|
818 |
producer.start("lackey/lackey", serverArguments);
|
|
819 |
QVERIFY(producer.waitForStarted(-1));
|
|
820 |
QTest::qWait(2000);
|
|
821 |
for (int i = 0; i < processes; ++i) {
|
|
822 |
QStringList arguments = QStringList() << SRCDIR "lackey/scripts/client.js";
|
|
823 |
#ifdef Q_WS_QWS
|
|
824 |
arguments << "-qws";
|
|
825 |
#endif
|
|
826 |
QProcess *p = new QProcess;
|
|
827 |
p->setProcessChannelMode(QProcess::ForwardedChannels);
|
|
828 |
consumers.append(p);
|
|
829 |
p->start("lackey/lackey", arguments);
|
|
830 |
}
|
|
831 |
|
|
832 |
while (!consumers.isEmpty()) {
|
|
833 |
consumers.first()->waitForFinished(20000);
|
|
834 |
QCOMPARE(consumers.first()->exitStatus(), QProcess::NormalExit);
|
|
835 |
QCOMPARE(consumers.first()->exitCode(), 0);
|
|
836 |
QProcess *consumer = consumers.takeFirst();
|
|
837 |
consumer->terminate();
|
|
838 |
delete consumer;
|
|
839 |
}
|
|
840 |
producer.waitForFinished(15000);
|
|
841 |
#endif
|
|
842 |
}
|
|
843 |
|
|
844 |
void tst_QLocalSocket::longPath()
|
|
845 |
{
|
|
846 |
#ifndef Q_OS_WIN
|
|
847 |
QString name;
|
|
848 |
for (int i = 0; i < 256; ++i)
|
|
849 |
name += 'a';
|
|
850 |
LocalServer server;
|
|
851 |
QVERIFY(!server.listen(name));
|
|
852 |
|
|
853 |
LocalSocket socket;
|
|
854 |
socket.connectToServer(name);
|
|
855 |
QCOMPARE(socket.state(), QLocalSocket::UnconnectedState);
|
|
856 |
#endif
|
|
857 |
}
|
|
858 |
|
|
859 |
void tst_QLocalSocket::waitForDisconnect()
|
|
860 |
{
|
|
861 |
QString name = "tst_localsocket";
|
|
862 |
#ifdef Q_OS_SYMBIAN
|
|
863 |
unlink(name);
|
|
864 |
#endif
|
|
865 |
LocalServer server;
|
|
866 |
QVERIFY(server.listen(name));
|
|
867 |
LocalSocket socket;
|
|
868 |
socket.connectToServer(name);
|
|
869 |
QVERIFY(socket.waitForConnected(3000));
|
|
870 |
QVERIFY(server.waitForNewConnection(3000));
|
|
871 |
QLocalSocket *serverSocket = server.nextPendingConnection();
|
|
872 |
QVERIFY(serverSocket);
|
|
873 |
socket.disconnectFromServer();
|
|
874 |
QTime timer;
|
|
875 |
timer.start();
|
|
876 |
QVERIFY(serverSocket->waitForDisconnected(3000));
|
|
877 |
QVERIFY(timer.elapsed() < 2000);
|
|
878 |
}
|
|
879 |
|
|
880 |
void tst_QLocalSocket::waitForDisconnectByServer()
|
|
881 |
{
|
|
882 |
QString name = "tst_localsocket";
|
|
883 |
LocalServer server;
|
|
884 |
QVERIFY(server.listen(name));
|
|
885 |
LocalSocket socket;
|
|
886 |
QSignalSpy spy(&socket, SIGNAL(disconnected()));
|
|
887 |
QVERIFY(spy.isValid());
|
|
888 |
socket.connectToServer(name);
|
|
889 |
QVERIFY(socket.waitForConnected(3000));
|
|
890 |
QVERIFY(server.waitForNewConnection(3000));
|
|
891 |
QLocalSocket *serverSocket = server.nextPendingConnection();
|
|
892 |
QVERIFY(serverSocket);
|
|
893 |
serverSocket->close();
|
|
894 |
QVERIFY(serverSocket->state() == QLocalSocket::UnconnectedState);
|
|
895 |
QVERIFY(socket.waitForDisconnected(3000));
|
|
896 |
QCOMPARE(spy.count(), 1);
|
|
897 |
}
|
|
898 |
|
|
899 |
void tst_QLocalSocket::removeServer()
|
|
900 |
{
|
|
901 |
// this is a hostile takeover, but recovering from a crash results in the same
|
|
902 |
QLocalServer server, server2;
|
|
903 |
QVERIFY(QLocalServer::removeServer("cleanuptest"));
|
|
904 |
QVERIFY(server.listen("cleanuptest"));
|
|
905 |
#ifndef Q_OS_WIN
|
|
906 |
// on Windows, there can be several sockets listening on the same pipe
|
|
907 |
// on Unix, there can only be one socket instance
|
|
908 |
QVERIFY(! server2.listen("cleanuptest"));
|
|
909 |
#endif
|
|
910 |
QVERIFY(QLocalServer::removeServer("cleanuptest"));
|
|
911 |
QVERIFY(server2.listen("cleanuptest"));
|
|
912 |
}
|
|
913 |
|
|
914 |
void tst_QLocalSocket::recycleServer()
|
|
915 |
{
|
|
916 |
#ifdef Q_OS_SYMBIAN
|
|
917 |
unlink("recycletest1");
|
|
918 |
#endif
|
|
919 |
|
|
920 |
QLocalServer server;
|
|
921 |
QLocalSocket client;
|
|
922 |
|
|
923 |
QVERIFY(server.listen("recycletest1"));
|
|
924 |
client.connectToServer("recycletest1");
|
|
925 |
QVERIFY(client.waitForConnected(201));
|
|
926 |
QVERIFY(server.waitForNewConnection(201));
|
|
927 |
QVERIFY(server.nextPendingConnection() != 0);
|
|
928 |
|
|
929 |
server.close();
|
|
930 |
client.disconnectFromServer();
|
|
931 |
qApp->processEvents();
|
|
932 |
|
|
933 |
QVERIFY(server.listen("recycletest2"));
|
|
934 |
client.connectToServer("recycletest2");
|
|
935 |
QVERIFY(client.waitForConnected(202));
|
|
936 |
QVERIFY(server.waitForNewConnection(202));
|
|
937 |
QVERIFY(server.nextPendingConnection() != 0);
|
|
938 |
}
|
|
939 |
|
|
940 |
void tst_QLocalSocket::multiConnect()
|
|
941 |
{
|
|
942 |
QLocalServer server;
|
|
943 |
QLocalSocket client1;
|
|
944 |
QLocalSocket client2;
|
|
945 |
QLocalSocket client3;
|
|
946 |
|
|
947 |
QVERIFY(server.listen("multiconnect"));
|
|
948 |
|
|
949 |
client1.connectToServer("multiconnect");
|
|
950 |
client2.connectToServer("multiconnect");
|
|
951 |
client3.connectToServer("multiconnect");
|
|
952 |
|
|
953 |
QVERIFY(client1.waitForConnected(201));
|
|
954 |
QVERIFY(client2.waitForConnected(202));
|
|
955 |
QVERIFY(client3.waitForConnected(203));
|
|
956 |
|
|
957 |
QVERIFY(server.waitForNewConnection(201));
|
|
958 |
QVERIFY(server.nextPendingConnection() != 0);
|
|
959 |
QVERIFY(server.waitForNewConnection(202));
|
|
960 |
QVERIFY(server.nextPendingConnection() != 0);
|
|
961 |
QVERIFY(server.waitForNewConnection(203));
|
|
962 |
QVERIFY(server.nextPendingConnection() != 0);
|
|
963 |
}
|
|
964 |
|
|
965 |
void tst_QLocalSocket::writeOnlySocket()
|
|
966 |
{
|
|
967 |
QLocalServer server;
|
|
968 |
#ifdef Q_OS_SYMBIAN
|
|
969 |
unlink("writeOnlySocket");
|
|
970 |
#endif
|
|
971 |
QVERIFY(server.listen("writeOnlySocket"));
|
|
972 |
|
|
973 |
QLocalSocket client;
|
|
974 |
client.connectToServer("writeOnlySocket", QIODevice::WriteOnly);
|
|
975 |
QVERIFY(client.waitForConnected());
|
|
976 |
#if defined(Q_OS_SYMBIAN)
|
|
977 |
QTest::qWait(250);
|
|
978 |
#endif
|
|
979 |
QVERIFY(server.waitForNewConnection());
|
|
980 |
QLocalSocket* serverSocket = server.nextPendingConnection();
|
|
981 |
QVERIFY(serverSocket);
|
|
982 |
|
|
983 |
QCOMPARE(client.bytesAvailable(), qint64(0));
|
|
984 |
QCOMPARE(client.state(), QLocalSocket::ConnectedState);
|
|
985 |
}
|
|
986 |
|
|
987 |
void tst_QLocalSocket::writeToClientAndDisconnect()
|
|
988 |
{
|
|
989 |
#ifdef Q_OS_SYMBIAN
|
|
990 |
unlink("writeAndDisconnectServer");
|
|
991 |
#endif
|
|
992 |
|
|
993 |
QLocalServer server;
|
|
994 |
QLocalSocket client;
|
|
995 |
|
|
996 |
QVERIFY(server.listen("writeAndDisconnectServer"));
|
|
997 |
client.connectToServer("writeAndDisconnectServer");
|
|
998 |
QVERIFY(client.waitForConnected(200));
|
|
999 |
QVERIFY(server.waitForNewConnection(200));
|
|
1000 |
QLocalSocket* clientSocket = server.nextPendingConnection();
|
|
1001 |
QVERIFY(clientSocket);
|
|
1002 |
|
|
1003 |
char buffer[100];
|
|
1004 |
memset(buffer, 0, sizeof(buffer));
|
|
1005 |
QCOMPARE(clientSocket->write(buffer, sizeof(buffer)), (qint64)sizeof(buffer));
|
|
1006 |
clientSocket->waitForBytesWritten();
|
|
1007 |
clientSocket->disconnectFromServer();
|
|
1008 |
QVERIFY(client.waitForReadyRead());
|
|
1009 |
QCOMPARE(client.read(buffer, sizeof(buffer)), (qint64)sizeof(buffer));
|
|
1010 |
QVERIFY(client.waitForDisconnected());
|
|
1011 |
QCOMPARE(client.state(), QLocalSocket::UnconnectedState);
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
void tst_QLocalSocket::debug()
|
|
1015 |
{
|
|
1016 |
// Make sure this compiles
|
|
1017 |
qDebug() << QLocalSocket::ConnectionRefusedError << QLocalSocket::UnconnectedState;
|
|
1018 |
}
|
|
1019 |
|
|
1020 |
class WriteThread : public QThread
|
|
1021 |
{
|
|
1022 |
Q_OBJECT
|
|
1023 |
public:
|
|
1024 |
void run() {
|
|
1025 |
QLocalSocket socket;
|
|
1026 |
socket.connectToServer("qlocalsocket_readyread");
|
|
1027 |
|
|
1028 |
if (!socket.waitForConnected(3000))
|
|
1029 |
exec();
|
|
1030 |
connect(&socket, SIGNAL(bytesWritten(qint64)),
|
|
1031 |
this, SLOT(bytesWritten(qint64)), Qt::QueuedConnection);
|
|
1032 |
socket.write("testing\n");
|
|
1033 |
exec();
|
|
1034 |
}
|
|
1035 |
public slots:
|
|
1036 |
void bytesWritten(qint64) {
|
|
1037 |
exit();
|
|
1038 |
}
|
|
1039 |
|
|
1040 |
private:
|
|
1041 |
};
|
|
1042 |
|
|
1043 |
/*
|
|
1044 |
Tests the emission of the bytesWritten(qint64)
|
|
1045 |
signal.
|
|
1046 |
|
|
1047 |
Create a thread that will write to a socket.
|
|
1048 |
If the bytesWritten(qint64) signal is generated,
|
|
1049 |
the slot connected to it will exit the thread,
|
|
1050 |
indicating test success.
|
|
1051 |
|
|
1052 |
*/
|
|
1053 |
void tst_QLocalSocket::bytesWrittenSignal()
|
|
1054 |
{
|
|
1055 |
QLocalServer server;
|
|
1056 |
QVERIFY(server.listen("qlocalsocket_readyread"));
|
|
1057 |
WriteThread writeThread;
|
|
1058 |
writeThread.start();
|
|
1059 |
bool timedOut = false;
|
|
1060 |
QVERIFY(server.waitForNewConnection(3000, &timedOut));
|
|
1061 |
QVERIFY(!timedOut);
|
|
1062 |
QTest::qWait(2000);
|
|
1063 |
QVERIFY(writeThread.wait(2000));
|
|
1064 |
}
|
|
1065 |
|
|
1066 |
#ifdef Q_OS_SYMBIAN
|
|
1067 |
void tst_QLocalSocket::unlink(QString name)
|
|
1068 |
{
|
|
1069 |
if(name.length() == 0)
|
|
1070 |
return;
|
|
1071 |
|
|
1072 |
QString fullName;
|
|
1073 |
// determine the full server path
|
|
1074 |
if (name.startsWith(QLatin1Char('/'))) {
|
|
1075 |
fullName = name;
|
|
1076 |
} else {
|
|
1077 |
fullName = QDir::cleanPath(QDir::tempPath());
|
|
1078 |
fullName += QLatin1Char('/') + name;
|
|
1079 |
fullName = QDir::toNativeSeparators(fullName);
|
|
1080 |
}
|
|
1081 |
|
|
1082 |
int result = ::unlink(fullName.toUtf8().data());
|
|
1083 |
|
|
1084 |
if(result != 0) {
|
|
1085 |
qWarning() << "Unlinking " << fullName << " failed with " << strerror(errno);
|
|
1086 |
}
|
|
1087 |
}
|
|
1088 |
#endif
|
|
1089 |
QTEST_MAIN(tst_QLocalSocket)
|
|
1090 |
#include "tst_qlocalsocket.moc"
|
|
1091 |
|