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