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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libjar/zipwriter/test/unit/test_zippermissions.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     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
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 + */
     1.8 +
     1.9 +const DATA = "ZIP WRITER TEST DATA";
    1.10 +
    1.11 +var TESTS = [];
    1.12 +
    1.13 +function build_tests() {
    1.14 +  var id = 0;
    1.15 +
    1.16 +  // Minimum mode is 0o400
    1.17 +  for (let u = 4; u <= 7; u++) {
    1.18 +    for (let g = 0; g <= 7; g++) {
    1.19 +      for (let o = 0; o <= 7; o++) {
    1.20 +        TESTS[id] = {
    1.21 +          name: "test" + u + g + o,
    1.22 +          permission: (u << 6) + (g << 3) + o
    1.23 +        };
    1.24 +        id++;
    1.25 +      }
    1.26 +    }
    1.27 +  }
    1.28 +}
    1.29 +
    1.30 +function run_test() {
    1.31 +  build_tests();
    1.32 +
    1.33 +  var foStream = Cc["@mozilla.org/network/file-output-stream;1"].
    1.34 +                 createInstance(Ci.nsIFileOutputStream);
    1.35 +
    1.36 +  var tmp = tmpDir.clone();
    1.37 +  tmp.append("temp-permissions");
    1.38 +  tmp.createUnique(Ci.nsILocalFile.DIRECTORY_TYPE, 0755);
    1.39 +
    1.40 +  var file = tmp.clone();
    1.41 +  file.append("tempfile");
    1.42 +
    1.43 +  zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
    1.44 +  for (let i = 0; i < TESTS.length; i++) {
    1.45 +    // Open the file with the permissions to match how the zipreader extracts
    1.46 +    // This obeys the umask
    1.47 +    foStream.init(file, 0x02 | 0x08 | 0x20, TESTS[i].permission, 0);
    1.48 +    foStream.close();
    1.49 +
    1.50 +    // umask may have altered the permissions so test against what they really were.
    1.51 +    // This reduces the coverage of the test but there isn't much we can do
    1.52 +    var perm = file.permissions & 0xfff;
    1.53 +    if (TESTS[i].permission != perm) {
    1.54 +      dump("File permissions for " + TESTS[i].name + " were " + perm.toString(8) + "\n");
    1.55 +      TESTS[i].permission = perm;
    1.56 +    }
    1.57 +
    1.58 +    zipW.addEntryFile(TESTS[i].name, Ci.nsIZipWriter.COMPRESSION_NONE, file, false);
    1.59 +    do_check_eq(zipW.getEntry(TESTS[i].name).permissions, TESTS[i].permission | 0o400);
    1.60 +    file.permissions = 0o600;
    1.61 +    file.remove(true);
    1.62 +  }
    1.63 +  zipW.close();
    1.64 +
    1.65 +  zipW.open(tmpFile, PR_RDWR);
    1.66 +  for (let i = 0; i < TESTS.length; i++) {
    1.67 +    dump("Testing zipwriter file permissions for " + TESTS[i].name + "\n");
    1.68 +    do_check_eq(zipW.getEntry(TESTS[i].name).permissions, TESTS[i].permission | 0o400);
    1.69 +  }
    1.70 +  zipW.close();
    1.71 +
    1.72 +  var zipR = new ZipReader(tmpFile);
    1.73 +  for (let i = 0; i < TESTS.length; i++) {
    1.74 +    dump("Testing zipreader file permissions for " + TESTS[i].name + "\n");
    1.75 +    do_check_eq(zipR.getEntry(TESTS[i].name).permissions, TESTS[i].permission | 0o400);
    1.76 +    dump("Testing extracted file permissions for " + TESTS[i].name + "\n");
    1.77 +    zipR.extract(TESTS[i].name, file);
    1.78 +    do_check_eq(file.permissions & 0xfff, TESTS[i].permission);
    1.79 +    do_check_false(file.isDirectory());
    1.80 +    file.permissions = 0o600;
    1.81 +    file.remove(true);
    1.82 +  }
    1.83 +  zipR.close();
    1.84 +
    1.85 +  tmp.remove(true);
    1.86 +}

mercurial