tor-browser

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

test_create.js (3875B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 function run_test() {
      5  /**
      6   * Creates MAR from the passed files, compares it to the reference MAR.
      7   *
      8   * @param refMARFileName The name of the MAR file that should match
      9   * @param files          The files that should go in the created MAR
     10   * @param checkNoMAR     If true return an error if a file already exists
     11   */
     12  function run_one_test(refMARFileName, files, checkNoMAR) {
     13    if (checkNoMAR === undefined) {
     14      checkNoMAR = true;
     15    }
     16 
     17    // Ensure the MAR we will create doesn't already exist.
     18    let outMAR = tempDir.clone();
     19    outMAR.append("out.mar");
     20    if (checkNoMAR) {
     21      Assert.ok(!outMAR.exists());
     22    }
     23 
     24    // Create the actual MAR file.
     25    createMAR(outMAR, do_get_file("data"), files);
     26 
     27    // Get the reference MAR data.
     28    let refMAR = do_get_file("data/" + refMARFileName);
     29    let refMARData = getBinaryFileData(refMAR);
     30 
     31    // Verify the data of the MAR is what it should be.
     32    let outMARData = getBinaryFileData(outMAR);
     33    if (mozinfo.os != "win") {
     34      // Modify the array index that contains the file permission in this mar so
     35      // the comparison succeeds. This value is only changed when the value is
     36      // the expected value on non-Windows platforms since the MAR files are
     37      // created on Windows. This makes it possible to use the same MAR files for
     38      // all platforms.
     39      switch (refMARFileName) {
     40        case "0_sized.mar":
     41          if (outMARData[143] == 180) {
     42            outMARData[143] = 182;
     43          }
     44          break;
     45        case "1_byte.mar":
     46          if (outMARData[144] == 180) {
     47            outMARData[144] = 182;
     48          }
     49          break;
     50        case "binary_data.mar":
     51          if (outMARData[655] == 180) {
     52            outMARData[655] = 182;
     53          }
     54          break;
     55        case "multiple_file.mar":
     56          if (outMARData[656] == 180) {
     57            outMARData[656] = 182;
     58          }
     59          if (outMARData[681] == 180) {
     60            outMARData[681] = 182;
     61          }
     62          if (outMARData[705] == 180) {
     63            outMARData[705] = 182;
     64          }
     65      }
     66    }
     67    compareBinaryData(outMARData, refMARData);
     68  }
     69 
     70  // Define the unit tests to run.
     71  let tests = {
     72    // Test creating a MAR file with a 0 byte file.
     73    test_zero_sized: function _test_zero_sized() {
     74      return run_one_test("0_sized.mar", ["0_sized_file"]);
     75    },
     76    // Test creating a MAR file with a 1 byte file.
     77    test_one_byte: function _test_one_byte() {
     78      return run_one_test("1_byte.mar", ["1_byte_file"]);
     79    },
     80    // Test creating a MAR file with binary data.
     81    test_binary_data: function _test_binary_data() {
     82      return run_one_test("binary_data.mar", ["binary_data_file"]);
     83    },
     84    // Test creating a MAR file with multiple files inside of it.
     85    test_multiple_file: function _test_multiple_file() {
     86      return run_one_test("multiple_file.mar", [
     87        "0_sized_file",
     88        "1_byte_file",
     89        "binary_data_file",
     90      ]);
     91    },
     92    // Test creating a MAR file on top of a different one that already exists
     93    // at the location the new one will be created at.
     94    test_overwrite_already_exists: function _test_overwrite_already_exists() {
     95      let differentFile = do_get_file("data/1_byte.mar");
     96      let outMARDir = tempDir.clone();
     97      differentFile.copyTo(outMARDir, "out.mar");
     98      return run_one_test("binary_data.mar", ["binary_data_file"], false);
     99    },
    100    // Between each test make sure the out MAR does not exist.
    101    cleanup_per_test: function _cleanup_per_test() {
    102      let outMAR = tempDir.clone();
    103      outMAR.append("out.mar");
    104      if (outMAR.exists()) {
    105        outMAR.remove(false);
    106      }
    107    },
    108  };
    109 
    110  // Run all the tests
    111  Assert.equal(run_tests(tests), Object.keys(tests).length - 1);
    112 }