2009-08-06 16:43:53 -07:00
|
|
|
var gExpectedCookies;
|
|
|
|
var gExpectedHeaders;
|
|
|
|
var gExpectedLoads;
|
2008-03-12 00:53:50 -07:00
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
var gObs;
|
|
|
|
var gPopup;
|
2008-03-12 00:53:50 -07:00
|
|
|
|
|
|
|
var gHeaders = 0;
|
2009-08-06 16:43:53 -07:00
|
|
|
var gLoads = 0;
|
2008-03-12 00:53:50 -07:00
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
// setupTest() is run from 'onload='.
|
2008-03-12 00:53:50 -07:00
|
|
|
function setupTest(uri, domain, cookies, loads, headers) {
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(true, "setupTest uri: " + uri + " domain: " + domain + " cookies: " + cookies +
|
|
|
|
" loads: " + loads + " headers: " + headers);
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
2009-07-24 18:16:33 -07:00
|
|
|
Components.classes["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPrefBranch)
|
|
|
|
.setIntPref("network.cookie.cookieBehavior", 1);
|
2008-03-12 00:53:50 -07:00
|
|
|
|
|
|
|
var cs = Components.classes["@mozilla.org/cookiemanager;1"]
|
2009-07-24 18:16:33 -07:00
|
|
|
.getService(Components.interfaces.nsICookieManager2);
|
2010-06-08 12:18:30 -07:00
|
|
|
|
|
|
|
ok(true, "we are going to remove these cookies");
|
|
|
|
var count = 0;
|
|
|
|
var list = cs.enumerator;
|
|
|
|
while (list.hasMoreElements()) {
|
|
|
|
var cookie = list.getNext().QueryInterface(Components.interfaces.nsICookie);
|
|
|
|
ok(true, "cookie: " + cookie);
|
|
|
|
ok(true, "cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name +
|
|
|
|
" value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ok(true, count + " cookies");
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
cs.removeAll();
|
|
|
|
cs.add(domain, "", "oh", "hai", false, false, true, Math.pow(2, 62));
|
2009-08-06 16:43:53 -07:00
|
|
|
is(cs.countCookiesFromHost(domain), 1, "number of cookies for domain " + domain);
|
2008-03-12 00:53:50 -07:00
|
|
|
|
|
|
|
gExpectedCookies = cookies;
|
|
|
|
gExpectedLoads = loads;
|
|
|
|
gExpectedHeaders = headers;
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
gObs = new obs();
|
|
|
|
// Listen for MessageEvents.
|
|
|
|
window.addEventListener("message", messageReceiver, false);
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
// load a window which contains an iframe; each will attempt to set
|
|
|
|
// cookies from their respective domains.
|
|
|
|
gPopup = window.open(uri, 'hai', 'width=100,height=100');
|
|
|
|
}
|
|
|
|
|
2010-01-21 11:11:49 -08:00
|
|
|
function finishTest()
|
|
|
|
{
|
|
|
|
gObs.remove();
|
|
|
|
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
Components.classes["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPrefBranch)
|
|
|
|
.clearUserPref("network.cookie.cookieBehavior");
|
|
|
|
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
// Count headers.
|
2008-03-12 00:53:50 -07:00
|
|
|
function obs () {
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
2009-07-24 18:16:33 -07:00
|
|
|
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(true, "adding observer");
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
this.window = window;
|
2008-03-12 00:53:50 -07:00
|
|
|
this.os = Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService);
|
|
|
|
this.os.addObserver(this, "http-on-modify-request", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
obs.prototype = {
|
|
|
|
observe: function obs_observe (theSubject, theTopic, theData)
|
|
|
|
{
|
|
|
|
this.window.netscape.security
|
|
|
|
.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(true, "theSubject " + theSubject);
|
|
|
|
ok(true, "theTopic " + theTopic);
|
|
|
|
ok(true, "theData " + theData);
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
var channel = theSubject.QueryInterface(
|
|
|
|
this.window.Components.interfaces.nsIHttpChannel);
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(true, "channel " + channel);
|
|
|
|
try {
|
|
|
|
ok(true, "channel.URI " + channel.URI);
|
|
|
|
ok(true, "channel.URI.spec " + channel.URI.spec);
|
|
|
|
channel.visitRequestHeaders({
|
|
|
|
visitHeader: function(aHeader, aValue) {
|
|
|
|
ok(true, aHeader + ": " + aValue);
|
|
|
|
}});
|
|
|
|
} catch (err) {
|
|
|
|
ok(false, "catch error " + err);
|
|
|
|
}
|
2010-04-30 15:48:40 -07:00
|
|
|
|
|
|
|
// Ignore notifications we don't care about (like favicons)
|
|
|
|
if (channel.URI.spec.indexOf(
|
|
|
|
"http://example.org/tests/extensions/cookie/test/") == -1) {
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(true, "ignoring this one");
|
2010-04-30 15:48:40 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
this.window.isnot(channel.getRequestHeader("Cookie").indexOf("oh=hai"), -1,
|
|
|
|
"cookie 'oh=hai' is in header for " + channel.URI.spec);
|
|
|
|
++gHeaders;
|
2008-03-12 00:53:50 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: function obs_remove()
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
2009-07-24 18:16:33 -07:00
|
|
|
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(true, "removing observer");
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
this.os.removeObserver(this, "http-on-modify-request");
|
|
|
|
this.os = null;
|
|
|
|
this.window = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Receives MessageEvents to this window. */
|
2009-08-06 16:43:53 -07:00
|
|
|
// Count and check loads.
|
2008-03-12 00:53:50 -07:00
|
|
|
function messageReceiver(evt)
|
|
|
|
{
|
2010-05-25 16:40:45 -07:00
|
|
|
ok(evt.data == "f_lf_i msg data img" || evt.data == "f_lf_i msg data page",
|
|
|
|
"message data received from popup");
|
|
|
|
if (evt.data == "f_lf_i msg data img") {
|
|
|
|
ok(true, "message data received from popup for image");
|
|
|
|
}
|
|
|
|
if (evt.data == "f_lf_i msg data page") {
|
|
|
|
ok(true, "message data received from popup for page");
|
|
|
|
}
|
|
|
|
if (evt.data != "f_lf_i msg data img" && evt.data != "f_lf_i msg data page") {
|
|
|
|
ok(true, "got this message but don't know what it is " + evt.data);
|
2009-08-06 16:43:53 -07:00
|
|
|
gPopup.close();
|
2009-07-24 18:16:33 -07:00
|
|
|
window.removeEventListener("message", messageReceiver, false);
|
|
|
|
|
2010-01-21 11:11:49 -08:00
|
|
|
finishTest();
|
2009-07-24 18:16:33 -07:00
|
|
|
return;
|
2008-03-12 00:53:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// only run the test when all our children are done loading & setting cookies
|
2009-07-24 18:16:33 -07:00
|
|
|
if (++gLoads == gExpectedLoads) {
|
2009-08-06 16:43:53 -07:00
|
|
|
gPopup.close();
|
2009-07-24 18:16:33 -07:00
|
|
|
window.removeEventListener("message", messageReceiver, false);
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
runTest();
|
2009-07-24 18:16:33 -07:00
|
|
|
}
|
2008-03-12 00:53:50 -07:00
|
|
|
}
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
// runTest() is run by messageReceiver().
|
|
|
|
// Check headers, and count and check cookies.
|
2008-03-12 00:53:50 -07:00
|
|
|
function runTest() {
|
|
|
|
// set a cookie from a domain of "localhost"
|
|
|
|
document.cookie = "o=noes";
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
is(gHeaders, gExpectedHeaders, "number of observed request headers");
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
2009-07-24 18:16:33 -07:00
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
var cs = Components.classes["@mozilla.org/cookiemanager;1"]
|
|
|
|
.getService(Components.interfaces.nsICookieManager);
|
|
|
|
var count = 0;
|
2010-05-25 16:40:45 -07:00
|
|
|
var list = cs.enumerator;
|
|
|
|
while (list.hasMoreElements()) {
|
|
|
|
var cookie = list.getNext().QueryInterface(Components.interfaces.nsICookie);
|
|
|
|
ok(true, "cookie: " + cookie);
|
|
|
|
ok(true, "cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name +
|
|
|
|
" value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires);
|
2009-08-06 16:43:53 -07:00
|
|
|
++count;
|
2010-05-25 16:40:45 -07:00
|
|
|
}
|
2009-08-06 16:43:53 -07:00
|
|
|
is(count, gExpectedCookies, "total number of cookies");
|
2009-07-24 18:16:33 -07:00
|
|
|
cs.removeAll();
|
|
|
|
|
2010-06-08 12:18:30 -07:00
|
|
|
setTimeout(finishTest, 5000);
|
2008-03-12 00:53:50 -07:00
|
|
|
}
|