dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 <html xmlns="http://www.w3.org/1999/xhtml">
michael@0 2 <head>
michael@0 3 <title>localStorage and DOM quota test</title>
michael@0 4
michael@0 5 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 7
michael@0 8 <script type="text/javascript">
michael@0 9 SimpleTest.waitForExplicitFinish();
michael@0 10
michael@0 11 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 12
michael@0 13 const Ci = Components.interfaces;
michael@0 14 const CONTENT_PAGE = "http://mochi.test:8888/chrome/dom/tests/mochitest/localstorage/page_blank.html";
michael@0 15 const slavePath = "/tests/dom/tests/mochitest/localstorage/";
michael@0 16 var currentTest = 1;
michael@0 17 var quota;
michael@0 18
michael@0 19 try {
michael@0 20 quota = Services.prefs.getIntPref("dom.storage.default_quota");
michael@0 21 } catch (ex) {
michael@0 22 quota = 5 * 1024;
michael@0 23 }
michael@0 24 Services.prefs.setIntPref("browser.startup.page", 0);
michael@0 25 Services.prefs.setIntPref("dom.storage.default_quota", 1);
michael@0 26
michael@0 27 var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor).
michael@0 28 getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).
michael@0 29 rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).
michael@0 30 getInterface(Ci.nsIDOMWindow);
michael@0 31 var slaveLoadsPending = 1;
michael@0 32 var slaveOrigin = "";
michael@0 33 var slave = null;
michael@0 34 var failureRegExp = new RegExp("^FAILURE");
michael@0 35
michael@0 36 function startTest() {
michael@0 37 testOnWindow(true, function(aWindow) {
michael@0 38 info("Private window loaded");
michael@0 39 var frame = aWindow.content.document.createElement("iframe");
michael@0 40 aWindow.content.document.body.appendChild(frame);
michael@0 41 aWindow.content.addEventListener("message", function(aEvent) {
michael@0 42 onMessageReceived(aEvent, aWindow)
michael@0 43 }, false);
michael@0 44 slave = aWindow.content.frames[0];
michael@0 45
michael@0 46 SimpleTest.waitForFocus(function() doNextTest(aWindow), aWindow);
michael@0 47 });
michael@0 48 }
michael@0 49
michael@0 50 function doNextTest(aWindow) {
michael@0 51 info("Running test: " + currentTest);
michael@0 52 switch (currentTest) {
michael@0 53 // Initialy setup the quota to testing value of 1024B and
michael@0 54 // set a 500 bytes key with name length 1 (allocate 501 bytes)
michael@0 55 case 1:
michael@0 56 slaveOrigin = "http://example.com";
michael@0 57 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&A&success";
michael@0 58 break;
michael@0 59
michael@0 60 // In subdomain now set another key with length 500 bytes, i.e.
michael@0 61 // allocate 501 bytes
michael@0 62 case 2:
michael@0 63 slaveOrigin = "http://test1.example.com";
michael@0 64 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success";
michael@0 65 break;
michael@0 66
michael@0 67 // Try to set the same key value again to check we don't fail
michael@0 68 // even 1002 bytes has already been exhausted from the quota
michael@0 69 // We just change the value of an existing key.
michael@0 70 case 3:
michael@0 71 slaveOrigin = "http://test1.example.com";
michael@0 72 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&B&success";
michael@0 73 break;
michael@0 74
michael@0 75 // Try to set the same key to a larger value that would lead to
michael@0 76 // quota reach and check that the value is still the old one
michael@0 77 case 4:
michael@0 78 slaveOrigin = "http://test1.example.com";
michael@0 79 slave.location = slaveOrigin + slavePath + "frameQuota.html?add2&B&failure";
michael@0 80 break;
michael@0 81
michael@0 82 // In a different subdomain try to set a new 500 bytes key
michael@0 83 // and check we fail because we are over the quota
michael@0 84 case 5:
michael@0 85 slaveOrigin = "https://test2.example.com";
michael@0 86 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&failure";
michael@0 87 break;
michael@0 88
michael@0 89 // Remove from the second subdomain the second key, it must not fail
michael@0 90 // This should release the allocated space of the quota assigned to
michael@0 91 // example.com.
michael@0 92 case 6:
michael@0 93 slaveOrigin = "http://test1.example.com";
michael@0 94 slave.location = slaveOrigin + slavePath + "frameQuota.html?remove&B&success";
michael@0 95 break;
michael@0 96
michael@0 97 // Now try again to set 500 bytes key, it must succeed.
michael@0 98 case 7:
michael@0 99 slaveOrigin = "https://test2.example.com";
michael@0 100 slave.location = slaveOrigin + slavePath + "frameQuota.html?add&C&success";
michael@0 101 break;
michael@0 102
michael@0 103 case 8:
michael@0 104 // Do a clean up...
michael@0 105 // TODO Bug 455070, use just ?clear what invokes call
michael@0 106 // of clear() in the target frame. W/o clear method we must
michael@0 107 // call clear implemented as removeItem for each item in
michael@0 108 // the localStorage.
michael@0 109 slaveOrigin = "http://example.com";
michael@0 110 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&A&";
michael@0 111 break;
michael@0 112
michael@0 113 case 9:
michael@0 114 // Do a clean up...
michael@0 115 slaveOrigin = "http://test1.example.com";
michael@0 116 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&B&";
michael@0 117 break;
michael@0 118
michael@0 119 case 10:
michael@0 120 // Do a clean up...
michael@0 121 slaveOrigin = "https://test2.example.com";
michael@0 122 slave.location = slaveOrigin + slavePath + "frameQuota.html?clear&C&";
michael@0 123 break;
michael@0 124
michael@0 125 default:
michael@0 126 Services.prefs.clearUserPref("browser.startup.page")
michael@0 127 Services.prefs.setIntPref("dom.storage.default_quota", quota);
michael@0 128 aWindow.close();
michael@0 129 SimpleTest.finish();
michael@0 130 }
michael@0 131
michael@0 132 ++currentTest;
michael@0 133 }
michael@0 134
michael@0 135 function onMessageReceived(event, aWindow) {
michael@0 136 info("Message received: " + event.data);
michael@0 137 switch (event.data) {
michael@0 138 // Indication of the frame onload event
michael@0 139 case "frame loaded":
michael@0 140 if (--slaveLoadsPending)
michael@0 141 break;
michael@0 142 // Just fall through...
michael@0 143 // Indication of successfully finished step of a test
michael@0 144 case "perf":
michael@0 145 slave.postMessage("step", slaveOrigin);
michael@0 146 break;
michael@0 147 // Indication of all test parts finish (from any of the frames)
michael@0 148 case "done":
michael@0 149 aWindow.content.localStorage.clear();
michael@0 150 slaveLoadsPending = 1;
michael@0 151 doNextTest(aWindow);
michael@0 152 break;
michael@0 153 // Any other message indicates error or succes message of a test
michael@0 154 default:
michael@0 155 SimpleTest.ok(!event.data.match(failureRegExp), event.data);
michael@0 156 break;
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 function testOnWindow(aIsPrivate, aCallback) {
michael@0 161 var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
michael@0 162 win.addEventListener("load", function onLoad() {
michael@0 163 win.removeEventListener("load", onLoad, false);
michael@0 164 win.addEventListener("DOMContentLoaded", function onInnerLoad() {
michael@0 165 if (win.content.location.href != CONTENT_PAGE) {
michael@0 166 win.gBrowser.loadURI(CONTENT_PAGE);
michael@0 167 return;
michael@0 168 }
michael@0 169 win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
michael@0 170 win.gBrowser.selectedBrowser.focus();
michael@0 171 SimpleTest.executeSoon(function() { aCallback(win); });
michael@0 172 }, true);
michael@0 173 win.gBrowser.loadURI(CONTENT_PAGE);
michael@0 174 }, true);
michael@0 175 }
michael@0 176
michael@0 177 </script>
michael@0 178 </head>
michael@0 179 <body onload="startTest();">
michael@0 180 </body>
michael@0 181 </html>

mercurial