dom/browser-element/mochitest/browserElement_Auth.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/browser-element/mochitest/browserElement_Auth.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +/* Any copyright is dedicated to the public domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Test that auth prompt works.
     1.8 +"use strict";
     1.9 +
    1.10 +SimpleTest.waitForExplicitFinish();
    1.11 +browserElementTestHelpers.setEnabledPref(true);
    1.12 +browserElementTestHelpers.addPermission();
    1.13 +
    1.14 +function testFail(msg) {
    1.15 +  ok(false, JSON.stringify(msg));
    1.16 +}
    1.17 +
    1.18 +var iframe;
    1.19 +
    1.20 +function runTest() {
    1.21 +  iframe = document.createElement('iframe');
    1.22 +  SpecialPowers.wrap(iframe).mozbrowser = true;
    1.23 +  document.body.appendChild(iframe);
    1.24 +
    1.25 +  // Wait for the initial load to finish, then navigate the page, then start test
    1.26 +  // by loading SJS with http 401 response.
    1.27 +  iframe.addEventListener('mozbrowserloadend', function loadend() {
    1.28 +    iframe.removeEventListener('mozbrowserloadend', loadend);
    1.29 +    iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuthCancel);
    1.30 +    SimpleTest.executeSoon(function() {
    1.31 +      // Use absolute path because we need to specify host.
    1.32 +      iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
    1.33 +    });
    1.34 +  });
    1.35 +}
    1.36 +
    1.37 +function testHttpAuthCancel(e) {
    1.38 +  iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuthCancel);
    1.39 +  // Will cancel authentication, but prompt should not be shown again. Instead,
    1.40 +  // we will be led to fail message
    1.41 +  iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail);
    1.42 +  iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
    1.43 +    iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
    1.44 +    iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail);
    1.45 +    is(e.detail, 'http auth failed', 'expected authentication to fail');
    1.46 +    iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuth);
    1.47 +    SimpleTest.executeSoon(function() {
    1.48 +      // Use absolute path because we need to specify host.
    1.49 +      iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
    1.50 +    });
    1.51 +  });
    1.52 +
    1.53 +  is(e.detail.realm, 'http_realm', 'expected realm matches');
    1.54 +  is(e.detail.host, 'http://test', 'expected host matches');
    1.55 +  e.preventDefault();
    1.56 +
    1.57 +  SimpleTest.executeSoon(function() {
    1.58 +    e.detail.cancel();
    1.59 +  });
    1.60 +}
    1.61 +
    1.62 +function testHttpAuth(e) {
    1.63 +  iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuth);
    1.64 +
    1.65 +  // Will authenticate with correct password, prompt should not be
    1.66 +  // called again.
    1.67 +  iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail);
    1.68 +  iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
    1.69 +    iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
    1.70 +    iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail);
    1.71 +    is(e.detail, 'http auth success', 'expect authentication to succeed');
    1.72 +    SimpleTest.executeSoon(testAuthJarNoInterfere);
    1.73 +  });
    1.74 +
    1.75 +  is(e.detail.realm, 'http_realm', 'expected realm matches');
    1.76 +  is(e.detail.host, 'http://test', 'expected host matches');
    1.77 +  e.preventDefault();
    1.78 +
    1.79 +  SimpleTest.executeSoon(function() {
    1.80 +    e.detail.authenticate("httpuser", "httppass");
    1.81 +  });
    1.82 +}
    1.83 +
    1.84 +function testAuthJarNoInterfere(e) {
    1.85 +  var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
    1.86 +    .getService(SpecialPowers.Ci.nsIHttpAuthManager);
    1.87 +  var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
    1.88 +               .getService(SpecialPowers.Ci.nsIScriptSecurityManager);
    1.89 +  var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
    1.90 +                  .getService(SpecialPowers.Ci.nsIIOService);
    1.91 +  var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null);
    1.92 +
    1.93 +  // Set a bunch of auth data that should not conflict with the correct auth data already
    1.94 +  // stored in the cache.
    1.95 +  var principal = secMan.getAppCodebasePrincipal(uri, 1, false);
    1.96 +  authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
    1.97 +                          'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
    1.98 +                          '', 'httpuser', 'wrongpass', false, principal);
    1.99 +  principal = secMan.getAppCodebasePrincipal(uri, 1, true);
   1.100 +  authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
   1.101 +                          'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
   1.102 +                          '', 'httpuser', 'wrongpass', false, principal);
   1.103 +  principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, false);
   1.104 +  authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
   1.105 +                          'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
   1.106 +                          '', 'httpuser', 'wrongpass', false, principal);
   1.107 +
   1.108 +  // Will authenticate with correct password, prompt should not be
   1.109 +  // called again.
   1.110 +  iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail);
   1.111 +  iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
   1.112 +    iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
   1.113 +    iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail);
   1.114 +    is(e.detail, 'http auth success', 'expected authentication success');
   1.115 +    SimpleTest.executeSoon(testAuthJarInterfere);
   1.116 +  });
   1.117 +
   1.118 +  // Once more with feeling. Ensure that our new auth data doesn't interfere with this mozbrowser's
   1.119 +  // auth data.
   1.120 +  iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
   1.121 +}
   1.122 +
   1.123 +function testAuthJarInterfere(e) {
   1.124 +  var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
   1.125 +    .getService(SpecialPowers.Ci.nsIHttpAuthManager);
   1.126 +  var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
   1.127 +               .getService(SpecialPowers.Ci.nsIScriptSecurityManager);
   1.128 +  var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
   1.129 +                  .getService(SpecialPowers.Ci.nsIIOService);
   1.130 +  var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null);
   1.131 +
   1.132 +  // Set some auth data that should overwrite the successful stored details.
   1.133 +  var principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, true);
   1.134 +  authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
   1.135 +                          'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
   1.136 +                          '', 'httpuser', 'wrongpass', false, principal);
   1.137 +
   1.138 +  // Will authenticate with correct password, prompt should not be
   1.139 +  // called again.
   1.140 +  var gotusernamepasswordrequired = false;
   1.141 +  function onUserNameAndPasswordRequired() {
   1.142 +      gotusernamepasswordrequired = true;
   1.143 +  }
   1.144 +  iframe.addEventListener("mozbrowserusernameandpasswordrequired",
   1.145 +                          onUserNameAndPasswordRequired);
   1.146 +  iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
   1.147 +    iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
   1.148 +    iframe.removeEventListener("mozbrowserusernameandpasswordrequired",
   1.149 +                               onUserNameAndPasswordRequired);
   1.150 +    ok(gotusernamepasswordrequired,
   1.151 +       "Should have dispatched mozbrowserusernameandpasswordrequired event");
   1.152 +    testFinish();
   1.153 +  });
   1.154 +
   1.155 +  // Once more with feeling. Ensure that our new auth data interferes with this mozbrowser's
   1.156 +  // auth data.
   1.157 +  iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
   1.158 +}
   1.159 +
   1.160 +function testFinish() {
   1.161 +  // Clear login information stored in password manager.
   1.162 +  var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
   1.163 +    .getService(SpecialPowers.Ci.nsIHttpAuthManager);
   1.164 +  authMgr.clearAll();
   1.165 +
   1.166 +  var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]
   1.167 +    .getService(SpecialPowers.Ci.nsILoginManager);
   1.168 +  pwmgr.removeAllLogins();
   1.169 +
   1.170 +  SimpleTest.finish();
   1.171 +}
   1.172 +
   1.173 +addEventListener('testready', runTest);

mercurial