test: refactor assertions.

This commit is contained in:
Christopher Jeffrey 2017-08-11 18:25:54 -07:00
parent 28d2f1478e
commit 3a7484d782
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
18 changed files with 182 additions and 93 deletions

View File

@ -6,13 +6,13 @@
const assert = require('./util/assert');
const aes = require('../lib/crypto/aes');
const key = Buffer.from(
'3a0c0bf669694ac7685e6806eeadee8e56c9b9bd22c3caa81c718ed4bbf809a1',
'hex');
const iv = Buffer.from('6dd26d9045b73c377a9ed2ffeca72ffd', 'hex');
describe('AES', function() {
const key = Buffer.from(
'3a0c0bf669694ac7685e6806eeadee8e56c9b9bd22c3caa81c718ed4bbf809a1',
'hex');
const iv = Buffer.from('6dd26d9045b73c377a9ed2ffeca72ffd', 'hex');
it('should encrypt and decrypt with 2 blocks', () => {
const data = Buffer.from(
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',

View File

@ -70,7 +70,7 @@ describe('BIP150', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -82,7 +82,7 @@ describe('BIP150', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -94,7 +94,7 @@ describe('BIP150', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -106,7 +106,7 @@ describe('BIP150', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -145,7 +145,7 @@ describe('BIP150', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -157,7 +157,7 @@ describe('BIP150', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -169,7 +169,7 @@ describe('BIP150', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -181,7 +181,7 @@ describe('BIP150', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -195,12 +195,12 @@ describe('BIP150', function() {
client.once('packet', (cmd, body) => {
cemitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.once('packet', (cmd, body) => {
semitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(spacket);
server.feed(cpacket);

View File

@ -45,7 +45,7 @@ describe('BIP151', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -57,7 +57,7 @@ describe('BIP151', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -69,7 +69,7 @@ describe('BIP151', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -81,7 +81,7 @@ describe('BIP151', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -120,7 +120,7 @@ describe('BIP151', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -132,7 +132,7 @@ describe('BIP151', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -144,7 +144,7 @@ describe('BIP151', function() {
server.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.feed(packet);
assert(emitted);
@ -156,7 +156,7 @@ describe('BIP151', function() {
client.once('packet', (cmd, body) => {
emitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(packet);
assert(emitted);
@ -170,12 +170,12 @@ describe('BIP151', function() {
client.once('packet', (cmd, body) => {
cemitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
server.once('packet', (cmd, body) => {
semitted = true;
assert.strictEqual(cmd, 'fake');
assert.strictEqual(body.toString('hex'), 'deadbeef');
assert.bufferEqual(body, payload());
});
client.feed(spacket);
server.feed(cpacket);

View File

@ -182,7 +182,8 @@ describe('BIP70', function() {
assert.deepStrictEqual(req.paymentDetails.getData('json'), {foo:'bar'});
assert.strictEqual(req.version, 25);
assert.strictEqual(req.paymentDetails.paymentUrl, 'http://bcoin.io/payment');
assert.strictEqual(req.paymentDetails.paymentUrl,
'http://bcoin.io/payment');
assert.strictEqual(req.paymentDetails.network, 'testnet');
assert(req.paymentDetails.time <= util.now());
assert.strictEqual(req.paymentDetails.expires,

View File

@ -39,10 +39,11 @@ const undo928828 = fs.readFileSync(`${__dirname}/data/undo928828.raw`);
const block1087400 = fs.readFileSync(`${__dirname}/data/block1087400.raw`);
const undo1087400 = fs.readFileSync(`${__dirname}/data/undo1087400.raw`);
describe('Block', function() {
const mblock = MerkleBlock.fromRaw(merkle300025);
const block = Block.fromRaw(block300025);
// Abstract
const mblock = MerkleBlock.fromRaw(merkle300025);
const block = Block.fromRaw(block300025);
describe('Block', function() {
this.timeout(10000);
it('should parse partial merkle tree', () => {

View File

@ -67,15 +67,18 @@ describe('Bloom', function() {
const item2 = '047b00000078da0dca3b0ec2300c00d0ab4466ed10'
+ 'e763272c6c9ca052972c69e3884a9022084215e2eef'
+ '0e6f781656b5d5a87231cd4349e534b6dea55ad4ff55e';
const filterRaw = Buffer.from(''
const expected = Buffer.from(''
+ '000000000000000000000000000000000000000000000000088004000000000000000'
+ '000000000200000000000000000000000000000000800000000000000000002000000'
+ '000000000000002000000000000000000000000000000000000000000040000200000'
+ '0000000001000000800000080000000',
'hex');
filter.add(item1, 'hex');
filter.add(item2, 'hex');
assert.bufferEqual(filter.filter, filterRaw);
assert.bufferEqual(filter.filter, expected);
});
it('should handle 1m ops with regular filter', () => {

View File

@ -173,7 +173,12 @@ describe('ChaCha20 / Poly1305 / AEAD', function() {
const tag = Buffer.from('a8061dc1305136c6c22b8baf0c0127a9', 'hex');
const mac = Poly1305.auth(msg, key);
assert(Poly1305.verify(mac, tag));
mac[0] = 0;
assert(!Poly1305.verify(mac, tag));
});
it('should create an AEAD and encrypt', () => {

View File

@ -44,13 +44,12 @@ describe('Coins', function() {
const entry = coins.get(0);
assert(entry);
assert(!entry.spent);
assert.strictEqual(entry.version, 1);
assert.strictEqual(entry.height, 1);
assert.strictEqual(entry.coinbase, false);
assert.strictEqual(entry.raw, null);
assert(entry.output instanceof Output);
assert.instanceOf(entry.output, Output);
assert.strictEqual(entry.spent, false);
const output = view.getOutputFor(input);

View File

@ -142,12 +142,12 @@ describe('GCS', function() {
match = filter2.matchAny(key, contents2);
assert(!match);
const c = contents2.slice();
c.push(Buffer.from('Nate'));
const contents = contents2.slice();
contents.push(Buffer.from('Nate'));
match = filter1.matchAny(key, c);
match = filter1.matchAny(key, contents);
assert(match);
match = filter2.matchAny(key, c);
match = filter2.matchAny(key, contents);
assert(match);
});

View File

@ -14,13 +14,9 @@ const vector2 = vectors.vector2;
let master = null;
let child = null;
function unbase58(data) {
return base58.decode(data).toString('hex');
}
function base58Equal(a, b) {
assert.strictEqual(a, b);
assert.strictEqual(unbase58(a), unbase58(b));
assert.bufferEqual(base58.decode(a), base58.decode(b));
}
describe('HD', function() {
@ -93,16 +89,18 @@ describe('HD', function() {
HD.PublicKey.fromBase58(master.toPublic().toBase58());
});
it('should deserialize and reserialize', () => {
it('should deserialize and reserialize json', () => {
const key = HD.generate();
assert.strictEqual(HD.fromJSON(key.toJSON()).toBase58(), key.toBase58());
const json = key.toJSON();
base58Equal(HD.fromJSON(json).toBase58(), key.toBase58());
});
for (const vector of [vector1, vector2]) {
let master = null;
it('should create from a seed', () => {
const key = HD.PrivateKey.fromSeed(Buffer.from(vector.seed, 'hex'));
const seed = Buffer.from(vector.seed, 'hex');
const key = HD.PrivateKey.fromSeed(seed);
const pub = key.toPublic();
base58Equal(key.toBase58(), vector.m.prv);
@ -112,10 +110,10 @@ describe('HD', function() {
});
for (const path of Object.keys(vector)) {
const kp = vector[path];
if (path === 'seed' || path === 'm')
return;
continue;
const kp = vector[path];
it(`should derive ${path} from master`, () => {
const key = master.derivePath(path);

View File

@ -51,17 +51,19 @@ describe('HTTP', function() {
const info = await wallet.client.getInfo();
assert.strictEqual(info.network, node.network.type);
assert.strictEqual(info.version, pkg.version);
assert.typeOf(info.pool, 'object');
assert.strictEqual(info.pool.agent, node.pool.options.agent);
assert.strictEqual(typeof info.chain, 'object');
assert.typeOf(info.chain, 'object');
assert.strictEqual(info.chain.height, 0);
});
it('should get wallet info', async () => {
const info = await wallet.getInfo();
assert.strictEqual(info.id, 'test');
addr = info.account.receiveAddress;
assert.strictEqual(typeof addr, 'string');
addr = Address.fromString(addr);
assert.typeOf(info.account, 'object');
const str = info.account.receiveAddress;
assert.typeOf(str, 'string');
addr = Address.fromString(str);
});
it('should fill with funds', async () => {

View File

@ -471,10 +471,11 @@ describe('Node', function() {
id: '1'
}, {});
assert.strictEqual(typeof json.result.curtime, 'number');
assert.strictEqual(typeof json.result.mintime, 'number');
assert.strictEqual(typeof json.result.maxtime, 'number');
assert.strictEqual(typeof json.result.expires, 'number');
assert.typeOf(json.result, 'object');
assert.typeOf(json.result.curtime, 'number');
assert.typeOf(json.result.mintime, 'number');
assert.typeOf(json.result.maxtime, 'number');
assert.typeOf(json.result.expires, 'number');
assert.deepStrictEqual(json, {
result: {
@ -584,7 +585,7 @@ describe('Node', function() {
await wallet.db.addTX(tx);
const missing = await node.mempool.addTX(tx);
assert(!missing || missing.length === 0);
assert(!missing);
assert.strictEqual(node.mempool.map.size, 1);
@ -609,7 +610,7 @@ describe('Node', function() {
await wallet.db.addTX(tx);
const missing = await node.mempool.addTX(tx);
assert(!missing || missing.length === 0);
assert(!missing);
assert.strictEqual(node.mempool.map.size, 2);

View File

@ -97,15 +97,15 @@ describe('Protocol', function() {
];
packetTest('addr', new packets.AddrPacket(hosts), (payload) => {
assert.strictEqual(typeof payload.items.length, 'number');
assert.typeOf(payload.items, 'array');
assert.strictEqual(payload.items.length, 2);
assert.strictEqual(typeof payload.items[0].time, 'number');
assert.typeOf(payload.items[0].time, 'number');
assert.strictEqual(payload.items[0].services, 1);
assert.strictEqual(payload.items[0].host, hosts[0].host);
assert.strictEqual(payload.items[0].port, hosts[0].port);
assert.strictEqual(typeof payload.items[1].time, 'number');
assert.typeOf(payload.items[1].time, 'number');
assert.strictEqual(payload.items[1].services, 1);
assert.strictEqual(payload.items[1].host, hosts[1].host);
assert.strictEqual(payload.items[1].port, hosts[1].port);

View File

@ -14,7 +14,7 @@ describe('Schnorr', function() {
const pub = secp256k1.publicKeyCreate(key, true);
const msg = digest.hash256(Buffer.from('foo', 'ascii'));
const sig = schnorr.sign(msg, key);
assert(schnorr.verify(msg, sig, pub));
assert.strictEqual(schnorr.verify(msg, sig, pub), true);
assert.bufferEqual(schnorr.recover(sig, msg), pub);
});
});

View File

@ -345,7 +345,7 @@ describe('Script', function() {
}
if (expected !== 'OK') {
assert(err);
assert.typeOf(err, 'error');
assert.strictEqual(err.code, expected);
return;
}

View File

@ -41,8 +41,9 @@ function clearCache(tx, noCache) {
}
const copy = tx.clone();
assert.strictEqual(tx.txid(), copy.txid());
assert.strictEqual(tx.wtxid(), copy.wtxid());
assert.bufferEqual(tx.hash(), copy.hash());
assert.bufferEqual(tx.witnessHash(), copy.witnessHash());
}
function parseTXTest(data) {

View File

@ -9,11 +9,61 @@ const assert = function assert(ok, message) {
Object.setPrototypeOf(assert, _assert);
assert.typeOf = function typeOf(value, expected, message) {
const type = _typeOf(value);
if (type !== expected) {
throw new assert.AssertionError({
mesage: message,
actual: type,
expected: expected,
operator: '===',
stackStartFunction: typeOf
});
}
};
assert.notTypeOf = function notTypeOf(value, expected, message) {
const type = _typeOf(value);
if (type === expected) {
throw new assert.AssertionError({
mesage: message,
actual: type,
expected: expected,
operator: '!==',
stackStartFunction: notTypeOf
});
}
};
assert.instanceOf = function instanceOf(object, parent, message) {
if (!(object instanceof parent)) {
throw new assert.AssertionError({
mesage: message,
actual: _getConstructor(object),
expected: parent.name,
operator: '===',
stackStartFunction: instanceOf
});
}
};
assert.notInstanceOf = function notInstanceOf(object, parent, message) {
if (object instanceof parent) {
throw new assert.AssertionError({
mesage: message,
actual: _getConstructor(object),
expected: parent.name,
operator: '!==',
stackStartFunction: notInstanceOf
});
}
};
assert.isBuffer = function isBuffer(value, message) {
if (!Buffer.isBuffer(value)) {
throw new assert.AssertionError({
mesage: message,
actual: typeOf(value),
actual: _typeOf(value),
expected: 'buffer',
operator: '===',
stackStartFunction: isBuffer
@ -25,6 +75,9 @@ assert.bufferEqual = function bufferEqual(actual, expected, message) {
assert.isBuffer(actual, message);
assert.isBuffer(expected, message);
if (actual === expected)
return;
if (!actual.equals(expected)) {
throw new assert.AssertionError({
mesage: message,
@ -51,7 +104,7 @@ assert.notBufferEqual = function notBufferEqual(actual, expected, message) {
}
};
function typeOf(value) {
function _typeOf(value) {
if (value === null)
return 'null';
@ -73,4 +126,22 @@ function typeOf(value) {
return typeof value;
}
function _getConstructor(value) {
if (value === undefined)
return 'Undefined';
if (value === null)
return 'Null';
const proto = Object.getPrototypeOf(value);
if (proto == null)
return 'Null';
const ctor = proto.constructor;
const name = ctor ? ctor.name : null;
return name || 'Unknown';
}
module.exports = assert;

View File

@ -33,6 +33,30 @@ const base58Tests = [
['00000000000000000000', '1111111111']
];
const unsigned = [
new BN('ffeeffee'),
new BN('001fffeeffeeffee'),
new BN('eeffeeff'),
new BN('001feeffeeffeeff'),
new BN(0),
new BN(1)
];
const signed = [
new BN('ffeeffee'),
new BN('001fffeeffeeffee'),
new BN('eeffeeff'),
new BN('001feeffeeffeeff'),
new BN(0),
new BN(1),
new BN('ffeeffee').ineg(),
new BN('001fffeeffeeffee').ineg(),
new BN('eeffeeff').ineg(),
new BN('001feeffeeffeeff').ineg(),
new BN(0).ineg(),
new BN(1).ineg()
];
describe('Utils', function() {
it('should encode/decode base58', () => {
const buf = Buffer.from('000000deadbeef', 'hex');
@ -56,14 +80,16 @@ describe('Utils', function() {
'hex'
);
assert(consensus.verifyPOW(hash, bits));
assert.strictEqual(consensus.verifyPOW(hash, bits), true);
});
it('should convert satoshi to btc', () => {
let btc = Amount.btc(5460);
assert.strictEqual(btc, '0.0000546');
btc = Amount.btc(54678 * 1000000);
assert.strictEqual(btc, '546.78');
btc = Amount.btc(5460 * 10000000);
assert.strictEqual(btc, '546.0');
});
@ -71,12 +97,16 @@ describe('Utils', function() {
it('should convert btc to satoshi', () => {
let btc = Amount.value('0.0000546');
assert.strictEqual(btc, 5460);
btc = Amount.value('546.78');
assert.strictEqual(btc, 54678 * 1000000);
btc = Amount.value('546');
assert.strictEqual(btc, 5460 * 10000000);
btc = Amount.value('546.0');
assert.strictEqual(btc, 5460 * 10000000);
btc = Amount.value('546.0000');
assert.strictEqual(btc, 5460 * 10000000);
@ -105,6 +135,7 @@ describe('Utils', function() {
});
assert.strictEqual(parseFloat('0.15645647') * 1e8, 15645646.999999998);
assert.strictEqual(util.fromFloat(0.15645647, 8), 15645647);
assert.strictEqual(util.fromFixed('0.15645647', 8), 15645647);
assert.strictEqual(util.toFixed(15645647, 8), '0.15645647');
});
@ -182,30 +213,6 @@ describe('Utils', function() {
assert.deepEqual(b, [0x8e, 0xfe, 0xfe, 0xff, 0x00]);
});
const unsigned = [
new BN('ffeeffee'),
new BN('001fffeeffeeffee'),
new BN('eeffeeff'),
new BN('001feeffeeffeeff'),
new BN(0),
new BN(1)
];
const signed = [
new BN('ffeeffee'),
new BN('001fffeeffeeffee'),
new BN('eeffeeff'),
new BN('001feeffeeffeeff'),
new BN(0),
new BN(1),
new BN('ffeeffee').ineg(),
new BN('001fffeeffeeffee').ineg(),
new BN('eeffeeff').ineg(),
new BN('001feeffeeffeeff').ineg(),
new BN(0).ineg(),
new BN(1).ineg()
];
for (const num of unsigned) {
const bits = num.bitLength();