Merge pull request #1023 from bitcoinjs/nosinon

rm sinon, sinon-test
This commit is contained in:
Daniel Cousens 2018-03-20 21:28:52 +11:00 committed by GitHub
commit 058fa24e09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 78 deletions

View file

@ -51,12 +51,11 @@
"bip65": "^1.0.1",
"bs58": "^4.0.0",
"dhttp": "^2.4.2",
"hoodwink": "^1.0.0",
"minimaldata": "^1.0.2",
"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"

View file

@ -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 hoodwink = require('hoodwink')
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', hoodwink(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', hoodwink(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) {

View file

@ -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 hoodwink = require('hoodwink')
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', hoodwink(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', hoodwink(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', hoodwink(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', hoodwink(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', hoodwink(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)
}))

View file

@ -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 hoodwink = require('hoodwink')
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', hoodwink(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', hoodwink(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', hoodwink(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', hoodwink(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', hoodwink(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', hoodwink(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', hoodwink(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')
}))
})
})