tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_alignment.js (3061B)


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