2023-11-09 00:20:43 +03:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
import { hexEncode, utf8Decode, utf8EncodeToBuffer } from '@mtcute/tl-runtime'
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
import { buffersEqual, bufferToReversed, cloneBuffer, concatBuffers, randomBytes } from './buffer-utils.js'
|
|
|
|
import { xorBuffer, xorBufferInPlace } from './crypto/utils.js'
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
describe('buffersEqual', () => {
|
|
|
|
it('should return true for equal buffers', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(buffersEqual(new Uint8Array([]), new Uint8Array([]))).is.true
|
|
|
|
expect(buffersEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]))).is.true
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false for non-equal buffers', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(buffersEqual(new Uint8Array([1]), new Uint8Array([]))).is.false
|
|
|
|
expect(buffersEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 4]))).is.false
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('xorBuffer', () => {
|
|
|
|
it('should xor buffers without modifying original', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const data = utf8EncodeToBuffer('hello')
|
|
|
|
const key = utf8EncodeToBuffer('xor')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
const xored = xorBuffer(data, key)
|
|
|
|
expect(data.toString()).eq('hello')
|
|
|
|
expect(key.toString()).eq('xor')
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(xored)).eq('100a1e6c6f')
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should be deterministic', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const data = utf8EncodeToBuffer('hello')
|
|
|
|
const key = utf8EncodeToBuffer('xor')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
const xored1 = xorBuffer(data, key)
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(xored1)).eq('100a1e6c6f')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
const xored2 = xorBuffer(data, key)
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(xored2)).eq('100a1e6c6f')
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('second call should decode content', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const data = utf8EncodeToBuffer('hello')
|
|
|
|
const key = utf8EncodeToBuffer('xor')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
const xored1 = xorBuffer(data, key)
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(xored1)).eq('100a1e6c6f')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
const xored2 = xorBuffer(xored1, key)
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(utf8Decode(xored2)).eq('hello')
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('xorBufferInPlace', () => {
|
|
|
|
it('should xor buffers by modifying original', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const data = utf8EncodeToBuffer('hello')
|
|
|
|
const key = utf8EncodeToBuffer('xor')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
xorBufferInPlace(data, key)
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(data)).eq('100a1e6c6f')
|
2021-04-08 12:19:38 +03:00
|
|
|
expect(key.toString()).eq('xor')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('second call should decode content', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const data = utf8EncodeToBuffer('hello')
|
|
|
|
const key = utf8EncodeToBuffer('xor')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
xorBufferInPlace(data, key)
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(data)).eq('100a1e6c6f')
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
xorBufferInPlace(data, key)
|
|
|
|
expect(data.toString()).eq('hello')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('randomBytes', () => {
|
|
|
|
it('should return exactly N bytes', () => {
|
|
|
|
expect(randomBytes(0).length).eq(0)
|
|
|
|
expect(randomBytes(5).length).eq(5)
|
|
|
|
expect(randomBytes(10).length).eq(10)
|
|
|
|
expect(randomBytes(256).length).eq(256)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not be deterministic', () => {
|
|
|
|
expect([...randomBytes(8)]).not.eql([...randomBytes(8)])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('cloneBuffer', () => {
|
|
|
|
it('should clone buffer', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const orig = new Uint8Array([1, 2, 3])
|
2021-04-08 12:19:38 +03:00
|
|
|
const copy = cloneBuffer(orig)
|
|
|
|
|
|
|
|
expect([...copy]).eql([1, 2, 3])
|
|
|
|
orig[0] = 0xff
|
|
|
|
expect(copy[0]).not.eql(0xff)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should clone buffer partially', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
const orig = new Uint8Array([1, 2, 3, 4, 5])
|
2021-04-08 12:19:38 +03:00
|
|
|
const copy = cloneBuffer(orig, 1, 4)
|
|
|
|
|
|
|
|
expect([...copy]).eql([2, 3, 4])
|
|
|
|
orig[0] = 0xff
|
|
|
|
expect(copy[0]).not.eql(0xff)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
describe('concatBuffers', () => {
|
|
|
|
it('should concat buffers', () => {
|
2023-11-06 02:28:35 +03:00
|
|
|
const buf = concatBuffers([new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])])
|
2023-10-16 19:23:53 +03:00
|
|
|
|
|
|
|
expect([...buf]).eql([1, 2, 3, 4, 5, 6])
|
2021-05-23 20:33:10 +03:00
|
|
|
})
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
it('should create a new buffer', () => {
|
|
|
|
const buf1 = new Uint8Array([1, 2, 3])
|
|
|
|
const buf2 = new Uint8Array([4, 5, 6])
|
|
|
|
const buf = concatBuffers([buf1, buf2])
|
|
|
|
|
|
|
|
buf[0] = 0xff
|
|
|
|
expect(buf1[0]).not.eql(0xff)
|
2021-05-23 20:33:10 +03:00
|
|
|
})
|
|
|
|
})
|
2023-11-06 02:28:35 +03:00
|
|
|
|
|
|
|
describe('bufferToReversed', () => {
|
|
|
|
it('should reverse the buffer', () => {
|
|
|
|
const buf = bufferToReversed(new Uint8Array([1, 2, 3, 4, 5, 6]))
|
|
|
|
|
|
|
|
expect([...buf]).eql([6, 5, 4, 3, 2, 1])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should reverse a part of the buffer', () => {
|
|
|
|
const buf = bufferToReversed(new Uint8Array([1, 2, 3, 4, 5, 6]), 1, 5)
|
|
|
|
|
|
|
|
expect([...buf]).eql([5, 4, 3, 2])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a new buffer', () => {
|
|
|
|
const buf1 = new Uint8Array([1, 2, 3])
|
|
|
|
const buf2 = bufferToReversed(buf1)
|
|
|
|
|
|
|
|
buf2[0] = 0xff
|
|
|
|
expect([...buf1]).eql([1, 2, 3])
|
|
|
|
})
|
|
|
|
})
|