toolkit/components/osfile/tests/xpcshell/test_compression.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_compression.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,52 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/osfile.jsm");
    1.10 +
    1.11 +function run_test() {
    1.12 +  do_test_pending();
    1.13 +  run_next_test();
    1.14 +}
    1.15 +
    1.16 +add_task(function test_compress_lz4() {
    1.17 +  let path = OS.Path.join(OS.Constants.Path.tmpDir, "compression.lz");
    1.18 +  let array = new Uint8Array(1024);
    1.19 +  for (let i = 0; i < array.byteLength; ++i) {
    1.20 +    array[i] = i;
    1.21 +  }
    1.22 +
    1.23 +  do_print("Writing data with lz4 compression");
    1.24 +  let bytes = yield OS.File.writeAtomic(path, array, { compression: "lz4" });
    1.25 +  do_print("Compressed " + array.byteLength + " bytes into " + bytes);
    1.26 +
    1.27 +  do_print("Reading back with lz4 decompression");
    1.28 +  let decompressed = yield OS.File.read(path, { compression: "lz4" });
    1.29 +  do_print("Decompressed into " + decompressed.byteLength + " bytes");
    1.30 +  do_check_eq(Array.prototype.join.call(array), Array.prototype.join.call(decompressed));
    1.31 +});
    1.32 +
    1.33 +add_task(function test_uncompressed() {
    1.34 +  do_print("Writing data without compression");
    1.35 +  let path = OS.Path.join(OS.Constants.Path.tmpDir, "no_compression.tmp");
    1.36 +  let array = new Uint8Array(1024);
    1.37 +  for (let i = 0; i < array.byteLength; ++i) {
    1.38 +    array[i] = i;
    1.39 +  }
    1.40 +  let bytes = yield OS.File.writeAtomic(path, array); // No compression
    1.41 +
    1.42 +  let exn;
    1.43 +  // Force decompression, reading should fail
    1.44 +  try {
    1.45 +    yield OS.File.read(path, { compression: "lz4" });
    1.46 +  } catch (ex) {
    1.47 +    exn = ex;
    1.48 +  }
    1.49 +  do_check_true(!!exn);
    1.50 +  do_check_true(exn.message.indexOf("Invalid header") != -1);
    1.51 +});
    1.52 +
    1.53 +add_task(function() {
    1.54 +  do_test_finished();
    1.55 +});

mercurial