Skip to content

Commit

Permalink
add a few lines of coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed Feb 19, 2025
1 parent 38d6897 commit 38e4a9d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/tdf3/src/models/encryption-information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class SplitKey {
}

async getKeyAccessObjects(policy: Policy, keyInfo: KeyInfo): Promise<KeyAccessObject[]> {
const splitIds = [...new Set(this.keyAccess.map(({ sid }) => sid))].sort((a, b) =>
const splitIds = [...new Set(this.keyAccess.map(({ sid }) => sid))].sort((a = '', b = '') =>
a.localeCompare(b)
);
const unwrappedKeySplitBuffers = await keySplit(
Expand All @@ -93,7 +93,7 @@ export class SplitKey {
const keyAccessObjects = [];
for (const item of this.keyAccess) {
// use the key split to encrypt metadata for each key access object
const unwrappedKeySplitBuffer = splitsByName[item.sid];
const unwrappedKeySplitBuffer = splitsByName[item.sid || ''];
const unwrappedKeySplitBinary = Binary.fromArrayBuffer(unwrappedKeySplitBuffer.buffer);

const metadata = item.metadata || '';
Expand Down
8 changes: 2 additions & 6 deletions lib/tdf3/src/models/key-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export type KeyAccessType = 'remote' | 'wrapped' | 'ec-wrapped';

export const schemaVersion = '1.0';

export function isRemote(keyAccessJSON: KeyAccess | KeyAccessObject): boolean {
return keyAccessJSON.type === 'remote';
}

export class ECWrapped {
readonly type = 'ec-wrapped';
readonly ephemeralKeyPair: Promise<CryptoKeyPair>;
Expand All @@ -25,7 +21,7 @@ export class ECWrapped {
public readonly kid: string | undefined,
public readonly publicKey: string,
public readonly metadata: unknown,
public readonly sid: string
public readonly sid?: string
) {
this.ephemeralKeyPair = crypto.subtle.generateKey(
{
Expand Down Expand Up @@ -96,7 +92,7 @@ export class Wrapped {
public readonly kid: string | undefined,
public readonly publicKey: string,
public readonly metadata: unknown,
public readonly sid: string
public readonly sid?: string
) {}

async write(
Expand Down
80 changes: 80 additions & 0 deletions lib/tests/mocha/unit/key-access.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { expect } from 'chai';

import { ECWrapped, Wrapped } from '../../../tdf3/src/models/key-access.js';
import { Policy } from '../../../tdf3/src/models/policy.js';
import { base64 } from '../../../src/encodings/index.js';

describe('ECWrapped', () => {
const url = 'https://example.com';
const kid = 'test-kid';
const publicKey = 'test-public-key';
const metadata = { key: 'value' };
const sid = 'test-sid';
const policy: Policy = { uuid: 'test-policy' };
const dek = new Uint8Array([1, 2, 3, 4, 5]);
const encryptedMetadataStr = 'encrypted-metadata';

['ECWrapped', 'Wrapped'].forEach((type) => {
describe(type, () => {
it(`should write and return a KeyAccessObject for ${type}`, async () => {
const wrappedInstance = new (type === 'ECWrapped' ? ECWrapped : Wrapped)(
url,
kid,
publicKey,
metadata,
sid
);

const keyAccessObject = await wrappedInstance.write(policy, dek, encryptedMetadataStr);

expect(keyAccessObject).to.have.property('type', type.toLowerCase());
expect(keyAccessObject).to.have.property('url', url);
expect(keyAccessObject).to.have.property('protocol', 'kas');
expect(keyAccessObject).to.have.property('wrappedKey');
expect(keyAccessObject).to.have.property(
'encryptedMetadata',
base64.encode(encryptedMetadataStr)
);
expect(keyAccessObject).to.have.property('policyBinding');
expect(keyAccessObject.policyBinding).to.have.property('alg', 'HS256');
expect(keyAccessObject.policyBinding).to.have.property('hash');
expect(keyAccessObject).to.have.property('schemaVersion', '1.0');
expect(keyAccessObject).to.have.property('ephemeralPublicKey', 'ephemeral-public-key-pem');
expect(keyAccessObject).to.have.property('kid', kid);
expect(keyAccessObject).to.have.property('sid', sid);
});
});
});

it('should initialize ECWrapped with correct properties', async () => {
const ecWrapped = new ECWrapped(url, kid, publicKey, metadata, sid);
expect(ecWrapped.type).to.equal('ec-wrapped');
const ek = await ecWrapped.ephemeralKeyPair;
expect(ek).to.have('publicKey');
});

it('should initialize Wrapped with correct properties', async () => {
const wrapped = new Wrapped(url, kid, publicKey, metadata, sid);
expect(wrapped.type).to.equal('wrapped');
});

it(`should handle undefined kid for ECWrapped`, () => {
const wrappedInstance = new ECWrapped(url, undefined, publicKey, metadata);
expect(wrappedInstance.kid).to.be.undefined;
});

it(`should handle undefined kid for Wrapped`, () => {
const wrappedInstance = new Wrapped(url, undefined, publicKey, metadata);
expect(wrappedInstance.kid).to.be.undefined;
});

it(`should handle undefined sid for ECWrapped`, () => {
const wrappedInstance = new ECWrapped(url, kid, publicKey, metadata);
expect(wrappedInstance.sid).to.be.undefined;
});

it(`should handle undefined sid for Wrapped`, () => {
const wrappedInstance = new Wrapped(url, kid, publicKey, metadata);
expect(wrappedInstance.sid).to.be.undefined;
});
});

0 comments on commit 38e4a9d

Please sign in to comment.