test: refactor and clean up a few tests.

This commit is contained in:
Christopher Jeffrey 2017-08-01 15:17:17 -07:00
parent 1d7b8ca7c1
commit 860e51e390
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
15 changed files with 172 additions and 39101 deletions

View File

@ -113,8 +113,8 @@ describe('BIP150', function() {
});
it('client should rekey', () => {
let rekeyed = false;
const bytes = client.output.processed;
let rekeyed = false;
client.once('rekey', () => {
rekeyed = true;

View File

@ -88,8 +88,8 @@ describe('BIP151', function() {
});
it('client should rekey', () => {
let rekeyed = false;
const bytes = client.output.processed;
let rekeyed = false;
client.once('rekey', () => {
rekeyed = true;

View File

@ -24,26 +24,26 @@ tests.ca = {
x509.allowUntrusted = true;
x509.trusted.clear();
x509.verifyTime = function() {
return true;
};
describe('BIP70', function() {
function testRequest(data) {
const request = bip70.PaymentRequest.fromRaw(data);
const req = bip70.PaymentRequest.fromRaw(data);
assert.equal(request.pkiType, 'x509+sha256');
assert(request.pkiData);
assert(request.getChain());
assert(request.paymentDetails);
assert(request.paymentDetails.memo.length !== 0);
assert(request.paymentDetails.paymentUrl.length !== 0);
assert.equal(req.pkiType, 'x509+sha256');
assert(req.pkiData);
assert(req.getChain());
assert(req.paymentDetails);
assert(req.paymentDetails.memo.length !== 0);
assert(req.paymentDetails.paymentUrl.length !== 0);
const ser = request.toRaw();
assert.equal(ser.toString('hex'), data.toString('hex'));
assert(request.verify());
const raw = req.toRaw();
assert.equal(raw.toString('hex'), data.toString('hex'));
assert(req.verify());
}
x509.verifyTime = function() {
return true;
};
it('should parse and verify a payment request', () => {
testRequest(tests.valid);
testRequest(tests.invalid);
@ -51,101 +51,101 @@ describe('BIP70', function() {
});
it('should verify cert chain', () => {
const request = bip70.PaymentRequest.fromRaw(tests.valid);
const req1 = bip70.PaymentRequest.fromRaw(tests.valid);
assert.equal(request.version, 1);
assert.equal(request.getChain().length, 4);
assert.equal(request.paymentDetails.paymentUrl,
assert.equal(req1.version, 1);
assert.equal(req1.getChain().length, 4);
assert.equal(req1.paymentDetails.paymentUrl,
'https://test.bitpay.com/i/CMWpuFsjgmQ2ZLiyGfcF1W');
assert.equal(request.paymentDetails.network, 'test');
assert.equal(request.paymentDetails.time, 1408645830);
assert.equal(request.paymentDetails.expires, 1408646730);
assert.equal(request.paymentDetails.outputs.length, 1);
assert(!request.paymentDetails.merchantData);
assert(request.paymentDetails.isExpired());
assert.equal(req1.paymentDetails.network, 'test');
assert.equal(req1.paymentDetails.time, 1408645830);
assert.equal(req1.paymentDetails.expires, 1408646730);
assert.equal(req1.paymentDetails.outputs.length, 1);
assert(!req1.paymentDetails.merchantData);
assert(req1.paymentDetails.isExpired());
assert(request.verifyChain());
assert(req1.verifyChain());
const request2 = bip70.PaymentRequest.fromRaw(tests.invalid);
const req2 = bip70.PaymentRequest.fromRaw(tests.invalid);
assert.equal(request2.version, 1);
assert.equal(request2.getChain().length, 3);
assert.equal(request2.paymentDetails.paymentUrl,
assert.equal(req2.version, 1);
assert.equal(req2.getChain().length, 3);
assert.equal(req2.paymentDetails.paymentUrl,
'https://bitpay.com/i/PAQtNxX7KL8BtJBnfXyTaH');
assert.equal(request2.paymentDetails.network, 'main');
assert.equal(request2.paymentDetails.time, 1442409238);
assert.equal(request2.paymentDetails.expires, 1442410138);
assert.equal(request2.paymentDetails.outputs.length, 1);
assert.equal(request2.paymentDetails.merchantData.length, 76);
assert(request2.paymentDetails.getData('json'));
assert(request2.paymentDetails.isExpired());
assert.equal(req2.paymentDetails.network, 'main');
assert.equal(req2.paymentDetails.time, 1442409238);
assert.equal(req2.paymentDetails.expires, 1442410138);
assert.equal(req2.paymentDetails.outputs.length, 1);
assert.equal(req2.paymentDetails.merchantData.length, 76);
assert(req2.paymentDetails.getData('json'));
assert(req2.paymentDetails.isExpired());
assert(request2.verifyChain());
assert(req2.verifyChain());
request2.paymentDetails.setData({foo:1}, 'json');
assert.equal(request2.paymentDetails.merchantData.length, 9);
assert.deepStrictEqual(request2.paymentDetails.getData('json'), {foo:1});
assert(!request2.verify());
req2.paymentDetails.setData({foo:1}, 'json');
assert.equal(req2.paymentDetails.merchantData.length, 9);
assert.deepStrictEqual(req2.paymentDetails.getData('json'), {foo:1});
assert(!req2.verify());
const request3 = bip70.PaymentRequest.fromRaw(tests.untrusted);
const req3 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert.equal(request3.version, -1);
assert.equal(request3.getChain().length, 2);
assert.equal(request3.paymentDetails.paymentUrl,
assert.equal(req3.version, -1);
assert.equal(req3.getChain().length, 2);
assert.equal(req3.paymentDetails.paymentUrl,
'https://www.coinbase.com/rp/55f9ca703d5d80008c0001f4');
assert.equal(request3.paymentDetails.network, null);
assert.equal(request3.paymentDetails.time, 1442433682);
assert.equal(request3.paymentDetails.expires, 1442434548);
assert.equal(request3.paymentDetails.outputs.length, 1);
assert.equal(request3.paymentDetails.merchantData.length, 32);
assert.equal(request3.paymentDetails.getData('utf8'),
assert.equal(req3.paymentDetails.network, null);
assert.equal(req3.paymentDetails.time, 1442433682);
assert.equal(req3.paymentDetails.expires, 1442434548);
assert.equal(req3.paymentDetails.outputs.length, 1);
assert.equal(req3.paymentDetails.merchantData.length, 32);
assert.equal(req3.paymentDetails.getData('utf8'),
'bb79b6f2310e321bd3b1d929edbeb358');
assert(request3.paymentDetails.isExpired());
assert(req3.paymentDetails.isExpired());
assert(request3.verifyChain());
assert(req3.verifyChain());
});
it('should fail to verify cert signatures when enforcing trust', () => {
x509.allowUntrusted = false;
const request = bip70.PaymentRequest.fromRaw(tests.valid);
assert(!request.verifyChain());
const req1 = bip70.PaymentRequest.fromRaw(tests.valid);
assert(!req1.verifyChain());
const request2 = bip70.PaymentRequest.fromRaw(tests.invalid);
assert(!request2.verifyChain());
const req2 = bip70.PaymentRequest.fromRaw(tests.invalid);
assert(!req2.verifyChain());
const request3 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert(!request3.verifyChain());
const req3 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert(!req3.verifyChain());
});
it('should verify cert signatures once root cert is added', () => {
const request = bip70.PaymentRequest.fromRaw(tests.valid);
x509.setTrust([request.getChain().pop()]);
assert(request.verifyChain());
const req1 = bip70.PaymentRequest.fromRaw(tests.valid);
x509.setTrust([req1.getChain().pop()]);
assert(req1.verifyChain());
const request2 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert(!request2.verifyChain());
const req2 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert(!req2.verifyChain());
});
it('should still fail to verify cert signatures for invalid', () => {
const request = bip70.PaymentRequest.fromRaw(tests.invalid);
assert(!request.verifyChain());
const req = bip70.PaymentRequest.fromRaw(tests.invalid);
assert(!req.verifyChain());
});
it('should get chain and ca for request', () => {
const request = bip70.PaymentRequest.fromRaw(tests.valid);
assert.equal(request.getChain().length, 4);
assert.equal(request.getCA().name,
const req = bip70.PaymentRequest.fromRaw(tests.valid);
assert.equal(req.getChain().length, 4);
assert.equal(req.getCA().name,
'Go Daddy Class 2 Certification Authority');
});
it('should validate untrusted once again', () => {
const request = bip70.PaymentRequest.fromRaw(tests.untrusted);
x509.setTrust([request.getChain().pop()]);
const req1 = bip70.PaymentRequest.fromRaw(tests.untrusted);
x509.setTrust([req1.getChain().pop()]);
const request2 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert(request2.verifyChain());
assert.equal(request2.getCA().name,
const req2 = bip70.PaymentRequest.fromRaw(tests.untrusted);
assert(req2.verifyChain());
assert.equal(req2.getCA().name,
'DigiCert SHA2 Extended Validation Server CA');
});
@ -158,7 +158,7 @@ describe('BIP70', function() {
});
it('should create a payment request, sign, and verify', () => {
const request = new bip70.PaymentRequest({
const req = new bip70.PaymentRequest({
version: 25,
paymentDetails: {
network: 'testnet',
@ -174,41 +174,42 @@ describe('BIP70', function() {
}
});
assert.equal(request.pkiType, null);
assert(!request.pkiData);
assert.equal(request.getChain().length, 0);
assert(request.paymentDetails);
assert(request.paymentDetails.memo.length !== 0);
assert(request.paymentDetails.paymentUrl.length !== 0);
assert.deepStrictEqual(request.paymentDetails.getData('json'), {foo:'bar'});
assert.equal(req.pkiType, null);
assert(!req.pkiData);
assert.equal(req.getChain().length, 0);
assert(req.paymentDetails);
assert(req.paymentDetails.memo.length !== 0);
assert(req.paymentDetails.paymentUrl.length !== 0);
assert.deepStrictEqual(req.paymentDetails.getData('json'), {foo:'bar'});
assert.equal(request.version, 25);
assert.equal(request.paymentDetails.paymentUrl, 'http://bcoin.io/payme');
assert.equal(request.paymentDetails.network, 'testnet');
assert(request.paymentDetails.time <= util.now());
assert.equal(request.paymentDetails.expires,
request.paymentDetails.time + 3600);
assert.equal(request.paymentDetails.outputs.length, 2);
assert(request.paymentDetails.merchantData);
assert(!request.paymentDetails.isExpired());
assert.equal(req.version, 25);
assert.equal(req.paymentDetails.paymentUrl, 'http://bcoin.io/payme');
assert.equal(req.paymentDetails.network, 'testnet');
assert(req.paymentDetails.time <= util.now());
assert.equal(req.paymentDetails.expires,
req.paymentDetails.time + 3600);
assert.equal(req.paymentDetails.outputs.length, 2);
assert(req.paymentDetails.merchantData);
assert(!req.paymentDetails.isExpired());
assert(!request.pkiData);
request.sign(tests.ca.priv, [tests.ca.crt]);
assert(!req.pkiData);
req.setChain([tests.ca.crt]);
req.sign(tests.ca.priv)
assert(request.pkiData);
assert.equal(request.pkiType, 'x509+sha256');
assert.equal(request.getChain().length, 1);
assert(req.pkiData);
assert.equal(req.pkiType, 'x509+sha256');
assert.equal(req.getChain().length, 1);
assert(request.verify());
assert(!request.verifyChain());
assert(req.verify());
assert(!req.verifyChain());
testRequest(request.toRaw());
testRequest(req.toRaw());
x509.setTrust([tests.ca.crt]);
assert(request.verifyChain());
assert.equal(request.getCA().name, 'JJs CA');
assert(req.verifyChain());
assert.equal(req.getCA().name, 'JJs CA');
request.version = 24;
assert(!request.verify());
req.version = 24;
assert(!req.verify());
});
});

View File

@ -22,14 +22,13 @@ const CompactBlock = bip152.CompactBlock;
const TXRequest = bip152.TXRequest;
const TXResponse = bip152.TXResponse;
const block300025 = require('./data/block300025.json');
const cmpct2block = fs.readFileSync(`${__dirname}/data/cmpct2.bin`);
const block300025 = fs.readFileSync(`${__dirname}/data/block300025.raw`);
const undo300025 = fs.readFileSync(`${__dirname}/data/undo300025.raw`);
let cmpct1 = fs.readFileSync(`${__dirname}/data/compactblock.hex`, 'utf8');
let cmpct2 = fs.readFileSync(`${__dirname}/data/cmpct2`, 'utf8');
cmpct1 = cmpct1.trim().split('\n');
cmpct2 = cmpct2.trim();
const cmpct1 = fs.readFileSync(`${__dirname}/data/compact1.raw`);
const cmpct1block = fs.readFileSync(`${__dirname}/data/compact1-block.raw`);
const cmpct2 = fs.readFileSync(`${__dirname}/data/compact2.raw`);
const cmpct2block = fs.readFileSync(`${__dirname}/data/compact2-block.raw`);
function parseUndo(data) {
const br = new BufferReader(data);
@ -102,7 +101,7 @@ describe('Block', function() {
mblock = MerkleBlock.fromRaw(raw2, 'hex');
const block = Block.fromJSON(block300025);
const block = Block.fromRaw(block300025);
this.timeout(10000);
@ -169,7 +168,7 @@ describe('Block', function() {
});
it('should parse JSON', () => {
const block = Block.fromJSON(block300025);
const block = Block.fromJSON(Block.fromRaw(block300025).toJSON());
assert.equal(block.hash('hex'),
'8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000');
assert.equal(block.rhash(),
@ -195,20 +194,13 @@ describe('Block', function() {
});
it('should verify a historical block', () => {
const view = new CoinView();
const height = block300025.height;
const block = Block.fromRaw(block300025);
const undo = parseUndo(undo300025);
const view = applyUndo(block, undo);
const height = 300025;
let sigops = 0;
let reward = 0;
for (let i = 1; i < block300025.txs.length; i++) {
const tx = block300025.txs[i];
for (let j = 0; j < tx.inputs.length; j++) {
const input = tx.inputs[j];
const coin = Coin.fromJSON(input.coin);
view.addCoin(coin);
}
}
assert(block.verify());
assert(block.txs[0].isCoinbase());
assert(block.txs[0].isSane());
@ -289,15 +281,15 @@ describe('Block', function() {
});
it('should handle compact block', () => {
const block = Block.fromRaw(cmpct1[1], 'hex');
const cblock1 = CompactBlock.fromRaw(cmpct1[0], 'hex');
const block = Block.fromRaw(cmpct1block);
const cblock1 = CompactBlock.fromRaw(cmpct1);
const cblock2 = CompactBlock.fromBlock(block, false, cblock1.keyNonce);
const map = new Map();
assert(cblock1.init());
assert.equal(cblock1.toRaw().toString('hex'), cmpct1[0]);
assert.equal(cblock2.toRaw().toString('hex'), cmpct1[0]);
assert.deepEqual(cblock1.toRaw(), cmpct1);
assert.deepEqual(cblock2.toRaw(), cmpct1);
for (let i = 0; i < block.txs.length; i++) {
const tx = block.txs[i];
@ -322,15 +314,15 @@ describe('Block', function() {
});
it('should handle half-full compact block', () => {
const block = Block.fromRaw(cmpct1[1], 'hex');
const cblock1 = CompactBlock.fromRaw(cmpct1[0], 'hex');
const block = Block.fromRaw(cmpct1block);
const cblock1 = CompactBlock.fromRaw(cmpct1);
const cblock2 = CompactBlock.fromBlock(block, false, cblock1.keyNonce);
const map = new Map();
assert(cblock1.init());
assert.equal(cblock1.toRaw().toString('hex'), cmpct1[0]);
assert.equal(cblock2.toRaw().toString('hex'), cmpct1[0]);
assert.deepEqual(cblock1.toRaw(), cmpct1);
assert.deepEqual(cblock2.toRaw(), cmpct1);
for (let i = 0; i < block.txs.length >>> 1; i++) {
const tx = block.txs[i];
@ -370,14 +362,14 @@ describe('Block', function() {
it('should handle compact block', () => {
const block = Block.fromRaw(cmpct2block);
const cblock1 = CompactBlock.fromRaw(cmpct2, 'hex');
const cblock1 = CompactBlock.fromRaw(cmpct2);
const cblock2 = CompactBlock.fromBlock(block, false, cblock1.keyNonce);
const map = new Map();
assert(cblock1.init());
assert.equal(cblock1.toRaw().toString('hex'), cmpct2);
assert.equal(cblock2.toRaw().toString('hex'), cmpct2);
assert.deepEqual(cblock1.toRaw(), cmpct2);
assert.deepEqual(cblock2.toRaw(), cmpct2);
for (let i = 0; i < block.txs.length; i++) {
const tx = block.txs[i];
@ -401,14 +393,14 @@ describe('Block', function() {
it('should handle half-full compact block', () => {
const block = Block.fromRaw(cmpct2block);
const cblock1 = CompactBlock.fromRaw(cmpct2, 'hex');
const cblock1 = CompactBlock.fromRaw(cmpct2);
const cblock2 = CompactBlock.fromBlock(block, false, cblock1.keyNonce);
const map = new Map();
assert(cblock1.init());
assert.equal(cblock1.toRaw().toString('hex'), cmpct2);
assert.equal(cblock2.toRaw().toString('hex'), cmpct2);
assert.deepEqual(cblock1.toRaw(), cmpct2);
assert.deepEqual(cblock2.toRaw(), cmpct2);
for (let i = 0; i < block.txs.length >>> 1; i++) {
const tx = block.txs[i];

File diff suppressed because it is too large Load Diff

BIN
test/data/block300025.raw Normal file

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

BIN
test/data/compact1.raw Normal file

Binary file not shown.

BIN
test/data/compact2.raw Normal file

Binary file not shown.

File diff suppressed because one or more lines are too long

BIN
test/data/undo300025.raw Normal file

Binary file not shown.

View File

@ -134,19 +134,21 @@ MemWallet.prototype.derivePath = function derivePath(path) {
MemWallet.prototype.deriveKey = function deriveKey(branch, index) {
let key = this.master.deriveBIP44(this.account);
key = key.derive(branch).derive(index);
key = new KeyRing({
const ring = new KeyRing({
network: this.network,
privateKey: key.privateKey,
witness: this.witness
});
key.witness = this.witness;
return key;
ring.witness = this.witness;
return ring;
};
MemWallet.prototype.getKey = function getKey(hash) {
const path = this.paths.get(hash);
if (!path)
return null;
return this.derivePath(path);
};
@ -226,17 +228,15 @@ MemWallet.prototype.syncKey = function syncKey(path) {
MemWallet.prototype.addBlock = function addBlock(entry, txs) {
let i, tx;
for (i = 0; i < txs.length; i++) {
tx = txs[i];
for (let i = 0; i < txs.length; i++) {
const tx = txs[i];
this.addTX(tx, entry.height);
}
};
MemWallet.prototype.removeBlock = function removeBlock(entry, txs) {
let i, tx;
for (i = txs.length - 1; i >= 0; i--) {
tx = txs[i];
for (let i = txs.length - 1; i >= 0; i--) {
const tx = txs[i];
this.removeTX(tx, entry.height);
}
};
@ -244,7 +244,6 @@ MemWallet.prototype.removeBlock = function removeBlock(entry, txs) {
MemWallet.prototype.addTX = function addTX(tx, height) {
const hash = tx.hash('hex');
let result = false;
let i, op, path, addr, coin, input, output;
if (height == null)
height = -1;
@ -252,10 +251,10 @@ MemWallet.prototype.addTX = function addTX(tx, height) {
if (this.map.has(hash))
return true;
for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i];
op = input.prevout.toKey();
coin = this.getCoin(op);
for (let i = 0; i < tx.inputs.length; i++) {
const input = tx.inputs[i];
const op = input.prevout.toKey();
const coin = this.getCoin(op);
if (!coin)
continue;
@ -265,20 +264,21 @@ MemWallet.prototype.addTX = function addTX(tx, height) {
this.removeCoin(op);
}
for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i];
addr = output.getHash('hex');
for (let i = 0; i < tx.outputs.length; i++) {
const output = tx.outputs[i];
const addr = output.getHash('hex');
if (!addr)
continue;
path = this.getPath(addr);
const path = this.getPath(addr);
if (!path)
continue;
result = true;
coin = Coin.fromTX(tx, i, height);
const coin = Coin.fromTX(tx, i, height);
this.addCoin(coin);
this.syncKey(path);
@ -295,14 +295,13 @@ MemWallet.prototype.addTX = function addTX(tx, height) {
MemWallet.prototype.removeTX = function removeTX(tx, height) {
const hash = tx.hash('hex');
let result = false;
let i, op, coin, input;
if (!this.map.has(hash))
return false;
for (i = 0; i < tx.outputs.length; i++) {
op = Outpoint(hash, i).toKey();
coin = this.getCoin(op);
for (let i = 0; i < tx.outputs.length; i++) {
const op = Outpoint(hash, i).toKey();
const coin = this.getCoin(op);
if (!coin)
continue;
@ -312,10 +311,10 @@ MemWallet.prototype.removeTX = function removeTX(tx, height) {
this.removeCoin(op);
}
for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i];
op = input.prevout.toKey();
coin = this.getUndo(op);
for (let i = 0; i < tx.inputs.length; i++) {
const input = tx.inputs[i];
const op = input.prevout.toKey();
const coin = this.getUndo(op);
if (!coin)
continue;
@ -335,26 +334,25 @@ MemWallet.prototype.removeTX = function removeTX(tx, height) {
MemWallet.prototype.deriveInputs = function deriveInputs(mtx) {
const keys = [];
let i, input, coin, addr, path, key;
for (i = 0; i < mtx.inputs.length; i++) {
input = mtx.inputs[i];
coin = mtx.view.getOutputFor(input);
for (let i = 0; i < mtx.inputs.length; i++) {
const input = mtx.inputs[i];
const coin = mtx.view.getOutputFor(input);
if (!coin)
continue;
addr = coin.getHash('hex');
const addr = coin.getHash('hex');
if (!addr)
continue;
path = this.getPath(addr);
const path = this.getPath(addr);
if (!path)
continue;
key = this.derivePath(path);
const key = this.derivePath(path);
keys.push(key);
}

View File

@ -18,16 +18,14 @@ function NodeContext(network, size) {
};
NodeContext.prototype.init = function init() {
let i, port, last, node;
for (i = 0; i < this.size; i++) {
port = this.network.port + i;
last = port - 1;
for (let i = 0; i < this.size; i++) {
const port = this.network.port + i;
let last = port - 1;
if (last < this.network.port)
last = port;
node = new FullNode({
const node = new FullNode({
network: this.network,
db: 'memory',
logger: new Logger({
@ -80,10 +78,8 @@ NodeContext.prototype.connect = async function connect() {
};
NodeContext.prototype.disconnect = async function disconnect() {
let i, node;
for (i = this.nodes.length - 1; i >= 0; i--) {
node = this.nodes[i];
for (let i = this.nodes.length - 1; i >= 0; i--) {
const node = this.nodes[i];
await node.disconnect();
await co.timeout(1000);
}
@ -104,12 +100,11 @@ NodeContext.prototype.stopSync = function stopSync() {
NodeContext.prototype.generate = async function generate(index, blocks) {
const node = this.nodes[index];
let i, block;
assert(node);
for (i = 0; i < blocks; i++) {
block = await node.miner.mineBlock();
for (let i = 0; i < blocks; i++) {
const block = await node.miner.mineBlock();
await node.chain.add(block);
}
};