|
| 1 | +// Engine exception reconciliation tests. |
| 2 | + |
| 3 | +test("engine-thrown DOMException is a true DOMException instance (NotSupportedError)", async () => { |
| 4 | + let caught = null; |
| 5 | + try { |
| 6 | + await crypto.subtle.digest("UNKNOWN", new Uint8Array()); |
| 7 | + } catch (e) { |
| 8 | + caught = e; |
| 9 | + } |
| 10 | + |
| 11 | + assertEquals(caught !== null, true); |
| 12 | + assertEquals(caught instanceof DOMException, true); |
| 13 | + assertEquals(caught.name, "NotSupportedError"); |
| 14 | + assertEquals(caught.message.includes("unsupported digest"), true); |
| 15 | +}); |
| 16 | + |
| 17 | +test("engine-thrown DOMException with a different name (DataError)", async () => { |
| 18 | + let caught = null; |
| 19 | + try { |
| 20 | + // Importing invalid key material throws a DataError from the native op |
| 21 | + await crypto.subtle.importKey("spki", new Uint8Array([1, 2, 3]), { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, true, ["verify"]); |
| 22 | + } catch (e) { |
| 23 | + caught = e; |
| 24 | + } |
| 25 | + |
| 26 | + assertEquals(caught !== null, true); |
| 27 | + assertEquals(caught instanceof DOMException, true); |
| 28 | + assertEquals(caught.name, "DataError"); |
| 29 | +}); |
| 30 | + |
| 31 | +test("engine-thrown native TypeError is a true TypeError instance", async () => { |
| 32 | + let caught = null; |
| 33 | + try { |
| 34 | + // Passing a number where a string is expected |
| 35 | + await crypto.subtle.digest(123, new Uint8Array()); |
| 36 | + } catch (e) { |
| 37 | + caught = e; |
| 38 | + } |
| 39 | + |
| 40 | + assertEquals(caught !== null, true); |
| 41 | + assertEquals(caught instanceof TypeError, true); |
| 42 | + assertEquals(caught.name, "TypeError"); |
| 43 | +}); |
| 44 | + |
| 45 | +test("engine-thrown DOMException with OperationError name", async () => { |
| 46 | + let caught = null; |
| 47 | + try { |
| 48 | + // AES-GCM decrypt with corrupted ciphertext throws OperationError |
| 49 | + const key = await crypto.subtle.generateKey({ name: "AES-GCM", length: 128 }, false, ["encrypt", "decrypt"]); |
| 50 | + const iv = new Uint8Array(12); |
| 51 | + const ct = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, new Uint8Array([1, 2, 3])); |
| 52 | + // Corrupt the ciphertext tag |
| 53 | + const corrupted = new Uint8Array(ct); |
| 54 | + corrupted[corrupted.length - 1] ^= 1; |
| 55 | + await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, corrupted); |
| 56 | + } catch (e) { |
| 57 | + caught = e; |
| 58 | + } |
| 59 | + |
| 60 | + assertEquals(caught !== null, true); |
| 61 | + assertEquals(caught instanceof DOMException, true); |
| 62 | + assertEquals(caught.name, "OperationError"); |
| 63 | +}); |
0 commit comments