Bug 854169 - Tests for the Lz4 worker module. r=froydnj
☠☠ backed out by 2d4fd5a493b1 ☠ ☠
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Fri, 08 Nov 2013 09:16:04 -0500
changeset 173020 cbf7e38a49aa7ce0340636c7f0023d12182213a2
parent 173019 c0f9bfe37d744f82b74c5d35edd8b568077570ea
child 173021 bd7733e5d5cf2f5505639e18055889b57487a468
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs854169
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 854169 - Tests for the Lz4 worker module. r=froydnj
toolkit/components/workerlz4/tests/xpcshell/data/chrome.manifest
toolkit/components/workerlz4/tests/xpcshell/data/compression.lz
toolkit/components/workerlz4/tests/xpcshell/data/worker_lz4.js
toolkit/components/workerlz4/tests/xpcshell/test_lz4.js
toolkit/components/workerlz4/tests/xpcshell/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/workerlz4/tests/xpcshell/data/chrome.manifest
@@ -0,0 +1,1 @@
+content test_lz4 ./
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a354edc03673f2d3fc3b9f07f9f39d1bd59bf656
GIT binary patch
literal 23
ec$~}4ukxufF<{_gU|?9_k(!f}ucMGtWdZ<A%?5=4
new file mode 100644
--- /dev/null
+++ b/toolkit/components/workerlz4/tests/xpcshell/data/worker_lz4.js
@@ -0,0 +1,146 @@
+importScripts("resource://gre/modules/workers/require.js");
+importScripts("resource://gre/modules/osfile.jsm");
+
+
+function do_print(x) {
+  //self.postMessage({kind: "do_print", args: [x]});
+  dump("TEST-INFO: " + x + "\n");
+}
+
+function do_check_true(x) {
+  self.postMessage({kind: "do_check_true", args: [!!x]});
+  if (x) {
+    dump("TEST-PASS: " + x + "\n");
+  } else {
+    throw new Error("do_check_true failed");
+  }
+}
+
+function do_check_eq(a, b) {
+  let result = a == b;
+  self.postMessage({kind: "do_check_true", args: [result]});
+  if (!result) {
+    throw new Error("do_check_eq failed " + a + " != " + b);
+  }
+}
+
+function do_test_complete() {
+  self.postMessage({kind: "do_test_complete", args:[]});
+}
+
+self.onmessage = function() {
+  try {
+    run_test();
+  } catch (ex) {
+    let {message, moduleStack, moduleName, lineNumber} = ex;
+    let error = new Error(message, moduleName, lineNumber);
+    error.stack = moduleStack;
+    dump("Uncaught error: " + error + "\n");
+    dump("Full stack: " + moduleStack + "\n");
+    throw error;
+  }
+};
+
+let Lz4;
+let Internals;
+function test_import() {
+  Lz4 = require("resource://gre/modules/workers/lz4.js");
+  Internals = require("resource://gre/modules/workers/lz4_internal.js");
+}
+
+function test_bound() {
+  for (let k of ["compress", "decompress", "maxCompressedSize"]) {
+    try {
+      do_print("Checking the existence of " + k + "\n");
+      do_check_true(!!Internals[k]);
+      do_print(k + " exists");
+    } catch (ex) {
+      // Ignore errors
+      do_print(k + " doesn't exist!");
+    }
+  }
+}
+
+function test_reference_file() {
+  do_print("Decompress reference file");
+  let path = OS.Path.join("data", "compression.lz");
+  let data = OS.File.read(path);
+  let decompressed = Lz4.decompressFileContent(data);
+  let text = (new TextDecoder()).decode(decompressed);
+  do_check_eq(text, "Hello, lz4");
+}
+
+function compare_arrays(a, b) {
+  return Array.prototype.join.call(a) == Array.prototype.join.call(a);
+}
+
+function run_rawcompression(name, array) {
+  do_print("Raw compression test " + name);
+  let length = array.byteLength;
+  let compressedArray = new Uint8Array(Internals.maxCompressedSize(length));
+  let compressedBytes = Internals.compress(array, length, compressedArray);
+  compressedArray = new Uint8Array(compressedArray.buffer, 0, compressedBytes);
+  do_print("Raw compressed: " + length + " into " + compressedBytes);
+
+  let decompressedArray = new Uint8Array(length);
+  let decompressedBytes = new ctypes.size_t();
+  let success = Internals.decompress(compressedArray, compressedBytes,
+                                     decompressedArray, length,
+                                     decompressedBytes.address());
+  do_print("Raw decompression success? " + success);
+  do_print("Raw decompression size: " + decompressedBytes.value);
+  do_check_true(compare_arrays(array, decompressedArray));
+}
+
+function run_filecompression(name, array) {
+  do_print("File compression test " + name);
+  let compressed = Lz4.compressFileContent(array);
+  do_print("Compressed " + array.byteLength + " bytes into " + compressed.byteLength);
+
+  let decompressed = Lz4.decompressFileContent(compressed);
+  do_print("Decompressed " + compressed.byteLength + " bytes into " + decompressed.byteLength);
+  do_check_true(compare_arrays(array, decompressed));
+}
+
+function run_faileddecompression(name, array) {
+  do_print("invalid decompression test " + name);
+
+  // Ensure that raw decompression doesn't segfault
+  let length = 1 << 14;
+  let decompressedArray = new Uint8Array(length);
+  let decompressedBytes = new ctypes.size_t();
+  Internals.decompress(array, array.byteLength,
+    decompressedArray, length,
+    decompressedBytes.address());
+
+  // File decompression should fail with an acceptable exception
+  let exn = null;
+  try {
+    Lz4.decompressFileContent(array);
+  } catch (ex) {
+    exn = ex;
+  }
+  do_check_true(exn);
+  if (array.byteLength < 10) {
+    do_check_true(exn.becauseLZNoHeader);
+  } else {
+    do_check_true(exn.becauseLZWrongMagicNumber);
+  }
+}
+
+function run_test() {
+  test_import();
+  test_bound();
+  test_reference_file();
+  for (let length of [0, 1, 1024]) {
+    let array = new Uint8Array(length);
+    for (let i = 0; i < length; ++i) {
+      array[i] = i % 256;
+    }
+    let name = length + " bytes";
+    run_rawcompression(name, array);
+    run_filecompression(name, array);
+    run_faileddecompression(name, array);
+  }
+  do_test_complete();
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/workerlz4/tests/xpcshell/test_lz4.js
@@ -0,0 +1,43 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+Components.utils.import("resource://gre/modules/Promise.jsm");
+
+let WORKER_SOURCE_URI = "chrome://test_lz4/content/worker_lz4.js";
+do_load_manifest("data/chrome.manifest");
+
+function run_test() {
+  run_next_test();
+}
+
+
+add_task(function() {
+  let worker = new ChromeWorker(WORKER_SOURCE_URI);
+  let deferred = Promise.defer();
+  worker.onmessage = function(event) {
+    let data = event.data;
+    switch (data.kind) {
+      case "do_check_true":
+        try {
+          do_check_true(data.args[0]);
+        } catch (ex) {
+          // Ignore errors
+        }
+        return;
+      case "do_test_complete":
+        deferred.resolve();
+        worker.terminate();
+        break;
+      case "do_print":
+        do_print(data.args[0]);
+    }
+  };
+  worker.onerror = function(event) {
+    let error = new Error(event.message, event.filename, event.lineno);
+    worker.terminate();
+    deferred.reject(error);
+  };
+  worker.postMessage("START");
+  return deferred.promise;
+});
+
new file mode 100644
--- /dev/null
+++ b/toolkit/components/workerlz4/tests/xpcshell/xpcshell.ini
@@ -0,0 +1,9 @@
+[DEFAULT]
+head =
+tail =
+support-files =
+  data/worker_lz4.js
+  data/chrome.manifest
+  data/compression.lz
+
+[test_lz4.js]