summaryrefslogtreecommitdiffstats
path: root/modules/libmar/tests/unit/test_create.js
blob: 224364b41928be7f0b4374cac9098c411eebe14d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function run_test() {
  /**
   * Creates MAR from the passed files, compares it to the reference MAR.
   *
   * @param refMARFileName The name of the MAR file that should match
   * @param files          The files that should go in the created MAR
   * @param checkNoMAR     If true return an error if a file already exists
   */
  function run_one_test(refMARFileName, files, checkNoMAR) {
    if (checkNoMAR === undefined) {
      checkNoMAR = true;
    }

    // Ensure the MAR we will create doesn't already exist.
    let outMAR = tempDir.clone();
    outMAR.append("out.mar");
    if (checkNoMAR) {
      Assert.ok(!outMAR.exists());
    }

    // Create the actual MAR file.
    createMAR(outMAR, do_get_file("data"), files);

    // Get the reference MAR data.
    let refMAR = do_get_file("data/" + refMARFileName);
    let refMARData = getBinaryFileData(refMAR);

    // Verify the data of the MAR is what it should be.
    let outMARData = getBinaryFileData(outMAR);
    if (mozinfo.os != "win") {
      // Modify the array index that contains the file permission in this mar so
      // the comparison succeeds. This value is only changed when the value is
      // the expected value on non-Windows platforms since the MAR files are
      // created on Windows. This makes it possible to use the same MAR files for
      // all platforms.
      switch (refMARFileName) {
        case "0_sized.mar":
          if (outMARData[143] == 180) {
            outMARData[143] = 182;
          }
          break;
        case "1_byte.mar":
          if (outMARData[144] == 180) {
            outMARData[144] = 182;
          }
          break;
        case "binary_data.mar":
          if (outMARData[655] == 180) {
            outMARData[655] = 182;
          }
          break;
        case "multiple_file.mar":
          if (outMARData[656] == 180) {
            outMARData[656] = 182;
          }
          if (outMARData[681] == 180) {
            outMARData[681] = 182;
          }
          if (outMARData[705] == 180) {
            outMARData[705] = 182;
          }
      }
    }
    compareBinaryData(outMARData, refMARData);
  }

  // Define the unit tests to run.
  let tests = {
    // Test creating a MAR file with a 0 byte file.
    test_zero_sized: function _test_zero_sized() {
      return run_one_test("0_sized.mar", ["0_sized_file"]);
    },
    // Test creating a MAR file with a 1 byte file.
    test_one_byte: function _test_one_byte() {
      return run_one_test("1_byte.mar", ["1_byte_file"]);
    },
    // Test creating a MAR file with binary data.
    test_binary_data: function _test_binary_data() {
      return run_one_test("binary_data.mar", ["binary_data_file"]);
    },
    // Test creating a MAR file with multiple files inside of it.
    test_multiple_file: function _test_multiple_file() {
      return run_one_test("multiple_file.mar", [
        "0_sized_file",
        "1_byte_file",
        "binary_data_file",
      ]);
    },
    // Test creating a MAR file on top of a different one that already exists
    // at the location the new one will be created at.
    test_overwrite_already_exists: function _test_overwrite_already_exists() {
      let differentFile = do_get_file("data/1_byte.mar");
      let outMARDir = tempDir.clone();
      differentFile.copyTo(outMARDir, "out.mar");
      return run_one_test("binary_data.mar", ["binary_data_file"], false);
    },
    // Between each test make sure the out MAR does not exist.
    cleanup_per_test: function _cleanup_per_test() {
      let outMAR = tempDir.clone();
      outMAR.append("out.mar");
      if (outMAR.exists()) {
        outMAR.remove(false);
      }
    },
  };

  // Run all the tests
  Assert.equal(run_tests(tests), Object.keys(tests).length - 1);
}