tor-browser

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

test_asyncadd.js (2885B)


      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 { NetUtil } = ChromeUtils.importESModule(
      7  "resource://gre/modules/NetUtil.sys.mjs"
      8 );
      9 
     10 // Values taken from using zipinfo to list the test.zip contents
     11 var TESTS = [
     12  {
     13    name: "test.txt",
     14    size: 232,
     15    crc: 0x0373ac26,
     16  },
     17  {
     18    name: "test.png",
     19    size: 3402,
     20    crc: 0x504a5c30,
     21  },
     22 ];
     23 
     24 var size = 0;
     25 
     26 var observer = {
     27  onStartRequest() {},
     28 
     29  onStopRequest(request, status) {
     30    Assert.equal(status, Cr.NS_OK);
     31 
     32    zipW.close();
     33    size += ZIP_EOCDR_HEADER_SIZE;
     34 
     35    Assert.equal(size, tmpFile.fileSize);
     36 
     37    // Test the stored data with the zipreader
     38    var zipR = new ZipReader(tmpFile);
     39 
     40    for (var i = 0; i < TESTS.length; i++) {
     41      var source = do_get_file(DATA_DIR + TESTS[i].name);
     42      for (let method in methods) {
     43        var entryName = method + "/" + TESTS[i].name;
     44        Assert.ok(zipR.hasEntry(entryName));
     45 
     46        var entry = zipR.getEntry(entryName);
     47        Assert.equal(entry.realSize, TESTS[i].size);
     48        Assert.equal(entry.size, TESTS[i].size);
     49        Assert.equal(entry.CRC32, TESTS[i].crc);
     50        Assert.equal(
     51          Math.floor(entry.lastModifiedTime / PR_USEC_PER_SEC),
     52          Math.floor(source.lastModifiedTime / PR_MSEC_PER_SEC)
     53        );
     54 
     55        zipR.test(entryName);
     56      }
     57    }
     58 
     59    zipR.close();
     60    do_test_finished();
     61  },
     62 };
     63 
     64 var methods = {
     65  file: function method_file(entry, source) {
     66    zipW.addEntryFile(entry, Ci.nsIZipWriter.COMPRESSION_NONE, source, true);
     67  },
     68  channel: function method_channel(entry, source) {
     69    zipW.addEntryChannel(
     70      entry,
     71      source.lastModifiedTime * PR_MSEC_PER_SEC,
     72      Ci.nsIZipWriter.COMPRESSION_NONE,
     73      NetUtil.newChannel({
     74        uri: ioSvc.newFileURI(source),
     75        loadUsingSystemPrincipal: true,
     76      }),
     77      true
     78    );
     79  },
     80  stream: function method_stream(entry, source) {
     81    zipW.addEntryStream(
     82      entry,
     83      source.lastModifiedTime * PR_MSEC_PER_SEC,
     84      Ci.nsIZipWriter.COMPRESSION_NONE,
     85      NetUtil.newChannel({
     86        uri: ioSvc.newFileURI(source),
     87        loadUsingSystemPrincipal: true,
     88      }).open(),
     89      true
     90    );
     91  },
     92 };
     93 
     94 function run_test() {
     95  zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
     96 
     97  for (var i = 0; i < TESTS.length; i++) {
     98    var source = do_get_file(DATA_DIR + TESTS[i].name);
     99    for (let method in methods) {
    100      var entry = method + "/" + TESTS[i].name;
    101      methods[method](entry, source);
    102      size +=
    103        ZIP_FILE_HEADER_SIZE +
    104        ZIP_CDS_HEADER_SIZE +
    105        ZIP_EXTENDED_TIMESTAMP_SIZE * 2 +
    106        entry.length * 2 +
    107        TESTS[i].size;
    108    }
    109  }
    110  do_test_pending();
    111  zipW.processQueue(observer, null);
    112  Assert.ok(zipW.inQueue);
    113 }