Skip to content

Commit

Permalink
[FABN-599] Unit tests for hash
Browse files Browse the repository at this point in the history
- Hash unit tests using mocha at 100%
- Fixed licensing errors in hash
- Removed hash.js Tape unit tests

Change-Id: I9407c267c1a40394c2b6e4f7ce86cbdb64a9ba37
Signed-off-by: Liam Grace <liamgrace.896@gmail.com>
  • Loading branch information
liam-grace committed Sep 14, 2018
1 parent e222a83 commit 3e09b99
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 51 deletions.
15 changes: 11 additions & 4 deletions fabric-client/lib/hash.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/**
* Copyright 2016, 2018 IBM All Rights Reserved.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
SPDX-License-Identifier: Apache-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
Expand Down
274 changes: 274 additions & 0 deletions fabric-client/test/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const rewire = require('rewire');
const Hash = rewire('../lib/hash');
const {hash_sha2_256, hash_sha2_384, hash_sha3_256, hash_sha3_384} = Hash;
const sinon = require('sinon');

describe('hash_sha2_256', () => {
let sandbox;
let revert;
let hash;

beforeEach(() => {
revert = [];
sandbox = sinon.createSandbox();
});

afterEach(() => {
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
sandbox.restore();
});

describe('#hash', () => {
it('should call the reset reset.update.finalize and return the correct value', () => {
hash = new hash_sha2_256();
const finalizeStub = sandbox.stub().returns('finalize');
const updateStub = sandbox.stub().returns({finalize: finalizeStub});
const resetStub = sandbox.stub(hash, 'reset').returns({update: updateStub});
hash.hash('data').should.equal('finalize');
sinon.assert.called(resetStub);
sinon.assert.calledWith(updateStub, 'data');
sinon.assert.calledWith(finalizeStub, 'hex');
});
});

describe('#reset', () => {
it('should call crypto.createHash', () => {
const cryptoStub = {createHash: sandbox.stub()};
revert.push(Hash.__set__('crypto', cryptoStub));
hash = new hash_sha2_256();
hash.reset();
sinon.assert.calledWith(cryptoStub.createHash, 'sha256');
});
});

describe('#finalize', () => {
it('should call _hash.digest, reset and reurn the hash', () => {
hash = new hash_sha2_256();
const digestStub = sandbox.stub().returns('hash');
hash._hash = {digest: digestStub};
const resetStub = sandbox.stub(hash, 'reset');
hash.finalize('encoding').should.equal('hash');
sinon.assert.calledWith(digestStub, 'encoding');
sinon.assert.called(resetStub);
});
});
});

describe('hash_sha2_384', () => {
let sandbox;
let revert;
let hash;

beforeEach(() => {
revert = [];
sandbox = sinon.createSandbox();
});

afterEach(() => {
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
sandbox.restore();
});

describe('#hash', () => {
it('should call the reset reset.update.finalize and return the correct value', () => {
hash = new hash_sha2_384();
const finalizeStub = sandbox.stub().returns('finalize');
const updateStub = sandbox.stub().returns({finalize: finalizeStub});
const resetStub = sandbox.stub(hash, 'reset').returns({update: updateStub});
hash.hash('data').should.equal('finalize');
sinon.assert.called(resetStub);
sinon.assert.calledWith(updateStub, 'data');
sinon.assert.calledWith(finalizeStub, 'hex');
});
});

describe('#reset', () => {
it('should call crypto.createHash', () => {
const cryptoStub = {createHash: sandbox.stub()};
revert.push(Hash.__set__('crypto', cryptoStub));
hash = new hash_sha2_384();
hash.reset();
sinon.assert.calledWith(cryptoStub.createHash, 'sha384');
});
});

describe('#finalize', () => {
it('should call _hash.digest, reset and reurn the hash', () => {
hash = new hash_sha2_384();
const digestStub = sandbox.stub().returns('hash');
hash._hash = {digest: digestStub};
const resetStub = sandbox.stub(hash, 'reset');
hash.finalize('encoding').should.equal('hash');
sinon.assert.calledWith(digestStub, 'encoding');
sinon.assert.called(resetStub);
});
});
});

describe('hash_sha3_256', () => {
let sandbox;
let revert;
let hash;

beforeEach(() => {
revert = [];
sandbox = sinon.createSandbox();
});

afterEach(() => {
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
sandbox.restore();
});

describe('hashSimple', () => {
it('should create an instance of sha3_256', () => {
const mockSha3256 = sandbox.stub();
revert.push(Hash.__set__('sha3_256', mockSha3256));
hash_sha3_256.hashSimple('data');
sinon.assert.calledWith(mockSha3256, 'data');
});
});

describe('#reset', () => {
it('should call sha3_256.create', () => {
const sha3_256Stub = {create: sandbox.stub()};
revert.push(Hash.__set__('sha3_256', sha3_256Stub));
hash = new hash_sha3_256();
hash.reset();
sinon.assert.called(sha3_256Stub.create);
});
});

describe('#finalize', () => {
it('should call _hash.hex, reset and reurn the hash', () => {
hash = new hash_sha3_256();
const hexStub = sandbox.stub().returns('hash');
hash._hash = {hex: hexStub};
const resetStub = sandbox.stub(hash, 'reset');
hash.finalize().should.equal('hash');
sinon.assert.called(hexStub);
sinon.assert.called(resetStub);
});
});
});

describe('hash_sha3_384', () => {
let sandbox;
let revert;
let hash;

beforeEach(() => {
revert = [];
sandbox = sinon.createSandbox();
});

afterEach(() => {
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
sandbox.restore();
});

describe('hashSimple', () => {
it('should create an instance of hash_sha3_384', () => {
const mockSha3384 = sandbox.stub();
revert.push(Hash.__set__('sha3_384', mockSha3384));
hash_sha3_384.hashSimple('data');
sinon.assert.calledWith(mockSha3384, 'data');
});
});

describe('#reset', () => {
it('should call sha3_384.create', () => {
const sha3_384Stub = {create: sandbox.stub()};
revert.push(Hash.__set__('sha3_384', sha3_384Stub));
hash = new hash_sha3_384();
hash.reset();
sinon.assert.called(sha3_384Stub.create);
});
});

describe('#finalize', () => {
it('should call _hash.hex, reset and reurn the hash', () => {
hash = new hash_sha3_384();
const hexStub = sandbox.stub().returns('hash');
hash._hash = {hex: hexStub};
const resetStub = sandbox.stub(hash, 'reset');
hash.finalize().should.equal('hash');
sinon.assert.called(hexStub);
sinon.assert.called(resetStub);
});
});
});

describe('SHA2_256', () => {
let sandbox;
let revert;

beforeEach(() => {
revert = [];
sandbox = sinon.createSandbox();
});

afterEach(() => {
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
sandbox.restore();
});
it('should call hash and return an insatnce of hash_sha2_256', () => {
const mockHashFunction = sandbox.stub().returns('hash');
const mockHash = sandbox.stub().returns({hash: mockHashFunction});
revert.push(Hash.__set__('hash_sha2_256', mockHash));
Hash.SHA2_256('data').should.equal('hash');
sinon.assert.called(mockHash);
sinon.assert.calledWith(mockHashFunction, 'data');
});
});

describe('SHA2_384', () => {
let sandbox;
let revert;

beforeEach(() => {
revert = [];
sandbox = sinon.createSandbox();
});

afterEach(() => {
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
sandbox.restore();
});
it('should call hash and return an insatnce of hash_sha2_384', () => {
const mockHashFunction = sandbox.stub().returns('hash');
const mockHash = sandbox.stub().returns({hash: mockHashFunction});
revert.push(Hash.__set__('hash_sha2_384', mockHash));
Hash.SHA2_384('data').should.equal('hash');
sinon.assert.called(mockHash);
sinon.assert.calledWith(mockHashFunction, 'data');
});
});
47 changes: 0 additions & 47 deletions test/unit/hash.js

This file was deleted.

0 comments on commit 3e09b99

Please sign in to comment.