tor-browser

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

browser_dbg-inline-cache.js (5048B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 /*
      6 * Test loading inline scripts from cache:
      7 *   - Load document with inline script
      8 *   - Reload inside debugger with toolbox caching disabled
      9 *   - Reload inside debugger with toolbox caching enabled
     10 */
     11 
     12 "use strict";
     13 
     14 // Breakpoint position calculations can throw when interrupted by a navigation.
     15 PromiseTestUtils.allowMatchingRejectionsGlobally(/Resource .*? does not exist/);
     16 
     17 const server = createTestHTTPServer();
     18 
     19 let docValue = 1;
     20 server.registerPathHandler("/inline-cache.html", (request, response) => {
     21  response.setHeader("Content-Type", "text/html");
     22  // HTTP should assume cacheable by default.
     23  // Toolbox defaults to disabling cache for subsequent loads when open.
     24  response.setHeader("Cache-Control", "public, max-age=3600");
     25  response.write(`
     26    <html>
     27    <head>
     28    <script>
     29      let x = ${docValue};
     30    </script>
     31    </head>
     32    </html>
     33  `);
     34 });
     35 
     36 const SOURCE_URL = `http://localhost:${server.identity.primaryPort}/inline-cache.html`;
     37 
     38 add_task(async function () {
     39  info("Load document with inline script");
     40  const tab = await addTab(SOURCE_URL);
     41  info("Open debugger");
     42  clearDebuggerPreferences();
     43  const toolbox = await openToolboxForTab(tab, "jsdebugger");
     44  const dbg = createDebuggerContext(toolbox);
     45  await waitForSource(dbg, "inline-cache.html");
     46  info("Reload tab to ensure debugger finds script");
     47  await reloadBrowser();
     48  let pageValue = await getPageValue(tab);
     49  is(pageValue, "let x = 1;", "Content loads from network, has doc value 1");
     50  await waitForSource(dbg, "inline-cache.html");
     51  await selectSource(dbg, "inline-cache.html");
     52  await waitForLoadedSource(dbg, "inline-cache.html");
     53  let dbgValue = findSourceContent(dbg, "inline-cache.html");
     54  info(`Debugger text: ${dbgValue.value}`);
     55  ok(
     56    dbgValue.value.includes(pageValue),
     57    "Debugger loads from cache, gets value 1 like page"
     58  );
     59 
     60  info("Disable HTTP cache for page");
     61  await toolbox.commands.targetConfigurationCommand.updateConfiguration({
     62    cacheDisabled: true,
     63  });
     64  makeChanges();
     65 
     66  info("Reload inside debugger with toolbox caching disabled (attempt 1)");
     67  await reloadBrowser();
     68  pageValue = await getPageValue(tab);
     69  is(pageValue, "let x = 2;", "Content loads from network, has doc value 2");
     70  await waitForLoadedSource(dbg, "inline-cache.html");
     71  dbgValue = findSourceContent(dbg, "inline-cache.html");
     72 
     73  info(`Debugger text: ${dbgValue.value}`);
     74  ok(
     75    dbgValue.value.includes(pageValue),
     76    "Debugger loads from network, gets value 2 like page"
     77  );
     78 
     79  makeChanges();
     80 
     81  info("Reload inside debugger with toolbox caching disabled (attempt 2)");
     82  await reloadBrowser();
     83  pageValue = await getPageValue(tab);
     84  is(pageValue, "let x = 3;", "Content loads from network, has doc value 3");
     85  await waitForLoadedSource(dbg, "inline-cache.html");
     86  dbgValue = findSourceContent(dbg, "inline-cache.html");
     87  info(`Debugger text: ${dbgValue.value}`);
     88  ok(
     89    dbgValue.value.includes(pageValue),
     90    "Debugger loads from network, gets value 3 like page"
     91  );
     92 
     93  info("Enable HTTP cache for page");
     94  await toolbox.commands.targetConfigurationCommand.updateConfiguration({
     95    cacheDisabled: false,
     96  });
     97  makeChanges();
     98 
     99  // Even though the HTTP cache is now enabled, Gecko sets the VALIDATE_ALWAYS flag when
    100  // reloading the page.  So, it will always make a request to the server for the main
    101  // document contents.
    102 
    103  info("Reload inside debugger with toolbox caching enabled (attempt 1)");
    104  await reloadBrowser();
    105  pageValue = await getPageValue(tab);
    106  is(pageValue, "let x = 4;", "Content loads from network, has doc value 4");
    107  await waitForLoadedSource(dbg, "inline-cache.html");
    108  dbgValue = findSourceContent(dbg, "inline-cache.html");
    109  info(`Debugger text: ${dbgValue.value}`);
    110  ok(
    111    dbgValue.value.includes(pageValue),
    112    "Debugger loads from cache, gets value 4 like page"
    113  );
    114 
    115  makeChanges();
    116 
    117  info("Reload inside debugger with toolbox caching enabled (attempt 2)");
    118  await reloadBrowser();
    119  pageValue = await getPageValue(tab);
    120  is(pageValue, "let x = 5;", "Content loads from network, has doc value 5");
    121  await waitForLoadedSource(dbg, "inline-cache.html");
    122  await waitForSelectedSource(dbg, "inline-cache.html");
    123  dbgValue = findSourceContent(dbg, "inline-cache.html");
    124  info(`Debugger text: ${dbgValue.value}`);
    125  ok(
    126    dbgValue.value.includes(pageValue),
    127    "Debugger loads from cache, gets value 5 like page"
    128  );
    129 
    130  await closeTabAndToolbox();
    131 });
    132 
    133 /**
    134 * This is meant to simulate the developer editing the inline source and saving.
    135 * Effectively, we change the source during the test at specific controlled points.
    136 */
    137 function makeChanges() {
    138  docValue++;
    139 }
    140 
    141 function getPageValue(tab) {
    142  return SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    143    return content.document.querySelector("script").textContent.trim();
    144  });
    145 }