test_navigator_cookieEnabled.html (3909B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1753586 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Navigator.cookieEnabled</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 <script type="application/javascript"> 12 "use strict"; 13 14 /** Test for Bug 1753586 */ 15 16 const BEHAVIOR_REJECT = 2; 17 const BEHAVIOR_REJECT_TRACKER = 4; 18 const BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN = 5; 19 20 function behavior_to_string(behavior) { 21 switch (behavior) { 22 case BEHAVIOR_REJECT: 23 return "BEHAVIOR_REJECT"; 24 case BEHAVIOR_REJECT_TRACKER: 25 return "BEHAVIOR_REJECT_TRACKER"; 26 case BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN: 27 return "BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN"; 28 default: 29 throw new Error("unreachable"); 30 } 31 } 32 33 const TESTS = [ 34 { cookieBehavior: BEHAVIOR_REJECT, expect: false }, 35 { cookieBehavior: BEHAVIOR_REJECT_TRACKER, expect: true }, 36 { 37 cookieBehavior: BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN, 38 expect: true, 39 }, 40 ]; 41 42 SimpleTest.waitForExplicitFinish(); 43 44 function createAndWaitIframeLoading(win, src, sandbox) { 45 return new Promise(resolve => { 46 let iframe = win.document.createElement("iframe"); 47 48 iframe.addEventListener( 49 "load", 50 () => resolve(iframe), 51 { once: true } 52 ); 53 54 if (sandbox) { 55 iframe.sandbox = sandbox; 56 } 57 iframe.src = src; 58 win.document.body.appendChild(iframe); 59 }); 60 } 61 62 async function runTestInAnotherWindow(expect) { 63 let win = window.open(); 64 65 is( 66 win.navigator.cookieEnabled, 67 expect, 68 `Navigator.cookieEnabled returns expected value in first-party context.` 69 ); 70 71 // Create a first-party iframe and check if navigator.cookieEnabled is 72 // expected. 73 let iframe = await createAndWaitIframeLoading( 74 win, 75 "http://mochi.test:8888/" 76 ); 77 78 await SpecialPowers.spawn(iframe, [expect], value => { 79 is(content.location.href, "http://mochi.test:8888/"); 80 is( 81 content.navigator.cookieEnabled, 82 value, 83 "Navigator.cookieEnabled returns expected value in first-party iframe." 84 ); 85 }); 86 87 // Create a third-party iframe and check if navigator.cookieEnabled is 88 // expected. 89 iframe = await createAndWaitIframeLoading( 90 win, 91 "http://example.com/" 92 ); 93 94 await SpecialPowers.spawn(iframe, [expect], value => { 95 is(content.location.href, "http://example.com/"); 96 is( 97 content.navigator.cookieEnabled, 98 value, 99 "Navigator.cookieEnabled returns expected value in third-party iframe." 100 ); 101 }); 102 103 // Create a sandboxed iframe and check if navigator.cookieEnabled is 104 // expected. It should still return true even if setting cookies in 105 // sandboxed iframe will throw a security error. 106 iframe = await createAndWaitIframeLoading( 107 win, 108 "http://mochi.test:8888/", 109 "allow-scripts" 110 ); 111 112 await SpecialPowers.spawn(iframe, [expect], value => { 113 is(content.location.href, "http://mochi.test:8888/"); 114 is( 115 content.navigator.cookieEnabled, 116 value, 117 "Navigator.cookieEnabled returns expected value in sandboxed iframe." 118 ); 119 }); 120 121 win.close(); 122 } 123 124 add_task(async function doTests() { 125 for (let test of TESTS) { 126 info(`cookieBehavior ${behavior_to_string(test.cookieBehavior)} expect ${test.expect}`); 127 128 await SpecialPowers.pushPrefEnv({"set": [ 129 ["network.cookie.cookieBehavior", test.cookieBehavior], 130 ]}); 131 132 // We need to run the test in another window so that the cookieBehavior 133 // setting would take effect. 134 await runTestInAnotherWindow(test.expect); 135 } 136 }); 137 138 </script> 139 </head> 140 <body> 141 </body> 142 </html>