test_bug650995.js (5132B)
1 // 2 // Test that "max_entry_size" prefs for disk- and memory-cache prevents 3 // caching resources with size out of bounds 4 // 5 6 "use strict"; 7 8 const { HttpServer } = ChromeUtils.importESModule( 9 "resource://testing-common/httpd.sys.mjs" 10 ); 11 12 do_get_profile(); 13 14 const prefService = Services.prefs; 15 16 const httpserver = new HttpServer(); 17 18 // Repeats the given data until the total size is larger than 1K 19 function repeatToLargerThan1K(data) { 20 while (data.length <= 1024) { 21 data += data; 22 } 23 return data; 24 } 25 26 function setupChannel(suffix, value) { 27 var chan = NetUtil.newChannel({ 28 uri: "http://localhost:" + httpserver.identity.primaryPort + suffix, 29 loadUsingSystemPrincipal: true, 30 }); 31 var httpChan = chan.QueryInterface(Ci.nsIHttpChannel); 32 httpChan.setRequestHeader("x-request", value, false); 33 34 return httpChan; 35 } 36 37 var tests = [ 38 new InitializeCacheDevices(true, false), // enable and create mem-device 39 new TestCacheEntrySize( 40 function () { 41 prefService.setIntPref("browser.cache.memory.max_entry_size", 1); 42 }, 43 "012345", 44 "9876543210", 45 "012345" 46 ), // expect cached value 47 new TestCacheEntrySize( 48 function () { 49 prefService.setIntPref("browser.cache.memory.max_entry_size", 1); 50 }, 51 "0123456789a", 52 "9876543210", 53 "9876543210" 54 ), // expect fresh value 55 new TestCacheEntrySize( 56 function () { 57 prefService.setIntPref("browser.cache.memory.max_entry_size", -1); 58 }, 59 "0123456789a", 60 "9876543210", 61 "0123456789a" 62 ), // expect cached value 63 64 new InitializeCacheDevices(false, true), // enable and create disk-device 65 new TestCacheEntrySize( 66 function () { 67 prefService.setIntPref("browser.cache.disk.max_entry_size", 1); 68 }, 69 "012345", 70 "9876543210", 71 "012345" 72 ), // expect cached value 73 new TestCacheEntrySize( 74 function () { 75 prefService.setIntPref("browser.cache.disk.max_entry_size", 1); 76 }, 77 "0123456789a", 78 "9876543210", 79 "9876543210" 80 ), // expect fresh value 81 new TestCacheEntrySize( 82 function () { 83 prefService.setIntPref("browser.cache.disk.max_entry_size", -1); 84 }, 85 "0123456789a", 86 "9876543210", 87 "0123456789a" 88 ), // expect cached value 89 ]; 90 91 function nextTest() { 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); 97 }); 98 } 99 100 function runNextTest() { 101 var aTest = tests.shift(); 102 if (!aTest) { 103 httpserver.stop(do_test_finished); 104 return; 105 } 106 executeSoon(function () { 107 aTest.start(); 108 }); 109 } 110 111 // Just make sure devices are created 112 function InitializeCacheDevices(memDevice, diskDevice) { 113 this.start = function () { 114 prefService.setBoolPref("browser.cache.memory.enable", memDevice); 115 if (memDevice) { 116 let cap = prefService.getIntPref("browser.cache.memory.capacity", 0); 117 if (cap == 0) { 118 prefService.setIntPref("browser.cache.memory.capacity", 1024); 119 } 120 } 121 prefService.setBoolPref("browser.cache.disk.enable", diskDevice); 122 if (diskDevice) { 123 let cap = prefService.getIntPref("browser.cache.disk.capacity", 0); 124 if (cap == 0) { 125 prefService.setIntPref("browser.cache.disk.capacity", 1024); 126 } 127 } 128 var channel = setupChannel("/bug650995", "Initial value"); 129 channel.asyncOpen(new ChannelListener(nextTest, null)); 130 }; 131 } 132 133 function TestCacheEntrySize( 134 setSizeFunc, 135 firstRequest, 136 secondRequest, 137 secondExpectedReply 138 ) { 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); 144 } 145 if (secondExpectedReply.length > 10) { 146 secondExpectedReply = repeatToLargerThan1K(secondExpectedReply); 147 } 148 149 this.start = function () { 150 setSizeFunc(); 151 var channel = setupChannel("/bug650995", firstRequest); 152 channel.asyncOpen(new ChannelListener(this.initialLoad, this)); 153 }; 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)); 159 }); 160 }; 161 this.testAndTriggerNext = function (request, data) { 162 Assert.equal(secondExpectedReply, data); 163 executeSoon(nextTest); 164 }; 165 } 166 167 function run_test() { 168 httpserver.registerPathHandler("/bug650995", handler); 169 httpserver.start(-1); 170 171 prefService.setBoolPref("network.http.rcwn.enabled", false); 172 173 nextTest(); 174 do_test_pending(); 175 } 176 177 function handler(metadata, response) { 178 var body = "BOOM!"; 179 try { 180 body = metadata.getHeader("x-request"); 181 } catch (e) {} 182 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); 187 }