Skip to content

Commit

Permalink
test: add tests for AlgorithmProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Aug 16, 2023
1 parent 9313d6a commit 9dd8818
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/algorithm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import * as assert from "node:assert";
import "reflect-metadata";
import { container } from "tsyringe";
import { AlgorithmProvider, diAlgorithmProvider } from "../src";

context("AlgorithmProvider", () => {

let algorithmProvider: AlgorithmProvider;
before(() => {
algorithmProvider = container.resolve<AlgorithmProvider>(diAlgorithmProvider);
assert.ok(algorithmProvider);
});

context("toAsnAlgorithm", () => {
it("should convert RSASSA-PKCS1-v1_5 with string hash to AlgorithmIdentifier", () => {
const alg = algorithmProvider.toAsnAlgorithm({
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-1",
} as Algorithm);
assert.strictEqual(alg.algorithm, "1.2.840.113549.1.1.5");
assert.strictEqual(alg.parameters, null);
});

it("should convert RSASSA-PKCS1-v1_5 with HashAlgorithm to AlgorithmIdentifier", () => {
const alg = algorithmProvider.toAsnAlgorithm({
name: "RSASSA-PKCS1-v1_5",
hash: { name: "SHA-1" },
} as Algorithm);
assert.strictEqual(alg.algorithm, "1.2.840.113549.1.1.5");
assert.strictEqual(alg.parameters, null);
});

it("should convert RSASSA-PKCS1-v1_5 without hash to AlgorithmIdentifier", () => {
const alg = algorithmProvider.toAsnAlgorithm({
name: "RSASSA-PKCS1-v1_5",
} as Algorithm);
assert.strictEqual(alg.algorithm, "1.2.840.113549.1.1.1");
assert.strictEqual(alg.parameters, null);
});

it("should throw error if RSA algorithm has hash like Algorithm but with not string name", () => {
assert.throws(() => {
algorithmProvider.toAsnAlgorithm({
name: "RSASSA-PKCS1-v1_5",
hash: { name: 1 },
} as Algorithm);
}, /Cannot get hash algorithm name/);
});

it("should convert ECDSA with SHA-1 to AlgorithmIdentifier", () => {
const alg = algorithmProvider.toAsnAlgorithm({
name: "ECDSA",
hash: "SHA-1",
} as Algorithm);
assert.strictEqual(alg.algorithm, "1.2.840.10045.4.1");
assert.strictEqual(alg.parameters, undefined);
});

it("should throw error if hash algorithm is not supported", async () => {
assert.throws(() => algorithmProvider.toAsnAlgorithm({
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-2",
} as Algorithm), /Cannot convert WebCrypto algorithm to ASN.1 algorithm/);
});
});

});

0 comments on commit 9dd8818

Please sign in to comment.