Skip to content

Commit

Permalink
wip: jest - crypto unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nimish-ks committed Oct 21, 2023
1 parent 575f07a commit fb48b05
Show file tree
Hide file tree
Showing 4 changed files with 1,571 additions and 20 deletions.
7 changes: 7 additions & 0 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1'
}
};
6 changes: 5 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@graphql-codegen/client-preset": "2.1.1",
"@graphql-codegen/typescript-react-apollo": "^3.3.7",
"@types/country-flag-icons": "^1.2.0",
"@types/jest": "^29.5.6",
"@types/jsonwebtoken": "^9.0.1",
"@types/libsodium-wrappers-sumo": "^0.7.5",
"@types/react-icons": "^3.0.0",
Expand All @@ -60,8 +61,11 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"jest": "^29.7.0",
"jest-module-name-mapper": "^0.1.5",
"postcss": "^8.4.21",
"prettier": "^2.8.4",
"tailwindcss": "^3.2.7"
"tailwindcss": "^3.2.7",
"ts-jest": "^29.1.1"
}
}
81 changes: 81 additions & 0 deletions frontend/tests/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
randomKeyPair,
clientSessionKeys,
encryptAsymmetric,
} from '../utils/crypto';

import * as sodium from 'libsodium-wrappers-sumo'; // <-- Import sodium

// Mocking libsodium-wrappers-sumo
jest.mock('libsodium-wrappers-sumo', () => ({
ready: Promise.resolve(),
crypto_kx_keypair: jest.fn().mockResolvedValue({
publicKey: Uint8Array.from([1, 2, 3]),
privateKey: Uint8Array.from([4, 5, 6])
}),
from_hex: jest.fn(),
crypto_kx_client_session_keys: jest.fn(),
randombytes_buf: jest.fn().mockReturnValue(Uint8Array.from([/* some random bytes */])),
crypto_aead_xchacha20poly1305_ietf_encrypt: jest.fn().mockReturnValue(Uint8Array.from([/* some bytes representing encrypted data */])),
to_hex: jest.fn().mockImplementation((data: Uint8Array) => {
return Array.from(data).map(byte => byte.toString(16)).join('');
}),
// ... other mocks
}));

const mockEncryptString = jest.fn();

jest.doMock('./../utils/crypto', () => {
return {
...jest.requireActual('./../utils/crypto'),
encryptString: mockEncryptString
};
});

describe('encryptAsymmetric', () => {
let publicKeyInHex: string;
let privateKeyInHex: string;

beforeAll(async () => {
await sodium.ready; // <-- Ensure sodium is ready

const keyPair = await randomKeyPair();
console.log("Returned keyPair:", keyPair);

// Use sodium.to_hex() to convert keys to hex
publicKeyInHex = sodium.to_hex(keyPair.publicKey);
privateKeyInHex = sodium.to_hex(keyPair.privateKey);

console.log('Public Key (Hex):', publicKeyInHex);
console.log('Private Key (Hex):', privateKeyInHex);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should encrypt a plaintext string asymmetrically', async () => {
const mockKeyPair = {
publicKey: Uint8Array.from([1, 2, 3]),
privateKey: Uint8Array.from([4, 5, 6]),
};
const mockSymmetricKeys = {
sharedTx: Uint8Array.from([7, 8, 9]),
sharedRx: Uint8Array.from([10, 11, 12]),
};
const mockCiphertext = 'encrypted_data';

require('libsodium-wrappers-sumo').crypto_kx_keypair.mockResolvedValueOnce(mockKeyPair);
require('libsodium-wrappers-sumo').from_hex.mockImplementation((data: string) => Uint8Array.from(data.split('').map((char: string) => char.charCodeAt(0))));
require('libsodium-wrappers-sumo').crypto_kx_client_session_keys.mockResolvedValueOnce(mockSymmetricKeys);
mockEncryptString.mockResolvedValueOnce(mockCiphertext);

const plaintext = 'hello world';

const result = await encryptAsymmetric(plaintext, publicKeyInHex);

expect(result).toContain('ph:v1');
expect(result).toContain('encrypted_data');
});
});

Loading

0 comments on commit fb48b05

Please sign in to comment.