Fix tests

This commit is contained in:
Pieter Vander Vennet 2024-01-11 05:37:25 +01:00
parent 503cecf4b8
commit 639253d971

View file

@ -81,6 +81,8 @@ async function validateScriptIntegrityOf(path: string): Promise<void> {
const doc = parse_html(htmlContents) const doc = parse_html(htmlContents)
// @ts-ignore // @ts-ignore
const scripts = Array.from(doc.getElementsByTagName("script")) const scripts = Array.from(doc.getElementsByTagName("script"))
// Maps source URL onto hash
const cachedHashes: Record<string, string> = {}
for (const script of scripts) { for (const script of scripts) {
let src = script.getAttribute("src") let src = script.getAttribute("src")
if (src === undefined) { if (src === undefined) {
@ -102,13 +104,15 @@ async function validateScriptIntegrityOf(path: string): Promise<void> {
if (src.startsWith("//")) { if (src.startsWith("//")) {
src = "https:" + src src = "https:" + src
} }
// Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks if (cachedHashes[src] === undefined) {
const data: string = (await ScriptUtils.Download(src))["content"] // Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks
const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data)) const data: string = (await ScriptUtils.Download(src))["content"]
const hashedStr = _arrayBufferToBase64(hashed) const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data))
console.log(src, hashedStr, integrity) cachedHashes[src] = _arrayBufferToBase64(hashed)
}
console.log(src, cachedHashes[src], integrity)
expect(integrity).to.equal( expect(integrity).to.equal(
"sha384-" + hashedStr, "sha384-" + cachedHashes[src],
"Loading a script from '" + src + "' in the file " + path + " has a mismatched checksum" "Loading a script from '" + src + "' in the file " + path + " has a mismatched checksum"
) )
} }