test_cache2_clear_with_principal.js (4313B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { HttpServer } = ChromeUtils.importESModule( 8 "resource://testing-common/httpd.sys.mjs" 9 ); 10 11 const { TestUtils } = ChromeUtils.importESModule( 12 "resource://testing-common/TestUtils.sys.mjs" 13 ); 14 15 var httpserver = new HttpServer(); 16 var port; 17 18 function make_uri(urlStr) { 19 return Services.io.newURI(urlStr); 20 } 21 22 function serverHandler(_metadata, response) { 23 const body = "hello world"; 24 response.setHeader("Content-Type", "text/plain", false); 25 response.bodyOutputStream.write(body, body.length); 26 } 27 28 add_setup(async function setup() { 29 httpserver.registerPathHandler("/test", serverHandler); 30 httpserver.registerPathHandler("/ignore", serverHandler); 31 httpserver.start(-1); 32 port = httpserver.identity.primaryPort; 33 34 info("Starting test with port " + port); 35 36 registerCleanupFunction(async function () { 37 await httpserver.stop(); 38 }); 39 }); 40 41 async function load_get_principal(url, oa) { 42 let chan = makeHTTPChannel(url); 43 chan.loadInfo.originAttributes = oa; 44 await new Promise(resolve => { 45 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL)); 46 }); 47 return chan.loadInfo.loadingPrincipal; 48 } 49 50 async function test(oaLoad, oaClear, shouldExist) { 51 // populate cache entry 52 let url = `http://localhost:${port}/test`; 53 await load_get_principal(url, oaLoad); 54 55 let clearPrincipal = Services.scriptSecurityManager.createContentPrincipal( 56 Services.io.newURI(`http://localhost:${port}/`), 57 oaClear 58 ); 59 // sanity check that the originAttributes match 60 let expectedOa = { ...clearPrincipal.originAttributes, ...oaClear }; 61 Assert.deepEqual(clearPrincipal.originAttributes, expectedOa); 62 63 let cache_storage = getCacheStorage( 64 "disk", 65 Services.loadContextInfo.custom(false, oaLoad) 66 ); 67 let exists = cache_storage.exists(make_uri(url), null); 68 Assert.ok(exists, "Entry should be in cache"); 69 70 Services.cache2.clearOriginsByPrincipal(clearPrincipal); 71 72 // clearOriginsByPrincipal is async, so we block on cacheIOThread 73 await new Promise(resolve => { 74 syncWithCacheIOThread(resolve, true); 75 }); 76 77 await TestUtils.waitForCondition( 78 () => { 79 let existsAgain = cache_storage.exists(make_uri(url), null); 80 // times out when codition isn't met. 81 return shouldExist == existsAgain; 82 }, 83 shouldExist ? "Entry stay in cache" : "Entry get cleared from the cache" 84 ); 85 } 86 87 add_task(async function test_usercontextid_same() { 88 await test({ userContextId: 0 }, { userContextId: 0 }, false); 89 }); 90 91 add_task(async function test_usercontextid_same_nondefault() { 92 await test({ userContextId: 1 }, { userContextId: 1 }, false); 93 }); 94 95 add_task(async function test_usercontextid_different_1() { 96 await test({ userContextId: 1 }, { userContextId: 0 }, true); 97 }); 98 99 add_task(async function test_usercontextid_different_2() { 100 await test({ userContextId: 0 }, { userContextId: 1 }, true); 101 }); 102 103 add_task(async function test_privatebrowsingid_same() { 104 await test({ privateBrowsingId: 1 }, { privateBrowsingId: 1 }, false); 105 }); 106 107 add_task(async function test_privatebrowsingid_default() { 108 await test({ privateBrowsingId: 0 }, { privateBrowsingId: 1 }, true); 109 }); 110 111 add_task(async function test_privatebrowsingid_private() { 112 await test({ privateBrowsingId: 1 }, { privateBrowsingId: 0 }, true); 113 }); 114 115 add_task(async function test_partitionkey_same() { 116 await test( 117 { partitionKey: "(https,example.com)" }, 118 { partitionKey: "(https,example.com)" }, 119 false 120 ); 121 }); 122 123 add_task(async function test_partitionkey_different_1() { 124 await test( 125 { partitionKey: "(http,example.com)" }, 126 { partitionKey: "(https,example.com)" }, 127 true 128 ); 129 }); 130 131 add_task(async function test_partitionkey_different_2() { 132 await test( 133 { partitionKey: "(http,example.com)" }, 134 { partitionKey: "(https,example.com)" }, 135 true 136 ); 137 }); 138 139 add_task(async function test_partitionkey_nonexisting_firstparty() { 140 await test({ partitionKey: `(http,localhost,${port})` }, {}, true); 141 }); 142 143 add_task(async function test_partitionkey_firstparty_nonexisting() { 144 await test({}, { partitionKey: `(http,localhost,${port})` }, true); 145 });