Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_cookies_purge_counting_per_host.js
blob83a10c75ac6d7ee999593141eef912b5be7723c2
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 add_setup(function test_setup() {
5 // FOG needs a profile directory to put its data in.
6 do_get_profile();
8 // FOG needs to be initialized in order for data to flow.
9 Services.fog.initializeFOG();
10 });
12 add_task(async function test_purge_counting_per_host() {
13 let profile = do_get_profile();
14 let dbFile = do_get_cookie_file(profile);
15 Assert.ok(!dbFile.exists());
17 let schema12db = new CookieDatabaseConnection(dbFile, 12);
19 let now = Date.now() * 1000; // date in microseconds
20 let pastExpiry = Math.round(now / 1e6 - 1000);
21 let futureExpiry = Math.round(now / 1e6 + 1000);
23 let host = "cookie-host1.com";
25 let manyCookies = 180;
26 let cookieMax = 150;
27 let cookiesPurged = manyCookies - cookieMax;
29 // add many expired cookies for a single host
30 for (let i = 0; i < manyCookies; i++) {
31 let cookie = new Cookie(
32 "cookie-name" + i,
33 "cookie-value" + i,
34 host,
35 "/", // path
36 pastExpiry,
37 now, // last accessed
38 now, // creation time
39 false,
40 false,
41 false
43 schema12db.insertCookie(cookie);
46 // check that the cookies were added to the db
47 Assert.equal(do_count_cookies_in_db(schema12db.db), manyCookies);
48 Assert.equal(
49 do_count_cookies_in_db(schema12db.db, "cookie-host1.com"),
50 manyCookies
53 // startup the cookie service and check the cookie count
54 let validCookies = Services.cookies.countCookiesFromHost(host); // includes expired cookies
55 Assert.equal(validCookies, manyCookies);
57 // add a cookie - this will trigger the purge
58 Services.cookies.add(
59 host,
60 "/", // path
61 "cookie-name-x",
62 "cookie-value-x",
63 false, // secure
64 true, // http-only
65 true, // isSession
66 futureExpiry,
67 {}, // OA
68 Ci.nsICookie.SAMESITE_NONE, // SameSite
69 Ci.nsICookie.SCHEME_HTTPS
72 // check that we purge down to the cookieMax (plus the cookie added)
73 validCookies = Services.cookies.countCookiesFromHost(host);
74 Assert.equal(validCookies, cookieMax + 1);
76 // check that the telemetry fired
77 let cpem = await Glean.networking.cookiePurgeEntryMax.testGetValue();
78 Assert.equal(cpem.sum, cookiesPurged, "Purge the expected number of cookies");
80 schema12db.close();
81 });