Skip to content

Commit

Permalink
test: added tests serializing and deserializing of CryptoSignaturePri…
Browse files Browse the repository at this point in the history
…vateKeyHandle
  • Loading branch information
WyvernIXTL committed Mar 4, 2025
1 parent 089d534 commit ed0644d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 15 deletions.
89 changes: 74 additions & 15 deletions test/crypto-layer/CryptoSignaturePrivateKeyHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,40 @@ import { CryptoHashAlgorithm, CryptoSignature } from "@nmshd/crypto";
import { KeyPairSpec } from "@nmshd/rs-crypto-types";
import { expect } from "chai";
import { CoreBuffer } from "src/CoreBuffer";
import { CryptoSignaturePrivateKeyHandle } from "src/crypto-layer/signature/CryptoSignaturePrivateKeyHandle";
import { CryptoSignatures } from "src/signature/CryptoSignatures";
import { expectCryptoSignatureKeypairHandle } from "./CryptoSignatureKeypairHandle.test";

export async function expectCryptoSignaturePrivateKeyHandle(
value: CryptoSignaturePrivateKeyHandle,
id: string,
spec: KeyPairSpec,
providerName: string
): Promise<void> {
expect(value).to.be.instanceOf(CryptoSignaturePrivateKeyHandle);
expect(value.id).to.equal(id);
expect(value.providerName).to.equal(providerName);
expect(value.spec).to.deep.equal(spec);
expect(value.keyPairHandle).to.be.ok;
expect(value.provider).to.be.ok;
expect(await value.keyPairHandle.id()).to.equal(id);
expect(await value.provider.providerName()).to.equal(providerName);
}

export class CryptoSignaturePrivateKeyHandleTest {
public static run(): void {
describe("CryptoSignaturePrivateKeyHandle", function () {
describe("sign() with SoftwareProvider", function () {
it("sign() and verify() with P256", async function () {
const spec: KeyPairSpec = {
asym_spec: "P256",
cipher: null,
signing_hash: "Sha2_512",
ephemeral: false,
non_exportable: false
};
const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(
{ providerName: "SoftwareProvider" },
spec
);
await expectCryptoSignatureKeypairHandle(cryptoKeyPairHandle, "SoftwareProvider", spec);
describe("CryptoSignaturePrivateKeyHandle SoftwareProvider P256 Sha2_512", function () {
const spec: KeyPairSpec = {
asym_spec: "P256",
cipher: null,
signing_hash: "Sha2_512",
ephemeral: false,
non_exportable: false
};
const providerIdent = { providerName: "SoftwareProvider" };

it("sign and verify", async function () {
const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec);

const data = new CoreBuffer("0123456789ABCDEF");
const signature = await CryptoSignatures.sign(
Expand All @@ -41,6 +55,51 @@ export class CryptoSignaturePrivateKeyHandleTest {
true
);
});

it("toJSON and fromJSON", async function () {
const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec);
const privateKeyHandle = cryptoKeyPairHandle.privateKey;
const id = privateKeyHandle.id;
const providerName = privateKeyHandle.providerName;

const serializedPrivateKeyHandle = privateKeyHandle.toJSON();
expect(serializedPrivateKeyHandle).to.be.instanceOf(Object);
expect(serializedPrivateKeyHandle.cid).to.equal(id);
expect(serializedPrivateKeyHandle.pnm).to.equal(providerName);
expect(serializedPrivateKeyHandle.spc).to.deep.equal(spec);
expect(serializedPrivateKeyHandle["@type"]).to.equal("CryptoSignaturePrivateKeyHandle");

const loadedPrivateKeyHandle =
await CryptoSignaturePrivateKeyHandle.fromJSON(serializedPrivateKeyHandle);
await expectCryptoSignaturePrivateKeyHandle(loadedPrivateKeyHandle, id, spec, providerName);
});

it("toBase64 and fromBase64", async function () {
const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec);
const privateKeyHandle = cryptoKeyPairHandle.privateKey;
const id = privateKeyHandle.id;
const providerName = privateKeyHandle.providerName;

const serializedPrivateKey = privateKeyHandle.toBase64();
expect(serializedPrivateKey).to.be.ok;
const deserializedPrivateKey = CryptoSignaturePrivateKeyHandle.fromBase64(serializedPrivateKey);
await expectCryptoSignaturePrivateKeyHandle(await deserializedPrivateKey, id, spec, providerName);
});

// eslint-disable-next-line jest/expect-expect
it("from", async function () {
const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec);
const privateKeyHandle = cryptoKeyPairHandle.privateKey;
const id = privateKeyHandle.id;
const providerName = privateKeyHandle.providerName;

const loadedPrivateKeyHandle = await CryptoSignaturePrivateKeyHandle.from({
spec: spec,
id: id,
providerName: providerName
});
await expectCryptoSignaturePrivateKeyHandle(loadedPrivateKeyHandle, id, spec, providerName);
});
});
});
}
Expand Down
3 changes: 3 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getAllProviders,
getProviderCapabilities
} from "@nmshd/rs-crypto-node";
import chai from "chai";
import { BufferTest } from "./BufferTest.test";
import { CryptoLayerProviderTest } from "./crypto-layer/CryptoLayerProviderTest.test";
import { CryptoSignatureKeypairHandleTest } from "./crypto-layer/CryptoSignatureKeypairHandle.test";
Expand All @@ -24,6 +25,8 @@ import { CryptoSignatureTest } from "./crypto/CryptoSignature.test";
import { CryptoStateTest } from "./crypto/CryptoStateTest.test";
import { SodiumWrapperTest } from "./crypto/SodiumWrapperTest.test";

chai.config.truncateThreshold = 0;

// This is valid: https://mochajs.org/#delayed-root-suite
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async function () {
Expand Down

0 comments on commit ed0644d

Please sign in to comment.