test_httpauth.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Private Browsing Tests.
15  *
16  * The Initial Developer of the Original Code is
17  * Ehsan Akhgari.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either of the GNU General Public License Version 2 or later (the "GPL"),
26  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 // This test makes sure the HTTP authenticated sessions are correctly cleared
39 // when entering and leaving the private browsing mode.
40 
41 function run_test_on_service() {
43  getService(Ci.nsIPrivateBrowsingService);
44 
45  var am = Cc["@mozilla.org/network/http-auth-manager;1"].
46  getService(Ci.nsIHttpAuthManager);
47 
48  const kHost1 = "pbtest3.example.com";
49  const kHost2 = "pbtest4.example.com";
50  const kPort = 80;
51  const kHTTP = "http";
52  const kBasic = "basic";
53  const kRealm = "realm";
54  const kDomain = "example.com";
55  const kUser = "user";
56  const kUser2 = "user2";
57  const kPassword = "pass";
58  const kPassword2 = "pass2";
59  const kEmpty = "";
60 
61  try {
62  var domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty};
63  // simulate a login via HTTP auth outside of the private mode
64  am.setAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, kDomain, kUser, kPassword);
65  // make sure the recently added auth entry is available outside the private browsing mode
66  am.getAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, domain, user, pass);
67  do_check_eq(domain.value, kDomain);
68  do_check_eq(user.value, kUser);
69  do_check_eq(pass.value, kPassword);
70  // enter private browsing mode
71  pb.privateBrowsingEnabled = true;
72  // make sure the added auth entry is no longer accessible
73  domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty};
74  try {
75  // should throw
76  am.getAuthIdentity(kHTTP, kHost1, kPort, kBasic, kRealm, kEmpty, domain, user, pass);
77  do_throw("Auth entry should not be retrievable after entering the private browsing mode");
78  } catch (e) {
79  do_check_eq(domain.value, kEmpty);
80  do_check_eq(user.value, kEmpty);
81  do_check_eq(pass.value, kEmpty);
82  }
83 
84  // simulate a login via HTTP auth inside of the private mode
85  am.setAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, kDomain, kUser2, kPassword2);
86  // make sure the recently added auth entry is available outside the private browsing mode
87  domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty};
88  am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass);
89  do_check_eq(domain.value, kDomain);
90  do_check_eq(user.value, kUser2);
91  do_check_eq(pass.value, kPassword2);
92  // exit private browsing mode
93  pb.privateBrowsingEnabled = false;
94  // make sure the added auth entry is no longer accessible
95  domain = {value: kEmpty}, user = {value: kEmpty}, pass = {value: kEmpty};
96  try {
97  // should throw
98  am.getAuthIdentity(kHTTP, kHost2, kPort, kBasic, kRealm, kEmpty, domain, user, pass);
99  do_throw("Auth entry should not be retrievable after exiting the private browsing mode");
100  } catch (e) {
101  do_check_eq(domain.value, kEmpty);
102  do_check_eq(user.value, kEmpty);
103  do_check_eq(pass.value, kEmpty);
104  }
105  } catch (e) {
106  do_throw("Unexpected exception while testing HTTP auth manager: " + e);
107  }
108 }
109 
110 // Support running tests on both the service itself and its wrapper
111 function run_test() {
113 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
function run_test_on_service()
function run_test()
getService(Ci.sbIFaceplateManager)
countRef value
Definition: FeedWriter.js:1423
const Ci
function run_test_on_all_services()
var PRIVATEBROWSING_CONTRACT_ID