test_compressappend.js (2018B)
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 "use strict"; 7 8 function write_and_check(str, data, len) { 9 var written = str.write(data, len); 10 if (written != len) { 11 do_throw( 12 "str.write has not written all data!\n" + 13 " Expected: " + 14 len + 15 "\n" + 16 " Actual: " + 17 written + 18 "\n" 19 ); 20 } 21 } 22 23 function TestAppend(compress, callback) { 24 this._compress = compress; 25 this._callback = callback; 26 this.run(); 27 } 28 29 TestAppend.prototype = { 30 _compress: false, 31 _callback: null, 32 33 run() { 34 evict_cache_entries(); 35 asyncOpenCacheEntry( 36 "http://data/", 37 "disk", 38 Ci.nsICacheStorage.OPEN_NORMALLY, 39 null, 40 this.writeData.bind(this) 41 ); 42 }, 43 44 writeData(status, entry) { 45 Assert.equal(status, Cr.NS_OK); 46 if (this._compress) { 47 entry.setMetaDataElement("uncompressed-len", "0"); 48 } 49 var os = entry.openOutputStream(0, 5); 50 write_and_check(os, "12345", 5); 51 os.close(); 52 53 asyncOpenCacheEntry( 54 "http://data/", 55 "disk", 56 Ci.nsICacheStorage.OPEN_NORMALLY, 57 null, 58 this.appendData.bind(this) 59 ); 60 }, 61 62 appendData(status, entry) { 63 Assert.equal(status, Cr.NS_OK); 64 var os = entry.openOutputStream(entry.storageDataSize, 5); 65 write_and_check(os, "abcde", 5); 66 os.close(); 67 68 asyncOpenCacheEntry( 69 "http://data/", 70 "disk", 71 Ci.nsICacheStorage.OPEN_READONLY, 72 null, 73 this.checkData.bind(this) 74 ); 75 }, 76 77 checkData(status, entry) { 78 Assert.equal(status, Cr.NS_OK); 79 var self = this; 80 pumpReadStream(entry.openInputStream(0), function (str) { 81 Assert.equal(str.length, 10); 82 Assert.equal(str, "12345abcde"); 83 84 executeSoon(self._callback); 85 }); 86 }, 87 }; 88 89 function run_test() { 90 do_get_profile(); 91 new TestAppend(false, run_test2); 92 do_test_pending(); 93 } 94 95 function run_test2() { 96 new TestAppend(true, do_test_finished); 97 }