|
| 1 | +/** |
| 2 | + * Copyright 2019 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +const rewire = require('rewire'); |
| 10 | +const PKCS11_Rewire = rewire('../../lib/impl/bccsp_pkcs11'); |
| 11 | + |
| 12 | +const chai = require('chai'); |
| 13 | +chai.should(); |
| 14 | +const sinon = require('sinon'); |
| 15 | + |
| 16 | +describe('CryptoSuite_PKCS11', () => { |
| 17 | + |
| 18 | + const sandbox = sinon.createSandbox(); |
| 19 | + let utilsStub; |
| 20 | + let configStub; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + configStub = sandbox.stub(); |
| 24 | + configStub.withArgs('crypto-pkcs11-lib').returns('/temp'); |
| 25 | + configStub.withArgs('crypto-pkcs11-slot').returns(2); |
| 26 | + configStub.withArgs('crypto-pkcs11-usertype').returns(2); |
| 27 | + configStub.withArgs('crypto-pkcs11-readwrite').returns('true'); |
| 28 | + configStub.withArgs('crypto-pkcs11-pin').returns('pin'); |
| 29 | + configStub.withArgs('crypto-hash-algo').returns('sha2'); |
| 30 | + |
| 31 | + utilsStub = { |
| 32 | + getConfigSetting: configStub |
| 33 | + }; |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + sandbox.restore(); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('#constructor', () => { |
| 41 | + it('should throw when no params are given', () => { |
| 42 | + (() => { |
| 43 | + new PKCS11_Rewire(); |
| 44 | + }).should.throw(/keySize must be specified/); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw when unsupported bits key sizes are given', () => { |
| 48 | + (() => { |
| 49 | + new PKCS11_Rewire(222); |
| 50 | + }).should.throw(/only 256 or 384 bits key sizes are supported/); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should throw when no library path is given', () => { |
| 54 | + (() => { |
| 55 | + new PKCS11_Rewire(256); |
| 56 | + }).should.throw(/PKCS11 library path must be specified/); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should throw when no library path is given', () => { |
| 60 | + (() => { |
| 61 | + new PKCS11_Rewire(256); |
| 62 | + }).should.throw(/PKCS11 library path must be specified/); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw if pkcs11 slot not given', () => { |
| 66 | + (() => { |
| 67 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp'}); |
| 68 | + }).should.throw(/PKCS11 slot must be specified/); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should throw if invalid [string] pkcs11 slot given', () => { |
| 72 | + (() => { |
| 73 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 'a'}); |
| 74 | + }).should.throw(/PKCS11 slot number invalid/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should throw if invalid [double] pkcs11 slot given', () => { |
| 78 | + (() => { |
| 79 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2.2}); |
| 80 | + }).should.throw(/PKCS11 slot number invalid/); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should throw if pkcs11 slot PIN not given', () => { |
| 84 | + (() => { |
| 85 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2}); |
| 86 | + }).should.throw(/PKCS11 PIN must be set/); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should throw if pkcs11 slot PIN is not a string', () => { |
| 90 | + (() => { |
| 91 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2, pin: 7}); |
| 92 | + }).should.throw(/PKCS11 PIN must be set/); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should throw if invalid usertype', () => { |
| 96 | + (() => { |
| 97 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2, pin: 'pin', usertype: 'invalid'}); |
| 98 | + }).should.throw(/usertype number invalid/); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should throw if invalid readwrite', () => { |
| 102 | + (() => { |
| 103 | + new PKCS11_Rewire(256, 'sha2', {lib: '/temp', slot: 2, pin: 'pin', usertype: 2, readwrite: 'not'}); |
| 104 | + }).should.throw(/readwrite setting must be "true" or "false"/); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should retrieve crypto-pkcs11-lib from config setting if no opts specified', () => { |
| 108 | + PKCS11_Rewire.__set__('utils', utilsStub); |
| 109 | + PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub(); |
| 110 | + new PKCS11_Rewire(256, 'sha2'); |
| 111 | + sinon.assert.calledWith(configStub, 'crypto-pkcs11-lib'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should retrieve crypto-pkcs11-slot from config setting if no opts specified', () => { |
| 115 | + PKCS11_Rewire.__set__('utils', utilsStub); |
| 116 | + PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub(); |
| 117 | + new PKCS11_Rewire(256, 'sha2'); |
| 118 | + sinon.assert.calledWith(configStub, 'crypto-pkcs11-slot'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should retrieve crypto-pkcs11-usertype from config setting if no opts specified', () => { |
| 122 | + PKCS11_Rewire.__set__('utils', utilsStub); |
| 123 | + PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub(); |
| 124 | + new PKCS11_Rewire(256, 'sha2'); |
| 125 | + sinon.assert.calledWith(configStub, 'crypto-pkcs11-usertype'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should retrieve crypto-pkcs11-readwrite from config setting if no opts specified', () => { |
| 129 | + PKCS11_Rewire.__set__('utils', utilsStub); |
| 130 | + PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub(); |
| 131 | + new PKCS11_Rewire(256, 'sha2'); |
| 132 | + sinon.assert.calledWith(configStub, 'crypto-pkcs11-readwrite'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should retrieve crypto-hash-algo from config setting if not provided', () => { |
| 136 | + PKCS11_Rewire.__set__('utils', utilsStub); |
| 137 | + PKCS11_Rewire.prototype._pkcs11OpenSession = sandbox.stub(); |
| 138 | + new PKCS11_Rewire(256); |
| 139 | + sinon.assert.calledWith(configStub, 'crypto-hash-algo'); |
| 140 | + }); |
| 141 | + |
| 142 | + }); |
| 143 | +}); |
0 commit comments