summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qabstractfileengine/tst_qabstractfileengine.cpp
blob: edb5ec05a5225a431b4f0e0097363b5d0f716812 (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
853
854
855
856
857
858
859
860
861
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the FOO module 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/private/qabstractfileengine_p.h>
#include <QtCore/private/qfsfileengine_p.h>

#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/QSharedPointer>
#include <QtCore/QScopedPointer>
#include <QtCore/QHash>
#include <QtCore/QDir>
#include <QtCore/QDirIterator>

#include <QtTest/QTest>

#include <QtCore/QDebug>
#include "../../../../shared/filesystem.h"

class tst_QAbstractFileEngine
    : public QObject
{
    Q_OBJECT
public slots:
    void initTestCase();
    void cleanupTestCase();

private slots:
    void customHandler();

    void fileIO_data();
    void fileIO();

    void mounting_data();
    void mounting();
private:
    QStringList filesForRemoval;
    QSharedPointer<QTemporaryDir> m_currentDir;
    QString m_previousCurrent;
};

class ReferenceFileEngine
    : public QAbstractFileEngine
{
public:
    ReferenceFileEngine(const QString &fileName)
        : fileName_(QDir::cleanPath(fileName))
        , position_(-1)
        , openForRead_(false)
        , openForWrite_(false)
    {
    }

    bool open(QIODevice::OpenMode openMode, std::optional<QFile::Permissions> permissions) override
    {
        Q_UNUSED(permissions);

        if (openForRead_ || openForWrite_) {
            qWarning("%s: file is already open for %s",
                     Q_FUNC_INFO,
                     (openForRead_ ? "reading" : "writing"));
            return false;
        }

        openFile_ = resolveFile(openMode & QIODevice::WriteOnly);
        if (!openFile_)
            return false;

        position_ = 0;
        if (openMode & QIODevice::ReadOnly)
            openForRead_ = true;

        if (openMode & QIODevice::WriteOnly) {
            openForWrite_ = true;

            QMutexLocker lock(&openFile_->mutex);
            if (openMode & QIODevice::Truncate
                    || !(openForRead_ || openMode & QIODevice::Append))
                openFile_->content.clear();

            if (openMode & QIODevice::Append)
                position_ = openFile_->content.size();
        }

        return true;
    }

    bool close() override
    {
        openFile_.clear();

        openForRead_ = false;
        openForWrite_ = false;
        position_ = -1;

        return true;
    }

    qint64 size() const override
    {
        QSharedPointer<File> file = resolveFile(false);
        if (!file)
            return 0;

        QMutexLocker lock(&file->mutex);
        return file->content.size();
    }

    qint64 pos() const override
    {
        if (!openForRead_ && !openForWrite_) {
            qWarning("%s: file is not open", Q_FUNC_INFO);
            return -1;
        }
        return position_;
    }

    bool seek(qint64 pos) override
    {
        if (!openForRead_ && !openForWrite_) {
            qWarning("%s: file is not open", Q_FUNC_INFO);
            return false;
        }

        if (pos >= 0) {
            position_ = pos;
            return true;
        }

        return false;
    }

    bool flush() override
    {
        if (!openForRead_ && !openForWrite_) {
            qWarning("%s: file is not open", Q_FUNC_INFO);
            return false;
        }

        return true;
    }

    bool remove() override
    {
        QMutexLocker lock(&fileSystemMutex);
        int count = fileSystem.remove(fileName_);

        return (count == 1);
    }

    bool copy(const QString &newName) override
    {
        QMutexLocker lock(&fileSystemMutex);
        if (!fileSystem.contains(fileName_)
                || fileSystem.contains(newName))
            return false;

        fileSystem.insert(newName, fileSystem.value(fileName_));
        return true;
    }

    bool rename(const QString &newName) override
    {
        QMutexLocker lock(&fileSystemMutex);
        if (!fileSystem.contains(fileName_)
                || fileSystem.contains(newName))
            return false;

        fileSystem.insert(newName, fileSystem.take(fileName_));
        return true;
    }

    bool setSize(qint64 size) override
    {
        if (size < 0)
            return false;

        QSharedPointer<File> file = resolveFile(false);
        if (!file)
            return false;

        QMutexLocker lock(&file->mutex);
        file->content.resize(size);

        if (openForRead_ || openForWrite_)
            if (position_ > size)
                position_ = size;

        return (file->content.size() == size);
    }

    FileFlags fileFlags(FileFlags type) const override
    {
        QSharedPointer<File> file = resolveFile(false);
        if (file) {
            QMutexLocker lock(&file->mutex);
            return (file->fileFlags & type);
        }

        return FileFlags();
    }

    QString fileName(FileName file) const override
    {
        switch (file) {
            case DefaultName:
                return QLatin1String("DefaultName");
            case BaseName:
                return QLatin1String("BaseName");
            case PathName:
                return QLatin1String("PathName");
            case AbsoluteName:
                return QLatin1String("AbsoluteName");
            case AbsolutePathName:
                return QLatin1String("AbsolutePathName");
            case AbsoluteLinkTarget:
                return QLatin1String("AbsoluteLinkTarget");
            case CanonicalName:
                return QLatin1String("CanonicalName");
            case CanonicalPathName:
                return QLatin1String("CanonicalPathName");
            case BundleName:
                return QLatin1String("BundleName");

            default:
                break;
        }

        return QString();
    }

    uint ownerId(FileOwner owner) const override
    {
        QSharedPointer<File> file = resolveFile(false);
        if (file) {
            switch (owner) {
                case OwnerUser:
                {
                    QMutexLocker lock(&file->mutex);
                    return file->userId;
                }
                case OwnerGroup:
                {
                    QMutexLocker lock(&file->mutex);
                    return file->groupId;
                }
            }
        }

        return -2;
    }

    QString owner(FileOwner owner) const override
    {
        QSharedPointer<File> file = resolveFile(false);
        if (file) {
            uint ownerId;
            switch (owner) {
                case OwnerUser:
                {
                    QMutexLocker lock(&file->mutex);
                    ownerId = file->userId;
                }

                {
                    QMutexLocker lock(&fileSystemMutex);
                    return fileSystemUsers.value(ownerId);
                }

                case OwnerGroup:
                {
                    QMutexLocker lock(&file->mutex);
                    ownerId = file->groupId;
                }

                {
                    QMutexLocker lock(&fileSystemMutex);
                    return fileSystemGroups.value(ownerId);
                }
            }
        }

        return QString();
    }

    QDateTime fileTime(FileTime time) const override
    {
        QSharedPointer<File> file = resolveFile(false);
        if (file) {
            QMutexLocker lock(&file->mutex);
            switch (time) {
                case BirthTime:
                    return file->birth;
                case MetadataChangeTime:
                    return file->change;
                case ModificationTime:
                    return file->modification;
                case AccessTime:
                    return file->access;
            }
        }

        return QDateTime();
    }

    void setFileName(const QString &file) override
    {
        if (openForRead_ || openForWrite_)
            qWarning("%s: Can't set file name while file is open", Q_FUNC_INFO);
        else
            fileName_ = file;
    }

    qint64 read(char *data, qint64 maxLen) override
    {
        if (!openForRead_) {
            qWarning("%s: file must be open for reading", Q_FUNC_INFO);
            return -1;
        }

        if (openFile_.isNull()) {
            qWarning("%s: file must not be null", Q_FUNC_INFO);
            return -1;
        }

        QMutexLocker lock(&openFile_->mutex);
        qint64 readSize = qMin(openFile_->content.size() - position_, maxLen);
        if (readSize < 0)
            return -1;

        memcpy(data, openFile_->content.constData() + position_, readSize);
        position_ += readSize;

        return readSize;
    }

    qint64 write(const char *data, qint64 length) override
    {
        if (!openForWrite_) {
            qWarning("%s: file must be open for writing", Q_FUNC_INFO);
            return -1;
        }

        if (openFile_.isNull()) {
            qWarning("%s: file must not be null", Q_FUNC_INFO);
            return -1;
        }

        if (length < 0)
            return -1;

        QMutexLocker lock(&openFile_->mutex);
        if (openFile_->content.size() == position_)
            openFile_->content.append(data, length);
        else {
            if (position_ + length > openFile_->content.size())
                openFile_->content.resize(position_ + length);
            openFile_->content.replace(position_, length, data, length);
        }

        qint64 writeSize = qMin(length, openFile_->content.size() - position_);
        position_ += writeSize;

        return writeSize;
    }

protected:
    // void setError(QFile::FileError error, const QString &str);

    struct File
    {
        File()
            : userId(0)
            , groupId(0)
            , fileFlags(
                    ReadOwnerPerm | WriteOwnerPerm | ExeOwnerPerm
                    | ReadUserPerm | WriteUserPerm | ExeUserPerm
                    | ReadGroupPerm | WriteGroupPerm | ExeGroupPerm
                    | ReadOtherPerm | WriteOtherPerm | ExeOtherPerm
                    | FileType | ExistsFlag)
        {
        }

        QMutex mutex;

        uint userId, groupId;
        QAbstractFileEngine::FileFlags fileFlags;
        QDateTime birth, change, modification, access;

        QByteArray content;
    };

    QSharedPointer<File> resolveFile(bool create) const
    {
        if (openForRead_ || openForWrite_) {
            if (!openFile_)
                qWarning("%s: file should not be null", Q_FUNC_INFO);
            return openFile_;
        }

        QMutexLocker lock(&fileSystemMutex);
        if (create) {
            QSharedPointer<File> &p = fileSystem[fileName_];
            if (p.isNull())
                p = QSharedPointer<File>::create();
            return p;
        }

        return fileSystem.value(fileName_);
    }

    static QMutex fileSystemMutex;
    static QHash<uint, QString> fileSystemUsers, fileSystemGroups;
    static QHash<QString, QSharedPointer<File> > fileSystem;

private:
    QString fileName_;
    qint64 position_;
    bool openForRead_;
    bool openForWrite_;

    mutable QSharedPointer<File> openFile_;
};

class MountingFileEngine : public QFSFileEngine
{
public:
    class Iterator : public QAbstractFileEngineIterator
    {
    public:
        Iterator(QDir::Filters filters, const QStringList &filterNames)
            : QAbstractFileEngineIterator(filters, filterNames)
        {
            names.append("foo");
            names.append("bar");
            index = -1;
        }
        QString currentFileName() const override
        {
            return names.at(index);
        }
        bool hasNext() const override
        {
            return index < names.size() - 1;
        }
        QString next() override
        {
            if (!hasNext())
                return QString();
            ++index;
            return currentFilePath();
        }
        QStringList names;
        int index;
    };
    MountingFileEngine(QString fileName)
        : QFSFileEngine(fileName)
    {
    }
    Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames) override
    {
        return new Iterator(filters, filterNames);
    }
    FileFlags fileFlags(FileFlags type) const override
    {
        if (fileName(DefaultName).endsWith(".tar")) {
            FileFlags ret = QFSFileEngine::fileFlags(type);
            //make this file in file system appear to be a directory
            ret &= ~FileType;
            ret |= DirectoryType;
            return ret;
        } else {
            //file inside the archive
            return ExistsFlag | FileType;
        }
    }
};

QMutex ReferenceFileEngine::fileSystemMutex;
QHash<uint, QString> ReferenceFileEngine::fileSystemUsers, ReferenceFileEngine::fileSystemGroups;
QHash<QString, QSharedPointer<ReferenceFileEngine::File> > ReferenceFileEngine::fileSystem;

class FileEngineHandler
    : QAbstractFileEngineHandler
{
    QAbstractFileEngine *create(const QString &fileName) const override
    {
        if (fileName.endsWith(".tar") || fileName.contains(".tar/"))
            return new MountingFileEngine(fileName);
        if (fileName.startsWith("QFSFileEngine:"))
            return new QFSFileEngine(fileName.mid(14));
        if (fileName.startsWith("reference-file-engine:"))
            return new ReferenceFileEngine(fileName.mid(22));
        if (fileName.startsWith("resource:"))
            return QAbstractFileEngine::create(QLatin1String(":/tst_qabstractfileengine/resources/") + fileName.mid(9));
        return 0;
    }
};

void tst_QAbstractFileEngine::initTestCase()
{
    m_previousCurrent = QDir::currentPath();
    m_currentDir = QSharedPointer<QTemporaryDir>::create();
    QVERIFY2(!m_currentDir.isNull(), qPrintable("Could not create current directory."));
    QDir::setCurrent(m_currentDir->path());
}

void tst_QAbstractFileEngine::cleanupTestCase()
{
    bool failed = false;

    FileEngineHandler handler;
    Q_FOREACH(QString file, filesForRemoval)
        if (!QFile::remove(file)
                || QFile::exists(file)) {
            failed = true;
            qDebug() << "Couldn't remove file:" << file;
        }

    QVERIFY(!failed);

    QDir::setCurrent(m_previousCurrent);
}

void tst_QAbstractFileEngine::customHandler()
{
    QScopedPointer<QAbstractFileEngine> file;
    {
        file.reset(QAbstractFileEngine::create("resource:file.txt"));

        QVERIFY(file);
    }

    {
        FileEngineHandler handler;

        QFile file("resource:file.txt");
        QVERIFY(file.exists());
    }

    {
        QFile file("resource:file.txt");
        QVERIFY(!file.exists());
    }
}

void tst_QAbstractFileEngine::fileIO_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::addColumn<QByteArray>("readContent");
    QTest::addColumn<QByteArray>("writeContent");
    QTest::addColumn<bool>("fileExists");

    QString resourceTxtFile(":/tst_qabstractfileengine/resources/file.txt");
    QByteArray readContent("This is a simple text file.\n");
    QByteArray writeContent("This contains two lines of text.\n");

    QTest::newRow("resource") << resourceTxtFile << readContent << QByteArray() << true;
    QTest::newRow("native") << "native-file.txt" << readContent << writeContent << false;
    QTest::newRow("Forced QFSFileEngine") << "QFSFileEngine:QFSFileEngine-file.txt" << readContent << writeContent << false;
    QTest::newRow("Custom FE") << "reference-file-engine:file.txt" << readContent << writeContent << false;

    QTest::newRow("Forced QFSFileEngine (native)") << "QFSFileEngine:native-file.txt" << readContent << writeContent << true;
    QTest::newRow("native (Forced QFSFileEngine)") << "QFSFileEngine-file.txt" << readContent << writeContent << true;
    QTest::newRow("Custom FE (2)") << "reference-file-engine:file.txt" << readContent << writeContent << true;
}

void tst_QAbstractFileEngine::fileIO()
{
    QFETCH(QString, fileName);
    QFETCH(QByteArray, readContent);
    QFETCH(QByteArray, writeContent);
    QFETCH(bool, fileExists);

    FileEngineHandler handler;


    {
        QFile file(fileName);
        QCOMPARE(file.exists(), fileExists);

        if (!fileExists) {
            QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Unbuffered));
            filesForRemoval.append(fileName);

            QCOMPARE(file.write(readContent), qint64(readContent.size()));
        }
    }

    //
    // File content is: readContent
    //

    qint64 fileSize = readContent.size();
    {
        // Reading
        QFile file(fileName);
        QVERIFY(!file.isOpen());

        /* For an exact match, this test requires the repository to
         * be checked out with UNIX-style line endings on Windows.
         * Try to succeed also for the common case of checking out with autocrlf
         * by reading the file as text and checking if the size matches
         * the original size + the '\r' characters added by autocrlf. */

        QFile::OpenMode openMode = QIODevice::ReadOnly | QIODevice::Unbuffered;
#ifdef Q_OS_WIN
        openMode |= QIODevice::Text;
#endif
        QVERIFY(file.open(openMode));
        QVERIFY(file.isOpen());
#ifdef Q_OS_WIN
        const qint64 convertedSize = fileSize + readContent.count('\n');
        if (file.size() == convertedSize)
            fileSize = convertedSize;
#endif
        QCOMPARE(file.size(), fileSize);
        QCOMPARE(file.pos(), qint64(0));

        QCOMPARE(file.size(), fileSize);
        QCOMPARE(file.readAll(), readContent);
        QCOMPARE(file.pos(), fileSize);

        file.close();
        QVERIFY(!file.isOpen());
        QCOMPARE(file.size(), fileSize);
    }

    if (writeContent.isEmpty())
        return;

    {
        // Writing / appending
        QFile file(fileName);

        QVERIFY(!file.isOpen());
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered));

        QVERIFY(file.isOpen());
        QCOMPARE(file.size(), fileSize);
        QCOMPARE(file.pos(), fileSize);

        QCOMPARE(file.write(writeContent), qint64(writeContent.size()));

        fileSize += writeContent.size();
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        file.close();
        QVERIFY(!file.isOpen());
        QCOMPARE(file.size(), fileSize);
    }

    //
    // File content is: readContent + writeContent
    //

    {
        // Reading and Writing
        QFile file(fileName);

        QVERIFY(!file.isOpen());
        QVERIFY(file.open(QIODevice::ReadWrite | QIODevice::Unbuffered));

        QVERIFY(file.isOpen());
        QCOMPARE(file.size(), fileSize);
        QCOMPARE(file.pos(), qint64(0));

        QCOMPARE(file.readAll(), readContent + writeContent);
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        QVERIFY(file.seek(writeContent.size()));
        QCOMPARE(file.pos(), qint64(writeContent.size()));
        QCOMPARE(file.size(), fileSize);

        QCOMPARE(file.write(readContent), qint64(readContent.size()));
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        QVERIFY(file.seek(0));
        QCOMPARE(file.pos(), qint64(0));
        QCOMPARE(file.size(), fileSize);

        QCOMPARE(file.write(writeContent), qint64(writeContent.size()));
        QCOMPARE(file.pos(), qint64(writeContent.size()));
        QCOMPARE(file.size(), fileSize);

        QVERIFY(file.seek(0));
        QCOMPARE(file.read(writeContent.size()), writeContent);
        QCOMPARE(file.pos(), qint64(writeContent.size()));
        QCOMPARE(file.size(), fileSize);

        QCOMPARE(file.readAll(), readContent);
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        file.close();
        QVERIFY(!file.isOpen());
        QCOMPARE(file.size(), fileSize);
    }

    //
    // File content is: writeContent + readContent
    //

    {
        // Writing
        QFile file(fileName);

        QVERIFY(!file.isOpen());
        QVERIFY(file.open(QIODevice::ReadWrite | QIODevice::Unbuffered));

        QVERIFY(file.isOpen());
        QCOMPARE(file.size(), fileSize);
        QCOMPARE(file.pos(), qint64(0));

        QCOMPARE(file.write(writeContent), qint64(writeContent.size()));
        QCOMPARE(file.pos(), qint64(writeContent.size()));
        QCOMPARE(file.size(), fileSize);

        QVERIFY(file.resize(writeContent.size()));
        QCOMPARE(file.size(), qint64(writeContent.size()));

        file.close();
        QVERIFY(!file.isOpen());
        QCOMPARE(file.size(), qint64(writeContent.size()));

        QVERIFY(file.resize(fileSize));
        QCOMPARE(file.size(), fileSize);
    }

    //
    // File content is: writeContent + <undefined>
    // File size is   : (readContent + writeContent).size()
    //

    {
        // Writing / extending
        QFile file(fileName);

        QVERIFY(!file.isOpen());
        QVERIFY(file.open(QIODevice::ReadWrite | QIODevice::Unbuffered));

        QVERIFY(file.isOpen());
        QCOMPARE(file.size(), fileSize);
        QCOMPARE(file.pos(), qint64(0));

        QVERIFY(file.seek(1024));
        QCOMPARE(file.pos(), qint64(1024));
        QCOMPARE(file.size(), fileSize);

        fileSize = 1024 + writeContent.size();
        QCOMPARE(file.write(writeContent), qint64(writeContent.size()));
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        QVERIFY(file.seek(1028));
        QCOMPARE(file.pos(), qint64(1028));
        QCOMPARE(file.size(), fileSize);

        fileSize = 1028 + writeContent.size();
        QCOMPARE(file.write(writeContent), qint64(writeContent.size()));
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        file.close();
        QVERIFY(!file.isOpen());
        QCOMPARE(file.size(), fileSize);
    }

    //
    // File content is: writeContent + <undefined> + writeContent
    // File size is   : 1024 + writeContent.size()
    //

    {
        // Writing / truncating
        QFile file(fileName);

        QVERIFY(!file.isOpen());
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered));

        QVERIFY(file.isOpen());
        QCOMPARE(file.size(), qint64(0));
        QCOMPARE(file.pos(), qint64(0));

        fileSize = readContent.size();
        QCOMPARE(file.write(readContent), fileSize);
        QCOMPARE(file.pos(), fileSize);
        QCOMPARE(file.size(), fileSize);

        file.close();
        QVERIFY(!file.isOpen());
        QCOMPARE(file.size(), fileSize);
    }

    //
    // File content is: readContent
    //
}

void tst_QAbstractFileEngine::mounting_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::newRow("native") << "test.tar";
    QTest::newRow("Forced QFSFileEngine") << "QFSFileEngine:test.tar";
}

void tst_QAbstractFileEngine::mounting()
{
    FileSystem fs;
    QVERIFY(fs.createFile("test.tar"));
    FileEngineHandler handler;

    QFETCH(QString, fileName);
    const QString absName = fs.absoluteFilePath(fileName);

    QVERIFY(QFileInfo(absName).isDir());
    QDir dir(absName);
    QCOMPARE(dir.entryList(), (QStringList() << "bar" << "foo"));
    QDir dir2(fs.path());
    bool found = false;
    foreach (QFileInfo info, dir2.entryInfoList()) {
        if (info.fileName() == QLatin1String("test.tar")) {
            QVERIFY(!found);
            found = true;
            QVERIFY(info.isDir());
        }
    }
    QVERIFY(found);
}

QTEST_APPLESS_MAIN(tst_QAbstractFileEngine)
#include "tst_qabstractfileengine.moc"