Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This test makes sure the HTTP authenticated sessions are correctly cleared |
michael@0 | 6 | // when entering and leaving the private browsing mode. |
michael@0 | 7 | |
michael@0 | 8 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 9 | |
michael@0 | 10 | function run_test() { |
michael@0 | 11 | var am = Cc["@mozilla.org/network/http-auth-manager;1"]. |
michael@0 | 12 | getService(Ci.nsIHttpAuthManager); |
michael@0 | 13 | |
michael@0 | 14 | const kHost1 = "pbtest3.example.com"; |
michael@0 | 15 | const kHost2 = "pbtest4.example.com"; |
michael@0 | 16 | const kPort = 80; |
michael@0 | 17 | const kHTTP = "http"; |
michael@0 | 18 | const kBasic = "basic"; |
michael@0 | 19 | const kRealm = "realm"; |
michael@0 | 20 | const kDomain = "example.com"; |
michael@0 | 21 | const kUser = "user"; |
michael@0 | 22 | const kUser2 = "user2"; |
michael@0 | 23 | const kPassword = "pass"; |
michael@0 | 24 | const kPassword2 = "pass2"; |
michael@0 | 25 | const kEmpty = ""; |
michael@0 | 26 | |
michael@0 | 27 | const PRIVATE = true; |
michael@0 | 28 | const NOT_PRIVATE = false; |
michael@0 | 29 | |
michael@0 | 30 | try { |
michael@0 | 31 | var domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; |
michael@0 | 32 | // simulate a login via HTTP auth outside of the private mode |
michael@0 | 33 | am.setAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, kDomain, kUser, kPassword); |
michael@0 | 34 | // make sure the recently added auth entry is available outside the private browsing mode |
michael@0 | 35 | am.getAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, domain, user, pass, NOT_PRIVATE); |
michael@0 | 36 | do_check_eq(domain.value, kDomain); |
michael@0 | 37 | do_check_eq(user.value, kUser); |
michael@0 | 38 | do_check_eq(pass.value, kPassword); |
michael@0 | 39 | |
michael@0 | 40 | // make sure the added auth entry is no longer accessible in private |
michael@0 | 41 | domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; |
michael@0 | 42 | try { |
michael@0 | 43 | // should throw |
michael@0 | 44 | am.getAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, domain, user, pass, PRIVATE); |
michael@0 | 45 | do_throw("Auth entry should not be retrievable after entering the private browsing mode"); |
michael@0 | 46 | } catch (e) { |
michael@0 | 47 | do_check_eq(domain.value, kEmpty); |
michael@0 | 48 | do_check_eq(user.value, kEmpty); |
michael@0 | 49 | do_check_eq(pass.value, kEmpty); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | // simulate a login via HTTP auth inside of the private mode |
michael@0 | 53 | am.setAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, kDomain, kUser2, kPassword2, PRIVATE); |
michael@0 | 54 | // make sure the recently added auth entry is available inside the private browsing mode |
michael@0 | 55 | domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; |
michael@0 | 56 | am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, PRIVATE); |
michael@0 | 57 | do_check_eq(domain.value, kDomain); |
michael@0 | 58 | do_check_eq(user.value, kUser2); |
michael@0 | 59 | do_check_eq(pass.value, kPassword2); |
michael@0 | 60 | |
michael@0 | 61 | try { |
michael@0 | 62 | // make sure the recently added auth entry is not available outside the private browsing mode |
michael@0 | 63 | domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; |
michael@0 | 64 | am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, NOT_PRIVATE); |
michael@0 | 65 | do_throw("Auth entry should not be retrievable outside of private browsing mode"); |
michael@0 | 66 | } catch (x) { |
michael@0 | 67 | do_check_eq(domain.value, kEmpty); |
michael@0 | 68 | do_check_eq(user.value, kEmpty); |
michael@0 | 69 | do_check_eq(pass.value, kEmpty); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // simulate leaving private browsing mode |
michael@0 | 73 | Services.obs.notifyObservers(null, "last-pb-context-exited", null); |
michael@0 | 74 | |
michael@0 | 75 | // make sure the added auth entry is no longer accessible in any privacy state |
michael@0 | 76 | domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty}; |
michael@0 | 77 | try { |
michael@0 | 78 | // should throw (not available in public mode) |
michael@0 | 79 | am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, NOT_PRIVATE); |
michael@0 | 80 | do_throw("Auth entry should not be retrievable after exiting the private browsing mode"); |
michael@0 | 81 | } catch (e) { |
michael@0 | 82 | do_check_eq(domain.value, kEmpty); |
michael@0 | 83 | do_check_eq(user.value, kEmpty); |
michael@0 | 84 | do_check_eq(pass.value, kEmpty); |
michael@0 | 85 | } |
michael@0 | 86 | try { |
michael@0 | 87 | // should throw (no longer available in private mode) |
michael@0 | 88 | am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass, PRIVATE); |
michael@0 | 89 | do_throw("Auth entry should not be retrievable in private mode after exiting the private browsing mode"); |
michael@0 | 90 | } catch (x) { |
michael@0 | 91 | do_check_eq(domain.value, kEmpty); |
michael@0 | 92 | do_check_eq(user.value, kEmpty); |
michael@0 | 93 | do_check_eq(pass.value, kEmpty); |
michael@0 | 94 | } |
michael@0 | 95 | } catch (e) { |
michael@0 | 96 | do_throw("Unexpected exception while testing HTTP auth manager: " + e); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 |