partitioned-estimate-usage-details-caches.tentative.https.sub.html (7171B)
1 <!DOCTYPE html> 2 <meta name=help href="https://privacycg.github.io/storage-partitioning/"> 3 <title>Partitioned estimate() usage details for caches test</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/common/utils.js"></script> 7 8 <body> 9 <script> 10 // Helper function to obtain cache usage data for this test window. 11 const usageDetails = async () => 12 (await navigator.storage.estimate()).usageDetails.caches || 0; 13 14 // Helper function to create usage of the cache so that our test 15 // can estimate that usage. 16 const createSomeUsage = async () => { 17 const cache_name = token(); 18 const cache_url = `/foo-${cache_name}`; 19 const cache = await caches.open(cache_name); 20 await cache.put(cache_url, new Response('x'.repeat(128))); 21 return [cache, cache_url]; 22 } 23 24 // Helper function for creating pathnames to test resources. 25 const testPath = () => location.pathname.split("/").slice(0, -1).join("/"); 26 27 // Define our test variables. 28 let alt_origin = "https://{{hosts[alt][]}}:{{ports[https][0]}}"; 29 let details = {}; 30 31 // Step 0: Construct an iframe. The content of this iframe includes 32 // a script that will intercept and send postMessages back to this test 33 // window. 34 const iframe = document.createElement("iframe"); 35 iframe.src = `https://{{host}}:{{ports[https][0]}}${testPath()}` + 36 `/resources/partitioned-estimate-usage-details-caches-helper-frame.html` 37 document.body.appendChild(iframe); 38 39 // Our test will perform the following steps to demonstrate the partitioning 40 // of storage estimate usage details for cache. Some steps are defined in: 41 // wpt/storage/resources/partitioned-estimate-usage-details-caches-helper-frame.html 42 // -------------------- 43 // (0) Construct a same-partition iframe on our test page. The content of 44 // this iframe includes a script that will intercept and send postMessages 45 // back to this test window. 46 // (1) The same-partition iframe sends a postMessage notifying that the 47 // iframe was constructed and we are ready to proceed with the test. 48 // (2) Our test window intercepts this "iframe-is-ready" message. 49 // (3) We create some cache usage and ensure that the cache usage data 50 // reflects that increase in the test window. 51 // (4) postMessage the same-partition iframe to obtain cache usage data 52 // from that frame. 53 // (5) Our same-partition iframe intercepts the "get-details" postMessage, 54 // obtains the cache usage details available to the iframe, and 55 // postMessages the usage back to the test window. 56 // (6) Our test window intercepts the "same-site" message from the same- 57 // partition iframe containing usage data obtained there. 58 // (7) We record the same-partition usage data. Then, we open a cross- 59 // site window containing this test script. As a result, Step 0 will be 60 // repeated, and a cross-partition iframe will be created in that cross- 61 // site window. Our cross-partition iframe will receive the same script as 62 // our same-partition iframe. We then return early to avoid running another 63 // instance of this test. 64 // (8) Once created and loaded, our cross-partition iframe has an on-load 65 // listener that is triggered. To check that our script is executing in the 66 // cross-partition iframe (and not the same-partition iframe), we check that 67 // our parent has a valid opener value. 68 // (9) Then, our cross-partition iframe obtains the cache usage details 69 // available to it and postMessages the usage back to the test window. 70 // (10) Our test window intercepts the "cross-site" message from the cross- 71 // partition iframe containing the usage data obtained there. 72 // (11) We record the cross-partition usage data. Then we make our final 73 // assertions. 74 async_test(test => { 75 // Since this script is loaded in two windows (our original test window 76 // and the cross-site window opened later in the test), and we only want 77 // to run the test body in the original test window, we return early 78 // if our origin matches the "cross-site" window. 79 if (location.origin === alt_origin) 80 return; 81 82 // Step 2: Our test window intercepts the "iframe-is-ready" message from 83 // the same-partition iframe. 84 let cache, cache_url; 85 window.addEventListener("message", test.step_func(async event => { 86 if (event.data === "iframe-is-ready") { 87 // Step 3: We create some cache usage and ensure that the cache usage 88 // data reflects that increase in the test window. 89 details.init = await usageDetails(); 90 [cache, cache_url] = await createSomeUsage(test); 91 details.after = await usageDetails(); 92 assert_greater_than(details.after, details.init); 93 94 // Step 4: postMessage the same-partition iframe to request that cache 95 // usage data be obtained and sent from that frame. 96 iframe.contentWindow.postMessage("get-details", iframe.origin); 97 } 98 })); 99 100 window.addEventListener("message", test.step_func(event => { 101 // Step 6: Our test window intercepts the "same-site" message from the 102 // same-partition iframe containing usage data obtained there. 103 if (event.data.source === "same-site") { 104 // Step 7: We record the same-partition data here. Then, we open a 105 // cross-site window containing this test script. As a result, Step 0 106 // will be repeated, and a cross-partition iframe will be created in 107 // that cross-site window. Our cross-partition iframe will receive the 108 // same script as our same-partition iframe. We then return early to 109 // avoid running another instance of this test. 110 details.same_site = event.data; 111 112 const cross_site_window = window 113 .open(`${alt_origin}${location.pathname}`, "", "noopener=false"); 114 115 test.add_cleanup(() => cross_site_window.close()); 116 } 117 // Step 10: Our test window intercepts the "cross-site" message from 118 // the cross-partition iframe containing the usage data obtained there. 119 if (event.data.source === "cross-site") { 120 // Step 11: We record the cross-partition data. Then we make our final 121 // assertions. 122 details.cross_site = event.data; 123 124 // Some cleanup. 125 test.step(async () => await cache.delete(cache_url)); 126 127 // Usage data is correctly partitioned if: 128 // a. Our cross-partition iframe recorded no cache usage data AND 129 // b. The cache usage data for our test window (after the usage was 130 // created) is equal to the cache usage data recorded by our 131 // same-partition iframe. 132 test.step(() => { 133 assert_true(details.cross_site.init == 0, "Usage should be 0."); 134 assert_equals(details.same_site.init, details.after); 135 }); 136 137 test.done(); 138 } 139 })); 140 }, "Partitioned estimate() usage details for caches test."); 141 </script> 142 </body>