1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is Private Browsing Tests.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
22 * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
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.
36 * ***** END LICENSE BLOCK ***** */
39 function get_PBSvc() {
44 _PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"].
45 getService(Components.interfaces.nsIPrivateBrowsingService);
52 function get_CookieManager() {
56 return _CMSvc = Components.classes["@mozilla.org/cookiemanager;1"].
57 getService(Components.interfaces.nsICookieManager2);
60 function is_cookie_available1(domain, path, name, value,
61 secure, httponly, session, expires) {
62 var cm = get_CookieManager();
63 var enumerator = cm.enumerator;
64 while (enumerator.hasMoreElements()) {
65 var cookie = enumerator.getNext().QueryInterface(Components.interfaces.nsICookie);
66 if (cookie.host == domain &&
67 cookie.path == path &&
68 cookie.name == name &&
69 cookie.value == value &&
70 cookie.isSecure == secure &&
71 cookie.expires == expires)
77 function is_cookie_available2(domain, path, name, value,
78 secure, httponly, session, expires) {
92 QueryInterface: function(iid) {
93 var validIIDs = [Components.interfaces.nsISupports,
94 Components.interfaces.nsICookie,
95 Components.interfaces.nsICookie2];
96 for (var i = 0; i < validIIDs.length; ++i) {
97 if (iid == validIIDs[i])
100 throw Components.results.NS_ERROR_NO_INTERFACE;
104 var cm = get_CookieManager();
105 return cm.cookieExists(cookie);
108 var cc_observer = null;
109 function setup_cookie_changed_observer() {
112 QueryInterface: function (iid) {
113 const interfaces = [Components.interfaces.nsIObserver,
114 Components.interfaces.nsISupports];
115 if (!interfaces.some(function(v) iid.equals(v)))
116 throw Components.results.NS_ERROR_NO_INTERFACE;
119 observe: function (subject, topic, data) {
120 if (topic == "cookie-changed") {
122 if (data == "reload")
123 this.gotReloaded = true;
128 var os = Components.classes["@mozilla.org/observer-service;1"].
129 getService(Components.interfaces.nsIObserverService);
130 os.addObserver(cc_observer, "cookie-changed", false);
133 function run_test() {
134 var pb = get_PBSvc();
135 if (pb) { // Private Browsing might not be available
136 var prefBranch = Components.classes["@mozilla.org/preferences-service;1"].
137 getService(Components.interfaces.nsIPrefBranch);
138 prefBranch.setBoolPref("browser.privatebrowsing.keep_current_session", true);
140 var cm = get_CookieManager();
141 do_check_neq(cm, null);
143 setup_cookie_changed_observer();
144 do_check_neq(cc_observer, null);
148 const time = (new Date("Jan 1, 2030")).getTime() / 1000;
149 cm.add("pbtest.example.com", "/", "C1", "V1", false, true, false, time);
150 // make sure Cookie-A is retrievable
151 do_check_true(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
152 do_check_true(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
153 // enter private browsing mode
154 pb.privateBrowsingEnabled = true;
155 // make sure the "cleared" notification was fired
156 do_check_true(cc_observer.gotReloaded);
157 cc_observer.gotReloaded = false;
158 // make sure Cookie-A is not retrievable
159 do_check_false(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
160 do_check_false(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
162 const time2 = (new Date("Jan 2, 2030")).getTime() / 1000;
163 cm.add("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2);
164 // make sure Cookie-B is retrievable
165 do_check_true(is_cookie_available1("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
166 do_check_true(is_cookie_available2("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
167 // exit private browsing mode
168 pb.privateBrowsingEnabled = false;
169 // make sure the "reload" notification was fired
170 do_check_true(cc_observer.gotReloaded);
171 // make sure Cookie-B is not retrievable
172 do_check_false(is_cookie_available1("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
173 do_check_false(is_cookie_available2("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
174 // make sure Cookie-A is retrievable
175 do_check_true(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
176 do_check_true(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
178 do_throw("Unexpected exception while testing cookies: " + e);
181 prefBranch.clearUserPref("browser.privatebrowsing.keep_current_session");