source: s10k/CommonLibs/quazip-0.7.2/qztest/testquazipdir.cpp@ 1099

Last change on this file since 1099 was 1096, checked in by s10k, 7 years ago

Added zlib, quazip, basicxmlsyntaxhighlighter, conditionalsemaphore and linenumberdisplay libraries. zlib and quazip are pre-compiled, but you can compile them yourself, just delete the dll files (or equivalent binary files to your OS)

File size: 13.8 KB
RevLine 
[1096]1/*
2Copyright (C) 2005-2014 Sergey A. Tachenov
3
4This file is part of QuaZIP test suite.
5
6QuaZIP is free software: you can redistribute it and/or modify
7it under the terms of the GNU Lesser General Public License as published by
8the Free Software Foundation, either version 2.1 of the License, or
9(at your option) any later version.
10
11QuaZIP is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General Public License
17along with QuaZIP. If not, see <http://www.gnu.org/licenses/>.
18
19See COPYING file for the full LGPL text.
20
21Original ZIP package is copyrighted by Gilles Vollant and contributors,
22see quazip/(un)zip.h files for details. Basically it's the zlib license.
23*/
24
25#include "testquazipdir.h"
26#include "qztest.h"
27#include <QtTest/QtTest>
28#include <quazip/quazip.h>
29#include <quazip/quazipdir.h>
30
31void TestQuaZipDir::entryList_data()
32{
33 QTest::addColumn<QString>("zipName");
34 QTest::addColumn<QStringList>("fileNames");
35 QTest::addColumn<QString>("dirName");
36 QTest::addColumn<QStringList>("nameFilters");
37 // QDir::Filters type breaks Qt meta type system on MSVC
38 QTest::addColumn<int>("filter");
39 QTest::addColumn<int>("sort");
40 QTest::addColumn<QStringList>("entries");
41 QTest::addColumn<int>("caseSensitivity");
42 QTest::newRow("simple") << "simple.zip" << (
43 QStringList() << "test0.txt" << "testdir1/test1.txt"
44 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
45 << "testdir2" << QStringList()
46 << static_cast<int>(QDir::NoFilter)
47 << static_cast<int>(QDir::Unsorted)
48 << (QStringList() << "test2.txt" << "subdir/") << -1;
49 QTest::newRow("separate dir") << "sepdir.zip" << (
50 QStringList() << "laj/" << "laj/lajfile.txt")
51 << "" << QStringList()
52 << static_cast<int>(QDir::NoFilter)
53 << static_cast<int>(QDir::Unsorted)
54 << (QStringList() << "laj/") << -1;
55 QTest::newRow("separate dir (subdir listing)") << "sepdirsub.zip" << (
56 QStringList() << "laj/" << "laj/lajfile.txt")
57 << "laj" << QStringList()
58 << static_cast<int>(QDir::NoFilter)
59 << static_cast<int>(QDir::Unsorted)
60 << (QStringList() << "lajfile.txt") << -1;
61 QTest::newRow("dirs only") << "dirsonly.zip" << (
62 QStringList() << "file" << "dir/")
63 << "" << QStringList()
64 << static_cast<int>(QDir::Dirs)
65 << static_cast<int>(QDir::Unsorted)
66 << (QStringList() << "dir/") << -1;
67 QTest::newRow("files only") << "filesonly.zip" << (
68 QStringList() << "file1" << "parent/dir/" << "parent/file2")
69 << "parent" << QStringList()
70 << static_cast<int>(QDir::Files)
71 << static_cast<int>(QDir::Unsorted)
72 << (QStringList() << "file2") << -1;
73 QTest::newRow("sorted") << "sorted.zip" << (
74 QStringList() << "file1" << "parent/subdir/" << "parent/subdir2/file3" << "parent/file2" << "parent/file0")
75 << "parent" << QStringList()
76 << static_cast<int>(QDir::NoFilter)
77 << static_cast<int>(QDir::Name)
78 << (QStringList() << "file0" << "file2" << "subdir/" << "subdir2/")
79 << -1;
80 QTest::newRow("sorted dirs first") << "sorted-dirs.zip" << (
81 QStringList() << "file1" << "parent/subdir/" << "parent/subdir2/file3" << "parent/file2" << "parent/file0")
82 << "parent" << QStringList()
83 << static_cast<int>(QDir::NoFilter)
84 << static_cast<int>(QDir::Name | QDir::DirsFirst)
85 << (QStringList() << "subdir/" << "subdir2/" << "file0" << "file2")
86 << -1;
87 QTest::newRow("sorted dirs first reversed") << "sorted-reverse.zip" << (
88 QStringList() << "file1" << "parent/subdir/" << "parent/subdir2/file3" << "parent/file2" << "parent/file0")
89 << "parent" << QStringList()
90 << static_cast<int>(QDir::NoFilter)
91 << static_cast<int>(QDir::Name | QDir::DirsFirst | QDir::Reversed)
92 << (QStringList() << "subdir2/" << "subdir/" << "file2" << "file0")
93 << -1;
94 QTest::newRow("sorted by size") << "sorted-size.zip" << (
95 QStringList() << "file000" << "file10")
96 << "/" << QStringList()
97 << static_cast<int>(QDir::NoFilter)
98 << static_cast<int>(QDir::Size)
99 << (QStringList() << "file10" << "file000") << -1;
100 QTest::newRow("sorted by time") << "sorted-time.zip" << (
101 QStringList() << "file04" << "file03" << "file02" << "subdir/subfile")
102 << "/" << QStringList()
103 << static_cast<int>(QDir::NoFilter)
104 << static_cast<int>(QDir::Time)
105 << (QStringList() << "subdir/" << "file04" << "file02" << "file03")
106 << -1;
107 QTest::newRow("sorted by type") << "sorted-type.zip" << (
108 QStringList() << "file1.txt" << "file2.dat")
109 << "/" << QStringList()
110 << static_cast<int>(QDir::NoFilter)
111 << static_cast<int>(QDir::Type)
112 << (QStringList() << "file2.dat" << "file1.txt") << -1;
113 QTest::newRow("name filter") << "name-filter.zip" << (
114 QStringList() << "file01" << "file02" << "laj")
115 << "/" << QStringList("file*")
116 << static_cast<int>(QDir::NoFilter)
117 << static_cast<int>(QDir::Name)
118 << (QStringList() << "file01" << "file02") << -1;
119 QTest::newRow("case sensitive") << "case-sensitive.zip" << (
120 QStringList() << "a" << "B")
121 << "/" << QStringList()
122 << static_cast<int>(QDir::NoFilter)
123 << static_cast<int>(QDir::Name)
124 << (QStringList() << "B" << "a")
125 << static_cast<int>(QuaZip::csSensitive);
126 QTest::newRow("case insensitive") << "case-insensitive.zip" << (
127 QStringList() << "B" << "a")
128 << "/" << QStringList()
129 << static_cast<int>(QDir::NoFilter)
130 << static_cast<int>(QDir::Name)
131 << (QStringList() << "a" << "B")
132 << static_cast<int>(QuaZip::csInsensitive);
133}
134
135void TestQuaZipDir::entryList()
136{
137 QFETCH(QString, zipName);
138 QFETCH(QStringList, fileNames);
139 QFETCH(QString, dirName);
140 QFETCH(QStringList, nameFilters);
141 QFETCH(int, filter);
142 QFETCH(int, sort);
143 QDir::Filters filters = static_cast<QDir::Filters>(filter);
144 QDir::SortFlags sorting = static_cast<QDir::SortFlags>(sort);
145 QFETCH(QStringList, entries);
146 QFETCH(int, caseSensitivity);
147 QDir curDir;
148 if (!createTestFiles(fileNames)) {
149 QFAIL("Couldn't create test files");
150 }
151 if (!createTestArchive(zipName, fileNames)) {
152 QFAIL("Couldn't create test archive");
153 }
154 removeTestFiles(fileNames);
155 QuaZip zip(zipName);
156 QVERIFY(zip.open(QuaZip::mdUnzip));
157 QuaZipDir dir(&zip, dirName);
158 QVERIFY(dir.exists());
159 if (caseSensitivity != -1) {
160 dir.setCaseSensitivity(static_cast<QuaZip::CaseSensitivity>(
161 caseSensitivity));
162 QCOMPARE(dir.caseSensitivity(), static_cast<QuaZip::CaseSensitivity>(
163 caseSensitivity));
164 }
165 QCOMPARE(dir.entryList(nameFilters, filters, sorting), entries);
166 // Test default sorting setting.
167 dir.setSorting(sorting);
168 QCOMPARE(dir.sorting(), sorting);
169 QCOMPARE(dir.entryList(nameFilters, filters), entries);
170 // Test default name filter setting.
171 dir.setNameFilters(nameFilters);
172 QCOMPARE(dir.nameFilters(), nameFilters);
173 QCOMPARE(dir.entryList(filters), entries);
174 // Test default filters.
175 dir.setFilter(filters);
176 QCOMPARE(dir.filter(), filters);
177 QCOMPARE(dir.entryList(), entries);
178 QCOMPARE(dir.entryList().count(), static_cast<int>(dir.count()));
179 zip.close();
180 curDir.remove(zipName);
181}
182
183void TestQuaZipDir::cd_data()
184{
185 QTest::addColumn<QString>("zipName");
186 QTest::addColumn<QStringList>("fileNames");
187 QTest::addColumn<QString>("dirName");
188 QTest::addColumn<QString>("targetDirName");
189 QTest::addColumn<QString>("result");
190 QTest::newRow("cdDown") << "simple.zip" << (
191 QStringList() << "cddown.txt" << "testdir1/test1.txt"
192 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
193 << "" << "testdir1" << "testdir1";
194 QTest::newRow("cdUp") << "cdup.zip" << (
195 QStringList() << "test0.txt" << "testdir1/test1.txt"
196 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
197 << "testdir1" << ".." << "";
198 QTest::newRow("cdSide") << "cdside.zip" << (
199 QStringList() << "test0.txt" << "testdir1/test1.txt"
200 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
201 << "testdir1" << "../testdir2" << "testdir2";
202 QTest::newRow("cdDownUp") << "cdside.zip" << (
203 QStringList() << "test0.txt" << "testdir1/test1.txt"
204 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
205 << "" << "testdir1/.." << "";
206 QTest::newRow("cdDeep") << "cdside.zip" << (
207 QStringList() << "test0.txt" << "testdir1/test1.txt"
208 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
209 << "" << "testdir2/subdir" << "testdir2/subdir";
210 QTest::newRow("cdDeeper") << "cdside.zip" << (
211 QStringList() << "test0.txt" << "testdir1/test1.txt"
212 << "testdir2/test2.txt" << "testdir2/subdir/subdir2/subdir3/test2sub.txt")
213 << "testdir2/subdir" << "subdir2/subdir3" << "testdir2/subdir/subdir2/subdir3";
214 QTest::newRow("cdRoot") << "cdup.zip" << (
215 QStringList() << "test0.txt" << "testdir1/test1.txt"
216 << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
217 << "testdir1" << "/" << "";
218}
219
220void TestQuaZipDir::cd()
221{
222 QFETCH(QString, zipName);
223 QFETCH(QStringList, fileNames);
224 QFETCH(QString, dirName);
225 QFETCH(QString, targetDirName);
226 QFETCH(QString, result);
227 QDir curDir;
228 if (!createTestFiles(fileNames)) {
229 QFAIL("Couldn't create test files");
230 }
231 if (!createTestArchive(zipName, fileNames)) {
232 QFAIL("Couldn't create test archive");
233 }
234 removeTestFiles(fileNames);
235 QuaZip zip(zipName);
236 QVERIFY(zip.open(QuaZip::mdUnzip));
237 QuaZipDir dir(&zip, dirName);
238 if (dirName.startsWith('/')) {
239 dirName = dirName.mid(1);
240 }
241 if (dirName.endsWith('/')) {
242 dirName.chop(1);
243 }
244 QCOMPARE(dir.path(), dirName);
245 {
246 int lastSlash = dirName.lastIndexOf('/');
247 if (lastSlash == -1) {
248 // dirName is just a single name
249 if (dirName.isEmpty()) {
250 QCOMPARE(dir.dirName(), QString::fromLatin1("."));
251 } else {
252 QCOMPARE(dir.dirName(), dirName);
253 }
254 } else {
255 // dirName is a sequence
256 QCOMPARE(dir.dirName(), dirName.mid(lastSlash + 1));
257 }
258 }
259 if (targetDirName == "..") {
260 QVERIFY(dir.cdUp());
261 } else {
262 QVERIFY(dir.cd(targetDirName));
263 }
264 QCOMPARE(dir.path(), result);
265 // Try to go back
266 dir.setPath(dirName);
267 QCOMPARE(dir.path(), dirName);
268 zip.close();
269 curDir.remove(zipName);
270}
271
272void TestQuaZipDir::entryInfoList()
273{
274 QString zipName = "entryInfoList.zip";
275 QStringList fileNames;
276 fileNames << "test.txt";
277 if (!createTestFiles(fileNames)) {
278 QFAIL("Couldn't create test files");
279 }
280 if (!createTestArchive(zipName, fileNames)) {
281 QFAIL("Couldn't create test archive");
282 }
283 removeTestFiles(fileNames);
284 QuaZip zip(zipName);
285 QDir curDir;
286 QVERIFY(zip.open(QuaZip::mdUnzip));
287 QuaZipDir dir(&zip, "/");
288 QCOMPARE(dir.entryInfoList().size(), 1);
289 QCOMPARE(dir.entryInfoList64().size(), 1);
290 zip.close();
291 curDir.remove(zipName);
292}
293
294void TestQuaZipDir::operators()
295{
296 QString zipName = "zipDirOperators.zip";
297 QStringList fileNames;
298 fileNames << "dir/test.txt" << "root.txt";
299 if (!createTestFiles(fileNames)) {
300 QFAIL("Couldn't create test files");
301 }
302 if (!createTestArchive(zipName, fileNames)) {
303 QFAIL("Couldn't create test archive");
304 }
305 removeTestFiles(fileNames);
306 QuaZip zip(zipName);
307 QDir curDir;
308 QVERIFY(zip.open(QuaZip::mdUnzip));
309 QuaZipDir dir(&zip, "dir");
310 QuaZipDir dir2 = dir; // Copy constructor
311 QCOMPARE(dir2.path(), QString::fromLatin1("dir"));
312 dir2.cdUp();
313 QCOMPARE(dir2.path(), QString::fromLatin1(""));
314 QCOMPARE(dir.path(), QString::fromLatin1("dir"));
315 dir2 = dir; // operator=()
316 QCOMPARE(dir2.path(), QString::fromLatin1("dir"));
317 QVERIFY(dir2 == dir); // opertor==
318 dir.cd("/");
319 QCOMPARE(dir[0], QString::fromLatin1("dir/"));
320 QCOMPARE(dir[1], QString::fromLatin1("root.txt"));
321 zip.close();
322 curDir.remove(zipName);
323}
324
325void TestQuaZipDir::filePath()
326{
327 QString zipName = "entryInfoList.zip";
328 QStringList fileNames;
329 fileNames << "root.txt" << "dir/test.txt";
330 if (!createTestFiles(fileNames)) {
331 QFAIL("Couldn't create test files");
332 }
333 if (!createTestArchive(zipName, fileNames)) {
334 QFAIL("Couldn't create test archive");
335 }
336 removeTestFiles(fileNames);
337 QuaZip zip(zipName);
338 QDir curDir;
339 QVERIFY(zip.open(QuaZip::mdUnzip));
340 QuaZipDir dir(&zip);
341 QVERIFY(dir.cd("dir"));
342 QCOMPARE(dir.relativeFilePath("/root.txt"),
343 QString::fromLatin1("../root.txt"));
344 QCOMPARE(dir.filePath("test.txt"),
345 QString::fromLatin1("dir/test.txt"));
346 zip.close();
347 curDir.remove(zipName);
348}
Note: See TracBrowser for help on using the repository browser.