Bug 1334587 - Fix cookie store removal and updating to use originAttributes. r=baku

MozReview-Commit-ID: HpdlT4gxrl0

--HG--
extra : rebase_source : e34967207ee07f30f236402a24212eeb810c6b1b
This commit is contained in:
Jonathan Kingston 2017-04-16 17:58:53 +01:00
parent 9e10eadecb
commit d9b9a81f4e
3 changed files with 129 additions and 2 deletions

View File

@ -12,6 +12,7 @@ support-files =
[browser_favicon.js] [browser_favicon.js]
[browser_forgetaboutsite.js] [browser_forgetaboutsite.js]
[browser_forgetAPI_cookie_getCookiesWithOriginAttributes.js] [browser_forgetAPI_cookie_getCookiesWithOriginAttributes.js]
[browser_restore_getCookiesWithOriginAttributes.js]
[browser_forgetAPI_EME_forgetThisSite.js] [browser_forgetAPI_EME_forgetThisSite.js]
[browser_forgetAPI_quota_clearStoragesForPrincipal.js] [browser_forgetAPI_quota_clearStoragesForPrincipal.js]
[browser_newtabButton.js] [browser_newtabButton.js]

View File

@ -0,0 +1,114 @@
/*
* Bug 1334587 - A Test case for checking whether forgetting APIs are working for cookies.
*/
const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu } = Components;
const TEST_HOST = "example.com";
const TEST_URL = "http://" + TEST_HOST + "/browser/browser/components/contextualidentity/test/browser/";
const USER_CONTEXTS = [
"default",
"personal",
"work"
];
const DELETE_CONTEXT = 1;
const COOKIE_NAME = "userContextId";
//
// Support functions.
//
function* openTabInUserContext(uri, userContextId) {
// Open the tab in the correct userContextId.
let tab = gBrowser.addTab(uri, {userContextId});
// Select tab and make sure its browser is focused.
gBrowser.selectedTab = tab;
tab.ownerGlobal.focus();
let browser = gBrowser.getBrowserForTab(tab);
yield BrowserTestUtils.browserLoaded(browser);
return {tab, browser};
}
function getCookiesForOA(host, userContextId) {
return Services.cookies.getCookiesFromHost(host, {userContextId});
}
//
// Test functions.
//
add_task(function* setup() {
// Make sure userContext is enabled.
yield SpecialPowers.pushPrefEnv({"set": [
[ "privacy.userContext.enabled", true ],
]});
});
function checkCookies(ignoreContext = null) {
for (let userContextId of Object.keys(USER_CONTEXTS)) {
if (ignoreContext && userContextId === String(ignoreContext)) {
continue;
}
let enumerator = getCookiesForOA(TEST_HOST, userContextId);
ok(enumerator.hasMoreElements(), "Cookies available");
let foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
is(foundCookie["name"], COOKIE_NAME, "Check cookie name");
is(foundCookie["value"], USER_CONTEXTS[userContextId], "Check cookie value");
}
}
function deleteCookies(onlyContext = null) {
// Using getCookiesWithOriginAttributes() to get all cookies for a certain
// domain by using the originAttributes pattern, and clear all these cookies.
let enumerator = Services.cookies.getCookiesWithOriginAttributes(JSON.stringify({}), TEST_HOST);
while (enumerator.hasMoreElements()) {
let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie);
if (!onlyContext || cookie.originAttributes.userContextId == onlyContext) {
Services.cookies.remove(cookie.host, cookie.name, cookie.path, false, cookie.originAttributes);
}
}
}
add_task(function* test_cookie_getCookiesWithOriginAttributes() {
let tabs = [];
for (let userContextId of Object.keys(USER_CONTEXTS)) {
// Load the page in different contexts and set a cookie
// which should only be visible in that context.
let value = USER_CONTEXTS[userContextId];
// Open our tab in the given user context.
tabs[userContextId] = yield* openTabInUserContext(TEST_URL + "file_reflect_cookie_into_title.html?" + value, userContextId);
// Close this tab.
yield BrowserTestUtils.removeTab(tabs[userContextId].tab);
}
// Check that cookies have been set properly.
for (let userContextId of Object.keys(USER_CONTEXTS)) {
let enumerator = getCookiesForOA(TEST_HOST, userContextId);
ok(enumerator.hasMoreElements(), "Cookies available");
let foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
is(foundCookie["name"], COOKIE_NAME, "Check cookie name");
is(foundCookie["value"], USER_CONTEXTS[userContextId], "Check cookie value");
}
checkCookies();
deleteCookies(DELETE_CONTEXT);
checkCookies(DELETE_CONTEXT);
deleteCookies();
// Check that whether cookies has been cleared.
for (let userContextId of Object.keys(USER_CONTEXTS)) {
let e = getCookiesForOA(TEST_HOST, userContextId);
ok(!e.hasMoreElements(), "No Cookie should be here");
}
});

View File

@ -1517,13 +1517,13 @@ nsCookieService::TryInitDB(bool aRecreateDB)
rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING( rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
"DELETE FROM moz_cookies " "DELETE FROM moz_cookies "
"WHERE name = :name AND host = :host AND path = :path"), "WHERE name = :name AND host = :host AND path = :path AND originAttributes = :originAttributes"),
getter_AddRefs(mDefaultDBState->stmtDelete)); getter_AddRefs(mDefaultDBState->stmtDelete));
NS_ENSURE_SUCCESS(rv, RESULT_RETRY); NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING( rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
"UPDATE moz_cookies SET lastAccessed = :lastAccessed " "UPDATE moz_cookies SET lastAccessed = :lastAccessed "
"WHERE name = :name AND host = :host AND path = :path"), "WHERE name = :name AND host = :host AND path = :path AND originAttributes = :originAttributes"),
getter_AddRefs(mDefaultDBState->stmtUpdate)); getter_AddRefs(mDefaultDBState->stmtUpdate));
NS_ENSURE_SUCCESS(rv, RESULT_RETRY); NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
@ -5004,6 +5004,12 @@ nsCookieService::RemoveCookieFromList(const nsListIter &aIter,
aIter.Cookie()->Path()); aIter.Cookie()->Path());
NS_ASSERT_SUCCESS(rv); NS_ASSERT_SUCCESS(rv);
nsAutoCString suffix;
aIter.Cookie()->OriginAttributesRef().CreateSuffix(suffix);
rv = params->BindUTF8StringByName(
NS_LITERAL_CSTRING("originAttributes"), suffix);
NS_ASSERT_SUCCESS(rv);
rv = paramsArray->AddParams(params); rv = paramsArray->AddParams(params);
NS_ASSERT_SUCCESS(rv); NS_ASSERT_SUCCESS(rv);
@ -5183,6 +5189,12 @@ nsCookieService::UpdateCookieInList(nsCookie *aCookie,
aCookie->Path()); aCookie->Path());
NS_ASSERT_SUCCESS(rv); NS_ASSERT_SUCCESS(rv);
nsAutoCString suffix;
aCookie->OriginAttributesRef().CreateSuffix(suffix);
rv = params->BindUTF8StringByName(
NS_LITERAL_CSTRING("originAttributes"), suffix);
NS_ASSERT_SUCCESS(rv);
// Add our bound parameters to the array. // Add our bound parameters to the array.
rv = aParamsArray->AddParams(params); rv = aParamsArray->AddParams(params);
NS_ASSERT_SUCCESS(rv); NS_ASSERT_SUCCESS(rv);