modules/libmar/tests/unit/head_libmar.js.in

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libmar/tests/unit/head_libmar.js.in	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +const BIN_SUFFIX = "@BIN_SUFFIX@";
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +
    1.11 +#ifdef XP_WIN
    1.12 +  let refMARPrefix = "win_";
    1.13 +#else
    1.14 +  let refMARPrefix = "";
    1.15 +#endif
    1.16 +
    1.17 +let tempDir = do_get_tempdir();
    1.18 +
    1.19 +/**
    1.20 + * Compares binary data of 2 arrays and throws if they aren't the same.
    1.21 + * Throws on mismatch, does nothing on match.
    1.22 + *
    1.23 + * @param arr1 The first array to compare
    1.24 + * @param arr2 The second array to compare
    1.25 +*/
    1.26 +function compareBinaryData(arr1, arr2) {
    1.27 +  do_check_eq(arr1.length, arr2.length);
    1.28 +  for (let i = 0; i < arr1.length; i++) {
    1.29 +    if (arr1[i] != arr2[i]) {
    1.30 +      throw "Data differs at index " + i + 
    1.31 +            ", arr1: " + arr1[i] + ", arr2: " + arr2[i];
    1.32 +    }
    1.33 +  }
    1.34 +}
    1.35 +
    1.36 +/** 
    1.37 + * Reads a file's data and returns it
    1.38 + *
    1.39 + * @param file The file to read the data from
    1.40 + * @return a byte array for the data in the file.
    1.41 +*/
    1.42 +function getBinaryFileData(file) {
    1.43 +  let fileStream = Cc["@mozilla.org/network/file-input-stream;1"].
    1.44 +                   createInstance(Ci.nsIFileInputStream);
    1.45 +  // Open as RD_ONLY with default permissions.
    1.46 +  fileStream.init(file, -1, -1, null);
    1.47 +
    1.48 +  // Check the returned size versus the expected size.
    1.49 +  let stream = Cc["@mozilla.org/binaryinputstream;1"].
    1.50 +               createInstance(Ci.nsIBinaryInputStream);
    1.51 +  stream.setInputStream(fileStream);
    1.52 +  let bytes = stream.readByteArray(stream.available());
    1.53 +  fileStream.close();
    1.54 +  return bytes;
    1.55 +}
    1.56 +
    1.57 +/**
    1.58 + * Runs each method in the passed in object
    1.59 + * Every method of the passed in object that starts with test_ will be ran
    1.60 + * The cleanup_per_test method of the object will be run right away, it will be
    1.61 + * registered to be the cleanup function, and it will be run between each test.
    1.62 + *
    1.63 + * @return The number of tests ran
    1.64 +*/
    1.65 +function run_tests(obj) {
    1.66 +  let cleanup_per_test = obj.cleanup_per_test;
    1.67 +  if (cleanup_per_test === undefined) {
    1.68 +    cleanup_per_test = function() {};
    1.69 +  }
    1.70 +
    1.71 +  do_register_cleanup(cleanup_per_test);
    1.72 +
    1.73 +  // Make sure there's nothing left over from a preious failed test
    1.74 +  cleanup_per_test();
    1.75 +
    1.76 +  let ranCount = 0;
    1.77 +  // hasOwnProperty ensures we only see direct properties and not all
    1.78 +  for (let f in obj) {
    1.79 +    if (typeof obj[f] === "function" && 
    1.80 +        obj.hasOwnProperty(f) &&
    1.81 +        f.toString().indexOf("test_") === 0) {
    1.82 +      obj[f]();
    1.83 +      cleanup_per_test();
    1.84 +      ranCount++;
    1.85 +    }
    1.86 +  }
    1.87 +  return ranCount;
    1.88 +}
    1.89 +
    1.90 +/**
    1.91 + * Creates a MAR file with the content of files.
    1.92 + *
    1.93 + * @param outMAR  The file where the MAR should be created to
    1.94 + * @param dataDir The directory where the relative file paths exist
    1.95 + * @param files   The relative file paths of the files to include in the MAR
    1.96 +*/
    1.97 +function createMAR(outMAR, dataDir, files) {
    1.98 +  // You cannot create an empy MAR.
    1.99 +  do_check_true(files.length > 0);
   1.100 +
   1.101 +  // Get an nsIProcess to the signmar binary.
   1.102 +  let process = Cc["@mozilla.org/process/util;1"].
   1.103 +                createInstance(Ci.nsIProcess);
   1.104 +  let signmarBin = do_get_file("signmar" + BIN_SUFFIX);
   1.105 +
   1.106 +  // Make sure the signmar binary exists and is an executable.
   1.107 +  do_check_true(signmarBin.exists());
   1.108 +  do_check_true(signmarBin.isExecutable());
   1.109 +
   1.110 +  // Ensure on non Windows platforms we encode the same permissions
   1.111 +  // as the refernence MARs contain.  On Windows this is also safe.
   1.112 +  // The reference MAR files have permissions of 0664, so in case
   1.113 +  // someone is running these tests locally with another permission
   1.114 +  // (perhaps 0777), make sure that we encode them as 0664.
   1.115 +  for (filePath of files) {
   1.116 +    let f = dataDir.clone();
   1.117 +    f.append(filePath);
   1.118 +    f.permissions = 0664;
   1.119 +  }
   1.120 +
   1.121 +  // Setup the command line arguments to create the MAR.
   1.122 +  let args = ["-C", dataDir.path, "-H", "\@MAR_CHANNEL_ID\@", 
   1.123 +              "-V", "13.0a1", "-c", outMAR.path];
   1.124 +  args = args.concat(files);
   1.125 +
   1.126 +  do_print('Running: ' + signmarBin.path);
   1.127 +  process.init(signmarBin);
   1.128 +  process.run(true, args, args.length);
   1.129 +
   1.130 +  // Verify signmar returned 0 for success.
   1.131 +  do_check_eq(process.exitValue, 0);
   1.132 +
   1.133 +  // Verify the out MAR file actually exists.
   1.134 +  do_check_true(outMAR.exists());
   1.135 +}
   1.136 +
   1.137 +/**
   1.138 + * Extracts a MAR file to the specified output directory.
   1.139 + *
   1.140 + * @param mar     The MAR file that should be matched
   1.141 + * @param dataDir The directory to extract to
   1.142 +*/
   1.143 +function extractMAR(mar, dataDir) {
   1.144 +  // Get an nsIProcess to the signmar binary.
   1.145 +  let process = Cc["@mozilla.org/process/util;1"].
   1.146 +                createInstance(Ci.nsIProcess);
   1.147 +  let signmarBin = do_get_file("signmar" + BIN_SUFFIX);
   1.148 +
   1.149 +  // Make sure the signmar binary exists and is an executable.
   1.150 +  do_check_true(signmarBin.exists());
   1.151 +  do_check_true(signmarBin.isExecutable());
   1.152 +
   1.153 +  // Setup the command line arguments to create the MAR.
   1.154 +  let args = ["-C", dataDir.path, "-x", mar.path];
   1.155 +
   1.156 +  do_print('Running: ' + signmarBin.path);
   1.157 +  process.init(signmarBin);
   1.158 +  process.run(true, args, args.length);
   1.159 +
   1.160 +  // Verify signmar returned 0 for success.
   1.161 +  do_check_eq(process.exitValue, 0);
   1.162 +}
   1.163 +
   1.164 +

mercurial