test_doomentry.js (2326B)
1 /** 2 * Test for nsICacheStorage.asyncDoomURI(). 3 * It tests dooming 4 * - an existent inactive entry 5 * - a non-existent inactive entry 6 * - an existent active entry 7 */ 8 9 "use strict"; 10 11 function doom(url, callback) { 12 Services.cache2 13 .diskCacheStorage(Services.loadContextInfo.default) 14 .asyncDoomURI(createURI(url), "", { 15 onCacheEntryDoomed(result) { 16 callback(result); 17 }, 18 }); 19 } 20 21 function write_and_check(str, data, len) { 22 var written = str.write(data, len); 23 if (written != len) { 24 do_throw( 25 "str.write has not written all data!\n" + 26 " Expected: " + 27 len + 28 "\n" + 29 " Actual: " + 30 written + 31 "\n" 32 ); 33 } 34 } 35 36 function write_entry() { 37 asyncOpenCacheEntry( 38 "http://testentry/", 39 "disk", 40 Ci.nsICacheStorage.OPEN_TRUNCATE, 41 null, 42 function (status, entry) { 43 write_entry_cont(entry, entry.openOutputStream(0, -1)); 44 } 45 ); 46 } 47 48 function write_entry_cont(entry, ostream) { 49 var data = "testdata"; 50 write_and_check(ostream, data, data.length); 51 ostream.close(); 52 doom("http://testentry/", check_doom1); 53 } 54 55 function check_doom1(status) { 56 Assert.equal(status, Cr.NS_OK); 57 doom("http://nonexistententry/", check_doom2); 58 } 59 60 function check_doom2(status) { 61 Assert.equal(status, Cr.NS_ERROR_NOT_AVAILABLE); 62 asyncOpenCacheEntry( 63 "http://testentry/", 64 "disk", 65 Ci.nsICacheStorage.OPEN_TRUNCATE, 66 null, 67 function (stat, entry) { 68 write_entry2(entry, entry.openOutputStream(0, -1)); 69 } 70 ); 71 } 72 73 var gOstream; 74 function write_entry2(entry, ostream) { 75 // write some data and doom the entry while it is active 76 var data = "testdata"; 77 write_and_check(ostream, data, data.length); 78 gOstream = ostream; 79 doom("http://testentry/", check_doom3); 80 } 81 82 function check_doom3(status) { 83 Assert.equal(status, Cr.NS_OK); 84 // entry was doomed but writing should still succeed 85 var data = "testdata"; 86 write_and_check(gOstream, data, data.length); 87 gOstream.close(); 88 // dooming the same entry again should fail 89 doom("http://testentry/", check_doom4); 90 } 91 92 function check_doom4(status) { 93 Assert.equal(status, Cr.NS_ERROR_NOT_AVAILABLE); 94 do_test_finished(); 95 } 96 97 function run_test() { 98 do_get_profile(); 99 100 // clear the cache 101 evict_cache_entries(); 102 write_entry(); 103 do_test_pending(); 104 }