Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit e6667fb

Browse files
full test coverage and benchmarks added
1 parent b012472 commit e6667fb

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

benchmarks/hash.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const Benchmark = require('benchmark')
4+
const multihashing = require('../src')
5+
6+
const suite = new Benchmark.Suite('multihashing')
7+
const list = []
8+
9+
const algs = ['sha1', 'sha2-256', 'sha2-512', 'sha3']
10+
11+
algs.forEach((alg) => {
12+
suite.add(alg, function (d) {
13+
const buf = new Buffer(10 * 1024)
14+
buf.fill(Math.ceil(Math.random() * 100))
15+
16+
multihashing(buf, alg, (err, res) => {
17+
if (err) throw err
18+
list.push(res)
19+
d.resolve()
20+
})
21+
}, {
22+
defer: true
23+
})
24+
})
25+
suite
26+
.on('cycle', (event) => {
27+
console.log(String(event.target))
28+
})
29+
// run async
30+
.run({
31+
async: true
32+
})

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "lib/index.js",
66
"jsnext:main": "src/index.js",
77
"browser": {
8-
"./src/crypto.js": "./src/crypto-browser.js"
8+
"./src/crypto.js": "./src/crypto-browser.js",
9+
"./lib/crypto.js": "./lib/crypto-browser.js"
910
},
1011
"scripts": {
1112
"test": "PHANTOM=off aegir-test",
@@ -17,7 +18,8 @@
1718
"release-major": "PHANTOM=off aegir-release major",
1819
"build": "aegir-build",
1920
"coverage": "aegir-coverage",
20-
"coverage-publish": "aegir-coverage publish"
21+
"coverage-publish": "aegir-coverage publish",
22+
"bench": "node benchmarks/hash.js"
2123
},
2224
"pre-commit": [
2325
"lint",
@@ -42,6 +44,7 @@
4244
},
4345
"devDependencies": {
4446
"aegir": "^8.1.0",
47+
"benchmark": "^2.1.1",
4548
"chai": "^3.5.0",
4649
"pre-commit": "^1.1.2"
4750
},

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function Multihashing (buf, func, length, callback) {
1111
length = undefined
1212
}
1313

14+
if (!callback) {
15+
throw new Error('Missing callback')
16+
}
17+
1418
Multihashing.digest(buf, func, length, (err, digest) => {
1519
if (err) {
1620
return callback(err)

test/index.spec.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,49 @@ describe('multihashing', () => {
6060
})
6161
})
6262

63-
it('throws on non implemented func', () => {
64-
multihashing(new Buffer('beep boop'), 'blake2b', (err) => {
65-
expect(err.message).to.match(/not yet supported/)
63+
it('digest only, without length', (done) => {
64+
const buf = new Buffer('beep boop')
65+
66+
multihashing.digest(buf, 'sha2-256', (err, digest) => {
67+
if (err) {
68+
return done(err)
69+
}
70+
71+
expect(
72+
digest
73+
).to.be.eql(
74+
new Buffer('90ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c', 'hex')
75+
)
76+
77+
done()
78+
})
79+
})
80+
81+
describe('invalid arguments', () => {
82+
it('returns an error on non implemented func', (done) => {
83+
multihashing(new Buffer('beep boop'), 'blake2b', (err) => {
84+
expect(err.message).to.match(/not yet supported/)
85+
done()
86+
})
87+
})
88+
89+
it('digest only, with length, returns error on non implmented func', (done) => {
90+
multihashing.digest(new Buffer('beep boop'), 'blake2b', 10, (err) => {
91+
expect(err.message).to.match(/not yet supported/)
92+
done()
93+
})
94+
})
95+
96+
it('throws on missing callback', () => {
97+
expect(
98+
() => multihashing(new Buffer('beep'), 'sha3')
99+
).to.throw(/Missing callback/)
100+
})
101+
102+
it('digest only, throws on missing callback', () => {
103+
expect(
104+
() => multihashing.digest(new Buffer('beep'), 'sha3')
105+
).to.throw(/Missing callback/)
66106
})
67107
})
68108
})

0 commit comments

Comments
 (0)