1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QtTest> |
30 | #include <QDebug> |
31 | #include <private/qzipwriter_p.h> |
32 | #include <private/qzipreader_p.h> |
33 | |
34 | class tst_QZip : public QObject |
35 | { |
36 | Q_OBJECT |
37 | |
38 | private slots: |
39 | void basicUnpack(); |
40 | void symlinks(); |
41 | void readTest(); |
42 | void createArchive(); |
43 | }; |
44 | |
45 | void tst_QZip::basicUnpack() |
46 | { |
47 | QZipReader zip(QFINDTESTDATA("/testdata/test.zip" ), QIODevice::ReadOnly); |
48 | QVector<QZipReader::FileInfo> files = zip.fileInfoList(); |
49 | QCOMPARE(files.count(), 2); |
50 | |
51 | QZipReader::FileInfo fi = files.at(i: 0); |
52 | QVERIFY(fi.isValid()); |
53 | QCOMPARE(fi.filePath, QString("test" )); |
54 | QCOMPARE(uint(fi.isDir), (uint) 1); |
55 | QCOMPARE(uint(fi.isFile), (uint) 0); |
56 | QCOMPARE(uint(fi.isSymLink), (uint) 0); |
57 | |
58 | QCOMPARE(fi.permissions,QFile::Permissions( QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
59 | | QFile::ReadUser | QFile::WriteUser | QFile::ExeUser )); |
60 | |
61 | QCOMPARE(fi.lastModified, QDateTime::fromString("2005.11.11 13:08:02" , "yyyy.MM.dd HH:mm:ss" )); |
62 | |
63 | fi = files.at(i: 1); |
64 | QVERIFY(fi.isValid()); |
65 | QCOMPARE(fi.filePath, QString("test/test.txt" )); |
66 | QCOMPARE(uint(fi.isDir), (uint) 0); |
67 | QCOMPARE(uint(fi.isFile), (uint) 1); |
68 | QCOMPARE(uint(fi.isSymLink), (uint) 0); |
69 | |
70 | QVERIFY(fi.permissions == QFile::Permissions( QFile::ReadOwner | QFile::WriteOwner |
71 | | QFile::ReadUser | QFile::WriteUser )); |
72 | |
73 | QCOMPARE(fi.lastModified, QDateTime::fromString("2005.11.11 13:08:02" , "yyyy.MM.dd HH:mm:ss" )); |
74 | |
75 | QCOMPARE(zip.fileData("test/test.txt" ), QByteArray("content\n" )); |
76 | |
77 | fi = zip.entryInfoAt(index: -1); |
78 | QVERIFY(!fi.isValid()); |
79 | } |
80 | |
81 | void tst_QZip::symlinks() |
82 | { |
83 | QZipReader zip(QFINDTESTDATA("/testdata/symlink.zip" ), QIODevice::ReadOnly); |
84 | QVector<QZipReader::FileInfo> files = zip.fileInfoList(); |
85 | QCOMPARE(files.count(), 2); |
86 | |
87 | QZipReader::FileInfo fi = files.at(i: 0); |
88 | QVERIFY(fi.isValid()); |
89 | QCOMPARE(fi.filePath, QString("symlink" )); |
90 | QVERIFY(!fi.isDir); |
91 | QVERIFY(!fi.isFile); |
92 | QVERIFY(fi.isSymLink); |
93 | |
94 | QCOMPARE(zip.fileData("symlink" ), QByteArray("destination" )); |
95 | |
96 | fi = files.at(i: 1); |
97 | QVERIFY(fi.isValid()); |
98 | QCOMPARE(fi.filePath, QString("destination" )); |
99 | QVERIFY(!fi.isDir); |
100 | QVERIFY(fi.isFile); |
101 | QVERIFY(!fi.isSymLink); |
102 | } |
103 | |
104 | void tst_QZip::readTest() |
105 | { |
106 | QZipReader zip("foobar.zip" , QIODevice::ReadOnly); // non existing file. |
107 | QVector<QZipReader::FileInfo> files = zip.fileInfoList(); |
108 | QCOMPARE(files.count(), 0); |
109 | QByteArray b = zip.fileData(fileName: "foobar" ); |
110 | QCOMPARE(b.size(), 0); |
111 | } |
112 | |
113 | void tst_QZip::createArchive() |
114 | { |
115 | QBuffer buffer; |
116 | QZipWriter zip(&buffer); |
117 | QByteArray fileContents("simple file contents\nline2\n" ); |
118 | zip.addFile(fileName: "My Filename" , data: fileContents); |
119 | zip.close(); |
120 | QByteArray zipFile = buffer.buffer(); |
121 | |
122 | // QFile f("createArchiveTest.zip"); f.open(QIODevice::WriteOnly); f.write(zipFile); f.close(); |
123 | |
124 | QBuffer buffer2(&zipFile); |
125 | QZipReader zip2(&buffer2); |
126 | QVector<QZipReader::FileInfo> files = zip2.fileInfoList(); |
127 | QCOMPARE(files.count(), 1); |
128 | QZipReader::FileInfo file = files.at(i: 0); |
129 | QCOMPARE(file.filePath, QString("My Filename" )); |
130 | QCOMPARE(uint(file.isDir), (uint) 0); |
131 | QCOMPARE(uint(file.isFile), (uint) 1); |
132 | QCOMPARE(uint(file.isSymLink), (uint) 0); |
133 | QCOMPARE(file.permissions, QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser) ); |
134 | QCOMPARE(file.size, (long long) 27); |
135 | QCOMPARE(zip2.fileData("My Filename" ), fileContents); |
136 | } |
137 | |
138 | QTEST_MAIN(tst_QZip) |
139 | #include "tst_qzip.moc" |
140 | |