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