netwerk/test/unit/test_httpauth.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial