mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-06-13 13:41:01 +02:00
Fix the unit-tests for on-demand password handling of encrypted attachments (issue 21425)
These unit-tests used a PDF that prompted for password on document load, which meant that the on-demand password handling wasn't actually being tested as intended. Updating the unit-tests also caused the "re-prompts for encrypted attachments after incorrect passwords" test to fail, since the `INCORRECT_PASSWORD` password reason was being accidentally "swallowed" in the worker-thread.
This commit is contained in:
parent
ac64bcfa2b
commit
f3f5acc418
@ -448,36 +448,34 @@ class WorkerMessageHandler {
|
||||
* Unique attachment identifier (required).
|
||||
*/
|
||||
async function (id) {
|
||||
async function getPassword(ex) {
|
||||
const task = new WorkerTask(`PasswordException: response ${ex.code}`);
|
||||
startWorkerTask(task);
|
||||
|
||||
try {
|
||||
const res = await handler.sendWithPromise("PasswordRequest", ex);
|
||||
return res.password;
|
||||
} finally {
|
||||
finishWorkerTask(task);
|
||||
}
|
||||
}
|
||||
let passwordEx;
|
||||
|
||||
// Loop to prompt again after an incorrect password.
|
||||
while (true) {
|
||||
const password = passwordEx ? await getPassword(passwordEx) : null;
|
||||
|
||||
try {
|
||||
if (password) {
|
||||
pdfManager.updatePassword(password);
|
||||
}
|
||||
return await pdfManager.ensureCatalog("attachmentContent", [id]);
|
||||
} catch (error) {
|
||||
if (!(error instanceof PasswordException)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
const task = new WorkerTask(
|
||||
`PasswordException: response ${error.code}`
|
||||
);
|
||||
startWorkerTask(task);
|
||||
|
||||
try {
|
||||
const { password } = await handler.sendWithPromise(
|
||||
"PasswordRequest",
|
||||
error
|
||||
);
|
||||
try {
|
||||
pdfManager.updatePassword(password);
|
||||
} catch (exception) {
|
||||
if (exception instanceof PasswordException) {
|
||||
continue;
|
||||
}
|
||||
throw exception;
|
||||
}
|
||||
} finally {
|
||||
finishWorkerTask(task);
|
||||
} catch (ex) {
|
||||
if (ex instanceof PasswordException) {
|
||||
passwordEx = ex;
|
||||
continue;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1814,8 +1814,9 @@ describe("api", function () {
|
||||
|
||||
it("gets encrypted attachments when password is requested on demand", async function () {
|
||||
const loadingTask = getDocument(
|
||||
buildGetDocumentParams("encrypted-attachment.pdf")
|
||||
buildGetDocumentParams("auth-event-ef-open.pdf")
|
||||
);
|
||||
const pdfDoc = await loadingTask.promise;
|
||||
|
||||
let passwordRequests = 0;
|
||||
loadingTask.onPassword = (updatePassword, reason) => {
|
||||
@ -1824,8 +1825,6 @@ describe("api", function () {
|
||||
updatePassword("000000");
|
||||
};
|
||||
|
||||
const pdfDoc = await loadingTask.promise;
|
||||
|
||||
const attachments = await pdfDoc.getAttachments();
|
||||
const { description, filename, rawFilename } =
|
||||
attachments.get("attachment.pdf");
|
||||
@ -1843,8 +1842,9 @@ describe("api", function () {
|
||||
|
||||
it("re-prompts for encrypted attachments after incorrect passwords", async function () {
|
||||
const loadingTask = getDocument(
|
||||
buildGetDocumentParams("encrypted-attachment.pdf")
|
||||
buildGetDocumentParams("auth-event-ef-open.pdf")
|
||||
);
|
||||
const pdfDoc = await loadingTask.promise;
|
||||
|
||||
/** @type {Array<unknown>} */
|
||||
const reasons = [];
|
||||
@ -1858,8 +1858,6 @@ describe("api", function () {
|
||||
updatePassword("000000");
|
||||
};
|
||||
|
||||
const pdfDoc = await loadingTask.promise;
|
||||
|
||||
const attachments = await pdfDoc.getAttachments();
|
||||
const { description, filename, rawFilename } =
|
||||
attachments.get("attachment.pdf");
|
||||
@ -1886,24 +1884,21 @@ describe("api", function () {
|
||||
);
|
||||
let embeddedLoadingTask = null;
|
||||
|
||||
try {
|
||||
const pdfDoc = await loadingTask.promise;
|
||||
const attachments = await pdfDoc.getAttachments();
|
||||
const attachment = attachments.get("attachment.pdf");
|
||||
const pdfDoc = await loadingTask.promise;
|
||||
const attachments = await pdfDoc.getAttachments();
|
||||
const attachment = attachments.get("attachment.pdf");
|
||||
|
||||
expect(attachment).toBeDefined();
|
||||
expect(attachment.filename).toEqual("attachment.pdf");
|
||||
expect(attachment).toBeDefined();
|
||||
expect(attachment.filename).toEqual("attachment.pdf");
|
||||
|
||||
const content = await pdfDoc.getAttachmentContent("attachment.pdf");
|
||||
expect(content).toBeInstanceOf(Uint8Array);
|
||||
const content = await pdfDoc.getAttachmentContent("attachment.pdf");
|
||||
expect(content).toBeInstanceOf(Uint8Array);
|
||||
|
||||
embeddedLoadingTask = getDocument({ data: content });
|
||||
const embeddedPdfDoc = await embeddedLoadingTask.promise;
|
||||
expect(embeddedPdfDoc.numPages).toBe(1);
|
||||
} finally {
|
||||
await embeddedLoadingTask?.destroy();
|
||||
await loadingTask.destroy();
|
||||
}
|
||||
embeddedLoadingTask = getDocument({ data: content });
|
||||
const embeddedPdfDoc = await embeddedLoadingTask.promise;
|
||||
expect(embeddedPdfDoc.numPages).toBe(1);
|
||||
|
||||
await Promise.all([embeddedLoadingTask.destroy(), loadingTask.destroy()]);
|
||||
});
|
||||
|
||||
it("gets javascript with printing instructions (JS action)", async function () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user