From 7c9c6ed20454e55ec49d0b6e4a43bf5ee394c951 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 20 Mar 2018 13:19:39 +1100 Subject: [PATCH] rm sinon, sinon-test --- package.json | 2 -- test/ecdsa.js | 40 ++++++++++++------------------ test/ecpair.js | 60 +++++++++++++++++++++++++++------------------ test/hdnode.js | 66 ++++++++++++++++++++++++++++---------------------- test/mockme.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+), 78 deletions(-) create mode 100644 test/mockme.js diff --git a/package.json b/package.json index 5594b35..7250f63 100644 --- a/package.json +++ b/package.json @@ -55,8 +55,6 @@ "mocha": "^5.0.1", "nyc": "^11.4.1", "proxyquire": "^1.4.0", - "sinon": "^4.3.0", - "sinon-test": "^2.1.3", "standard": "^9.0.2" }, "license": "MIT" diff --git a/test/ecdsa.js b/test/ecdsa.js index 8d8b17c..f93beb9 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -3,9 +3,7 @@ var assert = require('assert') var bcrypto = require('../src/crypto') var ecdsa = require('../src/ecdsa') -var sinon = require('sinon') -var sinonTest = require('sinon-test') -var setupTest = sinonTest(sinon) +var mockme = require('./mockme') var BigInteger = require('bigi') var ECSignature = require('../src/ecsignature') @@ -30,12 +28,13 @@ describe('ecdsa', function () { }) }) - it('loops until an appropriate k value is found', setupTest(function () { - this.mock(BigInteger).expects('fromBuffer') - .exactly(3) - .onCall(0).returns(new BigInteger('0')) // < 1 - .onCall(1).returns(curve.n) // > n-1 - .onCall(2).returns(new BigInteger('42')) // valid + it('loops until an appropriate k value is found', mockme(function () { + this.mock(BigInteger, 'fromBuffer', function f (b) { + assert.strictEqual(b.length, 32) + if (f.calls === 0) return BigInteger.ZERO // < 1 + if (f.calls === 1) return curve.n // > n - 1 + if (f.calls === 2) return new BigInteger('42') // valid + }, 3) var x = new BigInteger('1').toBuffer(32) var h1 = Buffer.alloc(32) @@ -44,24 +43,17 @@ describe('ecdsa', function () { assert.strictEqual(k.toString(), '42') })) - it('loops until a suitable signature is found', setupTest(function () { - this.mock(BigInteger).expects('fromBuffer') - .exactly(4) - .onCall(0).returns(new BigInteger('0')) // < 1 - .onCall(1).returns(curve.n) // > n-1 - .onCall(2).returns(new BigInteger('42')) // valid, but 'bad' signature - .onCall(3).returns(new BigInteger('53')) // valid, good signature + it('loops until a suitable signature is found', mockme(function () { + var checkSigStub = this.stub(function f () { + if (f.calls === 0) return false // bad signature + if (f.calls === 1) return true // good signature + }, 2) - var mockCheckSig = this.mock() - mockCheckSig.exactly(2) - mockCheckSig.onCall(0).returns(false) // bad signature - mockCheckSig.onCall(1).returns(true) // good signature - - var x = new BigInteger('1').toBuffer(32) + var x = BigInteger.ONE.toBuffer(32) var h1 = Buffer.alloc(32) - var k = ecdsa.deterministicGenerateK(h1, x, mockCheckSig) + var k = ecdsa.deterministicGenerateK(h1, x, checkSigStub) - assert.strictEqual(k.toString(), '53') + assert.strictEqual(k.toHex(), 'a9b1a1a84a4c2f96b6158ed7a81404c50cb74373c22e8d9e02d0411d719acae2') })) fixtures.valid.rfc6979.forEach(function (f) { diff --git a/test/ecpair.js b/test/ecpair.js index 7972585..342b166 100644 --- a/test/ecpair.js +++ b/test/ecpair.js @@ -5,9 +5,7 @@ var assert = require('assert') var ecdsa = require('../src/ecdsa') var ecurve = require('ecurve') var proxyquire = require('proxyquire') -var sinon = require('sinon') -var sinonTest = require('sinon-test') -var setupTest = sinonTest(sinon) +var mockme = require('./mockme') var BigInteger = require('bigi') var ECPair = require('../src/ecpair') @@ -76,9 +74,10 @@ describe('ECPair', function () { keyPair = new ECPair(BigInteger.ONE) }) - it('wraps Q.getEncoded', setupTest(function () { - this.mock(keyPair.Q).expects('getEncoded') - .once().withArgs(keyPair.compressed) + it('wraps Q.getEncoded', mockme(function () { + this.mock(keyPair.Q, 'getEncoded', function (compressed) { + assert.strictEqual(compressed, keyPair.compressed) + }, 1) keyPair.getPublicKeyBuffer() })) @@ -167,21 +166,31 @@ describe('ECPair', function () { assert.strictEqual(keyPair.network, NETWORKS.testnet) }) - it('loops until d is within interval [1, n - 1] : 1', setupTest(function () { - var rng = this.mock() - rng.exactly(2) - rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // invalid length - rng.onCall(1).returns(BigInteger.ONE.toBuffer(32)) // === 1 + it('throws if d is bad length', function () { + function rng () { + return BigInteger.ZERO.toBuffer(28) + } + + assert.throws(function () { + ECPair.makeRandom({ rng: rng }) + }, /Expected Buffer\(Length: 32\), got Buffer\(Length: 28\)/) + }) + + it('loops until d is within interval [1, n - 1] : 1', mockme(function () { + var rng = this.stub(function f () { + if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // 0 + return BigInteger.ONE.toBuffer(32) // >0 + }, 2) ECPair.makeRandom({ rng: rng }) })) - it('loops until d is within interval [1, n - 1] : n - 1', setupTest(function () { - var rng = this.mock() - rng.exactly(3) - rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // < 1 - rng.onCall(1).returns(curve.n.toBuffer(32)) // > n-1 - rng.onCall(2).returns(curve.n.subtract(BigInteger.ONE).toBuffer(32)) // === n-1 + it('loops until d is within interval [1, n - 1] : n - 1', mockme(function () { + var rng = this.stub(function f () { + if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // <1 + if (f.calls === 1) return curve.n.toBuffer(32) // >n-1 + return curve.n.subtract(BigInteger.ONE).toBuffer(32) // n-1 + }, 3) ECPair.makeRandom({ rng: rng }) })) @@ -217,9 +226,11 @@ describe('ECPair', function () { }) describe('signing', function () { - it('wraps ecdsa.sign', setupTest(function () { - this.mock(ecdsa).expects('sign') - .once().withArgs(hash, keyPair.d) + it('wraps ecdsa.sign', mockme(function () { + this.mock(ecdsa, 'sign', function (h, d) { + assert.strictEqual(h, hash) + assert.strictEqual(d, keyPair.d) + }, 1) keyPair.sign(hash) })) @@ -240,9 +251,12 @@ describe('ECPair', function () { signature = keyPair.sign(hash) }) - it('wraps ecdsa.verify', setupTest(function () { - this.mock(ecdsa).expects('verify') - .once().withArgs(hash, signature, keyPair.Q) + it('wraps ecdsa.verify', mockme(function () { + this.mock(ecdsa, 'verify', function (h, s, q) { + assert.strictEqual(h, hash) + assert.strictEqual(s, signature) + assert.strictEqual(q, keyPair.Q) + }, 1) keyPair.verify(hash, signature) })) diff --git a/test/hdnode.js b/test/hdnode.js index 944dded..b5321a5 100644 --- a/test/hdnode.js +++ b/test/hdnode.js @@ -3,9 +3,7 @@ var assert = require('assert') var ecdsa = require('../src/ecdsa') -var sinon = require('sinon') -var sinonTest = require('sinon-test') -var setupTest = sinonTest(sinon) +var mockme = require('./mockme') var BigInteger = require('bigi') var ECPair = require('../src/ecpair') @@ -81,18 +79,20 @@ describe('HDNode', function () { }) }) - it('throws if IL is not within interval [1, n - 1] | IL === 0', setupTest(function () { - this.mock(BigInteger).expects('fromBuffer') - .once().returns(BigInteger.ZERO) + it('throws if IL is not within interval [1, n - 1] | IL === 0', mockme(function () { + this.mock(BigInteger, 'fromBuffer', function () { + return BigInteger.ZERO + }, 1) assert.throws(function () { HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff') }, /Private key must be greater than 0/) })) - it('throws if IL is not within interval [1, n - 1] | IL === n', setupTest(function () { - this.mock(BigInteger).expects('fromBuffer') - .once().returns(curve.n) + it('throws if IL is not within interval [1, n - 1] | IL === n', mockme(function () { + this.mock(BigInteger, 'fromBuffer', function () { + return curve.n + }, 1) assert.throws(function () { HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff') @@ -124,38 +124,43 @@ describe('HDNode', function () { }) describe('getAddress', function () { - it('wraps keyPair.getAddress', setupTest(function () { - this.mock(keyPair).expects('getAddress') - .once().withArgs().returns('foobar') + it('wraps keyPair.getAddress', mockme(function () { + this.mock(hd.keyPair, 'getAddress', function () { + return 'foo' + }, 1) - assert.strictEqual(hd.getAddress(), 'foobar') + assert.strictEqual(hd.getAddress(), 'foo') })) }) describe('getNetwork', function () { - it('wraps keyPair.getNetwork', setupTest(function () { - this.mock(keyPair).expects('getNetwork') - .once().withArgs().returns('network') + it('wraps keyPair.getNetwork', mockme(function () { + this.mock(hd.keyPair, 'getNetwork', function () { + return 'foo' + }, 1) - assert.strictEqual(hd.getNetwork(), 'network') + assert.strictEqual(hd.getNetwork(), 'foo') })) }) describe('getPublicKeyBuffer', function () { - it('wraps keyPair.getPublicKeyBuffer', setupTest(function () { - this.mock(keyPair).expects('getPublicKeyBuffer') - .once().withArgs().returns('pubKeyBuffer') + it('wraps keyPair.getPublicKeyBuffer', mockme(function () { + this.mock(hd.keyPair, 'getPublicKeyBuffer', function () { + return 'foo' + }, 1) - assert.strictEqual(hd.getPublicKeyBuffer(), 'pubKeyBuffer') + assert.strictEqual(hd.getPublicKeyBuffer(), 'foo') })) }) describe('sign', function () { - it('wraps keyPair.sign', setupTest(function () { - this.mock(keyPair).expects('sign') - .once().withArgs(hash).returns('signed') + it('wraps keyPair.sign', mockme(function () { + this.mock(hd.keyPair, 'sign', function (h) { + assert.strictEqual(hash, h) + return 'foo' + }, 1) - assert.strictEqual(hd.sign(hash), 'signed') + assert.strictEqual(hd.sign(hash), 'foo') })) }) @@ -166,11 +171,14 @@ describe('HDNode', function () { signature = hd.sign(hash) }) - it('wraps keyPair.verify', setupTest(function () { - this.mock(keyPair).expects('verify') - .once().withArgs(hash, signature).returns('verified') + it('wraps keyPair.verify', mockme(function () { + this.mock(hd.keyPair, 'verify', function (h, s) { + assert.strictEqual(hash, h) + assert.strictEqual(signature, s) + return 'foo' + }, 1) - assert.strictEqual(hd.verify(hash, signature), 'verified') + assert.strictEqual(hd.verify(hash, signature), 'foo') })) }) }) diff --git a/test/mockme.js b/test/mockme.js new file mode 100644 index 0000000..2507ef6 --- /dev/null +++ b/test/mockme.js @@ -0,0 +1,66 @@ +// TODO: move to own dependency +function mockme (f) { + var mocks = [] + + function mock (constructor, functionName, func, n) { + n = n || Infinity + + var initial = constructor[functionName] + var context = constructor.constructor.name !== 'Function' ? constructor : null + function __mock () { + if (func.calls > n) throw new RangeError('Exceeded expected number of calls') + var r = func.apply(context, arguments) + ++func.calls + return r + } + func.calls = 0 + func.expected = n + func.reset = function reset () { + constructor[functionName] = initial + } + constructor[functionName] = __mock + mocks.push(func) + } + + function stub (func, n) { + n = n || Infinity + + function __stub () { + if (func.calls > n) throw new RangeError('Exceeded expected number of calls') + var r = func.apply(null, arguments) + ++func.calls + return r + } + func.calls = 0 + func.expected = n + + mocks.push(func) + return __stub + } + + return function run () { + var err + try { + f.apply({ + mock: mock, + stub: stub + }, arguments) + } catch (e) { + err = e + } + + mocks.forEach(function (x) { + if (!err) { + if (x.expected !== Infinity && x.calls !== x.expected) { + err = new RangeError('Too few calls') + } + } + + if (x.reset) x.reset() + }) + + if (err) throw err + } +} + +module.exports = mockme