diff options
author | João Augusto Costa Branco Marado Torres <torres.dev@disroot.org> | 2025-06-24 12:08:41 -0300 |
---|---|---|
committer | João Augusto Costa Branco Marado Torres <torres.dev@disroot.org> | 2025-06-24 12:50:43 -0300 |
commit | f9a77c5c27aede4e5978eb55d9b7af781b680a1d (patch) | |
tree | d545e325ba1ae756fc2eac66fac1001b6753c40d /src/lib/pgp/sign.test.ts |
feat!: initial commit
Signed-off-by: João Augusto Costa Branco Marado Torres <torres.dev@disroot.org>
Diffstat (limited to 'src/lib/pgp/sign.test.ts')
-rw-r--r-- | src/lib/pgp/sign.test.ts | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/lib/pgp/sign.test.ts b/src/lib/pgp/sign.test.ts new file mode 100644 index 0000000..1f9c4db --- /dev/null +++ b/src/lib/pgp/sign.test.ts @@ -0,0 +1,121 @@ +import { + assert, + assertAlmostEquals, + assertArrayIncludes, + assertEquals, + assertExists, +} from "@std/assert"; +import { describe, it } from "@std/testing/bdd"; +import { createMessage, enums, readSignature, sign } from "openpgp"; +import { Signature } from "./sign.ts"; +import { get, instanciate } from "../../utils/anonymous.ts"; +import { bufferToBase } from "../../utils/bases.ts"; +import { generateKeyPair } from "../../../tests/fixtures/setup.ts"; + +describe("Signature wrapper", () => { + const now = new Date(); + const aliceKeyPair = generateKeyPair("Alice"); + const signature = Promise.all([ + aliceKeyPair.then(get("privateKey")), + createMessage({ text: "Hello world" }), + ]).then(([privateKey, message]) => + sign({ + message, + signingKeys: privateKey, + detached: true, + format: "object", + }) + ).then((x) => readSignature({ armoredSignature: x.armor() })).then( + instanciate(Signature), + ); + + describe("Single signer", () => { + it("signingKeyIDs", async () => { + const { publicKey } = await aliceKeyPair; + const sig = await signature; + + assertEquals(sig.signingKeyIDs.length, 1); + assert(sig.signingKeyIDs[0].equals(publicKey.getKeyID())); + }); + + it("getPackets", async () => { + const sig = await signature; + + assertEquals(sig.getPackets().length, 1); + assertEquals(sig.getPackets(sig.signingKeyIDs[0]).length, 1); + }); + + describe("Packet wrapper", () => { + const packet = signature.then((x) => x.getPackets()[0]); + + it("created", async () => { + const p = await packet; + + assertExists(p.created); + assertAlmostEquals(p.created.getTime(), now.getTime()); + }); + + it("issuerKeyID and issuerFingerprint", async () => { + const { privateKey } = await aliceKeyPair; + const p = await packet; + + assertEquals(p.issuerKeyID, privateKey.getKeyID()); + assertExists(p.issuerFingerprint); + assertEquals( + bufferToBase(p.issuerFingerprint, 16), + privateKey.getFingerprint(), + ); + }); + + it("signatureType", async () => { + const p = await packet; + + assertEquals(p.signatureType, enums.signature.text); + }); + }); + }); + + const bobKeyPair = generateKeyPair("Bob"); + const multiSignature = Promise.all([ + Promise.all([ + aliceKeyPair.then(get("privateKey")), + bobKeyPair.then(get("privateKey")), + ]), + createMessage({ text: "Hello world" }), + ]).then(([signingKeys, message]) => + sign({ + message, + signingKeys, + detached: true, + format: "object", + }) + ).then((x) => readSignature({ armoredSignature: x.armor() })).then( + instanciate(Signature), + ); + + describe("with multiple signers", () => { + it("signingKeyIDs", async () => { + const { publicKey: alice } = await aliceKeyPair; + const { publicKey: bob } = await bobKeyPair; + const sig = await multiSignature; + + assertEquals(sig.signingKeyIDs.length, 2); + assertArrayIncludes(sig.signingKeyIDs, [ + alice.getKeyID(), + bob.getKeyID(), + ]); + }); + + it("getPackets", async () => { + const sig = await multiSignature; + const { publicKey: alice } = await aliceKeyPair; + const { publicKey: bob } = await bobKeyPair; + + assertEquals(sig.getPackets().length, 2); + + assertEquals(sig.getPackets(alice.getKeyID()).length, 1); + + assertEquals(sig.getPackets(bob.getKeyID()).length, 1); + }); + }); +}); |