|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 Qt Mobility Components. |
|
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 #include <qpacketprotocol_p.h> |
|
43 |
|
44 #include <QByteArray> |
|
45 #include <QBuffer> |
|
46 #include <QLocalServer> |
|
47 #include <QLocalSocket> |
|
48 |
|
49 #include <QtTest/QTest> |
|
50 #include <QSignalSpy> |
|
51 |
|
52 #include <QDebug> |
|
53 |
|
54 #define QTRY_COMPARE(a,e) \ |
|
55 for (int _i = 0; _i < 5000; _i += 100) { \ |
|
56 if ((a) == (e)) break; \ |
|
57 QTest::qWait(100); \ |
|
58 } \ |
|
59 QCOMPARE(a, e) |
|
60 |
|
61 #define QTRY_VERIFY(a) \ |
|
62 for (int _i = 0; _i < 5000; _i += 100) { \ |
|
63 if (a) break; \ |
|
64 QTest::qWait(100); \ |
|
65 } \ |
|
66 QVERIFY(a) |
|
67 |
|
68 Q_DECLARE_METATYPE(QList<int>) |
|
69 |
|
70 QTM_USE_NAMESPACE |
|
71 class tst_QPacketProtocol : public QObject |
|
72 { |
|
73 Q_OBJECT |
|
74 |
|
75 private slots: |
|
76 void constructor(); |
|
77 |
|
78 void maximumPacketSize_data(); |
|
79 void maximumPacketSize(); |
|
80 |
|
81 void sendReceive(); |
|
82 }; |
|
83 |
|
84 void tst_QPacketProtocol::constructor() |
|
85 { |
|
86 QByteArray data; |
|
87 QBuffer buffer(&data); |
|
88 |
|
89 { |
|
90 QPacketProtocol protocol(&buffer); |
|
91 |
|
92 QCOMPARE(protocol.device(), (QIODevice*)&buffer); |
|
93 } |
|
94 } |
|
95 |
|
96 #define ROW_FOR_SIZE(x) x << x << (QList<int>() << 1 << 1 << (x - 4) << 1 << (x - 3) << 0) |
|
97 |
|
98 void tst_QPacketProtocol::maximumPacketSize_data() |
|
99 { |
|
100 QTest::addColumn<qint32>("maxPacketSize"); |
|
101 QTest::addColumn<qint32>("actualMaxPacketSize"); |
|
102 QTest::addColumn<QList<int> >("packetSizes"); // pairs of packetSize, readCount |
|
103 |
|
104 #ifndef Q_OS_WIN |
|
105 //Don't perform these tests under windows |
|
106 //because they time out due to QLocalSocket |
|
107 //being too slow. Packet sizes typically |
|
108 //won't be this large anyway. |
|
109 QTest::newRow("10MB") << ROW_FOR_SIZE(10 * 1024 * 1024); |
|
110 QTest::newRow("1MB") << ROW_FOR_SIZE(1024 * 1024); |
|
111 #endif |
|
112 QTest::newRow("100kB") << ROW_FOR_SIZE(100 * 1024); |
|
113 QTest::newRow("10kB") << ROW_FOR_SIZE(10 * 1024); |
|
114 QTest::newRow("1kB") << ROW_FOR_SIZE(1024); |
|
115 QTest::newRow("100B") << ROW_FOR_SIZE(100); |
|
116 QTest::newRow("10B") << ROW_FOR_SIZE(10); |
|
117 QTest::newRow("5B") << ROW_FOR_SIZE(5); |
|
118 QTest::newRow("4B") << 4 << 0x7FFFFFFF << QList<int>(); |
|
119 QTest::newRow("0B") << 0 << 0x7FFFFFFF << QList<int>(); |
|
120 QTest::newRow("-1") << -1 << 0x7FFFFFFF << QList<int>(); |
|
121 } |
|
122 |
|
123 void tst_QPacketProtocol::maximumPacketSize() |
|
124 { |
|
125 QFETCH(qint32, maxPacketSize); |
|
126 QFETCH(qint32, actualMaxPacketSize); |
|
127 QFETCH(QList<int>, packetSizes); |
|
128 |
|
129 QLocalServer server; |
|
130 server.listen("tst_QPacketProtocol"); |
|
131 |
|
132 QLocalSocket socket; |
|
133 socket.connectToServer("tst_QPacketProtocol", QIODevice::WriteOnly); |
|
134 |
|
135 bool timedOut; |
|
136 server.waitForNewConnection(5000, &timedOut); |
|
137 QVERIFY(!timedOut); |
|
138 |
|
139 QLocalSocket *serverSocket = server.nextPendingConnection(); |
|
140 |
|
141 QVERIFY(serverSocket); |
|
142 |
|
143 QPacketProtocol writeProtocol(&socket); |
|
144 QPacketProtocol readProtocol(serverSocket); |
|
145 |
|
146 readProtocol.setMaximumPacketSize(maxPacketSize); |
|
147 |
|
148 QCOMPARE(readProtocol.maximumPacketSize(), actualMaxPacketSize); |
|
149 |
|
150 if (maxPacketSize == actualMaxPacketSize) { |
|
151 QVERIFY(!packetSizes.isEmpty()); |
|
152 QVERIFY(packetSizes.count() % 2 == 0); |
|
153 |
|
154 while (!packetSizes.isEmpty()) { |
|
155 int packetSize = packetSizes.takeFirst(); |
|
156 int readCount = packetSizes.takeFirst(); |
|
157 |
|
158 QByteArray b(packetSize, 'T'); |
|
159 QPacket sendPacket; |
|
160 sendPacket.writeRawData(b.constData(), packetSize); |
|
161 writeProtocol.send(sendPacket); |
|
162 |
|
163 QSignalSpy spy(&readProtocol, SIGNAL(readyRead())); |
|
164 QSignalSpy invalidSpy(&readProtocol, SIGNAL(invalidPacket())); |
|
165 |
|
166 QTRY_COMPARE(spy.count(), readCount); |
|
167 QTRY_COMPARE(invalidSpy.count(), 1 - readCount); |
|
168 |
|
169 QCOMPARE(readProtocol.packetsAvailable(), qint64(readCount)); |
|
170 |
|
171 QPacket packet = readProtocol.read(); |
|
172 |
|
173 QCOMPARE(packet.device()->size(), qint64(readCount * packetSize)); |
|
174 } |
|
175 } |
|
176 } |
|
177 |
|
178 void tst_QPacketProtocol::sendReceive() |
|
179 { |
|
180 QLocalServer server; |
|
181 server.listen("tst_QPacketProtocol"); |
|
182 |
|
183 QLocalSocket socket; |
|
184 socket.connectToServer("tst_QPacketProtocol", QIODevice::WriteOnly); |
|
185 |
|
186 bool timedOut; |
|
187 server.waitForNewConnection(5000, &timedOut); |
|
188 QVERIFY(!timedOut); |
|
189 |
|
190 QLocalSocket *serverSocket = server.nextPendingConnection(); |
|
191 QVERIFY(serverSocket); |
|
192 |
|
193 QPacketProtocol writeProtocol(&socket); |
|
194 QPacketProtocol readProtocol(serverSocket); |
|
195 |
|
196 QByteArray dataString("Data string"); |
|
197 |
|
198 // Test sending with ::send(QPacket). |
|
199 for (int i = 1; i <= 10; ++i) { |
|
200 QSignalSpy writeSpy(&writeProtocol, SIGNAL(packetWritten())); |
|
201 QSignalSpy readSpy(&readProtocol, SIGNAL(readyRead())); |
|
202 |
|
203 // Test sending an empty packet. |
|
204 writeProtocol.send(QPacket()); |
|
205 |
|
206 QPacket sendPacket; |
|
207 sendPacket << dataString; |
|
208 for (int j = 0; j < i; ++j) |
|
209 writeProtocol.send(sendPacket); |
|
210 |
|
211 QTRY_COMPARE(writeSpy.count(), i); |
|
212 QTRY_COMPARE(readSpy.count(), i); |
|
213 |
|
214 QCOMPARE(readProtocol.packetsAvailable(), qint64(i)); |
|
215 for (int j = 0; j < i; ++j) { |
|
216 QPacket packet = readProtocol.read(); |
|
217 QByteArray r; |
|
218 packet >> r; |
|
219 QCOMPARE(r, dataString); |
|
220 } |
|
221 |
|
222 QCOMPARE(readProtocol.packetsAvailable(), qint64(0)); |
|
223 } |
|
224 |
|
225 // Test sending with ::send() << var. |
|
226 for (int i = 1; i <= 10; ++i) { |
|
227 QSignalSpy writeSpy(&writeProtocol, SIGNAL(packetWritten())); |
|
228 QSignalSpy readSpy(&readProtocol, SIGNAL(readyRead())); |
|
229 |
|
230 // Test sending an empty packet. |
|
231 writeProtocol.send(); |
|
232 |
|
233 for (int j = 0; j < i; ++j) |
|
234 writeProtocol.send() << dataString.constData(); |
|
235 |
|
236 QTRY_COMPARE(writeSpy.count(), i); |
|
237 QTRY_COMPARE(readSpy.count(), i); |
|
238 |
|
239 QCOMPARE(readProtocol.packetsAvailable(), qint64(i)); |
|
240 for (int j = 0; j < i; ++j) { |
|
241 QPacket packet = readProtocol.read(); |
|
242 char *string; |
|
243 packet >> string; |
|
244 QByteArray r(string); |
|
245 QCOMPARE(r, dataString); |
|
246 } |
|
247 |
|
248 QCOMPARE(readProtocol.packetsAvailable(), qint64(0)); |
|
249 } |
|
250 |
|
251 // Test ::clear(). |
|
252 for (int i = 1; i <= 10; ++i) { |
|
253 QByteArray b("Data string"); |
|
254 |
|
255 QSignalSpy writeSpy(&writeProtocol, SIGNAL(packetWritten())); |
|
256 QSignalSpy readSpy(&readProtocol, SIGNAL(readyRead())); |
|
257 |
|
258 QPacket sendPacket; |
|
259 sendPacket << b; |
|
260 for (int j = 0; j < i; ++j) |
|
261 writeProtocol.send(sendPacket); |
|
262 |
|
263 QTRY_COMPARE(writeSpy.count(), i); |
|
264 QTRY_COMPARE(readSpy.count(), i); |
|
265 |
|
266 QCOMPARE(readProtocol.packetsAvailable(), qint64(i)); |
|
267 |
|
268 readProtocol.clear(); |
|
269 |
|
270 QCOMPARE(readProtocol.packetsAvailable(), qint64(0)); |
|
271 } |
|
272 |
|
273 { |
|
274 QSignalSpy writeSpy(&writeProtocol, SIGNAL(packetWritten())); |
|
275 QSignalSpy readSpy(&readProtocol, SIGNAL(readyRead())); |
|
276 QSignalSpy closeSpy(serverSocket, SIGNAL(aboutToClose())); |
|
277 |
|
278 writeProtocol.send() << dataString.constData(); |
|
279 |
|
280 serverSocket->close(); |
|
281 socket.close(); |
|
282 |
|
283 QTRY_VERIFY(!closeSpy.isEmpty()); |
|
284 |
|
285 QTRY_VERIFY(writeSpy.isEmpty()); |
|
286 QTRY_VERIFY(readSpy.isEmpty()); |
|
287 } |
|
288 } |
|
289 |
|
290 QTEST_MAIN(tst_QPacketProtocol) |
|
291 #include "tst_qpacketprotocol.moc" |