browser/components/sessionstore/test/browser_467409-backslashplosion.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_467409-backslashplosion.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +// Test Summary:
    1.10 +// 1.  Open about:sessionrestore where formdata is a JS object, not a string
    1.11 +// 1a. Check that #sessionData on the page is readable after JSON.parse (skipped, checking formdata is sufficient)
    1.12 +// 1b. Check that there are no backslashes in the formdata
    1.13 +// 1c. Check that formdata doesn't require JSON.parse
    1.14 +//
    1.15 +// 2.  Use the current state (currently about:sessionrestore with data) and then open that in a new instance of about:sessionrestore
    1.16 +// 2a. Check that there are no backslashes in the formdata
    1.17 +// 2b. Check that formdata doesn't require JSON.parse
    1.18 +//
    1.19 +// 3.  [backwards compat] Use a stringified state as formdata when opening about:sessionrestore
    1.20 +// 3a. Make sure there are nodes in the tree on about:sessionrestore (skipped, checking formdata is sufficient)
    1.21 +// 3b. Check that there are no backslashes in the formdata
    1.22 +// 3c. Check that formdata doesn't require JSON.parse
    1.23 +
    1.24 +const CRASH_STATE = {windows: [{tabs: [{entries: [{url: "about:mozilla" }]}]}]};
    1.25 +const STATE = {entries: [createEntry(CRASH_STATE)]};
    1.26 +const STATE2 = {entries: [createEntry({windows: [{tabs: [STATE]}]})]};
    1.27 +const STATE3 = {entries: [createEntry(JSON.stringify(CRASH_STATE))]};
    1.28 +
    1.29 +function createEntry(sessionData) {
    1.30 +  return {
    1.31 +    url: "about:sessionrestore",
    1.32 +    formdata: {id: {sessionData: sessionData}}
    1.33 +  };
    1.34 +}
    1.35 +
    1.36 +add_task(function test_nested_about_sessionrestore() {
    1.37 +  // Prepare a blank tab.
    1.38 +  let tab = gBrowser.addTab("about:blank");
    1.39 +  let browser = tab.linkedBrowser;
    1.40 +  yield promiseBrowserLoaded(browser);
    1.41 +
    1.42 +  // test 1
    1.43 +  ss.setTabState(tab, JSON.stringify(STATE));
    1.44 +  yield promiseTabRestored(tab);
    1.45 +  checkState("test1", tab);
    1.46 +
    1.47 +  // test 2
    1.48 +  ss.setTabState(tab, JSON.stringify(STATE2));
    1.49 +  yield promiseTabRestored(tab);
    1.50 +  checkState("test2", tab);
    1.51 +
    1.52 +  // test 3
    1.53 +  ss.setTabState(tab, JSON.stringify(STATE3));
    1.54 +  yield promiseTabRestored(tab);
    1.55 +  checkState("test3", tab);
    1.56 +
    1.57 +  // Cleanup.
    1.58 +  gBrowser.removeTab(tab);
    1.59 +});
    1.60 +
    1.61 +function checkState(prefix, tab) {
    1.62 +  // Flush and query tab state.
    1.63 +  SyncHandlers.get(tab.linkedBrowser).flush();
    1.64 +  let {formdata} = JSON.parse(ss.getTabState(tab));
    1.65 +
    1.66 +  ok(formdata.id["sessionData"], prefix + ": we have form data for about:sessionrestore");
    1.67 +
    1.68 +  let sessionData_raw = JSON.stringify(formdata.id["sessionData"]);
    1.69 +  ok(!/\\/.test(sessionData_raw), prefix + ": #sessionData contains no backslashes");
    1.70 +  info(sessionData_raw);
    1.71 +
    1.72 +  let gotError = false;
    1.73 +  try {
    1.74 +    JSON.parse(formdata.id["sessionData"]);
    1.75 +  } catch (e) {
    1.76 +    info(prefix + ": got error: " + e);
    1.77 +    gotError = true;
    1.78 +  }
    1.79 +  ok(gotError, prefix + ": attempting to JSON.parse form data threw error");
    1.80 +}

mercurial