toolkit/components/workerlz4/tests/xpcshell/data/worker_lz4.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 importScripts("resource://gre/modules/workers/require.js");
michael@0 2 importScripts("resource://gre/modules/osfile.jsm");
michael@0 3
michael@0 4
michael@0 5 function do_print(x) {
michael@0 6 //self.postMessage({kind: "do_print", args: [x]});
michael@0 7 dump("TEST-INFO: " + x + "\n");
michael@0 8 }
michael@0 9
michael@0 10 function do_check_true(x) {
michael@0 11 self.postMessage({kind: "do_check_true", args: [!!x]});
michael@0 12 if (x) {
michael@0 13 dump("TEST-PASS: " + x + "\n");
michael@0 14 } else {
michael@0 15 throw new Error("do_check_true failed");
michael@0 16 }
michael@0 17 }
michael@0 18
michael@0 19 function do_check_eq(a, b) {
michael@0 20 let result = a == b;
michael@0 21 self.postMessage({kind: "do_check_true", args: [result]});
michael@0 22 if (!result) {
michael@0 23 throw new Error("do_check_eq failed " + a + " != " + b);
michael@0 24 }
michael@0 25 }
michael@0 26
michael@0 27 function do_test_complete() {
michael@0 28 self.postMessage({kind: "do_test_complete", args:[]});
michael@0 29 }
michael@0 30
michael@0 31 self.onmessage = function() {
michael@0 32 try {
michael@0 33 run_test();
michael@0 34 } catch (ex) {
michael@0 35 let {message, moduleStack, moduleName, lineNumber} = ex;
michael@0 36 let error = new Error(message, moduleName, lineNumber);
michael@0 37 error.stack = moduleStack;
michael@0 38 dump("Uncaught error: " + error + "\n");
michael@0 39 dump("Full stack: " + moduleStack + "\n");
michael@0 40 throw error;
michael@0 41 }
michael@0 42 };
michael@0 43
michael@0 44 let Lz4;
michael@0 45 let Internals;
michael@0 46 function test_import() {
michael@0 47 Lz4 = require("resource://gre/modules/workers/lz4.js");
michael@0 48 Internals = require("resource://gre/modules/workers/lz4_internal.js");
michael@0 49 }
michael@0 50
michael@0 51 function test_bound() {
michael@0 52 for (let k of ["compress", "decompress", "maxCompressedSize"]) {
michael@0 53 try {
michael@0 54 do_print("Checking the existence of " + k + "\n");
michael@0 55 do_check_true(!!Internals[k]);
michael@0 56 do_print(k + " exists");
michael@0 57 } catch (ex) {
michael@0 58 // Ignore errors
michael@0 59 do_print(k + " doesn't exist!");
michael@0 60 }
michael@0 61 }
michael@0 62 }
michael@0 63
michael@0 64 function test_reference_file() {
michael@0 65 do_print("Decompress reference file");
michael@0 66 let path = OS.Path.join("data", "compression.lz");
michael@0 67 let data = OS.File.read(path);
michael@0 68 let decompressed = Lz4.decompressFileContent(data);
michael@0 69 let text = (new TextDecoder()).decode(decompressed);
michael@0 70 do_check_eq(text, "Hello, lz4");
michael@0 71 }
michael@0 72
michael@0 73 function compare_arrays(a, b) {
michael@0 74 return Array.prototype.join.call(a) == Array.prototype.join.call(a);
michael@0 75 }
michael@0 76
michael@0 77 function run_rawcompression(name, array) {
michael@0 78 do_print("Raw compression test " + name);
michael@0 79 let length = array.byteLength;
michael@0 80 let compressedArray = new Uint8Array(Internals.maxCompressedSize(length));
michael@0 81 let compressedBytes = Internals.compress(array, length, compressedArray);
michael@0 82 compressedArray = new Uint8Array(compressedArray.buffer, 0, compressedBytes);
michael@0 83 do_print("Raw compressed: " + length + " into " + compressedBytes);
michael@0 84
michael@0 85 let decompressedArray = new Uint8Array(length);
michael@0 86 let decompressedBytes = new ctypes.size_t();
michael@0 87 let success = Internals.decompress(compressedArray, compressedBytes,
michael@0 88 decompressedArray, length,
michael@0 89 decompressedBytes.address());
michael@0 90 do_print("Raw decompression success? " + success);
michael@0 91 do_print("Raw decompression size: " + decompressedBytes.value);
michael@0 92 do_check_true(compare_arrays(array, decompressedArray));
michael@0 93 }
michael@0 94
michael@0 95 function run_filecompression(name, array) {
michael@0 96 do_print("File compression test " + name);
michael@0 97 let compressed = Lz4.compressFileContent(array);
michael@0 98 do_print("Compressed " + array.byteLength + " bytes into " + compressed.byteLength);
michael@0 99
michael@0 100 let decompressed = Lz4.decompressFileContent(compressed);
michael@0 101 do_print("Decompressed " + compressed.byteLength + " bytes into " + decompressed.byteLength);
michael@0 102 do_check_true(compare_arrays(array, decompressed));
michael@0 103 }
michael@0 104
michael@0 105 function run_faileddecompression(name, array) {
michael@0 106 do_print("invalid decompression test " + name);
michael@0 107
michael@0 108 // Ensure that raw decompression doesn't segfault
michael@0 109 let length = 1 << 14;
michael@0 110 let decompressedArray = new Uint8Array(length);
michael@0 111 let decompressedBytes = new ctypes.size_t();
michael@0 112 Internals.decompress(array, array.byteLength,
michael@0 113 decompressedArray, length,
michael@0 114 decompressedBytes.address());
michael@0 115
michael@0 116 // File decompression should fail with an acceptable exception
michael@0 117 let exn = null;
michael@0 118 try {
michael@0 119 Lz4.decompressFileContent(array);
michael@0 120 } catch (ex) {
michael@0 121 exn = ex;
michael@0 122 }
michael@0 123 do_check_true(exn);
michael@0 124 if (array.byteLength < 10) {
michael@0 125 do_check_true(exn.becauseLZNoHeader);
michael@0 126 } else {
michael@0 127 do_check_true(exn.becauseLZWrongMagicNumber);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131 function run_test() {
michael@0 132 test_import();
michael@0 133 test_bound();
michael@0 134 test_reference_file();
michael@0 135 for (let length of [0, 1, 1024]) {
michael@0 136 let array = new Uint8Array(length);
michael@0 137 for (let i = 0; i < length; ++i) {
michael@0 138 array[i] = i % 256;
michael@0 139 }
michael@0 140 let name = length + " bytes";
michael@0 141 run_rawcompression(name, array);
michael@0 142 run_filecompression(name, array);
michael@0 143 run_faileddecompression(name, array);
michael@0 144 }
michael@0 145 do_test_complete();
michael@0 146 }

mercurial