2 // Test that "max_entry_size" prefs for disk- and memory-cache prevents
3 // caching resources with size out of bounds
8 const { HttpServer } = ChromeUtils.importESModule(
9 "resource://testing-common/httpd.sys.mjs"
14 const prefService = Services.prefs;
16 const httpserver = new HttpServer();
18 // Repeats the given data until the total size is larger than 1K
19 function repeatToLargerThan1K(data) {
20 while (data.length <= 1024) {
26 function setupChannel(suffix, value) {
27 var chan = NetUtil.newChannel({
28 uri: "http://localhost:" + httpserver.identity.primaryPort + suffix,
29 loadUsingSystemPrincipal: true,
31 var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
32 httpChan.setRequestHeader("x-request", value, false);
38 new InitializeCacheDevices(true, false), // enable and create mem-device
39 new TestCacheEntrySize(
41 prefService.setIntPref("browser.cache.memory.max_entry_size", 1);
46 ), // expect cached value
47 new TestCacheEntrySize(
49 prefService.setIntPref("browser.cache.memory.max_entry_size", 1);
54 ), // expect fresh value
55 new TestCacheEntrySize(
57 prefService.setIntPref("browser.cache.memory.max_entry_size", -1);
62 ), // expect cached value
64 new InitializeCacheDevices(false, true), // enable and create disk-device
65 new TestCacheEntrySize(
67 prefService.setIntPref("browser.cache.disk.max_entry_size", 1);
72 ), // expect cached value
73 new TestCacheEntrySize(
75 prefService.setIntPref("browser.cache.disk.max_entry_size", 1);
80 ), // expect fresh value
81 new TestCacheEntrySize(
83 prefService.setIntPref("browser.cache.disk.max_entry_size", -1);
88 ), // expect cached value
92 // We really want each test to be self-contained. Make sure cache is
93 // cleared and also let all operations finish before starting a new test
94 syncWithCacheIOThread(function () {
95 Services.cache2.clear();
96 syncWithCacheIOThread(runNextTest);
100 function runNextTest() {
101 var aTest = tests.shift();
103 httpserver.stop(do_test_finished);
106 executeSoon(function () {
111 // Just make sure devices are created
112 function InitializeCacheDevices(memDevice, diskDevice) {
113 this.start = function () {
114 prefService.setBoolPref("browser.cache.memory.enable", memDevice);
116 let cap = prefService.getIntPref("browser.cache.memory.capacity", 0);
118 prefService.setIntPref("browser.cache.memory.capacity", 1024);
121 prefService.setBoolPref("browser.cache.disk.enable", diskDevice);
123 let cap = prefService.getIntPref("browser.cache.disk.capacity", 0);
125 prefService.setIntPref("browser.cache.disk.capacity", 1024);
128 var channel = setupChannel("/bug650995", "Initial value");
129 channel.asyncOpen(new ChannelListener(nextTest, null));
133 function TestCacheEntrySize(
139 // Initially, this test used 10 bytes as the limit for caching entries.
140 // Since we now use 1K granularity we have to extend lengths to be larger
141 // than 1K if it is larger than 10
142 if (firstRequest.length > 10) {
143 firstRequest = repeatToLargerThan1K(firstRequest);
145 if (secondExpectedReply.length > 10) {
146 secondExpectedReply = repeatToLargerThan1K(secondExpectedReply);
149 this.start = function () {
151 var channel = setupChannel("/bug650995", firstRequest);
152 channel.asyncOpen(new ChannelListener(this.initialLoad, this));
154 this.initialLoad = function (request, data, ctx) {
155 Assert.equal(firstRequest, data);
156 var channel = setupChannel("/bug650995", secondRequest);
157 executeSoon(function () {
158 channel.asyncOpen(new ChannelListener(ctx.testAndTriggerNext, ctx));
161 this.testAndTriggerNext = function (request, data, ctx) {
162 Assert.equal(secondExpectedReply, data);
163 executeSoon(nextTest);
167 function run_test() {
168 httpserver.registerPathHandler("/bug650995", handler);
169 httpserver.start(-1);
171 prefService.setBoolPref("network.http.rcwn.enabled", false);
177 function handler(metadata, response) {
180 body = metadata.getHeader("x-request");
183 response.setStatusLine(metadata.httpVersion, 200, "Ok");
184 response.setHeader("Content-Type", "text/plain", false);
185 response.setHeader("Cache-Control", "max-age=3600", false);
186 response.bodyOutputStream.write(body, body.length);