michael@0: /* Any copyright is dedicated to the public domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test that auth prompt works. michael@0: "use strict"; michael@0: michael@0: SimpleTest.waitForExplicitFinish(); michael@0: browserElementTestHelpers.setEnabledPref(true); michael@0: browserElementTestHelpers.addPermission(); michael@0: michael@0: function testFail(msg) { michael@0: ok(false, JSON.stringify(msg)); michael@0: } michael@0: michael@0: var iframe; michael@0: michael@0: function runTest() { michael@0: iframe = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe).mozbrowser = true; michael@0: document.body.appendChild(iframe); michael@0: michael@0: // Wait for the initial load to finish, then navigate the page, then start test michael@0: // by loading SJS with http 401 response. michael@0: iframe.addEventListener('mozbrowserloadend', function loadend() { michael@0: iframe.removeEventListener('mozbrowserloadend', loadend); michael@0: iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuthCancel); michael@0: SimpleTest.executeSoon(function() { michael@0: // Use absolute path because we need to specify host. michael@0: iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function testHttpAuthCancel(e) { michael@0: iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuthCancel); michael@0: // Will cancel authentication, but prompt should not be shown again. Instead, michael@0: // we will be led to fail message michael@0: iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail); michael@0: iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { michael@0: iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); michael@0: iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail); michael@0: is(e.detail, 'http auth failed', 'expected authentication to fail'); michael@0: iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuth); michael@0: SimpleTest.executeSoon(function() { michael@0: // Use absolute path because we need to specify host. michael@0: iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; michael@0: }); michael@0: }); michael@0: michael@0: is(e.detail.realm, 'http_realm', 'expected realm matches'); michael@0: is(e.detail.host, 'http://test', 'expected host matches'); michael@0: e.preventDefault(); michael@0: michael@0: SimpleTest.executeSoon(function() { michael@0: e.detail.cancel(); michael@0: }); michael@0: } michael@0: michael@0: function testHttpAuth(e) { michael@0: iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuth); michael@0: michael@0: // Will authenticate with correct password, prompt should not be michael@0: // called again. michael@0: iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail); michael@0: iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { michael@0: iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); michael@0: iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail); michael@0: is(e.detail, 'http auth success', 'expect authentication to succeed'); michael@0: SimpleTest.executeSoon(testAuthJarNoInterfere); michael@0: }); michael@0: michael@0: is(e.detail.realm, 'http_realm', 'expected realm matches'); michael@0: is(e.detail.host, 'http://test', 'expected host matches'); michael@0: e.preventDefault(); michael@0: michael@0: SimpleTest.executeSoon(function() { michael@0: e.detail.authenticate("httpuser", "httppass"); michael@0: }); michael@0: } michael@0: michael@0: function testAuthJarNoInterfere(e) { michael@0: var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] michael@0: .getService(SpecialPowers.Ci.nsIHttpAuthManager); michael@0: var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(SpecialPowers.Ci.nsIScriptSecurityManager); michael@0: var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIIOService); michael@0: var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null); michael@0: michael@0: // Set a bunch of auth data that should not conflict with the correct auth data already michael@0: // stored in the cache. michael@0: var principal = secMan.getAppCodebasePrincipal(uri, 1, false); michael@0: authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', michael@0: 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', michael@0: '', 'httpuser', 'wrongpass', false, principal); michael@0: principal = secMan.getAppCodebasePrincipal(uri, 1, true); michael@0: authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', michael@0: 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', michael@0: '', 'httpuser', 'wrongpass', false, principal); michael@0: principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, false); michael@0: authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', michael@0: 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', michael@0: '', 'httpuser', 'wrongpass', false, principal); michael@0: michael@0: // Will authenticate with correct password, prompt should not be michael@0: // called again. michael@0: iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail); michael@0: iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { michael@0: iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); michael@0: iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail); michael@0: is(e.detail, 'http auth success', 'expected authentication success'); michael@0: SimpleTest.executeSoon(testAuthJarInterfere); michael@0: }); michael@0: michael@0: // Once more with feeling. Ensure that our new auth data doesn't interfere with this mozbrowser's michael@0: // auth data. michael@0: iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; michael@0: } michael@0: michael@0: function testAuthJarInterfere(e) { michael@0: var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] michael@0: .getService(SpecialPowers.Ci.nsIHttpAuthManager); michael@0: var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(SpecialPowers.Ci.nsIScriptSecurityManager); michael@0: var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIIOService); michael@0: var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null); michael@0: michael@0: // Set some auth data that should overwrite the successful stored details. michael@0: var principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, true); michael@0: authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', michael@0: 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', michael@0: '', 'httpuser', 'wrongpass', false, principal); michael@0: michael@0: // Will authenticate with correct password, prompt should not be michael@0: // called again. michael@0: var gotusernamepasswordrequired = false; michael@0: function onUserNameAndPasswordRequired() { michael@0: gotusernamepasswordrequired = true; michael@0: } michael@0: iframe.addEventListener("mozbrowserusernameandpasswordrequired", michael@0: onUserNameAndPasswordRequired); michael@0: iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { michael@0: iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); michael@0: iframe.removeEventListener("mozbrowserusernameandpasswordrequired", michael@0: onUserNameAndPasswordRequired); michael@0: ok(gotusernamepasswordrequired, michael@0: "Should have dispatched mozbrowserusernameandpasswordrequired event"); michael@0: testFinish(); michael@0: }); michael@0: michael@0: // Once more with feeling. Ensure that our new auth data interferes with this mozbrowser's michael@0: // auth data. michael@0: iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; michael@0: } michael@0: michael@0: function testFinish() { michael@0: // Clear login information stored in password manager. michael@0: var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] michael@0: .getService(SpecialPowers.Ci.nsIHttpAuthManager); michael@0: authMgr.clearAll(); michael@0: michael@0: var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"] michael@0: .getService(SpecialPowers.Ci.nsILoginManager); michael@0: pwmgr.removeAllLogins(); michael@0: michael@0: SimpleTest.finish(); michael@0: } michael@0: michael@0: addEventListener('testready', runTest);