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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 const BIN_SUFFIX = "@BIN_SUFFIX@";
     5 const Cc = Components.classes;
     6 const Ci = Components.interfaces;
     8 #ifdef XP_WIN
     9   let refMARPrefix = "win_";
    10 #else
    11   let refMARPrefix = "";
    12 #endif
    14 let tempDir = do_get_tempdir();
    16 /**
    17  * Compares binary data of 2 arrays and throws if they aren't the same.
    18  * Throws on mismatch, does nothing on match.
    19  *
    20  * @param arr1 The first array to compare
    21  * @param arr2 The second array to compare
    22 */
    23 function compareBinaryData(arr1, arr2) {
    24   do_check_eq(arr1.length, arr2.length);
    25   for (let i = 0; i < arr1.length; i++) {
    26     if (arr1[i] != arr2[i]) {
    27       throw "Data differs at index " + i + 
    28             ", arr1: " + arr1[i] + ", arr2: " + arr2[i];
    29     }
    30   }
    31 }
    33 /** 
    34  * Reads a file's data and returns it
    35  *
    36  * @param file The file to read the data from
    37  * @return a byte array for the data in the file.
    38 */
    39 function getBinaryFileData(file) {
    40   let fileStream = Cc["@mozilla.org/network/file-input-stream;1"].
    41                    createInstance(Ci.nsIFileInputStream);
    42   // Open as RD_ONLY with default permissions.
    43   fileStream.init(file, -1, -1, null);
    45   // Check the returned size versus the expected size.
    46   let stream = Cc["@mozilla.org/binaryinputstream;1"].
    47                createInstance(Ci.nsIBinaryInputStream);
    48   stream.setInputStream(fileStream);
    49   let bytes = stream.readByteArray(stream.available());
    50   fileStream.close();
    51   return bytes;
    52 }
    54 /**
    55  * Runs each method in the passed in object
    56  * Every method of the passed in object that starts with test_ will be ran
    57  * The cleanup_per_test method of the object will be run right away, it will be
    58  * registered to be the cleanup function, and it will be run between each test.
    59  *
    60  * @return The number of tests ran
    61 */
    62 function run_tests(obj) {
    63   let cleanup_per_test = obj.cleanup_per_test;
    64   if (cleanup_per_test === undefined) {
    65     cleanup_per_test = function() {};
    66   }
    68   do_register_cleanup(cleanup_per_test);
    70   // Make sure there's nothing left over from a preious failed test
    71   cleanup_per_test();
    73   let ranCount = 0;
    74   // hasOwnProperty ensures we only see direct properties and not all
    75   for (let f in obj) {
    76     if (typeof obj[f] === "function" && 
    77         obj.hasOwnProperty(f) &&
    78         f.toString().indexOf("test_") === 0) {
    79       obj[f]();
    80       cleanup_per_test();
    81       ranCount++;
    82     }
    83   }
    84   return ranCount;
    85 }
    87 /**
    88  * Creates a MAR file with the content of files.
    89  *
    90  * @param outMAR  The file where the MAR should be created to
    91  * @param dataDir The directory where the relative file paths exist
    92  * @param files   The relative file paths of the files to include in the MAR
    93 */
    94 function createMAR(outMAR, dataDir, files) {
    95   // You cannot create an empy MAR.
    96   do_check_true(files.length > 0);
    98   // Get an nsIProcess to the signmar binary.
    99   let process = Cc["@mozilla.org/process/util;1"].
   100                 createInstance(Ci.nsIProcess);
   101   let signmarBin = do_get_file("signmar" + BIN_SUFFIX);
   103   // Make sure the signmar binary exists and is an executable.
   104   do_check_true(signmarBin.exists());
   105   do_check_true(signmarBin.isExecutable());
   107   // Ensure on non Windows platforms we encode the same permissions
   108   // as the refernence MARs contain.  On Windows this is also safe.
   109   // The reference MAR files have permissions of 0664, so in case
   110   // someone is running these tests locally with another permission
   111   // (perhaps 0777), make sure that we encode them as 0664.
   112   for (filePath of files) {
   113     let f = dataDir.clone();
   114     f.append(filePath);
   115     f.permissions = 0664;
   116   }
   118   // Setup the command line arguments to create the MAR.
   119   let args = ["-C", dataDir.path, "-H", "\@MAR_CHANNEL_ID\@", 
   120               "-V", "13.0a1", "-c", outMAR.path];
   121   args = args.concat(files);
   123   do_print('Running: ' + signmarBin.path);
   124   process.init(signmarBin);
   125   process.run(true, args, args.length);
   127   // Verify signmar returned 0 for success.
   128   do_check_eq(process.exitValue, 0);
   130   // Verify the out MAR file actually exists.
   131   do_check_true(outMAR.exists());
   132 }
   134 /**
   135  * Extracts a MAR file to the specified output directory.
   136  *
   137  * @param mar     The MAR file that should be matched
   138  * @param dataDir The directory to extract to
   139 */
   140 function extractMAR(mar, dataDir) {
   141   // Get an nsIProcess to the signmar binary.
   142   let process = Cc["@mozilla.org/process/util;1"].
   143                 createInstance(Ci.nsIProcess);
   144   let signmarBin = do_get_file("signmar" + BIN_SUFFIX);
   146   // Make sure the signmar binary exists and is an executable.
   147   do_check_true(signmarBin.exists());
   148   do_check_true(signmarBin.isExecutable());
   150   // Setup the command line arguments to create the MAR.
   151   let args = ["-C", dataDir.path, "-x", mar.path];
   153   do_print('Running: ' + signmarBin.path);
   154   process.init(signmarBin);
   155   process.run(true, args, args.length);
   157   // Verify signmar returned 0 for success.
   158   do_check_eq(process.exitValue, 0);
   159 }

mercurial