Added preconditions to ping message
This commit is contained in:
parent
79674a2d7b
commit
28b05e3cc1
@ -4,6 +4,9 @@ var Message = require('../message');
|
|||||||
var inherits = require('util').inherits;
|
var inherits = require('util').inherits;
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var utils = require('../utils');
|
var utils = require('../utils');
|
||||||
|
var $ = bitcore.util.preconditions;
|
||||||
|
var _ = bitcore.deps._;
|
||||||
|
var BufferUtil = bitcore.util.buffer;
|
||||||
var BufferReader = bitcore.encoding.BufferReader;
|
var BufferReader = bitcore.encoding.BufferReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,6 +21,10 @@ function PingMessage(arg, options) {
|
|||||||
Message.call(this, arg, options);
|
Message.call(this, arg, options);
|
||||||
this.command = 'ping';
|
this.command = 'ping';
|
||||||
this.magicNumber = options.magicNumber;
|
this.magicNumber = options.magicNumber;
|
||||||
|
$.checkArgument(
|
||||||
|
_.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8),
|
||||||
|
'First argument is expected to be an 8 byte buffer'
|
||||||
|
);
|
||||||
this.nonce = arg || utils.getNonce();
|
this.nonce = arg || utils.getNonce();
|
||||||
}
|
}
|
||||||
inherits(PingMessage, Message);
|
inherits(PingMessage, Message);
|
||||||
|
|||||||
@ -25,10 +25,7 @@ function PongMessage(arg, options) {
|
|||||||
_.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8),
|
_.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8),
|
||||||
'First argument is expected to be an 8 byte buffer'
|
'First argument is expected to be an 8 byte buffer'
|
||||||
);
|
);
|
||||||
this.nonce = arg;
|
this.nonce = arg || utils.getNonce();
|
||||||
if (!this.nonce) {
|
|
||||||
this.nonce = utils.getNonce();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
inherits(PongMessage, Message);
|
inherits(PongMessage, Message);
|
||||||
|
|
||||||
|
|||||||
@ -80,6 +80,28 @@ describe('Command Messages', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Ping', function() {
|
||||||
|
|
||||||
|
it('should error if nonce is not a buffer', function() {
|
||||||
|
(function() {
|
||||||
|
var message = messages.Ping('not a buffer');
|
||||||
|
}).should.throw('First argument is expected to be an 8 byte buffer');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should error if nonce buffer has invalid length', function() {
|
||||||
|
(function() {
|
||||||
|
var message = messages.Ping(new Buffer(Array(9)));
|
||||||
|
}).should.throw('First argument is expected to be an 8 byte buffer');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set a nonce if not included', function() {
|
||||||
|
var message = messages.Ping();
|
||||||
|
should.exist(message.nonce);
|
||||||
|
message.nonce.length.should.equal(8);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('FilterLoad', function() {
|
describe('FilterLoad', function() {
|
||||||
|
|
||||||
it('should return a null payload', function() {
|
it('should return a null payload', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user