tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_zippermissions.js (2752B)


      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 */
      5 
      6 var TESTS = [];
      7 
      8 function build_tests() {
      9  var id = 0;
     10 
     11  // Minimum mode is 0o400
     12  for (let u = 4; u <= 7; u++) {
     13    for (let g = 0; g <= 7; g++) {
     14      for (let o = 0; o <= 7; o++) {
     15        TESTS[id] = {
     16          name: "test" + u + g + o,
     17          permission: (u << 6) + (g << 3) + o,
     18        };
     19        id++;
     20      }
     21    }
     22  }
     23 }
     24 
     25 function run_test() {
     26  build_tests();
     27 
     28  var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
     29    Ci.nsIFileOutputStream
     30  );
     31 
     32  var tmp = tmpDir.clone();
     33  tmp.append("temp-permissions");
     34  tmp.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
     35 
     36  var file = tmp.clone();
     37  file.append("tempfile");
     38 
     39  zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
     40  for (let i = 0; i < TESTS.length; i++) {
     41    // Open the file with the permissions to match how the zipreader extracts
     42    // This obeys the umask
     43    foStream.init(file, 0x02 | 0x08 | 0x20, TESTS[i].permission, 0);
     44    foStream.close();
     45 
     46    // umask may have altered the permissions so test against what they really were.
     47    // This reduces the coverage of the test but there isn't much we can do
     48    var perm = file.permissions & 0xfff;
     49    if (TESTS[i].permission != perm) {
     50      dump(
     51        "File permissions for " +
     52          TESTS[i].name +
     53          " were " +
     54          perm.toString(8) +
     55          "\n"
     56      );
     57      TESTS[i].permission = perm;
     58    }
     59 
     60    zipW.addEntryFile(
     61      TESTS[i].name,
     62      Ci.nsIZipWriter.COMPRESSION_NONE,
     63      file,
     64      false
     65    );
     66    Assert.equal(
     67      zipW.getEntry(TESTS[i].name).permissions,
     68      TESTS[i].permission | 0o400
     69    );
     70    file.permissions = 0o600;
     71    file.remove(true);
     72  }
     73  zipW.close();
     74 
     75  zipW.open(tmpFile, PR_RDWR);
     76  for (let i = 0; i < TESTS.length; i++) {
     77    dump("Testing zipwriter file permissions for " + TESTS[i].name + "\n");
     78    Assert.equal(
     79      zipW.getEntry(TESTS[i].name).permissions,
     80      TESTS[i].permission | 0o400
     81    );
     82  }
     83  zipW.close();
     84 
     85  var zipR = new ZipReader(tmpFile);
     86  for (let i = 0; i < TESTS.length; i++) {
     87    dump("Testing zipreader file permissions for " + TESTS[i].name + "\n");
     88    Assert.equal(
     89      zipR.getEntry(TESTS[i].name).permissions,
     90      TESTS[i].permission | 0o400
     91    );
     92    dump("Testing extracted file permissions for " + TESTS[i].name + "\n");
     93    zipR.extract(TESTS[i].name, file);
     94    Assert.equal(file.permissions & 0xfff, TESTS[i].permission);
     95    Assert.ok(!file.isDirectory());
     96    file.permissions = 0o600;
     97    file.remove(true);
     98  }
     99  zipR.close();
    100 
    101  tmp.remove(true);
    102 }