Backed out changeset 6efabfb144b1 (bug 940617) for mochitest-bc failures.
[gecko.git] / mozglue / tests / TestZip.cpp
blob389c38528171e52d58a23d1698954ac71e7cda1e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include <cstdio>
6 #include <unistd.h>
7 #include "Zip.h"
8 #include "mozilla/RefPtr.h"
10 extern "C" void report_mapping() { }
12 /**
13 * test.zip is a basic test zip file with a central directory. It contains
14 * four entries, in the following order:
15 * "foo", "bar", "baz", "qux".
16 * The entries are going to be read out of order.
18 const char *test_entries[] = {
19 "baz", "foo", "bar", "qux"
22 /**
23 * no_central_dir.zip is a hand crafted test zip with no central directory
24 * entries. The Zip reader is expected to be able to traverse these entries
25 * if requested in order, without reading a central directory
26 * - First entry is a file "a", STOREd.
27 * - Second entry is a file "b", STOREd, using a data descriptor. CRC is
28 * unknown, but compressed and uncompressed sizes are known in the local
29 * file header.
30 * - Third entry is a file "c", DEFLATEd, using a data descriptor. CRC,
31 * compressed and uncompressed sizes are known in the local file header.
32 * This is the kind of entry that can be found in a zip that went through
33 * zipalign if it had a data descriptor originally.
34 * - Fourth entry is a file "d", STOREd.
36 const char *no_central_dir_entries[] = {
37 "a", "b", "c", "d"
40 int main(int argc, char *argv[])
42 if (argc != 2) {
43 fprintf(stderr, "TEST-FAIL | TestZip | Expecting the directory containing test Zips\n");
44 return 1;
46 chdir(argv[1]);
47 Zip::Stream s;
48 mozilla::RefPtr<Zip> z = ZipCollection::GetZip("test.zip");
49 for (size_t i = 0; i < sizeof(test_entries) / sizeof(*test_entries); i++) {
50 if (!z->GetStream(test_entries[i], &s)) {
51 fprintf(stderr, "TEST-UNEXPECTED-FAIL | TestZip | test.zip: Couldn't get entry \"%s\"\n", test_entries[i]);
52 return 1;
55 fprintf(stderr, "TEST-PASS | TestZip | test.zip could be accessed fully\n");
57 z = ZipCollection::GetZip("no_central_dir.zip");
58 for (size_t i = 0; i < sizeof(no_central_dir_entries)
59 / sizeof(*no_central_dir_entries); i++) {
60 if (!z->GetStream(no_central_dir_entries[i], &s)) {
61 fprintf(stderr, "TEST-UNEXPECTED-FAIL | TestZip | no_central_dir.zip: Couldn't get entry \"%s\"\n", no_central_dir_entries[i]);
62 return 1;
65 fprintf(stderr, "TEST-PASS | TestZip | no_central_dir.zip could be accessed in order\n");
67 return 0;