node-ebics-client/test/unit/keys.js

93 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2019-10-31 16:57:42 +00:00
'use strict';
2019-11-05 04:09:07 +00:00
/* eslint-env node, mocha */
2019-10-31 16:57:42 +00:00
const { assert } = require('chai');
2019-11-05 04:09:07 +00:00
const fixtures = require('../fixtures/keys');
2019-11-01 11:49:59 +00:00
// const Key = require('../lib/keymanagers/keyRSA');
2019-11-05 04:09:07 +00:00
const Key = require('../../lib/keymanagers/Key');
2019-10-31 16:57:42 +00:00
const stripWhitespace = str => str.replace(/\s+/g, '');
describe('Keys management', () => {
describe('generates brand new', () => {
const keySize = 2048;
2019-11-01 11:49:59 +00:00
// const newKey = Key().generate(keySize);
const newKey = Key.generate();
2019-10-31 16:57:42 +00:00
it('private key', () => {
assert.isTrue(newKey.isPrivate());
});
it('that has the right key size', () => {
// const newKeySize = newKey.size(); // console.log(newKey.size());
assert(newKey.size(), keySize);
});
});
describe('creates public key from mod and exp', () => {
const { pem, mod, exp } = fixtures.pblc.big;
describe('that are strings', () => {
const m = Buffer.from(mod.string, 'base64');
const e = Buffer.from(exp.string, 'base64');
2019-11-01 11:49:59 +00:00
/* const newKey = Key().importKey({
2019-10-31 16:57:42 +00:00
mod: m, exp: e, modulus: mod.string, exponent: exp.string,
2019-11-01 11:49:59 +00:00
}); */
const newKey = new Key({ mod: m, exp: e });
2019-10-31 16:57:42 +00:00
it('and is really public', () => {
assert.isTrue(newKey.isPublic());
});
2019-11-01 11:49:59 +00:00
2019-10-31 16:57:42 +00:00
it('and has a propper mod in bytes', () => {
assert.deepEqual([...newKey.n()], mod.bytes);
});
it('and has a propper pem string', () => {
assert.equal(stripWhitespace(newKey.toPem()), stripWhitespace(pem));
});
});
describe('that are bytes', () => {
const m = Buffer.from(mod.bytes);
const e = Buffer.from(exp.bytes);
2019-11-01 11:49:59 +00:00
/* const newKey = Key().importKey({
2019-10-31 16:57:42 +00:00
mod: m, exp: e, modulus: mod.string, exponent: exp.string,
2019-11-01 11:49:59 +00:00
}); */
const newKey = new Key({ mod: m, exp: e });
2019-10-31 16:57:42 +00:00
it('and is really public', () => {
assert.isTrue(newKey.isPublic());
});
it('and has a propper mod as a string', () => {
assert.equal(newKey.n().toString('base64'), mod.string);
});
it('and has a propper pem string', () => {
assert.equal(stripWhitespace(newKey.toPem()), stripWhitespace(pem));
});
});
});
describe('creates public key from pem string', () => {
2019-11-01 11:49:59 +00:00
const { pem } = fixtures.pblc.big;
// const newKey = Key(pem);
const newKey = new Key({ pem });
2019-10-31 16:57:42 +00:00
it('and is really public', () => {
assert.isTrue(newKey.isPublic());
});
it('and has a propper(the same) pem string', () => {
2019-11-01 11:49:59 +00:00
/* newKey.pempem = {
2019-10-31 16:57:42 +00:00
modulus: mod.string,
exponent: exp.string,
2019-11-01 11:49:59 +00:00
}; */
2019-10-31 16:57:42 +00:00
assert.equal(stripWhitespace(newKey.toPem()), stripWhitespace(pem));
});
});
});