Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | // |
michael@0 | 2 | // Test that "max_entry_size" prefs for disk- and memory-cache prevents |
michael@0 | 3 | // caching resources with size out of bounds |
michael@0 | 4 | // |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 7 | |
michael@0 | 8 | do_get_profile(); |
michael@0 | 9 | |
michael@0 | 10 | const prefService = Cc["@mozilla.org/preferences-service;1"] |
michael@0 | 11 | .getService(Ci.nsIPrefBranch); |
michael@0 | 12 | |
michael@0 | 13 | const httpserver = new HttpServer(); |
michael@0 | 14 | |
michael@0 | 15 | // Repeats the given data until the total size is larger than 1K |
michael@0 | 16 | function repeatToLargerThan1K(data) { |
michael@0 | 17 | while(data.length <= 1024) |
michael@0 | 18 | data += data; |
michael@0 | 19 | return data; |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function setupChannel(suffix, value) { |
michael@0 | 23 | var ios = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 24 | .getService(Ci.nsIIOService); |
michael@0 | 25 | var chan = ios.newChannel("http://localhost:" + |
michael@0 | 26 | httpserver.identity.primaryPort + |
michael@0 | 27 | suffix, "", null); |
michael@0 | 28 | var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel); |
michael@0 | 29 | httpChan.setRequestHeader("x-request", value, false); |
michael@0 | 30 | |
michael@0 | 31 | return httpChan; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | var tests = [ |
michael@0 | 35 | new InitializeCacheDevices(true, false), // enable and create mem-device |
michael@0 | 36 | new TestCacheEntrySize( |
michael@0 | 37 | function() { prefService.setIntPref("browser.cache.memory.max_entry_size", 1); }, |
michael@0 | 38 | "012345", "9876543210", "012345"), // expect cached value |
michael@0 | 39 | new TestCacheEntrySize( |
michael@0 | 40 | function() { prefService.setIntPref("browser.cache.memory.max_entry_size", 1); }, |
michael@0 | 41 | "0123456789a", "9876543210", "9876543210"), // expect fresh value |
michael@0 | 42 | new TestCacheEntrySize( |
michael@0 | 43 | function() { prefService.setIntPref("browser.cache.memory.max_entry_size", -1); }, |
michael@0 | 44 | "0123456789a", "9876543210", "0123456789a"), // expect cached value |
michael@0 | 45 | |
michael@0 | 46 | new InitializeCacheDevices(false, true), // enable and create disk-device |
michael@0 | 47 | new TestCacheEntrySize( |
michael@0 | 48 | function() { prefService.setIntPref("browser.cache.disk.max_entry_size", 1); }, |
michael@0 | 49 | "012345", "9876543210", "012345"), // expect cached value |
michael@0 | 50 | new TestCacheEntrySize( |
michael@0 | 51 | function() { prefService.setIntPref("browser.cache.disk.max_entry_size", 1); }, |
michael@0 | 52 | "0123456789a", "9876543210", "9876543210"), // expect fresh value |
michael@0 | 53 | new TestCacheEntrySize( |
michael@0 | 54 | function() { prefService.setIntPref("browser.cache.disk.max_entry_size", -1); }, |
michael@0 | 55 | "0123456789a", "9876543210", "0123456789a"), // expect cached value |
michael@0 | 56 | ]; |
michael@0 | 57 | |
michael@0 | 58 | function nextTest() { |
michael@0 | 59 | // We really want each test to be self-contained. Make sure cache is |
michael@0 | 60 | // cleared and also let all operations finish before starting a new test |
michael@0 | 61 | syncWithCacheIOThread(function() { |
michael@0 | 62 | get_cache_service().clear(); |
michael@0 | 63 | syncWithCacheIOThread(runNextTest); |
michael@0 | 64 | }); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function runNextTest() { |
michael@0 | 68 | var aTest = tests.shift(); |
michael@0 | 69 | if (!aTest) { |
michael@0 | 70 | httpserver.stop(do_test_finished); |
michael@0 | 71 | return; |
michael@0 | 72 | } |
michael@0 | 73 | do_execute_soon(function() { aTest.start(); } ); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // Just make sure devices are created |
michael@0 | 77 | function InitializeCacheDevices(memDevice, diskDevice) { |
michael@0 | 78 | this.start = function() { |
michael@0 | 79 | prefService.setBoolPref("browser.cache.memory.enable", memDevice); |
michael@0 | 80 | if (memDevice) { |
michael@0 | 81 | try { |
michael@0 | 82 | cap = prefService.getIntPref("browser.cache.memory.capacity"); |
michael@0 | 83 | } |
michael@0 | 84 | catch(ex) { |
michael@0 | 85 | cap = 0; |
michael@0 | 86 | } |
michael@0 | 87 | if (cap == 0) { |
michael@0 | 88 | prefService.setIntPref("browser.cache.memory.capacity", 1024); |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | prefService.setBoolPref("browser.cache.disk.enable", diskDevice); |
michael@0 | 92 | if (diskDevice) { |
michael@0 | 93 | try { |
michael@0 | 94 | cap = prefService.getIntPref("browser.cache.disk.capacity"); |
michael@0 | 95 | } |
michael@0 | 96 | catch(ex) { |
michael@0 | 97 | cap = 0; |
michael@0 | 98 | } |
michael@0 | 99 | if (cap == 0) { |
michael@0 | 100 | prefService.setIntPref("browser.cache.disk.capacity", 1024); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | var channel = setupChannel("/bug650995", "Initial value"); |
michael@0 | 104 | channel.asyncOpen(new ChannelListener( |
michael@0 | 105 | nextTest, null), |
michael@0 | 106 | null); |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | function TestCacheEntrySize(setSizeFunc, firstRequest, secondRequest, secondExpectedReply) { |
michael@0 | 111 | |
michael@0 | 112 | // Initially, this test used 10 bytes as the limit for caching entries. |
michael@0 | 113 | // Since we now use 1K granularity we have to extend lengths to be larger |
michael@0 | 114 | // than 1K if it is larger than 10 |
michael@0 | 115 | if (firstRequest.length > 10) |
michael@0 | 116 | firstRequest = repeatToLargerThan1K(firstRequest); |
michael@0 | 117 | if (secondExpectedReply.length > 10) |
michael@0 | 118 | secondExpectedReply = repeatToLargerThan1K(secondExpectedReply); |
michael@0 | 119 | |
michael@0 | 120 | this.start = function() { |
michael@0 | 121 | setSizeFunc(); |
michael@0 | 122 | var channel = setupChannel("/bug650995", firstRequest); |
michael@0 | 123 | channel.asyncOpen(new ChannelListener(this.initialLoad, this), null); |
michael@0 | 124 | }, |
michael@0 | 125 | |
michael@0 | 126 | this.initialLoad = function(request, data, ctx) { |
michael@0 | 127 | do_check_eq(firstRequest, data); |
michael@0 | 128 | var channel = setupChannel("/bug650995", secondRequest); |
michael@0 | 129 | do_execute_soon(function() { |
michael@0 | 130 | channel.asyncOpen(new ChannelListener(ctx.testAndTriggerNext, ctx), null); |
michael@0 | 131 | }); |
michael@0 | 132 | }, |
michael@0 | 133 | |
michael@0 | 134 | this.testAndTriggerNext = function(request, data, ctx) { |
michael@0 | 135 | do_check_eq(secondExpectedReply, data); |
michael@0 | 136 | do_execute_soon(nextTest); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | function run_test() |
michael@0 | 141 | { |
michael@0 | 142 | httpserver.registerPathHandler("/bug650995", handler); |
michael@0 | 143 | httpserver.start(-1); |
michael@0 | 144 | |
michael@0 | 145 | prefService.setBoolPref("browser.cache.offline.enable", false); |
michael@0 | 146 | |
michael@0 | 147 | nextTest(); |
michael@0 | 148 | do_test_pending(); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | function handler(metadata, response) { |
michael@0 | 152 | var body = "BOOM!"; |
michael@0 | 153 | try { |
michael@0 | 154 | body = metadata.getHeader("x-request"); |
michael@0 | 155 | } catch(e) {} |
michael@0 | 156 | |
michael@0 | 157 | response.setStatusLine(metadata.httpVersion, 200, "Ok"); |
michael@0 | 158 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 159 | response.setHeader("Cache-Control", "max-age=3600", false); |
michael@0 | 160 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 161 | } |