summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/socket/qlocalsocket
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2016-05-06 09:07:18 +0200
committerLiang Qi <liang.qi@qt.io>2016-05-06 15:36:44 +0200
commitdbef41f43ece97d8ac4d6b63f7a36afcb7e7b7f7 (patch)
tree334ebaec198a7f31d1e3a4639c70fbb8bb116af2 /tests/auto/network/socket/qlocalsocket
parent52d30e2850769d589772576e4714a14241c7da6e (diff)
parentf57d8f1341587e6b2aa84b8404aa218432584206 (diff)
Merge remote-tracking branch 'origin/5.6' into 5.7
Conflicts: examples/qtestlib/tutorial5/containers.cpp examples/widgets/tools/tools.pro src/corelib/io/qprocess.cpp src/corelib/io/qprocess_unix.cpp src/corelib/io/qprocess_win.cpp src/network/kernel/qdnslookup_unix.cpp src/plugins/platforms/xcb/qxcbconnection_xi2.cpp src/testlib/qtestcase.cpp tools/configure/configureapp.cpp Change-Id: I838ae7f082535a67a4a53aa13a21ba5580758be8
Diffstat (limited to 'tests/auto/network/socket/qlocalsocket')
-rw-r--r--tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp
index d0baa62e29..4bea9b116c 100644
--- a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp
+++ b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp
@@ -31,6 +31,7 @@
#include <QtTest/QtTest>
#include <qtextstream.h>
+#include <qdatastream.h>
#include <QtNetwork/qlocalsocket.h>
#include <QtNetwork/qlocalserver.h>
@@ -73,6 +74,9 @@ private slots:
void readBufferOverflow();
+ void simpleCommandProtocol1();
+ void simpleCommandProtocol2();
+
void fullPath();
void hitMaximumConnections_data();
@@ -622,6 +626,110 @@ void tst_QLocalSocket::readBufferOverflow()
QCOMPARE(client.bytesAvailable(), 0);
}
+static qint64 writeCommand(const QVariant &command, QIODevice *device, int commandCounter)
+{
+ QByteArray block;
+ QDataStream out(&block, QIODevice::WriteOnly);
+ out << qint64(0);
+ out << commandCounter;
+ out << command;
+ out.device()->seek(0);
+ out << qint64(block.size() - sizeof(qint64));
+ return device->write(block);
+}
+
+static QVariant readCommand(QIODevice *ioDevice, int *readCommandCounter, bool readSize = true)
+{
+ QDataStream in(ioDevice);
+ qint64 blockSize;
+ int commandCounter;
+ if (readSize)
+ in >> blockSize;
+ in >> commandCounter;
+ *readCommandCounter = commandCounter;
+
+ QVariant command;
+ in >> command;
+
+ return command;
+}
+
+void tst_QLocalSocket::simpleCommandProtocol1()
+{
+ QLocalServer server;
+ server.listen(QStringLiteral("simpleProtocol"));
+
+ QLocalSocket localSocketWrite;
+ localSocketWrite.connectToServer(server.serverName());
+ QVERIFY(server.waitForNewConnection());
+ QLocalSocket *localSocketRead = server.nextPendingConnection();
+ QVERIFY(localSocketRead);
+
+ int readCounter = 0;
+ for (int i = 0; i < 2000; ++i) {
+ const QVariant command(QRect(readCounter, i, 10, 10));
+ const qint64 blockSize = writeCommand(command, &localSocketWrite, i);
+ while (localSocketWrite.bytesToWrite())
+ QVERIFY(localSocketWrite.waitForBytesWritten());
+ while (localSocketRead->bytesAvailable() < blockSize) {
+ QVERIFY(localSocketRead->waitForReadyRead(1000));
+ }
+ const QVariant variant = readCommand(localSocketRead, &readCounter);
+ QCOMPARE(readCounter, i);
+ QCOMPARE(variant, command);
+ }
+}
+
+void tst_QLocalSocket::simpleCommandProtocol2()
+{
+ QLocalServer server;
+ server.listen(QStringLiteral("simpleProtocol"));
+
+ QLocalSocket localSocketWrite;
+ localSocketWrite.connectToServer(server.serverName());
+ QVERIFY(server.waitForNewConnection());
+ QLocalSocket* localSocketRead = server.nextPendingConnection();
+ QVERIFY(localSocketRead);
+
+ int readCounter = 0;
+ qint64 writtenBlockSize = 0;
+ qint64 blockSize = 0;
+
+ QObject::connect(localSocketRead, &QLocalSocket::readyRead, [&] {
+ forever {
+ if (localSocketRead->bytesAvailable() < sizeof(qint64))
+ return;
+
+ if (blockSize == 0) {
+ QDataStream in(localSocketRead);
+ in >> blockSize;
+ }
+
+ if (localSocketRead->bytesAvailable() < blockSize)
+ return;
+
+ int commandNumber = 0;
+ const QVariant variant = readCommand(localSocketRead, &commandNumber, false);
+ QCOMPARE(writtenBlockSize, blockSize);
+ QCOMPARE(readCounter, commandNumber);
+ QCOMPARE(variant.userType(), (int)QMetaType::QRect);
+
+ readCounter++;
+ blockSize = 0;
+ }
+ });
+
+ for (int i = 0; i < 500; ++i) {
+ const QVariant command(QRect(readCounter, i, 10, 10));
+ writtenBlockSize = writeCommand(command, &localSocketWrite, i) - sizeof(qint64);
+ if (i % 10 == 0)
+ QTest::qWait(1);
+ }
+
+ localSocketWrite.abort();
+ QVERIFY(localSocketRead->waitForDisconnected(1000));
+}
+
// QLocalSocket/Server can take a name or path, check that it works as expected
void tst_QLocalSocket::fullPath()
{