From 7858687e5bb4908a0b47ea62a9d581a16de6cf3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Thu, 15 Dec 2022 17:29:36 +0000 Subject: [PATCH] cli: test for signed-integrity gen-keypair action (ref. #66) --- .../plugins/signed-integrity.test.js | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/__denotests__/plugins/signed-integrity.test.js b/__denotests__/plugins/signed-integrity.test.js index b4a9990..3181628 100644 --- a/__denotests__/plugins/signed-integrity.test.js +++ b/__denotests__/plugins/signed-integrity.test.js @@ -3,7 +3,8 @@ import { assertThrows, assertRejects, assertEquals, - assertStringIncludes + assertStringIncludes, + assertObjectMatch } from "https://deno.land/std@0.167.0/testing/asserts.ts"; // this needs to be the same as the pubkey in: @@ -271,3 +272,63 @@ Deno.test("gen-integrity signs the data correctly", async () => { jwt['./__denotests__/mocks/hello.txt'], pubkey)) }) + +Deno.test("get-pubkey works correctly", async () => { + const bi = await import('../../plugins/signed-integrity/cli.js') + const gp = bi.actions["get-pubkey"] + assertRejects(gp.run, Error, "No keyfile provided.") + assertRejects(async ()=>{ + await gp.run('no-such-file') + }, Error, "No such file or directory") + assertRejects(async ()=>{ + await gp.run(['no-such-file']) + }, Error, "No such file or directory") + assertEquals( + await gp.run('./__denotests__/mocks/keyfile.json'), + '{"kty":"EC","crv":"P-384","alg":"ES384","x":"rrFawYTuFo8ZjoDxaztUU-c_RAwjw1Y9Tp3j4nH4WsY2Zlizf40Mvz_0BUkVVZCw","y":"HaFct6PVK2CQ7ZT2SHClnN-knmGfjY_DFwc6qrAu1s0DFZ8fEUuNdmkTlj9T4NQw","key_ops":["verify"],"ext":true}' + ) + assertEquals( + await gp.run(['./__denotests__/mocks/keyfile.json', 'irrelevant']), + '{"kty":"EC","crv":"P-384","alg":"ES384","x":"rrFawYTuFo8ZjoDxaztUU-c_RAwjw1Y9Tp3j4nH4WsY2Zlizf40Mvz_0BUkVVZCw","y":"HaFct6PVK2CQ7ZT2SHClnN-knmGfjY_DFwc6qrAu1s0DFZ8fEUuNdmkTlj9T4NQw","key_ops":["verify"],"ext":true}' + ) +}); + +Deno.test("gen-keypair works correctly", async () => { + const bi = await import('../../plugins/signed-integrity/cli.js') + const gk = bi.actions["gen-keypair"] + const keypair = JSON.parse(await gk.run()) + assert('privateKey' in keypair) + assert('x' in keypair.privateKey) + assert('y' in keypair.privateKey) + assert('d' in keypair.privateKey) + assertObjectMatch( + keypair.privateKey, + { + kty: "EC", + crv: "P-384", + alg: "ES384", + key_ops: [ + "sign" + ], + ext: true + } + ) + assert('publicKey' in keypair) + assert('x' in keypair.publicKey) + assert('y' in keypair.publicKey) + assert(!('d' in keypair.publicKey)) + assertObjectMatch( + keypair.publicKey, + { + kty: "EC", + crv: "P-384", + alg: "ES384", + key_ops: [ + "verify" + ], + ext: true + } + ) + assert((keypair.privateKey.x == keypair.publicKey.x)) + assert((keypair.privateKey.y == keypair.publicKey.y)) +});