browser/components/sessionstore/test/browser_formdata.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 /**
michael@0 7 * This test ensures that form data collection respects the privacy level as
michael@0 8 * set by the user.
michael@0 9 */
michael@0 10 add_task(function test_formdata() {
michael@0 11 const URL = "http://mochi.test:8888/browser/browser/components/" +
michael@0 12 "sessionstore/test/browser_formdata_sample.html";
michael@0 13
michael@0 14 const OUTER_VALUE = "browser_formdata_" + Math.random();
michael@0 15 const INNER_VALUE = "browser_formdata_" + Math.random();
michael@0 16
michael@0 17 // Creates a tab, loads a page with some form fields,
michael@0 18 // modifies their values and closes the tab.
michael@0 19 function createAndRemoveTab() {
michael@0 20 return Task.spawn(function () {
michael@0 21 // Create a new tab.
michael@0 22 let tab = gBrowser.addTab(URL);
michael@0 23 let browser = tab.linkedBrowser;
michael@0 24 yield promiseBrowserLoaded(browser);
michael@0 25
michael@0 26 // Modify form data.
michael@0 27 yield setInputValue(browser, {id: "txt", value: OUTER_VALUE});
michael@0 28 yield setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
michael@0 29
michael@0 30 // Remove the tab.
michael@0 31 gBrowser.removeTab(tab);
michael@0 32 });
michael@0 33 }
michael@0 34
michael@0 35 yield createAndRemoveTab();
michael@0 36 let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 37 is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
michael@0 38 is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
michael@0 39
michael@0 40 // Disable saving data for encrypted sites.
michael@0 41 Services.prefs.setIntPref("browser.sessionstore.privacy_level", 1);
michael@0 42
michael@0 43 yield createAndRemoveTab();
michael@0 44 let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 45 is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
michael@0 46 ok(!formdata.children, "inner value was *not* stored");
michael@0 47
michael@0 48 // Disable saving data for any site.
michael@0 49 Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2);
michael@0 50
michael@0 51 yield createAndRemoveTab();
michael@0 52 let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 53 ok(!formdata, "form data has *not* been stored");
michael@0 54
michael@0 55 // Restore the default privacy level.
michael@0 56 Services.prefs.clearUserPref("browser.sessionstore.privacy_level");
michael@0 57 });
michael@0 58
michael@0 59 /**
michael@0 60 * This test ensures that we maintain backwards compatibility with the form
michael@0 61 * data format used pre Fx 29.
michael@0 62 */
michael@0 63 add_task(function test_old_format() {
michael@0 64 const URL = "data:text/html;charset=utf-8,<input%20id=input>";
michael@0 65 const VALUE = "value-" + Math.random();
michael@0 66
michael@0 67 // Create a tab with an iframe containing an input field.
michael@0 68 let tab = gBrowser.addTab(URL);
michael@0 69 let browser = tab.linkedBrowser;
michael@0 70 yield promiseBrowserLoaded(browser);
michael@0 71
michael@0 72 // Check that the form value is restored.
michael@0 73 let state = {entries: [{url: URL, formdata: {id: {input: VALUE}}}]};
michael@0 74 ss.setTabState(tab, JSON.stringify(state));
michael@0 75 yield promiseTabRestored(tab);
michael@0 76 is((yield getInputValue(browser, "input")), VALUE, "form data restored");
michael@0 77
michael@0 78 // Cleanup.
michael@0 79 gBrowser.removeTab(tab);
michael@0 80 });
michael@0 81
michael@0 82 /**
michael@0 83 * This test ensures that we maintain backwards compatibility with the form
michael@0 84 * data form used pre Fx 29, esp. the .innerHTML property for editable docs.
michael@0 85 */
michael@0 86 add_task(function test_old_format_inner_html() {
michael@0 87 const URL = "data:text/html;charset=utf-8,<h1>mozilla</h1>" +
michael@0 88 "<script>document.designMode='on'</script>";
michael@0 89 const VALUE = "<h1>value-" + Math.random() + "</h1>";
michael@0 90
michael@0 91 // Create a tab with an iframe containing an input field.
michael@0 92 let tab = gBrowser.addTab(URL);
michael@0 93 let browser = tab.linkedBrowser;
michael@0 94 yield promiseBrowserLoaded(browser);
michael@0 95
michael@0 96 // Restore the tab state.
michael@0 97 let state = {entries: [{url: URL, innerHTML: VALUE}]};
michael@0 98 ss.setTabState(tab, JSON.stringify(state));
michael@0 99 yield promiseTabRestored(tab);
michael@0 100
michael@0 101 // Check that the innerHTML value was restored.
michael@0 102 let html = yield getInnerHTML(browser);
michael@0 103 is(html, VALUE, "editable document has been restored correctly");
michael@0 104
michael@0 105 // Cleanup.
michael@0 106 gBrowser.removeTab(tab);
michael@0 107 });
michael@0 108
michael@0 109 /**
michael@0 110 * This test ensures that a malicious website can't trick us into restoring
michael@0 111 * form data into a wrong website and that we always check the stored URL
michael@0 112 * before doing so.
michael@0 113 */
michael@0 114 add_task(function test_url_check() {
michael@0 115 const URL = "data:text/html;charset=utf-8,<input%20id=input>";
michael@0 116 const VALUE = "value-" + Math.random();
michael@0 117
michael@0 118 // Create a tab with an iframe containing an input field.
michael@0 119 let tab = gBrowser.addTab(URL);
michael@0 120 let browser = tab.linkedBrowser;
michael@0 121 yield promiseBrowserLoaded(browser);
michael@0 122
michael@0 123 // Restore a tab state with a given form data url.
michael@0 124 function restoreStateWithURL(url) {
michael@0 125 let state = {entries: [{url: URL}], formdata: {id: {input: VALUE}}};
michael@0 126
michael@0 127 if (url) {
michael@0 128 state.formdata.url = url;
michael@0 129 }
michael@0 130
michael@0 131 ss.setTabState(tab, JSON.stringify(state));
michael@0 132 return promiseTabRestored(tab).then(() => getInputValue(browser, "input"));
michael@0 133 }
michael@0 134
michael@0 135 // Check that the form value is restored with the correct URL.
michael@0 136 is((yield restoreStateWithURL(URL)), VALUE, "form data restored");
michael@0 137
michael@0 138 // Check that the form value is *not* restored with the wrong URL.
michael@0 139 is((yield restoreStateWithURL(URL + "?")), "", "form data not restored");
michael@0 140 is((yield restoreStateWithURL()), "", "form data not restored");
michael@0 141
michael@0 142 // Cleanup.
michael@0 143 gBrowser.removeTab(tab);
michael@0 144 });
michael@0 145
michael@0 146 /**
michael@0 147 * This test ensures that collecting form data works as expected when having
michael@0 148 * nested frame sets.
michael@0 149 */
michael@0 150 add_task(function test_nested() {
michael@0 151 const URL = "data:text/html;charset=utf-8," +
michael@0 152 "<iframe src='data:text/html;charset=utf-8," +
michael@0 153 "<input autofocus=true>'/>";
michael@0 154
michael@0 155 const FORM_DATA = {
michael@0 156 children: [{
michael@0 157 xpath: {"/xhtml:html/xhtml:body/xhtml:input": "M"},
michael@0 158 url: "data:text/html;charset=utf-8,<input%20autofocus=true>"
michael@0 159 }]
michael@0 160 };
michael@0 161
michael@0 162 // Create a tab with an iframe containing an input field.
michael@0 163 let tab = gBrowser.selectedTab = gBrowser.addTab(URL);
michael@0 164 let browser = tab.linkedBrowser;
michael@0 165 yield promiseBrowserLoaded(browser);
michael@0 166
michael@0 167 // Modify the input field's value.
michael@0 168 yield sendMessage(browser, "ss-test:sendKeyEvent", {key: "m", frame: 0});
michael@0 169
michael@0 170 // Remove the tab and check that we stored form data correctly.
michael@0 171 gBrowser.removeTab(tab);
michael@0 172 let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 173 is(JSON.stringify(formdata), JSON.stringify(FORM_DATA),
michael@0 174 "formdata for iframe stored correctly");
michael@0 175
michael@0 176 // Restore the closed tab.
michael@0 177 let tab = ss.undoCloseTab(window, 0);
michael@0 178 let browser = tab.linkedBrowser;
michael@0 179 yield promiseTabRestored(tab);
michael@0 180
michael@0 181 // Check that the input field has the right value.
michael@0 182 SyncHandlers.get(browser).flush();
michael@0 183 let {formdata} = JSON.parse(ss.getTabState(tab));
michael@0 184 is(JSON.stringify(formdata), JSON.stringify(FORM_DATA),
michael@0 185 "formdata for iframe restored correctly");
michael@0 186
michael@0 187 // Cleanup.
michael@0 188 gBrowser.removeTab(tab);
michael@0 189 });
michael@0 190
michael@0 191 /**
michael@0 192 * This test ensures that collecting form data for documents with
michael@0 193 * designMode=on works as expected.
michael@0 194 */
michael@0 195 add_task(function test_design_mode() {
michael@0 196 const URL = "data:text/html;charset=utf-8,<h1>mozilla</h1>" +
michael@0 197 "<script>document.designMode='on'</script>";
michael@0 198
michael@0 199 // Load a tab with an editable document.
michael@0 200 let tab = gBrowser.selectedTab = gBrowser.addTab(URL);
michael@0 201 let browser = tab.linkedBrowser;
michael@0 202 yield promiseBrowserLoaded(browser);
michael@0 203
michael@0 204 // Modify the document content.
michael@0 205 yield sendMessage(browser, "ss-test:sendKeyEvent", {key: "m"});
michael@0 206
michael@0 207 // Duplicate the modified tab.
michael@0 208 let tab2 = gBrowser.duplicateTab(tab);
michael@0 209 yield promiseTabRestored(tab2);
michael@0 210
michael@0 211 // Check that the innerHTML value was restored.
michael@0 212 let html = yield getInnerHTML(browser);
michael@0 213 let expected = "<h1>Mmozilla</h1><script>document.designMode='on'</script>";
michael@0 214 is(html, expected, "editable document has been restored correctly");
michael@0 215
michael@0 216 // Cleanup.
michael@0 217 gBrowser.removeTab(tab2);
michael@0 218 gBrowser.removeTab(tab);
michael@0 219 });
michael@0 220
michael@0 221 function getInputValue(browser, id) {
michael@0 222 return sendMessage(browser, "ss-test:getInputValue", {id: id});
michael@0 223 }
michael@0 224
michael@0 225 function setInputValue(browser, data) {
michael@0 226 return sendMessage(browser, "ss-test:setInputValue", data);
michael@0 227 }
michael@0 228
michael@0 229 function getInnerHTML(browser) {
michael@0 230 return sendMessage(browser, "ss-test:getInnerHTML", {selector: "body"});
michael@0 231 }

mercurial