Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
/* Any copyright is dedicated to the Public Domain.
"use strict";
const { ObliviousHTTP } = ChromeUtils.importESModule(
"resource://gre/modules/ObliviousHTTP.sys.mjs"
);
const { sinon } = ChromeUtils.importESModule(
);
/**
* Test that the protocol handler rejects malformed moz-cached-ohttp URIs.
*/
add_task(async function test_malformed_uri_rejection() {
const sandbox = sinon.createSandbox();
try {
// Stub OHTTP methods to avoid network calls
sandbox
.stub(ObliviousHTTP, "getOHTTPConfig")
.resolves(new Uint8Array([1, 2, 3, 4]));
sandbox.stub(ObliviousHTTP, "ohttpRequest").resolves({
ok: false,
status: 400,
statusText: "Bad Request",
});
const protocolHandler = new MozCachedOHTTPProtocolHandler();
const malformedURIs = [
];
// Use system principal for test environment
const principal = Services.scriptSecurityManager.getSystemPrincipal();
const loadInfo = NetUtil.newChannel({
uri: httpURI,
loadingPrincipal: principal,
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
}).loadInfo;
for (const uriString of malformedURIs) {
const uri = Services.io.newURI(uriString);
const channel = protocolHandler.newChannel(uri, loadInfo);
// Test that the channel properly rejects the malformed URI
let errorOccurred = false;
await new Promise(resolve => {
const listener = createCompletionListener(success => {
errorOccurred = !success;
resolve();
});
channel.asyncOpen(listener);
});
Assert.ok(errorOccurred, `Should reject malformed URI: ${uriString}`);
}
} finally {
sandbox.restore();
}
});
/**
* Test that the protocol handler accepts valid HTTPS URLs.
*/
add_task(async function test_valid_url_acceptance() {
const sandbox = sinon.createSandbox();
try {
// Mock successful OHTTP responses
sandbox
.stub(ObliviousHTTP, "getOHTTPConfig")
.resolves(new Uint8Array([1, 2, 3, 4]));
const validURIs = [
createTestOHTTPResourceURI(
),
];
for (const uriString of validURIs) {
const channel = createTestChannel(uriString);
// Channel should be created successfully
Assert.ok(channel, `Should create channel for valid URI: ${uriString}`);
Assert.equal(channel.URI.spec, uriString, "Channel URI should match");
// Test that the channel can load successfully
let success = false;
await new Promise(resolve => {
const listener = createCompletionListener(channelSuccess => {
success = channelSuccess;
resolve();
});
channel.asyncOpen(listener);
});
Assert.ok(success, `Should successfully load valid URI: ${uriString}`);
}
} finally {
sandbox.restore();
}
});