test_cookies_purge_counting_per_host.js (2452B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 add_setup(function test_setup() { 5 // FOG needs a profile directory to put its data in. 6 do_get_profile(); 7 8 // FOG needs to be initialized in order for data to flow. 9 Services.fog.initializeFOG(); 10 }); 11 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()); 16 17 let schema12db = new CookieDatabaseConnection(dbFile, 12); 18 19 let now = Date.now() * 1000; // date in microseconds 20 let pastExpiry = Math.round(now / 1e3 - 1000); 21 let futureExpiry = Math.round(now / 1e3 + 1000); 22 23 let host = "cookie-host1.com"; 24 25 let cookieCountMax = 180; 26 let cookieCountPurgeTo = 150; 27 let cookiesPurged = cookieCountMax - cookieCountPurgeTo + 1; 28 29 // add many expired cookies for a single host 30 for (let i = 0; i < cookieCountMax; 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 42 ); 43 schema12db.insertCookie(cookie); 44 } 45 46 // check that the cookies were added to the db 47 Assert.equal(do_count_cookies_in_db(schema12db.db), cookieCountMax); 48 Assert.equal( 49 do_count_cookies_in_db(schema12db.db, "cookie-host1.com"), 50 cookieCountMax 51 ); 52 53 // startup the cookie service and check the cookie count 54 let validCookies = Services.cookies.countCookiesFromHost(host); // includes expired cookies 55 Assert.equal(validCookies, cookieCountMax); 56 57 // add a cookie - this will trigger the purge 58 const cv = 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_UNSET, // SameSite 69 Ci.nsICookie.SCHEME_HTTPS 70 ); 71 Assert.equal(cv.result, Ci.nsICookieValidation.eOK, "Valid cookie"); 72 73 // check that we purge down to the cookieMax (plus the cookie added) 74 validCookies = Services.cookies.countCookiesFromHost(host); 75 Assert.equal(validCookies, cookieCountPurgeTo); 76 77 // check that the telemetry fired 78 let cpem = await Glean.networking.cookiePurgeEntryMax.testGetValue(); 79 Assert.equal(cpem.sum, cookiesPurged, "Purge the expected number of cookies"); 80 81 schema12db.close(); 82 });