2022-10-03 17:57:08 +00:00
|
|
|
import * as bcu from '#pkg'
|
|
|
|
|
2020-04-20 22:50:53 +00:00
|
|
|
const iterations = 10
|
2020-04-21 00:41:32 +00:00
|
|
|
const bitLengths = [-1, 0, 3, 8, 16, 511, 2048]
|
2022-12-20 22:51:18 +00:00
|
|
|
const byteLengths = [-7, 0, 1, 8, 33, 40, 65536, 67108864]
|
2020-04-20 22:50:53 +00:00
|
|
|
|
2021-03-25 12:40:04 +00:00
|
|
|
describe('testing randBits', function () {
|
2020-04-20 22:50:53 +00:00
|
|
|
for (const bitLength of bitLengths) {
|
2020-04-21 00:41:32 +00:00
|
|
|
describe(`${iterations} iterations of randBitsSync(${bitLength})`, function () {
|
|
|
|
if (bitLength > 0) {
|
|
|
|
it('should return buffers', function () {
|
|
|
|
let ret = true
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbits = bcu.randBitsSync(bitLength)
|
2020-04-20 22:50:53 +00:00
|
|
|
// console.log(JSON.stringify(randbits))
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbits2 = bcu.randBitsSync(bitLength, true)
|
2020-04-20 22:50:53 +00:00
|
|
|
// console.log(JSON.stringify(randbits2))
|
2020-04-21 00:41:32 +00:00
|
|
|
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
|
|
|
|
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
|
|
|
|
ret = false
|
|
|
|
break
|
|
|
|
}
|
2020-04-20 22:50:53 +00:00
|
|
|
}
|
2020-04-21 00:41:32 +00:00
|
|
|
chai.expect(ret).to.equal(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('should throw RangeError', function () {
|
2022-10-03 17:57:08 +00:00
|
|
|
chai.expect(() => bcu.randBitsSync(bitLength)).to.throw(RangeError)
|
2020-04-20 22:50:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-03-25 12:40:04 +00:00
|
|
|
describe(`${iterations} iterations of randBits(${bitLength})`, function () {
|
2020-04-21 00:41:32 +00:00
|
|
|
if (bitLength > 0) {
|
|
|
|
it('should return buffers', async function () {
|
|
|
|
let ret = true
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbits = await bcu.randBits(bitLength)
|
2020-04-20 22:50:53 +00:00
|
|
|
// console.log(JSON.stringify(randbits))
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbits2 = await bcu.randBits(bitLength, true)
|
2020-04-20 22:50:53 +00:00
|
|
|
// console.log(JSON.stringify(randbits2))
|
2020-04-21 00:41:32 +00:00
|
|
|
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
|
|
|
|
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
|
|
|
|
ret = false
|
|
|
|
break
|
|
|
|
}
|
2020-04-20 22:50:53 +00:00
|
|
|
}
|
2020-04-21 00:41:32 +00:00
|
|
|
chai.expect(ret).to.equal(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('should throw RangeError', function () {
|
2022-10-03 17:57:08 +00:00
|
|
|
chai.expect(() => bcu.randBits(bitLength)).to.throw(RangeError) // eslint-disable-line
|
2020-04-20 22:50:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-03-25 12:40:04 +00:00
|
|
|
describe('testing randBytes', function () {
|
2020-04-20 22:50:53 +00:00
|
|
|
for (const byteLength of byteLengths) {
|
2020-04-21 00:41:32 +00:00
|
|
|
describe(`${iterations} iterations of randBytesSync(${byteLength})`, function () {
|
|
|
|
if (byteLength > 0) {
|
|
|
|
it('should return buffers', function () {
|
|
|
|
let ret = true
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbytes = bcu.randBytesSync(byteLength)
|
2020-04-21 00:41:32 +00:00
|
|
|
// console.log(JSON.stringify(randbits))
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbytes2 = bcu.randBytesSync(byteLength, true)
|
2020-04-21 00:41:32 +00:00
|
|
|
// console.log(JSON.stringify(randbits2))
|
|
|
|
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
|
|
|
|
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
|
|
|
|
ret = false
|
|
|
|
}
|
2020-04-20 22:50:53 +00:00
|
|
|
}
|
2020-04-21 00:41:32 +00:00
|
|
|
chai.expect(ret).to.equal(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('should throw RangeError', function () {
|
2022-10-03 17:57:08 +00:00
|
|
|
chai.expect(() => bcu.randBytesSync(byteLength)).to.throw(RangeError)
|
2020-04-20 22:50:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-03-25 12:40:04 +00:00
|
|
|
describe(`${iterations} iterations of randBytes(${byteLength})`, function () {
|
2020-04-21 00:41:32 +00:00
|
|
|
if (byteLength > 0) {
|
2022-12-20 22:51:18 +00:00
|
|
|
it('should return buffers of the expected length', async function () {
|
2020-04-21 00:41:32 +00:00
|
|
|
let ret = true
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbytes = await bcu.randBytes(byteLength)
|
2022-12-20 22:51:18 +00:00
|
|
|
chai.expect(randbytes.length).to.equal(byteLength)
|
2020-04-21 00:41:32 +00:00
|
|
|
// console.log(JSON.stringify(randbits))
|
2022-10-03 17:57:08 +00:00
|
|
|
const randbytes2 = await bcu.randBytes(byteLength, true)
|
2022-12-20 22:51:18 +00:00
|
|
|
chai.expect(randbytes2.length).to.equal(byteLength)
|
2020-04-21 00:41:32 +00:00
|
|
|
// console.log(JSON.stringify(randbits2))
|
|
|
|
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
|
|
|
|
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
|
|
|
|
ret = false
|
|
|
|
}
|
2020-04-20 22:50:53 +00:00
|
|
|
}
|
2020-04-21 00:41:32 +00:00
|
|
|
chai.expect(ret).to.equal(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('should throw RangeError', function () {
|
2022-10-03 17:57:08 +00:00
|
|
|
chai.expect(() => bcu.randBytes(byteLength)).to.throw(RangeError) // eslint-disable-line
|
2020-04-20 22:50:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|