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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial