gecko-dev/netwerk/cookie/test/unit/test_eviction.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

145 lines
5.4 KiB
JavaScript

const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const BASE_HOSTNAMES = ["example.org", "example.co.uk"];
const SUBDOMAINS = ["", "pub.", "www.", "other."];
const cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
const cm = cs.QueryInterface(Ci.nsICookieManager);
function run_test() {
Services.prefs.setIntPref("network.cookie.staleThreshold", 0);
add_task(async function() {
await test_basic_eviction("example.org");
cm.removeAll();
});
run_next_test();
}
async function test_basic_eviction(base_host) {
Services.prefs.setIntPref("network.cookie.quotaPerHost", 2);
Services.prefs.setIntPref("network.cookie.maxPerHost", 5);
const BASE_URI = Services.io.newURI("http://" + base_host);
const FOO_PATH = Services.io.newURI("http://" + base_host + "/foo/");
const BAR_PATH = Services.io.newURI("http://" + base_host + "/bar/");
await setCookie("session_foo_path_1", null, "/foo", null, FOO_PATH);
await setCookie("session_foo_path_2", null, "/foo", null, FOO_PATH);
await setCookie("session_foo_path_3", null, "/foo", null, FOO_PATH);
await setCookie("session_foo_path_4", null, "/foo", null, FOO_PATH);
await setCookie("session_foo_path_5", null, "/foo", null, FOO_PATH);
verifyCookies(["session_foo_path_1",
"session_foo_path_2",
"session_foo_path_3",
"session_foo_path_4",
"session_foo_path_5"], BASE_URI);
// Check if cookies are evicted by creation time.
await setCookie("session_foo_path_6", null, "/foo", null, FOO_PATH);
verifyCookies(["session_foo_path_4",
"session_foo_path_5",
"session_foo_path_6"], BASE_URI);
await setCookie("session_bar_path_1", null, "/bar", null, BAR_PATH);
await setCookie("session_bar_path_2", null, "/bar", null, BAR_PATH);
verifyCookies(["session_foo_path_4",
"session_foo_path_5",
"session_foo_path_6",
"session_bar_path_1",
"session_bar_path_2"], BASE_URI);
// Check if cookies are evicted by last accessed time.
cs.getCookieString(FOO_PATH, null);
await setCookie("session_foo_path_7", null, "/foo", null, FOO_PATH);
verifyCookies(["session_foo_path_5",
"session_foo_path_6",
"session_foo_path_7"], BASE_URI);
const EXPIRED_TIME = 3;
await setCookie("non_session_expired_foo_path_1", null, "/foo", EXPIRED_TIME, FOO_PATH);
await setCookie("non_session_expired_foo_path_2", null, "/foo", EXPIRED_TIME, FOO_PATH);
verifyCookies(["session_foo_path_5",
"session_foo_path_6",
"session_foo_path_7",
"non_session_expired_foo_path_1",
"non_session_expired_foo_path_2"], BASE_URI);
// Check if expired cookies are evicted first.
await new Promise(resolve => do_timeout(EXPIRED_TIME * 1000, resolve));
await setCookie("session_foo_path_8", null, "/foo", null, FOO_PATH);
verifyCookies(["session_foo_path_6",
"session_foo_path_7",
"session_foo_path_8"], BASE_URI);
}
// Verify that the given cookie names exist, and are ordered from least to most recently accessed
function verifyCookies(names, uri) {
Assert.equal(cm.countCookiesFromHost(uri.host), names.length);
let actual_cookies = [];
for (let cookie of cm.getCookiesFromHost(uri.host, {})) {
actual_cookies.push(cookie);
}
if (names.length != actual_cookies.length) {
let left = names.filter(function(n) {
return actual_cookies.findIndex(function(c) {
return c.name == n;
}) == -1;
});
let right = actual_cookies.filter(function(c) {
return names.findIndex(function(n) {
return c.name == n;
}) == -1;
}).map(function(c) { return c.name; });
if (left.length) {
info("unexpected cookies: " + left);
}
if (right.length) {
info("expected cookies: " + right);
}
}
Assert.equal(names.length, actual_cookies.length);
actual_cookies.sort(function(a, b) {
if (a.lastAccessed < b.lastAccessed)
return -1;
if (a.lastAccessed > b.lastAccessed)
return 1;
return 0;
});
for (var i = 0; i < names.length; i++) {
Assert.equal(names[i], actual_cookies[i].name);
Assert.equal(names[i].startsWith("session"), actual_cookies[i].isSession);
}
}
var lastValue = 0;
function setCookie(name, domain, path, maxAge, url) {
let value = name + "=" + ++lastValue;
var s = "setting cookie " + value;
if (domain) {
value += "; Domain=" + domain;
s += " (d=" + domain + ")";
}
if (path) {
value += "; Path=" + path;
s += " (p=" + path + ")";
}
if (maxAge) {
value += "; Max-Age=" + maxAge;
s += " (non-session)";
} else {
s += " (session)";
}
s += " for " + url.spec;
info(s);
cs.setCookieStringFromHttp(url, null, null, value, null, null);
return new Promise(function(resolve) {
// Windows XP has low precision timestamps that cause our cookie eviction
// algorithm to produce different results from other platforms. We work around
// this by ensuring that there's a clear gap between each cookie update.
do_timeout(10, resolve);
});
}