dom/browser-element/mochitest/browserElement_Auth.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* Any copyright is dedicated to the public domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Test that auth prompt works.
michael@0 5 "use strict";
michael@0 6
michael@0 7 SimpleTest.waitForExplicitFinish();
michael@0 8 browserElementTestHelpers.setEnabledPref(true);
michael@0 9 browserElementTestHelpers.addPermission();
michael@0 10
michael@0 11 function testFail(msg) {
michael@0 12 ok(false, JSON.stringify(msg));
michael@0 13 }
michael@0 14
michael@0 15 var iframe;
michael@0 16
michael@0 17 function runTest() {
michael@0 18 iframe = document.createElement('iframe');
michael@0 19 SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0 20 document.body.appendChild(iframe);
michael@0 21
michael@0 22 // Wait for the initial load to finish, then navigate the page, then start test
michael@0 23 // by loading SJS with http 401 response.
michael@0 24 iframe.addEventListener('mozbrowserloadend', function loadend() {
michael@0 25 iframe.removeEventListener('mozbrowserloadend', loadend);
michael@0 26 iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuthCancel);
michael@0 27 SimpleTest.executeSoon(function() {
michael@0 28 // Use absolute path because we need to specify host.
michael@0 29 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
michael@0 30 });
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 function testHttpAuthCancel(e) {
michael@0 35 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuthCancel);
michael@0 36 // Will cancel authentication, but prompt should not be shown again. Instead,
michael@0 37 // we will be led to fail message
michael@0 38 iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail);
michael@0 39 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
michael@0 40 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
michael@0 41 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail);
michael@0 42 is(e.detail, 'http auth failed', 'expected authentication to fail');
michael@0 43 iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuth);
michael@0 44 SimpleTest.executeSoon(function() {
michael@0 45 // Use absolute path because we need to specify host.
michael@0 46 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
michael@0 47 });
michael@0 48 });
michael@0 49
michael@0 50 is(e.detail.realm, 'http_realm', 'expected realm matches');
michael@0 51 is(e.detail.host, 'http://test', 'expected host matches');
michael@0 52 e.preventDefault();
michael@0 53
michael@0 54 SimpleTest.executeSoon(function() {
michael@0 55 e.detail.cancel();
michael@0 56 });
michael@0 57 }
michael@0 58
michael@0 59 function testHttpAuth(e) {
michael@0 60 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuth);
michael@0 61
michael@0 62 // Will authenticate with correct password, prompt should not be
michael@0 63 // called again.
michael@0 64 iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail);
michael@0 65 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
michael@0 66 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
michael@0 67 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail);
michael@0 68 is(e.detail, 'http auth success', 'expect authentication to succeed');
michael@0 69 SimpleTest.executeSoon(testAuthJarNoInterfere);
michael@0 70 });
michael@0 71
michael@0 72 is(e.detail.realm, 'http_realm', 'expected realm matches');
michael@0 73 is(e.detail.host, 'http://test', 'expected host matches');
michael@0 74 e.preventDefault();
michael@0 75
michael@0 76 SimpleTest.executeSoon(function() {
michael@0 77 e.detail.authenticate("httpuser", "httppass");
michael@0 78 });
michael@0 79 }
michael@0 80
michael@0 81 function testAuthJarNoInterfere(e) {
michael@0 82 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
michael@0 83 .getService(SpecialPowers.Ci.nsIHttpAuthManager);
michael@0 84 var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 85 .getService(SpecialPowers.Ci.nsIScriptSecurityManager);
michael@0 86 var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
michael@0 87 .getService(SpecialPowers.Ci.nsIIOService);
michael@0 88 var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null);
michael@0 89
michael@0 90 // Set a bunch of auth data that should not conflict with the correct auth data already
michael@0 91 // stored in the cache.
michael@0 92 var principal = secMan.getAppCodebasePrincipal(uri, 1, false);
michael@0 93 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
michael@0 94 'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
michael@0 95 '', 'httpuser', 'wrongpass', false, principal);
michael@0 96 principal = secMan.getAppCodebasePrincipal(uri, 1, true);
michael@0 97 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
michael@0 98 'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
michael@0 99 '', 'httpuser', 'wrongpass', false, principal);
michael@0 100 principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, false);
michael@0 101 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
michael@0 102 'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
michael@0 103 '', 'httpuser', 'wrongpass', false, principal);
michael@0 104
michael@0 105 // Will authenticate with correct password, prompt should not be
michael@0 106 // called again.
michael@0 107 iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail);
michael@0 108 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
michael@0 109 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
michael@0 110 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail);
michael@0 111 is(e.detail, 'http auth success', 'expected authentication success');
michael@0 112 SimpleTest.executeSoon(testAuthJarInterfere);
michael@0 113 });
michael@0 114
michael@0 115 // Once more with feeling. Ensure that our new auth data doesn't interfere with this mozbrowser's
michael@0 116 // auth data.
michael@0 117 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
michael@0 118 }
michael@0 119
michael@0 120 function testAuthJarInterfere(e) {
michael@0 121 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
michael@0 122 .getService(SpecialPowers.Ci.nsIHttpAuthManager);
michael@0 123 var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 124 .getService(SpecialPowers.Ci.nsIScriptSecurityManager);
michael@0 125 var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
michael@0 126 .getService(SpecialPowers.Ci.nsIIOService);
michael@0 127 var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null);
michael@0 128
michael@0 129 // Set some auth data that should overwrite the successful stored details.
michael@0 130 var principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, true);
michael@0 131 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm',
michael@0 132 'tests/dom/browser-element/mochitest/file_http_401_response.sjs',
michael@0 133 '', 'httpuser', 'wrongpass', false, principal);
michael@0 134
michael@0 135 // Will authenticate with correct password, prompt should not be
michael@0 136 // called again.
michael@0 137 var gotusernamepasswordrequired = false;
michael@0 138 function onUserNameAndPasswordRequired() {
michael@0 139 gotusernamepasswordrequired = true;
michael@0 140 }
michael@0 141 iframe.addEventListener("mozbrowserusernameandpasswordrequired",
michael@0 142 onUserNameAndPasswordRequired);
michael@0 143 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) {
michael@0 144 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange);
michael@0 145 iframe.removeEventListener("mozbrowserusernameandpasswordrequired",
michael@0 146 onUserNameAndPasswordRequired);
michael@0 147 ok(gotusernamepasswordrequired,
michael@0 148 "Should have dispatched mozbrowserusernameandpasswordrequired event");
michael@0 149 testFinish();
michael@0 150 });
michael@0 151
michael@0 152 // Once more with feeling. Ensure that our new auth data interferes with this mozbrowser's
michael@0 153 // auth data.
michael@0 154 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs';
michael@0 155 }
michael@0 156
michael@0 157 function testFinish() {
michael@0 158 // Clear login information stored in password manager.
michael@0 159 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
michael@0 160 .getService(SpecialPowers.Ci.nsIHttpAuthManager);
michael@0 161 authMgr.clearAll();
michael@0 162
michael@0 163 var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]
michael@0 164 .getService(SpecialPowers.Ci.nsILoginManager);
michael@0 165 pwmgr.removeAllLogins();
michael@0 166
michael@0 167 SimpleTest.finish();
michael@0 168 }
michael@0 169
michael@0 170 addEventListener('testready', runTest);

mercurial