Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/argon2/__test__/argon2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down Expand Up @@ -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')
})