tor-browser

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

browser_webchannel-get-js-sources.js (5642B)


      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 "use strict";
      6 
      7 const FRONTEND_URL =
      8  "https://example.com/browser/devtools/client/performance-new/test/browser/webchannel-js-sources.html";
      9 
     10 add_task(async function test_webchannel_get_js_sources() {
     11  info("Test the WebChannel GET_JS_SOURCES request functionality");
     12 
     13  // Register some mock JS sources for testing. We already test the logic that
     14  // retrieves the sources in other tests. This test focuses on the webchannel
     15  // itself only.
     16  const mockJSSources = {
     17    "uuid-12345-1": "function testFunction() { return 42; }",
     18    "uuid-12345-2": "console.log('Hello from source 2');",
     19    "uuid-67890-5": "function anotherFunction() { return 'test'; }",
     20    "uuid-67890-6": "alert('Hello world');",
     21  };
     22 
     23  const sourcesToRequest = ["uuid-12345-1", "uuid-12345-2", "uuid-67890-5"];
     24 
     25  const expectedResponse = [
     26    { sourceText: "function testFunction() { return 42; }" },
     27    { sourceText: "console.log('Hello from source 2');" },
     28    { sourceText: "function anotherFunction() { return 'test'; }" },
     29  ];
     30 
     31  await BrowserTestUtils.withNewTab(
     32    {
     33      gBrowser,
     34      url: "about:blank",
     35    },
     36    async browser => {
     37      // Register the mock sources for the current browser
     38      BackgroundJSM.registerProfileCaptureForBrowser(
     39        browser,
     40        Promise.resolve(new ArrayBuffer(0)), // Mock profile result
     41        null, // No symbolication service
     42        mockJSSources
     43      );
     44 
     45      // Now navigate to the test HTML page
     46      const url =
     47        FRONTEND_URL +
     48        "?testType=basic" +
     49        "&sourceUuids=" +
     50        encodeURIComponent(JSON.stringify(sourcesToRequest)) +
     51        "&expectedResponse=" +
     52        encodeURIComponent(JSON.stringify(expectedResponse));
     53 
     54      const loaded = BrowserTestUtils.browserLoaded(browser);
     55      BrowserTestUtils.startLoadingURIString(browser, url);
     56      await loaded;
     57 
     58      await waitForTabTitle("JS sources received");
     59      ok(true, "The JS sources are successfully retrieved by the WebChannel.");
     60    }
     61  );
     62 });
     63 
     64 add_task(async function test_webchannel_get_js_sources_nonexistent() {
     65  info("Test GET_JS_SOURCES WebChannel request with non-existent sources");
     66 
     67  // Register some mock JS sources
     68  const mockJSSources = {
     69    "uuid-12345-1": "function testFunction() { return 42; }",
     70  };
     71 
     72  // Request non-existent sources (should return null)
     73  const sourcesToRequest = [
     74    "uuid-12345-999", // Non-existent UUID
     75    "uuid-99999-1", // Non-existent UUID
     76  ];
     77 
     78  const expectedResponse = [
     79    { error: "Source not found in the browser" },
     80    { error: "Source not found in the browser" },
     81  ];
     82 
     83  await BrowserTestUtils.withNewTab(
     84    {
     85      gBrowser,
     86      url: "about:blank",
     87    },
     88    async browser => {
     89      // Register the mock sources for the current browser
     90      BackgroundJSM.registerProfileCaptureForBrowser(
     91        browser,
     92        Promise.resolve(new ArrayBuffer(0)),
     93        null,
     94        mockJSSources
     95      );
     96 
     97      // Now navigate to the test HTML page
     98      const url =
     99        FRONTEND_URL +
    100        "?testType=nonexistent" +
    101        "&sourceUuids=" +
    102        encodeURIComponent(JSON.stringify(sourcesToRequest)) +
    103        "&expectedResponse=" +
    104        encodeURIComponent(JSON.stringify(expectedResponse));
    105 
    106      const loaded = BrowserTestUtils.browserLoaded(browser);
    107      BrowserTestUtils.startLoadingURIString(browser, url);
    108      await loaded;
    109 
    110      await waitForTabTitle("JS sources received");
    111      ok(
    112        true,
    113        "Successfully verified GET_JS_SOURCES with non-existent sources"
    114      );
    115    }
    116  );
    117 });
    118 
    119 add_task(async function test_webchannel_get_js_sources_no_data() {
    120  info("Test GET_JS_SOURCES WebChannel request when no data is registered");
    121 
    122  const sourcesToRequest = ["uuid-12345-1"];
    123 
    124  // Open a new tab without registering JS sources
    125  await BrowserTestUtils.withNewTab(
    126    {
    127      gBrowser,
    128      url: "about:blank",
    129    },
    130    async browser => {
    131      // Don't register any JS sources - this should cause an error
    132 
    133      // Now navigate to the test HTML page
    134      const url =
    135        FRONTEND_URL +
    136        "?testType=no-data" +
    137        "&sourceUuids=" +
    138        encodeURIComponent(JSON.stringify(sourcesToRequest)) +
    139        "&expectError=true";
    140 
    141      const loaded = BrowserTestUtils.browserLoaded(browser);
    142      BrowserTestUtils.startLoadingURIString(browser, url);
    143      await loaded;
    144 
    145      await waitForTabTitle("JS sources received");
    146      ok(true, "Successfully verified GET_JS_SOURCES error handling");
    147    }
    148  );
    149 });
    150 
    151 add_task(async function test_webchannel_get_js_sources_invalid_request() {
    152  info("Test GET_JS_SOURCES WebChannel request with invalid sources parameter");
    153 
    154  await BrowserTestUtils.withNewTab(
    155    {
    156      gBrowser,
    157      url: "about:blank",
    158    },
    159    async browser => {
    160      // Don't need to register JS sources for this test
    161 
    162      // For this test, we need to pass invalid sourceUuids directly in the HTML
    163      // We'll use a special URL parameter that the HTML will use directly
    164      const url =
    165        FRONTEND_URL +
    166        "?testType=invalid-request" +
    167        "&sourceUuids=invalid_not_array" + // This is intentionally not JSON
    168        "&expectError=true";
    169 
    170      const loaded = BrowserTestUtils.browserLoaded(browser);
    171      BrowserTestUtils.startLoadingURIString(browser, url);
    172      await loaded;
    173 
    174      await waitForTabTitle("JS sources received");
    175      ok(true, "Successfully verified GET_JS_SOURCES invalid request handling");
    176    }
    177  );
    178 });