modules/libmar/tests/unit/test_extract.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bb2c727b215f
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 function run_test() {
5
6 /**
7 * Extracts a MAR and makes sure each file matches the reference files.
8 *
9 * @param marFileName The name of the MAR file to extract
10 * @param files The files that the extracted MAR should contain
11 */
12 function run_one_test(marFileName, files) {
13 // Get the MAR file that we will be extracting
14 let mar = do_get_file("data/" + marFileName);
15
16 // Get the path that we will extract to
17 let outDir = tempDir.clone();
18 outDir.append("out");
19 do_check_false(outDir.exists());
20 outDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0777);
21
22 // Get the ref files and the files that will be extracted.
23 let outFiles = [];
24 let refFiles = [];
25 for (let i = 0; i < files.length; i++) {
26 let outFile = outDir.clone();
27 outFile.append(files[i]);
28 do_check_false(outFile.exists());
29
30 outFiles.push(outFile);
31 refFiles.push(do_get_file("data/" + files[i]));
32 }
33
34 // Extract the MAR contents into the ./out dir.
35 extractMAR(mar, outDir);
36
37 // Compare to make sure the extracted files are the same.
38 for (let i = 0; i < files.length; i++) {
39 do_check_true(outFiles[i].exists());
40 let refFileData = getBinaryFileData(refFiles[i]);
41 let outFileData = getBinaryFileData(outFiles[i]);
42 compareBinaryData(refFileData, outFileData);
43 }
44 }
45
46 // Define the unit tests to run.
47 let tests = {
48 // Test extracting a MAR file with a 0 byte file.
49 test_zero_sized: function() {
50 return run_one_test("0_sized_mar.mar", ["0_sized_file"]);
51 },
52 // Test extracting a MAR file with a 1 byte file.
53 test_one_byte: function() {
54 return run_one_test("1_byte_mar.mar", ["1_byte_file"]);
55 },
56 // Test extracting a MAR file with binary data.
57 test_binary_data: function() {
58 return run_one_test("binary_data_mar.mar", ["binary_data_file"]);
59 },
60 // Test extracting a MAR without a product information block (PIB) which
61 // contains binary data.
62 test_no_pib: function() {
63 return run_one_test("no_pib_mar.mar", ["binary_data_file"]);
64 },
65 // Test extracting a MAR without a product information block (PIB) that is
66 // signed and which contains binary data.
67 test_no_pib_signed: function() {
68 return run_one_test("signed_no_pib_mar.mar", ["binary_data_file"]);
69 },
70 // Test extracting a MAR with a product information block (PIB) that is
71 // signed and which contains binary data.
72 test_pib_signed: function() {
73 return run_one_test("signed_pib_mar.mar", ["binary_data_file"]);
74 },
75 // Test extracting a MAR file with multiple files inside of it.
76 test_multiple_file: function() {
77 return run_one_test("multiple_file_mar.mar",
78 ["0_sized_file", "1_byte_file", "binary_data_file"]);
79 },
80 // Between each test make sure the out directory and its subfiles do
81 // not exist.
82 cleanup_per_test: function() {
83 let outDir = tempDir.clone();
84 outDir.append("out");
85 if (outDir.exists()) {
86 outDir.remove(true);
87 }
88 }
89 };
90
91 // Run all the tests
92 do_check_eq(run_tests(tests), Object.keys(tests).length - 1);
93 }

mercurial