tor-browser

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

test_rate_limit_location_change.html (3891B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1314912
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 1314912</title>
      9  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11  <script type="application/javascript">
     12 
     13  /** Test for Bug 1314912 */
     14 
     15  const RATE_LIMIT_COUNT = 90;
     16  const RATE_LIMIT_TIME_SPAN = 3;
     17 
     18  async function setup() {
     19    await SpecialPowers.pushPrefEnv({set: [
     20      ["dom.navigation.navigationRateLimit.count", RATE_LIMIT_COUNT],
     21      ["dom.navigation.navigationRateLimit.timespan", RATE_LIMIT_TIME_SPAN],
     22      ["dom.navigation.webidl.enabled", false]]});
     23  }
     24 
     25  let inc = 0;
     26 
     27  const rateLimitedFunctions = (win) => ({
     28    "history.replaceState": () => win.history.replaceState(null, "test", `${win.location.href}#${inc++}`),
     29    "history.pushState":  () => win.history.pushState(null, "test", `${win.location.href}#${inc++}`),
     30    "history.SetScrollRestoration": () => win.history.scrollRestoration = "auto",
     31    "history.back": () => win.history.back(),
     32    "history.forward": () => win.history.forward(),
     33    "history.go": () => win.history.go(-1),
     34    "location.href": () => win.location.href = win.location.href + "",
     35    "location.hash": () => win.location.hash = inc++,
     36    "location.host": () => win.location.host = win.location.host + "",
     37    "location.hostname": () => win.location.hostname = win.location.hostname + "",
     38    "location.pathname": () => win.location.pathname = win.location.pathname + "",
     39    "location.port": () => win.location.port = win.location.port + "",
     40    "location.protocol": () => win.location.protocol = win.location.protocol + "",
     41    "location.search": () => win.location.search = win.location.search + "",
     42    "location.assign": () => win.location.assign(`${win.location.href}#${inc++}`),
     43    "location.replace": () => win.location.replace(`${win.location.href}#${inc++}`),
     44    "location.reload": () => win.location.reload(),
     45  });
     46 
     47  async function test() {
     48    await setup();
     49 
     50    // Open new window and wait for it to load
     51    let win = window.open("blank.html");
     52    await new Promise((resolve) => SimpleTest.waitForFocus(resolve, win))
     53 
     54    // Execute the history and location functions
     55    Object.entries(rateLimitedFunctions(win)).forEach(([name, fn]) => {
     56      // Reset the rate limit for the next run.
     57      info("Reset rate limit.");
     58      SpecialPowers.wrap(win).browsingContext.resetNavigationRateLimit();
     59 
     60      info(`Calling ${name} ${RATE_LIMIT_COUNT} times to reach the rate limit.`);
     61      for(let i = 0; i< RATE_LIMIT_COUNT; i++) {
     62        fn.call(this);
     63      }
     64      // Next calls should throw because we're above the rate limit
     65      for(let i = 0; i < 5; i++)  {
     66        SimpleTest.doesThrow(() => fn.call(this), `Call #${RATE_LIMIT_COUNT + i + 1} to ${name} should throw.`);
     67      }
     68    })
     69 
     70    // We didn't reset the rate limit after the last loop iteration above.
     71    // Wait for the rate limit timer to expire.
     72    SimpleTest.requestFlakyTimeout("Waiting to trigger rate limit reset.");
     73    await new Promise((resolve) => setTimeout(resolve, 5000));
     74 
     75    // Calls should be allowed again.
     76    Object.entries(rateLimitedFunctions(win)).forEach(([name, fn]) => {
     77      let didThrow = false;
     78      try {
     79        fn.call(this);
     80      } catch(error) {
     81        didThrow = true;
     82      }
     83      is(didThrow, false, `Call to ${name} must not throw.`)
     84    });
     85 
     86    // Cleanup
     87    win.close();
     88    SpecialPowers.wrap(win).browsingContext.resetNavigationRateLimit();
     89    SimpleTest.finish();
     90  }
     91 
     92  </script>
     93 </head>
     94 <body onload="setTimeout(test, 0);">
     95 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1314912">Mozilla Bug 1314912</a>
     96 <p id="display"></p>
     97 <div id="content" style="display: none">
     98 </div>
     99 <pre id="test">
    100 </pre>
    101 </body>
    102 </html>