Line data Source code
1 : /*
2 : Zipios++ - a small C++ library that provides easy access to .zip files.
3 :
4 : Copyright (C) 2000-2007 Thomas Sondergaard
5 : Copyright (C) 2015 Made to Order Software Corporation
6 :
7 : This library is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU Lesser General Public
9 : License as published by the Free Software Foundation; either
10 : version 2 of the License, or (at your option) any later version.
11 :
12 : This library is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this library; if not, write to the Free Software
19 : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 : */
21 :
22 : /** \file
23 : *
24 : * Zipios++ unit tests for the DirectoryEntry class.
25 : */
26 :
27 : #include "catch_tests.hpp"
28 :
29 : #include "zipios++/directoryentry.hpp"
30 : #include "zipios++/zipiosexceptions.hpp"
31 :
32 : #include "src/dostime.h"
33 :
34 : #include <fstream>
35 :
36 : #include <sys/stat.h>
37 : #include <unistd.h>
38 :
39 :
40 : /** \brief Local definitions used globally in the DirectoryEntry tests.
41 : *
42 : */
43 : namespace
44 : {
45 :
46 :
47 : /** \brief Expected level defined as a variable.
48 : *
49 : * This definition is used because otherwise catch fails seeing the
50 : * definition as a global and the linkage fails.
51 : */
52 : zipios::FileEntry::CompressionLevel const g_expected_level(zipios::FileEntry::COMPRESSION_LEVEL_DEFAULT);
53 :
54 : /** \brief Directories make use of level 'none'.
55 : *
56 : * This value is to verify that the compression level of a directory
57 : * is properly defined.
58 : */
59 : zipios::FileEntry::CompressionLevel const g_directory_level(zipios::FileEntry::COMPRESSION_LEVEL_NONE);
60 :
61 :
62 : } // no name namespace
63 :
64 :
65 24 : SCENARIO("DirectoryEntry with invalid paths", "[DirectoryEntry] [FileEntry]")
66 : {
67 23 : GIVEN("test a fantom file (path that \"cannot\" exists) and no comment")
68 : {
69 22 : zipios::DirectoryEntry de(zipios::FilePath("/this/file/really/should/not/exist/period.txt"), "");
70 :
71 : // first, check that the object is setup as expected
72 22 : SECTION("verify that the object looks as expected")
73 : {
74 1 : REQUIRE(de.isEqual(de));
75 1 : REQUIRE(de.getComment().empty());
76 1 : REQUIRE(de.getCompressedSize() == 0);
77 1 : REQUIRE(de.getCrc() == 0);
78 1 : REQUIRE(de.getEntryOffset() == 0);
79 1 : REQUIRE(de.getExtra().empty());
80 1 : REQUIRE(de.getHeaderSize() == 0);
81 1 : REQUIRE(de.getLevel() == g_expected_level);
82 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
83 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
84 1 : REQUIRE(de.getFileName() == "period.txt");
85 1 : REQUIRE(de.getSize() == 0);
86 1 : REQUIRE(de.getTime() == 0); // invalid date
87 1 : REQUIRE(de.getUnixTime() == 0);
88 1 : REQUIRE_FALSE(de.hasCrc());
89 1 : REQUIRE_FALSE(de.isDirectory());
90 1 : REQUIRE_FALSE(de.isValid());
91 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
92 :
93 1 : REQUIRE_THROWS_AS(de.read(std::cin), zipios::IOException);
94 1 : REQUIRE_THROWS_AS(de.write(std::cout), zipios::IOException);
95 :
96 1 : zipios::FileEntry::pointer_t null_entry;
97 1 : REQUIRE_FALSE(de.isEqual(*null_entry)); // here we are passing a NULL reference which most people think is something impossible to do...
98 : //REQUIRE_FALSE(null_entry->isEqual(de)); -- that would obviously crash!
99 :
100 2 : zipios::DirectoryEntry empty(zipios::FilePath(""), "");
101 1 : REQUIRE_FALSE(de.isEqual(empty));
102 1 : REQUIRE_FALSE(empty.isEqual(de));
103 :
104 : // attempt a clone now, should have the same content
105 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
106 :
107 1 : REQUIRE(clone->getComment().empty());
108 1 : REQUIRE(clone->getCompressedSize() == 0);
109 1 : REQUIRE(clone->getCrc() == 0);
110 1 : REQUIRE(clone->getEntryOffset() == 0);
111 1 : REQUIRE(clone->getExtra().empty());
112 1 : REQUIRE(clone->getHeaderSize() == 0);
113 1 : REQUIRE(clone->getLevel() == g_expected_level);
114 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
115 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
116 1 : REQUIRE(clone->getFileName() == "period.txt");
117 1 : REQUIRE(clone->getSize() == 0);
118 1 : REQUIRE(clone->getTime() == 0); // invalid date
119 1 : REQUIRE(clone->getUnixTime() == 0);
120 1 : REQUIRE_FALSE(clone->hasCrc());
121 1 : REQUIRE_FALSE(clone->isDirectory());
122 1 : REQUIRE_FALSE(clone->isValid());
123 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
124 1 : REQUIRE(clone->isEqual(de));
125 2 : REQUIRE(de.isEqual(*clone));
126 22 : }
127 :
128 22 : WHEN("setting the comment")
129 : {
130 2 : de.setComment("new comment");
131 :
132 2 : THEN("we can read it and nothing else changed")
133 : {
134 1 : REQUIRE(de.getComment() == "new comment");
135 1 : REQUIRE(de.getCompressedSize() == 0);
136 1 : REQUIRE(de.getCrc() == 0);
137 1 : REQUIRE(de.getEntryOffset() == 0);
138 1 : REQUIRE(de.getExtra().empty());
139 1 : REQUIRE(de.getHeaderSize() == 0);
140 1 : REQUIRE(de.getLevel() == g_expected_level);
141 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
142 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
143 1 : REQUIRE(de.getFileName() == "period.txt");
144 1 : REQUIRE(de.getSize() == 0);
145 1 : REQUIRE(de.getTime() == 0); // invalid date
146 1 : REQUIRE(de.getUnixTime() == 0);
147 1 : REQUIRE_FALSE(de.hasCrc());
148 1 : REQUIRE_FALSE(de.isDirectory());
149 1 : REQUIRE_FALSE(de.isValid());
150 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
151 :
152 1 : zipios::DirectoryEntry other(zipios::FilePath("another/path"), "");
153 1 : REQUIRE_FALSE(de.isEqual(other));
154 1 : REQUIRE_FALSE(other.isEqual(de));
155 :
156 : // attempt a clone now, should have the same content
157 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
158 :
159 1 : REQUIRE(clone->getComment() == "new comment");
160 1 : REQUIRE(clone->getCompressedSize() == 0);
161 1 : REQUIRE(clone->getCrc() == 0);
162 1 : REQUIRE(clone->getEntryOffset() == 0);
163 1 : REQUIRE(clone->getExtra().empty());
164 1 : REQUIRE(clone->getHeaderSize() == 0);
165 1 : REQUIRE(clone->getLevel() == g_expected_level);
166 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
167 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
168 1 : REQUIRE(clone->getFileName() == "period.txt");
169 1 : REQUIRE(clone->getSize() == 0);
170 1 : REQUIRE(clone->getTime() == 0); // invalid date
171 1 : REQUIRE(clone->getUnixTime() == 0);
172 1 : REQUIRE_FALSE(clone->hasCrc());
173 1 : REQUIRE_FALSE(clone->isDirectory());
174 1 : REQUIRE_FALSE(clone->isValid());
175 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
176 1 : REQUIRE(clone->isEqual(de));
177 2 : REQUIRE(de.isEqual(*clone));
178 2 : }
179 22 : }
180 :
181 22 : WHEN("setting the compressed size")
182 : {
183 : // zero would not really prove anything so skip such
184 : // (although it may be extremely rare...)
185 : size_t r;
186 2 : do
187 : {
188 2 : r = zipios_test::rand_size_t();
189 : }
190 : while(r == 0);
191 2 : de.setCompressedSize(r);
192 :
193 2 : THEN("we ignore it")
194 : {
195 1 : REQUIRE(de.getComment().empty());
196 1 : REQUIRE(de.getCompressedSize() == 0);
197 1 : REQUIRE(de.getCrc() == 0);
198 1 : REQUIRE(de.getEntryOffset() == 0);
199 1 : REQUIRE(de.getExtra().empty());
200 1 : REQUIRE(de.getHeaderSize() == 0);
201 1 : REQUIRE(de.getLevel() == g_expected_level);
202 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
203 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
204 1 : REQUIRE(de.getFileName() == "period.txt");
205 1 : REQUIRE(de.getSize() == 0);
206 1 : REQUIRE(de.getTime() == 0); // invalid date
207 1 : REQUIRE(de.getUnixTime() == 0);
208 1 : REQUIRE_FALSE(de.hasCrc());
209 1 : REQUIRE_FALSE(de.isDirectory());
210 1 : REQUIRE_FALSE(de.isValid());
211 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
212 :
213 1 : zipios::DirectoryEntry same(zipios::FilePath("/this/file/really/should/not/exist/period.txt"), "");
214 1 : REQUIRE(de.isEqual(same));
215 1 : REQUIRE(same.isEqual(de));
216 :
217 2 : zipios::DirectoryEntry other(zipios::FilePath("this/file/really/should/not/exist/period.txt"), "");
218 1 : REQUIRE_FALSE(de.isEqual(other));
219 1 : REQUIRE_FALSE(other.isEqual(de));
220 :
221 : // attempt a clone now, should have the same content
222 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
223 :
224 1 : REQUIRE(clone->getComment().empty());
225 1 : REQUIRE(clone->getCompressedSize() == 0);
226 1 : REQUIRE(clone->getCrc() == 0);
227 1 : REQUIRE(clone->getEntryOffset() == 0);
228 1 : REQUIRE(clone->getExtra().empty());
229 1 : REQUIRE(clone->getHeaderSize() == 0);
230 1 : REQUIRE(clone->getLevel() == g_expected_level);
231 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
232 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
233 1 : REQUIRE(clone->getFileName() == "period.txt");
234 1 : REQUIRE(clone->getSize() == 0);
235 1 : REQUIRE(clone->getTime() == 0); // invalid date
236 1 : REQUIRE(clone->getUnixTime() == 0);
237 1 : REQUIRE_FALSE(clone->hasCrc());
238 1 : REQUIRE_FALSE(clone->isDirectory());
239 1 : REQUIRE_FALSE(clone->isValid());
240 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
241 1 : REQUIRE(clone->isEqual(de));
242 2 : REQUIRE(de.isEqual(*clone));
243 2 : }
244 22 : }
245 :
246 22 : WHEN("setting the CRC")
247 : {
248 : // zero would not really prove anything so skip such
249 : uint32_t r;
250 2 : do
251 : {
252 2 : r = rand();
253 : }
254 : while(r == 0);
255 2 : de.setCrc(rand());
256 :
257 2 : THEN("we ignore it")
258 : {
259 1 : REQUIRE(de.getComment().empty());
260 1 : REQUIRE(de.getCompressedSize() == 0);
261 1 : REQUIRE(de.getCrc() == 0);
262 1 : REQUIRE(de.getEntryOffset() == 0);
263 1 : REQUIRE(de.getExtra().empty());
264 1 : REQUIRE(de.getHeaderSize() == 0);
265 1 : REQUIRE(de.getLevel() == g_expected_level);
266 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
267 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
268 1 : REQUIRE(de.getFileName() == "period.txt");
269 1 : REQUIRE(de.getSize() == 0);
270 1 : REQUIRE(de.getTime() == 0); // invalid date
271 1 : REQUIRE(de.getUnixTime() == 0);
272 1 : REQUIRE_FALSE(de.hasCrc());
273 1 : REQUIRE_FALSE(de.isDirectory());
274 1 : REQUIRE_FALSE(de.isValid());
275 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
276 :
277 1 : zipios::DirectoryEntry other(zipios::FilePath("/this/file/really/should/not/exist/period"), "");
278 1 : REQUIRE_FALSE(de.isEqual(other));
279 1 : REQUIRE_FALSE(other.isEqual(de));
280 :
281 : // attempt a clone now, should have the same content
282 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
283 :
284 1 : REQUIRE(clone->getComment().empty());
285 1 : REQUIRE(clone->getCompressedSize() == 0);
286 1 : REQUIRE(clone->getCrc() == 0);
287 1 : REQUIRE(clone->getEntryOffset() == 0);
288 1 : REQUIRE(clone->getExtra().empty());
289 1 : REQUIRE(clone->getHeaderSize() == 0);
290 1 : REQUIRE(clone->getLevel() == g_expected_level);
291 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
292 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
293 1 : REQUIRE(clone->getFileName() == "period.txt");
294 1 : REQUIRE(clone->getSize() == 0);
295 1 : REQUIRE(clone->getTime() == 0); // invalid date
296 1 : REQUIRE(clone->getUnixTime() == 0);
297 1 : REQUIRE_FALSE(clone->hasCrc());
298 1 : REQUIRE_FALSE(clone->isDirectory());
299 1 : REQUIRE_FALSE(clone->isValid());
300 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
301 1 : REQUIRE(clone->isEqual(de));
302 2 : REQUIRE(de.isEqual(*clone));
303 2 : }
304 22 : }
305 :
306 22 : WHEN("setting an extra buffer")
307 : {
308 : // zero would not really prove anything so skip such
309 2 : zipios::FileEntry::buffer_t b;
310 2 : uint32_t size(rand() % 100 + 20);
311 183 : for(uint32_t i(0); i < size; ++i)
312 : {
313 181 : b.push_back(rand());
314 : }
315 2 : de.setExtra(b);
316 :
317 2 : THEN("we ignore it")
318 : {
319 1 : REQUIRE(de.getComment().empty());
320 1 : REQUIRE(de.getCompressedSize() == 0);
321 1 : REQUIRE(de.getCrc() == 0);
322 1 : REQUIRE(de.getEntryOffset() == 0);
323 1 : REQUIRE_FALSE(de.getExtra().empty());
324 1 : REQUIRE(de.getHeaderSize() == 0);
325 1 : REQUIRE(de.getLevel() == g_expected_level);
326 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
327 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
328 1 : REQUIRE(de.getFileName() == "period.txt");
329 1 : REQUIRE(de.getSize() == 0);
330 1 : REQUIRE(de.getTime() == 0); // invalid date
331 1 : REQUIRE(de.getUnixTime() == 0);
332 1 : REQUIRE_FALSE(de.hasCrc());
333 1 : REQUIRE_FALSE(de.isDirectory());
334 1 : REQUIRE_FALSE(de.isValid());
335 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
336 1 : REQUIRE(de.getExtra() == b);
337 :
338 1 : zipios::DirectoryEntry other(zipios::FilePath("period.txt"), "");
339 1 : REQUIRE_FALSE(de.isEqual(other));
340 1 : REQUIRE_FALSE(other.isEqual(de));
341 :
342 : // attempt a clone now, should have the same content
343 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
344 :
345 1 : REQUIRE(clone->getComment().empty());
346 1 : REQUIRE(clone->getCompressedSize() == 0);
347 1 : REQUIRE(clone->getCrc() == 0);
348 1 : REQUIRE(clone->getEntryOffset() == 0);
349 1 : REQUIRE_FALSE(clone->getExtra().empty());
350 1 : REQUIRE(clone->getHeaderSize() == 0);
351 1 : REQUIRE(clone->getLevel() == g_expected_level);
352 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
353 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
354 1 : REQUIRE(clone->getFileName() == "period.txt");
355 1 : REQUIRE(clone->getSize() == 0);
356 1 : REQUIRE(clone->getTime() == 0); // invalid date
357 1 : REQUIRE(clone->getUnixTime() == 0);
358 1 : REQUIRE_FALSE(clone->hasCrc());
359 1 : REQUIRE_FALSE(clone->isDirectory());
360 1 : REQUIRE_FALSE(clone->isValid());
361 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
362 1 : REQUIRE(clone->getExtra() == b);
363 1 : REQUIRE(clone->isEqual(de));
364 2 : REQUIRE(de.isEqual(*clone));
365 2 : }
366 22 : }
367 :
368 22 : SECTION("setting an invalid level")
369 : {
370 2002 : for(zipios::FileEntry::CompressionLevel level(-1000); level <= 1000; ++level)
371 : {
372 2001 : if(level >= -3 && level <= 100)
373 : {
374 : // this is considered valid
375 104 : de.setLevel(level);
376 :
377 104 : REQUIRE(de.getComment().empty());
378 104 : REQUIRE(de.getCompressedSize() == 0);
379 104 : REQUIRE(de.getCrc() == 0);
380 104 : REQUIRE(de.getEntryOffset() == 0);
381 104 : REQUIRE(de.getExtra().empty());
382 104 : REQUIRE(de.getHeaderSize() == 0);
383 104 : REQUIRE(de.getLevel() == level);
384 104 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
385 104 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
386 104 : REQUIRE(de.getFileName() == "period.txt");
387 104 : REQUIRE(de.getSize() == 0);
388 104 : REQUIRE(de.getTime() == 0); // invalid date
389 104 : REQUIRE(de.getUnixTime() == 0);
390 104 : REQUIRE_FALSE(de.hasCrc());
391 104 : REQUIRE_FALSE(de.isDirectory());
392 104 : REQUIRE_FALSE(de.isValid());
393 104 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
394 :
395 104 : zipios::DirectoryEntry other(zipios::FilePath("/file/really/should/not/exist/period.txt"), "");
396 104 : REQUIRE_FALSE(de.isEqual(other));
397 104 : REQUIRE_FALSE(other.isEqual(de));
398 :
399 : // attempt a clone now, should have the same content
400 208 : zipios::DirectoryEntry::pointer_t clone(de.clone());
401 :
402 104 : REQUIRE(clone->getComment().empty());
403 104 : REQUIRE(clone->getCompressedSize() == 0);
404 104 : REQUIRE(clone->getCrc() == 0);
405 104 : REQUIRE(clone->getEntryOffset() == 0);
406 104 : REQUIRE(clone->getExtra().empty());
407 104 : REQUIRE(clone->getHeaderSize() == 0);
408 104 : REQUIRE(clone->getLevel() == level);
409 104 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
410 104 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
411 104 : REQUIRE(clone->getFileName() == "period.txt");
412 104 : REQUIRE(clone->getSize() == 0);
413 104 : REQUIRE(clone->getTime() == 0); // invalid date
414 104 : REQUIRE(clone->getUnixTime() == 0);
415 104 : REQUIRE_FALSE(clone->hasCrc());
416 104 : REQUIRE_FALSE(clone->isDirectory());
417 104 : REQUIRE_FALSE(clone->isValid());
418 104 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
419 104 : REQUIRE(clone->isEqual(de));
420 208 : REQUIRE(de.isEqual(*clone));
421 : }
422 : else
423 : {
424 1897 : REQUIRE_THROWS_AS(de.setLevel(level), zipios::InvalidStateException);
425 : }
426 : }
427 22 : }
428 :
429 22 : SECTION("setting an invalid method")
430 : {
431 : // WARNING: the StorageMethod is a uint8_t so testing
432 : // with negative and such would wrap the number...
433 : // (i.e. no need to do more than the followin)
434 257 : for(int i(0); i < 256; ++i)
435 : {
436 256 : switch(i)
437 : {
438 : case 0: // Stored
439 : case 8: // Deflated
440 2 : break;
441 :
442 : default:
443 254 : REQUIRE_THROWS_AS(de.setMethod(static_cast<zipios::StorageMethod>(i)), zipios::InvalidStateException);
444 254 : break;
445 :
446 : }
447 : }
448 22 : }
449 :
450 22 : WHEN("setting the method")
451 : {
452 : // set a method other than STORED, which is 0
453 : // in the newer version, though, the set only accepts
454 : // with STORED and DEFLATED
455 : //
456 2 : zipios::StorageMethod storage_method(zipios::StorageMethod::DEFLATED);
457 2 : de.setMethod(storage_method);
458 :
459 2 : THEN("we ignore it")
460 : {
461 1 : REQUIRE(de.getComment().empty());
462 1 : REQUIRE(de.getCompressedSize() == 0);
463 1 : REQUIRE(de.getCrc() == 0);
464 1 : REQUIRE(de.getEntryOffset() == 0);
465 1 : REQUIRE(de.getExtra().empty());
466 1 : REQUIRE(de.getHeaderSize() == 0);
467 1 : REQUIRE(de.getLevel() == g_expected_level);
468 1 : REQUIRE(de.getMethod() == storage_method);
469 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
470 1 : REQUIRE(de.getFileName() == "period.txt");
471 1 : REQUIRE(de.getSize() == 0);
472 1 : REQUIRE(de.getTime() == 0); // invalid date
473 1 : REQUIRE(de.getUnixTime() == 0);
474 1 : REQUIRE_FALSE(de.hasCrc());
475 1 : REQUIRE_FALSE(de.isDirectory());
476 1 : REQUIRE_FALSE(de.isValid());
477 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
478 :
479 1 : zipios::DirectoryEntry other(zipios::FilePath("/file/really/should/not/exist/period.txt"), "");
480 1 : REQUIRE_FALSE(de.isEqual(other));
481 1 : REQUIRE_FALSE(other.isEqual(de));
482 :
483 : // attempt a clone now, should have the same content
484 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
485 :
486 1 : REQUIRE(clone->getComment().empty());
487 1 : REQUIRE(clone->getCompressedSize() == 0);
488 1 : REQUIRE(clone->getCrc() == 0);
489 1 : REQUIRE(clone->getEntryOffset() == 0);
490 1 : REQUIRE(clone->getExtra().empty());
491 1 : REQUIRE(clone->getHeaderSize() == 0);
492 1 : REQUIRE(clone->getLevel() == g_expected_level);
493 1 : REQUIRE(clone->getMethod() == storage_method);
494 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
495 1 : REQUIRE(clone->getFileName() == "period.txt");
496 1 : REQUIRE(clone->getSize() == 0);
497 1 : REQUIRE(clone->getTime() == 0); // invalid date
498 1 : REQUIRE(clone->getUnixTime() == 0);
499 1 : REQUIRE_FALSE(clone->hasCrc());
500 1 : REQUIRE_FALSE(clone->isDirectory());
501 1 : REQUIRE_FALSE(clone->isValid());
502 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
503 1 : REQUIRE(clone->isEqual(de));
504 2 : REQUIRE(de.isEqual(*clone));
505 2 : }
506 22 : }
507 :
508 22 : WHEN("setting the uncompressed size")
509 : {
510 : // zero would not really prove anything so skip such
511 : // (although it may be extremely rare...)
512 : size_t r;
513 2 : do
514 : {
515 2 : r = zipios_test::rand_size_t();
516 : }
517 2 : while(r == 0);
518 2 : de.setSize(r);
519 :
520 2 : THEN("we take it as is")
521 : {
522 1 : REQUIRE(de.getComment().empty());
523 1 : REQUIRE(de.getCompressedSize() == r);
524 1 : REQUIRE(de.getCrc() == 0);
525 1 : REQUIRE(de.getEntryOffset() == 0);
526 1 : REQUIRE(de.getExtra().empty());
527 1 : REQUIRE(de.getHeaderSize() == 0);
528 1 : REQUIRE(de.getLevel() == g_expected_level);
529 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
530 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
531 1 : REQUIRE(de.getFileName() == "period.txt");
532 1 : REQUIRE(de.getSize() == r);
533 1 : REQUIRE(de.getTime() == 0); // invalid date
534 1 : REQUIRE(de.getUnixTime() == 0);
535 1 : REQUIRE(!de.hasCrc());
536 1 : REQUIRE(!de.isDirectory());
537 1 : REQUIRE(!de.isValid());
538 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (" + std::to_string(r) + " bytes)");
539 :
540 1 : zipios::DirectoryEntry other(zipios::FilePath("really/.should"), "");
541 1 : REQUIRE_FALSE(de.isEqual(other));
542 1 : REQUIRE_FALSE(other.isEqual(de));
543 :
544 : // attempt a clone now, should have the same content
545 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
546 :
547 1 : REQUIRE(clone->getComment().empty());
548 1 : REQUIRE(clone->getCompressedSize() == r);
549 1 : REQUIRE(clone->getCrc() == 0);
550 1 : REQUIRE(clone->getEntryOffset() == 0);
551 1 : REQUIRE(clone->getExtra().empty());
552 1 : REQUIRE(clone->getHeaderSize() == 0);
553 1 : REQUIRE(clone->getLevel() == g_expected_level);
554 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
555 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
556 1 : REQUIRE(clone->getFileName() == "period.txt");
557 1 : REQUIRE(clone->getSize() == r);
558 1 : REQUIRE(clone->getTime() == 0); // invalid date
559 1 : REQUIRE(clone->getUnixTime() == 0);
560 1 : REQUIRE(!clone->hasCrc());
561 1 : REQUIRE(!clone->isDirectory());
562 1 : REQUIRE(!clone->isValid());
563 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (" + std::to_string(r) + " bytes)");
564 1 : REQUIRE(clone->isEqual(de));
565 2 : REQUIRE(de.isEqual(*clone));
566 2 : }
567 22 : }
568 :
569 22 : WHEN("setting the DOS time")
570 : {
571 : // DOS time numbers are not linear so we test until we get one
572 : // that works...
573 2 : time_t t((static_cast<time_t>(zipios_test::rand_size_t())) % (4354848000LL - 315561600LL) + 315561600);
574 2 : dostime_t r(unix2dostime(t));
575 2 : de.setTime(r);
576 :
577 2 : THEN("we take it as is")
578 : {
579 1 : REQUIRE(de.getComment().empty());
580 1 : REQUIRE(de.getCompressedSize() == 0);
581 1 : REQUIRE(de.getCrc() == 0);
582 1 : REQUIRE(de.getEntryOffset() == 0);
583 1 : REQUIRE(de.getExtra().empty());
584 1 : REQUIRE(de.getHeaderSize() == 0);
585 1 : REQUIRE(de.getLevel() == g_expected_level);
586 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
587 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
588 1 : REQUIRE(de.getFileName() == "period.txt");
589 1 : REQUIRE(de.getSize() == 0);
590 1 : REQUIRE(de.getTime() == r);
591 1 : REQUIRE(de.getUnixTime() == dos2unixtime(r));
592 1 : REQUIRE(!de.hasCrc());
593 1 : REQUIRE(!de.isDirectory());
594 1 : REQUIRE(!de.isValid());
595 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
596 :
597 1 : zipios::DirectoryEntry other(zipios::FilePath("other-name.txt"), "");
598 1 : REQUIRE_FALSE(de.isEqual(other));
599 1 : REQUIRE_FALSE(other.isEqual(de));
600 :
601 : // attempt a clone now, should have the same content
602 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
603 :
604 1 : REQUIRE(clone->getComment().empty());
605 1 : REQUIRE(clone->getCompressedSize() == 0);
606 1 : REQUIRE(clone->getCrc() == 0);
607 1 : REQUIRE(clone->getEntryOffset() == 0);
608 1 : REQUIRE(clone->getExtra().empty());
609 1 : REQUIRE(clone->getHeaderSize() == 0);
610 1 : REQUIRE(clone->getLevel() == g_expected_level);
611 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
612 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
613 1 : REQUIRE(clone->getFileName() == "period.txt");
614 1 : REQUIRE(clone->getSize() == 0);
615 1 : REQUIRE(clone->getTime() == r);
616 1 : REQUIRE(clone->getUnixTime() == dos2unixtime(r));
617 1 : REQUIRE(!clone->hasCrc());
618 1 : REQUIRE(!clone->isDirectory());
619 1 : REQUIRE(!clone->isValid());
620 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
621 1 : REQUIRE(clone->isEqual(de));
622 2 : REQUIRE(de.isEqual(*clone));
623 2 : }
624 22 : }
625 :
626 22 : WHEN("setting the Unix time")
627 : {
628 : // DOS time are limited to a smaller range and on every other
629 : // second so we get a valid DOS time and convert it to a Unix time
630 2 : time_t r((static_cast<time_t>(zipios_test::rand_size_t())) % (4354848000LL - 315561600LL) + 315561600);
631 2 : de.setUnixTime(r);
632 :
633 2 : THEN("we take it as is")
634 : {
635 1 : REQUIRE(de.getComment().empty());
636 1 : REQUIRE(de.getCompressedSize() == 0);
637 1 : REQUIRE(de.getCrc() == 0);
638 1 : REQUIRE(de.getEntryOffset() == 0);
639 1 : REQUIRE(de.getExtra().empty());
640 1 : REQUIRE(de.getHeaderSize() == 0);
641 1 : REQUIRE(de.getLevel() == g_expected_level);
642 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
643 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
644 1 : REQUIRE(de.getFileName() == "period.txt");
645 1 : REQUIRE(de.getSize() == 0);
646 1 : REQUIRE(de.getTime() == unix2dostime(r));
647 1 : REQUIRE(de.getUnixTime() == r);
648 1 : REQUIRE(!de.hasCrc());
649 1 : REQUIRE(!de.isDirectory());
650 1 : REQUIRE(!de.isValid());
651 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
652 :
653 1 : zipios::DirectoryEntry other(zipios::FilePath("path/incorrect"), "");
654 1 : REQUIRE_FALSE(de.isEqual(other));
655 1 : REQUIRE_FALSE(other.isEqual(de));
656 :
657 : // attempt a clone now, should have the same content
658 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
659 :
660 1 : REQUIRE(clone->getComment().empty());
661 1 : REQUIRE(clone->getCompressedSize() == 0);
662 1 : REQUIRE(clone->getCrc() == 0);
663 1 : REQUIRE(clone->getEntryOffset() == 0);
664 1 : REQUIRE(clone->getExtra().empty());
665 1 : REQUIRE(clone->getHeaderSize() == 0);
666 1 : REQUIRE(clone->getLevel() == g_expected_level);
667 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
668 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
669 1 : REQUIRE(clone->getFileName() == "period.txt");
670 1 : REQUIRE(clone->getSize() == 0);
671 1 : REQUIRE(clone->getTime() == unix2dostime(r));
672 1 : REQUIRE(clone->getUnixTime() == r);
673 1 : REQUIRE(!clone->hasCrc());
674 1 : REQUIRE(!clone->isDirectory());
675 1 : REQUIRE(!clone->isValid());
676 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
677 1 : REQUIRE(clone->isEqual(de));
678 2 : REQUIRE(de.isEqual(*clone));
679 2 : }
680 22 : }
681 :
682 22 : WHEN("setting the entry offset")
683 : {
684 : // DOS time are limited to a smaller range and on every other
685 : // second so we get a valid DOS time and convert it to a Unix time
686 2 : std::streampos r(zipios_test::rand_size_t());
687 2 : de.setEntryOffset(r);
688 :
689 2 : THEN("we retrive the same value")
690 : {
691 1 : REQUIRE(de.getComment().empty());
692 1 : REQUIRE(de.getCompressedSize() == 0);
693 1 : REQUIRE(de.getCrc() == 0);
694 1 : REQUIRE(de.getEntryOffset() == r);
695 1 : REQUIRE(de.getExtra().empty());
696 1 : REQUIRE(de.getHeaderSize() == 0);
697 1 : REQUIRE(de.getLevel() == g_expected_level);
698 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
699 1 : REQUIRE(de.getName() == "/this/file/really/should/not/exist/period.txt");
700 1 : REQUIRE(de.getFileName() == "period.txt");
701 1 : REQUIRE(de.getSize() == 0);
702 1 : REQUIRE(de.getTime() == 0);
703 1 : REQUIRE(de.getUnixTime() == 0);
704 1 : REQUIRE_FALSE(de.hasCrc());
705 1 : REQUIRE_FALSE(de.isDirectory());
706 1 : REQUIRE_FALSE(de.isValid());
707 1 : REQUIRE(de.toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
708 :
709 1 : zipios::DirectoryEntry other(zipios::FilePath("path/incorrect"), "");
710 1 : REQUIRE_FALSE(de.isEqual(other));
711 1 : REQUIRE_FALSE(other.isEqual(de));
712 :
713 : // attempt a clone now, should have the same content
714 2 : zipios::DirectoryEntry::pointer_t clone(de.clone());
715 :
716 1 : REQUIRE(clone->getComment().empty());
717 1 : REQUIRE(clone->getCompressedSize() == 0);
718 1 : REQUIRE(clone->getCrc() == 0);
719 1 : REQUIRE(clone->getEntryOffset() == r);
720 1 : REQUIRE(clone->getExtra().empty());
721 1 : REQUIRE(clone->getHeaderSize() == 0);
722 1 : REQUIRE(clone->getLevel() == g_expected_level);
723 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
724 1 : REQUIRE(clone->getName() == "/this/file/really/should/not/exist/period.txt");
725 1 : REQUIRE(clone->getFileName() == "period.txt");
726 1 : REQUIRE(clone->getSize() == 0);
727 1 : REQUIRE(clone->getTime() == 0);
728 1 : REQUIRE(clone->getUnixTime() == 0);
729 1 : REQUIRE_FALSE(clone->hasCrc());
730 1 : REQUIRE_FALSE(clone->isDirectory());
731 1 : REQUIRE_FALSE(clone->isValid());
732 1 : REQUIRE(clone->toString() == "/this/file/really/should/not/exist/period.txt (0 bytes)");
733 1 : REQUIRE(clone->isEqual(de));
734 2 : REQUIRE(de.isEqual(*clone));
735 2 : }
736 22 : }
737 23 : }
738 23 : }
739 :
740 :
741 3 : TEST_CASE("DirectoryEntry with one valid file", "[DirectoryEntry] [FileEntry]")
742 : {
743 22 : for(int i(0); i < 10; ++i)
744 : {
745 : // create a random file
746 20 : int const file_size(rand() % 100 + 20);
747 : {
748 : // create a file
749 20 : std::ofstream f("filepath-test.txt", std::ios::out | std::ios::binary);
750 1441 : for(int j(0); j < file_size; ++j)
751 : {
752 1421 : char const c(rand());
753 1421 : f << c;
754 20 : }
755 : }
756 :
757 : {
758 20 : zipios::DirectoryEntry de(zipios::FilePath("filepath-test.txt"), "");
759 :
760 : struct stat file_stats;
761 20 : REQUIRE(stat("filepath-test.txt", &file_stats) == 0);
762 :
763 : {
764 20 : REQUIRE(de.getComment().empty());
765 20 : REQUIRE(de.getCompressedSize() == file_size);
766 20 : REQUIRE(de.getCrc() == 0);
767 20 : REQUIRE(de.getEntryOffset() == 0);
768 20 : REQUIRE(de.getExtra().empty());
769 20 : REQUIRE(de.getHeaderSize() == 0);
770 20 : REQUIRE(de.getLevel() == g_expected_level);
771 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
772 20 : REQUIRE(de.getName() == "filepath-test.txt");
773 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
774 20 : REQUIRE(de.getSize() == file_size);
775 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
776 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
777 20 : REQUIRE(!de.hasCrc());
778 20 : REQUIRE(!de.isDirectory());
779 20 : REQUIRE(de.isValid());
780 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
781 :
782 : // attempt a clone now, should have the same content
783 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
784 :
785 20 : REQUIRE(clone->getComment().empty());
786 20 : REQUIRE(clone->getCompressedSize() == file_size);
787 20 : REQUIRE(clone->getCrc() == 0);
788 20 : REQUIRE(clone->getEntryOffset() == 0);
789 20 : REQUIRE(clone->getExtra().empty());
790 20 : REQUIRE(clone->getHeaderSize() == 0);
791 20 : REQUIRE(clone->getLevel() == g_expected_level);
792 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
793 20 : REQUIRE(clone->getName() == "filepath-test.txt");
794 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
795 20 : REQUIRE(clone->getSize() == file_size);
796 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
797 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
798 20 : REQUIRE(!clone->hasCrc());
799 20 : REQUIRE(!clone->isDirectory());
800 20 : REQUIRE(clone->isValid());
801 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
802 : }
803 :
804 : {
805 20 : de.setComment("new comment");
806 :
807 20 : REQUIRE(de.getComment() == "new comment");
808 20 : REQUIRE(de.getCompressedSize() == file_size);
809 20 : REQUIRE(de.getCrc() == 0);
810 20 : REQUIRE(de.getEntryOffset() == 0);
811 20 : REQUIRE(de.getExtra().empty());
812 20 : REQUIRE(de.getHeaderSize() == 0);
813 20 : REQUIRE(de.getLevel() == g_expected_level);
814 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
815 20 : REQUIRE(de.getName() == "filepath-test.txt");
816 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
817 20 : REQUIRE(de.getSize() == file_size);
818 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
819 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
820 20 : REQUIRE(!de.hasCrc());
821 20 : REQUIRE(!de.isDirectory());
822 20 : REQUIRE(de.isValid());
823 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
824 :
825 : // attempt a clone now, should have the same content
826 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
827 :
828 20 : REQUIRE(clone->getComment() == "new comment");
829 20 : REQUIRE(clone->getCompressedSize() == file_size);
830 20 : REQUIRE(clone->getCrc() == 0);
831 20 : REQUIRE(clone->getEntryOffset() == 0);
832 20 : REQUIRE(clone->getExtra().empty());
833 20 : REQUIRE(clone->getHeaderSize() == 0);
834 20 : REQUIRE(clone->getLevel() == g_expected_level);
835 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
836 20 : REQUIRE(clone->getName() == "filepath-test.txt");
837 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
838 20 : REQUIRE(clone->getSize() == file_size);
839 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
840 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
841 20 : REQUIRE(!clone->hasCrc());
842 20 : REQUIRE(!clone->isDirectory());
843 20 : REQUIRE(clone->isValid());
844 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
845 :
846 20 : de.setComment("");
847 : }
848 :
849 : {
850 : // zero would not really prove anything so skip such
851 : // (although it may be extremely rare...)
852 : size_t r;
853 20 : do
854 : {
855 20 : r = zipios_test::rand_size_t();
856 : }
857 : while(r == 0);
858 20 : de.setCompressedSize(r);
859 :
860 20 : REQUIRE(de.getComment().empty());
861 20 : REQUIRE(de.getCompressedSize() == file_size);
862 20 : REQUIRE(de.getCrc() == 0);
863 20 : REQUIRE(de.getEntryOffset() == 0);
864 20 : REQUIRE(de.getExtra().empty());
865 20 : REQUIRE(de.getHeaderSize() == 0);
866 20 : REQUIRE(de.getLevel() == g_expected_level);
867 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
868 20 : REQUIRE(de.getName() == "filepath-test.txt");
869 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
870 20 : REQUIRE(de.getSize() == file_size);
871 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
872 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
873 20 : REQUIRE(!de.hasCrc());
874 20 : REQUIRE(!de.isDirectory());
875 20 : REQUIRE(de.isValid());
876 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
877 :
878 : // attempt a clone now, should have the same content
879 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
880 :
881 20 : REQUIRE(clone->getComment().empty());
882 20 : REQUIRE(clone->getCompressedSize() == file_size);
883 20 : REQUIRE(clone->getCrc() == 0);
884 20 : REQUIRE(clone->getEntryOffset() == 0);
885 20 : REQUIRE(clone->getExtra().empty());
886 20 : REQUIRE(clone->getHeaderSize() == 0);
887 20 : REQUIRE(clone->getLevel() == g_expected_level);
888 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
889 20 : REQUIRE(clone->getName() == "filepath-test.txt");
890 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
891 20 : REQUIRE(clone->getSize() == file_size);
892 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
893 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
894 20 : REQUIRE(!clone->hasCrc());
895 20 : REQUIRE(!clone->isDirectory());
896 20 : REQUIRE(clone->isValid());
897 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
898 : }
899 :
900 : {
901 : // zero would not really prove anything so skip such
902 : uint32_t r;
903 20 : do
904 : {
905 20 : r = rand();
906 : }
907 : while(r == 0);
908 20 : de.setCrc(rand());
909 :
910 20 : REQUIRE(de.getComment().empty());
911 20 : REQUIRE(de.getCompressedSize() == file_size);
912 20 : REQUIRE(de.getCrc() == 0);
913 20 : REQUIRE(de.getEntryOffset() == 0);
914 20 : REQUIRE(de.getExtra().empty());
915 20 : REQUIRE(de.getHeaderSize() == 0);
916 20 : REQUIRE(de.getLevel() == g_expected_level);
917 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
918 20 : REQUIRE(de.getName() == "filepath-test.txt");
919 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
920 20 : REQUIRE(de.getSize() == file_size);
921 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
922 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
923 20 : REQUIRE(!de.hasCrc());
924 20 : REQUIRE(!de.isDirectory());
925 20 : REQUIRE(de.isValid());
926 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
927 :
928 : // attempt a clone now, should have the same content
929 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
930 :
931 20 : REQUIRE(clone->getComment().empty());
932 20 : REQUIRE(clone->getCompressedSize() == file_size);
933 20 : REQUIRE(clone->getCrc() == 0);
934 20 : REQUIRE(clone->getEntryOffset() == 0);
935 20 : REQUIRE(clone->getExtra().empty());
936 20 : REQUIRE(clone->getHeaderSize() == 0);
937 20 : REQUIRE(clone->getLevel() == g_expected_level);
938 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
939 20 : REQUIRE(clone->getName() == "filepath-test.txt");
940 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
941 20 : REQUIRE(clone->getSize() == file_size);
942 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
943 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
944 20 : REQUIRE(!clone->hasCrc());
945 20 : REQUIRE(!clone->isDirectory());
946 20 : REQUIRE(clone->isValid());
947 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
948 : }
949 :
950 : {
951 : // zero would not really prove anything so skip such
952 20 : zipios::FileEntry::buffer_t b;
953 20 : uint32_t size(rand() % 100 + 20);
954 1430 : for(uint32_t j(0); j < size; ++j)
955 : {
956 1410 : b.push_back(rand());
957 : }
958 20 : de.setExtra(b);
959 :
960 20 : REQUIRE(de.getComment().empty());
961 20 : REQUIRE(de.getCompressedSize() == file_size);
962 20 : REQUIRE(de.getCrc() == 0);
963 20 : REQUIRE(de.getEntryOffset() == 0);
964 20 : REQUIRE_FALSE(de.getExtra().empty());
965 20 : REQUIRE(de.getHeaderSize() == 0);
966 20 : REQUIRE(de.getLevel() == g_expected_level);
967 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
968 20 : REQUIRE(de.getName() == "filepath-test.txt");
969 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
970 20 : REQUIRE(de.getSize() == file_size);
971 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
972 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
973 20 : REQUIRE_FALSE(de.hasCrc());
974 20 : REQUIRE_FALSE(de.isDirectory());
975 20 : REQUIRE(de.isValid());
976 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
977 20 : REQUIRE(de.getExtra() == b);
978 :
979 : // attempt a clone now, should have the same content
980 40 : zipios::DirectoryEntry::pointer_t clone(de.clone());
981 :
982 20 : REQUIRE(clone->getComment().empty());
983 20 : REQUIRE(clone->getCompressedSize() == file_size);
984 20 : REQUIRE(clone->getCrc() == 0);
985 20 : REQUIRE(clone->getEntryOffset() == 0);
986 20 : REQUIRE_FALSE(clone->getExtra().empty());
987 20 : REQUIRE(clone->getHeaderSize() == 0);
988 20 : REQUIRE(clone->getLevel() == g_expected_level);
989 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
990 20 : REQUIRE(clone->getName() == "filepath-test.txt");
991 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
992 20 : REQUIRE(clone->getSize() == file_size);
993 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
994 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
995 20 : REQUIRE_FALSE(clone->hasCrc());
996 20 : REQUIRE_FALSE(clone->isDirectory());
997 20 : REQUIRE(clone->isValid());
998 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_stats.st_size) + " bytes)");
999 20 : REQUIRE(clone->getExtra() == b);
1000 :
1001 : // reset the buffer
1002 40 : de.setExtra(zipios::FileEntry::buffer_t());
1003 : }
1004 :
1005 20 : SECTION("setting all levels, including many invalid ones")
1006 : {
1007 2002 : for(zipios::FileEntry::CompressionLevel level(-1000); level <= 1000; ++level)
1008 : {
1009 2001 : if(level >= -3 && level <= 100)
1010 : {
1011 : // this is considered valid for a standard file
1012 104 : de.setLevel(level);
1013 :
1014 104 : REQUIRE(de.getComment().empty());
1015 104 : REQUIRE(de.getCompressedSize() == file_size);
1016 104 : REQUIRE(de.getCrc() == 0);
1017 104 : REQUIRE(de.getEntryOffset() == 0);
1018 104 : REQUIRE(de.getExtra().empty());
1019 104 : REQUIRE(de.getHeaderSize() == 0);
1020 104 : REQUIRE(de.getLevel() == level);
1021 104 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1022 104 : REQUIRE(de.getName() == "filepath-test.txt");
1023 104 : REQUIRE(de.getFileName() == "filepath-test.txt");
1024 104 : REQUIRE(de.getSize() == file_size);
1025 104 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1026 104 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1027 104 : REQUIRE_FALSE(de.hasCrc());
1028 104 : REQUIRE_FALSE(de.isDirectory());
1029 104 : REQUIRE(de.isValid());
1030 104 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1031 : }
1032 : else
1033 : {
1034 1897 : REQUIRE_THROWS_AS(de.setLevel(level), zipios::InvalidStateException);
1035 : }
1036 : }
1037 :
1038 : // restore before continuing test
1039 1 : de.setLevel(g_expected_level);
1040 20 : }
1041 :
1042 : {
1043 : // set a method other than STORED, which is 1, so just us % 8 instead of % 9 and do a +1
1044 20 : de.setMethod(zipios::StorageMethod::DEFLATED);
1045 :
1046 20 : REQUIRE(de.getComment().empty());
1047 20 : REQUIRE(de.getCompressedSize() == file_size);
1048 20 : REQUIRE(de.getCrc() == 0);
1049 20 : REQUIRE(de.getEntryOffset() == 0);
1050 20 : REQUIRE(de.getExtra().empty());
1051 20 : REQUIRE(de.getHeaderSize() == 0);
1052 20 : REQUIRE(de.getLevel() == g_expected_level);
1053 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::DEFLATED);
1054 20 : REQUIRE(de.getName() == "filepath-test.txt");
1055 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
1056 20 : REQUIRE(de.getSize() == file_size);
1057 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1058 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1059 20 : REQUIRE_FALSE(de.hasCrc());
1060 20 : REQUIRE_FALSE(de.isDirectory());
1061 20 : REQUIRE(de.isValid());
1062 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1063 :
1064 : // attempt a clone now, should have the same content
1065 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1066 :
1067 20 : REQUIRE(clone->getComment().empty());
1068 20 : REQUIRE(clone->getCompressedSize() == file_size);
1069 20 : REQUIRE(clone->getCrc() == 0);
1070 20 : REQUIRE(clone->getEntryOffset() == 0);
1071 20 : REQUIRE(clone->getExtra().empty());
1072 20 : REQUIRE(clone->getHeaderSize() == 0);
1073 20 : REQUIRE(clone->getLevel() == g_expected_level);
1074 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::DEFLATED);
1075 20 : REQUIRE(clone->getName() == "filepath-test.txt");
1076 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
1077 20 : REQUIRE(clone->getSize() == file_size);
1078 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1079 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1080 20 : REQUIRE_FALSE(clone->hasCrc());
1081 20 : REQUIRE_FALSE(clone->isDirectory());
1082 20 : REQUIRE(clone->isValid());
1083 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1084 : }
1085 :
1086 : {
1087 : // zero would not really prove anything so skip such
1088 : // (although it may be extremely rare...)
1089 : size_t r;
1090 : {
1091 20 : r = zipios_test::rand_size_t();
1092 : }
1093 20 : while(r == 0);
1094 20 : de.setSize(r);
1095 :
1096 20 : REQUIRE(de.getComment().empty());
1097 20 : REQUIRE(de.getCompressedSize() == r);
1098 20 : REQUIRE(de.getCrc() == 0);
1099 20 : REQUIRE(de.getEntryOffset() == 0);
1100 20 : REQUIRE(de.getExtra().empty());
1101 20 : REQUIRE(de.getHeaderSize() == 0);
1102 20 : REQUIRE(de.getLevel() == g_expected_level);
1103 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::DEFLATED);
1104 20 : REQUIRE(de.getName() == "filepath-test.txt");
1105 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
1106 20 : REQUIRE(de.getSize() == r);
1107 20 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1108 20 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1109 20 : REQUIRE(!de.hasCrc());
1110 20 : REQUIRE(!de.isDirectory());
1111 20 : REQUIRE(de.isValid());
1112 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(r) + " bytes)");
1113 :
1114 : // attempt a clone now, should have the same content
1115 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1116 :
1117 20 : REQUIRE(clone->getComment().empty());
1118 20 : REQUIRE(clone->getCompressedSize() == r);
1119 20 : REQUIRE(clone->getCrc() == 0);
1120 20 : REQUIRE(clone->getEntryOffset() == 0);
1121 20 : REQUIRE(clone->getExtra().empty());
1122 20 : REQUIRE(clone->getHeaderSize() == 0);
1123 20 : REQUIRE(clone->getLevel() == g_expected_level);
1124 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::DEFLATED);
1125 20 : REQUIRE(clone->getName() == "filepath-test.txt");
1126 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
1127 20 : REQUIRE(clone->getSize() == r);
1128 20 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1129 20 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1130 20 : REQUIRE(!clone->hasCrc());
1131 20 : REQUIRE(!clone->isDirectory());
1132 20 : REQUIRE(clone->isValid());
1133 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(r) + " bytes)");
1134 :
1135 20 : de.setSize(file_size);
1136 : }
1137 :
1138 : {
1139 : // DOS time numbers are not linear so we use a Unix date and
1140 : // convert to DOS time (since we know our convertor works)
1141 : //
1142 : // Jan 1, 1980 at 00:00:00 is 315561600 (min)
1143 : // Dec 31, 2107 at 23:59:59 is 4354847999 (max)
1144 20 : time_t t(static_cast<time_t>(zipios_test::rand_size_t()) % (4354848000LL - 315561600LL) + 315561600);
1145 20 : dostime_t r(unix2dostime(t));
1146 20 : de.setTime(r);
1147 :
1148 20 : REQUIRE(de.getComment().empty());
1149 20 : REQUIRE(de.getCompressedSize() == file_size);
1150 20 : REQUIRE(de.getCrc() == 0);
1151 20 : REQUIRE(de.getEntryOffset() == 0);
1152 20 : REQUIRE(de.getExtra().empty());
1153 20 : REQUIRE(de.getHeaderSize() == 0);
1154 20 : REQUIRE(de.getLevel() == g_expected_level);
1155 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::DEFLATED);
1156 20 : REQUIRE(de.getName() == "filepath-test.txt");
1157 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
1158 20 : REQUIRE(de.getSize() == file_size);
1159 20 : REQUIRE(de.getTime() == r);
1160 20 : REQUIRE(de.getUnixTime() == dos2unixtime(r)); // WARNING: r is not always equal to t because setTime() may use the next even second
1161 20 : REQUIRE(!de.hasCrc());
1162 20 : REQUIRE(!de.isDirectory());
1163 20 : REQUIRE(de.isValid());
1164 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1165 :
1166 : // attempt a clone now, should have the same content
1167 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1168 :
1169 20 : REQUIRE(clone->getComment().empty());
1170 20 : REQUIRE(clone->getCompressedSize() == file_size);
1171 20 : REQUIRE(clone->getCrc() == 0);
1172 20 : REQUIRE(clone->getEntryOffset() == 0);
1173 20 : REQUIRE(clone->getExtra().empty());
1174 20 : REQUIRE(clone->getHeaderSize() == 0);
1175 20 : REQUIRE(clone->getLevel() == g_expected_level);
1176 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::DEFLATED);
1177 20 : REQUIRE(clone->getName() == "filepath-test.txt");
1178 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
1179 20 : REQUIRE(clone->getSize() == file_size);
1180 20 : REQUIRE(clone->getTime() == r);
1181 20 : REQUIRE(clone->getUnixTime() == dos2unixtime(r));
1182 20 : REQUIRE(!clone->hasCrc());
1183 20 : REQUIRE(!clone->isDirectory());
1184 20 : REQUIRE(clone->isValid());
1185 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1186 : }
1187 :
1188 : {
1189 : // DOS time are limited to a smaller range and on every other
1190 : // second so we get a valid DOS time and convert it to a Unix time
1191 20 : time_t r(static_cast<time_t>(zipios_test::rand_size_t()) % (4354848000LL - 315561600LL) + 315561600);
1192 20 : de.setUnixTime(r);
1193 :
1194 20 : REQUIRE(de.getComment().empty());
1195 20 : REQUIRE(de.getCompressedSize() == file_size);
1196 20 : REQUIRE(de.getCrc() == 0);
1197 20 : REQUIRE(de.getEntryOffset() == 0);
1198 20 : REQUIRE(de.getExtra().empty());
1199 20 : REQUIRE(de.getHeaderSize() == 0);
1200 20 : REQUIRE(de.getLevel() == g_expected_level);
1201 20 : REQUIRE(de.getMethod() == zipios::StorageMethod::DEFLATED);
1202 20 : REQUIRE(de.getName() == "filepath-test.txt");
1203 20 : REQUIRE(de.getFileName() == "filepath-test.txt");
1204 20 : REQUIRE(de.getSize() == file_size);
1205 20 : REQUIRE(de.getTime() == unix2dostime(r));
1206 20 : REQUIRE(de.getUnixTime() == r);
1207 20 : REQUIRE(!de.hasCrc());
1208 20 : REQUIRE(!de.isDirectory());
1209 20 : REQUIRE(de.isValid());
1210 20 : REQUIRE(de.toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1211 :
1212 : // attempt a clone now, should have the same content
1213 20 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1214 :
1215 20 : REQUIRE(clone->getComment().empty());
1216 20 : REQUIRE(clone->getCompressedSize() == file_size);
1217 20 : REQUIRE(clone->getCrc() == 0);
1218 20 : REQUIRE(clone->getEntryOffset() == 0);
1219 20 : REQUIRE(clone->getExtra().empty());
1220 20 : REQUIRE(clone->getHeaderSize() == 0);
1221 20 : REQUIRE(clone->getLevel() == g_expected_level);
1222 20 : REQUIRE(clone->getMethod() == zipios::StorageMethod::DEFLATED);
1223 20 : REQUIRE(clone->getName() == "filepath-test.txt");
1224 20 : REQUIRE(clone->getFileName() == "filepath-test.txt");
1225 20 : REQUIRE(clone->getSize() == file_size);
1226 20 : REQUIRE(clone->getTime() == unix2dostime(r));
1227 20 : REQUIRE(clone->getUnixTime() == r);
1228 20 : REQUIRE(!clone->hasCrc());
1229 20 : REQUIRE(!clone->isDirectory());
1230 20 : REQUIRE(clone->isValid());
1231 20 : REQUIRE(clone->toString() == "filepath-test.txt (" + std::to_string(file_size) + " bytes)");
1232 20 : }
1233 : }
1234 :
1235 20 : unlink("filepath-test.txt");
1236 : }
1237 2 : }
1238 :
1239 :
1240 21 : SCENARIO("DirectoryEntry for a valid directory", "[DirectoryEntry] [FileEntry]")
1241 : {
1242 20 : GIVEN("test an existing directory and no comment")
1243 : {
1244 : // make sure the directory is gone before re-creating it
1245 19 : static_cast<void>(system("rm -rf filepath-test"));
1246 :
1247 : // create a directory
1248 19 : REQUIRE(mkdir("filepath-test", 0777) == 0);
1249 :
1250 19 : zipios::DirectoryEntry de(zipios::FilePath("filepath-test"), "");
1251 :
1252 : struct stat file_stats;
1253 19 : REQUIRE(stat("filepath-test", &file_stats) == 0);
1254 :
1255 : // first, check that the object is setup as expected
1256 19 : SECTION("verify that the object looks as expected")
1257 : {
1258 1 : REQUIRE(de.getComment().empty());
1259 1 : REQUIRE(de.getCompressedSize() == 0);
1260 1 : REQUIRE(de.getCrc() == 0);
1261 1 : REQUIRE(de.getEntryOffset() == 0);
1262 1 : REQUIRE(de.getExtra().empty());
1263 1 : REQUIRE(de.getHeaderSize() == 0);
1264 1 : REQUIRE(de.getLevel() == g_directory_level);
1265 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1266 1 : REQUIRE(de.getName() == "filepath-test");
1267 1 : REQUIRE(de.getFileName() == "filepath-test");
1268 1 : REQUIRE(de.getSize() == 0);
1269 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1270 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1271 1 : REQUIRE(!de.hasCrc());
1272 1 : REQUIRE(de.isDirectory());
1273 1 : REQUIRE(de.isValid());
1274 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1275 :
1276 : // attempt a clone now, should have the same content
1277 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1278 :
1279 1 : REQUIRE(clone->getComment().empty());
1280 1 : REQUIRE(clone->getCompressedSize() == 0);
1281 1 : REQUIRE(clone->getCrc() == 0);
1282 1 : REQUIRE(clone->getEntryOffset() == 0);
1283 1 : REQUIRE(clone->getExtra().empty());
1284 1 : REQUIRE(clone->getHeaderSize() == 0);
1285 1 : REQUIRE(clone->getLevel() == g_directory_level);
1286 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1287 1 : REQUIRE(clone->getName() == "filepath-test");
1288 1 : REQUIRE(clone->getFileName() == "filepath-test");
1289 1 : REQUIRE(clone->getSize() == 0);
1290 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1291 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1292 1 : REQUIRE_FALSE(clone->hasCrc());
1293 1 : REQUIRE(clone->isDirectory());
1294 1 : REQUIRE(clone->isValid());
1295 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1296 19 : }
1297 :
1298 19 : WHEN("setting the comment")
1299 : {
1300 2 : de.setComment("new comment");
1301 :
1302 2 : THEN("we can read it and nothing else changed")
1303 : {
1304 1 : REQUIRE(de.getComment() == "new comment");
1305 1 : REQUIRE(de.getCompressedSize() == 0);
1306 1 : REQUIRE(de.getCrc() == 0);
1307 1 : REQUIRE(de.getEntryOffset() == 0);
1308 1 : REQUIRE(de.getExtra().empty());
1309 1 : REQUIRE(de.getHeaderSize() == 0);
1310 1 : REQUIRE(de.getLevel() == g_directory_level);
1311 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1312 1 : REQUIRE(de.getName() == "filepath-test");
1313 1 : REQUIRE(de.getFileName() == "filepath-test");
1314 1 : REQUIRE(de.getSize() == 0);
1315 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1316 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1317 1 : REQUIRE_FALSE(de.hasCrc());
1318 1 : REQUIRE(de.isDirectory());
1319 1 : REQUIRE(de.isValid());
1320 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1321 :
1322 : // attempt a clone now, should have the same content
1323 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1324 :
1325 1 : REQUIRE(clone->getComment() == "new comment");
1326 1 : REQUIRE(clone->getCompressedSize() == 0);
1327 1 : REQUIRE(clone->getCrc() == 0);
1328 1 : REQUIRE(clone->getEntryOffset() == 0);
1329 1 : REQUIRE(clone->getExtra().empty());
1330 1 : REQUIRE(clone->getHeaderSize() == 0);
1331 1 : REQUIRE(clone->getLevel() == g_directory_level);
1332 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1333 1 : REQUIRE(clone->getName() == "filepath-test");
1334 1 : REQUIRE(clone->getFileName() == "filepath-test");
1335 1 : REQUIRE(clone->getSize() == 0);
1336 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1337 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1338 1 : REQUIRE_FALSE(clone->hasCrc());
1339 1 : REQUIRE(clone->isDirectory());
1340 1 : REQUIRE(clone->isValid());
1341 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1342 2 : }
1343 19 : }
1344 :
1345 19 : WHEN("setting the compressed size")
1346 : {
1347 : // zero would not really prove anything so skip such
1348 : // (although it may be extremely rare...)
1349 : size_t r;
1350 2 : do
1351 : {
1352 2 : r = zipios_test::rand_size_t();
1353 : }
1354 : while(r == 0);
1355 2 : de.setCompressedSize(r);
1356 :
1357 2 : THEN("we ignore it")
1358 : {
1359 1 : REQUIRE(de.getComment().empty());
1360 1 : REQUIRE(de.getCompressedSize() == 0);
1361 1 : REQUIRE(de.getCrc() == 0);
1362 1 : REQUIRE(de.getEntryOffset() == 0);
1363 1 : REQUIRE(de.getExtra().empty());
1364 1 : REQUIRE(de.getHeaderSize() == 0);
1365 1 : REQUIRE(de.getLevel() == g_directory_level);
1366 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1367 1 : REQUIRE(de.getName() == "filepath-test");
1368 1 : REQUIRE(de.getFileName() == "filepath-test");
1369 1 : REQUIRE(de.getSize() == 0);
1370 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1371 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1372 1 : REQUIRE_FALSE(de.hasCrc());
1373 1 : REQUIRE(de.isDirectory());
1374 1 : REQUIRE(de.isValid());
1375 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1376 :
1377 : // attempt a clone now, should have the same content
1378 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1379 :
1380 1 : REQUIRE(clone->getComment().empty());
1381 1 : REQUIRE(clone->getCompressedSize() == 0);
1382 1 : REQUIRE(clone->getCrc() == 0);
1383 1 : REQUIRE(clone->getEntryOffset() == 0);
1384 1 : REQUIRE(clone->getExtra().empty());
1385 1 : REQUIRE(clone->getHeaderSize() == 0);
1386 1 : REQUIRE(clone->getLevel() == g_directory_level);
1387 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1388 1 : REQUIRE(clone->getName() == "filepath-test");
1389 1 : REQUIRE(clone->getFileName() == "filepath-test");
1390 1 : REQUIRE(clone->getSize() == 0);
1391 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1392 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1393 1 : REQUIRE_FALSE(clone->hasCrc());
1394 1 : REQUIRE(clone->isDirectory());
1395 1 : REQUIRE(clone->isValid());
1396 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1397 2 : }
1398 19 : }
1399 :
1400 19 : WHEN("setting the CRC")
1401 : {
1402 : // zero would not really prove anything so skip such
1403 : uint32_t r;
1404 2 : do
1405 : {
1406 2 : r = rand();
1407 : }
1408 : while(r == 0);
1409 2 : de.setCrc(rand());
1410 :
1411 2 : THEN("we ignore it")
1412 : {
1413 1 : REQUIRE(de.getComment().empty());
1414 1 : REQUIRE(de.getCompressedSize() == 0);
1415 1 : REQUIRE(de.getCrc() == 0);
1416 1 : REQUIRE(de.getEntryOffset() == 0);
1417 1 : REQUIRE(de.getExtra().empty());
1418 1 : REQUIRE(de.getHeaderSize() == 0);
1419 1 : REQUIRE(de.getLevel() == g_directory_level);
1420 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1421 1 : REQUIRE(de.getName() == "filepath-test");
1422 1 : REQUIRE(de.getFileName() == "filepath-test");
1423 1 : REQUIRE(de.getSize() == 0);
1424 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1425 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1426 1 : REQUIRE(!de.hasCrc());
1427 1 : REQUIRE(de.isDirectory());
1428 1 : REQUIRE(de.isValid());
1429 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1430 :
1431 : // attempt a clone now, should have the same content
1432 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1433 :
1434 1 : REQUIRE(clone->getComment().empty());
1435 1 : REQUIRE(clone->getCompressedSize() == 0);
1436 1 : REQUIRE(clone->getCrc() == 0);
1437 1 : REQUIRE(clone->getEntryOffset() == 0);
1438 1 : REQUIRE(clone->getExtra().empty());
1439 1 : REQUIRE(clone->getHeaderSize() == 0);
1440 1 : REQUIRE(clone->getLevel() == g_directory_level);
1441 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1442 1 : REQUIRE(clone->getName() == "filepath-test");
1443 1 : REQUIRE(clone->getFileName() == "filepath-test");
1444 1 : REQUIRE(clone->getSize() == 0);
1445 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1446 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1447 1 : REQUIRE(!clone->hasCrc());
1448 1 : REQUIRE(clone->isDirectory());
1449 1 : REQUIRE(clone->isValid());
1450 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1451 2 : }
1452 19 : }
1453 :
1454 19 : WHEN("setting an extra buffer")
1455 : {
1456 : // zero would not really prove anything so skip such
1457 2 : zipios::FileEntry::buffer_t b;
1458 2 : uint32_t size(rand() % 100 + 20);
1459 167 : for(uint32_t i(0); i < size; ++i)
1460 : {
1461 165 : b.push_back(rand());
1462 : }
1463 2 : de.setExtra(b);
1464 :
1465 2 : THEN("we ignore it")
1466 : {
1467 1 : REQUIRE(de.getComment().empty());
1468 1 : REQUIRE(de.getCompressedSize() == 0);
1469 1 : REQUIRE(de.getCrc() == 0);
1470 1 : REQUIRE(de.getEntryOffset() == 0);
1471 1 : REQUIRE_FALSE(de.getExtra().empty());
1472 1 : REQUIRE(de.getHeaderSize() == 0);
1473 1 : REQUIRE(de.getLevel() == g_directory_level);
1474 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1475 1 : REQUIRE(de.getName() == "filepath-test");
1476 1 : REQUIRE(de.getFileName() == "filepath-test");
1477 1 : REQUIRE(de.getSize() == 0);
1478 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1479 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1480 1 : REQUIRE(!de.hasCrc());
1481 1 : REQUIRE(de.isDirectory());
1482 1 : REQUIRE(de.isValid());
1483 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1484 1 : REQUIRE(de.getExtra() == b);
1485 :
1486 : // attempt a clone now, should have the same content
1487 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1488 :
1489 1 : REQUIRE(clone->getComment().empty());
1490 1 : REQUIRE(clone->getCompressedSize() == 0);
1491 1 : REQUIRE(clone->getCrc() == 0);
1492 1 : REQUIRE(clone->getEntryOffset() == 0);
1493 1 : REQUIRE_FALSE(clone->getExtra().empty());
1494 1 : REQUIRE(clone->getHeaderSize() == 0);
1495 1 : REQUIRE(clone->getLevel() == g_directory_level);
1496 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1497 1 : REQUIRE(clone->getName() == "filepath-test");
1498 1 : REQUIRE(clone->getFileName() == "filepath-test");
1499 1 : REQUIRE(clone->getSize() == 0);
1500 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1501 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1502 1 : REQUIRE(!clone->hasCrc());
1503 1 : REQUIRE(clone->isDirectory());
1504 1 : REQUIRE(clone->isValid());
1505 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1506 1 : REQUIRE(clone->getExtra() == b);
1507 2 : }
1508 19 : }
1509 :
1510 19 : SECTION("setting all levels, including many invalid ones")
1511 : {
1512 2002 : for(zipios::FileEntry::CompressionLevel level(-1000); level <= 1000; ++level)
1513 : {
1514 : // directories do not accept values from 1 to 100
1515 2001 : if(level >= -3 && level <= 0)
1516 : {
1517 : // this is considered valid for a standard file
1518 4 : de.setLevel(level);
1519 :
1520 4 : REQUIRE(de.getComment().empty());
1521 4 : REQUIRE(de.getCompressedSize() == 0);
1522 4 : REQUIRE(de.getCrc() == 0);
1523 4 : REQUIRE(de.getEntryOffset() == 0);
1524 4 : REQUIRE(de.getExtra().empty());
1525 4 : REQUIRE(de.getHeaderSize() == 0);
1526 4 : REQUIRE(de.getLevel() == g_directory_level);
1527 4 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1528 4 : REQUIRE(de.getName() == "filepath-test");
1529 4 : REQUIRE(de.getFileName() == "filepath-test");
1530 4 : REQUIRE(de.getSize() == 0);
1531 4 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1532 4 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1533 4 : REQUIRE_FALSE(de.hasCrc());
1534 4 : REQUIRE(de.isDirectory());
1535 4 : REQUIRE(de.isValid());
1536 4 : REQUIRE(de.toString() == "filepath-test (directory)");
1537 : }
1538 : else
1539 : {
1540 1997 : REQUIRE_THROWS_AS(de.setLevel(level), zipios::InvalidStateException);
1541 : }
1542 : }
1543 :
1544 : // restore before continuing test
1545 1 : de.setLevel(g_expected_level);
1546 19 : }
1547 :
1548 19 : WHEN("setting the method")
1549 : {
1550 : // set to DEFLATED which has no effect because the de is a
1551 : // directory and directories accept DEFLATED but ignore it
1552 2 : de.setMethod(zipios::StorageMethod::DEFLATED);
1553 :
1554 2 : THEN("we ignore it")
1555 : {
1556 1 : REQUIRE(de.getComment().empty());
1557 1 : REQUIRE(de.getCompressedSize() == 0);
1558 1 : REQUIRE(de.getCrc() == 0);
1559 1 : REQUIRE(de.getEntryOffset() == 0);
1560 1 : REQUIRE(de.getExtra().empty());
1561 1 : REQUIRE(de.getHeaderSize() == 0);
1562 1 : REQUIRE(de.getLevel() == g_directory_level);
1563 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1564 1 : REQUIRE(de.getName() == "filepath-test");
1565 1 : REQUIRE(de.getFileName() == "filepath-test");
1566 1 : REQUIRE(de.getSize() == 0);
1567 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1568 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1569 1 : REQUIRE_FALSE(de.hasCrc());
1570 1 : REQUIRE(de.isDirectory());
1571 1 : REQUIRE(de.isValid());
1572 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1573 :
1574 : // attempt a clone now, should have the same content
1575 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1576 :
1577 1 : REQUIRE(clone->getComment().empty());
1578 1 : REQUIRE(clone->getCompressedSize() == 0);
1579 1 : REQUIRE(clone->getCrc() == 0);
1580 1 : REQUIRE(clone->getEntryOffset() == 0);
1581 1 : REQUIRE(clone->getExtra().empty());
1582 1 : REQUIRE(clone->getHeaderSize() == 0);
1583 1 : REQUIRE(clone->getLevel() == g_directory_level);
1584 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1585 1 : REQUIRE(clone->getName() == "filepath-test");
1586 1 : REQUIRE(clone->getFileName() == "filepath-test");
1587 1 : REQUIRE(clone->getSize() == 0);
1588 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1589 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1590 1 : REQUIRE_FALSE(clone->hasCrc());
1591 1 : REQUIRE(clone->isDirectory());
1592 1 : REQUIRE(clone->isValid());
1593 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1594 2 : }
1595 19 : }
1596 :
1597 19 : WHEN("setting the uncompressed size")
1598 : {
1599 : // zero would not really prove anything so skip such
1600 : // (although it may be extremely rare...)
1601 : size_t r;
1602 2 : do
1603 : {
1604 2 : r = zipios_test::rand_size_t();
1605 : }
1606 2 : while(r == 0);
1607 2 : de.setSize(r);
1608 :
1609 2 : THEN("we take it as is")
1610 : {
1611 1 : REQUIRE(de.getComment().empty());
1612 1 : REQUIRE(de.getCompressedSize() == r);
1613 1 : REQUIRE(de.getCrc() == 0);
1614 1 : REQUIRE(de.getEntryOffset() == 0);
1615 1 : REQUIRE(de.getExtra().empty());
1616 1 : REQUIRE(de.getHeaderSize() == 0);
1617 1 : REQUIRE(de.getLevel() == g_directory_level);
1618 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1619 1 : REQUIRE(de.getName() == "filepath-test");
1620 1 : REQUIRE(de.getFileName() == "filepath-test");
1621 1 : REQUIRE(de.getSize() == r);
1622 1 : REQUIRE(de.getTime() == unix2dostime(file_stats.st_mtime));
1623 1 : REQUIRE(de.getUnixTime() == file_stats.st_mtime);
1624 1 : REQUIRE(!de.hasCrc());
1625 1 : REQUIRE(de.isDirectory());
1626 1 : REQUIRE(de.isValid());
1627 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1628 :
1629 : // attempt a clone now, should have the same content
1630 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1631 :
1632 1 : REQUIRE(clone->getComment().empty());
1633 1 : REQUIRE(clone->getCompressedSize() == r);
1634 1 : REQUIRE(clone->getCrc() == 0);
1635 1 : REQUIRE(clone->getEntryOffset() == 0);
1636 1 : REQUIRE(clone->getExtra().empty());
1637 1 : REQUIRE(clone->getHeaderSize() == 0);
1638 1 : REQUIRE(clone->getLevel() == g_directory_level);
1639 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1640 1 : REQUIRE(clone->getName() == "filepath-test");
1641 1 : REQUIRE(clone->getFileName() == "filepath-test");
1642 1 : REQUIRE(clone->getSize() == r);
1643 1 : REQUIRE(clone->getTime() == unix2dostime(file_stats.st_mtime));
1644 1 : REQUIRE(clone->getUnixTime() == file_stats.st_mtime);
1645 1 : REQUIRE(!clone->hasCrc());
1646 1 : REQUIRE(clone->isDirectory());
1647 1 : REQUIRE(clone->isValid());
1648 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1649 2 : }
1650 19 : }
1651 :
1652 19 : WHEN("setting the DOS time")
1653 : {
1654 : // DOS time numbers are not linear so we test until we get one
1655 : // that works...
1656 2 : time_t t(static_cast<time_t>(zipios_test::rand_size_t()) % (4354848000LL - 315561600LL) + 315561600);
1657 2 : dostime_t r(unix2dostime(t));
1658 2 : de.setTime(r);
1659 :
1660 2 : THEN("we take it as is")
1661 : {
1662 1 : REQUIRE(de.getComment().empty());
1663 1 : REQUIRE(de.getCompressedSize() == 0);
1664 1 : REQUIRE(de.getCrc() == 0);
1665 1 : REQUIRE(de.getEntryOffset() == 0);
1666 1 : REQUIRE(de.getExtra().empty());
1667 1 : REQUIRE(de.getHeaderSize() == 0);
1668 1 : REQUIRE(de.getLevel() == g_directory_level);
1669 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1670 1 : REQUIRE(de.getName() == "filepath-test");
1671 1 : REQUIRE(de.getFileName() == "filepath-test");
1672 1 : REQUIRE(de.getSize() == 0);
1673 1 : REQUIRE(de.getTime() == r);
1674 1 : REQUIRE(de.getUnixTime() == dos2unixtime(r));
1675 1 : REQUIRE(!de.hasCrc());
1676 1 : REQUIRE(de.isDirectory());
1677 1 : REQUIRE(de.isValid());
1678 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1679 :
1680 : // attempt a clone now, should have the same content
1681 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1682 :
1683 1 : REQUIRE(clone->getComment().empty());
1684 1 : REQUIRE(clone->getCompressedSize() == 0);
1685 1 : REQUIRE(clone->getCrc() == 0);
1686 1 : REQUIRE(clone->getEntryOffset() == 0);
1687 1 : REQUIRE(clone->getExtra().empty());
1688 1 : REQUIRE(clone->getHeaderSize() == 0);
1689 1 : REQUIRE(clone->getLevel() == g_directory_level);
1690 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1691 1 : REQUIRE(clone->getName() == "filepath-test");
1692 1 : REQUIRE(clone->getFileName() == "filepath-test");
1693 1 : REQUIRE(clone->getSize() == 0);
1694 1 : REQUIRE(clone->getTime() == r);
1695 1 : REQUIRE(clone->getUnixTime() == dos2unixtime(r));
1696 1 : REQUIRE(!clone->hasCrc());
1697 1 : REQUIRE(clone->isDirectory());
1698 1 : REQUIRE(clone->isValid());
1699 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1700 2 : }
1701 19 : }
1702 :
1703 19 : WHEN("setting the Unix time")
1704 : {
1705 : // DOS time are limited to a smaller range and on every other
1706 : // second so we get a valid DOS time and convert it to a Unix time
1707 2 : time_t r(static_cast<time_t>(zipios_test::rand_size_t()) % (4354848000LL - 315561600LL) + 315561600);
1708 2 : de.setUnixTime(r);
1709 :
1710 2 : THEN("we take it as is")
1711 : {
1712 1 : REQUIRE(de.getComment().empty());
1713 1 : REQUIRE(de.getCompressedSize() == 0);
1714 1 : REQUIRE(de.getCrc() == 0);
1715 1 : REQUIRE(de.getEntryOffset() == 0);
1716 1 : REQUIRE(de.getExtra().empty());
1717 1 : REQUIRE(de.getHeaderSize() == 0);
1718 1 : REQUIRE(de.getLevel() == g_directory_level);
1719 1 : REQUIRE(de.getMethod() == zipios::StorageMethod::STORED);
1720 1 : REQUIRE(de.getName() == "filepath-test");
1721 1 : REQUIRE(de.getFileName() == "filepath-test");
1722 1 : REQUIRE(de.getSize() == 0);
1723 1 : REQUIRE(de.getTime() == unix2dostime(r));
1724 1 : REQUIRE(de.getUnixTime() == r);
1725 1 : REQUIRE(!de.hasCrc());
1726 1 : REQUIRE(de.isDirectory());
1727 1 : REQUIRE(de.isValid());
1728 1 : REQUIRE(de.toString() == "filepath-test (directory)");
1729 :
1730 : // attempt a clone now, should have the same content
1731 1 : zipios::DirectoryEntry::pointer_t clone(de.clone());
1732 :
1733 1 : REQUIRE(clone->getComment().empty());
1734 1 : REQUIRE(clone->getCompressedSize() == 0);
1735 1 : REQUIRE(clone->getCrc() == 0);
1736 1 : REQUIRE(clone->getEntryOffset() == 0);
1737 1 : REQUIRE(clone->getExtra().empty());
1738 1 : REQUIRE(clone->getHeaderSize() == 0);
1739 1 : REQUIRE(clone->getLevel() == g_directory_level);
1740 1 : REQUIRE(clone->getMethod() == zipios::StorageMethod::STORED);
1741 1 : REQUIRE(clone->getName() == "filepath-test");
1742 1 : REQUIRE(clone->getFileName() == "filepath-test");
1743 1 : REQUIRE(clone->getSize() == 0);
1744 1 : REQUIRE(clone->getTime() == unix2dostime(r));
1745 1 : REQUIRE(clone->getUnixTime() == r);
1746 1 : REQUIRE(!clone->hasCrc());
1747 1 : REQUIRE(clone->isDirectory());
1748 1 : REQUIRE(clone->isValid());
1749 1 : REQUIRE(clone->toString() == "filepath-test (directory)");
1750 2 : }
1751 19 : }
1752 :
1753 19 : rmdir("filepath-test");
1754 20 : }
1755 23 : }
1756 :
1757 :
1758 :
1759 :
1760 : // Local Variables:
1761 : // mode: cpp
1762 : // indent-tabs-mode: nil
1763 : // c-basic-offset: 4
1764 : // tab-width: 4
1765 : // End:
1766 :
1767 : // vim: ts=4 sw=4 et
|