Skip to content

Commit

Permalink
test: test key exporting according to type of key returned
Browse files Browse the repository at this point in the history
  • Loading branch information
starkfire committed Aug 3, 2023
1 parent b7fc533 commit 1975340
Showing 1 changed file with 47 additions and 89 deletions.
136 changes: 47 additions & 89 deletions test/exportKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,73 @@ import exportKey from "../src/utils/exportKey";
import CMatrix from "../src/utils/compatibilityMatrix";
import { SYNC_ALGS, ASYNC_ALGS } from "../src/algorithms";

import getRSAType from "../src/rsa/getRSAType";
async function exportSecretKeys(signatureAlgorithm: string, jwsHeader: string) {
for (const alg of SYNC_ALGS[signatureAlgorithm]) {
let secret = await generateSecret(alg);

describe("Test exportKey()", () => {
test("should be able to export keys under HMAC_SHA algorithms", async () => {
for (const alg of SYNC_ALGS['HMAC_SHA']) {
let secret = await generateSecret(alg);
if (secret && isCryptoKey(secret)) {
for (let format in CMatrix) {
const isSupportedByFormat = CMatrix[format].includes(jwsHeader);

if (secret && isCryptoKey(secret)) {
for(let format in CMatrix) {
const isSupportedByFormat = CMatrix[format].includes('HMAC');

if (isSupportedByFormat) {
let exported = await exportKey(secret, format);
if (isSupportedByFormat) {
let exported = await exportKey(secret, format);

expect(exported).not.toBeUndefined();
}
expect(exported).not.toBeUndefined();
}
}
}
});
}
}

test("should be able to export keys under AES_KW algorithms", async () => {
for (const alg of SYNC_ALGS['AES_KW']) {
let secret = await generateSecret(alg);
async function exportPairedKeys(signatureAlgorithm: string) {
for (const alg of ASYNC_ALGS[signatureAlgorithm]) {
let secret = await generateKeyPair(alg);

if (secret && isCryptoKey(secret)) {
for (let format in CMatrix) {
const isSupportedByFormat = CMatrix[format].includes('AES-KW');
if (secret) {
let { publicKey, privateKey } = secret;

if (isSupportedByFormat) {
let exported = await exportKey(secret, format);
// JWK
let jwk_pub = await exportKey(publicKey, 'jwk');
let jwk_pvt = await exportKey(privateKey, 'jwk');

expect(exported).not.toBeUndefined();
}
}
// SPKI
let spki_pub = await exportKey(publicKey, 'spki');

// PKCS8
let pkcs8_pvt = await exportKey(privateKey, 'pkcs8');

if (signatureAlgorithm === 'ECDSA') {
let raw_pub = await exportKey(publicKey, 'raw');

expect(raw_pub).not.toBeUndefined();
}

expect(jwk_pub).not.toBeUndefined();
expect(jwk_pvt).not.toBeUndefined();
expect(spki_pub).not.toBeUndefined();
expect(pkcs8_pvt).not.toBeUndefined();
}
});
}
}

test("should be able to export keys under AES_GCM algorithms", async () => {
for (const alg of SYNC_ALGS['AES_GCM']) {
let secret = await generateSecret(alg);

if (secret && isCryptoKey(secret)) {
for (let format in CMatrix) {
const isSupportedByFormat = CMatrix[format].includes('AES-GCM');
describe("Test exportKey()", () => {
test("should be able to export keys under HMAC_SHA algorithms", async () => {
await exportSecretKeys('HMAC_SHA', 'HMAC');
});

if (isSupportedByFormat) {
let exported = await exportKey(secret, format);
test("should be able to export keys under AES_KW algorithms", async () => {
await exportSecretKeys('AES_KW', 'AES-KW');
});

expect(exported).not.toBeUndefined();
}
}
}
}
test("should be able to export keys under AES_GCM algorithms", async () => {
await exportSecretKeys('AES_GCM', 'AES-GCM');
});

test("should be able to export keys under RSA algorithms", async () => {
for (const alg of ASYNC_ALGS['RSA']) {
let secret = await generateKeyPair(alg);
let rsaType = getRSAType(alg);

if (secret && rsaType) {
let { publicKey, privateKey } = secret;

// JWK
let jwk_pub = await exportKey(publicKey, 'jwk');
let jwk_pvt = await exportKey(privateKey, 'jwk');

// SPKI
let spki_pub = await exportKey(publicKey, 'spki');

// PKCS8
let pkcs8_pvt = await exportKey(privateKey, 'pkcs8');

expect(jwk_pub).not.toBeUndefined();
expect(jwk_pvt).not.toBeUndefined();
expect(spki_pub).not.toBeUndefined();
expect(pkcs8_pvt).not.toBeUndefined();
}
}
await exportPairedKeys('RSA');
});

test("should be able to export keys under ECDSA algorithms", async () => {
for (const alg of ASYNC_ALGS['ECDSA']) {
let secret = await generateKeyPair(alg);

if (secret) {
let { publicKey, privateKey } = secret;

// JWK
let jwk_pub = await exportKey(publicKey, 'jwk');
let jwk_pvt = await exportKey(privateKey, 'jwk');

// SPKI
let spki_pub = await exportKey(publicKey, 'spki');

// PKCS8
let pkcs8_pvt = await exportKey(privateKey, 'pkcs8');

// raw
let raw_pub = await exportKey(publicKey, 'raw');

expect(jwk_pub).not.toBeUndefined();
expect(jwk_pvt).not.toBeUndefined();
expect(spki_pub).not.toBeUndefined();
expect(pkcs8_pvt).not.toBeUndefined();
expect(raw_pub).not.toBeUndefined();
}
}
await exportPairedKeys('ECDSA');
});
});

0 comments on commit 1975340

Please sign in to comment.