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