From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- netwerk/test/unit/test_hpke_config_manager.js | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 netwerk/test/unit/test_hpke_config_manager.js (limited to 'netwerk/test/unit/test_hpke_config_manager.js') diff --git a/netwerk/test/unit/test_hpke_config_manager.js b/netwerk/test/unit/test_hpke_config_manager.js new file mode 100644 index 0000000000..66d06c3464 --- /dev/null +++ b/netwerk/test/unit/test_hpke_config_manager.js @@ -0,0 +1,112 @@ +/* Any copyright is dedicated to the Public Domain. +http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +let { HPKEConfigManager } = ChromeUtils.importESModule( + "resource://gre/modules/HPKEConfigManager.sys.mjs" +); + +const { HttpServer } = ChromeUtils.importESModule( + "resource://testing-common/httpd.sys.mjs" +); + +let gHttpServer; +let gValidRequestCount = 0; +let gFickleIsWorking = true; + +add_setup(async function () { + gHttpServer = new HttpServer(); + let invalidHandler = (req, res) => { + res.setStatusLine(req.httpVersion, 500, "Oh no, it broke"); + res.write("Uh oh, it broke."); + }; + let validHandler = (req, res) => { + res.setHeader("Content-Type", "application/ohttp-keys"); + res.write("1234"); + gValidRequestCount++; + }; + + gHttpServer.registerPathHandler("/.wellknown/invalid", invalidHandler); + gHttpServer.registerPathHandler("/.wellknown/valid", validHandler); + + gHttpServer.registerPathHandler("/.wellknown/fickle", (req, res) => { + if (gFickleIsWorking) { + return validHandler(req, res); + } + return invalidHandler(req, res); + }); + + gHttpServer.start(-1); +}); + +function getLocalURL(path) { + return `http://localhost:${gHttpServer.identity.primaryPort}/.wellknown/${path}`; +} + +add_task(async function test_broken_url_returns_null() { + Assert.equal(await HPKEConfigManager.get(getLocalURL("invalid")), null); +}); + +add_task(async function test_working_url_returns_data() { + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("valid")), + new TextEncoder().encode("1234") + ); +}); + +add_task(async function test_we_only_request_once() { + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("valid")), + new TextEncoder().encode("1234") + ); + let oldRequestCount = gValidRequestCount; + + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("valid")), + new TextEncoder().encode("1234") + ); + Assert.equal( + oldRequestCount, + gValidRequestCount, + "Shouldn't have made another request." + ); +}); + +add_task(async function test_maxAge_forces_refresh() { + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("valid")), + new TextEncoder().encode("1234") + ); + let oldRequestCount = gValidRequestCount; + + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("valid"), { maxAge: 0 }), + new TextEncoder().encode("1234") + ); + Assert.equal( + oldRequestCount + 1, + gValidRequestCount, + "Should have made another request due to maxAge." + ); +}); + +add_task(async function test_maxAge_handling_of_invalid_requests() { + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("fickle")), + new TextEncoder().encode("1234") + ); + + gFickleIsWorking = false; + + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("fickle"), { maxAge: 0 }), + null + ); + + Assert.deepEqual( + await HPKEConfigManager.get(getLocalURL("fickle")), + new TextEncoder().encode("1234"), + "Should still have the cached config if no max age is passed." + ); +}); -- cgit v1.2.3