workerStoragePrevented.js (3437B)
1 // Unfortunately, workers can't share the code from storagePermissionsUtils. 2 // These are basic mechanisms for communicating to the test runner. 3 4 function ok(condition, text) { 5 if (!condition) { 6 self.postMessage("FAILURE: " + text); 7 } else { 8 self.postMessage(text); 9 } 10 } 11 12 function finishTest() { 13 self.postMessage("done"); 14 self.close(); 15 } 16 17 // Workers don't have access to localstorage or sessionstorage 18 ok(typeof self.localStorage == "undefined", "localStorage should be undefined"); 19 ok( 20 typeof self.sessionStorage == "undefined", 21 "sessionStorage should be undefined" 22 ); 23 24 // Make sure that we can access indexedDB handle 25 try { 26 indexedDB; 27 ok(true, "WORKER getting indexedDB didn't throw"); 28 } catch (e) { 29 ok(false, "WORKER getting indexedDB threw"); 30 } 31 32 // Make sure that we cannot access indexedDB methods 33 idbOpenTest(); 34 35 // Make sure that we can't access indexedDB deleteDatabase 36 function idbDeleteTest() { 37 try { 38 indexedDB.deleteDatabase("door"); 39 ok(false, "WORKER deleting indexedDB database succeeded"); 40 } catch (e) { 41 ok(true, "WORKER deleting indexedDB database failed"); 42 ok( 43 e.name == "SecurityError", 44 "WORKER deleting indexedDB database threw a security error" 45 ); 46 } finally { 47 cacheTest(); 48 } 49 } 50 51 // Make sure that we can't access indexedDB databases 52 function idbDatabasesTest() { 53 indexedDB 54 .databases() 55 .then(() => { 56 ok(false, "WORKER querying indexedDB databases succeeded"); 57 }) 58 .catch(e => { 59 ok(true, "WORKER querying indexedDB databases failed"); 60 ok( 61 e.name == "SecurityError", 62 "WORKER querying indexedDB databases threw a security error" 63 ); 64 }) 65 .finally(idbDeleteTest); 66 } 67 68 // Make sure that we can't access indexedDB open 69 function idbOpenTest() { 70 try { 71 indexedDB.open("door"); 72 ok(false, "WORKER opening indexedDB database succeeded"); 73 } catch (e) { 74 ok(true, "WORKER opening indexedDB database failed"); 75 ok( 76 e.name == "SecurityError", 77 "WORKER opening indexedDB database threw a security error" 78 ); 79 } finally { 80 idbDatabasesTest(); 81 } 82 } 83 84 // Make sure that we can't access caches 85 function cacheTest() { 86 try { 87 var promise = caches.keys(); 88 ok(true, "WORKER getting caches didn't throw"); 89 90 promise.then( 91 function () { 92 ok(false, "WORKER The promise should have rejected"); 93 workerTest(); 94 }, 95 function () { 96 ok(true, "WORKER The promise was rejected"); 97 workerTest(); 98 } 99 ); 100 } catch (e) { 101 ok( 102 location.protocol !== "https:", 103 "WORKER getting caches should not have thrown" 104 ); 105 workerTest(); 106 } 107 } 108 109 // Try to spawn an inner worker, and make sure that it also can't access storage 110 function workerTest() { 111 if (location.hash != "#outer") { 112 // Don't recurse infinitely, if we are the inner worker, don't spawn another 113 finishTest(); 114 return; 115 } 116 // Create the inner worker, and listen for test messages from it 117 var worker = new Worker("workerStoragePrevented.js#inner"); 118 worker.addEventListener("message", function (e) { 119 const isFail = e.data.match(/^FAILURE/); 120 ok(!isFail, e.data + " (WORKER = workerStoragePrevented.js#inner)"); 121 122 if (e.data == "done" || isFail) { 123 finishTest(); 124 } 125 }); 126 worker.addEventListener("error", function (e) { 127 ok(false, e.data + " (WORKER = workerStoragePrevented.js#inner)"); 128 129 finishTest(); 130 }); 131 }