From 8c9babc09308e409683ae23c3ea48c944e61a0b7 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 13 Mar 2015 13:45:51 -0400 Subject: [PATCH] added tests for command edge cases --- lib/messages/commands/tx.js | 2 +- test/messages/commands/index.js | 138 ++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 test/messages/commands/index.js diff --git a/lib/messages/commands/tx.js b/lib/messages/commands/tx.js index 047a771..6ac0c7d 100644 --- a/lib/messages/commands/tx.js +++ b/lib/messages/commands/tx.js @@ -28,7 +28,7 @@ TransactionMessage.fromObject = function(options) { TransactionMessage.fromBuffer = function(payload) { var transaction; if (Transaction.prototype.fromBuffer) { - transaction = Transaction().fromBuffer(payload); + transaction = new Transaction().fromBuffer(payload); } else { transaction = Transaction.fromBuffer(payload); } diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js new file mode 100644 index 0000000..4b67a83 --- /dev/null +++ b/test/messages/commands/index.js @@ -0,0 +1,138 @@ +'use strict'; + +var should = require('chai').should(); +var P2P = require('../../../'); +var Messages = P2P.Messages; +var sinon = require('sinon'); + +describe('Command Messages', function() { + + var messages = new Messages(); + var constructors = ['GetData', 'Inventory', 'NotFound']; + + describe('Inventory helpers for: ' + constructors.join(', '), function() { + + var fakeHash = 'e2dfb8afe1575bfacae1a0b4afc49af7ddda69285857267bae0e22be15f74a3a'; + + describe('#forTransaction', function() { + constructors.forEach(function(name) { + it(name, function() { + var message = messages[name].forTransaction(fakeHash); + should.exist(message); + message.should.be.instanceof(messages[name]); + }); + }); + }); + + describe('#forBlock', function() { + constructors.forEach(function(name) { + it(name, function() { + var message = messages[name].forBlock(fakeHash); + should.exist(message); + message.should.be.instanceof(messages[name]); + }); + }); + }); + + describe('#forFilteredBlock', function() { + constructors.forEach(function(name) { + it(name, function() { + var message = messages[name].forFilteredBlock(fakeHash); + should.exist(message); + message.should.be.instanceof(messages[name]); + }); + }); + }); + + }); + + + describe('FilterLoad', function() { + + it('should return a null payload', function() { + var message = messages.FilterLoad(); + var payload = message.getPayload(); + payload.length.should.equal(0); + payload.should.be.instanceof(Buffer); + }); + + }); + + describe('Transaction', function() { + + it('should be able to pass a custom Transaction', function(done) { + var Transaction = function(){}; + Transaction.prototype.fromBuffer = function() { + done(); + }; + var messagesCustom = new Messages({Transaction: Transaction}); + var message = messagesCustom.Transaction.fromBuffer(); + should.exist(message); + }); + + it('should work with Transaction.fromBuffer', function(done) { + var Transaction = sinon.stub(); + Transaction.fromBuffer = function() { + done(); + }; + var messagesCustom = new Messages({Transaction: Transaction}); + var message = messagesCustom.Transaction.fromBuffer(); + should.exist(message); + }); + + }); + + describe('Block', function() { + + it('should be able to pass a custom Block', function(done) { + var Block = sinon.stub(); + Block.fromBuffer = function() { + done(); + }; + var messagesCustom = new Messages({Block: Block}); + var message = messagesCustom.Block.fromBuffer(); + should.exist(message); + }); + + }); + + describe('GetBlocks', function() { + + it('should error with invalid stop', function() { + var invalidStop = '000000'; + var starts = ['000000000000000013413cf2536b491bf0988f52e90c476ffeb701c8bfdb1db9']; + (function() { + var message = messages.GetBlocks({starts: starts, stop: invalidStop}); + var buffer = message.toBuffer(); + should.not.exist(buffer); + }).should.throw('Invalid hash length'); + }); + + }); + + describe('GetHeaders', function() { + + it('should error with invalid stop', function() { + var invalidStop = '000000'; + var starts = ['000000000000000013413cf2536b491bf0988f52e90c476ffeb701c8bfdb1db9']; + (function() { + var message = messages.GetHeaders({starts: starts, stop: invalidStop}); + var buffer = message.toBuffer(); + should.not.exist(buffer); + }).should.throw('Invalid hash length'); + }); + + }); + + describe('MerkleBlock', function() { + + it('should return null buffer for payload', function() { + var message = messages.MerkleBlock(); + var payload = message.getPayload(); + payload.length.should.equal(0); + }); + + }); + + +});