gecko-dev/netwerk/test/unit/test_bug654926_doom_and_read.js
Michal Novotny 2ec7aba459 Bug 737615: Replace usage of openCacheEntry with asyncOpenCacheEntry in Necko unit tests, r=bsmith
--HG--
extra : rebase_source : 626a90178480b47f24956a86914fdc3e76cbd20b
2012-06-12 14:23:32 -07:00

86 lines
1.9 KiB
JavaScript

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
function gen_1MiB()
{
var i;
var data="x";
for (i=0 ; i < 20 ; i++)
data+=data;
return data;
}
function write_and_check(str, data, len)
{
var written = str.write(data, len);
if (written != len) {
do_throw("str.write has not written all data!\n" +
" Expected: " + len + "\n" +
" Actual: " + written + "\n");
}
}
function make_input_stream_scriptable(input) {
var wrapper = Cc["@mozilla.org/scriptableinputstream;1"].
createInstance(Ci.nsIScriptableInputStream);
wrapper.init(input);
return wrapper;
}
function write_datafile(status, entry)
{
do_check_eq(status, Cr.NS_OK);
var os = entry.openOutputStream(0);
var data = gen_1MiB();
write_and_check(os, data, data.length);
os.close();
entry.close();
// open, doom, append, read
asyncOpenCacheEntry("data",
"HTTP",
Ci.nsICache.STORE_ON_DISK,
Ci.nsICache.ACCESS_READ_WRITE,
test_read_after_doom);
}
function test_read_after_doom(status, entry)
{
do_check_eq(status, Cr.NS_OK);
var os = entry.openOutputStream(entry.dataSize);
var data = gen_1MiB();
entry.doom();
write_and_check(os, data, data.length);
os.close();
var is = make_input_stream_scriptable(entry.openInputStream(0));
var read = is.read(is.available());
do_check_eq(read.length, 2*1024*1024);
is.close();
entry.close();
do_test_finished();
}
function run_test() {
do_get_profile();
// clear the cache
evict_cache_entries();
// force to write file bigger than 5MiB
asyncOpenCacheEntry("data",
"HTTP",
Ci.nsICache.STORE_ON_DISK_AS_FILE,
Ci.nsICache.ACCESS_WRITE,
write_datafile);
do_test_pending();
}