security/manager/ssl/tests/mochitest/mixedcontent/mixedContentTest.js

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

michael@0 1 /**
michael@0 2 * Helper script for mixed content testing. It opens a new top-level window
michael@0 3 * from a secure origin and '?runtest' query. That tells us to run the test
michael@0 4 * body, function runTest(). Then we wait for call of finish(). On its first
michael@0 5 * call it loads helper page 'backward.html' that immediately navigates
michael@0 6 * back to the test secure test. This checks the bfcache. We got second call
michael@0 7 * to onload and this time we call afterNavigationTest() function to let the
michael@0 8 * test check security state after re-navigation back. Then we again wait for
michael@0 9 * finish() call, that this time finishes completelly the test.
michael@0 10 */
michael@0 11
michael@0 12 // Tells the framework if to load the test in an insecure page (http://)
michael@0 13 var loadAsInsecure = false;
michael@0 14 // Set true to bypass the navigation forward/back test
michael@0 15 var bypassNavigationTest = false;
michael@0 16 // Set true to do forward/back navigation over an http:// page, test state leaks
michael@0 17 var navigateToInsecure = false;
michael@0 18 // Open the test in two separate windows, test requests sharing among windows
michael@0 19 var openTwoWindows = false;
michael@0 20 // Override the name of the test page to load, useful e.g. to prevent load
michael@0 21 // of images or other content before the test starts; this is actually
michael@0 22 // a 'redirect' to a different test page.
michael@0 23 var testPage = "";
michael@0 24 // Assign a function to this variable to have a clean up at the end
michael@0 25 var testCleanUp = null;
michael@0 26 // Contains mixed active content that needs to load to run the test
michael@0 27 var hasMixedActiveContent = false;
michael@0 28
michael@0 29
michael@0 30 // Internal variables
michael@0 31 var _windowCount = 0;
michael@0 32
michael@0 33 window.onload = function onLoad()
michael@0 34 {
michael@0 35 if (location.search == "?runtest")
michael@0 36 {
michael@0 37 try
michael@0 38 {
michael@0 39 if (history.length == 1)
michael@0 40 runTest();
michael@0 41 else
michael@0 42 afterNavigationTest();
michael@0 43 }
michael@0 44 catch (ex)
michael@0 45 {
michael@0 46 ok(false, "Exception thrown during test: " + ex);
michael@0 47 finish();
michael@0 48 }
michael@0 49 }
michael@0 50 else
michael@0 51 {
michael@0 52 window.addEventListener("message", onMessageReceived, false);
michael@0 53
michael@0 54 var secureTestLocation;
michael@0 55 if (loadAsInsecure)
michael@0 56 secureTestLocation = "http://example.com";
michael@0 57 else
michael@0 58 secureTestLocation = "https://example.com";
michael@0 59 secureTestLocation += location.pathname
michael@0 60 if (testPage != "")
michael@0 61 {
michael@0 62 array = secureTestLocation.split("/");
michael@0 63 array.pop();
michael@0 64 array.push(testPage);
michael@0 65 secureTestLocation = array.join("/");
michael@0 66 }
michael@0 67 secureTestLocation += "?runtest";
michael@0 68
michael@0 69 if (hasMixedActiveContent)
michael@0 70 {
michael@0 71 SpecialPowers.pushPrefEnv(
michael@0 72 {"set": [["security.mixed_content.block_active_content", false]]},
michael@0 73 null);
michael@0 74 }
michael@0 75 if (openTwoWindows)
michael@0 76 {
michael@0 77 _windowCount = 2;
michael@0 78 window.open(secureTestLocation, "_new1", "");
michael@0 79 window.open(secureTestLocation, "_new2", "");
michael@0 80 }
michael@0 81 else
michael@0 82 {
michael@0 83 _windowCount = 1;
michael@0 84 window.open(secureTestLocation);
michael@0 85 }
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 function onMessageReceived(event)
michael@0 90 {
michael@0 91 switch (event.data)
michael@0 92 {
michael@0 93 // Indication of all test parts finish (from any of the frames)
michael@0 94 case "done":
michael@0 95 if (--_windowCount == 0)
michael@0 96 {
michael@0 97 if (testCleanUp)
michael@0 98 testCleanUp();
michael@0 99 if (hasMixedActiveContent) {
michael@0 100 SpecialPowers.popPrefEnv(null);
michael@0 101 }
michael@0 102
michael@0 103 SimpleTest.finish();
michael@0 104 }
michael@0 105 break;
michael@0 106
michael@0 107 // Any other message indicates error or succes message of a test
michael@0 108 default:
michael@0 109 var failureRegExp = new RegExp("^FAILURE");
michael@0 110 var todoRegExp = new RegExp("^TODO");
michael@0 111 if (event.data.match(todoRegExp))
michael@0 112 SimpleTest.todo(false, event.data);
michael@0 113 else
michael@0 114 SimpleTest.ok(!event.data.match(failureRegExp), event.data);
michael@0 115 break;
michael@0 116 }
michael@0 117 }
michael@0 118
michael@0 119 function postMsg(message)
michael@0 120 {
michael@0 121 opener.postMessage(message, "http://mochi.test:8888");
michael@0 122 }
michael@0 123
michael@0 124 function finish()
michael@0 125 {
michael@0 126 if (history.length == 1 && !bypassNavigationTest)
michael@0 127 {
michael@0 128 window.setTimeout(function()
michael@0 129 {
michael@0 130 window.location.assign(navigateToInsecure ?
michael@0 131 "http://example.com/tests/security/manager/ssl/tests/mochitest/mixedcontent/backward.html" :
michael@0 132 "https://example.com/tests/security/manager/ssl/tests/mochitest/mixedcontent/backward.html");
michael@0 133 }, 0);
michael@0 134 }
michael@0 135 else
michael@0 136 {
michael@0 137 postMsg("done");
michael@0 138 window.close();
michael@0 139 }
michael@0 140 }
michael@0 141
michael@0 142 function ok(a, message)
michael@0 143 {
michael@0 144 if (!a)
michael@0 145 postMsg("FAILURE: " + message);
michael@0 146 else
michael@0 147 postMsg(message);
michael@0 148 }
michael@0 149
michael@0 150 function is(a, b, message)
michael@0 151 {
michael@0 152 if (a != b)
michael@0 153 postMsg("FAILURE: " + message + ", expected "+b+" got "+a);
michael@0 154 else
michael@0 155 postMsg(message + ", expected "+b+" got "+a);
michael@0 156 }
michael@0 157
michael@0 158 function todo(a, message)
michael@0 159 {
michael@0 160 if (a)
michael@0 161 postMsg("FAILURE: TODO works? " + message);
michael@0 162 else
michael@0 163 postMsg("TODO: " + message);
michael@0 164 }
michael@0 165
michael@0 166 function isSecurityState(expectedState, message, test)
michael@0 167 {
michael@0 168 if (!test)
michael@0 169 test = ok;
michael@0 170
michael@0 171 // Quit nasty but working :)
michael@0 172 var ui = SpecialPowers.wrap(window)
michael@0 173 .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
michael@0 174 .getInterface(SpecialPowers.Ci.nsIWebNavigation)
michael@0 175 .QueryInterface(SpecialPowers.Ci.nsIDocShell)
michael@0 176 .securityUI;
michael@0 177
michael@0 178 var isInsecure = !ui ||
michael@0 179 (ui.state & SpecialPowers.Ci.nsIWebProgressListener.STATE_IS_INSECURE);
michael@0 180 var isBroken = ui &&
michael@0 181 (ui.state & SpecialPowers.Ci.nsIWebProgressListener.STATE_IS_BROKEN);
michael@0 182 var isEV = ui &&
michael@0 183 (ui.state & SpecialPowers.Ci.nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL);
michael@0 184
michael@0 185 var gotState;
michael@0 186 if (isInsecure)
michael@0 187 gotState = "insecure";
michael@0 188 else if (isBroken)
michael@0 189 gotState = "broken";
michael@0 190 else if (isEV)
michael@0 191 gotState = "EV";
michael@0 192 else
michael@0 193 gotState = "secure";
michael@0 194
michael@0 195 test(gotState == expectedState, (message || "") + ", " + "expected " + expectedState + " got " + gotState);
michael@0 196
michael@0 197 switch (expectedState)
michael@0 198 {
michael@0 199 case "insecure":
michael@0 200 test(isInsecure && !isBroken && !isEV, "for 'insecure' excpected flags [1,0,0], " + (message || ""));
michael@0 201 break;
michael@0 202 case "broken":
michael@0 203 test(ui && !isInsecure && isBroken && !isEV, "for 'broken' expected flags [0,1,0], " + (message || ""));
michael@0 204 break;
michael@0 205 case "secure":
michael@0 206 test(ui && !isInsecure && !isBroken && !isEV, "for 'secure' expected flags [0,0,0], " + (message || ""));
michael@0 207 break;
michael@0 208 case "EV":
michael@0 209 test(ui && !isInsecure && !isBroken && isEV, "for 'EV' expected flags [0,0,1], " + (message || ""));
michael@0 210 break;
michael@0 211 default:
michael@0 212 throw "Invalid isSecurityState state";
michael@0 213 }
michael@0 214 }
michael@0 215
michael@0 216 function waitForSecurityState(expectedState, callback)
michael@0 217 {
michael@0 218 var roundsLeft = 200; // Wait for 20 seconds (=200*100ms)
michael@0 219 var interval =
michael@0 220 window.setInterval(function() {
michael@0 221 isSecurityState(expectedState, "", function(isok) {if (isok) {roundsLeft = 0;}});
michael@0 222 if (!roundsLeft--) {
michael@0 223 window.clearInterval(interval);
michael@0 224 callback();
michael@0 225 }
michael@0 226 }, 100);
michael@0 227 }

mercurial