mtcute/packages/wasm/tests/hash.test.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

import { beforeAll, describe, expect, it } from 'vitest'
2023-11-29 20:31:18 +03:00
import { hexEncode, utf8EncodeToBuffer } from '@mtcute/tl-runtime'
import { __getWasm, initAsync, sha1, sha256 } from '../src/index.js'
beforeAll(async () => {
await initAsync()
})
describe('sha256', () => {
it('should correctly calculate sha-256 hash', () => {
2023-11-29 20:31:18 +03:00
const hash = sha256(utf8EncodeToBuffer('abc'))
2023-11-29 20:31:18 +03:00
expect(hexEncode(hash)).toEqual('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')
})
it('should not leak memory', () => {
const mem = __getWasm().memory.buffer
const memSize = mem.byteLength
for (let i = 0; i < 100; i++) {
2023-11-29 20:31:18 +03:00
sha256(utf8EncodeToBuffer('abc'))
}
expect(mem.byteLength).toEqual(memSize)
})
})
describe('sha1', () => {
it('should correctly calculate sha-1 hash', () => {
2023-11-29 20:31:18 +03:00
const hash = sha1(utf8EncodeToBuffer('abc'))
2023-11-29 20:31:18 +03:00
expect(hexEncode(hash)).toEqual('a9993e364706816aba3e25717850c26c9cd0d89d')
})
it('should not leak memory', () => {
const mem = __getWasm().memory.buffer
const memSize = mem.byteLength
for (let i = 0; i < 100; i++) {
2023-11-29 20:31:18 +03:00
sha1(utf8EncodeToBuffer('abc'))
}
expect(mem.byteLength).toEqual(memSize)
})
})