test_timestamp_fixup.js (3867B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { AppConstants } = ChromeUtils.importESModule( 5 "resource://gre/modules/AppConstants.sys.mjs" 6 ); 7 8 const USEC_PER_SEC = 1000 * 1000; 9 const ONE_DAY = 60 * 60 * 24 * USEC_PER_SEC; 10 const ONE_YEAR = ONE_DAY * 365; 11 const LAST_ACCESSED_DIFF = 10 * ONE_YEAR; 12 const CREATION_DIFF = 100 * ONE_YEAR; 13 14 function initDB(conn, now) { 15 // Write the schema v7 to the database. 16 conn.schemaVersion = 7; 17 conn.executeSimpleSQL( 18 "CREATE TABLE moz_cookies (" + 19 "id INTEGER PRIMARY KEY, " + 20 "baseDomain TEXT, " + 21 "originAttributes TEXT NOT NULL DEFAULT '', " + 22 "name TEXT, " + 23 "value TEXT, " + 24 "host TEXT, " + 25 "path TEXT, " + 26 "expiry INTEGER, " + 27 "lastAccessed INTEGER, " + 28 "creationTime INTEGER, " + 29 "isSecure INTEGER, " + 30 "isHttpOnly INTEGER, " + 31 "appId INTEGER DEFAULT 0, " + 32 "inBrowserElement INTEGER DEFAULT 0, " + 33 "CONSTRAINT moz_uniqueid UNIQUE (name, host, path, originAttributes)" + 34 ")" 35 ); 36 conn.executeSimpleSQL( 37 "CREATE INDEX moz_basedomain ON moz_cookies (baseDomain, " + 38 "originAttributes)" 39 ); 40 41 conn.executeSimpleSQL("PRAGMA synchronous = OFF"); 42 conn.executeSimpleSQL("PRAGMA journal_mode = WAL"); 43 conn.executeSimpleSQL("PRAGMA wal_autocheckpoint = 16"); 44 45 conn.executeSimpleSQL( 46 `INSERT INTO moz_cookies(baseDomain, host, name, value, path, expiry, lastAccessed, creationTime, isSecure, isHttpOnly) 47 VALUES ('foo.com', '.foo.com', 'foo', 'bar=baz', '/', 48 ${now + ONE_DAY}, ${now + LAST_ACCESSED_DIFF} , ${ 49 now + CREATION_DIFF 50 } , 1, 1)` 51 ); 52 } 53 54 add_task(async function test_timestamp_fixup() { 55 let now = Date.now() * 1000; // date in microseconds 56 Services.prefs.setBoolPref("network.cookie.fixup_on_db_load", true); 57 do_get_profile(); 58 let dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile); 59 dbFile.append("cookies.sqlite"); 60 let conn = Services.storage.openDatabase(dbFile); 61 initDB(conn, now); 62 63 if (AppConstants.platform != "android") { 64 Services.fog.initializeFOG(); 65 } 66 Services.fog.testResetFOG(); 67 68 // Now start the cookie service, and then check the fields in the table. 69 // Get sessionCookies to wait for the initialization in cookie thread 70 Assert.lessOrEqual( 71 Math.floor(Services.cookies.cookies[0].creationTime / 1000), 72 now 73 ); 74 Assert.greaterOrEqual(conn.schemaVersion, 13); 75 76 Assert.equal( 77 await Glean.networking.cookieTimestampFixedCount.creationTime.testGetValue(), 78 1, 79 "One fixup of creation time" 80 ); 81 Assert.equal( 82 await Glean.networking.cookieTimestampFixedCount.lastAccessed.testGetValue(), 83 1, 84 "One fixup of lastAccessed" 85 ); 86 { 87 let { values } = 88 await Glean.networking.cookieCreationFixupDiff.testGetValue(); 89 info(JSON.stringify(values)); 90 let keys = Object.keys(values).splice(-2, 2); 91 Assert.equal(keys.length, 1, "There should be one entry in telemetry"); 92 Assert.equal(values[keys[0]], 1, "First entry should have value 1"); 93 const creationDiffInSeconds = CREATION_DIFF / USEC_PER_SEC; 94 Assert.lessOrEqual( 95 parseInt(keys[0]), 96 creationDiffInSeconds, 97 "The bucket should be smaller than time diff" 98 ); 99 } 100 101 { 102 let { values } = 103 await Glean.networking.cookieAccessFixupDiff.testGetValue(); 104 info(JSON.stringify(values)); 105 let keys = Object.keys(values).splice(-2, 2); 106 Assert.equal(keys.length, 1, "There should be one entry in telemetry"); 107 Assert.equal(values[keys[0]], 1, "First entry should have value 1"); 108 info(now); 109 const lastAccessedDiffInSeconds = LAST_ACCESSED_DIFF / USEC_PER_SEC; 110 Assert.lessOrEqual( 111 parseInt(keys[0]), 112 lastAccessedDiffInSeconds, 113 "The bucket should be smaller than time diff" 114 ); 115 } 116 117 conn.close(); 118 });