modules/libjar/zipwriter/test/unit/test_zippermissions.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  */
     6 const DATA = "ZIP WRITER TEST DATA";
     8 var TESTS = [];
    10 function build_tests() {
    11   var id = 0;
    13   // Minimum mode is 0o400
    14   for (let u = 4; u <= 7; u++) {
    15     for (let g = 0; g <= 7; g++) {
    16       for (let o = 0; o <= 7; o++) {
    17         TESTS[id] = {
    18           name: "test" + u + g + o,
    19           permission: (u << 6) + (g << 3) + o
    20         };
    21         id++;
    22       }
    23     }
    24   }
    25 }
    27 function run_test() {
    28   build_tests();
    30   var foStream = Cc["@mozilla.org/network/file-output-stream;1"].
    31                  createInstance(Ci.nsIFileOutputStream);
    33   var tmp = tmpDir.clone();
    34   tmp.append("temp-permissions");
    35   tmp.createUnique(Ci.nsILocalFile.DIRECTORY_TYPE, 0755);
    37   var file = tmp.clone();
    38   file.append("tempfile");
    40   zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
    41   for (let i = 0; i < TESTS.length; i++) {
    42     // Open the file with the permissions to match how the zipreader extracts
    43     // This obeys the umask
    44     foStream.init(file, 0x02 | 0x08 | 0x20, TESTS[i].permission, 0);
    45     foStream.close();
    47     // umask may have altered the permissions so test against what they really were.
    48     // This reduces the coverage of the test but there isn't much we can do
    49     var perm = file.permissions & 0xfff;
    50     if (TESTS[i].permission != perm) {
    51       dump("File permissions for " + TESTS[i].name + " were " + perm.toString(8) + "\n");
    52       TESTS[i].permission = perm;
    53     }
    55     zipW.addEntryFile(TESTS[i].name, Ci.nsIZipWriter.COMPRESSION_NONE, file, false);
    56     do_check_eq(zipW.getEntry(TESTS[i].name).permissions, TESTS[i].permission | 0o400);
    57     file.permissions = 0o600;
    58     file.remove(true);
    59   }
    60   zipW.close();
    62   zipW.open(tmpFile, PR_RDWR);
    63   for (let i = 0; i < TESTS.length; i++) {
    64     dump("Testing zipwriter file permissions for " + TESTS[i].name + "\n");
    65     do_check_eq(zipW.getEntry(TESTS[i].name).permissions, TESTS[i].permission | 0o400);
    66   }
    67   zipW.close();
    69   var zipR = new ZipReader(tmpFile);
    70   for (let i = 0; i < TESTS.length; i++) {
    71     dump("Testing zipreader file permissions for " + TESTS[i].name + "\n");
    72     do_check_eq(zipR.getEntry(TESTS[i].name).permissions, TESTS[i].permission | 0o400);
    73     dump("Testing extracted file permissions for " + TESTS[i].name + "\n");
    74     zipR.extract(TESTS[i].name, file);
    75     do_check_eq(file.permissions & 0xfff, TESTS[i].permission);
    76     do_check_false(file.isDirectory());
    77     file.permissions = 0o600;
    78     file.remove(true);
    79   }
    80   zipR.close();
    82   tmp.remove(true);
    83 }

mercurial