From b1d3d5d3066994e7cdc89044e2d8b41a8218048d Mon Sep 17 00:00:00 2001 From: yisibl Date: Fri, 31 Dec 2021 22:48:33 +0800 Subject: [PATCH] test: add more test cases --- packages/argon2/__test__/argon2.spec.ts | 64 +++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/packages/argon2/__test__/argon2.spec.ts b/packages/argon2/__test__/argon2.spec.ts index 91b5cb13..fed6a5e7 100644 --- a/packages/argon2/__test__/argon2.spec.ts +++ b/packages/argon2/__test__/argon2.spec.ts @@ -4,6 +4,36 @@ import test from 'ava' import { Algorithm, hash, verify, Version } from '../index.js' +const passwordString = 'some_string123' +const passwordBuffer = Buffer.from(passwordString) + +test('should allow buffer input', async (t) => { + const hashed = await hash(passwordBuffer) + t.true(await verify(hashed, passwordString)) +}) + +test('should allow changing timeCost', async (t) => { + const hashed = await hash(passwordString, { + timeCost: 5, + }) + t.true(await verify(hashed, passwordString)) +}) + +test('should allow changing memoryCost', async (t) => { + const hashed = await hash(passwordString, { + memoryCost: 16384, + }) + t.true(await verify(hashed, passwordString)) +}) + +test('should allow changing parallelism', async (t) => { + const hashed = await hash(passwordString, { + memoryCost: 65536, + parallelism: 2, + }) + t.true(await verify(hashed, passwordString)) +}) + test('should be able to hash string', async (t) => { await t.notThrowsAsync(() => hash('whatever')) await t.notThrowsAsync(() => @@ -47,3 +77,37 @@ test('should be able to verify hashed string', async (t) => { ), ) }) + +// error +test('should return memoryCost error', async (t) => { + const error = await t.throwsAsync(() => + hash(passwordString, { + timeCost: 2, + memoryCost: 1, + parallelism: 1, + }), + ) + + t.is(error.message, 'memory cost is too small') +}) + +test('should return timeCost error', async (t) => { + const error = await t.throwsAsync(() => + hash(passwordString, { + timeCost: 0.6, + }), + ) + + t.is(error.message, 'time cost is too small') +}) + +test('should return parallelism error', async (t) => { + const error = await t.throwsAsync(() => + hash(passwordString, { + timeCost: 3, + parallelism: 0, + }), + ) + + t.is(error.message, 'not enough threads') +})