test_extract.js (5835B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 function run_test() { 5 /** 6 * Extracts a MAR and makes sure each file matches the reference files. 7 * 8 * @param marFileName The name of the MAR file to extract 9 * @param files The files that the extracted MAR should contain 10 */ 11 function extract_and_compare(marFileName, files) { 12 // Get the MAR file that we will be extracting 13 let mar = do_get_file("data/" + marFileName); 14 15 // Get the path that we will extract to 16 let outDir = tempDir.clone(); 17 outDir.append("out"); 18 Assert.ok(!outDir.exists()); 19 outDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o777); 20 21 // Get the ref files and the files that will be extracted. 22 let outFiles = []; 23 let refFiles = []; 24 for (let i = 0; i < files.length; i++) { 25 let outFile = outDir.clone(); 26 outFile.append(files[i]); 27 Assert.ok(!outFile.exists()); 28 29 outFiles.push(outFile); 30 refFiles.push(do_get_file("data/" + files[i])); 31 } 32 33 // Extract the MAR contents to ./out dir and verify 0 for success. 34 Assert.equal(extractMAR(mar, outDir), 0); 35 36 // Compare to make sure the extracted files are the same. 37 for (let i = 0; i < files.length; i++) { 38 Assert.ok(outFiles[i].exists()); 39 let refFileData = getBinaryFileData(refFiles[i]); 40 let outFileData = getBinaryFileData(outFiles[i]); 41 compareBinaryData(refFileData, outFileData); 42 } 43 } 44 45 /** 46 * Attempts to extract a MAR and expects a failure 47 * 48 * @param marFileName The name of the MAR file to extract 49 */ 50 function extract_and_fail(marFileName) { 51 // Get the MAR file that we will be extracting 52 let mar = do_get_file("data/" + marFileName); 53 54 // Get the path that we will extract to 55 let outDir = tempDir.clone(); 56 outDir.append("out"); 57 Assert.ok(!outDir.exists()); 58 outDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o777); 59 60 // Extract the MAR contents to ./out dir and verify -1 (255 from the 61 // nsIprocess) for failure 62 Assert.equal(extractMAR(mar, outDir), 1); 63 } 64 65 // Define the unit tests to run. 66 let tests = { 67 // Test extracting a MAR file with a 0 byte file. 68 test_zero_sized: function _test_zero_sized() { 69 return extract_and_compare("0_sized.mar", ["0_sized_file"]); 70 }, 71 // Test extracting a MAR file with a 1 byte file. 72 test_one_byte: function _test_one_byte() { 73 return extract_and_compare("1_byte.mar", ["1_byte_file"]); 74 }, 75 // Test extracting a MAR file with binary data. 76 test_binary_data: function _test_binary_data() { 77 return extract_and_compare("binary_data.mar", ["binary_data_file"]); 78 }, 79 // Test extracting a MAR without a product information block (PIB) which 80 // contains binary data. 81 test_no_pib: function _test_no_pib() { 82 return extract_and_compare("no_pib.mar", ["binary_data_file"]); 83 }, 84 // Test extracting a MAR without a product information block (PIB) that is 85 // signed and which contains binary data. 86 test_no_pib_signed: function _test_no_pib_signed() { 87 return extract_and_compare("signed_no_pib.mar", ["binary_data_file"]); 88 }, 89 // Test extracting a MAR with a product information block (PIB) that is 90 // signed and which contains binary data. 91 test_pib_signed: function _test_pib_signed() { 92 return extract_and_compare("signed_pib.mar", ["binary_data_file"]); 93 }, 94 // Test extracting a MAR file with multiple files inside of it. 95 test_multiple_file: function _test_multiple_file() { 96 return extract_and_compare("multiple_file.mar", [ 97 "0_sized_file", 98 "1_byte_file", 99 "binary_data_file", 100 ]); 101 }, 102 // Test collision detection where file A + B are the same offset 103 test_collision_same_offset: function test_collision_same_offset() { 104 return extract_and_fail("manipulated_same_offset.mar"); 105 }, 106 // Test collision detection where file A's indexes are a subset of file B's 107 test_collision_is_contained: function test_collision_is_contained() { 108 return extract_and_fail("manipulated_is_container.mar"); 109 }, 110 // Test collision detection where file B's indexes are a subset of file A's 111 test_collision_contained_by: function test_collision_contained_by() { 112 return extract_and_fail("manipulated_is_contained.mar"); 113 }, 114 // Test collision detection where file A ends in file B's indexes 115 test_collision_a_onto_b: function test_collision_a_onto_b() { 116 return extract_and_fail("manipulated_frontend_collision.mar"); 117 }, 118 // Test collision detection where file B ends in file A's indexes 119 test_collsion_b_onto_a: function test_collsion_b_onto_a() { 120 return extract_and_fail("manipulated_backend_collision.mar"); 121 }, 122 // Test collision detection where file C shares indexes with both file A & B 123 test_collision_multiple: function test_collision_multiple() { 124 return extract_and_fail("manipulated_multiple_collision.mar"); 125 }, 126 // Test collision detection where A is the last file in the list 127 test_collision_last: function test_collision_multiple_last() { 128 return extract_and_fail("manipulated_multiple_collision_last.mar"); 129 }, 130 // Test collision detection where A is the first file in the list 131 test_collision_first: function test_collision_multiple_first() { 132 return extract_and_fail("manipulated_multiple_collision_first.mar"); 133 }, 134 // Between each test make sure the out directory and its subfiles do 135 // not exist. 136 cleanup_per_test: function _cleanup_per_test() { 137 let outDir = tempDir.clone(); 138 outDir.append("out"); 139 if (outDir.exists()) { 140 outDir.remove(true); 141 } 142 }, 143 }; 144 145 // Run all the tests 146 Assert.equal(run_tests(tests), Object.keys(tests).length - 1); 147 }