test_cookies_partition_counting.js (5499B)
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() { 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 let now = Date.now() * 1000; // date in microseconds 19 20 // add some non-partitioned cookies for key 21 let cookieNum1 = 1; 22 let hostNonPartitioned = "cookie-host-non-partitioned.com"; 23 for (let i = 0; i < cookieNum1; i++) { 24 let cookie = new Cookie( 25 "cookie-name" + i, 26 "cookie-value" + i, 27 hostNonPartitioned, 28 "/", // path 29 now, // expiry 30 now, // needed to get the cookie by the db init 31 now, // creation time 32 false, // session 33 false, // secure 34 false, // http-only 35 false, // inBrowserElement 36 {} // OA 37 ); 38 schema12db.insertCookie(cookie); 39 } 40 41 // add some non-partitioned cookies different key 42 let cookieNum2 = 10; 43 for (let i = 0; i < cookieNum2; i++) { 44 let cookie = new Cookie( 45 "cookie-name" + i, 46 "cookie-value" + i, 47 hostNonPartitioned, 48 "/", // path 49 now, // expiry 50 now, // needed to get the cookie by the db init 51 now, // creation time 52 false, // session 53 false, // secure 54 false, // http-only 55 false, // inBrowserElement 56 { userContextId: 8 } // OA 57 ); 58 schema12db.insertCookie(cookie); 59 } 60 61 // add some partitioned cookies 62 let hostPartitioned = "host-partitioned.com"; 63 let cookieNum3 = 3; 64 for (let i = 0; i < cookieNum3; i++) { 65 let cookie = new Cookie( 66 "cookie-name" + i, 67 "cookie-value" + i, 68 hostPartitioned, 69 "/", // path 70 now, // expiry 71 now, // needed to get the cookie by the db init 72 now, // creation time 73 false, // session 74 false, // secure 75 false, // http-only 76 false, // inBrowserElement 77 { partitionKey: "(https,example.com)" } 78 ); 79 schema12db.insertCookie(cookie); 80 } 81 82 // add some partitioned with different OA 83 let cookieNum4 = 35; 84 for (let i = 0; i < cookieNum4; i++) { 85 let cookie = new Cookie( 86 "cookie-name" + i, 87 "cookie-value" + i, 88 hostPartitioned, 89 "/", // path 90 now, // expiry 91 now, // needed to get the cookie by the db init 92 now, // creation time 93 false, // session 94 false, // secure 95 false, // http-only 96 false, // inBrowserElement 97 { partitionKey: "(https,example.com)", userContextId: 7 } 98 ); 99 schema12db.insertCookie(cookie); 100 } 101 102 let allCookieCount = cookieNum1 + cookieNum2 + cookieNum3 + cookieNum4; 103 Assert.equal(do_count_cookies_in_db(schema12db.db), allCookieCount); 104 105 // startup the cookie service and check the cookie counts 106 let cookieCountNonPart = 107 Services.cookies.countCookiesFromHost(hostNonPartitioned); // includes expired cookies 108 Assert.equal(cookieCountNonPart, cookieNum1); 109 let cookieCountNonPartOA = Services.cookies.getCookiesFromHost( 110 hostNonPartitioned, 111 { userContextId: 8 } 112 ).length; // includes expired cookies 113 Assert.equal(cookieCountNonPartOA, cookieNum2); 114 let cookieCountPart = Services.cookies.getCookiesFromHost(hostPartitioned, { 115 partitionKey: "(https,example.com)", 116 }).length; // includes expired cookies 117 Assert.equal(cookieCountPart, cookieNum3); 118 let cookieCountPartOA = Services.cookies.getCookiesFromHost(hostPartitioned, { 119 partitionKey: "(https,example.com)", 120 userContextId: 7, 121 }).length; // includes expired cookies 122 Assert.equal(cookieCountPartOA, cookieNum4); 123 124 // trigger the collection 125 Services.obs.notifyObservers(null, "idle-daily"); 126 127 // check telem fired for all cookie count 128 let cct = await Glean.networking.cookieCountTotal.testGetValue(); 129 Assert.equal(cct.sum, allCookieCount, "All cookies telem"); 130 131 // check telem for all un/partitioned counts 132 let ccp = await Glean.networking.cookieCountPartitioned.testGetValue(); 133 Assert.equal( 134 ccp.sum, 135 cookieNum3 + cookieNum4, 136 "All partitioned cookies telem" 137 ); 138 139 let ccu = await Glean.networking.cookieCountUnpartitioned.testGetValue(); 140 Assert.equal( 141 ccu.sum, 142 cookieNum1 + cookieNum2, 143 "All unpartitioned cookies telem" 144 ); 145 146 // check telem for part by key (host+OA) 147 // Note: With the decided histogram layout we see the buckets 148 // (used for indexing the histogram's buckets) 149 let histPartByKey = 150 await Glean.networking.cookieCountPartByKey.testGetValue(); 151 Assert.equal(histPartByKey.values[2], 1, "Partitioned bucket 2 has 1 value"); 152 Assert.equal( 153 histPartByKey.values[31], 154 1, 155 "Partitioned bucket 31 has 1 value" 156 ); 157 Assert.equal( 158 histPartByKey.sum, 159 cookieNum3 + cookieNum4, 160 "Partitioned bucket sums correctly" 161 ); 162 163 // check telem for unpart by key (host+OA) 164 let histUnpartByKey = 165 await Glean.networking.cookieCountUnpartByKey.testGetValue(); 166 Assert.equal( 167 histUnpartByKey.values[1], 168 1, 169 "Unpartitioned bucket 1 has 1 value" 170 ); 171 Assert.equal( 172 histUnpartByKey.values[8], 173 1, 174 "Unpartitioned bucket 8 has 1 value" 175 ); 176 Assert.equal( 177 histUnpartByKey.sum, 178 cookieNum1 + cookieNum2, 179 "Unpartitioned bucket sums correctly" 180 ); 181 182 schema12db.close(); 183 });