2013-10-25 03:02:25 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-12-20 05:23:32 -08:00
|
|
|
const RAND = Math.random();
|
|
|
|
const URL = "http://mochi.test:8888/browser/" +
|
|
|
|
"browser/components/sessionstore/test/browser_sessionStorage.html" +
|
|
|
|
"?" + RAND;
|
2013-10-25 03:02:25 -07:00
|
|
|
|
2013-12-20 05:23:32 -08:00
|
|
|
const OUTER_VALUE = "outer-value-" + RAND;
|
|
|
|
const INNER_VALUE = "inner-value-" + RAND;
|
2013-10-25 03:02:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This test ensures that setting, modifying and restoring sessionStorage data
|
|
|
|
* works as expected.
|
|
|
|
*/
|
|
|
|
add_task(function session_storage() {
|
2013-12-20 05:23:32 -08:00
|
|
|
let tab = gBrowser.addTab(URL);
|
2013-10-25 03:02:25 -07:00
|
|
|
let browser = tab.linkedBrowser;
|
2013-12-20 05:23:32 -08:00
|
|
|
yield promiseBrowserLoaded(browser);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
|
|
|
// Flush to make sure chrome received all data.
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
|
|
|
let {storage} = JSON.parse(ss.getTabState(tab));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://example.com"].test, INNER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for example.com has been serialized correctly");
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://mochi.test:8888"].test, OUTER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for mochi.test has been serialized correctly");
|
|
|
|
|
2014-09-23 15:48:53 -07:00
|
|
|
// Ensure that modifying sessionStore values works for the inner frame only.
|
|
|
|
yield modifySessionStorage2(browser, {test: "modified1"});
|
|
|
|
TabState.flush(browser);
|
|
|
|
|
|
|
|
({storage} = JSON.parse(ss.getTabState(tab)));
|
|
|
|
is(storage["http://example.com"].test, "modified1",
|
|
|
|
"sessionStorage data for example.com has been serialized correctly");
|
|
|
|
is(storage["http://mochi.test:8888"].test, OUTER_VALUE,
|
|
|
|
"sessionStorage data for mochi.test has been serialized correctly");
|
|
|
|
|
|
|
|
// Ensure that modifying sessionStore values works for both frames.
|
2013-10-25 03:02:25 -07:00
|
|
|
yield modifySessionStorage(browser, {test: "modified"});
|
2013-12-20 05:23:32 -08:00
|
|
|
yield modifySessionStorage2(browser, {test: "modified2"});
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
2014-09-15 16:30:47 -07:00
|
|
|
({storage} = JSON.parse(ss.getTabState(tab)));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://example.com"].test, "modified2",
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for example.com has been serialized correctly");
|
|
|
|
is(storage["http://mochi.test:8888"].test, "modified",
|
|
|
|
"sessionStorage data for mochi.test has been serialized correctly");
|
|
|
|
|
|
|
|
// Test that duplicating a tab works.
|
|
|
|
let tab2 = gBrowser.duplicateTab(tab);
|
|
|
|
let browser2 = tab2.linkedBrowser;
|
|
|
|
yield promiseTabRestored(tab2);
|
|
|
|
|
|
|
|
// Flush to make sure chrome received all data.
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser2);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
2014-09-15 16:30:47 -07:00
|
|
|
({storage} = JSON.parse(ss.getTabState(tab2)));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://example.com"].test, "modified2",
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for example.com has been duplicated correctly");
|
|
|
|
is(storage["http://mochi.test:8888"].test, "modified",
|
|
|
|
"sessionStorage data for mochi.test has been duplicated correctly");
|
|
|
|
|
|
|
|
// Ensure that the content script retains restored data
|
2013-12-20 05:23:32 -08:00
|
|
|
// (by e.g. duplicateTab) and sends it along with new data.
|
|
|
|
yield modifySessionStorage(browser2, {test: "modified3"});
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser2);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
2014-09-15 16:30:47 -07:00
|
|
|
({storage} = JSON.parse(ss.getTabState(tab2)));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://example.com"].test, "modified2",
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for example.com has been duplicated correctly");
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://mochi.test:8888"].test, "modified3",
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for mochi.test has been duplicated correctly");
|
|
|
|
|
2013-12-20 05:23:32 -08:00
|
|
|
// Check that loading a new URL discards data.
|
|
|
|
browser2.loadURI("http://mochi.test:8888/");
|
|
|
|
yield promiseBrowserLoaded(browser2);
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser2);
|
2013-12-20 05:23:32 -08:00
|
|
|
|
2014-09-15 16:30:47 -07:00
|
|
|
({storage} = JSON.parse(ss.getTabState(tab2)));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://mochi.test:8888"].test, "modified3",
|
|
|
|
"navigating retains correct storage data");
|
|
|
|
ok(!storage["http://example.com"], "storage data was discarded");
|
|
|
|
|
|
|
|
// Check that loading a new URL discards data.
|
|
|
|
browser2.loadURI("about:mozilla");
|
|
|
|
yield promiseBrowserLoaded(browser2);
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser2);
|
2013-12-20 05:23:32 -08:00
|
|
|
|
|
|
|
let state = JSON.parse(ss.getTabState(tab2));
|
|
|
|
ok(!state.hasOwnProperty("storage"), "storage data was discarded");
|
|
|
|
|
2013-10-25 03:02:25 -07:00
|
|
|
// Clean up.
|
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
gBrowser.removeTab(tab2);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test ensures that purging domain data also purges data from the
|
|
|
|
* sessionStorage data collected for tabs.
|
|
|
|
*/
|
|
|
|
add_task(function purge_domain() {
|
2013-12-20 05:23:32 -08:00
|
|
|
let tab = gBrowser.addTab(URL);
|
2013-10-25 03:02:25 -07:00
|
|
|
let browser = tab.linkedBrowser;
|
2013-12-20 05:23:32 -08:00
|
|
|
yield promiseBrowserLoaded(browser);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
2013-12-20 05:23:32 -08:00
|
|
|
// Purge data for "mochi.test".
|
|
|
|
yield purgeDomainData(browser, "mochi.test");
|
2013-11-24 02:22:09 -08:00
|
|
|
|
|
|
|
// Flush to make sure chrome received all data.
|
2014-09-23 15:48:53 -07:00
|
|
|
TabState.flush(browser);
|
2013-10-25 03:02:25 -07:00
|
|
|
|
|
|
|
let {storage} = JSON.parse(ss.getTabState(tab));
|
|
|
|
ok(!storage["http://mochi.test:8888"],
|
|
|
|
"sessionStorage data for mochi.test has been purged");
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://example.com"].test, INNER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"sessionStorage data for example.com has been preserved");
|
|
|
|
|
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test ensures that collecting sessionStorage data respects the privacy
|
|
|
|
* levels as set by the user.
|
|
|
|
*/
|
|
|
|
add_task(function respect_privacy_level() {
|
2013-12-20 05:23:32 -08:00
|
|
|
let tab = gBrowser.addTab(URL + "&secure");
|
|
|
|
yield promiseBrowserLoaded(tab.linkedBrowser);
|
2013-10-25 03:02:25 -07:00
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
|
|
|
|
let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://mochi.test:8888"].test, OUTER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"http sessionStorage data has been saved");
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["https://example.com"].test, INNER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"https sessionStorage data has been saved");
|
|
|
|
|
|
|
|
// Disable saving data for encrypted sites.
|
|
|
|
Services.prefs.setIntPref("browser.sessionstore.privacy_level", 1);
|
|
|
|
|
2014-09-15 16:30:47 -07:00
|
|
|
tab = gBrowser.addTab(URL + "&secure");
|
2013-12-20 05:23:32 -08:00
|
|
|
yield promiseBrowserLoaded(tab.linkedBrowser);
|
2013-10-25 03:02:25 -07:00
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
|
2014-09-15 16:30:47 -07:00
|
|
|
[{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://mochi.test:8888"].test, OUTER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"http sessionStorage data has been saved");
|
|
|
|
ok(!storage["https://example.com"],
|
|
|
|
"https sessionStorage data has *not* been saved");
|
|
|
|
|
|
|
|
// Disable saving data for any site.
|
|
|
|
Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2);
|
|
|
|
|
|
|
|
// Check that duplicating a tab copies all private data.
|
2014-09-15 16:30:47 -07:00
|
|
|
tab = gBrowser.addTab(URL + "&secure");
|
2013-12-20 05:23:32 -08:00
|
|
|
yield promiseBrowserLoaded(tab.linkedBrowser);
|
2013-10-25 03:02:25 -07:00
|
|
|
let tab2 = gBrowser.duplicateTab(tab);
|
2013-12-20 05:23:32 -08:00
|
|
|
yield promiseTabRestored(tab2);
|
2013-10-25 03:02:25 -07:00
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
|
|
|
|
// With privacy_level=2 the |tab| shouldn't have any sessionStorage data.
|
2014-09-15 16:30:47 -07:00
|
|
|
[{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
|
2013-12-20 05:23:32 -08:00
|
|
|
ok(!storage, "sessionStorage data has *not* been saved");
|
2013-10-25 03:02:25 -07:00
|
|
|
|
|
|
|
// Restore the default privacy level and close the duplicated tab.
|
|
|
|
Services.prefs.clearUserPref("browser.sessionstore.privacy_level");
|
|
|
|
gBrowser.removeTab(tab2);
|
|
|
|
|
|
|
|
// With privacy_level=0 the duplicated |tab2| should persist all data.
|
2014-09-15 16:30:47 -07:00
|
|
|
[{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["http://mochi.test:8888"].test, OUTER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"http sessionStorage data has been saved");
|
2013-12-20 05:23:32 -08:00
|
|
|
is(storage["https://example.com"].test, INNER_VALUE,
|
2013-10-25 03:02:25 -07:00
|
|
|
"https sessionStorage data has been saved");
|
|
|
|
});
|
|
|
|
|
|
|
|
function waitForStorageEvent(browser) {
|
|
|
|
return promiseContentMessage(browser, "ss-test:MozStorageChanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
function modifySessionStorage(browser, data) {
|
|
|
|
browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data);
|
|
|
|
return waitForStorageEvent(browser);
|
|
|
|
}
|
|
|
|
|
2013-12-20 05:23:32 -08:00
|
|
|
function modifySessionStorage2(browser, data) {
|
|
|
|
browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage2", data);
|
|
|
|
return waitForStorageEvent(browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
function purgeDomainData(browser, domain) {
|
|
|
|
return sendMessage(browser, "ss-test:purgeDomainData", domain);
|
2013-10-25 03:02:25 -07:00
|
|
|
}
|