|
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 <QtCore/QtCore> |
|
44 #include <QtNetwork/QtNetwork> |
|
45 #include <QtTest/QtTest> |
|
46 |
|
47 #include "../network-settings.h" |
|
48 |
|
49 //TESTED_CLASS= |
|
50 //TESTED_FILES= |
|
51 |
|
52 #ifdef Q_OS_SYMBIAN |
|
53 #define SRCDIR "" |
|
54 #endif |
|
55 |
|
56 class tst_QIODevice : public QObject |
|
57 { |
|
58 Q_OBJECT |
|
59 |
|
60 public: |
|
61 tst_QIODevice(); |
|
62 virtual ~tst_QIODevice(); |
|
63 |
|
64 |
|
65 public slots: |
|
66 void init(); |
|
67 void cleanup(); |
|
68 private slots: |
|
69 void getSetCheck(); |
|
70 void constructing_QTcpSocket(); |
|
71 void constructing_QFile(); |
|
72 void read_QByteArray(); |
|
73 void unget(); |
|
74 void peek(); |
|
75 void getch(); |
|
76 void putch(); |
|
77 |
|
78 void readLine_data(); |
|
79 void readLine(); |
|
80 }; |
|
81 |
|
82 // Testing get/set functions |
|
83 void tst_QIODevice::getSetCheck() |
|
84 { |
|
85 // OpenMode QIODevice::openMode() |
|
86 // void QIODevice::setOpenMode(OpenMode) |
|
87 class MyIODevice : public QIODevice { |
|
88 public: |
|
89 void setOpenMode(OpenMode openMode) { QIODevice::setOpenMode(openMode); } |
|
90 }; |
|
91 QTcpSocket var1; |
|
92 MyIODevice *obj1 = reinterpret_cast<MyIODevice*>(&var1); |
|
93 obj1->setOpenMode(QIODevice::OpenMode(QIODevice::NotOpen)); |
|
94 QCOMPARE(QIODevice::OpenMode(QIODevice::NotOpen), obj1->openMode()); |
|
95 obj1->setOpenMode(QIODevice::OpenMode(QIODevice::ReadWrite)); |
|
96 QCOMPARE(QIODevice::OpenMode(QIODevice::ReadWrite), obj1->openMode()); |
|
97 } |
|
98 |
|
99 tst_QIODevice::tst_QIODevice() |
|
100 { |
|
101 Q_SET_DEFAULT_IAP |
|
102 } |
|
103 |
|
104 tst_QIODevice::~tst_QIODevice() |
|
105 { |
|
106 } |
|
107 |
|
108 void tst_QIODevice::init() |
|
109 { |
|
110 } |
|
111 |
|
112 void tst_QIODevice::cleanup() |
|
113 { |
|
114 } |
|
115 |
|
116 //---------------------------------------------------------------------------------- |
|
117 void tst_QIODevice::constructing_QTcpSocket() |
|
118 { |
|
119 #if defined(Q_OS_WINCE) && defined(WINCE_EMULATOR_TEST) |
|
120 QSKIP("Networking tests in a WinCE emulator are unstable", SkipAll); |
|
121 #endif |
|
122 QTcpSocket socket; |
|
123 QIODevice *device = &socket; |
|
124 |
|
125 QVERIFY(!device->isOpen()); |
|
126 |
|
127 socket.connectToHost(QtNetworkSettings::serverName(), 143); |
|
128 QVERIFY(socket.waitForConnected(5000)); |
|
129 QVERIFY(device->isOpen()); |
|
130 |
|
131 while (!device->canReadLine()) |
|
132 QVERIFY(device->waitForReadyRead(5000)); |
|
133 |
|
134 char buf[1024]; |
|
135 memset(buf, 0, sizeof(buf)); |
|
136 qlonglong lineLength = device->readLine(buf, sizeof(buf)); |
|
137 QVERIFY(lineLength > 0); |
|
138 QCOMPARE(socket.pos(), qlonglong(0)); |
|
139 |
|
140 socket.close(); |
|
141 socket.connectToHost(QtNetworkSettings::serverName(), 143); |
|
142 QVERIFY(socket.waitForConnected(5000)); |
|
143 QVERIFY(device->isOpen()); |
|
144 |
|
145 while (!device->canReadLine()) |
|
146 QVERIFY(device->waitForReadyRead(5000)); |
|
147 |
|
148 char buf2[1024]; |
|
149 memset(buf2, 0, sizeof(buf2)); |
|
150 QCOMPARE(socket.readLine(buf2, sizeof(buf2)), lineLength); |
|
151 |
|
152 char *c1 = buf; |
|
153 char *c2 = buf2; |
|
154 while (*c1 && *c2) { |
|
155 QCOMPARE(*c1, *c2); |
|
156 ++c1; |
|
157 ++c2; |
|
158 } |
|
159 QCOMPARE(*c1, *c2); |
|
160 } |
|
161 |
|
162 //---------------------------------------------------------------------------------- |
|
163 void tst_QIODevice::constructing_QFile() |
|
164 { |
|
165 QFile file; |
|
166 QIODevice *device = &file; |
|
167 |
|
168 QVERIFY(!device->isOpen()); |
|
169 |
|
170 file.setFileName(SRCDIR "tst_qiodevice.cpp"); |
|
171 QVERIFY(file.open(QFile::ReadOnly)); |
|
172 QVERIFY(device->isOpen()); |
|
173 QCOMPARE((int) device->openMode(), (int) QFile::ReadOnly); |
|
174 |
|
175 char buf[1024]; |
|
176 memset(buf, 0, sizeof(buf)); |
|
177 qlonglong lineLength = device->readLine(buf, sizeof(buf)); |
|
178 QVERIFY(lineLength > 0); |
|
179 QCOMPARE(file.pos(), lineLength); |
|
180 |
|
181 file.seek(0); |
|
182 char buf2[1024]; |
|
183 memset(buf2, 0, sizeof(buf2)); |
|
184 QCOMPARE(file.readLine(buf2, sizeof(buf2)), lineLength); |
|
185 |
|
186 char *c1 = buf; |
|
187 char *c2 = buf2; |
|
188 while (*c1 && *c2) { |
|
189 QCOMPARE(*c1, *c2); |
|
190 ++c1; |
|
191 ++c2; |
|
192 } |
|
193 QCOMPARE(*c1, *c2); |
|
194 } |
|
195 |
|
196 |
|
197 void tst_QIODevice::read_QByteArray() |
|
198 { |
|
199 QFile f(SRCDIR "tst_qiodevice.cpp"); |
|
200 f.open(QIODevice::ReadOnly); |
|
201 |
|
202 QByteArray b = f.read(10); |
|
203 QCOMPARE(b.length(), 10); |
|
204 |
|
205 b = f.read(256); |
|
206 QCOMPARE(b.length(), 256); |
|
207 |
|
208 b = f.read(0); |
|
209 QCOMPARE(b.length(), 0); |
|
210 } |
|
211 |
|
212 //-------------------------------------------------------------------- |
|
213 void tst_QIODevice::unget() |
|
214 { |
|
215 #if defined(Q_OS_WINCE) && defined(WINCE_EMULATOR_TEST) |
|
216 QSKIP("Networking tests in a WinCE emulator are unstable", SkipAll); |
|
217 #endif |
|
218 QBuffer buffer; |
|
219 buffer.open(QBuffer::ReadWrite); |
|
220 buffer.write("ZXCV"); |
|
221 buffer.seek(0); |
|
222 QCOMPARE(buffer.read(4), QByteArray("ZXCV")); |
|
223 QCOMPARE(buffer.pos(), qint64(4)); |
|
224 |
|
225 buffer.ungetChar('a'); |
|
226 buffer.ungetChar('b'); |
|
227 buffer.ungetChar('c'); |
|
228 buffer.ungetChar('d'); |
|
229 |
|
230 QCOMPARE(buffer.pos(), qint64(0)); |
|
231 |
|
232 char buf[6]; |
|
233 QCOMPARE(buffer.readLine(buf, 5), qint64(4)); |
|
234 QCOMPARE(buffer.pos(), qint64(4)); |
|
235 QCOMPARE(static_cast<const char*>(buf), "dcba"); |
|
236 |
|
237 buffer.ungetChar('a'); |
|
238 buffer.ungetChar('b'); |
|
239 buffer.ungetChar('c'); |
|
240 buffer.ungetChar('d'); |
|
241 |
|
242 QCOMPARE(buffer.pos(), qint64(0)); |
|
243 |
|
244 for (int i = 0; i < 5; ++i) { |
|
245 buf[0] = '@'; |
|
246 buf[1] = '@'; |
|
247 QTest::ignoreMessage(QtWarningMsg, |
|
248 "QIODevice::readLine: Called with maxSize < 2"); |
|
249 QCOMPARE(buffer.readLine(buf, 1), qint64(-1)); |
|
250 QCOMPARE(buffer.readLine(buf, 2), qint64(i < 4 ? 1 : -1)); |
|
251 switch (i) { |
|
252 case 0: QCOMPARE(buf[0], 'd'); break; |
|
253 case 1: QCOMPARE(buf[0], 'c'); break; |
|
254 case 2: QCOMPARE(buf[0], 'b'); break; |
|
255 case 3: QCOMPARE(buf[0], 'a'); break; |
|
256 case 4: QCOMPARE(buf[0], '\0'); break; |
|
257 } |
|
258 QCOMPARE(buf[1], i < 4 ? '\0' : '@'); |
|
259 } |
|
260 |
|
261 buffer.ungetChar('\n'); |
|
262 QCOMPARE(buffer.readLine(), QByteArray("\n")); |
|
263 |
|
264 buffer.seek(1); |
|
265 buffer.readLine(buf, 3); |
|
266 QCOMPARE(static_cast<const char*>(buf), "XC"); |
|
267 |
|
268 buffer.seek(4); |
|
269 buffer.ungetChar('Q'); |
|
270 QCOMPARE(buffer.readLine(buf, 3), qint64(1)); |
|
271 |
|
272 for (int i = 0; i < 2; ++i) { |
|
273 QTcpSocket socket; |
|
274 QIODevice *dev; |
|
275 QByteArray result; |
|
276 const char *lineResult; |
|
277 if (i == 0) { |
|
278 dev = &buffer; |
|
279 result = QByteArray("ZXCV"); |
|
280 lineResult = "ZXCV"; |
|
281 } else { |
|
282 socket.connectToHost(QtNetworkSettings::serverName(), 80); |
|
283 socket.write("GET / HTTP/1.0\r\n\r\n"); |
|
284 QVERIFY(socket.waitForReadyRead()); |
|
285 dev = &socket; |
|
286 result = QByteArray("HTTP"); |
|
287 lineResult = "Date"; |
|
288 } |
|
289 char ch, ch2; |
|
290 dev->seek(0); |
|
291 dev->getChar(&ch); |
|
292 dev->ungetChar(ch); |
|
293 QCOMPARE(dev->peek(4), result); |
|
294 dev->getChar(&ch); |
|
295 dev->getChar(&ch2); |
|
296 dev->ungetChar(ch2); |
|
297 dev->ungetChar(ch); |
|
298 QCOMPARE(dev->read(1), result.left(1)); |
|
299 QCOMPARE(dev->read(3), result.right(3)); |
|
300 |
|
301 if (i == 0) |
|
302 dev->seek(0); |
|
303 else |
|
304 dev->readLine(); |
|
305 dev->getChar(&ch); |
|
306 dev->ungetChar(ch); |
|
307 dev->readLine(buf, 5); |
|
308 QCOMPARE(static_cast<const char*>(buf), lineResult); |
|
309 |
|
310 if (i == 1) |
|
311 socket.close(); |
|
312 } |
|
313 } |
|
314 |
|
315 //-------------------------------------------------------------------- |
|
316 void tst_QIODevice::peek() |
|
317 { |
|
318 QBuffer buffer; |
|
319 QFile::remove("peektestfile"); |
|
320 QFile file("peektestfile"); |
|
321 |
|
322 for (int i = 0; i < 2; ++i) { |
|
323 QIODevice *device = i ? (QIODevice *)&file : (QIODevice *)&buffer; |
|
324 |
|
325 device->open(QBuffer::ReadWrite); |
|
326 device->write("ZXCV"); |
|
327 |
|
328 device->seek(0); |
|
329 QCOMPARE(device->peek(4), QByteArray("ZXCV")); |
|
330 QCOMPARE(device->pos(), qint64(0)); |
|
331 device->write("ABCDE"); |
|
332 device->seek(3); |
|
333 QCOMPARE(device->peek(1), QByteArray("D")); |
|
334 QCOMPARE(device->peek(5), QByteArray("DE")); |
|
335 device->seek(0); |
|
336 QCOMPARE(device->read(4), QByteArray("ABCD")); |
|
337 QCOMPARE(device->pos(), qint64(4)); |
|
338 |
|
339 device->seek(0); |
|
340 device->write("ZXCV"); |
|
341 device->seek(0); |
|
342 char buf[5]; |
|
343 buf[4] = 0; |
|
344 device->peek(buf, 4); |
|
345 QCOMPARE(static_cast<const char *>(buf), "ZXCV"); |
|
346 QCOMPARE(device->pos(), qint64(0)); |
|
347 device->read(buf, 4); |
|
348 QCOMPARE(static_cast<const char *>(buf), "ZXCV"); |
|
349 QCOMPARE(device->pos(), qint64(4)); |
|
350 } |
|
351 QFile::remove("peektestfile"); |
|
352 } |
|
353 |
|
354 void tst_QIODevice::getch() |
|
355 { |
|
356 #ifdef QT3_SUPPORT |
|
357 QBuffer buffer; |
|
358 buffer.open(QBuffer::ReadWrite); |
|
359 buffer.write("\xff\x7f\x80\x00", 4); |
|
360 buffer.reset(); |
|
361 QCOMPARE(buffer.getch(), 0xff); |
|
362 QCOMPARE(buffer.getch(), 0x7f); |
|
363 QCOMPARE(buffer.getch(), 0x80); |
|
364 QCOMPARE(buffer.getch(), 0x00); |
|
365 |
|
366 buffer.ungetch(0x00); |
|
367 buffer.ungetch(0x80); |
|
368 buffer.ungetch(0x7f); |
|
369 buffer.ungetch(0xff); |
|
370 |
|
371 QCOMPARE(buffer.getch(), 0xff); |
|
372 QCOMPARE(buffer.getch(), 0x7f); |
|
373 QCOMPARE(buffer.getch(), 0x80); |
|
374 QCOMPARE(buffer.getch(), 0x00); |
|
375 #endif |
|
376 } |
|
377 |
|
378 void tst_QIODevice::putch() |
|
379 { |
|
380 #ifdef QT3_SUPPORT |
|
381 QBuffer buffer; |
|
382 buffer.open(QBuffer::ReadWrite); |
|
383 buffer.putch(0xff); |
|
384 buffer.putch(0x7f); |
|
385 buffer.putch(0x80); |
|
386 buffer.putch(0x00); |
|
387 buffer.reset(); |
|
388 QCOMPARE(buffer.getch(), 0xff); |
|
389 QCOMPARE(buffer.getch(), 0x7f); |
|
390 QCOMPARE(buffer.getch(), 0x80); |
|
391 QCOMPARE(buffer.getch(), 0x00); |
|
392 #endif |
|
393 } |
|
394 |
|
395 void tst_QIODevice::readLine_data() |
|
396 { |
|
397 QTest::addColumn<QByteArray>("data"); |
|
398 |
|
399 QTest::newRow("0") << QByteArray("\nAA"); |
|
400 QTest::newRow("1") << QByteArray("A\nAA"); |
|
401 |
|
402 QByteArray data(9000, 'A'); |
|
403 data[8193] = '\n'; |
|
404 QTest::newRow("8194") << data; |
|
405 data[8193] = 'A'; |
|
406 data[8192] = '\n'; |
|
407 QTest::newRow("8193") << data; |
|
408 data[8192] = 'A'; |
|
409 data[8191] = '\n'; |
|
410 QTest::newRow("8192") << data; |
|
411 data[8191] = 'A'; |
|
412 data[8190] = '\n'; |
|
413 QTest::newRow("8191") << data; |
|
414 |
|
415 data[5999] = '\n'; |
|
416 QTest::newRow("6000") << data; |
|
417 |
|
418 data[4095] = '\n'; |
|
419 QTest::newRow("4096") << data; |
|
420 |
|
421 data[4094] = '\n'; |
|
422 data[4095] = 'A'; |
|
423 QTest::newRow("4095") << data; |
|
424 } |
|
425 |
|
426 void tst_QIODevice::readLine() |
|
427 { |
|
428 QFETCH(QByteArray, data); |
|
429 QBuffer buffer(&data); |
|
430 QVERIFY(buffer.open(QIODevice::ReadWrite)); |
|
431 QVERIFY(buffer.canReadLine()); |
|
432 |
|
433 int linelen = data.indexOf('\n') + 1; |
|
434 QByteArray line; |
|
435 line.reserve(linelen + 100); |
|
436 |
|
437 int result = buffer.readLine(line.data(), linelen + 100); |
|
438 QCOMPARE(result, linelen); |
|
439 |
|
440 // try the exact length of the line (plus terminating \0) |
|
441 QVERIFY(buffer.seek(0)); |
|
442 result = buffer.readLine(line.data(), linelen + 1); |
|
443 QCOMPARE(result, linelen); |
|
444 |
|
445 // try with a line length limit |
|
446 QVERIFY(buffer.seek(0)); |
|
447 line = buffer.readLine(linelen + 100); |
|
448 QCOMPARE(line.size(), linelen); |
|
449 |
|
450 // try without a length limit |
|
451 QVERIFY(buffer.seek(0)); |
|
452 line = buffer.readLine(); |
|
453 QCOMPARE(line.size(), linelen); |
|
454 } |
|
455 |
|
456 QTEST_MAIN(tst_QIODevice) |
|
457 #include "tst_qiodevice.moc" |