diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /netwerk/test/unit/test_bug650995.js | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'netwerk/test/unit/test_bug650995.js')
-rw-r--r-- | netwerk/test/unit/test_bug650995.js | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/netwerk/test/unit/test_bug650995.js b/netwerk/test/unit/test_bug650995.js new file mode 100644 index 0000000000..73a004390a --- /dev/null +++ b/netwerk/test/unit/test_bug650995.js @@ -0,0 +1,189 @@ +// +// Test that "max_entry_size" prefs for disk- and memory-cache prevents +// caching resources with size out of bounds +// + +"use strict"; + +const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); + +do_get_profile(); + +const prefService = Cc["@mozilla.org/preferences-service;1"].getService( + Ci.nsIPrefBranch +); + +const httpserver = new HttpServer(); + +// Repeats the given data until the total size is larger than 1K +function repeatToLargerThan1K(data) { + while (data.length <= 1024) { + data += data; + } + return data; +} + +function setupChannel(suffix, value) { + var chan = NetUtil.newChannel({ + uri: "http://localhost:" + httpserver.identity.primaryPort + suffix, + loadUsingSystemPrincipal: true, + }); + var httpChan = chan.QueryInterface(Ci.nsIHttpChannel); + httpChan.setRequestHeader("x-request", value, false); + + return httpChan; +} + +var tests = [ + new InitializeCacheDevices(true, false), // enable and create mem-device + new TestCacheEntrySize( + function() { + prefService.setIntPref("browser.cache.memory.max_entry_size", 1); + }, + "012345", + "9876543210", + "012345" + ), // expect cached value + new TestCacheEntrySize( + function() { + prefService.setIntPref("browser.cache.memory.max_entry_size", 1); + }, + "0123456789a", + "9876543210", + "9876543210" + ), // expect fresh value + new TestCacheEntrySize( + function() { + prefService.setIntPref("browser.cache.memory.max_entry_size", -1); + }, + "0123456789a", + "9876543210", + "0123456789a" + ), // expect cached value + + new InitializeCacheDevices(false, true), // enable and create disk-device + new TestCacheEntrySize( + function() { + prefService.setIntPref("browser.cache.disk.max_entry_size", 1); + }, + "012345", + "9876543210", + "012345" + ), // expect cached value + new TestCacheEntrySize( + function() { + prefService.setIntPref("browser.cache.disk.max_entry_size", 1); + }, + "0123456789a", + "9876543210", + "9876543210" + ), // expect fresh value + new TestCacheEntrySize( + function() { + prefService.setIntPref("browser.cache.disk.max_entry_size", -1); + }, + "0123456789a", + "9876543210", + "0123456789a" + ), // expect cached value +]; + +function nextTest() { + // We really want each test to be self-contained. Make sure cache is + // cleared and also let all operations finish before starting a new test + syncWithCacheIOThread(function() { + get_cache_service().clear(); + syncWithCacheIOThread(runNextTest); + }); +} + +function runNextTest() { + var aTest = tests.shift(); + if (!aTest) { + httpserver.stop(do_test_finished); + return; + } + executeSoon(function() { + aTest.start(); + }); +} + +// Just make sure devices are created +function InitializeCacheDevices(memDevice, diskDevice) { + this.start = function() { + prefService.setBoolPref("browser.cache.memory.enable", memDevice); + if (memDevice) { + let cap = prefService.getIntPref("browser.cache.memory.capacity", 0); + if (cap == 0) { + prefService.setIntPref("browser.cache.memory.capacity", 1024); + } + } + prefService.setBoolPref("browser.cache.disk.enable", diskDevice); + if (diskDevice) { + let cap = prefService.getIntPref("browser.cache.disk.capacity", 0); + if (cap == 0) { + prefService.setIntPref("browser.cache.disk.capacity", 1024); + } + } + var channel = setupChannel("/bug650995", "Initial value"); + channel.asyncOpen(new ChannelListener(nextTest, null)); + }; +} + +function TestCacheEntrySize( + setSizeFunc, + firstRequest, + secondRequest, + secondExpectedReply +) { + // Initially, this test used 10 bytes as the limit for caching entries. + // Since we now use 1K granularity we have to extend lengths to be larger + // than 1K if it is larger than 10 + if (firstRequest.length > 10) { + firstRequest = repeatToLargerThan1K(firstRequest); + } + if (secondExpectedReply.length > 10) { + secondExpectedReply = repeatToLargerThan1K(secondExpectedReply); + } + + this.start = function() { + setSizeFunc(); + var channel = setupChannel("/bug650995", firstRequest); + channel.asyncOpen(new ChannelListener(this.initialLoad, this)); + }; + this.initialLoad = function(request, data, ctx) { + Assert.equal(firstRequest, data); + var channel = setupChannel("/bug650995", secondRequest); + executeSoon(function() { + channel.asyncOpen(new ChannelListener(ctx.testAndTriggerNext, ctx)); + }); + }; + this.testAndTriggerNext = function(request, data, ctx) { + Assert.equal(secondExpectedReply, data); + executeSoon(nextTest); + }; +} + +function run_test() { + httpserver.registerPathHandler("/bug650995", handler); + httpserver.start(-1); + + prefService.setBoolPref("browser.cache.offline.enable", false); + prefService.setBoolPref("browser.cache.offline.storage.enable", false); + prefService.setBoolPref("network.http.rcwn.enabled", false); + + nextTest(); + do_test_pending(); +} + +function handler(metadata, response) { + var body = "BOOM!"; + try { + body = metadata.getHeader("x-request"); + } catch (e) {} + + response.setStatusLine(metadata.httpVersion, 200, "Ok"); + response.setHeader("Content-Type", "text/plain", false); + response.setHeader("Cache-Control", "max-age=3600", false); + response.bodyOutputStream.write(body, body.length); +} |