Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
fix: split vector test template
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLaw committed Jan 7, 2022
1 parent 285932d commit b63b6d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
57 changes: 34 additions & 23 deletions test/BasicVectorTest.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,43 +94,54 @@ export const AddressMosaicIdTester = <T extends Network>(
});
};

export const CipherVectorTester = (testCipherVectorFile: string): void => {
describe('cipher - test vector', () => {
export const NemCipherVectorTester = (testCipherVectorFile: string): void => {
describe('nem cipher - test vector', () => {
tester.run(
testCipherVectorFile,
(item: {
privateKey: string;
otherPublicKey: string;
tag: string;
salt: string;
iv: string;
cipherText: string;
clearText: string;
}) => {
(item: { privateKey: string; otherPublicKey: string; salt: string; iv: string; cipherText: string; clearText: string }) => {
// Arrange:
const privateKey = Key.createFromHex(item.privateKey);
const otherPublicKey = Key.createFromHex(item.otherPublicKey);
const clearText = Converter.hexToUint8(item.clearText);
const iv = Converter.hexToUint8(item.iv);
const salt = item.salt ? Converter.hexToUint8(item.salt) : undefined;
const salt = Converter.hexToUint8(item.salt);

// Act:
const encoded = salt
? NemCrypto.encode(privateKey, otherPublicKey, clearText, iv, salt)
: encode(privateKey, otherPublicKey, clearText, iv);
const payload = item.salt + item.iv + item.cipherText;
const encoded = NemCrypto.encode(privateKey, otherPublicKey, clearText, iv, salt);
const decoded = NemCrypto.decode(privateKey, otherPublicKey, Converter.hexToUint8(payload));

const decoded = salt
? NemCrypto.decode(privateKey, otherPublicKey, Converter.hexToUint8(item.salt + item.iv + item.cipherText))
: decode(privateKey, otherPublicKey, encoded);
// Assert:
const message = ` from ${item.clearText}`;
expect(Converter.uint8ToHex(encoded), `nem cipher encode ${message}`).equal(payload);
expect(Converter.uint8ToHex(decoded), `nem cipher decoded ${message}`).equal(item.clearText);
},
'nem cipher test',
);
});
};

export const SymbolCipherVectorTester = (testCipherVectorFile: string): void => {
describe('symbol cipher - test vector', () => {
tester.run(
testCipherVectorFile,
(item: { privateKey: string; otherPublicKey: string; tag: string; iv: string; cipherText: string; clearText: string }) => {
// Arrange:
const privateKey = Key.createFromHex(item.privateKey);
const otherPublicKey = Key.createFromHex(item.otherPublicKey);
const clearText = Converter.hexToUint8(item.clearText);
const iv = Converter.hexToUint8(item.iv);

// Act:
const encoded = encode(privateKey, otherPublicKey, clearText, iv);
const decoded = decode(privateKey, otherPublicKey, encoded);

// Assert:
const message = ` from ${item.clearText}`;
expect(Converter.uint8ToHex(encoded), `cipher encode ${message}`).equal(
`${salt ? item.salt : item.tag}${item.iv}${item.cipherText}`,
);
expect(Converter.uint8ToHex(decoded), `cipher decoded ${message}`).equal(item.clearText);
expect(Converter.uint8ToHex(encoded), `symbol cipher encode ${message}`).equal(`${item.tag}${item.iv}${item.cipherText}`);
expect(Converter.uint8ToHex(decoded), `symbol cipher decoded ${message}`).equal(item.clearText);
},
'cipher test',
'symbol cipher test',
);
});
};
Expand Down
9 changes: 5 additions & 4 deletions test/vector-tests/AllTests.vector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { NemKeyPair, NemNetwork, SymbolKeyPair, SymbolNetwork } from '@core';
import {
AddressMosaicIdTester,
CipherVectorTester,
DeriveVectorTester,
KeyPairVectorTester,
NemCipherVectorTester,
SignAndVerifyTester,
} from 'test/BasicVectorTest.template';
SymbolCipherVectorTester,
} from '../BasicVectorTest.template';
import path = require('path');

describe('Nem', () => {
Expand All @@ -27,7 +28,7 @@ describe('Nem', () => {

describe('test-cipher vector', () => {
const vectorFile = path.join(__dirname, '../test-vector/nem/4.test-cipher.json');
CipherVectorTester(vectorFile);
NemCipherVectorTester(vectorFile);
});
});

Expand Down Expand Up @@ -61,6 +62,6 @@ describe('Symbol', () => {

describe('test-cipher vector', () => {
const vectorFile = path.join(__dirname, '../test-vector/symbol/4.test-cipher.json');
CipherVectorTester(vectorFile);
SymbolCipherVectorTester(vectorFile);
});
});

0 comments on commit b63b6d1

Please sign in to comment.