1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/manager/ssl/tests/mochitest/mixedcontent/mixedContentTest.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,227 @@ 1.4 +/** 1.5 + * Helper script for mixed content testing. It opens a new top-level window 1.6 + * from a secure origin and '?runtest' query. That tells us to run the test 1.7 + * body, function runTest(). Then we wait for call of finish(). On its first 1.8 + * call it loads helper page 'backward.html' that immediately navigates 1.9 + * back to the test secure test. This checks the bfcache. We got second call 1.10 + * to onload and this time we call afterNavigationTest() function to let the 1.11 + * test check security state after re-navigation back. Then we again wait for 1.12 + * finish() call, that this time finishes completelly the test. 1.13 + */ 1.14 + 1.15 +// Tells the framework if to load the test in an insecure page (http://) 1.16 +var loadAsInsecure = false; 1.17 +// Set true to bypass the navigation forward/back test 1.18 +var bypassNavigationTest = false; 1.19 +// Set true to do forward/back navigation over an http:// page, test state leaks 1.20 +var navigateToInsecure = false; 1.21 +// Open the test in two separate windows, test requests sharing among windows 1.22 +var openTwoWindows = false; 1.23 +// Override the name of the test page to load, useful e.g. to prevent load 1.24 +// of images or other content before the test starts; this is actually 1.25 +// a 'redirect' to a different test page. 1.26 +var testPage = ""; 1.27 +// Assign a function to this variable to have a clean up at the end 1.28 +var testCleanUp = null; 1.29 +// Contains mixed active content that needs to load to run the test 1.30 +var hasMixedActiveContent = false; 1.31 + 1.32 + 1.33 +// Internal variables 1.34 +var _windowCount = 0; 1.35 + 1.36 +window.onload = function onLoad() 1.37 +{ 1.38 + if (location.search == "?runtest") 1.39 + { 1.40 + try 1.41 + { 1.42 + if (history.length == 1) 1.43 + runTest(); 1.44 + else 1.45 + afterNavigationTest(); 1.46 + } 1.47 + catch (ex) 1.48 + { 1.49 + ok(false, "Exception thrown during test: " + ex); 1.50 + finish(); 1.51 + } 1.52 + } 1.53 + else 1.54 + { 1.55 + window.addEventListener("message", onMessageReceived, false); 1.56 + 1.57 + var secureTestLocation; 1.58 + if (loadAsInsecure) 1.59 + secureTestLocation = "http://example.com"; 1.60 + else 1.61 + secureTestLocation = "https://example.com"; 1.62 + secureTestLocation += location.pathname 1.63 + if (testPage != "") 1.64 + { 1.65 + array = secureTestLocation.split("/"); 1.66 + array.pop(); 1.67 + array.push(testPage); 1.68 + secureTestLocation = array.join("/"); 1.69 + } 1.70 + secureTestLocation += "?runtest"; 1.71 + 1.72 + if (hasMixedActiveContent) 1.73 + { 1.74 + SpecialPowers.pushPrefEnv( 1.75 + {"set": [["security.mixed_content.block_active_content", false]]}, 1.76 + null); 1.77 + } 1.78 + if (openTwoWindows) 1.79 + { 1.80 + _windowCount = 2; 1.81 + window.open(secureTestLocation, "_new1", ""); 1.82 + window.open(secureTestLocation, "_new2", ""); 1.83 + } 1.84 + else 1.85 + { 1.86 + _windowCount = 1; 1.87 + window.open(secureTestLocation); 1.88 + } 1.89 + } 1.90 +} 1.91 + 1.92 +function onMessageReceived(event) 1.93 +{ 1.94 + switch (event.data) 1.95 + { 1.96 + // Indication of all test parts finish (from any of the frames) 1.97 + case "done": 1.98 + if (--_windowCount == 0) 1.99 + { 1.100 + if (testCleanUp) 1.101 + testCleanUp(); 1.102 + if (hasMixedActiveContent) { 1.103 + SpecialPowers.popPrefEnv(null); 1.104 + } 1.105 + 1.106 + SimpleTest.finish(); 1.107 + } 1.108 + break; 1.109 + 1.110 + // Any other message indicates error or succes message of a test 1.111 + default: 1.112 + var failureRegExp = new RegExp("^FAILURE"); 1.113 + var todoRegExp = new RegExp("^TODO"); 1.114 + if (event.data.match(todoRegExp)) 1.115 + SimpleTest.todo(false, event.data); 1.116 + else 1.117 + SimpleTest.ok(!event.data.match(failureRegExp), event.data); 1.118 + break; 1.119 + } 1.120 +} 1.121 + 1.122 +function postMsg(message) 1.123 +{ 1.124 + opener.postMessage(message, "http://mochi.test:8888"); 1.125 +} 1.126 + 1.127 +function finish() 1.128 +{ 1.129 + if (history.length == 1 && !bypassNavigationTest) 1.130 + { 1.131 + window.setTimeout(function() 1.132 + { 1.133 + window.location.assign(navigateToInsecure ? 1.134 + "http://example.com/tests/security/manager/ssl/tests/mochitest/mixedcontent/backward.html" : 1.135 + "https://example.com/tests/security/manager/ssl/tests/mochitest/mixedcontent/backward.html"); 1.136 + }, 0); 1.137 + } 1.138 + else 1.139 + { 1.140 + postMsg("done"); 1.141 + window.close(); 1.142 + } 1.143 +} 1.144 + 1.145 +function ok(a, message) 1.146 +{ 1.147 + if (!a) 1.148 + postMsg("FAILURE: " + message); 1.149 + else 1.150 + postMsg(message); 1.151 +} 1.152 + 1.153 +function is(a, b, message) 1.154 +{ 1.155 + if (a != b) 1.156 + postMsg("FAILURE: " + message + ", expected "+b+" got "+a); 1.157 + else 1.158 + postMsg(message + ", expected "+b+" got "+a); 1.159 +} 1.160 + 1.161 +function todo(a, message) 1.162 +{ 1.163 + if (a) 1.164 + postMsg("FAILURE: TODO works? " + message); 1.165 + else 1.166 + postMsg("TODO: " + message); 1.167 +} 1.168 + 1.169 +function isSecurityState(expectedState, message, test) 1.170 +{ 1.171 + if (!test) 1.172 + test = ok; 1.173 + 1.174 + // Quit nasty but working :) 1.175 + var ui = SpecialPowers.wrap(window) 1.176 + .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor) 1.177 + .getInterface(SpecialPowers.Ci.nsIWebNavigation) 1.178 + .QueryInterface(SpecialPowers.Ci.nsIDocShell) 1.179 + .securityUI; 1.180 + 1.181 + var isInsecure = !ui || 1.182 + (ui.state & SpecialPowers.Ci.nsIWebProgressListener.STATE_IS_INSECURE); 1.183 + var isBroken = ui && 1.184 + (ui.state & SpecialPowers.Ci.nsIWebProgressListener.STATE_IS_BROKEN); 1.185 + var isEV = ui && 1.186 + (ui.state & SpecialPowers.Ci.nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL); 1.187 + 1.188 + var gotState; 1.189 + if (isInsecure) 1.190 + gotState = "insecure"; 1.191 + else if (isBroken) 1.192 + gotState = "broken"; 1.193 + else if (isEV) 1.194 + gotState = "EV"; 1.195 + else 1.196 + gotState = "secure"; 1.197 + 1.198 + test(gotState == expectedState, (message || "") + ", " + "expected " + expectedState + " got " + gotState); 1.199 + 1.200 + switch (expectedState) 1.201 + { 1.202 + case "insecure": 1.203 + test(isInsecure && !isBroken && !isEV, "for 'insecure' excpected flags [1,0,0], " + (message || "")); 1.204 + break; 1.205 + case "broken": 1.206 + test(ui && !isInsecure && isBroken && !isEV, "for 'broken' expected flags [0,1,0], " + (message || "")); 1.207 + break; 1.208 + case "secure": 1.209 + test(ui && !isInsecure && !isBroken && !isEV, "for 'secure' expected flags [0,0,0], " + (message || "")); 1.210 + break; 1.211 + case "EV": 1.212 + test(ui && !isInsecure && !isBroken && isEV, "for 'EV' expected flags [0,0,1], " + (message || "")); 1.213 + break; 1.214 + default: 1.215 + throw "Invalid isSecurityState state"; 1.216 + } 1.217 +} 1.218 + 1.219 +function waitForSecurityState(expectedState, callback) 1.220 +{ 1.221 + var roundsLeft = 200; // Wait for 20 seconds (=200*100ms) 1.222 + var interval = 1.223 + window.setInterval(function() { 1.224 + isSecurityState(expectedState, "", function(isok) {if (isok) {roundsLeft = 0;}}); 1.225 + if (!roundsLeft--) { 1.226 + window.clearInterval(interval); 1.227 + callback(); 1.228 + } 1.229 + }, 100); 1.230 +}