1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/workerlz4/tests/xpcshell/data/worker_lz4.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +importScripts("resource://gre/modules/workers/require.js"); 1.5 +importScripts("resource://gre/modules/osfile.jsm"); 1.6 + 1.7 + 1.8 +function do_print(x) { 1.9 + //self.postMessage({kind: "do_print", args: [x]}); 1.10 + dump("TEST-INFO: " + x + "\n"); 1.11 +} 1.12 + 1.13 +function do_check_true(x) { 1.14 + self.postMessage({kind: "do_check_true", args: [!!x]}); 1.15 + if (x) { 1.16 + dump("TEST-PASS: " + x + "\n"); 1.17 + } else { 1.18 + throw new Error("do_check_true failed"); 1.19 + } 1.20 +} 1.21 + 1.22 +function do_check_eq(a, b) { 1.23 + let result = a == b; 1.24 + self.postMessage({kind: "do_check_true", args: [result]}); 1.25 + if (!result) { 1.26 + throw new Error("do_check_eq failed " + a + " != " + b); 1.27 + } 1.28 +} 1.29 + 1.30 +function do_test_complete() { 1.31 + self.postMessage({kind: "do_test_complete", args:[]}); 1.32 +} 1.33 + 1.34 +self.onmessage = function() { 1.35 + try { 1.36 + run_test(); 1.37 + } catch (ex) { 1.38 + let {message, moduleStack, moduleName, lineNumber} = ex; 1.39 + let error = new Error(message, moduleName, lineNumber); 1.40 + error.stack = moduleStack; 1.41 + dump("Uncaught error: " + error + "\n"); 1.42 + dump("Full stack: " + moduleStack + "\n"); 1.43 + throw error; 1.44 + } 1.45 +}; 1.46 + 1.47 +let Lz4; 1.48 +let Internals; 1.49 +function test_import() { 1.50 + Lz4 = require("resource://gre/modules/workers/lz4.js"); 1.51 + Internals = require("resource://gre/modules/workers/lz4_internal.js"); 1.52 +} 1.53 + 1.54 +function test_bound() { 1.55 + for (let k of ["compress", "decompress", "maxCompressedSize"]) { 1.56 + try { 1.57 + do_print("Checking the existence of " + k + "\n"); 1.58 + do_check_true(!!Internals[k]); 1.59 + do_print(k + " exists"); 1.60 + } catch (ex) { 1.61 + // Ignore errors 1.62 + do_print(k + " doesn't exist!"); 1.63 + } 1.64 + } 1.65 +} 1.66 + 1.67 +function test_reference_file() { 1.68 + do_print("Decompress reference file"); 1.69 + let path = OS.Path.join("data", "compression.lz"); 1.70 + let data = OS.File.read(path); 1.71 + let decompressed = Lz4.decompressFileContent(data); 1.72 + let text = (new TextDecoder()).decode(decompressed); 1.73 + do_check_eq(text, "Hello, lz4"); 1.74 +} 1.75 + 1.76 +function compare_arrays(a, b) { 1.77 + return Array.prototype.join.call(a) == Array.prototype.join.call(a); 1.78 +} 1.79 + 1.80 +function run_rawcompression(name, array) { 1.81 + do_print("Raw compression test " + name); 1.82 + let length = array.byteLength; 1.83 + let compressedArray = new Uint8Array(Internals.maxCompressedSize(length)); 1.84 + let compressedBytes = Internals.compress(array, length, compressedArray); 1.85 + compressedArray = new Uint8Array(compressedArray.buffer, 0, compressedBytes); 1.86 + do_print("Raw compressed: " + length + " into " + compressedBytes); 1.87 + 1.88 + let decompressedArray = new Uint8Array(length); 1.89 + let decompressedBytes = new ctypes.size_t(); 1.90 + let success = Internals.decompress(compressedArray, compressedBytes, 1.91 + decompressedArray, length, 1.92 + decompressedBytes.address()); 1.93 + do_print("Raw decompression success? " + success); 1.94 + do_print("Raw decompression size: " + decompressedBytes.value); 1.95 + do_check_true(compare_arrays(array, decompressedArray)); 1.96 +} 1.97 + 1.98 +function run_filecompression(name, array) { 1.99 + do_print("File compression test " + name); 1.100 + let compressed = Lz4.compressFileContent(array); 1.101 + do_print("Compressed " + array.byteLength + " bytes into " + compressed.byteLength); 1.102 + 1.103 + let decompressed = Lz4.decompressFileContent(compressed); 1.104 + do_print("Decompressed " + compressed.byteLength + " bytes into " + decompressed.byteLength); 1.105 + do_check_true(compare_arrays(array, decompressed)); 1.106 +} 1.107 + 1.108 +function run_faileddecompression(name, array) { 1.109 + do_print("invalid decompression test " + name); 1.110 + 1.111 + // Ensure that raw decompression doesn't segfault 1.112 + let length = 1 << 14; 1.113 + let decompressedArray = new Uint8Array(length); 1.114 + let decompressedBytes = new ctypes.size_t(); 1.115 + Internals.decompress(array, array.byteLength, 1.116 + decompressedArray, length, 1.117 + decompressedBytes.address()); 1.118 + 1.119 + // File decompression should fail with an acceptable exception 1.120 + let exn = null; 1.121 + try { 1.122 + Lz4.decompressFileContent(array); 1.123 + } catch (ex) { 1.124 + exn = ex; 1.125 + } 1.126 + do_check_true(exn); 1.127 + if (array.byteLength < 10) { 1.128 + do_check_true(exn.becauseLZNoHeader); 1.129 + } else { 1.130 + do_check_true(exn.becauseLZWrongMagicNumber); 1.131 + } 1.132 +} 1.133 + 1.134 +function run_test() { 1.135 + test_import(); 1.136 + test_bound(); 1.137 + test_reference_file(); 1.138 + for (let length of [0, 1, 1024]) { 1.139 + let array = new Uint8Array(length); 1.140 + for (let i = 0; i < length; ++i) { 1.141 + array[i] = i % 256; 1.142 + } 1.143 + let name = length + " bytes"; 1.144 + run_rawcompression(name, array); 1.145 + run_filecompression(name, array); 1.146 + run_faileddecompression(name, array); 1.147 + } 1.148 + do_test_complete(); 1.149 +}