michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This test makes sure the HTTP authenticated sessions are correctly cleared michael@0: // when entering and leaving the private browsing mode. michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function run_test() { michael@0: var am = Cc["@mozilla.org/network/http-auth-manager;1"]. michael@0: getService(Ci.nsIHttpAuthManager); michael@0: michael@0: const kHost1 = "pbtest3.example.com"; michael@0: const kHost2 = "pbtest4.example.com"; michael@0: const kPort = 80; michael@0: const kHTTP = "http"; michael@0: const kBasic = "basic"; michael@0: const kRealm = "realm"; michael@0: const kDomain = "example.com"; michael@0: const kUser = "user"; michael@0: const kUser2 = "user2"; michael@0: const kPassword = "pass"; michael@0: const kPassword2 = "pass2"; michael@0: const kEmpty = ""; michael@0: michael@0: const PRIVATE = true; michael@0: const NOT_PRIVATE = false; michael@0: michael@0: try { michael@0: var domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; michael@0: // simulate a login via HTTP auth outside of the private mode michael@0: am.setAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, kDomain, kUser, kPassword); michael@0: // make sure the recently added auth entry is available outside the private browsing mode michael@0: am.getAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, domain, user, pass, NOT_PRIVATE); michael@0: do_check_eq(domain.value, kDomain); michael@0: do_check_eq(user.value, kUser); michael@0: do_check_eq(pass.value, kPassword); michael@0: michael@0: // make sure the added auth entry is no longer accessible in private michael@0: domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; michael@0: try { michael@0: // should throw michael@0: am.getAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, domain, user, pass, PRIVATE); michael@0: do_throw("Auth entry should not be retrievable after entering the private browsing mode"); michael@0: } catch (e) { michael@0: do_check_eq(domain.value, kEmpty); michael@0: do_check_eq(user.value, kEmpty); michael@0: do_check_eq(pass.value, kEmpty); michael@0: } michael@0: michael@0: // simulate a login via HTTP auth inside of the private mode michael@0: am.setAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, kDomain, kUser2, kPassword2, PRIVATE); michael@0: // make sure the recently added auth entry is available inside the private browsing mode michael@0: domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; michael@0: am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, PRIVATE); michael@0: do_check_eq(domain.value, kDomain); michael@0: do_check_eq(user.value, kUser2); michael@0: do_check_eq(pass.value, kPassword2); michael@0: michael@0: try { michael@0: // make sure the recently added auth entry is not available outside the private browsing mode michael@0: domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; michael@0: am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, NOT_PRIVATE); michael@0: do_throw("Auth entry should not be retrievable outside of private browsing mode"); michael@0: } catch (x) { michael@0: do_check_eq(domain.value, kEmpty); michael@0: do_check_eq(user.value, kEmpty); michael@0: do_check_eq(pass.value, kEmpty); michael@0: } michael@0: michael@0: // simulate leaving private browsing mode michael@0: Services.obs.notifyObservers(null, "last-pb-context-exited", null); michael@0: michael@0: // make sure the added auth entry is no longer accessible in any privacy state michael@0: domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; michael@0: try { michael@0: // should throw (not available in public mode) michael@0: am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, NOT_PRIVATE); michael@0: do_throw("Auth entry should not be retrievable after exiting the private browsing mode"); michael@0: } catch (e) { michael@0: do_check_eq(domain.value, kEmpty); michael@0: do_check_eq(user.value, kEmpty); michael@0: do_check_eq(pass.value, kEmpty); michael@0: } michael@0: try { michael@0: // should throw (no longer available in private mode) michael@0: am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, PRIVATE); michael@0: do_throw("Auth entry should not be retrievable in private mode after exiting the private browsing mode"); michael@0: } catch (x) { michael@0: do_check_eq(domain.value, kEmpty); michael@0: do_check_eq(user.value, kEmpty); michael@0: do_check_eq(pass.value, kEmpty); michael@0: } michael@0: } catch (e) { michael@0: do_throw("Unexpected exception while testing HTTP auth manager: " + e); michael@0: } michael@0: } michael@0: