Skip to content

Commit

Permalink
[FABN-1140] move pkcs and edsca to mocha
Browse files Browse the repository at this point in the history
- new mocha unit tests replicating action of pkcs and key tape tests
- delete replaced tape tests

Change-Id: I6506dc5cffeb4e25b748f9c7c3d19f2ba418e9e8
Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
nklincoln committed Feb 18, 2019
1 parent d3ba24c commit acd1d95
Show file tree
Hide file tree
Showing 5 changed files with 514 additions and 440 deletions.
2 changes: 0 additions & 2 deletions fabric-client/lib/impl/ecdsa/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ module.exports = class ECDSA_KEY extends Key {
}

try {
// var before = Date.now() - 60000;
// var after = Date.now() + 60000;
const certPEM = asn1.x509.X509Util.newCertPEM({
serial: {int: 4},
sigalg: {name: 'SHA256withECDSA'},
Expand Down
143 changes: 143 additions & 0 deletions fabric-client/test/impl/bccsp_pkcs11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const rewire = require('rewire');
const PKCS11_Rewire = rewire('../../lib/impl/bccsp_pkcs11');

const chai = require('chai');
chai.should();
const sinon = require('sinon');

describe('CryptoSuite_PKCS11', () => {

const sandbox = sinon.createSandbox();
let utilsStub;
let configStub;

beforeEach(() => {
configStub = sandbox.stub();
configStub.withArgs('crypto-pkcs11-lib').returns('/temp');
configStub.withArgs('crypto-pkcs11-slot').returns(2);
configStub.withArgs('crypto-pkcs11-usertype').returns(2);
configStub.withArgs('crypto-pkcs11-readwrite').returns('true');
configStub.withArgs('crypto-pkcs11-pin').returns('pin');
configStub.withArgs('crypto-hash-algo').returns('sha2');

utilsStub = {
getConfigSetting: configStub
};
});

afterEach(() => {
sandbox.restore();
});

describe('#constructor', () => {
it('should throw when no params are given', () => {
(() => {
new PKCS11_Rewire();
}).should.throw(/keySize must be specified/);
});

it('should throw when unsupported bits key sizes are given', () => {
(() => {
new PKCS11_Rewire(222);
}).should.throw(/only 256 or 384 bits key sizes are supported/);
});

it('should throw when no library path is given', () => {
(() => {
new PKCS11_Rewire(256);
}).should.throw(/PKCS11 library path must be specified/);
});

it('should throw when no library path is given', () => {
(() => {
new PKCS11_Rewire(256);
}).should.throw(/PKCS11 library path must be specified/);
});

it('should throw if pkcs11 slot not given', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp'});
}).should.throw(/PKCS11 slot must be specified/);
});

it('should throw if invalid [string] pkcs11 slot given', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 'a'});
}).should.throw(/PKCS11 slot number invalid/);
});

it('should throw if invalid [double] pkcs11 slot given', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2.2});
}).should.throw(/PKCS11 slot number invalid/);
});

it('should throw if pkcs11 slot PIN not given', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2});
}).should.throw(/PKCS11 PIN must be set/);
});

it('should throw if pkcs11 slot PIN is not a string', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2, pin: 7});
}).should.throw(/PKCS11 PIN must be set/);
});

it('should throw if invalid usertype', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2, pin: 'pin', usertype: 'invalid'});
}).should.throw(/usertype number invalid/);
});

it('should throw if invalid readwrite', () => {
(() => {
new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2, pin: 'pin', usertype: 2, readwrite: 'not'});
}).should.throw(/readwrite setting must be "true" or "false"/);
});

it('should retrieve crypto-pkcs11-lib from config setting if no opts specified', () => {
PKCS11_Rewire.__set__('utils', utilsStub);
PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub();
new PKCS11_Rewire(256, 'sha2');
sinon.assert.calledWith(configStub, 'crypto-pkcs11-lib');
});

it('should retrieve crypto-pkcs11-slot from config setting if no opts specified', () => {
PKCS11_Rewire.__set__('utils', utilsStub);
PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub();
new PKCS11_Rewire(256, 'sha2');
sinon.assert.calledWith(configStub, 'crypto-pkcs11-slot');
});

it('should retrieve crypto-pkcs11-usertype from config setting if no opts specified', () => {
PKCS11_Rewire.__set__('utils', utilsStub);
PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub();
new PKCS11_Rewire(256, 'sha2');
sinon.assert.calledWith(configStub, 'crypto-pkcs11-usertype');
});

it('should retrieve crypto-pkcs11-readwrite from config setting if no opts specified', () => {
PKCS11_Rewire.__set__('utils', utilsStub);
PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub();
new PKCS11_Rewire(256, 'sha2');
sinon.assert.calledWith(configStub, 'crypto-pkcs11-readwrite');
});

it('should retrieve crypto-hash-algo from config setting if not provided', () => {
PKCS11_Rewire.__set__('utils', utilsStub);
PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub();
new PKCS11_Rewire(256);
sinon.assert.calledWith(configStub, 'crypto-hash-algo');
});

});
});
Loading

0 comments on commit acd1d95

Please sign in to comment.