From 4e2af473abbd1c091684843a6df550ff22fbee41 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 10 Aug 2017 11:17:10 -0700 Subject: [PATCH] test: add better assertion methods. --- test/aes-test.js | 10 +++--- test/bech32-test.js | 2 +- test/bip150-test.js | 2 +- test/bip151-test.js | 2 +- test/bip70-test.js | 6 ++-- test/block-test.js | 34 +++++++++--------- test/bloom-test.js | 4 +-- test/chachapoly-test.js | 20 +++++------ test/chain-test.js | 4 +-- test/coins-test.js | 4 +-- test/gcs-test.js | 10 +++--- test/hd-test.js | 2 +- test/hkdf-test.js | 10 +++--- test/http-test.js | 2 +- test/keyring-test.js | 2 +- test/mempool-test.js | 2 +- test/mnemonic-test.js | 14 ++++---- test/node-test.js | 4 +-- test/protocol-test.js | 4 +-- test/schnorr-test.js | 4 +-- test/script-test.js | 8 +++-- test/scrypt-test.js | 2 +- test/siphash-test.js | 2 +- test/tx-test.js | 4 +-- test/util/assert.js | 76 +++++++++++++++++++++++++++++++++++++++++ test/utils-test.js | 23 +++++++++---- test/wallet-test.js | 2 +- 27 files changed, 174 insertions(+), 85 deletions(-) create mode 100644 test/util/assert.js diff --git a/test/aes-test.js b/test/aes-test.js index aa560d04..139c301e 100644 --- a/test/aes-test.js +++ b/test/aes-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const aes = require('../lib/crypto/aes'); describe('AES', function() { @@ -24,10 +24,10 @@ describe('AES', function() { 'hex'); const ciphertext = aes.encipher(data, key, iv); - assert.deepStrictEqual(ciphertext, expected); + assert.bufferEqual(ciphertext, expected); const plaintext = aes.decipher(ciphertext, key, iv); - assert.deepStrictEqual(plaintext, data); + assert.bufferEqual(plaintext, data); }); it('should encrypt and decrypt with uneven blocks', () => { @@ -41,9 +41,9 @@ describe('AES', function() { 'hex'); const ciphertext = aes.encipher(data, key, iv); - assert.deepStrictEqual(ciphertext, expected); + assert.bufferEqual(ciphertext, expected); const plaintext = aes.decipher(ciphertext, key, iv); - assert.deepStrictEqual(plaintext, data); + assert.bufferEqual(plaintext, data); }); }); diff --git a/test/bech32-test.js b/test/bech32-test.js index 996a36b9..15ff60be 100644 --- a/test/bech32-test.js +++ b/test/bech32-test.js @@ -26,7 +26,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const bech32 = require('../lib/utils/bech32'); const Address = require('../lib/primitives/address'); diff --git a/test/bip150-test.js b/test/bip150-test.js index 79d2735f..579e874d 100644 --- a/test/bip150-test.js +++ b/test/bip150-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const secp256k1 = require('../lib/crypto/secp256k1'); const BIP150 = require('../lib/net/bip150'); const BIP151 = require('../lib/net/bip151'); diff --git a/test/bip151-test.js b/test/bip151-test.js index e45d2800..b7b0a6ab 100644 --- a/test/bip151-test.js +++ b/test/bip151-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const BIP151 = require('../lib/net/bip151'); const client = new BIP151(); diff --git a/test/bip70-test.js b/test/bip70-test.js index e51f5c06..d45446ea 100644 --- a/test/bip70-test.js +++ b/test/bip70-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const util = require('../lib/utils/util'); const bip70 = require('../lib/bip70'); const Address = require('../lib/primitives/address'); @@ -38,7 +38,7 @@ function testRequest(data) { assert(req.paymentDetails.memo.length !== 0); assert(req.paymentDetails.paymentUrl.length !== 0); - assert.deepStrictEqual(req.toRaw(), data); + assert.bufferEqual(req.toRaw(), data); assert(req.verify()); } @@ -153,7 +153,7 @@ describe('BIP70', function() { assert.strictEqual(ack.memo.length, 95); assert.strictEqual(ack.memo, 'Transaction received by BitPay.' + ' Invoice will be marked as paid if the transaction is confirmed.'); - assert.deepStrictEqual(ack.toRaw(), tests.ack); + assert.bufferEqual(ack.toRaw(), tests.ack); }); it('should create a payment request, sign, and verify', () => { diff --git a/test/block-test.js b/test/block-test.js index efb71527..6ee8c6b1 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const fs = require('../lib/utils/fs'); const common = require('./util/common'); const Bloom = require('../lib/utils/bloom'); @@ -68,8 +68,8 @@ describe('Block', function() { it('should decode/encode merkle block', () => { const merkle = MerkleBlock.fromRaw(merkle300025); merkle.refresh(); - assert.deepStrictEqual(merkle.toRaw(), merkle300025); - assert.deepStrictEqual(merkle300025, mblock.toRaw()); + assert.bufferEqual(merkle.toRaw(), merkle300025); + assert.bufferEqual(mblock.toRaw(), merkle300025); }); it('should be verify merkle block', () => { @@ -80,7 +80,7 @@ describe('Block', function() { it('should be encoded/decoded and still verify', () => { const raw = mblock.toRaw(); const merkle = MerkleBlock.fromRaw(raw); - assert.deepStrictEqual(merkle.toRaw(), raw); + assert.bufferEqual(merkle.toRaw(), raw); assert(merkle.verify()); }); @@ -131,7 +131,7 @@ describe('Block', function() { const merkle = MerkleBlock.fromBlock(block, filter); assert(merkle.verifyBody()); - assert.deepStrictEqual(merkle.toRaw(), mblock.toRaw()); + assert.bufferEqual(merkle.toRaw(), mblock.toRaw()); }); it('should verify a historical block', () => { @@ -228,8 +228,8 @@ describe('Block', function() { assert(cblock1.init()); - assert.deepStrictEqual(cblock1.toRaw(), compact426884); - assert.deepStrictEqual(cblock2.toRaw(), compact426884); + assert.bufferEqual(cblock1.toRaw(), compact426884); + assert.bufferEqual(cblock2.toRaw(), compact426884); const map = new Map(); @@ -244,7 +244,7 @@ describe('Block', function() { for (const tx of cblock1.available) assert(tx); - assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); + assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw()); }); it('should handle half-full compact block', () => { @@ -254,8 +254,8 @@ describe('Block', function() { assert(cblock1.init()); - assert.deepStrictEqual(cblock1.toRaw(), compact426884); - assert.deepStrictEqual(cblock2.toRaw(), compact426884); + assert.bufferEqual(cblock1.toRaw(), compact426884); + assert.bufferEqual(cblock2.toRaw(), compact426884); const map = new Map(); @@ -282,7 +282,7 @@ describe('Block', function() { for (const tx of cblock1.available) assert(tx); - assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); + assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw()); }); it('should handle compact block', () => { @@ -292,8 +292,8 @@ describe('Block', function() { assert(cblock1.init()); - assert.deepStrictEqual(cblock1.toRaw(), compact898352); - assert.deepStrictEqual(cblock2.toRaw(), compact898352); + assert.bufferEqual(cblock1.toRaw(), compact898352); + assert.bufferEqual(cblock2.toRaw(), compact898352); assert.strictEqual(cblock1.sid(block.txs[1].hash()), 125673511480291); @@ -310,7 +310,7 @@ describe('Block', function() { for (const tx of cblock1.available) assert(tx); - assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); + assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw()); }); it('should handle half-full compact block', () => { @@ -320,8 +320,8 @@ describe('Block', function() { assert(cblock1.init()); - assert.deepStrictEqual(cblock1.toRaw(), compact898352); - assert.deepStrictEqual(cblock2.toRaw(), compact898352); + assert.bufferEqual(cblock1.toRaw(), compact898352); + assert.bufferEqual(cblock2.toRaw(), compact898352); assert.strictEqual(cblock1.sid(block.txs[1].hash()), 125673511480291); @@ -352,7 +352,7 @@ describe('Block', function() { for (const tx of cblock1.available) assert(tx); - assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); + assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw()); }); it('should count sigops for block 928927 (testnet)', () => { diff --git a/test/bloom-test.js b/test/bloom-test.js index 45387f0e..520509a5 100644 --- a/test/bloom-test.js +++ b/test/bloom-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const Bloom = require('../lib/utils/bloom'); const RollingFilter = require('../lib/utils/rollingfilter'); const murmur3 = require('../lib/utils/murmur3'); @@ -75,7 +75,7 @@ describe('Bloom', function() { 'hex'); filter.add(item1, 'hex'); filter.add(item2, 'hex'); - assert.deepStrictEqual(filter.filter, filterRaw); + assert.bufferEqual(filter.filter, filterRaw); }); it('should handle 1m ops with regular filter', () => { diff --git a/test/chachapoly-test.js b/test/chachapoly-test.js index d595b74f..3dd297be 100644 --- a/test/chachapoly-test.js +++ b/test/chachapoly-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const ChaCha20 = require('../lib/crypto/chacha20'); const Poly1305 = require('../lib/crypto/poly1305'); const AEAD = require('../lib/crypto/aead'); @@ -19,12 +19,12 @@ function testChaCha(options) { ctx1.init(key, nonce, counter); const plainenc = Buffer.from(plain); ctx1.encrypt(plainenc); - assert.deepStrictEqual(plainenc, ciphertext); + assert.bufferEqual(plainenc, ciphertext); const ctx2 = new ChaCha20(); ctx2.init(key, nonce, counter); ctx2.encrypt(ciphertext); - assert.deepStrictEqual(plain, ciphertext); + assert.bufferEqual(plain, ciphertext); } function testAEAD(options) { @@ -39,21 +39,21 @@ function testAEAD(options) { const ctx1 = new AEAD(); ctx1.init(key, nonce); assert.strictEqual(ctx1.chacha20.getCounter(), 1); - assert.deepStrictEqual(ctx1.polyKey, pk); + assert.bufferEqual(ctx1.polyKey, pk); ctx1.aad(aad); const plainenc = Buffer.from(plain); ctx1.encrypt(plainenc); - assert.deepStrictEqual(plainenc, ciphertext); - assert.deepStrictEqual(ctx1.finish(), tag); + assert.bufferEqual(plainenc, ciphertext); + assert.bufferEqual(ctx1.finish(), tag); const ctx2 = new AEAD(); ctx2.init(key, nonce); assert.strictEqual(ctx2.chacha20.getCounter(), 1); - assert.deepStrictEqual(ctx2.polyKey, pk); + assert.bufferEqual(ctx2.polyKey, pk); ctx2.aad(aad); ctx2.decrypt(ciphertext); - assert.deepStrictEqual(ciphertext, plain); - assert.deepStrictEqual(ctx2.finish(), tag); + assert.bufferEqual(ciphertext, plain); + assert.bufferEqual(ctx2.finish(), tag); } describe('ChaCha20 / Poly1305 / AEAD', function() { @@ -160,7 +160,7 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { const mac = Poly1305.auth(msg, key); assert(Poly1305.verify(mac, expected)); - assert.deepStrictEqual(mac, expected); + assert.bufferEqual(mac, expected); }); it('should perform poly1305', () => { diff --git a/test/chain-test.js b/test/chain-test.js index 298cc5a8..ee47adcb 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const BN = require('../lib/crypto/bn'); const consensus = require('../lib/protocol/consensus'); const encoding = require('../lib/utils/encoding'); @@ -316,7 +316,7 @@ describe('Chain', function() { const coin = await chain.db.getCoin(tx.hash('hex'), 2); - assert.deepStrictEqual(coin.toRaw(), output.toRaw()); + assert.bufferEqual(coin.toRaw(), output.toRaw()); }); it('should have correct wallet balance', async () => { diff --git a/test/coins-test.js b/test/coins-test.js index ec07aba7..a19d4366 100644 --- a/test/coins-test.js +++ b/test/coins-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const Output = require('../lib/primitives/output'); const Input = require('../lib/primitives/input'); const Outpoint = require('../lib/primitives/outpoint'); @@ -27,7 +27,7 @@ function deepCoinsEqual(a, b) { assert.strictEqual(a.version, b.version); assert.strictEqual(a.height, b.height); assert.strictEqual(a.coinbase, b.coinbase); - assert.deepStrictEqual(a.raw, b.raw); + assert.bufferEqual(a.raw, b.raw); } describe('Coins', function() { diff --git a/test/gcs-test.js b/test/gcs-test.js index c5b5c823..67e4040d 100644 --- a/test/gcs-test.js +++ b/test/gcs-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const fs = require('../lib/utils/fs'); const GCSFilter = require('../lib/utils/gcs'); const random = require('../lib/crypto/random'); @@ -104,16 +104,16 @@ describe('GCS', function() { assert.strictEqual(filter1.n, contents1.length); assert.strictEqual(filter1.p, filter2.p); assert.strictEqual(filter1.n, filter2.n); - assert.deepStrictEqual(filter1.data, filter2.data); + assert.bufferEqual(filter1.data, filter2.data); assert.strictEqual(filter1.p, filter3.p); assert.strictEqual(filter1.n, filter3.n); - assert.deepStrictEqual(filter1.data, filter3.data); + assert.bufferEqual(filter1.data, filter3.data); assert.strictEqual(filter1.p, filter4.p); assert.strictEqual(filter1.n, filter4.n); - assert.deepStrictEqual(filter1.data, filter4.data); + assert.bufferEqual(filter1.data, filter4.data); assert.strictEqual(filter1.p, filter5.p); assert.strictEqual(filter1.n, filter5.n); - assert.deepStrictEqual(filter1.data, filter5.data); + assert.bufferEqual(filter1.data, filter5.data); }); it('should test GCS filter match', () => { diff --git a/test/hd-test.js b/test/hd-test.js index 51e2ffe9..a059aee8 100644 --- a/test/hd-test.js +++ b/test/hd-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const HD = require('../lib/hd'); const base58 = require('../lib/utils/base58'); const pbkdf2 = require('../lib/crypto/pbkdf2'); diff --git a/test/hkdf-test.js b/test/hkdf-test.js index a2493f9b..8a43dcc6 100644 --- a/test/hkdf-test.js +++ b/test/hkdf-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const hkdf = require('../lib/crypto/hkdf'); describe('HKDF', function() { @@ -28,8 +28,8 @@ describe('HKDF', function() { const prk = hkdf.extract(ikm, salt, alg); const okm = hkdf.expand(prk, info, len, alg); - assert.deepStrictEqual(prk, prkE); - assert.deepStrictEqual(okm, okmE); + assert.bufferEqual(prk, prkE); + assert.bufferEqual(okm, okmE); }); it('should do proper hkdf (2)', () => { @@ -78,7 +78,7 @@ describe('HKDF', function() { const prk = hkdf.extract(ikm, salt, alg); const okm = hkdf.expand(prk, info, len, alg); - assert.deepStrictEqual(prk, prkE); - assert.deepStrictEqual(okm, okmE); + assert.bufferEqual(prk, prkE); + assert.bufferEqual(okm, okmE); }); }); diff --git a/test/http-test.js b/test/http-test.js index f29632b3..173fc789 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const consensus = require('../lib/protocol/consensus'); const encoding = require('../lib/utils/encoding'); const co = require('../lib/utils/co'); diff --git a/test/keyring-test.js b/test/keyring-test.js index 70bccde1..a7b8c0c9 100644 --- a/test/keyring-test.js +++ b/test/keyring-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const KeyRing = require('../lib/primitives/keyring'); describe('KeyRing', function() { diff --git a/test/mempool-test.js b/test/mempool-test.js index 47e385b7..d1af65e5 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const encoding = require('../lib/utils/encoding'); const random = require('../lib/crypto/random'); const MempoolEntry = require('../lib/mempool/mempoolentry'); diff --git a/test/mnemonic-test.js b/test/mnemonic-test.js index cea0e783..10429cee 100644 --- a/test/mnemonic-test.js +++ b/test/mnemonic-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const Mnemonic = require('../lib/hd/mnemonic'); const HDPrivateKey = require('../lib/hd/private'); @@ -32,8 +32,8 @@ describe('Mnemonic', function() { }); assert.strictEqual(mnemonic.getPhrase(), phrase); - assert.deepStrictEqual(mnemonic.getEntropy(), entropy); - assert.deepStrictEqual(mnemonic.toSeed(), seed); + assert.bufferEqual(mnemonic.getEntropy(), entropy); + assert.bufferEqual(mnemonic.toSeed(), seed); const key = HDPrivateKey.fromMnemonic(mnemonic); assert.strictEqual(key.toBase58(), xpriv); @@ -47,8 +47,8 @@ describe('Mnemonic', function() { }); assert.strictEqual(mnemonic.getPhrase(), phrase); - assert.deepStrictEqual(mnemonic.getEntropy(), entropy); - assert.deepStrictEqual(mnemonic.toSeed(), seed); + assert.bufferEqual(mnemonic.getEntropy(), entropy); + assert.bufferEqual(mnemonic.toSeed(), seed); const key = HDPrivateKey.fromMnemonic(mnemonic); assert.strictEqual(key.toBase58(), xpriv); @@ -61,9 +61,9 @@ describe('Mnemonic', function() { it('should verify phrase', () => { const m1 = new Mnemonic(); const m2 = Mnemonic.fromPhrase(m1.getPhrase()); - assert.deepStrictEqual(m2.getEntropy(), m1.getEntropy()); + assert.bufferEqual(m2.getEntropy(), m1.getEntropy()); assert.strictEqual(m2.bits, m1.bits); assert.strictEqual(m2.language, m1.language); - assert.deepStrictEqual(m2.toSeed(), m1.toSeed()); + assert.bufferEqual(m2.toSeed(), m1.toSeed()); }); }); diff --git a/test/node-test.js b/test/node-test.js index 70ae859e..0516276c 100644 --- a/test/node-test.js +++ b/test/node-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const BN = require('../lib/crypto/bn'); const consensus = require('../lib/protocol/consensus'); const co = require('../lib/utils/co'); @@ -248,7 +248,7 @@ describe('Node', function() { const coin = await chain.db.getCoin(tx.hash('hex'), 1); - assert.deepStrictEqual(coin.toRaw(), output.toRaw()); + assert.bufferEqual(coin.toRaw(), output.toRaw()); }); it('should get balance', async () => { diff --git a/test/protocol-test.js b/test/protocol-test.js index 2231bea7..22e26aa3 100644 --- a/test/protocol-test.js +++ b/test/protocol-test.js @@ -4,7 +4,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const Network = require('../lib/protocol/network'); const util = require('../lib/utils/util'); const NetAddress = require('../lib/primitives/netaddress'); @@ -117,6 +117,6 @@ describe('Protocol', function() { const tx = TX.fromRaw(raw); tx.refresh(); - assert.deepStrictEqual(tx.toRaw(), tx8.tx.toRaw()); + assert.bufferEqual(tx.toRaw(), tx8.tx.toRaw()); }); }); diff --git a/test/schnorr-test.js b/test/schnorr-test.js index c748f840..8992e304 100644 --- a/test/schnorr-test.js +++ b/test/schnorr-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const secp256k1 = require('../lib/crypto/secp256k1'); const digest = require('../lib/crypto/digest'); const schnorr = require('../lib/crypto/schnorr'); @@ -15,6 +15,6 @@ describe('Schnorr', function() { const msg = digest.hash256(Buffer.from('foo', 'ascii')); const sig = schnorr.sign(msg, key); assert(schnorr.verify(msg, sig, pub)); - assert.deepStrictEqual(schnorr.recover(sig, msg), pub); + assert.bufferEqual(schnorr.recover(sig, msg), pub); }); }); diff --git a/test/script-test.js b/test/script-test.js index 199a9057..16484579 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const Script = require('../lib/script/script'); const Witness = require('../lib/script/witness'); const Stack = require('../lib/script/stack'); @@ -82,7 +82,7 @@ describe('Script', function() { assert.strictEqual(decoded.code[2].value, opcodes.OP_CHECKSIG); const dst = decoded.toRaw(); - assert.deepStrictEqual(dst, src); + assert.bufferEqual(dst, src); }); it('should encode/decode numbers', () => { @@ -130,6 +130,7 @@ describe('Script', function() { { const input = new Script([opcodes.OP_1, opcodes.OP_2]); + const output = new Script([ opcodes.OP_9, opcodes.OP_EQUAL, @@ -151,6 +152,7 @@ describe('Script', function() { { const input = new Script([opcodes.OP_1, opcodes.OP_2]); + const output = new Script([ opcodes.OP_2, opcodes.OP_EQUAL, @@ -170,6 +172,7 @@ describe('Script', function() { { const input = new Script([opcodes.OP_1, opcodes.OP_2]); + const output = new Script([ opcodes.OP_9, opcodes.OP_EQUAL, @@ -189,6 +192,7 @@ describe('Script', function() { { const input = new Script([opcodes.OP_1, opcodes.OP_2]); + const output = new Script([ opcodes.OP_9, opcodes.OP_EQUAL, diff --git a/test/scrypt-test.js b/test/scrypt-test.js index ab5c9210..93f13e1f 100644 --- a/test/scrypt-test.js +++ b/test/scrypt-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const scrypt = require('../lib/crypto/scrypt'); describe('Scrypt', function() { diff --git a/test/siphash-test.js b/test/siphash-test.js index 343626e4..76aa8932 100644 --- a/test/siphash-test.js +++ b/test/siphash-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const siphash = require('../lib/crypto/siphash'); const siphash256 = siphash.siphash256; diff --git a/test/tx-test.js b/test/tx-test.js index 50508cf8..40ebe0e9 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const util = require('../lib/utils/util'); const encoding = require('../lib/utils/encoding'); const random = require('../lib/crypto/random'); @@ -235,7 +235,7 @@ describe('TX', function() { tx.refresh(); const raw2 = tx.toRaw(); - assert.deepStrictEqual(raw1, raw2); + assert.bufferEqual(raw1, raw2); const tx2 = TX.fromRaw(raw2); clearCache(tx2, noCache); diff --git a/test/util/assert.js b/test/util/assert.js new file mode 100644 index 00000000..6beb7a1d --- /dev/null +++ b/test/util/assert.js @@ -0,0 +1,76 @@ +'use strict'; + +const _assert = require('assert'); +const util = require('util'); + +const assert = function assert(ok, message) { + return _assert(ok, message); +}; + +Object.setPrototypeOf(assert, _assert); + +assert.isBuffer = function isBuffer(value, message) { + if (!Buffer.isBuffer(value)) { + throw new assert.AssertionError({ + mesage: message, + actual: typeOf(value), + expected: 'buffer', + operator: '===', + stackStartFunction: isBuffer + }); + } +}; + +assert.bufferEqual = function bufferEqual(actual, expected, message) { + assert.isBuffer(actual, message); + assert.isBuffer(expected, message); + + if (!actual.equals(expected)) { + throw new assert.AssertionError({ + mesage: message, + actual: actual.toString('hex'), + expected: expected.toString('hex'), + operator: '===', + stackStartFunction: bufferEqual + }); + } +}; + +assert.notBufferEqual = function notBufferEqual(actual, expected, message) { + assert.isBuffer(actual, message); + assert.isBuffer(expected, message); + + if (actual.equals(expected)) { + throw new assert.AssertionError({ + mesage: message, + actual: actual.toString('hex'), + expected: expected.toString('hex'), + operator: '!==', + stackStartFunction: notBufferEqual + }); + } +}; + +function typeOf(value) { + if (value === null) + return 'null'; + + if (util.isDate(value)) + return 'date'; + + if (util.isRegExp(value)) + return 'regexp'; + + if (util.isError(value)) + return 'error'; + + if (Array.isArray(value)) + return 'array'; + + if (Buffer.isBuffer(value)) + return 'buffer'; + + return typeof value; +} + +module.exports = assert; diff --git a/test/utils-test.js b/test/utils-test.js index 6f5ecdbb..b6b26ffc 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const BN = require('../lib/crypto/bn'); const base58 = require('../lib/utils/base58'); const encoding = require('../lib/utils/encoding'); @@ -39,12 +39,12 @@ describe('Utils', function() { const str = base58.encode(buf); assert.strictEqual(str, '1116h8cQN'); - assert.deepStrictEqual(base58.decode(str), buf); + assert.bufferEqual(base58.decode(str), buf); for (const [hex, b58] of base58Tests) { const data = Buffer.from(hex, 'hex'); assert.strictEqual(base58.encode(data), b58); - assert.deepStrictEqual(base58.decode(b58), data); + assert.bufferEqual(base58.decode(b58), data); } }); @@ -212,12 +212,14 @@ describe('Utils', function() { it(`should write+read a ${bits} bit unsigned int`, () => { const buf1 = Buffer.allocUnsafe(8); const buf2 = Buffer.allocUnsafe(8); + encoding.writeU64BN(buf1, num, 0); encoding.writeU64(buf2, num.toNumber(), 0); - assert.deepStrictEqual(buf1, buf2); + assert.bufferEqual(buf1, buf2); const n1 = encoding.readU64BN(buf1, 0); const n2 = encoding.readU64(buf2, 0); + assert.strictEqual(n1.toNumber(), n2); }); } @@ -229,23 +231,27 @@ describe('Utils', function() { it(`should write+read a ${bits} bit ${sign} int`, () => { const buf1 = Buffer.allocUnsafe(8); const buf2 = Buffer.allocUnsafe(8); + encoding.writeI64BN(buf1, num, 0); encoding.writeI64(buf2, num.toNumber(), 0); - assert.deepStrictEqual(buf1, buf2); + assert.bufferEqual(buf1, buf2); const n1 = encoding.readI64BN(buf1, 0); const n2 = encoding.readI64(buf2, 0); + assert.strictEqual(n1.toNumber(), n2); }); it(`should write+read a ${bits} bit ${sign} int as unsigned`, () => { const buf1 = Buffer.allocUnsafe(8); const buf2 = Buffer.allocUnsafe(8); + encoding.writeU64BN(buf1, num, 0); encoding.writeU64(buf2, num.toNumber(), 0); - assert.deepStrictEqual(buf1, buf2); + assert.bufferEqual(buf1, buf2); const n1 = encoding.readU64BN(buf1, 0); + if (num.isNeg()) { assert.throws(() => encoding.readU64(buf2, 0)); } else { @@ -256,7 +262,10 @@ describe('Utils', function() { } it('should validate integers 0 and 1 as booleans', () => { - const validator = new Validator({shouldBeTrue: 1, shouldBeFalse: 0}); + const validator = new Validator({ + shouldBeTrue: 1, + shouldBeFalse: 0 + }); assert.strictEqual(validator.bool('shouldBeTrue'), true); assert.strictEqual(validator.bool('shouldBeFalse'), false); }); diff --git a/test/wallet-test.js b/test/wallet-test.js index 9dfe2294..b05b3533 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -3,7 +3,7 @@ 'use strict'; -const assert = require('assert'); +const assert = require('./util/assert'); const consensus = require('../lib/protocol/consensus'); const util = require('../lib/utils/util'); const encoding = require('../lib/utils/encoding');