modules/libjar/zipwriter/test/unit/test_alignment.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4 */
michael@0 5
michael@0 6 const DATA = "ZIP WRITER TEST DATA";
michael@0 7 const FILENAME = "test_data.txt";
michael@0 8 const CRC = 0xe6164331;
michael@0 9 const time = 1199145600000; // Jan 1st 2008
michael@0 10
michael@0 11 var TESTS = [
michael@0 12 {
michael@0 13 name: "test.txt",
michael@0 14 compression: Ci.nsIZipWriter.COMPRESSION_DEFAULT
michael@0 15 },
michael@0 16 {
michael@0 17 name: "test.png",
michael@0 18 compression: Ci.nsIZipWriter.COMPRESSION_NONE
michael@0 19 }
michael@0 20 ];
michael@0 21
michael@0 22 function swap16(n)
michael@0 23 {
michael@0 24 return (((n >> 8) & 0xFF) << 0) |
michael@0 25 (((n >> 0) & 0xFF) << 8);
michael@0 26 }
michael@0 27
michael@0 28 function swap32(n)
michael@0 29 {
michael@0 30 return (((n >> 24) & 0xFF) << 0) |
michael@0 31 (((n >> 16) & 0xFF) << 8) |
michael@0 32 (((n >> 8) & 0xFF) << 16) |
michael@0 33 (((n >> 0) & 0xFF) << 24);
michael@0 34 }
michael@0 35
michael@0 36 function move_to_data(bis, offset)
michael@0 37 {
michael@0 38 bis.readBytes(18); // Move to compressed size
michael@0 39 var size = swap32(bis.read32());
michael@0 40 bis.readBytes(4);
michael@0 41 var file_len = swap16(bis.read16());
michael@0 42 var extra_len = swap16(bis.read16());
michael@0 43 var file = bis.readBytes(file_len);
michael@0 44 bis.readBytes(extra_len);
michael@0 45 offset += ZIP_FILE_HEADER_SIZE + file_len + extra_len;
michael@0 46
michael@0 47 return {offset: offset, size: size};
michael@0 48 }
michael@0 49
michael@0 50 function test_alignment(align_size)
michael@0 51 {
michael@0 52 // Create zip for testing.
michael@0 53 zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
michael@0 54 for (var i = 0; i < TESTS.length; i++) {
michael@0 55 var source = do_get_file(DATA_DIR + TESTS[i].name);
michael@0 56 zipW.addEntryFile(TESTS[i].name, TESTS[i].compression, source, false);
michael@0 57 }
michael@0 58 var stream = Cc["@mozilla.org/io/string-input-stream;1"]
michael@0 59 .createInstance(Ci.nsIStringInputStream);
michael@0 60 stream.setData(DATA, DATA.length);
michael@0 61 zipW.addEntryStream(FILENAME, time * PR_USEC_PER_MSEC,
michael@0 62 Ci.nsIZipWriter.COMPRESSION_NONE, stream, false);
michael@0 63 zipW.alignStoredFiles(align_size);
michael@0 64 zipW.close();
michael@0 65
michael@0 66 // Check data can be decompressed.
michael@0 67 var zipR = new ZipReader(tmpFile);
michael@0 68 var stream = Cc["@mozilla.org/scriptableinputstream;1"]
michael@0 69 .createInstance(Ci.nsIScriptableInputStream);
michael@0 70 stream.init(zipR.getInputStream(FILENAME));
michael@0 71 var result = stream.read(DATA.length);
michael@0 72 do_check_eq(result, DATA);
michael@0 73 stream.close();
michael@0 74 zipR.close();
michael@0 75
michael@0 76 // Check data is correct and aligned.
michael@0 77 var fis = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 78 .createInstance(Ci.nsIFileInputStream);
michael@0 79 fis.init(tmpFile, -1, -1, null);
michael@0 80 let bis = Cc["@mozilla.org/binaryinputstream;1"]
michael@0 81 .createInstance(Ci.nsIBinaryInputStream);
michael@0 82 bis.setInputStream(fis);
michael@0 83 var offset = 0;
michael@0 84
michael@0 85 var ret = move_to_data(bis, offset); // "test.txt"
michael@0 86 offset = ret.offset;
michael@0 87 bis.readBytes(ret.size);
michael@0 88 offset += ret.size;
michael@0 89
michael@0 90 ret = move_to_data(bis, offset); // "test.png"
michael@0 91 offset = ret.offset;
michael@0 92 do_check_eq(offset % align_size, 0);
michael@0 93 bis.readBytes(ret.size);
michael@0 94 offset += ret.size;
michael@0 95
michael@0 96 ret = move_to_data(bis, offset); // "test_data.txt"
michael@0 97 offset = ret.offset;
michael@0 98 var result = bis.readBytes(DATA.length);
michael@0 99 do_check_eq(result, DATA);
michael@0 100 do_check_eq(offset % align_size, 0);
michael@0 101
michael@0 102 fis.close();
michael@0 103 bis.close();
michael@0 104 }
michael@0 105
michael@0 106 function run_test()
michael@0 107 {
michael@0 108 test_alignment(2);
michael@0 109 test_alignment(4);
michael@0 110 test_alignment(16);
michael@0 111 test_alignment(4096);
michael@0 112 test_alignment(32768);
michael@0 113 }

mercurial