Bug 648429: HTTP cache: compress all compressible files - new test for appending to compressed stream; r=michal.novotny

This commit is contained in:
Geoff Brown 2011-12-17 11:32:02 +01:00
Родитель 67cc765f77
Коммит d72998fbbb
2 изменённых файлов: 107 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,106 @@
//
// Test that data can be appended to a cache entry even when the data is
// compressed by the cache compression feature - bug 648429.
//
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
var _CSvc;
function get_cache_service() {
if (_CSvc)
return _CSvc;
return _CSvc = Cc["@mozilla.org/network/cache-service;1"].
getService(Ci.nsICacheService);
}
function get_ostream_for_entry(key, append, compress, entryRef)
{
var cache = get_cache_service();
var session = cache.createSession(
"HTTP",
Ci.nsICache.STORE_ON_DISK,
Ci.nsICache.STREAM_BASED);
var cacheEntry = session.openCacheEntry(
key,
append ? Ci.nsICache.ACCESS_READ_WRITE
: Ci.nsICache.ACCESS_WRITE,
true);
if (compress)
if (!append)
cacheEntry.setMetaDataElement("uncompressed-len", "0");
var oStream = cacheEntry.openOutputStream(append ? cacheEntry.storageDataSize : 0);
entryRef.value = cacheEntry;
return oStream;
}
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 check_datafile()
{
var cache = get_cache_service();
var session = cache.createSession(
"HTTP",
Ci.nsICache.STORE_ON_DISK,
Ci.nsICache.STREAM_BASED);
var entry = session.openCacheEntry(
"data",
Ci.nsICache.ACCESS_READ,
true);
var wrapper = Cc["@mozilla.org/scriptableinputstream;1"].
createInstance(Ci.nsIScriptableInputStream);
wrapper.init(entry.openInputStream(0));
var str = wrapper.read(wrapper.available());
do_check_eq(str.length, 10);
do_check_eq(str, "12345abcde");
wrapper.close();
entry.close();
}
function write_datafile(compress)
{
var entry = {};
var oStr = get_ostream_for_entry("data", false, compress, entry);
write_and_check(oStr, "12345", 5);
oStr.close();
entry.value.close();
}
function append_datafile()
{
var entry = {};
var oStr = get_ostream_for_entry("data", true, false, entry);
write_and_check(oStr, "abcde", 5);
oStr.close();
entry.value.close();
}
function test_append(compress)
{
get_cache_service().evictEntries(Ci.nsICache.STORE_ANYWHERE);
write_datafile(compress);
append_datafile();
check_datafile();
}
function run_test() {
do_get_profile();
test_append(false);
test_append(true);
}

Просмотреть файл

@ -86,6 +86,7 @@ fail-if = os == "android"
[test_cacheflags.js]
[test_channel_close.js]
[test_compareURIs.js]
[test_compressappend.js]
[test_content_sniffer.js]
[test_cookie_header.js]
[test_data_protocol.js]