mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 11:51:07 +00:00
test: add test for different rsa crypto libs
This commit is contained in:
parent
b05e77f375
commit
f4c00893ad
@ -3,6 +3,10 @@
|
||||
const crypto = require('crypto')
|
||||
let keypair
|
||||
try {
|
||||
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') {
|
||||
throw new Error('Force keypair usage')
|
||||
}
|
||||
|
||||
const ursa = require('ursa-optional') // throws if not compiled
|
||||
keypair = ({bits}) => {
|
||||
const key = ursa.generatePrivateKey(bits)
|
||||
@ -12,6 +16,10 @@ try {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'ursa') {
|
||||
throw e
|
||||
}
|
||||
|
||||
keypair = require('keypair')
|
||||
}
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
65
test/keys/rsa-crypto-libs.js
Normal file
65
test/keys/rsa-crypto-libs.js
Normal file
@ -0,0 +1,65 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint-env mocha */
|
||||
/* eslint max-nested-callbacks: ["error", 8] */
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
chai.use(require('chai-string'))
|
||||
|
||||
const LIBS = ['ursa', 'keypair']
|
||||
|
||||
describe('RSA crypto libs', function () {
|
||||
this.timeout(20 * 1000)
|
||||
|
||||
LIBS.forEach(lib => {
|
||||
describe(lib, () => {
|
||||
let crypto
|
||||
let rsa
|
||||
|
||||
before(() => {
|
||||
process.env.LP2P_FORCE_CRYPTO_LIB = lib
|
||||
|
||||
for (const path in require.cache) { // clear module cache
|
||||
if (path.endsWith('.js')) {
|
||||
delete require.cache[path]
|
||||
}
|
||||
}
|
||||
|
||||
crypto = require('../../src')
|
||||
rsa = crypto.keys.supportedKeys.rsa
|
||||
})
|
||||
|
||||
it('generates a valid key', (done) => {
|
||||
crypto.keys.generateKeyPair('RSA', 512, (err, key) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
expect(key).to.be.an.instanceof(rsa.RsaPrivateKey)
|
||||
|
||||
key.hash((err, digest) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
expect(digest).to.have.length(34)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
after(() => {
|
||||
for (const path in require.cache) { // clear module cache
|
||||
if (path.endsWith('.js')) {
|
||||
delete require.cache[path]
|
||||
}
|
||||
}
|
||||
|
||||
delete process.env.LP2P_FORCE_CRYPTO_LIB
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
3
test/node.js
Normal file
3
test/node.js
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict'
|
||||
|
||||
require('./keys/rsa-crypto-libs')
|
Loading…
x
Reference in New Issue
Block a user