Skip to content

Commit

Permalink
Merge pull request mozilla#17367 from timvandermeij/modernize-test
Browse files Browse the repository at this point in the history
Modernize the `calculateMD5` and `verifyManifestFiles` test helper functions
  • Loading branch information
Snuffleupagus committed Dec 4, 2023
2 parents 5111b6d + ac56671 commit 476cb84
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 68 deletions.
85 changes: 33 additions & 52 deletions test/downloadutils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,79 +110,60 @@ function downloadManifestFiles(manifest, callback) {
downloadNext();
}

function calculateMD5(file, callback) {
var hash = crypto.createHash("md5");
var stream = fs.createReadStream(file);
stream.on("data", function (data) {
hash.update(data);
});
stream.on("error", function (err) {
callback(err);
});
stream.on("end", function () {
var result = hash.digest("hex");
callback(null, result);
function calculateMD5(file) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash("md5");
const stream = fs.createReadStream(file);
stream.on("data", data => hash.update(data));
stream.on("error", error => reject(error));
stream.on("end", () => resolve(hash.digest("hex")));
});
}

function verifyManifestFiles(manifest, callback) {
function verifyNext() {
if (i >= manifest.length) {
callback(error);
return;
}
var item = manifest[i];
if (fs.existsSync(item.file + ".error")) {
async function verifyManifestFiles(manifest) {
let error = false;

for (const item of manifest) {
if (fs.existsSync(`${item.file}.error`)) {
console.error(
'WARNING: File was not downloaded. See "' + item.file + '.error" file.'
`WARNING: "${item.file}" was not downloaded; see "${item.file}.error" file.`
);
error = true;
i++;
verifyNext();
return;
continue;
}
if (item.link && !fs.existsSync(item.file + ".link")) {

if (item.link && !fs.existsSync(`${item.file}.link`)) {
console.error(
`WARNING: Unneeded \`"link": true\`-entry for the "${item.id}" test.`
);
error = true;
i++;
verifyNext();
return;
continue;
}
calculateMD5(item.file, function (err, md5) {
if (err) {
console.log('WARNING: Unable to open file for reading "' + err + '".');
error = true;
} else if (!item.md5) {

try {
const md5 = await calculateMD5(item.file);
if (!item.md5) {
console.error(
'WARNING: Missing md5 for file "' +
item.file +
'". ' +
'Hash for current file is "' +
md5 +
'"'
`WARNING: MD5 hash missing for "${item.file}" (computed "${md5}").`
);
error = true;
} else if (md5 !== item.md5) {
console.error(
'WARNING: MD5 of file "' +
item.file +
'" does not match file. Expected "' +
item.md5 +
'" computed "' +
md5 +
'"'
`WARNING: MD5 hash mismatch for "${item.file}" (expected "${item.md5}", computed "${md5}").`
);
error = true;
}
i++;
verifyNext();
});
} catch (ex) {
console.log(
`WARNING: MD5 hash calculation failed for "${item.file}" ("${ex}").`
);
error = true;
}
}

if (error) {
throw new Error("Manifest validation failed");
}
var i = 0;
var error = false;
verifyNext();
}

export { downloadManifestFiles, verifyManifestFiles };
32 changes: 16 additions & 16 deletions test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,23 +1071,23 @@ async function closeSession(browser) {

function ensurePDFsDownloaded(callback) {
var manifest = getTestManifest();
downloadManifestFiles(manifest, function () {
verifyManifestFiles(manifest, function (hasErrors) {
if (hasErrors) {
console.log(
"Unable to verify the checksum for the files that are " +
"used for testing."
);
console.log(
"Please re-download the files, or adjust the MD5 " +
"checksum in the manifest for the files listed above.\n"
);
if (options.strictVerify) {
process.exit(1);
}
downloadManifestFiles(manifest, async function () {
try {
await verifyManifestFiles(manifest);
} catch {
console.log(
"Unable to verify the checksum for the files that are " +
"used for testing."
);
console.log(
"Please re-download the files, or adjust the MD5 " +
"checksum in the manifest for the files listed above.\n"
);
if (options.strictVerify) {
process.exit(1);
}
callback();
});
}
callback();
});
}

Expand Down

0 comments on commit 476cb84

Please sign in to comment.