tor-browser

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

test_host_configuration.js (2852B)


      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 host validation and configuration mapping.
     15 */
     16 add_task(async function test_host_validation_and_mapping() {
     17  const protocolHandler = new MozCachedOHTTPProtocolHandler();
     18 
     19  // Test invalid host throws error in getOHTTPGatewayConfigAndRelayURI
     20  await Assert.rejects(
     21    protocolHandler.getOHTTPGatewayConfigAndRelayURI("invalid-host"),
     22    /Unrecognized host for OHTTP config: invalid-host/,
     23    "Should reject invalid host in getOHTTPConfig"
     24  );
     25 });
     26 
     27 /**
     28 * Test lazy preference loading and caching behavior.
     29 */
     30 add_task(async function test_lazy_preference_loading() {
     31  const protocolHandler = new MozCachedOHTTPProtocolHandler();
     32 
     33  // Set up preferences for testing
     34  const gatewayConfigURL = "https://example.com/ohttp-config";
     35  const relayURL = "https://example.com/ohttp-relay";
     36 
     37  Services.prefs.setCharPref(
     38    "browser.newtabpage.activity-stream.discoverystream.ohttp.configURL",
     39    gatewayConfigURL
     40  );
     41  Services.prefs.setCharPref(
     42    "browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL",
     43    relayURL
     44  );
     45 
     46  // Stub ObliviousHTTP.getOHTTPConfig to avoid network requests
     47  const stub = sinon
     48    .stub(ObliviousHTTP, "getOHTTPConfig")
     49    .resolves(new Uint8Array([1, 2, 3, 4]));
     50 
     51  try {
     52    // First call should load preferences lazily
     53    const { ohttpGatewayConfig: config1 } =
     54      await protocolHandler.getOHTTPGatewayConfigAndRelayURI("newtab-image");
     55    Assert.ok(config1 instanceof Uint8Array, "Should return OHTTP config");
     56    Assert.ok(
     57      stub.calledWith(gatewayConfigURL),
     58      "Should call ObliviousHTTP with correct config URL"
     59    );
     60 
     61    // Second call should use cached preferences
     62    stub.resetHistory();
     63    const { ohttpGatewayConfig: config2 } =
     64      await protocolHandler.getOHTTPGatewayConfigAndRelayURI("newtab-image");
     65    Assert.ok(
     66      config2 instanceof Uint8Array,
     67      "Should return OHTTP config on second call"
     68    );
     69    Assert.ok(
     70      stub.calledWith(gatewayConfigURL),
     71      "Should still call ObliviousHTTP with same config URL"
     72    );
     73 
     74    // Test relay URI loading
     75    const { relayURI: relay1 } =
     76      await protocolHandler.getOHTTPGatewayConfigAndRelayURI("newtab-image");
     77    Assert.equal(relay1.spec, relayURL, "Should return correct relay URI");
     78  } finally {
     79    stub.restore();
     80    Services.prefs.clearUserPref(
     81      "browser.newtabpage.activity-stream.discoverystream.ohttp.configURL"
     82    );
     83    Services.prefs.clearUserPref(
     84      "browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL"
     85    );
     86  }
     87 });