Bug 1632187 - Introduce nsICookieService::setCookieStringFromDocument - part 3 - remove nsICookieService::setCookieString from tests, r=mayhemer

Differential Revision: https://phabricator.services.mozilla.com/D71976
This commit is contained in:
Andrea Marchesini 2020-05-09 22:28:53 +00:00
parent 871dd4ea6a
commit 3321630999
26 changed files with 498 additions and 496 deletions

View File

@ -69,8 +69,8 @@ async function test_cookie_settings({
Ci.nsIHttpChannelInternal
).forceAllowThirdPartyCookie = true;
Services.cookies.removeAll();
Services.cookies.setCookieString(firstPartyURI, "key=value", channel);
Services.cookies.setCookieString(thirdPartyURI, "key=value", channel);
Services.cookies.setCookieStringFromHttp(firstPartyURI, "key=value", channel);
Services.cookies.setCookieStringFromHttp(thirdPartyURI, "key=value", channel);
let expectedFirstPartyCookies = 1;
let expectedThirdPartyCookies = 1;
@ -94,7 +94,7 @@ async function test_cookie_settings({
// Add a cookie so we can check if it persists past the end of the session
// but, first remove existing cookies set by this host to put us in a known state
Services.cookies.removeAll();
Services.cookies.setCookieString(
Services.cookies.setCookieStringFromHttp(
firstPartyURI,
"key=value; max-age=1000",
channel

View File

@ -0,0 +1,39 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["CookieXPCShellUtils"];
const { ExtensionTestUtils } = ChromeUtils.import(
"resource://testing-common/ExtensionXPCShellUtils.jsm"
);
const { AddonTestUtils } = ChromeUtils.import(
"resource://testing-common/AddonTestUtils.jsm"
);
const CookieXPCShellUtils = {
init(scope) {
AddonTestUtils.maybeInit(scope);
ExtensionTestUtils.init(scope);
},
createServer(args) {
const server = AddonTestUtils.createHttpServer(args);
server.registerPathHandler("/", (metadata, response) => {
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/html", false);
let body = "<body><h1>Hello world!</h1></body>";
response.bodyOutputStream.write(body, body.length);
});
return server;
},
async loadContentPage(uri) {
return ExtensionTestUtils.loadContentPage(uri);
},
};

View File

@ -64,6 +64,10 @@ LOCAL_INCLUDES += [
'/netwerk/protocol/http',
]
TESTING_JS_MODULES += [
'CookieXPCShellUtils.jsm',
]
include('/ipc/chromium/chromium-config.mozbuild')
FINAL_LIBRARY = 'xul'

View File

@ -1,7 +1,13 @@
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { CookieXPCShellUtils } = ChromeUtils.import(
"resource://testing-common/CookieXPCShellUtils.jsm"
);
function run_test() {
CookieXPCShellUtils.init(this);
CookieXPCShellUtils.createServer({ hosts: ["example.net"] });
add_task(async () => {
// Allow all cookies.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref(
@ -25,12 +31,16 @@ function run_test() {
let actual = cs.getCookieStringFromHttp(uri, channel);
Assert.equal(actual, expected);
uri = NetUtil.newURI("http://example.com/");
cs.setCookieString(uri, set, null);
uri = NetUtil.newURI("http://example.net/");
const contentPage = await CookieXPCShellUtils.loadContentPage(uri.spec);
// eslint-disable-next-line no-undef
await contentPage.spawn(set, cookie => (content.document.cookie = cookie));
await contentPage.close();
expected = "foo=bar";
actual = cs.getCookieStringForPrincipal(
Services.scriptSecurityManager.createContentPrincipal(uri, {})
);
Assert.equal(actual, expected);
}
});

View File

@ -107,16 +107,6 @@ void SetACookie(nsICookieService* aCookieService, const char* aSpec,
SetACookieInternal(aCookieService, aSpec, aCookieString, true);
}
void SetACookieNoHttp(nsICookieService* aCookieService, const char* aSpec,
const char* aCookieString) {
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), aSpec);
nsresult rv = aCookieService->SetCookieString(
uri, nsDependentCString(aCookieString), nullptr);
EXPECT_TRUE(NS_SUCCEEDED(rv));
}
// The cookie string is returned via aCookie.
void GetACookie(nsICookieService* aCookieService, const char* aSpec,
nsACString& aCookie) {
@ -626,52 +616,6 @@ TEST(TestCookie, TestCookieMain)
"test7=path; test6=path; test3=path; test1=path; "
"test5=path; test4=path; test2=path; test8=path"));
// *** httponly tests
// Since this cookie is NOT set via http, setting it fails
SetACookieNoHttp(cookieService, "http://httponly.test/",
"test=httponly; httponly");
GetACookie(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));
// Since this cookie is set via http, it can be retrieved
SetACookie(cookieService, "http://httponly.test/", "test=httponly; httponly");
GetACookie(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_EQUAL, "test=httponly"));
// ... but not by web content
GetACookieNoHttp(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));
// Non-Http cookies should not replace HttpOnly cookies
SetACookie(cookieService, "http://httponly.test/", "test=httponly; httponly");
SetACookieNoHttp(cookieService, "http://httponly.test/", "test=not-httponly");
GetACookie(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_EQUAL, "test=httponly"));
// ... and, if an HttpOnly cookie already exists, should not be set at all
GetACookieNoHttp(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));
// Non-Http cookies should not delete HttpOnly cookies
SetACookie(cookieService, "http://httponly.test/", "test=httponly; httponly");
SetACookieNoHttp(cookieService, "http://httponly.test/",
"test=httponly; max-age=-1");
GetACookie(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_EQUAL, "test=httponly"));
// ... but HttpOnly cookies should
SetACookie(cookieService, "http://httponly.test/",
"test=httponly; httponly; max-age=-1");
GetACookie(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));
// Non-Httponly cookies can replace HttpOnly cookies when set over http
SetACookie(cookieService, "http://httponly.test/", "test=httponly; httponly");
SetACookie(cookieService, "http://httponly.test/", "test=not-httponly");
GetACookieNoHttp(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_EQUAL, "test=not-httponly"));
// scripts should not be able to set httponly cookies by replacing an existing
// non-httponly cookie
SetACookie(cookieService, "http://httponly.test/", "test=not-httponly");
SetACookieNoHttp(cookieService, "http://httponly.test/",
"test=httponly; httponly");
GetACookieNoHttp(cookieService, "http://httponly.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_EQUAL, "test=not-httponly"));
// *** Cookie prefix tests
// prefixed cookies can't be set from insecure HTTP

View File

@ -8,6 +8,12 @@
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const { CookieXPCShellUtils } = ChromeUtils.import(
"resource://testing-common/CookieXPCShellUtils.jsm"
);
CookieXPCShellUtils.init(this);
XPCOMUtils.defineLazyServiceGetter(
Services,
"cookies",
@ -95,6 +101,51 @@ function do_close_profile(generator) {
service.observe(null, "profile-before-change", "shutdown-persist");
}
function _promise_observer(topic) {
Services.obs.addObserver(this, topic);
this.topic = topic;
return new Promise(resolve => (this.resolve = resolve));
}
_promise_observer.prototype = {
observe(subject, topic, data) {
Assert.equal(this.topic, topic);
Services.obs.removeObserver(this, this.topic);
if (this.resolve) {
this.resolve();
}
this.resolve = null;
this.topic = null;
},
};
// Close the cookie database. And resolve a promise.
function promise_close_profile() {
// Register an observer for db close.
let promise = new _promise_observer("cookie-db-closed");
// Close the db.
let service = Services.cookies.QueryInterface(Ci.nsIObserver);
service.observe(null, "profile-before-change", "shutdown-persist");
return promise;
}
// Load the cookie database.
function promise_load_profile() {
// Register an observer for read completion.
let promise = new _promise_observer("cookie-db-read");
// Load the profile.
let service = Services.cookies.QueryInterface(Ci.nsIObserver);
service.observe(null, "profile-do-change", "");
return promise;
}
// Load the cookie database. If a generator is supplied, it will be invoked
// once the load is complete.
function do_load_profile(generator) {
@ -113,20 +164,40 @@ function do_set_single_http_cookie(uri, channel, expected) {
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri.host), expected);
}
// Set four cookies; with & without channel, http and non-http; and test
// the cookie count against 'expected' after each set.
function do_set_cookies(uri, channel, session, expected) {
// Set two cookies; via document.channel and via http request.
async function do_set_cookies(uri, channel, session, expected) {
let suffix = session ? "" : "; max-age=1000";
// without channel
Services.cookies.setCookieString(uri, "oh=hai" + suffix, null);
// via document.cookie
const thirdPartyUrl = "http://third.com/";
const contentPage = await CookieXPCShellUtils.loadContentPage(thirdPartyUrl);
await contentPage.spawn(
{
cookie: "can=has" + suffix,
url: uri.spec,
},
async obj => {
// eslint-disable-next-line no-undef
await new content.Promise(resolve => {
// eslint-disable-next-line no-undef
const ifr = content.document.createElement("iframe");
// eslint-disable-next-line no-undef
content.document.body.appendChild(ifr);
ifr.src = obj.url;
ifr.onload = () => {
ifr.contentDocument.cookie = obj.cookie;
resolve();
};
});
}
);
await contentPage.close();
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri.host), expected[0]);
// with channel
Services.cookies.setCookieString(uri, "can=has" + suffix, channel);
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri.host), expected[1]);
// with channel, from http
// via http request
Services.cookies.setCookieStringFromHttp(uri, "hot=dog" + suffix, channel);
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri.host), expected[2]);
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri.host), expected[1]);
}
function do_count_cookies() {

View File

@ -3,7 +3,7 @@
"use strict";
function run_test() {
add_task(async () => {
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
var expiry = (Date.now() + 1000) * 1000;
@ -136,6 +136,10 @@ function run_test() {
cm.removeAll();
CookieXPCShellUtils.createServer({
hosts: ["baz.com", "192.168.0.1", "localhost", "co.uk", "foo.com"],
});
var uri = NetUtil.newURI("http://baz.com/");
const principal = Services.scriptSecurityManager.createContentPrincipal(
uri,
@ -143,7 +147,12 @@ function run_test() {
);
Assert.equal(uri.asciiHost, "baz.com");
cs.setCookieString(uri, "foo=bar", null);
const contentPage = await CookieXPCShellUtils.loadContentPage(uri.spec);
// eslint-disable-next-line no-undef
await contentPage.spawn(null, () => (content.document.cookie = "foo=bar"));
await contentPage.close();
Assert.equal(cs.getCookieStringForPrincipal(principal), "foo=bar");
Assert.equal(cm.countCookiesFromHost(""), 0);
@ -177,46 +186,6 @@ function run_test() {
cm.removeAll();
// test that an empty file:// host works
let emptyuri = NetUtil.newURI("file:///");
const emptyprincipal = Services.scriptSecurityManager.createContentPrincipal(
emptyuri,
{}
);
Assert.equal(emptyuri.asciiHost, "");
Assert.equal(NetUtil.newURI("file://./").asciiHost, "");
Assert.equal(NetUtil.newURI("file://foo.bar/").asciiHost, "");
cs.setCookieString(emptyuri, "foo2=bar", null);
Assert.equal(getCookieCount(), 1);
cs.setCookieString(emptyuri, "foo3=bar; domain=", null);
Assert.equal(getCookieCount(), 2);
cs.setCookieString(emptyuri, "foo4=bar; domain=.", null);
Assert.equal(getCookieCount(), 2);
cs.setCookieString(emptyuri, "foo5=bar; domain=bar.com", null);
Assert.equal(getCookieCount(), 2);
Assert.equal(
cs.getCookieStringForPrincipal(emptyprincipal),
"foo2=bar; foo3=bar"
);
Assert.equal(cm.countCookiesFromHost("baz.com"), 0);
Assert.equal(cm.countCookiesFromHost(""), 2);
do_check_throws(function() {
cm.countCookiesFromHost(".");
}, Cr.NS_ERROR_ILLEGAL_VALUE);
cookies = cm.getCookiesFromHost("baz.com", {});
Assert.ok(!cookies.length);
cookies = cm.getCookiesFromHost("", {});
Assert.equal(cookies.length, 2);
do_check_throws(function() {
cm.getCookiesFromHost(".", {});
}, Cr.NS_ERROR_ILLEGAL_VALUE);
cm.removeAll();
// test that an empty host to add() or remove() works,
// but a host of '.' doesn't
cm.add(
@ -257,17 +226,17 @@ function run_test() {
// test that the 'domain' attribute accepts a leading dot for IP addresses,
// aliases such as 'localhost', and eTLD's such as 'co.uk'; but that the
// resulting cookie is for the exact host only.
testDomainCookie("http://192.168.0.1/", "192.168.0.1");
testDomainCookie("http://localhost/", "localhost");
testDomainCookie("http://co.uk/", "co.uk");
await testDomainCookie("http://192.168.0.1/", "192.168.0.1");
await testDomainCookie("http://localhost/", "localhost");
await testDomainCookie("http://co.uk/", "co.uk");
// Test that trailing dots are treated differently for purposes of the
// 'domain' attribute when using setCookieString.
testTrailingDotCookie("http://localhost", "localhost");
testTrailingDotCookie("http://foo.com", "foo.com");
// 'domain' attribute when using setCookieStringFromDocument.
await testTrailingDotCookie("http://localhost/", "localhost");
await testTrailingDotCookie("http://foo.com/", "foo.com");
cm.removeAll();
}
});
function getCookieCount() {
var count = 0;
@ -278,40 +247,53 @@ function getCookieCount() {
return count;
}
function testDomainCookie(uriString, domain) {
async function testDomainCookie(uriString, domain) {
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
cm.removeAll();
var uri = NetUtil.newURI(uriString);
cs.setCookieString(uri, "foo=bar; domain=" + domain, null);
var contentPage = await CookieXPCShellUtils.loadContentPage(uriString);
await contentPage.spawn(
"foo=bar; domain=" + domain,
// eslint-disable-next-line no-undef
cookie => (content.document.cookie = cookie)
);
await contentPage.close();
var cookies = cm.getCookiesFromHost(domain, {});
Assert.ok(cookies.length);
Assert.equal(cookies[0].host, domain);
cm.removeAll();
cs.setCookieString(uri, "foo=bar; domain=." + domain, null);
contentPage = await CookieXPCShellUtils.loadContentPage(uriString);
await contentPage.spawn(
"foo=bar; domain=." + domain,
// eslint-disable-next-line no-undef
cookie => (content.document.cookie = cookie)
);
await contentPage.close();
cookies = cm.getCookiesFromHost(domain, {});
Assert.ok(cookies.length);
Assert.equal(cookies[0].host, domain);
cm.removeAll();
}
function testTrailingDotCookie(uriString, domain) {
async function testTrailingDotCookie(uriString, domain) {
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
cm.removeAll();
var uri = NetUtil.newURI(uriString);
cs.setCookieString(uri, "foo=bar; domain=" + domain + ".", null);
Assert.equal(cm.countCookiesFromHost(domain), 0);
Assert.equal(cm.countCookiesFromHost(domain + "."), 0);
cm.removeAll();
var contentPage = await CookieXPCShellUtils.loadContentPage(uriString);
await contentPage.spawn(
"foo=bar; domain=" + domain + "/",
// eslint-disable-next-line no-undef
cookie => (content.document.cookie = cookie)
);
await contentPage.close();
uri = NetUtil.newURI(uriString + ".");
cs.setCookieString(uri, "foo=bar; domain=" + domain, null);
Assert.equal(cm.countCookiesFromHost(domain), 0);
Assert.equal(cm.countCookiesFromHost(domain + "."), 0);
cm.removeAll();

View File

@ -42,7 +42,7 @@ function inChildProcess() {
);
}
function run_test() {
add_task(async () => {
// Start the HTTP server.
httpServer = new HttpServer();
httpServer.registerPathHandler(preRedirectPath, preRedirectHandler);
@ -64,9 +64,7 @@ function run_test() {
);
// Set up a channel with forceAllowThirdPartyCookie set to true. We'll use
// the channel both to set a cookie (since nsICookieService::setCookieString
// requires such a channel in order to successfully set a cookie) and then
// to load the pre-redirect URI.
// the channel both to set a cookie and then to load the pre-redirect URI.
var chan = NetUtil.newChannel({
uri: preRedirectURL,
loadUsingSystemPrincipal: true,
@ -79,16 +77,22 @@ function run_test() {
// they're both from the same host, which is enough for the cookie service
// to send the cookie with both requests.
var postRedirectURI = ioService.newURI(postRedirectURL);
Cc["@mozilla.org/cookieService;1"]
.getService(Ci.nsICookieService)
.setCookieString(postRedirectURI, sentCookieVal, chan);
const contentPage = await CookieXPCShellUtils.loadContentPage(
postRedirectURI.spec
);
await contentPage.spawn(
sentCookieVal,
// eslint-disable-next-line no-undef
cookie => (content.document.cookie = cookie)
);
await contentPage.close();
// Load the pre-redirect URI.
chan.asyncOpen(new ChannelListener(finish_test, null));
do_test_pending();
}
await new Promise(resolve => {
chan.asyncOpen(new ChannelListener(resolve, null));
});
function finish_test(event) {
Assert.equal(receivedCookieVal, sentCookieVal);
httpServer.stop(do_test_finished);
}
});

View File

@ -6,26 +6,47 @@ function makeURI(str) {
.newURI(str);
}
function run_test() {
add_task(async () => {
// Allow all cookies.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
);
var serv = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var uri = makeURI("http://example.com/");
var channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
const principal = Services.scriptSecurityManager.createContentPrincipal(
uri,
{}
);
CookieXPCShellUtils.createServer({ hosts: ["example.com"] });
// Try an expiration time before the epoch
serv.setCookieString(
uri,
"test=test; path=/; domain=example.com; expires=Sun, 31-Dec-1899 16:00:00 GMT;",
null
const contentPage = await CookieXPCShellUtils.loadContentPage(uri.spec);
await contentPage.spawn(
null,
() =>
// eslint-disable-next-line no-undef
(content.document.cookie =
"test=test; path=/; domain=example.com; expires=Sun, 31-Dec-1899 16:00:00 GMT;")
);
await contentPage.close();
Assert.equal(serv.getCookieStringForPrincipal(principal), "");
// Now sanity check
serv.setCookieString(uri, "test2=test2; path=/; domain=example.com;", null);
serv.setCookieStringFromHttp(
uri,
"test2=test2; path=/; domain=example.com;",
channel
);
Assert.equal(serv.getCookieStringForPrincipal(principal), "test2=test2");
}
});

View File

@ -49,9 +49,9 @@ var listener = {
throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
},
onStopRequest: function test_onStopR(request, status) {
onStopRequest: async function test_onStopR(request, status) {
if (this._iteration == 1) {
run_test_continued();
await run_test_continued();
} else {
do_test_pending();
httpserv.stop(do_test_finished);
@ -93,14 +93,23 @@ function run_test() {
do_test_pending();
}
function run_test_continued() {
async function run_test_continued() {
var chan = makeChan();
var cookServ = Cc["@mozilla.org/cookieService;1"].getService(
Ci.nsICookieService
);
var cookie2 = "C2=V2";
cookServ.setCookieString(chan.URI, cookie2, chan);
const contentPage = await CookieXPCShellUtils.loadContentPage(chan.URI.spec);
await contentPage.spawn(
cookie2,
// eslint-disable-next-line no-undef
cookie => (content.document.cookie = cookie)
);
await contentPage.close();
chan.setRequestHeader("Cookie", cookieVal, false);
// We expect that the setRequestHeader overrides the

View File

@ -21,29 +21,25 @@
"use strict";
let profile;
let sub_generator;
let cookie;
var test_generator = do_run_test();
function run_test() {
do_test_pending();
do_run_generator(test_generator);
}
function finish_test() {
executeSoon(function() {
test_generator.return();
do_test_finished();
});
}
function* do_run_test() {
add_task(async () => {
// Set up a profile.
profile = do_get_profile();
// Allow all cookies.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
);
// The server.
const hosts = ["foo.com", "hither.com", "haithur.com", "bar.com"];
for (let i = 0; i < 3000; ++i) {
hosts.push(i + ".com");
}
CookieXPCShellUtils.createServer({ hosts });
// Get the cookie file and the backup file.
Assert.ok(!do_get_cookie_file(profile).exists());
@ -65,28 +61,12 @@ function* do_run_test() {
false
);
sub_generator = run_test_1(test_generator);
sub_generator.next();
yield;
sub_generator = run_test_2(test_generator);
sub_generator.next();
yield;
sub_generator = run_test_3(test_generator);
sub_generator.next();
yield;
sub_generator = run_test_4(test_generator);
sub_generator.next();
yield;
sub_generator = run_test_5(test_generator);
sub_generator.next();
yield;
finish_test();
}
await run_test_1();
await run_test_2();
await run_test_3();
await run_test_4();
await run_test_5();
});
function do_get_backup_file(profile) {
let file = profile.clone();
@ -131,14 +111,20 @@ function do_corrupt_db(file) {
return size;
}
function* run_test_1(generator) {
async function run_test_1() {
// Load the profile and populate it.
let uri = NetUtil.newURI("http://foo.com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
const contentPage = await CookieXPCShellUtils.loadContentPage(
"http://foo.com/"
);
await contentPage.spawn(
null,
// eslint-disable-next-line no-undef
() => (content.document.cookie = "oh=hai; max-age=1000")
);
await contentPage.close();
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Open a database connection now, before we load the profile and begin
// asynchronous write operations.
@ -146,8 +132,7 @@ function* run_test_1(generator) {
Assert.equal(do_count_cookies_in_db(db.db), 1);
// Load the profile, and wait for async read completion...
do_load_profile(sub_generator);
yield;
await promise_load_profile();
// Insert a row.
db.insertCookie(cookie);
@ -183,22 +168,16 @@ function* run_test_1(generator) {
// the chaos status.
for (let i = 0; i < 10; ++i) {
Assert.equal(Services.cookiemgr.countCookiesFromHost(cookie.host), 1);
executeSoon(function() {
do_run_generator(sub_generator);
});
yield;
await new Promise(resolve => executeSoon(resolve));
}
// Wait for the cookie service to rename the old database and rebuild if not yet.
if (!isRebuildingDone) {
Services.obs.removeObserver(rebuildingObserve, "cookie-db-rebuilding");
new _observer(sub_generator, "cookie-db-rebuilding");
yield;
await new _promise_observer("cookie-db-rebuilding");
}
executeSoon(function() {
do_run_generator(sub_generator);
});
yield;
await new Promise(resolve => executeSoon(resolve));
// At this point, the cookies should still be in memory.
Assert.equal(Services.cookiemgr.countCookiesFromHost("foo.com"), 1);
@ -206,8 +185,7 @@ function* run_test_1(generator) {
Assert.equal(do_count_cookies(), 2);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Check that the original database was renamed, and that it contains the
// original cookie.
@ -226,30 +204,39 @@ function* run_test_1(generator) {
Assert.equal(dbcookie.value, "hallo");
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Clean up.
do_get_cookie_file(profile).remove(false);
do_get_backup_file(profile).remove(false);
Assert.ok(!do_get_cookie_file(profile).exists());
Assert.ok(!do_get_backup_file(profile).exists());
do_run_generator(generator);
}
function* run_test_2(generator) {
async function run_test_2() {
// Load the profile and populate it.
do_load_profile();
Services.cookies.runInTransaction(_ => {
let uri = NetUtil.newURI("http://foo.com/");
const channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
for (let i = 0; i < 3000; ++i) {
let uri = NetUtil.newURI("http://" + i + ".com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; max-age=1000",
channel
);
}
});
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Corrupt the database file.
let size = do_corrupt_db(do_get_cookie_file(profile));
@ -266,8 +253,7 @@ function* run_test_2(generator) {
Assert.equal(do_count_cookies(), 0);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Check that the original database was renamed.
Assert.ok(do_get_backup_file(profile).exists());
@ -280,18 +266,16 @@ function* run_test_2(generator) {
Assert.equal(do_count_cookies(), 0);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Clean up.
do_get_cookie_file(profile).remove(false);
do_get_backup_file(profile).remove(false);
Assert.ok(!do_get_cookie_file(profile).exists());
Assert.ok(!do_get_backup_file(profile).exists());
do_run_generator(generator);
}
function* run_test_3(generator) {
async function run_test_3() {
// Set the maximum cookies per base domain limit to a large value, so that
// corrupting the database is easier.
Services.prefs.setIntPref("network.cookie.maxPerHost", 3000);
@ -299,27 +283,36 @@ function* run_test_3(generator) {
// Load the profile and populate it.
do_load_profile();
Services.cookies.runInTransaction(_ => {
let uri = NetUtil.newURI("http://hither.com/");
let channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
for (let i = 0; i < 10; ++i) {
let uri = NetUtil.newURI("http://hither.com/");
Services.cookies.setCookieString(
Services.cookies.setCookieStringFromHttp(
uri,
"oh" + i + "=hai; max-age=1000",
null
channel
);
}
uri = NetUtil.newURI("http://haithur.com/");
channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
for (let i = 10; i < 3000; ++i) {
let uri = NetUtil.newURI("http://haithur.com/");
Services.cookies.setCookieString(
Services.cookies.setCookieStringFromHttp(
uri,
"oh" + i + "=hai; max-age=1000",
null
channel
);
}
});
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Corrupt the database file.
let size = do_corrupt_db(do_get_cookie_file(profile));
@ -336,8 +329,8 @@ function* run_test_3(generator) {
Assert.equal(Services.cookiemgr.countCookiesFromHost("haithur.com"), 0);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
let db = Services.storage.openDatabase(do_get_cookie_file(profile));
Assert.equal(do_count_cookies_in_db(db, "hither.com"), 0);
Assert.equal(do_count_cookies_in_db(db), 0);
@ -359,8 +352,8 @@ function* run_test_3(generator) {
Assert.equal(do_count_cookies(), 0);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
db = Services.storage.openDatabase(do_get_cookie_file(profile));
Assert.equal(do_count_cookies_in_db(db), 0);
db.close();
@ -374,22 +367,30 @@ function* run_test_3(generator) {
do_get_backup_file(profile).remove(false);
Assert.ok(!do_get_cookie_file(profile).exists());
Assert.ok(!do_get_backup_file(profile).exists());
do_run_generator(generator);
}
function* run_test_4(generator) {
async function run_test_4() {
// Load the profile and populate it.
do_load_profile();
Services.cookies.runInTransaction(_ => {
let uri = NetUtil.newURI("http://foo.com/");
let channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
for (let i = 0; i < 3000; ++i) {
let uri = NetUtil.newURI("http://" + i + ".com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; max-age=1000",
channel
);
}
});
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Corrupt the database file.
let size = do_corrupt_db(do_get_cookie_file(profile));
@ -406,16 +407,22 @@ function* run_test_4(generator) {
// Queue up an INSERT for the same base domain. This should also go into
// memory and be written out during database rebuild.
let uri = NetUtil.newURI("http://0.com/");
Services.cookies.setCookieString(uri, "oh2=hai; max-age=1000", null);
const contentPage = await CookieXPCShellUtils.loadContentPage(
"http://0.com/"
);
await contentPage.spawn(
null,
// eslint-disable-next-line no-undef
() => (content.document.cookie = "oh2=hai; max-age=1000")
);
await contentPage.close();
// At this point, the cookies should still be in memory.
Assert.equal(Services.cookiemgr.countCookiesFromHost("0.com"), 1);
Assert.equal(do_count_cookies(), 1);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Check that the original database was renamed.
Assert.ok(do_get_backup_file(profile).exists());
@ -427,32 +434,42 @@ function* run_test_4(generator) {
Assert.equal(do_count_cookies(), 1);
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Clean up.
do_get_cookie_file(profile).remove(false);
do_get_backup_file(profile).remove(false);
Assert.ok(!do_get_cookie_file(profile).exists());
Assert.ok(!do_get_backup_file(profile).exists());
do_run_generator(generator);
}
function* run_test_5(generator) {
async function run_test_5() {
// Load the profile and populate it.
do_load_profile();
Services.cookies.runInTransaction(_ => {
let uri = NetUtil.newURI("http://bar.com/");
Services.cookies.setCookieString(uri, "oh=hai; path=/; max-age=1000", null);
const channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; path=/; max-age=1000",
channel
);
for (let i = 0; i < 3000; ++i) {
let uri = NetUtil.newURI("http://" + i + ".com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; max-age=1000",
channel
);
}
});
// Close the profile.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Corrupt the database file.
let size = do_corrupt_db(do_get_cookie_file(profile));
@ -490,13 +507,11 @@ function* run_test_5(generator) {
// Close the profile. We do not need to wait for completion, because the
// database has already been closed. Ensure the cookie file is unlocked.
do_close_profile(sub_generator);
yield;
await promise_close_profile();
// Clean up.
do_get_cookie_file(profile).remove(false);
do_get_backup_file(profile).remove(false);
Assert.ok(!do_get_cookie_file(profile).exists());
Assert.ok(!do_get_backup_file(profile).exists());
do_run_generator(generator);
}

View File

@ -7,24 +7,14 @@
"use strict";
var test_generator = do_run_test();
function run_test() {
do_test_pending();
test_generator.next();
}
function finish_test() {
executeSoon(function() {
test_generator.return();
do_test_finished();
});
}
function* do_run_test() {
add_task(async () => {
// Set up a profile.
let profile = do_get_profile();
CookieXPCShellUtils.createServer({
hosts: ["foo.com", "bar.com", "third.com"],
});
// Create URIs and channels pointing to foo.com and bar.com.
// We will use these to put foo.com into first and third party contexts.
var spec1 = "http://foo.com/foo.html";
@ -55,36 +45,32 @@ function* do_run_test() {
true
);
do_set_cookies(uri1, channel1, false, [1, 2, 3]);
do_set_cookies(uri2, channel2, true, [1, 2, 3]);
await do_set_cookies(uri1, channel1, false, [1, 2]);
await do_set_cookies(uri2, channel2, true, [1, 2]);
// fake a profile change
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 3);
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 2);
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
// Again, but don't wait for the async close to complete. This should always
// work, since we blocked on close above and haven't kicked off any writes
// since then.
do_close_profile();
await promise_close_profile();
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 3);
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 2);
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
// test with cookies set to session-only
Services.prefs.setIntPref("network.cookie.lifetimePolicy", 2);
Services.cookies.removeAll();
do_set_cookies(uri1, channel1, false, [1, 2, 3]);
do_set_cookies(uri2, channel2, true, [1, 2, 3]);
await do_set_cookies(uri1, channel1, false, [1, 2]);
await do_set_cookies(uri2, channel2, true, [1, 2]);
// fake a profile change
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 0);
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
finish_test();
}
});

View File

@ -50,7 +50,11 @@ function* do_run_test() {
});
// Set a cookie for host 1.
Services.cookies.setCookieString(uri1, "oh=hai; max-age=1000", null);
Services.cookies.setCookieStringFromHttp(
uri1,
"oh=hai; max-age=1000",
make_channel(uri1.spec)
);
Assert.equal(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
// Enter private browsing mode, set a cookie for host 2, and check the counts.
@ -62,7 +66,7 @@ function* do_run_test() {
chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel);
chan2.setPrivate(true);
Services.cookies.setCookieString(uri2, "oh=hai; max-age=1000", chan2);
Services.cookies.setCookieStringFromHttp(uri2, "oh=hai; max-age=1000", chan2);
Assert.equal(Services.cookiemgr.getCookieStringForPrincipal(principal1), "");
Assert.equal(
Services.cookiemgr.getCookieStringForPrincipal(principal2),
@ -74,7 +78,7 @@ function* do_run_test() {
Assert.equal(Services.cookiemgr.getCookieStringForPrincipal(principal1), "");
Assert.equal(Services.cookiemgr.getCookieStringForPrincipal(principal2), "");
Services.cookies.setCookieString(uri2, "oh=hai; max-age=1000", chan2);
Services.cookies.setCookieStringFromHttp(uri2, "oh=hai; max-age=1000", chan2);
Assert.equal(
Services.cookiemgr.getCookieStringForPrincipal(principal2),
"oh=hai"
@ -97,7 +101,7 @@ function* do_run_test() {
// Enter private browsing mode, set a cookie for host 2, and check the counts.
Assert.equal(Services.cookiemgr.getCookieStringForPrincipal(principal1), "");
Assert.equal(Services.cookiemgr.getCookieStringForPrincipal(principal2), "");
Services.cookies.setCookieString(uri2, "oh=hai; max-age=1000", chan2);
Services.cookies.setCookieStringFromHttp(uri2, "oh=hai; max-age=1000", chan2);
Assert.equal(
Services.cookiemgr.getCookieStringForPrincipal(principal2),
"oh=hai"

View File

@ -5,21 +5,7 @@
"use strict";
var test_generator = do_run_test();
function run_test() {
do_test_pending();
test_generator.next();
}
function finish_test() {
executeSoon(function() {
test_generator.return();
do_test_finished();
});
}
function* do_run_test() {
add_task(async () => {
// Set up a profile.
let profile = do_get_profile();
@ -33,6 +19,8 @@ function* do_run_test() {
// Start the cookieservice.
Services.cookies;
CookieXPCShellUtils.createServer({ hosts: ["foo.com"] });
// Set a cookie.
let uri = NetUtil.newURI("http://foo.com");
let channel = NetUtil.newChannel({
@ -46,7 +34,14 @@ function* do_run_test() {
{}
);
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
let contentPage = await CookieXPCShellUtils.loadContentPage(uri.spec);
await contentPage.spawn(
null,
// eslint-disable-next-line no-undef
() => (content.document.cookie = "oh=hai; max-age=1000")
);
await contentPage.close();
let cookies = Services.cookiemgr.cookies;
Assert.ok(cookies.length == 1);
let cookie = cookies[0];
@ -54,10 +49,17 @@ function* do_run_test() {
// Fire 'profile-before-change'.
do_close_profile();
let promise = new _promise_observer("cookie-db-closed");
// Check that the APIs behave appropriately.
Assert.equal(Services.cookies.getCookieStringForPrincipal(principal), "");
Assert.equal(Services.cookies.getCookieStringFromHttp(uri, channel), "");
Services.cookies.setCookieString(uri, "oh2=hai", null);
contentPage = await CookieXPCShellUtils.loadContentPage(uri.spec);
// eslint-disable-next-line no-undef
await contentPage.spawn(null, () => (content.document.cookie = "oh2=hai"));
await contentPage.close();
Services.cookies.setCookieStringFromHttp(uri, "oh3=hai", channel);
Assert.equal(Services.cookies.getCookieStringForPrincipal(principal), "");
@ -101,14 +103,11 @@ function* do_run_test() {
}, Cr.NS_ERROR_NOT_AVAILABLE);
// Wait for the database to finish closing.
new _observer(test_generator, "cookie-db-closed");
yield;
await promise;
// Load the profile and check that the API is available.
do_load_profile();
Assert.ok(
Services.cookiemgr.cookieExists(cookie.host, cookie.path, cookie.name, {})
);
finish_test();
}
});

View File

@ -5,28 +5,18 @@
"use strict";
var test_generator = do_run_test();
var CMAX = 1000; // # of cookies to create
function run_test() {
do_test_pending();
test_generator.next();
}
function finish_test() {
executeSoon(function() {
test_generator.return();
do_test_finished();
});
}
function* do_run_test() {
add_task(async () => {
// Set up a profile.
let profile = do_get_profile();
// Allow all cookies.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
);
// Start the cookieservice, to force creation of a database.
// Get the sessionCookies to join the initialization in cookie thread
@ -38,19 +28,26 @@ function* do_run_test() {
Assert.ok(do_get_cookie_file(profile).exists());
let db = new CookieDatabaseConnection(do_get_cookie_file(profile), 11);
let uri = NetUtil.newURI("http://foo.com/");
let channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
for (let i = 0; i < CMAX; ++i) {
let uri = NetUtil.newURI("http://" + i + ".com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; max-age=1000",
channel
);
}
Assert.equal(do_count_cookies(), CMAX);
// Wait until all CMAX cookies have been written out to the database.
while (do_count_cookies_in_db(db.db) < CMAX) {
executeSoon(function() {
do_run_generator(test_generator);
});
yield;
await new Promise(resolve => executeSoon(resolve));
}
// Check the WAL file size. We set it to 16 pages of 32k, which means it
@ -61,8 +58,7 @@ function* do_run_test() {
db.close();
// fake a profile change
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
// test a few random cookies
@ -82,8 +78,7 @@ function* do_run_test() {
}
// reload again, to make sure the additions were written correctly
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
// remove some of the cookies, in both reverse and forward order
@ -100,18 +95,15 @@ function* do_run_test() {
Assert.equal(do_count_cookies(), CMAX - 200);
// reload again, to make sure the removals were written correctly
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
// check the count
Assert.equal(do_count_cookies(), CMAX - 200);
// reload again, but wait for async read completion
do_close_profile(test_generator);
yield;
do_load_profile(test_generator);
yield;
await promise_close_profile();
await promise_load_profile();
// check that everything's precisely correct
Assert.equal(do_count_cookies(), CMAX - 200);
@ -119,6 +111,4 @@ function* do_run_test() {
let host = i.toString() + ".com";
Assert.equal(Services.cookiemgr.countCookiesFromHost(host), 1);
}
finish_test();
}
});

View File

@ -50,6 +50,10 @@ function* do_run_test() {
// Allow all cookies.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
);
// Get the cookie file and the backup file.
cookieFile = profile.clone();
@ -170,9 +174,19 @@ function* run_test_1(generator) {
// Create a garbage database file.
create_garbage_file(cookieFile);
// Load the profile and populate it.
let uri = NetUtil.newURI("http://foo.com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
const channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
// Load the profile and populate it.
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; max-age=1000",
channel
);
// Fake a profile change.
do_close_profile(sub_generator);
@ -198,7 +212,16 @@ function* run_test_2(generator) {
// Load the profile and populate it.
do_load_profile();
let uri = NetUtil.newURI("http://foo.com/");
Services.cookies.setCookieString(uri, "oh=hai; max-age=1000", null);
const channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
Services.cookies.setCookieStringFromHttp(
uri,
"oh=hai; max-age=1000",
channel
);
// Fake a profile change.
do_close_profile(sub_generator);

View File

@ -7,7 +7,7 @@
"use strict";
function run_test() {
add_task(async () => {
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
@ -18,6 +18,10 @@ function run_test() {
false
);
CookieXPCShellUtils.createServer({
hosts: ["foo.com", "bar.com", "third.com"],
});
// Create URIs and channels pointing to foo.com and bar.com.
// We will use these to put foo.com into first and third party contexts.
let spec1 = "http://foo.com/foo.html";
@ -41,9 +45,9 @@ function run_test() {
loadUsingSystemPrincipal: true,
});
do_set_cookies(uri1, channel1, true, [1, 2, 3]);
await do_set_cookies(uri1, channel1, true, [1, 2]);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, true, [1, 2, 3]);
await do_set_cookies(uri1, channel2, true, [1, 2]);
Services.cookies.removeAll();
}
@ -63,9 +67,9 @@ function run_test() {
loadUsingSystemPrincipal: true,
});
do_set_cookies(uri1, channel1, true, [0, 0, 0]);
await do_set_cookies(uri1, channel1, true, [0, 0]);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, true, [0, 0, 0]);
await do_set_cookies(uri1, channel2, true, [0, 0]);
Services.cookies.removeAll();
}
@ -94,9 +98,9 @@ function run_test() {
let httpchannel2 = channel2.QueryInterface(Ci.nsIHttpChannelInternal);
httpchannel2.forceAllowThirdPartyCookie = true;
do_set_cookies(uri1, channel1, true, [1, 2, 3]);
await do_set_cookies(uri1, channel1, true, [1, 2]);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, true, [1, 2, 3]);
await do_set_cookies(uri1, channel2, true, [1, 2]);
Services.cookies.removeAll();
}
@ -121,9 +125,9 @@ function run_test() {
let httpchannel2 = channel2.QueryInterface(Ci.nsIHttpChannelInternal);
httpchannel2.forceAllowThirdPartyCookie = true;
do_set_cookies(uri1, channel1, true, [0, 1, 2]);
await do_set_cookies(uri1, channel1, true, [0, 1]);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, true, [0, 0, 0]);
await do_set_cookies(uri1, channel2, true, [0, 0]);
Services.cookies.removeAll();
}
@ -148,12 +152,12 @@ function run_test() {
let httpchannel2 = channel2.QueryInterface(Ci.nsIHttpChannelInternal);
httpchannel2.forceAllowThirdPartyCookie = true;
do_set_cookies(uri1, channel1, true, [0, 1, 2]);
await do_set_cookies(uri1, channel1, true, [0, 1]);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, true, [0, 0, 0]);
await do_set_cookies(uri1, channel2, true, [0, 0]);
Services.cookies.removeAll();
do_set_single_http_cookie(uri1, channel1, 1);
do_set_cookies(uri1, channel2, true, [2, 3, 4]);
await do_set_cookies(uri1, channel2, true, [1, 2]);
Services.cookies.removeAll();
}
}
});

View File

@ -1,89 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// test nonsecure third party persistence across sessions, for the cases:
// 1) network.cookie.thirdparty.nonsecureSessionOnly = false
// 2) network.cookie.thirdparty.nonsecureSessionOnly = true
"use strict";
var test_generator = do_run_test();
function run_test() {
do_test_pending();
test_generator.next();
}
function finish_test() {
executeSoon(function() {
test_generator.return();
do_test_finished();
});
}
function* do_run_test() {
// Set up a profile.
let profile = do_get_profile();
// We don't want to have CookieJarSettings blocking this test.
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
);
// Create URIs and channels pointing to foo.com and bar.com.
// We will use these to put foo.com into first and third party contexts.
var spec1 = "http://foo.com/foo.html";
var spec2 = "https://bar.com/bar.html";
var uri1 = NetUtil.newURI(spec1);
var uri2 = NetUtil.newURI(spec2);
var channel1 = NetUtil.newChannel({
uri: uri1,
loadUsingSystemPrincipal: true,
});
var channel2 = NetUtil.newChannel({
uri: uri2,
loadUsingSystemPrincipal: true,
});
// Force the channel URI to be used when determining the originating URI of
// the channel.
var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
var httpchannel2 = channel2.QueryInterface(Ci.nsIHttpChannelInternal);
httpchannel1.forceAllowThirdPartyCookie = true;
httpchannel2.forceAllowThirdPartyCookie = true;
// test with cookies enabled and nonsecure third party cookies persistent.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref(
"network.cookie.thirdparty.nonsecureSessionOnly",
false
);
do_set_cookies(uri1, channel2, false, [1, 2, 3]); // third-party HTTP
do_set_cookies(uri2, channel1, false, [1, 2, 3]); // third-party HTTPS
// fake a profile change
do_close_profile(test_generator);
yield;
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 3); // HTTP cookies OK
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 3); // HTTPS cookies OK
// test with nonsecure third party cookies for session only.
Services.prefs.setBoolPref(
"network.cookie.thirdparty.nonsecureSessionOnly",
true
);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, false, [1, 2, 3]); // third-party HTTP
do_set_cookies(uri2, channel1, false, [1, 2, 3]); // third-party HTTPS
// fake a profile change
do_close_profile(test_generator);
yield;
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 0); // no HTTP cookies!
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 3); // HTTPS cookies OK
finish_test();
}

View File

@ -7,21 +7,7 @@
"use strict";
var test_generator = do_run_test();
function run_test() {
do_test_pending();
test_generator.next();
}
function finish_test() {
executeSoon(function() {
test_generator.return();
do_test_finished();
});
}
function* do_run_test() {
add_task(async () => {
// Set up a profile.
let profile = do_get_profile();
@ -31,6 +17,10 @@ function* do_run_test() {
true
);
CookieXPCShellUtils.createServer({
hosts: ["foo.com", "bar.com", "third.com"],
});
// Create URIs and channels pointing to foo.com and bar.com.
// We will use these to put foo.com into first and third party contexts.
var spec1 = "http://foo.com/foo.html";
@ -56,28 +46,26 @@ function* do_run_test() {
// test with cookies enabled, and third party cookies persistent.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
do_set_cookies(uri1, channel2, false, [1, 2, 3]);
do_set_cookies(uri2, channel1, true, [1, 2, 3]);
await do_set_cookies(uri1, channel2, false, [1, 2]);
await do_set_cookies(uri2, channel1, true, [1, 2]);
// fake a profile change
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 3);
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 2);
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
// test with third party cookies for session only.
Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", true);
Services.cookies.removeAll();
do_set_cookies(uri1, channel2, false, [1, 2, 3]);
do_set_cookies(uri2, channel1, true, [1, 2, 3]);
await do_set_cookies(uri1, channel2, false, [1, 2]);
await do_set_cookies(uri2, channel1, true, [1, 2]);
// fake a profile change
do_close_profile(test_generator);
yield;
await promise_close_profile();
do_load_profile();
Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 0);
Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
finish_test();
}
});

View File

@ -36,9 +36,9 @@ function run_test() {
let uri = NetUtil.newURI("http://foo.com/");
let publicChan = makeChan(uri, false);
let svc = Services.cookies.QueryInterface(Ci.nsICookieService);
svc.setCookieString(uri, "oh=hai", publicChan);
svc.setCookieStringFromHttp(uri, "oh=hai", publicChan);
let privateChan = makeChan(uri, true);
svc.setCookieString(uri, "oh=hai", privateChan);
svc.setCookieStringFromHttp(uri, "oh=hai", privateChan);
Assert.equal(publicNotifications, 1);
Assert.equal(privateNotifications, 1);
}

View File

@ -265,7 +265,7 @@ function socketAccepted(socket, transport) {
}
function stopListening(socket, status) {
if (do_throw) {
if (tests && tests.length !== 0 && do_throw) {
do_throw("should never stop");
}
}

View File

@ -192,7 +192,6 @@ skip-if = true # Bug 863738
[test_cookies_read.js]
[test_cookies_sync_failure.js]
[test_cookies_thirdparty.js]
[test_cookies_thirdparty_nonsecure_session.js]
[test_cookies_thirdparty_session.js]
[test_cookies_upgrade_10-11.js]
[test_dns_cancel.js]

View File

@ -1,10 +0,0 @@
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
function run_test() {
Services.prefs.setBoolPref(
"network.cookieJarSettings.unblocked_for_testing",
true
);
Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
run_test_in_child("../unit/test_bug528292.js");
}

View File

@ -63,7 +63,6 @@ support-files =
child_cookie_header.js
child_esni_dns_fetch1.js
[test_bug528292_wrap.js]
[test_cookie_header_stripped.js]
[test_cacheflags_wrap.js]
[test_cache-entry-id_wrap.js]

View File

@ -847,7 +847,12 @@ add_task(async function test_not_sending_cookie() {
Ci.nsICookieService
);
let uri = CommonUtils.makeURI(server.baseURI);
cookieSer.setCookieString(uri, "test=test; path=/;", null);
let channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
cookieSer.setCookieStringFromHttp(uri, "test=test; path=/;", channel);
let res = new RESTRequest(server.baseURI + "/test");
let response = await res.get();

View File

@ -968,7 +968,12 @@ add_task(async function testCookies() {
PingServer.clearRequests();
let uri = Services.io.newURI("http://localhost:" + PingServer.port);
Services.cookies.setCookieString(uri, "cookie-time=yes", null);
let channel = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
Services.cookies.setCookieStringFromHttp(uri, "cookie-time=yes", channel);
const id = await TelemetryController.submitExternalPing(TEST_TYPE, {});
let foundit = false;