1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
49 #include <private/qpacketprotocol_p.h>
51 #include "../shared/debugutil_p.h"
53 class tst_QPacketProtocol : public QObject
60 QTcpSocket *m_serverConn;
66 void maximumPacketSize();
67 void setMaximumPacketSize();
68 void setMaximumPacketSize_data();
71 void packetsAvailable();
72 void packetsAvailable_data();
77 void tst_QPacket_clear();
80 void tst_QPacketProtocol::init()
82 m_server = new QTcpServer(this);
84 QVERIFY(m_server->listen(QHostAddress("127.0.0.1")));
86 m_client = new QTcpSocket(this);
87 m_client->connectToHost(m_server->serverAddress(), m_server->serverPort());
89 QVERIFY(m_client->waitForConnected());
90 QVERIFY(m_server->waitForNewConnection(5000));
91 m_serverConn = m_server->nextPendingConnection();
94 void tst_QPacketProtocol::cleanup()
101 void tst_QPacketProtocol::maximumPacketSize()
103 QPacketProtocol p(m_client);
104 QCOMPARE(p.maximumPacketSize(), 0x7FFFFFFF);
107 void tst_QPacketProtocol::setMaximumPacketSize()
109 QFETCH(qint32, size);
110 QFETCH(qint32, expected);
112 QPacketProtocol out(m_serverConn);
113 QCOMPARE(out.setMaximumPacketSize(size), expected);
116 void tst_QPacketProtocol::setMaximumPacketSize_data()
118 QTest::addColumn<int>("size");
119 QTest::addColumn<int>("expected");
121 QTest::newRow("invalid") << qint32(sizeof(qint32) - 1) << qint32(0x7FFFFFFF);
122 QTest::newRow("still invalid") << qint32(sizeof(qint32)) << qint32(0x7FFFFFFF);
123 QTest::newRow("valid") << qint32(sizeof(qint32) + 1) << qint32(sizeof(qint32) + 1);
126 void tst_QPacketProtocol::send()
128 QFETCH(bool, useAutoSend);
130 QPacketProtocol in(m_client);
131 QPacketProtocol out(m_serverConn);
137 out.send() << "Hello world" << 123;
140 packet << "Hello world" << 123;
144 QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
146 QPacket p = in.read();
148 QCOMPARE(ba, QByteArray("Hello world") + '\0');
152 void tst_QPacketProtocol::send_data()
154 QTest::addColumn<bool>("useAutoSend");
156 QTest::newRow("auto send") << true;
157 QTest::newRow("no auto send") << false;
160 void tst_QPacketProtocol::packetsAvailable()
162 QFETCH(int, packetCount);
164 QPacketProtocol out(m_client);
165 QPacketProtocol in(m_serverConn);
167 QCOMPARE(out.packetsAvailable(), qint64(0));
168 QCOMPARE(in.packetsAvailable(), qint64(0));
170 for (int i=0; i<packetCount; i++)
171 out.send() << "Hello";
173 QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
174 QCOMPARE(in.packetsAvailable(), qint64(packetCount));
177 void tst_QPacketProtocol::packetsAvailable_data()
179 QTest::addColumn<int>("packetCount");
181 QTest::newRow("1") << 1;
182 QTest::newRow("2") << 2;
183 QTest::newRow("10") << 10;
186 void tst_QPacketProtocol::clear()
188 QPacketProtocol in(m_client);
189 QPacketProtocol out(m_serverConn);
193 QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
196 QVERIFY(in.read().isEmpty());
199 void tst_QPacketProtocol::read()
201 QPacketProtocol in(m_client);
202 QPacketProtocol out(m_serverConn);
204 QVERIFY(in.read().isEmpty());
208 QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
212 QPacket p1 = in.read();
213 QVERIFY(!p1.isEmpty());
217 QPacket p2 = in.read();
218 QVERIFY(!p2.isEmpty());
222 QVERIFY(in.read().isEmpty());
225 void tst_QPacketProtocol::device()
227 QPacketProtocol p(m_client);
228 QVERIFY(p.device() == m_client);
231 void tst_QPacketProtocol::tst_QPacket_clear()
233 QPacketProtocol protocol(m_client);
237 packet << "Hello world!" << 123;
238 protocol.send(packet);
241 QVERIFY(packet.isEmpty());
242 packet << "Goodbyte world!" << 789;
243 protocol.send(packet);
247 QPacketProtocol in(m_serverConn);
248 QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
250 QPacket p1 = in.read();
252 QCOMPARE(ba, QByteArray("Hello world!") + '\0');
255 QPacket p2 = in.read();
257 QCOMPARE(ba, QByteArray("Goodbyte world!") + '\0');
261 QTEST_MAIN(tst_QPacketProtocol)
263 #include "tst_qpacketprotocol.moc"