Skip to content

Commit

Permalink
Remove randomness from some tests, avoids ff specific errors with old…
Browse files Browse the repository at this point in the history
…er versions.
  • Loading branch information
caedesvvv committed Oct 16, 2014
1 parent 1182fad commit afc4a1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 22 additions & 1 deletion test/unit/model/txSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
define(['model/wallet', 'model/tx', 'bitcoinjs-lib'], function(Wallet, Transaction, Bitcoin) {
describe('Wallet model', function() {

var identity, wallet, _store, _private, initIfEmpty;
var identity, wallet, _store, _private, initIfEmpty, origMakeRandom = window.crypto.getRandomValues;

var removeRandomness = function() {
// remove randomness from the test, this changes how ECKey.makeRandom and crypto browserify rng works
// fixes issues with older ff in tests
window.crypto.getRandomValues = function(bytes) { bytes[0] = 255; };
}
var restoreRandomness = function() {
// restore the original getRandom
window.crypto.getRandomValues = origMakeRandom;
}

beforeEach(function() {
// inhibit initIfEmpty for part of the tests so it doesn't need
// so much processing
Expand Down Expand Up @@ -119,6 +129,7 @@ define(['model/wallet', 'model/tx', 'bitcoinjs-lib'], function(Wallet, Transacti
});
afterEach(function() {
Wallet.prototype.initIfEmpty = initIfEmpty;
restoreRandomness();
});

it('is created properly', function() {
Expand Down Expand Up @@ -168,8 +179,13 @@ define(['model/wallet', 'model/tx', 'bitcoinjs-lib'], function(Wallet, Transacti
var tx = identity.tx.prepare(0, recipients, change, 10000);

var recipients = [{amount: 200000, address: 'vJmtCy3scMRLEDSbFc3xwLXB3Q8fmDLRVaMjC4S2en6KetnAyvUfMT7tvsZPS8xhGGfSmoDGQ8AKRyi7oRYhrhLQJRdvdYLh2z2j5k'}];

removeRandomness();

var tx2 = identity.tx.prepare(0, recipients, change, 10000);

restoreRandomness();

// Stealth addresses have the same values than a normal address
commonTransactionChecks(tx, tx2);

Expand Down Expand Up @@ -200,13 +216,16 @@ define(['model/wallet', 'model/tx', 'bitcoinjs-lib'], function(Wallet, Transacti

it('to multiple stealth addresses', function() {
var recipients = [{amount: 200000, address: juiceRapNews}];

removeRandomness();
var tx = identity.tx.prepare(0, recipients, change, 10000);

recipients = [
{amount: 100000, address: 'vJmtCy3scMRLEDSbFc3xwLXB3Q8fmDLRVaMjC4S2en6KetnAyvUfMT7tvsZPS8xhGGfSmoDGQ8AKRyi7oRYhrhLQJRdvdYLh2z2j5k'},
{amount: 100000, address: 'vJmtCy3scMRLEDSbFc3xwLXB3Q8fmDLRVaMjC4S2en6KetnAyubqxGCwKCgqXQRzqP3b8nbU8yUnuaazNxAq1ZgmtM6ft6tGWptnkx'}
];
var tx4 = identity.tx.prepare(0, recipients, change, 10000);
restoreRandomness();

commonTransactionChecks(tx, tx4);

Expand All @@ -225,7 +244,9 @@ define(['model/wallet', 'model/tx', 'bitcoinjs-lib'], function(Wallet, Transacti
{amount: 100000, address: 'vJmtCy3scMRLEDSbFc3xwLXB3Q8fmDLRVaMjC4S2en6KetnAyubqxGCwKCgqXQRzqP3b8nbU8yUnuaazNxAq1ZgmtM6ft6tGWptnkx'},
{amount: 100000, address: satoshiForest}
];
removeRandomness();
var tx5 = identity.tx.prepare(0, recipients, change, 10000);
restoreRandomness();

commonTransactionChecks(tx, tx5);

Expand Down
7 changes: 7 additions & 0 deletions test/unit/util/stealthSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ define(['util/stealth', 'bitcoinjs-lib'], function(Stealth, Bitcoin) {
var scanBytes = scanKeyPubBytes;
var spendBytes = spendKeyPubBytes;

// remove randomness from the test, this changes how ECKey.makeRandom and crypto browserify rng works
// fixes issues with older ff in tests
var prevMakeRandom = window.crypto.getRandomValues
window.crypto.getRandomValues = function(bytes) { bytes[0] = 255; };

var res = Stealth.initiateStealth(scanBytes, spendBytes, undefined, ephemKeyBytes);
var address = res[0];
var ephemKey = res[1];
Expand All @@ -68,6 +73,8 @@ define(['util/stealth', 'bitcoinjs-lib'], function(Stealth, Bitcoin) {
// try with no ephemKeyBytes so it will be generated (normal case)
res = Stealth.initiateStealth(scanBytes, spendBytes);
expect(res.length).toBe(3);
// restore the original getRandom
window.crypto.getRandomValues = prevMakeRandom;
});

it('uncovers the stealth secret', function() {
Expand Down

0 comments on commit afc4a1d

Please sign in to comment.