tests: adds ecdsa test enforcing valid signature callback

This commit is contained in:
Daniel Cousens 2015-01-05 11:15:07 +11:00
parent e9778ae358
commit 1e7f537f38

View file

@ -30,17 +30,36 @@ describe('ecdsa', function() {
it('loops until an appropriate k value is found', sinon.test(function() { it('loops until an appropriate k value is found', sinon.test(function() {
this.mock(BigInteger).expects('fromBuffer') this.mock(BigInteger).expects('fromBuffer')
.exactly(3) .exactly(3)
.onCall(0).returns(new BigInteger('0')) .onCall(0).returns(new BigInteger('0')) // < 1
.onCall(1).returns(curve.n) .onCall(1).returns(curve.n) // > n-1
.onCall(2).returns(new BigInteger('42')) .onCall(2).returns(new BigInteger('42')) // valid
var d = new BigInteger('1') var d = new BigInteger('1')
var h1 = new Buffer(32) var h1 = new Buffer(32)
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig) var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig)
assert.equal(k.toString(), '42') assert.equal(k.toString(), '42')
})) }))
it('loops until a suitable signature is found', sinon.test(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
var checkSig = this.mock()
checkSig.exactly(2)
checkSig.onCall(0).returns(false) // bad signature
checkSig.onCall(1).returns(true) // good signature
var d = new BigInteger('1')
var h1 = new Buffer(32)
var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig)
assert.equal(k.toString(), '53')
}))
}) })
describe('recoverPubKey', function() { describe('recoverPubKey', function() {