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