1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mozglue/tests/TestZip.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include <cstdio> 1.9 +#include <unistd.h> 1.10 +#include "Zip.h" 1.11 +#include "mozilla/RefPtr.h" 1.12 + 1.13 +extern "C" void report_mapping() { } 1.14 + 1.15 +/** 1.16 + * test.zip is a basic test zip file with a central directory. It contains 1.17 + * four entries, in the following order: 1.18 + * "foo", "bar", "baz", "qux". 1.19 + * The entries are going to be read out of order. 1.20 + */ 1.21 +const char *test_entries[] = { 1.22 + "baz", "foo", "bar", "qux" 1.23 +}; 1.24 + 1.25 +/** 1.26 + * no_central_dir.zip is a hand crafted test zip with no central directory 1.27 + * entries. The Zip reader is expected to be able to traverse these entries 1.28 + * if requested in order, without reading a central directory 1.29 + * - First entry is a file "a", STOREd. 1.30 + * - Second entry is a file "b", STOREd, using a data descriptor. CRC is 1.31 + * unknown, but compressed and uncompressed sizes are known in the local 1.32 + * file header. 1.33 + * - Third entry is a file "c", DEFLATEd, using a data descriptor. CRC, 1.34 + * compressed and uncompressed sizes are known in the local file header. 1.35 + * This is the kind of entry that can be found in a zip that went through 1.36 + * zipalign if it had a data descriptor originally. 1.37 + * - Fourth entry is a file "d", STOREd. 1.38 + */ 1.39 +const char *no_central_dir_entries[] = { 1.40 + "a", "b", "c", "d" 1.41 +}; 1.42 + 1.43 +int main(int argc, char *argv[]) 1.44 +{ 1.45 + if (argc != 2) { 1.46 + fprintf(stderr, "TEST-FAIL | TestZip | Expecting the directory containing test Zips\n"); 1.47 + return 1; 1.48 + } 1.49 + chdir(argv[1]); 1.50 + Zip::Stream s; 1.51 + mozilla::RefPtr<Zip> z = ZipCollection::GetZip("test.zip"); 1.52 + for (size_t i = 0; i < sizeof(test_entries) / sizeof(*test_entries); i++) { 1.53 + if (!z->GetStream(test_entries[i], &s)) { 1.54 + fprintf(stderr, "TEST-UNEXPECTED-FAIL | TestZip | test.zip: Couldn't get entry \"%s\"\n", test_entries[i]); 1.55 + return 1; 1.56 + } 1.57 + } 1.58 + fprintf(stderr, "TEST-PASS | TestZip | test.zip could be accessed fully\n"); 1.59 + 1.60 + z = ZipCollection::GetZip("no_central_dir.zip"); 1.61 + for (size_t i = 0; i < sizeof(no_central_dir_entries) 1.62 + / sizeof(*no_central_dir_entries); i++) { 1.63 + if (!z->GetStream(no_central_dir_entries[i], &s)) { 1.64 + fprintf(stderr, "TEST-UNEXPECTED-FAIL | TestZip | no_central_dir.zip: Couldn't get entry \"%s\"\n", no_central_dir_entries[i]); 1.65 + return 1; 1.66 + } 1.67 + } 1.68 + fprintf(stderr, "TEST-PASS | TestZip | no_central_dir.zip could be accessed in order\n"); 1.69 + 1.70 + return 0; 1.71 +}