test_console_messages.html (3092B)
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title>Test messages logged to console</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <iframe id="embeddingFrame"></iframe> 11 12 <script class="testbody" type="text/javascript"> 13 SimpleTest.waitForExplicitFinish(); 14 15 function cleanup() { 16 SpecialPowers.postConsoleSentinel(); 17 SimpleTest.finish(); 18 } 19 20 const TEST_CASES = [ 21 { 22 headers: [ 23 ["Integrity-Policy", "blocked-destinations=(script style)"], 24 ], 25 expected: { 26 blockedScript: 1, 27 blockedStylesheet: 1, 28 blockedScriptRO: 0, 29 blockedStylesheetRO: 0, 30 }, 31 }, 32 { 33 headers: [ 34 [ 35 "Integrity-Policy-Report-Only", 36 "blocked-destinations=(script style)", 37 ], 38 ], 39 expected: { 40 blockedScript: 0, 41 blockedStylesheet: 0, 42 blockedScriptRO: 1, 43 blockedStylesheetRO: 1, 44 }, 45 }, 46 { 47 headers: [ 48 ["Integrity-Policy", "blocked-destinations=(script style)"], 49 [ 50 "Integrity-Policy-Report-Only", 51 "blocked-destinations=(script style)", 52 ], 53 ], 54 expected: { 55 blockedScript: 1, 56 blockedStylesheet: 1, 57 blockedScriptRO: 0, 58 blockedStylesheetRO: 0, 59 }, 60 }, 61 ]; 62 63 const messageRegex = 64 /^(?<reportonly>\(Report-Only policy\) )?The page’s settings (?<would>would block|blocked) a (?<destination>script|stylesheet) at (.+) from being loaded because it is missing integrity metadata\.$/; 65 66 function isTestCaseZeroed(testCase) { 67 return Object.keys(testCase.expected).every( 68 key => testCase.expected[key] === 0 69 ); 70 } 71 72 function loadTestCase(testCase) { 73 document.getElementById("embeddingFrame").src = 74 "file_console_messages.sjs?" + 75 encodeURIComponent( 76 JSON.stringify({ 77 res: "html", 78 headers: testCase.headers, 79 }) 80 ); 81 } 82 83 let currentTestI = 0; 84 SpecialPowers.registerConsoleListener(msg => { 85 const { errorMessage } = msg; 86 if (!errorMessage) { 87 return; 88 } 89 90 const testCase = TEST_CASES[currentTestI]; 91 const match = messageRegex.exec(errorMessage); 92 if (!match) { 93 // Blocked script loads log `Loading failed for the <script> with source “...".` 94 if ( 95 errorMessage.startsWith( 96 "Loading failed for the <script> with source" 97 ) 98 ) { 99 return; 100 } 101 ok(false, `Unexpected message: "${errorMessage}"`); 102 return; 103 } 104 105 const { reportonly, would, destination } = match.groups; 106 const isReportOnly = !!reportonly; 107 108 is(would, isReportOnly ? "would block" : "blocked"); 109 110 const key = 111 "blocked" + 112 destination.charAt(0).toUpperCase() + 113 destination.slice(1) + 114 (isReportOnly ? "RO" : ""); 115 testCase.expected[key]--; 116 117 if (isTestCaseZeroed(testCase)) { 118 currentTestI++; 119 if (currentTestI < TEST_CASES.length) { 120 // Load the next test case. 121 loadTestCase(TEST_CASES[currentTestI]); 122 } else { 123 cleanup(); 124 } 125 } 126 }); 127 128 // Start with the first test case. 129 loadTestCase(TEST_CASES[currentTestI]); 130 </script> 131 </body> 132 </html>