1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/modules/libmar/tests/unit/test_create.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function run_test() { 1.8 + 1.9 + /** 1.10 + * Creates MAR from the passed files, compares it to the reference MAR. 1.11 + * 1.12 + * @param refMARFileName The name of the MAR file that should match 1.13 + * @param files The files that should go in the created MAR 1.14 + * @param checkNoMAR If true return an error if a file already exists 1.15 + */ 1.16 + function run_one_test(refMARFileName, files, checkNoMAR) { 1.17 + if (checkNoMAR === undefined) { 1.18 + checkNoMAR = true; 1.19 + } 1.20 + 1.21 + // Ensure the MAR we will create doesn't already exist. 1.22 + let outMAR = tempDir.clone(); 1.23 + outMAR.append("out.mar"); 1.24 + if (checkNoMAR) { 1.25 + do_check_false(outMAR.exists()); 1.26 + } 1.27 + 1.28 + // Create the actual MAR file. 1.29 + createMAR(outMAR, do_get_file("data"), files); 1.30 + 1.31 + // Get the reference MAR data. 1.32 + let refMAR = do_get_file("data/" + refMARFileName); 1.33 + let refMARData = getBinaryFileData(refMAR); 1.34 + 1.35 + // Verify the data of the MAR is what it should be. 1.36 + let outMARData = getBinaryFileData(outMAR); 1.37 + compareBinaryData(outMARData, refMARData); 1.38 + } 1.39 + 1.40 + // Define the unit tests to run. 1.41 + let tests = { 1.42 + // Test creating a MAR file with a 0 byte file. 1.43 + test_zero_sized: function() { 1.44 + return run_one_test(refMARPrefix + "0_sized_mar.mar", ["0_sized_file"]); 1.45 + }, 1.46 + // Test creating a MAR file with a 1 byte file. 1.47 + test_one_byte: function() { 1.48 + return run_one_test(refMARPrefix + "1_byte_mar.mar", ["1_byte_file"]); 1.49 + }, 1.50 + // Test creating a MAR file with binary data. 1.51 + test_binary_data: function() { 1.52 + return run_one_test(refMARPrefix + "binary_data_mar.mar", 1.53 + ["binary_data_file"]); 1.54 + }, 1.55 + // Test creating a MAR file with multiple files inside of it. 1.56 + test_multiple_file: function() { 1.57 + return run_one_test(refMARPrefix + "multiple_file_mar.mar", 1.58 + ["0_sized_file", "1_byte_file", "binary_data_file"]); 1.59 + }, 1.60 + // Test creating a MAR file on top of a different one that already exists 1.61 + // at the location the new one will be created at. 1.62 + test_overwrite_already_exists: function() { 1.63 + let differentFile = do_get_file("data/1_byte_mar.mar"); 1.64 + let outMARDir = tempDir.clone(); 1.65 + differentFile.copyTo(outMARDir, "out.mar"); 1.66 + return run_one_test(refMARPrefix + "binary_data_mar.mar", 1.67 + ["binary_data_file"], false); 1.68 + }, 1.69 + // Between each test make sure the out MAR does not exist. 1.70 + cleanup_per_test: function() { 1.71 + let outMAR = tempDir.clone(); 1.72 + outMAR.append("out.mar"); 1.73 + if (outMAR.exists()) { 1.74 + outMAR.remove(false); 1.75 + } 1.76 + } 1.77 + }; 1.78 + 1.79 + // Run all the tests 1.80 + do_check_eq(run_tests(tests), Object.keys(tests).length - 1); 1.81 +}