Skip to content

Commit

Permalink
FABN-1391] Refactor cryptosuite tape UT
Browse files Browse the repository at this point in the history
- Created new User, Utils, CryptoSuite UTs
- migrated constants to TestUtils
- Also removed a duplicate test

Signed-off-by: heatherlp <[email protected]>
Change-Id: Iad69240a46a38c2dd4e7e45ec0b94649f48ae6fb
  • Loading branch information
heatherlp committed Oct 10, 2019
1 parent 5b01202 commit 5834c7d
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 543 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ tmp
.nyc_output/*
*.csv
*.heapsnapshot
.scannerwork/*
sonar-project.properties

#test files
test/temp/**
Expand Down
2 changes: 2 additions & 0 deletions fabric-common/lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const Long = require('long');
const util = require('util');
const winston = require('winston');
Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/CryptoSuite_ECDSA_AES.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class CryptoSuite_ECDSA_AES extends CryptoSuite {

/**
* This is an implementation of {@link module:api.CryptoSuite#sign}
* Signs digest using key k.
* Signs digest using key.
*/
sign(key, digest) {
if (typeof key === 'undefined' || key === null) {
Expand Down
42 changes: 42 additions & 0 deletions fabric-common/test/TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const Long = require('long');
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -160,3 +161,44 @@ module.exports.createResponsePayload = (results = 'results') => {

return payload.toBuffer();
};

module.exports.TEST_KEY_PRIVATE_CERT_PEM = '-----BEGIN CERTIFICATE-----' +
'MIICEDCCAbagAwIBAgIUXoY6X7jIpHAAgL267xHEpVr6NSgwCgYIKoZIzj0EAwIw' +
'fzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh' +
'biBGcmFuY2lzY28xHzAdBgNVBAoTFkludGVybmV0IFdpZGdldHMsIEluYy4xDDAK' +
'BgNVBAsTA1dXVzEUMBIGA1UEAxMLZXhhbXBsZS5jb20wHhcNMTcwMTAzMDEyNDAw' +
'WhcNMTgwMTAzMDEyNDAwWjAQMQ4wDAYDVQQDEwVhZG1pbjBZMBMGByqGSM49AgEG' +
'CCqGSM49AwEHA0IABLoGEWBb+rQ/OuTBPlGVZO3jVWBcuC4+/pAq8axbtKorpORw' +
'J/GxahKPLr+vVLPNMyeLcnyJBGgneug+ajE8srijfzB9MA4GA1UdDwEB/wQEAwIF' +
'oDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDAYDVR0TAQH/BAIwADAd' +
'BgNVHQ4EFgQU9BUt7QfgDXx9g6zpzCyJGxXsNM0wHwYDVR0jBBgwFoAUF2dCPaqe' +
'gj/ExR2fW8OZ0bWcSBAwCgYIKoZIzj0EAwIDSAAwRQIgcWQbMzluyZsmvQCvGzPg' +
'f5B7ECxK0kdmXPXIEBiizYACIQD2x39Q4oVwO5uL6m3AVNI98C2LZWa0g2iea8wk' +
'BAHpeA==' +
'-----END CERTIFICATE-----';

module.exports.TEST_PUBLIC_KEY_SKI = 'f7b61538c52260e83cf4f2693d1' +
'1019f73e7495056c5b54f1e05bae80e9402a7';

module.exports.TEST_PRIVATE_KEY_SKI = 'bced195e7aacb5705bbad45598' +
'535d2f41564953680c5cf696becbb2dfebf39c';

module.exports.TEST_MSG = 'this is a test message';
module.exports.TEST_LONG_MSG = 'The Hyperledger project is an open source collaborative effort created to advance cross-industry blockchain technologies. ' +
'It is a global collaboration including leaders in finance, banking, Internet of Things, supply chains, manufacturing and Technology. The Linux ' +
'Foundation hosts Hyperledger as a Collaborative Project under the foundation. Why Create the Project? Not since the Web itself has a technology ' +
'promised broader and more fundamental revolution than blockchain technology. A blockchain is a peer-to-peer distributed ledger forged by consensus, ' +
'combined with a system for “smart contracts” and other assistive technologies. Together these can be used to build a new generation of transactional ' +
'applications that establishes trust, accountability and transparency at their core, while streamlining business processes and legal constraints. ' +
'Think of it as an operating system for marketplaces, data-sharing networks, micro-currencies, and decentralized digital communities. It has the potential ' +
'to vastly reduce the cost and complexity of getting things done in the real world. Only an Open Source, collaborative software development approach can ' +
'ensure the transparency, longevity, interoperability and support required to bring blockchain technologies forward to mainstream commercial adoption. That ' +
'is what Hyperledger is about – communities of software developers building blockchain frameworks and platforms.';

module.exports.HASH_MSG_SHA2_256 = '4e4aa09b6d80efbd684e80f54a70c1d8605625c3380f4cb012b32644a002b5be';
module.exports.HASH_LONG_MSG_SHA2_256 = '0d98987f5e4e3ea611f0e3d768c594ff9aac25404265d73554d12c86d7f6fbbc';
module.exports.HASH_MSG_SHA2_384 = '6247065855a812ecd182476576c02d46a675845ef4b0056e973ca42dcf8191d3adabc8c6c4b909f20f96136032ab723a';
module.exports.HASH_MSG_SHA3_256 = '7daeff454f7e91e3cd2d1c1bd5fcd1b6c9d4d5fffc6c327710d8fae7b06ee4a3';
module.exports.HASH_LONG_MSG_SHA3_256 = '577174210438a85ae4311a62e5fccf2441b960013f5691993cdf38ed6ba0c84f';
module.exports.HASH_MSG_SHA3_384 = '9e9c2e5edf6cbc0b512807a8efa2917daff71b83e04dee28fcc00b1a1dd935fb5afc5eafa06bf55bd64792a597e2a8f3';
module.exports.HASH_LONG_MSG_SHA3_384 = '47a90d6721523682e09b81da0a60e6ee1faf839f0503252316638daf038cf682c0a842edaf310eb0f480a2e181a07af0';
33 changes: 33 additions & 0 deletions fabric-common/test/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const rewire = require('rewire');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
Expand All @@ -12,6 +14,9 @@ chai.use(chaiAsPromised);

const User = rewire('../lib/User');
const TestUtils = require('./TestUtils');
const {Utils} = require('..');
const path = require('path');
const fs = require('fs-extra');

describe('User', () => {
TestUtils.setCryptoConfigSettings();
Expand Down Expand Up @@ -107,6 +112,34 @@ describe('User', () => {
// f_user.fromString(string, true);
// await f_user._name.should.be.equal('user');
// });

it('should throw an error when the private key is missing from a user enrollment object', async () => {
const testUserEnrollment = {
'name': 'admin2',
'mspid': 'test',
'roles': null,
'affiliation': '',
'enrollmentSecret': '',
'enrollment': {
'signingIdentity': '0e67f7fa577fd76e487ea3b660e1a3ff15320dbc95e396d8b0ff616c87f8c81a',
'identity': {
'certificate': TestUtils.TEST_KEY_PRIVATE_CERT_PEM
}
}
};
// manufacture an error condition where the private key does not exist for the SKI, and only the public key does
const cryptoSuite = Utils.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Utils.newCryptoKeyStore());
await cryptoSuite.importKey(cert);

fs.removeSync(path.join(Utils.getDefaultKeyStorePath(), '0e67f7fa577fd76e487ea3b660e1a3ff15320dbc95e396d8b0ff616c87f8c81a-priv'));

const user = new User('admin2');
user.setCryptoSuite(cryptoSuite);
const enrollmentString = JSON.stringify(testUserEnrollment);
await user.fromString(enrollmentString).should.be.rejectedWith(/Private key missing from key store/);

});
});

describe('#toString', () => {
Expand Down
99 changes: 99 additions & 0 deletions fabric-common/test/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';

const {Utils} = require('..');
const path = require('path');
const CryptoSuite_ECDSA_AES = require('../lib/impl/CryptoSuite_ECDSA_AES');
const testUtils = require('./TestUtils');

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const sinonChai = require('sinon-chai');

const should = chai.should();
chai.use(chaiAsPromised);
chai.use(sinonChai);

describe('Utils', () => {

describe('#newCryptoSuite', () => {

beforeEach(() => {
testUtils.setCryptoConfigSettings();
});

it('should return a default instance of CryptoSuite_ECDSA_AES with the correct properties', () => {
const defaultCryptoSuite = Utils.newCryptoSuite();
defaultCryptoSuite.should.be.an.instanceOf(CryptoSuite_ECDSA_AES);
defaultCryptoSuite._keySize.should.equal(256);
should.exist(defaultCryptoSuite._ecdsaCurve);
should.exist(defaultCryptoSuite._ecdsa);
});

it('should return an instance of CryptoSuite_ECDSA_AES with the correct keysize', () => {
const cryptoSuite = Utils.newCryptoSuite({keysize: 384, algorithm: 'EC'});
cryptoSuite.should.be.an.instanceOf(CryptoSuite_ECDSA_AES);
cryptoSuite._keySize.should.equal(384);
});

it('should return an instance of CryptoSuite_ECDSA_AES with the correct keysize', () => {
const cryptoSuite = Utils.newCryptoSuite({keysize: 384});
cryptoSuite.should.be.an.instanceOf(CryptoSuite_ECDSA_AES);
cryptoSuite._keySize.should.equal(384);
});

it('should return an instance of CryptoSuite_ECDSA_AES with the default keysize', () => {
const cryptoSuite = Utils.newCryptoSuite({algorithm: 'EC'});
cryptoSuite.should.be.an.instanceOf(CryptoSuite_ECDSA_AES);
cryptoSuite._keySize.should.equal(256);
});

it('should throw an error when an illegal key size is given', () => {
(() => {
Utils.newCryptoSuite({keysize: 123});
}).should.throw(/Illegal key size/);
});

it('should throw an error when using HSM and a fake library path', () => {
Utils.setConfigSetting('crypto-hsm', true);
Utils.setConfigSetting('crypto-suite-hsm', {'EC': 'fabric-common/lib/impl/bccsp_pkcs11.js'});
const fakePath = path.join('some', 'fake', 'path');
(() => {
Utils.newCryptoSuite({lib: fakePath, slot: 0, pin: '1234'});
}).should.throw(fakePath);
});
it('should throw an error when using HSM and no library path is given', () => {
Utils.setConfigSetting('crypto-hsm', true);
Utils.setConfigSetting('crypto-suite-hsm', {'EC': 'fabric-common/lib/impl/bccsp_pkcs11.js'});
(() => {
Utils.newCryptoSuite({keysize: 384, algorithm: 'EC'});
}).should.throw(/PKCS11 library path must be specified/);
});

it('should throw an error when an illegal hashing algorithm has been set', () => {
Utils.setConfigSetting('crypto-hash-algo', 19745);
(() => {
Utils.newCryptoSuite({});
}).should.throw(/Unsupported hash algorithm/);
});

it('should throw an error when an unsupported hashing algorithm has been set', () => {
Utils.setConfigSetting('crypto-hash-algo', '12345');
(() => {
Utils.newCryptoSuite({});
}).should.throw(/Unsupported hash algorithm and key size pair/);
});

it('should throw an error when an incorrect hashing algorithm is specified', () => {
(() => {
Utils.newCryptoSuite({algorithm: 'cake'});
}).should.throw(/Desired CryptoSuite module not found supporting algorithm/);
});

});

});
Loading

0 comments on commit 5834c7d

Please sign in to comment.