modules/libmar/tests/unit/test_extract.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libmar/tests/unit/test_extract.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function run_test() {
     1.8 +
     1.9 +  /**
    1.10 +   * Extracts a MAR and makes sure each file matches the reference files.
    1.11 +   *
    1.12 +   * @param marFileName The name of the MAR file to extract
    1.13 +   * @param files       The files that the extracted MAR should contain
    1.14 +  */
    1.15 +  function run_one_test(marFileName, files) { 
    1.16 +    // Get the MAR file that we will be extracting
    1.17 +    let mar = do_get_file("data/" + marFileName);
    1.18 +
    1.19 +    // Get the path that we will extract to
    1.20 +    let outDir = tempDir.clone();
    1.21 +    outDir.append("out");
    1.22 +    do_check_false(outDir.exists());
    1.23 +    outDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0777);
    1.24 +
    1.25 +    // Get the ref files and the files that will be extracted.
    1.26 +    let outFiles = [];
    1.27 +    let refFiles = [];
    1.28 +    for (let i = 0; i < files.length; i++) {
    1.29 +      let outFile = outDir.clone();
    1.30 +      outFile.append(files[i]);
    1.31 +      do_check_false(outFile.exists());
    1.32 +
    1.33 +      outFiles.push(outFile);
    1.34 +      refFiles.push(do_get_file("data/" + files[i]));
    1.35 +    }
    1.36 +
    1.37 +    // Extract the MAR contents into the ./out dir.
    1.38 +    extractMAR(mar, outDir);
    1.39 +
    1.40 +    // Compare to make sure the extracted files are the same.
    1.41 +    for (let i = 0; i < files.length; i++) {
    1.42 +      do_check_true(outFiles[i].exists());
    1.43 +      let refFileData = getBinaryFileData(refFiles[i]);
    1.44 +      let outFileData = getBinaryFileData(outFiles[i]);
    1.45 +      compareBinaryData(refFileData, outFileData);
    1.46 +    }
    1.47 +  }
    1.48 +
    1.49 +  // Define the unit tests to run.
    1.50 +  let tests = {
    1.51 +    // Test extracting a MAR file with a 0 byte file.
    1.52 +    test_zero_sized: function() {
    1.53 +      return run_one_test("0_sized_mar.mar", ["0_sized_file"]);
    1.54 +    }, 
    1.55 +    // Test extracting a MAR file with a 1 byte file.
    1.56 +    test_one_byte: function() {
    1.57 +      return run_one_test("1_byte_mar.mar", ["1_byte_file"]);
    1.58 +    },
    1.59 +    // Test extracting a MAR file with binary data.
    1.60 +    test_binary_data: function() {
    1.61 +      return run_one_test("binary_data_mar.mar", ["binary_data_file"]);
    1.62 +    },
    1.63 +    // Test extracting a MAR without a product information block (PIB) which
    1.64 +    // contains binary data.
    1.65 +    test_no_pib: function() {
    1.66 +      return run_one_test("no_pib_mar.mar", ["binary_data_file"]);
    1.67 +    },
    1.68 +    // Test extracting a MAR without a product information block (PIB) that is
    1.69 +    // signed and which contains binary data.
    1.70 +    test_no_pib_signed: function() {
    1.71 +      return run_one_test("signed_no_pib_mar.mar", ["binary_data_file"]);
    1.72 +    },
    1.73 +    // Test extracting a MAR with a product information block (PIB) that is
    1.74 +    // signed and which contains binary data.
    1.75 +    test_pib_signed: function() {
    1.76 +      return run_one_test("signed_pib_mar.mar", ["binary_data_file"]);
    1.77 +    },
    1.78 +    // Test extracting a MAR file with multiple files inside of it.
    1.79 +    test_multiple_file: function() {
    1.80 +      return run_one_test("multiple_file_mar.mar", 
    1.81 +                          ["0_sized_file", "1_byte_file", "binary_data_file"]);
    1.82 +    }, 
    1.83 +    // Between each test make sure the out directory and its subfiles do 
    1.84 +    // not exist.
    1.85 +    cleanup_per_test: function() {
    1.86 +      let outDir = tempDir.clone();
    1.87 +      outDir.append("out");
    1.88 +      if (outDir.exists()) {
    1.89 +        outDir.remove(true);
    1.90 +      }
    1.91 +    }
    1.92 +  };
    1.93 +
    1.94 +  // Run all the tests
    1.95 +  do_check_eq(run_tests(tests), Object.keys(tests).length - 1);
    1.96 +}

mercurial