Skip to content

Commit ae70a5d

Browse files
authored
Merge pull request #20969 from calixteman/fix_test_comparaison
Strip private ancillary PNG chunks before comparing images in ref tests
2 parents 2bcf2bb + 3850280 commit ae70a5d

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

test/test.mjs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ import { WebServer } from "./webserver.mjs";
3333

3434
const __dirname = import.meta.dirname;
3535

36+
// Strip private ancillary PNG chunks before comparing snapshots. Firefox adds
37+
// a `deBG` chunk with a per-session unique ID to canvas.toDataURL("image/png")
38+
// output, causing false failures when ref and test were captured in different
39+
// browser sessions.
40+
// For reference:
41+
// https://searchfox.org/firefox-main/rev/1427c88632d1474d2653928745d78feca1a64ee0/image/encoders/png/nsPNGEncoder.cpp#367
42+
function stripPrivatePngChunks(buf) {
43+
const PNG_SIGNATURE = 8;
44+
let pos = PNG_SIGNATURE;
45+
const chunks = [];
46+
const pre_chunk_data = 8; // len (4) + type (4)
47+
const post_chunk_data = 4; // CRC
48+
while (pos < buf.length) {
49+
const len = buf.readUInt32BE(pos);
50+
const type = buf.slice(pos + 4, pos + 8).toString("latin1");
51+
const to_skip = pre_chunk_data + len + post_chunk_data;
52+
// Keep critical chunks (uppercase first letter) and public ancillary
53+
// chunks (uppercase second letter). Drop private ancillary chunks
54+
// (lowercase second letter), e.g. "deBG" added by Firefox.
55+
// See PNG specification for details on chunk types:
56+
// https://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#:~:text=4%2E3%2E,-Summary
57+
if (
58+
type[0] === type[0].toUpperCase() ||
59+
type[1] === type[1].toUpperCase()
60+
) {
61+
chunks.push(buf.slice(pos, pos + to_skip));
62+
}
63+
pos += to_skip;
64+
}
65+
return Buffer.concat([buf.slice(0, PNG_SIGNATURE), ...chunks]);
66+
}
67+
3668
function parseOptions() {
3769
const { values } = parseArgs({
3870
args: process.argv.slice(2),
@@ -395,7 +427,9 @@ function checkEq(task, results, browser, masterMode) {
395427
}
396428
} else {
397429
refSnapshot = fs.readFileSync(refPath);
398-
eq = refSnapshot.toString("hex") === testSnapshot.toString("hex");
430+
eq =
431+
stripPrivatePngChunks(refSnapshot).toString("hex") ===
432+
stripPrivatePngChunks(testSnapshot).toString("hex");
399433
if (!eq) {
400434
console.log(
401435
"TEST-UNEXPECTED-FAIL | " +
@@ -899,6 +933,7 @@ async function startBrowser({
899933
"browser.ml.linkPreview.enabled": false,
900934
"browser.tabs.groups.smart.enabled": false,
901935
"browser.tabs.groups.smart.userEnabled": false,
936+
"privacy.baselineFingerprintingProtection": false,
902937
...extraPrefsFirefox,
903938
};
904939
}

0 commit comments

Comments
 (0)