[1049] | 1 | #include "testquazipnewinfo.h"
|
---|
| 2 |
|
---|
| 3 | #include "qztest.h"
|
---|
| 4 |
|
---|
| 5 | #include <QDir>
|
---|
| 6 | #include <QFileInfo>
|
---|
| 7 | #include <QtTest/QtTest>
|
---|
| 8 |
|
---|
| 9 | #include <quazip/quazip.h>
|
---|
| 10 | #include <quazip/quazipfile.h>
|
---|
| 11 | #include <quazip/quazipnewinfo.h>
|
---|
| 12 | #include <quazip/quazipfileinfo.h>
|
---|
| 13 |
|
---|
| 14 | TestQuaZipNewInfo::TestQuaZipNewInfo(QObject *parent) :
|
---|
| 15 | QObject(parent)
|
---|
| 16 | {
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | void TestQuaZipNewInfo::setFileNTFSTimes()
|
---|
| 20 | {
|
---|
| 21 | QString zipName = "newtimes.zip";
|
---|
| 22 | QStringList testFiles;
|
---|
| 23 | testFiles << "test.txt";
|
---|
| 24 | QDir curDir;
|
---|
| 25 | if (curDir.exists(zipName)) {
|
---|
| 26 | if (!curDir.remove(zipName))
|
---|
| 27 | QFAIL("Can't remove zip file");
|
---|
| 28 | }
|
---|
| 29 | if (!createTestFiles(testFiles)) {
|
---|
| 30 | QFAIL("Can't create test file");
|
---|
| 31 | }
|
---|
| 32 | QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
|
---|
| 33 | quint64 mTicks, aTicks, cTicks;
|
---|
| 34 | {
|
---|
| 35 | // create
|
---|
| 36 | QuaZip zip(zipName);
|
---|
| 37 | QVERIFY(zip.open(QuaZip::mdCreate));
|
---|
| 38 | QuaZipFile zipFile(&zip);
|
---|
| 39 | QFileInfo fileInfo("tmp/test.txt");
|
---|
| 40 | QDateTime lm = fileInfo.lastModified().toUTC();
|
---|
| 41 | QDateTime lr = fileInfo.lastRead().toUTC();
|
---|
| 42 | QDateTime cr = fileInfo.created().toUTC();
|
---|
| 43 | mTicks = (static_cast<qint64>(base.date().daysTo(lm.date()))
|
---|
| 44 | * Q_UINT64_C(86400000)
|
---|
| 45 | + static_cast<qint64>(base.time().msecsTo(lm.time())))
|
---|
| 46 | * Q_UINT64_C(10000);
|
---|
| 47 | aTicks = (static_cast<qint64>(base.date().daysTo(lr.date()))
|
---|
| 48 | * Q_UINT64_C(86400000)
|
---|
| 49 | + static_cast<qint64>(base.time().msecsTo(lr.time())))
|
---|
| 50 | * Q_UINT64_C(10000);
|
---|
| 51 | cTicks = (static_cast<qint64>(base.date().daysTo(cr.date()))
|
---|
| 52 | * Q_UINT64_C(86400000)
|
---|
| 53 | + static_cast<qint64>(base.time().msecsTo(cr.time())))
|
---|
| 54 | * Q_UINT64_C(10000);
|
---|
| 55 | QuaZipNewInfo newInfo("test.txt", "tmp/test.txt");
|
---|
| 56 | newInfo.setFileNTFSTimes("tmp/test.txt");
|
---|
| 57 | QVERIFY(zipFile.open(QIODevice::WriteOnly, newInfo));
|
---|
| 58 | zipFile.close();
|
---|
| 59 | zip.close();
|
---|
| 60 | }
|
---|
| 61 | {
|
---|
| 62 | // check
|
---|
| 63 | QuaZip zip(zipName);
|
---|
| 64 | QVERIFY(zip.open(QuaZip::mdUnzip));
|
---|
| 65 | zip.goToFirstFile();
|
---|
| 66 | QuaZipFileInfo64 fileInfo;
|
---|
| 67 | QVERIFY(zip.getCurrentFileInfo(&fileInfo));
|
---|
| 68 | zip.close();
|
---|
| 69 | QByteArray &extra = fileInfo.extra;
|
---|
| 70 | bool ntfsFound = false;
|
---|
| 71 | int timesFound = 0;
|
---|
| 72 | for (int i = 0; i <= extra.size() - 4; ) {
|
---|
| 73 | unsigned type = static_cast<unsigned>(static_cast<unsigned char>(
|
---|
| 74 | extra.at(i)))
|
---|
| 75 | | (static_cast<unsigned>(static_cast<unsigned char>(
|
---|
| 76 | extra.at(i + 1))) << 8);
|
---|
| 77 | i += 2;
|
---|
| 78 | unsigned length = static_cast<unsigned>(static_cast<unsigned char>(
|
---|
| 79 | extra.at(i)))
|
---|
| 80 | | (static_cast<unsigned>(static_cast<unsigned char>(
|
---|
| 81 | extra.at(i + 1))) << 8);
|
---|
| 82 | i += 2;
|
---|
| 83 | if (type == 0x000Au && length >= 32) {
|
---|
| 84 | ntfsFound = true;
|
---|
| 85 | i += 4; // reserved
|
---|
| 86 | while (i <= extra.size() - 4) {
|
---|
| 87 | unsigned tag = static_cast<unsigned>(
|
---|
| 88 | static_cast<unsigned char>(extra.at(i)))
|
---|
| 89 | | (static_cast<unsigned>(
|
---|
| 90 | static_cast<unsigned char>(extra.at(i + 1)))
|
---|
| 91 | << 8);
|
---|
| 92 | i += 2;
|
---|
| 93 | unsigned tagsize = static_cast<unsigned>(
|
---|
| 94 | static_cast<unsigned char>(extra.at(i)))
|
---|
| 95 | | (static_cast<unsigned>(
|
---|
| 96 | static_cast<unsigned char>(extra.at(i + 1)))
|
---|
| 97 | << 8);
|
---|
| 98 | i += 2;
|
---|
| 99 | QCOMPARE(tagsize, static_cast<unsigned>(24));
|
---|
| 100 | if (tag == 0x0001u && tagsize >= 24) {
|
---|
| 101 | for (int timesPos = i;
|
---|
| 102 | i < timesPos + 24;
|
---|
| 103 | i += 8, tagsize -= 8) {
|
---|
| 104 | quint64 time = static_cast<quint64>(
|
---|
| 105 | static_cast<unsigned char>(extra.at(i)))
|
---|
| 106 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 107 | extra.at(i + 1))) << 8)
|
---|
| 108 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 109 | extra.at(i + 2))) << 16)
|
---|
| 110 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 111 | extra.at(i + 3))) << 24)
|
---|
| 112 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 113 | extra.at(i + 4))) << 32)
|
---|
| 114 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 115 | extra.at(i + 5))) << 40)
|
---|
| 116 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 117 | extra.at(i + 6))) << 48)
|
---|
| 118 | | (static_cast<quint64>(static_cast<unsigned char>(
|
---|
| 119 | extra.at(i + 7))) << 56);
|
---|
| 120 | ++timesFound;
|
---|
| 121 | if (i - timesPos == 0) {
|
---|
| 122 | QCOMPARE(time, mTicks);
|
---|
| 123 | } else if (i - timesPos == 8) {
|
---|
| 124 | QCOMPARE(time, aTicks);
|
---|
| 125 | } else if (i - timesPos == 16) {
|
---|
| 126 | QCOMPARE(time, cTicks);
|
---|
| 127 | } else {
|
---|
| 128 | QFAIL("Wrong position");
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | i += tagsize;
|
---|
| 132 | } else {
|
---|
| 133 | i += tagsize;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | }
|
---|
| 137 | } else {
|
---|
| 138 | i += length;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | QVERIFY(ntfsFound);
|
---|
| 142 | QCOMPARE(timesFound, 3);
|
---|
| 143 | }
|
---|
| 144 | removeTestFiles(testFiles);
|
---|
| 145 | curDir.remove(zipName);
|
---|
| 146 | }
|
---|