This commit is contained in:
2022-10-13 22:06:51 +02:00
parent f1f5e44b54
commit c87fd3eaf5
52 changed files with 2252 additions and 4729 deletions

15
test/encoding.spec.js Normal file
View File

@@ -0,0 +1,15 @@
import { Base64 } from '../dist/esm/index.js'
import { Precomputed } from './values.js'
describe('Encoding', () => {
describe('Base64', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Base64)) {
it(`Should encode ${input} to ${output}`, () => {
chai.expect(Base64.encode(input)).to.equal(output)
})
it(`Should decode ${output} to ${input}`, () => {
chai.expect(Base64.decode(output)).to.equal(input)
})
}
})
})

16
test/hash.spec.js Normal file
View File

@@ -0,0 +1,16 @@
import { hash, Hashes } from '../dist/esm/index.js'
import { Precomputed } from './values.js'
describe('Hash', () => {
for (const type of Object.keys(Hashes)) {
describe(type, () => {
const values = Precomputed.Hash[type]
for (const [input, output] of Object.entries(values)) {
it(`Should hash ${input} to ${output}`, async () => {
const hashed = await hash(input, Hashes[type])
chai.expect(hashed).to.equal(output)
})
}
})
}
})

12
test/hooks.js Normal file
View File

@@ -0,0 +1,12 @@
/**
* Hook for mocha tests in node
* Initialises chai and chai-as-promised as global variables
*/
export const mochaHooks = {
async beforeEach() {
if (typeof chai === 'undefined') {
global.chai = await import('chai')
}
},
}

View File

@@ -1,89 +0,0 @@
const { describe, it } = require('mocha')
const a = require('assert')
describe('Check imports', () => {
it('default', () => {
const Occulto = require('../').default
a.notStrictEqual(undefined, Occulto.RSA)
a.notStrictEqual(undefined, Occulto.Symmetric)
})
it('normal', () => {
const { RSA, Symmetric } = require('../')
a.notStrictEqual(undefined, RSA)
a.notStrictEqual(undefined, Symmetric)
})
})
const { RSA, Symmetric, Hash } = require('../')
describe('Asymmetric', () => {
describe('RSA', () => {
it('Encrypt and Decrypt small string', async () => {
const pair = await RSA.gen(2 ** 10)
const text = `some small string`
const e = RSA.encrypt(text, pair.pub)
const d = RSA.decrypt(e, pair.prv)
a.strictEqual(text, d)
})
})
})
describe('Symmetric', () => {
const keyLong = `yruyLaCAbcfJD9DKk3Ef6zuSFMPatwbg63ayPHVDSSxAePqtYxmd7N5BX2ShCgaG`
const keyShort = `simple`
const dataShort = `a sentence`
const dataLong = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
describe('Symmetric', () => {
describe('Default', () => {
it('Short', () => {
const e = Symmetric.encrypt(dataShort, keyShort)
const d = Symmetric.decrypt(e, keyShort)
a.strictEqual(dataShort, d)
})
it('Long', () => {
const e = Symmetric.encrypt(dataLong, keyLong)
const d = Symmetric.decrypt(e, keyLong)
a.strictEqual(dataLong, d)
})
})
describe('All Ciphers', () => {
for (const [key, value] of Object.entries(Symmetric.Ciphers)) {
if (!isNaN(key)) continue
it(key, () => {
const e = Symmetric.encrypt(dataShort, keyShort, value)
const d = Symmetric.decrypt(e, keyShort)
a.strictEqual(dataShort, d)
})
}
})
})
})
describe('Hashes', () => {
const dataShort = `a sentence`
const dataLong = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
describe('All Hashes', () => {
for (const [key, value] of Object.entries(Hash.Hashes)) {
if (!isNaN(key)) continue
it(key, () => {
const short = Hash.digest(dataShort, value)
a.notStrictEqual(undefined, short)
const long = Hash.digest(dataLong, value)
a.notStrictEqual(undefined, long)
})
}
})
})

18
test/random.spec.js Normal file
View File

@@ -0,0 +1,18 @@
import { getRandomBytes } from '../dist/esm/index.js'
import { Promises } from './utils.js'
describe('Random', () => {
it('Should be able to create random values', async () => {
const buffer = await getRandomBytes(8)
chai.expect(buffer).to.be.instanceOf(Uint8Array)
chai.expect(buffer.byteLength).to.equal(8)
})
it('Should throw error on empty array', async () => {
await Promises.reject(() => getRandomBytes(0))
})
it('Should throw error on negative bytes', async () => {
await Promises.reject(() => getRandomBytes(-1))
})
})

8
test/utils.js Normal file
View File

@@ -0,0 +1,8 @@
export class Promises {
static async reject(fn) {
try {
await fn()
chai.expect.fail('Should have thrown error')
} catch {}
}
}

33
test/values.js Normal file
View File

@@ -0,0 +1,33 @@
export const Precomputed = {
Encoding: {
Base64: {
occulto: 'b2NjdWx0bw==',
test: 'dGVzdA==',
'hello world': 'aGVsbG8gd29ybGQ=',
},
},
Hash: {
SHA_1: {
test: 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3',
'hello world': '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed',
occulto: 'f4b27cfb9e01492f409295fbbc339753fa839c0f',
},
SHA_256: {
test: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
'hello world': 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9',
occulto: 'df2b97515886051821e4375a33df10486ce55cb3d14acd05dd7465f820ef2481',
},
SHA_384: {
test: '768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9',
'hello world': 'fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd',
occulto: '133c1968e937462ed66732409fa305c63335ee62b1114dd2d0ae98b4dd8fa6aca4656c919b295e41efa2d63f0d3c9951',
},
SHA_512: {
test: 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff',
'hello world':
'309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f',
occulto:
'9f7ff06148d415b12290ab7c21f021964ed627574f94f66c994aad4a8e319aa3168a9871edace3e736096cbd957cafa42dbf3feb6efd7763bf936ddc933c9470',
},
},
}