indexeddb.tentative.https.window.js (3598B)
1 // META: timeout=long 2 // META: script=/common/get-host-info.sub.js 3 // META: script=/common/utils.js 4 // META: script=/common/dispatcher/dispatcher.js 5 // META: script=/html/cross-origin-embedder-policy/credentialless/resources/common.js 6 // META: script=./resources/common.js 7 8 // "token()" is used to get unique value for every execution of the test. This 9 // avoids potential side effects of one run toward the second. 10 const g_db_store = token(); 11 const g_db_name = token(); 12 const g_db_version = 1; 13 14 // A script storing "|id|=|value|" in IndexedDB. 15 const write_script = (id, value, done) => ` 16 // Open the database: 17 const request = indexedDB.open("${g_db_name}", "${g_db_version}"); 18 request.onupgradeneeded = () => { 19 request.result.createObjectStore("${g_db_store}", {keyPath: "id"}); 20 }; 21 await new Promise(r => request.onsuccess = r); 22 const db = request.result; 23 24 // Write the value: 25 const transaction_write = db.transaction("${g_db_store}", "readwrite"); 26 transaction_write.objectStore("${g_db_store}").add({ 27 id: "${id}", 28 value: "${value}", 29 }); 30 await transaction_write.complete; 31 32 db.close(); 33 send("${done}", "Done"); 34 `; 35 36 // A script retrieving what was stored inside IndexedDB. 37 const read_script = (done) => ` 38 // Open the database: 39 const request = indexedDB.open("${g_db_name}", "${g_db_version}"); 40 await new Promise(r => request.onsuccess = r); 41 const db = request.result; 42 43 // Read: 44 const transaction_read = db.transaction("${g_db_store}", "readonly"); 45 const get_all = transaction_read.objectStore("${g_db_store}").getAll(); 46 await new Promise(r => transaction_read.oncomplete = r); 47 48 db.close(); 49 send("${done}", JSON.stringify(get_all.result)); 50 `; 51 52 promise_test(async test => { 53 // 4 actors: 2 credentialless iframe and 2 normal iframe. 54 const origin = get_host_info().HTTPS_REMOTE_ORIGIN; 55 const iframes = [ 56 newIframeCredentialless(origin), 57 newIframeCredentialless(origin), 58 newIframe(origin), 59 newIframe(origin), 60 ]; 61 62 // 1. Write a different key-value pair from the iframes in IndexedDB: 63 const keys = iframes.map(token); 64 const values = iframes.map(token); 65 const response_queues = iframes.map(token); 66 await Promise.all(iframes.map(async (_, i) => { 67 send(iframes[i], write_script(keys[i], values[i], response_queues[i])); 68 assert_equals(await receive(response_queues[i]), "Done"); 69 })); 70 71 // 2. Read the state from every iframes: 72 const states = await Promise.all(iframes.map(async (_, i) => { 73 send(iframes[i], read_script(response_queues[i])); 74 const reply = JSON.parse(await receive(response_queues[i])); 75 76 const state = {} 77 for(entry of reply) 78 state[entry.id] = entry.value; 79 return state; 80 })); 81 82 83 // Verify the two credentialless iframe share the same state and the normal 84 // iframe share a second state 85 assert_equals(states[0][keys[0]], values[0]); 86 assert_equals(states[0][keys[1]], values[1]); 87 assert_equals(states[0][keys[2]], undefined); 88 assert_equals(states[0][keys[3]], undefined); 89 90 assert_equals(states[1][keys[0]], values[0]); 91 assert_equals(states[1][keys[1]], values[1]); 92 assert_equals(states[1][keys[2]], undefined); 93 assert_equals(states[1][keys[3]], undefined); 94 95 assert_equals(states[2][keys[0]], undefined); 96 assert_equals(states[2][keys[1]], undefined); 97 assert_equals(states[2][keys[2]], values[2]); 98 assert_equals(states[2][keys[3]], values[3]); 99 100 assert_equals(states[3][keys[0]], undefined); 101 assert_equals(states[3][keys[1]], undefined); 102 assert_equals(states[3][keys[2]], values[2]); 103 assert_equals(states[3][keys[3]], values[3]); 104 })