tor-browser

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

test_host_integration.js (2731B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { ObliviousHTTP } = ChromeUtils.importESModule(
      7  "resource://gre/modules/ObliviousHTTP.sys.mjs"
      8 );
      9 const { sinon } = ChromeUtils.importESModule(
     10  "resource://testing-common/Sinon.sys.mjs"
     11 );
     12 
     13 /**
     14 * Test that host validation is properly integrated with channel loading.
     15 */
     16 add_task(async function test_channel_loading_with_host_validation() {
     17  const sandbox = sinon.createSandbox();
     18 
     19  // Set up valid OHTTP preferences
     20  Services.prefs.setCharPref(
     21    "browser.newtabpage.activity-stream.discoverystream.ohttp.configURL",
     22    "https://example.com/ohttp-config"
     23  );
     24  Services.prefs.setCharPref(
     25    "browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL",
     26    "https://example.com/ohttp-relay"
     27  );
     28 
     29  // Stub OHTTP methods
     30  sandbox
     31    .stub(ObliviousHTTP, "getOHTTPConfig")
     32    .resolves(new Uint8Array([1, 2, 3, 4]));
     33 
     34  MockOHTTPService.reset();
     35 
     36  try {
     37    // Test valid newtab-image host
     38    const validImageURL = "https://example.com/test-host-validation.jpg";
     39    const validTestURI = createTestOHTTPResourceURI(validImageURL);
     40    const validChannel = createTestChannel(validTestURI);
     41 
     42    let validLoadCompleted = false;
     43    await new Promise(resolve => {
     44      const listener = createCompletionListener(success => {
     45        validLoadCompleted = success;
     46        resolve();
     47      });
     48 
     49      validChannel.asyncOpen(listener);
     50    });
     51 
     52    Assert.ok(
     53      validLoadCompleted,
     54      "Should successfully load with valid newtab-image host"
     55    );
     56    Assert.ok(
     57      MockOHTTPService.channelCreated,
     58      "Should call OHTTP service for valid host"
     59    );
     60 
     61    // Reset mock for next test
     62    MockOHTTPService.reset();
     63 
     64    // Test invalid host
     65    const invalidHostURL =
     66      "moz-cached-ohttp://invalid-host/?url=" +
     67      encodeURIComponent("https://example.com/test-invalid-host.jpg");
     68    const invalidChannel = createTestChannel(invalidHostURL);
     69 
     70    let invalidLoadFailed = false;
     71    await new Promise(resolve => {
     72      const listener = createCompletionListener(success => {
     73        invalidLoadFailed = !success;
     74        resolve();
     75      });
     76 
     77      invalidChannel.asyncOpen(listener);
     78    });
     79 
     80    Assert.ok(invalidLoadFailed, "Should fail to load with invalid host");
     81    Assert.ok(
     82      !MockOHTTPService.channelCreated,
     83      "Should not call OHTTP service for invalid host"
     84    );
     85  } finally {
     86    sandbox.restore();
     87    Services.prefs.clearUserPref(
     88      "browser.newtabpage.activity-stream.discoverystream.ohttp.configURL"
     89    );
     90    Services.prefs.clearUserPref(
     91      "browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL"
     92    );
     93  }
     94 });