| |
1 /* Any copyright is dedicated to the Public Domain. |
| |
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
| |
3 |
| |
4 "use strict"; |
| |
5 |
| |
6 Components.utils.import("resource://gre/modules/osfile.jsm"); |
| |
7 |
| |
8 function run_test() { |
| |
9 do_test_pending(); |
| |
10 run_next_test(); |
| |
11 } |
| |
12 |
| |
13 add_task(function test_compress_lz4() { |
| |
14 let path = OS.Path.join(OS.Constants.Path.tmpDir, "compression.lz"); |
| |
15 let array = new Uint8Array(1024); |
| |
16 for (let i = 0; i < array.byteLength; ++i) { |
| |
17 array[i] = i; |
| |
18 } |
| |
19 |
| |
20 do_print("Writing data with lz4 compression"); |
| |
21 let bytes = yield OS.File.writeAtomic(path, array, { compression: "lz4" }); |
| |
22 do_print("Compressed " + array.byteLength + " bytes into " + bytes); |
| |
23 |
| |
24 do_print("Reading back with lz4 decompression"); |
| |
25 let decompressed = yield OS.File.read(path, { compression: "lz4" }); |
| |
26 do_print("Decompressed into " + decompressed.byteLength + " bytes"); |
| |
27 do_check_eq(Array.prototype.join.call(array), Array.prototype.join.call(decompressed)); |
| |
28 }); |
| |
29 |
| |
30 add_task(function test_uncompressed() { |
| |
31 do_print("Writing data without compression"); |
| |
32 let path = OS.Path.join(OS.Constants.Path.tmpDir, "no_compression.tmp"); |
| |
33 let array = new Uint8Array(1024); |
| |
34 for (let i = 0; i < array.byteLength; ++i) { |
| |
35 array[i] = i; |
| |
36 } |
| |
37 let bytes = yield OS.File.writeAtomic(path, array); // No compression |
| |
38 |
| |
39 let exn; |
| |
40 // Force decompression, reading should fail |
| |
41 try { |
| |
42 yield OS.File.read(path, { compression: "lz4" }); |
| |
43 } catch (ex) { |
| |
44 exn = ex; |
| |
45 } |
| |
46 do_check_true(!!exn); |
| |
47 do_check_true(exn.message.indexOf("Invalid header") != -1); |
| |
48 }); |
| |
49 |
| |
50 add_task(function() { |
| |
51 do_test_finished(); |
| |
52 }); |