Add option to check stream in object.validate
This commit is contained in:
parent
956d58ce2f
commit
f29f32b3aa
|
@ -367,7 +367,7 @@ var object = exports.object = {
|
|||
var decodedStream = var_int.decode(decodedVersion.rest);
|
||||
var headerLength = 20 + decodedVersion.length + decodedStream.length;
|
||||
|
||||
if (opts._validate) { return; }
|
||||
if (opts._validate) { return {stream: decodedStream.value}; }
|
||||
|
||||
var objectPayload = new Buffer(decodedStream.rest.length);
|
||||
decodedStream.rest.copy(objectPayload);
|
||||
|
@ -387,7 +387,8 @@ var object = exports.object = {
|
|||
* Check whether given `object` message is valid.
|
||||
* @param {Buffer} buf - Message
|
||||
* @param {Object=} opts - Any of [object.decode]{@link
|
||||
* module:bitmessage/structs.object.decode} options
|
||||
* module:bitmessage/structs.object.decode} options and:
|
||||
* @param {number} opts.stream - Expected object's stream
|
||||
* @return {?Error} Return an error with description if object is
|
||||
* invalid.
|
||||
*/
|
||||
|
@ -411,11 +412,18 @@ var object = exports.object = {
|
|||
*/
|
||||
validatePayload: function(buf, opts) {
|
||||
opts = objectAssign({}, opts, {_validate: true});
|
||||
var decoded;
|
||||
try {
|
||||
object.decodePayload(buf, opts);
|
||||
decoded = object.decodePayload(buf, opts);
|
||||
} catch(e) {
|
||||
return e;
|
||||
}
|
||||
if (opts.stream && decoded.stream !== opts.stream) {
|
||||
return new Error(
|
||||
"The stream number " + opts.stream +
|
||||
" is not the one we are interested in"
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
14
test.js
14
test.js
|
@ -261,7 +261,19 @@ describe("Common structures", function() {
|
|||
err = object.validatePayload(obj);
|
||||
expect(err.message).to.match(/insufficient pow/i);
|
||||
|
||||
expect(object.validatePayload(obj, {skipPow: true})).to.not.exist;
|
||||
expect(object.validatePayload(obj, skipPow)).to.not.exist;
|
||||
});
|
||||
|
||||
it("should allow to validate stream", function() {
|
||||
var obj = object.encodePayload({
|
||||
nonce: Buffer(8),
|
||||
ttl: 111,
|
||||
type: object.MSG,
|
||||
version: 1,
|
||||
objectPayload: Buffer(0),
|
||||
});
|
||||
var err = object.validatePayload(obj, {skipPow: true, stream: 3});
|
||||
expect(err.message).to.match(/stream.*is not the one/i);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user