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.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const RAND = Math.random(); |
michael@0 | 7 | const URL = "http://mochi.test:8888/browser/" + |
michael@0 | 8 | "browser/components/sessionstore/test/browser_sessionStorage.html" + |
michael@0 | 9 | "?" + RAND; |
michael@0 | 10 | |
michael@0 | 11 | const OUTER_VALUE = "outer-value-" + RAND; |
michael@0 | 12 | const INNER_VALUE = "inner-value-" + RAND; |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * This test ensures that setting, modifying and restoring sessionStorage data |
michael@0 | 16 | * works as expected. |
michael@0 | 17 | */ |
michael@0 | 18 | add_task(function session_storage() { |
michael@0 | 19 | let tab = gBrowser.addTab(URL); |
michael@0 | 20 | let browser = tab.linkedBrowser; |
michael@0 | 21 | yield promiseBrowserLoaded(browser); |
michael@0 | 22 | |
michael@0 | 23 | // Flush to make sure chrome received all data. |
michael@0 | 24 | SyncHandlers.get(browser).flush(); |
michael@0 | 25 | |
michael@0 | 26 | let {storage} = JSON.parse(ss.getTabState(tab)); |
michael@0 | 27 | is(storage["http://example.com"].test, INNER_VALUE, |
michael@0 | 28 | "sessionStorage data for example.com has been serialized correctly"); |
michael@0 | 29 | is(storage["http://mochi.test:8888"].test, OUTER_VALUE, |
michael@0 | 30 | "sessionStorage data for mochi.test has been serialized correctly"); |
michael@0 | 31 | |
michael@0 | 32 | // Ensure that modifying sessionStore values works. |
michael@0 | 33 | yield modifySessionStorage(browser, {test: "modified"}); |
michael@0 | 34 | yield modifySessionStorage2(browser, {test: "modified2"}); |
michael@0 | 35 | SyncHandlers.get(browser).flush(); |
michael@0 | 36 | |
michael@0 | 37 | let {storage} = JSON.parse(ss.getTabState(tab)); |
michael@0 | 38 | is(storage["http://example.com"].test, "modified2", |
michael@0 | 39 | "sessionStorage data for example.com has been serialized correctly"); |
michael@0 | 40 | is(storage["http://mochi.test:8888"].test, "modified", |
michael@0 | 41 | "sessionStorage data for mochi.test has been serialized correctly"); |
michael@0 | 42 | |
michael@0 | 43 | // Test that duplicating a tab works. |
michael@0 | 44 | let tab2 = gBrowser.duplicateTab(tab); |
michael@0 | 45 | let browser2 = tab2.linkedBrowser; |
michael@0 | 46 | yield promiseTabRestored(tab2); |
michael@0 | 47 | |
michael@0 | 48 | // Flush to make sure chrome received all data. |
michael@0 | 49 | SyncHandlers.get(browser2).flush(); |
michael@0 | 50 | |
michael@0 | 51 | let {storage} = JSON.parse(ss.getTabState(tab2)); |
michael@0 | 52 | is(storage["http://example.com"].test, "modified2", |
michael@0 | 53 | "sessionStorage data for example.com has been duplicated correctly"); |
michael@0 | 54 | is(storage["http://mochi.test:8888"].test, "modified", |
michael@0 | 55 | "sessionStorage data for mochi.test has been duplicated correctly"); |
michael@0 | 56 | |
michael@0 | 57 | // Ensure that the content script retains restored data |
michael@0 | 58 | // (by e.g. duplicateTab) and sends it along with new data. |
michael@0 | 59 | yield modifySessionStorage(browser2, {test: "modified3"}); |
michael@0 | 60 | SyncHandlers.get(browser2).flush(); |
michael@0 | 61 | |
michael@0 | 62 | let {storage} = JSON.parse(ss.getTabState(tab2)); |
michael@0 | 63 | is(storage["http://example.com"].test, "modified2", |
michael@0 | 64 | "sessionStorage data for example.com has been duplicated correctly"); |
michael@0 | 65 | is(storage["http://mochi.test:8888"].test, "modified3", |
michael@0 | 66 | "sessionStorage data for mochi.test has been duplicated correctly"); |
michael@0 | 67 | |
michael@0 | 68 | // Check that loading a new URL discards data. |
michael@0 | 69 | browser2.loadURI("http://mochi.test:8888/"); |
michael@0 | 70 | yield promiseBrowserLoaded(browser2); |
michael@0 | 71 | SyncHandlers.get(browser2).flush(); |
michael@0 | 72 | |
michael@0 | 73 | let {storage} = JSON.parse(ss.getTabState(tab2)); |
michael@0 | 74 | is(storage["http://mochi.test:8888"].test, "modified3", |
michael@0 | 75 | "navigating retains correct storage data"); |
michael@0 | 76 | ok(!storage["http://example.com"], "storage data was discarded"); |
michael@0 | 77 | |
michael@0 | 78 | // Check that loading a new URL discards data. |
michael@0 | 79 | browser2.loadURI("about:mozilla"); |
michael@0 | 80 | yield promiseBrowserLoaded(browser2); |
michael@0 | 81 | SyncHandlers.get(browser2).flush(); |
michael@0 | 82 | |
michael@0 | 83 | let state = JSON.parse(ss.getTabState(tab2)); |
michael@0 | 84 | ok(!state.hasOwnProperty("storage"), "storage data was discarded"); |
michael@0 | 85 | |
michael@0 | 86 | // Clean up. |
michael@0 | 87 | gBrowser.removeTab(tab); |
michael@0 | 88 | gBrowser.removeTab(tab2); |
michael@0 | 89 | }); |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * This test ensures that purging domain data also purges data from the |
michael@0 | 93 | * sessionStorage data collected for tabs. |
michael@0 | 94 | */ |
michael@0 | 95 | add_task(function purge_domain() { |
michael@0 | 96 | let tab = gBrowser.addTab(URL); |
michael@0 | 97 | let browser = tab.linkedBrowser; |
michael@0 | 98 | yield promiseBrowserLoaded(browser); |
michael@0 | 99 | |
michael@0 | 100 | // Purge data for "mochi.test". |
michael@0 | 101 | yield purgeDomainData(browser, "mochi.test"); |
michael@0 | 102 | |
michael@0 | 103 | // Flush to make sure chrome received all data. |
michael@0 | 104 | SyncHandlers.get(browser).flush(); |
michael@0 | 105 | |
michael@0 | 106 | let {storage} = JSON.parse(ss.getTabState(tab)); |
michael@0 | 107 | ok(!storage["http://mochi.test:8888"], |
michael@0 | 108 | "sessionStorage data for mochi.test has been purged"); |
michael@0 | 109 | is(storage["http://example.com"].test, INNER_VALUE, |
michael@0 | 110 | "sessionStorage data for example.com has been preserved"); |
michael@0 | 111 | |
michael@0 | 112 | gBrowser.removeTab(tab); |
michael@0 | 113 | }); |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * This test ensures that collecting sessionStorage data respects the privacy |
michael@0 | 117 | * levels as set by the user. |
michael@0 | 118 | */ |
michael@0 | 119 | add_task(function respect_privacy_level() { |
michael@0 | 120 | let tab = gBrowser.addTab(URL + "&secure"); |
michael@0 | 121 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 122 | gBrowser.removeTab(tab); |
michael@0 | 123 | |
michael@0 | 124 | let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); |
michael@0 | 125 | is(storage["http://mochi.test:8888"].test, OUTER_VALUE, |
michael@0 | 126 | "http sessionStorage data has been saved"); |
michael@0 | 127 | is(storage["https://example.com"].test, INNER_VALUE, |
michael@0 | 128 | "https sessionStorage data has been saved"); |
michael@0 | 129 | |
michael@0 | 130 | // Disable saving data for encrypted sites. |
michael@0 | 131 | Services.prefs.setIntPref("browser.sessionstore.privacy_level", 1); |
michael@0 | 132 | |
michael@0 | 133 | let tab = gBrowser.addTab(URL + "&secure"); |
michael@0 | 134 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 135 | gBrowser.removeTab(tab); |
michael@0 | 136 | |
michael@0 | 137 | let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); |
michael@0 | 138 | is(storage["http://mochi.test:8888"].test, OUTER_VALUE, |
michael@0 | 139 | "http sessionStorage data has been saved"); |
michael@0 | 140 | ok(!storage["https://example.com"], |
michael@0 | 141 | "https sessionStorage data has *not* been saved"); |
michael@0 | 142 | |
michael@0 | 143 | // Disable saving data for any site. |
michael@0 | 144 | Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2); |
michael@0 | 145 | |
michael@0 | 146 | // Check that duplicating a tab copies all private data. |
michael@0 | 147 | let tab = gBrowser.addTab(URL + "&secure"); |
michael@0 | 148 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 149 | let tab2 = gBrowser.duplicateTab(tab); |
michael@0 | 150 | yield promiseTabRestored(tab2); |
michael@0 | 151 | gBrowser.removeTab(tab); |
michael@0 | 152 | |
michael@0 | 153 | // With privacy_level=2 the |tab| shouldn't have any sessionStorage data. |
michael@0 | 154 | let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); |
michael@0 | 155 | ok(!storage, "sessionStorage data has *not* been saved"); |
michael@0 | 156 | |
michael@0 | 157 | // Restore the default privacy level and close the duplicated tab. |
michael@0 | 158 | Services.prefs.clearUserPref("browser.sessionstore.privacy_level"); |
michael@0 | 159 | gBrowser.removeTab(tab2); |
michael@0 | 160 | |
michael@0 | 161 | // With privacy_level=0 the duplicated |tab2| should persist all data. |
michael@0 | 162 | let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); |
michael@0 | 163 | is(storage["http://mochi.test:8888"].test, OUTER_VALUE, |
michael@0 | 164 | "http sessionStorage data has been saved"); |
michael@0 | 165 | is(storage["https://example.com"].test, INNER_VALUE, |
michael@0 | 166 | "https sessionStorage data has been saved"); |
michael@0 | 167 | }); |
michael@0 | 168 | |
michael@0 | 169 | function waitForStorageEvent(browser) { |
michael@0 | 170 | return promiseContentMessage(browser, "ss-test:MozStorageChanged"); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | function modifySessionStorage(browser, data) { |
michael@0 | 174 | browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data); |
michael@0 | 175 | return waitForStorageEvent(browser); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | function modifySessionStorage2(browser, data) { |
michael@0 | 179 | browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage2", data); |
michael@0 | 180 | return waitForStorageEvent(browser); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | function purgeDomainData(browser, domain) { |
michael@0 | 184 | return sendMessage(browser, "ss-test:purgeDomainData", domain); |
michael@0 | 185 | } |