/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html

http://github.com/zpao/v8monkey · HTML · 268 lines · 204 code · 28 blank · 36 comment · 0 complexity · 3d0d2a3d0e3a9c1abb18ce71607948af MD5 · raw file

  1. <!-- ***** BEGIN LICENSE BLOCK *****
  2. - Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. -
  4. - The contents of this file are subject to the Mozilla Public License Version
  5. - 1.1 (the "License"); you may not use this file except in compliance with
  6. - the License. You may obtain a copy of the License at
  7. - http://www.mozilla.org/MPL/
  8. -
  9. - Software distributed under the License is distributed on an "AS IS" basis,
  10. - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. - for the specific language governing rights and limitations under the
  12. - License.
  13. -
  14. - The Original Code is Strict-Transport-Security.
  15. -
  16. - The Initial Developer of the Original Code is
  17. - Mozilla Foundation.
  18. - Portions created by the Initial Developer are Copyright (C) 2010
  19. - the Initial Developer. All Rights Reserved.
  20. -
  21. - Contributor(s):
  22. - Sid Stamm <sid@mozilla.com>
  23. -
  24. - Alternatively, the contents of this file may be used under the terms of
  25. - either the GNU General Public License Version 2 or later (the "GPL"), or
  26. - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. - in which case the provisions of the GPL or the LGPL are applicable instead
  28. - of those above. If you wish to allow use of your version of this file only
  29. - under the terms of either the GPL or the LGPL, and not to allow others to
  30. - use your version of this file under the terms of the MPL, indicate your
  31. - decision by deleting the provisions above and replace them with the notice
  32. - and other provisions required by the LGPL or the GPL. If you do not delete
  33. - the provisions above, a recipient may use your version of this file under
  34. - the terms of any one of the MPL, the GPL or the LGPL.
  35. -
  36. - ***** END LICENSE BLOCK ***** -->
  37. <!DOCTYPE HTML>
  38. <html>
  39. <head>
  40. <title>opens additional content that should be converted to https</title>
  41. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  42. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  43. <script class="testbody" type="text/javascript">
  44. SimpleTest.waitForExplicitFinish();
  45. const STSPATH = "/tests/security/ssl/stricttransportsecurity";
  46. const NUM_TEST_FRAMES = 4;
  47. var testframes = {
  48. 'samedom':
  49. {'url': "http://example.com" + STSPATH + "/verify.sjs",
  50. 'expected': {'plain': 'SECURE',
  51. 'subdom': 'SECURE',
  52. 'nosts': 'INSECURE'}},
  53. 'subdom':
  54. {'url': "http://test1.example.com" + STSPATH + "/verify.sjs",
  55. 'expected': {'plain': 'INSECURE',
  56. 'subdom': 'SECURE',
  57. 'nosts': 'INSECURE'}},
  58. 'otherdom':
  59. {'url': "http://example.org" + STSPATH + "/verify.sjs",
  60. 'expected': {'plain': 'INSECURE',
  61. 'subdom': 'INSECURE',
  62. 'nosts': 'INSECURE'}},
  63. 'alreadysecure':
  64. {'url': "https://test2.example.com" + STSPATH + "/verify.sjs",
  65. 'expected': {'plain': 'SECURE',
  66. 'subdom': 'SECURE',
  67. 'nosts': 'SECURE'}},
  68. };
  69. // This is how many sub-tests (testframes) in each round.
  70. // When the round begins, this will be initialized.
  71. var testsleftinround = 0;
  72. var currentround = "";
  73. var _PBSvc = null;
  74. var _PrefSvc = null;
  75. function _getPBService() {
  76. if (_PBSvc)
  77. return _PBSvc;
  78. // not all apps will have the private browsing service.
  79. try {
  80. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  81. _PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"]
  82. .getService(Components.interfaces.nsIPrivateBrowsingService);
  83. return _PBSvc;
  84. } catch (e) {}
  85. return null;
  86. }
  87. function _getPrefService() {
  88. if (_PrefSvc)
  89. return _PrefSvc;
  90. // not all apps will have the private browsing service.
  91. try {
  92. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  93. _PrefSvc = Components.classes["@mozilla.org/preferences-service;1"]
  94. .getService(Components.interfaces.nsIPrefService)
  95. .QueryInterface(Components.interfaces.nsIPrefBranch);
  96. return _PrefSvc;
  97. } catch (e) {}
  98. return null;
  99. }
  100. function startRound(round) {
  101. currentround = round;
  102. testsleftinround = NUM_TEST_FRAMES;
  103. dump("TESTS LEFT IN ROUND: " + testsleftinround + "\n");
  104. var frame = document.createElement("iframe");
  105. frame.setAttribute('id', 'ifr_bootstrap');
  106. frame.setAttribute('src', "https://example.com" + STSPATH +
  107. "/" + round + "_bootstrap.html");
  108. document.body.appendChild(frame);
  109. }
  110. function loadVerifyFrames(round) {
  111. for (var test in testframes) {
  112. var frame = document.createElement("iframe");
  113. frame.setAttribute('id', 'ifr_' + test);
  114. frame.setAttribute('src', testframes[test].url + '?id=' + test);
  115. document.body.appendChild(frame);
  116. }
  117. }
  118. /* Messages received are in this format:
  119. * (BOOTSTRAP|SECURE|INSECURE) testid
  120. * For example: "BOOTSTRAP subdom"
  121. * or: "INSECURE otherdom"
  122. */
  123. function onMessageReceived(event) {
  124. // otherwise, it's a test result
  125. var result = event.data.split(/\s+/);
  126. if (result.length != 2) {
  127. SimpleTest.ok(false, event.data);
  128. return;
  129. }
  130. if (result[0] === "BOOTSTRAP") {
  131. loadVerifyFrames(currentround);
  132. return;
  133. }
  134. // check if the result (SECURE/INSECURE) is expected for this round/test
  135. // combo
  136. dump_STSState();
  137. dump( "*** in ROUND " + currentround +
  138. ", test " + result[1] +
  139. " is " + result[0] + "\n");
  140. SimpleTest.is(result[0], testframes[result[1]].expected[currentround],
  141. "in ROUND " + currentround +
  142. ", test " + result[1]);
  143. testsleftinround--;
  144. // if this round is complete...
  145. if (testsleftinround < 1) {
  146. dump("DONE WITH ROUND " + currentround + "\n");
  147. // remove all the iframes in the document
  148. document.body.removeChild(document.getElementById('ifr_bootstrap'));
  149. for (var test in testframes)
  150. document.body.removeChild(document.getElementById('ifr_' + test));
  151. currentround = "";
  152. // And advance to the next test.
  153. // Defer this so it doesn't muck with the stack too much.
  154. SimpleTest.executeSoon(nextTest);
  155. }
  156. }
  157. function test_sts_before_private_mode() {
  158. dump_STSState();
  159. dump("*** not in private browsing mode\n");
  160. startRound('plain');
  161. }
  162. function test_sts_in_private_mode() {
  163. dump_STSState();
  164. dump("*** Entering private browsing mode\n");
  165. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  166. _getPrefService().setBoolPref("browser.privatebrowsing.keep_current_session",
  167. true);
  168. _getPBService().privateBrowsingEnabled = true;
  169. dump("*** ... done\n");
  170. dump_STSState();
  171. startRound('subdom');
  172. }
  173. function test_sts_after_exiting_private_mode() {
  174. dump_STSState();
  175. dump("*** Exiting private browsing mode\n");
  176. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  177. _getPBService().privateBrowsingEnabled = false;
  178. _getPrefService().clearUserPref("browser.privatebrowsing.keep_current_session");
  179. dump("*** ... done\n");
  180. dump_STSState();
  181. startRound('nosts');
  182. }
  183. function clean_up_sts_state() {
  184. // erase all signs that this test ran.
  185. dump("*** Cleaning up STS data.\n");
  186. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  187. const Cc = Components.classes;
  188. const Ci = Components.interfaces;
  189. var ios = Cc["@mozilla.org/network/io-service;1"]
  190. .getService(Ci.nsIIOService);
  191. var thehost = ios.newURI("http://example.com", null, null);
  192. var stss = Cc["@mozilla.org/stsservice;1"]
  193. .getService(Ci.nsIStrictTransportSecurityService);
  194. stss.removeStsState(thehost);
  195. dump_STSState();
  196. SimpleTest.executeSoon(nextTest);
  197. }
  198. function dump_STSState() {
  199. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  200. var stss = Components.classes["@mozilla.org/stsservice;1"]
  201. .getService(Components.interfaces.nsIStrictTransportSecurityService);
  202. dump("*** State of example.com: " + stss.isStsHost("example.com") + "\n");
  203. }
  204. // these are executed in the order presented.
  205. // 0. test that STS works before entering private browsing mode.
  206. // (load sts-bootstrapped "plain" tests)
  207. // ... clear any STS data ...
  208. // 1. test that STS works in private browsing mode
  209. // (load sts-bootstrapped "subdomain" tests)
  210. // 2. test that after exiting private browsing, STS data is forgotten
  211. // (verified with non-sts-bootstrapped pages)
  212. var tests = [];
  213. { // skip these tests if there's no private mode support
  214. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  215. if ("@mozilla.org/privatebrowsing;1" in Components.classes) {
  216. tests = [
  217. test_sts_before_private_mode,
  218. clean_up_sts_state,
  219. test_sts_in_private_mode,
  220. test_sts_after_exiting_private_mode,
  221. clean_up_sts_state,
  222. ];
  223. }
  224. }
  225. function nextTest() {
  226. if (tests.length)
  227. SimpleTest.executeSoon(tests.shift());
  228. else
  229. SimpleTest.executeSoon(SimpleTest.finish);
  230. }
  231. // listen for calls back from the sts-setting iframe and then
  232. // the verification frames.
  233. window.addEventListener("message", onMessageReceived, false);
  234. window.addEventListener('load', nextTest, false);
  235. </script>
  236. </head>
  237. <body>
  238. This test will load some iframes and do some tests.
  239. </body>
  240. </html>