/
Utils.js
277 lines (232 loc) · 10.8 KB
/
Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* 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 fs = require('fs-extra');
const winston = require('winston');
const os = require('os');
const should = chai.should();
chai.use(chaiAsPromised);
chai.use(sinonChai);
describe('Utils', () => {
describe('#byteToNormalizedPEM', () => {
let pem, pem_no_new_line;
beforeEach(() => {
pem =
'-----BEGIN CERTIFICATE-----\n' +
'MIICSTCCAe+gAwIBAgIQPHXmPqjzn2bon7JrBRPS2DAKBggqhkjOPQQDAjB2MQsw\n' +
'CQYDVQQGEwJVUzETMBEGA1UECBMKQ2Fsa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vbTAeFw0xOTAyMjExNDI4MDBaFw0yOTAyMTgxNDI4\n' +
'MDBaMHYxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH\n' +
'Ew1TYW4gRnJhbmNpc2NvMRkwFwYDVQQKExBvcmcxLmV4YW1wbGUuY29tMR8wHQYD\n' +
'VQQDExZ0bHNjYS5vcmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0D\n' +
'AQcDQgAELAsSPvzK3EdhGPZAMKYh67s02WqfYUe09xMzy7BzNODUKcbyIW5i7GVQ\n' +
'3YurSkR/auRsk6FG45Q1zTZaEvwVH6NfMF0wDgYDVR0PAQH/BAQDAgGmMA8GA1Ud\n' +
'JQQIMAYGBFUdJQAwDwYDVR0TAQH/BAUwAwEB/zApBgNVHQ4EIgQg8HHn3ScArMdH\n' +
'lkp+jpcDXtIAzWnVf4F9rBHvUNjcC1owCgYIKoZIzj0EAwIDSAAwRQIhAMi+R+ZI\n' +
'XgZV40IztD8aQDr/sntDTu/8Nw7Y0DGEhwaQAiBEnBCdRXaBcENWnAnastAg+RA5\n' +
'XALSidlQqZKrK4L3Yg==\n' +
'-----END CERTIFICATE-----';
});
it('should convert one byte to a good PEM', () => {
const normalized = Utils.byteToNormalizedPEM(Buffer.from(pem));
normalized.should.be.equal(pem);
});
it('should convert two certs in byte to a good PEM', () => {
const twoCerts = Buffer.concat([Buffer.from(pem), Buffer.from(pem)]);
const normalized = Utils.byteToNormalizedPEM(twoCerts);
normalized.should.be.equal(pem + pem);
});
it('should convert one byte to a good PEM with no ending new line', () => {
const normalized = Utils.byteToNormalizedPEM(Buffer.from(pem_no_new_line));
normalized.should.be.equal(pem);
});
it('should convert two certs in byte to a good PEM', () => {
const twoCerts = Buffer.concat([Buffer.from(pem_no_new_line), Buffer.from(pem_no_new_line)]);
const normalized = Utils.byteToNormalizedPEM(twoCerts);
normalized.should.be.equal(pem + pem);
});
it('should convert three certs in byte to a good PEM', () => {
const twoCerts = Buffer.concat([Buffer.from(pem_no_new_line), Buffer.from(pem_no_new_line), Buffer.from(pem_no_new_line)]);
const normalized = Utils.byteToNormalizedPEM(twoCerts);
normalized.should.be.equal(pem + pem + pem);
});
});
describe('#convertBytetoString', () => {
it('should ignore additional fields on array-like input', () => {
const arraylike = [];
arraylike.foo = () => 'bar';
const normalized = Utils.convertBytetoString(arraylike);
normalized.should.equal('');
});
});
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/);
});
});
describe('getLogger', () => {
const loggingSetting = Utils.getConfigSetting('hfc-logging');
it('should return a default winston logger', () => {
testUtils.setHFCLogging('undefined');
const logger = Utils.getLogger('testLogger');
logger.transports.should.be.an.instanceOf(Object);
logger.transports.console.should.be.an.instanceOf(winston.transports.Console);
logger.transports.console.exceptionsLevel.should.equal('error');
logger.level.should.equal('info');
});
it('should return a default logger if the settings are defined incorrectly', () => {
testUtils.setHFCLogging("{'debug': 'console'}"); // eslint-disable-line quotes
const logger = Utils.getLogger('testLogger');
logger.transports.console.should.be.an.instanceOf(winston.transports.Console);
logger.transports.console.exceptionsLevel.should.equal('error');
logger.level.should.equal('info');
});
it('should return a default logger if the settings are defined illegally', () => {
testUtils.setHFCLogging(5435453);
const logger = Utils.getLogger('testLogger');
logger.transports.console.should.be.an.instanceOf(winston.transports.Console);
logger.transports.console.exceptionsLevel.should.equal('error');
logger.level.should.equal('info');
});
describe('log to a file', async () => {
let debugFilePath;
let errorFilePath;
let tmpDir;
before(async () => {
// create a temp file
const prefix = `${os.tmpdir()}${path.sep}`;
tmpDir = await fs.promises.mkdtemp(prefix);
debugFilePath = path.join(tmpDir, 'debug.log');
errorFilePath = path.join(tmpDir, 'error.log');
await fs.ensureFile(debugFilePath);
await fs.ensureFile(errorFilePath);
});
it('should write to a log file with the correct logging level', async () => {
testUtils.setHFCLogging(`{"debug": "${debugFilePath}", "error": "${errorFilePath}"}`);
const loggerName = 'fileLogger';
const logger = Utils.getLogger(loggerName);
logger.transports.should.be.an.instanceOf(Object);
logger.transports.debugfile.should.be.an.instanceOf(winston.transports.File);
logger.transports.debugfile.dirname.should.equal(tmpDir);
logger.transports.errorfile.should.be.an.instanceOf(winston.transports.File);
logger.transports.errorfile.dirname.should.equal(tmpDir);
// Log to the file
logger.error('Test logger - error');
logger.warn('Test logger - warn');
logger.info('Test logger - info');
logger.debug('Test logger - debug');
// wait for the logger to log
await new Promise(resolve => setTimeout(resolve, 10));
// read the debug file
const debugData = await fs.readFile(debugFilePath);
// debug file should contain all logging
debugData.indexOf(`error: [${loggerName}]: Test logger - error`).should.be.greaterThan(0);
debugData.indexOf(`warn: [${loggerName}]: Test logger - warn`).should.be.greaterThan(0);
debugData.indexOf(`info: [${loggerName}]: Test logger - info`).should.be.greaterThan(0);
debugData.indexOf(`debug: [${loggerName}]: Test logger - debug`).should.be.greaterThan(0);
debugData.indexOf('Successfully constructed a winston logger with configurations').should.be.greaterThan(0);
// read the error file
const errorData = await fs.readFile(errorFilePath);
// error file should only contain errors
errorData.indexOf(`error: [${loggerName}]: Test logger - error`).should.be.greaterThan(0);
errorData.indexOf('Test logger - warn').should.equal(-1);
errorData.indexOf('Test logger - info').should.equal(-1);
errorData.indexOf('Test logger - debug').should.equal(-1);
});
after(async () => {
// Remove tmp dir
await fs.remove(tmpDir);
});
});
after(async () => {
// restore logging settings
Utils.setConfigSetting('hfc-logging', loggingSetting);
});
});
});