toolkit/content/tests/browser/browser_save_resend_postdata.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var MockFilePicker = SpecialPowers.MockFilePicker;
michael@0 6 MockFilePicker.init(window);
michael@0 7
michael@0 8 /**
michael@0 9 * Test for bug 471962 <https://bugzilla.mozilla.org/show_bug.cgi?id=471962>:
michael@0 10 * When saving an inner frame as file only, the POST data of the outer page is
michael@0 11 * sent to the address of the inner page.
michael@0 12 *
michael@0 13 * Test for bug 485196 <https://bugzilla.mozilla.org/show_bug.cgi?id=485196>:
michael@0 14 * Web page generated by POST is retried as GET when Save Frame As used, and the
michael@0 15 * page is no longer in the cache.
michael@0 16 */
michael@0 17 function test() {
michael@0 18 waitForExplicitFinish();
michael@0 19
michael@0 20 gBrowser.selectedTab = gBrowser.addTab();
michael@0 21
michael@0 22 gBrowser.loadURI("http://mochi.test:8888/browser/toolkit/content/tests/browser/data/post_form_outer.sjs");
michael@0 23
michael@0 24 registerCleanupFunction(function () {
michael@0 25 gBrowser.removeCurrentTab();
michael@0 26 });
michael@0 27
michael@0 28 gBrowser.addEventListener("pageshow", function pageShown(event) {
michael@0 29 if (event.target.location == "about:blank")
michael@0 30 return;
michael@0 31 gBrowser.removeEventListener("pageshow", pageShown);
michael@0 32
michael@0 33 // Submit the form in the outer page, then wait for both the outer
michael@0 34 // document and the inner frame to be loaded again.
michael@0 35 gBrowser.addEventListener("DOMContentLoaded", handleOuterSubmit);
michael@0 36 gBrowser.contentDocument.getElementById("postForm").submit();
michael@0 37 });
michael@0 38
michael@0 39 var framesLoaded = 0;
michael@0 40 var innerFrame;
michael@0 41
michael@0 42 function handleOuterSubmit() {
michael@0 43 if (++framesLoaded < 2)
michael@0 44 return;
michael@0 45
michael@0 46 gBrowser.removeEventListener("DOMContentLoaded", handleOuterSubmit);
michael@0 47
michael@0 48 innerFrame = gBrowser.contentDocument.getElementById("innerFrame");
michael@0 49
michael@0 50 // Submit the form in the inner page.
michael@0 51 gBrowser.addEventListener("DOMContentLoaded", handleInnerSubmit);
michael@0 52 innerFrame.contentDocument.getElementById("postForm").submit();
michael@0 53 }
michael@0 54
michael@0 55 function handleInnerSubmit() {
michael@0 56 gBrowser.removeEventListener("DOMContentLoaded", handleInnerSubmit);
michael@0 57
michael@0 58 // Create the folder the page will be saved into.
michael@0 59 var destDir = createTemporarySaveDirectory();
michael@0 60 var file = destDir.clone();
michael@0 61 file.append("no_default_file_name");
michael@0 62 MockFilePicker.returnFiles = [file];
michael@0 63 MockFilePicker.showCallback = function(fp) {
michael@0 64 MockFilePicker.filterIndex = 1; // kSaveAsType_URL
michael@0 65 };
michael@0 66
michael@0 67 mockTransferCallback = onTransferComplete;
michael@0 68 mockTransferRegisterer.register();
michael@0 69
michael@0 70 registerCleanupFunction(function () {
michael@0 71 mockTransferRegisterer.unregister();
michael@0 72 MockFilePicker.cleanup();
michael@0 73 destDir.remove(true);
michael@0 74 });
michael@0 75
michael@0 76 var docToSave = innerFrame.contentDocument;
michael@0 77 // We call internalSave instead of saveDocument to bypass the history
michael@0 78 // cache.
michael@0 79 internalSave(docToSave.location.href, docToSave, null, null,
michael@0 80 docToSave.contentType, false, null, null,
michael@0 81 docToSave.referrer ? makeURI(docToSave.referrer) : null,
michael@0 82 docToSave, false, null);
michael@0 83 }
michael@0 84
michael@0 85 function onTransferComplete(downloadSuccess) {
michael@0 86 ok(downloadSuccess, "The inner frame should have been downloaded successfully");
michael@0 87
michael@0 88 // Read the entire saved file.
michael@0 89 var file = MockFilePicker.returnFiles[0];
michael@0 90 var fileContents = readShortFile(file);
michael@0 91
michael@0 92 // Check if outer POST data is found (bug 471962).
michael@0 93 is(fileContents.indexOf("inputfield=outer"), -1,
michael@0 94 "The saved inner frame does not contain outer POST data");
michael@0 95
michael@0 96 // Check if inner POST data is found (bug 485196).
michael@0 97 isnot(fileContents.indexOf("inputfield=inner"), -1,
michael@0 98 "The saved inner frame was generated using the correct POST data");
michael@0 99
michael@0 100 finish();
michael@0 101 }
michael@0 102 }
michael@0 103
michael@0 104 Cc["@mozilla.org/moz/jssubscript-loader;1"]
michael@0 105 .getService(Ci.mozIJSSubScriptLoader)
michael@0 106 .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
michael@0 107 this);
michael@0 108
michael@0 109 function createTemporarySaveDirectory() {
michael@0 110 var saveDir = Cc["@mozilla.org/file/directory_service;1"]
michael@0 111 .getService(Ci.nsIProperties)
michael@0 112 .get("TmpD", Ci.nsIFile);
michael@0 113 saveDir.append("testsavedir");
michael@0 114 if (!saveDir.exists())
michael@0 115 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
michael@0 116 return saveDir;
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Reads the contents of the provided short file (up to 1 MiB).
michael@0 121 *
michael@0 122 * @param aFile
michael@0 123 * nsIFile object pointing to the file to be read.
michael@0 124 *
michael@0 125 * @return
michael@0 126 * String containing the raw octets read from the file.
michael@0 127 */
michael@0 128 function readShortFile(aFile) {
michael@0 129 var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 130 .createInstance(Ci.nsIFileInputStream);
michael@0 131 inputStream.init(aFile, -1, 0, 0);
michael@0 132 try {
michael@0 133 var scrInputStream = Cc["@mozilla.org/scriptableinputstream;1"]
michael@0 134 .createInstance(Ci.nsIScriptableInputStream);
michael@0 135 scrInputStream.init(inputStream);
michael@0 136 try {
michael@0 137 // Assume that the file is much shorter than 1 MiB.
michael@0 138 return scrInputStream.read(1048576);
michael@0 139 }
michael@0 140 finally {
michael@0 141 // Close the scriptable stream after reading, even if the operation
michael@0 142 // failed.
michael@0 143 scrInputStream.close();
michael@0 144 }
michael@0 145 }
michael@0 146 finally {
michael@0 147 // Close the stream after reading, if it is still open, even if the read
michael@0 148 // operation failed.
michael@0 149 inputStream.close();
michael@0 150 }
michael@0 151 }

mercurial