|
1 // |
|
2 // Test that data can be appended to a cache entry even when the data is |
|
3 // compressed by the cache compression feature - bug 648429. |
|
4 // |
|
5 |
|
6 function write_and_check(str, data, len) |
|
7 { |
|
8 var written = str.write(data, len); |
|
9 if (written != len) { |
|
10 do_throw("str.write has not written all data!\n" + |
|
11 " Expected: " + len + "\n" + |
|
12 " Actual: " + written + "\n"); |
|
13 } |
|
14 } |
|
15 |
|
16 function TestAppend(compress, callback) |
|
17 { |
|
18 this._compress = compress; |
|
19 this._callback = callback; |
|
20 this.run(); |
|
21 } |
|
22 |
|
23 TestAppend.prototype = { |
|
24 _compress: false, |
|
25 _callback: null, |
|
26 |
|
27 run: function() { |
|
28 evict_cache_entries(); |
|
29 asyncOpenCacheEntry("http://data/", |
|
30 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null, |
|
31 this.writeData.bind(this)); |
|
32 }, |
|
33 |
|
34 writeData: function(status, entry) { |
|
35 do_check_eq(status, Cr.NS_OK); |
|
36 if (this._compress) |
|
37 entry.setMetaDataElement("uncompressed-len", "0"); |
|
38 var os = entry.openOutputStream(0); |
|
39 write_and_check(os, "12345", 5); |
|
40 os.close(); |
|
41 entry.close(); |
|
42 asyncOpenCacheEntry("http://data/", |
|
43 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null, |
|
44 this.appendData.bind(this)); |
|
45 }, |
|
46 |
|
47 appendData: function(status, entry) { |
|
48 do_check_eq(status, Cr.NS_OK); |
|
49 var os = entry.openOutputStream(entry.storageDataSize); |
|
50 write_and_check(os, "abcde", 5); |
|
51 os.close(); |
|
52 entry.close(); |
|
53 |
|
54 asyncOpenCacheEntry("http://data/", |
|
55 "disk", Ci.nsICacheStorage.OPEN_READONLY, null, |
|
56 this.checkData.bind(this)); |
|
57 }, |
|
58 |
|
59 checkData: function(status, entry) { |
|
60 do_check_eq(status, Cr.NS_OK); |
|
61 var self = this; |
|
62 pumpReadStream(entry.openInputStream(0), function(str) { |
|
63 do_check_eq(str.length, 10); |
|
64 do_check_eq(str, "12345abcde"); |
|
65 entry.close(); |
|
66 |
|
67 do_execute_soon(self._callback); |
|
68 }); |
|
69 } |
|
70 }; |
|
71 |
|
72 function run_test() { |
|
73 do_get_profile(); |
|
74 new TestAppend(false, run_test2); |
|
75 do_test_pending(); |
|
76 } |
|
77 |
|
78 function run_test2() { |
|
79 new TestAppend(true, do_test_finished); |
|
80 } |