tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_font_whitelist.html (3166B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1121643
      5 -->
      6 <head>
      7  <title>Test for Bug 1121643</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1121643">Mozilla Bug 1121643</a>
     13 <span id="mono" style="font-family: monospace; font-size: 64px;">M</span>
     14 <span id="sans" style="font-family: sans-serif; font-size: 64px;">M</span>
     15 <span id="serif" style="font-family: serif; font-size: 64px;">M</span>
     16 <div id="content" style="display: none">
     17 
     18 </div>
     19 <script class="testbody" type="application/javascript">
     20 
     21 /** Test for Bug 1121643 */
     22 
     23 const InspectorUtils = SpecialPowers.InspectorUtils;
     24 
     25 // Given an element id, returns the first font face name encountered.
     26 let fontUsed = id => {
     27  let element = document.getElementById(id),
     28      range = document.createRange();
     29  range.selectNode(element);
     30  return InspectorUtils.getUsedFontFaces(range)[0].CSSFamilyName;
     31 };
     32 
     33 // A map of the default mono, sans and serif fonts, obtained when
     34 // whitelisting is disabled.
     35 const fonts = { mono: fontUsed("mono"),
     36                sans: fontUsed("sans"),
     37                serif: fontUsed("serif") };
     38 
     39 let hack = 0;
     40 
     41 // Set the font whitelist to contain none, some, or all of the
     42 // default mono, sans, and serif fonts. Check that the rendering
     43 // of our three test elements uses only fonts present in the
     44 // whitelist.
     45 let testFontWhitelist = async function(useMono, useSans, useSerif) {
     46  let whitelist = [];
     47  if (useMono) {
     48    whitelist.push(fonts.mono);
     49  }
     50  if (useSans) {
     51    whitelist.push(fonts.sans);
     52  }
     53  if (useSerif) {
     54    whitelist.push(fonts.serif);
     55  }
     56  await SpecialPowers.pushPrefEnv({"set": [["font.system.whitelist",
     57                                            whitelist.join(", ")]]});
     58 
     59  await new Promise(SimpleTest.executeSoon);
     60  await SpecialPowers.setIntPref("font.fixme.hack", hack++);
     61  await new Promise(SimpleTest.executeSoon);
     62 
     63  // If whitelist is empty, then whitelisting is considered disabled
     64  // and all fonts are allowed.
     65  info("font whitelist: " + JSON.stringify(whitelist));
     66  let whitelistEmpty = whitelist.length === 0;
     67  is(useMono || whitelistEmpty, fontUsed("mono") === fonts.mono,
     68     "Correct mono whitelisting state; got " + fontUsed("mono") + ", requested " + fonts.mono);
     69  is(useSans || whitelistEmpty, fontUsed("sans") === fonts.sans,
     70     "Correct sans whitelisting state; got " + fontUsed("sans") + ", requested " + fonts.sans);
     71  is(useSerif || whitelistEmpty, fontUsed("serif") === fonts.serif,
     72     "Correct serif whitelisting state; got " + fontUsed("serif") + ", requested " + fonts.serif);
     73 };
     74 
     75 // Run tests to confirm that only whitelisting fonts are present in a
     76 // rendered page. Try turning mono, sans, and serif off and on in
     77 // every combination.
     78 add_task(async function() {
     79  for (let useMono of [false, true]) {
     80    for (let useSans of [false, true]) {
     81      for (let useSerif of [false, true]) {
     82        await testFontWhitelist(useMono, useSans, useSerif);
     83      }
     84    }
     85  }
     86 });
     87 
     88 </script>
     89 </body>
     90 </html>