test: add better assertion methods.

This commit is contained in:
Christopher Jeffrey 2017-08-10 11:17:10 -07:00
parent acfeede8e2
commit 4e2af473ab
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
27 changed files with 174 additions and 85 deletions

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const aes = require('../lib/crypto/aes'); const aes = require('../lib/crypto/aes');
describe('AES', function() { describe('AES', function() {
@ -24,10 +24,10 @@ describe('AES', function() {
'hex'); 'hex');
const ciphertext = aes.encipher(data, key, iv); const ciphertext = aes.encipher(data, key, iv);
assert.deepStrictEqual(ciphertext, expected); assert.bufferEqual(ciphertext, expected);
const plaintext = aes.decipher(ciphertext, key, iv); const plaintext = aes.decipher(ciphertext, key, iv);
assert.deepStrictEqual(plaintext, data); assert.bufferEqual(plaintext, data);
}); });
it('should encrypt and decrypt with uneven blocks', () => { it('should encrypt and decrypt with uneven blocks', () => {
@ -41,9 +41,9 @@ describe('AES', function() {
'hex'); 'hex');
const ciphertext = aes.encipher(data, key, iv); const ciphertext = aes.encipher(data, key, iv);
assert.deepStrictEqual(ciphertext, expected); assert.bufferEqual(ciphertext, expected);
const plaintext = aes.decipher(ciphertext, key, iv); const plaintext = aes.decipher(ciphertext, key, iv);
assert.deepStrictEqual(plaintext, data); assert.bufferEqual(plaintext, data);
}); });
}); });

View File

@ -26,7 +26,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const bech32 = require('../lib/utils/bech32'); const bech32 = require('../lib/utils/bech32');
const Address = require('../lib/primitives/address'); const Address = require('../lib/primitives/address');

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const secp256k1 = require('../lib/crypto/secp256k1'); const secp256k1 = require('../lib/crypto/secp256k1');
const BIP150 = require('../lib/net/bip150'); const BIP150 = require('../lib/net/bip150');
const BIP151 = require('../lib/net/bip151'); const BIP151 = require('../lib/net/bip151');

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const BIP151 = require('../lib/net/bip151'); const BIP151 = require('../lib/net/bip151');
const client = new BIP151(); const client = new BIP151();

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const util = require('../lib/utils/util'); const util = require('../lib/utils/util');
const bip70 = require('../lib/bip70'); const bip70 = require('../lib/bip70');
const Address = require('../lib/primitives/address'); const Address = require('../lib/primitives/address');
@ -38,7 +38,7 @@ function testRequest(data) {
assert(req.paymentDetails.memo.length !== 0); assert(req.paymentDetails.memo.length !== 0);
assert(req.paymentDetails.paymentUrl.length !== 0); assert(req.paymentDetails.paymentUrl.length !== 0);
assert.deepStrictEqual(req.toRaw(), data); assert.bufferEqual(req.toRaw(), data);
assert(req.verify()); assert(req.verify());
} }
@ -153,7 +153,7 @@ describe('BIP70', function() {
assert.strictEqual(ack.memo.length, 95); assert.strictEqual(ack.memo.length, 95);
assert.strictEqual(ack.memo, 'Transaction received by BitPay.' assert.strictEqual(ack.memo, 'Transaction received by BitPay.'
+ ' Invoice will be marked as paid if the transaction is confirmed.'); + ' 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', () => { it('should create a payment request, sign, and verify', () => {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const fs = require('../lib/utils/fs'); const fs = require('../lib/utils/fs');
const common = require('./util/common'); const common = require('./util/common');
const Bloom = require('../lib/utils/bloom'); const Bloom = require('../lib/utils/bloom');
@ -68,8 +68,8 @@ describe('Block', function() {
it('should decode/encode merkle block', () => { it('should decode/encode merkle block', () => {
const merkle = MerkleBlock.fromRaw(merkle300025); const merkle = MerkleBlock.fromRaw(merkle300025);
merkle.refresh(); merkle.refresh();
assert.deepStrictEqual(merkle.toRaw(), merkle300025); assert.bufferEqual(merkle.toRaw(), merkle300025);
assert.deepStrictEqual(merkle300025, mblock.toRaw()); assert.bufferEqual(mblock.toRaw(), merkle300025);
}); });
it('should be verify merkle block', () => { it('should be verify merkle block', () => {
@ -80,7 +80,7 @@ describe('Block', function() {
it('should be encoded/decoded and still verify', () => { it('should be encoded/decoded and still verify', () => {
const raw = mblock.toRaw(); const raw = mblock.toRaw();
const merkle = MerkleBlock.fromRaw(raw); const merkle = MerkleBlock.fromRaw(raw);
assert.deepStrictEqual(merkle.toRaw(), raw); assert.bufferEqual(merkle.toRaw(), raw);
assert(merkle.verify()); assert(merkle.verify());
}); });
@ -131,7 +131,7 @@ describe('Block', function() {
const merkle = MerkleBlock.fromBlock(block, filter); const merkle = MerkleBlock.fromBlock(block, filter);
assert(merkle.verifyBody()); assert(merkle.verifyBody());
assert.deepStrictEqual(merkle.toRaw(), mblock.toRaw()); assert.bufferEqual(merkle.toRaw(), mblock.toRaw());
}); });
it('should verify a historical block', () => { it('should verify a historical block', () => {
@ -228,8 +228,8 @@ describe('Block', function() {
assert(cblock1.init()); assert(cblock1.init());
assert.deepStrictEqual(cblock1.toRaw(), compact426884); assert.bufferEqual(cblock1.toRaw(), compact426884);
assert.deepStrictEqual(cblock2.toRaw(), compact426884); assert.bufferEqual(cblock2.toRaw(), compact426884);
const map = new Map(); const map = new Map();
@ -244,7 +244,7 @@ describe('Block', function() {
for (const tx of cblock1.available) for (const tx of cblock1.available)
assert(tx); assert(tx);
assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw());
}); });
it('should handle half-full compact block', () => { it('should handle half-full compact block', () => {
@ -254,8 +254,8 @@ describe('Block', function() {
assert(cblock1.init()); assert(cblock1.init());
assert.deepStrictEqual(cblock1.toRaw(), compact426884); assert.bufferEqual(cblock1.toRaw(), compact426884);
assert.deepStrictEqual(cblock2.toRaw(), compact426884); assert.bufferEqual(cblock2.toRaw(), compact426884);
const map = new Map(); const map = new Map();
@ -282,7 +282,7 @@ describe('Block', function() {
for (const tx of cblock1.available) for (const tx of cblock1.available)
assert(tx); assert(tx);
assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw());
}); });
it('should handle compact block', () => { it('should handle compact block', () => {
@ -292,8 +292,8 @@ describe('Block', function() {
assert(cblock1.init()); assert(cblock1.init());
assert.deepStrictEqual(cblock1.toRaw(), compact898352); assert.bufferEqual(cblock1.toRaw(), compact898352);
assert.deepStrictEqual(cblock2.toRaw(), compact898352); assert.bufferEqual(cblock2.toRaw(), compact898352);
assert.strictEqual(cblock1.sid(block.txs[1].hash()), 125673511480291); assert.strictEqual(cblock1.sid(block.txs[1].hash()), 125673511480291);
@ -310,7 +310,7 @@ describe('Block', function() {
for (const tx of cblock1.available) for (const tx of cblock1.available)
assert(tx); assert(tx);
assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw());
}); });
it('should handle half-full compact block', () => { it('should handle half-full compact block', () => {
@ -320,8 +320,8 @@ describe('Block', function() {
assert(cblock1.init()); assert(cblock1.init());
assert.deepStrictEqual(cblock1.toRaw(), compact898352); assert.bufferEqual(cblock1.toRaw(), compact898352);
assert.deepStrictEqual(cblock2.toRaw(), compact898352); assert.bufferEqual(cblock2.toRaw(), compact898352);
assert.strictEqual(cblock1.sid(block.txs[1].hash()), 125673511480291); assert.strictEqual(cblock1.sid(block.txs[1].hash()), 125673511480291);
@ -352,7 +352,7 @@ describe('Block', function() {
for (const tx of cblock1.available) for (const tx of cblock1.available)
assert(tx); assert(tx);
assert.deepStrictEqual(cblock1.toBlock().toRaw(), block.toRaw()); assert.bufferEqual(cblock1.toBlock().toRaw(), block.toRaw());
}); });
it('should count sigops for block 928927 (testnet)', () => { it('should count sigops for block 928927 (testnet)', () => {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const Bloom = require('../lib/utils/bloom'); const Bloom = require('../lib/utils/bloom');
const RollingFilter = require('../lib/utils/rollingfilter'); const RollingFilter = require('../lib/utils/rollingfilter');
const murmur3 = require('../lib/utils/murmur3'); const murmur3 = require('../lib/utils/murmur3');
@ -75,7 +75,7 @@ describe('Bloom', function() {
'hex'); 'hex');
filter.add(item1, 'hex'); filter.add(item1, 'hex');
filter.add(item2, 'hex'); filter.add(item2, 'hex');
assert.deepStrictEqual(filter.filter, filterRaw); assert.bufferEqual(filter.filter, filterRaw);
}); });
it('should handle 1m ops with regular filter', () => { it('should handle 1m ops with regular filter', () => {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const ChaCha20 = require('../lib/crypto/chacha20'); const ChaCha20 = require('../lib/crypto/chacha20');
const Poly1305 = require('../lib/crypto/poly1305'); const Poly1305 = require('../lib/crypto/poly1305');
const AEAD = require('../lib/crypto/aead'); const AEAD = require('../lib/crypto/aead');
@ -19,12 +19,12 @@ function testChaCha(options) {
ctx1.init(key, nonce, counter); ctx1.init(key, nonce, counter);
const plainenc = Buffer.from(plain); const plainenc = Buffer.from(plain);
ctx1.encrypt(plainenc); ctx1.encrypt(plainenc);
assert.deepStrictEqual(plainenc, ciphertext); assert.bufferEqual(plainenc, ciphertext);
const ctx2 = new ChaCha20(); const ctx2 = new ChaCha20();
ctx2.init(key, nonce, counter); ctx2.init(key, nonce, counter);
ctx2.encrypt(ciphertext); ctx2.encrypt(ciphertext);
assert.deepStrictEqual(plain, ciphertext); assert.bufferEqual(plain, ciphertext);
} }
function testAEAD(options) { function testAEAD(options) {
@ -39,21 +39,21 @@ function testAEAD(options) {
const ctx1 = new AEAD(); const ctx1 = new AEAD();
ctx1.init(key, nonce); ctx1.init(key, nonce);
assert.strictEqual(ctx1.chacha20.getCounter(), 1); assert.strictEqual(ctx1.chacha20.getCounter(), 1);
assert.deepStrictEqual(ctx1.polyKey, pk); assert.bufferEqual(ctx1.polyKey, pk);
ctx1.aad(aad); ctx1.aad(aad);
const plainenc = Buffer.from(plain); const plainenc = Buffer.from(plain);
ctx1.encrypt(plainenc); ctx1.encrypt(plainenc);
assert.deepStrictEqual(plainenc, ciphertext); assert.bufferEqual(plainenc, ciphertext);
assert.deepStrictEqual(ctx1.finish(), tag); assert.bufferEqual(ctx1.finish(), tag);
const ctx2 = new AEAD(); const ctx2 = new AEAD();
ctx2.init(key, nonce); ctx2.init(key, nonce);
assert.strictEqual(ctx2.chacha20.getCounter(), 1); assert.strictEqual(ctx2.chacha20.getCounter(), 1);
assert.deepStrictEqual(ctx2.polyKey, pk); assert.bufferEqual(ctx2.polyKey, pk);
ctx2.aad(aad); ctx2.aad(aad);
ctx2.decrypt(ciphertext); ctx2.decrypt(ciphertext);
assert.deepStrictEqual(ciphertext, plain); assert.bufferEqual(ciphertext, plain);
assert.deepStrictEqual(ctx2.finish(), tag); assert.bufferEqual(ctx2.finish(), tag);
} }
describe('ChaCha20 / Poly1305 / AEAD', function() { describe('ChaCha20 / Poly1305 / AEAD', function() {
@ -160,7 +160,7 @@ describe('ChaCha20 / Poly1305 / AEAD', function() {
const mac = Poly1305.auth(msg, key); const mac = Poly1305.auth(msg, key);
assert(Poly1305.verify(mac, expected)); assert(Poly1305.verify(mac, expected));
assert.deepStrictEqual(mac, expected); assert.bufferEqual(mac, expected);
}); });
it('should perform poly1305', () => { it('should perform poly1305', () => {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const BN = require('../lib/crypto/bn'); const BN = require('../lib/crypto/bn');
const consensus = require('../lib/protocol/consensus'); const consensus = require('../lib/protocol/consensus');
const encoding = require('../lib/utils/encoding'); const encoding = require('../lib/utils/encoding');
@ -316,7 +316,7 @@ describe('Chain', function() {
const coin = await chain.db.getCoin(tx.hash('hex'), 2); 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 () => { it('should have correct wallet balance', async () => {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const Output = require('../lib/primitives/output'); const Output = require('../lib/primitives/output');
const Input = require('../lib/primitives/input'); const Input = require('../lib/primitives/input');
const Outpoint = require('../lib/primitives/outpoint'); const Outpoint = require('../lib/primitives/outpoint');
@ -27,7 +27,7 @@ function deepCoinsEqual(a, b) {
assert.strictEqual(a.version, b.version); assert.strictEqual(a.version, b.version);
assert.strictEqual(a.height, b.height); assert.strictEqual(a.height, b.height);
assert.strictEqual(a.coinbase, b.coinbase); assert.strictEqual(a.coinbase, b.coinbase);
assert.deepStrictEqual(a.raw, b.raw); assert.bufferEqual(a.raw, b.raw);
} }
describe('Coins', function() { describe('Coins', function() {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const fs = require('../lib/utils/fs'); const fs = require('../lib/utils/fs');
const GCSFilter = require('../lib/utils/gcs'); const GCSFilter = require('../lib/utils/gcs');
const random = require('../lib/crypto/random'); const random = require('../lib/crypto/random');
@ -104,16 +104,16 @@ describe('GCS', function() {
assert.strictEqual(filter1.n, contents1.length); assert.strictEqual(filter1.n, contents1.length);
assert.strictEqual(filter1.p, filter2.p); assert.strictEqual(filter1.p, filter2.p);
assert.strictEqual(filter1.n, filter2.n); 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.p, filter3.p);
assert.strictEqual(filter1.n, filter3.n); 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.p, filter4.p);
assert.strictEqual(filter1.n, filter4.n); 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.p, filter5.p);
assert.strictEqual(filter1.n, filter5.n); assert.strictEqual(filter1.n, filter5.n);
assert.deepStrictEqual(filter1.data, filter5.data); assert.bufferEqual(filter1.data, filter5.data);
}); });
it('should test GCS filter match', () => { it('should test GCS filter match', () => {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const HD = require('../lib/hd'); const HD = require('../lib/hd');
const base58 = require('../lib/utils/base58'); const base58 = require('../lib/utils/base58');
const pbkdf2 = require('../lib/crypto/pbkdf2'); const pbkdf2 = require('../lib/crypto/pbkdf2');

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const hkdf = require('../lib/crypto/hkdf'); const hkdf = require('../lib/crypto/hkdf');
describe('HKDF', function() { describe('HKDF', function() {
@ -28,8 +28,8 @@ describe('HKDF', function() {
const prk = hkdf.extract(ikm, salt, alg); const prk = hkdf.extract(ikm, salt, alg);
const okm = hkdf.expand(prk, info, len, alg); const okm = hkdf.expand(prk, info, len, alg);
assert.deepStrictEqual(prk, prkE); assert.bufferEqual(prk, prkE);
assert.deepStrictEqual(okm, okmE); assert.bufferEqual(okm, okmE);
}); });
it('should do proper hkdf (2)', () => { it('should do proper hkdf (2)', () => {
@ -78,7 +78,7 @@ describe('HKDF', function() {
const prk = hkdf.extract(ikm, salt, alg); const prk = hkdf.extract(ikm, salt, alg);
const okm = hkdf.expand(prk, info, len, alg); const okm = hkdf.expand(prk, info, len, alg);
assert.deepStrictEqual(prk, prkE); assert.bufferEqual(prk, prkE);
assert.deepStrictEqual(okm, okmE); assert.bufferEqual(okm, okmE);
}); });
}); });

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const consensus = require('../lib/protocol/consensus'); const consensus = require('../lib/protocol/consensus');
const encoding = require('../lib/utils/encoding'); const encoding = require('../lib/utils/encoding');
const co = require('../lib/utils/co'); const co = require('../lib/utils/co');

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const KeyRing = require('../lib/primitives/keyring'); const KeyRing = require('../lib/primitives/keyring');
describe('KeyRing', function() { describe('KeyRing', function() {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const encoding = require('../lib/utils/encoding'); const encoding = require('../lib/utils/encoding');
const random = require('../lib/crypto/random'); const random = require('../lib/crypto/random');
const MempoolEntry = require('../lib/mempool/mempoolentry'); const MempoolEntry = require('../lib/mempool/mempoolentry');

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const Mnemonic = require('../lib/hd/mnemonic'); const Mnemonic = require('../lib/hd/mnemonic');
const HDPrivateKey = require('../lib/hd/private'); const HDPrivateKey = require('../lib/hd/private');
@ -32,8 +32,8 @@ describe('Mnemonic', function() {
}); });
assert.strictEqual(mnemonic.getPhrase(), phrase); assert.strictEqual(mnemonic.getPhrase(), phrase);
assert.deepStrictEqual(mnemonic.getEntropy(), entropy); assert.bufferEqual(mnemonic.getEntropy(), entropy);
assert.deepStrictEqual(mnemonic.toSeed(), seed); assert.bufferEqual(mnemonic.toSeed(), seed);
const key = HDPrivateKey.fromMnemonic(mnemonic); const key = HDPrivateKey.fromMnemonic(mnemonic);
assert.strictEqual(key.toBase58(), xpriv); assert.strictEqual(key.toBase58(), xpriv);
@ -47,8 +47,8 @@ describe('Mnemonic', function() {
}); });
assert.strictEqual(mnemonic.getPhrase(), phrase); assert.strictEqual(mnemonic.getPhrase(), phrase);
assert.deepStrictEqual(mnemonic.getEntropy(), entropy); assert.bufferEqual(mnemonic.getEntropy(), entropy);
assert.deepStrictEqual(mnemonic.toSeed(), seed); assert.bufferEqual(mnemonic.toSeed(), seed);
const key = HDPrivateKey.fromMnemonic(mnemonic); const key = HDPrivateKey.fromMnemonic(mnemonic);
assert.strictEqual(key.toBase58(), xpriv); assert.strictEqual(key.toBase58(), xpriv);
@ -61,9 +61,9 @@ describe('Mnemonic', function() {
it('should verify phrase', () => { it('should verify phrase', () => {
const m1 = new Mnemonic(); const m1 = new Mnemonic();
const m2 = Mnemonic.fromPhrase(m1.getPhrase()); 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.bits, m1.bits);
assert.strictEqual(m2.language, m1.language); assert.strictEqual(m2.language, m1.language);
assert.deepStrictEqual(m2.toSeed(), m1.toSeed()); assert.bufferEqual(m2.toSeed(), m1.toSeed());
}); });
}); });

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const BN = require('../lib/crypto/bn'); const BN = require('../lib/crypto/bn');
const consensus = require('../lib/protocol/consensus'); const consensus = require('../lib/protocol/consensus');
const co = require('../lib/utils/co'); const co = require('../lib/utils/co');
@ -248,7 +248,7 @@ describe('Node', function() {
const coin = await chain.db.getCoin(tx.hash('hex'), 1); 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 () => { it('should get balance', async () => {

View File

@ -4,7 +4,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const Network = require('../lib/protocol/network'); const Network = require('../lib/protocol/network');
const util = require('../lib/utils/util'); const util = require('../lib/utils/util');
const NetAddress = require('../lib/primitives/netaddress'); const NetAddress = require('../lib/primitives/netaddress');
@ -117,6 +117,6 @@ describe('Protocol', function() {
const tx = TX.fromRaw(raw); const tx = TX.fromRaw(raw);
tx.refresh(); tx.refresh();
assert.deepStrictEqual(tx.toRaw(), tx8.tx.toRaw()); assert.bufferEqual(tx.toRaw(), tx8.tx.toRaw());
}); });
}); });

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const secp256k1 = require('../lib/crypto/secp256k1'); const secp256k1 = require('../lib/crypto/secp256k1');
const digest = require('../lib/crypto/digest'); const digest = require('../lib/crypto/digest');
const schnorr = require('../lib/crypto/schnorr'); const schnorr = require('../lib/crypto/schnorr');
@ -15,6 +15,6 @@ describe('Schnorr', function() {
const msg = digest.hash256(Buffer.from('foo', 'ascii')); const msg = digest.hash256(Buffer.from('foo', 'ascii'));
const sig = schnorr.sign(msg, key); const sig = schnorr.sign(msg, key);
assert(schnorr.verify(msg, sig, pub)); assert(schnorr.verify(msg, sig, pub));
assert.deepStrictEqual(schnorr.recover(sig, msg), pub); assert.bufferEqual(schnorr.recover(sig, msg), pub);
}); });
}); });

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const Script = require('../lib/script/script'); const Script = require('../lib/script/script');
const Witness = require('../lib/script/witness'); const Witness = require('../lib/script/witness');
const Stack = require('../lib/script/stack'); const Stack = require('../lib/script/stack');
@ -82,7 +82,7 @@ describe('Script', function() {
assert.strictEqual(decoded.code[2].value, opcodes.OP_CHECKSIG); assert.strictEqual(decoded.code[2].value, opcodes.OP_CHECKSIG);
const dst = decoded.toRaw(); const dst = decoded.toRaw();
assert.deepStrictEqual(dst, src); assert.bufferEqual(dst, src);
}); });
it('should encode/decode numbers', () => { it('should encode/decode numbers', () => {
@ -130,6 +130,7 @@ describe('Script', function() {
{ {
const input = new Script([opcodes.OP_1, opcodes.OP_2]); const input = new Script([opcodes.OP_1, opcodes.OP_2]);
const output = new Script([ const output = new Script([
opcodes.OP_9, opcodes.OP_9,
opcodes.OP_EQUAL, opcodes.OP_EQUAL,
@ -151,6 +152,7 @@ describe('Script', function() {
{ {
const input = new Script([opcodes.OP_1, opcodes.OP_2]); const input = new Script([opcodes.OP_1, opcodes.OP_2]);
const output = new Script([ const output = new Script([
opcodes.OP_2, opcodes.OP_2,
opcodes.OP_EQUAL, opcodes.OP_EQUAL,
@ -170,6 +172,7 @@ describe('Script', function() {
{ {
const input = new Script([opcodes.OP_1, opcodes.OP_2]); const input = new Script([opcodes.OP_1, opcodes.OP_2]);
const output = new Script([ const output = new Script([
opcodes.OP_9, opcodes.OP_9,
opcodes.OP_EQUAL, opcodes.OP_EQUAL,
@ -189,6 +192,7 @@ describe('Script', function() {
{ {
const input = new Script([opcodes.OP_1, opcodes.OP_2]); const input = new Script([opcodes.OP_1, opcodes.OP_2]);
const output = new Script([ const output = new Script([
opcodes.OP_9, opcodes.OP_9,
opcodes.OP_EQUAL, opcodes.OP_EQUAL,

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const scrypt = require('../lib/crypto/scrypt'); const scrypt = require('../lib/crypto/scrypt');
describe('Scrypt', function() { describe('Scrypt', function() {

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const siphash = require('../lib/crypto/siphash'); const siphash = require('../lib/crypto/siphash');
const siphash256 = siphash.siphash256; const siphash256 = siphash.siphash256;

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const util = require('../lib/utils/util'); const util = require('../lib/utils/util');
const encoding = require('../lib/utils/encoding'); const encoding = require('../lib/utils/encoding');
const random = require('../lib/crypto/random'); const random = require('../lib/crypto/random');
@ -235,7 +235,7 @@ describe('TX', function() {
tx.refresh(); tx.refresh();
const raw2 = tx.toRaw(); const raw2 = tx.toRaw();
assert.deepStrictEqual(raw1, raw2); assert.bufferEqual(raw1, raw2);
const tx2 = TX.fromRaw(raw2); const tx2 = TX.fromRaw(raw2);
clearCache(tx2, noCache); clearCache(tx2, noCache);

76
test/util/assert.js Normal file
View File

@ -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;

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const BN = require('../lib/crypto/bn'); const BN = require('../lib/crypto/bn');
const base58 = require('../lib/utils/base58'); const base58 = require('../lib/utils/base58');
const encoding = require('../lib/utils/encoding'); const encoding = require('../lib/utils/encoding');
@ -39,12 +39,12 @@ describe('Utils', function() {
const str = base58.encode(buf); const str = base58.encode(buf);
assert.strictEqual(str, '1116h8cQN'); assert.strictEqual(str, '1116h8cQN');
assert.deepStrictEqual(base58.decode(str), buf); assert.bufferEqual(base58.decode(str), buf);
for (const [hex, b58] of base58Tests) { for (const [hex, b58] of base58Tests) {
const data = Buffer.from(hex, 'hex'); const data = Buffer.from(hex, 'hex');
assert.strictEqual(base58.encode(data), b58); 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`, () => { it(`should write+read a ${bits} bit unsigned int`, () => {
const buf1 = Buffer.allocUnsafe(8); const buf1 = Buffer.allocUnsafe(8);
const buf2 = Buffer.allocUnsafe(8); const buf2 = Buffer.allocUnsafe(8);
encoding.writeU64BN(buf1, num, 0); encoding.writeU64BN(buf1, num, 0);
encoding.writeU64(buf2, num.toNumber(), 0); encoding.writeU64(buf2, num.toNumber(), 0);
assert.deepStrictEqual(buf1, buf2); assert.bufferEqual(buf1, buf2);
const n1 = encoding.readU64BN(buf1, 0); const n1 = encoding.readU64BN(buf1, 0);
const n2 = encoding.readU64(buf2, 0); const n2 = encoding.readU64(buf2, 0);
assert.strictEqual(n1.toNumber(), n2); assert.strictEqual(n1.toNumber(), n2);
}); });
} }
@ -229,23 +231,27 @@ describe('Utils', function() {
it(`should write+read a ${bits} bit ${sign} int`, () => { it(`should write+read a ${bits} bit ${sign} int`, () => {
const buf1 = Buffer.allocUnsafe(8); const buf1 = Buffer.allocUnsafe(8);
const buf2 = Buffer.allocUnsafe(8); const buf2 = Buffer.allocUnsafe(8);
encoding.writeI64BN(buf1, num, 0); encoding.writeI64BN(buf1, num, 0);
encoding.writeI64(buf2, num.toNumber(), 0); encoding.writeI64(buf2, num.toNumber(), 0);
assert.deepStrictEqual(buf1, buf2); assert.bufferEqual(buf1, buf2);
const n1 = encoding.readI64BN(buf1, 0); const n1 = encoding.readI64BN(buf1, 0);
const n2 = encoding.readI64(buf2, 0); const n2 = encoding.readI64(buf2, 0);
assert.strictEqual(n1.toNumber(), n2); assert.strictEqual(n1.toNumber(), n2);
}); });
it(`should write+read a ${bits} bit ${sign} int as unsigned`, () => { it(`should write+read a ${bits} bit ${sign} int as unsigned`, () => {
const buf1 = Buffer.allocUnsafe(8); const buf1 = Buffer.allocUnsafe(8);
const buf2 = Buffer.allocUnsafe(8); const buf2 = Buffer.allocUnsafe(8);
encoding.writeU64BN(buf1, num, 0); encoding.writeU64BN(buf1, num, 0);
encoding.writeU64(buf2, num.toNumber(), 0); encoding.writeU64(buf2, num.toNumber(), 0);
assert.deepStrictEqual(buf1, buf2); assert.bufferEqual(buf1, buf2);
const n1 = encoding.readU64BN(buf1, 0); const n1 = encoding.readU64BN(buf1, 0);
if (num.isNeg()) { if (num.isNeg()) {
assert.throws(() => encoding.readU64(buf2, 0)); assert.throws(() => encoding.readU64(buf2, 0));
} else { } else {
@ -256,7 +262,10 @@ describe('Utils', function() {
} }
it('should validate integers 0 and 1 as booleans', () => { 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('shouldBeTrue'), true);
assert.strictEqual(validator.bool('shouldBeFalse'), false); assert.strictEqual(validator.bool('shouldBeFalse'), false);
}); });

View File

@ -3,7 +3,7 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('./util/assert');
const consensus = require('../lib/protocol/consensus'); const consensus = require('../lib/protocol/consensus');
const util = require('../lib/utils/util'); const util = require('../lib/utils/util');
const encoding = require('../lib/utils/encoding'); const encoding = require('../lib/utils/encoding');