summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp
blob: 28e39c636934fe9726eaf5556d5062a50a827083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/QCoreApplication>
#include <QtNetwork/QtNetwork>
#include <QTest>

#include "../../../network-settings.h"

class tst_QIODevice : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void cleanupTestCase();
    void getSetCheck();
    void constructing_QTcpSocket();
    void constructing_QFile();
    void read_QByteArray();
    void unget();
    void peek();
    void peekAndRead();

    void readLine_data();
    void readLine();

    void readLine2_data();
    void readLine2();

    void readAllKeepPosition();
    void writeInTextMode();
    void skip_data();
    void skip();
    void skipAfterPeek_data();
    void skipAfterPeek();

    void transaction_data();
    void transaction();

private:
    QSharedPointer<QTemporaryDir> m_tempDir;
    QString m_previousCurrent;
};

void tst_QIODevice::initTestCase()
{
#ifdef Q_OS_ANDROID
    QVERIFY(QFileInfo(QStringLiteral("./tst_qiodevice.cpp")).exists()
            || QFile::copy(QStringLiteral(":/tst_qiodevice.cpp"), QStringLiteral("./tst_qiodevice.cpp")));
#endif
    m_previousCurrent = QDir::currentPath();
    m_tempDir = QSharedPointer<QTemporaryDir>::create();
    QVERIFY2(!m_tempDir.isNull(), qPrintable("Could not create temporary directory."));
    QVERIFY2(QDir::setCurrent(m_tempDir->path()), qPrintable("Could not switch current directory"));
}

void tst_QIODevice::cleanupTestCase()
{
    QDir::setCurrent(m_previousCurrent);
}

// Testing get/set functions
void tst_QIODevice::getSetCheck()
{
    // OpenMode QIODevice::openMode()
    // void QIODevice::setOpenMode(OpenMode)
    class MyIODevice : public QTcpSocket {
    public:
        using QTcpSocket::setOpenMode;
    };
    MyIODevice var1;
    var1.setOpenMode(QIODevice::OpenMode(QIODevice::NotOpen));
    QCOMPARE(QIODevice::OpenMode(QIODevice::NotOpen), var1.openMode());
    var1.setOpenMode(QIODevice::OpenMode(QIODevice::ReadWrite));
    QCOMPARE(QIODevice::OpenMode(QIODevice::ReadWrite), var1.openMode());
}

//----------------------------------------------------------------------------------
void tst_QIODevice::constructing_QTcpSocket()
{
#ifdef QT_TEST_SERVER
    if (!QtNetworkSettings::verifyConnection(QtNetworkSettings::imapServerName(), 143))
        QSKIP("No network test server available");
#else
    if (!QtNetworkSettings::verifyTestNetworkSettings())
        QSKIP("No network test server available");
#endif

    QTcpSocket socket;
    QIODevice *device = &socket;

    QVERIFY(!device->isOpen());

    socket.connectToHost(QtNetworkSettings::imapServerName(), 143);
    QVERIFY(socket.waitForConnected(30000));
    QVERIFY(device->isOpen());
    QCOMPARE(device->readChannelCount(), 1);
    QCOMPARE(device->writeChannelCount(), 1);

    while (!device->canReadLine())
        QVERIFY(device->waitForReadyRead(30000));

    char buf[1024];
    memset(buf, 0, sizeof(buf));
    qlonglong lineLength = device->readLine(buf, sizeof(buf));
    QVERIFY(lineLength > 0);
    QCOMPARE(socket.pos(), qlonglong(0));

    socket.close();
    QCOMPARE(socket.readChannelCount(), 0);
    QCOMPARE(socket.writeChannelCount(), 0);
    socket.connectToHost(QtNetworkSettings::imapServerName(), 143);
    QVERIFY(socket.waitForConnected(30000));
    QVERIFY(device->isOpen());

    while (!device->canReadLine())
        QVERIFY(device->waitForReadyRead(30000));

    char buf2[1024];
    memset(buf2, 0, sizeof(buf2));
    QCOMPARE(socket.readLine(buf2, sizeof(buf2)), lineLength);

    char *c1 = buf;
    char *c2 = buf2;
    while (*c1 && *c2) {
        QCOMPARE(*c1, *c2);
        ++c1;
        ++c2;
    }
    QCOMPARE(*c1, *c2);
}

//----------------------------------------------------------------------------------
void tst_QIODevice::constructing_QFile()
{
    QFile file;
    QIODevice *device = &file;

    QVERIFY(!device->isOpen());

    file.setFileName(QFINDTESTDATA("tst_qiodevice.cpp"));
    QVERIFY(file.open(QFile::ReadOnly));
    QVERIFY(device->isOpen());
    QCOMPARE((int) device->openMode(), (int) QFile::ReadOnly);
    QCOMPARE(device->readChannelCount(), 1);
    QCOMPARE(device->writeChannelCount(), 0);

    char buf[1024];
    memset(buf, 0, sizeof(buf));
    qlonglong lineLength = device->readLine(buf, sizeof(buf));
    QVERIFY(lineLength > 0);
    QCOMPARE(file.pos(), lineLength);

    file.seek(0);
    char buf2[1024];
    memset(buf2, 0, sizeof(buf2));
    QCOMPARE(file.readLine(buf2, sizeof(buf2)), lineLength);

    char *c1 = buf;
    char *c2 = buf2;
    while (*c1 && *c2) {
        QCOMPARE(*c1, *c2);
        ++c1;
        ++c2;
    }
    QCOMPARE(*c1, *c2);
}


void tst_QIODevice::read_QByteArray()
{
    QFile f(QFINDTESTDATA("tst_qiodevice.cpp"));
    f.open(QIODevice::ReadOnly);

    QByteArray b = f.read(10);
    QCOMPARE(b.length(), 10);

    b = f.read(256);
    QCOMPARE(b.length(), 256);

    b = f.read(0);
    QCOMPARE(b.length(), 0);
}

//--------------------------------------------------------------------
void tst_QIODevice::unget()
{
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    buffer.write("ZXCV");
    buffer.seek(0);
    QCOMPARE(buffer.read(4), QByteArray("ZXCV"));
    QCOMPARE(buffer.pos(), qint64(4));

    buffer.ungetChar('a');
    buffer.ungetChar('b');
    buffer.ungetChar('c');
    buffer.ungetChar('d');

    QCOMPARE(buffer.pos(), qint64(0));

    char buf[6];
    QCOMPARE(buffer.readLine(buf, 5), qint64(4));
    QCOMPARE(buffer.pos(), qint64(4));
    QCOMPARE(static_cast<const char*>(buf), "dcba");

    buffer.ungetChar('a');
    buffer.ungetChar('b');
    buffer.ungetChar('c');
    buffer.ungetChar('d');

    QCOMPARE(buffer.pos(), qint64(0));

    for (int i = 0; i < 5; ++i) {
        buf[0] = '@';
        buf[1] = '@';
        QTest::ignoreMessage(QtWarningMsg,
                              "QIODevice::readLine (QBuffer): Called with maxSize < 2");
        QCOMPARE(buffer.readLine(buf, 1), qint64(-1));
        QCOMPARE(buffer.readLine(buf, 2), qint64(i < 4 ? 1 : -1));
        switch (i) {
        case 0: QCOMPARE(buf[0], 'd'); break;
        case 1: QCOMPARE(buf[0], 'c'); break;
        case 2: QCOMPARE(buf[0], 'b'); break;
        case 3: QCOMPARE(buf[0], 'a'); break;
        case 4: QCOMPARE(buf[0], '\0'); break;
        }
        QCOMPARE(buf[1], i < 4 ? '\0' : '@');
    }

    buffer.ungetChar('\n');
    QCOMPARE(buffer.readLine(), QByteArray("\n"));

    buffer.seek(1);
    buffer.readLine(buf, 3);
    QCOMPARE(static_cast<const char*>(buf), "XC");

    buffer.seek(4);
    buffer.ungetChar('Q');
    QCOMPARE(buffer.readLine(buf, 3), qint64(1));

    for (int i = 0; i < 2; ++i) {
        QTcpSocket socket;
        QIODevice *dev;
        QByteArray result;
        const char *lineResult;
        if (i == 0) {
            dev = &buffer;
            result = QByteArray("ZXCV");
            lineResult = "ZXCV";
        } else {
#ifdef QT_TEST_SERVER
            const bool hasNetworkServer =
                    QtNetworkSettings::verifyConnection(QtNetworkSettings::httpServerName(), 80);
#else
            const bool hasNetworkServer = QtNetworkSettings::verifyTestNetworkSettings();
#endif
            if (!hasNetworkServer) {
                qInfo("No network test server: skipping QTcpSocket part of test.");
                continue;
            }
            socket.connectToHost(QtNetworkSettings::httpServerName(), 80);
            socket.write("GET / HTTP/1.0\r\n\r\n");
            QVERIFY(socket.waitForReadyRead());
            dev = &socket;
            result = QByteArray("HTTP");
            lineResult = "Date";
        }
        char ch, ch2;
        dev->seek(0);
        dev->getChar(&ch);
        dev->ungetChar(ch);
        QCOMPARE(dev->peek(4), result);
        dev->getChar(&ch);
        dev->getChar(&ch2);
        dev->ungetChar(ch2);
        dev->ungetChar(ch);
        QCOMPARE(dev->read(1), result.left(1));
        QCOMPARE(dev->read(3), result.right(3));

        if (i == 0)
            dev->seek(0);
        else
            dev->readLine();
        dev->getChar(&ch);
        dev->ungetChar(ch);
        dev->readLine(buf, 5);
        QCOMPARE(static_cast<const char*>(buf), lineResult);

        if (i == 1)
            socket.close();
    }
}

//--------------------------------------------------------------------
void tst_QIODevice::peek()
{
    QBuffer buffer;
    QFile::remove("peektestfile");
    QFile file("peektestfile");

    for (int i = 0; i < 2; ++i) {
        QIODevice *device = i ? (QIODevice *)&file : (QIODevice *)&buffer;

        device->open(QBuffer::ReadWrite);
        device->write("ZXCV");

        device->seek(0);
        QCOMPARE(device->peek(4), QByteArray("ZXCV"));
        QCOMPARE(device->pos(), qint64(0));
        device->write("ABCDE");
        device->seek(3);
        QCOMPARE(device->peek(1), QByteArray("D"));
        QCOMPARE(device->peek(5), QByteArray("DE"));
        device->seek(0);
        QCOMPARE(device->read(4), QByteArray("ABCD"));
        QCOMPARE(device->pos(), qint64(4));

        device->seek(0);
        device->write("ZXCV");
        device->seek(0);
        char buf[5];
        buf[4] = 0;
        device->peek(buf, 4);
        QCOMPARE(static_cast<const char *>(buf), "ZXCV");
        QCOMPARE(device->pos(), qint64(0));
        device->read(buf, 4);
        QCOMPARE(static_cast<const char *>(buf), "ZXCV");
        QCOMPARE(device->pos(), qint64(4));
    }
    QFile::remove("peektestfile");
}

void tst_QIODevice::peekAndRead()
{
    QByteArray originalData;
    for (int i=0;i<1000;i++)
        originalData += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    QBuffer buffer;
    QFile::remove("peektestfile");
    QFile file("peektestfile");

    for (int i = 0; i < 2; ++i) {
        QByteArray readData;
        QIODevice *device = i ? (QIODevice *)&file : (QIODevice *)&buffer;
        device->open(QBuffer::ReadWrite);
        device->write(originalData);
        device->seek(0);
        while (!device->atEnd()) {
            char peekIn[26];
            device->peek(peekIn, 26);
            readData += device->read(26);
        }
        QCOMPARE(readData, originalData);
    }
    QFile::remove("peektestfile");
}

void tst_QIODevice::readLine_data()
{
    QTest::addColumn<QByteArray>("data");

    QTest::newRow("0") << QByteArray("\nAA");
    QTest::newRow("1") << QByteArray("A\nAA");

    QByteArray data(9000, 'A');
    data[8193] = '\n';
    QTest::newRow("8194") << data;
    data[8193] = 'A';
    data[8192] = '\n';
    QTest::newRow("8193") << data;
    data[8192] = 'A';
    data[8191] = '\n';
    QTest::newRow("8192") << data;
    data[8191] = 'A';
    data[8190] = '\n';
    QTest::newRow("8191") << data;

    data[5999] = '\n';
    QTest::newRow("6000") << data;

    data[4095] = '\n';
    QTest::newRow("4096") << data;

    data[4094] = '\n';
    data[4095] = 'A';
    QTest::newRow("4095") << data;
}

void tst_QIODevice::readLine()
{
    QFETCH(QByteArray, data);
    QBuffer buffer(&data);
    QVERIFY(buffer.open(QIODevice::ReadWrite));
    QVERIFY(buffer.canReadLine());

    QTest::ignoreMessage(QtWarningMsg, "QIODevice::readLine (QBuffer): Called with maxSize < 2");
    QCOMPARE(buffer.readLine(nullptr, 0), qint64(-1));

    int linelen = data.indexOf('\n') + 1;
    QByteArray line;
    line.reserve(linelen + 100);

    int result = buffer.readLine(line.data(), linelen + 100);
    QCOMPARE(result, linelen);

    // try the exact length of the line (plus terminating \0)
    QVERIFY(buffer.seek(0));
    result = buffer.readLine(line.data(), linelen + 1);
    QCOMPARE(result, linelen);

    // try with a line length limit
    QVERIFY(buffer.seek(0));
    line = buffer.readLine(linelen + 100);
    QCOMPARE(line.size(), linelen);

    // try without a length limit
    QVERIFY(buffer.seek(0));
    line = buffer.readLine();
    QCOMPARE(line.size(), linelen);
}

void tst_QIODevice::readLine2_data()
{
    QTest::addColumn<QByteArray>("line");

    QTest::newRow("1024 - 4") << QByteArray(1024 - 4, 'x');
    QTest::newRow("1024 - 3") << QByteArray(1024 - 3, 'x');
    QTest::newRow("1024 - 2") << QByteArray(1024 - 2, 'x');
    QTest::newRow("1024 - 1") << QByteArray(1024 - 1, 'x');
    QTest::newRow("1024"    ) << QByteArray(1024    , 'x');
    QTest::newRow("1024 + 1") << QByteArray(1024 + 1, 'x');
    QTest::newRow("1024 + 2") << QByteArray(1024 + 2, 'x');

    QTest::newRow("4096 - 4") << QByteArray(4096 - 4, 'x');
    QTest::newRow("4096 - 3") << QByteArray(4096 - 3, 'x');
    QTest::newRow("4096 - 2") << QByteArray(4096 - 2, 'x');
    QTest::newRow("4096 - 1") << QByteArray(4096 - 1, 'x');
    QTest::newRow("4096"    ) << QByteArray(4096    , 'x');
    QTest::newRow("4096 + 1") << QByteArray(4096 + 1, 'x');
    QTest::newRow("4096 + 2") << QByteArray(4096 + 2, 'x');

    QTest::newRow("8192 - 4") << QByteArray(8192 - 4, 'x');
    QTest::newRow("8192 - 3") << QByteArray(8192 - 3, 'x');
    QTest::newRow("8192 - 2") << QByteArray(8192 - 2, 'x');
    QTest::newRow("8192 - 1") << QByteArray(8192 - 1, 'x');
    QTest::newRow("8192"    ) << QByteArray(8192    , 'x');
    QTest::newRow("8192 + 1") << QByteArray(8192 + 1, 'x');
    QTest::newRow("8192 + 2") << QByteArray(8192 + 2, 'x');

    QTest::newRow("16384 - 4") << QByteArray(16384 - 4, 'x');
    QTest::newRow("16384 - 3") << QByteArray(16384 - 3, 'x');
    QTest::newRow("16384 - 2") << QByteArray(16384 - 2, 'x');
    QTest::newRow("16384 - 1") << QByteArray(16384 - 1, 'x');
    QTest::newRow("16384"    ) << QByteArray(16384    , 'x');
    QTest::newRow("16384 + 1") << QByteArray(16384 + 1, 'x');
    QTest::newRow("16384 + 2") << QByteArray(16384 + 2, 'x');

    QTest::newRow("20000") << QByteArray(20000, 'x');

    QTest::newRow("32768 - 4") << QByteArray(32768 - 4, 'x');
    QTest::newRow("32768 - 3") << QByteArray(32768 - 3, 'x');
    QTest::newRow("32768 - 2") << QByteArray(32768 - 2, 'x');
    QTest::newRow("32768 - 1") << QByteArray(32768 - 1, 'x');
    QTest::newRow("32768"    ) << QByteArray(32768    , 'x');
    QTest::newRow("32768 + 1") << QByteArray(32768 + 1, 'x');
    QTest::newRow("32768 + 2") << QByteArray(32768 + 2, 'x');

    QTest::newRow("40000") << QByteArray(40000, 'x');
}

void tst_QIODevice::readLine2()
{
    QFETCH(QByteArray, line);

    int length = line.size();

    QByteArray data("First line.\r\n");
    data.append(line);
    data.append("\r\n");
    data.append(line);
    data.append("\r\n");
    data.append("\r\n0123456789");

    {
        QBuffer buffer(&data);
        buffer.open(QIODevice::ReadOnly);

        buffer.seek(0);
        QByteArray temp;
        temp.resize(64536);
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(13));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(length + 2));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(length + 2));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(2));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(10));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(-1));

        buffer.seek(0);
        QCOMPARE(buffer.readLine().size(), 13);
        QCOMPARE(buffer.readLine().size(), length + 2);
        QCOMPARE(buffer.readLine().size(), length + 2);
        QCOMPARE(buffer.readLine().size(), 2);
        QCOMPARE(buffer.readLine().size(), 10);
        QVERIFY(buffer.readLine().isNull());
    }

    {
        QBuffer buffer(&data);
        buffer.open(QIODevice::ReadOnly | QIODevice::Text);

        buffer.seek(0);
        QByteArray temp;
        temp.resize(64536);
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(12));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(length + 1));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(length + 1));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(1));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(10));
        QCOMPARE(buffer.readLine(temp.data(), temp.size()), qint64(-1));

        buffer.seek(0);
        QCOMPARE(buffer.readLine().size(), 12);
        QCOMPARE(buffer.readLine().size(), length + 1);
        QCOMPARE(buffer.readLine().size(), length + 1);
        QCOMPARE(buffer.readLine().size(), 1);
        QCOMPARE(buffer.readLine().size(), 10);
        QVERIFY(buffer.readLine().isNull());
    }
}

class SequentialReadBuffer : public QIODevice
{
public:
    SequentialReadBuffer(const char *data)
        : QIODevice(), buf(new QByteArray(data)), offset(0), ownbuf(true) { }
    SequentialReadBuffer(QByteArray *byteArray)
        : QIODevice(), buf(byteArray), offset(0), ownbuf(false) { }
    virtual ~SequentialReadBuffer() { if (ownbuf) delete buf; }

    bool isSequential() const override { return true; }
    const QByteArray &buffer() const { return *buf; }

protected:
    qint64 readData(char *data, qint64 maxSize) override
    {
        maxSize = qMin(maxSize, qint64(buf->size() - offset));
        if (maxSize > 0)
            memcpy(data, buf->constData() + offset, maxSize);
        offset += maxSize;
        return maxSize;
    }
    qint64 writeData(const char * /* data */, qint64 /* maxSize */) override
    {
        return -1;
    }

private:
    QByteArray *buf;
    int offset;
    bool ownbuf;
};

// Test readAll() on position change for sequential device
void tst_QIODevice::readAllKeepPosition()
{
    SequentialReadBuffer buffer("Hello world!");
    buffer.open(QIODevice::ReadOnly);
    char c;

    QCOMPARE(buffer.readChannelCount(), 1);
    QCOMPARE(buffer.writeChannelCount(), 0);
    QVERIFY(buffer.getChar(&c));
    QCOMPARE(buffer.pos(), qint64(0));
    buffer.ungetChar(c);
    QCOMPARE(buffer.pos(), qint64(0));

    QByteArray resultArray = buffer.readAll();
    QCOMPARE(buffer.pos(), qint64(0));
    QCOMPARE(resultArray, buffer.buffer());
}

class RandomAccessBuffer : public QIODevice
{
public:
    RandomAccessBuffer(const char *data) : QIODevice(), buf(data) { }

protected:
    qint64 readData(char *data, qint64 maxSize) override
    {
        maxSize = qMin(maxSize, qint64(buf.size() - pos()));
        if (maxSize > 0)
            memcpy(data, buf.constData() + pos(), maxSize);
        return maxSize;
    }
    qint64 writeData(const char *data, qint64 maxSize) override
    {
        maxSize = qMin(maxSize, qint64(buf.size() - pos()));
        if (maxSize > 0)
            memcpy(buf.data() + pos(), data, maxSize);
        return maxSize;
    }

private:
    QByteArray buf;
};

// Test write() on skipping correct number of bytes in read buffer
void tst_QIODevice::writeInTextMode()
{
    // Unlike other platforms, Windows implementation expands '\n' into
    // "\r\n" sequence in write(). Ensure that write() properly works with
    // a read buffer on random-access devices.
#ifndef Q_OS_WIN
    QSKIP("This is a Windows-only test");
#else
    RandomAccessBuffer buffer("one\r\ntwo\r\nthree\r\n");
    buffer.open(QBuffer::ReadWrite | QBuffer::Text);
    QCOMPARE(buffer.readLine(), QByteArray("one\n"));
    QCOMPARE(buffer.write("two\n"), 4);
    QCOMPARE(buffer.readLine(), QByteArray("three\n"));
#endif
}

void tst_QIODevice::skip_data()
{
    QTest::addColumn<bool>("sequential");
    QTest::addColumn<QByteArray>("data");
    QTest::addColumn<int>("read");
    QTest::addColumn<int>("skip");
    QTest::addColumn<int>("skipped");
    QTest::addColumn<char>("expect");

    QByteArray bigData;
    bigData.fill('a', 20000);
    bigData[10001] = 'x';

    bool sequential = true;
    do {
        QByteArray devName(sequential ? "sequential" : "random-access");

        QTest::newRow(qPrintable(devName + "-small_data")) << sequential
                                                           << QByteArray("abcdefghij")
                                                           << 3 << 6 << 6 << 'j';
        QTest::newRow(qPrintable(devName + "-big_data")) << sequential << bigData
                                                         << 1 << 10000 << 10000 << 'x';
        QTest::newRow(qPrintable(devName + "-beyond_the_end")) << sequential << bigData
                                                               << 1 << 20000 << 19999 << '\0';

        sequential = !sequential;
    } while (!sequential);
}

void tst_QIODevice::skip()
{
    QFETCH(bool, sequential);
    QFETCH(QByteArray, data);
    QFETCH(int, read);
    QFETCH(int, skip);
    QFETCH(int, skipped);
    QFETCH(char, expect);
    char lastChar = 0;

    QScopedPointer<QIODevice> dev(sequential ? (QIODevice *) new SequentialReadBuffer(&data)
                                             : (QIODevice *) new QBuffer(&data));
    dev->open(QIODevice::ReadOnly);

    for (int i = 0; i < read; ++i)
        dev->getChar(nullptr);

    QCOMPARE(dev->skip(skip), skipped);
    dev->getChar(&lastChar);
    QCOMPARE(lastChar, expect);
}

void tst_QIODevice::skipAfterPeek_data()
{
    QTest::addColumn<bool>("sequential");
    QTest::addColumn<QByteArray>("data");

    QByteArray bigData;
    for (int i = 0; i < 1000; ++i)
        bigData += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    QTest::newRow("sequential") << true  << bigData;
    QTest::newRow("random-access") << false << bigData;
}

void tst_QIODevice::skipAfterPeek()
{
    QFETCH(bool, sequential);
    QFETCH(QByteArray, data);

    QScopedPointer<QIODevice> dev(sequential ? (QIODevice *) new SequentialReadBuffer(&data)
                                             : (QIODevice *) new QBuffer(&data));
    int readSoFar = 0;
    qint64 bytesToSkip = 1;

    dev->open(QIODevice::ReadOnly);
    forever {
        QByteArray chunk = dev->peek(bytesToSkip);
        if (chunk.isEmpty())
            break;

        QCOMPARE(dev->skip(bytesToSkip), qint64(chunk.size()));
        QCOMPARE(chunk, data.mid(readSoFar, chunk.size()));
        readSoFar += chunk.size();
        bytesToSkip <<= 1;
    }
    QCOMPARE(readSoFar, data.size());
}

void tst_QIODevice::transaction_data()
{
    QTest::addColumn<bool>("sequential");
    QTest::addColumn<qint8>("i8Data");
    QTest::addColumn<qint16>("i16Data");
    QTest::addColumn<qint32>("i32Data");
    QTest::addColumn<qint64>("i64Data");
    QTest::addColumn<bool>("bData");
    QTest::addColumn<float>("fData");
    QTest::addColumn<double>("dData");
    QTest::addColumn<QByteArray>("strData");

    bool sequential = true;
    do {
        QByteArray devName(sequential ? "sequential" : "random-access");

        QTest::newRow(qPrintable(devName + '1')) << sequential << qint8(1) << qint16(2)
                                                 << qint32(3) << qint64(4) << true
                                                 << 5.0f << double(6.0)
                                                 << QByteArray("Hello world!");
        QTest::newRow(qPrintable(devName + '2')) << sequential << qint8(1 << 6) << qint16(1 << 14)
                                                 << qint32(1 << 30) << (qint64(1) << 62) << false
                                                 << 123.0f << double(234.0)
                                                 << QByteArray("abcdefghijklmnopqrstuvwxyz");
        QTest::newRow(qPrintable(devName + '3')) << sequential << qint8(-1) << qint16(-2)
                                                 << qint32(-3) << qint64(-4) << true
                                                 << -123.0f << double(-234.0)
                                                 << QByteArray("Qt rocks!");
        sequential = !sequential;
    } while (!sequential);
}

// Test transaction integrity
void tst_QIODevice::transaction()
{
    QByteArray testBuffer;

    QFETCH(bool, sequential);
    QFETCH(qint8, i8Data);
    QFETCH(qint16, i16Data);
    QFETCH(qint32, i32Data);
    QFETCH(qint64, i64Data);
    QFETCH(bool, bData);
    QFETCH(float, fData);
    QFETCH(double, dData);
    QFETCH(QByteArray, strData);

    {
        QDataStream stream(&testBuffer, QIODevice::WriteOnly);

        stream << i8Data << i16Data << i32Data << i64Data
               << bData << fData << dData << strData.constData();
    }

    for (int splitPos = 0; splitPos <= testBuffer.size(); ++splitPos) {
        QByteArray readBuffer(testBuffer.left(splitPos));
        QIODevice *dev = sequential ? (QIODevice *) new SequentialReadBuffer(&readBuffer)
                                    : (QIODevice *) new QBuffer(&readBuffer);
        dev->open(QIODevice::ReadOnly);
        QDataStream stream(dev);

        qint8 i8;
        qint16 i16;
        qint32 i32;
        qint64 i64;
        bool b;
        float f;
        double d;
        char *str;

        forever {
            QVERIFY(!dev->isTransactionStarted());
            dev->startTransaction();
            QVERIFY(dev->isTransactionStarted());

            // Try to read all data in one go. If the status of the data stream
            // indicates an unsuccessful operation, restart a read transaction
            // on the completed buffer.
            stream >> i8 >> i16 >> i32 >> i64 >> b >> f >> d >> str;

            QVERIFY(stream.atEnd());
            if (stream.status() == QDataStream::Ok) {
                dev->commitTransaction();
                break;
            }

            dev->rollbackTransaction();
            QVERIFY(splitPos == 0 || !stream.atEnd());
            QCOMPARE(dev->pos(), Q_INT64_C(0));
            QCOMPARE(dev->bytesAvailable(), qint64(readBuffer.size()));
            QVERIFY(readBuffer.size() < testBuffer.size());
            delete [] str;
            readBuffer.append(testBuffer.right(testBuffer.size() - splitPos));
            stream.resetStatus();
        }

        QVERIFY(!dev->isTransactionStarted());
        QVERIFY(stream.atEnd());
        QCOMPARE(i8, i8Data);
        QCOMPARE(i16, i16Data);
        QCOMPARE(i32, i32Data);
        QCOMPARE(i64, i64Data);
        QCOMPARE(b, bData);
        QCOMPARE(f, fData);
        QCOMPARE(d, dData);
        QVERIFY(strData == str);
        delete [] str;
        stream.setDevice(0);
        delete dev;
    }
}

QTEST_MAIN(tst_QIODevice)
#include "tst_qiodevice.moc"