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) {
|
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);
|
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
|
|
|
|
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");
|
|
|
|
|
2009-08-06 16:43:53 -07:00
|
|
|
var channel = theSubject.QueryInterface(
|
|
|
|
this.window.Components.interfaces.nsIHttpChannel);
|
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) {
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
{
|
2009-08-06 16:43:53 -07:00
|
|
|
is(evt.data, "f_lf_i msg data", "message data received from popup");
|
|
|
|
if (evt.data != "f_lf_i msg data") {
|
|
|
|
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;
|
2009-08-06 16:43:53 -07:00
|
|
|
for(var list = cs.enumerator; list.hasMoreElements(); list.getNext())
|
|
|
|
++count;
|
|
|
|
is(count, gExpectedCookies, "total number of cookies");
|
2009-07-24 18:16:33 -07:00
|
|
|
cs.removeAll();
|
|
|
|
|
2010-01-21 11:11:49 -08:00
|
|
|
finishTest();
|
2008-03-12 00:53:50 -07:00
|
|
|
}
|